本文介绍了objcopy把prepends目录路径名符号名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑使用 objcopy把来包括文本文件转换成可执行的二进制形式。 (在运行时我需要的文件作为字符串)。直到连接器需要找到符号名引用这工作得很好。问题是, objcopy把 $ P $的路径名的文件ppends符号名。由于我利用GNU autotools寄送包裹这prepended路径变化,我不知道在C / C ++程序使用什么样的外部链接符号。

I am tying to use objcopy to include a binary form of a text file into an executable. (At runtime I need the file as a string). This works fine until the linker needs to find the references from the symbol names. The problem is that objcopy prepends the symbol names with the pathname to the file. Since I am using GNU Autotools to ship the package this prepended pathname changes and I don't know what external linker symbol to use in the C/C++ program.

nm libtest.a |grep textfile
textfile.o:
00001d21 D _binary__home_git_textfile_end
00001d21 A _binary__home_git_textfile_size
00000000 D _binary__home_git_textfile_start

libtest.a 与(摘自Makefile.am)制作:

libtest.a was produced with (extract from Makefile.am):

SUFFIXES = .txt
.txt.$(OBJEXT):
    objcopy --input binary --output elf32-i386 --binary-architecture i386 $< $@

我怎么能告诉 objcopy把来只有我们的文件名作为连接符号的干?还是有解决此问题的另一种方式?

How can I tell objcopy to only us the stem of the filename as linker symbols? Or is there another way around the problem?

推荐答案

包括原始数据转化为ELF的通用方法是通过的汇编指令。

Generic method of including raw data into ELF is supported by .incbin assembler directive.

关键是要创建模板.s文件可能看起来像这样的:

The trick is to create template .S file that could look like this:

        .global foo_start
foo_start:
        .incbin "foo.raw"

        .global foo_end
foo_end:

此文件是通过CPP pprocessed $ P $,所以我们不必硬code文件名存在,例如。我们可以这样写:

This file is preprocessed via cpp so we don't have to hardcode file name there, eg. we can write:

        .incbin __raw_file_path__

...然后通过它,而编译:

... and then pass it while compiling:

gcc -D__raw_file_path__='"data/foo.png"' foo.S -c -o data/foo.o

最后,因为我们prepare .s文件我们自己,我们可以添加一些额外的数据和/或信息。如果包括原始文本文件,并希望这些可用的C字符串可以加0字节刚过原始数据:

Lastly, as we prepare .S file ourself we can add some extra data and/or information. If you include raw "text files" and want these to be available as C strings you can add '0' byte just after raw data:

        .global foo_start
foo_start:
        .incbin "foo.raw"

        .global foo_end
foo_end:
        .byte 0

        .global foo_size
foo_size:
        .int foo_end - foo_start

如果你想全面的灵活性,你当然可以pre-进程文件的手动更改的任何部分,例如:

If you want full-blown flexibility, you can of course pre-process file manually to alter any part of it, eg.

.global @sym@_start
@sym@_start:
       .incbin "@file@"
       .global @sym@_end
@sym@_end:

...然后编译:

... and then compile it:

sed -e "s,@sym@,passwd,g" -e "s,@file@,/etc/passwd," <foo.S.in | gcc -x assembler-with-cpp - -o passwd.o -c

这篇关于objcopy把prepends目录路径名符号名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:04