| OLD | NEW |
| (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_SNAPSHOT_MAC_MACH_O_IMAGE_SEGMENT_READER_H_ |
| 16 #define CRASHPAD_SNAPSHOT_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 "snapshot/mac/process_types.h" |
| 28 #include "util/misc/initialization_state_dcheck.h" |
| 29 |
| 30 namespace crashpad { |
| 31 |
| 32 //! \brief A reader for `LC_SEGMENT` or `LC_SEGMENT_64` load commands in Mach-O |
| 33 //! images mapped into another process. |
| 34 //! |
| 35 //! This class is capable of reading both `LC_SEGMENT` and `LC_SEGMENT_64` based |
| 36 //! on the bitness of the remote process. |
| 37 //! |
| 38 //! A MachOImageSegmentReader will normally be instantiated by a |
| 39 //! MachOImageReader. |
| 40 class MachOImageSegmentReader { |
| 41 public: |
| 42 MachOImageSegmentReader(); |
| 43 ~MachOImageSegmentReader(); |
| 44 |
| 45 //! \brief Reads the segment load command from another process. |
| 46 //! |
| 47 //! This method must only be called once on an object. This method must be |
| 48 //! called successfully before any other method in this class may be called. |
| 49 //! |
| 50 //! \param[in] process_reader The reader for the remote process. |
| 51 //! \param[in] load_command_address The address, in the remote process’ |
| 52 //! address space, where the `LC_SEGMENT` or `LC_SEGMENT_64` load command |
| 53 //! to be read is located. This address is determined by a Mach-O image |
| 54 //! reader, such as MachOImageReader, as it walks Mach-O load commands. |
| 55 //! \param[in] load_command_info A string to be used in logged messages. This |
| 56 //! string is for diagnostic purposes only, and may be empty. |
| 57 //! \param[in] module_name The path used to load the module. This string is |
| 58 //! used to relax otherwise strict parsing rules for common modules with |
| 59 //! known defects. |
| 60 //! \param[in] file_type The module’s Mach-O file type. This is used to relax |
| 61 //! otherwise strict parsing rules for common modules with known defects. |
| 62 //! |
| 63 //! \return `true` if the load command was read successfully. `false` |
| 64 //! otherwise, with an appropriate message logged. |
| 65 bool Initialize(ProcessReader* process_reader, |
| 66 mach_vm_address_t load_command_address, |
| 67 const std::string& load_command_info, |
| 68 const std::string& module_name, |
| 69 uint32_t file_type); |
| 70 |
| 71 //! \brief Sets the image’s slide value. |
| 72 //! |
| 73 //! This method must only be called once on an object, after Initialize() is |
| 74 //! called successfully. It must be called before Address(), Size(), |
| 75 //! GetSectionByName(), or GetSectionAtIndex() can be called. |
| 76 //! |
| 77 //! This method is provided because slide is a property of the image that |
| 78 //! cannot be determined until at least some segments have been read. As such, |
| 79 //! it is not necessarily known at the time that Initialize() is called. |
| 80 void SetSlide(mach_vm_size_t slide); |
| 81 |
| 82 //! \brief Returns the segment’s name. |
| 83 //! |
| 84 //! The segment’s name is taken from the load command’s `segname` field. |
| 85 //! Common segment names are `"__TEXT"`, `"__DATA"`, and `"__LINKEDIT"`. |
| 86 //! Symbolic constants for these common names are defined in |
| 87 //! `<mach-o/loader.h>`. |
| 88 std::string Name() const; |
| 89 |
| 90 //! \return The segment’s actual load address in memory, adjusted for any |
| 91 //! “slide”. |
| 92 //! |
| 93 //! \note For the segment’s preferred load address, not adjusted for slide, |
| 94 //! use vmaddr(). |
| 95 mach_vm_address_t Address() const; |
| 96 |
| 97 //! \return The segment’s actual size address in memory, adjusted for any |
| 98 //! growth in the case of a nonsliding segment. |
| 99 //! |
| 100 //! \note For the segment’s preferred size, not adjusted for growth, use |
| 101 //! vmsize(). |
| 102 mach_vm_address_t Size() const; |
| 103 |
| 104 //! \brief The segment’s preferred load address. |
| 105 //! |
| 106 //! \return The segment’s preferred load address as stored in the Mach-O file. |
| 107 //! |
| 108 //! \note This value is not adjusted for any “slide” that may have occurred |
| 109 //! when the image was loaded. Use Address() for a value adjusted for |
| 110 //! slide. |
| 111 //! |
| 112 //! \sa MachOImageReader::GetSegmentByName() |
| 113 mach_vm_address_t vmaddr() const { return segment_command_.vmaddr; } |
| 114 |
| 115 //! \brief Returns the segment’s size as mapped into memory. |
| 116 //! |
| 117 //! \note For non-sliding segments, this value is not adjusted for any growth |
| 118 //! that may have occurred when the image was loaded. Use Size() for a |
| 119 //! value adjusted for growth. |
| 120 mach_vm_size_t vmsize() const { return segment_command_.vmsize; } |
| 121 |
| 122 //! \brief Returns the file offset of the mapped segment in the file from |
| 123 //! which it was mapped. |
| 124 //! |
| 125 //! The file offset is the difference between the beginning of the |
| 126 //! `mach_header` or `mach_header_64` and the beginning of the segment’s |
| 127 //! mapped region. For segments that are not mapped from a file (such as |
| 128 //! `__PAGEZERO` segments), this will be `0`. |
| 129 mach_vm_size_t fileoff() const { return segment_command_.fileoff; } |
| 130 |
| 131 //! \brief Returns the number of sections in the segment. |
| 132 //! |
| 133 //! This will return `0` for a segment without any sections, typical for |
| 134 //! `__PAGEZERO` and `__LINKEDIT` segments. |
| 135 //! |
| 136 //! Although the Mach-O file format uses a `uint32_t` for this field, there is |
| 137 //! an overall limit of 255 sections in an entire Mach-O image file (not just |
| 138 //! in a single segment) imposed by the symbol table format. Symbols will not |
| 139 //! be able to reference anything in a section beyond the first 255 in a |
| 140 //! Mach-O image file. |
| 141 uint32_t nsects() const { return segment_command_.nsects; } |
| 142 |
| 143 //! \brief Obtain section information by section name. |
| 144 //! |
| 145 //! \param[in] section_name The name of the section to search for, without the |
| 146 //! leading segment name. For example, use `"__text"`, not |
| 147 //! `"__TEXT,__text"` or `"__TEXT.__text"`. |
| 148 //! \param[out] address The actual address that the section was loaded at in |
| 149 //! memory, taking any “slide” into account if the section did not load at |
| 150 //! its preferred address as stored in the Mach-O image file. This |
| 151 //! parameter can be `nullptr`. |
| 152 //! |
| 153 //! \return A pointer to the section information if it was found, or `nullptr` |
| 154 //! if it was not found. The caller does not take ownership; the lifetime |
| 155 //! of the returned object is scoped to the lifetime of this |
| 156 //! MachOImageSegmentReader object. |
| 157 //! |
| 158 //! \note The process_types::section::addr field gives the section’s preferred |
| 159 //! load address as stored in the Mach-O image file, and is not adjusted |
| 160 //! for any “slide” that may have occurred when the image was loaded. |
| 161 //! |
| 162 //! \sa MachOImageReader::GetSectionByName() |
| 163 const process_types::section* GetSectionByName( |
| 164 const std::string& section_name, |
| 165 mach_vm_address_t* address) const; |
| 166 |
| 167 //! \brief Obtain section information by section index. |
| 168 //! |
| 169 //! \param[in] index The index of the section to return, in the order that it |
| 170 //! appears in the segment load command. Unlike |
| 171 //! MachOImageReader::GetSectionAtIndex(), this is a 0-based index. This |
| 172 //! parameter must be in the range of valid indices aas reported by |
| 173 //! nsects(). |
| 174 //! \param[out] address The actual address that the section was loaded at in |
| 175 //! memory, taking any “slide” into account if the section did not load at |
| 176 //! its preferred address as stored in the Mach-O image file. This |
| 177 //! parameter can be `nullptr`. |
| 178 //! |
| 179 //! \return A pointer to the section information. If \a index is out of range, |
| 180 //! execution is aborted. The caller does not take ownership; the |
| 181 //! lifetime of the returned object is scoped to the lifetime of this |
| 182 //! MachOImageSegmentReader object. |
| 183 //! |
| 184 //! \note The process_types::section::addr field gives the section’s preferred |
| 185 //! load address as stored in the Mach-O image file, and is not adjusted |
| 186 //! for any “slide” that may have occurred when the image was loaded. |
| 187 //! \note Unlike MachOImageReader::GetSectionAtIndex(), this method does not |
| 188 //! accept out-of-range values for \a index, and aborts execution instead |
| 189 //! of returning `nullptr` upon encountering an out-of-range value. This |
| 190 //! is because this method is expected to be used in a loop that can be |
| 191 //! limited to nsects() iterations, so an out-of-range error can be |
| 192 //! treated more harshly as a logic error, as opposed to a data error. |
| 193 //! |
| 194 //! \sa MachOImageReader::GetSectionAtIndex() |
| 195 const process_types::section* GetSectionAtIndex( |
| 196 size_t index, |
| 197 mach_vm_address_t* address) const; |
| 198 |
| 199 //! Returns whether the segment slides. |
| 200 //! |
| 201 //! Most segments slide, but the `__PAGEZERO` segment does not, it grows |
| 202 //! instead. This method identifies non-sliding segments in the same way that |
| 203 //! the kernel does. |
| 204 bool SegmentSlides() const; |
| 205 |
| 206 //! \brief Returns a segment name string. |
| 207 //! |
| 208 //! Segment names may be 16 characters long, and are not necessarily |
| 209 //! `NUL`-terminated. This function will return a segment name based on up to |
| 210 //! the first 16 characters found at \a segment_name_c. |
| 211 static std::string SegmentNameString(const char* segment_name_c); |
| 212 |
| 213 //! \brief Returns a section name string. |
| 214 //! |
| 215 //! Section names may be 16 characters long, and are not necessarily |
| 216 //! `NUL`-terminated. This function will return a section name based on up to |
| 217 //! the first 16 characters found at \a section_name_c. |
| 218 static std::string SectionNameString(const char* section_name_c); |
| 219 |
| 220 //! \brief Returns a segment and section name string. |
| 221 //! |
| 222 //! A segment and section name string is composed of a segment name string |
| 223 //! (see SegmentNameString()) and a section name string (see |
| 224 //! SectionNameString()) separated by a comma. An example is |
| 225 //! `"__TEXT,__text"`. |
| 226 static std::string SegmentAndSectionNameString(const char* segment_name_c, |
| 227 const char* section_name_c); |
| 228 |
| 229 private: |
| 230 //! \brief The internal implementation of Name(). |
| 231 //! |
| 232 //! This is identical to Name() but does not perform the |
| 233 //! InitializationStateDcheck check. It may be called during initialization |
| 234 //! provided that the caller only does so after segment_command_ has been |
| 235 //! read successfully. |
| 236 std::string NameInternal() const; |
| 237 |
| 238 // The segment command data read from the remote process. |
| 239 process_types::segment_command segment_command_; |
| 240 |
| 241 // Section structures read from the remote process in the order that they are |
| 242 // given in the remote process. |
| 243 std::vector<process_types::section> sections_; |
| 244 |
| 245 // Maps section names to indices into the sections_ vector. |
| 246 std::map<std::string, size_t> section_map_; |
| 247 |
| 248 // The image’s slide. Note that the segment’s slide may be 0 and not the value |
| 249 // of the image’s slide if SegmentSlides() is false. In that case, the |
| 250 // segment is extended instead of slid, so its size as loaded will be |
| 251 // increased by this value. |
| 252 mach_vm_size_t slide_; |
| 253 |
| 254 InitializationStateDcheck initialized_; |
| 255 InitializationStateDcheck initialized_slide_; |
| 256 |
| 257 DISALLOW_COPY_AND_ASSIGN(MachOImageSegmentReader); |
| 258 }; |
| 259 |
| 260 } // namespace crashpad |
| 261 |
| 262 #endif // CRASHPAD_SNAPSHOT_MAC_MACH_O_IMAGE_SEGMENT_READER_H_ |
| OLD | NEW |