Bug 12807 - OBJCOPY from ELF to COFF changes relocation type from 32 to 16 bits
Summary: OBJCOPY from ELF to COFF changes relocation type from 32 to 16 bits
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-25 20:28 UTC by Emil
Modified: 2011-05-27 01:23 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Patcher for COFF files produced by OBJCOPY (2.96 KB, text/x-csrc)
2011-05-27 01:21 UTC, Emil
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Emil 2011-05-25 20:28:18 UTC
Create a small C program and compile it with TCC compiler:

char *str = "STR";

void
empty( void )
{
}

$tcc -c -o t.o t.c
$objdump -x t.o
[...]
RELOCATION RECORDS FOR [.data]:
OFFSET   TYPE              VALUE
00000000 R_386_32          L.0

// disassembled:
.data:08000020             _data           segment para public 'DATA' use32
.data:08000020                             assume cs:_data
.data:08000020                             ;org 8000020h
.data:08000020                             public str
.data:08000020 24 00 00 08 str             dd offset L_0           ; "STR"
.data:08000024 53 54 52 00 L_0             db 'STR',0              ; 

$objcopy -O pe-i386 t.o tw.obj
$objdump -x tw.obj
[...]
RELOCATION RECORDS FOR [.data]:
OFFSET   TYPE              VALUE
00000000 1                 L.0+0xfffffffc

// disassembled:
.data:00000020             _data           segment para public 'DATA' use32
.data:00000020                             assume cs:_data
.data:00000020                             ;org 20h
.data:00000020                             public str
.data:00000020 24 00       str             dw offset L_0           ; "STR"
.data:00000022 00 00                       align 4
.data:00000024 53 54 52 00 L_0             db 'STR',0              ; 
===========================================================================

objcopy decides to change the R_386_32 type into type "1" - 16 bits 
(instead of "6" - 32 bits). The PE format doesn't handle 16 bit relocations 
so when you link the copied object into a program and run it you'll get a segfault because the "str" pointer is null.
No error or warning is displayed by objcopy.
Comment 1 Emil 2011-05-27 01:21:02 UTC
Created attachment 5755 [details]
Patcher for COFF files produced by OBJCOPY

rename the executable to patchcoff for patching, otherwise it will just parse and dump the object file
Comment 2 Emil 2011-05-27 01:23:12 UTC
(In reply to comment #0)
> objcopy decides to change the R_386_32 type into type "1" - 16 bits 
> (instead of "6" - 32 bits). The PE format doesn't handle 16 bit relocations 
> so when you link the copied object into a program and run it you'll get a
> segfault because the "str" pointer is null.
> No error or warning is displayed by objcopy.

Actually there are more bugs:
R_386_32   converts to type "1"
R_386_PC32 converts to type "2"
"Microsoft Portable Executable and Common Object File Format Specification"
Revision 8.2 states that types "1" and "2" are not supported.
The correct transformation is
R_386_32   to IMAGE_REL_I386_DIR32  (type 6)
R_386_PC32 to IMAGE_REL_I386_REL32  (type 20)

Beside the above changes symbols of the last relocation type (which covers
branch and call instructions) should have symbol_type = 0x20 (now it is 0)
Another bug is that the actual relative displacement is 4 bytes off.

I have attached a little C program which patches COFF pe-i386 files produced
by OBJCOPY so they are now linking correctly and the linker generates working
EXE files.