3 ms·
Is it a poor debugger that the author uses? The code looked wrong to me until I understood that instead of writing the labels inside of the line the line conta
by acqq 13y ago
Is it a poor debugger that the author uses? The code looked wrong to me until I understood that instead of writing the labels inside of the line the line contains the value of 0 and the unconventional decoding and then the label follows in the next line e.g.
2: mov 0x0(%rip),%rax
5: R_X86_64_GOTPCREL NSS_ERROR_NOT_FOUND+0xfffffffffffffffc
was probably supposed to mean something like
mov addr NSS_ERROR_NOT_FOUND, %rax
That is, the next line is actually content of the part of the instruction of the previous, and the later was incomplete and with 0 instead of the value. If the value in the second line would appear after the instruction in the first line is over, during the execution the second line would be executed as the instruction. That it's not the case is visible from the offset 5 vs. 2 of the start of the instruction. I'm more used to Intal than AT&T notation, but I don't believe that it's the effect of the notation. Anybody knows more, what produces such a strange code?
This weirdness
2: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax
5: R_X86_64_GOTPCREL NSS_ERROR_NOT_FOUND+0xfffffffffffffffc
9: 8b 38 mov (%rax),%edi
b: e8 00 00 00 00 callq 10 <NSSTrustDomain_GenerateSymmetricKeyFromPassword+0x10>
c: R_X86_64_PLT32 nss_SetError+0xfffffffffffffffc
probably just means
mov addr NSS_ERROR_NOT_FOUND, rax
mov (%rax),%edi
call nss_SetError
Then what and why produces the longer and stranger form?
- cnvogel 13y agoThat's objdump's default format when outputting relocations together with the disassembly. As you have already found out, the code is compiled with dummy values for the actual addresses being accessed or jumped to, and the reloacation table instructs the linker to overwrite these dummy values with the actual addresses, once they are known. R_X86_64_GOTPCREL is a constant defined in /usr/include/elf.h (I think it's from libbdf, the library dealing with the different file formats binutils understand). #define R_X86_64_GOTPC32 26 /* 32 bit signed pc relative offset to GOT */ Here's another example, with the invocation of objdump: $ cat hackernews.c int doit(int a) { return blah(a); } $ cc -Os -c hackernews.c $ objdump -r -S hackernews.o hackernews.o: file format elf64-x86-64 Disassembly of section .text: 0000000000000000 <doit>: 0: 31 c0 xor %eax,%eax 2: e9 00 00 00 00 jmpq 7 <doit+0x7> 3: R_X86_64_PC32 blah-0x4 on ARM (raspberry pi, the relocation looks a little bit different) $ objdump -r -S hackernews.o hackernews.o: file format elf32-littlearm Disassembly of section .text: 00000000 <doit>: 0: eafffffe b 0 <blah> 0: R_ARM_JUMP24 blah #define R_ARM_JUMP24 29 /* PC relative 24 bit (B, BL<cond>). */ To only see the relocation table, use "objdump --relocs": hackernews.o: file format elf32-littlearm RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE 00000000 R_ARM_JUMP24 blah
- gsg 13y agoBy the way, I believe the zeros are addends and not actually dummy values. This is a bit strange because ELF has existing provisions for relocations with addends, but there it is.
- acqq 13y agoThanks! So it's objdump producing something nobody who doesn't debug linker's rellocation code needs. Is there an option "make sane output"? I guess gcc's -S option would make something prettier?
- gsg 13y agoThose are just ordinary relocations in PI code. You'll see this kind of information if you run, say, objdump -rd foo.o on a x86-64 object file that has been compiled as PIC. Note that your code fragment is not PIC, so it would not substitute in this situation.
- acqq 13y agoThanks. That explains me the part of "why." Still I believe even PIC is not written as the input for the assembler in that form? Then I can imagine a nicer objdump which would produce something more readable (or even immediately usable by the assembler).
- gsg 13y agoMore or less, yeah. You would still need to use RIP-relative addressing since that is how PIC functions, but you could have nice symbolic addresses. objdump is a very simple tool. If you want more featureful disassembly tools you should probably look at something like IDA.
- cnvogel 13y agoThe relocation mechanism is used for either pic or non-pic code. If parts of your code reside in different compilation units (.c files), there need to be a way to tell the linker how to jump from one to the other, even if the final program will be run at a well-known static position in the address space. Of course, when compiling -fpic versus -fno-pic, different types of relocation information will be produced, and the compiler might produce different instructions for branching. And unresolved symbols will remain in your library to be fixed up by the dynamic linker. But relocation entries will be produced for either case.
- gsg 13y agoI was referring to the PIC-specific 0x0(%rip) construct as well as the relocation. Sorry if that was unclear.