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_SEGMENT_READER_H_ | |
16 #define CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SEGMENT_READER_H_ | |
17 | |
18 #include <mach/mach.h> | |
19 #include <stdint.h> | |
20 #include <sys/types.h> | |
21 | |
22 #include <map> | |
23 #include <string> | |
24 #include <vector> | |
25 | |
26 #include "base/basictypes.h" | |
27 #include "util/mac/process_types.h" | |
28 #include "util/misc/initialization_state_dcheck.h" | |
29 | |
30 namespace crashpad { | |
31 | |
32 //! \brief A reader for `LC_SEGMENT` or `LC_SEGMENT_64` load commands in Mach-O | |
33 //! images mapped into another process. | |
34 //! | |
35 //! This class is capable of reading both `LC_SEGMENT` and `LC_SEGMENT_64` based | |
36 //! on the bitness of the remote process. | |
37 //! | |
38 //! A MachOImageSegmentReader will normally be instantiated by a | |
39 //! MachOImageReader. | |
40 class MachOImageSegmentReader { | |
41 public: | |
42 MachOImageSegmentReader(); | |
43 ~MachOImageSegmentReader(); | |
44 | |
45 //! \brief Reads the segment load command from another process. | |
46 //! | |
47 //! This method must only be called once on an object. This method must be | |
48 //! called successfully before any other method in this class may be called. | |
49 //! | |
50 //! \param[in] process_reader The reader for the remote process. | |
51 //! \param[in] load_command_address The address, in the remote process’ | |
52 //! address space, where the `LC_SEGMENT` or `LC_SEGMENT_64` load command | |
53 //! to be read is located. This address is determined by a Mach-O image | |
54 //! reader, such as MachOImageReader, as it walks Mach-O load commands. | |
55 //! \param[in] load_command_info A string to be used in logged messages. This | |
56 //! string is for diagnostic purposes only, and may be empty. | |
57 //! | |
58 //! \return `true` if the load command was read successfully. `false` | |
59 //! otherwise, with an appropriate message logged. | |
60 bool Initialize(ProcessReader* process_reader, | |
61 mach_vm_address_t load_command_address, | |
62 const std::string& load_command_info); | |
63 | |
64 //! \brief Sets the image’s slide value. | |
65 //! | |
66 //! This method must only be called once on an object, after Initialize() is | |
67 //! called successfully. It must be called before Address(), Size(), | |
68 //! GetSectionByName(), or GetSectionAtIndex() can be called. | |
69 //! | |
70 //! This method is provided because slide is a property of the image that | |
71 //! cannot be determined until at least some segments have been read. As such, | |
72 //! it is not necessarily known at the time that Initialize() is called. | |
73 void SetSlide(mach_vm_size_t slide); | |
74 | |
75 //! \brief Returns the segment’s name. | |
76 //! | |
77 //! The segment’s name is taken from the load command’s `segname` field. | |
78 //! Common segment names are `"__TEXT"`, `"__DATA"`, and `"__LINKEDIT"`. | |
79 //! Symbolic constants for these common names are defined in | |
80 //! `<mach-o/loader.h>`. | |
81 std::string Name() const; | |
82 | |
83 //! \return The segment’s actual load address in memory, adjusted for any | |
84 //! “slide”. | |
85 //! | |
86 //! \note For the segment’s preferred load address, not adjusted for slide, | |
87 //! use vmaddr(). | |
88 mach_vm_address_t Address() const; | |
89 | |
90 //! \return The segment’s actual size address in memory, adjusted for any | |
91 //! growth in the case of a nonsliding segment. | |
92 //! | |
93 //! \note For the segment’s preferred size, not adjusted for growth, use | |
94 //! vmsize(). | |
95 mach_vm_address_t Size() const; | |
96 | |
97 //! \brief The segment’s preferred load address. | |
98 //! | |
99 //! \return The segment’s preferred load address as stored in the Mach-O file. | |
100 //! | |
101 //! \note This value is not adjusted for any “slide” that may have occurred | |
102 //! when the image was loaded. Use Address() for a value adjusted for | |
103 //! slide. | |
104 //! | |
105 //! \sa MachOImageReader::GetSegmentByName() | |
106 mach_vm_address_t vmaddr() const { return segment_command_.vmaddr; } | |
107 | |
108 //! \brief Returns the segment’s size as mapped into memory. | |
109 //! | |
110 //! \note For non-sliding segments, this value is not adjusted for any growth | |
111 //! that may have occurred when the image was loaded. Use Size() for a | |
112 //! value adjusted for growth. | |
113 mach_vm_size_t vmsize() const { return segment_command_.vmsize; } | |
114 | |
115 //! \brief Returns the file offset of the mapped segment in the file from | |
116 //! which it was mapped. | |
117 //! | |
118 //! The file offset is the difference between the beginning of the | |
119 //! `mach_header` or `mach_header_64` and the beginning of the segment’s | |
120 //! mapped region. For segments that are not mapped from a file (such as | |
121 //! `__PAGEZERO` segments), this will be `0`. | |
122 mach_vm_size_t fileoff() const { return segment_command_.fileoff; } | |
123 | |
124 //! \brief Returns the number of sections in the segment. | |
125 //! | |
126 //! This will return `0` for a segment without any sections, typical for | |
127 //! `__PAGEZERO` and `__LINKEDIT` segments. | |
128 //! | |
129 //! Although the Mach-O file format uses a `uint32_t` for this field, there is | |
130 //! an overall limit of 255 sections in an entire Mach-O image file (not just | |
131 //! in a single segment) imposed by the symbol table format. Symbols will not | |
132 //! be able to reference anything in a section beyond the first 255 in a | |
133 //! Mach-O image file. | |
134 uint32_t nsects() const { return segment_command_.nsects; } | |
135 | |
136 //! \brief Obtain section information by section name. | |
137 //! | |
138 //! \param[in] section_name The name of the section to search for, without the | |
139 //! leading segment name. For example, use `"__text"`, not | |
140 //! `"__TEXT,__text"` or `"__TEXT.__text"`. | |
141 //! \param[out] address The actual address that the section was loaded at in | |
142 //! memory, taking any “slide” into account if the section did not load at | |
143 //! its preferred address as stored in the Mach-O image file. This | |
144 //! parameter can be `nullptr`. | |
145 //! | |
146 //! \return A pointer to the section information if it was found, or `nullptr` | |
147 //! if it was not found. The caller does not take ownership; the lifetime | |
148 //! of the returned object is scoped to the lifetime of this | |
149 //! MachOImageSegmentReader object. | |
150 //! | |
151 //! \note The process_types::section::addr field gives the section’s preferred | |
152 //! load address as stored in the Mach-O image file, and is not adjusted | |
153 //! for any “slide” that may have occurred when the image was loaded. | |
154 //! | |
155 //! \sa MachOImageReader::GetSectionByName() | |
156 const process_types::section* GetSectionByName( | |
157 const std::string& section_name, | |
158 mach_vm_address_t* address) const; | |
159 | |
160 //! \brief Obtain section information by section index. | |
161 //! | |
162 //! \param[in] index The index of the section to return, in the order that it | |
163 //! appears in the segment load command. Unlike | |
164 //! MachOImageReader::GetSectionAtIndex(), this is a 0-based index. This | |
165 //! parameter must be in the range of valid indices aas reported by | |
166 //! nsects(). | |
167 //! \param[out] address The actual address that the section was loaded at in | |
168 //! memory, taking any “slide” into account if the section did not load at | |
169 //! its preferred address as stored in the Mach-O image file. This | |
170 //! parameter can be `nullptr`. | |
171 //! | |
172 //! \return A pointer to the section information. If \a index is out of range, | |
173 //! execution is aborted. The caller does not take ownership; the | |
174 //! lifetime of the returned object is scoped to the lifetime of this | |
175 //! MachOImageSegmentReader object. | |
176 //! | |
177 //! \note The process_types::section::addr field gives the section’s preferred | |
178 //! load address as stored in the Mach-O image file, and is not adjusted | |
179 //! for any “slide” that may have occurred when the image was loaded. | |
180 //! \note Unlike MachOImageReader::GetSectionAtIndex(), this method does not | |
181 //! accept out-of-range values for \a index, and aborts execution instead | |
182 //! of returning `nullptr` upon encountering an out-of-range value. This | |
183 //! is because this method is expected to be used in a loop that can be | |
184 //! limited to nsects() iterations, so an out-of-range error can be | |
185 //! treated more harshly as a logic error, as opposed to a data error. | |
186 //! | |
187 //! \sa MachOImageReader::GetSectionAtIndex() | |
188 const process_types::section* GetSectionAtIndex( | |
189 size_t index, | |
190 mach_vm_address_t* address) const; | |
191 | |
192 //! Returns whether the segment slides. | |
193 //! | |
194 //! Most segments slide, but the `__PAGEZERO` segment does not, it grows | |
195 //! instead. This method identifies non-sliding segments in the same way that | |
196 //! the kernel does. | |
197 bool SegmentSlides() const; | |
198 | |
199 //! \brief Returns a segment name string. | |
200 //! | |
201 //! Segment names may be 16 characters long, and are not necessarily | |
202 //! `NUL`-terminated. This function will return a segment name based on up to | |
203 //! the first 16 characters found at \a segment_name_c. | |
204 static std::string SegmentNameString(const char* segment_name_c); | |
205 | |
206 //! \brief Returns a section name string. | |
207 //! | |
208 //! Section names may be 16 characters long, and are not necessarily | |
209 //! `NUL`-terminated. This function will return a section name based on up to | |
210 //! the first 16 characters found at \a section_name_c. | |
211 static std::string SectionNameString(const char* section_name_c); | |
212 | |
213 //! \brief Returns a segment and section name string. | |
214 //! | |
215 //! A segment and section name string is composed of a segment name string | |
216 //! (see SegmentNameString()) and a section name string (see | |
217 //! SectionNameString()) separated by a comma. An example is | |
218 //! `"__TEXT,__text"`. | |
219 static std::string SegmentAndSectionNameString(const char* segment_name_c, | |
220 const char* section_name_c); | |
221 | |
222 private: | |
223 //! \brief The internal implementation of Name(). | |
224 //! | |
225 //! This is identical to Name() but does not perform the | |
226 //! InitializationStateDcheck check. It may be called during initialization | |
227 //! provided that the caller only does so after segment_command_ has been | |
228 //! read successfully. | |
229 std::string NameInternal() const; | |
230 | |
231 // The segment command data read from the remote process. | |
232 process_types::segment_command segment_command_; | |
233 | |
234 // Section structures read from the remote process in the order that they are | |
235 // given in the remote process. | |
236 std::vector<process_types::section> sections_; | |
237 | |
238 // Maps section names to indices into the sections_ vector. | |
239 std::map<std::string, size_t> section_map_; | |
240 | |
241 // The image’s slide. Note that the segment’s slide may be 0 and not the value | |
242 // of the image’s slide if SegmentSlides() is false. In that case, the | |
243 // segment is extended instead of slid, so its size as loaded will be | |
244 // increased by this value. | |
245 mach_vm_size_t slide_; | |
246 | |
247 InitializationStateDcheck initialized_; | |
248 InitializationStateDcheck initialized_slide_; | |
249 | |
250 DISALLOW_COPY_AND_ASSIGN(MachOImageSegmentReader); | |
251 }; | |
252 | |
253 } // namespace crashpad | |
254 | |
255 #endif // CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SEGMENT_READER_H_ | |
OLD | NEW |