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

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

Powered by Google App Engine
This is Rietveld 408576698