Home | History | Annotate | Download | only in src
      1 /* libunwind - a platform-independent unwind library
      2    Copyright (C) 2003-2005 Hewlett-Packard Co
      3 	Contributed by David Mosberger-Tang <davidm (at) hpl.hp.com>
      4 
      5 This file is part of libunwind.
      6 
      7 Permission is hereby granted, free of charge, to any person obtaining
      8 a copy of this software and associated documentation files (the
      9 "Software"), to deal in the Software without restriction, including
     10 without limitation the rights to use, copy, modify, merge, publish,
     11 distribute, sublicense, and/or sell copies of the Software, and to
     12 permit persons to whom the Software is furnished to do so, subject to
     13 the following conditions:
     14 
     15 The above copyright notice and this permission notice shall be
     16 included in all copies or substantial portions of the Software.
     17 
     18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     24 WITH 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 "libunwind-ptrace.h"
     31 #include "map_info.h"
     32 #include "os-linux.h"
     33 
     34 /* ANDROID support update. */
     35 HIDDEN struct map_info *
     36 map_create_list (int map_create_type, pid_t pid)
     37 {
     38   struct map_iterator mi;
     39   unsigned long start, end, offset, flags;
     40   struct map_info *map_list = NULL;
     41   struct map_info *cur_map;
     42   unw_addr_space_t as = NULL;
     43   struct unw_addr_space local_as;
     44   void* as_arg = NULL;
     45 
     46   if (maps_init (&mi, pid) < 0)
     47     return NULL;
     48 
     49   while (maps_next (&mi, &start, &end, &offset, &flags))
     50     {
     51       cur_map = map_alloc_info ();
     52       if (cur_map == MAP_FAILED)
     53         break;
     54       cur_map->next = map_list;
     55       cur_map->start = start;
     56       cur_map->end = end;
     57       cur_map->offset = offset;
     58       cur_map->load_base = 0;
     59       cur_map->flags = flags;
     60       cur_map->path = strdup (mi.path);
     61       mutex_init (&cur_map->ei_lock);
     62       cur_map->ei.valid = false;
     63       cur_map->ei.load_attempted = false;
     64       cur_map->ei.mapped = false;
     65       cur_map->ei.mini_debug_info_data = NULL;
     66       cur_map->ei.mini_debug_info_size = 0;
     67 
     68       /* Indicate mapped memory of devices is special and should not
     69          be read or written. Use a special flag instead of zeroing the
     70          flags to indicate that the maps do not need to be rebuilt if
     71          any values ever wind up in these special maps.
     72          /dev/ashmem/... maps are special and don't have any restrictions,
     73          so don't mark them as device memory.  */
     74       if (strncmp ("/dev/", cur_map->path, 5) == 0
     75           && strncmp ("ashmem/", cur_map->path + 5, 7) != 0)
     76         cur_map->flags |= MAP_FLAGS_DEVICE_MEM;
     77 
     78       /* If this is a readable executable map, and not a stack map or an
     79          empty map, find the load_base.  */
     80       if (cur_map->path[0] != '\0' && strncmp ("[stack:", cur_map->path, 7) != 0
     81           && (flags & (PROT_EXEC | PROT_READ)) == (PROT_EXEC | PROT_READ)
     82           && !(cur_map->flags & MAP_FLAGS_DEVICE_MEM))
     83         {
     84           struct elf_image ei;
     85           // Do not map elf for local unwinds, it's faster to read
     86           // from memory directly.
     87           if (map_create_type == UNW_MAP_CREATE_REMOTE
     88               && elf_map_image (&ei, cur_map->path))
     89             {
     90               unw_word_t load_base;
     91               if (elf_w (get_load_base) (&ei, offset, &load_base))
     92                 cur_map->load_base = load_base;
     93               munmap (ei.u.mapped.image, ei.u.mapped.size);
     94             }
     95           else
     96             {
     97               // Create an address space right here with enough initialized
     98               // to read data.
     99               if (as == NULL)
    100                 {
    101                   if (map_create_type == UNW_MAP_CREATE_LOCAL)
    102                     {
    103                       as = &local_as;
    104                       unw_local_access_addr_space_init (as);
    105                     }
    106                   else
    107                     {
    108                       // For a remote unwind, create the address space
    109                       // and arg data the first time we need it.
    110                       // We'll reuse these values if we need to attempt
    111                       // to get elf data for another map.
    112                       as = unw_create_addr_space (&_UPT_accessors, 0);
    113                       if (as)
    114                         {
    115                           as_arg = (void*) _UPT_create (pid);
    116                           if (!as_arg)
    117                             {
    118                               unw_destroy_addr_space (as);
    119                               as = NULL;
    120                             }
    121                         }
    122                     }
    123                 }
    124               if (as)
    125                 {
    126                   ei.mapped = false;
    127                   ei.u.memory.start = cur_map->start;
    128                   ei.u.memory.end = cur_map->end;
    129                   ei.u.memory.as = as;
    130                   ei.u.memory.as_arg = as_arg;
    131                   ei.valid = elf_w (valid_object_memory) (&ei);
    132                   unw_word_t load_base;
    133                   if (ei.valid && elf_w (get_load_base) (&ei, cur_map->offset, &load_base))
    134                     cur_map->load_base = load_base;
    135                 }
    136             }
    137         }
    138 
    139       map_list = cur_map;
    140     }
    141 
    142   maps_close (&mi);
    143 
    144   if (as && map_create_type == UNW_MAP_CREATE_REMOTE)
    145     {
    146       unw_destroy_addr_space (as);
    147       _UPT_destroy (as_arg);
    148     }
    149 
    150   return map_list;
    151 }
    152 /* End of ANDROID update. */
    153