On Tue, Apr 20, 2021 at 4:27 PM Peng Yu via Gcc-help <gcc-help@xxxxxxxxxxx> wrote: > > How does the linker know that it should look for the string literal in > .rodata just using the object file? You should dump the objects file's relocations, then you'll understand. > $ objdump --disassemble=main a.o > ... > 0000000000000000 <main>: > 0: 55 push %rbp > 1: 48 89 e5 mov %rsp,%rbp > 4: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # b <main+0xb> > b: e8 00 00 00 00 callq 10 <main+0x10> > 10: b8 00 00 00 00 mov $0x0,%eax > 15: 5d pop %rbp > 16: c3 retq > > Where is the number "10" in the callq line from? "00 00 00 00" is just > address 0. So the disassembler knows "00 00 00 00" is not a legal > address, so it just put the address of the next instruction which is > "10"? The displacement in the call instruction is 0, which is relative to the address after the current instruction, which is 10. So the disassembler displays it as a call to address 10. > $ objdump --disassemble=main a.out > ... > 0000000000001135 <main>: > 1135: 55 push %rbp > 1136: 48 89 e5 mov %rsp,%rbp > 1139: 48 8d 3d c4 0e 00 00 lea 0xec4(%rip),%rdi # 2004 > <_IO_stdin_used+0x4> > 1140: e8 eb fe ff ff callq 1030 <puts@plt> > 1145: b8 00 00 00 00 mov $0x0,%eax > 114a: 5d pop %rbp > 114b: c3 retq > ... > > How does objdump figure out 0xec4(%rip) is the address _IO_stdin_used+0x4? I guess it just picks the closest symbol. It's basically meaningless in this case.