Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf_reader: permit multiple .data* sections #1546

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elf_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func LoadCollectionSpecFromReader(rd io.ReaderAt) (*CollectionSpec, error) {
sections[idx] = newElfSection(sec, mapSection)
case sec.Name == ".maps":
sections[idx] = newElfSection(sec, btfMapSection)
case sec.Name == ".bss" || sec.Name == ".data" || strings.HasPrefix(sec.Name, ".rodata"):
case sec.Name == ".bss" || strings.HasPrefix(sec.Name, ".data") || strings.HasPrefix(sec.Name, ".rodata"):
sections[idx] = newElfSection(sec, dataSection)
case sec.Type == elf.SHT_REL:
// Store relocations under the section index of the target
Expand Down
8 changes: 8 additions & 0 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ func TestLoadCollectionSpec(t *testing.T) {
ValueSize: 8,
MaxEntries: 1,
},
".data.custom": {
Name: SanitizeName(".data.custom", -1),
Type: Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
Contents: []MapKV{{Key: uint32(0), Value: make([]uint8, 4)}},
},
},
Programs: map[string]*ProgramSpec{
"xdp_prog": {
Expand Down
2 changes: 1 addition & 1 deletion info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func TestProgInfoExtBTF(t *testing.T) {
t.Fatal(err)
}

expectedLineInfoCount := 26
expectedLineInfoCount := 28
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the downside of overloading loader.c, I guess. To be honest, I do not understand where 26 is coming from, but it (kind of) makes sense that the value grew by 2 after two lines were added. @ti-mo

Copy link
Collaborator

@ti-mo ti-mo Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, that's quite straightforward to investigate!

		if src := ins.Source(); src != nil {
			lineInfoCount++
+			fmt.Println(src)
+			fmt.Println(ins)
		}

Results in:

...
LoadMapValue dst: r1, fd: 40 off: 0
	return static_fn(arg) + global_fn(arg) + arg2;
AddReg dst: r0 src: r1
	return static_fn(arg) + global_fn(arg) + arg2;
Exit

int __attribute__((noinline)) global_fn(uint32_t arg) {
...
	return static_fn(arg) + global_fn2(arg) + global_fn3(arg);
AddReg dst: r8 src: r0
	return static_fn(arg) + global_fn2(arg) + global_fn3(arg);
MovReg dst: r0 src: r8

One instruction for the variable access and one for accumulating into r0. (this is on main) Adding another variable to the return statement would result in 2 extra insns/lineinfo.

expectedFuncInfo := map[string]bool{
"xdp_prog": false,
"static_fn": false,
Expand Down
Binary file modified testdata/loader-clang-11-eb.elf
Binary file not shown.
Binary file modified testdata/loader-clang-11-el.elf
Binary file not shown.
Binary file modified testdata/loader-clang-14-eb.elf
Binary file not shown.
Binary file modified testdata/loader-clang-14-el.elf
Binary file not shown.
Binary file modified testdata/loader-clang-17-eb.elf
Binary file not shown.
Binary file modified testdata/loader-clang-17-el.elf
Binary file not shown.
4 changes: 3 additions & 1 deletion testdata/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,14 @@ volatile const unsigned int key3 = 2; // .rodata
static volatile const uint32_t arg; // .rodata, populated by loader
// custom .rodata section, populated by loader
static volatile const uint32_t arg2 __section(".rodata.test");
// custom .data section
static volatile uint32_t arg3 __section(".data.custom");

__section("xdp") int xdp_prog() {
map_lookup_elem(&hash_map, (void *)&key1);
map_lookup_elem(&hash_map2, (void *)&key2);
map_lookup_elem(&hash_map2, (void *)&key3);
return static_fn(arg) + global_fn(arg) + arg2;
return static_fn(arg) + global_fn(arg) + arg2 + arg3;
}

// This function has no relocations, and is thus parsed differently.
Expand Down
Loading