compiler cs 449 executables and
play

Compiler CS 449 Executables and gcc Object Linking Preprocessed - PDF document

Compiler CS 449 Executables and gcc Object Linking Preprocessed C source files source Executable .c cpp cc1 .o ld Preprocessor Compiler Linker Jonathan Misurda jmisurda@cs.pitt.edu Executables Older Executable Formats What


  1. Compiler CS 449 – Executables and gcc Object Linking Preprocessed C source files source Executable .c cpp cc1 .o ld Preprocessor Compiler Linker Jonathan Misurda jmisurda@cs.pitt.edu Executables Older Executable Formats • What do we need to store? • a.out (Assembler OUTput) – Code – Oldest UNIX format – Data – No longer commonly used – More? • COFF (Common Object File Format) • Agree on a common format (much like with – Older UNIX Format ID3 tags) – No longer commonly used Modern Executable Formats a.out • PE (Portable Executable) • exec header – Based on COFF • text segment – Used in 32 ‐ and 64 ‐ bit Windows • data segment • text relocations • ELF (Executable and Linkable Format) • data relocations – Linux/UNIX • symbol table • Mach ‐ O file • string table – Mac 1

  2. Header Process’s Address Space • Every a.out formatted binary file begins with an exec 0x7fffffff structure: Stack $sp struct exec { brk unsigned long a_midmag; //magic number brk unsigned long a_text; Data (Heap) unsigned long a_data; Data (Heap) unsigned long a_bss; unsigned long a_syms; _end unsigned long a_entry; Data (Globals) unsigned long a_trsize; unsigned long a_drsize; Text (Code) }; 0 CS 1550 ‐ 2077 Libraries Linking • Not all code in a program is what you wrote • Static Linking – Copy code into executable at compile time – Done by linker • Use code that others have written in your own program • Dynamic Linking – Copy code into Address Space at load time or later • How to include this code in your address – Done by link loader space? Static Linking Dynamic Linking #include <stdio.h> #include <math.h> Shared Objects int main() { printf(“The sqrt of 9 is %f\n”, sqrt(9)); Stack /usr/lib/libc.so /usr/lib/libm.so return 0; } Archives Executable Data (Heap) /usr/lib/libc.a /usr/lib/libm.a Data (Heap) ld Data (Globals) Object files Executable Link Loader Text (Code) .o ld Linker 2

  3. Dynamic Loading Function Pointers • How do we call a function when we can’t be DLL 2 sure what address it’s loaded at? DLL 1 Stack Stack • Need a level of indirection Data (Heap) Data (Heap) Data (Heap) Data (Heap) LoadLibrary(“DLL1.dll”); • Use a function pointer LoadLibrary(“DLL2.dll”); Data (Globals) Data (Globals) Text (Code) Text (Code) Function Pointers in C #include <stdio.h> int f(int x) { return x; } int main() { int (*g)(int x); g = f; printf(“%d\n”,g(3)); return 0; } 3

Recommend


More recommend