<div dir="ltr">








    
    <a class="gmail-js-post-issue gmail-flex--item gmail-s-btn gmail-s-btn__unset gmail-c-pointer gmail-py6 gmail-mx-auto" href="https://stackoverflow.com/posts/77169835/timeline" aria-label="Timeline" aria-describedby="--stacks-s-tooltip-6rzypq33"></a><div class="gmail-votecell gmail-post-layout--left"><div class="gmail-js-voting-container gmail-d-flex gmail-jc-center gmail-fd-column gmail-ai-stretch gmail-gs4 gmail-fc-black-200">

</div>

        </div>

        

<div class="gmail-postcell gmail-post-layout--right">
    
    <div class="gmail-s-prose gmail-js-post-body">
                
<p>I am trying to implement a dwarf parser in C++ without using any 
external dependencies. As mentioned in dwarf5 standard, debug info first
 4 bytes or 12 bytes denotes the unit length
Basically this:</p>
<pre class="gmail-lang-none gmail-s-code-block"><code>unit_length (initial length)
A 4-byte or 12-byte unsigned integer representing the length of the3
.debug_info contribution for that compilation unit, not including the length field itself. In the 32-bit DWARF format, this is a 4-byte unsigned integer (which must be less than 0xfffffff0); in the 64-bit DWARF format, this consists of the 4-byte value 0xffffffff followed by an 8-byte unsigned integer that gives the actual length (see Section 7.4 on page 196).
</code></pre>
<p>When I am dumping the .debug_info section hexadecimally using objdump I am getting this(see readelf output below).</p>
<p>objdump -s -j .debug_info hello.o</p>
<p>hello.o:     file format elf64-x86-64</p>
<p>Contents of section .debug_info:</p>
<pre class="gmail-lang-none gmail-s-code-block"><code> 0000 01000000 00000000 9a000000 00000000  ................  
 0010 01000000 00000000 789c9bc6 c0c0c0ca  ........x.......  
 0020 c0c801a4 18984084 2c031a10 42623372  ......@.,...Bb3r  
 0030 b0832916 0805d1c6 c804e5b1 4178ac20  ..).........Ax.  
 0040 8a998535 33af04a8 8115498e 05aa2002  ...53.....I... .  
 0050 8bf18c73 58131918 99394172 4c137318  ...sX....9ArL.s.  
 0060 180011e5 0560  
</code></pre>
<p>So according to this, the length should be 0x01000000, but the actual length is 0x96.(see readelf output below)<br>
readelf -wi hello.o<br>
Contents of the .debug_info section:</p>
<p>Compilation Unit @ offset 0:<br>
Length:        0x96 (32-bit)<br>
Version:       5
Unit Type:     DW_UT_compile (1)
Abbrev Offset: 0<br>
Pointer Size:  8</p>
<p>I know I am missing something basic, but even after reading the standards
 for many times. I am unable to find my mistake. One more thing, I 
searched for some basic dwarf parsers so that I could understand what they 
are doing but could not find any. All of the parsers were big 
libraries, which was difficult to understand. If any of you can at least 
provide some readable and understandable parser code, It will be helpful 
too.</p><p>ELF header dump:</p><p>ELF Header:<br>  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00<br>  Class:                             ELF64<br>  Data:                              2's complement, little endian<br>  Version:                           1 (current)<br>  OS/ABI:                            UNIX - System V<br>  ABI Version:                       0<br>  Type:                              REL (Relocatable file)<br>  Machine:                           Advanced Micro Devices X86-64<br>  Version:                           0x1<br>  Entry point address:               0x0<br>  Start of program headers:          0 (bytes into file)<br>  Start of section headers:          3048 (bytes into file)<br>  Flags:                             0x0<br>  Size of this header:               64 (bytes)<br>  Size of program headers:           0 (bytes)<br>  Number of program headers:         0<br>  Size of section headers:           64 (bytes)</p>
    </div></div></div>