Lab 1 Part 2 The Boot loaderloading the Kernel
We can now take a closer look at the C-language part of boot loader, the BOOT/MAIN.C. But before we analyze it, we should review some basic knowledge of C language.
Exercise 4:
Read the knowledge of the pointers section of the C language. The best reference book is Naturally "the C programming Language".
Read Sections 5.1 to 5.5. Then download the code for POINTERS.C and run it, making sure you understand how all the values printed on the screen come from. In particular, it is important to understand the 1th line, how the pointer address of line 6th is obtained, and how the values from line 2nd to 4th are obtained, and why the values printed in the 5th line appear to be crashing.
Waring: Don't skip this exercise unless you already know the C language quite well.
answers to this exercise connection: http://www.cnblogs.com/fatsheep9146/p/5216735.html
To be able to understand the BOOT/MAIN.C program, you must first know what an elf file is. When you compile and link a C language program like the Jos kernel, the compiler converts the C language source file (. c suffix) to the target file (. o suffix). The target file contains machine instructions that can be executed directly by the machine. The linker combines all the target files into a single binary image (binary images), such as Obj/kern/kernel. Such binary image files are in elf format.
In 6.828, you can assume that an elf file that can be executed consists of three main parts: a file header with loading information, followed by the program segment table, followed by a few program sections. Each segment is a contiguous piece of code or data. They are loaded into memory first when they are run. The job of boot loader is to load them into memory.
An elf file, beginning with a fixed-length elf file header, followed by a program segment table that lists all the segments to be loaded into memory. The format of the Elf file header is stated in the Inc/elf.h file. In 6.828 we were very interested in three segments:
* . Text segment : executable code to store all programs
* . Rodata segment : Data segments that hold all read-only data, such as String constants.
* . Data segment : holds all initialized data segments, such as global variables with initial values.
When the linker calculates the memory layout of the entire program, it will be a variable that has not been initialized, such as int x, in a segment immediately following the. Data segment, preserving their information in the. BSS segment . The C language requires all variables that are not initialized to have a value of 0. Therefore, we do not need to store the values of these variables in the elf file, because it must be 0. So the linker simply stores the address and size of these variables in the. BSS segment. The loader assigns an initial value of 0 to these segments only when the program is loaded into memory.
You can examine the names, sizes, and addresses of all the segments in the Jos kernel by following the instructions below.
objdump-h Obj/kern/kernel
The results are as follows:
In the diagram we will find information about all the segments of this executable, not just the four segments we mentioned earlier, but also some of the other ones, which are mainly used to store some debug information and so on.
There are two more important fields in each segment, VMA (link address), LMA (load address). Where the load address represents the physical address where the segment is loaded into memory. The link address refers to the logical address to which this segment is expected to be stored.
Each elf file has a program Headers Table that indicates which parts of the elf file are loaded into memory and which addresses are loaded into memory. You can get the Kernel program Headers table information by entering the following command:
Objdump-x Obj/kern/kernel
The program header lists all of the information that is loaded into the memory segment, which is also the table entry for Project Headers table. Each table item diagram lists all the fields that are involved in this table entry. It can be seen that some segments were not added to memory at the end. In, the segments that need to be loaded into memory are marked as load.
The BIOS typically loads the boot sector to the memory address 0x7c00, which is the load address of the boot sector and also the link address of the boot sector. We can set the link address of the boot sector through the-ttext 0X7C00 statement in the Boot/makefrag file, and the link address will later be used by the linker to ensure that the linker produces the correct code.
Exercise 5
Once again, follow the instructions at the beginning of boot loader to find the first command that meets the following conditions:
When I modify the link address of the boot loader, this command will have an error.
After finding such a command, modify the link address of the boot loader, we will modify its link address in the Boot/makefrag file, run make clean after the modification is completed, then recompile the kernel with the Make command, and then find the command to see what happens. Finally, don't forget to change back.
Practice Answer Connection: http://www.cnblogs.com/fatsheep9146/p/5220004.html
Let's review the kernel's load address and link address. Unlike the boot loader, the two addresses of the kernel are different. The kernel tells boot loader to load it to a low address (load address), but it wants to run at the high address (the link address). We will take a closer look at this issue in the next chapter.
In addition to the information in each paragraph, in the Elf head, there is also a very important information is the E_entry field. This field holds the link address of the execution entrance of this executable program. You can view the entrance to the kernel program by following the instructions below.
The Visible program entry address is 0x0010000c, and this address is consistent with our previous analysis.
Exercise 6
In this exercise, we will try to use GDB's X command (see Memory command). X/nx ADDR. This command will print out the contents of N words after the start of the addr address. Restart QEMU. Before the BIOS enters boot loader, the memory address 0x00100000 8 Words of content, then enters the boot loader run to the beginning of the kernel stop, and then look at the value at this address. Why are they different? What is the meaning of the value stored in this memory for the second time?
Answer:
Before entering boot loader, the contents of 8 words after starting at the memory address 0x00100000 are:
Before entering kernel, the contents of the 8 words from the memory address 0x00100000 start at:
Why this change, because the Bootmain function in the end will be the kernel of the various program segments into the memory address 0x00100000, so here is now stored in the kernel of a segment of the content, because the program entry address is 0x0010000c, exactly in this section, Therefore, it can be inferred that this should be stored in the instruction segment, that is, the contents of the. Text segment.
So, all of the Lab 1 Part 2 experiments are done.
If you have questions and suggestions, please feel free to harass
[Email protected]
MIT 6.828 Jos Study Note 7. Lab 1 part 2.2:the Boot Loader