无法访问 objcopy 创建的文件中的大小变量
我正在尝试使用 objcopy 将 xml 文件转换为对象文件,然后将其链接到 RHEL5 上的另一个共享库并由其使用。我使用以下命令转换文件:
objcopy --input-format binary --output-target i386-pc-linux-gnu --binary-architecture i386 benchmarks.xml benchmarks.0
创建目标文件并使用 readelf 我得到以下:
符号表“.symtab”包含 5 个条目: Num:值大小类型绑定 Vis Ndx 名称 0: 00000000 0 无类型本地默认值 1: 00000000 0 部分本地默认值 1 2: 00000000 0 NOTYPE 全局默认值 1 _binary_baselines_xml_sta 3: 0000132b 0 NOTYPE 全局默认值 1 _binary_baselines_xml_end 4: 0000132b 0 NOTYPE GLOBAL DEFAULT ABS _binary_baselines_xml_siz
所以看起来大小就在那里。我转储了该文件并验证了 xml 在偏移量 34(由 .data 值指定)处嵌入为 ascii,并且它是正确的。数据大小为 0x132b 字节,由变量指定。
然后在代码中,我声明了几个变量:
extern "C"
{
extern char _binary_baselines_xml_start;
extern char _binary_baselines_xml_size;
}
static const char* xml_start = &_binary_baselines_xml_start;
const uint32_t xml_size = reinterpret_cast<uint32_t>(&_binary_baselines_xml_size);
当我进入此阶段时,xml 指针是正确的,并且我可以在调试器中看到 xml 文本。但是,大小符号显示的值为 0x132b(这就是我想要的),但它也表明“地址 0x132b 超出范围”。当我使用该变量时,它是一个非常大的不正确的随机数。我尝试了各种其他语法来声明 extern 变量,例如 char*、char[]、int、int* 等。结果总是相同的。价值就在那里,但我似乎无法理解它。
另一个有趣的点是,这段代码在 Windows 机器上运行良好,外部变量上没有前置下划线,但其他一切都相同。
我似乎在网上找不到太多关于以这种方式使用 objcopy 的信息,因此非常感谢任何帮助。
I am attempting to use objcopy to convert an xml file to an object file that is then linked into and used by another shared library on RHEL5. I convert the file with this command:
objcopy --input-format binary --output-target i386-pc-linux-gnu --binary-architecture i386 baselines.xml baselines.0
The object file is created and using readelf I get the following:
Symbol table '.symtab' contains 5 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00000000 0 NOTYPE GLOBAL DEFAULT 1 _binary_baselines_xml_sta
3: 0000132b 0 NOTYPE GLOBAL DEFAULT 1 _binary_baselines_xml_end
4: 0000132b 0 NOTYPE GLOBAL DEFAULT ABS _binary_baselines_xml_siz
So it looks like the size is in there. I dumped the file and verified the xml is embedded as ascii at offset 34 (specified by the .data value) and that it's correct. The data is 0x132b bytes in size, as specified by the variable.
Then in the code, I declare a couple variables:
extern "C"
{
extern char _binary_baselines_xml_start;
extern char _binary_baselines_xml_size;
}
static const char* xml_start = &_binary_baselines_xml_start;
const uint32_t xml_size = reinterpret_cast<uint32_t>(&_binary_baselines_xml_size);
When I step into this, the xml pointer is correct and I can see the xml text in the debugger. However, the size symbol shows the value as 0x132b (which is what I want) but it also indicates that "Address 0x132b is out of bounds". When I use the variable it is a very large incorrect random number. I've tried all sorts of other syntax to declare the extern variable such as char*, char[], int, int*, etc. The result is always the same. The value is there but I can't seem to get to it.
Another point of interest is that this code works fine on a windows machine without the prepended underscore on the extern variables but all else the same.
I can't seem to find much online about using objcopy in this manner so any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你的实际问题是什么。 *_size 符号是表示大小的绝对符号。您不应该能够实际引用该位置(除非意外),它只是将整数值偷偷放入链接器中而不实际定义数据变量的一种方法。您正在做的事情就您的使用方式而言是正确的。
考虑这个问题的最佳方法是如果您有以下代码:
唯一的区别是链接器通过符号为您填充 0x1234 值。
I am not sure what you actual issue is. The *_size symbol is an absolute symbol to indicate the size. You are not supposed to be able to actually reference the location (unless by accident) it is just a way of sneaking an integer value into the linker without actually defining a data variable. What you are doing is correct in how you are using it.
The best way to think about this would be if you had the following code:
The only difference is the linker fills in the 0x1234 value for you via a symbol.