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/. */
14 * Library Handle class for ELF libraries we don't let the system linker
17 class CustomElf
: public BaseElf
, private ElfLoader::link_map
{
18 friend class ElfLoader
;
19 friend class SEGVHandler
;
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
,
35 * Inherited from LibHandle/BaseElf
41 * Returns the instance, casted as BaseElf. (short of a better way to do
44 virtual BaseElf
* AsBaseElf() { return this; }
48 * Scan dependent libraries to find the address corresponding to the
49 * given symbol name. This is used to find symbols that are undefined
52 void* GetSymbolPtrInDeps(const char* symbol
) const;
57 CustomElf(Mappable
* mappable
, const char* path
)
58 : BaseElf(path
, mappable
),
63 has_text_relocs(false) {}
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;
72 * Initializes the library according to information found in the given
74 * Returns whether this succeeded or failed.
76 bool InitDyn(const Elf::Phdr
* pt_dyn
);
79 * Apply .rel.dyn/.rela.dyn relocations.
80 * Returns whether this succeeded or failed.
85 * Apply .rel.plt/.rela.plt relocations.
86 * Returns whether this succeeded or failed.
91 * Call initialization functions (.init/.init_array)
97 * Call destructor functions (.fini_array/.fini)
98 * Returns whether this succeeded or failed.
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. */
113 DEBUG_LOG("%s: Calling function @%p", GetPath(), ptr
);
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
133 Elf::Addr init
, fini
;
135 /* List of initialization and destruction functions
136 * (.init_array/.fini_array) */
137 Array
<void*> init_array
, fini_array
;
141 bool has_text_relocs
;
144 #endif /* CustomElf_h */