简体   繁体   English

有没有办法重命名由 gcc 编译的 .o 或 .a 文件中的节名称?

[英]Is there any way to rename the section name in a .o or .a file compiled by gcc?

I have a precompiled library compiled with a gcc-based compiler and I would like to move the functions from the default .text section to some other section name (let's say foo ).我有一个使用基于 gcc 的编译器编译的预编译库,我想将函数从默认的.text部分移动到其他一些部分名称(假设foo )。 Is there a way to do this using binutils without recompiling?有没有办法在不重新编译的情况下使用 binutils 做到这一点?

.a library or static library is nothing but collection of object files only. .a库或静态库只是对象文件的集合。

So before linking you can use objcpy rename command to change name of your sections. 因此,在链接之前,您可以使用objcpy rename命令更改部分的名称。 Similarly you can change symbol name also using " -redefine-sym" 同样,您也可以使用“-redefine-sym”更改符号名称

--rename-section oldname=newname[,flags] Rename a section from oldname to newname, optionally changing the section's flags to flags in the process. --rename-section oldname = newname [,flags]将一个节从oldname重命名为newname,可选择将节的标志更改为进程中的标志。 This has the advantage over using a linker script to perform the rename in that the output stays as an object file and does not become a linked executable. 这比使用链接描述文件执行重命名的优点在于输出保留为目标文件而不会成为链接可执行文件。 Example:- 例:-

                 objcopy -I binary -O <output_format> -B <architecture> \
                  --rename-section .data=.rodata,alloc,load,readonly,data,contents \
                  <input_binary_file> <output_object_file>

Refer: http://man7.org/linux/man-pages/man1/objcopy.1.html 参考: http//man7.org/linux/man-pages/man1/objcopy.1.html

Linker Script链接脚本

You can also us ld to rename sections using a linker script.您还可以使用ld使用链接描述文件重命名部分。

ld -r -T section_rename.xsc -o output.o input.o

Where the section_rename.xsc might look similar to the following:其中section_rename.xsc可能类似于以下内容:

SECTIONS
{
  .newtext : { *(.text) }
  .newdata : { *(.data) }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM