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