使用 GNU ld 链接器脚本包含二进制文件 [英] Include binary file with GNU ld linker script

查看:19
本文介绍了使用 GNU ld 链接器脚本包含二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可用的链接器脚本.我想添加另一个数据部分,其内容是直接从文件中提取的(ld 不应该解析它并提取这些部分等等).我该怎么做?

I have a working linker script. I want to add another data section whose contents is pulled directly from a file (ld shouldn't parse it and extract the sections and so on). How can I do that?

OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
SECTIONS
{
  .text 0x100000 : {
    *(.multiboot)
    *(.text)
    *(.code)
    *(.rodata*)
  }
  .data : {
    *(.data)
  }
  .bss : {
    *(.bss)
  }
  kernel_end = .;
  roottask_start = .;
  .data : {

    HERE I WANT TO INCLUDE THE ENTIRE CONTENTS OF ANOTHER (BINARY) FILE

  }
  roottask_end = .;
}

推荐答案

您可以尝试使用 objcopy 将其转换为可以链接的普通对象,然后在链接器脚本中引用其符号就像你对普通物体所做的那样.来自 objcopy 手册页:

You could try using objcopy to convert it to a normal object you can link in, and then reference its symbols in the linker script like you would do to a normal object. From the objcopy manual page:

-B bfdarch--二进制架构=bfdarch将原始二进制输入文件转换为对象时很有用文件.在这种情况下,输出架构可以设置为 bfdarch.如果输入文件具有已知的 bfdarch,则此选项将被忽略.您可以通过引用由转换过程创建的特殊符号.这些符号被称为 _binary_objfile_start_binary_objfile_end_binary_objfile_size.例如您可以将图片文件转换为一个目标文件,然后使用这些符号在您的代码中访问它.

-B bfdarch --binary-architecture=bfdarch Useful when transforming a raw binary input file into an object file. In this case the output architecture can be set to bfdarch. This option will be ignored if the input file has a known bfdarch. You can access this binary data inside a program by referencing the special symbols that are created by the conversion process. These symbols are called _binary_objfile_start, _binary_objfile_end and _binary_objfile_size. e.g. you can transform a picture file into an object file and then access it in your code using these symbols.

...其中 objfile 将扩展为输入对象文件的名称.

...where objfile will be expanded to the name of the input object file.

另见 --rename-section 选项.

这篇关于使用 GNU ld 链接器脚本包含二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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