Bug 1845134 - Part 2: Import the new acorn-icons r=android-reviewers,007
[gecko.git] / mozglue / linker / CustomElf.h
blob12cff4ce5a8383b3b543a44fd08e5f918bc569f6
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef CustomElf_h
6 #define CustomElf_h
8 #include "ElfLoader.h"
9 #include "BaseElf.h"
10 #include "Logging.h"
11 #include "Elfxx.h"
13 /**
14 * Library Handle class for ELF libraries we don't let the system linker
15 * handle.
17 class CustomElf : public BaseElf, private ElfLoader::link_map {
18 friend class ElfLoader;
19 friend class SEGVHandler;
21 public:
22 /**
23 * Returns a new CustomElf using the given file descriptor to map ELF
24 * content. The file descriptor ownership is stolen, and it will be closed
25 * in CustomElf's destructor if an instance is created, or by the Load
26 * method otherwise. The path corresponds to the file descriptor, and flags
27 * are the same kind of flags that would be given to dlopen(), though
28 * currently, none are supported and the behaviour is more or less that of
29 * RTLD_GLOBAL | RTLD_BIND_NOW.
31 static already_AddRefed<LibHandle> Load(Mappable* mappable, const char* path,
32 int flags);
34 /**
35 * Inherited from LibHandle/BaseElf
37 virtual ~CustomElf();
39 public:
40 /**
41 * Returns the instance, casted as BaseElf. (short of a better way to do
42 * this without RTTI)
44 virtual BaseElf* AsBaseElf() { return this; }
46 private:
47 /**
48 * Scan dependent libraries to find the address corresponding to the
49 * given symbol name. This is used to find symbols that are undefined
50 * in the Elf object.
52 void* GetSymbolPtrInDeps(const char* symbol) const;
54 /**
55 * Private constructor
57 CustomElf(Mappable* mappable, const char* path)
58 : BaseElf(path, mappable),
59 link_map(),
60 init(0),
61 fini(0),
62 initialized(false),
63 has_text_relocs(false) {}
65 /**
66 * Loads an Elf segment defined by the given PT_LOAD header.
67 * Returns whether this succeeded or failed.
69 bool LoadSegment(const Elf::Phdr* pt_load) const;
71 /**
72 * Initializes the library according to information found in the given
73 * PT_DYNAMIC header.
74 * Returns whether this succeeded or failed.
76 bool InitDyn(const Elf::Phdr* pt_dyn);
78 /**
79 * Apply .rel.dyn/.rela.dyn relocations.
80 * Returns whether this succeeded or failed.
82 bool Relocate();
84 /**
85 * Apply .rel.plt/.rela.plt relocations.
86 * Returns whether this succeeded or failed.
88 bool RelocateJumps();
90 /**
91 * Call initialization functions (.init/.init_array)
92 * Returns true;
94 bool CallInit();
96 /**
97 * Call destructor functions (.fini_array/.fini)
98 * Returns whether this succeeded or failed.
100 void CallFini();
103 * Call a function given a pointer to its location.
105 void CallFunction(void* ptr) const {
106 /* C++ doesn't allow direct conversion between pointer-to-object
107 * and pointer-to-function. */
108 union {
109 void* ptr;
110 void (*func)(void);
111 } f;
112 f.ptr = ptr;
113 DEBUG_LOG("%s: Calling function @%p", GetPath(), ptr);
114 f.func();
118 * Call a function given a an address relative to the library base
120 void CallFunction(Elf::Addr addr) const { return CallFunction(GetPtr(addr)); }
122 /* List of dependent libraries */
123 std::vector<RefPtr<LibHandle> > dependencies;
125 /* List of .rel.dyn/.rela.dyn relocations */
126 Array<Elf::Reloc> relocations;
128 /* List of .rel.plt/.rela.plt relocation */
129 Array<Elf::Reloc> jumprels;
131 /* Relative address of the initialization and destruction functions
132 * (.init/.fini) */
133 Elf::Addr init, fini;
135 /* List of initialization and destruction functions
136 * (.init_array/.fini_array) */
137 Array<void*> init_array, fini_array;
139 bool initialized;
141 bool has_text_relocs;
144 #endif /* CustomElf_h */