PageRenderTime 38ms CodeModel.GetById 25ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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