From 8b7b8659ecfe55fabefc13bdada67c1d78265f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Thu, 31 May 2018 12:30:07 +0200 Subject: [PATCH] eh_elf: parse and store memory map --- src/eh_elf/memory_map.c | 102 ++++++++++++++++++++++++++++++++++++++++ src/eh_elf/memory_map.h | 48 +++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 src/eh_elf/memory_map.c create mode 100644 src/eh_elf/memory_map.h diff --git a/src/eh_elf/memory_map.c b/src/eh_elf/memory_map.c new file mode 100644 index 00000000..dedd00b3 --- /dev/null +++ b/src/eh_elf/memory_map.c @@ -0,0 +1,102 @@ +#include "memory_map.h" +#include +#include +#include +#include +#include + +static mmap_entry_t* _memory_map = NULL; +static size_t _memory_map_size = 0; + +/// Init the memory map with a given /proc/XX/ argument +int mmap_init_procdir(const char* procdir); + + +int mmap_init_local() { + return mmap_init_procdir("/proc/self/"); +} + + +int mmap_init_pid(pid_t pid) { + char procdir[64]; + sprintf(procdir, "/proc/%d/", pid); + return mmap_init_procdir(procdir); +} + +int mmap_init_procdir(const char* procdir) { + // This function reads /proc/pid/maps and deduces the memory map + + // Open the mmap file + char map_path[128]; + sprintf(map_path, "%s/maps", procdir); + FILE* map_handle = fopen(map_path, "r"); + if(map_handle == NULL) + return -1; + + // Get line count + int nb_entries = 0; + int lastch; + while((lastch = fgetc(map_handle)) != EOF) { + if(lastch == '\n') + nb_entries++; + } + rewind(map_handle); + _memory_map = (mmap_entry_t*) calloc(nb_entries, sizeof(mmap_entry_t)); + _memory_map_size = nb_entries; + + // Read all lines + uintptr_t ip_beg, ip_end, offset, inode; + char is_x; + char path[256]; + int cur_entry = 0; + while(fscanf(map_handle, + "%lX-%lX %*c%*c%c%*c %lX %*[0-9a-fA-F:] %ld %s", + &ip_beg, &ip_end, &is_x, &offset, &inode, path) != EOF) + { + if(cur_entry >= nb_entries) { + mmap_clear(); + return -2; // Bad entry count, somehow + } + + if(inode == 0) // Special region, out of our scope + continue; + if(is_x != 'x') // Not executable, out of our scope + continue; + + _memory_map[cur_entry].id = cur_entry; + _memory_map[cur_entry].offset = offset; + _memory_map[cur_entry].beg_ip = ip_beg; + _memory_map[cur_entry].end_ip = ip_end; + _memory_map[cur_entry].object_name = + (char*) malloc(sizeof(char) * (strlen(path) + 1)); + strcpy(_memory_map[cur_entry].object_name, path); + + cur_entry++; + } + + // Shrink _memory_map to only use up the number of relevant entries + assert(_memory_map_size >= cur_entry); + _memory_map_size = cur_entry; // Because of skipped entries + _memory_map = (mmap_entry_t*) + realloc(_memory_map, _memory_map_size * sizeof(mmap_entry_t)); + + // dlopen corresponding eh_elf objects + for(size_t id = 0; id < _memory_map_size; ++id) { + char eh_elf_path[256]; + char* obj_basename = basename(_memory_map[id].object_name); + sprintf(eh_elf_path, "%s.eh_elf.so", obj_basename); + _memory_map[id].eh_elf = dlopen(eh_elf_path, RTLD_LAZY); + } + + return 0; +} + +void mmap_clear() { + if(_memory_map != NULL) { + for(size_t pos=0; pos < _memory_map_size; ++pos) { + free(_memory_map[pos].object_name); + dlclose(_memory_map[pos].eh_elf); + } + free(_memory_map); + } +} diff --git a/src/eh_elf/memory_map.h b/src/eh_elf/memory_map.h new file mode 100644 index 00000000..34dbc723 --- /dev/null +++ b/src/eh_elf/memory_map.h @@ -0,0 +1,48 @@ +/********** Libunwind -- eh_elf flavour ********** + * This is the eh_elf version of libunwind, made for academic purposes. + * + * Théophile Bastian + ************************************************* + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + ************************************************/ + +#pragma once + +#include +#include +#include + +/// A type representing a dlopen handle +typedef void* dl_obj_t; + +/// A structure containing the informations gathererd about a line in the +/// memory map +typedef struct { + int id; ///< ID of this entry, for fast access + uintptr_t offset; ///< Total offset: ip + offset = ip in original ELF file + char* object_name; ///< Name of the object mapped here + uintptr_t beg_ip, end_ip; ///< Start and end IP of this object in memory + dl_obj_t eh_elf; ///< Corresponding eh_elf file, dlopen'd +} mmap_entry_t; + +/// Dealloc all allocated memory and reset internal state +void mmap_clear(); + +/** Init the memory map for the local process + * @returns 0 upon success, or a negative value upon failure. + **/ +int mmap_init_local(); + +/** Init the memory map for a remote process with the given pid + * @returns 0 upon success, or a negative value upon failure. + **/ +int mmap_init_pid(pid_t pid); + +/// Get the `mmap_entry_t` corresponding to the given IP +int mmap_get_entry(uintptr_t ip, mmap_entry_t* entry);