1*c87b03e5Sespie /* Implements unwind table entry lookup for AIX (cf. fde-glibc.c).
2*c87b03e5Sespie    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3*c87b03e5Sespie    Contributed by Timothy Wall <twall@redhat.com>
4*c87b03e5Sespie 
5*c87b03e5Sespie    This file is part of GNU CC.
6*c87b03e5Sespie 
7*c87b03e5Sespie    GNU CC is free software; you can redistribute it and/or modify
8*c87b03e5Sespie    it under the terms of the GNU General Public License as published by
9*c87b03e5Sespie    the Free Software Foundation; either version 2, or (at your option)
10*c87b03e5Sespie    any later version.
11*c87b03e5Sespie 
12*c87b03e5Sespie    GNU CC is distributed in the hope that it will be useful,
13*c87b03e5Sespie    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*c87b03e5Sespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*c87b03e5Sespie    GNU General Public License for more details.
16*c87b03e5Sespie 
17*c87b03e5Sespie    You should have received a copy of the GNU General Public License
18*c87b03e5Sespie    along with GNU CC; see the file COPYING.  If not, write to
19*c87b03e5Sespie    the Free Software Foundation, 59 Temple Place - Suite 330,
20*c87b03e5Sespie    Boston, MA 02111-1307, USA.  */
21*c87b03e5Sespie 
22*c87b03e5Sespie #include "tconfig.h"
23*c87b03e5Sespie #include "tsystem.h"
24*c87b03e5Sespie #include "unwind.h"
25*c87b03e5Sespie #include "unwind-ia64.h"
26*c87b03e5Sespie 
27*c87b03e5Sespie #include <dlfcn.h>
28*c87b03e5Sespie #include <link.h>
29*c87b03e5Sespie #include <sys/mman.h>
30*c87b03e5Sespie 
31*c87b03e5Sespie static struct unw_table_entry *
find_fde_for_dso(Elf64_Addr pc,rt_link_map * map,unsigned long * pseg_base,unsigned long * pgp)32*c87b03e5Sespie find_fde_for_dso (Elf64_Addr pc, rt_link_map *map,
33*c87b03e5Sespie                   unsigned long* pseg_base, unsigned long* pgp)
34*c87b03e5Sespie {
35*c87b03e5Sespie   rt_segment *seg;
36*c87b03e5Sespie   Elf64_Addr seg_base;
37*c87b03e5Sespie   struct unw_table_entry *f_base;
38*c87b03e5Sespie   size_t lo, hi;
39*c87b03e5Sespie 
40*c87b03e5Sespie   /* See if PC falls into one of the loaded segments.  */
41*c87b03e5Sespie   for (seg = map->l_segments; seg; seg = (rt_segment *)seg->s_next)
42*c87b03e5Sespie     {
43*c87b03e5Sespie       if (pc >= seg->s_map_addr && pc < seg->s_map_addr + seg->s_mapsz)
44*c87b03e5Sespie         break;
45*c87b03e5Sespie     }
46*c87b03e5Sespie   if (!seg)
47*c87b03e5Sespie     return NULL;
48*c87b03e5Sespie 
49*c87b03e5Sespie   /* Search for the entry within the unwind table.  */
50*c87b03e5Sespie   f_base = (struct unw_table_entry *) (map->l_unwind_table);
51*c87b03e5Sespie   seg_base = (Elf64_Addr) seg->s_map_addr;
52*c87b03e5Sespie   lo = 0;
53*c87b03e5Sespie   hi = map->l_unwind_sz / sizeof (struct unw_table_entry);
54*c87b03e5Sespie 
55*c87b03e5Sespie   while (lo < hi)
56*c87b03e5Sespie     {
57*c87b03e5Sespie       size_t mid = (lo + hi) / 2;
58*c87b03e5Sespie       struct unw_table_entry *f = f_base + mid;
59*c87b03e5Sespie 
60*c87b03e5Sespie       if (pc < f->start_offset + seg_base)
61*c87b03e5Sespie         hi = mid;
62*c87b03e5Sespie       else if (pc >= f->end_offset + seg_base)
63*c87b03e5Sespie         lo = mid + 1;
64*c87b03e5Sespie       else {
65*c87b03e5Sespie         /* AIX executables are *always* dynamic.  Look up GP for this
66*c87b03e5Sespie            object.  */
67*c87b03e5Sespie         Elf64_Dyn *dyn = map->l_ld;
68*c87b03e5Sespie         *pgp = 0;
69*c87b03e5Sespie         for (; dyn->d_tag != DT_NULL ; dyn++)
70*c87b03e5Sespie           {
71*c87b03e5Sespie             if (dyn->d_tag == DT_PLTGOT)
72*c87b03e5Sespie               {
73*c87b03e5Sespie                 *pgp = dyn->d_un.d_ptr;
74*c87b03e5Sespie                 break;
75*c87b03e5Sespie               }
76*c87b03e5Sespie           }
77*c87b03e5Sespie         *pseg_base = seg_base;
78*c87b03e5Sespie         return f;
79*c87b03e5Sespie       }
80*c87b03e5Sespie     }
81*c87b03e5Sespie   return NULL;
82*c87b03e5Sespie }
83*c87b03e5Sespie 
84*c87b03e5Sespie /* Return a pointer to the unwind table entry for the function containing
85*c87b03e5Sespie    PC.  */
86*c87b03e5Sespie struct unw_table_entry *
_Unwind_FindTableEntry(void * pc,unsigned long * pseg_base,unsigned long * pgp)87*c87b03e5Sespie _Unwind_FindTableEntry (void *pc, unsigned long *pseg_base, unsigned long *pgp)
88*c87b03e5Sespie {
89*c87b03e5Sespie   extern rt_r_debug _r_debug;
90*c87b03e5Sespie   struct unw_table_entry *ret;
91*c87b03e5Sespie   rt_link_map *map = _r_debug.r_map; /* address of link map */
92*c87b03e5Sespie 
93*c87b03e5Sespie   /* Check the main application first, hoping that most of the user's
94*c87b03e5Sespie      code is there instead of in some library.  */
95*c87b03e5Sespie   ret = find_fde_for_dso ((Elf64_Addr)pc, map, pseg_base, pgp);
96*c87b03e5Sespie   if (ret)
97*c87b03e5Sespie     {
98*c87b03e5Sespie       /* If we're in the main application, use the current GP value.  */
99*c87b03e5Sespie       register unsigned long gp __asm__("gp");
100*c87b03e5Sespie       *pgp = gp;
101*c87b03e5Sespie       return ret;
102*c87b03e5Sespie     }
103*c87b03e5Sespie 
104*c87b03e5Sespie   /* FIXME need a DSO lock mechanism for AIX here, to ensure shared
105*c87b03e5Sespie      libraries aren't changed while we're examining them.  */
106*c87b03e5Sespie 
107*c87b03e5Sespie   for (map = _r_debug.r_map; map; map = map->l_next)
108*c87b03e5Sespie     {
109*c87b03e5Sespie       /* Skip the main application's entry.  */
110*c87b03e5Sespie       if (!map->l_name)
111*c87b03e5Sespie       continue;
112*c87b03e5Sespie       ret = find_fde_for_dso ((Elf64_Addr)pc, map, pseg_base, pgp);
113*c87b03e5Sespie       if (ret)
114*c87b03e5Sespie       break;
115*c87b03e5Sespie     }
116*c87b03e5Sespie 
117*c87b03e5Sespie   /* FIXME need a DSO unlock mechanism for AIX here.  */
118*c87b03e5Sespie 
119*c87b03e5Sespie   return ret;
120*c87b03e5Sespie }
121