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