1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/solaris/file_id.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,197 @@ 1.4 +// Copyright (c) 2007, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// file_id.cc: Return a unique identifier for a file 1.34 +// 1.35 +// See file_id.h for documentation 1.36 +// 1.37 +// Author: Alfred Peng 1.38 + 1.39 +#include <elf.h> 1.40 +#include <fcntl.h> 1.41 +#include <gelf.h> 1.42 +#include <sys/mman.h> 1.43 +#include <sys/ksyms.h> 1.44 +#include <stdio.h> 1.45 +#include <string.h> 1.46 +#include <unistd.h> 1.47 + 1.48 +#include <cassert> 1.49 +#include <cstdio> 1.50 + 1.51 +#include "common/md5.h" 1.52 +#include "common/solaris/file_id.h" 1.53 +#include "common/solaris/message_output.h" 1.54 +#include "google_breakpad/common/minidump_format.h" 1.55 + 1.56 +namespace google_breakpad { 1.57 + 1.58 +class AutoElfEnder { 1.59 + public: 1.60 + AutoElfEnder(Elf *elf) : elf_(elf) {} 1.61 + ~AutoElfEnder() { if (elf_) elf_end(elf_); } 1.62 + private: 1.63 + Elf *elf_; 1.64 +}; 1.65 + 1.66 +// Find the text section in elf object file. 1.67 +// Return the section start address and the size. 1.68 +static bool FindElfTextSection(int fd, const void *elf_base, 1.69 + const void **text_start, 1.70 + int *text_size) { 1.71 + assert(text_start); 1.72 + assert(text_size); 1.73 + 1.74 + *text_start = NULL; 1.75 + *text_size = 0; 1.76 + 1.77 + if (elf_version(EV_CURRENT) == EV_NONE) { 1.78 + print_message2(2, "elf_version() failed: %s\n", elf_errmsg(0)); 1.79 + return false; 1.80 + } 1.81 + 1.82 + GElf_Ehdr elf_header; 1.83 + lseek(fd, 0L, 0); 1.84 + Elf *elf = elf_begin(fd, ELF_C_READ, NULL); 1.85 + AutoElfEnder elfEnder(elf); 1.86 + 1.87 + if (gelf_getehdr(elf, &elf_header) == (GElf_Ehdr *)NULL) { 1.88 + print_message2(2, "failed to read elf header: %s\n", elf_errmsg(-1)); 1.89 + return false; 1.90 + } 1.91 + 1.92 + if (elf_header.e_ident[EI_MAG0] != ELFMAG0 || 1.93 + elf_header.e_ident[EI_MAG1] != ELFMAG1 || 1.94 + elf_header.e_ident[EI_MAG2] != ELFMAG2 || 1.95 + elf_header.e_ident[EI_MAG3] != ELFMAG3) { 1.96 + print_message1(2, "header magic doesn't match\n"); 1.97 + return false; 1.98 + } 1.99 + 1.100 + static const char kTextSectionName[] = ".text"; 1.101 + const GElf_Shdr *text_section = NULL; 1.102 + Elf_Scn *scn = NULL; 1.103 + GElf_Shdr shdr; 1.104 + 1.105 + while ((scn = elf_nextscn(elf, scn)) != NULL) { 1.106 + if (gelf_getshdr(scn, &shdr) == (GElf_Shdr *)0) { 1.107 + print_message2(2, "failed to read section header: %s\n", elf_errmsg(0)); 1.108 + return false; 1.109 + } 1.110 + 1.111 + if (shdr.sh_type == SHT_PROGBITS) { 1.112 + const char *section_name = elf_strptr(elf, elf_header.e_shstrndx, 1.113 + shdr.sh_name); 1.114 + if (!section_name) { 1.115 + print_message2(2, "Section name error: %s\n", elf_errmsg(-1)); 1.116 + continue; 1.117 + } 1.118 + 1.119 + if (strcmp(section_name, kTextSectionName) == 0) { 1.120 + text_section = &shdr; 1.121 + break; 1.122 + } 1.123 + } 1.124 + } 1.125 + if (text_section != NULL && text_section->sh_size > 0) { 1.126 + *text_start = (char *)elf_base + text_section->sh_offset; 1.127 + *text_size = text_section->sh_size; 1.128 + return true; 1.129 + } 1.130 + 1.131 + return false; 1.132 +} 1.133 + 1.134 +FileID::FileID(const char *path) { 1.135 + strcpy(path_, path); 1.136 +} 1.137 + 1.138 +class AutoCloser { 1.139 + public: 1.140 + AutoCloser(int fd) : fd_(fd) {} 1.141 + ~AutoCloser() { if (fd_) close(fd_); } 1.142 + private: 1.143 + int fd_; 1.144 +}; 1.145 + 1.146 +bool FileID::ElfFileIdentifier(unsigned char identifier[16]) { 1.147 + int fd = 0; 1.148 + if ((fd = open(path_, O_RDONLY)) < 0) 1.149 + return false; 1.150 + 1.151 + AutoCloser autocloser(fd); 1.152 + struct stat st; 1.153 + if (fstat(fd, &st) != 0 || st.st_size <= 0) 1.154 + return false; 1.155 + 1.156 + void *base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 1.157 + if (base == MAP_FAILED) 1.158 + return false; 1.159 + 1.160 + bool success = false; 1.161 + const void *text_section = NULL; 1.162 + int text_size = 0; 1.163 + 1.164 + if (FindElfTextSection(fd, base, &text_section, &text_size)) { 1.165 + MD5Context md5; 1.166 + MD5Init(&md5); 1.167 + MD5Update(&md5, (const unsigned char *)text_section, text_size); 1.168 + MD5Final(identifier, &md5); 1.169 + success = true; 1.170 + } 1.171 + 1.172 + munmap((char *)base, st.st_size); 1.173 + return success; 1.174 +} 1.175 + 1.176 +// static 1.177 +bool FileID::ConvertIdentifierToString(const unsigned char identifier[16], 1.178 + char *buffer, int buffer_length) { 1.179 + if (buffer_length < 34) 1.180 + return false; 1.181 + 1.182 + int buffer_idx = 0; 1.183 + for (int idx = 0; idx < 16; ++idx) { 1.184 + int hi = (identifier[idx] >> 4) & 0x0F; 1.185 + int lo = (identifier[idx]) & 0x0F; 1.186 + 1.187 + buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; 1.188 + buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; 1.189 + } 1.190 + 1.191 + // Add an extra "0" by the end. 1.192 + buffer[buffer_idx++] = '0'; 1.193 + 1.194 + // NULL terminate 1.195 + buffer[buffer_idx] = 0; 1.196 + 1.197 + return true; 1.198 +} 1.199 + 1.200 +} // namespace google_breakpad