PageRenderTime 38ms CodeModel.GetById 14ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/thirdparty/breakpad/common/linux/dump_symbols_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 166 lines | 110 code | 19 blank | 37 comment | 2 complexity | ffe2e1a83c4c9a8f1178b8b422654a46 MD5 | raw file
  1// Copyright (c) 2011 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// Original author: Ted Mielczarek <ted.mielczarek@gmail.com>
 31
 32// dump_symbols_unittest.cc:
 33// Unittests for google_breakpad::DumpSymbols
 34
 35#include <elf.h>
 36#include <link.h>
 37#include <stdio.h>
 38
 39#include <sstream>
 40#include <string>
 41#include <vector>
 42
 43#include "breakpad_googletest_includes.h"
 44#include "common/linux/synth_elf.h"
 45
 46namespace google_breakpad {
 47bool WriteSymbolFileInternal(uint8_t* obj_file,
 48                             const std::string &obj_filename,
 49                             const std::string &debug_dir,
 50                             bool cfi,
 51                             std::ostream &sym_stream);
 52}
 53
 54using google_breakpad::synth_elf::ELF;
 55using google_breakpad::synth_elf::StringTable;
 56using google_breakpad::synth_elf::SymbolTable;
 57using google_breakpad::test_assembler::kLittleEndian;
 58using google_breakpad::test_assembler::Section;
 59using google_breakpad::WriteSymbolFileInternal;
 60using std::string;
 61using std::stringstream;
 62using std::vector;
 63using ::testing::Test;
 64
 65class DumpSymbols : public Test {
 66 public:
 67  void GetElfContents(ELF& elf) {
 68    string contents;
 69    ASSERT_TRUE(elf.GetContents(&contents));
 70    ASSERT_LT(0, contents.size());
 71
 72    elfdata_v.clear();
 73    elfdata_v.insert(elfdata_v.begin(), contents.begin(), contents.end());
 74    elfdata = &elfdata_v[0];
 75  }
 76
 77  vector<uint8_t> elfdata_v;
 78  uint8_t* elfdata;
 79};
 80
 81TEST_F(DumpSymbols, Invalid) {
 82  Elf32_Ehdr header;
 83  memset(&header, 0, sizeof(header));
 84  stringstream s;
 85  EXPECT_FALSE(WriteSymbolFileInternal(reinterpret_cast<uint8_t*>(&header),
 86                                       "foo",
 87                                       "",
 88                                       true,
 89                                       s));
 90}
 91
 92// TODO(ted): Fix the dump_symbols code to deal with cross-word-size
 93// ELF files.
 94#if __ELF_NATIVE_CLASS == 32
 95TEST_F(DumpSymbols, SimplePublic32) {
 96  ELF elf(EM_386, ELFCLASS32, kLittleEndian);
 97  // Zero out text section for simplicity.
 98  Section text(kLittleEndian);
 99  text.Append(4096, 0);
100  elf.AddSection(".text", text, SHT_PROGBITS);
101
102  // Add a public symbol.
103  StringTable table(kLittleEndian);
104  SymbolTable syms(kLittleEndian, 4, table);
105  syms.AddSymbol("superfunc", (uint32_t)0x1000, (uint32_t)0x10,
106                 ELF32_ST_INFO(STB_GLOBAL, STT_FUNC),
107                 SHN_UNDEF + 1);
108  int index = elf.AddSection(".dynstr", table, SHT_STRTAB);
109  elf.AddSection(".dynsym", syms,
110                 SHT_DYNSYM,          // type
111                 SHF_ALLOC,           // flags
112                 0,                   // addr
113                 index,               // link
114                 sizeof(Elf32_Sym));  // entsize
115
116  elf.Finish();
117  GetElfContents(elf);
118
119  stringstream s;
120  ASSERT_TRUE(WriteSymbolFileInternal(elfdata,
121                                      "foo",
122                                      "",
123                                      true,
124                                      s));
125  EXPECT_EQ("MODULE Linux x86 000000000000000000000000000000000 foo\n"
126            "PUBLIC 1000 0 superfunc\n",
127            s.str());
128}
129#endif
130
131#if __ELF_NATIVE_CLASS == 64
132TEST_F(DumpSymbols, SimplePublic64) {
133  ELF elf(EM_X86_64, ELFCLASS64, kLittleEndian);
134  // Zero out text section for simplicity.
135  Section text(kLittleEndian);
136  text.Append(4096, 0);
137  elf.AddSection(".text", text, SHT_PROGBITS);
138
139  // Add a public symbol.
140  StringTable table(kLittleEndian);
141  SymbolTable syms(kLittleEndian, 8, table);
142  syms.AddSymbol("superfunc", (uint64_t)0x1000, (uint64_t)0x10,
143                 ELF64_ST_INFO(STB_GLOBAL, STT_FUNC),
144                 SHN_UNDEF + 1);
145  int index = elf.AddSection(".dynstr", table, SHT_STRTAB);
146  elf.AddSection(".dynsym", syms,
147                 SHT_DYNSYM,          // type
148                 SHF_ALLOC,           // flags
149                 0,                   // addr
150                 index,               // link
151                 sizeof(Elf64_Sym));  // entsize
152
153  elf.Finish();
154  GetElfContents(elf);
155
156  stringstream s;
157  ASSERT_TRUE(WriteSymbolFileInternal(elfdata,
158                                      "foo",
159                                      "",
160                                      true,
161                                      s));
162  EXPECT_EQ("MODULE Linux x86_64 000000000000000000000000000000000 foo\n"
163            "PUBLIC 1000 0 superfunc\n",
164            s.str());
165}
166#endif