]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/ia64/fde-glibc.c
* gcc.c (default_compilers) [@cpp-output]: Add -fpreprocessed.
[thirdparty/gcc.git] / gcc / config / ia64 / fde-glibc.c
CommitLineData
fee0225a
RH
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 "frame-ia64.h"
35
36
37/* Initialized by crtbegin from the main application. */
38extern 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/* ??? _dl_load_lock is not exported from glibc 2.1, but it is
45 from glibc 2.2. Remove this when folks have migrated. */
46#pragma weak _dl_load_lock
47
48/* This always exists, even in a static application. */
49extern struct link_map *_dl_loaded;
50
51static fde *
52find_fde_for_dso (Elf64_Addr pc, Elf64_Ehdr *ehdr)
53{
54 Elf64_Phdr *phdr, *p_unwind;
55 long n, match;
56 Elf64_Addr load_base, seg_base;
57 fde *f;
58
59 /* Verify that we are looking at an ELF header. */
60 if (ehdr->e_ident[0] != 0x7f
61 || ehdr->e_ident[1] != 'E'
62 || ehdr->e_ident[2] != 'L'
63 || ehdr->e_ident[3] != 'F'
64 || ehdr->e_ident[EI_CLASS] != ELFCLASS64
65 || ehdr->e_ident[EI_DATA] != ELFDATA2LSB
66 || ehdr->e_machine != EM_IA_64)
67 abort ();
68
69 match = 0;
70 phdr = (Elf64_Phdr *)((char *)ehdr + ehdr->e_phoff);
71 load_base = (ehdr->e_type == ET_DYN ? (Elf64_Addr)ehdr : 0);
72 p_unwind = NULL;
73
74 /* See if PC falls into one of the loaded segments. Find the unwind
75 segment at the same time. */
76 for (n = ehdr->e_phnum; --n >= 0; phdr++)
77 {
78 if (phdr->p_type == PT_LOAD)
79 {
80 Elf64_Addr vaddr = phdr->p_vaddr + load_base;
81 if (pc >= vaddr && pc < vaddr + phdr->p_memsz)
82 match = 1;
83 }
84 else if (phdr->p_type == PT_IA_64_UNWIND)
85 p_unwind = phdr;
86 }
87 if (!match || !p_unwind)
88 return NULL;
89
90 /* Search for the FDE within the unwind segment. */
91 /* ??? Ideally ld would have sorted this for us by address. Until
92 that's fixed, we must do a linear search. */
93
94 f = (fde *) (p_unwind->p_vaddr + load_base);
95 seg_base = (Elf64_Addr) ehdr;
96 for (n = p_unwind->p_memsz / sizeof (fde); --n >= 0; ++f)
97 if (pc >= f->start_offset + seg_base && pc < f->end_offset + seg_base)
98 return f;
99
100 return NULL;
101}
102
103/* Return a pointer to the FDE for the function containing PC. */
104fde *
105__ia64_find_fde (void *pc, void **pc_base)
106{
107 fde *ret;
108 struct link_map *map;
109
110 /* Check the main application first, hoping that most of the user's
111 code is there instead of in some library. */
112 ret = find_fde_for_dso ((Elf64_Addr)pc, __ia64_app_header);
113 if (ret)
114 {
115 *pc_base = __ia64_app_header;
116 return ret;
117 }
118
119 /* Glibc is probably unique in that we can (with certain restrictions)
120 dynamicly load libraries into staticly linked applications. Thus
121 we _always_ check _dl_loaded. */
122
123 if (&_dl_load_lock)
124 __libc_lock_lock (_dl_load_lock);
125
126 for (map = _dl_loaded; map ; map = map->l_next)
127 {
128 /* Skip the main application's entry. */
129 if (map->l_name[0] == 0)
130 continue;
131 ret = find_fde_for_dso ((Elf64_Addr)pc, (Elf64_Ehdr *)map->l_addr);
132 if (ret)
133 break;
134 }
135
136 if (&_dl_load_lock)
137 __libc_lock_unlock (_dl_load_lock);
138
139 *pc_base = (void *)(map ? map->l_addr : 0);
140 return ret;
141}