/thirdparty/breakpad/common/linux/elf_symbols_to_module.cc

http://github.com/tomahawk-player/tomahawk · C++ · 168 lines · 87 code · 22 blank · 59 comment · 14 complexity · 564e4ccc43e830d6a2ee42a69a545847 MD5 · raw file

  1. // -*- mode: c++ -*-
  2. // Copyright (c) 2011 Google Inc. 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. #include "common/linux/elf_symbols_to_module.h"
  31. #include <elf.h>
  32. #include <string.h>
  33. #include "common/byte_cursor.h"
  34. #include "common/module.h"
  35. namespace google_breakpad {
  36. class ELFSymbolIterator {
  37. public:
  38. // The contents of an ELF symbol, adjusted for the host's endianness,
  39. // word size, and so on. Corresponds to the data in Elf32_Sym / Elf64_Sym.
  40. struct Symbol {
  41. // True if this iterator has reached the end of the symbol array. When
  42. // this is set, the other members of this structure are not valid.
  43. bool at_end;
  44. // The number of this symbol within the list.
  45. size_t index;
  46. // The current symbol's name offset. This is the offset within the
  47. // string table.
  48. size_t name_offset;
  49. // The current symbol's value, size, info and shndx fields.
  50. uint64_t value;
  51. uint64_t size;
  52. unsigned char info;
  53. uint16_t shndx;
  54. };
  55. // Create an ELFSymbolIterator walking the symbols in BUFFER. Treat the
  56. // symbols as big-endian if BIG_ENDIAN is true, as little-endian
  57. // otherwise. Assume each symbol has a 'value' field whose size is
  58. // VALUE_SIZE.
  59. //
  60. ELFSymbolIterator(const ByteBuffer *buffer, bool big_endian,
  61. size_t value_size)
  62. : value_size_(value_size), cursor_(buffer, big_endian) {
  63. // Actually, weird sizes could be handled just fine, but they're
  64. // probably mistakes --- expressed in bits, say.
  65. assert(value_size == 4 || value_size == 8);
  66. symbol_.index = 0;
  67. Fetch();
  68. }
  69. // Move to the next symbol. This function's behavior is undefined if
  70. // at_end() is true when it is called.
  71. ELFSymbolIterator &operator++() { Fetch(); symbol_.index++; return *this; }
  72. // Dereferencing this iterator produces a reference to an Symbol structure
  73. // that holds the current symbol's values. The symbol is owned by this
  74. // SymbolIterator, and will be invalidated at the next call to operator++.
  75. const Symbol &operator*() const { return symbol_; }
  76. const Symbol *operator->() const { return &symbol_; }
  77. private:
  78. // Read the symbol at cursor_, and set symbol_ appropriately.
  79. void Fetch() {
  80. // Elf32_Sym and Elf64_Sym have different layouts.
  81. unsigned char other;
  82. if (value_size_ == 4) {
  83. // Elf32_Sym
  84. cursor_
  85. .Read(4, false, &symbol_.name_offset)
  86. .Read(4, false, &symbol_.value)
  87. .Read(4, false, &symbol_.size)
  88. .Read(1, false, &symbol_.info)
  89. .Read(1, false, &other)
  90. .Read(2, false, &symbol_.shndx);
  91. } else {
  92. // Elf64_Sym
  93. cursor_
  94. .Read(4, false, &symbol_.name_offset)
  95. .Read(1, false, &symbol_.info)
  96. .Read(1, false, &other)
  97. .Read(2, false, &symbol_.shndx)
  98. .Read(8, false, &symbol_.value)
  99. .Read(8, false, &symbol_.size);
  100. }
  101. symbol_.at_end = !cursor_;
  102. }
  103. // The size of symbols' value field, in bytes.
  104. size_t value_size_;
  105. // A byte cursor traversing buffer_.
  106. ByteCursor cursor_;
  107. // Values for the symbol this iterator refers to.
  108. Symbol symbol_;
  109. };
  110. const char *SymbolString(ptrdiff_t offset, ByteBuffer& strings) {
  111. if (offset < 0 || (size_t) offset >= strings.Size()) {
  112. // Return the null string.
  113. offset = 0;
  114. }
  115. return reinterpret_cast<const char *>(strings.start + offset);
  116. }
  117. bool ELFSymbolsToModule(const uint8_t *symtab_section,
  118. size_t symtab_size,
  119. const uint8_t *string_section,
  120. size_t string_size,
  121. const bool big_endian,
  122. size_t value_size,
  123. Module *module) {
  124. ByteBuffer symbols(symtab_section, symtab_size);
  125. // Ensure that the string section is null-terminated.
  126. if (string_section[string_size - 1] != '\0') {
  127. const void* null_terminator = memrchr(string_section, '\0', string_size);
  128. string_size = reinterpret_cast<const uint8_t*>(null_terminator)
  129. - string_section;
  130. }
  131. ByteBuffer strings(string_section, string_size);
  132. // The iterator walking the symbol table.
  133. ELFSymbolIterator iterator(&symbols, big_endian, value_size);
  134. while(!iterator->at_end) {
  135. if (ELF32_ST_TYPE(iterator->info) == STT_FUNC &&
  136. iterator->shndx != SHN_UNDEF) {
  137. Module::Extern *ext = new Module::Extern;
  138. ext->name = SymbolString(iterator->name_offset, strings);
  139. ext->address = iterator->value;
  140. module->AddExtern(ext);
  141. }
  142. ++iterator;
  143. }
  144. return true;
  145. }
  146. } // namespace google_breakpad