/thirdparty/breakpad/common/solaris/file_id.cc

http://github.com/tomahawk-player/tomahawk · C++ · 197 lines · 127 code · 32 blank · 38 comment · 32 complexity · fc0be53b1defc8fdf761d031b2c173ee MD5 · raw file

  1. // Copyright (c) 2007, 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. // file_id.cc: Return a unique identifier for a file
  30. //
  31. // See file_id.h for documentation
  32. //
  33. // Author: Alfred Peng
  34. #include <elf.h>
  35. #include <fcntl.h>
  36. #include <gelf.h>
  37. #include <sys/mman.h>
  38. #include <sys/ksyms.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <cassert>
  43. #include <cstdio>
  44. #include "common/md5.h"
  45. #include "common/solaris/file_id.h"
  46. #include "common/solaris/message_output.h"
  47. #include "google_breakpad/common/minidump_format.h"
  48. namespace google_breakpad {
  49. class AutoElfEnder {
  50. public:
  51. AutoElfEnder(Elf *elf) : elf_(elf) {}
  52. ~AutoElfEnder() { if (elf_) elf_end(elf_); }
  53. private:
  54. Elf *elf_;
  55. };
  56. // Find the text section in elf object file.
  57. // Return the section start address and the size.
  58. static bool FindElfTextSection(int fd, const void *elf_base,
  59. const void **text_start,
  60. int *text_size) {
  61. assert(text_start);
  62. assert(text_size);
  63. *text_start = NULL;
  64. *text_size = 0;
  65. if (elf_version(EV_CURRENT) == EV_NONE) {
  66. print_message2(2, "elf_version() failed: %s\n", elf_errmsg(0));
  67. return false;
  68. }
  69. GElf_Ehdr elf_header;
  70. lseek(fd, 0L, 0);
  71. Elf *elf = elf_begin(fd, ELF_C_READ, NULL);
  72. AutoElfEnder elfEnder(elf);
  73. if (gelf_getehdr(elf, &elf_header) == (GElf_Ehdr *)NULL) {
  74. print_message2(2, "failed to read elf header: %s\n", elf_errmsg(-1));
  75. return false;
  76. }
  77. if (elf_header.e_ident[EI_MAG0] != ELFMAG0 ||
  78. elf_header.e_ident[EI_MAG1] != ELFMAG1 ||
  79. elf_header.e_ident[EI_MAG2] != ELFMAG2 ||
  80. elf_header.e_ident[EI_MAG3] != ELFMAG3) {
  81. print_message1(2, "header magic doesn't match\n");
  82. return false;
  83. }
  84. static const char kTextSectionName[] = ".text";
  85. const GElf_Shdr *text_section = NULL;
  86. Elf_Scn *scn = NULL;
  87. GElf_Shdr shdr;
  88. while ((scn = elf_nextscn(elf, scn)) != NULL) {
  89. if (gelf_getshdr(scn, &shdr) == (GElf_Shdr *)0) {
  90. print_message2(2, "failed to read section header: %s\n", elf_errmsg(0));
  91. return false;
  92. }
  93. if (shdr.sh_type == SHT_PROGBITS) {
  94. const char *section_name = elf_strptr(elf, elf_header.e_shstrndx,
  95. shdr.sh_name);
  96. if (!section_name) {
  97. print_message2(2, "Section name error: %s\n", elf_errmsg(-1));
  98. continue;
  99. }
  100. if (strcmp(section_name, kTextSectionName) == 0) {
  101. text_section = &shdr;
  102. break;
  103. }
  104. }
  105. }
  106. if (text_section != NULL && text_section->sh_size > 0) {
  107. *text_start = (char *)elf_base + text_section->sh_offset;
  108. *text_size = text_section->sh_size;
  109. return true;
  110. }
  111. return false;
  112. }
  113. FileID::FileID(const char *path) {
  114. strcpy(path_, path);
  115. }
  116. class AutoCloser {
  117. public:
  118. AutoCloser(int fd) : fd_(fd) {}
  119. ~AutoCloser() { if (fd_) close(fd_); }
  120. private:
  121. int fd_;
  122. };
  123. bool FileID::ElfFileIdentifier(unsigned char identifier[16]) {
  124. int fd = 0;
  125. if ((fd = open(path_, O_RDONLY)) < 0)
  126. return false;
  127. AutoCloser autocloser(fd);
  128. struct stat st;
  129. if (fstat(fd, &st) != 0 || st.st_size <= 0)
  130. return false;
  131. void *base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  132. if (base == MAP_FAILED)
  133. return false;
  134. bool success = false;
  135. const void *text_section = NULL;
  136. int text_size = 0;
  137. if (FindElfTextSection(fd, base, &text_section, &text_size)) {
  138. MD5Context md5;
  139. MD5Init(&md5);
  140. MD5Update(&md5, (const unsigned char *)text_section, text_size);
  141. MD5Final(identifier, &md5);
  142. success = true;
  143. }
  144. munmap((char *)base, st.st_size);
  145. return success;
  146. }
  147. // static
  148. bool FileID::ConvertIdentifierToString(const unsigned char identifier[16],
  149. char *buffer, int buffer_length) {
  150. if (buffer_length < 34)
  151. return false;
  152. int buffer_idx = 0;
  153. for (int idx = 0; idx < 16; ++idx) {
  154. int hi = (identifier[idx] >> 4) & 0x0F;
  155. int lo = (identifier[idx]) & 0x0F;
  156. buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi;
  157. buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo;
  158. }
  159. // Add an extra "0" by the end.
  160. buffer[buffer_idx++] = '0';
  161. // NULL terminate
  162. buffer[buffer_idx] = 0;
  163. return true;
  164. }
  165. } // namespace google_breakpad