https://sourceware.org/bugzilla/show_bug.cgi?id=34757

            Bug ID: 34757
           Summary: gas: --gdwarf-cie-version=4 writes address size 8 in
                    the CIE on 32-bit targets
           Product: binutils
           Version: 2.48 (HEAD)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: gas
          Assignee: unassigned at sourceware dot org
          Reporter: martin.mirchev at u dot nus.edu
  Target Milestone: ---

With --gdwarf-cie-version=4, output_cie() writes the CIE address size as

      int address_size = dwarf2_format_32bit ? 4 : 8;

dwarf2_format_32bit is the enum constant 0, so this is always 8, also on
32-bit targets. For .debug_frame the FDEs are then emitted with
DWARF2_ADDR_SIZE (stdoutput) = 4 bytes per address while the CIE says 8,
so consumers misread the FDE ranges:

$ cat t.s
        .cfi_sections .debug_frame
        .text
f:
        .cfi_startproc
        nop
        nop
        .cfi_endproc
g:
        .cfi_startproc
        ret
        .cfi_endproc
$ as --32 --gdwarf-cie-version=4 t.s -o t.o && readelf --debug-dump=frames t.o
00000000 0000000000000010 ffffffff CIE
  Version:               4
  Augmentation:          ""
  Pointer Size:          8
  ...
00000014 000000000000000c 00000000 FDE cie=00000000
pc=0000000200000000..0000000200000000
00000024 000000000000000c 00000000 FDE cie=00000000
pc=0000000100000002..0000000100000002

With --gdwarf-cie-version=3 the same input gives pc=00000000..00000002 and
pc=00000002..00000003. The .eh_frame CIE is affected the same way
("Pointer Size: 8" for --32).

Suggested fix: use the address size the FDEs are written with.

--- a/gas/dw2gencfi.c
+++ b/gas/dw2gencfi.c
@@ -1962,7 +1962,7 @@
     {
       /* For now we are assuming a flat address space with 4 or 8 byte
          addresses.  */
-      int address_size = dwarf2_format_32bit ? 4 : 8;
+      int address_size = DWARF2_ADDR_SIZE (stdoutput);
       out_one (address_size);                  /* Address size.  */
       out_one (0);                             /* Segment size.  */
     }

With it, --32 and --x32 report "Pointer Size: 4" and the FDE ranges above
are decoded correctly; --64 still reports 8, and output for CIE versions 1
and 3 is unchanged. The existing cfi/cie-version-4.d test accepts any
pointer size, which is why it did not catch this; a 32-bit variant
expecting "Pointer Size: 4" would.

Seen with binutils 2.47; the same code is present on current master.

This issue was found by an LLM-Enhanced Static Analysis Engine and verified
manually.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to