PageRenderTime 54ms CodeModel.GetById 13ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/common/mac/macho_walker.cc

http://github.com/tomahawk-player/tomahawk
C++ | 267 lines | 182 code | 42 blank | 43 comment | 55 complexity | f725888230b609aeb70cb6fa5fd31836 MD5 | raw file
  1// Copyright (c) 2006, Google Inc.
  2// All rights reserved.
  3//
  4// Redistribution and use in source and binary forms, with or without
  5// modification, are permitted provided that the following conditions are
  6// met:
  7//
  8//     * Redistributions of source code must retain the above copyright
  9// notice, this list of conditions and the following disclaimer.
 10//     * Redistributions in binary form must reproduce the above
 11// copyright notice, this list of conditions and the following disclaimer
 12// in the documentation and/or other materials provided with the
 13// distribution.
 14//     * Neither the name of Google Inc. nor the names of its
 15// contributors may be used to endorse or promote products derived from
 16// this software without specific prior written permission.
 17//
 18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 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.
 29
 30// macho_walker.cc: Iterate over the load commands in a mach-o file
 31//
 32// See macho_walker.h for documentation
 33//
 34// Author: Dan Waylonis
 35
 36extern "C" {  // necessary for Leopard
 37  #include <assert.h>
 38  #include <fcntl.h>
 39  #include <mach-o/arch.h>
 40  #include <mach-o/loader.h>
 41  #include <mach-o/swap.h>
 42  #include <string.h>
 43  #include <unistd.h>
 44}
 45
 46#include "common/mac/byteswap.h"
 47#include "common/mac/macho_walker.h"
 48#include "common/mac/macho_utilities.h"
 49
 50namespace MacFileUtilities {
 51
 52MachoWalker::MachoWalker(const char *path, LoadCommandCallback callback,
 53                         void *context)
 54    : file_(0),
 55      memory_(NULL),
 56      memory_size_(0),
 57      callback_(callback),
 58      callback_context_(context),
 59      current_header_(NULL),
 60      current_header_size_(0),
 61      current_header_offset_(0) {
 62  file_ = open(path, O_RDONLY);
 63}
 64
 65MachoWalker::MachoWalker(void *memory, size_t size,
 66                         LoadCommandCallback callback, void *context)
 67    : file_(0),
 68      memory_(memory),
 69      memory_size_(size),
 70      callback_(callback),
 71      callback_context_(context),
 72      current_header_(NULL),
 73      current_header_size_(0),
 74      current_header_offset_(0) {
 75}
 76
 77MachoWalker::~MachoWalker() {
 78  if (file_ != -1)
 79    close(file_);
 80}
 81
 82int MachoWalker::ValidateCPUType(int cpu_type) {
 83  // If the user didn't specify, use the local architecture.
 84  if (cpu_type == 0) {
 85    const NXArchInfo *arch = NXGetLocalArchInfo();
 86    assert(arch);
 87    cpu_type = arch->cputype;
 88  }
 89
 90  return cpu_type;
 91}
 92
 93bool MachoWalker::WalkHeader(int cpu_type) {
 94  int valid_cpu_type = ValidateCPUType(cpu_type);
 95  off_t offset;
 96  if (FindHeader(valid_cpu_type, offset)) {
 97    if (cpu_type & CPU_ARCH_ABI64)
 98      return WalkHeader64AtOffset(offset);
 99
100    return WalkHeaderAtOffset(offset);
101  }
102
103  return false;
104}
105
106bool MachoWalker::ReadBytes(void *buffer, size_t size, off_t offset) {
107  if (memory_) {
108    if (offset < 0)
109      return false;
110    bool result = true;
111    if (offset + size > memory_size_) {
112      if (static_cast<size_t>(offset) >= memory_size_)
113        return false;
114      size = memory_size_ - offset;
115      result = false;
116    }
117    memcpy(buffer, static_cast<char *>(memory_) + offset, size);
118    return result;
119  } else {
120    return pread(file_, buffer, size, offset) == (ssize_t)size;
121  }
122}
123
124bool MachoWalker::CurrentHeader(struct mach_header_64 *header, off_t *offset) {
125  if (current_header_) {
126    memcpy(header, current_header_, sizeof(mach_header_64));
127    *offset = current_header_offset_;
128    return true;
129  }
130
131  return false;
132}
133
134bool MachoWalker::FindHeader(int cpu_type, off_t &offset) {
135  int valid_cpu_type = ValidateCPUType(cpu_type);
136  // Read the magic bytes that's common amongst all mach-o files
137  uint32_t magic;
138  if (!ReadBytes(&magic, sizeof(magic), 0))
139    return false;
140
141  offset = sizeof(magic);
142
143  // Figure out what type of file we've got
144  bool is_fat = false;
145  if (magic == FAT_MAGIC || magic == FAT_CIGAM) {
146    is_fat = true;
147  }
148  else if (magic != MH_MAGIC && magic != MH_CIGAM && magic != MH_MAGIC_64 &&
149           magic != MH_CIGAM_64) {
150    return false;
151  }
152
153  if (!is_fat) {
154    // If we don't have a fat header, check if the cpu type matches the single
155    // header
156    cpu_type_t header_cpu_type;
157    if (!ReadBytes(&header_cpu_type, sizeof(header_cpu_type), offset))
158      return false;
159
160    if (magic == MH_CIGAM || magic == MH_CIGAM_64)
161      header_cpu_type = ByteSwap(header_cpu_type);
162
163    if (valid_cpu_type != header_cpu_type)
164      return false;
165
166    offset = 0;
167    return true;
168  } else {
169    // Read the fat header and find an appropriate architecture
170    offset = 0;
171    struct fat_header fat;
172    if (!ReadBytes(&fat, sizeof(fat), offset))
173      return false;
174
175    if (NXHostByteOrder() != NX_BigEndian)
176      swap_fat_header(&fat, NXHostByteOrder());
177
178    offset += sizeof(fat);
179
180    // Search each architecture for the desired one
181    struct fat_arch arch;
182    for (uint32_t i = 0; i < fat.nfat_arch; ++i) {
183      if (!ReadBytes(&arch, sizeof(arch), offset))
184        return false;
185
186      if (NXHostByteOrder() != NX_BigEndian)
187        swap_fat_arch(&arch, 1, NXHostByteOrder());
188
189      if (arch.cputype == valid_cpu_type) {
190        offset = arch.offset;
191        return true;
192      }
193
194      offset += sizeof(arch);
195    }
196  }
197
198  return false;
199}
200
201bool MachoWalker::WalkHeaderAtOffset(off_t offset) {
202  struct mach_header header;
203  if (!ReadBytes(&header, sizeof(header), offset))
204    return false;
205
206  bool swap = (header.magic == MH_CIGAM);
207  if (swap)
208    swap_mach_header(&header, NXHostByteOrder());
209
210  // Copy the data into the mach_header_64 structure.  Since the 32-bit and
211  // 64-bit only differ in the last field (reserved), this is safe to do.
212  struct mach_header_64 header64;
213  memcpy((void *)&header64, (const void *)&header, sizeof(header));
214  header64.reserved = 0;
215
216  current_header_ = &header64;
217  current_header_size_ = sizeof(header); // 32-bit, not 64-bit
218  current_header_offset_ = offset;
219  offset += current_header_size_;
220  bool result = WalkHeaderCore(offset, header.ncmds, swap);
221  current_header_ = NULL;
222  current_header_size_ = 0;
223  current_header_offset_ = 0;
224  return result;
225}
226
227bool MachoWalker::WalkHeader64AtOffset(off_t offset) {
228  struct mach_header_64 header;
229  if (!ReadBytes(&header, sizeof(header), offset))
230    return false;
231
232  bool swap = (header.magic == MH_CIGAM_64);
233  if (swap)
234    breakpad_swap_mach_header_64(&header, NXHostByteOrder());
235
236  current_header_ = &header;
237  current_header_size_ = sizeof(header);
238  current_header_offset_ = offset;
239  offset += current_header_size_;
240  bool result = WalkHeaderCore(offset, header.ncmds, swap);
241  current_header_ = NULL;
242  current_header_size_ = 0;
243  current_header_offset_ = 0;
244  return result;
245}
246
247bool MachoWalker::WalkHeaderCore(off_t offset, uint32_t number_of_commands,
248                                 bool swap) {
249  for (uint32_t i = 0; i < number_of_commands; ++i) {
250    struct load_command cmd;
251    if (!ReadBytes(&cmd, sizeof(cmd), offset))
252      return false;
253
254    if (swap)
255      swap_load_command(&cmd, NXHostByteOrder());
256
257    // Call the user callback
258    if (callback_ && !callback_(this, &cmd, offset, swap, callback_context_))
259      break;
260
261    offset += cmd.cmdsize;
262  }
263
264  return true;
265}
266
267}  // namespace MacFileUtilities