PageRenderTime 50ms CodeModel.GetById 14ms app.highlight 31ms RepoModel.GetById 2ms app.codeStats 0ms

/thirdparty/breakpad/client/mac/handler/breakpad_nlist_64.cc

http://github.com/tomahawk-player/tomahawk
C++ | 413 lines | 273 code | 39 blank | 101 comment | 76 complexity | d608acb4097418f3d80d970d556d395c MD5 | raw file
  1/*
  2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
  3 *
  4 * @APPLE_LICENSE_HEADER_START@
  5 * 
  6 * This file contains Original Code and/or Modifications of Original Code
  7 * as defined in and that are subject to the Apple Public Source License
  8 * Version 2.0 (the 'License'). You may not use this file except in
  9 * compliance with the License. Please obtain a copy of the License at
 10 * http://www.opensource.apple.com/apsl/ and read it before using this
 11 * file.
 12 * 
 13 * The Original Code and all software distributed under the License are
 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 18 * Please see the License for the specific language governing rights and
 19 * limitations under the License.
 20 * 
 21 * @APPLE_LICENSE_HEADER_END@
 22 */
 23/*
 24 * Copyright (c) 1989, 1993
 25 * The Regents of the University of California.  All rights reserved.
 26 *
 27 * Redistribution and use in source and binary forms, with or without
 28 * modification, are permitted provided that the following conditions
 29 * are met:
 30 * 1. Redistributions of source code must retain the above copyright
 31 *    notice, this list of conditions and the following disclaimer.
 32 * 2. Redistributions in binary form must reproduce the above copyright
 33 *    notice, this list of conditions and the following disclaimer in the
 34 *    documentation and/or other materials provided with the distribution.
 35 * 3. All advertising materials mentioning features or use of this software
 36 *    must display the following acknowledgement:
 37 *      This product includes software developed by the University of
 38 *      California, Berkeley and its contributors.
 39 * 4. Neither the name of the University nor the names of its contributors
 40 *    may be used to endorse or promote products derived from this software
 41 *    without specific prior written permission.
 42 *
 43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 46 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 53 * SUCH DAMAGE.
 54 */
 55
 56
 57/*
 58 * This file was copied from libc/gen/nlist.c from Darwin's source code       
 59 * The version of nlist used as a base is from 10.5.2, libc-498               
 60 * http://www.opensource.apple.com/darwinsource/10.5.2/Libc-498/gen/nlist.c   
 61 *                                                                            
 62 * The full tarball is at:                                                    
 63 * http://www.opensource.apple.com/darwinsource/tarballs/apsl/Libc-498.tar.gz 
 64 *                                                                            
 65 * I've modified it to be compatible with 64-bit images.
 66*/
 67
 68#include "breakpad_nlist_64.h"
 69
 70#include <CoreFoundation/CoreFoundation.h>
 71#include <fcntl.h>
 72#include <mach-o/nlist.h>
 73#include <mach-o/loader.h>
 74#include <mach-o/fat.h>
 75#include <mach/mach.h>
 76#include <stdio.h>
 77#include <stdlib.h>
 78#include <sys/types.h>
 79#include <sys/uio.h>
 80#include <TargetConditionals.h>
 81#include <unistd.h>
 82
 83/* Stuff lifted from <a.out.h> and <sys/exec.h> since they are gone */
 84/*
 85 * Header prepended to each a.out file.
 86 */
 87struct exec {
 88  unsigned short  a_machtype;     /* machine type */
 89  unsigned short  a_magic;        /* magic number */
 90  unsigned long a_text;         /* size of text segment */
 91  unsigned long a_data;         /* size of initialized data */
 92  unsigned long a_bss;          /* size of uninitialized data */
 93  unsigned long a_syms;         /* size of symbol table */
 94  unsigned long a_entry;        /* entry point */
 95  unsigned long a_trsize;       /* size of text relocation */
 96  unsigned long a_drsize;       /* size of data relocation */
 97};
 98
 99#define OMAGIC  0407            /* old impure format */
