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. :^)
This commit is contained in:
Andreas Kling 2021-05-15 00:13:44 +02:00
parent f70d0f03de
commit 16221305ad
Notes: sideshowbarker 2024-07-18 18:08:15 +09:00
6 changed files with 18 additions and 19 deletions

View File

@ -65,7 +65,9 @@ KResultOr<int> Process::sys$module_load(Userspace<const char*> user_path, size_t
return IterationDecision::Continue;
auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
VERIFY(section_storage);
section.relocations().for_each_relocation([&](const ELF::Image::Relocation& relocation) {
auto relocations = section.relocations();
VERIFY(relocations.has_value());
relocations->for_each_relocation([&](const ELF::Image::Relocation& relocation) {
auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
switch (relocation.type()) {
case R_386_PC32: {

View File

@ -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()) {

View File

@ -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()

View File

@ -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

View File

@ -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; }

View File

@ -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) {