Emit types for all globals. Previously, the types for[BPF] Support external
variables andal globals without section names are omitted.s
For external variablesPreviously, also provide additional informationonly types for external variables and globals without
about the purpose of the extern's through section namesection names are omitted.
For example,This patch emitted types for the following example,all non-common-linkage globals.
-bash-4.4$ cat t.cFrom https://llvm.org/docs/LangRef.html,
truct linux_t { int f1; char f2; };common
struct cross_module_t { int c1; char c2; }; “common” linkage is most similar to “weak” linkage,
struct curr_module_t { char m1; int m2; }; but they are used for tentative definitions in C,
extern struct linux_t g1 __attribute__((section(".gvar.linux"))); such as “int X;” at global scope. ...
It is not clear what are "common"/"weak" semantics for bpf programs,
extern struct cross_module_t g2 __attribute__((section(".gvar.cross_module")));so omit them for now.
For non-extern global variables without section names,
static volatile const struct curr_module_t g3 __attribute__((section(".gvar.curr_module")));- a BTF_KIND_VAR will be generated.
int test() {- the BTF_KIND_VAR will be put into .bss/.data DataSec
return g1.f1 + g2.c1 + g3.m1;based on whether they are zero initialized or not.
For extern globals:
}- A BTF_KIND_VAR will be generated.
-bash-4.4$ clang -target bpf -g -O2 -S t.c
Generated BTF_KIND_VARs:
.long 78 # BTF_KIND_VAR(id = 4) # g1
.long 234881024 # 0xe000000
.long 0 # type id #0
.long 2 # extern
.long 81 # BTF_KIND_VAR(id = 5) # g2
.long 234881024 # 0xe000000
.long 0 # type id #0
.long 2 # extern
.long 109 # BTF_KIND_VAR(id = 10) # g3
.long 234881024 # 0xe000000
.long 6 # type id #6 if with section name, which is CONST => VOLATILE => curr_module_ta DataSec with that section name
.long 0 # static
Generated BTF_KIND_DATASECs:
.long 112 # BTF_KIND_DATASEC(id = 11) # ".gvar.cross_module"
.long 251658241 # 0xf000001
.long 0
.long 5
.long g2 <== will resolve to 0 as this is an external without data allocation
.long 8
.long 131 # BTF_KIND_DATASEC(id = 12) # ".gvar.curr_module"
.long 251658241 # 0xf000001
.long 0
.long 10
.long g3 <== will resolve to 0 as its offset is 0 within the section.
.long 8
.long 149 # BTF_KIND_DATASEC(id = 13) # ".gvar.linux"
.long 251658241 # 0xf000001
.long 0will be created containing that global variable.
.long 4- external globals do not have types in BTF_KIND_VAR since
.long g1 <== will resolve to 0 as this iis anclang does not generate debug info type for external without data allocationvariables.
.long 8
In summary, llvm/clang does not generate debuginfo- the in-section offset for external variables, so their types will be all 0
in BTF_KIND_VAR will be 0. The same for the offset within the section, which will be 0 since these variables are not allocated in current ELF.
for external variables.