]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/gcc/config/ia64/fde-glibc.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / gcc / config / ia64 / fde-glibc.c
1 /* Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@cygnus.com>.
3
4    This file is part of GCC.
5
6    GCC 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    GCC 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 GCC; see the file COPYING.  If not, write to
18    the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, 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 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE 1
33 #endif
34 #include "config.h"
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <link.h>
38 #include "unwind-ia64.h"
39
40 #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 2) \
41     || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 2 && !defined(DT_CONFIG))
42 # error You need GLIBC 2.2.4 or later on IA-64 Linux
43 #endif
44
45 struct unw_ia64_callback_data
46 {
47   Elf64_Addr pc;
48   unsigned long *segment_base;
49   unsigned long *gp;
50   struct unw_table_entry *ret;
51 };
52
53 static int
54 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
55 {
56   struct unw_ia64_callback_data *data = (struct unw_ia64_callback_data *) ptr;
57   const Elf64_Phdr *phdr, *p_unwind, *p_dynamic;
58   long n, match;
59   Elf64_Addr load_base, seg_base;
60   struct unw_table_entry *f_base, *f;
61   size_t lo, hi;
62
63   /* Make sure struct dl_phdr_info is at least as big as we need.  */
64   if (size < offsetof (struct dl_phdr_info, dlpi_phnum)
65              + sizeof (info->dlpi_phnum))
66     return -1;
67
68   match = 0;
69   phdr = info->dlpi_phdr;
70   load_base = info->dlpi_addr;
71   p_unwind = NULL;
72   p_dynamic = NULL;
73   seg_base = ~(Elf64_Addr) 0;
74
75   /* See if PC falls into one of the loaded segments.  Find the unwind
76      segment at the same time.  */
77   for (n = info->dlpi_phnum; --n >= 0; phdr++)
78     {
79       if (phdr->p_type == PT_LOAD)
80         {
81           Elf64_Addr vaddr = phdr->p_vaddr + load_base;
82           if (data->pc >= vaddr && data->pc < vaddr + phdr->p_memsz)
83             match = 1;
84           if (vaddr < seg_base)
85             seg_base = vaddr;
86         }
87       else if (phdr->p_type == PT_IA_64_UNWIND)
88         p_unwind = phdr;
89       else if (phdr->p_type == PT_DYNAMIC)
90         p_dynamic = phdr;
91     }
92   if (!match || !p_unwind)
93     return 0;
94
95   /* Search for the FDE within the unwind segment.  */
96
97   f_base = (struct unw_table_entry *) (p_unwind->p_vaddr + load_base);
98   lo = 0;
99   hi = p_unwind->p_memsz / sizeof (struct unw_table_entry);
100
101   while (lo < hi)
102     {
103       size_t mid = (lo + hi) / 2;
104
105       f = f_base + mid;
106       if (data->pc < f->start_offset + seg_base)
107         hi = mid;
108       else if (data->pc >= f->end_offset + seg_base)
109         lo = mid + 1;
110       else
111         goto found;
112     }
113   /* No need to search for further libraries when we know pc is contained
114      in this library.  */
115   return 1;
116
117  found:
118   *data->segment_base = seg_base;
119   *data->gp = 0;
120   data->ret = f;
121
122   if (p_dynamic)
123     {
124       /* For dynamically linked executables and shared libraries,
125          DT_PLTGOT is the gp value for that object.  */
126       Elf64_Dyn *dyn = (Elf64_Dyn *)(p_dynamic->p_vaddr + load_base);
127       for (; dyn->d_tag != DT_NULL ; dyn++)
128         if (dyn->d_tag == DT_PLTGOT)
129           {
130             /* On IA-64, _DYNAMIC is writable and GLIBC has relocated it.  */
131             *data->gp = dyn->d_un.d_ptr;
132             break;
133           }
134     }
135   else
136     {
137       /* Otherwise this is a static executable with no _DYNAMIC.
138          The gp is constant program-wide.  */
139       register unsigned long gp __asm__("gp");
140       *data->gp = gp;
141     }
142
143   return 1;
144 }
145
146 /* Return a pointer to the unwind table entry for the function
147    containing PC.  */
148
149 struct unw_table_entry *
150 _Unwind_FindTableEntry (void *pc, unsigned long *segment_base,
151                         unsigned long *gp)
152 {
153   struct unw_ia64_callback_data data;
154
155   data.pc = (Elf64_Addr) pc;
156   data.segment_base = segment_base;
157   data.gp = gp;
158   data.ret = NULL;
159
160   if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
161     return NULL;
162
163   return data.ret;
164 }