【问题标题】:Calling C code from a bootloader从引导加载程序调用 C 代码
【发布时间】:2012-03-13 21:53:27
【问题描述】:

我正在尝试编写引导加载程序。我想编译一些 C 代码,以便引导加载程序可以将其加载到内存中并跳转到那里。

我有两个问题:

  • 调用约定是否与 x86 上的相同?即,堆栈上的参数,从左到右。
  • 如何使用 gcc 生成原始二进制文件?

【问题讨论】:

  • 您可能需要检查bcc 编译器。我很久没有构建 Linux 内核了,但我记得 2.2 系列用它来编译一些引导代码。

标签: c gcc assembly x86 bootloader


【解决方案1】:

您可以使用链接器脚本使用 gcc 链接器创建普通二进制文件。 关键是 OUTPUT_FORMAT(binary) 指令:

//========================================
FILE: linker.ld
//========================================
OUTPUT_FORMAT(binary)
SECTIONS {
    .text : { *(.text) }
    .data : { *(.data) }
    .bss  : { *(.bss)  }
}
//========================================

我在 makefile 中调用链接器如下(而 linker.ld 是链接器脚本文件):

//========================================
ld -T linker.ld loaderEntry.o loaderMain.o -o EOSLOAD.BIN -L$(lib) -lsys16
//========================================

我已经编译了目标代码

//========================================
gcc -nostdinc -nostdlib -ffreestanding -c <code files> -o theObjectCode.o
//========================================

为了摆脱不能在 16 位模式下工作的标准库。

对于握手 MBR 加载程序和引导加载程序,我使用了以下 loaderMain.S gcc 汇编代码(如上所示,loaderMain.o 必须是传递给链接器的第一个文件,位于地址偏移 0x0000 处)。 我使用 -code16gcc 指令来生成 16 位代码。 但是,生成的代码可能无法在旧的 x86 机器上运行,因为我使用了不兼容的 仅适用于较新型号的代码指令(%esp、$ebp、leave 等)。

//========================================
FILE: loaderEntry.S
//========================================
  .text
  .code16gcc

  // the entry point at 0x9000:0x0000 // this is where I did a far call to by the MBR
  .globl loaderMain   // loader C entry function name declaration
  push  %cs           // initialize data segments   with same value as code segment
  pop   %ax           // (I managed only tiny model so far ...)
  movw  %ax, %ds
  movw  %ax, %es
  movw  %ax, %fs
  movw  %ax, %gs
  movw  %ax, %ss      // initialize stack segment with same value as     code segment
  movl  $0xffff, %esp // initialize stack pointers with 0xffff (usage of extended (dword) offsets does not work, so we're stuck in tiny model)
  movl  %esp, %ebp
  call  loaderMain   // call C entry function

  cli // halt the machine for the case the main function dares to return
  hlt
//========================================

汇编代码调用一个已在 C 语言文件 loaderMain.c 中定义的符号。 为了生成 16 位模式兼容代码,您必须在您使用的每个 C 文件的第一行代码之前声明使用 16 位指令集。这只能通过内联汇编指令 AFAIK 来完成:

  asm(".code16gcc\n"); // use 16bit real mode code set

  /*  ... some C code .. */


  // ... and here is the C entry code ... //
  void loaderMain() {
    uint cmdlen = 0;
    bool terminate = false;
    print(NL);
    print(NL);
    print("*** EOS LOADER has taken over control. ***\r\n\r\n");
    print("Enter commands on the command line below.\r\n");
    print("Command are executed by pressing the <ENTER> key.\r\n");
    print("The command \'help\' shows a list of all EOS LOADER commands.\r\n");
    print("HAVE FUN!\r\n");
    print(NL);
    while (!terminate) {
        print("EOS:>");
        cmdlen = readLine();
        buffer[cmdlen] = '\0';
        print(NL);
        terminate = command();
    }
    shutdown();
  }

到目前为止,我只能编写纯 C 代码 - 到目前为止,我还没有成功编写 C++ 代码, 我只设法产生了微小的内存模型(意味着 CS、SS、DS 和 ES 都是一样的)。 gcc 仅使用偏移量作为指针地址,因此如果没有额外的汇编代码似乎很难克服内存模型问题。 (虽然我听说有人在 gcc 中解决了这个问题)

调用约定是最后一个参数首先被压入堆栈,并且似乎所有值都是双字对齐的。下面贴出可以在 .code16gcc C 代码中调用的汇编代码示例:

