将二进制文件与内存中特定位置的函数/数据链接起来 [英] Linking binary against functions/data in specific location in memory

查看:21
本文介绍了将二进制文件与内存中特定位置的函数/数据链接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 ATMega 编写中间内存引导加载程序.

I'm currently in the process of writing an intermediate-memory bootloader for an ATMega.

我想将一段常用函数和数据放在内存中的特定位置,这样:

I'd like to place a section of commonly used functions and data in a specific location in memory, such that:

  • 无法克服引导加载程序部分的大小限制
  • 应用程序部分不会复制库函数、驱动程序等,因此会浪费空间

为了便于说明,下面是所需内存布局的地图:

For illustrative purposes, a map of the desired memory layout is below:

此线程 在 avrfreaks 上,我已经能够移动未标记为 __attribute__ ((section(".boot"))) 通过自定义链接器脚本成功地连接到共享库部分.

Following some help in this thread on avrfreaks, I'm to the point where I've been able to move all code (in my bootloader + library development environment - applications will be developed in separate projects) not tagged with __attribute__ ((section(".boot"))) to the shared library section successfully via means of a custom linker script.

在 avrfreaks 线程中建议我可以使用 avr-objcopy --strip-all --keep-symbol=fred --keep-symbol=greg ... boot.elf dummy 链接我的应用程序.elf 来创建我在共享库中的符号引用,然后使用 avr-gcc -o app.elf -Wl,--just-symbols= 将我的应用程序链接到此内存布局dummy.elf app1.o app2.o ....

It was suggested in the avrfreaks thread that I can link my applications by using avr-objcopy --strip-all --keep-symbol=fred --keep-symbol=greg ... boot.elf dummy.elf to create a symbol reference of what I have in my shared library, and then linking my applications against this memory layout with avr-gcc -o app.elf -Wl,--just-symbols=dummy.elf app1.o app2.o ....

我在这里面临的问题是我需要指定要保留在我的 dumy.elf 中的每个符号.我可以使用 keep-symbols=<file> 指令来指定要保留的符号的文本文件列表,但我仍然必须生成此列表.

The problem I face here is that I need to specify each symbol I want to keep in my dumy.elf. I can use the keep-symbols=<file> directive to specify a text file list of symbols to keep, but I still must generate this list.

我注意到有一堆我不想想要包含的符号(诸如 C 环境设置代码之类的东西,名称很常见,但功能不同,例如引导加载程序和应用程序)似乎以前缀_"开头(但当然,有一些有用的大型库函数具有相同的前缀,例如 *printf 和数学例程).如果我将我的应用程序与应用程序/引导加载程序中现有的运行时代码链接起来,也许不会有冲突?

I've noticed that there is a bunch of symbols that I don't want to include (stuff like C environment set-up code that is common in name, but different in functionality, for both the bootloader and application) that seems to start with the prefix '_' (but of course, there are some useful and large library functions with the same prefix, e.g. *printf and math routines). Perhaps there won't be conflicts if I link my application against the existing runtime code in the application/bootloader?

如何为我的库部分生成符号列表,其中包含我编写的代码(可能是一些 sed 魔术和扫描头文件)/排除在链接应用程序时可能发生冲突的任何符号?

How can I generate a list of symbols for my library section that contains the code that I've written (maybe some sed magic and scanning header files)/excludes any symbols that may conflict in linking the application?

可以在this github repository查看项目的当前状态.

The project can be viewed in its current state at this github repository.

我想明确一点,我可以__attribute__ ((section(".library"))) 标记我想要在共享库部分中的所有内容,但由于我还想在引导加载程序和应用程序之间共享一些相当大的 libc 内容(vsprintf 等),这很快就会变得很麻烦.因此,我选择通过链接描述文件将未标记为引导的所有内容放入库内存region.

I want to make clear that I could tag everything I want to be in the shared library section with __attribute__ ((section(".library"))), but as I also want to share some rather large libc stuff (vsprintf, etc) between the bootloader and application, this becomes cumbersome very quickly. As such, I've elected to put everything not tagged as boot in the library memory region via a linker script.

也许我只是需要一些关于 我的链接器脚本的建议,因为我不太确定我在那里做什么.

Perhaps I just need some advice on my linker script, as I'm not super sure what I'm doing there.

推荐答案

考虑使用 -R 作为链接器选项 (gcc -Wl,-R -Wl,<文件>).

Consider using -R <file> as linker option (gcc -Wl,-R -Wl,<file>).

这将在 <file> 中生成对(全局)符号的引用,就像它们被正常链接一样,但不包括引用的代码.

This will generate references to (global) symbols in <file> just as if they were linked normally, but not include the referenced code.

这篇关于将二进制文件与内存中特定位置的函数/数据链接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