//DRAFT// ELF: symbol table, linker script and assembly sugar

This is work in progress

  • define and export symbols to symbol table.
  • reserve space in either asm or linkerscript, and let others know about it.
  • how to understand readelf -s output.
  • synamic linker
  • global offset table (GOT)
  • Procedure Linkage Table (PLT)
  • RELRO

symbol table

test.c

#include <stdio.h>

extern char global_c;
int test(){
	printf("%d\n",global_c);
}

ext.c

const char global_c = 42;

compile with:

$ gcc -c -static *.c

read symbol tables:

$ readelf -s *.o

(test.o) Symbol table '.symtab' contains 8 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test.c
     2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 .text
     3: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 .rodata
     4: 0000000000000000    54 FUNC    GLOBAL DEFAULT    1 test
     5: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND puts
     6: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND global_c
     7: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND printf

(ext.o) Symbol table '.symtab' contains 3 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS ext.c
     2: 0000000000000000     1 OBJECT  GLOBAL DEFAULT    4 global_c

link them all (main.c simply calls test() as an extern function)

$ gcc -o main main.c test.o ext.o

Symbol table '.dynsym' contains 8 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND _[...]@GLIBC_2.34 (2)
     2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterT[...]
     3: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5 (3)
     4: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND [...]@GLIBC_2.2.5 (3)
     5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     6: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMC[...]
     7: 0000000000000000     0 FUNC    WEAK   DEFAULT  UND [...]@GLIBC_2.2.5 (3)

Symbol table '.symtab' contains 29 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS main.c
     2: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test.c
     3: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS ext.c
     4: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
     5: 0000000000003de0     0 OBJECT  LOCAL  DEFAULT   21 _DYNAMIC
     6: 0000000000002014     0 NOTYPE  LOCAL  DEFAULT   17 __GNU_EH_FRAME_HDR
     ... ... other stuffs from libc....

    19: 0000000000002012     1 OBJECT  GLOBAL DEFAULT   16 global_c

    20: 0000000000004028     0 NOTYPE  GLOBAL DEFAULT   25 _end
    21: 0000000000001050    38 FUNC    GLOBAL DEFAULT   14 _start
    22: 0000000000004020     0 NOTYPE  GLOBAL DEFAULT   25 __bss_start
    23: 0000000000001149    21 FUNC    GLOBAL DEFAULT   14 main
    27: 0000000000001000     0 FUNC    GLOBAL HIDDEN    12 _init
    28: 000000000000115e    54 FUNC    GLOBAL DEFAULT   14 test
edited 20.04.2024
created 16.02.2024
EOF
[+] click to leave a comment [+]
the comment system on this blog works via email. The button
below will generate a mailto: link based on this page's url 
and invoke your email client - please edit the comment there!

[optional] even better, encrypt the email with my public key

- don't modify the subject field
- specify a nickname, otherwise your comment will be shown as   
  anonymous
- your email address will not be disclosed
- you agree that the comment is to be made public.
- to take down a comment, send the request via email.

>> SEND COMMENT <<