OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006, 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 // file_id.cc: Return a unique identifier for a file | |
31 // | |
32 // See file_id.h for documentation | |
33 // | |
34 | |
35 #include "common/linux/file_id.h" | |
36 #include "common/linux/linux_libc_support.h" | |
37 #include "common/linux/linux_syscall_support.h" | |
38 | |
39 #include <arpa/inet.h> | |
40 #include <elf.h> | |
41 #include <fcntl.h> | |
42 #include <link.h> | |
43 #include <string.h> | |
44 #include <sys/mman.h> | |
45 #include <unistd.h> | |
46 | |
47 #include <algorithm> | |
48 #include <cassert> | |
49 #include <cstdio> | |
50 | |
51 namespace google_breakpad { | |
52 | |
53 FileID::FileID(const char* path) { | |
54 strncpy(path_, path, sizeof(path_)); | |
55 } | |
56 | |
57 // These two functions are also used inside the crashed process, so be safe | |
58 // and use the syscall/libc wrappers instead of direct syscalls or libc. | |
59 static bool FindElfTextSection(const void *elf_mapped_base, | |
60 const void **text_start, | |
61 int *text_size) { | |
62 assert(elf_mapped_base); | |
63 assert(text_start); | |
64 assert(text_size); | |
65 | |
66 const char* elf_base = | |
67 static_cast<const char*>(elf_mapped_base); | |
68 const ElfW(Ehdr)* elf_header = | |
69 reinterpret_cast<const ElfW(Ehdr)*>(elf_base); | |
70 if (my_strncmp(elf_base, ELFMAG, SELFMAG) != 0) | |
71 return false; | |
72 #if __ELF_NATIVE_CLASS == 32 | |
73 #define ELFCLASS ELFCLASS32 | |
74 #else | |
75 #define ELFCLASS ELFCLASS64 | |
76 #endif | |
77 //TODO: support dumping 32-bit binaries from a 64-bit dump_syms? | |
78 if (elf_header->e_ident[EI_CLASS] != ELFCLASS) | |
79 return false; | |
80 *text_start = NULL; | |
81 *text_size = 0; | |
82 const ElfW(Shdr)* sections = | |
83 reinterpret_cast<const ElfW(Shdr)*>(elf_base + elf_header->e_shoff); | |
84 const char* text_section_name = ".text"; | |
85 int name_len = my_strlen(text_section_name); | |
86 const ElfW(Shdr)* string_section = sections + elf_header->e_shstrndx; | |
87 const ElfW(Shdr)* text_section = NULL; | |
88 for (int i = 0; i < elf_header->e_shnum; ++i) { | |
89 if (sections[i].sh_type == SHT_PROGBITS) { | |
90 const char* section_name = (char*)(elf_base + | |
91 string_section->sh_offset + | |
92 sections[i].sh_name); | |
93 if (!my_strncmp(section_name, text_section_name, name_len)) { | |
94 text_section = §ions[i]; | |
95 break; | |
96 } | |
97 } | |
98 } | |
99 if (text_section != NULL && text_section->sh_size > 0) { | |
100 *text_start = elf_base + text_section->sh_offset; | |
101 *text_size = text_section->sh_size; | |
102 } | |
103 return true; | |
104 } | |
105 | |
106 // static | |
107 bool FileID::ElfFileIdentifierFromMappedFile(void* base, | |
108 uint8_t identifier[kMDGUIDSize]) | |
109 { | |
110 const void* text_section = NULL; | |
111 int text_size = 0; | |
112 bool success = false; | |
113 if (FindElfTextSection(base, &text_section, &text_size) && (text_size > 0)) { | |
114 my_memset(identifier, 0, kMDGUIDSize); | |
115 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section); | |
116 const uint8_t* ptr_end = ptr + std::min(text_size, 4096); | |
117 while (ptr < ptr_end) { | |
118 for (unsigned i = 0; i < kMDGUIDSize; i++) | |
119 identifier[i] ^= ptr[i]; | |
120 ptr += kMDGUIDSize; | |
121 } | |
122 success = true; | |
123 } | |
124 return success; | |
125 } | |
126 | |
127 bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) { | |
128 int fd = open(path_, O_RDONLY); | |
129 if (fd < 0) | |
130 return false; | |
131 struct stat st; | |
132 if (fstat(fd, &st) != 0) { | |
133 close(fd); | |
134 return false; | |
135 } | |
136 void* base = mmap(NULL, st.st_size, | |
137 PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
138 close(fd); | |
139 if (base == MAP_FAILED) | |
140 return false; | |
141 | |
142 bool success = ElfFileIdentifierFromMappedFile(base, identifier); | |
143 munmap(base, st.st_size); | |
144 return success; | |
145 } | |
146 | |
147 // static | |
148 void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], | |
149 char* buffer, int buffer_length) { | |
150 uint8_t identifier_swapped[kMDGUIDSize]; | |
151 | |
152 // Endian-ness swap to match dump processor expectation. | |
153 memcpy(identifier_swapped, identifier, kMDGUIDSize); | |
154 uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped); | |
155 *data1 = htonl(*data1); | |
156 uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4); | |
157 *data2 = htons(*data2); | |
158 uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6); | |
159 *data3 = htons(*data3); | |
160 | |
161 int buffer_idx = 0; | |
162 for (unsigned int idx = 0; | |
163 (buffer_idx < buffer_length) && (idx < kMDGUIDSize); | |
164 ++idx) { | |
165 int hi = (identifier_swapped[idx] >> 4) & 0x0F; | |
166 int lo = (identifier_swapped[idx]) & 0x0F; | |
167 | |
168 if (idx == 4 || idx == 6 || idx == 8 || idx == 10) | |
169 buffer[buffer_idx++] = '-'; | |
170 | |
171 buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; | |
172 buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; | |
173 } | |
174 | |
175 // NULL terminate | |
176 buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0; | |
177 } | |
178 | |
179 } // namespace google_breakpad | |
OLD | NEW |