Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006, Google Inc. | 1 // Copyright (c) 2006, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | 29 |
| 30 // macho_walker.cc: Iterate over the load commands in a mach-o file | 30 // macho_walker.cc: Iterate over the load commands in a mach-o file |
| 31 // | 31 // |
| 32 // See macho_walker.h for documentation | 32 // See macho_walker.h for documentation |
| 33 // | 33 // |
| 34 // Author: Dan Waylonis | 34 // Author: Dan Waylonis |
| 35 | 35 |
| 36 extern "C" { // necessary for Leopard | 36 extern "C" { // necessary for Leopard |
|
Mark Mentovai
2015/09/11 15:49:50
Same.
Ted Mielczarek
2015/09/15 12:58:36
Done.
| |
| 37 #include <assert.h> | 37 #include <assert.h> |
| 38 #include <fcntl.h> | 38 #include <fcntl.h> |
| 39 #include <mach-o/arch.h> | 39 #include <mach-o/arch.h> |
| 40 #include <mach-o/fat.h> | |
| 40 #include <mach-o/loader.h> | 41 #include <mach-o/loader.h> |
| 41 #include <mach-o/swap.h> | |
| 42 #include <string.h> | 42 #include <string.h> |
| 43 #include <unistd.h> | 43 #include <unistd.h> |
| 44 } | 44 } |
| 45 | 45 |
| 46 #include "common/mac/byteswap.h" | 46 #include "common/mac/byteswap.h" |
| 47 #include "common/mac/macho_walker.h" | 47 #include "common/mac/macho_walker.h" |
| 48 #include "common/mac/macho_utilities.h" | 48 #include "common/mac/macho_utilities.h" |
| 49 | 49 |
| 50 namespace MacFileUtilities { | 50 namespace MacFileUtilities { |
| 51 | 51 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 } | 149 } |
| 150 | 150 |
| 151 if (!is_fat) { | 151 if (!is_fat) { |
| 152 // If we don't have a fat header, check if the cpu type matches the single | 152 // If we don't have a fat header, check if the cpu type matches the single |
| 153 // header | 153 // header |
| 154 struct mach_header header; | 154 struct mach_header header; |
| 155 if (!ReadBytes(&header, sizeof(header), 0)) | 155 if (!ReadBytes(&header, sizeof(header), 0)) |
| 156 return false; | 156 return false; |
| 157 | 157 |
| 158 if (magic == MH_CIGAM || magic == MH_CIGAM_64) | 158 if (magic == MH_CIGAM || magic == MH_CIGAM_64) |
| 159 swap_mach_header(&header, NXHostByteOrder()); | 159 breakpad_swap_mach_header(&header); |
| 160 | 160 |
| 161 if (cpu_type != header.cputype || | 161 if (cpu_type != header.cputype || |
| 162 (cpu_subtype != CPU_SUBTYPE_MULTIPLE && | 162 (cpu_subtype != CPU_SUBTYPE_MULTIPLE && |
| 163 cpu_subtype != header.cpusubtype)) { | 163 cpu_subtype != header.cpusubtype)) { |
| 164 return false; | 164 return false; |
| 165 } | 165 } |
| 166 | 166 |
| 167 offset = 0; | 167 offset = 0; |
| 168 return true; | 168 return true; |
| 169 } else { | 169 } else { |
| 170 // Read the fat header and find an appropriate architecture | 170 // Read the fat header and find an appropriate architecture |
| 171 offset = 0; | 171 offset = 0; |
| 172 struct fat_header fat; | 172 struct fat_header fat; |
| 173 if (!ReadBytes(&fat, sizeof(fat), offset)) | 173 if (!ReadBytes(&fat, sizeof(fat), offset)) |
| 174 return false; | 174 return false; |
| 175 | 175 |
| 176 if (NXHostByteOrder() != NX_BigEndian) | 176 if (NXHostByteOrder() != NX_BigEndian) |
| 177 swap_fat_header(&fat, NXHostByteOrder()); | 177 breakpad_swap_fat_header(&fat); |
| 178 | 178 |
| 179 offset += sizeof(fat); | 179 offset += sizeof(fat); |
| 180 | 180 |
| 181 // Search each architecture for the desired one | 181 // Search each architecture for the desired one |
| 182 struct fat_arch arch; | 182 struct fat_arch arch; |
| 183 for (uint32_t i = 0; i < fat.nfat_arch; ++i) { | 183 for (uint32_t i = 0; i < fat.nfat_arch; ++i) { |
| 184 if (!ReadBytes(&arch, sizeof(arch), offset)) | 184 if (!ReadBytes(&arch, sizeof(arch), offset)) |
| 185 return false; | 185 return false; |
| 186 | 186 |
| 187 if (NXHostByteOrder() != NX_BigEndian) | 187 if (NXHostByteOrder() != NX_BigEndian) |
| 188 swap_fat_arch(&arch, 1, NXHostByteOrder()); | 188 breakpad_swap_fat_arch(&arch, 1); |
| 189 | 189 |
| 190 if (arch.cputype == cpu_type && | 190 if (arch.cputype == cpu_type && |
| 191 (cpu_subtype == CPU_SUBTYPE_MULTIPLE || | 191 (cpu_subtype == CPU_SUBTYPE_MULTIPLE || |
| 192 arch.cpusubtype == cpu_subtype)) { | 192 arch.cpusubtype == cpu_subtype)) { |
| 193 offset = arch.offset; | 193 offset = arch.offset; |
| 194 return true; | 194 return true; |
| 195 } | 195 } |
| 196 | 196 |
| 197 offset += sizeof(arch); | 197 offset += sizeof(arch); |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 return false; | 201 return false; |
| 202 } | 202 } |
| 203 | 203 |
| 204 bool MachoWalker::WalkHeaderAtOffset(off_t offset) { | 204 bool MachoWalker::WalkHeaderAtOffset(off_t offset) { |
| 205 struct mach_header header; | 205 struct mach_header header; |
| 206 if (!ReadBytes(&header, sizeof(header), offset)) | 206 if (!ReadBytes(&header, sizeof(header), offset)) |
| 207 return false; | 207 return false; |
| 208 | 208 |
| 209 bool swap = (header.magic == MH_CIGAM); | 209 bool swap = (header.magic == MH_CIGAM); |
| 210 if (swap) | 210 if (swap) |
| 211 swap_mach_header(&header, NXHostByteOrder()); | 211 breakpad_swap_mach_header(&header); |
| 212 | 212 |
| 213 // Copy the data into the mach_header_64 structure. Since the 32-bit and | 213 // Copy the data into the mach_header_64 structure. Since the 32-bit and |
| 214 // 64-bit only differ in the last field (reserved), this is safe to do. | 214 // 64-bit only differ in the last field (reserved), this is safe to do. |
| 215 struct mach_header_64 header64; | 215 struct mach_header_64 header64; |
| 216 memcpy((void *)&header64, (const void *)&header, sizeof(header)); | 216 memcpy((void *)&header64, (const void *)&header, sizeof(header)); |
| 217 header64.reserved = 0; | 217 header64.reserved = 0; |
| 218 | 218 |
| 219 current_header_ = &header64; | 219 current_header_ = &header64; |
| 220 current_header_size_ = sizeof(header); // 32-bit, not 64-bit | 220 current_header_size_ = sizeof(header); // 32-bit, not 64-bit |
| 221 current_header_offset_ = offset; | 221 current_header_offset_ = offset; |
| 222 offset += current_header_size_; | 222 offset += current_header_size_; |
| 223 bool result = WalkHeaderCore(offset, header.ncmds, swap); | 223 bool result = WalkHeaderCore(offset, header.ncmds, swap); |
| 224 current_header_ = NULL; | 224 current_header_ = NULL; |
| 225 current_header_size_ = 0; | 225 current_header_size_ = 0; |
| 226 current_header_offset_ = 0; | 226 current_header_offset_ = 0; |
| 227 return result; | 227 return result; |
| 228 } | 228 } |
| 229 | 229 |
| 230 bool MachoWalker::WalkHeader64AtOffset(off_t offset) { | 230 bool MachoWalker::WalkHeader64AtOffset(off_t offset) { |
| 231 struct mach_header_64 header; | 231 struct mach_header_64 header; |
| 232 if (!ReadBytes(&header, sizeof(header), offset)) | 232 if (!ReadBytes(&header, sizeof(header), offset)) |
| 233 return false; | 233 return false; |
| 234 | 234 |
| 235 bool swap = (header.magic == MH_CIGAM_64); | 235 bool swap = (header.magic == MH_CIGAM_64); |
| 236 if (swap) | 236 if (swap) |
| 237 breakpad_swap_mach_header_64(&header, NXHostByteOrder()); | 237 breakpad_swap_mach_header_64(&header); |
| 238 | 238 |
| 239 current_header_ = &header; | 239 current_header_ = &header; |
| 240 current_header_size_ = sizeof(header); | 240 current_header_size_ = sizeof(header); |
| 241 current_header_offset_ = offset; | 241 current_header_offset_ = offset; |
| 242 offset += current_header_size_; | 242 offset += current_header_size_; |
| 243 bool result = WalkHeaderCore(offset, header.ncmds, swap); | 243 bool result = WalkHeaderCore(offset, header.ncmds, swap); |
| 244 current_header_ = NULL; | 244 current_header_ = NULL; |
| 245 current_header_size_ = 0; | 245 current_header_size_ = 0; |
| 246 current_header_offset_ = 0; | 246 current_header_offset_ = 0; |
| 247 return result; | 247 return result; |
| 248 } | 248 } |
| 249 | 249 |
| 250 bool MachoWalker::WalkHeaderCore(off_t offset, uint32_t number_of_commands, | 250 bool MachoWalker::WalkHeaderCore(off_t offset, uint32_t number_of_commands, |
| 251 bool swap) { | 251 bool swap) { |
| 252 for (uint32_t i = 0; i < number_of_commands; ++i) { | 252 for (uint32_t i = 0; i < number_of_commands; ++i) { |
| 253 struct load_command cmd; | 253 struct load_command cmd; |
| 254 if (!ReadBytes(&cmd, sizeof(cmd), offset)) | 254 if (!ReadBytes(&cmd, sizeof(cmd), offset)) |
| 255 return false; | 255 return false; |
| 256 | 256 |
| 257 if (swap) | 257 if (swap) |
| 258 swap_load_command(&cmd, NXHostByteOrder()); | 258 breakpad_swap_load_command(&cmd); |
| 259 | 259 |
| 260 // Call the user callback | 260 // Call the user callback |
| 261 if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_)) | 261 if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_)) |
| 262 break; | 262 break; |
| 263 | 263 |
| 264 offset += cmd.cmdsize; | 264 offset += cmd.cmdsize; |
| 265 } | 265 } |
| 266 | 266 |
| 267 return true; | 267 return true; |
| 268 } | 268 } |
| 269 | 269 |
| 270 } // namespace MacFileUtilities | 270 } // namespace MacFileUtilities |
| OLD | NEW |