| OLD | NEW |
| 1 // Copyright (c) 2011 Google Inc. | 1 // Copyright (c) 2011 Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 // Unittests for google_breakpad::DumpSymbols | 33 // Unittests for google_breakpad::DumpSymbols |
| 34 | 34 |
| 35 #include <elf.h> | 35 #include <elf.h> |
| 36 #include <link.h> | 36 #include <link.h> |
| 37 #include <stdio.h> | 37 #include <stdio.h> |
| 38 | 38 |
| 39 #include <sstream> | 39 #include <sstream> |
| 40 #include <vector> | 40 #include <vector> |
| 41 | 41 |
| 42 #include "breakpad_googletest_includes.h" | 42 #include "breakpad_googletest_includes.h" |
| 43 #include "common/linux/elf_gnu_compat.h" |
| 44 #include "common/linux/elfutils.h" |
| 43 #include "common/linux/dump_symbols.h" | 45 #include "common/linux/dump_symbols.h" |
| 44 #include "common/linux/synth_elf.h" | 46 #include "common/linux/synth_elf.h" |
| 45 #include "common/module.h" | 47 #include "common/module.h" |
| 46 #include "common/using_std_string.h" | 48 #include "common/using_std_string.h" |
| 47 | 49 |
| 48 namespace google_breakpad { | 50 namespace google_breakpad { |
| 49 | 51 |
| 50 bool ReadSymbolDataInternal(const uint8_t* obj_file, | 52 bool ReadSymbolDataInternal(const uint8_t* obj_file, |
| 51 const string& obj_filename, | 53 const string& obj_filename, |
| 52 const std::vector<string>& debug_dir, | 54 const std::vector<string>& debug_dir, |
| 53 const DumpOptions& options, | 55 const DumpOptions& options, |
| 54 Module** module); | 56 Module** module); |
| 55 | 57 |
| 56 using google_breakpad::synth_elf::ELF; | 58 using google_breakpad::synth_elf::ELF; |
| 59 using google_breakpad::synth_elf::Notes; |
| 57 using google_breakpad::synth_elf::StringTable; | 60 using google_breakpad::synth_elf::StringTable; |
| 58 using google_breakpad::synth_elf::SymbolTable; | 61 using google_breakpad::synth_elf::SymbolTable; |
| 59 using google_breakpad::test_assembler::kLittleEndian; | 62 using google_breakpad::test_assembler::kLittleEndian; |
| 60 using google_breakpad::test_assembler::Section; | 63 using google_breakpad::test_assembler::Section; |
| 61 using std::stringstream; | 64 using std::stringstream; |
| 62 using std::vector; | 65 using std::vector; |
| 63 using ::testing::Test; | 66 using ::testing::Test; |
| 67 using ::testing::Types; |
| 64 | 68 |
| 69 template<typename ElfClass> |
| 65 class DumpSymbols : public Test { | 70 class DumpSymbols : public Test { |
| 66 public: | 71 public: |
| 67 void GetElfContents(ELF& elf) { | 72 void GetElfContents(ELF& elf) { |
| 68 string contents; | 73 string contents; |
| 69 ASSERT_TRUE(elf.GetContents(&contents)); | 74 ASSERT_TRUE(elf.GetContents(&contents)); |
| 70 ASSERT_LT(0U, contents.size()); | 75 ASSERT_LT(0U, contents.size()); |
| 71 | 76 |
| 72 elfdata_v.clear(); | 77 elfdata_v.clear(); |
| 73 elfdata_v.insert(elfdata_v.begin(), contents.begin(), contents.end()); | 78 elfdata_v.insert(elfdata_v.begin(), contents.begin(), contents.end()); |
| 74 elfdata = &elfdata_v[0]; | 79 elfdata = &elfdata_v[0]; |
| 75 } | 80 } |
| 76 | 81 |
| 77 vector<uint8_t> elfdata_v; | 82 vector<uint8_t> elfdata_v; |
| 78 uint8_t* elfdata; | 83 uint8_t* elfdata; |
| 79 }; | 84 }; |
| 80 | 85 |
| 81 TEST_F(DumpSymbols, Invalid) { | 86 typedef Types<ElfClass32, ElfClass64> ElfClasses; |
| 87 |
| 88 TYPED_TEST_CASE(DumpSymbols, ElfClasses); |
| 89 |
| 90 TYPED_TEST(DumpSymbols, Invalid) { |
| 82 Elf32_Ehdr header; | 91 Elf32_Ehdr header; |
| 83 memset(&header, 0, sizeof(header)); | 92 memset(&header, 0, sizeof(header)); |
| 84 Module* module; | 93 Module* module; |
| 85 DumpOptions options(ALL_SYMBOL_DATA, true); | 94 DumpOptions options(ALL_SYMBOL_DATA, true); |
| 86 EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(&header), | 95 EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(&header), |
| 87 "foo", | 96 "foo", |
| 88 vector<string>(), | 97 vector<string>(), |
| 89 options, | 98 options, |
| 90 &module)); | 99 &module)); |
| 91 } | 100 } |
| 92 | 101 |
| 93 TEST_F(DumpSymbols, SimplePublic32) { | 102 TYPED_TEST(DumpSymbols, SimplePublic) { |
| 94 ELF elf(EM_386, ELFCLASS32, kLittleEndian); | 103 ELF elf(TypeParam::kMachine, TypeParam::kClass, kLittleEndian); |
| 95 // Zero out text section for simplicity. | 104 // Zero out text section for simplicity. |
| 96 Section text(kLittleEndian); | 105 Section text(kLittleEndian); |
| 97 text.Append(4096, 0); | 106 text.Append(4096, 0); |
| 98 elf.AddSection(".text", text, SHT_PROGBITS); | 107 elf.AddSection(".text", text, SHT_PROGBITS); |
| 99 | 108 |
| 100 // Add a public symbol. | 109 // Add a public symbol. |
| 101 StringTable table(kLittleEndian); | 110 StringTable table(kLittleEndian); |
| 102 SymbolTable syms(kLittleEndian, 4, table); | 111 SymbolTable syms(kLittleEndian, TypeParam::kAddrSize, table); |
| 103 syms.AddSymbol("superfunc", (uint32_t)0x1000, (uint32_t)0x10, | 112 syms.AddSymbol("superfunc", |
| 113 (typename TypeParam::Addr)0x1000, |
| 114 (typename TypeParam::Addr)0x10, |
| 115 // ELF32_ST_INFO works for 32-or 64-bit. |
| 104 ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), | 116 ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), |
| 105 SHN_UNDEF + 1); | 117 SHN_UNDEF + 1); |
| 106 int index = elf.AddSection(".dynstr", table, SHT_STRTAB); | 118 int index = elf.AddSection(".dynstr", table, SHT_STRTAB); |
| 107 elf.AddSection(".dynsym", syms, | 119 elf.AddSection(".dynsym", syms, |
| 108 SHT_DYNSYM, // type | 120 SHT_DYNSYM, // type |
| 109 SHF_ALLOC, // flags | 121 SHF_ALLOC, // flags |
| 110 0, // addr | 122 0, // addr |
| 111 index, // link | 123 index, // link |
| 112 sizeof(Elf32_Sym)); // entsize | 124 sizeof(typename TypeParam::Sym)); // entsize |
| 113 | 125 |
| 114 elf.Finish(); | 126 elf.Finish(); |
| 115 GetElfContents(elf); | 127 this->GetElfContents(elf); |
| 116 | 128 |
| 117 Module* module; | 129 Module* module; |
| 118 DumpOptions options(ALL_SYMBOL_DATA, true); | 130 DumpOptions options(ALL_SYMBOL_DATA, true); |
| 119 EXPECT_TRUE(ReadSymbolDataInternal(elfdata, | 131 EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata, |
| 120 "foo", | 132 "foo", |
| 121 vector<string>(), | 133 vector<string>(), |
| 122 options, | 134 options, |
| 123 &module)); | 135 &module)); |
| 124 | 136 |
| 125 stringstream s; | 137 stringstream s; |
| 126 module->Write(s, ALL_SYMBOL_DATA); | 138 module->Write(s, ALL_SYMBOL_DATA); |
| 127 EXPECT_EQ("MODULE Linux x86 000000000000000000000000000000000 foo\n" | 139 const string expected = |
| 128 "PUBLIC 1000 0 superfunc\n", | 140 string("MODULE Linux ") + TypeParam::kMachineName |
| 129 s.str()); | 141 + " 000000000000000000000000000000000 foo\n" |
| 142 "INFO CODE_ID 00000000000000000000000000000000\n" |
| 143 "PUBLIC 1000 0 superfunc\n"; |
| 144 EXPECT_EQ(expected, s.str()); |
| 130 delete module; | 145 delete module; |
| 131 } | 146 } |
| 132 | 147 |
| 133 TEST_F(DumpSymbols, SimplePublic64) { | 148 TYPED_TEST(DumpSymbols, SimpleBuildID) { |
| 134 ELF elf(EM_X86_64, ELFCLASS64, kLittleEndian); | 149 ELF elf(TypeParam::kMachine, TypeParam::kClass, kLittleEndian); |
| 135 // Zero out text section for simplicity. | 150 // Zero out text section for simplicity. |
| 136 Section text(kLittleEndian); | 151 Section text(kLittleEndian); |
| 137 text.Append(4096, 0); | 152 text.Append(4096, 0); |
| 138 elf.AddSection(".text", text, SHT_PROGBITS); | 153 elf.AddSection(".text", text, SHT_PROGBITS); |
| 139 | 154 |
| 155 // Add a Build ID |
| 156 const uint8_t kExpectedIdentifierBytes[] = |
| 157 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 158 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 159 0x10, 0x11, 0x12, 0x13}; |
| 160 Notes notes(kLittleEndian); |
| 161 notes.AddNote(NT_GNU_BUILD_ID, "GNU", kExpectedIdentifierBytes, |
| 162 sizeof(kExpectedIdentifierBytes)); |
| 163 elf.AddSection(".note.gnu.build-id", notes, SHT_NOTE); |
| 164 |
| 140 // Add a public symbol. | 165 // Add a public symbol. |
| 141 StringTable table(kLittleEndian); | 166 StringTable table(kLittleEndian); |
| 142 SymbolTable syms(kLittleEndian, 8, table); | 167 SymbolTable syms(kLittleEndian, TypeParam::kAddrSize, table); |
| 143 syms.AddSymbol("superfunc", (uint64_t)0x1000, (uint64_t)0x10, | 168 syms.AddSymbol("superfunc", |
| 144 ELF64_ST_INFO(STB_GLOBAL, STT_FUNC), | 169 (typename TypeParam::Addr)0x1000, |
| 170 (typename TypeParam::Addr)0x10, |
| 171 // ELF32_ST_INFO works for 32-or 64-bit. |
| 172 ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), |
| 145 SHN_UNDEF + 1); | 173 SHN_UNDEF + 1); |
| 146 int index = elf.AddSection(".dynstr", table, SHT_STRTAB); | 174 int index = elf.AddSection(".dynstr", table, SHT_STRTAB); |
| 147 elf.AddSection(".dynsym", syms, | 175 elf.AddSection(".dynsym", syms, |
| 148 SHT_DYNSYM, // type | 176 SHT_DYNSYM, // type |
| 149 SHF_ALLOC, // flags | 177 SHF_ALLOC, // flags |
| 150 0, // addr | 178 0, // addr |
| 151 index, // link | 179 index, // link |
| 152 sizeof(Elf64_Sym)); // entsize | 180 sizeof(typename TypeParam::Sym)); // entsize |
| 153 | 181 |
| 154 elf.Finish(); | 182 elf.Finish(); |
| 155 GetElfContents(elf); | 183 this->GetElfContents(elf); |
| 156 | 184 |
| 157 Module* module; | 185 Module* module; |
| 158 DumpOptions options(ALL_SYMBOL_DATA, true); | 186 DumpOptions options(ALL_SYMBOL_DATA, true); |
| 159 EXPECT_TRUE(ReadSymbolDataInternal(elfdata, | 187 EXPECT_TRUE(ReadSymbolDataInternal(this->elfdata, |
| 160 "foo", | 188 "foo", |
| 161 vector<string>(), | 189 vector<string>(), |
| 162 options, | 190 options, |
| 163 &module)); | 191 &module)); |
| 164 | 192 |
| 165 stringstream s; | 193 stringstream s; |
| 166 module->Write(s, ALL_SYMBOL_DATA); | 194 module->Write(s, ALL_SYMBOL_DATA); |
| 167 EXPECT_EQ("MODULE Linux x86_64 000000000000000000000000000000000 foo\n" | 195 const string expected = |
| 168 "PUBLIC 1000 0 superfunc\n", | 196 string("MODULE Linux ") + TypeParam::kMachineName |
| 169 s.str()); | 197 + " 030201000504070608090A0B0C0D0E0F0 foo\n" |
| 198 "INFO CODE_ID 000102030405060708090A0B0C0D0E0F10111213\n" |
| 199 "PUBLIC 1000 0 superfunc\n"; |
| 200 EXPECT_EQ(expected, s.str()); |
| 201 delete module; |
| 170 } | 202 } |
| 171 | 203 |
| 172 } // namespace google_breakpad | 204 } // namespace google_breakpad |
| OLD | NEW |