简体   繁体   English

如何用用户定义的函数覆盖 C lib 函数,如 _sbrk?

[英]How to override C lib functions like _sbrk with user-defined one?

I want to use STL functions in C and C++ on RISCV that are provided by newlib .我想在由newlib提供的 RISCV 上的 C 和 C++ 中使用 STL 函数。 In order to use those I must replace the default implementation of _sbrk , _read , _write and some other functions.为了使用这些,我必须替换_sbrk_read_write和其他一些函数的默认实现。 By default, newlib provides these functions and they use syscalls using RISCV's ecall instruction.默认情况下, newlib提供这些函数,它们使用syscallsecall指令使用系统调用。 However, I don't want to implement ecall instructions and instead provide new functions for them.但是,我不想实现ecall指令,而是为它们提供新功能。

The problem is that I don't know how to replace a function in C and C++.问题是我不知道如何替换 C 和 C++ 中的 function。 So my question is basically, how do I override a function in C/C++?所以我的问题基本上是,如何在 C/C++ 中覆盖 function? Specifically the _sbrk and other syscall functions.特别是_sbrk和其他syscall函数。

Note: Since I don't have a cross-compiler for RiscV, this example uses malloc() instead of _sbrk() .注意:因为我没有 RiscV 的交叉编译器,所以这个例子使用malloc()而不是_sbrk() The principle is the same.原理是一样的。

  1. Implement these functions yourself.自己实现这些功能。

     #include <stdlib.h> void* malloc(size_t size) { (void)size; return NULL; }
  2. Then link that module to your other modules, and the functions will not be taken from the library.然后将该模块链接到您的其他模块,这些功能将不会从库中获取。

    Let's say we have this simple program:假设我们有这个简单的程序:

     #include <stdio.h> #include <stdlib.h> int main() { printf("%p\n", malloc(23)); return 0; }

    This is how I compiled and linked:这就是我编译和链接的方式:

     gcc -Wall -Wextra -pedantic -g -c main.c -o main.o gcc -Wall -Wextra -pedantic -g -c malloc.c -o malloc.o gcc -Wall -Wextra main.o malloc.o -g -Wl,-Map=my_own.map -o my_own

The produced map file is not trivial, however, these are the relevant lines:生成的 map 文件并不简单,但是,这些是相关行:

[--- lines cut ---]

 .plt           0x0000000000001020       0x20 /usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib/Scrt1.o
                0x0000000000001030                printf@@GLIBC_2.2.5
 *(.iplt)

[--- lines cut ---]

 .text          0x0000000000001040       0x2f /usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib/Scrt1.o
                0x0000000000001040                _start
 .text          0x000000000000106f        0x0 /usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib/crti.o
 *fill*         0x000000000000106f        0x1 
 .text          0x0000000000001070       0xc9 /usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0/crtbeginS.o
 .text          0x0000000000001139       0x29 main.o
                0x0000000000001139                main
 .text          0x0000000000001162        0xf malloc.o
                0x0000000000001162                malloc

[--- lines cut ---]

As shown, printf() is taken from the library, but my own malloc() is used.如图所示, printf()取自库,但使用了我自己的malloc()

This is what you need: https://github.com/openhwgroup/cv32e40p/blob/master/example_tb/core/custom/syscalls.c All required syscalls are there, malloc() is fully functional using this stub functions... Other stuff like printf() works as well, assuming the UART is at 0x10000000 (tested using qemu-system-riscv32 -machine virt)这就是您所需要的: https://github.com/openhwgroup/cv32e40p/blob/master/example_tb/core/custom/syscalls.c所有必需的系统调用都在那里,malloc() 使用此tub 函数功能齐全...假设 UART 位于 0x10000000(使用 qemu-system-riscv32 -machine virt 测试),其他类似 printf() 的东西也可以工作

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

相关问题 C++:使用用户定义的泛型函数 - C++:Use user-defined generic functions 调用联汇编用户定义的C函数 - Calling user-defined C functions in inline assembly 如何使用 C++ 中的用户定义函数添加二维方阵的左对角线 - How to add left diagonal of a 2D square matrix using user-defined functions in C++ gdb用户定义函数:如何传递复杂参数? - gdb user-defined functions: how to pass complex argument? 如何使用 3 个用户定义函数计算数字的真实平方根 - How to calculate the true square root of a number with 3 user-defined functions 用于接收/发送的 Libcurl 和用户定义函数 - Libcurl and user-defined functions for receiving/sending 如何在Visual C ++ 2012中读取用户定义的资源? - How to read user-defined resource in Visual C++ 2012? 如何在C ++中将用户定义的结构转换为变量? - How to convert user-defined struct to variant in c++? 在C ++中为用户定义的类分配了多少内存 - How much is the memory allocated for a user-defined class in C++ 在初始化 class 类型时,C++ 可以执行多少次隐式转换才能将一种用户定义类型转换为另一种? - How many implicit conversions can C++ do to convert one user-defined type to another when initializing class types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM