将符号链接到Linux上的固定地址 [英] Linking symbols to fixed addresses on Linux

查看:367
本文介绍了将符号链接到Linux上的固定地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用GNU ld将(某些)符号链接到特定的固定地址,以便二进制文件在Linux(x86)中仍可正常执行?这些符号不会有任何访问,但他们的地址是重要的。

How would one go about linking (some) symbols to specific fixed addresses using GNU ld so that the binary could still be executed as normal in Linux (x86)? There will not be any accesses to those symbols, but their addresses are important.

例如,我会有以下结构:

For example, I'd have the following structure:

struct FooBar {
    Register32 field_1;
    Register32 field_2;
    //...
};

struct FooBar foobar;

我想将 foobar 链接到地址0x76543210,但正常链接标准库和其他应用程序。然后,应用程序将使用foobar的地址,但不会引用它后面的(可能不存在的)内存。

I'd like to link foobar to address 0x76543210, but link the standard libraries and the rest of the application normally. The application will then make use of the address of foobar, but will not reference the (possibly non-existent) memory behind it.

这个请求的基本原理是这个可以在两个平台上使用相同的源代码:在本地平台上, Register32 可以简单地为 volatile uint32_t ,但在Linux Register32 是一个与 uint32_t 大小相同的C ++对象,它定义了例如 operator = ,然后它将使用该对象的地址并向具有该地址(和数据)的通信框架发送请求以在远程硬件上执行实际访问。因此,链接程序将确保结构的 Register32 字段引用正确的地址。

The rationale for this request is that this same source can be used on two platforms: On the native platform, Register32 can simply be a volatile uint32_t, but on Linux Register32 is a C++ object with the same size as a uint32_t that defines e.g. operator=, which will then use the address of the object and sends a request to a communication framework with that address (and the data) to perform the actual access on remote hardware. The linker would thus ensure the Register32 fields of the struct refer to the correct "addresses".

推荐答案

使用 - defsym symbol = address 的建议确实有效,但当您有几十个这样的实例需要映射时有点麻烦。但是, - just-symbols = symbolfile 只是一个窍门。我花了一段时间才找出 symbolfile 的语法,它是 $ b

The suggestion by litb to use --defsym symbol=address does work, but is a bit cumbersome when you have a few dozen such instances to map. However, --just-symbols=symbolfile does just the trick. It took me a while to find out the syntax of the symbolfile, which is

symbolname1 = address;
symbolname2 = address;
...

空格似乎是必需的,否则 ld 报告文件格式无法识别;视为链接器脚本

The spaces seem to be required, as otherwise ld reports file format not recognized; treating as linker script.

这篇关于将符号链接到Linux上的固定地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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