mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:57:45 +00:00
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:
parent
f70d0f03de
commit
16221305ad
6 changed files with 18 additions and 19 deletions
|
@ -65,7 +65,9 @@ KResultOr<int> Process::sys$module_load(Userspace<const char*> user_path, size_t
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
|
auto* section_storage = section_storage_by_name.get(section.name()).value_or(nullptr);
|
||||||
VERIFY(section_storage);
|
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());
|
auto& patch_ptr = *reinterpret_cast<ptrdiff_t*>(section_storage + relocation.offset());
|
||||||
switch (relocation.type()) {
|
switch (relocation.type()) {
|
||||||
case R_386_PC32: {
|
case R_386_PC32: {
|
||||||
|
|
|
@ -79,10 +79,10 @@ void DebugInfo::parse_scopes_impl(const Dwarf::DIE& die)
|
||||||
void DebugInfo::prepare_lines()
|
void DebugInfo::prepare_lines()
|
||||||
{
|
{
|
||||||
auto section = elf().lookup_section(".debug_line");
|
auto section = elf().lookup_section(".debug_line");
|
||||||
if (section.is_undefined())
|
if (!section.has_value())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
InputMemoryStream stream { section.bytes() };
|
InputMemoryStream stream { section->bytes() };
|
||||||
|
|
||||||
Vector<Dwarf::LineProgram::LineInfo> all_lines;
|
Vector<Dwarf::LineProgram::LineInfo> all_lines;
|
||||||
while (!stream.eof()) {
|
while (!stream.eof()) {
|
||||||
|
|
|
@ -24,9 +24,9 @@ DwarfInfo::DwarfInfo(const ELF::Image& elf)
|
||||||
ReadonlyBytes DwarfInfo::section_data(const String& section_name) const
|
ReadonlyBytes DwarfInfo::section_data(const String& section_name) const
|
||||||
{
|
{
|
||||||
auto section = m_elf.lookup_section(section_name);
|
auto section = m_elf.lookup_section(section_name);
|
||||||
if (section.is_undefined())
|
if (!section.has_value())
|
||||||
return {};
|
return {};
|
||||||
return section.bytes();
|
return section->bytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DwarfInfo::populate_compilation_units()
|
void DwarfInfo::populate_compilation_units()
|
||||||
|
|
|
@ -257,21 +257,21 @@ Image::Relocation Image::RelocationSection::relocation(unsigned index) const
|
||||||
return Relocation(m_image, rels[index]);
|
return Relocation(m_image, rels[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Image::RelocationSection Image::Section::relocations() const
|
Optional<Image::RelocationSection> Image::Section::relocations() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append(".rel"sv);
|
builder.append(".rel"sv);
|
||||||
builder.append(name());
|
builder.append(name());
|
||||||
|
|
||||||
auto relocation_section = m_image.lookup_section(builder.to_string());
|
auto relocation_section = m_image.lookup_section(builder.to_string());
|
||||||
if (relocation_section.type() != SHT_REL)
|
if (!relocation_section.has_value())
|
||||||
return static_cast<const RelocationSection>(m_image.section(0));
|
return {};
|
||||||
|
|
||||||
dbgln_if(ELF_IMAGE_DEBUG, "Found relocations for {} in {}", name(), relocation_section.name());
|
dbgln_if(ELF_IMAGE_DEBUG, "Found relocations for {} in {}", name(), relocation_section.value().name());
|
||||||
return static_cast<const RelocationSection>(relocation_section);
|
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);
|
VERIFY(m_valid);
|
||||||
for (unsigned i = 0; i < section_count(); ++i) {
|
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)
|
if (section.name() == name)
|
||||||
return section;
|
return section;
|
||||||
}
|
}
|
||||||
return section(0);
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
StringView Image::Symbol::raw_data() const
|
StringView Image::Symbol::raw_data() const
|
||||||
|
|
|
@ -114,8 +114,7 @@ public:
|
||||||
u32 address() const { return m_section_header.sh_addr; }
|
u32 address() const { return m_section_header.sh_addr; }
|
||||||
const char* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
|
const char* raw_data() const { return m_image.raw_data(m_section_header.sh_offset); }
|
||||||
ReadonlyBytes bytes() const { return { raw_data(), size() }; }
|
ReadonlyBytes bytes() const { return { raw_data(), size() }; }
|
||||||
bool is_undefined() const { return m_section_index == SHN_UNDEF; }
|
Optional<RelocationSection> relocations() const;
|
||||||
RelocationSection relocations() const;
|
|
||||||
u32 flags() const { return m_section_header.sh_flags; }
|
u32 flags() const { return m_section_header.sh_flags; }
|
||||||
bool is_writable() const { return flags() & SHF_WRITE; }
|
bool is_writable() const { return flags() & SHF_WRITE; }
|
||||||
bool is_executable() const { return flags() & PF_X; }
|
bool is_executable() const { return flags() & PF_X; }
|
||||||
|
@ -177,9 +176,7 @@ public:
|
||||||
template<typename F>
|
template<typename F>
|
||||||
void for_each_program_header(F) const;
|
void for_each_program_header(F) const;
|
||||||
|
|
||||||
// NOTE: Returns section(0) if section with name is not found.
|
Optional<Section> lookup_section(String const& name) const;
|
||||||
// FIXME: I don't love this API.
|
|
||||||
Section lookup_section(const String& name) const;
|
|
||||||
|
|
||||||
bool is_executable() const { return header().e_type == ET_EXEC; }
|
bool is_executable() const { return header().e_type == ET_EXEC; }
|
||||||
bool is_relocatable() const { return header().e_type == ET_REL; }
|
bool is_relocatable() const { return header().e_type == ET_REL; }
|
||||||
|
|
|
@ -176,10 +176,10 @@ Icon FileIconProvider::icon_for_executable(const String& path)
|
||||||
auto section = image.lookup_section(icon_section.section_name);
|
auto section = image.lookup_section(icon_section.section_name);
|
||||||
|
|
||||||
RefPtr<Gfx::Bitmap> bitmap;
|
RefPtr<Gfx::Bitmap> bitmap;
|
||||||
if (section.is_undefined()) {
|
if (!section.has_value()) {
|
||||||
bitmap = s_executable_icon.bitmap_for_size(icon_section.image_size);
|
bitmap = s_executable_icon.bitmap_for_size(icon_section.image_size);
|
||||||
} else {
|
} 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) {
|
if (!bitmap) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue