michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef ElfLoader_h michael@0: #define ElfLoader_h michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "mozilla/RefPtr.h" michael@0: #include "Zip.h" michael@0: #include "Elfxx.h" michael@0: #include "Mappable.h" michael@0: michael@0: /** michael@0: * dlfcn.h replacement functions michael@0: */ michael@0: extern "C" { michael@0: void *__wrap_dlopen(const char *path, int flags); michael@0: const char *__wrap_dlerror(void); michael@0: void *__wrap_dlsym(void *handle, const char *symbol); michael@0: int __wrap_dlclose(void *handle); michael@0: michael@0: #ifndef HAVE_DLADDR michael@0: typedef struct { michael@0: const char *dli_fname; michael@0: void *dli_fbase; michael@0: const char *dli_sname; michael@0: void *dli_saddr; michael@0: } Dl_info; michael@0: #endif michael@0: int __wrap_dladdr(void *addr, Dl_info *info); michael@0: michael@0: struct dl_phdr_info { michael@0: Elf::Addr dlpi_addr; michael@0: const char *dlpi_name; michael@0: const Elf::Phdr *dlpi_phdr; michael@0: Elf::Half dlpi_phnum; michael@0: }; michael@0: michael@0: typedef int (*dl_phdr_cb)(struct dl_phdr_info *, size_t, void *); michael@0: int __wrap_dl_iterate_phdr(dl_phdr_cb callback, void *data); michael@0: michael@0: #ifdef __ARM_EABI__ michael@0: const void *__wrap___gnu_Unwind_Find_exidx(void *pc, int *pcount); michael@0: #endif michael@0: michael@0: /** michael@0: * faulty.lib public API michael@0: */ michael@0: MFBT_API size_t michael@0: __dl_get_mappable_length(void *handle); michael@0: michael@0: MFBT_API void * michael@0: __dl_mmap(void *handle, void *addr, size_t length, off_t offset); michael@0: michael@0: MFBT_API void michael@0: __dl_munmap(void *handle, void *addr, size_t length); michael@0: michael@0: MFBT_API bool michael@0: IsSignalHandlingBroken(); michael@0: michael@0: } michael@0: michael@0: /** michael@0: * Specialize RefCounted template for LibHandle. We may get references to michael@0: * LibHandles during the execution of their destructor, so we need michael@0: * RefCounted::Release to support some reentrancy. See further michael@0: * below. michael@0: */ michael@0: class LibHandle; michael@0: michael@0: namespace mozilla { michael@0: namespace detail { michael@0: michael@0: template <> inline void RefCounted::Release() const; michael@0: michael@0: template <> inline RefCounted::~RefCounted() michael@0: { michael@0: MOZ_ASSERT(refCnt == 0x7fffdead); michael@0: } michael@0: michael@0: } /* namespace detail */ michael@0: } /* namespace mozilla */ michael@0: michael@0: /** michael@0: * Abstract class for loaded libraries. Libraries may be loaded through the michael@0: * system linker or this linker, both cases will be derived from this class. michael@0: */ michael@0: class LibHandle: public mozilla::AtomicRefCounted michael@0: { michael@0: public: michael@0: MOZ_DECLARE_REFCOUNTED_TYPENAME(LibHandle) michael@0: /** michael@0: * Constructor. Takes the path of the loaded library and will store a copy michael@0: * of the leaf name. michael@0: */ michael@0: LibHandle(const char *path) michael@0: : directRefCnt(0), path(path ? strdup(path) : nullptr), mappable(nullptr) { } michael@0: michael@0: /** michael@0: * Destructor. michael@0: */ michael@0: virtual ~LibHandle(); michael@0: michael@0: /** michael@0: * Returns the pointer to the address to which the given symbol resolves michael@0: * inside the library. It is not supposed to resolve the symbol in other michael@0: * libraries, although in practice, it will for system libraries. michael@0: */ michael@0: virtual void *GetSymbolPtr(const char *symbol) const = 0; michael@0: michael@0: /** michael@0: * Returns whether the given address is part of the virtual address space michael@0: * covered by the loaded library. michael@0: */ michael@0: virtual bool Contains(void *addr) const = 0; michael@0: michael@0: /** michael@0: * Returns the file name of the library without the containing directory. michael@0: */ michael@0: const char *GetName() const; michael@0: michael@0: /** michael@0: * Returns the full path of the library, when available. Otherwise, returns michael@0: * the file name. michael@0: */ michael@0: const char *GetPath() const michael@0: { michael@0: return path; michael@0: } michael@0: michael@0: /** michael@0: * Library handles can be referenced from other library handles or michael@0: * externally (when dlopen()ing using this linker). We need to be michael@0: * able to distinguish between the two kind of referencing for better michael@0: * bookkeeping. michael@0: */ michael@0: void AddDirectRef() michael@0: { michael@0: ++directRefCnt; michael@0: mozilla::AtomicRefCounted::AddRef(); michael@0: } michael@0: michael@0: /** michael@0: * Releases a direct reference, and returns whether there are any direct michael@0: * references left. michael@0: */ michael@0: bool ReleaseDirectRef() michael@0: { michael@0: bool ret = false; michael@0: if (directRefCnt) { michael@0: MOZ_ASSERT(directRefCnt <= michael@0: mozilla::AtomicRefCounted::refCount()); michael@0: if (--directRefCnt) michael@0: ret = true; michael@0: mozilla::AtomicRefCounted::Release(); michael@0: } michael@0: return ret; michael@0: } michael@0: michael@0: /** michael@0: * Returns the number of direct references michael@0: */ michael@0: MozRefCountType DirectRefCount() michael@0: { michael@0: return directRefCnt; michael@0: } michael@0: michael@0: /** michael@0: * Returns the complete size of the file or stream behind the library michael@0: * handle. michael@0: */ michael@0: size_t GetMappableLength() const; michael@0: michael@0: /** michael@0: * Returns a memory mapping of the file or stream behind the library michael@0: * handle. michael@0: */ michael@0: void *MappableMMap(void *addr, size_t length, off_t offset) const; michael@0: michael@0: /** michael@0: * Unmaps a memory mapping of the file or stream behind the library michael@0: * handle. michael@0: */ michael@0: void MappableMUnmap(void *addr, size_t length) const; michael@0: michael@0: #ifdef __ARM_EABI__ michael@0: /** michael@0: * Find the address and entry count of the ARM.exidx section michael@0: * associated with the library michael@0: */ michael@0: virtual const void *FindExidx(int *pcount) const = 0; michael@0: #endif michael@0: michael@0: protected: michael@0: /** michael@0: * Returns a mappable object for use by MappableMMap and related functions. michael@0: */ michael@0: virtual Mappable *GetMappable() const = 0; michael@0: michael@0: /** michael@0: * Returns whether the handle is a SystemElf or not. (short of a better way michael@0: * to do this without RTTI) michael@0: */ michael@0: friend class ElfLoader; michael@0: friend class CustomElf; michael@0: friend class SEGVHandler; michael@0: virtual bool IsSystemElf() const { return false; } michael@0: michael@0: private: michael@0: MozRefCountType directRefCnt; michael@0: char *path; michael@0: michael@0: /* Mappable object keeping the result of GetMappable() */ michael@0: mutable mozilla::RefPtr mappable; michael@0: }; michael@0: michael@0: /** michael@0: * Specialized RefCounted::Release. Under normal operation, when michael@0: * refCnt reaches 0, the LibHandle is deleted. Its refCnt is however increased michael@0: * to 1 on normal builds, and 0x7fffdead on debug builds so that the LibHandle michael@0: * can still be referenced while the destructor is executing. The refCnt is michael@0: * allowed to grow > 0x7fffdead, but not to decrease under that value, which michael@0: * would mean too many Releases from within the destructor. michael@0: */ michael@0: namespace mozilla { michael@0: namespace detail { michael@0: michael@0: template <> inline void RefCounted::Release() const { michael@0: #ifdef DEBUG michael@0: if (refCnt > 0x7fff0000) michael@0: MOZ_ASSERT(refCnt > 0x7fffdead); michael@0: #endif michael@0: MOZ_ASSERT(refCnt > 0); michael@0: if (refCnt > 0) { michael@0: if (0 == --refCnt) { michael@0: #ifdef DEBUG michael@0: refCnt = 0x7fffdead; michael@0: #else michael@0: refCnt = 1; michael@0: #endif michael@0: delete static_cast(this); michael@0: } michael@0: } michael@0: } michael@0: michael@0: } /* namespace detail */ michael@0: } /* namespace mozilla */ michael@0: michael@0: /** michael@0: * Class handling libraries loaded by the system linker michael@0: */ michael@0: class SystemElf: public LibHandle michael@0: { michael@0: public: michael@0: /** michael@0: * Returns a new SystemElf for the given path. The given flags are passed michael@0: * to dlopen(). michael@0: */ michael@0: static mozilla::TemporaryRef Load(const char *path, int flags); michael@0: michael@0: /** michael@0: * Inherited from LibHandle michael@0: */ michael@0: virtual ~SystemElf(); michael@0: virtual void *GetSymbolPtr(const char *symbol) const; michael@0: virtual bool Contains(void *addr) const { return false; /* UNIMPLEMENTED */ } michael@0: michael@0: #ifdef __ARM_EABI__ michael@0: virtual const void *FindExidx(int *pcount) const; michael@0: #endif michael@0: michael@0: protected: michael@0: virtual Mappable *GetMappable() const; michael@0: michael@0: /** michael@0: * Returns whether the handle is a SystemElf or not. (short of a better way michael@0: * to do this without RTTI) michael@0: */ michael@0: friend class ElfLoader; michael@0: virtual bool IsSystemElf() const { return true; } michael@0: michael@0: /** michael@0: * Remove the reference to the system linker handle. This avoids dlclose() michael@0: * being called when the instance is destroyed. michael@0: */ michael@0: void Forget() michael@0: { michael@0: dlhandle = nullptr; michael@0: } michael@0: michael@0: private: michael@0: /** michael@0: * Private constructor michael@0: */ michael@0: SystemElf(const char *path, void *handle) michael@0: : LibHandle(path), dlhandle(handle) { } michael@0: michael@0: /* Handle as returned by system dlopen() */ michael@0: void *dlhandle; michael@0: }; michael@0: michael@0: /** michael@0: * The ElfLoader registers its own SIGSEGV handler to handle segmentation michael@0: * faults within the address space of the loaded libraries. It however michael@0: * allows a handler to be set for faults in other places, and redispatches michael@0: * to the handler set through signal() or sigaction(). michael@0: */ michael@0: class SEGVHandler michael@0: { michael@0: public: michael@0: bool hasRegisteredHandler() { michael@0: return registeredHandler; michael@0: } michael@0: michael@0: bool isSignalHandlingBroken() { michael@0: return signalHandlingBroken; michael@0: } michael@0: michael@0: protected: michael@0: SEGVHandler(); michael@0: ~SEGVHandler(); michael@0: michael@0: private: michael@0: static int __wrap_sigaction(int signum, const struct sigaction *act, michael@0: struct sigaction *oldact); michael@0: michael@0: /** michael@0: * SIGSEGV handler registered with __wrap_signal or __wrap_sigaction. michael@0: */ michael@0: struct sigaction action; michael@0: michael@0: /** michael@0: * ElfLoader SIGSEGV handler. michael@0: */ michael@0: static void handler(int signum, siginfo_t *info, void *context); michael@0: michael@0: /** michael@0: * Temporary test handler. michael@0: */ michael@0: static void test_handler(int signum, siginfo_t *info, void *context); michael@0: michael@0: /** michael@0: * Size of the alternative stack. The printf family requires more than 8KB michael@0: * of stack, and our signal handler may print a few things. michael@0: */ michael@0: static const size_t stackSize = 12 * 1024; michael@0: michael@0: /** michael@0: * Alternative stack information used before initialization. michael@0: */ michael@0: stack_t oldStack; michael@0: michael@0: /** michael@0: * Pointer to an alternative stack for signals. Only set if oldStack is michael@0: * not set or not big enough. michael@0: */ michael@0: MappedPtr stackPtr; michael@0: michael@0: bool registeredHandler; michael@0: bool signalHandlingBroken; michael@0: bool signalHandlingSlow; michael@0: }; michael@0: michael@0: /** michael@0: * Elf Loader class in charge of loading and bookkeeping libraries. michael@0: */ michael@0: class ElfLoader: public SEGVHandler michael@0: { michael@0: public: michael@0: /** michael@0: * The Elf Loader instance michael@0: */ michael@0: static ElfLoader Singleton; michael@0: michael@0: /** michael@0: * Loads the given library with the given flags. Equivalent to dlopen() michael@0: * The extra "parent" argument optionally gives the handle of the library michael@0: * requesting the given library to be loaded. The loader may look in the michael@0: * directory containing that parent library for the library to load. michael@0: */ michael@0: mozilla::TemporaryRef Load(const char *path, int flags, michael@0: LibHandle *parent = nullptr); michael@0: michael@0: /** michael@0: * Returns the handle of the library containing the given address in michael@0: * its virtual address space, i.e. the library handle for which michael@0: * LibHandle::Contains returns true. Its purpose is to allow to michael@0: * implement dladdr(). michael@0: */ michael@0: mozilla::TemporaryRef GetHandleByPtr(void *addr); michael@0: michael@0: /** michael@0: * Returns a Mappable object for the path. Paths in the form michael@0: * /foo/bar/baz/archive!/directory/lib.so michael@0: * try to load the directory/lib.so in /foo/bar/baz/archive, provided michael@0: * that file is a Zip archive. michael@0: */ michael@0: static Mappable *GetMappableFromPath(const char *path); michael@0: michael@0: protected: michael@0: /** michael@0: * Registers the given handle. This method is meant to be called by michael@0: * LibHandle subclass creators. michael@0: */ michael@0: void Register(LibHandle *handle); michael@0: michael@0: /** michael@0: * Forget about the given handle. This method is meant to be called by michael@0: * LibHandle subclass destructors. michael@0: */ michael@0: void Forget(LibHandle *handle); michael@0: michael@0: /* Last error. Used for dlerror() */ michael@0: friend class SystemElf; michael@0: friend const char *__wrap_dlerror(void); michael@0: friend void *__wrap_dlsym(void *handle, const char *symbol); michael@0: friend int __wrap_dlclose(void *handle); michael@0: const char *lastError; michael@0: michael@0: private: michael@0: ~ElfLoader(); michael@0: michael@0: /* Bookkeeping */ michael@0: typedef std::vector LibHandleList; michael@0: LibHandleList handles; michael@0: michael@0: protected: michael@0: friend class CustomElf; michael@0: /** michael@0: * Show some stats about Mappables in CustomElfs. The when argument is to michael@0: * be used by the caller to give an identifier of the when the stats call michael@0: * is made. michael@0: */ michael@0: static void stats(const char *when); michael@0: michael@0: /* Definition of static destructors as to be used for C++ ABI compatibility */ michael@0: typedef void (*Destructor)(void *object); michael@0: michael@0: /** michael@0: * C++ ABI makes static initializers register destructors through a specific michael@0: * atexit interface. On glibc/linux systems, the dso_handle is a pointer michael@0: * within a given library. On bionic/android systems, it is an undefined michael@0: * symbol. Making sense of the value is not really important, and all that michael@0: * is really important is that it is different for each loaded library, so michael@0: * that they can be discriminated when shutting down. For convenience, on michael@0: * systems where the dso handle is a symbol, that symbol is resolved to michael@0: * point at corresponding CustomElf. michael@0: * michael@0: * Destructors are registered with __*_atexit with an associated object to michael@0: * be passed as argument when it is called. michael@0: * michael@0: * When __cxa_finalize is called, destructors registered for the given michael@0: * DSO handle are called in the reverse order they were registered. michael@0: */ michael@0: #ifdef __ARM_EABI__ michael@0: static int __wrap_aeabi_atexit(void *that, Destructor destructor, michael@0: void *dso_handle); michael@0: #else michael@0: static int __wrap_cxa_atexit(Destructor destructor, void *that, michael@0: void *dso_handle); michael@0: #endif michael@0: michael@0: static void __wrap_cxa_finalize(void *dso_handle); michael@0: michael@0: /** michael@0: * Registered destructor. Keeps track of the destructor function pointer, michael@0: * associated object to call it with, and DSO handle. michael@0: */ michael@0: class DestructorCaller { michael@0: public: michael@0: DestructorCaller(Destructor destructor, void *object, void *dso_handle) michael@0: : destructor(destructor), object(object), dso_handle(dso_handle) { } michael@0: michael@0: /** michael@0: * Call the destructor function with the associated object. michael@0: * Call only once, see CustomElf::~CustomElf. michael@0: */ michael@0: void Call(); michael@0: michael@0: /** michael@0: * Returns whether the destructor is associated to the given DSO handle michael@0: */ michael@0: bool IsForHandle(void *handle) const michael@0: { michael@0: return handle == dso_handle; michael@0: } michael@0: michael@0: private: michael@0: Destructor destructor; michael@0: void *object; michael@0: void *dso_handle; michael@0: }; michael@0: michael@0: private: michael@0: /* Keep track of all registered destructors */ michael@0: std::vector destructors; michael@0: michael@0: /* Forward declaration, see further below */ michael@0: class DebuggerHelper; michael@0: public: michael@0: /* Loaded object descriptor for the debugger interface below*/ michael@0: struct link_map { michael@0: /* Base address of the loaded object. */ michael@0: const void *l_addr; michael@0: /* File name */ michael@0: const char *l_name; michael@0: /* Address of the PT_DYNAMIC segment. */ michael@0: const void *l_ld; michael@0: michael@0: private: michael@0: friend class ElfLoader::DebuggerHelper; michael@0: /* Double linked list of loaded objects. */ michael@0: link_map *l_next, *l_prev; michael@0: }; michael@0: michael@0: private: michael@0: /* Data structure used by the linker to give details about shared objects it michael@0: * loaded to debuggers. This is normally defined in link.h, but Android michael@0: * headers lack this file. */ michael@0: struct r_debug { michael@0: /* Version number of the protocol. */ michael@0: int r_version; michael@0: michael@0: /* Head of the linked list of loaded objects. */ michael@0: link_map *r_map; michael@0: michael@0: /* Function to be called when updates to the linked list of loaded objects michael@0: * are going to occur. The function is to be called before and after michael@0: * changes. */ michael@0: void (*r_brk)(void); michael@0: michael@0: /* Indicates to the debugger what state the linked list of loaded objects michael@0: * is in when the function above is called. */ michael@0: enum { michael@0: RT_CONSISTENT, /* Changes are complete */ michael@0: RT_ADD, /* Beginning to add a new object */ michael@0: RT_DELETE /* Beginning to remove an object */ michael@0: } r_state; michael@0: }; michael@0: michael@0: /* Helper class used to integrate libraries loaded by this linker in michael@0: * r_debug */ michael@0: class DebuggerHelper michael@0: { michael@0: public: michael@0: DebuggerHelper(); michael@0: michael@0: operator bool() michael@0: { michael@0: return dbg; michael@0: } michael@0: michael@0: /* Make the debugger aware of a new loaded object */ michael@0: void Add(link_map *map); michael@0: michael@0: /* Make the debugger aware of the unloading of an object */ michael@0: void Remove(link_map *map); michael@0: michael@0: /* Iterates over all link_maps */ michael@0: class iterator michael@0: { michael@0: public: michael@0: const link_map *operator ->() const michael@0: { michael@0: return item; michael@0: } michael@0: michael@0: const link_map &operator ++() michael@0: { michael@0: item = item->l_next; michael@0: return *item; michael@0: } michael@0: michael@0: bool operator<(const iterator &other) const michael@0: { michael@0: if (other.item == nullptr) michael@0: return item ? true : false; michael@0: MOZ_CRASH("DebuggerHelper::iterator::operator< called with something else than DebuggerHelper::end()"); michael@0: } michael@0: protected: michael@0: friend class DebuggerHelper; michael@0: iterator(const link_map *item): item(item) { } michael@0: michael@0: private: michael@0: const link_map *item; michael@0: }; michael@0: michael@0: iterator begin() const michael@0: { michael@0: return iterator(dbg ? dbg->r_map : nullptr); michael@0: } michael@0: michael@0: iterator end() const michael@0: { michael@0: return iterator(nullptr); michael@0: } michael@0: michael@0: private: michael@0: r_debug *dbg; michael@0: link_map *firstAdded; michael@0: }; michael@0: friend int __wrap_dl_iterate_phdr(dl_phdr_cb callback, void *data); michael@0: DebuggerHelper dbg; michael@0: }; michael@0: michael@0: #endif /* ElfLoader_h */