breakpad_nlist_test.cc revision 4c39c138fe2a68206c2143d7401a113a1c4b130b
1/*
2 *  breakpad_nlist_test.cc
3 *  minidump_test
4 *
5 *  Created by Neal Sidhwaney on 4/13/08.
6 *  Copyright 2008 Google Inc. All rights reserved.
7 *
8 */
9
10#include "breakpad_nlist_test.h"
11#include <mach-o/nlist.h>
12#include "breakpad_nlist_64.h"
13
14BreakpadNlistTest test1(TEST_INVOCATION(BreakpadNlistTest, CompareToNM));
15
16BreakpadNlistTest::BreakpadNlistTest(TestInvocation *invocation)
17    : TestCase(invocation) {
18}
19
20
21BreakpadNlistTest::~BreakpadNlistTest() {
22}
23
24void BreakpadNlistTest::CompareToNM() {
25#if TARGET_CPU_X86_64
26  system("/usr/bin/nm -arch x86_64 /usr/lib/dyld > /tmp/dyld-namelist.txt");
27#elif TARGET_CPU_PPC64
28  system("/usr/bin/nm -arch ppc64 /usr/lib/dyld > /tmp/dyld-namelist.txt");
29#endif
30
31  FILE *fd = fopen("/tmp/dyld-namelist.txt","rt");
32
33  char oneNMAddr[30];
34  char symbolType;
35  char symbolName[500];
36  while(!feof(fd)) {
37    fscanf(fd,"%s %c %s",oneNMAddr, &symbolType, symbolName);
38    breakpad_nlist symbolList[2];
39    breakpad_nlist &list = symbolList[0];
40
41    memset(symbolList,0, sizeof(breakpad_nlist)*2);
42    const char *symbolNames[2];
43    symbolNames[0] = (const char*)symbolName;
44    symbolNames[1] = "\0";
45    breakpad_nlist_64("/usr/lib/dyld",&list, symbolNames);
46    uint64_t nmAddr = strtol(oneNMAddr,NULL,16);
47    if(!IsSymbolMoreThanOnceInDyld(symbolName)) {
48      CPTAssert(nmAddr == symbolList[0].n_value);
49    }
50  }
51
52  fclose(fd);
53}
54
55bool BreakpadNlistTest::IsSymbolMoreThanOnceInDyld(const char *symbolName) {
56  //These are the symbols that occur more than once when nm dumps
57  // the symbol table of /usr/lib/dyld.  Our nlist program returns
58  // the first address because it's doing a search so we need to exclude
59  // these from causing the test to fail
60  const char *multipleSymbols[] = {
61    "__Z41__static_initialization_and_destruction_0ii",
62    "___tcf_0",
63    "___tcf_1",
64    "_read_encoded_value_with_base",
65    "_read_sleb128",
66    "_read_uleb128",
67    "\0"};
68
69  bool found = false;
70
71  for(int i = 0; multipleSymbols[i][0]; i++) {
72    if(!strcmp(multipleSymbols[i],symbolName)) {
73      found = true;
74      break;
75    }
76
77  }
78
79  return found;
80
81}
82