Skip to content

Commit 589f1c4

Browse files
committed
Merging r369199:
------------------------------------------------------------------------ r369199 | yhs | 2019-08-18 00:12:00 +0200 (Sun, 18 Aug 2019) | 26 lines [BPF] Fix bpf llvm-objdump issues. Commit https://reviews.llvm.org/D57939 ("[DWARF] Refactor RelocVisitor and fix computation of SHT_RELA-typed relocation entries) made a change for relocation resolution when operating on an object file. The change unfortunately broke BPF as given SymbolValue (S) and Addent (A), previously relocation is resolved to S + A and after the change, it is resolved to S This patch fixed the issue by resolving relocation correctly. It looks not all relocation resolution reaches here and I did not trace down exactly when. But I do find if the object file includes codes in two different ELF sections than default ".text", the above bug will be triggered. This patch included a trivial two function source code to demonstrate this issue. The relocation for .debug_loc is resolved incorrectly due to this and llvm-objdump cannot display source annotated assembly. Differential Revision: https://reviews.llvm.org/D66372 ------------------------------------------------------------------------ llvm-svn: 369359
1 parent 356a2b3 commit 589f1c4

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

llvm/lib/Object/RelocationResolver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ static bool supportsBPF(uint64_t Type) {
9090
static uint64_t resolveBPF(RelocationRef R, uint64_t S, uint64_t A) {
9191
switch (R.getType()) {
9292
case ELF::R_BPF_64_32:
93-
return S & 0xFFFFFFFF;
93+
return (S + A) & 0xFFFFFFFF;
9494
case ELF::R_BPF_64_64:
95-
return S;
95+
return S + A;
9696
default:
9797
llvm_unreachable("Invalid relocation type");
9898
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
; RUN: llc -march=bpfel -filetype=obj -o - %s | llvm-objdump -S - | FileCheck %s
2+
; RUN: llc -march=bpfeb -filetype=obj -o - %s | llvm-objdump -S - | FileCheck %s
3+
;
4+
; Source code:
5+
; __attribute__((section("s1")))
6+
; int func1(int a) {
7+
; return a * a;
8+
; }
9+
; __attribute__((section("s2")))
10+
; int func2(int a) {
11+
; return a * a * a;
12+
; }
13+
; Compiler flag to generate IR:
14+
; clang -target bpf -S -gdwarf-5 -gembed-source -emit-llvm -g -O2 bug.c
15+
16+
; Function Attrs: norecurse nounwind readnone
17+
define dso_local i32 @func1(i32 %a) local_unnamed_addr #0 section "s1" !dbg !7 {
18+
entry:
19+
; CHECK: func1:
20+
call void @llvm.dbg.value(metadata i32 %a, metadata !12, metadata !DIExpression()), !dbg !13
21+
%mul = mul nsw i32 %a, %a, !dbg !14
22+
ret i32 %mul, !dbg !15
23+
; CHECK: ; return a * a;
24+
}
25+
26+
; Function Attrs: norecurse nounwind readnone
27+
define dso_local i32 @func2(i32 %a) local_unnamed_addr #0 section "s2" !dbg !16 {
28+
entry:
29+
; CHECK: func2:
30+
call void @llvm.dbg.value(metadata i32 %a, metadata !18, metadata !DIExpression()), !dbg !19
31+
%mul = mul nsw i32 %a, %a, !dbg !20
32+
%mul1 = mul nsw i32 %mul, %a, !dbg !21
33+
ret i32 %mul1, !dbg !22
34+
; CHECK: ; return a * a * a;
35+
}
36+
37+
; Function Attrs: nounwind readnone speculatable
38+
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
39+
40+
attributes #0 = { norecurse nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
41+
attributes #1 = { nounwind readnone speculatable }
42+
43+
!llvm.dbg.cu = !{!0}
44+
!llvm.module.flags = !{!3, !4, !5}
45+
!llvm.ident = !{!6}
46+
47+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 9.0.0 (trunk 366422) (llvm/trunk 366423)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
48+
!1 = !DIFile(filename: "bug.c", directory: "/tmp/home/yhs/work/tests/llvm/reloc", checksumkind: CSK_MD5, checksum: "c7c9938d4e6989ca33db748213aab194", source: "__attribute__((section(\22s1\22)))\0Aint func1(int a) {\0A return a * a;\0A}\0A__attribute__((section(\22s2\22)))\0Aint func2(int a) {\0A return a * a * a;\0A}\0A")
49+
!2 = !{}
50+
!3 = !{i32 2, !"Dwarf Version", i32 5}
51+
!4 = !{i32 2, !"Debug Info Version", i32 3}
52+
!5 = !{i32 1, !"wchar_size", i32 4}
53+
!6 = !{!"clang version 9.0.0 (trunk 366422) (llvm/trunk 366423)"}
54+
!7 = distinct !DISubprogram(name: "func1", scope: !1, file: !1, line: 2, type: !8, scopeLine: 2, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !11)
55+
!8 = !DISubroutineType(types: !9)
56+
!9 = !{!10, !10}
57+
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
58+
!11 = !{!12}
59+
!12 = !DILocalVariable(name: "a", arg: 1, scope: !7, file: !1, line: 2, type: !10)
60+
!13 = !DILocation(line: 0, scope: !7)
61+
!14 = !DILocation(line: 3, column: 14, scope: !7)
62+
!15 = !DILocation(line: 3, column: 5, scope: !7)
63+
!16 = distinct !DISubprogram(name: "func2", scope: !1, file: !1, line: 6, type: !8, scopeLine: 6, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !17)
64+
!17 = !{!18}
65+
!18 = !DILocalVariable(name: "a", arg: 1, scope: !16, file: !1, line: 6, type: !10)
66+
!19 = !DILocation(line: 0, scope: !16)
67+
!20 = !DILocation(line: 7, column: 14, scope: !16)
68+
!21 = !DILocation(line: 7, column: 18, scope: !16)
69+
!22 = !DILocation(line: 7, column: 5, scope: !16)

0 commit comments

Comments
 (0)