2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2018-11-04 13:52:53 +01:00
|
|
|
#include <AK/Function.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-05-23 16:42:13 +02:00
|
|
|
#if defined(KERNEL)
|
2019-06-07 12:56:50 +02:00
|
|
|
#include <Kernel/VirtualAddress.h>
|
2019-05-23 16:42:13 +02:00
|
|
|
#endif
|
|
|
|
#include <AK/ELF/ELFImage.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
class ELFLoader {
|
|
|
|
public:
|
2018-11-08 21:20:09 +01:00
|
|
|
explicit ELFLoader(const byte*);
|
2018-10-10 11:53:07 +02:00
|
|
|
~ELFLoader();
|
|
|
|
|
|
|
|
bool load();
|
2019-05-23 16:42:13 +02:00
|
|
|
#if defined(KERNEL)
|
2019-06-07 12:56:50 +02:00
|
|
|
Function<void*(VirtualAddress, size_t, size_t, bool, bool, const String&)> alloc_section_hook;
|
|
|
|
Function<void*(VirtualAddress, size_t, size_t, size_t, bool r, bool w, bool x, const String&)> map_section_hook;
|
|
|
|
VirtualAddress entry() const { return m_image.entry(); }
|
2019-05-23 16:42:13 +02:00
|
|
|
#endif
|
|
|
|
char* symbol_ptr(const char* name);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2019-05-18 14:48:53 +02:00
|
|
|
bool has_symbols() const { return m_image.symbol_count(); }
|
|
|
|
|
2019-05-16 17:18:25 +02:00
|
|
|
String symbolicate(dword address) const;
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
private:
|
2018-10-25 10:00:37 +02:00
|
|
|
bool layout();
|
2018-11-04 14:09:30 +01:00
|
|
|
bool perform_relocations();
|
2018-10-10 11:53:07 +02:00
|
|
|
void* lookup(const ELFImage::Symbol&);
|
2018-11-04 14:09:30 +01:00
|
|
|
char* area_for_section(const ELFImage::Section&);
|
|
|
|
char* area_for_section_name(const char*);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-11-04 13:52:53 +01:00
|
|
|
struct PtrAndSize {
|
|
|
|
PtrAndSize() { }
|
|
|
|
PtrAndSize(char* p, unsigned s)
|
|
|
|
: ptr(p)
|
|
|
|
, size(s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
char* ptr { nullptr };
|
|
|
|
unsigned size { 0 };
|
|
|
|
};
|
2018-11-04 14:09:30 +01:00
|
|
|
ELFImage m_image;
|
|
|
|
|
2019-05-16 17:18:25 +02:00
|
|
|
struct SortedSymbol {
|
|
|
|
dword address;
|
|
|
|
const char* name;
|
|
|
|
};
|
|
|
|
mutable Vector<SortedSymbol> m_sorted_symbols;
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|