Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / mozglue / linker / BaseElf.h
blobe76a756c7e50afec8b4ea41f17348180d0a83554
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 BaseElf_h
6 #define BaseElf_h
8 #include "ElfLoader.h"
9 #include "Elfxx.h"
11 /**
12 * Base class for ELF libraries. This class includes things that will be
13 * common between SystemElfs and CustomElfs.
15 class BaseElf : public LibHandle {
16 public:
17 /**
18 * Hash function for symbol lookup, as defined in ELF standard for System V.
20 static unsigned long Hash(const char* symbol);
22 /**
23 * Returns the address corresponding to the given symbol name (with a
24 * pre-computed hash).
26 void* GetSymbolPtr(const char* symbol, unsigned long hash) const;
28 /**
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) {}
37 protected:
38 /**
39 * Inherited from LibHandle. Those are temporary and are not supposed to
40 * be used.
42 virtual void* GetSymbolPtr(const char* symbol) const;
43 virtual bool Contains(void* addr) const;
44 virtual void* GetBase() const { return GetPtr(0); }
46 #ifdef __ARM_EABI__
47 virtual const void* FindExidx(int* pcount) const;
48 #endif
50 public:
51 /* private: */
52 /**
53 * Returns a pointer relative to the base address where the library is
54 * loaded.
56 void* GetPtr(const Elf::Addr offset) const {
57 if (reinterpret_cast<void*>(offset) > base)
58 return reinterpret_cast<void*>(offset);
59 return base + offset;
62 /**
63 * Like the above, but returns a typed (const) pointer
65 template <typename T>
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 */
78 MappedPtr base;
80 /* Buckets and chains for the System V symbol hash table */
81 Array<Elf::Word> buckets;
82 UnsizedArray<Elf::Word> chains;
84 /* protected: */
85 /* String table */
86 Elf::Strtab strtab;
88 /* Symbol table */
89 UnsizedArray<Elf::Sym> symtab;
91 #ifdef __ARM_EABI__
92 /* ARM.exidx information used by FindExidx */
93 Array<uint32_t[2]> arm_exidx;
94 #endif
97 /**
98 * Class for ELF libraries that already loaded in memory.
100 class LoadedElf : public BaseElf {
101 public:
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);
108 private:
109 explicit LoadedElf(const char* path) : BaseElf(path) {}
111 ~LoadedElf() {
112 /* Avoid base's destructor unmapping something that doesn't actually
113 * belong to it. */
114 base.release();
115 ElfLoader::Singleton.Forget(this);
119 * Initializes the library according to information found in the given
120 * PT_DYNAMIC header.
121 * Returns whether this succeeded or failed.
123 bool InitDyn(const Elf::Phdr* pt_dyn);
126 #endif /* BaseElf_h */