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