1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:27:45 +00:00

LibELF: Remove ELF::Loader and move everyone to ELF::Image

This commit gets rid of ELF::Loader entirely since its very ambiguous
purpose was actually to load executables for the kernel, and that is
now handled by the kernel itself.

This patch includes some drive-by cleanup in LibDebug and CrashDaemon
enabled by the fact that we no longer need to keep the ref-counted
ELF::Loader around.
This commit is contained in:
Andreas Kling 2020-12-25 02:14:56 +01:00
parent 7551a66f73
commit 1e4c010643
24 changed files with 178 additions and 318 deletions

View file

@ -28,7 +28,7 @@
#include <AK/MappedFile.h>
#include <AK/StringBuilder.h>
#include <LibDebug/DebugSession.h>
#include <LibELF/Loader.h>
#include <LibELF/Image.h>
#include <LibX86/Disassembler.h>
#include <LibX86/ELFSymbolProvider.h>
#include <ctype.h>
@ -44,25 +44,27 @@ DisassemblyModel::DisassemblyModel(const Debug::DebugSession& debug_session, con
return;
}
RefPtr<ELF::Loader> elf_loader;
OwnPtr<ELF::Image> kernel_elf;
const ELF::Image* elf = nullptr;
if (containing_function.value().address_low >= 0xc0000000) {
auto kernel_file = make<MappedFile>("/boot/Kernel");
if (!kernel_file->is_valid())
return;
elf_loader = ELF::Loader::create((const u8*)kernel_file->data(), kernel_file->size());
kernel_elf = make<ELF::Image>((const u8*)kernel_file->data(), kernel_file->size());
elf = kernel_elf.ptr();
} else {
elf_loader = debug_session.elf();
elf = &debug_session.elf();
}
auto symbol = elf_loader->find_symbol(containing_function.value().address_low);
auto symbol = elf->find_symbol(containing_function.value().address_low);
if (!symbol.has_value())
return;
ASSERT(symbol.has_value());
auto view = symbol.value().raw_data();
X86::ELFSymbolProvider symbol_provider(*elf_loader);
X86::ELFSymbolProvider symbol_provider(*elf);
X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
X86::Disassembler disassembler(stream);

View file

@ -27,7 +27,7 @@
#include "DisassemblyModel.h"
#include "Profile.h"
#include <AK/MappedFile.h>
#include <LibELF/Loader.h>
#include <LibELF/Image.h>
#include <LibGUI/Painter.h>
#include <LibX86/Disassembler.h>
#include <LibX86/ELFSymbolProvider.h>
@ -65,16 +65,16 @@ DisassemblyModel::DisassemblyModel(Profile& profile, ProfileNode& node)
if (!m_file->is_valid())
return;
auto elf_loader = ELF::Loader::create((const u8*)m_file->data(), m_file->size());
auto elf = ELF::Image((const u8*)m_file->data(), m_file->size());
auto symbol = elf_loader->find_symbol(node.address());
auto symbol = elf.find_symbol(node.address());
if (!symbol.has_value())
return;
ASSERT(symbol.has_value());
auto view = symbol.value().raw_data();
X86::ELFSymbolProvider symbol_provider(*elf_loader);
X86::ELFSymbolProvider symbol_provider(elf);
X86::SimpleInstructionStream stream((const u8*)view.characters_without_null_termination(), view.length());
X86::Disassembler disassembler(stream);

View file

