Challenge yourself
Problem
In proc.c, there is an array initcode[] of binary code. What is its relationship with initcode.S?
Solution
Research
Testing code: -xc to make the hex dump more readable. Example:
$ od -xc test.txt
0000000 6568 6c6c 0a6f
h e l l o \n
0000006The od Command

od man page

-t is the output format. x is hex. c is chars in default char set.
Note: Each Hexadecimal character represents 4 bits (0 - 15 decimal). A byte is 2 hex.
Answer
We want a memory dump of instructions in hex format, and separated by each char.
We have to load initcode, and use it to call system call exec to run init. We cannot directly load init binary as hex dump, otherwise we have to do something similar to what exec does, set up C stacks, parse ELF headers, etc.
So the simply solution is:
1. use objcopy to copy a stripped instruction only data file.
2. Use od to print the data file in hex, separated by byte (char).
3. Append 0x to each char.
4. That is the result if initcode array you see in proc.c.
References
od - dump files in various formats.
See od The UNIX School: 3 different ways of dumping hex contents of a file
Challenge!
Try to switch the hex dump of initcode to a program you wrote!
I modify initcode.S to use echo:
After running make, get the hex dump of the binary:
Change the array in proc.c:
Make a special echo:
As a result, your terminal prints the following forever:
We hacked the kernel to run our program in the very first user process!
Last updated
Was this helpful?