如何阻止GCC合并共享相同后缀的字符串文字? [英] How to stop GCC from combining string literals that share the same suffix?

查看:72
本文介绍了如何阻止GCC合并共享相同后缀的字符串文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC似乎在进行优化,在其中合并了共享相同后缀的字符串文字。

GCC seems to do an optimization where it combines string literals that share the same suffix.

例如,包含两个字符串文字<$ c $的C程序c> foo bar 和 bar 可以以最终的 ELF 形式结尾(1)字符串表更改为具有单个字符串文字 foo bar ,并且(2)更改了程序,以便任何指向 bar 转换为现在将 4 个字符指向字符串 foo bar 。请注意,从仅向前读取C程序的角度来看,第二个文字仍然看起来像 bar

For example a C program that contains the two string literals "foo bar" and "bar" can end up in final ELF form (1) the string table changed to have a single string literal "foo bar" and (2) the program changed so that any pointer to "bar" is converted to now point 4 characters into the string "foo bar". Note that, from the point of view of a C program reading only forward, the second literal will still look like "bar".

虽然我认为这是一种压缩字符串集合而不增加 ELF 文件格式复杂度的聪明方案,但出于深奥的原因,这也给我带来了麻烦(处理 ELF 文件并对其进行各种分析)。如何关闭GCC的此功能?

While I think this is a clever scheme for compressing a collection of strings without adding complexity to the format of an ELF file, for esoteric reasons it is also causing me problems (while post-processing ELF files and doing various analyses on them). How do I turn off this feature of GCC?

推荐答案

ELF字符串表由汇编器和链接编辑器构建,因此它不是GCC问题,而是与binutils相关。字符串表合并是在binutils 2.26中引入的:

The ELF string table is built by the assembler and link editor, so it is not a GCC matter, but rather binutils-related. String table merging was introduced in binutils 2.26:

  • Inefficient .strtab implementation
  • Use strtab with GC and suffix merging for .strtab

这会引起偶发性问题,例如在构建powerpc64内核模块时

This caused occasional issues, for example when building powerpc64 kernel modules.

不幸的是,我不知道在基于BFD的链接器中禁用字符串表合并的方法( ld.bfd )。

Unfortunately, I'm not aware of a way to disable string table merging in the BFD-based linker (ld.bfd).

不过,黄金( ld.gold (也是binutils的一部分)仅在优化时( -O2 及更高版本)执行字符串表合并;请注意,这是一个链接器 f落后)。如果binutils汇编器合并了字符串表项,它将再次复制它们。这意味着,如果您的项目与gold链接程序兼容,则可以使用它来解决此问题。

However, gold (ld.gold, also part of binutils) only performs string table merging when optimizing (at -O2 and higher; be aware that this is a linker flag). If the binutils assembler merged the string table entries, it will duplicate them again. This means that if your project is compatible with the gold linker, you can use it to address this problem.

这篇关于如何阻止GCC合并共享相同后缀的字符串文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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