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/. */
15 * Library Handle class for ELF libraries we don't let the system linker
18 class CustomElf
: public LibHandle
, private ElfLoader::link_map
20 friend class ElfLoader
;
21 friend class SEGVHandler
;
24 * Returns a new CustomElf using the given file descriptor to map ELF
25 * content. The file descriptor ownership is stolen, and it will be closed
26 * in CustomElf's destructor if an instance is created, or by the Load
27 * method otherwise. The path corresponds to the file descriptor, and flags
28 * are the same kind of flags that would be given to dlopen(), though
29 * currently, none are supported and the behaviour is more or less that of
30 * RTLD_GLOBAL | RTLD_BIND_NOW.
32 static mozilla::TemporaryRef
<LibHandle
> Load(Mappable
*mappable
,
33 const char *path
, int flags
);
36 * Inherited from LibHandle
39 virtual void *GetSymbolPtr(const char *symbol
) const;
40 virtual bool Contains(void *addr
) const;
43 * Shows some stats about the Mappable instance. The when argument is to be
44 * used by the caller to give an identifier of the when the stats call is
47 void stats(const char *when
) const;
51 * Returns a pointer to the Elf Symbol in the Dynamic Symbol table
52 * corresponding to the given symbol name (with a pre-computed hash).
54 const Elf::Sym
*GetSymbol(const char *symbol
, unsigned long hash
) const;
57 * Returns the address corresponding to the given symbol name (with a
60 void *GetSymbolPtr(const char *symbol
, unsigned long hash
) const;
63 * Scan dependent libraries to find the address corresponding to the
64 * given symbol name. This is used to find symbols that are undefined
67 void *GetSymbolPtrInDeps(const char *symbol
) const;
72 CustomElf(Mappable
*mappable
, const char *path
)
73 : LibHandle(path
), mappable(mappable
), init(0), fini(0), initialized(false)
77 * Returns a pointer relative to the base address where the library is
80 void *GetPtr(const Elf::Addr offset
) const
86 * Like the above, but returns a typed (const) pointer
89 const T
*GetPtr(const Elf::Addr offset
) const
91 return reinterpret_cast<const T
*>(base
+ offset
);
95 * Loads an Elf segment defined by the given PT_LOAD header.
96 * Returns whether this succeeded or failed.
98 bool LoadSegment(const Elf::Phdr
*pt_load
) const;
101 * Initializes the library according to information found in the given
103 * Returns whether this succeeded or failed.
105 bool InitDyn(const Elf::Phdr
*pt_dyn
);
108 * Apply .rel.dyn/.rela.dyn relocations.
109 * Returns whether this succeeded or failed.
114 * Apply .rel.plt/.rela.plt relocations.
115 * Returns whether this succeeded or failed.
117 bool RelocateJumps();
120 * Call initialization functions (.init/.init_array)
126 * Call destructor functions (.fini_array/.fini)
127 * Returns whether this succeeded or failed.
132 * Call a function given a pointer to its location.
134 void CallFunction(void *ptr
) const
136 /* C++ doesn't allow direct conversion between pointer-to-object
137 * and pointer-to-function. */
143 debug("%s: Calling function @%p", GetPath(), ptr
);
148 * Call a function given a an address relative to the library base
150 void CallFunction(Elf::Addr addr
) const
152 return CallFunction(GetPtr(addr
));
155 /* Appropriated Mappable */
158 /* Base address where the library is loaded */
165 UnsizedArray
<Elf::Sym
> symtab
;
167 /* Buckets and chains for the System V symbol hash table */
168 Array
<Elf::Word
> buckets
;
169 UnsizedArray
<Elf::Word
> chains
;
171 /* List of dependent libraries */
172 std::vector
<mozilla::RefPtr
<LibHandle
> > dependencies
;
174 /* List of .rel.dyn/.rela.dyn relocations */
175 Array
<Elf::Reloc
> relocations
;
177 /* List of .rel.plt/.rela.plt relocation */
178 Array
<Elf::Reloc
> jumprels
;
180 /* Relative address of the initialization and destruction functions
182 Elf::Addr init
, fini
;
184 /* List of initialization and destruction functions
185 * (.init_array/.fini_array) */
186 Array
<void *> init_array
, fini_array
;
191 #endif /* CustomElf_h */