//======================
  .text
  .code16gcc

  .globl kbdread             // declares a global symbol so that the function can be called from C
  .type  kbdread, @function  // declares the symbol as a function
kbdread:                     // the entry point label which has to the same as the symbol

  // this is the conventional stack frame for function entry
  pushl %ebp
  movl  %esp, %ebp

  // memory space for local variables would be allocated by decrementing the stack pointer accordingly
  // the parameter arguments are being addressed by the base pointer which points to the same address while bein within the function

  pushw %ds  // I'm paranoid, I know...
  pushw %es
  pushw %fs
  pushl %eax
  pushl %ebx
  pushl %ecx
  pushl %edx
  pushl %esi
  pushl %edi

  xorl %eax, %eax  // calls the keyboard interrupt in order to read char code and scan code
  int  $0x16

  xorl %edi, %edi
  movl 8(%ebp), %edi // moves the pointer to the memory location in which the char code will be stored into EDI             
  movb %al, (%edi)   // moves the char code from AL to the memory location to which EDI points

  xorl %edi, %edi // paranoid again (but who knows how well the bios handles extended registers??)..

  movl 12(%ebp), %edi // moves the pointer to the memory location in which the scan code will be stored into EDI
  movb %ah, (%edi)    // moves the scan code from AH to the memory location to which EDI points

  popl %edi // restoring the values from stack..
  popl %esi
  popl %edx
  popl %ecx
  popl %ebx
  popl %eax
  popw %fs
  popw %es
  popw %ds

  leave  // .. and the conventional end frame for functions.
  ret    // be aware that you are responsible to restore the stack when you have declared local variables on the stack ponter.
         // the leave instruction is a convenience method to do that. but it is part of not early X86 instruction set (as well as extended registers)
         // so be careful which instruftion you actually use if you have to stay compatible with older computer models.
//=====================

顺便说一句,函数的 C 头声明如下所示:

//=====================
void kbdread(char* pc, (unsigned char)* psc);
//=====================

希望这对您有所帮助。干杯。

【讨论】:

    【解决方案2】:

    根据您之前的问题,我假设您想为现代 x86 机器(即 386 或更高版本)创建引导加载程序。

    在实模式下,默认操作数和地址大小为 16 位。不幸的是,GCC 无法生成 16 位 x86 汇编代码。但是,通过将指令.code16gcc 放在每个文件的顶部,您可以告诉as 使用将覆盖地址和操作数大小的指令前缀。这些前缀在Intel 64 and IA-32 Architectures Software Developer's Manual Volume 1的第 3.3.5 节中有更详细的描述。

    有关.code16gcc 的更多信息,请访问here。请注意,本手册是 2003 年的,.code16gcc 不再是实验性的,或者至少足够稳定以供 Linux 使用。

    由于 gcc 不知道汇编代码调用约定将保持不变。 Here 是一个 ld 脚本,可用于生成引导加载程序。

    【讨论】:

    【解决方案3】:

    8086 x86。 8088/86 使用了不同的型号,小型、中型、大型、巨大。并且根据模型,您可能/将会在堆栈上获得差异。大/大返回地址是段和偏移量,其中小的返回地址只是偏移量(例如,导致整个堆栈设置发生变化)。 Carl 已经提到了堆栈的宽度。

    编译和反汇编几个简单的例子,这应该会变得很明显。如果 gcc 不做非平面目标,那么也许试试 djgpp。或 watcom 或 borland(免费)。

    【讨论】:

      【解决方案4】:

      首先,8086 是 x86。

      其次,调用约定特定于您正在使用的编译器及其任何可以更改它的功能(例如,您通常可以指定 cdeclstdcallfastcall 等内容)。你用的是什么编译器?

      第三,gcc 不会将代码编译成 16 位 x86 指令。

      按照@dwelch 的建议,使用 Open Watcom C/C++ 或古老的 Borland/Turbo C/C++,它们是免费的,可以编译 16 位代码。

      这一切都可以做到,12

      【讨论】:

        【解决方案5】:
        1. 调用约定与 IA32 类似,不同之处在于所有内容都以 16 位字而不是 32 位推送。
        2. 您可能无法使用 GCC 直接创建平面二进制文件。 GCC 是否仍然支持 16 位 Intel 机器?不过,您应该能够使用objcopy 从链接对象中获取原始二进制数据。您可能也想查看linker scripts

        【讨论】:

          猜你喜欢
          • 2012-03-29
          • 1970-01-01
          • 2015-01-19
          • 2020-06-23
          • 2011-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多