PR c++/24580
[official-gcc.git] / gcc / config / ia64 / fde-glibc.c
blob7506c17c2de315324562465af0f6692503bf6b03
1 /* Copyright (C) 2000, 2001, 2003 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@cygnus.com>.
4 This file is part of GCC.
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.
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.
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. */
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. */
28 /* Locate the FDE entry for a given address, using glibc ld.so routines
29 to avoid register/deregister calls at DSO load/unload. */
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"
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
45 struct unw_ia64_callback_data
47 Elf64_Addr pc;
48 unsigned long *segment_base;
49 unsigned long *gp;
50 struct unw_table_entry *ret;
53 static int
54 _Unwind_IteratePhdrCallback (struct dl_phdr_info *info, size_t size, void *ptr)
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;
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;
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;
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++)
79 if (phdr->p_type == PT_LOAD)
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;
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;
92 if (!match || !p_unwind)
93 return 0;
95 /* Search for the FDE within the unwind segment. */
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);
101 while (lo < hi)
103 size_t mid = (lo + hi) / 2;
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;
113 /* No need to search for further libraries when we know pc is contained
114 in this library. */
115 return 1;
117 found:
118 *data->segment_base = seg_base;
119 *data->gp = 0;
120 data->ret = f;
122 if (p_dynamic)
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)
130 /* On IA-64, _DYNAMIC is writable and GLIBC has relocated it. */
131 *data->gp = dyn->d_un.d_ptr;
132 break;
135 else
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;
143 return 1;
146 /* Return a pointer to the unwind table entry for the function
147 containing PC. */
149 struct unw_table_entry *
150 _Unwind_FindTableEntry (void *pc, unsigned long *segment_base,
151 unsigned long *gp)
153 struct unw_ia64_callback_data data;
155 data.pc = (Elf64_Addr) pc;
156 data.segment_base = segment_base;
157 data.gp = gp;
158 data.ret = NULL;
160 if (dl_iterate_phdr (_Unwind_IteratePhdrCallback, &data) < 0)
161 return NULL;
163 return data.ret;