Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, BPF only contains three relocations: R_BPF_NONE for no relocation R_BPF_64_64 for LD_imm64 and normal 64-bit data relocation R_BPF_64_32 for call insn and normal 32-bit data relocation Also .BTF and .BTF.ext sections contain symbols in allocated program and data sections. These two sections reserved 32bit space to hold the offset relative to the symbol's section. When LLVM JIT is used, the LLVM ExecutionEngine RuntimeDyld may attempt to resolve relocations for .BTF and .BTF.ext, which we want to prevent. So we used R_BPF_NONE for such relocations. This all works fine until when we try to do linking of multiple objects. . R_BPF_64_64 handling of LD_imm64 vs. normal 64-bit data is different, so lld target->relocate() needs more context to do a correct job. . The same for R_BPF_64_32. More context is needed for lld target->relocate() to differentiate call insn vs. normal 32-bit data relocation. . Since relocations in .BTF and .BTF.ext are set to R_BPF_NONE, they will not be relocated properly when multiple .BTF/.BTF.ext sections are merged by lld. This patch intends to address this issue by adding additional relocation kinds: R_BPF_64_ABS64 for normal 64-bit data relocation R_BPF_64_ABS32 for normal 32-bit data relocation R_BPF_64_NODYLD32 for .BTF and .BTF.ext style relocations. The old R_BPF_64_{64,32} semantics: R_BPF_64_64 for LD_imm64 relocation R_BPF_64_32 for call insn relocation The existing R_BPF_64_64/R_BPF_64_32 mapping to numeric values is maintained. They are the most common use cases for bpf programs and we want to maintain backward compatibility as much as possible. ExecutionEngine RuntimeDyld BPF relocations are adjusted as well. R_BPF_64_{ABS64,ABS32} relocations will be resolved properly and other relocations will be ignored. Two tests are added for RuntimeDyld. Not handling R_BPF_64_NODYLD32 in RuntimeDyldELF.cpp will result in "Relocation type not implemented yet!" fatal error. FK_SecRel_4 usages in BPFAsmBackend.cpp and BPFELFObjectWriter.cpp are removed as they are not triggered in BPF backend. BPF backend used FK_SecRel_8 for LD_imm64 instruction operands. Differential Revision: https://reviews.llvm.org/D102712
- Loading branch information
1 parent
993f38d
commit 6a2ea84
Showing
12 changed files
with
215 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
; RUN: llc -march=bpfel -filetype=obj -o %t.el < %s | ||
; RUN: llvm-objdump -r %t.el | FileCheck --check-prefix=RELOC %s | ||
; RUN: llvm-objdump -d --no-show-raw-insn %t.el | FileCheck --check-prefix=DUMP %s | ||
; RUN: llc -march=bpfeb -filetype=obj -o %t.eb < %s | ||
; RUN: llvm-objdump -r %t.eb | FileCheck --check-prefix=RELOC %s | ||
; RUN: llvm-objdump -d --no-show-raw-insn %t.eb | FileCheck --check-prefix=DUMP %s | ||
|
||
; source code: | ||
; static __attribute__((noinline)) __attribute__((section("sec1"))) | ||
; int add(int a, int b) { | ||
; return a + b; | ||
; } | ||
; static __attribute__((noinline)) | ||
; int sub(int a, int b) { | ||
; return a - b; | ||
; } | ||
; int test(int a, int b) { | ||
; return add(a, b) + sub(a, b); | ||
; } | ||
; compilation flag: | ||
; clang -target bpf -O2 -emit-llvm -S test.c | ||
|
||
define dso_local i32 @test(i32 %a, i32 %b) local_unnamed_addr #0 { | ||
entry: | ||
%call = tail call fastcc i32 @add(i32 %a, i32 %b) | ||
%call1 = tail call fastcc i32 @sub(i32 %a, i32 %b) | ||
%add = add nsw i32 %call1, %call | ||
ret i32 %add | ||
} | ||
|
||
define internal fastcc i32 @add(i32 %a, i32 %b) unnamed_addr #1 section "sec1" { | ||
entry: | ||
%add = add nsw i32 %b, %a | ||
ret i32 %add | ||
} | ||
|
||
; Function Attrs: nofree noinline norecurse nosync nounwind readnone willreturn mustprogress | ||
define internal fastcc i32 @sub(i32 %a, i32 %b) unnamed_addr #1 { | ||
entry: | ||
%sub = sub nsw i32 %a, %b | ||
ret i32 %sub | ||
} | ||
|
||
; DUMP: .text: | ||
; DUMP-EMPTY: | ||
; DUMP-NEXT: <test> | ||
; DUMP-NEXT: r[[#]] = r[[#]] | ||
; DUMP-NEXT: r[[#]] = r[[#]] | ||
; DUMP-NEXT: call -1 | ||
|
||
; DUMP: sec1: | ||
; DUMP-EMPTY: | ||
; DUMP-NEXT: <add> | ||
|
||
; RELOC: RELOCATION RECORDS FOR [.text]: | ||
; RELOC: R_BPF_64_32 sec1 | ||
; RELOC-NOT: R_BPF_64_32 | ||
|
||
attributes #0 = { nofree norecurse nosync nounwind readnone willreturn mustprogress "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } | ||
attributes #1 = { nofree noinline norecurse nosync nounwind readnone willreturn mustprogress "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
; RUN: llc -march=bpfel -filetype=obj -o %t.el < %s | ||
; RUN: llvm-readelf -r %t.el | FileCheck %s | ||
; RUN: llc -march=bpfeb -filetype=obj -o %t.eb < %s | ||
; RUN: llvm-readelf -r %t.eb | FileCheck %s | ||
|
||
; source code: | ||
; int g() { return 0; } | ||
; struct t { void *p; } gbl = { g }; | ||
; compilation flag: | ||
; clang -target bpf -O2 -emit-llvm -S test.c | ||
|
||
%struct.t = type { i8* } | ||
|
||
@gbl = dso_local local_unnamed_addr global %struct.t { i8* bitcast (i32 ()* @g to i8*) }, align 8 | ||
|
||
; CHECK: '.rel.data' | ||
; CHECK: 0000000000000000 0000000200000002 R_BPF_64_ABS64 0000000000000000 g | ||
|
||
; Function Attrs: nofree norecurse nosync nounwind readnone willreturn mustprogress | ||
define dso_local i32 @g() #0 { | ||
entry: | ||
ret i32 0 | ||
} | ||
|
||
attributes #0 = { nofree norecurse nosync nounwind readnone willreturn mustprogress "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
llvm/test/ExecutionEngine/RuntimeDyld/BPF/abs64-relocation.s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# RUN: rm -rf %t && mkdir -p %t | ||
# RUN: llvm-mc -triple=bpfel -filetype=obj -o %t/test_reloc_abs64.o %s | ||
# RUN: llvm-rtdyld -triple=bpfel -verify -check=%s %t/test_reloc_abs64.o | ||
|
||
# test R_BPF_64_ABS64 which should have relocation resolved properly. | ||
|
||
.text | ||
.file "t1.c" | ||
.globl g # -- Begin function g | ||
.p2align 3 | ||
.type g,@function | ||
g: # @g | ||
r0 = 0 | ||
exit | ||
.Lfunc_end0: | ||
.size g, .Lfunc_end0-g | ||
# -- End function | ||
.type gbl,@object # @gbl | ||
.data | ||
.globl gbl | ||
.p2align 3 | ||
gbl: | ||
.quad g | ||
.size gbl, 8 | ||
|
||
# rtdyld-check: *{8}gbl = section_addr(test_reloc_abs64.o, .text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
if not 'BPF' in config.root.targets: | ||
config.unsupported = True |
77 changes: 77 additions & 0 deletions
77
llvm/test/ExecutionEngine/RuntimeDyld/BPF/nodyld32-64-relocations.s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# RUN: llvm-mc -triple=bpfel -filetype=obj -o %t %s | ||
# RUN: llvm-rtdyld -triple=bpfel -verify -check=%s %t | ||
|
||
# test R_BPF_64_64 and R_BPF_64_NODYLD32 relocations, both should be ignored. | ||
|
||
.globl _main | ||
.p2align 3 | ||
.type _main,@function | ||
_main: # @_main | ||
r1 = a ll | ||
|
||
# rtdyld-check: decode_operand(_main, 1)[31:0] = 0x0 | ||
|
||
r0 = *(u32 *)(r1 + 0) | ||
exit | ||
.Lfunc_end0: | ||
.size _main, .Lfunc_end0-_main | ||
# -- End function | ||
|
||
.type a,@object # @a | ||
.section .bss,"aw",@nobits | ||
.globl a | ||
.p2align 2 | ||
a: | ||
.long 0 # 0x0 | ||
.size a, 4 | ||
|
||
# rtdyld-check: *{4}a = 0 | ||
|
||
.section .BTF,"",@progbits | ||
.short 60319 # 0xeb9f | ||
.byte 1 | ||
.byte 0 | ||
.long 24 | ||
.long 0 | ||
.long 80 | ||
.long 80 | ||
.long 87 | ||
.long 0 # BTF_KIND_FUNC_PROTO(id = 1) | ||
.long 218103808 # 0xd000000 | ||
.long 2 | ||
.long 1 # BTF_KIND_INT(id = 2) | ||
.long 16777216 # 0x1000000 | ||
.long 4 | ||
.long 16777248 # 0x1000020 | ||
.long 5 # BTF_KIND_FUNC(id = 3) | ||
.long 201326593 # 0xc000001 | ||
.long 1 | ||
.long 80 # BTF_KIND_VAR(id = 4) | ||
.long 234881024 # 0xe000000 | ||
.long 2 | ||
.long 1 | ||
.long 82 # BTF_KIND_DATASEC(id = 5) | ||
.long 251658241 # 0xf000001 | ||
.long 0 | ||
.long 4 | ||
btf_a: | ||
.long a | ||
|
||
# rtdyld-check: *{4}btf_a = 0 | ||
|
||
.long 4 | ||
.byte 0 # string offset=0 | ||
.ascii "int" # string offset=1 | ||
.byte 0 | ||
.ascii "_main" # string offset=5 | ||
.byte 0 | ||
.ascii ".text" # string offset=11 | ||
.byte 0 | ||
.ascii "/home/yhs/work/tests/llvm/rtdyld/t.c" # string offset=17 | ||
.byte 0 | ||
.ascii "int _main() { return a; }" # string offset=54 | ||
.byte 0 | ||
.byte 97 # string offset=80 | ||
.byte 0 | ||
.ascii ".bss" # string offset=82 | ||
.byte 0 |