aboutsummaryrefslogtreecommitdiff
path: root/src/common/linux/file_id.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/linux/file_id.cc')
-rw-r--r--src/common/linux/file_id.cc92
1 files changed, 78 insertions, 14 deletions
diff --git a/src/common/linux/file_id.cc b/src/common/linux/file_id.cc
index 34c9e508..8fae273a 100644
--- a/src/common/linux/file_id.cc
+++ b/src/common/linux/file_id.cc
@@ -33,6 +33,8 @@
//
#include "common/linux/file_id.h"
+#include "common/linux/linux_libc_support.h"
+#include "common/linux/linux_syscall_support.h"
#include <arpa/inet.h>
#include <elf.h>
@@ -42,6 +44,7 @@
#include <sys/mman.h>
#include <unistd.h>
+#include <algorithm>
#include <cassert>
#include <cstdio>
@@ -51,33 +54,94 @@ FileID::FileID(const char* path) {
strncpy(path_, path, sizeof(path_));
}
+// These two functions are also used inside the crashed process, so be safe
+// and use the syscall/libc wrappers instead of direct syscalls or libc.
+ static bool FindElfTextSection(const void *elf_mapped_base,
+ const void **text_start,
+ int *text_size) {
+ assert(elf_mapped_base);
+ assert(text_start);
+ assert(text_size);
+
+ const char* elf_base =
+ static_cast<const char*>(elf_mapped_base);
+ const ElfW(Ehdr)* elf_header =
+ reinterpret_cast<const ElfW(Ehdr)*>(elf_base);
+ if (my_strncmp(elf_base, ELFMAG, SELFMAG) != 0)
+ return false;
+#if __ELF_NATIVE_CLASS == 32
+#define ELFCLASS ELFCLASS32
+#else
+#define ELFCLASS ELFCLASS64
+#endif
+ //TODO: support dumping 32-bit binaries from a 64-bit dump_syms?
+ if (elf_header->e_ident[EI_CLASS] != ELFCLASS)
+ return false;
+ *text_start = NULL;
+ *text_size = 0;
+ const ElfW(Shdr)* sections =
+ reinterpret_cast<const ElfW(Shdr)*>(elf_base + elf_header->e_shoff);
+ const char* text_section_name = ".text";
+ int name_len = my_strlen(text_section_name);
+ const ElfW(Shdr)* string_section = sections + elf_header->e_shstrndx;
+ const ElfW(Shdr)* text_section = NULL;
+ for (int i = 0; i < elf_header->e_shnum; ++i) {
+ if (sections[i].sh_type == SHT_PROGBITS) {
+ const char* section_name = (char*)(elf_base +
+ string_section->sh_offset +
+ sections[i].sh_name);
+ if (!my_strncmp(section_name, text_section_name, name_len)) {
+ text_section = &sections[i];
+ break;
+ }
+ }
+ }
+ if (text_section != NULL && text_section->sh_size > 0) {
+ *text_start = elf_base + text_section->sh_offset;
+ *text_size = text_section->sh_size;
+ }
+ return true;
+}
+
+// static
+bool FileID::ElfFileIdentifierFromMappedFile(void* base,
+ uint8_t identifier[kMDGUIDSize])
+{
+ const void* text_section = NULL;
+ int text_size = 0;
+ bool success = false;
+ if (FindElfTextSection(base, &text_section, &text_size) && (text_size > 0)) {
+ my_memset(identifier, 0, kMDGUIDSize);
+ const uint8_t* ptr = reinterpret_cast<const uint8_t*>(text_section);
+ const uint8_t* ptr_end = ptr + std::min(text_size, 4096);
+ while (ptr < ptr_end) {
+ for (unsigned i = 0; i < kMDGUIDSize; i++)
+ identifier[i] ^= ptr[i];
+ ptr += kMDGUIDSize;
+ }
+ success = true;
+ }
+ return success;
+}
+
bool FileID::ElfFileIdentifier(uint8_t identifier[kMDGUIDSize]) {
- const ssize_t mapped_len = 4096; // Page size (matches WriteMappings())
int fd = open(path_, O_RDONLY);
if (fd < 0)
return false;
struct stat st;
- if (fstat(fd, &st) != 0 || st.st_size <= mapped_len) {
+ if (fstat(fd, &st) != 0) {
close(fd);
return false;
}
- void* base = mmap(NULL, mapped_len,
+ void* base = mmap(NULL, st.st_size,
PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
close(fd);
if (base == MAP_FAILED)
return false;
- memset(identifier, 0, kMDGUIDSize);
- uint8_t* ptr = reinterpret_cast<uint8_t*>(base);
- uint8_t* ptr_end = ptr + mapped_len;
- while (ptr < ptr_end) {
- for (unsigned i = 0; i < kMDGUIDSize; i++)
- identifier[i] ^= ptr[i];
- ptr += kMDGUIDSize;
- }
-
- munmap(base, mapped_len);
- return true;
+ bool success = ElfFileIdentifierFromMappedFile(base, identifier);
+ munmap(base, st.st_size);
+ return success;
}
// static