Skip to content

Commit cc290a9

Browse files
committed
[BPF] Don't fail for static variables
Currently, the LLVM will print an error like Unsupported relocation: try to compile with -O2 or above, or check your static variable usage if user defines more than one static variables in a single ELF section (e.g., .bss or .data). There is ongoing effort to support static and global variables in libbpf and kernel. This patch removed the assertion so user programs with static variables won't fail compilation. The static variable in-section offset is written to the "imm" field of the corresponding to-be-relocated bpf instruction. Below is an example to show how the application (e.g., libbpf) can relate variable to relocations. -bash-4.4$ cat g1.c static volatile long a = 2; static volatile int b = 3; int test() { return a + b; } -bash-4.4$ clang -target bpf -O2 -c g1.c -bash-4.4$ llvm-readelf -r g1.o Relocation section '.rel.text' at offset 0x158 contains 2 entries: Offset Info Type Symbol's Value Symbol's Name 0000000000000000 0000000400000001 R_BPF_64_64 0000000000000000 .data 0000000000000018 0000000400000001 R_BPF_64_64 0000000000000000 .data -bash-4.4$ llvm-readelf -s g1.o Symbol table '.symtab' contains 6 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000000 0 FILE LOCAL DEFAULT ABS g1.c 2: 0000000000000000 8 OBJECT LOCAL DEFAULT 4 a 3: 0000000000000008 4 OBJECT LOCAL DEFAULT 4 b 4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 5: 0000000000000000 64 FUNC GLOBAL DEFAULT 2 test -bash-4.4$ llvm-objdump -d g1.o g1.o: file format ELF64-BPF Disassembly of section .text: 0000000000000000 test: 0: 18 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 r1 = 0 ll 2: 79 11 00 00 00 00 00 00 r1 = *(u64 *)(r1 + 0) 3: 18 02 00 00 08 00 00 00 00 00 00 00 00 00 00 00 r2 = 8 ll 5: 61 20 00 00 00 00 00 00 r0 = *(u32 *)(r2 + 0) 6: 0f 10 00 00 00 00 00 00 r0 += r1 7: 95 00 00 00 00 00 00 00 exit -bash-4.4$ . from symbol table, static variable "a" is in section #4, offset 0. . from symbol table, static variable "b" is in section #4, offset 8. . the first relocation is against symbol #4: 4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 and in-section offset 0 (see llvm-objdump result) . the second relocation is against symbol #4: 4: 0000000000000000 0 SECTION LOCAL DEFAULT 4 and in-section offset 8 (see llvm-objdump result) . therefore, the first relocation is for variable "a", and the second relocation is for variable "b". Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: Yonghong Song <[email protected]> llvm-svn: 354954
1 parent c016430 commit cc290a9

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
7272
bool IsResolved,
7373
const MCSubtargetInfo *STI) const {
7474
if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
75-
if (Value) {
76-
MCContext &Ctx = Asm.getContext();
77-
Ctx.reportError(Fixup.getLoc(),
78-
"Unsupported relocation: try to compile with -O2 or above, "
79-
"or check your static variable usage");
80-
}
75+
// The Value is 0 for global variables, and the in-section offset
76+
// for static variables. Write to the immediate field of the inst.
77+
assert(Value <= UINT32_MAX);
78+
support::endian::write<uint32_t>(&Data[Fixup.getOffset() + 4],
79+
static_cast<uint32_t>(Value),
80+
Endian);
8181
} else if (Fixup.getKind() == FK_Data_4) {
8282
support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
8383
} else if (Fixup.getKind() == FK_Data_8) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
; RUN: llc -march=bpfel -filetype=obj -o - %s | llvm-objdump -d - | FileCheck --check-prefix=CHECK %s
2+
; RUN: llc -march=bpfeb -filetype=obj -o - %s | llvm-objdump -d - | FileCheck --check-prefix=CHECK %s
3+
4+
; src:
5+
; static volatile long a = 2;
6+
; static volatile int b = 3;
7+
; int test() { return a + b; }
8+
@a = internal global i64 2, align 8
9+
@b = internal global i32 3, align 4
10+
11+
; Function Attrs: norecurse nounwind
12+
define dso_local i32 @test() local_unnamed_addr #0 {
13+
%1 = load volatile i64, i64* @a, align 8, !tbaa !2
14+
; CHECK: r1 = 0 ll
15+
; CHECK: r1 = *(u64 *)(r1 + 0)
16+
%2 = load volatile i32, i32* @b, align 4, !tbaa !6
17+
; CHECK: r2 = 8 ll
18+
; CHECK: r0 = *(u32 *)(r2 + 0)
19+
%3 = trunc i64 %1 to i32
20+
%4 = add i32 %2, %3
21+
; CHECK: r0 += r1
22+
ret i32 %4
23+
; CHECK: exit
24+
}
25+
26+
attributes #0 = { norecurse nounwind }
27+
28+
!llvm.module.flags = !{!0}
29+
!llvm.ident = !{!1}
30+
31+
!0 = !{i32 1, !"wchar_size", i32 4}
32+
!1 = !{!"clang version 8.0.20181009 "}
33+
!2 = !{!3, !3, i64 0}
34+
!3 = !{!"long", !4, i64 0}
35+
!4 = !{!"omnipotent char", !5, i64 0}
36+
!5 = !{!"Simple C/C++ TBAA"}
37+
!6 = !{!7, !7, i64 0}
38+
!7 = !{!"int", !4, i64 0}

0 commit comments

Comments
 (0)