Search code examples
gccassemblydebug-symbolssections

Using compile flag -ffunction-sections with debug symbols


I am compiling a C file using the gcc flag -ffunction-sections, to move every function into it's own section. The assembler is throwing the error:

job_queue.s:2395: Error: operation combines symbols in different segments

The compiler's assembly output at line 2395 is given here:

.section    .debug_ranges,info
.Ldebug_ranges0:
.4byte  .LBB7-.Ltext0

The symbol LBB7 is in the function (and thus the section) named ".text.add_event_handler" The symbol Ltext0 is in the (otherwise empty) section named: ".text"

GCC --version gives:

pic30-elf-gcc.exe (GCC) 4.0.3 (dsPIC30, Microchip v3_30) (B) Build date: Jun 29 2011

If I use the compiler flag -g0 (to turn off debug info) everything compiles and runs perfectly.

My question: Is this GCC output clearly wrong? It seems to me that GCC should have calculated the symbol LBB7's offset from the beginning of the .add_even_handler section instead of the .text section.

I suspect I am misunderstanding something because I cannot find anyone having the same difficulty on the Google.


Solution

  • The GCC output is definitely wrong. Perhaps it's fixed in newer GCC versions. If you can't upgrade you compiler, try compiling with -gdwarf-2 or, failing that, with -gdwarf-2 -gstrict-dwarf (for -gstrict-dwarf you'll have to upgrade the compiler too).

    What this option does is to instruct GCC to generate (strict) DWARF2, which does not include non-contiguous address ranges support, introduced in DWARF3.

    Of course, this may degrade the debugging information quality somewhat, YMMV.