CS 449: Executables, Linking and Object Files - Prof. Jonathan R. Misurda, Study notes of Computer Science

An overview of the process of creating executables from c source code, focusing on the roles of the preprocessor, compiler, linker, and different executable formats. It covers static and dynamic linking, archives, libraries, and shared objects.

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-jeu
koofers-user-jeu 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 449 Executables and
Linking
David Kyle
Based on slides by Jonathan Misurda
Compiler
.c cpp cc1 .o ld
C source
Preprocessor Compiler
Preprocessed
source
Linker
Object
files Executable
gcc
Preprocessor
The first step is the preprocessor
It handles any of the # directives
For example, #include
#include takes a file name argument, and
"pastes" that file into the current one
If the filename is surrounded by <and >, it is
found in the include path (e.g., /usr/include)
If it's in quotes, it looks in the current directory
#define
#define is useful for declaring constants:
Any PI in code is replaced with 3.14…
Excludes PI within comments and string literals
#define'd constants are true replacements:
#define PI 3.1415926535
#define BUFSIZE 1024
char buffer[BUFSIZE];
char buffer2[BUFSIZE + 16];
Executables
What do we need to store?
Code
Data
More?
Agree on a common format (much like with
ID3 tags)
Older Executable Formats
a.out (Assembler OUTput)
Oldest UNIX format
No longer commonly used
COFF (Common Object File Format)
Older UNIX Format
No longer commonly used
pf3

Partial preview of the text

Download CS 449: Executables, Linking and Object Files - Prof. Jonathan R. Misurda and more Study notes Computer Science in PDF only on Docsity!

CS 449 – Executables and

Linking

David Kyle

[email protected]

Based on slides by Jonathan Misurda

Compiler

.c cpp^ cc1^ .o ld C source Preprocessor (^) Compiler Preprocessed source Linker Object files (^) Executable gcc

Preprocessor

• The first step is the preprocessor

• It handles any of the # directives

• For example, #include

• #include takes a file name argument, and

"pastes" that file into the current one

• If the filename is surrounded by < and >, it is

found in the include path (e.g., /usr/include)

• If it's in quotes, it looks in the current directory

#define

• #define is useful for declaring constants:

• Any PI in code is replaced with 3.14…

– Excludes PI within comments and string literals

• #define'd constants are true replacements:

#define PI 3. #define BUFSIZE 1024 char buffer[BUFSIZE]; char buffer2[BUFSIZE + 16];

Executables

• What do we need to store?

– Code

– Data

– More?

• Agree on a common format (much like with

ID3 tags)

Older Executable Formats

• a.out (Assembler OUTput)

– Oldest UNIX format

– No longer commonly used

• COFF (Common Object File Format)

– Older UNIX Format

– No longer commonly used

Modern Executable Formats

• PE (Portable Executable)

– Based on COFF

– Used in 32- and 64-bit Windows

• ELF (Executable and Linkable Format)

– Linux/UNIX

• Mach-O file

– Mac

a.out

• exec header

• text segment

• data segment

• text relocations

• data relocations

• symbol table

• string table

Header

• Every a.out formatted binary file begins with an exec

structure:

struct exec { unsigned long a_midmag; //magic number unsigned long a_text; unsigned long a_data; unsigned long a_bss; unsigned long a_syms; unsigned long a_entry; unsigned long a_trsize; unsigned long a_drsize; };

Process’s Address Space

Stack Data (Heap) Text (Code) 0x7fffffff 0 Data (Heap) CS 1550 - 2077 _end Data (Globals) brk $sp brk

Libraries

• Not all code in a program is what you wrote

• Use code that others have written in your own

program

• How to include this code in your address

space?

Linking

• Static Linking

– Copy code into executable at compile time

– Done by linker

• Dynamic Linking

– Copy code into Address Space at load time or later

– Done by link loader