michael@0: // Copyright (c) 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // file_id.cc: Return a unique identifier for a file michael@0: // michael@0: // See file_id.h for documentation michael@0: // michael@0: // Author: Alfred Peng michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "common/md5.h" michael@0: #include "common/solaris/file_id.h" michael@0: #include "common/solaris/message_output.h" michael@0: #include "google_breakpad/common/minidump_format.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: class AutoElfEnder { michael@0: public: michael@0: AutoElfEnder(Elf *elf) : elf_(elf) {} michael@0: ~AutoElfEnder() { if (elf_) elf_end(elf_); } michael@0: private: michael@0: Elf *elf_; michael@0: }; michael@0: michael@0: // Find the text section in elf object file. michael@0: // Return the section start address and the size. michael@0: static bool FindElfTextSection(int fd, const void *elf_base, michael@0: const void **text_start, michael@0: int *text_size) { michael@0: assert(text_start); michael@0: assert(text_size); michael@0: michael@0: *text_start = NULL; michael@0: *text_size = 0; michael@0: michael@0: if (elf_version(EV_CURRENT) == EV_NONE) { michael@0: print_message2(2, "elf_version() failed: %s\n", elf_errmsg(0)); michael@0: return false; michael@0: } michael@0: michael@0: GElf_Ehdr elf_header; michael@0: lseek(fd, 0L, 0); michael@0: Elf *elf = elf_begin(fd, ELF_C_READ, NULL); michael@0: AutoElfEnder elfEnder(elf); michael@0: michael@0: if (gelf_getehdr(elf, &elf_header) == (GElf_Ehdr *)NULL) { michael@0: print_message2(2, "failed to read elf header: %s\n", elf_errmsg(-1)); michael@0: return false; michael@0: } michael@0: michael@0: if (elf_header.e_ident[EI_MAG0] != ELFMAG0 || michael@0: elf_header.e_ident[EI_MAG1] != ELFMAG1 || michael@0: elf_header.e_ident[EI_MAG2] != ELFMAG2 || michael@0: elf_header.e_ident[EI_MAG3] != ELFMAG3) { michael@0: print_message1(2, "header magic doesn't match\n"); michael@0: return false; michael@0: } michael@0: michael@0: static const char kTextSectionName[] = ".text"; michael@0: const GElf_Shdr *text_section = NULL; michael@0: Elf_Scn *scn = NULL; michael@0: GElf_Shdr shdr; michael@0: michael@0: while ((scn = elf_nextscn(elf, scn)) != NULL) { michael@0: if (gelf_getshdr(scn, &shdr) == (GElf_Shdr *)0) { michael@0: print_message2(2, "failed to read section header: %s\n", elf_errmsg(0)); michael@0: return false; michael@0: } michael@0: michael@0: if (shdr.sh_type == SHT_PROGBITS) { michael@0: const char *section_name = elf_strptr(elf, elf_header.e_shstrndx, michael@0: shdr.sh_name); michael@0: if (!section_name) { michael@0: print_message2(2, "Section name error: %s\n", elf_errmsg(-1)); michael@0: continue; michael@0: } michael@0: michael@0: if (strcmp(section_name, kTextSectionName) == 0) { michael@0: text_section = &shdr; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (text_section != NULL && text_section->sh_size > 0) { michael@0: *text_start = (char *)elf_base + text_section->sh_offset; michael@0: *text_size = text_section->sh_size; michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: FileID::FileID(const char *path) { michael@0: strcpy(path_, path); michael@0: } michael@0: michael@0: class AutoCloser { michael@0: public: michael@0: AutoCloser(int fd) : fd_(fd) {} michael@0: ~AutoCloser() { if (fd_) close(fd_); } michael@0: private: michael@0: int fd_; michael@0: }; michael@0: michael@0: bool FileID::ElfFileIdentifier(unsigned char identifier[16]) { michael@0: int fd = 0; michael@0: if ((fd = open(path_, O_RDONLY)) < 0) michael@0: return false; michael@0: michael@0: AutoCloser autocloser(fd); michael@0: struct stat st; michael@0: if (fstat(fd, &st) != 0 || st.st_size <= 0) michael@0: return false; michael@0: michael@0: void *base = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); michael@0: if (base == MAP_FAILED) michael@0: return false; michael@0: michael@0: bool success = false; michael@0: const void *text_section = NULL; michael@0: int text_size = 0; michael@0: michael@0: if (FindElfTextSection(fd, base, &text_section, &text_size)) { michael@0: MD5Context md5; michael@0: MD5Init(&md5); michael@0: MD5Update(&md5, (const unsigned char *)text_section, text_size); michael@0: MD5Final(identifier, &md5); michael@0: success = true; michael@0: } michael@0: michael@0: munmap((char *)base, st.st_size); michael@0: return success; michael@0: } michael@0: michael@0: // static michael@0: bool FileID::ConvertIdentifierToString(const unsigned char identifier[16], michael@0: char *buffer, int buffer_length) { michael@0: if (buffer_length < 34) michael@0: return false; michael@0: michael@0: int buffer_idx = 0; michael@0: for (int idx = 0; idx < 16; ++idx) { michael@0: int hi = (identifier[idx] >> 4) & 0x0F; michael@0: int lo = (identifier[idx]) & 0x0F; michael@0: michael@0: buffer[buffer_idx++] = (hi >= 10) ? 'A' + hi - 10 : '0' + hi; michael@0: buffer[buffer_idx++] = (lo >= 10) ? 'A' + lo - 10 : '0' + lo; michael@0: } michael@0: michael@0: // Add an extra "0" by the end. michael@0: buffer[buffer_idx++] = '0'; michael@0: michael@0: // NULL terminate michael@0: buffer[buffer_idx] = 0; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: } // namespace google_breakpad