Skip to content

Commit

Permalink
BPF: add extern func to data sections if specified
Browse files Browse the repository at this point in the history
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

(cherry picked from commit 886f9ff)
  • Loading branch information
yonghong-song authored and tstellar committed Jun 10, 2021
1 parent 319a27b commit 7f6ceec
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
18 changes: 15 additions & 3 deletions llvm/lib/Target/BPF/BTFDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,8 @@ void BTFDebug::processGlobals(bool ProcessingMapDef) {
const DataLayout &DL = Global.getParent()->getDataLayout();
uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());

DataSecEntries[std::string(SecName)]->addVar(VarId, Asm->getSymbol(&Global),
Size);
DataSecEntries[std::string(SecName)]->addDataSecEntry(VarId,
Asm->getSymbol(&Global), Size);
}
}

Expand Down Expand Up @@ -1303,7 +1303,19 @@ void BTFDebug::processFuncPrototypes(const Function *F) {
uint8_t Scope = BTF::FUNC_EXTERN;
auto FuncTypeEntry =
std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
addType(std::move(FuncTypeEntry));
uint32_t FuncId = addType(std::move(FuncTypeEntry));
if (F->hasSection()) {
StringRef SecName = F->getSection();

if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
DataSecEntries[std::string(SecName)] =
std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
}

// We really don't know func size, set it to 0.
DataSecEntries[std::string(SecName)]->addDataSecEntry(FuncId,
Asm->getSymbol(F), 0);
}
}

void BTFDebug::endModule() {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/BPF/BTFDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class BTFKindDataSec : public BTFTypeBase {
uint32_t getSize() override {
return BTFTypeBase::getSize() + BTF::BTFDataSecVarSize * Vars.size();
}
void addVar(uint32_t Id, const MCSymbol *Sym, uint32_t Size) {
void addDataSecEntry(uint32_t Id, const MCSymbol *Sym, uint32_t Size) {
Vars.push_back(std::make_tuple(Id, Sym, Size));
}
std::string getName() { return Name; }
Expand Down
13 changes: 10 additions & 3 deletions llvm/test/CodeGen/BPF/BTF/extern-var-func-weak-section.ll
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ declare !dbg !4 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .long 24
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 88
; CHECK-NEXT: .long 88
; CHECK-NEXT: .long 72
; CHECK-NEXT: .long 112
; CHECK-NEXT: .long 112
; CHECK-NEXT: .long 76
; CHECK-NEXT: .long 0 # BTF_KIND_FUNC_PROTO(id = 1)
; CHECK-NEXT: .long 218103808 # 0xd000000
; CHECK-NEXT: .long 2
Expand All @@ -48,6 +48,12 @@ declare !dbg !4 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
; CHECK-NEXT: .long 60 # BTF_KIND_FUNC(id = 6)
; CHECK-NEXT: .long 201326594 # 0xc000002
; CHECK-NEXT: .long 4
; CHECK-NEXT: .long 72 # BTF_KIND_DATASEC(id = 7)
; CHECK-NEXT: .long 251658241 # 0xf000001
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 6
; CHECK-NEXT: .long global_func
; CHECK-NEXT: .long 0
; CHECK-NEXT: .byte 0 # string offset=0
; CHECK-NEXT: .ascii "int" # string offset=1
; CHECK-NEXT: .byte 0
Expand All @@ -61,6 +67,7 @@ declare !dbg !4 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "global_func" # string offset=60
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "abc" # string offset=72

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" }
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" }
Expand Down
9 changes: 6 additions & 3 deletions llvm/test/CodeGen/BPF/BTF/extern-var-section.ll
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ entry:
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .long 24
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 128
; CHECK-NEXT: .long 128
; CHECK-NEXT: .long 140
; CHECK-NEXT: .long 140
; CHECK-NEXT: .long 79
; CHECK-NEXT: .long 0 # BTF_KIND_FUNC_PROTO(id = 1)
; CHECK-NEXT: .long 218103808 # 0xd000000
Expand Down Expand Up @@ -58,7 +58,10 @@ entry:
; CHECK-NEXT: .long 5
; CHECK-NEXT: .long 2
; CHECK-NEXT: .long 75 # BTF_KIND_DATASEC(id = 8)
; CHECK-NEXT: .long 251658241 # 0xf000001
; CHECK-NEXT: .long 251658242 # 0xf000002
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 6
; CHECK-NEXT: .long global_func
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 7
; CHECK-NEXT: .long ch
Expand Down
9 changes: 6 additions & 3 deletions llvm/test/CodeGen/BPF/BTF/extern-var-weak-section.ll
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ declare !dbg !6 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .long 24
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 128
; CHECK-NEXT: .long 128
; CHECK-NEXT: .long 140
; CHECK-NEXT: .long 140
; CHECK-NEXT: .long 79
; CHECK-NEXT: .long 0 # BTF_KIND_FUNC_PROTO(id = 1)
; CHECK-NEXT: .long 218103808 # 0xd000000
Expand Down Expand Up @@ -58,7 +58,10 @@ declare !dbg !6 extern_weak dso_local i32 @global_func(i8 signext) local_unnamed
; CHECK-NEXT: .long 5
; CHECK-NEXT: .long 2
; CHECK-NEXT: .long 75 # BTF_KIND_DATASEC(id = 8)
; CHECK-NEXT: .long 251658241 # 0xf000001
; CHECK-NEXT: .long 251658242 # 0xf000002
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 6
; CHECK-NEXT: .long global_func
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 7
; CHECK-NEXT: .long ch
Expand Down

0 comments on commit 7f6ceec

Please sign in to comment.