• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ELFXX_H
28 #define ELFXX_H
29 #include <fcntl.h>
30 #include <unistd.h>
31 
32 #include <sys/mman.h>
33 #include <sys/stat.h>
34 
35 #include "libunwind_i.h"
36 
37 #if UNW_ELF_CLASS == UNW_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 extern int elf_w (get_proc_name) (unw_addr_space_t as,
48                                   pid_t pid, unw_word_t ip,
49                                   char *buf, size_t len,
50                                   unw_word_t *offp);
51 
52 extern int elf_w (get_proc_name_in_image) (unw_addr_space_t as,
53                                            struct elf_image *ei,
54                                            unsigned long segbase,
55                                            unsigned long mapoff,
56                                            unw_word_t ip,
57                                            char *buf, size_t buf_len, unw_word_t *offp);
58 
59 extern int elf_w (get_symbol_info_in_image) (struct elf_image *ei,
60                                              unsigned long segbase,
61                                              unsigned long mapoff,
62                                              uint64_t pc,
63                                              int buf_sz,
64                                              char *buf,
65                                              uint64_t *sym_start,
66                                              uint64_t *sym_end);
67 
68 extern Elf_W (Shdr)* elf_w (find_section) (struct elf_image *ei, const char* secname);
69 extern int elf_w (load_debuglink) (const char* file, struct elf_image *ei, int is_local);
70 
71 static inline int
elf_w(valid_object)72 elf_w (valid_object) (struct elf_image *ei)
73 {
74   if (ei->size <= EI_VERSION)
75     return 0;
76 
77   return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
78           && ((uint8_t *) ei->image)[EI_CLASS] == UNW_ELF_CLASS
79           && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
80           && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
81 }
82 
83 static inline int
elf_map_image(struct elf_image * ei,const char * path)84 elf_map_image (struct elf_image *ei, const char *path)
85 {
86   struct stat stat;
87   int fd;
88 
89   fd = open (path, O_RDONLY);
90   if (fd < 0)
91     return -1;
92 
93   if (fstat (fd, &stat) < 0)
94     {
95       close (fd);
96       return -1;
97     }
98 
99   ei->size = stat.st_size;
100   ei->image = mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
101   close (fd);
102   if (ei->image == MAP_FAILED)
103     return -1;
104 
105   if (!elf_w (valid_object) (ei))
106   {
107     munmap(ei->image, ei->size);
108     return -1;
109   }
110 
111   return 0;
112 }
113 
114 #endif
115