compiler construction
play

Compiler Construction Lecture 15: x86-64 and real world procedures - PowerPoint PPT Presentation

Compiler Construction Lecture 15: x86-64 and real world procedures 2020-02-28 Michael Engel Overview The real world: x86-64 Structure of ELF files Storage types Loading and running programs A quick x86-64 assembler intro


  1. Compiler Construction Lecture 15: x86-64 and real world procedures 2020-02-28 Michael Engel

  2. Overview • The real world: x86-64 • Structure of ELF files • Storage types • Loading and running programs • A quick x86-64 assembler intro • Procedures and parameters on x86-64 Compiler Construction 15: x86-64 and real world procedures � 2

  3. Structure of executable files • Early executable files were mostly just the program’s instructions and data dumped to disk • Example: MS-DOS and CP/M . C OM files • Modern executable files are no longer simple images of the program’s memory • Instead, they have structure and meta information • Different executable formats exist • Unix/Linux: ELF ("Executable and Linking Format") [1] • Windows: PE ("Portable Executable") [2] • MacOS X: Mach-O ("Mach-Object") [3] Compiler Construction 15: x86-64 and real world procedures � 3

  4. ELF file structure ELF header Program header ELF header table • ELF identifier • target architecture and size .text • pointers to start of program and section header table in the file Program header table .rodata • tells the system how to create a process image : Section header table .data • contains information about and pointers to the different code and Section header data sections of the ELF file table Compiler Construction 15: x86-64 and real world procedures � 4

  5. ELF header structure Defined (along with the rest of ELF file structure) in /us r / i n cl ude/e lf .h ty pede f s tr u ct 
 { 
 uns ig ned c ha r e_ i den t [EI_NIDENT]; /* Ma gic numbe r and o t he r i n f o */ 
 E lf 64_Ha lf e_ ty pe; /* Ob j e ct fil e ty pe */ 
 E lf 64_Ha lf e_ma c h i ne; /* A rc h it e ct u r e */ 
 E lf 64_Wo r d e_ v e r s i on; /* Ob j e ct fil e v e r s i on */ 
 E lf 64_Add r e_en try ; /* En try po i n t virt ua l add r ess */ 
 E lf 64_O ff e_pho ff ; /* P r o gr am heade r t ab l e fil e o ff se t */ 
 E lf 64_O ff e_sho ff ; /* Se cti on heade r t ab l e fil e o ff se t */ 
 E lf 64_Wo r d e_ fl a g s; /* P r o c esso r -spe cific fl a g s */ 
 E lf 64_Ha lf e_ehs i ze; /* ELF heade r s i ze i n b yt es */ 
 E lf 64_Ha lf e_phen t s i ze; /* P r o gr am heade r t ab l e en try s i ze */ 
 E lf 64_Ha lf e_phnum; /* P r o gr am heade r t ab l e en try c oun t */ 
 E lf 64_Ha lf e_shen t s i ze; /* Se cti on heade r t ab l e en try s i ze */ 
 E lf 64_Ha lf e_shnum; /* Se cti on heade r t ab l e en try c oun t */ 
 E lf 64_Ha lf e_shs tr ndx; /* Se cti on heade r s tri n g t ab l e i ndex */ 
 } E lf 64_Ehd r ; Compiler Construction 15: x86-64 and real world procedures � 5

  6. ELF header structure in real life Let’s take a look inside the l s program: ty pede f s tr u ct 
 { 
 uns ig ned c ha r e_ i den t [EI_NIDENT]; 
 $ xxd - c 8 /b i n/ l s 
 E lf 64_Ha lf e_ ty pe; 
 00000000: 7 f 45 4 c 46 0201 0100 .ELF.... 
 E lf 64_Ha lf e_ma c h i ne; 
 00000008: 0000 0000 0000 0000 ........ E lf 64_Wo r d e_ v e r s i on; 
 00000010: 0300 3e00 0100 0000 ..>..... E lf 64_Add r e_en try ; 
 00000018: 3061 0000 0000 0000 0a...... E lf 64_O ff e_pho ff ; 
 00000020: 4000 0000 0000 0000 @....... E lf 64_O ff e_sho ff ; 
 00000028: 2817 0200 0000 0000 (……. E lf 64_Wo r d e_ fl a g s; 
 00000030: 0000 0000 4000 3800 ....@.8. 
 00000038: 0b00 4000 1d00 1 c 00 ..@..... E lf 64_Ha lf e_ehs i ze; 
 E lf 64_Ha lf e_phen t s i ze; 
 E lf 64_Ha lf e_phnum; 
 Ugh! There has to be a better E lf 64_Ha lf e_shen t s i ze; 
 way to look inside ELF files! E lf 64_Ha lf e_shnum; 
 E lf 64_Ha lf e_shs tr ndx; 
 } E lf 64_Ehd r ; Compiler Construction 15: x86-64 and real world procedures � 6

  7. ELF header structure – readable $ r eade lf -h /b i n/ l s 
 ELF Heade r : 
 ty pede f s tr u ct 
 Ma gic : 7 f 45 4 c 46 02 01 01 00 00 00 00 00 00 00 00 00 
 { 
 Cl ass: ELF64 
 uns ig ned c ha r e_ i den t [EI_NIDENT]; 
 Da t a: 2's c omp l emen t , littl e end i an 
 E lf 64_Ha lf e_ ty pe; 
 V e r s i on: 1 ( c u rr en t ) 
 E lf 64_Ha lf e_ma c h i ne; 
 OS/ABI: UNIX - S y s t em V 
 E lf 64_Wo r d e_ v e r s i on; 
 ABI V e r s i on: 0 
 E lf 64_Add r e_en try ; 
 T y pe: DYN (Sha r ed ob j e ct fil e) 
 E lf 64_O ff e_pho ff ; 
 Ma c h i ne: Ad v an c ed M icr o De vic es X86-64 
 E lf 64_O ff e_sho ff ; 
 V e r s i on: 0x1 
 En try po i n t add r ess: 0x6130 
 E lf 64_Wo r d e_ fl a g s; 
 S t a rt o f p r o gr am heade r s: 64 (b yt es i n t o fil e) 
 E lf 64_Ha lf e_ehs i ze; 
 S t a rt o f se cti on heade r s: 137000 (b yt es i n t o fil e) 
 E lf 64_Ha lf e_phen t s i ze; 
 F l a g s: 0x0 
 E lf 64_Ha lf e_phnum; 
 S i ze o f t h i s heade r : 64 (b yt es) 
 E lf 64_Ha lf e_shen t s i ze; 
 S i ze o f p r o gr am heade r s: 56 (b yt es) 
 E lf 64_Ha lf e_shnum; 
 Numbe r o f p r o gr am heade r s: 11 
 E lf 64_Ha lf e_shs tr ndx; 
 S i ze o f se cti on heade r s: 64 (b yt es) 
 } E lf 64_Ehd r ; Numbe r o f se cti on heade r s: 29 
 Se cti on heade r s tri n g t ab l e i ndex: 28 Compiler Construction 15: x86-64 and real world procedures � 7

  8. ELF sections and symbols Section Function Text (.text) Machine code, entry point Read-only data Pre-initialized constants (.rodata) Read-write data Pre-initialized variables (.data) Base storage segment Uninitialized data (.bss) Symbols (.symtab) Addresses for symbolic names Debug information, C++ constructors, others … Compiler Construction 15: x86-64 and real world procedures � 8

  9. ELF sections and symbols $ r eade lf -W -S /b i n/ l s 
 The r e a r e 29 se cti on heade r s, s t a rti n g a t o ff se t 0x21728: 
 Se cti on Heade r s: 
 [N r ] Name T y pe Add r ess O ff S i ze ES F lg L k In f A l 
 [ 0] NULL 0000000000000000 000000 000000 00 0 0 0 
 [ 1] . i n t e r p PROGBITS 00000000000002a8 0002a8 00001 c 00 A 0 0 1 
 [ 5] .d y ns y m DYNSYM 00000000000003 c 0 0003 c 0 000 c 60 18 A 6 1 8 
 [ 6] .d y ns tr STRTAB 0000000000001020 001020 0005 cc 00 A 0 0 1 
 : 
 [14] . t ex t PROGBITS 00000000000046 f 0 0046 f 0 0125be 00 AX 0 0 16 
 : 
 [16] . r oda t a PROGBITS 0000000000017000 017000 005129 00 A 0 0 32 
 : 
 [25] .da t a PROGBITS 0000000000022380 021380 000268 00 WA 0 0 32 
 [26] .bss NOBITS 0000000000022600 0215e8 0012d8 00 WA 0 0 32 
 : 
 [28] .shs trt ab STRTAB 0000000000000000 02161 c 00010a 00 0 0 1 Compiler Construction 15: x86-64 and real world procedures � 9

  10. 
 From C code to ELF sections foo.c c ons t i n t a = 42; 
 i n t b = 23; 
 i n t c ; 
 • What goes where in our object file? i n t ma i n( v o i d) { 
 $ gcc - c f oo. c 
 c = a + b; 
 $ r eade lf -W -S f oo.o 
 r e t u r n c ; 
 The r e a r e 13 se cti on heade r s, s t a rti n g a t o ff se t 0x2d0: 
 } Se cti on Heade r s: 
 [N r ] Name T y pe Add r ess O ff S i ze ES F lg L k In f A l 
 [ 0] NULL 0000000000000000 000000 000000 00 0 0 0 
 [ 1] . t ex t PROGBITS 0000000000000000 000040 00001 f 00 AX 0 0 1 
 [ 2] . r e l a. t ex t RELA 0000000000000000 000208 000048 18 I 10 1 8 
 [ 3] .da t a PROGBITS 0000000000000000 000060 000004 00 WA 0 0 4 
 [ 4] .bss NOBITS 0000000000000000 000064 000000 00 WA 0 0 1 
 [ 5] . r oda t a PROGBITS 0000000000000000 000064 000004 00 A 0 0 4 Section Function Text (.text) Machine code, entry point Read-only data (.rodata) Pre-initialized constants Read-write data (.data) Pre-initialized variables Base storage segment (.bss) Uninitialized data Symbols (.symtab) Addresses for symbolic names Compiler Construction 15: x86-64 and real world procedures � 10 others Debug information, C++ constructors, …

  11. Code/data storage – from disk to RAM • What happens when the OS loads a program? • ELF gets loaded into virtual address space (47 bit = 128 TB) • Low addresses 
 (0x0 upwards): • Code (.text) • Data (.rodata, .data) • BSS CC-by-SA 3.0 by Huygens 25 • High addresses 
 (0x7fff ffff ffff down): • Stack • In between: • Heap ( ma ll o c ) • Shared libraries Compiler Construction 15: x86-64 and real world procedures � 11

  12. The 64-bit x86 processors • Probably what’s running in your PC / notebook… • Evolutionary design, started with the 8086 in 1978 • Still backwards compatible… with more and more features added • Complex Instruction Set Computer (CISC) • Lots of different instructions • Compilers only use a small subset! • 1985: Intel 80386: first 32-bit x86 CPU • 2003: AMD Opteron: first 64-bit x86 CPU ("AMD64" = x86-64) • Almost all modern x86 processors support the x86-64 mode • But there is lots of 32 bit code still used • We will concentrate on 64 bit only here CC-by-SA 3.0 by Konstantin Lanzet/ 
 Ruben de Rijcke Compiler Construction 15: x86-64 and real world procedures � 12

Recommend


More recommend