breakpad_nlist_test.cc revision d6f0f92de3692867d96bab15d70e0ce7a76cc04e
1// Copyright (c) 2008, Google Inc. 2// All rights reserved 3// Redistribution and use in source and binary forms, with or without 4// modification, are permitted provided that the following conditions are 5// met: 6// 7// * Redistributions of source code must retain the above copyright 8// notice, this list of conditions and the following disclaimer. 9// * Redistributions in binary form must reproduce the above 10// copyright notice, this list of conditions and the following disclaimer 11// in the documentation and/or other materials provided with the 12// distribution. 13// * Neither the name of Google Inc. nor the names of its 14// contributors may be used to endorse or promote products derived from 15// this software without specific prior written permission. 16// 17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29// 30// breakpad_nlist_test.cc 31// minidump_test 32// 33// Created by Neal Sidhwaney on 4/13/08. 34// Copyright 2008 Google Inc. All rights reserved. 35// 36 37#include "client/mac/handler/testcases/breakpad_nlist_test.h" 38#include <mach-o/nlist.h> 39#include "client/mac/handler/breakpad_nlist_64.h" 40 41BreakpadNlistTest test1(TEST_INVOCATION(BreakpadNlistTest, CompareToNM)); 42 43BreakpadNlistTest::BreakpadNlistTest(TestInvocation *invocation) 44 : TestCase(invocation) { 45} 46 47 48BreakpadNlistTest::~BreakpadNlistTest() { 49} 50 51void BreakpadNlistTest::CompareToNM() { 52#if TARGET_CPU_X86_64 53 system("/usr/bin/nm -arch x86_64 /usr/lib/dyld > /tmp/dyld-namelist.txt"); 54#elif TARGET_CPU_PPC64 55 system("/usr/bin/nm -arch ppc64 /usr/lib/dyld > /tmp/dyld-namelist.txt"); 56#endif 57 58 FILE *fd = fopen("/tmp/dyld-namelist.txt", "rt"); 59 60 char oneNMAddr[30]; 61 char symbolType; 62 char symbolName[500]; 63 while (!feof(fd)) { 64 fscanf(fd, "%s %c %s", oneNMAddr, &symbolType, symbolName); 65 breakpad_nlist symbolList[2]; 66 breakpad_nlist &list = symbolList[0]; 67 68 memset(symbolList, 0, sizeof(breakpad_nlist)*2); 69 const char *symbolNames[2]; 70 symbolNames[0] = (const char*)symbolName; 71 symbolNames[1] = "\0"; 72 breakpad_nlist_64("/usr/lib/dyld", &list, symbolNames); 73 uint64_t nmAddr = strtol(oneNMAddr, NULL, 16); 74 if (!IsSymbolMoreThanOnceInDyld(symbolName)) { 75 CPTAssert(nmAddr == symbolList[0].n_value); 76 } 77 } 78 79 fclose(fd); 80} 81 82bool BreakpadNlistTest::IsSymbolMoreThanOnceInDyld(const char *symbolName) { 83 // These are the symbols that occur more than once when nm dumps 84 // the symbol table of /usr/lib/dyld. Our nlist program returns 85 // the first address because it's doing a search so we need to exclude 86 // these from causing the test to fail 87 const char *multipleSymbols[] = { 88 "__Z41__static_initialization_and_destruction_0ii", 89 "___tcf_0", 90 "___tcf_1", 91 "_read_encoded_value_with_base", 92 "_read_sleb128", 93 "_read_uleb128", 94 "\0"}; 95 96 bool found = false; 97 98 for (int i = 0; multipleSymbols[i][0]; i++) { 99 if (!strcmp(multipleSymbols[i], symbolName)) { 100 found = true; 101 break; 102 } 103 } 104 105 return found; 106} 107