@ -33,7 +33,7 @@
#include <AK/RefPtr.h>
#include <LibCore/File.h>
#include <LibCoreDump/CoreDumpReader.h>
#include <LibELF/Loader.h>
#include <LibELF/Image.h>
#include <stdio.h>
#include <sys/stat.h>
@ -58,7 +58,7 @@ static String object_name(StringView memory_region_name)
struct CachedLibData {
OwnPtr<MappedFile> file;
NonnullRefPtr<ELF::Loader> lib_elf;
ELF::Image lib_elf;
};
static String symbolicate(FlatPtr eip, const ELF::Core::MemoryRegionInfo* region, u32& offset)
@ -86,13 +86,13 @@ static String symbolicate(FlatPtr eip, const ELF::Core::MemoryRegionInfo* region
auto lib_file = make<MappedFile>(path);
if (!lib_file->is_valid())
return {};
auto loader = ELF::Loader::create((const u8*)lib_file->data(), lib_file->size());
cached_libs.set(path, make<CachedLibData>(move(lib_file), loader));
auto image = ELF::Image((const u8*)lib_file->data(), lib_file->size());
cached_libs.set(path, make<CachedLibData>(move(lib_file), move(image)));
}
auto lib_data = cached_libs.get(path).value();
return String::format("[%s] %s", name.characters(), lib_data->lib_elf->symbolicate(eip - region->region_start, &offset).characters());
return String::format("[%s] %s", name.characters(), lib_data->lib_elf.symbolicate(eip - region->region_start, &offset).characters());
}
static String symbolicate_from_coredump(CoreDumpReader& coredump, u32 ptr, [[maybe_unused]] u32& offset)
@ -288,9 +288,9 @@ OwnPtr<Profile> Profile::load_from_perfcore_file(const StringView& path)
}
MappedFile kernel_elf_file("/boot/Kernel");
RefPtr<ELF::Loader> kernel_elf_loader;
OwnPtr<ELF::Image> kernel_elf;
if (kernel_elf_file.is_valid())
kernel_elf_loader = ELF::Loader::create(static_cast<const u8*>(kernel_elf_file.data()), kernel_elf_file.size());
kernel_elf = make<ELF::Image>(static_cast<const u8*>(kernel_elf_file.data()), kernel_elf_file.size());
auto events_value = object.get("events");
if (!events_value.is_array())
@ -325,8 +325,8 @@ OwnPtr<Profile> Profile::load_from_perfcore_file(const StringView& path)
String symbol;
if (ptr >= 0xc0000000) {
if (kernel_elf_loader) {
symbol = kernel_elf_loader->symbolicate(ptr, &offset);
if (kernel_elf) {
symbol = kernel_elf->symbolicate(ptr, &offset);
} else {
symbol = "??";
}

View file

@ -309,14 +309,13 @@ String Emulator::create_backtrace_line(FlatPtr address)
if (!mapped_file.is_valid())
return minimal;
auto loader = ELF::Loader::create((const u8*)mapped_file.data(), mapped_file.size());
auto debug_info = make<Debug::DebugInfo>(loader);
m_dynamic_library_cache.set(lib_path, CachedELF { move(mapped_file), move(loader), move(debug_info) });
auto debug_info = make<Debug::DebugInfo>(make<ELF::Image>((const u8*)mapped_file.data(), mapped_file.size()));
m_dynamic_library_cache.set(lib_path, CachedELF { move(mapped_file), move(debug_info) });
}
auto it = m_dynamic_library_cache.find(lib_path);
auto& loader = *it->value.elf_loader;
String symbol = loader.symbolicate(address - region->base());
auto& elf = it->value.debug_info->elf();
String symbol = elf.symbolicate(address - region->base());
auto line_without_source_info = String::format("=={%d}== %p [%s]: %s", getpid(), address, lib_name.characters(), symbol.characters());

View file

@ -34,7 +34,7 @@
#include <AK/Types.h>
#include <LibDebug/DebugInfo.h>
#include <LibELF/AuxiliaryVector.h>
#include <LibELF/Loader.h>
#include <LibELF/Image.h>
#include <LibX86/Instruction.h>
#include <signal.h>
#include <sys/types.h>
@ -208,8 +208,7 @@ private:
struct CachedELF {
MappedFile mapped_file;
NonnullRefPtr<ELF::Loader> elf_loader;
OwnPtr<Debug::DebugInfo> debug_info;
NonnullOwnPtr<Debug::DebugInfo> debug_info;
};
HashMap<String, CachedELF> m_dynamic_library_cache;

View file

@ -32,7 +32,7 @@
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <LibELF/Loader.h>
#include <LibELF/Image.h>
#include <getopt.h>
#include <pthread.h>
#include <string.h>