Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Sauermann J.Realtime operating systems.Concepts and implementation of microkernels for embedded systems.1997.pdf
Скачиваний:
27
Добавлен:
23.08.2013
Размер:
1.32 Mб
Скачать

2 Concepts

2.1Specification and Execution of Programs

The following sections describe the structure of a program, how a program is prepared for execution, and how the actual execution of the program works.

2.1.1 Compiling and Linking

Let us start with a variant of the well known “Hello World!” program:

#include <stdio.h>

const char * Text = "Hello World\n";

char Data[] = "Hello

Data\n";

int Uninitialized;

// Bad Practice

int main(int argc, char * argv[])

{

printf(Text);

}

This C++ program prints “Hello World”, followed by a line feed on the screen of a computer when it is executed. Before it can be executed, however, it has to be transformed into a format that is executable by the computer. This transformation is done in two steps: compilation and linking.

The first step, compilation, is performed by a program called compiler. The compiler takes the program text shown above from one file, for example Hello.cc, and produces another file, for example Hello.o. The command to compile a file is typically something like

g++ -o Hello.o Hello.cc

The name of the C++ compiler, g++ in our case, may vary from computer to computer. The Hello.o file, also referred to as object file , mainly consists of three sections: TEXT, DATA, and BSS. The so-called include file stdio.h is simply copied into Hello.cc in an early execution phase of the compiler, known as

8

2.1 Specification and Execution of Programs

 

 

preprocessing. The purpose of stdio.h is to tell the compiler that printf is not a spelling mistake, but the name of a function that is defined elsewhere. We can

imagine the generation of Hello.o as shown in Figure 2.1.1

#include <stdio.h>

...

...

.TEXT

.DATA

Hello.cc

Hello.o

FIGURE 2.1 Hello.o Structure

 

Several object files can be collected in one single file, a so-called library. An important library is libc.a (the name may vary with the operating system used): it contains the code for the printf function used in our example, and also for other functions. We can imagine the generation of libc.a as shown in Figure 2.2.

1. Note: The BSS section contains space for symbols that uninitialized when starting the program. For example, the integer variable Uninitialized will be included here in order to speed up the loading of the program. However, this is bad programming practice, and the bad style is not weighed up by the gain in speed. Apart from that, the memory of embedded systems is rather small, and thus loading does not take long anyway. Moreover, we will initialize the complete data memory for security reasons; so eventually, there is no speed advantage at all. Therefore, we assume that the BSS section is always empty, which is why it is not shown in Figure 2.1, and why it will not be considered further on.

2. Concepts

9

 

 

.TEXT

.TEXT

.DATA

.DATA

foo.o

foo.o

.TEXT

.TEXT

.DATA

.DATA

printf.o

printf.o

.TEXT

.TEXT

.DATA

.DATA

bar.o

bar.o

 

libc.a

FIGURE 2.2 libc.a Structure

 

The second step of transforming program text into an executable program is

linking. A typical link command is e.g.

 

ld -o Hello Hello.o

With the linking process, which is illustrated in Figure 2.3, all unresolved references are resolved. In our example, printf is such an unresolved reference, as it is used in main(), but defined in printf.o, which in turn is contained in libc.a. The linking process combines the TEXT and DATA sections of different object files in one single object file, consisting of one TEXT and one DTA section only.

If an object file is linked against a library, only those object files containing definitions for unresolved symbols are used. It should be noted that a linker can produce different file formats. For our purposes, the so-called Motorola S-record format will be used.

10

2.1 Specification and Execution of Programs

 

 

.TEXT

.DATA

Hello.o

.TEXT

 

.DATA

.TEXT

foo.o

.DATA

 

.TEXT

Hello

 

.DATA

 

printf.o

.TEXT

.DATA

bar.o

libc.a

FIGURE 2.3 Hello Structure