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

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

Issue 516983003: Add MachOImageSegmentReader (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
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_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 class ProcessReader;
33
34 //! \brief A reader for LC_SEGMENT or LC_SEGMENT_64 load commands in Mach-O
35 //! images mapped into another process.
36 //!
37 //! This class is capable of reading both LC_SEGMENT and LC_SEGMENT_64 based on
38 //! the bitness of the remote process.
39 class MachOImageSegmentReader {
40 public:
41 MachOImageSegmentReader();
42 ~MachOImageSegmentReader();
43
44 //! \brief Reads the segment load command from another process.
45 //!
46 //! This method must only be called once on an object. This method must be
47 //! called successfully before any other method in this class may be called.
48 //!
49 //! \param[in] process_reader The reader for the remote process.
50 //! \param[in] load_command_address The address, in the remote process’
51 //! address space, where the LC_SEGMENT or LC_SEGMENT_64 load command
52 //! to be read is located.
53 //! \param[in] load_command_info A string to be used in logged messages. This
54 //! string is for diagnostic purposes only, and may be empty.
55 //!
56 //! \return `true` if the load command was read successfully. `false`
57 //! otherwise, with an appropriate message logged.
58 bool Initialize(ProcessReader* process_reader,
59 mach_vm_address_t load_command_address,
Robert Sesek 2014/09/02 16:40:09 It's not clear to me where I'm supposed to get thi
60 const std::string& load_command_info);
61
62 //! \brief Returns the segment’s name.
63 //!
64 //! The segment’s name is taken from the load command’s `segname` field.
65 //! Common segment names are `"__TEXT"`, `"__DATA"`, and `"__LINKEDIT"`.
66 //! Symbolic constants for these common names are defined in
67 //! `<mach-o/loader.h>`.
68 std::string Name() const;
69
70 //! \brief The segment’s preferred load address.
71 //!
72 //! \return The segment’s preferred load address as stored in the Mach-O file.
73 //!
74 //! \note This value is not adjusted for any “slide” that may have occurred
75 //! when the image was loaded.
76 //!
77 //! \sa MachOImageReader::GetSegmentByName()
78 mach_vm_address_t vmaddr() const { return segment_command_.vmaddr; }
79
80 //! \brief Returns the segment’s size as mapped into memory.
81 mach_vm_size_t vmsize() const { return segment_command_.vmsize; }
82
83 //! \brief Returns the file offset of the mapped segment in the file from
84 //! which it was mapped.
85 //!
86 //! The file offset is the difference between the beginning of the
87 //! `mach_header` or `mach_header_64` and the beginning of the segment’s
88 //! mapped region. For segments that are not mapped from a file (such as
89 //! `"__PAGEZERO"` segments), this will be `0`.
90 mach_vm_size_t fileoff() const { return segment_command_.fileoff; }
91
92 //! \brief Returns the number of sections in the segment.
93 //!
94 //! This will return `0` for a segment without any sections, typical for
95 //! `"__PAGEZERO"` and `"__LINKEDIT"` segments.
96 //!
97 //! Although the Mach-O file format uses a `uint32_t` for this field, there is
98 //! an overall limit of 255 sections in an entire Mach-O image file (not just
99 //! in a single segment) imposed by the symbol table format. Symbols will not
100 //! be able to reference anything in a section beyond the first 255 in a
101 //! Mach-O image file.
102 uint32_t nsects() const { return segment_command_.nsects; }
103
104 //! \brief Obtain section information by section name.
105 //!
106 //! \param[in] section_name The name of the section to search for, without the
107 //! leading segment name. For example, use `"__text"`, not
108 //! `"__TEXT,__text"` or `"__TEXT.__text"`.
109 //!
110 //! \return A pointer to the section information if it was found, or `NULL` if
111 //! it was not found.
112 //!
113 //! \note The process_types::section::addr field gives the section’s preferred
114 //! load address as stored in the Mach-O image file, and is not adjusted
115 //! for any “slide” that may have occurred when the image was loaded.
116 //!
117 //! \sa MachOImageReader::GetSectionByName()
118 const process_types::section* GetSectionByName(
119 const std::string& section_name) const;
120
121 //! \brief Obtain section information by section index.
122 //!
123 //! \param[in] index The index of the section to return, in the order that it
124 //! appears in the segment load command. Unlike
125 //! MachOImageReader::GetSectionAtIndex(), this is a 0-based index. This
126 //! parameter must be in the range of valid indices aas reported by
127 //! nsects().
128 //!
129 //! \return A pointer to the section information. If \a index is out of range,
130 //! execution is aborted.
131 //!
132 //! \note The process_types::section::addr field gives the section’s preferred
133 //! load address as stored in the Mach-O image file, and is not adjusted
134 //! for any “slide” that may have occurred when the image was loaded.
135 //!
136 //! \sa MachOImageReader::GetSectionAtIndex()
137 const process_types::section* GetSectionAtIndex(size_t index) const;
138
139 //! \brief Returns a segment name string.
140 //!
141 //! Segment names may be 16 characters long, and are not necessarily
142 //! `NUL`-terminated. This function will return a segment name based on up to
143 //! the first 16 characters found at \a segment_name_c.
144 static std::string SegmentNameString(const char* segment_name_c);
145
146 //! \brief Returns a section name string.
147 //!
148 //! Section names may be 16 characters long, and are not necessarily
149 //! `NUL`-terminated. This function will return a section name based on up to
150 //! the first 16 characters found at \a section_name_c.
151 static std::string SectionNameString(const char* section_name_c);
152
153 //! \brief Returns a segment and section name string.
154 //!
155 //! A segment and section name string is composed of a segment name string
156 //! (see SegmentNameString()) and a section name string (see
157 //! SectionNameString()) separated by a comma. An example is
158 //! `"__TEXT,__text"`.
159 static std::string SegmentAndSectionNameString(const char* segment_name_c,
160 const char* section_name_c);
161
162 private:
163 //! \brief The internal implementation of Name().
164 //!
165 //! This is identical to Name() but does not perform the
166 //! InitializationStateDcheck check. It may be called during initialization
167 //! provided that the caller only does so after segment_command_ has been
168 //! read successfully.
169 std::string NameInternal() const;
170
171 // The segment command data read from the remote process.
172 process_types::segment_command segment_command_;
173
174 // Section structures read from the remote process in the order that they are
175 // given in the remote process.
176 std::vector<process_types::section> sections_;
177
178 // Maps section names to indices into the sections_ vector.
179 std::map<std::string, size_t> section_map_;
180
181 InitializationStateDcheck initialized_;
182
183 DISALLOW_COPY_AND_ASSIGN(MachOImageSegmentReader);
184 };
185
186 } // namespace crashpad
187
188 #endif // CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SEGMENT_READER_H_
OLDNEW
« no previous file with comments | « no previous file | util/mac/mach_o_image_segment_reader.cc » ('j') | util/mac/mach_o_image_segment_reader.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698