1 /* Copyright (C) 2000 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@cygnus.com>.
4 This file is part of GNU CC.
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)
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.
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. */
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. */
33 #include <bits/libc-lock.h>
34 #include "unwind-ia64.h"
37 /* Initialized by crtbegin from the main application. */
38 extern Elf64_Ehdr *__ia64_app_header;
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)
44 /* This always exists, even in a static application. */
45 extern struct link_map *_dl_loaded;
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)
51 Elf64_Phdr *phdr, *p_unwind, *p_dynamic;
53 Elf64_Addr load_base, seg_base;
54 struct unw_table_entry *f_base, *f;
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)
68 phdr = (Elf64_Phdr *)((char *)ehdr + ehdr->e_phoff);
69 load_base = (ehdr->e_type == ET_DYN ? (Elf64_Addr)ehdr : 0);
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++)
77 if (phdr->p_type == PT_LOAD)
79 Elf64_Addr vaddr = phdr->p_vaddr + load_base;
80 if (pc >= vaddr && pc < vaddr + phdr->p_memsz)
83 else if (phdr->p_type == PT_IA_64_UNWIND)
85 else if (phdr->p_type == PT_DYNAMIC)
88 if (!match || !p_unwind)
91 /* Search for the FDE within the unwind segment. */
93 f_base = (struct unw_table_entry *) (p_unwind->p_vaddr + load_base);
94 seg_base = (Elf64_Addr) ehdr;
96 hi = p_unwind->p_memsz / sizeof (struct unw_table_entry);
100 size_t mid = (lo + hi) / 2;
103 if (pc < f->start_offset + seg_base)
105 else if (pc >= f->end_offset + seg_base)
113 *pseg_base = seg_base;
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)
124 /* ??? Glibc seems to have relocated this already. */
125 *pgp = dyn->d_un.d_ptr;
131 /* Otherwise this is a static executable with no _DYNAMIC.
132 The gp is constant program-wide. */
133 register unsigned long gp __asm__("gp");
140 /* Return a pointer to the unwind table entry for the function
143 struct unw_table_entry *
144 _Unwind_FindTableEntry (void *pc, unsigned long *segment_base,
147 struct unw_table_entry *ret;
148 struct link_map *map;
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,
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. */
161 __libc_lock_lock (_dl_load_lock);
163 for (map = _dl_loaded; map ; map = map->l_next)
165 /* Skip the main application's entry. */
166 if (map->l_name[0] == 0)
168 ret = find_fde_for_dso ((Elf64_Addr)pc, (Elf64_Ehdr *)map->l_addr,
174 __libc_lock_unlock (_dl_load_lock);