Using DW_TAG_imported_unit for eliminating discarded section DWARF info

Senthil Kumar Selvaraj <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <[email protected]>
All,

  The DWARF 4 standard (section E.3.3) suggests using
  DW_TAG_imported_unit (together with section groups) to generate
  per function DWARF information in a way that's suitable for the linker
  to discard unused functions. My understanding of the standard is that
  there is one CU containing a DW_TAG_imported_unit generated for each 
  subprogram, with the DW_AT_import attribute pointing to the "main" CU 
  that contains file level declarations and definitions.
  
  However, I don't see gdb supporting usage of the tag for this purpose - I 
  only see it being used for referring *to* other units from a 
  DW_TAG_compile_unit, not the other way around. Is my finding correct,
  or am I missing something?

  FWIW, I managed to hack GCC into generating the following DWARF output if
  -ffunction-sections is turned on. gdb treats the CUs at offset 0x31 and 0x66 as full fledged CUs though,
  and eventually crashes when trying to resolve main (psymtab->symtab = NULL for the CU containing main).

Regards
Senthil

➜  ~  cat test2.c
int _exit(int code) { return 0; }
int main() { return 0; }

➜  ~  /scratch/arm/install/bin/arm-none-eabi-gcc test2.c -g3  -Wl,-Map=test.map -Wl,--gc-sections -ffunction-sections
test2.c:1:5: warning: conflicting types for built-in function '_exit' [enabled by default]
 int _exit(int code) { return 0; }
     ^
➜  ~  /scratch/arm/install/bin/arm-none-eabi-objdump -Wi a.out

a.out:     file format elf32-littlearm

Contents of the .debug_info section:

  Compilation Unit @ offset 0x0:
   Length:        0x2d (32-bit)
   Version:       4
   Abbrev Offset: 0x0
   Pointer Size:  4
 <0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <c>   DW_AT_producer    : (indirect string, offset: 0x1401): GNU C 4.9.0 20131226 (experimental) -g3 -ffunction-sections
    <10>   DW_AT_language    : 1    (ANSI C)
    <11>   DW_AT_name        : (indirect string, offset: 0xbb1): test2.c
    <15>   DW_AT_comp_dir    : (indirect string, offset: 0x23): /home/saaadhu
    <19>   DW_AT_ranges      : 0x0
    <1d>   DW_AT_low_pc      : 0x0
    <21>   DW_AT_stmt_list   : 0x0
    <25>   DW_AT_GNU_macros  : 0x0
 <1><29>: Abbrev Number: 4 (DW_TAG_base_type)
    <2a>   DW_AT_byte_size   : 4
    <2b>   DW_AT_encoding    : 5    (signed)
    <2c>   DW_AT_name        : int
 <1><30>: Abbrev Number: 0
  Compilation Unit @ offset 0x31:
   Length:        0x31 (32-bit)
   Version:       4
   Abbrev Offset: 0x0
   Pointer Size:  4
 <0><3c>: Abbrev Number: 6 (DW_TAG_imported_unit)
    <3d>   DW_AT_import      : <0xb>    [Abbrev Number: 1]
 <1><41>: Abbrev Number: 2 (DW_TAG_subprogram)
    <42>   DW_AT_external    : 1
    <42>   DW_AT_name        : (indirect string, offset: 0xcfa): _exit
    <46>   DW_AT_decl_file   : 1
    <47>   DW_AT_decl_line   : 1
    <48>   DW_AT_prototyped  : 1
    <48>   DW_AT_type        : <0x5a>
    <4c>   DW_AT_low_pc      : 0x8350
    <50>   DW_AT_high_pc     : 0x24
    <54>   DW_AT_frame_base  : 1 byte block: 9c     (DW_OP_call_frame_cfa)
    <56>   DW_AT_GNU_all_call_sites: 1
 <2><56>: Abbrev Number: 3 (DW_TAG_formal_parameter)
    <57>   DW_AT_name        : (indirect string, offset: 0x1d23): code
    <5b>   DW_AT_decl_file   : 1
    <5c>   DW_AT_decl_line   : 1
    <5d>   DW_AT_type        : <0x5a>
    <61>   DW_AT_location    : 2 byte block: 91 74  (DW_OP_fbreg: -12)
 <2><64>: Abbrev Number: 0
 <1><65>: Abbrev Number: 0
  Compilation Unit @ offset 0x66:
   Length:        0x22 (32-bit)
   Version:       4
   Abbrev Offset: 0x0
   Pointer Size:  4
 <0><71>: Abbrev Number: 6 (DW_TAG_imported_unit)
    <72>   DW_AT_import      : <0xb>    [Abbrev Number: 1]
 <1><76>: Abbrev Number: 5 (DW_TAG_subprogram)
    <77>   DW_AT_external    : 1
    <77>   DW_AT_name        : (indirect string, offset: 0x2106): main
    <7b>   DW_AT_decl_file   : 1
    <7c>   DW_AT_decl_line   : 2
    <7d>   DW_AT_type        : <0x8f>
    <81>   DW_AT_low_pc      : 0x8374
    <85>   DW_AT_high_pc     : 0x1c
    <89>   DW_AT_frame_base  : 1 byte block: 9c     (DW_OP_call_frame_cfa)
    <8b>   DW_AT_GNU_all_call_sites: 1
 <1><8b>: Abbrev Number: 0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.