/src/mozilla-central/tools/profiler/lul/LulElf.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | |
4 | | // Copyright (c) 2006, 2011, 2012 Google Inc. |
5 | | // All rights reserved. |
6 | | // |
7 | | // Redistribution and use in source and binary forms, with or without |
8 | | // modification, are permitted provided that the following conditions are |
9 | | // met: |
10 | | // |
11 | | // * Redistributions of source code must retain the above copyright |
12 | | // notice, this list of conditions and the following disclaimer. |
13 | | // * Redistributions in binary form must reproduce the above |
14 | | // copyright notice, this list of conditions and the following disclaimer |
15 | | // in the documentation and/or other materials provided with the |
16 | | // distribution. |
17 | | // * Neither the name of Google Inc. nor the names of its |
18 | | // contributors may be used to endorse or promote products derived from |
19 | | // this software without specific prior written permission. |
20 | | // |
21 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
25 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | |
33 | | // Restructured in 2009 by: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
34 | | |
35 | | // (derived from) |
36 | | // dump_symbols.cc: implement google_breakpad::WriteSymbolFile: |
37 | | // Find all the debugging info in a file and dump it as a Breakpad symbol file. |
38 | | // |
39 | | // dump_symbols.h: Read debugging information from an ELF file, and write |
40 | | // it out as a Breakpad symbol file. |
41 | | |
42 | | // This file is derived from the following files in |
43 | | // toolkit/crashreporter/google-breakpad: |
44 | | // src/common/linux/dump_symbols.cc |
45 | | // src/common/linux/elfutils.cc |
46 | | // src/common/linux/file_id.cc |
47 | | |
48 | | #include <errno.h> |
49 | | #include <fcntl.h> |
50 | | #include <stdio.h> |
51 | | #include <string.h> |
52 | | #include <sys/mman.h> |
53 | | #include <sys/stat.h> |
54 | | #include <unistd.h> |
55 | | #include <arpa/inet.h> |
56 | | |
57 | | #include <set> |
58 | | #include <string> |
59 | | #include <vector> |
60 | | |
61 | | #include "mozilla/Assertions.h" |
62 | | #include "mozilla/Sprintf.h" |
63 | | |
64 | | #include "PlatformMacros.h" |
65 | | #include "LulCommonExt.h" |
66 | | #include "LulDwarfExt.h" |
67 | | #include "LulElfInt.h" |
68 | | #include "LulMainInt.h" |
69 | | |
70 | | |
71 | | #if defined(GP_PLAT_arm_android) && !defined(SHT_ARM_EXIDX) |
72 | | // bionic and older glibsc don't define it |
73 | | # define SHT_ARM_EXIDX (SHT_LOPROC + 1) |
74 | | #endif |
75 | | |
76 | | // Old Linux header doesn't define EM_AARCH64 |
77 | | #ifndef EM_AARCH64 |
78 | | #define EM_AARCH64 183 |
79 | | #endif |
80 | | |
81 | | // This namespace contains helper functions. |
82 | | namespace { |
83 | | |
84 | | using lul::DwarfCFIToModule; |
85 | | using lul::FindElfSectionByName; |
86 | | using lul::GetOffset; |
87 | | using lul::IsValidElf; |
88 | | using lul::Module; |
89 | | using lul::UniqueStringUniverse; |
90 | | using lul::scoped_ptr; |
91 | | using lul::Summariser; |
92 | | using std::string; |
93 | | using std::vector; |
94 | | using std::set; |
95 | | |
96 | | // |
97 | | // FDWrapper |
98 | | // |
99 | | // Wrapper class to make sure opened file is closed. |
100 | | // |
101 | | class FDWrapper { |
102 | | public: |
103 | | explicit FDWrapper(int fd) : |
104 | 0 | fd_(fd) {} |
105 | 0 | ~FDWrapper() { |
106 | 0 | if (fd_ != -1) |
107 | 0 | close(fd_); |
108 | 0 | } |
109 | 0 | int get() { |
110 | 0 | return fd_; |
111 | 0 | } |
112 | 0 | int release() { |
113 | 0 | int fd = fd_; |
114 | 0 | fd_ = -1; |
115 | 0 | return fd; |
116 | 0 | } |
117 | | private: |
118 | | int fd_; |
119 | | }; |
120 | | |
121 | | // |
122 | | // MmapWrapper |
123 | | // |
124 | | // Wrapper class to make sure mapped regions are unmapped. |
125 | | // |
126 | | class MmapWrapper { |
127 | | public: |
128 | 0 | MmapWrapper() : is_set_(false), base_(NULL), size_(0){} |
129 | 0 | ~MmapWrapper() { |
130 | 0 | if (is_set_ && base_ != NULL) { |
131 | 0 | MOZ_ASSERT(size_ > 0); |
132 | 0 | munmap(base_, size_); |
133 | 0 | } |
134 | 0 | } |
135 | 0 | void set(void *mapped_address, size_t mapped_size) { |
136 | 0 | is_set_ = true; |
137 | 0 | base_ = mapped_address; |
138 | 0 | size_ = mapped_size; |
139 | 0 | } |
140 | 0 | void release() { |
141 | 0 | MOZ_ASSERT(is_set_); |
142 | 0 | is_set_ = false; |
143 | 0 | base_ = NULL; |
144 | 0 | size_ = 0; |
145 | 0 | } |
146 | | |
147 | | private: |
148 | | bool is_set_; |
149 | | void *base_; |
150 | | size_t size_; |
151 | | }; |
152 | | |
153 | | |
154 | | // Set NUM_DW_REGNAMES to be the number of Dwarf register names |
155 | | // appropriate to the machine architecture given in HEADER. Return |
156 | | // true on success, or false if HEADER's machine architecture is not |
157 | | // supported. |
158 | | template<typename ElfClass> |
159 | | bool DwarfCFIRegisterNames(const typename ElfClass::Ehdr* elf_header, |
160 | 0 | unsigned int* num_dw_regnames) { |
161 | 0 | switch (elf_header->e_machine) { |
162 | 0 | case EM_386: |
163 | 0 | *num_dw_regnames = DwarfCFIToModule::RegisterNames::I386(); |
164 | 0 | return true; |
165 | 0 | case EM_ARM: |
166 | 0 | *num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM(); |
167 | 0 | return true; |
168 | 0 | case EM_X86_64: |
169 | 0 | *num_dw_regnames = DwarfCFIToModule::RegisterNames::X86_64(); |
170 | 0 | return true; |
171 | 0 | case EM_MIPS: |
172 | 0 | *num_dw_regnames = DwarfCFIToModule::RegisterNames::MIPS(); |
173 | 0 | return true; |
174 | 0 | case EM_AARCH64: |
175 | 0 | *num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM64(); |
176 | 0 | return true; |
177 | 0 | default: |
178 | 0 | MOZ_ASSERT(0); |
179 | 0 | return false; |
180 | 0 | } |
181 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::DwarfCFIRegisterNames<lul::ElfClass32>(lul::ElfClass32::Ehdr const*, unsigned int*) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::DwarfCFIRegisterNames<lul::ElfClass64>(lul::ElfClass64::Ehdr const*, unsigned int*) |
182 | | |
183 | | template<typename ElfClass> |
184 | | bool LoadDwarfCFI(const string& dwarf_filename, |
185 | | const typename ElfClass::Ehdr* elf_header, |
186 | | const char* section_name, |
187 | | const typename ElfClass::Shdr* section, |
188 | | const bool eh_frame, |
189 | | const typename ElfClass::Shdr* got_section, |
190 | | const typename ElfClass::Shdr* text_section, |
191 | | const bool big_endian, |
192 | | SecMap* smap, |
193 | | uintptr_t text_bias, |
194 | | UniqueStringUniverse* usu, |
195 | 0 | void (*log)(const char*)) { |
196 | 0 | // Find the appropriate set of register names for this file's |
197 | 0 | // architecture. |
198 | 0 | unsigned int num_dw_regs = 0; |
199 | 0 | if (!DwarfCFIRegisterNames<ElfClass>(elf_header, &num_dw_regs)) { |
200 | 0 | fprintf(stderr, "%s: unrecognized ELF machine architecture '%d';" |
201 | 0 | " cannot convert DWARF call frame information\n", |
202 | 0 | dwarf_filename.c_str(), elf_header->e_machine); |
203 | 0 | return false; |
204 | 0 | } |
205 | 0 | |
206 | 0 | const lul::Endianness endianness |
207 | 0 | = big_endian ? lul::ENDIANNESS_BIG : lul::ENDIANNESS_LITTLE; |
208 | 0 |
|
209 | 0 | // Find the call frame information and its size. |
210 | 0 | const char* cfi = |
211 | 0 | GetOffset<ElfClass, char>(elf_header, section->sh_offset); |
212 | 0 | size_t cfi_size = section->sh_size; |
213 | 0 |
|
214 | 0 | // Plug together the parser, handler, and their entourages. |
215 | 0 |
|
216 | 0 | // Here's a summariser, which will receive the output of the |
217 | 0 | // parser, create summaries, and add them to |smap|. |
218 | 0 | Summariser summ(smap, text_bias, log); |
219 | 0 |
|
220 | 0 | lul::ByteReader reader(endianness); |
221 | 0 | reader.SetAddressSize(ElfClass::kAddrSize); |
222 | 0 |
|
223 | 0 | DwarfCFIToModule::Reporter module_reporter(log, dwarf_filename, section_name); |
224 | 0 | DwarfCFIToModule handler(num_dw_regs, &module_reporter, &reader, usu, &summ); |
225 | 0 |
|
226 | 0 | // Provide the base addresses for .eh_frame encoded pointers, if |
227 | 0 | // possible. |
228 | 0 | reader.SetCFIDataBase(section->sh_addr, cfi); |
229 | 0 | if (got_section) |
230 | 0 | reader.SetDataBase(got_section->sh_addr); |
231 | 0 | if (text_section) |
232 | 0 | reader.SetTextBase(text_section->sh_addr); |
233 | 0 |
|
234 | 0 | lul::CallFrameInfo::Reporter dwarf_reporter(log, dwarf_filename, |
235 | 0 | section_name); |
236 | 0 | lul::CallFrameInfo parser(cfi, cfi_size, |
237 | 0 | &reader, &handler, &dwarf_reporter, |
238 | 0 | eh_frame); |
239 | 0 | parser.Start(); |
240 | 0 |
|
241 | 0 | return true; |
242 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::LoadDwarfCFI<lul::ElfClass32>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, lul::ElfClass32::Ehdr const*, char const*, lul::ElfClass32::Shdr const*, bool, lul::ElfClass32::Shdr const*, lul::ElfClass32::Shdr const*, bool, lul::SecMap*, unsigned long, lul::UniqueStringUniverse*, void (*)(char const*)) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::LoadDwarfCFI<lul::ElfClass64>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, lul::ElfClass64::Ehdr const*, char const*, lul::ElfClass64::Shdr const*, bool, lul::ElfClass64::Shdr const*, lul::ElfClass64::Shdr const*, bool, lul::SecMap*, unsigned long, lul::UniqueStringUniverse*, void (*)(char const*)) |
243 | | |
244 | | bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper, |
245 | 0 | void** elf_header) { |
246 | 0 | int obj_fd = open(obj_file.c_str(), O_RDONLY); |
247 | 0 | if (obj_fd < 0) { |
248 | 0 | fprintf(stderr, "Failed to open ELF file '%s': %s\n", |
249 | 0 | obj_file.c_str(), strerror(errno)); |
250 | 0 | return false; |
251 | 0 | } |
252 | 0 | FDWrapper obj_fd_wrapper(obj_fd); |
253 | 0 | struct stat st; |
254 | 0 | if (fstat(obj_fd, &st) != 0 && st.st_size <= 0) { |
255 | 0 | fprintf(stderr, "Unable to fstat ELF file '%s': %s\n", |
256 | 0 | obj_file.c_str(), strerror(errno)); |
257 | 0 | return false; |
258 | 0 | } |
259 | 0 | // Mapping it read-only is good enough. In any case, mapping it |
260 | 0 | // read-write confuses Valgrind's debuginfo acquire/discard |
261 | 0 | // heuristics, making it hard to profile the profiler. |
262 | 0 | void *obj_base = mmap(nullptr, st.st_size, |
263 | 0 | PROT_READ, MAP_PRIVATE, obj_fd, 0); |
264 | 0 | if (obj_base == MAP_FAILED) { |
265 | 0 | fprintf(stderr, "Failed to mmap ELF file '%s': %s\n", |
266 | 0 | obj_file.c_str(), strerror(errno)); |
267 | 0 | return false; |
268 | 0 | } |
269 | 0 | map_wrapper->set(obj_base, st.st_size); |
270 | 0 | *elf_header = obj_base; |
271 | 0 | if (!IsValidElf(*elf_header)) { |
272 | 0 | fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str()); |
273 | 0 | return false; |
274 | 0 | } |
275 | 0 | return true; |
276 | 0 | } |
277 | | |
278 | | // Get the endianness of ELF_HEADER. If it's invalid, return false. |
279 | | template<typename ElfClass> |
280 | | bool ElfEndianness(const typename ElfClass::Ehdr* elf_header, |
281 | 0 | bool* big_endian) { |
282 | 0 | if (elf_header->e_ident[EI_DATA] == ELFDATA2LSB) { |
283 | 0 | *big_endian = false; |
284 | 0 | return true; |
285 | 0 | } |
286 | 0 | if (elf_header->e_ident[EI_DATA] == ELFDATA2MSB) { |
287 | 0 | *big_endian = true; |
288 | 0 | return true; |
289 | 0 | } |
290 | 0 | |
291 | 0 | fprintf(stderr, "bad data encoding in ELF header: %d\n", |
292 | 0 | elf_header->e_ident[EI_DATA]); |
293 | 0 | return false; |
294 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::ElfEndianness<lul::ElfClass32>(lul::ElfClass32::Ehdr const*, bool*) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::ElfEndianness<lul::ElfClass64>(lul::ElfClass64::Ehdr const*, bool*) |
295 | | |
296 | | // |
297 | | // LoadSymbolsInfo |
298 | | // |
299 | | // Holds the state between the two calls to LoadSymbols() in case it's necessary |
300 | | // to follow the .gnu_debuglink section and load debug information from a |
301 | | // different file. |
302 | | // |
303 | | template<typename ElfClass> |
304 | | class LoadSymbolsInfo { |
305 | | public: |
306 | | typedef typename ElfClass::Addr Addr; |
307 | | |
308 | | explicit LoadSymbolsInfo(const vector<string>& dbg_dirs) : |
309 | | debug_dirs_(dbg_dirs), |
310 | 0 | has_loading_addr_(false) {} Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:(anonymous namespace)::LoadSymbolsInfo<lul::ElfClass32>::LoadSymbolsInfo(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:(anonymous namespace)::LoadSymbolsInfo<lul::ElfClass64>::LoadSymbolsInfo(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) |
311 | | |
312 | | // Keeps track of which sections have been loaded so sections don't |
313 | | // accidentally get loaded twice from two different files. |
314 | 0 | void LoadedSection(const string §ion) { |
315 | 0 | if (loaded_sections_.count(section) == 0) { |
316 | 0 | loaded_sections_.insert(section); |
317 | 0 | } else { |
318 | 0 | fprintf(stderr, "Section %s has already been loaded.\n", |
319 | 0 | section.c_str()); |
320 | 0 | } |
321 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:(anonymous namespace)::LoadSymbolsInfo<lul::ElfClass32>::LoadedSection(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:(anonymous namespace)::LoadSymbolsInfo<lul::ElfClass64>::LoadedSection(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) |
322 | | |
323 | 0 | string debuglink_file() const { |
324 | 0 | return debuglink_file_; |
325 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:(anonymous namespace)::LoadSymbolsInfo<lul::ElfClass32>::debuglink_file() const Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:(anonymous namespace)::LoadSymbolsInfo<lul::ElfClass64>::debuglink_file() const |
326 | | |
327 | | private: |
328 | | const vector<string>& debug_dirs_; // Directories in which to |
329 | | // search for the debug ELF file. |
330 | | |
331 | | string debuglink_file_; // Full path to the debug ELF file. |
332 | | |
333 | | bool has_loading_addr_; // Indicate if LOADING_ADDR_ is valid. |
334 | | |
335 | | set<string> loaded_sections_; // Tracks the Loaded ELF sections |
336 | | // between calls to LoadSymbols(). |
337 | | }; |
338 | | |
339 | | // Find the preferred loading address of the binary. |
340 | | template<typename ElfClass> |
341 | | typename ElfClass::Addr GetLoadingAddress( |
342 | | const typename ElfClass::Phdr* program_headers, |
343 | 0 | int nheader) { |
344 | 0 | typedef typename ElfClass::Phdr Phdr; |
345 | 0 |
|
346 | 0 | // For non-PIC executables (e_type == ET_EXEC), the load address is |
347 | 0 | // the start address of the first PT_LOAD segment. (ELF requires |
348 | 0 | // the segments to be sorted by load address.) For PIC executables |
349 | 0 | // and dynamic libraries (e_type == ET_DYN), this address will |
350 | 0 | // normally be zero. |
351 | 0 | for (int i = 0; i < nheader; ++i) { |
352 | 0 | const Phdr& header = program_headers[i]; |
353 | 0 | if (header.p_type == PT_LOAD) |
354 | 0 | return header.p_vaddr; |
355 | 0 | } |
356 | 0 | return 0; |
357 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:lul::ElfClass32::Addr (anonymous namespace)::GetLoadingAddress<lul::ElfClass32>(lul::ElfClass32::Phdr const*, int) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:lul::ElfClass64::Addr (anonymous namespace)::GetLoadingAddress<lul::ElfClass64>(lul::ElfClass64::Phdr const*, int) |
358 | | |
359 | | template<typename ElfClass> |
360 | | bool LoadSymbols(const string& obj_file, |
361 | | const bool big_endian, |
362 | | const typename ElfClass::Ehdr* elf_header, |
363 | | const bool read_gnu_debug_link, |
364 | | LoadSymbolsInfo<ElfClass>* info, |
365 | | SecMap* smap, |
366 | | void* rx_avma, size_t rx_size, |
367 | | UniqueStringUniverse* usu, |
368 | 0 | void (*log)(const char*)) { |
369 | 0 | typedef typename ElfClass::Phdr Phdr; |
370 | 0 | typedef typename ElfClass::Shdr Shdr; |
371 | 0 |
|
372 | 0 | char buf[500]; |
373 | 0 | SprintfLiteral(buf, "LoadSymbols: BEGIN %s\n", obj_file.c_str()); |
374 | 0 | buf[sizeof(buf)-1] = 0; |
375 | 0 | log(buf); |
376 | 0 |
|
377 | 0 | // This is how the text bias is calculated. |
378 | 0 | // BEGIN CALCULATE BIAS |
379 | 0 | uintptr_t loading_addr = GetLoadingAddress<ElfClass>( |
380 | 0 | GetOffset<ElfClass, Phdr>(elf_header, elf_header->e_phoff), |
381 | 0 | elf_header->e_phnum); |
382 | 0 | uintptr_t text_bias = ((uintptr_t)rx_avma) - loading_addr; |
383 | 0 | SprintfLiteral(buf, |
384 | 0 | "LoadSymbols: rx_avma=%llx, text_bias=%llx", |
385 | 0 | (unsigned long long int)(uintptr_t)rx_avma, |
386 | 0 | (unsigned long long int)text_bias); |
387 | 0 | buf[sizeof(buf)-1] = 0; |
388 | 0 | log(buf); |
389 | 0 | // END CALCULATE BIAS |
390 | 0 |
|
391 | 0 | const Shdr* sections = |
392 | 0 | GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff); |
393 | 0 | const Shdr* section_names = sections + elf_header->e_shstrndx; |
394 | 0 | const char* names = |
395 | 0 | GetOffset<ElfClass, char>(elf_header, section_names->sh_offset); |
396 | 0 | const char *names_end = names + section_names->sh_size; |
397 | 0 | bool found_usable_info = false; |
398 | 0 |
|
399 | 0 | // Dwarf Call Frame Information (CFI) is actually independent from |
400 | 0 | // the other DWARF debugging information, and can be used alone. |
401 | 0 | const Shdr* dwarf_cfi_section = |
402 | 0 | FindElfSectionByName<ElfClass>(".debug_frame", SHT_PROGBITS, |
403 | 0 | sections, names, names_end, |
404 | 0 | elf_header->e_shnum); |
405 | 0 | if (dwarf_cfi_section) { |
406 | 0 | // Ignore the return value of this function; even without call frame |
407 | 0 | // information, the other debugging information could be perfectly |
408 | 0 | // useful. |
409 | 0 | info->LoadedSection(".debug_frame"); |
410 | 0 | bool result = |
411 | 0 | LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".debug_frame", |
412 | 0 | dwarf_cfi_section, false, 0, 0, big_endian, |
413 | 0 | smap, text_bias, usu, log); |
414 | 0 | found_usable_info = found_usable_info || result; |
415 | 0 | if (result) |
416 | 0 | log("LoadSymbols: read CFI from .debug_frame"); |
417 | 0 | } |
418 | 0 |
|
419 | 0 | // Linux C++ exception handling information can also provide |
420 | 0 | // unwinding data. |
421 | 0 | const Shdr* eh_frame_section = |
422 | 0 | FindElfSectionByName<ElfClass>(".eh_frame", SHT_PROGBITS, |
423 | 0 | sections, names, names_end, |
424 | 0 | elf_header->e_shnum); |
425 | 0 | if (eh_frame_section) { |
426 | 0 | // Pointers in .eh_frame data may be relative to the base addresses of |
427 | 0 | // certain sections. Provide those sections if present. |
428 | 0 | const Shdr* got_section = |
429 | 0 | FindElfSectionByName<ElfClass>(".got", SHT_PROGBITS, |
430 | 0 | sections, names, names_end, |
431 | 0 | elf_header->e_shnum); |
432 | 0 | const Shdr* text_section = |
433 | 0 | FindElfSectionByName<ElfClass>(".text", SHT_PROGBITS, |
434 | 0 | sections, names, names_end, |
435 | 0 | elf_header->e_shnum); |
436 | 0 | info->LoadedSection(".eh_frame"); |
437 | 0 | // As above, ignore the return value of this function. |
438 | 0 | bool result = |
439 | 0 | LoadDwarfCFI<ElfClass>(obj_file, elf_header, ".eh_frame", |
440 | 0 | eh_frame_section, true, |
441 | 0 | got_section, text_section, big_endian, |
442 | 0 | smap, text_bias, usu, log); |
443 | 0 | found_usable_info = found_usable_info || result; |
444 | 0 | if (result) |
445 | 0 | log("LoadSymbols: read CFI from .eh_frame"); |
446 | 0 | } |
447 | 0 |
|
448 | 0 | SprintfLiteral(buf, "LoadSymbols: END %s\n", obj_file.c_str()); |
449 | 0 | buf[sizeof(buf)-1] = 0; |
450 | 0 | log(buf); |
451 | 0 |
|
452 | 0 | return found_usable_info; |
453 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::LoadSymbols<lul::ElfClass32>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool, lul::ElfClass32::Ehdr const*, bool, (anonymous namespace)::LoadSymbolsInfo<lul::ElfClass32>*, lul::SecMap*, void*, unsigned long, lul::UniqueStringUniverse*, void (*)(char const*)) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::LoadSymbols<lul::ElfClass64>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool, lul::ElfClass64::Ehdr const*, bool, (anonymous namespace)::LoadSymbolsInfo<lul::ElfClass64>*, lul::SecMap*, void*, unsigned long, lul::UniqueStringUniverse*, void (*)(char const*)) |
454 | | |
455 | | // Return the breakpad symbol file identifier for the architecture of |
456 | | // ELF_HEADER. |
457 | | template<typename ElfClass> |
458 | | const char* ElfArchitecture(const typename ElfClass::Ehdr* elf_header) { |
459 | | typedef typename ElfClass::Half Half; |
460 | | Half arch = elf_header->e_machine; |
461 | | switch (arch) { |
462 | | case EM_386: return "x86"; |
463 | | case EM_ARM: return "arm"; |
464 | | case EM_AARCH64: return "arm64"; |
465 | | case EM_MIPS: return "mips"; |
466 | | case EM_PPC64: return "ppc64"; |
467 | | case EM_PPC: return "ppc"; |
468 | | case EM_S390: return "s390"; |
469 | | case EM_SPARC: return "sparc"; |
470 | | case EM_SPARCV9: return "sparcv9"; |
471 | | case EM_X86_64: return "x86_64"; |
472 | | default: return NULL; |
473 | | } |
474 | | } |
475 | | |
476 | | // Format the Elf file identifier in IDENTIFIER as a UUID with the |
477 | | // dashes removed. |
478 | 0 | string FormatIdentifier(unsigned char identifier[16]) { |
479 | 0 | char identifier_str[40]; |
480 | 0 | lul::FileID::ConvertIdentifierToString( |
481 | 0 | identifier, |
482 | 0 | identifier_str, |
483 | 0 | sizeof(identifier_str)); |
484 | 0 | string id_no_dash; |
485 | 0 | for (int i = 0; identifier_str[i] != '\0'; ++i) |
486 | 0 | if (identifier_str[i] != '-') |
487 | 0 | id_no_dash += identifier_str[i]; |
488 | 0 | // Add an extra "0" by the end. PDB files on Windows have an 'age' |
489 | 0 | // number appended to the end of the file identifier; this isn't |
490 | 0 | // really used or necessary on other platforms, but be consistent. |
491 | 0 | id_no_dash += '0'; |
492 | 0 | return id_no_dash; |
493 | 0 | } |
494 | | |
495 | | // Return the non-directory portion of FILENAME: the portion after the |
496 | | // last slash, or the whole filename if there are no slashes. |
497 | 0 | string BaseFileName(const string &filename) { |
498 | 0 | // Lots of copies! basename's behavior is less than ideal. |
499 | 0 | char *c_filename = strdup(filename.c_str()); |
500 | 0 | string base = basename(c_filename); |
501 | 0 | free(c_filename); |
502 | 0 | return base; |
503 | 0 | } |
504 | | |
505 | | template<typename ElfClass> |
506 | | bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header, |
507 | | const string& obj_filename, |
508 | | const vector<string>& debug_dirs, |
509 | | SecMap* smap, void* rx_avma, size_t rx_size, |
510 | | UniqueStringUniverse* usu, |
511 | 0 | void (*log)(const char*)) { |
512 | 0 | typedef typename ElfClass::Ehdr Ehdr; |
513 | 0 |
|
514 | 0 | unsigned char identifier[16]; |
515 | 0 | if (!lul |
516 | 0 | ::FileID::ElfFileIdentifierFromMappedFile(elf_header, identifier)) { |
517 | 0 | fprintf(stderr, "%s: unable to generate file identifier\n", |
518 | 0 | obj_filename.c_str()); |
519 | 0 | return false; |
520 | 0 | } |
521 | 0 | |
522 | 0 | const char *architecture = ElfArchitecture<ElfClass>(elf_header); |
523 | 0 | if (!architecture) { |
524 | 0 | fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n", |
525 | 0 | obj_filename.c_str(), elf_header->e_machine); |
526 | 0 | return false; |
527 | 0 | } |
528 | 0 | |
529 | 0 | // Figure out what endianness this file is. |
530 | 0 | bool big_endian; |
531 | 0 | if (!ElfEndianness<ElfClass>(elf_header, &big_endian)) |
532 | 0 | return false; |
533 | 0 | |
534 | 0 | string name = BaseFileName(obj_filename); |
535 | 0 | string os = "Linux"; |
536 | 0 | string id = FormatIdentifier(identifier); |
537 | 0 |
|
538 | 0 | LoadSymbolsInfo<ElfClass> info(debug_dirs); |
539 | 0 | if (!LoadSymbols<ElfClass>(obj_filename, big_endian, elf_header, |
540 | 0 | !debug_dirs.empty(), &info, |
541 | 0 | smap, rx_avma, rx_size, usu, log)) { |
542 | 0 | const string debuglink_file = info.debuglink_file(); |
543 | 0 | if (debuglink_file.empty()) |
544 | 0 | return false; |
545 | 0 | |
546 | 0 | // Load debuglink ELF file. |
547 | 0 | fprintf(stderr, "Found debugging info in %s\n", debuglink_file.c_str()); |
548 | 0 | MmapWrapper debug_map_wrapper; |
549 | 0 | Ehdr* debug_elf_header = NULL; |
550 | 0 | if (!LoadELF(debuglink_file, &debug_map_wrapper, |
551 | 0 | reinterpret_cast<void**>(&debug_elf_header))) |
552 | 0 | return false; |
553 | 0 | // Sanity checks to make sure everything matches up. |
554 | 0 | const char *debug_architecture = |
555 | 0 | ElfArchitecture<ElfClass>(debug_elf_header); |
556 | 0 | if (!debug_architecture) { |
557 | 0 | fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n", |
558 | 0 | debuglink_file.c_str(), debug_elf_header->e_machine); |
559 | 0 | return false; |
560 | 0 | } |
561 | 0 | if (strcmp(architecture, debug_architecture)) { |
562 | 0 | fprintf(stderr, "%s with ELF machine architecture %s does not match " |
563 | 0 | "%s with ELF architecture %s\n", |
564 | 0 | debuglink_file.c_str(), debug_architecture, |
565 | 0 | obj_filename.c_str(), architecture); |
566 | 0 | return false; |
567 | 0 | } |
568 | 0 | |
569 | 0 | bool debug_big_endian; |
570 | 0 | if (!ElfEndianness<ElfClass>(debug_elf_header, &debug_big_endian)) |
571 | 0 | return false; |
572 | 0 | if (debug_big_endian != big_endian) { |
573 | 0 | fprintf(stderr, "%s and %s does not match in endianness\n", |
574 | 0 | obj_filename.c_str(), debuglink_file.c_str()); |
575 | 0 | return false; |
576 | 0 | } |
577 | 0 | |
578 | 0 | if (!LoadSymbols<ElfClass>(debuglink_file, debug_big_endian, |
579 | 0 | debug_elf_header, false, &info, |
580 | 0 | smap, rx_avma, rx_size, usu, log)) { |
581 | 0 | return false; |
582 | 0 | } |
583 | 0 | } |
584 | 0 | |
585 | 0 | return true; |
586 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::ReadSymbolDataElfClass<lul::ElfClass32>(lul::ElfClass32::Ehdr const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, lul::SecMap*, void*, unsigned long, lul::UniqueStringUniverse*, void (*)(char const*)) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool (anonymous namespace)::ReadSymbolDataElfClass<lul::ElfClass64>(lul::ElfClass64::Ehdr const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, lul::SecMap*, void*, unsigned long, lul::UniqueStringUniverse*, void (*)(char const*)) |
587 | | |
588 | | } // namespace (anon) |
589 | | |
590 | | |
591 | | namespace lul { |
592 | | |
593 | | bool ReadSymbolDataInternal(const uint8_t* obj_file, |
594 | | const string& obj_filename, |
595 | | const vector<string>& debug_dirs, |
596 | | SecMap* smap, void* rx_avma, size_t rx_size, |
597 | | UniqueStringUniverse* usu, |
598 | 0 | void (*log)(const char*)) { |
599 | 0 |
|
600 | 0 | if (!IsValidElf(obj_file)) { |
601 | 0 | fprintf(stderr, "Not a valid ELF file: %s\n", obj_filename.c_str()); |
602 | 0 | return false; |
603 | 0 | } |
604 | 0 | |
605 | 0 | int elfclass = ElfClass(obj_file); |
606 | 0 | if (elfclass == ELFCLASS32) { |
607 | 0 | return ReadSymbolDataElfClass<ElfClass32>( |
608 | 0 | reinterpret_cast<const Elf32_Ehdr*>(obj_file), |
609 | 0 | obj_filename, debug_dirs, smap, rx_avma, rx_size, usu, log); |
610 | 0 | } |
611 | 0 | if (elfclass == ELFCLASS64) { |
612 | 0 | return ReadSymbolDataElfClass<ElfClass64>( |
613 | 0 | reinterpret_cast<const Elf64_Ehdr*>(obj_file), |
614 | 0 | obj_filename, debug_dirs, smap, rx_avma, rx_size, usu, log); |
615 | 0 | } |
616 | 0 | |
617 | 0 | return false; |
618 | 0 | } |
619 | | |
620 | | bool ReadSymbolData(const string& obj_file, |
621 | | const vector<string>& debug_dirs, |
622 | | SecMap* smap, void* rx_avma, size_t rx_size, |
623 | | UniqueStringUniverse* usu, |
624 | 0 | void (*log)(const char*)) { |
625 | 0 | MmapWrapper map_wrapper; |
626 | 0 | void* elf_header = NULL; |
627 | 0 | if (!LoadELF(obj_file, &map_wrapper, &elf_header)) |
628 | 0 | return false; |
629 | 0 | |
630 | 0 | return ReadSymbolDataInternal(reinterpret_cast<uint8_t*>(elf_header), |
631 | 0 | obj_file, debug_dirs, |
632 | 0 | smap, rx_avma, rx_size, usu, log); |
633 | 0 | } |
634 | | |
635 | | |
636 | | namespace { |
637 | | |
638 | | template<typename ElfClass> |
639 | | void FindElfClassSection(const char *elf_base, |
640 | | const char *section_name, |
641 | | typename ElfClass::Word section_type, |
642 | | const void **section_start, |
643 | 0 | int *section_size) { |
644 | 0 | typedef typename ElfClass::Ehdr Ehdr; |
645 | 0 | typedef typename ElfClass::Shdr Shdr; |
646 | 0 |
|
647 | 0 | MOZ_ASSERT(elf_base); |
648 | 0 | MOZ_ASSERT(section_start); |
649 | 0 | MOZ_ASSERT(section_size); |
650 | 0 |
|
651 | 0 | MOZ_ASSERT(strncmp(elf_base, ELFMAG, SELFMAG) == 0); |
652 | 0 |
|
653 | 0 | const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); |
654 | 0 | MOZ_ASSERT(elf_header->e_ident[EI_CLASS] == ElfClass::kClass); |
655 | 0 |
|
656 | 0 | const Shdr* sections = |
657 | 0 | GetOffset<ElfClass,Shdr>(elf_header, elf_header->e_shoff); |
658 | 0 | const Shdr* section_names = sections + elf_header->e_shstrndx; |
659 | 0 | const char* names = |
660 | 0 | GetOffset<ElfClass,char>(elf_header, section_names->sh_offset); |
661 | 0 | const char *names_end = names + section_names->sh_size; |
662 | 0 |
|
663 | 0 | const Shdr* section = |
664 | 0 | FindElfSectionByName<ElfClass>(section_name, section_type, |
665 | 0 | sections, names, names_end, |
666 | 0 | elf_header->e_shnum); |
667 | 0 |
|
668 | 0 | if (section != NULL && section->sh_size > 0) { |
669 | 0 | *section_start = elf_base + section->sh_offset; |
670 | 0 | *section_size = section->sh_size; |
671 | 0 | } |
672 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:void lul::(anonymous namespace)::FindElfClassSection<lul::ElfClass32>(char const*, char const*, lul::ElfClass32::Word, void const**, int*) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:void lul::(anonymous namespace)::FindElfClassSection<lul::ElfClass64>(char const*, char const*, lul::ElfClass64::Word, void const**, int*) |
673 | | |
674 | | template<typename ElfClass> |
675 | | void FindElfClassSegment(const char *elf_base, |
676 | | typename ElfClass::Word segment_type, |
677 | | const void **segment_start, |
678 | 0 | int *segment_size) { |
679 | 0 | typedef typename ElfClass::Ehdr Ehdr; |
680 | 0 | typedef typename ElfClass::Phdr Phdr; |
681 | 0 |
|
682 | 0 | MOZ_ASSERT(elf_base); |
683 | 0 | MOZ_ASSERT(segment_start); |
684 | 0 | MOZ_ASSERT(segment_size); |
685 | 0 |
|
686 | 0 | MOZ_ASSERT(strncmp(elf_base, ELFMAG, SELFMAG) == 0); |
687 | 0 |
|
688 | 0 | const Ehdr* elf_header = reinterpret_cast<const Ehdr*>(elf_base); |
689 | 0 | MOZ_ASSERT(elf_header->e_ident[EI_CLASS] == ElfClass::kClass); |
690 | 0 |
|
691 | 0 | const Phdr* phdrs = |
692 | 0 | GetOffset<ElfClass,Phdr>(elf_header, elf_header->e_phoff); |
693 | 0 |
|
694 | 0 | for (int i = 0; i < elf_header->e_phnum; ++i) { |
695 | 0 | if (phdrs[i].p_type == segment_type) { |
696 | 0 | *segment_start = elf_base + phdrs[i].p_offset; |
697 | 0 | *segment_size = phdrs[i].p_filesz; |
698 | 0 | return; |
699 | 0 | } |
700 | 0 | } |
701 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:void lul::(anonymous namespace)::FindElfClassSegment<lul::ElfClass32>(char const*, lul::ElfClass32::Word, void const**, int*) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:void lul::(anonymous namespace)::FindElfClassSegment<lul::ElfClass64>(char const*, lul::ElfClass64::Word, void const**, int*) |
702 | | |
703 | | } // namespace (anon) |
704 | | |
705 | 0 | bool IsValidElf(const void* elf_base) { |
706 | 0 | return strncmp(reinterpret_cast<const char*>(elf_base), |
707 | 0 | ELFMAG, SELFMAG) == 0; |
708 | 0 | } |
709 | | |
710 | 0 | int ElfClass(const void* elf_base) { |
711 | 0 | const ElfW(Ehdr)* elf_header = |
712 | 0 | reinterpret_cast<const ElfW(Ehdr)*>(elf_base); |
713 | 0 |
|
714 | 0 | return elf_header->e_ident[EI_CLASS]; |
715 | 0 | } |
716 | | |
717 | | bool FindElfSection(const void *elf_mapped_base, |
718 | | const char *section_name, |
719 | | uint32_t section_type, |
720 | | const void **section_start, |
721 | | int *section_size, |
722 | 0 | int *elfclass) { |
723 | 0 | MOZ_ASSERT(elf_mapped_base); |
724 | 0 | MOZ_ASSERT(section_start); |
725 | 0 | MOZ_ASSERT(section_size); |
726 | 0 |
|
727 | 0 | *section_start = NULL; |
728 | 0 | *section_size = 0; |
729 | 0 |
|
730 | 0 | if (!IsValidElf(elf_mapped_base)) |
731 | 0 | return false; |
732 | 0 | |
733 | 0 | int cls = ElfClass(elf_mapped_base); |
734 | 0 | if (elfclass) { |
735 | 0 | *elfclass = cls; |
736 | 0 | } |
737 | 0 |
|
738 | 0 | const char* elf_base = |
739 | 0 | static_cast<const char*>(elf_mapped_base); |
740 | 0 |
|
741 | 0 | if (cls == ELFCLASS32) { |
742 | 0 | FindElfClassSection<ElfClass32>(elf_base, section_name, section_type, |
743 | 0 | section_start, section_size); |
744 | 0 | return *section_start != NULL; |
745 | 0 | } else if (cls == ELFCLASS64) { |
746 | 0 | FindElfClassSection<ElfClass64>(elf_base, section_name, section_type, |
747 | 0 | section_start, section_size); |
748 | 0 | return *section_start != NULL; |
749 | 0 | } |
750 | 0 | |
751 | 0 | return false; |
752 | 0 | } |
753 | | |
754 | | bool FindElfSegment(const void *elf_mapped_base, |
755 | | uint32_t segment_type, |
756 | | const void **segment_start, |
757 | | int *segment_size, |
758 | 0 | int *elfclass) { |
759 | 0 | MOZ_ASSERT(elf_mapped_base); |
760 | 0 | MOZ_ASSERT(segment_start); |
761 | 0 | MOZ_ASSERT(segment_size); |
762 | 0 |
|
763 | 0 | *segment_start = NULL; |
764 | 0 | *segment_size = 0; |
765 | 0 |
|
766 | 0 | if (!IsValidElf(elf_mapped_base)) |
767 | 0 | return false; |
768 | 0 | |
769 | 0 | int cls = ElfClass(elf_mapped_base); |
770 | 0 | if (elfclass) { |
771 | 0 | *elfclass = cls; |
772 | 0 | } |
773 | 0 |
|
774 | 0 | const char* elf_base = |
775 | 0 | static_cast<const char*>(elf_mapped_base); |
776 | 0 |
|
777 | 0 | if (cls == ELFCLASS32) { |
778 | 0 | FindElfClassSegment<ElfClass32>(elf_base, segment_type, |
779 | 0 | segment_start, segment_size); |
780 | 0 | return *segment_start != NULL; |
781 | 0 | } else if (cls == ELFCLASS64) { |
782 | 0 | FindElfClassSegment<ElfClass64>(elf_base, segment_type, |
783 | 0 | segment_start, segment_size); |
784 | 0 | return *segment_start != NULL; |
785 | 0 | } |
786 | 0 | |
787 | 0 | return false; |
788 | 0 | } |
789 | | |
790 | | |
791 | | // (derived from) |
792 | | // file_id.cc: Return a unique identifier for a file |
793 | | // |
794 | | // See file_id.h for documentation |
795 | | // |
796 | | |
797 | | // ELF note name and desc are 32-bits word padded. |
798 | 0 | #define NOTE_PADDING(a) ((a + 3) & ~3) |
799 | | |
800 | | // These functions are also used inside the crashed process, so be safe |
801 | | // and use the syscall/libc wrappers instead of direct syscalls or libc. |
802 | | |
803 | | template<typename ElfClass> |
804 | | static bool ElfClassBuildIDNoteIdentifier(const void *section, int length, |
805 | 0 | uint8_t identifier[kMDGUIDSize]) { |
806 | 0 | typedef typename ElfClass::Nhdr Nhdr; |
807 | 0 |
|
808 | 0 | const void* section_end = reinterpret_cast<const char*>(section) + length; |
809 | 0 | const Nhdr* note_header = reinterpret_cast<const Nhdr*>(section); |
810 | 0 | while (reinterpret_cast<const void *>(note_header) < section_end) { |
811 | 0 | if (note_header->n_type == NT_GNU_BUILD_ID) |
812 | 0 | break; |
813 | 0 | note_header = reinterpret_cast<const Nhdr*>( |
814 | 0 | reinterpret_cast<const char*>(note_header) + sizeof(Nhdr) + |
815 | 0 | NOTE_PADDING(note_header->n_namesz) + |
816 | 0 | NOTE_PADDING(note_header->n_descsz)); |
817 | 0 | } |
818 | 0 | if (reinterpret_cast<const void *>(note_header) >= section_end || |
819 | 0 | note_header->n_descsz == 0) { |
820 | 0 | return false; |
821 | 0 | } |
822 | 0 | |
823 | 0 | const char* build_id = reinterpret_cast<const char*>(note_header) + |
824 | 0 | sizeof(Nhdr) + NOTE_PADDING(note_header->n_namesz); |
825 | 0 | // Copy as many bits of the build ID as will fit |
826 | 0 | // into the GUID space. |
827 | 0 | memset(identifier, 0, kMDGUIDSize); |
828 | 0 | memcpy(identifier, build_id, |
829 | 0 | std::min(kMDGUIDSize, (size_t)note_header->n_descsz)); |
830 | 0 |
|
831 | 0 | return true; |
832 | 0 | } Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool lul::ElfClassBuildIDNoteIdentifier<lul::ElfClass32>(void const*, int, unsigned char*) Unexecuted instantiation: Unified_cpp_tools_profiler1.cpp:bool lul::ElfClassBuildIDNoteIdentifier<lul::ElfClass64>(void const*, int, unsigned char*) |
833 | | |
834 | | // Attempt to locate a .note.gnu.build-id section in an ELF binary |
835 | | // and copy as many bytes of it as will fit into |identifier|. |
836 | | static bool FindElfBuildIDNote(const void *elf_mapped_base, |
837 | 0 | uint8_t identifier[kMDGUIDSize]) { |
838 | 0 | void* note_section; |
839 | 0 | int note_size, elfclass; |
840 | 0 | if ((!FindElfSegment(elf_mapped_base, PT_NOTE, |
841 | 0 | (const void**)¬e_section, ¬e_size, &elfclass) || |
842 | 0 | note_size == 0) && |
843 | 0 | (!FindElfSection(elf_mapped_base, ".note.gnu.build-id", SHT_NOTE, |
844 | 0 | (const void**)¬e_section, ¬e_size, &elfclass) || |
845 | 0 | note_size == 0)) { |
846 | 0 | return false; |
847 | 0 | } |
848 | 0 | |
849 | 0 | if (elfclass == ELFCLASS32) { |
850 | 0 | return ElfClassBuildIDNoteIdentifier<ElfClass32>(note_section, note_size, |
851 | 0 | identifier); |
852 | 0 | } else if (elfclass == ELFCLASS64) { |
853 | 0 | return ElfClassBuildIDNoteIdentifier<ElfClass64>(note_section, note_size, |
854 | 0 | identifier); |
855 | 0 | } |
856 | 0 | |
857 | 0 | return false; |
858 | 0 | } |
859 | | |
860 | | // Attempt to locate the .text section of an ELF binary and generate |
861 | | // a simple hash by XORing the first page worth of bytes into |identifier|. |
862 | | static bool HashElfTextSection(const void *elf_mapped_base, |
863 | 0 | uint8_t identifier[kMDGUIDSize]) { |
864 | 0 | void* text_section; |
865 | 0 | int text_size; |
866 | 0 | if (!FindElfSection(elf_mapped_base, ".text", SHT_PROGBITS, |
867 | 0 | (const void**)&text_section, &text_size, NULL) || |
868 | 0 | text_size == 0) { |
869 | 0 | return false; |
870 | 0 | } |
871 | 0 | |
872 | 0 | memset(identifier, 0, kMDGUIDSize); |
873 | 0 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section); |
874 | 0 | const uint8_t* ptr_end = ptr + std::min(text_size, 4096); |
875 | 0 | while (ptr < ptr_end) { |
876 | 0 | for (unsigned i = 0; i < kMDGUIDSize; i++) |
877 | 0 | identifier[i] ^= ptr[i]; |
878 | 0 | ptr += kMDGUIDSize; |
879 | 0 | } |
880 | 0 | return true; |
881 | 0 | } |
882 | | |
883 | | // static |
884 | | bool FileID::ElfFileIdentifierFromMappedFile(const void* base, |
885 | 0 | uint8_t identifier[kMDGUIDSize]) { |
886 | 0 | // Look for a build id note first. |
887 | 0 | if (FindElfBuildIDNote(base, identifier)) |
888 | 0 | return true; |
889 | 0 | |
890 | 0 | // Fall back on hashing the first page of the text section. |
891 | 0 | return HashElfTextSection(base, identifier); |
892 | 0 | } |
893 | | |
894 | | // static |
895 | | void FileID::ConvertIdentifierToString(const uint8_t identifier[kMDGUIDSize], |
896 | 0 | char* buffer, int buffer_length) { |
897 | 0 | uint8_t identifier_swapped[kMDGUIDSize]; |
898 | 0 |
|
899 | 0 | // Endian-ness swap to match dump processor expectation. |
900 | 0 | memcpy(identifier_swapped, identifier, kMDGUIDSize); |
901 | 0 | uint32_t* data1 = reinterpret_cast<uint32_t*>(identifier_swapped); |
902 | 0 | *data1 = htonl(*data1); |
903 | 0 | uint16_t* data2 = reinterpret_cast<uint16_t*>(identifier_swapped + 4); |
904 | 0 | *data2 = htons(*data2); |
905 | 0 | uint16_t* data3 = reinterpret_cast<uint16_t*>(identifier_swapped + 6); |
906 | 0 | *data3 = htons(*data3); |
907 | 0 |
|
908 | 0 | int buffer_idx = 0; |
909 | 0 | for (unsigned int idx = 0; |
910 | 0 | (buffer_idx < buffer_length) && (idx < kMDGUIDSize); |
911 | 0 | ++idx) { |
912 | 0 | int hi = (identifier_swapped[idx] >> 4) & 0x0F; |
913 | 0 | int lo = (identifier_swapped[idx]) & 0x0F; |
914 | 0 |
|
915 | 0 | if (idx == 4 || idx == 6 || idx == 8 || idx == 10) |
916 | 0 | buffer[buffer_idx++] = '-'; |
917 | 0 |
|
918 | 0 | buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; |
919 | 0 | buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; |
920 | 0 | } |
921 | 0 |
|
922 | 0 | // NULL terminate |
923 | 0 | buffer[(buffer_idx < buffer_length) ? buffer_idx : buffer_idx - 1] = 0; |
924 | 0 | } |
925 | | |
926 | | } // namespace lul |