tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] strange behavior when linking with gcc compiled objec


From: Jin Qian
Subject: Re: [Tinycc-devel] strange behavior when linking with gcc compiled object file
Date: Sat, 16 Jul 2016 17:36:37 +0000 (UTC)

Even without using objcopy to convert the format, I saw the generated object file has the difference in offset "-4" vs "0".
"objdump -r" shows they the displacement starts from the same offset in the instruction. 

I had to use objcopy to convert because tcc doesn't object file in COFF format (or is there a workaround)? 

Thanks
Jin

=============here are the difference in the generated object file, without objcopy======================
C:\temp>gcc -c te1.c

C:\temp>objdump -d te1.o

te1.o:     file format pe-x86-64


Disassembly of section .text:

0000000000000000 <dummy>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 83 ec 20             sub    $0x20,%rsp
   8:   48 8d 0d 00 00 00 00    lea    0x0(%rip),%rcx        # f <dummy+0xf>
   f:   e8 00 00 00 00          callq  14 <dummy+0x14>
  14:   90                      nop
  15:   48 83 c4 20             add    $0x20,%rsp
  19:   5d                      pop    %rbp
  1a:   c3                      retq
  1b:   90                      nop
  1c:   90                      nop
  1d:   90                      nop
  1e:   90                      nop
  1f:   90                      nop

C:\temp>objdump -r te1.o

te1.o:     file format pe-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
000000000000000b R_X86_64_PC32     .rdata
0000000000000010 R_X86_64_PC32     printf1


RELOCATION RECORDS FOR [.pdata]:
OFFSET           TYPE              VALUE
0000000000000000 rva32             .text
0000000000000004 rva32             .text
0000000000000008 rva32             .xdata



C:\temp>tcc -c te1.c

C:\temp>objdump -d te1.o

te1.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <dummy>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 81 ec 20 00 00 00    sub    $0x20,%rsp
   b:   48 8d 0d fc ff ff ff    lea    -0x4(%rip),%rcx        # e <dummy+0xe>
  12:   e8 fc ff ff ff          callq  13 <dummy+0x13>
  17:   c9                      leaveq
  18:   c3                      retq
  19:   00 00                   add    %al,(%rax)
  1b:   00 01                   add    %al,(%rcx)
  1d:   04 02                   add    $0x2,%al
  1f:   05 04 03 01 50          add    $0x50010304,%eax

C:\temp>objdump -r te1.o

te1.o:     file format elf64-x86-64

RELOCATION RECORDS FOR [.text]:
OFFSET           TYPE              VALUE
000000000000000e R_X86_64_PC32     L.0
0000000000000013 R_X86_64_PC32     printf1


RELOCATION RECORDS FOR [.pdata]:
OFFSET           TYPE              VALUE
0000000000000000 R_X86_64_RELATIVE
0000000000000004 R_X86_64_RELATIVE
0000000000000008 R_X86_64_RELATIVE



On Saturday, July 16, 2016 12:26 PM, grischka <address@hidden> wrote:


Jin Qian wrote:

> I even did the experiment by modifying the few bytes of machine code in gcc compiled object file just to make it look like tcc compiled, then linking with tcc produced a good exe.  Any ideas why tcc does this, why is the "-4" offset in lea instruction?


You are using objcopy to convert PE-COFF objects from gcc/mingw
to ELF for tcc.

COFF and ELF have different ideas where the relative displacement
starts:

      48 8d 0d XX XX XX XX ...
                          ^
                          COFF/DISP32 displacement start

      48 8d 0d XX XX XX XX ...
              ^
              ELF/R_XXX_PC32 displacement start

      48 8d 0d XX XX XX XX ...
                          ^
                          x86 machine displacement start

Which means that for ELF on x86, the actual distance is shorter by 4.

Objcopy ignores this.  Looks like a bug in objcopy.

-- gr


_______________________________________________
Tinycc-devel mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/tinycc-devel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]