Skip to content

Commit cbe7739

Browse files
committed
elf: support weak functions
This commit adds support for the weak attribute on global functions. This attribute can be useful to prevent the compiler from changing the prototype of the function on some callsites. This change was also tested in Cilium with a new global, weak function. Without this change, loading would fail with the following error: offset 122800: relocating instruction: call: snat_v6_needs_masquerade: unsupported binding: STB_WEAK Signed-off-by: Paul Chaignon <[email protected]>
1 parent 72ba074 commit cbe7739

File tree

10 files changed

+15
-4
lines changed

10 files changed

+15
-4
lines changed

elf_reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ func (ec *elfCode) relocateInstruction(ins *asm.Instruction, rel elf.Symbol) err
588588

589589
switch typ {
590590
case elf.STT_NOTYPE, elf.STT_FUNC:
591-
if bind != elf.STB_GLOBAL {
591+
if bind != elf.STB_GLOBAL && bind != elf.STB_WEAK {
592592
return fmt.Errorf("call: %s: %w: %s", name, errUnsupportedBinding, bind)
593593
}
594594

elf_reader_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ func TestLoadCollectionSpec(t *testing.T) {
207207
SectionName: "other",
208208
License: "MIT",
209209
},
210+
"global_fn4": {
211+
Name: "global_fn4",
212+
Type: UnspecifiedProgram,
213+
SectionName: "other",
214+
License: "MIT",
215+
},
210216
"static_fn": {
211217
Name: "static_fn",
212218
Type: UnspecifiedProgram,
@@ -266,7 +272,7 @@ func TestLoadCollectionSpec(t *testing.T) {
266272
}
267273

268274
ret := mustRun(t, coll.Programs["xdp_prog"], nil)
269-
qt.Assert(t, qt.Equals(ret, 7))
275+
qt.Assert(t, qt.Equals(ret, 11))
270276
})
271277
}
272278

info_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,13 @@ func TestProgInfoExtBTF(t *testing.T) {
523523
t.Fatal(err)
524524
}
525525

526-
expectedLineInfoCount := 28
526+
expectedLineInfoCount := 33
527527
expectedFuncInfo := map[string]bool{
528528
"xdp_prog": false,
529529
"static_fn": false,
530530
"global_fn2": false,
531531
"global_fn3": false,
532+
"global_fn4": false,
532533
}
533534

534535
lineInfoCount := 0

testdata/loader-clang-14-eb.elf

456 Bytes
Binary file not shown.

testdata/loader-clang-14-el.elf

456 Bytes
Binary file not shown.

testdata/loader-clang-17-eb.elf

456 Bytes
Binary file not shown.

testdata/loader-clang-17-el.elf

456 Bytes
Binary file not shown.

testdata/loader-clang-20-eb.elf

464 Bytes
Binary file not shown.

testdata/loader-clang-20-el.elf

464 Bytes
Binary file not shown.

testdata/loader.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,12 @@ int __attribute__((noinline)) __section("other") global_fn3(uint32_t arg) {
115115
return arg + 1;
116116
}
117117

118+
int __attribute__((noinline)) __attribute__((weak)) __section("other") global_fn4(uint32_t arg) {
119+
return arg + 3;
120+
}
121+
118122
int __attribute__((noinline)) global_fn(uint32_t arg) {
119-
return static_fn(arg) + global_fn2(arg) + global_fn3(arg);
123+
return static_fn(arg) + global_fn2(arg) + global_fn3(arg) + global_fn4(arg);
120124
}
121125

122126
volatile unsigned int key1 = 0; // .bss

0 commit comments

Comments
 (0)