Skip to content

Commit 886f9ff

Browse files
committed
BPF: add extern func to data sections if specified
This permits extern function (BTF_KIND_FUNC) be added to BTF_KIND_DATASEC if a section name is specified. For example, -bash-4.4$ cat t.c void foo(int) __attribute__((section(".kernel.funcs"))); int test(void) { foo(5); return 0; } The extern function foo (BTF_KIND_FUNC) will be put into BTF_KIND_DATASEC with name ".kernel.funcs". This will help to differentiate two kinds of external functions, functions in kernel and functions defined in other bpf programs. Differential Revision: https://reviews.llvm.org/D93563
1 parent 3fd64cc commit 886f9ff

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

llvm/lib/Target/BPF/BTFDebug.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,8 +1224,8 @@ void BTFDebug::processGlobals(bool ProcessingMapDef) {
12241224
const DataLayout &DL = Global.getParent()->getDataLayout();
12251225
uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());
12261226

1227-
DataSecEntries[std::string(SecName)]->addVar(VarId, Asm->getSymbol(&Global),
1228-
Size);
1227+
DataSecEntries[std::string(SecName)]->addDataSecEntry(VarId,
1228+
Asm->getSymbol(&Global), Size);
12291229
}
12301230
}
12311231

@@ -1303,7 +1303,19 @@ void BTFDebug::processFuncPrototypes(const Function *F) {
13031303
uint8_t Scope = BTF::FUNC_EXTERN;
13041304
auto FuncTypeEntry =
13051305
std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
1306-
addType(std::move(FuncTypeEntry));
1306+
uint32_t FuncId = addType(std::move(FuncTypeEntry));
1307+
if (F->hasSection()) {
1308+
StringRef SecName = F->getSection();
1309+
1310+
if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1311+
DataSecEntries[std::string(SecName)] =
1312+
std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1313+
}
1314+
1315+
// We really don't know func size, set it to 0.
1316+
DataSecEntries[std::string(SecName)]->addDataSecEntry(FuncId,
1317+
Asm->getSymbol(F), 0);
1318+
}
13071319
}
13081320

13091321
void BTFDebug::endModule() {

llvm/lib/Target/BPF/BTFDebug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class BTFKindDataSec : public BTFTypeBase {
187187
uint32_t getSize() override {
188188
return BTFTypeBase::getSize() + BTF::BTFDataSecVarSize * Vars.size();
189189
}
190-
void addVar(uint32_t Id, const MCSymbol *Sym, uint32_t Size) {
190+
void addDataSecEntry(uint32_t Id, const MCSymbol *Sym, uint32_t Size) {
191191
Vars.push_back(std::make_tuple(Id, Sym, Size));
192192
}
193193
std::string getName() { return Name; }

llvm/test/CodeGen/BPF/BTF/extern-var-func-weak-section.ll

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ declare !dbg !4 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
2323
; CHECK-NEXT: .byte 0
2424
; CHECK-NEXT: .long 24
2525
; CHECK-NEXT: .long 0
26-
; CHECK-NEXT: .long 88
27-
; CHECK-NEXT: .long 88
28-
; CHECK-NEXT: .long 72
26+
; CHECK-NEXT: .long 112
27+
; CHECK-NEXT: .long 112
28+
; CHECK-NEXT: .long 76
2929
; CHECK-NEXT: .long 0 # BTF_KIND_FUNC_PROTO(id = 1)
3030
; CHECK-NEXT: .long 218103808 # 0xd000000
3131
; CHECK-NEXT: .long 2
@@ -48,6 +48,12 @@ declare !dbg !4 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
4848
; CHECK-NEXT: .long 60 # BTF_KIND_FUNC(id = 6)
4949
; CHECK-NEXT: .long 201326594 # 0xc000002
5050
; CHECK-NEXT: .long 4
51+
; CHECK-NEXT: .long 72 # BTF_KIND_DATASEC(id = 7)
52+
; CHECK-NEXT: .long 251658241 # 0xf000001
53+
; CHECK-NEXT: .long 0
54+
; CHECK-NEXT: .long 6
55+
; CHECK-NEXT: .long global_func
56+
; CHECK-NEXT: .long 0
5157
; CHECK-NEXT: .byte 0 # string offset=0
5258
; CHECK-NEXT: .ascii "int" # string offset=1
5359
; CHECK-NEXT: .byte 0
@@ -61,6 +67,7 @@ declare !dbg !4 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
6167
; CHECK-NEXT: .byte 0
6268
; CHECK-NEXT: .ascii "global_func" # string offset=60
6369
; CHECK-NEXT: .byte 0
70+
; CHECK-NEXT: .ascii "abc" # string offset=72
6471

6572
attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "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" }
6673
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="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" }

llvm/test/CodeGen/BPF/BTF/extern-var-section.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ entry:
2828
; CHECK-NEXT: .byte 0
2929
; CHECK-NEXT: .long 24
3030
; CHECK-NEXT: .long 0
31-
; CHECK-NEXT: .long 128
32-
; CHECK-NEXT: .long 128
31+
; CHECK-NEXT: .long 140
32+
; CHECK-NEXT: .long 140
3333
; CHECK-NEXT: .long 79
3434
; CHECK-NEXT: .long 0 # BTF_KIND_FUNC_PROTO(id = 1)
3535
; CHECK-NEXT: .long 218103808 # 0xd000000
@@ -58,7 +58,10 @@ entry:
5858
; CHECK-NEXT: .long 5
5959
; CHECK-NEXT: .long 2
6060
; CHECK-NEXT: .long 75 # BTF_KIND_DATASEC(id = 8)
61-
; CHECK-NEXT: .long 251658241 # 0xf000001
61+
; CHECK-NEXT: .long 251658242 # 0xf000002
62+
; CHECK-NEXT: .long 0
63+
; CHECK-NEXT: .long 6
64+
; CHECK-NEXT: .long global_func
6265
; CHECK-NEXT: .long 0
6366
; CHECK-NEXT: .long 7
6467
; CHECK-NEXT: .long ch

llvm/test/CodeGen/BPF/BTF/extern-var-weak-section.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ declare !dbg !6 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
2828
; CHECK-NEXT: .byte 0
2929
; CHECK-NEXT: .long 24
3030
; CHECK-NEXT: .long 0
31-
; CHECK-NEXT: .long 128
32-
; CHECK-NEXT: .long 128
31+
; CHECK-NEXT: .long 140
32+
; CHECK-NEXT: .long 140
3333
; CHECK-NEXT: .long 79
3434
; CHECK-NEXT: .long 0 # BTF_KIND_FUNC_PROTO(id = 1)
3535
; CHECK-NEXT: .long 218103808 # 0xd000000
@@ -58,7 +58,10 @@ declare !dbg !6 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
5858
; CHECK-NEXT: .long 5
5959
; CHECK-NEXT: .long 2
6060
; CHECK-NEXT: .long 75 # BTF_KIND_DATASEC(id = 8)
61-
; CHECK-NEXT: .long 251658241 # 0xf000001
61+
; CHECK-NEXT: .long 251658242 # 0xf000002
62+
; CHECK-NEXT: .long 0
63+
; CHECK-NEXT: .long 6
64+
; CHECK-NEXT: .long global_func
6265
; CHECK-NEXT: .long 0
6366
; CHECK-NEXT: .long 7
6467
; CHECK-NEXT: .long ch

0 commit comments

Comments
 (0)