|
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 <vector> |
|
41 |
|
42 #include "breakpad_googletest_includes.h" |
|
43 #include "common/linux/synth_elf.h" |
|
44 #include "common/module.h" |
|
45 #include "common/using_std_string.h" |
|
46 |
|
47 namespace google_breakpad { |
|
48 bool ReadSymbolDataInternal(const uint8_t* obj_file, |
|
49 const string& obj_filename, |
|
50 const std::vector<string>& debug_dir, |
|
51 SymbolData symbol_data, |
|
52 Module** module); |
|
53 } |
|
54 |
|
55 using google_breakpad::synth_elf::ELF; |
|
56 using google_breakpad::synth_elf::StringTable; |
|
57 using google_breakpad::synth_elf::SymbolTable; |
|
58 using google_breakpad::test_assembler::kLittleEndian; |
|
59 using google_breakpad::test_assembler::Section; |
|
60 using google_breakpad::Module; |
|
61 using google_breakpad::ReadSymbolDataInternal; |
|
62 using std::stringstream; |
|
63 using std::vector; |
|
64 using ::testing::Test; |
|
65 |
|
66 class DumpSymbols : public Test { |
|
67 public: |
|
68 void GetElfContents(ELF& elf) { |
|
69 string contents; |
|
70 ASSERT_TRUE(elf.GetContents(&contents)); |
|
71 ASSERT_LT(0U, contents.size()); |
|
72 |
|
73 elfdata_v.clear(); |
|
74 elfdata_v.insert(elfdata_v.begin(), contents.begin(), contents.end()); |
|
75 elfdata = &elfdata_v[0]; |
|
76 } |
|
77 |
|
78 vector<uint8_t> elfdata_v; |
|
79 uint8_t* elfdata; |
|
80 }; |
|
81 |
|
82 TEST_F(DumpSymbols, Invalid) { |
|
83 Elf32_Ehdr header; |
|
84 memset(&header, 0, sizeof(header)); |
|
85 Module* module; |
|
86 EXPECT_FALSE(ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(&header), |
|
87 "foo", |
|
88 vector<string>(), |
|
89 ALL_SYMBOL_DATA, |
|
90 &module)); |
|
91 } |
|
92 |
|
93 TEST_F(DumpSymbols, SimplePublic32) { |
|
94 ELF elf(EM_386, ELFCLASS32, kLittleEndian); |
|
95 // Zero out text section for simplicity. |
|
96 Section text(kLittleEndian); |
|
97 text.Append(4096, 0); |
|
98 elf.AddSection(".text", text, SHT_PROGBITS); |
|
99 |
|
100 // Add a public symbol. |
|
101 StringTable table(kLittleEndian); |
|
102 SymbolTable syms(kLittleEndian, 4, table); |
|
103 syms.AddSymbol("superfunc", (uint32_t)0x1000, (uint32_t)0x10, |
|
104 ELF32_ST_INFO(STB_GLOBAL, STT_FUNC), |
|
105 SHN_UNDEF + 1); |
|
106 int index = elf.AddSection(".dynstr", table, SHT_STRTAB); |
|
107 elf.AddSection(".dynsym", syms, |
|
108 SHT_DYNSYM, // type |
|
109 SHF_ALLOC, // flags |
|
110 0, // addr |
|
111 index, // link |
|
112 sizeof(Elf32_Sym)); // entsize |
|
113 |
|
114 elf.Finish(); |
|
115 GetElfContents(elf); |
|
116 |
|
117 Module* module; |
|
118 EXPECT_TRUE(ReadSymbolDataInternal(elfdata, |
|
119 "foo", |
|
120 vector<string>(), |
|
121 ALL_SYMBOL_DATA, |
|
122 &module)); |
|
123 |
|
124 stringstream s; |
|
125 module->Write(s, ALL_SYMBOL_DATA); |
|
126 EXPECT_EQ("MODULE Linux x86 000000000000000000000000000000000 foo\n" |
|
127 "PUBLIC 1000 0 superfunc\n", |
|
128 s.str()); |
|
129 delete module; |
|
130 } |
|
131 |
|
132 TEST_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 Module* module; |
|
157 EXPECT_TRUE(ReadSymbolDataInternal(elfdata, |
|
158 "foo", |
|
159 vector<string>(), |
|
160 ALL_SYMBOL_DATA, |
|
161 &module)); |
|
162 |
|
163 stringstream s; |
|
164 module->Write(s, ALL_SYMBOL_DATA); |
|
165 EXPECT_EQ("MODULE Linux x86_64 000000000000000000000000000000000 foo\n" |
|
166 "PUBLIC 1000 0 superfunc\n", |
|
167 s.str()); |
|
168 } |