2

I am trying to see everything in big picture regarding compilation and linking.

I have below simple function implemented in add_caller.c :

extern int add(int a, int b);
int
main(int argc, char **argv){

    int a = 45;
    int b = 43;
    int c = add(45,43);
    return 0;
}

Function add is defined in other C file. Now I compile and create object file add_caller.o file using cmd :

gcc -g -c add_caller.c -o add_caller.o

Now i disassemble the add_caller.o file. I want to see how disassembly code put a reference to external add function prior linking.

vmx@vmx:~/Documents/NASMAssembly$ objdump -S --disassemble add_caller.o

add_caller.o:     file format elf32-i386


Disassembly of section .text:

00000000 <main>:
extern int add(int a, int b);

int
main(int argc, char **argv){
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 e4 f0                and    $0xfffffff0,%esp
   6:   83 ec 20                sub    $0x20,%esp

    int a = 45;
   9:   c7 44 24 14 2d 00 00    movl   $0x2d,0x14(%esp)
  10:   00
    int b = 43;
  11:   c7 44 24 18 2b 00 00    movl   $0x2b,0x18(%esp)
  18:   00
    int c = add(45,43);
  19:   c7 44 24 04 2b 00 00    movl   $0x2b,0x4(%esp)
  20:   00
  21:   c7 04 24 2d 00 00 00    movl   $0x2d,(%esp)
  28:   e8 fc ff ff ff          call   29 <main+0x29>
  2d:   89 44 24 1c             mov    %eax,0x1c(%esp)
    return 0;
  31:   b8 00 00 00 00          mov    $0x0,%eax
}
  36:   c9                      leave
  37:   c3                      ret

Here in disassemble code above, I understand it fully except I was expecting some info which relates to add function in line(= address) no 28. I am sure it relates to it somehow, but can someone explain how info in line 28 maps to call to function 'add'. What does 29 <main+0x29> mean in line

`28:   e8 fc ff ff ff          call   29 <main+0x29>`
Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44
  • 1
    Add a `-r` option to objdump to print out the relocation information. `e8 fc ff ff ff` is the `call add` instruction but needs to still be fixed up by the linker since the location of `add` isn't yet known in an unlinked object file. `fc ff ff ff` is the place holder that will be relocated at link time. It actually is pointing at itself right (the offset relative to the beginning of the next instruction to the byte where the relocation starts). Printing the relocation entries will show additional info and which instructions still require relocation by the linker. – Michael Petch Aug 04 '19 at 11:03
  • use `objdump -r` you will find an relocation entry pointing to 29 which says that put address of symbol `add` at position 29, since you use PIC (by default), then the symbol location relative to the position will used, that's why there is `0xfffffffc` representing `-4` offset introduced by `call` instruction. because call use address relative to the end of current instruction, but linker calculate offset relative to the relocation slot. – Zang MingJie Aug 04 '19 at 12:09
  • 3
    If you want to understand assembly, it's usually a better idea to use the `-S` option to the compiler to generate assembly code instead of disassembling an object file. – fuz Aug 04 '19 at 15:15

0 Answers0