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/. */
12 * Base class for ELF libraries. This class includes things that will be
13 * common between SystemElfs and CustomElfs.
15 class BaseElf
: public LibHandle
{
18 * Hash function for symbol lookup, as defined in ELF standard for System V.
20 static unsigned long Hash(const char* symbol
);
23 * Returns the address corresponding to the given symbol name (with a
26 void* GetSymbolPtr(const char* symbol
, unsigned long hash
) const;
29 * Returns a pointer to the Elf Symbol in the Dynamic Symbol table
30 * corresponding to the given symbol name (with a pre-computed hash).
32 const Elf::Sym
* GetSymbol(const char* symbol
, unsigned long hash
) const;
34 explicit BaseElf(const char* path
, Mappable
* mappable
= nullptr)
35 : LibHandle(path
), mappable(mappable
) {}
39 * Inherited from LibHandle. Those are temporary and are not supposed to
42 virtual void* GetSymbolPtr(const char* symbol
) const;
43 virtual bool Contains(void* addr
) const;
44 virtual void* GetBase() const { return GetPtr(0); }
47 virtual const void* FindExidx(int* pcount
) const;
53 * Returns a pointer relative to the base address where the library is
56 void* GetPtr(const Elf::Addr offset
) const {
57 if (reinterpret_cast<void*>(offset
) > base
)
58 return reinterpret_cast<void*>(offset
);
63 * Like the above, but returns a typed (const) pointer
66 const T
* GetPtr(const Elf::Addr offset
) const {
67 if (reinterpret_cast<void*>(offset
) > base
)
68 return reinterpret_cast<const T
*>(offset
);
69 return reinterpret_cast<const T
*>(base
+ offset
);
72 /* Appropriated Mappable */
73 /* /!\ we rely on this being nullptr for BaseElf instances, but not
74 * CustomElf instances. */
75 RefPtr
<Mappable
> mappable
;
77 /* Base address where the library is loaded */
80 /* Buckets and chains for the System V symbol hash table */
81 Array
<Elf::Word
> buckets
;
82 UnsizedArray
<Elf::Word
> chains
;
89 UnsizedArray
<Elf::Sym
> symtab
;
92 /* ARM.exidx information used by FindExidx */
93 Array
<uint32_t[2]> arm_exidx
;
98 * Class for ELF libraries that already loaded in memory.
100 class LoadedElf
: public BaseElf
{
103 * Returns a LoadedElf corresponding to the already loaded ELF
104 * at the given base address.
106 static already_AddRefed
<LibHandle
> Create(const char* path
, void* base_addr
);
109 explicit LoadedElf(const char* path
) : BaseElf(path
) {}
112 /* Avoid base's destructor unmapping something that doesn't actually
115 ElfLoader::Singleton
.Forget(this);
119 * Initializes the library according to information found in the given
121 * Returns whether this succeeded or failed.
123 bool InitDyn(const Elf::Phdr
* pt_dyn
);
126 #endif /* BaseElf_h */