From f8eb4d2e9060b7917f1429aa3bfd2cb9945021e3 Mon Sep 17 00:00:00 2001 From: Dylan Reimerink Date: Wed, 20 Jul 2022 16:46:11 +0200 Subject: [PATCH] elf_reader: Allow strings read-only global data sections In the past we had the limitation that string sections like `.rodata.str1.1` could not not be loaded since clang/LLVM doesn't provide BTF information for such sections. In #675 we lifted the requirement to have BTF information for global data sections. This means that we can now also allow string sections to be loaded as global data. All facilities to do so are already in place, this commit just removes the check for references to sections with the `SHF_STRINGS` flag set. Fixes: #741 Signed-off-by: Dylan Reimerink --- elf_reader.go | 4 ---- elf_reader_test.go | 20 ++++++++++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/elf_reader.go b/elf_reader.go index 4d8d4bed9..04d57c50d 100644 --- a/elf_reader.go +++ b/elf_reader.go @@ -261,10 +261,6 @@ func (ec *elfCode) loadRelocations(relSections map[elf.SectionIndex]*elf.Section return fmt.Errorf("section %q: reference to %q in section %s: %w", section.Name, rel.Name, rel.Section, ErrNotSupported) } - if target.Flags&elf.SHF_STRINGS > 0 { - return fmt.Errorf("section %q: string is not stack allocated: %w", section.Name, ErrNotSupported) - } - target.references++ } diff --git a/elf_reader_test.go b/elf_reader_test.go index 2b51fba0d..b9f589bee 100644 --- a/elf_reader_test.go +++ b/elf_reader_test.go @@ -423,10 +423,22 @@ func TestLoadInvalidInitializedBTFMap(t *testing.T) { func TestStringSection(t *testing.T) { testutils.Files(t, testutils.Glob(t, "testdata/strings-*.elf"), func(t *testing.T, file string) { - _, err := LoadCollectionSpec(file) - t.Log(err) - if !errors.Is(err, ErrNotSupported) { - t.Error("References to a string section should be unsupported") + coll, err := LoadCollectionSpec(file) + if err != nil { + t.Fatal(err) + } + + strMap, found := coll.Maps[".rodata.str1.1"] + if !found { + t.Fatal("Unable to find map '.rodata.str1.1' in loaded collection") + } + + if !strMap.Freeze { + t.Fatal("Read only data maps should be frozen") + } + + if strMap.Flags != unix.BPF_F_RDONLY_PROG { + t.Fatal("Read only data maps should have the prog-read-only flag set") } }) }