Bootloader in C won't compile

后端 未结 5 1950
你的背包
你的背包 2021-01-30 04:30

I am a newbie in writing bootloaders. I have written a helloworld bootloader in asm, and I am now trying to write one in C. I have written a helloworld bootloader in C, but I ca

5条回答
  •  灰色年华
    2021-01-30 05:05

    Let me assume a lot of things here: you want to run your bootloader on an x86 system, you have the gcc toolchain set up on a *nix box.

    There are some points to be taken into account when writing a bootloader:

    1. the 510 byte limit for a VBR, even lesser for MBR due to partition table (if your system needs one)
    2. real mode - 16 bit registers and seg:off addressing
    3. bootloader must be flat binary that must be linked to run at physical address 7c00h
    4. no external 'library' references (duh!)

    now if you want gcc to output such a binary, you need to play some tricks with it.

    1. gcc by default splits out 32bit code. To have gcc output code that would run in real mode, add __asm__(".code16gcc\n") at the top of each C file.
    2. gcc outputs compiled objects in ELF. We need a bin that is statically linked at 7c00h. Create a file linker.ld with following contents

      ENTRY(main);
      SECTIONS
      {    
          . = 0x7C00;    
          .text : AT(0x7C00)
          {
              _text = .;
              *(.text);
              _text_end = .;
          }
          .data :
          {
              _data = .;
              *(.bss);
              *(.bss*);
              *(.data);
              *(.rodata*);
              *(COMMON)
              _data_end = .;
          }    
          .sig : AT(0x7DFE)    
          {        
              SHORT(0xaa55);
          }    
          /DISCARD/ :
          {
              *(.note*);
              *(.iplt*);
              *(.igot*);
              *(.rel*);
              *(.comment);
              /* add any unwanted sections spewed out by your version of gcc and flags here */    
          }
      }
      
    3. write your bootloader code in bootloader.c and build the bootloader

      $ gcc -c -g -Os -march=i686 -ffreestanding -Wall -Werror -I. -o bootloader.o bootloader.c
      $ ld -static -Tlinker.ld -nostdlib --nmagic -o bootloader.elf bootloader.o
      $ objcopy -O binary bootloader.elf bootloader.bin
      
    4. Since you already have built boot loaders with ASM, I guess the rest is obvious to you.

    - taken from my blog: http://dc0d32.blogspot.in/2010/06/real-mode-in-c-with-gcc-writing.html

提交回复
热议问题