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

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: 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') | util/mac/mach_o_image_reader.cc » ('J')
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
Robert Sesek 2014/09/04 13:18:42 A similar \note about how address may differ from
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 const MachOImageSegmentReader* GetSegmentByName(
122 const std::string& segment_name,
123 mach_vm_address_t* address,
124 mach_vm_size_t* size) const;
125
126 //! \brief Obtain section information by segment and section name.
127 //!
128 //! \param[in] segment_name The name of the segment to search for, for
129 //! example, `"__TEXT"`.
130 //! \param[in] section_name The name of the section within the segment to
131 //! search for, for example, `"__text"`.
132 //! \param[out] address The actual address that the section was loaded at in
133 //! memory, taking any “slide” into account if the section did not load at
134 //! its preferred address as stored in the Mach-O image file. This
135 //! parameter can be `NULL`.
136 //!
137 //! \return A pointer to the section information if it was found, or `NULL` if
138 //! it was not found.
139 //!
140 //! No parameter is provided for the section’s size, because it can be
141 //! obtained from the returned process_types::section::size field.
142 //!
143 //! \note The process_types::section::addr field gives the section’s preferred
144 //! load address as stored in the Mach-O image file, and is not adjusted
145 //! for any “slide” that may have occurred when the image was loaded. Use
146 //! \a address to obtain the section’s actual load address.
147 const process_types::section* GetSectionByName(
148 const std::string& segment_name,
149 const std::string& section_name,
150 mach_vm_address_t* address) const;
151
152 //! \brief Obtain section information by section index.
153 //!
154 //! \param[in] index The index of the section to return, in the order that it
155 //! appears in the segment load commands. This is a 1-based index,
156 //! matching the section number values used for `nlist::n_sect`.
157 //! \param[out] address The actual address that the section was loaded at in
158 //! memory, taking any “slide” into account if the section did not load at
159 //! its preferred address as stored in the Mach-O image file. This
160 //! parameter can be `NULL`.
161 //!
162 //! \return A pointer to the section information. If \a index is out of range,
Robert Sesek 2014/09/04 13:18:42 This is a behavior difference from MachOImageSegme
163 //! logs a warning and returns `NULL`.
164 //!
165 //! No parameter is provided for the section’s size, because it can be
166 //! obtained from the returned process_types::section::size field.
167 //!
168 //! \note The process_types::section::addr field gives the section’s preferred
169 //! load address as stored in the Mach-O image file, and is not adjusted
170 //! for any “slide” that may have occurred when the image was loaded. Use
171 //! \a address to obtain the section’s actual load address.
172 const process_types::section* GetSectionAtIndex(
173 size_t index,
174 mach_vm_address_t* address) const;
175
176 //! \brief Returns a Mach-O dylib image’s current version.
177 //!
178 //! This information comes from the `dylib_current_version` field of a dylib’s
179 //! `LC_ID_DYLIB` load command. For dylibs without this load command, `0` will
180 //! be returned.
181 //!
182 //! This method may only be called on Mach-O images for which FileType()
183 //! returns `MH_DYLIB`.
184 uint32_t DylibVersion() const;
185
186 //! \brief Returns a Mach-O image’s source version.
187 //!
188 //! This information comes from a Mach-O image’s `LC_SOURCE_VERSION` load
189 //! command. For Mach-O images without this load command, `0` will be
190 //! returned.
191 uint64_t SourceVersion() const { return source_version_; }
192
193 //! \brief Returns a Mach-O image’s UUID.
194 //!
195 //! This information comes from a Mach-O image’s `LC_UUID` load command. For
196 //! Mach-O images without this load command, a zeroed-out UUID value will be
197 //! returned.
198 void UUID(struct UUID* uuid) const;
Robert Sesek 2014/09/04 13:18:42 Why `struct UUID` instead of just `UUID`?
199
200 //! \brief Returns the dynamic linker’s pathname.
201 //!
202 //! The dynamic linker is normally /usr/lib/dyld.
203 //!
204 //! For executable images (those with file type `MH_EXECUTE`), this is the
205 //! name provided in the `LC_LOAD_DYLINKER` load command, if any. For dynamic
206 //! linker images (those with file type `MH_DYLINKER`), this is the name
207 //! provided in the `LC_ID_DYLINKER` load command. In other cases, this will
208 //! be empty.
209 std::string DylinkerName() const { return dylinker_name_; }
210
211 private:
212 // A generic helper routine for the other Read*Command() methods.
213 template <typename T>
214 bool ReadLoadCommand(mach_vm_address_t load_command_address,
215 const std::string& load_command_info,
216 uint32_t expected_load_command_id,
217 T* load_command);
218
219 // The Read*Command() methods are subroutines called by Initialize(). They are
220 // responsible for reading a single load command. They may update
221 // |initialization_data| or the member fields of their MachOImageReader
Robert Sesek 2014/09/04 13:18:42 What's |initialization_data|?
222 // object. If they can’t make sense of a load command, they return false.
223 bool ReadSegmentCommand(mach_vm_address_t load_command_address,
224 const std::string& load_command_info);
225 bool ReadSymTabCommand(mach_vm_address_t load_command_address,
226 const std::string& load_command_info);
227 bool ReadDySymTabCommand(mach_vm_address_t load_command_address,
228 const std::string& load_command_info);
229 bool ReadIdDylibCommand(mach_vm_address_t load_command_address,
230 const std::string& load_command_info);
231 bool ReadDylinkerCommand(mach_vm_address_t load_command_address,
232 const std::string& load_command_info);
233 bool ReadUUIDCommand(mach_vm_address_t load_command_address,
234 const std::string& load_command_info);
235 bool ReadSourceVersionCommand(mach_vm_address_t load_command_address,
236 const std::string& load_command_info);
237 bool ReadUnexpectedCommand(mach_vm_address_t load_command_address,
238 const std::string& load_command_info);
239
240 PointerVector<MachOImageSegmentReader> segments_;
241 std::map<std::string, size_t> segment_map_;
242 std::string module_info_;
243 std::string dylinker_name_;
244 struct UUID uuid_;
245 mach_vm_address_t address_;
246 mach_vm_size_t size_;
247 mach_vm_size_t slide_;
248 uint64_t source_version_;
249 scoped_ptr<process_types::symtab_command> symtab_command_;
250 scoped_ptr<process_types::dysymtab_command> dysymtab_command_;
251 scoped_ptr<process_types::dylib_command> id_dylib_command_;
252 ProcessReader* process_reader_; // weak
253 uint32_t file_type_;
254 InitializationStateDcheck initialized_;
255
256 DISALLOW_COPY_AND_ASSIGN(MachOImageReader);
257 };
258
259 } // namespace crashpad
260
261 #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') | util/mac/mach_o_image_reader.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698