diff options
author | Andreas Kling <kling@serenityos.org> | 2021-05-15 00:13:44 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-15 00:17:55 +0200 |
commit | 16221305ade1e37d68c95d9bf674021afc18570f (patch) | |
tree | c4f1a8704da4e5f9b87641a3ba11a878ad493f13 /Userland/Libraries/LibELF | |
parent | f70d0f03de9eec4ee790f7bc7671d0c399be5e94 (diff) | |
download | serenity-16221305ade1e37d68c95d9bf674021afc18570f.zip |
LibELF: Remove sketchy use of "undefined" ELF::Image::Section
We were using ELF::Image::section(0) to indicate the "undefined"
section, when what we really wanted was just Optional<Section>.
So let's use Optional instead. :^)
Diffstat (limited to 'Userland/Libraries/LibELF')
-rw-r--r-- | Userland/Libraries/LibELF/Image.cpp | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibELF/Image.h | 7 |
2 files changed, 9 insertions, 12 deletions
diff --git a/Userland/Libraries/LibELF/Image.cpp b/Userland/Libraries/LibELF/Image.cpp index 1f2b78693e..281a4ea75e 100644 --- a/Userland/Libraries/LibELF/Image.cpp +++ b/Userland/Libraries/LibELF/Image.cpp @@ -257,21 +257,21 @@ Image::Relocation Image::RelocationSection::relocation(unsigned index) const return Relocation(m_image, rels[index]); } -Image::RelocationSection Image::Section::relocations() const +Optional<Image::RelocationSection> Image::Section::relocations() const { StringBuilder builder; builder.append(".rel"sv); builder.append(name()); auto relocation_section = m_image.lookup_section(builder.to_string()); - if (relocation_section.type() != SHT_REL) - return static_cast<const RelocationSection>(m_image.section(0)); + if (!relocation_section.has_value()) + return {}; - dbgln_if(ELF_IMAGE_DEBUG, "Found relocations for {} in {}", name(), relocation_section.name()); - return static_cast<const RelocationSection>(relocation_section); + dbgln_if(ELF_IMAGE_DEBUG, "Found relocations for {} in {}", name(), relocation_section.value().name()); + return static_cast<RelocationSection>(relocation_section.value()); } -Image::Section Image::lookup_section(const String& name) const +Optional<Image::Section> Image::lookup_section(const String& name) const { VERIFY(m_valid); for (unsigned i = 0; i < section_count(); ++i) { @@ -279,7 +279,7 @@ Image::Section Image::lookup_section(const String& name) const if (section.name() == name) return section; } - return section(0); + return {}; } StringView Image::Symbol::raw_data() const diff --git a/Userland/Libraries/LibELF/Image.h b/Userland/Libraries/LibELF/Image.h index 64c9ecedde..189c7a1682 100644 --- a/Userland/Libraries/LibELF/Image.h +++ b/Userland/Libraries/LibELF/Image.h @@ -114,8 +114,7 @@ public: u32 address() const { return m_section_header.sh_addr; } const char* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); } ReadonlyBytes bytes() const { return { raw_data(), size() }; } - bool is_undefined() const { return m_section_index == SHN_UNDEF; } - RelocationSection relocations() const; + Optional<RelocationSection> relocations() const; u32 flags() const { return m_section_header.sh_flags; } bool is_writable() const { return flags() & SHF_WRITE; } bool is_executable() const { return flags() & PF_X; } @@ -177,9 +176,7 @@ public: template<typename F> void for_each_program_header(F) const; - // NOTE: Returns section(0) if section with name is not found. - // FIXME: I don't love this API. - Section lookup_section(const String& name) const; + Optional<Section> lookup_section(String const& name) const; bool is_executable() const { return header().e_type == ET_EXEC; } bool is_relocatable() const { return header().e_type == ET_REL; } |