gpt4 book ai didi

从引导加载程序调用 C 代码

转载 作者:太空狗 更新时间:2023-10-29 15:15:09 28 4
gpt4 key购买 nike

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

我有两个问题:

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

最佳答案

您可以使用链接器脚本使用 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 机器上运行,因为我使用了 incompatible仅适用于较新型号的代码说明(%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);
//=====================

希望这在某种程度上有所帮助。干杯。

关于从引导加载程序调用 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9692787/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com