0% found this document useful (0 votes)
808 views5 pages

The Compiler, Assembler, Linker, Loader and Process Address Space Tutorial - Hacking The Process of Building Programs Using C Language - Notes and Illustrations

The document summarizes the process of compiling, assembling, linking and loading a C program from source code into an executable program. It describes the four main stages: 1) preprocessing, 2) compilation, 3) assembly, and 4) linking. It then discusses the output at each stage, including object files and executable files. Common file formats and sections within object files like .text, .data, .bss are also defined.

Uploaded by

Haseeb Ahmed
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
808 views5 pages

The Compiler, Assembler, Linker, Loader and Process Address Space Tutorial - Hacking The Process of Building Programs Using C Language - Notes and Illustrations

The document summarizes the process of compiling, assembling, linking and loading a C program from source code into an executable program. It describes the four main stages: 1) preprocessing, 2) compilation, 3) assembly, and 4) linking. It then discusses the output at each stage, including object files and executable files. Common file formats and sections within object files like .text, .data, .bss are also defined.

Uploaded by

Haseeb Ahmed
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

The compiler, assembler, linker, loader and process address space tutorial...

http://www.tenouk.com/ModuleW.html

|< The main() and command line arguments | Main | C Memory Allocation and De-allocation Functions >| Site Index | Download |

COMPILER, ASSEMBLER, LINKER AND LOADER: A BRIEF STORY

1 of 17

10/6/2011 9:38 PM

The compiler, assembler, linker, loader and process address space tutorial...

http://www.tenouk.com/ModuleW.html

My Training Period: xx hours

Note: This Module presents quite a detail story of a process (running program). However, it is an exc buffer overflow Tutorial. It tries to investigate how the C/C++ source codes preprocessed, com running program. It is based on the GCC (GNU Compiler Collection). When you use th Environment) compilers such as Microsoft Visual C++, Borland C++ Builder etc. the pr transparent. The commands and examples of the gcc, gdb, g++, gas and friends are di gas 1 and Linux gnu gcc, g++, gdb and gas 2. Have a nice day! The C compiler ability: Able to understand and appreciate the processes involved in preprocessing, compiling programs. W.1 COMPILERS, ASSEMBLERS and LINKERS Normally the Cs program building process involves four stages and utilizes differen compiler, assembler, and linker. At the end there should be a single executable file. Below are the stages that happ system/compiler and graphically illustrated in Figure w.1.

1. Preprocessing is the first pass of any C compilation. It processes include-files, con instructions and macros. 2. Compilation is the second pass. It takes the output of the preprocessor, and the s assembler source code. 3. Assembly is the third stage of compilation. It takes the assembly source code and listing with offsets. The assembler output is stored in an object file. 4. Linking is the final stage of compilation. It takes one or more object files or librarie them to produce a single (usually executable) file. In doing so, it resolves referen assigns final addresses to procedures/functions and variables, and revises code an addresses (a process called relocation).

Bear in mind that if you use the IDE type compilers, these processes quite transpare Now we are going to examine more details about the process that happen before and af input file, the file name suffix (file extension) determines what kind of compilation is done in Table w.1. In UNIX/Linux, the executable or binary file doesnt have extension whereas in Windo have .exe, .com and .dll. File extension file_name.c file_name.i file_name.ii file_name.h file_name.cc file_name.cp file_name.cxx file_name.cpp file_name.c++ file_name.C file_name.s file_name.S file_name.o Description C source code which must be preprocessed. C source code which should not be preprocessed. C++ source code which should not be preprocessed. C header file (not to be compiled or linked).

C++ source code which must be preprocessed. For file_name.c literally character x and file_name.C, is capital c.

Assembler code. Assembler code which must be preprocessed. Object file by default, the object file name for a source file is ma extension .c, .i, .s etc with .o Table w.1

2 of 17

10/6/2011 9:38 PM

The compiler, assembler, linker, loader and process address space tutorial...

http://www.tenouk.com/ModuleW.html

The following Figure shows the steps involved in the process of building the C progr the loading of the executable image into the memory for program running.

