1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2003, 2005 Hewlett-Packard Co
3    Copyright (C) 2007 David Mosberger-Tang
4 	Contributed by David Mosberger-Tang <dmosberger@gmail.com>
5 
6 This file is part of libunwind.
7 
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15 
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
26 
27 #include <fcntl.h>
28 #include <stddef.h>
29 #include <unistd.h>
30 
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 
34 #include "libunwind_i.h"
35 #include "map_info.h"
36 
37 #if ELF_CLASS == ELFCLASS32
38 # define ELF_W(x)	ELF32_##x
39 # define Elf_W(x)	Elf32_##x
40 # define elf_w(x)	_Uelf32_##x
41 #else
42 # define ELF_W(x)	ELF64_##x
43 # define Elf_W(x)	Elf64_##x
44 # define elf_w(x)	_Uelf64_##x
45 #endif
46 
47 #define GET_FIELD(ei, offset, struct_name, elf_struct, field, check_cached) \
48   { \
49     if (!check_cached || (elf_struct)->field == 0) { \
50       if (sizeof((elf_struct)->field) != elf_w (memory_read) ( \
51           ei, ei->u.memory.map->start + offset + offsetof(struct_name, field), \
52           (uint8_t*) &((elf_struct)->field), sizeof((elf_struct)->field), false)) { \
53         return false; \
54       } \
55     } \
56   }
57 
58 #define GET_EHDR_FIELD(ei, ehdr, field, check_cached) \
59   GET_FIELD(ei, 0, Elf_W(Ehdr), ehdr, field, check_cached)
60 
61 #define GET_PHDR_FIELD(ei, offset, phdr, field) \
62   GET_FIELD(ei, offset, Elf_W(Phdr), phdr, field, false)
63 
64 #define GET_SHDR_FIELD(ei, offset, shdr, field) \
65   GET_FIELD(ei, offset, Elf_W(Shdr), shdr, field, false)
66 
67 #define GET_SYM_FIELD(ei, offset, sym, field) \
68   GET_FIELD(ei, offset, Elf_W(Sym), sym, field, false)
69 
70 #define GET_DYN_FIELD(ei, offset, dyn, field) \
71   GET_FIELD(ei, offset, Elf_W(Dyn), dyn, field, false)
72 
73 extern bool elf_w (get_proc_name) (
74     unw_addr_space_t as, pid_t pid, unw_word_t ip, char* buf, size_t len,
75     unw_word_t* offp, void* as_arg);
76 
77 extern bool elf_w (get_proc_name_in_image) (
78     unw_addr_space_t as, struct elf_image* ei, unsigned long segbase,
79     unsigned long mapoff, unw_word_t ip, char* buf, size_t buf_len, unw_word_t* offp);
80 
81 extern bool elf_w (get_load_base) (struct elf_image* ei, unw_word_t mapoff, unw_word_t* load_base);
82 
83 extern size_t elf_w (memory_read) (
84     struct elf_image* ei, unw_word_t addr, uint8_t* buffer, size_t bytes, bool string_read);
85 
elf_w(valid_object_mapped)86 static inline bool elf_w (valid_object_mapped) (struct elf_image* ei) {
87   if (ei->u.mapped.size <= EI_VERSION) {
88     return false;
89   }
90 
91   uint8_t* e_ident = (uint8_t*) ei->u.mapped.image;
92   return (memcmp (ei->u.mapped.image, ELFMAG, SELFMAG) == 0
93           && e_ident[EI_CLASS] == ELF_CLASS && e_ident[EI_VERSION] != EV_NONE
94           && e_ident[EI_VERSION] <= EV_CURRENT);
95 }
96 
elf_w(valid_object_memory)97 static inline bool elf_w (valid_object_memory) (struct elf_image* ei) {
98   uint8_t e_ident[EI_NIDENT];
99   struct map_info* map = ei->u.memory.map;
100   if (SELFMAG != elf_w (memory_read) (ei, map->start, e_ident, SELFMAG, false)) {
101     return false;
102   }
103   if (memcmp (e_ident, ELFMAG, SELFMAG) != 0) {
104     return false;
105   }
106   // Read the rest of the ident data.
107   if (EI_NIDENT - SELFMAG != elf_w (memory_read) (
108       ei, map->start + SELFMAG, e_ident + SELFMAG, EI_NIDENT - SELFMAG, false)) {
109     return false;
110   }
111   return e_ident[EI_CLASS] == ELF_CLASS && e_ident[EI_VERSION] != EV_NONE
112          && e_ident[EI_VERSION] <= EV_CURRENT;
113 }
114 
elf_map_image(struct elf_image * ei,const char * path)115 static inline bool elf_map_image (struct elf_image* ei, const char* path) {
116   struct stat stat;
117   int fd;
118 
119   fd = open (path, O_RDONLY);
120   if (fd < 0) {
121     return false;
122   }
123 
124   if (fstat (fd, &stat) == -1) {
125     close (fd);
126     return false;
127   }
128 
129   ei->u.mapped.size = stat.st_size;
130   ei->u.mapped.image = mmap (NULL, ei->u.mapped.size, PROT_READ, MAP_PRIVATE, fd, 0);
131   close (fd);
132   if (ei->u.mapped.image == MAP_FAILED) {
133     return false;
134   }
135 
136   ei->valid = elf_w (valid_object_mapped) (ei);
137   if (!ei->valid) {
138     munmap (ei->u.mapped.image, ei->u.mapped.size);
139     return false;
140   }
141 
142   ei->mapped = true;
143   // Set to true for cases where this is called outside of elf_map_cached.
144   ei->load_attempted = true;
145 
146   return true;
147 }
148 
elf_map_cached_image(unw_addr_space_t as,void * as_arg,struct map_info * map,unw_word_t ip)149 static inline bool elf_map_cached_image (
150     unw_addr_space_t as, void* as_arg, struct map_info* map, unw_word_t ip) {
151   intrmask_t saved_mask;
152 
153   // Lock while loading the cached elf image.
154   lock_acquire (&map->ei_lock, saved_mask);
155   if (!map->ei.load_attempted) {
156     map->ei.load_attempted = true;
157 
158     if (!elf_map_image (&map->ei, map->path)) {
159       // If the image cannot be loaded, we'll read data directly from
160       // the process using the access_mem function.
161       if (map->flags & PROT_READ) {
162         map->ei.u.memory.map = map;
163         map->ei.u.memory.as = as;
164         map->ei.u.memory.as_arg = as_arg;
165         map->ei.valid = elf_w (valid_object_memory) (&map->ei);
166       }
167     }
168     unw_word_t load_base;
169     if (map->ei.valid && elf_w (get_load_base) (&map->ei, map->offset, &load_base)) {
170       map->load_base = load_base;
171     }
172   }
173   lock_release (&map->ei_lock, saved_mask);
174   return map->ei.valid;
175 }
176