michael@0: // Copyright (c) 2011 Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Ted Mielczarek michael@0: michael@0: // dump_symbols_unittest.cc: michael@0: // Unittests for google_breakpad::DumpSymbols michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "breakpad_googletest_includes.h" michael@0: #include "common/linux/synth_elf.h" michael@0: #include "common/module.h" michael@0: #include "common/using_std_string.h" michael@0: michael@0: namespace google_breakpad { michael@0: bool ReadSymbolDataInternal(const uint8_t* obj_file, michael@0: const string& obj_filename, michael@0: const std::vector& debug_dir, michael@0: SymbolData symbol_data, michael@0: Module** module); michael@0: } michael@0: michael@0: using google_breakpad::synth_elf::ELF; michael@0: using google_breakpad::synth_elf::StringTable; michael@0: using google_breakpad::synth_elf::SymbolTable; michael@0: using google_breakpad::test_assembler::kLittleEndian; michael@0: using google_breakpad::test_assembler::Section; michael@0: using google_breakpad::Module; michael@0: using google_breakpad::ReadSymbolDataInternal; michael@0: using std::stringstream; michael@0: using std::vector; michael@0: using ::testing::Test; michael@0: michael@0: class DumpSymbols : public Test { michael@0: public: michael@0: void GetElfContents(ELF& elf) { michael@0: string contents; michael@0: ASSERT_TRUE(elf.GetContents(&contents)); michael@0: ASSERT_LT(0U, contents.size()); michael@0: michael@0: elfdata_v.clear(); michael@0: elfdata_v.insert(elfdata_v.begin(), contents.begin(), contents.end()); michael@0: elfdata = &elfdata_v[0]; michael@0: } michael@0: michael@0: vector elfdata_v; michael@0: uint8_t* elfdata; michael@0: }; michael@0: michael@0: TEST_F(DumpSymbols, Invalid) { michael@0: Elf32_Ehdr header; michael@0: memset(&header, 0, sizeof(header)); michael@0: Module* module; michael@0: EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast(&header), michael@0: "foo", michael@0: vector(), michael@0: ALL_SYMBOL_DATA, michael@0: &module)); michael@0: } michael@0: michael@0: TEST_F(DumpSymbols, SimplePublic32) { michael@0: ELF elf(EM_386, ELFCLASS32, kLittleEndian); michael@0: // Zero out text section for simplicity. michael@0: Section text(kLittleEndian); michael@0: text.Append(4096, 0); michael@0: elf.AddSection(".text", text, SHT_PROGBITS); michael@0: michael@0: // Add a public symbol. michael@0: StringTable table(kLittleEndian); michael@0: SymbolTable syms(kLittleEndian, 4, table); michael@0: syms.AddSymbol("superfunc", (uint32_t)0x1000, (uint32_t)0x10, michael@0: ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), michael@0: SHN_UNDEF + 1); michael@0: int index = elf.AddSection(".dynstr", table, SHT_STRTAB); michael@0: elf.AddSection(".dynsym", syms, michael@0: SHT_DYNSYM, // type michael@0: SHF_ALLOC, // flags michael@0: 0, // addr michael@0: index, // link michael@0: sizeof(Elf32_Sym)); // entsize michael@0: michael@0: elf.Finish(); michael@0: GetElfContents(elf); michael@0: michael@0: Module* module; michael@0: EXPECT_TRUE(ReadSymbolDataInternal(elfdata, michael@0: "foo", michael@0: vector(), michael@0: ALL_SYMBOL_DATA, michael@0: &module)); michael@0: michael@0: stringstream s; michael@0: module->Write(s, ALL_SYMBOL_DATA); michael@0: EXPECT_EQ("MODULE Linux x86 000000000000000000000000000000000 foo\n" michael@0: "PUBLIC 1000 0 superfunc\n", michael@0: s.str()); michael@0: delete module; michael@0: } michael@0: michael@0: TEST_F(DumpSymbols, SimplePublic64) { michael@0: ELF elf(EM_X86_64, ELFCLASS64, kLittleEndian); michael@0: // Zero out text section for simplicity. michael@0: Section text(kLittleEndian); michael@0: text.Append(4096, 0); michael@0: elf.AddSection(".text", text, SHT_PROGBITS); michael@0: michael@0: // Add a public symbol. michael@0: StringTable table(kLittleEndian); michael@0: SymbolTable syms(kLittleEndian, 8, table); michael@0: syms.AddSymbol("superfunc", (uint64_t)0x1000, (uint64_t)0x10, michael@0: ELF64_ST_INFO(STB_GLOBAL, STT_FUNC), michael@0: SHN_UNDEF + 1); michael@0: int index = elf.AddSection(".dynstr", table, SHT_STRTAB); michael@0: elf.AddSection(".dynsym", syms, michael@0: SHT_DYNSYM, // type michael@0: SHF_ALLOC, // flags michael@0: 0, // addr michael@0: index, // link michael@0: sizeof(Elf64_Sym)); // entsize michael@0: michael@0: elf.Finish(); michael@0: GetElfContents(elf); michael@0: michael@0: Module* module; michael@0: EXPECT_TRUE(ReadSymbolDataInternal(elfdata, michael@0: "foo", michael@0: vector(), michael@0: ALL_SYMBOL_DATA, michael@0: &module)); michael@0: michael@0: stringstream s; michael@0: module->Write(s, ALL_SYMBOL_DATA); michael@0: EXPECT_EQ("MODULE Linux x86_64 000000000000000000000000000000000 foo\n" michael@0: "PUBLIC 1000 0 superfunc\n", michael@0: s.str()); michael@0: }