summaryrefslogtreecommitdiff
path: root/AK/ELF/ELFLoader.h
blob: ef02e8eab89d9085bc1d7682a0ea9cf3eb6fcd10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
#include <AK/ELF/ELFImage.h>

#ifdef KERNEL
#include <Kernel/VM/VirtualAddress.h>
class Region;
#endif

class ELFLoader {
public:
    explicit ELFLoader(const u8*);
    ~ELFLoader();

    bool load();
#if defined(KERNEL)
    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(); }
#endif
    char* symbol_ptr(const char* name);

    bool has_symbols() const { return m_image.symbol_count(); }

    String symbolicate(u32 address) const;

private:
    bool layout();
    bool perform_relocations();
    void* lookup(const ELFImage::Symbol&);
    char* area_for_section(const ELFImage::Section&);
    char* area_for_section_name(const char*);

    struct PtrAndSize {
        PtrAndSize() {}
        PtrAndSize(char* p, unsigned s)
            : ptr(p)
            , size(s)
        {
        }

        char* ptr { nullptr };
        unsigned size { 0 };
    };
    ELFImage m_image;

    struct SortedSymbol {
        u32 address;
        const char* name;
    };
#ifdef KERNEL
    mutable RefPtr<Region> m_sorted_symbols_region;
#else
    mutable Vector<SortedSymbol> m_sorted_symbols;
#endif
};