OSDN Git Service

2003-05-28 Jeff Johnston <jjohnstn@redhat.com>
[pf3gnuchains/pf3gnuchains4x.git] / newlib / libc / sys / linux / dl / dynamic-link.h
1 /* Inline functions for dynamic linking.
2    Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <elf.h>
21 #include <machine/dl-machine.h>
22 #include <assert.h>
23
24 #ifndef VERSYMIDX
25 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
26 #endif
27
28
29 /* Global read-only variable defined in rtld.c which is nonzero if we
30    shall give more warning messages.  */
31 extern int _dl_verbose __attribute__ ((unused));
32
33
34 /* Read the dynamic section at DYN and fill in INFO with indices DT_*.  */
35
36 static inline void __attribute__ ((unused))
37 elf_get_dynamic_info (struct link_map *l)
38 {
39   ElfW(Dyn) *dyn = l->l_ld;
40   ElfW(Addr) l_addr;
41   ElfW(Dyn) **info;
42
43   if (! dyn)
44     return;
45
46   l_addr = l->l_addr;
47   info = l->l_info;
48
49   while (dyn->d_tag != DT_NULL)
50     {
51       if (dyn->d_tag < DT_NUM)
52         info[dyn->d_tag] = dyn;
53       else if (dyn->d_tag >= DT_LOPROC &&
54                dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
55         info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
56       else if ((Elf32_Word) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
57         info[VERSYMIDX (dyn->d_tag)] = dyn;
58       else if ((Elf32_Word) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
59         info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
60              + DT_VERSIONTAGNUM] = dyn;
61       else
62         assert (! "bad dynamic tag");
63       ++dyn;
64     }
65 #ifndef DL_RO_DYN_SECTION
66   if (info[DT_PLTGOT] != NULL)
67     info[DT_PLTGOT]->d_un.d_ptr += l_addr;
68   if (info[DT_STRTAB] != NULL)
69     info[DT_STRTAB]->d_un.d_ptr += l_addr;
70   if (info[DT_SYMTAB] != NULL)
71     info[DT_SYMTAB]->d_un.d_ptr += l_addr;
72 # if ! ELF_MACHINE_NO_RELA
73   if (info[DT_RELA] != NULL)
74     {
75       assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
76       info[DT_RELA]->d_un.d_ptr += l_addr;
77     }
78 # endif
79 # if ! ELF_MACHINE_NO_REL
80   if (info[DT_REL] != NULL)
81     {
82       assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
83       info[DT_REL]->d_un.d_ptr += l_addr;
84     }
85 # endif
86 #endif
87   if (info[DT_PLTREL] != NULL)
88     {
89 # if ELF_MACHINE_NO_RELA
90       assert (info[DT_PLTREL]->d_un.d_val == DT_REL);
91 # elif ELF_MACHINE_NO_REL
92       assert (info[DT_PLTREL]->d_un.d_val == DT_RELA);
93 # else
94       assert (info[DT_PLTREL]->d_un.d_val == DT_REL
95               || info[DT_PLTREL]->d_un.d_val == DT_RELA);
96 # endif
97     }
98 #ifndef DL_RO_DYN_SECTION
99   if (info[DT_JMPREL] != NULL)
100     info[DT_JMPREL]->d_un.d_ptr += l_addr;
101   if (info[VERSYMIDX (DT_VERSYM)] != NULL)
102     info[VERSYMIDX (DT_VERSYM)]->d_un.d_ptr += l_addr;
103 #endif
104   if (info[DT_FLAGS] != NULL)
105     {
106       /* Flags are used.  Translate to the old form where available.
107          Since these l_info entries are only tested for NULL pointers it
108          is ok if they point to the DT_FLAGS entry.  */
109       ElfW(Word) flags = info[DT_FLAGS]->d_un.d_val;
110       if (flags & DF_SYMBOLIC)
111         info[DT_SYMBOLIC] = info[DT_FLAGS];
112       if (flags & DF_TEXTREL)
113         info[DT_TEXTREL] = info[DT_FLAGS];
114       if (flags & DF_BIND_NOW)
115         info[DT_BIND_NOW] = info[DT_FLAGS];
116     }
117   if (info[VERSYMIDX (DT_FLAGS_1)] != NULL)
118     l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val;
119   if (info[DT_RUNPATH] != NULL)
120     /* If both RUNPATH and RPATH are given, the latter is ignored.  */
121     info[DT_RPATH] = NULL;
122 }
123
124 #ifdef RESOLVE
125
126 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
127    These functions are almost identical, so we use cpp magic to avoid
128    duplicating their code.  It cannot be done in a more general function
129    because we must be able to completely inline.  */
130
131 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
132    range.  Note that according to the ELF spec, this is completely legal!
133    But conditionally define things so that on machines we know this will
134    not happen we do something more optimal.  */
135
136 # ifdef ELF_MACHINE_PLTREL_OVERLAP
137 #  define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
138   do {                                                                        \
139     struct { ElfW(Addr) start, size; int lazy; } ranges[3];                   \
140     int ranges_index;                                                         \
141                                                                               \
142     ranges[0].lazy = ranges[2].lazy = 0;                                      \
143     ranges[1].lazy = 1;                                                       \
144     ranges[0].size = ranges[1].size = ranges[2].size = 0;                     \
145                                                                               \
146     if ((map)->l_info[DT_##RELOC])                                            \
147       {                                                                       \
148         ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]);                  \
149         ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;           \
150       }                                                                       \
151                                                                               \
152      if ((do_lazy)                                                            \
153         && (map)->l_info[DT_PLTREL]                                           \
154         && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
155       {                                                                       \
156         ranges[1].start = D_PTR ((map), l_info[DT_JMPREL]);                   \
157         ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;              \
158         ranges[2].start = ranges[1].start + ranges[1].size;                   \
159         ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start;  \
160         ranges[0].size = ranges[1].start - ranges[0].start;                   \
161       }                                                                       \
162                                                                               \
163     for (ranges_index = 0; ranges_index < 3; ++ranges_index)                  \
164       elf_dynamic_do_##reloc ((map),                                          \
165                               ranges[ranges_index].start,                     \
166                               ranges[ranges_index].size,                      \
167                               ranges[ranges_index].lazy);                     \
168   } while (0)
169 # else
170 #  define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy, test_rel) \
171   do {                                                                        \
172     struct { ElfW(Addr) start, size; int lazy; } ranges[2];                   \
173     int ranges_index;                                                         \
174     ranges[0].lazy = 0;                                                       \
175     ranges[0].size = ranges[1].size = 0;                                      \
176     ranges[0].start = 0;                                                      \
177                                                                               \
178     if ((map)->l_info[DT_##RELOC])                                            \
179       {                                                                       \
180         ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]);                  \
181         ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;           \
182       }                                                                       \
183     if ((map)->l_info[DT_PLTREL]                                              \
184         && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \
185       {                                                                       \
186         ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]);                  \
187                                                                               \
188         if ((do_lazy)                                                         \
189             /* This test does not only detect whether the relocation          \
190                sections are in the right order, it also checks whether        \
191                there is a DT_REL/DT_RELA section.  */                         \
192             || ranges[0].start + ranges[0].size != start)                     \
193           {                                                                   \
194             ranges[1].start = start;                                          \
195             ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;          \
196             ranges[1].lazy = (do_lazy);                                       \
197           }                                                                   \
198         else                                                                  \
199           /* Combine processing the sections.  */                             \
200           ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val;           \
201       }                                                                       \
202                                                                               \
203     for (ranges_index = 0; ranges_index < 2; ++ranges_index)                  \
204       elf_dynamic_do_##reloc ((map),                                          \
205                               ranges[ranges_index].start,                     \
206                               ranges[ranges_index].size,                      \
207                               ranges[ranges_index].lazy);                     \
208   } while (0)
209 # endif
210
211 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
212 #  define _ELF_CHECK_REL 0
213 # else
214 #  define _ELF_CHECK_REL 1
215 # endif
216
217 # if ! ELF_MACHINE_NO_REL
218 #  include "do-rel.h"
219 #  define ELF_DYNAMIC_DO_REL(map, lazy) \
220   _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy, _ELF_CHECK_REL)
221 # else
222 #  define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do.  */
223 # endif
224
225 # if ! ELF_MACHINE_NO_RELA
226 #  define DO_RELA
227 #  include "do-rel.h"
228 #  define ELF_DYNAMIC_DO_RELA(map, lazy) \
229   _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy, _ELF_CHECK_REL)
230 # else
231 #  define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do.  */
232 # endif
233
234 /* This can't just be an inline function because GCC is too dumb
235    to inline functions containing inlines themselves.  */
236 # define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
237   do {                                                                        \
238     int edr_lazy = elf_machine_runtime_setup ((map), (lazy),                  \
239                                               (consider_profile));            \
240     ELF_DYNAMIC_DO_REL ((map), edr_lazy);                                     \
241     ELF_DYNAMIC_DO_RELA ((map), edr_lazy);                                    \
242   } while (0)
243
244 #endif