100#define NMAGIC  0410            /* read-only text */
101#define ZMAGIC  0413            /* demand load format */
102
103#define N_BADMAG(x)                                                     \
104  (((x).a_magic)!=OMAGIC && ((x).a_magic)!=NMAGIC && ((x).a_magic)!=ZMAGIC)
105#define N_TXTOFF(x)                                     \
106  ((x).a_magic==ZMAGIC ? 0 : sizeof (struct exec))
107#define N_SYMOFF(x)                                                     \
108  (N_TXTOFF(x) + (x).a_text+(x).a_data + (x).a_trsize+(x).a_drsize)
109
110// Traits structs for specializing function templates to handle
111// 32-bit/64-bit Mach-O files.
112template<typename T>
113struct MachBits {};
114
115typedef struct nlist nlist32;
116typedef struct nlist_64 nlist64;
117
118template<>
119struct MachBits<nlist32> {
120  typedef mach_header mach_header_type;
121  typedef uint32_t word_type;
122  static const uint32_t magic = MH_MAGIC;
123};
124
125template<>
126struct MachBits<nlist64> {
127  typedef mach_header_64 mach_header_type;
128  typedef uint64_t word_type;
129  static const uint32_t magic = MH_MAGIC_64;
130};
131
132template<typename nlist_type>
133int
134__breakpad_fdnlist(int fd, nlist_type *list, const char **symbolNames,
135                   cpu_type_t cpu_type);
136
137/*
138 * nlist - retreive attributes from name list (string table version)
139 */
140
141template <typename nlist_type>
142int breakpad_nlist_common(const char *name,
143                          nlist_type *list,
144                          const char **symbolNames,
145                          cpu_type_t cpu_type) {
146  int fd = open(name, O_RDONLY, 0);
147  if (fd < 0)
148    return -1;
149  int n = __breakpad_fdnlist(fd, list, symbolNames, cpu_type);
150  close(fd);
151  return n;
152}
153
154int breakpad_nlist(const char *name,
155                   struct nlist *list,
156                   const char **symbolNames,
157                   cpu_type_t cpu_type) {
158  return breakpad_nlist_common(name, list, symbolNames, cpu_type);
159}
160
161int breakpad_nlist(const char *name,
162                   struct nlist_64 *list,
163                   const char **symbolNames,
164                   cpu_type_t cpu_type) {
165  return breakpad_nlist_common(name, list, symbolNames, cpu_type);
166}
167
168/* Note: __fdnlist() is called from kvm_nlist in libkvm's kvm.c */
169
170template<typename nlist_type>
171int __breakpad_fdnlist(int fd, nlist_type *list, const char **symbolNames,
172                       cpu_type_t cpu_type) {
173  typedef typename MachBits<nlist_type>::mach_header_type mach_header_type;
174  typedef typename MachBits<nlist_type>::word_type word_type;
175
176  const uint32_t magic = MachBits<nlist_type>::magic;
177
178  int maxlen = 500;
179  int nreq = 0;
180  for (nlist_type* q = list;
181       symbolNames[q-list] && symbolNames[q-list][0];
182       q++, nreq++) {
183
184    q->n_type = 0;
185    q->n_value = 0;
186    q->n_desc = 0;
187    q->n_sect = 0;
188    q->n_un.n_strx = 0;
189  }
190
191  struct exec buf;
192  if (read(fd, (char *)&buf, sizeof(buf)) != sizeof(buf) ||
193      (N_BADMAG(buf) && *((uint32_t *)&buf) != magic &&
194        CFSwapInt32BigToHost(*((uint32_t *)&buf)) != FAT_MAGIC &&
195       /* The following is the big-endian ppc64 check */
196       (*((uint32_t*)&buf)) != FAT_MAGIC)) {
197    return -1;
198  }
199
200  /* Deal with fat file if necessary */
201  unsigned arch_offset = 0;
202  if (CFSwapInt32BigToHost(*((uint32_t *)&buf)) == FAT_MAGIC ||
203      /* The following is the big-endian ppc64 check */
204      *((unsigned int *)&buf) == FAT_MAGIC) {
205    /* Get host info */
206    host_t host = mach_host_self();
207    unsigned i = HOST_BASIC_INFO_COUNT;
208    struct host_basic_info hbi;
209    kern_return_t kr;
210    if ((kr = host_info(host, HOST_BASIC_INFO,
211                        (host_info_t)(&hbi), &i)) != KERN_SUCCESS) {
212      return -1;
213    }
214    mach_port_deallocate(mach_task_self(), host);
215
216    /* Read in the fat header */
217    struct fat_header fh;
218    if (lseek(fd, 0, SEEK_SET) == -1) {
219      return -1;
220    }
221    if (read(fd, (char *)&fh, sizeof(fh)) != sizeof(fh)) {
222      return -1;
223    }
224
225    /* Convert fat_narchs to host byte order */
226    fh.nfat_arch = CFSwapInt32BigToHost(fh.nfat_arch);
227
228    /* Read in the fat archs */
229    struct fat_arch *fat_archs =
230        (struct fat_arch *)malloc(fh.nfat_arch * sizeof(struct fat_arch));
231    if (fat_archs == NULL) {
232      return -1;
233    }
234    if (read(fd, (char *)fat_archs,
235             sizeof(struct fat_arch) * fh.nfat_arch) !=
236        (ssize_t)(sizeof(struct fat_arch) * fh.nfat_arch)) {
237      free(fat_archs);
238      return -1;
239    }
240
241    /*
242     * Convert archs to host byte ordering (a constraint of
243     * cpusubtype_getbestarch()
244     */
245    for (unsigned i = 0; i < fh.nfat_arch; i++) {
246      fat_archs[i].cputype =
247        CFSwapInt32BigToHost(fat_archs[i].cputype);
248      fat_archs[i].cpusubtype =
249        CFSwapInt32BigToHost(fat_archs[i].cpusubtype);
250      fat_archs[i].offset =
251        CFSwapInt32BigToHost(fat_archs[i].offset);
252      fat_archs[i].size =
253        CFSwapInt32BigToHost(fat_archs[i].size);
254      fat_archs[i].align =
255        CFSwapInt32BigToHost(fat_archs[i].align);
256    }
257
258    struct fat_arch *fap = NULL;
259    for (unsigned i = 0; i < fh.nfat_arch; i++) {
260      if (fat_archs[i].cputype == cpu_type) {
261        fap = &fat_archs[i];
262        break;
263      }
264    }
265
266    if (!fap) {
267      free(fat_archs);
268      return -1;
269    }
270    arch_offset = fap->offset;
271    free(fat_archs);
272
273    /* Read in the beginning of the architecture-specific file */
274    if (lseek(fd, arch_offset, SEEK_SET) == -1) {
275      return -1;
276    }
277    if (read(fd, (char *)&buf, sizeof(buf)) != sizeof(buf)) {
278      return -1;
279    }
280  }
281
282  off_t sa;  /* symbol address */
283  off_t ss;  /* start of strings */
284  register register_t n;
285  if (*((unsigned int *)&buf) == magic) {
286    if (lseek(fd, arch_offset, SEEK_SET) == -1) {
287      return -1;
288    }
289    mach_header_type mh;
290    if (read(fd, (char *)&mh, sizeof(mh)) != sizeof(mh)) {
291      return -1;
292    }
293
294    struct load_command *load_commands =
295        (struct load_command *)malloc(mh.sizeofcmds);
296    if (load_commands == NULL) {
297      return -1;
298    }
299    if (read(fd, (char *)load_commands, mh.sizeofcmds) !=
300        (ssize_t)mh.sizeofcmds) {
301      free(load_commands);
302      return -1;
303    }
304    struct symtab_command *stp = NULL;
305    struct load_command *lcp = load_commands;
306    // iterate through all load commands, looking for
307    // LC_SYMTAB load command
308    for (uint32_t i = 0; i < mh.ncmds; i++) {
309      if (lcp->cmdsize % sizeof(word_type) != 0 ||
310          lcp->cmdsize <= 0 ||
311          (char *)lcp + lcp->cmdsize >
312          (char *)load_commands + mh.sizeofcmds) {
313        free(load_commands);
314        return -1;
315      }
316      if (lcp->cmd == LC_SYMTAB) {
317        if (lcp->cmdsize !=
318            sizeof(struct symtab_command)) {
319          free(load_commands);
320          return -1;
321        }
322        stp = (struct symtab_command *)lcp;
323        break;
324      }
325      lcp = (struct load_command *)
326        ((char *)lcp + lcp->cmdsize);
327    }
328    if (stp == NULL) {
329      free(load_commands);
330      return -1;
331    }
332    // sa points to the beginning of the symbol table
333    sa = stp->symoff + arch_offset;
334    // ss points to the beginning of the string table
335    ss = stp->stroff + arch_offset;
336    // n is the number of bytes in the symbol table
337    // each symbol table entry is an nlist structure
338    n = stp->nsyms * sizeof(nlist_type);
339    free(load_commands);
340  } else {
341    sa = N_SYMOFF(buf) + arch_offset;
342    ss = sa + buf.a_syms + arch_offset;
343    n = buf.a_syms;
344  }
345
346  if (lseek(fd, sa, SEEK_SET) == -1) {
347    return -1;
348  }
349
350  // the algorithm here is to read the nlist entries in m-sized
351  // chunks into q.  q is then iterated over. for each entry in q,
352  // use the string table index(q->n_un.n_strx) to read the symbol 
353  // name, then scan the nlist entries passed in by the user(via p),
354  // and look for a match
355  while (n) {
356    nlist_type space[BUFSIZ/sizeof (nlist_type)];
357    register register_t m = sizeof (space);
358
359    if (n < m)
360      m = n;
361    if (read(fd, (char *)space, m) != m)
362      break;
363    n -= m;
364    long savpos = lseek(fd, 0, SEEK_CUR);
365    if (savpos == -1) {
366      return -1;
367    }
368    for (nlist_type* q = space; (m -= sizeof(nlist_type)) >= 0; q++) {
369      char nambuf[BUFSIZ];
370
371      if (q->n_un.n_strx == 0 || q->n_type & N_STAB)
372        continue;
373
374      // seek to the location in the binary where the symbol
375      // name is stored & read it into memory
376      if (lseek(fd, ss+q->n_un.n_strx, SEEK_SET) == -1) {
377        return -1;
378      }
379      if (read(fd, nambuf, maxlen+1) == -1) {
380        return -1;
381      }
382      const char *s2 = nambuf;
383      for (nlist_type *p = list; 
384           symbolNames[p-list] && symbolNames[p-list][0];
385           p++) {
386        // get the symbol name the user has passed in that 
387        // corresponds to the nlist entry that we're looking at
388        const char *s1 = symbolNames[p - list];
389        while (*s1) {
390          if (*s1++ != *s2++)
391            goto cont;
392        }
393        if (*s2)
394          goto cont;
395
396        p->n_value = q->n_value;
397        p->n_type = q->n_type;
398        p->n_desc = q->n_desc;
399        p->n_sect = q->n_sect;
400        p->n_un.n_strx = q->n_un.n_strx;
401        if (--nreq == 0)
402          return nreq;
403
404        break;
405      cont:           ;
406      }
407    }
408    if (lseek(fd, savpos, SEEK_SET) == -1) {
409      return -1;
410    }
411  }
412  return nreq;
413}