Initial import
[external/libunwind.git] / src / elfxx.h
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 <elf.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33
34 #if ELF_CLASS == ELFCLASS32
35 # define ELF_W(x)       ELF32_##x
36 # define Elf_W(x)       Elf32_##x
37 # define elf_w(x)       _Uelf32_##x
38 #else
39 # define ELF_W(x)       ELF64_##x
40 # define Elf_W(x)       Elf64_##x
41 # define elf_w(x)       _Uelf64_##x
42 #endif
43
44 #include "libunwind_i.h"
45
46 extern int elf_w (get_proc_name) (unw_addr_space_t as,
47                                   pid_t pid, unw_word_t ip,
48                                   char *buf, size_t len,
49                                   unw_word_t *offp);
50
51 extern int elf_w (get_proc_name_in_image) (unw_addr_space_t as,
52                                            struct elf_image *ei,
53                                            unsigned long segbase,
54                                            unsigned long mapoff,
55                                            unw_word_t ip,
56                                            char *buf, size_t buf_len, unw_word_t *offp);
57
58 extern int elf_w (get_proc_name) (unw_addr_space_t as,
59                                   pid_t pid, unw_word_t ip,
60                                   char *buf, size_t len,
61                                   unw_word_t *offp);
62
63 static inline int
64 elf_w (valid_object) (struct elf_image *ei)
65 {
66   if (ei->size <= EI_VERSION)
67     return 0;
68
69   return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
70           && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS
71           && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
72           && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
73 }
74
75 static inline int
76 elf_map_image (struct elf_image *ei, const char *path)
77 {
78   struct stat stat;
79   int fd;
80
81   fd = open (path, O_RDONLY);
82   if (fd < 0)
83     return -1;
84
85   if (fstat (fd, &stat) < 0)
86     {
87       close (fd);
88       return -1;
89     }
90
91   ei->size = stat.st_size;
92   ei->image = mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
93   close (fd);
94   if (ei->image == MAP_FAILED)
95     return -1;
96
97   if (!elf_w (valid_object) (ei))
98   {
99     munmap(ei->image, ei->size);
100     return -1;
101   }
102
103   return 0;
104 }