[llvm-dev] [X86_64] [Q] Object code generated to access global variables

S. Bharadwaj Yadavalli via llvm-dev llvm-dev at lists.llvm.org
Wed Jun 27 14:27:08 PDT 2018


Hi,

Consider the following C source:

$ cat globaltest.c
int MyGlobal;
void set_global() {
   MyGlobal = 5;
}

I generated object files using clang and gcc as follows:
$ clang -o globaltest_clang.o -c globaltest.c
$ gcc -o globaltest_gcc.o -c globaltest.c

I notice that gcc generated encoding of the instruction that moves 5
to global is pc-relative location but clang generated encoding of the
same instruction is 0-reg relative (See instruction at location 4 in
the objdumps below).

I understand upon linking this object (given the relocation record)
with other objects to get a final executable (or DSO) results in a
fully functional binary. But, as far as the correctness of generated
object code, is this a bug?

Additional information:

objdump of the generated object files:

$ objdump -d globaltest_clang.o

globaltest_clang.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <set_global>:
   0:    55                       push   %rbp
   1:    48 89 e5                 mov    %rsp,%rbp
   4:    c7 04 25 00 00 00 00     movl   $0x5,0x0
   b:    05 00 00 00
   f:    5d                       pop    %rbp
  10:    c3                       retq

$ objdump -d globaltest_gcc.o

globaltest_gcc.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <set_global>:
   0:    55                       push   %rbp
   1:    48 89 e5                 mov    %rsp,%rbp
   4:    c7 05 00 00 00 00 05     movl   $0x5,0x0(%rip)        # e
<set_global+0xe>
   b:    00 00 00
   e:    90                       nop
   f:    5d                       pop    %rbp
  10:    c3                       retq

Thanks,

Bharadwaj


More information about the llvm-dev mailing list