Bug 1858509 add thread-safety annotations around MediaSourceDemuxer::mMonitor r=alwu
[gecko.git] / mozglue / linker / BaseElf.h
blob9569dbc579ea103c6378531007ccb6648868e483
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 virtual Mappable* GetMappable() const { return NULL; };
52 public:
53 /* private: */
54 /**
55 * Returns a pointer relative to the base address where the library is
56 * loaded.
58 void* GetPtr(const Elf::Addr offset) const {
59 if (reinterpret_cast<void*>(offset) > base)
60 return reinterpret_cast<void*>(offset);
61 return base + offset;
64 /**
65 * Like the above, but returns a typed (const) pointer
67 template <typename T>
68 const T* GetPtr(const Elf::Addr offset) const {
69 if (reinterpret_cast<void*>(offset) > base)
70 return reinterpret_cast<const T*>(offset);
71 return reinterpret_cast<const T*>(base + offset);
74 /* Appropriated Mappable */
75 /* /!\ we rely on this being nullptr for BaseElf instances, but not
76 * CustomElf instances. */
77 RefPtr<Mappable> mappable;
79 /* Base address where the library is loaded */
80 MappedPtr base;
82 /* Buckets and chains for the System V symbol hash table */
83 Array<Elf::Word> buckets;
84 UnsizedArray<Elf::Word> chains;
86 /* protected: */
87 /* String table */
88 Elf::Strtab strtab;
90 /* Symbol table */
91 UnsizedArray<Elf::Sym> symtab;
93 #ifdef __ARM_EABI__
94 /* ARM.exidx information used by FindExidx */
95 Array<uint32_t[2]> arm_exidx;
96 #endif
99 /**
100 * Class for ELF libraries that already loaded in memory.
102 class LoadedElf : public BaseElf {
103 public:
105 * Returns a LoadedElf corresponding to the already loaded ELF
106 * at the given base address.
108 static already_AddRefed<LibHandle> Create(const char* path, void* base_addr);
110 private:
111 explicit LoadedElf(const char* path) : BaseElf(path) {}
113 ~LoadedElf() {
114 /* Avoid base's destructor unmapping something that doesn't actually
115 * belong to it. */
116 base.release();
117 ElfLoader::Singleton.Forget(this);
121 * Initializes the library according to information found in the given
122 * PT_DYNAMIC header.
123 * Returns whether this succeeded or failed.
125 bool InitDyn(const Elf::Phdr* pt_dyn);
128 #endif /* BaseElf_h */