Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: util/mac/mach_o_image_reader.h

Issue 535343004: Add MachOImageReader and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Minor comment updates Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | util/mac/mach_o_image_reader.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_READER_H_
16 #define CRASHPAD_UTIL_MAC_MACH_O_IMAGE_READER_H_
17
18 #include <mach/mach.h>
19 #include <stdint.h>
20
21 #include <map>
22 #include <string>
23
24 #include "base/basictypes.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "util/misc/initialization_state_dcheck.h"
27 #include "util/misc/uuid.h"
28 #include "util/stdlib/pointer_container.h"
29 #include "util/mac/process_types.h"
30
31 namespace crashpad {
32
33 class MachOImageSegmentReader;
34 class ProcessReader;
35
36 //! \brief A reader for Mach-O images mapped into another process.
37 //!
38 //! This class is capable of reading both 32-bit (`mach_header`/`MH_MAGIC`) and
39 //! 64-bit (`mach_header_64`/`MH_MAGIC_64`) images based on the bitness of the
40 //! remote process.
41 class MachOImageReader {
42 public:
43 MachOImageReader();
44 ~MachOImageReader();
45
46 //! \brief Reads the Mach-O image file’s load commands from another process.
47 //!
48 //! This method must only be called once on an object. This method must be
49 //! called successfully before any other method in this class may be called.
50 //!
51 //! \param[in] process_reader The reader for the remote process.
52 //! \param[in] address The address, in the remote process’ address space,
53 //! where the `mach_header` or `mach_header_64` at the beginning of the
54 //! image to be read is located. This address can be determined by reading
55 //! the remote process’ dyld information (see
56 //! util/mac/process_types/dyld_images.proctype).
57 //! \param[in] name The module’s name, a string to be used in logged messages.
58 //! This string is for diagnostic purposes only, and may be empty.
59 //!
60 //! \return `true` if the image was read successfully, including all load
61 //! commands. `false` otherwise, with an appropriate message logged.
62 bool Initialize(ProcessReader* process_reader,
63 mach_vm_address_t address,
64 const std::string& name);
65
66 //! \brief Returns the Mach-O file type.
67 //!
68 //! This value comes from the `filetype` field of the `mach_header` or
69 //! `mach_header_64`. Common values include `MH_EXECUTE`, `MH_DYLIB`,
70 //! `MH_DYLINKER`, and `MH_BUNDLE`.
71 uint32_t FileType() const { return file_type_; }
72
73 //! \brief Returns the Mach-O image’s load address.
74 //!
75 //! This is the value passed as \a address to Initialize().
76 mach_vm_address_t Address() const { return address_; }
77
78 //! \brief Returns the mapped size of the Mach-O image’s __TEXT segment.
79 //!
80 //! Note that this is returns only the size of the __TEXT segment, not of any
81 //! other segment. This is because the interface only allows one load address
82 //! and size to be reported, but Mach-O image files may consist of multiple
83 //! discontiguous segments. By convention, the __TEXT segment is always mapped
84 //! at the beginning of a Mach-O image file, and it is the most useful for the
85 //! expected intended purpose of collecting data to obtain stack backtraces.
86 //! The implementation insists during initialization that the __TEXT segment
87 //! be mapped at the beginning of the file.
88 //!
89 //! In practice, discontiguous segments are only found for images that have
90 //! loaded out of the dyld shared cache, but the __TEXT segment’s size is
91 //! returned for modules that loaded with contiguous segments as well for
92 //! consistency.
93 mach_vm_size_t Size() const { return size_; }
94
95 //! \brief Returns the Mach-O image’s “slide,” the difference between its
96 //! actual load address and its preferred load address.
97 //!
98 //! “Slide” is computed by subtracting the __TEXT segment’s preferred load
99 //! address from its actual load address. It will be reported as a positive
100 //! offset when the actual load address is greater than the preferred load
101 //! address. The preferred load address is taken to be the segment’s reported
102 //! `vmaddr` value.
103 mach_vm_size_t Slide() const { return slide_; }
104
105 //! \brief Obtain segment information by segment name.
106 //!
107 //! \param[in] segment_name The name of the segment to search for, for
108 //! example, `"__TEXT"`.
109 //! \param[out] address The actual address that the segment was loaded at in
110 //! memory, taking any “slide” into account if the segment did not load at
111 //! its preferred address as stored in the Mach-O image file. This
112 //! parameter can be `NULL`.
113 //! \param[out] size The actual size of the segment as loaded at in memory.
114 //! This value takes any expansion of the segment into account, which
115 //! occurs when a nonsliding segment in a sliding image loads at its
116 //! preferred address but grows by the value of the slide. This parameter
117 //! can be `NULL`.
118 //!
119 //! \return A pointer to the segment information if it was found, or `NULL` if
120 //! it was not found.
121 //!
122 //! \note The \a address parameter takes “slide” into account, and the \a size
123 //! parameter takes growth into account for non-sliding segments, so that
124 //! these parameters reflect the actual address and size of the segment as
125 //! loaded into a process’ address space. This is distinct from the
126 //! segment’s preferred load address and size, which may be obtained by
127 //! calling MachOImageSegmentReader::vmaddr() and
128 //! MachOImageSegmentReader::vmsize(), respectively.
129 const MachOImageSegmentReader* GetSegmentByName(
130 const std::string& segment_name,
131 mach_vm_address_t* address,
132 mach_vm_size_t* size) const;
133
134 //! \brief Obtain section information by segment and section name.
135 //!
136 //! \param[in] segment_name The name of the segment to search for, for
137 //! example, `"__TEXT"`.
138 //! \param[in] section_name The name of the section within the segment to
139 //! search for, for example, `"__text"`.
140 //! \param[out] address The actual address that the section was loaded at in
141 //! memory, taking any “slide” into account if the section did not load at
142 //! its preferred address as stored in the Mach-O image file. This
143 //! parameter can be `NULL`.
144 //!
145 //! \return A pointer to the section information if it was found, or `NULL` if
146 //! it was not found.
147 //!
148 //! No parameter is provided for the section’s size, because it can be
149 //! obtained from the returned process_types::section::size field.
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. Use
154 //! \a address to obtain the section’s actual load address.
155 const process_types::section* GetSectionByName(
156 const std::string& segment_name,
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 commands. This is a 1-based index,
164 //! matching the section number values used for `nlist::n_sect`.
165 //! \param[out] address The actual address that the section was loaded at in
166 //! memory, taking any “slide” into account if the section did not load at
167 //! its preferred address as stored in the Mach-O image file. This
168 //! parameter can be `NULL`.
169 //!
170 //! \return A pointer to the section information. If \a index is out of range,
171 //! logs a warning and returns `NULL`.
172 //!
173 //! No parameter is provided for the section’s size, because it can be
174 //! obtained from the returned process_types::section::size field.
175 //!
176 //! \note The process_types::section::addr field gives the section’s preferred
177 //! load address as stored in the Mach-O image file, and is not adjusted
178 //! for any “slide” that may have occurred when the image was loaded. Use
179 //! \a address to obtain the section’s actual load address.
180 //! \note Unlike MachOImageSegmentReader::GetSectionAtIndex(), this method
181 //! accepts out-of-range values for \a index, and returns `NULL` instead
182 //! of aborting execution upon encountering an out-of-range value. This is
183 //! because a Mach-O image file’s symbol table refers to this per-module
184 //! section index, and an out-of-range index in that case should be
185 //! treated as a data error (where the data is beyond this code’s control)
186 //! and handled non-fatally by reporting the error to the caller.
187 const process_types::section* GetSectionAtIndex(
188 size_t index,
189 mach_vm_address_t* address) const;
190
191 //! \brief Returns a Mach-O dylib image’s current version.
192 //!
193 //! This information comes from the `dylib_current_version` field of a dylib’s
194 //! `LC_ID_DYLIB` load command. For dylibs without this load command, `0` will
195 //! be returned.
196 //!
197 //! This method may only be called on Mach-O images for which FileType()
198 //! returns `MH_DYLIB`.
199 uint32_t DylibVersion() const;
200
201 //! \brief Returns a Mach-O image’s source version.
202 //!
203 //! This information comes from a Mach-O image’s `LC_SOURCE_VERSION` load
204 //! command. For Mach-O images without this load command, `0` will be
205 //! returned.
206 uint64_t SourceVersion() const { return source_version_; }
207
208 //! \brief Returns a Mach-O image’s UUID.
209 //!
210 //! This information comes from a Mach-O image’s `LC_UUID` load command. For
211 //! Mach-O images without this load command, a zeroed-out UUID value will be
212 //! returned.
213 //
214 // UUID is a name in this scope (referring to this method), so the parameter’s
215 // type needs to be qualified with |crashpad::|.
216 void UUID(crashpad::UUID* uuid) const;
217
218 //! \brief Returns the dynamic linker’s pathname.
219 //!
220 //! The dynamic linker is normally /usr/lib/dyld.
221 //!
222 //! For executable images (those with file type `MH_EXECUTE`), this is the
223 //! name provided in the `LC_LOAD_DYLINKER` load command, if any. For dynamic
224 //! linker images (those with file type `MH_DYLINKER`), this is the name
225 //! provided in the `LC_ID_DYLINKER` load command. In other cases, this will
226 //! be empty.
227 std::string DylinkerName() const { return dylinker_name_; }
228
229 private:
230 // A generic helper routine for the other Read*Command() methods.
231 template <typename T>
232 bool ReadLoadCommand(mach_vm_address_t load_command_address,
233 const std::string& load_command_info,
234 uint32_t expected_load_command_id,
235 T* load_command);
236
237 // The Read*Command() methods are subroutines called by Initialize(). They are
238 // responsible for reading a single load command. They may update the member
239 // fields of their MachOImageReader object. If they can’t make sense of a load
240 // command, they return false.
241 bool ReadSegmentCommand(mach_vm_address_t load_command_address,
242 const std::string& load_command_info);
243 bool ReadSymTabCommand(mach_vm_address_t load_command_address,
244 const std::string& load_command_info);
245 bool ReadDySymTabCommand(mach_vm_address_t load_command_address,
246 const std::string& load_command_info);
247 bool ReadIdDylibCommand(mach_vm_address_t load_command_address,
248 const std::string& load_command_info);
249 bool ReadDylinkerCommand(mach_vm_address_t load_command_address,
250 const std::string& load_command_info);
251 bool ReadUUIDCommand(mach_vm_address_t load_command_address,
252 const std::string& load_command_info);
253 bool ReadSourceVersionCommand(mach_vm_address_t load_command_address,
254 const std::string& load_command_info);
255 bool ReadUnexpectedCommand(mach_vm_address_t load_command_address,
256 const std::string& load_command_info);
257
258 PointerVector<MachOImageSegmentReader> segments_;
259 std::map<std::string, size_t> segment_map_;
260 std::string module_info_;
261 std::string dylinker_name_;
262 struct UUID uuid_;
Robert Sesek 2014/09/04 14:38:17 struct UUID here, too.
263 mach_vm_address_t address_;
264 mach_vm_size_t size_;
265 mach_vm_size_t slide_;
266 uint64_t source_version_;
267 scoped_ptr<process_types::symtab_command> symtab_command_;
268 scoped_ptr<process_types::dysymtab_command> dysymtab_command_;
269 scoped_ptr<process_types::dylib_command> id_dylib_command_;
270 ProcessReader* process_reader_; // weak
271 uint32_t file_type_;
272 InitializationStateDcheck initialized_;
273
274 DISALLOW_COPY_AND_ASSIGN(MachOImageReader);
275 };
276
277 } // namespace crashpad
278
279 #endif // CRASHPAD_UTIL_MAC_MACH_O_IMAGE_READER_H_
OLDNEW
« no previous file with comments | « no previous file | util/mac/mach_o_image_reader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698