os-linux.c revision f6cc9135efb61bbcc41656aea78f89d213c51976
1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2003-2005 Hewlett-Packard Co
3	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25
26#include <limits.h>
27#include <stdio.h>
28
29#include "libunwind_i.h"
30#include "map_info.h"
31#include "os-linux.h"
32
33/* ANDROID support update. */
34struct map_info *
35maps_create_list(pid_t pid)
36{
37  struct map_iterator mi;
38  unsigned long start, end, offset, flags;
39  struct map_info *map_list = NULL;
40  struct map_info *cur_map;
41
42  if (maps_init (&mi, pid) < 0)
43    return NULL;
44
45  while (maps_next (&mi, &start, &end, &offset, &flags))
46    {
47      cur_map = (struct map_info *)malloc(sizeof(struct map_info));
48      if (cur_map == NULL)
49        break;
50      cur_map->next = map_list;
51      cur_map->start = start;
52      cur_map->end = end;
53      cur_map->offset = offset;
54      cur_map->flags = flags;
55      cur_map->path = strdup(mi.path);
56      cur_map->ei.size = 0;
57      cur_map->ei.image = NULL;
58
59      map_list = cur_map;
60    }
61
62  maps_close (&mi);
63
64  return map_list;
65}
66
67void
68maps_destroy_list(struct map_info *map_info)
69{
70  struct map_info *map;
71  while (map_info)
72    {
73      map = map_info;
74      map_info = map->next;
75      if (map->ei.image != MAP_FAILED && map->ei.image != NULL)
76        munmap(map->ei.image, map->ei.size);
77      if (map->path)
78        free(map->path);
79      free(map);
80    }
81}
82
83static struct map_info *
84get_map(struct map_info *map_list, unw_word_t addr)
85{
86  while (map_list)
87    {
88      if (addr >= map_list->start && addr < map_list->end)
89        return map_list;
90      map_list = map_list->next;
91    }
92  return NULL;
93}
94
95int maps_is_readable(struct map_info *map_list, unw_word_t addr)
96{
97  struct map_info *map = get_map(map_list, addr);
98  if (map != NULL)
99    return map->flags & PROT_READ;
100  return 0;
101}
102
103int maps_is_writable(struct map_info *map_list, unw_word_t addr)
104{
105  struct map_info *map = get_map(map_list, addr);
106  if (map != NULL)
107    return map->flags & PROT_WRITE;
108  return 0;
109}
110/* End of ANDROID update. */
111
112PROTECTED struct map_info*
113tdep_get_elf_image(unw_addr_space_t as, pid_t pid, unw_word_t ip)
114{
115  /* ANDROID support update. */
116  struct map_info *map;
117
118  if (as->map_list == NULL)
119    as->map_list = maps_create_list(pid);
120
121  map = as->map_list;
122  while (map)
123    {
124      if (ip >= map->start && ip < map->end)
125        break;
126      map = map->next;
127    }
128  if (!map)
129    return NULL;
130
131  if (map->ei.image == NULL)
132    {
133      int ret = elf_map_image(&map->ei, map->path);
134      if (ret < 0)
135        {
136          map->ei.image = NULL;
137          return NULL;
138        }
139    }
140  /* End of ANDROID update. */
141  return map;
142}
143