Skip to content

Commit 8fd1bf2

Browse files
[BPF] Remove unused weak symbol __bpf_trap (#166003)
Nikita Popov reported an issue ([1]) where a dangling weak symbol __bpf_trap is in the final binary and this caused libbpf failing like below: $ veristat -v ./t.o Processing 't.o'... libbpf: elf: skipping unrecognized data section(4) .eh_frame libbpf: elf: skipping relo section(5) .rel.eh_frame for section(4) .eh_frame libbpf: failed to find BTF for extern '__bpf_trap': -3 Failed to open './t.o': -3 In llvm, the dag selection phase generates __bpf_trap in code. Later the UnreachableBlockElim pass removed __bpf_trap from the code, but __bpf_trap symbol survives in the symbol table. Having a dangling __bpf_trap weak symbol is not good for old kernels as seen in the above veristat failure. Although users could use compiler flag `-mllvm -bpf-disable-trap-unreachable` to workaround the issue, this patch fixed the issue by removing the dangling __bpf_trap. [1] #165696
1 parent d200df0 commit 8fd1bf2

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

llvm/lib/Target/BPF/BPFAsmPrinter.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ bool BPFAsmPrinter::doFinalization(Module &M) {
8888
}
8989
}
9090

91+
for (GlobalObject &GO : M.global_objects()) {
92+
if (!GO.hasExternalWeakLinkage())
93+
continue;
94+
95+
if (!SawTrapCall && GO.getName() == BPF_TRAP) {
96+
GO.eraseFromParent();
97+
break;
98+
}
99+
}
100+
91101
return AsmPrinter::doFinalization(M);
92102
}
93103

@@ -160,6 +170,20 @@ bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
160170
}
161171

162172
void BPFAsmPrinter::emitInstruction(const MachineInstr *MI) {
173+
if (MI->isCall()) {
174+
for (const MachineOperand &Op : MI->operands()) {
175+
if (Op.isGlobal()) {
176+
if (const GlobalValue *GV = Op.getGlobal())
177+
if (GV->getName() == BPF_TRAP)
178+
SawTrapCall = true;
179+
} else if (Op.isSymbol()) {
180+
if (const MCSymbol *Sym = Op.getMCSymbol())
181+
if (Sym->getName() == BPF_TRAP)
182+
SawTrapCall = true;
183+
}
184+
}
185+
}
186+
163187
BPF_MC::verifyInstructionPredicates(MI->getOpcode(),
164188
getSubtargetInfo().getFeatureBits());
165189

llvm/lib/Target/BPF/BPFAsmPrinter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class BPFAsmPrinter : public AsmPrinter {
3939
private:
4040
BTFDebug *BTF;
4141
TargetMachine &TM;
42+
bool SawTrapCall = false;
4243

4344
const BPFTargetMachine &getBTM() const;
4445
};

llvm/test/CodeGen/BPF/bpf_trap.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; RUN: llc < %s | FileCheck %s
2+
;
3+
target triple = "bpf"
4+
5+
define i32 @test(i8 %x) {
6+
entry:
7+
%0 = and i8 %x, 3
8+
switch i8 %0, label %default.unreachable4 [
9+
i8 0, label %return
10+
i8 1, label %sw.bb1
11+
i8 2, label %sw.bb2
12+
i8 3, label %sw.bb3
13+
]
14+
15+
sw.bb1: ; preds = %entry
16+
br label %return
17+
18+
sw.bb2: ; preds = %entry
19+
br label %return
20+
21+
sw.bb3: ; preds = %entry
22+
br label %return
23+
24+
default.unreachable4: ; preds = %entry
25+
unreachable
26+
27+
return: ; preds = %entry, %sw.bb3, %sw.bb2, %sw.bb1
28+
%retval.0 = phi i32 [ 12, %sw.bb1 ], [ 43, %sw.bb2 ], [ 54, %sw.bb3 ], [ 32, %entry ]
29+
ret i32 %retval.0
30+
}
31+
32+
; CHECK-NOT: __bpf_trap

0 commit comments

Comments
 (0)