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 namespace internal { | |
34 class MachOImageSymbolTableReaderInitializer; | |
35 } // namespace internal | |
36 | |
37 //! \brief A reader for symbol tables in Mach-O images mapped into another | |
38 //! process. | |
39 class MachOImageSymbolTableReader { | |
40 public: | |
41 //! \brief Information about a symbol in a module’s symbol table. | |
42 //! | |
43 //! This is a more minimal form of the `nlist` (or `nlist_64`) structure, | |
44 //! only containing the equivalent of the `n_value` and `n_sect` fields. | |
45 struct SymbolInformation { | |
Robert Sesek
2014/09/05 19:04:16
For ProcessReader, you didn't use a nested struct
Mark Mentovai
2014/09/05 20:22:31
rsesek wrote:
| |
46 //! \brief The address of the symbol as it exists in the symbol table, not | |
47 //! adjusted for any “slide.” | |
48 mach_vm_address_t value; | |
49 | |
50 //! \brief The 1-based section index in the module in which the symbol is | |
51 //! found. | |
52 //! | |
53 //! For symbols defined in a section (`N_SECT`), this is the section index | |
54 //! that can be passed to MachOImageReader::GetSectionAtIndex(), and \a | |
55 //! value will need to be adjusted for segment slide if the containing | |
56 //! segment slid when loaded. For absolute symbols (`N_ABS`), this will be | |
57 //! `NO_SECT` (`0`), and \a value must not be adjusted for segment slide. | |
58 uint8_t section; | |
59 }; | |
60 | |
61 MachOImageSymbolTableReader(); | |
62 ~MachOImageSymbolTableReader(); | |
63 | |
64 //! \brief Reads the symbol table from another process. | |
65 //! | |
66 //! This method must only be called once on an object. This method must be | |
67 //! called successfully before any other method in this class may be called. | |
68 //! | |
69 //! \param[in] process_reader The reader for the remote process. | |
70 //! \param[in] symtab_command The `LC_SYMTAB` load command that identifies | |
71 //! the symbol table. | |
72 //! \param[in] dysymtab_command The `LC_DYSYMTAB` load command that identifies | |
73 //! dynamic symbol information within the symbol table. This load command | |
74 //! is not present in all modules, and this parameter may be `NULL` for | |
75 //! modules that do not have this information. When present, \a | |
76 //! dysymtab_command is an optimization that allows the symbol table | |
77 //! reader to only examine symbol table entries known to be relevant for | |
78 //! its purposes. | |
79 //! \param[in] linkedit_segment The `__LINKEDIT` segment. This segment should | |
80 //! contain the data referenced by \a symtab_command and \a | |
81 //! dysymtab_command. This may be any segment in the module, but by | |
82 //! convention, the name `__LINKEDIT` is used for this purpose. | |
83 //! \param[in] linkedit_address The address where \a linkedit_segment is | |
84 //! loaded in the remote process. | |
85 //! \param[in] linkedit_size The size of \a linkedit_segment, starting at \a | |
86 //! linkedit_address, as it exists in the remote process. | |
87 //! \param[in] module_info A string to be used in logged messages. This string | |
88 //! is for diagnostic purposes only, and may be empty. | |
89 //! | |
90 //! \return `true` if the symbol table was read successfully. `false` | |
91 //! otherwise, with an appropriate message logged. | |
92 bool Initialize(ProcessReader* process_reader, | |
93 const process_types::symtab_command* symtab_command, | |
94 const process_types::dysymtab_command* dysymtab_command, | |
95 const MachOImageSegmentReader* linkedit_segment, | |
96 mach_vm_address_t linkedit_address, | |
97 mach_vm_size_t linkedit_size, | |
98 const std::string& module_info); | |
99 | |
100 //! \brief Looks up a symbol in the image’s symbol table. | |
101 //! | |
102 //! The returned information captures the symbol as it exists in the image’s | |
103 //! symbol table, not adjusted for any “slide.” | |
104 //! | |
105 //! \param[in] name The name of the symbol to look up, “mangled” or | |
106 //! “decorated” appropriately. For example, use `"_main"` to look up the | |
107 //! symbol for the C `main()` function, and use `"__Z4Funcv"` to look up | |
108 //! the symbol for the C++ `Func()` function. | |
109 //! | |
110 //! \return A SymbolInformation* object with information about the symbol if | |
111 //! it was found, or `NULL` if the symbol was not found or if an error | |
112 //! occurred. On error, a warning message will also be logged. | |
113 //! | |
114 //! \note Symbol values returned via this interface are not adjusted for | |
115 //! “slide.” For slide-adjusted values, use the higher-level | |
116 //! MachOImageReader::LookUpExternalDefinedSymbol() interface. | |
117 const SymbolInformation* LookUpExternalDefinedSymbol( | |
118 const std::string& name) const; | |
119 | |
120 private: | |
121 // TODO(mark): Use unordered_map or a similar hash-based map? For now, | |
122 // std::map is fine because this map only stores external defined symbols, | |
123 // and there aren’t expected to be very many of those that performance would | |
124 // become a problem. std::map is also guaranteed to be part of the standard | |
125 // library, which isn’t the case for std::unordered_map, which requires the | |
126 // C++11 library. In reality, std::unordered_map does not appear to provide | |
127 // a performance advantage. It appears that the memory copies currently done | |
128 // by TaskMemory::Read() have substantially more impact on symbol table | |
129 // operations. | |
130 typedef std::map<std::string, SymbolInformation> SymbolInformationMap; | |
131 | |
132 SymbolInformationMap external_defined_symbols_; | |
133 InitializationStateDcheck initialized_; | |
134 | |
135 friend class internal::MachOImageSymbolTableReaderInitializer; | |
Robert Sesek
2014/09/05 19:04:16
Is the friend only required to access the SymbolIn
Mark Mentovai
2014/09/05 20:22:31
rsesek wrote:
Robert Sesek
2014/09/05 20:41:31
Yeah, I think so.
| |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(MachOImageSymbolTableReader); | |
138 }; | |
139 | |
140 } // namespace crashpad | |
141 | |
142 #endif // CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SYMBOL_TABLE_READER_H_ | |
OLD | NEW |