7dd698452aef811e8ea3f29427f15fa7f671083e
[platform/upstream/gcc.git] / gcc / config / ia64 / fde-glibc.c
1 /* Copyright (C) 2000 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@cygnus.com>.
3
4    This file is part of GNU CC.
5
6    GNU CC is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GNU CC is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GNU CC; see the file COPYING.  If not, write to
18    the Free Software Foundation, 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 /* As a special exception, if you link this library with other files,
22    some of which are compiled with GCC, to produce an executable,
23    this library does not by itself cause the resulting executable
24    to be covered by the GNU General Public License.
25    This exception does not however invalidate any other reasons why
26    the executable file might be covered by the GNU General Public License.  */
27
28 /* Locate the FDE entry for a given address, using glibc ld.so routines
29    to avoid register/deregister calls at DSO load/unload.  */
30
31 #include <stdlib.h>
32 #include <link.h>
33 #include <bits/libc-lock.h>
34 #include "unwind-ia64.h"
35
36
37 /* Initialized by crtbegin from the main application.  */
38 extern Elf64_Ehdr *__ia64_app_header;
39
40 /* ??? A redeclaration of the lock in ld.so.  Perhaps this should
41    appear in <link.h> in a new glibc version.  */
42 __libc_lock_define (extern, _dl_load_lock)
43
44 /* This always exists, even in a static application.  */
45 extern struct link_map *_dl_loaded;
46
47 static struct unw_table_entry *
48 find_fde_for_dso (Elf64_Addr pc, Elf64_Ehdr *ehdr,
49                   unsigned long *pseg_base, unsigned long *pgp)
50 {
51   Elf64_Phdr *phdr, *p_unwind, *p_dynamic;
52   long n, match;
53   Elf64_Addr load_base, seg_base;
54   struct unw_table_entry *f_base, *f;
55   size_t lo, hi;
56
57   /* Verify that we are looking at an ELF header.  */
58   if (ehdr->e_ident[0] != 0x7f
59       || ehdr->e_ident[1] != 'E'
60       || ehdr->e_ident[2] != 'L'
61       || ehdr->e_ident[3] != 'F'
62       || ehdr->e_ident[EI_CLASS] != ELFCLASS64
63       || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
64       || ehdr->e_machine != EM_IA_64)
65     abort ();
66
67   match = 0;
68   phdr = (Elf64_Phdr *)((char *)ehdr + ehdr->e_phoff);
69   load_base = (ehdr->e_type == ET_DYN ? (Elf64_Addr)ehdr : 0);
70   p_unwind = NULL;
71   p_dynamic = NULL;
72
73   /* See if PC falls into one of the loaded segments.  Find the unwind
74      segment at the same time.  */
75   for (n = ehdr->e_phnum; --n >= 0; phdr++)
76     {
77       if (phdr->p_type == PT_LOAD)
78         {
79           Elf64_Addr vaddr = phdr->p_vaddr + load_base;
80           if (pc >= vaddr && pc < vaddr + phdr->p_memsz)
81             match = 1;
82         }
83       else if (phdr->p_type == PT_IA_64_UNWIND)
84         p_unwind = phdr;
85       else if (phdr->p_type == PT_DYNAMIC)
86         p_dynamic = phdr;
87     }
88   if (!match || !p_unwind)
89     return NULL;
90
91   /* Search for the FDE within the unwind segment.  */
92
93   f_base = (struct unw_table_entry *) (p_unwind->p_vaddr + load_base);
94   seg_base = (Elf64_Addr) ehdr;
95   lo = 0;
96   hi = p_unwind->p_memsz / sizeof (struct unw_table_entry);
97
98   while (lo < hi)
99     {
100       size_t mid = (lo + hi) / 2;
101
102       f = f_base + mid;
103       if (pc < f->start_offset + seg_base)
104         hi = mid;
105       else if (pc >= f->end_offset + seg_base)
106         lo = mid + 1;
107       else
108         goto found;
109     }
110   return NULL;
111
112  found:
113   *pseg_base = seg_base;
114   *pgp = 0;
115
116   if (p_dynamic)
117     {
118       /* For dynamicly linked executables and shared libraries,
119          DT_PLTGOT is the gp value for that object.  */
120       Elf64_Dyn *dyn = (Elf64_Dyn *)(p_dynamic->p_vaddr + load_base);
121       for (; dyn->d_tag != DT_NULL ; dyn++)
122         if (dyn->d_tag == DT_PLTGOT)
123           {
124             /* ??? Glibc seems to have relocated this already.  */
125             *pgp = dyn->d_un.d_ptr;
126             break;
127           }
128     }
129   else
130     {
131       /* Otherwise this is a static executable with no _DYNAMIC.
132          The gp is constant program-wide.  */
133       register unsigned long gp __asm__("gp");
134       *pgp = gp;
135     }
136
137   return f;
138 }
139
140 /* Return a pointer to the unwind table entry for the function
141    containing PC.  */
142
143 struct unw_table_entry *
144 _Unwind_FindTableEntry (void *pc, unsigned long *segment_base,
145                         unsigned long *gp)
146 {
147   struct unw_table_entry *ret;
148   struct link_map *map;
149
150   /* Check the main application first, hoping that most of the user's
151      code is there instead of in some library.  */
152   ret = find_fde_for_dso ((Elf64_Addr)pc, __ia64_app_header,
153                           segment_base, gp);
154   if (ret)
155     return ret;
156
157   /* Glibc is probably unique in that we can (with certain restrictions)
158      dynamicly load libraries into staticly linked applications.  Thus
159      we _always_ check _dl_loaded.  */
160
161   __libc_lock_lock (_dl_load_lock);
162
163   for (map = _dl_loaded; map ; map = map->l_next)
164     {
165       /* Skip the main application's entry.  */
166       if (map->l_name[0] == 0)
167         continue;
168       ret = find_fde_for_dso ((Elf64_Addr)pc, (Elf64_Ehdr *)map->l_addr,
169                               segment_base, gp);
170       if (ret)
171         break;
172     }
173
174   __libc_lock_unlock (_dl_load_lock);
175
176   return ret;
177 }