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/. */
13 * Library Handle class for ELF libraries we don't let the system linker
16 class CustomElf
: public LibHandle
, private ElfLoader::link_map
18 friend class ElfLoader
;
19 friend class SEGVHandler
;
22 * Returns a new CustomElf using the given file descriptor to map ELF
23 * content. The file descriptor ownership is stolen, and it will be closed
24 * in CustomElf's destructor if an instance is created, or by the Load
25 * method otherwise. The path corresponds to the file descriptor, and flags
26 * are the same kind of flags that would be given to dlopen(), though
27 * currently, none are supported and the behaviour is more or less that of
28 * RTLD_GLOBAL | RTLD_BIND_NOW.
30 static mozilla::TemporaryRef
<LibHandle
> Load(Mappable
*mappable
,
31 const char *path
, int flags
);
34 * Inherited from LibHandle
37 virtual void *GetSymbolPtr(const char *symbol
) const;
38 virtual bool Contains(void *addr
) const;
41 virtual const void *FindExidx(int *pcount
) const;
45 virtual Mappable
*GetMappable() const;
49 * Shows some stats about the Mappable instance. The when argument is to be
50 * used by the caller to give an identifier of the when the stats call is
53 void stats(const char *when
) const;
57 * Returns a pointer to the Elf Symbol in the Dynamic Symbol table
58 * corresponding to the given symbol name (with a pre-computed hash).
60 const Elf::Sym
*GetSymbol(const char *symbol
, unsigned long hash
) const;
63 * Returns the address corresponding to the given symbol name (with a
66 void *GetSymbolPtr(const char *symbol
, unsigned long hash
) const;
69 * Scan dependent libraries to find the address corresponding to the
70 * given symbol name. This is used to find symbols that are undefined
73 void *GetSymbolPtrInDeps(const char *symbol
) const;
78 CustomElf(Mappable
*mappable
, const char *path
)
84 , has_text_relocs(false)
88 * Returns a pointer relative to the base address where the library is
91 void *GetPtr(const Elf::Addr offset
) const
97 * Like the above, but returns a typed (const) pointer
100 const T
*GetPtr(const Elf::Addr offset
) const
102 return reinterpret_cast<const T
*>(base
+ offset
);
106 * Loads an Elf segment defined by the given PT_LOAD header.
107 * Returns whether this succeeded or failed.
109 bool LoadSegment(const Elf::Phdr
*pt_load
) const;
112 * Initializes the library according to information found in the given
114 * Returns whether this succeeded or failed.
116 bool InitDyn(const Elf::Phdr
*pt_dyn
);
119 * Apply .rel.dyn/.rela.dyn relocations.
120 * Returns whether this succeeded or failed.
125 * Apply .rel.plt/.rela.plt relocations.
126 * Returns whether this succeeded or failed.
128 bool RelocateJumps();
131 * Call initialization functions (.init/.init_array)
137 * Call destructor functions (.fini_array/.fini)
138 * Returns whether this succeeded or failed.
143 * Call a function given a pointer to its location.
145 void CallFunction(void *ptr
) const
147 /* C++ doesn't allow direct conversion between pointer-to-object
148 * and pointer-to-function. */
154 DEBUG_LOG("%s: Calling function @%p", GetPath(), ptr
);
159 * Call a function given a an address relative to the library base
161 void CallFunction(Elf::Addr addr
) const
163 return CallFunction(GetPtr(addr
));
166 /* Appropriated Mappable */
167 mozilla::RefPtr
<Mappable
> mappable
;
169 /* Base address where the library is loaded */
176 UnsizedArray
<Elf::Sym
> symtab
;
178 /* Buckets and chains for the System V symbol hash table */
179 Array
<Elf::Word
> buckets
;
180 UnsizedArray
<Elf::Word
> chains
;
182 /* List of dependent libraries */
183 std::vector
<mozilla::RefPtr
<LibHandle
> > dependencies
;
185 /* List of .rel.dyn/.rela.dyn relocations */
186 Array
<Elf::Reloc
> relocations
;
188 /* List of .rel.plt/.rela.plt relocation */
189 Array
<Elf::Reloc
> jumprels
;
191 /* Relative address of the initialization and destruction functions
193 Elf::Addr init
, fini
;
195 /* List of initialization and destruction functions
196 * (.init_array/.fini_array) */
197 Array
<void *> init_array
, fini_array
;
201 bool has_text_relocs
;
204 /* ARM.exidx information used by FindExidx */
205 Array
<uint32_t[2]> arm_exidx
;
209 #endif /* CustomElf_h */