blob: c331e50aa03c5e77c71830268e46035adecdff08 [file] [log] [blame]
mostang.com!davidm824d6612003-02-08 10:10:59 +00001/* libunwind - a platform-independent unwind library
hp.com!davidm5724bee2005-05-20 09:48:08 +00002 Copyright (C) 2003, 2005 Hewlett-Packard Co
David Mosberger-Tange6b9f352007-08-22 13:02:09 -06003 Copyright (C) 2007 David Mosberger-Tang
4 Contributed by David Mosberger-Tang <dmosberger@gmail.com>
mostang.com!davidm824d6612003-02-08 10:10:59 +00005
6This file is part of libunwind.
7
8Permission is hereby granted, free of charge, to any person obtaining
9a copy of this software and associated documentation files (the
10"Software"), to deal in the Software without restriction, including
11without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense, and/or sell copies of the Software, and to
13permit persons to whom the Software is furnished to do so, subject to
14the following conditions:
15
16The above copyright notice and this permission notice shall be
17included in all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26
mostang.com!davidm824d6612003-02-08 10:10:59 +000027#include <fcntl.h>
28#include <unistd.h>
29
30#include <sys/mman.h>
mostang.com!davidm63d70032003-03-19 19:25:18 +000031#include <sys/stat.h>
mostang.com!davidm824d6612003-02-08 10:10:59 +000032
Arun Sharmad41a4532013-05-19 00:04:15 -070033#include "libunwind_i.h"
Christopher Ferrisf4a8df52014-03-07 19:40:06 -080034#include "map_info.h"
Arun Sharmad41a4532013-05-19 00:04:15 -070035
mostang.com!davidm824d6612003-02-08 10:10:59 +000036#if ELF_CLASS == ELFCLASS32
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000037# define ELF_W(x) ELF32_##x
38# define Elf_W(x) Elf32_##x
39# define elf_w(x) _Uelf32_##x
mostang.com!davidm824d6612003-02-08 10:10:59 +000040#else
hp.com!davidm4fafd8c2003-12-20 11:23:44 +000041# define ELF_W(x) ELF64_##x
42# define Elf_W(x) Elf64_##x
43# define elf_w(x) _Uelf64_##x
mostang.com!davidm824d6612003-02-08 10:10:59 +000044#endif
45
Arun Sharmaa83e96c2011-01-23 17:55:55 -080046extern 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
Arun Sharmad276b7a2012-03-12 18:45:50 -070051extern 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
mostang.com!davidm824d6612003-02-08 10:10:59 +000058static inline int
Arun Sharma3d085062012-02-12 19:53:38 -080059elf_w (valid_object) (struct elf_image *ei)
60{
61 if (ei->size <= EI_VERSION)
62 return 0;
63
64 return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
65 && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS
66 && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
67 && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
68}
69
70static inline int
mostang.com!davidm9c23f9c2003-04-03 07:59:15 +000071elf_map_image (struct elf_image *ei, const char *path)
mostang.com!davidm824d6612003-02-08 10:10:59 +000072{
73 struct stat stat;
74 int fd;
75
76 fd = open (path, O_RDONLY);
77 if (fd < 0)
78 return -1;
79
80 if (fstat (fd, &stat) < 0)
81 {
82 close (fd);
83 return -1;
84 }
85
86 ei->size = stat.st_size;
87 ei->image = mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
88 close (fd);
89 if (ei->image == MAP_FAILED)
90 return -1;
91
Arun Sharmaa83e96c2011-01-23 17:55:55 -080092 if (!elf_w (valid_object) (ei))
93 {
94 munmap(ei->image, ei->size);
95 return -1;
96 }
97
mostang.com!davidm824d6612003-02-08 10:10:59 +000098 return 0;
99}
Christopher Ferrisf4a8df52014-03-07 19:40:06 -0800100
101/* ANDROID support update */
102static inline int
103elf_map_cached_image (struct map_info *map, unw_word_t ip)
104{
105 intrmask_t saved_mask;
106 int return_value = 0;
107
108 /* Lock while loading the cached elf image. */
109 lock_acquire (&map->ei_lock, saved_mask);
110 if (map->ei.image == NULL)
111 {
112 if (elf_map_image(&map->ei, map->path) < 0)
113 {
114 map->ei.image = NULL;
115 return_value = -1;
116 }
117 }
118 lock_release (&map->ei_lock, saved_mask);
119 return return_value;
120}
121/* End of ANDROID update */