OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include <map> | 22 #include <map> |
23 #include <string> | 23 #include <string> |
24 #include <vector> | 24 #include <vector> |
25 | 25 |
26 #include "base/basictypes.h" | 26 #include "base/basictypes.h" |
27 #include "util/mac/process_types.h" | 27 #include "util/mac/process_types.h" |
28 #include "util/misc/initialization_state_dcheck.h" | 28 #include "util/misc/initialization_state_dcheck.h" |
29 | 29 |
30 namespace crashpad { | 30 namespace crashpad { |
31 | 31 |
32 class ProcessReader; | |
33 | |
34 //! \brief A reader for LC_SEGMENT or LC_SEGMENT_64 load commands in Mach-O | 32 //! \brief A reader for LC_SEGMENT or LC_SEGMENT_64 load commands in Mach-O |
35 //! images mapped into another process. | 33 //! images mapped into another process. |
36 //! | 34 //! |
37 //! This class is capable of reading both LC_SEGMENT and LC_SEGMENT_64 based on | 35 //! This class is capable of reading both LC_SEGMENT and LC_SEGMENT_64 based on |
38 //! the bitness of the remote process. | 36 //! the bitness of the remote process. |
39 //! | 37 //! |
40 //! A MachOImageSegmentReader will normally be instantiated by a | 38 //! A MachOImageSegmentReader will normally be instantiated by a |
41 //! MachOImageReader. | 39 //! MachOImageReader. |
42 class MachOImageSegmentReader { | 40 class MachOImageSegmentReader { |
43 public: | 41 public: |
(...skipping 12 matching lines...) Expand all Loading... |
56 //! reader, such as MachOImageReader, as it walks Mach-O load commands. | 54 //! 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 | 55 //! \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. | 56 //! string is for diagnostic purposes only, and may be empty. |
59 //! | 57 //! |
60 //! \return `true` if the load command was read successfully. `false` | 58 //! \return `true` if the load command was read successfully. `false` |
61 //! otherwise, with an appropriate message logged. | 59 //! otherwise, with an appropriate message logged. |
62 bool Initialize(ProcessReader* process_reader, | 60 bool Initialize(ProcessReader* process_reader, |
63 mach_vm_address_t load_command_address, | 61 mach_vm_address_t load_command_address, |
64 const std::string& load_command_info); | 62 const std::string& load_command_info); |
65 | 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 |
66 //! \brief Returns the segment’s name. | 75 //! \brief Returns the segment’s name. |
67 //! | 76 //! |
68 //! The segment’s name is taken from the load command’s `segname` field. | 77 //! The segment’s name is taken from the load command’s `segname` field. |
69 //! Common segment names are `"__TEXT"`, `"__DATA"`, and `"__LINKEDIT"`. | 78 //! Common segment names are `"__TEXT"`, `"__DATA"`, and `"__LINKEDIT"`. |
70 //! Symbolic constants for these common names are defined in | 79 //! Symbolic constants for these common names are defined in |
71 //! `<mach-o/loader.h>`. | 80 //! `<mach-o/loader.h>`. |
72 std::string Name() const; | 81 std::string Name() const; |
73 | 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 |
74 //! \brief The segment’s preferred load address. | 97 //! \brief The segment’s preferred load address. |
75 //! | 98 //! |
76 //! \return The segment’s preferred load address as stored in the Mach-O file. | 99 //! \return The segment’s preferred load address as stored in the Mach-O file. |
77 //! | 100 //! |
78 //! \note This value is not adjusted for any “slide” that may have occurred | 101 //! \note This value is not adjusted for any “slide” that may have occurred |
79 //! when the image was loaded. | 102 //! when the image was loaded. Use Address() for a value adjusted for |
| 103 //! slide. |
80 //! | 104 //! |
81 //! \sa MachOImageReader::GetSegmentByName() | 105 //! \sa MachOImageReader::GetSegmentByName() |
82 mach_vm_address_t vmaddr() const { return segment_command_.vmaddr; } | 106 mach_vm_address_t vmaddr() const { return segment_command_.vmaddr; } |
83 | 107 |
84 //! \brief Returns the segment’s size as mapped into memory. | 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. |
85 mach_vm_size_t vmsize() const { return segment_command_.vmsize; } | 113 mach_vm_size_t vmsize() const { return segment_command_.vmsize; } |
86 | 114 |
87 //! \brief Returns the file offset of the mapped segment in the file from | 115 //! \brief Returns the file offset of the mapped segment in the file from |
88 //! which it was mapped. | 116 //! which it was mapped. |
89 //! | 117 //! |
90 //! The file offset is the difference between the beginning of the | 118 //! 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 | 119 //! `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 | 120 //! mapped region. For segments that are not mapped from a file (such as |
93 //! `"__PAGEZERO"` segments), this will be `0`. | 121 //! `"__PAGEZERO"` segments), this will be `0`. |
94 mach_vm_size_t fileoff() const { return segment_command_.fileoff; } | 122 mach_vm_size_t fileoff() const { return segment_command_.fileoff; } |
95 | 123 |
96 //! \brief Returns the number of sections in the segment. | 124 //! \brief Returns the number of sections in the segment. |
97 //! | 125 //! |
98 //! This will return `0` for a segment without any sections, typical for | 126 //! This will return `0` for a segment without any sections, typical for |
99 //! `"__PAGEZERO"` and `"__LINKEDIT"` segments. | 127 //! `"__PAGEZERO"` and `"__LINKEDIT"` segments. |
100 //! | 128 //! |
101 //! Although the Mach-O file format uses a `uint32_t` for this field, there is | 129 //! 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 | 130 //! 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 | 131 //! 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 | 132 //! be able to reference anything in a section beyond the first 255 in a |
105 //! Mach-O image file. | 133 //! Mach-O image file. |
106 uint32_t nsects() const { return segment_command_.nsects; } | 134 uint32_t nsects() const { return segment_command_.nsects; } |
107 | 135 |
108 //! \brief Obtain section information by section name. | 136 //! \brief Obtain section information by section name. |
109 //! | 137 //! |
110 //! \param[in] section_name The name of the section to search for, without the | 138 //! \param[in] section_name The name of the section to search for, without the |
111 //! leading segment name. For example, use `"__text"`, not | 139 //! leading segment name. For example, use `"__text"`, not |
112 //! `"__TEXT,__text"` or `"__TEXT.__text"`. | 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 `NULL`. |
113 //! | 145 //! |
114 //! \return A pointer to the section information if it was found, or `NULL` if | 146 //! \return A pointer to the section information if it was found, or `NULL` if |
115 //! it was not found. | 147 //! it was not found. The caller does not take ownership; the lifetime of |
| 148 //! the returned object is scoped to the lifetime of this |
| 149 //! MachOImageSegmentReader object. |
116 //! | 150 //! |
117 //! \note The process_types::section::addr field gives the section’s preferred | 151 //! \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 | 152 //! 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. | 153 //! for any “slide” that may have occurred when the image was loaded. |
120 //! | 154 //! |
121 //! \sa MachOImageReader::GetSectionByName() | 155 //! \sa MachOImageReader::GetSectionByName() |
122 const process_types::section* GetSectionByName( | 156 const process_types::section* GetSectionByName( |
123 const std::string& section_name) const; | 157 const std::string& section_name, |
| 158 mach_vm_address_t* address) const; |
124 | 159 |
125 //! \brief Obtain section information by section index. | 160 //! \brief Obtain section information by section index. |
126 //! | 161 //! |
127 //! \param[in] index The index of the section to return, in the order that it | 162 //! \param[in] index The index of the section to return, in the order that it |
128 //! appears in the segment load command. Unlike | 163 //! appears in the segment load command. Unlike |
129 //! MachOImageReader::GetSectionAtIndex(), this is a 0-based index. This | 164 //! MachOImageReader::GetSectionAtIndex(), this is a 0-based index. This |
130 //! parameter must be in the range of valid indices aas reported by | 165 //! parameter must be in the range of valid indices aas reported by |
131 //! nsects(). | 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 `NULL`. |
132 //! | 171 //! |
133 //! \return A pointer to the section information. If \a index is out of range, | 172 //! \return A pointer to the section information. If \a index is out of range, |
134 //! execution is aborted. | 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. |
135 //! | 176 //! |
136 //! \note The process_types::section::addr field gives the section’s preferred | 177 //! \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 | 178 //! 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. | 179 //! for any “slide” that may have occurred when the image was loaded. |
139 //! \note Unlike MachOImageReader::GetSectionAtIndex(), this method does not | 180 //! \note Unlike MachOImageReader::GetSectionAtIndex(), this method does not |
140 //! accept out-of-range values for \a index, and aborts execution instead | 181 //! accept out-of-range values for \a index, and aborts execution instead |
141 //! of returning `NULL` upon encountering an out-of-range value. This is | 182 //! of returning `NULL` upon encountering an out-of-range value. This is |
142 //! because this method is expected to be used in a loop that can be | 183 //! because this method is expected to be used in a loop that can be |
143 //! limited to nsects() iterations, so an out-of-range error can be | 184 //! limited to nsects() iterations, so an out-of-range error can be |
144 //! treated more harshly as a logic error, as opposed to a data error. | 185 //! treated more harshly as a logic error, as opposed to a data error. |
145 //! | 186 //! |
146 //! \sa MachOImageReader::GetSectionAtIndex() | 187 //! \sa MachOImageReader::GetSectionAtIndex() |
147 const process_types::section* GetSectionAtIndex(size_t index) const; | 188 const process_types::section* GetSectionAtIndex( |
| 189 size_t index, |
| 190 mach_vm_address_t* address) const; |
148 | 191 |
149 //! Returns whether the segment slides. | 192 //! Returns whether the segment slides. |
150 //! | 193 //! |
151 //! Most segments slide, but the __PAGEZERO segment does not, it grows | 194 //! Most segments slide, but the __PAGEZERO segment does not, it grows |
152 //! instead. This method identifies non-sliding segments in the same way that | 195 //! instead. This method identifies non-sliding segments in the same way that |
153 //! the kernel does. | 196 //! the kernel does. |
154 bool SegmentSlides() const; | 197 bool SegmentSlides() const; |
155 | 198 |
156 //! \brief Returns a segment name string. | 199 //! \brief Returns a segment name string. |
157 //! | 200 //! |
(...skipping 30 matching lines...) Expand all Loading... |
188 // The segment command data read from the remote process. | 231 // The segment command data read from the remote process. |
189 process_types::segment_command segment_command_; | 232 process_types::segment_command segment_command_; |
190 | 233 |
191 // Section structures read from the remote process in the order that they are | 234 // Section structures read from the remote process in the order that they are |
192 // given in the remote process. | 235 // given in the remote process. |
193 std::vector<process_types::section> sections_; | 236 std::vector<process_types::section> sections_; |
194 | 237 |
195 // Maps section names to indices into the sections_ vector. | 238 // Maps section names to indices into the sections_ vector. |
196 std::map<std::string, size_t> section_map_; | 239 std::map<std::string, size_t> section_map_; |
197 | 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 |
198 InitializationStateDcheck initialized_; | 247 InitializationStateDcheck initialized_; |
| 248 InitializationStateDcheck initialized_slide_; |
199 | 249 |
200 DISALLOW_COPY_AND_ASSIGN(MachOImageSegmentReader); | 250 DISALLOW_COPY_AND_ASSIGN(MachOImageSegmentReader); |
201 }; | 251 }; |
202 | 252 |
203 } // namespace crashpad | 253 } // namespace crashpad |
204 | 254 |
205 #endif // CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SEGMENT_READER_H_ | 255 #endif // CRASHPAD_UTIL_MAC_MACH_O_IMAGE_SEGMENT_READER_H_ |
OLD | NEW |