For those who want to have some fun(!) in these boring days, I would recommend you start reviewing your first days of embedded programming..Saying hello to our cruel world as every new-born programmer does :) I wont talk about how to write a Hello World program in C but rather about how this runs on your system..Yes the build process..When you write a program, as you understand it, the machine has to be able to understand and interpret the code. So how does it work? What does the OS do so that you see the execution results on the screen? It uses some compiler tools ---->Compile - Link- Relocate and Load. I am sure many of you are familiar with this process. Especially if you are using GNU tools like gcc, as, ld!!! If you did not use it before I recommend that you start soon since it is open source, easy to use.
Well, when you develop software, the very first thing you need to do is compile each source file..If the compilation process is successful, then you will have object files (.o extensions). All the source files must be assembled to an object file. The second step is to produce one object file. This file will be the combination of all the object files in one single file. Every object file has text, data, and bss segments. The machine code resides in the text segment, the initialized variables go to the data segment and unitialized variables go to the bss segment. In the final object file these segments include these segments of all the object files. After this step, the relative addresses must be assigned in the code, which is relocation..Relocation is important since when the code is loaded into the physical memory, the relative offsets play an important role. Because the code will not be loaded to the 0th memory location but to any empty location with the relative offsets. This final file with the relocation information is ready to run in the memory. As I mentioned above, these steps can be fulfilled with GNU tools. GNU gcc is a compilation tool. You can compile your code with a command such as: "gcc -g -c -Wall -I../include source_files.c". -g flag is for debug information, -c tells the compiler not to link files, and -Wall flag means turn the warnings on, and -I../include tells the compiler to look in the include directory for the header files..I also want to say that, for an embedded systems, you will be using a cross-compiler and there must be a toolchain that converts the code into your embedded target board machine code. I am using ARM board and in my case, I use arm-elf-gcc instead of native gcc. Why am I doing this? Because it doesnt make sense to compile the code on the embedded target since it is already resource restricted. For that reason we use host platforms and develop the code using a cross compiler. After compilation, we need to link and locate. GNU has the ld tool. A sample command can be such as: "arm-elf-ld -Map source.map -T arm.ld -o source.exe source_files.o". -Map flag is used to generate a map file. arm.ld is a linker script which gives information about the ram, rom start adress, memory sizes as well as text, data, and bss start address. -o flag tells to link the object files and make an executable file, which is sources.exe in this case. Now the code is ready to run on your embedded system as it is transfered to the board.
Rather than doing these steps manually, we can also use the make utility. It simplifes the compilation process and used in every project. But I will talk about it in my next post..
For now have a good night.
No comments:
Post a Comment