Files
clang-p2996/llvm/test/DebugInfo/RISCV/relax-debug-frame.ll
Fangrui Song ffa829c4c5 [RISCV] Allow delayed decision for ADD/SUB relocations
For a label difference `A-B` in assembly, if A and B are separated by a
linker-relaxable instruction, we should emit a pair of ADD/SUB
relocations (e.g. R_RISCV_ADD32/R_RISCV_SUB32,
R_RISCV_ADD64/R_RISCV_SUB64).

However, the decision is made upfront at parsing time with inadequate
heuristics (`requiresFixup`). As a result, LLVM integrated assembler
incorrectly suppresses R_RISCV_ADD32/R_RISCV_SUB32 for the following
code:
```
// Simplified from a workaround https://android-review.googlesource.com/c/platform/art/+/2619609
// Both end and begin are not defined yet. We decide ADD/SUB relocations upfront and don't know they will be needed.
.4byte end-begin

begin:
  call foo
end:
```

To fix the bug, make two primary changes:

* Delete `requiresFixups` and the overridden emitValueImpl (from D103539).
  This deletion requires accurate evaluateAsAbolute (D153097).
* In MCAssembler::evaluateFixup, call handleAddSubRelocations to emit
  ADD/SUB relocations.

However, there is a remaining issue in
MCExpr.cpp:AttemptToFoldSymbolOffsetDifference. With MCAsmLayout, we may
incorrectly fold A-B even when A and B are separated by a
linker-relaxable instruction. This deficiency is acknowledged (see
D153097), but was previously bypassed by eagerly emitting ADD/SUB using
`requiresFixups`. To address this, we partially reintroduce `canFold` (from
D61584, removed by D103539).

Some expressions (e.g. .size and .fill) need to take the `MCAsmLayout`
code path in AttemptToFoldSymbolOffsetDifference, avoiding relocations
(weird, but matching GNU assembler and needed to match user
expectation). Switch to evaluateKnownAbsolute to leverage the `InSet`
condition.

As a bonus, this change allows for the removal of some relocations for
the FDE `address_range` field in the .eh_frame section.

riscv64-64b-pcrel.s contains the main test.
Add a linker relaxable instruction to dwarf-riscv-relocs.ll to test what
it intends to test.
Merge fixups-relax-diff.ll into fixups-diff.ll.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D155357
2023-07-21 08:37:58 -07:00

69 lines
2.1 KiB
LLVM

; RUN: llc -filetype=obj -mtriple=riscv32 -mattr=+relax %s -o %t.o
; RUN: llvm-readobj -r %t.o | FileCheck -check-prefix=RELAX %s
; RUN: llvm-dwarfdump --debug-frame %t.o 2>&1 \
; RUN: | FileCheck -check-prefix=RELAX-DWARFDUMP %s
;
; RELAX: Section ({{.*}}) .rela.eh_frame {
; RELAX-NEXT: 0x1C R_RISCV_32_PCREL <null> 0x0
; RELAX-NEXT: 0x30 R_RISCV_32_PCREL <null> 0x0
; RELAX-NEXT: 0x44 R_RISCV_32_PCREL <null> 0x0
; RELAX-NEXT: 0x48 R_RISCV_ADD32 <null> 0x0
; RELAX-NEXT: 0x48 R_RISCV_SUB32 <null> 0x0
; RELAX-NEXT: }
; RELAX-DWARFDUMP-NOT: error: failed to compute relocation
; RELAX-DWARFDUMP: FDE
; RELAX-DWARFDUMP-NEXT: Format:
; RELAX-DWARFDUMP: DW_CFA_advance_loc: 4
; RELAX-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset: +16
; RELAX-DWARFDUMP-EMPTY:
; RELAX-DWARFDUMP: FDE
; RELAX-DWARFDUMP: Format:
; RELAX-DWARFDUMP-NEXT: DW_CFA_advance_loc: 4
; RELAX-DWARFDUMP-NEXT: DW_CFA_def_cfa_offset: +16
; RELAX-DWARFDUMP-NEXT: DW_CFA_advance_loc: 4
; RELAX-DWARFDUMP-NEXT: DW_CFA_offset: X1 -4
; RELAX-DWARFDUMP-NEXT: DW_CFA_nop
; RELAX-DWARFDUMP-EMPTY:
source_filename = "frame.c"
; Function Attrs: noinline nounwind optnone
define i32 @init() {
entry:
ret i32 0
}
; Function Attrs: noinline nounwind optnone
define i32 @foo(i32 signext %value) {
entry:
%value.addr = alloca i32, align 4
store i32 %value, ptr %value.addr, align 4
%0 = load i32, ptr %value.addr, align 4
ret i32 %0
}
; Function Attrs: noinline nounwind optnone
define i32 @bar() {
entry:
%result = alloca i32, align 4
%v = alloca i32, align 4
%call = call i32 @init()
store i32 %call, ptr %v, align 4
%0 = load i32, ptr %v, align 4
%call1 = call i32 @foo(i32 signext %0)
store i32 %call1, ptr %result, align 4
%1 = load i32, ptr %result, align 4
ret i32 %1
}
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4, !5}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
!1 = !DIFile(filename: "line.c", directory: "./")
!2 = !{}
!3 = !{i32 2, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = !{i32 1, !"wchar_size", i32 4}