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
This commit is contained in:
Yonghong Song 2021-03-25 14:09:19 -07:00
parent 3fd64cc7a3
commit 886f9ff531
5 changed files with 38 additions and 13 deletions

View File

@ -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);
}
}
@ -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() {

View File

@ -187,7 +187,7 @@ public:
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; }

View File

@ -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
@ -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
@ -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" }

View File

@ -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
@ -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

View File

@ -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
@ -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