| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 | |
| 15 #ifndef CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SYMBOL_TABLE_READER_H_ | |
| 16 #define CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SYMBOL_TABLE_READER_H_ | |
| 17 | |
| 18 #include "base/basictypes.h" | |
| 19 | |
| 20 #include <map> | |
| 21 #include <string> | |
| 22 | |
| 23 #include <mach/mach.h> | |
| 24 #include <stdint.h> | |
| 25 | |
| 26 #include "util/mac/mach_o_image_segment_reader.h" | |
| 27 #include "util/mac/process_reader.h" | |
| 28 #include "util/mac/process_types.h" | |
| 29 #include "util/misc/initialization_state_dcheck.h" | |
| 30 | |
| 31 namespace crashpad { | |
| 32 | |
| 33 //! \brief A reader for symbol tables in Mach-O images mapped into another | |
| 34 //! process. | |
| 35 class MachOImageSymbolTableReader { | |
| 36 public: | |
| 37 //! \brief Information about a symbol in a module’s symbol table. | |
| 38 //! | |
| 39 //! This is a more minimal form of the `nlist` (or `nlist_64`) structure, | |
| 40 //! only containing the equivalent of the `n_value` and `n_sect` fields. | |
| 41 struct SymbolInformation { | |
| 42 //! \brief The address of the symbol as it exists in the symbol table, not | |
| 43 //! adjusted for any “slide.” | |
| 44 mach_vm_address_t value; | |
| 45 | |
| 46 //! \brief The 1-based section index in the module in which the symbol is | |
| 47 //! found. | |
| 48 //! | |
| 49 //! For symbols defined in a section (`N_SECT`), this is the section index | |
| 50 //! that can be passed to MachOImageReader::GetSectionAtIndex(), and \a | |
| 51 //! value will need to be adjusted for segment slide if the containing | |
| 52 //! segment slid when loaded. For absolute symbols (`N_ABS`), this will be | |
| 53 //! `NO_SECT` (`0`), and \a value must not be adjusted for segment slide. | |
| 54 uint8_t section; | |
| 55 }; | |
| 56 | |
| 57 // TODO(mark): Use unordered_map or a similar hash-based map? For now, | |
| 58 // std::map is fine because this map only stores external defined symbols, | |
| 59 // and there aren’t expected to be very many of those that performance would | |
| 60 // become a problem. std::map is also guaranteed to be part of the standard | |
| 61 // library, which isn’t the case for std::unordered_map, which requires the | |
| 62 // C++11 library. In reality, std::unordered_map does not appear to provide | |
| 63 // a performance advantage. It appears that the memory copies currently done | |
| 64 // by TaskMemory::Read() have substantially more impact on symbol table | |
| 65 // operations. | |
| 66 // | |
| 67 // This is public so that the type is available to | |
| 68 // MachOImageSymbolTableReaderInitializer. | |
| 69 typedef std::map<std::string, SymbolInformation> SymbolInformationMap; | |
| 70 | |
| 71 MachOImageSymbolTableReader(); | |
| 72 ~MachOImageSymbolTableReader(); | |
| 73 | |
| 74 //! \brief Reads the symbol table from another process. | |
| 75 //! | |
| 76 //! This method must only be called once on an object. This method must be | |
| 77 //! called successfully before any other method in this class may be called. | |
| 78 //! | |
| 79 //! \param[in] process_reader The reader for the remote process. | |
| 80 //! \param[in] symtab_command The `LC_SYMTAB` load command that identifies | |
| 81 //! the symbol table. | |
| 82 //! \param[in] dysymtab_command The `LC_DYSYMTAB` load command that identifies | |
| 83 //! dynamic symbol information within the symbol table. This load command | |
| 84 //! is not present in all modules, and this parameter may be `nullptr` for | |
| 85 //! modules that do not have this information. When present, \a | |
| 86 //! dysymtab_command is an optimization that allows the symbol table | |
| 87 //! reader to only examine symbol table entries known to be relevant for | |
| 88 //! its purposes. | |
| 89 //! \param[in] linkedit_segment The `__LINKEDIT` segment. This segment should | |
| 90 //! contain the data referenced by \a symtab_command and \a | |
| 91 //! dysymtab_command. This may be any segment in the module, but by | |
| 92 //! convention, the name `__LINKEDIT` is used for this purpose. | |
| 93 //! \param[in] module_info A string to be used in logged messages. This string | |
| 94 //! is for diagnostic purposes only, and may be empty. | |
| 95 //! | |
| 96 //! \return `true` if the symbol table was read successfully. `false` | |
| 97 //! otherwise, with an appropriate message logged. | |
| 98 bool Initialize(ProcessReader* process_reader, | |
| 99 const process_types::symtab_command* symtab_command, | |
| 100 const process_types::dysymtab_command* dysymtab_command, | |
| 101 const MachOImageSegmentReader* linkedit_segment, | |
| 102 const std::string& module_info); | |
| 103 | |
| 104 //! \brief Looks up a symbol in the image’s symbol table. | |
| 105 //! | |
| 106 //! The returned information captures the symbol as it exists in the image’s | |
| 107 //! symbol table, not adjusted for any “slide.” | |
| 108 //! | |
| 109 //! \param[in] name The name of the symbol to look up, “mangled” or | |
| 110 //! “decorated” appropriately. For example, use `"_main"` to look up the | |
| 111 //! symbol for the C `main()` function, and use `"__Z4Funcv"` to look up | |
| 112 //! the symbol for the C++ `Func()` function. | |
| 113 //! | |
| 114 //! \return A SymbolInformation* object with information about the symbol if | |
| 115 //! it was found, or `nullptr` if the symbol was not found or if an error | |
| 116 //! occurred. On error, a warning message will also be logged. The caller | |
| 117 //! does not take ownership; the lifetime of the returned object is scoped | |
| 118 //! to the lifetime of this MachOImageSymbolTableReader object. | |
| 119 //! | |
| 120 //! \note Symbol values returned via this interface are not adjusted for | |
| 121 //! “slide.” For slide-adjusted values, use the higher-level | |
| 122 //! MachOImageReader::LookUpExternalDefinedSymbol() interface. | |
| 123 const SymbolInformation* LookUpExternalDefinedSymbol( | |
| 124 const std::string& name) const; | |
| 125 | |
| 126 private: | |
| 127 SymbolInformationMap external_defined_symbols_; | |
| 128 InitializationStateDcheck initialized_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(MachOImageSymbolTableReader); | |
| 131 }; | |
| 132 | |
| 133 } // namespace crashpad | |
| 134 | |
| 135 #endif // CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SYMBOL_TABLE_READER_H_ | |
| OLD | NEW |