https://sourceware.org/bugzilla/show_bug.cgi?id=35042
Bug ID: 35042
Summary: ARM: .ARM.exidx entries corrupted when relocated RAM
code LMA order differs from VMA order (VMA merge vs.
SHF_LINK_ORDER LMA sort)
Product: binutils
Version: 2.48 (HEAD)
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: ld
Assignee: unassigned at sourceware dot org
Reporter: nicholasewalt at waymo dot com
Target Milestone: ---
When linking ARM EABI objects with `-funwind-tables` (or `-fexceptions`) and a
relocated code section whose Flash LMA order differs from its runtime VMA order
(e.g. `.text.ram` placed `> RAM AT> FLASH` between `.text.startup` and
`.text`), GNU `ld` silently corrupts `.ARM.exidx` in two ways:
1. `gld..._after_allocation` (`ld/emultempl/armelf.em`) sorts text sections by
**VMA** (`compare_output_sec_vma`) and calls `elf32_arm_fix_exidx_coverage`
(`bfd/elf32-arm.c`), which elides duplicate inline `.ARM.exidx` entries across
sections that are adjacent in VMA order and appends `EXIDX_CANTUNWIND` to the
highest-VMA section.
2. Since Binutils 2.34 (commit changing `SHF_LINK_ORDER` sorting from VMA to
LMA), `.ARM.exidx` input sections are subsequently re-sorted by **LMA**.
As a result:
- `.ARM.exidx` is emitted in LMA order rather than runtime VMA order, breaking
`libgcc`'s `__gnu_Unwind_Find_exidx` binary search (ARM EHABI32 requires
`.ARM.exidx` to be sorted by runtime function address).
- Worse, entries elided in VMA order during Pass 1 end up adjacent to
completely different sections after Pass 2's LMA sort, causing functions to
silently inherit the wrong unwind opcodes or `EXIDX_CANTUNWIND` (unless
`-Wl,--no-merge-exidx-entries` is passed).
### Minimal Reproducer
`repro.c`:
```c
void __aeabi_unwind_cpp_pr0(void) {}
void __aeabi_unwind_cpp_pr1(void) {}
volatile int sink;
// VMA 0x08040200, LMA 0x08040200: inline unwind 0x80aab0b0 (pop {r4-r6, lr})
__attribute__((section(".text.startup"), noinline))
void startup_fn(int a) {
__asm__ volatile("bl text_fn2" ::: "r4", "r5", "r6", "lr", "memory");
sink = a;
}
// VMA 0x20050000, LMA 0x08040240: inline unwind 0x80a8b0b0 (pop {r4, lr})
__attribute__((section(".text.ram.r1"), noinline))
void ram_fn1(int a) {
__asm__ volatile("nop" ::: "r4", "lr", "memory");
sink = a + 3;
}
// VMA 0x08042000, LMA 0x08042000: inline unwind 0x80aab0b0 (matches startup_fn
in VMA order)
__attribute__((section(".text.aaa_first"), noinline))
void text_fn1(int a) {
__asm__ volatile("bl text_fn2" ::: "r4", "r5", "r6", "lr", "memory");
sink = a + 1;
}
// VMA 0x08042020, LMA 0x08042020: inline unwind 0x80a8b0b0 (matches ram_fn1 in
VMA order)
__attribute__((section(".text.zzz_last"), noinline))
void text_fn2(int a) {
__asm__ volatile("nop" ::: "r4", "lr", "memory");
sink = a + 2;
}
void (*volatile fn_ptr)(int) = ram_fn1;
__attribute__((section(".text.mmm"), noinline))
void _start(void) { startup_fn(1); text_fn1(2); text_fn2(3); fn_ptr(4); }
```
`repro.ld`:
```ld
MEMORY {
FLASH (rx) : ORIGIN = 0x08040000, LENGTH = 128K
RAM (rwx) : ORIGIN = 0x20050000, LENGTH = 64K
}
SECTIONS {
.text.startup 0x08040200 : { *(.text.startup*) } > FLASH
.text.ram : ALIGN(64) { *(.text.ram*) . = ALIGN(64); } > RAM AT> FLASH
.text 0x08042000 : { *(.text.aaa_first*) *(.text*) } > FLASH
.ARM.exidx : ALIGN(8) { *(.ARM.exidx*) } > FLASH
.ARM.extab : ALIGN(8) { *(.ARM.extab*) } > FLASH
}
```
### Steps to Reproduce
```sh
arm-none-eabi-gcc -mcpu=cortex-m33 -mthumb -O2 -ffunction-sections \
-funwind-tables -nostdlib -T repro.ld repro.c -o repro.elf
arm-none-eabi-readelf -u repro.elf
```
### Actual Output (GNU ld 2.41 / arm-none-eabi-gcc 13.2.1)
```text
Unwind section '.ARM.exidx' at offset 0x2058 contains 4 entries:
0x8040200 <startup_fn>: 0x80aab0b0
Compact model index: 0
0xaa pop {r4, r5, r6, r14}
0xb0 finish
0xb0 finish
0x20050010 <ram_fn1+0x10>: 0x1 [cantunwind]
0x8042014 <__aeabi_unwind_cpp_pr0>: 0x80b0b0b0
...
0x8042020 <text_fn2>: 0x80a8b0b0
```
- `ram_fn1` (`0x20050000`, `pop {r4, lr}`) was elided against `text_fn2` in VMA
order, then moved after `startup_fn` (`pop {r4-r6, lr}`) in LMA order.
- `text_fn1` (`0x08042000`, `pop {r4-r6, lr}`) was elided against `startup_fn`
in VMA order, then preceded by `0x20050010: 0x1 [cantunwind]` in LMA order.
- Furthermore, `0x20050010` sits in the middle of `0x0804xxxx` entries, so
`.ARM.exidx` is not sorted by VMA.
--
You are receiving this mail because:
You are on the CC list for the bug.