1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CRAZY_LINKER_LIBRARY_LIST_H 6 #define CRAZY_LINKER_LIBRARY_LIST_H 7 8 #include <link.h> 9 10 #include "crazy_linker_error.h" 11 #include "crazy_linker_search_path_list.h" 12 #include "elf_traits.h" 13 14 // This header contains definitions related to the global list of 15 // library views maintained by the crazy linker. Each library view 16 // points to either a crazy library, or a system one. 17 18 namespace crazy { 19 20 class SharedLibrary; 21 class LibraryView; 22 23 // The list of all shared libraries loaded by the crazy linker. 24 // IMPORTANT: This class is not thread-safe! 25 class LibraryList { 26 public: 27 LibraryList(); 28 ~LibraryList(); 29 30 // Find a library in the list by its base name. 31 // |base_name| must not contain a directory separator. 32 LibraryView* FindLibraryByName(const char* base_name); 33 34 // Lookup for a given |symbol_name|, starting from |from_lib| 35 // then through its dependencies in breadth-first search order. 36 // On failure, returns NULL. 37 void* FindSymbolFrom(const char* symbol_name, LibraryView* from_lib); 38 39 // Return the address of a visible given symbol. Used to implement 40 // the dlsym() wrapper. Returns NULL on failure. 41 void* FindAddressForSymbol(const char* symbol_name); 42 43 // Find a SharedLibrary that contains a given address, or NULL if none 44 // could be found. This simply scans all libraries. 45 LibraryView* FindLibraryForAddress(void* address); 46 47 #ifdef __arm__ 48 // Find the base address of the .ARM.exidx section corresponding 49 // to the address |pc|, as well as the number of 8-byte entries in 50 // the table into |*count|. Used to implement the wrapper for 51 // dl_unwind_find_exidx(). 52 _Unwind_Ptr FindArmExIdx(void* pc, int* count); 53 #else 54 typedef int (*PhdrIterationCallback)(dl_phdr_info* info, 55 size_t info_size, 56 void* data); 57 58 // Loop over all loaded libraries and call the |cb| callback 59 // on each iteration. If the function returns 0, stop immediately 60 // and return its value. Used to implement the wrapper for 61 // dl_iterate_phdr(). 62 int IteratePhdr(PhdrIterationCallback callback, void* data); 63 #endif 64 65 // Try to load a library, possibly at a fixed address. 66 // On failure, returns NULL and sets the |error| message. 67 LibraryView* LoadLibrary(const char* path, 68 int dlopen_flags, 69 uintptr_t load_address, 70 off_t file_offset, 71 SearchPathList* search_path_list, 72 Error* error); 73 74 // Unload a given shared library. This really decrements the library's 75 // internal reference count. When it reaches zero, the library's 76 // destructors are run, its dependencies are unloaded, then the 77 // library is removed from memory. 78 void UnloadLibrary(LibraryView* lib); 79 80 // Used internally by the wrappers only. 81 void AddLibrary(LibraryView* lib); 82 83 private: 84 LibraryList(const LibraryList&); 85 LibraryList& operator=(const LibraryList&); 86 87 void ClearError(); 88 89 // The list of all known libraries. 90 Vector<LibraryView*> known_libraries_; 91 92 LibraryView* FindKnownLibrary(const char* name); 93 94 // The list of all libraries loaded by the crazy linker. 95 // This does _not_ include system libraries present in known_libraries_. 96 SharedLibrary* head_; 97 98 size_t count_; 99 bool has_error_; 100 char error_buffer_[512]; 101 }; 102 103 } // namespace crazy 104 105 #endif // CRAZY_LINKER_LIBRARY_LIST_H