Bug 752461 - Hide click-to-play overlays when choosing "never activate plugins.....
[gecko.git] / mozglue / linker / CustomElf.h
blob74f789b27c3fceadd833eb3760045c4c3e4a07ec
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 CustomElf_h
6 #define CustomElf_h
8 #include "ElfLoader.h"
9 #include "Logging.h"
10 #include "Elfxx.h"
12 class Mappable;
14 /**
15 * Library Handle class for ELF libraries we don't let the system linker
16 * handle.
18 class CustomElf: public LibHandle, private ElfLoader::link_map
20 friend class ElfLoader;
21 friend class SEGVHandler;
22 public:
23 /**
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);
35 /**
36 * Inherited from LibHandle
38 virtual ~CustomElf();
39 virtual void *GetSymbolPtr(const char *symbol) const;
40 virtual bool Contains(void *addr) const;
42 /**
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
45 * made.
47 void stats(const char *when) const;
49 private:
50 /**
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;
56 /**
57 * Returns the address corresponding to the given symbol name (with a
58 * pre-computed hash).
60 void *GetSymbolPtr(const char *symbol, unsigned long hash) const;
62 /**
63 * Scan dependent libraries to find the address corresponding to the
64 * given symbol name. This is used to find symbols that are undefined
65 * in the Elf object.
67 void *GetSymbolPtrInDeps(const char *symbol) const;
69 /**
70 * Private constructor
72 CustomElf(Mappable *mappable, const char *path)
73 : LibHandle(path), mappable(mappable), init(0), fini(0), initialized(false)
74 { }
76 /**
77 * Returns a pointer relative to the base address where the library is
78 * loaded.
80 void *GetPtr(const Elf::Addr offset) const
82 return base + offset;
85 /**
86 * Like the above, but returns a typed (const) pointer
88 template <typename T>
89 const T *GetPtr(const Elf::Addr offset) const
91 return reinterpret_cast<const T *>(base + offset);
94 /**
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
102 * PT_DYNAMIC header.
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.
111 bool Relocate();
114 * Apply .rel.plt/.rela.plt relocations.
115 * Returns whether this succeeded or failed.
117 bool RelocateJumps();
120 * Call initialization functions (.init/.init_array)
121 * Returns true;
123 bool CallInit();
126 * Call destructor functions (.fini_array/.fini)
127 * Returns whether this succeeded or failed.
129 void CallFini();
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. */
138 union {
139 void *ptr;
140 void (*func)(void);
141 } f;
142 f.ptr = ptr;
143 debug("%s: Calling function @%p", GetPath(), ptr);
144 f.func();
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 */
156 Mappable *mappable;
158 /* Base address where the library is loaded */
159 MappedPtr base;
161 /* String table */
162 Elf::Strtab strtab;
164 /* Symbol table */
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
181 * (.init/.fini) */
182 Elf::Addr init, fini;
184 /* List of initialization and destruction functions
185 * (.init_array/.fini_array) */
186 Array<void *> init_array, fini_array;
188 bool initialized;
191 #endif /* CustomElf_h */