简体   繁体   English

从ASM调用的C ++代码中的标准库的链接

[英]Linkage of standard libraries in C++ code called from ASM

as I am developing my "OsDev" project, where I am learning a new stuff (for somebody who did not code in C/C++ for a long time due to web development it is kinda "new"). 当我开发“ OsDev”项目时,我正在学习一个新的东西(对于那些由于Web开发很长时间没有用C / C ++编写代码的人来说,这有点“新”)。 I figured out in the other thread, that calling a C++ function from ASM needs to have a extern "C" prefix but now I have problem with the lining of standard libraries as a for example cstdio etc. I stuck with this message. 我在另一个线程中发现,从ASM调用C ++函数需要有一个extern "C"前缀,但是现在我对标准库的内衬(例如cstdio等)有疑问。我坚持此消息。

kc.o: In function `kmain':
kernel.cpp:(.text+0x3e4): undefined reference to `strlen`

C++ C ++

#include <string.h>
#include <cstdio>

#include "inc/screen.h"

extern "C" void kmain()
{
    clearScreen();
    kernel_print((char*)"Hello Github! :-)", 0x04);
}

and if I try to use strlen() it won't link. 如果我尝试使用strlen() ,它将不会链接。 (BTW. including screen.h is working for some reason). (顺便说一句,包括screen.h由于某种原因在工作)。 Compiling script 编译脚本

nasm -f elf32 kernel.asm -o kasm.o
g++ -c kernel.cpp -o kc.o -lgcc -m32  -Wall -Wextra -O2
ld -m elf_i386 -T link.ld -o kernel.bin kasm.o kc.o

link.ld 链接

OUTPUT_FORMAT(elf32-i386)
ENTRY(start)
SECTIONS
 {
   . = 0x100000;
   .text : { *(.text) }
   .data : { *(.data) }
   .bss  : { *(.bss)  }
 }

Thanks for any suggestions. 感谢您的任何建议。 :) :)

Your code cannot work as kernel's can't directly use shared libraries. 您的代码无法工作,因为内核无法直接使用共享库。

Why can't I use shared libraries directly in my kernel? 为什么我不能直接在内核中使用共享库?

When an application is loaded by the operating system, all the required files are brought into its address space. 当操作系统加载应用程序时,所有必需的文件都被带入其地址空间。 This includes the executable file and any dynamic libraries (all ABI-conforming ELF applications will always link with a system library - the C Standard Library or just libc ). 这包括可执行文件和任何动态库(所有符合ABI的ELF应用程序将始终与系统库-C标准库或libc链接 )。

But while loading the kernel, only the original executable is loaded. 但是在加载内核时,仅加载原始可执行文件。 Multiboot 2 (with GRUB bootloader) will allow you to load kernel-modules which can be dynamic libraries. Multiboot 2(使用GRUB bootloader)将允许您加载可以是动态库的内核模块 But still, your kernel must know how to link itself and the kernel-modules in physical memory. 但是,内核仍然必须知道如何链接自身和物理内存中的内核模块。 To do so, you must implement a ELF parser and dynamic linker in your kernel. 为此,您必须在内核中实现ELF解析器和动态链接器。

Before implementing one, make sure your kernel is mature enough to systematically handle dynamic memory allocation, pagination, and other basic features. 在实施内核之前,请确保您的内核足够成熟,可以系统地处理动态内存分配,分页和其他基本功能。

How can I use the sweet features of libc? 如何使用libc的功能?

Usually, you won't use all of the userspace functionality of libc . 通常,您不会使用libc的所有用户空间功能。 But things like memcpy , strlen , strcpyn and so on are absolutely necessary. 但是像memcpystrlenstrcpyn类的东西是绝对必要的。 You will have to implement these functions on your own, but the better part here is that, you can change the names of these functions. 您将必须自己实现这些功能,但是更好的是,您可以更改这些功能的名称。 For example, if you prefere camelCase for function names, then you can also use function names like copyMemory , lengthOfString , etc. 例如,如果您更喜欢使用camelCase作为函数名,那么还可以使用函数名,例如copyMemorylengthOfString等。

https://github.com/SukantPal/Silcos-Kernel https://github.com/SukantPal/Silcos-Kernel

I have built my own kernel, which has a few implementations of the required functions in KernelHost/Source/Util/CircuitPrimitive.cpp . 我已经构建了自己的内核,该内核在KernelHost / Source / Util / CircuitPrimitive.cpp中具有所需功能的一些实现。 You can look into that. 您可以调查一下。 Also, it has a full-fledged module linker. 此外,它还具有完整的模块链接器。 (KernelHost, ModuleFramework, etc. those parent folders contain separate kernel-module source code). (KernelHost,ModuleFramework等。这些父文件夹包含单独的内核模块源代码)。

Make sure not to use the standard C headers in your kernel, as for now. 到目前为止,请确保不要在内核中使用标准的C标头。 Implement all required functions on your own, including printf 自行实现所有必需的功能,包括printf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM