views:

464

answers:

4

Hi,

I wanted to know how linker resolves the printf symbol in the following assembly code.

#include<stdio.h>
void main()
{
     printf("Hello ");
}




    .file "test.c"
    .def ___main; .scl 2; .type 32; .endef
    .section .rdata,"dr"
LC0:
    .ascii "Hello \0"
    .text
.globl _main
    .def _main; .scl 2; .type 32; .endef
_main:
    pushl %ebp
    movl %esp, %ebp
    subl $8, %esp
    andl $-16, %esp
    movl $0, %eax
    addl $15, %eax
    addl $15, %eax
    shrl $4, %eax
    sall $4, %eax
    movl %eax, -4(%ebp)
    movl -4(%ebp), %eax
    call __alloca
    call ___main
    movl $LC0, (%esp)
    **call _printf**
    leave
    ret
    .def **_printf**; .scl 3; .type 32; .endef

Bit of Low Level Explanation will be highly appreciated.

Thanks in advance.

+15  A: 

Assuming ELF file format, the assembler will generate an undefined symbol reference in the object file. This'll look like this:

Symbol table '.symtab' contains 11 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 FILE    LOCAL  DEFAULT  ABS test.c
     2: 00000000     0 SECTION LOCAL  DEFAULT    1
     3: 00000000     0 SECTION LOCAL  DEFAULT    3
     4: 00000000     0 SECTION LOCAL  DEFAULT    4
     5: 00000000     0 SECTION LOCAL  DEFAULT    5
     6: 00000000     0 SECTION LOCAL  DEFAULT    6
     7: 00000000     0 SECTION LOCAL  DEFAULT    7
     8: 00000000    52 FUNC    GLOBAL DEFAULT    1 main
     9: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND printf
    10: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND exit

It'll also create a relocation entry to point to the part of the code image that needs to be updated by the linker with the correct address. It'll look like this:

tool2 0>readelf -r test.o

Relocation section '.rel.text' at offset 0x358 contains 3 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0000001f  00000501 R_386_32          00000000   .rodata
00000024  00000902 R_386_PC32        00000000   printf
00000030  00000a02 R_386_PC32        00000000   exit

The linker's job is then to walk through the relocation table fixing up the code image with the final symbol addresses.

There's an excellent book, but I can't find the details right now (and it's out of print). However, this looks like it may be useful: http://www.linuxjournal.com/article/6463

Dave.

DaveS
Impressive Answer Dave. Very clearly delineated. Thank you so much. It would be great full to you if you could remind yourself and let me that book as well :)
mahesh
+1  A: 

For a great book on the linking process, see Linkers & Loaders by John Levine. You can get the manuscript chapters in HTML format here.

anon
+1  A: 

A paper that might help you is How To Write Shared Libraries by Ulrich Drepper. Ulritch the Linux glibc maintainer and he is an authority on ELF.

Even though this paper is about how to write shared libraries and how to export or not to export symbols it explains how those symbols are resolved dynamically inside an exefile with ELF format.

I guess it might answer your question.

fco.javier.sanz
Thank you For the Link :). Looks Good.
mahesh
A: 

Another good resource on linkers is this series of articles: http://www.google.fr/search?q=site%3Awww.airs.com%2Fblog%2Farchives+%22linkers+part%22.

Bastien Léonard