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 | |
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')
-rw-r--r-- | Userland/Libraries/LibDebug/DebugInfo.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibELF/Image.cpp | 14 | ||||
-rw-r--r-- | Userland/Libraries/LibELF/Image.h | 7 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/FileIconProvider.cpp | 4 |
5 files changed, 15 insertions, 18 deletions
diff --git a/Userland/Libraries/LibDebug/DebugInfo.cpp b/Userland/Libraries/LibDebug/DebugInfo.cpp index 2a9568c69d..a3b03cfebc 100644 --- a/Userland/Libraries/LibDebug/DebugInfo.cpp +++ b/Userland/Libraries/LibDebug/DebugInfo.cpp @@ -79,10 +79,10 @@ void DebugInfo::parse_scopes_impl(const Dwarf::DIE& die) void DebugInfo::prepare_lines() { auto section = elf().lookup_section(".debug_line"); - if (section.is_undefined()) + if (!section.has_value()) return; - InputMemoryStream stream { section.bytes() }; + InputMemoryStream stream { section->bytes() }; Vector<Dwarf::LineProgram::LineInfo> all_lines; while (!stream.eof()) { diff --git a/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp b/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp index 34d7e016e3..eae334a961 100644 --- a/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp +++ b/Userland/Libraries/LibDebug/Dwarf/DwarfInfo.cpp @@ -24,9 +24,9 @@ DwarfInfo::DwarfInfo(const ELF::Image& elf) ReadonlyBytes DwarfInfo::section_data(const String& section_name) const { auto section = m_elf.lookup_section(section_name); - if (section.is_undefined()) + if (!section.has_value()) return {}; - return section.bytes(); + return section->bytes(); } void DwarfInfo::populate_compilation_units() 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; } diff --git a/Userland/Libraries/LibGUI/FileIconProvider.cpp b/Userland/Libraries/LibGUI/FileIconProvider.cpp index 8ac7cd6527..bff9ed0b10 100644 --- a/Userland/Libraries/LibGUI/FileIconProvider.cpp +++ b/Userland/Libraries/LibGUI/FileIconProvider.cpp @@ -176,10 +176,10 @@ Icon FileIconProvider::icon_for_executable(const String& path) auto section = image.lookup_section(icon_section.section_name); RefPtr<Gfx::Bitmap> bitmap; - if (section.is_undefined()) { + if (!section.has_value()) { bitmap = s_executable_icon.bitmap_for_size(icon_section.image_size); } else { - bitmap = Gfx::load_png_from_memory(reinterpret_cast<const u8*>(section.raw_data()), section.size()); + bitmap = Gfx::load_png_from_memory(reinterpret_cast<u8 const*>(section->raw_data()), section->size()); } if (!bitmap) { |