Figure w.1: Compile, link and execute stages for running program (a W.2 OBJECT FILES and EXECUTABLE

After the source code has been assembled, it will produce an Object files (e.g. .o executable files. An object and executable come in several formats such as ELF (Executable and Linking Object-File Format). For example, ELF is used on Linux systems, while COFF is used Other object file formats are listed in the following Table. Object File Format a.out Description

COFF

The a.out format is the original file format for Unix. It consists of three sections for program code, initialized data, and uninitialized data, respectively. This for have any reserved place for debugging information. The only debugging form encoded as a set of normal symbols with distinctive attributes. The COFF (Common Object File Format) format was introduced with System V files may have multiple sections, each prefixed by a header. The number of se

3 of 17

10/6/2011 9:38 PM

The compiler, assembler, linker, loader and process address space tutorial...

http://www.tenouk.com/ModuleW.html

specification includes support for debugging but the debugging information wa extension for this format. ECOFF A variant of COFF. ECOFF is an Extended COFF originally introduced for Mip The IBM RS/6000 running AIX uses an object file format called XCOFF (e XCOFF symbols, and line numbers are used, but debugging symbols are dbx-style sta the .debug section (rather than the string table). The default name for an XCO Windows 9x and NT use the PE (Portable Executable) format for their executa PE additional headers. The extension normally .exe. The ELF (Executable and Linking Format) format came with System V Release ELF COFF in being organized into a number of sections, but it removes many of CO most modern Unix systems, including GNU/Linux, Solaris and Irix. Also used o SOM (System Object Module) and ESOM (Extended SOM) is HP's object file a SOM/ESOM confused with IBM's SOM, which is a cross-language Application Binary Interfa Table w.2 When we examine the content of these object files there are areas called sections. Sections can hold executable code, data, dynamic linking information, debugging data, symbol tables, relocation information, comments, string tables, and notes. Some sections are loaded into the process image and some provide information needed in the building of a process image while still others are used only in linking object files. There are several sections that are common to all executable formats (may be named differently, depending on the compiler/linker) as listed below: Section .text Description

This section contains the executable instruction codes and is shared among every process runn section usually has READ and EXECUTE permissions only. This section is the one most

.bss

.data .rdata .reloc Symbol table

BSS stands for Block Started by Symbol. It holds un-initialized global and static variables. S variables that don't have any values yet, it doesn't actually need to store the image of these var will require at runtime is recorded in the object file, but the BSS (unlike the data section) doesn't in the object file. Contains the initialized global and static variables and their values. It is usually the largest usually has READ/WRITE permissions. Also known as .rodata (read-only data) section. This contains constants and string literals.

Relocation records

Stores the information required for relocating the image while loading. A symbol is basically a name and an address. Symbol table holds information needed to locate symbolic definitions and references. A symbol table index is a subscript into this array. Index 0 bo entry in the table and serves as the undefined symbol index. The symbol table contains an array Relocation is the process of connecting symbolic references with symbolic definitions. For examp a function, the associated call instruction must transfer control to the proper destination address Re-locatable files must have relocation entries which are necessary because they contain inform to modify their section contents, thus allowing executable and shared object files to hold the righ process's program image. Simply said relocation records are information used by the linker to ad Table w.3: Segments in executable file

The following is an example of the object file content dumping using readelf program. Other utility can be used is objdump. These utilities presented in Linux gcc, g++, gdb and gas 1 and Linux gcc, g++, gdb and gas 2. For Windows dumpbin utility (coming with Visual C++ compiler) or more powerful one is a free PEBrowse program that can be used for the same purpose. /* testprog1.c */ #include <stdio.h> static void display(int i, int *ptr);

4 of 17

10/6/2011 9:38 PM

The compiler, assembler, linker, loader and process address space tutorial...

http://www.tenouk.com/ModuleW.html

int main(void) { int x = 5; int *xptr = &x; printf("In main() program:\n"); printf("x value is %d and is stored at address %p.\n", x, &x); printf("xptr pointer points to address %p which holds a value of %d.\n", xptr, *xptr); display(x, xptr); return 0; } void display(int y, int *yptr) { char var[7] = "ABCDEF"; printf("In display() function:\n"); printf("y value is %d and is stored at address %p.\n", y, &y); printf("yptr pointer points to address %p which holds a value of %d.\n", yptr, *yptr); } [bodo@bakawali test]$ gcc -c testprog1.c [bodo@bakawali test]$ readelf -a testprog1.o ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Class: ELF32 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: REL (Relocatable file) Machine: Intel 80386 Version: 0x1 Entry point address: 0x0 Start of program headers: 0 (bytes into file) Start of section headers: 672 (bytes into file) Flags: 0x0 Size of this header: 52 (bytes) Size of program headers: 0 (bytes) Number of program headers: 0 Size of section headers: 40 (bytes) Number of section headers: 11 Section header string table index: 8 Section Headers: [Nr] Name Type Addr Off Inf Al [ 0] NULL 00000000 000000 0 [ 1] .text PROGBITS 00000000 000034 4 [ 2] .rel.text REL 00000000 00052c 4 [ 3] .data PROGBIT 00000000 000114 0 0 4 [ 4] .bss NOBIT 00000000 000114 4 [ 5] .rodata PROGBITS 00000000 A 0 0 4 [ 6] .note.GNU-stack PROGBITS 00000000

Size 000000 00 0000de 00 000068 08 000000 00 000000 00

ES Flg Lk 0 AX 0 9 0 0 1 WA WA 0 0

000114 00010a 00 00021e 000000 00

5 of 17

10/6/2011 9:38 PM

You might also like