1/* libunwind - a platform-independent unwind library 2 3This file is part of libunwind. 4 5Permission is hereby granted, free of charge, to any person obtaining 6a copy of this software and associated documentation files (the 7"Software"), to deal in the Software without restriction, including 8without limitation the rights to use, copy, modify, merge, publish, 9distribute, sublicense, and/or sell copies of the Software, and to 10permit persons to whom the Software is furnished to do so, subject to 11the following conditions: 12 13The above copyright notice and this permission notice shall be 14included in all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 23 24#include <elf.h> 25 26#include "_UCD_lib.h" 27#include "_UCD_internal.h" 28 29static coredump_phdr_t * 30CD_elf_map_image(struct UCD_info *ui, coredump_phdr_t *phdr) 31{ 32 struct elf_image *ei = &ui->edi.ei; 33 34 if (phdr->backing_fd < 0) 35 { 36 /* Note: coredump file contains only phdr->p_filesz bytes. 37 * We want to map bigger area (phdr->p_memsz bytes) to make sure 38 * these pages are allocated, but non-accessible. 39 */ 40 /* addr, length, prot, flags, fd, fd_offset */ 41 ei->image = mmap(NULL, phdr->p_memsz, PROT_READ, MAP_PRIVATE, ui->coredump_fd, phdr->p_offset); 42 if (ei->image == MAP_FAILED) 43 { 44 ei->image = NULL; 45 return NULL; 46 } 47 ei->size = phdr->p_filesz; 48 size_t remainder_len = phdr->p_memsz - phdr->p_filesz; 49 if (remainder_len > 0) 50 { 51 void *remainder_base = (char*) ei->image + phdr->p_filesz; 52 munmap(remainder_base, remainder_len); 53 } 54 } else { 55 /* We have a backing file for this segment. 56 * This file is always longer than phdr->p_memsz, 57 * and if phdr->p_filesz !=0, first phdr->p_filesz bytes in coredump 58 * are the same as first bytes in the file. (Thus no need to map coredump) 59 * We map the entire file: 60 * unwinding may need data which is past phdr->p_memsz bytes. 61 */ 62 /* addr, length, prot, flags, fd, fd_offset */ 63 ei->image = mmap(NULL, phdr->backing_filesize, PROT_READ, MAP_PRIVATE, phdr->backing_fd, 0); 64 if (ei->image == MAP_FAILED) 65 { 66 ei->image = NULL; 67 return NULL; 68 } 69 ei->size = phdr->backing_filesize; 70 } 71 72 /* Check ELF header for sanity */ 73 if (!elf_w(valid_object)(ei)) 74 { 75 munmap(ei->image, ei->size); 76 ei->image = NULL; 77 ei->size = 0; 78 return NULL; 79 } 80 81 return phdr; 82} 83 84HIDDEN coredump_phdr_t * 85_UCD_get_elf_image(struct UCD_info *ui, unw_word_t ip) 86{ 87 unsigned i; 88 for (i = 0; i < ui->phdrs_count; i++) 89 { 90 coredump_phdr_t *phdr = &ui->phdrs[i]; 91 if (phdr->p_vaddr <= ip && ip < phdr->p_vaddr + phdr->p_memsz) 92 { 93 phdr = CD_elf_map_image(ui, phdr); 94 return phdr; 95 } 96 } 97 return NULL; 98} 99