linux0.12 linking Process

Source: Internet
Author: User

Finally compile OK. Links are a lot of mistakes

Question 1:

boot/Head. o:infunction' Startup_32':(. text+0x10): Undefined reference to ' _stack_start'boot/Head. o:infunction' Startup_32':(. text+0x2e): Undefined reference to ' _stack_start'boot/Head. o:infunction' After_page_tables':(. text+0x540c): Undefined reference to ' _main'boot/Head. o:infunction' Ignore_int':(. text+0x5440): Undefined reference to ' _PRINTK'

A bunch of undefined reference to Xxxxx,stack_start are defined in KERNEL/SCHED.C, and main is defined in INIT/MAIN.C, in fact these symbols are present and can not be found when linked. Finally found the reason, because now GCC in the compilation of C will not add "_" before the function, that is, GCC compiled a C function, for example: Fun () compiled will become _fun (), and now still fun (), so that the symbol is not found, this is called name Magling, Interested can be self-Google under.

There are two ways to solve this problem:

1. On-line is to put a reference to the C symbol. S and. s in the variable before the "_" all deleted, the use of the regular is not very good to get deleted.

2. When checking the name magling, I was fortunate to see-fleading-underscore this option, which allows GCC to be forced to add "_" when compiling. This only needs to add-fleading-underscore in the Cflags, the second method is more convenient.

Scenario 2 Specific Solutions

Msed-o-o\-fleading-underscore

Remember to make a clean after you change it, and then do it.

Question 2:

MAIN.C: (. text+0x6c): Undefined reference to ' _printf'main.c: (. text+0x87): undefined Reference to ' _printf'main.c: (. text+0x142): Undefined reference to ' _puts'main.c: (. text+ 0x1e9): Undefined reference to ' _printf'

Well, before the compile time seems to be not all match to, had to manually changed, the INIT/MAIN.C in the printf all changed into PRINTW

Question 3:

VSPRINTF.C: (. text+0x18cb): Undefined reference to ' ___stack_chk_fail'function ' _do_ Execve':(. text+0x3554): Undefined reference to ' ___stack_chk_fail'function ' _tty_ioctl':tty_ioctl.c: (. text+0x51d): Undefined reference to ' ___stack_chk_ Fail'

Direct Google to the answer, this is because the Ubuntu gcc default is add-fstack-protector, here in cflags remove this option, add-fno-stack-protector, As to what this option really does, please be interested in your own Google

Msed-o-o\-fno-stack-protector

Question 4:

BUILD.C: (. text+0x1c): Undefined reference to ' _stderr'build.c: (. text+0x24): undefined Reference to ' ___fprintf_chk'function ' _main':build.c: (. text+0x7d): Undefined reference to ' _strcmp'build.c: (. text+0x99): Undefined reference to ' ___xstat' BUILD.C: (. text+0xab): Undefined reference to ' _perror'

This problem is introduced by the solution of problem 1, BUILD.C link with glibc, and glibc is not-fleading-underscore this option, so in compiling build.c we take this option to remove ... , there is no good way to find, I had to manually put Cflags, and then remove the-fleading-underscore

Modify Makefile Line 50th in the linux0.12 root directory

tools/build:tools/build.c     $ (CC) $ (CFLAGS)      -o tools/build tools/build.c

The above is the original, the following is the expansion of the

tools/build:tools/build.c    -w -o-fno-stack-protecto-m32-fstrength-reduce-fomit-frame-  Pointer     -o tools/build tools/build.c

Question 5:

BUILD.C: (. text+0xcb): Undefined reference to ' MAJOR'build.c: (. text+0xe3): undefined Reference to ' MINOR'build.c: (. text+0x16c): Undefined reference to ' MAJOR'build.c: (. text+ 0x184): Undefined reference to ' MINOR'

Missing two macro definitions, add the following two definitions to BUILD.C

#define MAJOR (x) (((unsigned) (x)) >>8)                                                                                                           #define MINOR (x) ((x) &0xff)

Question 6:

file or Directorycouldn ' T stat root device.

This is defined in makefile in the linux0.12 root directory.

root_dev=/dev/Hd6swap_dev=/dev/hd2

But we don't have this thing, read the BUILD.C code, and learn that this can be written floppy instead of/DEV/HD6

if(Argc >4) {        if(strcmp (argv[4],"Floppy")) {            if(Stat (argv[4], &SB)) {perror (argv[4]); Die ("couldn ' t stat root device."); } major_root=MAJOR (Sb.st_rdev); Minor_root=MINOR (Sb.st_rdev); } Else{major_root=0; Minor_root=0; }    } Else{major_root=Default_major_root; Minor_root=Default_minor_root; }

Question 6:

file or Directorycouldn ' T stat root device.  Make 1

Similar to the previous question, the same place, reading BUILD.C, can replace/DEV/HD2 with none

if(ARGC = =6) {        if(strcmp (argv[5],"NONE")) {            if(Stat(argv[5], &SB)) {perror (argv[5]); Die ("couldn ' t stat root device."); } Major_swap=MAJOR (Sb.st_rdev); Minor_swap=MINOR (Sb.st_rdev); } Else{Major_swap=0; Minor_swap=0; }    } Else{Major_swap=Default_major_swap; Minor_swap=Default_minor_swap; }

Question 7:

' system '  Make 1

Let's look at the code first

if((Id=open (argv[3],o_rdonly,0)) <0) Die ("Unable to open ' system '");if(Read (id,buf,gcc_header)! =Gcc_header) Die ("unable to read header of ' system '");if(((Long*) BUF) [5] !=0) Die ("NON-GCC header of ' system '"); for(i=0; (C=read (Id,buf,sizeofBUF)) >0; i+=c)if(Write (1, buf,c)! =c) Die ("Write call failed"); 

Roughly 4 steps, 1. Open the 4th parameter of the file, which is system,2. Read the first 1024 bytes of system; 3. Determine if the program entry address is 0x0;4. Reads the remainder of the write to the image. Wrong in the third step. Let's take a look at the readelf-h system.

~/workspace/blog/linux-0.12/tools$ Readelf-h system ELF header:magic:7f $4c $  on  on  on xx xx xx xx xx xx xx xx xxclass:elf32 Data:2's complement, little endianVersion:1(current) OS/abi:unix-System V ABI Version:0type:exec (executable file) Machine:intel80386Version:0x1Entry Point Address:0x80480a0Start of program headers: the(bytes to file) Start of section headers:133532(bytes into file) Flags:0x0Size of ThisHeader the(bytes) Size of program headers: +(bytes) Number of program headers:3Size of section headers: +(bytes) Number of section headers:8Section HeaderstringTable index:7

Here you can see the entrance of the program is 0x80480a0, not 0x0, so to tell the linker, put the text section on the 0x0, under the linux0.12 root directory makefile, modify the LD command, followed by the-ttext 0x0-e startup_32, At the same time, add startup_32 in Head.s. Global for external access. In other words, the 24th byte of the GCC header is version, and the 26th byte begins the program entry ... The amount, simply directly to judge GCC head that section to kill ...

Then the link is OK, and the image is built into the debug phase.

linux0.12 linking Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.