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

编译带调试信息的ELF二进制后.debug_loc段为空的原因咨询

Why is the .debug_loc section empty in my compiled ELF?

First, let's clarify what the .debug_loc section is used for: it stores complex location expressions for variables whose position changes during the execution of a function (e.g., a variable that starts in a register, then gets spilled to the stack mid-function, or vice versa). When a variable's location is fixed for its entire lifetime, GCC doesn't need to use .debug_loc—instead, it embeds the fixed location directly in the .debug_info section via the DW_AT_location attribute.

Looking at your code and compilation setup:

  • You're using GCC 7.4.0 with -g (no optimizations, since -O0 is the default)
  • All variables in your code have fixed locations:
    • my_arg (function parameter) starts in the rdi register (x86_64 calling convention) and doesn't move
    • my_local and i are stack-allocated local variables, with fixed offsets from the base pointer (rbp) for their entire lifetimes

Since none of your variables have dynamic location changes, GCC has no reason to generate content for .debug_loc—this is completely normal behavior, not a bug or missing feature.

How to verify this?

You can check the variable locations directly in the .debug_info section using readelf:

readelf -wi scratch1.out | grep -A5 -B5 DW_AT_location

You'll see entries like this (example output for my_local):

<123>   DW_AT_name        : (indirect string, offset: 0x456): my_local
    <127>   DW_AT_decl_file   : 1
    <128>   DW_AT_decl_line   : 3
    <129>   DW_AT_type        : <0x789>
    <12d>   DW_AT_location    : 9 byte block: 91 10 00 00 00 00 00 00 00  (DW_OP_fbreg: -16)

The DW_OP_fbreg: -16 means my_local is always at -0x10(%rbp)—a fixed stack offset, so no need for .debug_loc.

When would .debug_loc get populated?

If you modify your code to create variables with dynamic location changes (e.g., enable optimizations with -O2, or write code that forces a variable to move between registers and stack), GCC will generate .debug_loc entries. For example, compile with -O2 and re-run objdump --dwarf=loc:

gcc -g -O2 scratch1.c -o scratch1_opt.out
objdump --dwarf=loc scratch1_opt.out

You should now see content in .debug_loc as variables are optimized to registers and spilled as needed.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:26:06