You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何每个成员函数拥有独立.text.*段?附代码示例与疑问

Why Does MyClass::doStuff End Up in Its Own Section?

Great question! Let's unpack what's happening here and how to adjust this behavior.

What's Causing the Separate Section?

The standalone section named .text._ZN7MyClass7doStuffEi comes from GCC's function sections feature, enabled by the -ffunction-sections compiler flag. Even though you used -O0 (no optimization), this flag might be enabled by default in your environment—some Linux distributions or build configurations turn it on automatically, especially for targets that benefit from link-time optimizations.

This feature places every function (including class member functions like doStuff) into its own dedicated .text.<mangled-symbol> section instead of merging all code into the main .text section. The key benefits here are:

  • Link-time garbage collection: The linker can strip out unused functions entirely, reducing the final executable's size.
  • Optimized code alignment: Individual functions can be aligned perfectly for your CPU architecture.
  • Better dynamic linking support: It aids with tasks like lazy binding or code patching in shared libraries.

As you correctly pointed out, this isn't strictly required for linking—symbol matching works regardless of which section the code lives in. The main .text section would function perfectly fine for basic use cases.

How to Disable This Behavior?

To force GCC to put all function code into the main .text section (and eliminate the separate per-function sections), add the -fno-function-sections flag to your compile command:

g++ -c -O0 -fno-function-sections blah.cpp && objdump -h blah.o

After running this, you'll see MyClass::doStuff's code is now part of the main .text section, and the .text._ZN7MyClass7doStuffEi section will no longer appear in the objdump output.

For completeness, there's a corresponding -fdata-sections flag that places global variables into individual data sections—you can disable that with -fno-data-sections if needed, though it doesn't apply to your current example.

内容的提问来源于stack exchange,提问作者PizzaBeer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:02:52