Use MAP_ANON instead of MAP_ANONYMOUS.
[kopensolaris-gnu/glibc.git] / elf / dynamic-link.h
1 /* Inline functions for dynamic linking.
2    Copyright (C) 1995, 1996, 1997, 1998 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 Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <elf.h>
21 #include <dl-machine.h>
22 #include <assert.h>
23
24
25 /* Global read-only variable defined in rtld.c which is nonzero if we
26    shall give more warning messages.  */
27 extern int _dl_verbose __attribute__ ((unused));
28
29
30 /* Read the dynamic section at DYN and fill in INFO with indices DT_*.  */
31
32 static inline void __attribute__ ((unused))
33 elf_get_dynamic_info (ElfW(Dyn) *dyn,
34                       ElfW(Dyn) *info[DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM
35                                      + DT_EXTRANUM])
36 {
37   unsigned int i;
38
39   for (i = 0; i < DT_NUM + DT_PROCNUM + DT_VERSIONTAGNUM + DT_EXTRANUM; ++i)
40     info[i] = NULL;
41
42   if (! dyn)
43     return;
44
45   while (dyn->d_tag != DT_NULL)
46     {
47       if (dyn->d_tag < DT_NUM)
48         info[dyn->d_tag] = dyn;
49       else if (dyn->d_tag >= DT_LOPROC &&
50                dyn->d_tag < DT_LOPROC + DT_PROCNUM)
51         info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
52       else if ((Elf32_Word) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM)
53         info[DT_VERSIONTAGIDX (dyn->d_tag) + DT_NUM + DT_PROCNUM] = dyn;
54       else if ((Elf32_Word) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM)
55         info[DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_PROCNUM
56              + DT_VERSIONTAGNUM] = dyn;
57       else
58         assert (! "bad dynamic tag");
59       ++dyn;
60     }
61
62   if (info[DT_RELA])
63     assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
64   if (info[DT_REL])
65     assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
66   if (info[DT_PLTREL])
67     assert (info[DT_PLTREL]->d_un.d_val == DT_REL ||
68             info[DT_PLTREL]->d_un.d_val == DT_RELA);
69 }
70
71 #ifdef RESOLVE
72
73 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
74    These functions are almost identical, so we use cpp magic to avoid
75    duplicating their code.  It cannot be done in a more general function
76    because we must be able to completely inline.  */
77
78 /* On some machines, notably Sparc, DT_REL* includes DT_JMPREL in its
79    range.  Note that according to the ELF spec, this is completely legal!
80    But conditionally define things so that on machines we know this will
81    not happen we do something more optimal.  */
82
83 #ifdef ELF_MACHINE_PLTREL_OVERLAP
84 #define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy) \
85   do {                                                                        \
86     struct { ElfW(Addr) start, size; int lazy; } ranges[3];                   \
87     int ranges_index;                                                         \
88                                                                               \
89     ranges[0].lazy = ranges[2].lazy = 0;                                      \
90     ranges[1].lazy = 1;                                                       \
91     ranges[0].size = ranges[1].size = ranges[2].size = 0;                     \
92                                                                               \
93     if ((map)->l_info[DT_##RELOC])                                            \
94       {                                                                       \
95         ranges[0].start = (map)->l_info[DT_##RELOC]->d_un.d_ptr;              \
96         ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;           \
97       }                                                                       \
98                                                                               \
99      if ((do_lazy)                                                            \
100         && (map)->l_info[DT_PLTREL]                                           \
101         && (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)                \
102       {                                                                       \
103         ranges[1].start = (map)->l_info[DT_JMPREL]->d_un.d_ptr;               \
104         ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;              \
105         ranges[2].start = ranges[1].start + ranges[1].size;                   \
106         ranges[2].size = ranges[0].start + ranges[0].size - ranges[2].start;  \
107         ranges[0].size = ranges[1].start - ranges[0].start;                   \
108       }                                                                       \
109                                                                               \
110     for (ranges_index = 0; ranges_index < 3; ++ranges_index)                  \
111       elf_dynamic_do_##reloc ((map),                                          \
112                               ranges[ranges_index].start,                     \
113                               ranges[ranges_index].size,                      \
114                               ranges[ranges_index].lazy);                     \
115   } while (0)
116 #else
117 #define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, do_lazy) \
118   do {                                                                        \
119     struct { ElfW(Addr) start, size; int lazy; } ranges[2];                   \
120     int ranges_index;                                                         \
121     ranges[0].lazy = 0;                                                       \
122     ranges[0].size = ranges[1].size = 0;                                      \
123     ranges[0].start = 0;                                                      \
124                                                                               \
125     if ((map)->l_info[DT_##RELOC])                                            \
126       {                                                                       \
127         ranges[0].start = (map)->l_info[DT_##RELOC]->d_un.d_ptr;              \
128         ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;           \
129       }                                                                       \
130     if ((map)->l_info[DT_PLTREL] &&                                           \
131         (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)                   \
132       {                                                                       \
133         ElfW(Addr) start = (map)->l_info[DT_JMPREL]->d_un.d_ptr;              \
134                                                                               \
135         if ((do_lazy)                                                         \
136             /* This test does not only detect whether the relocation          \
137                sections are in the right order, it also checks whether        \
138                there is a DT_REL/DT_RELA section.  */                         \
139             || ranges[0].start + ranges[0].size != start)                     \
140           {                                                                   \
141             ranges[1].start = start;                                          \
142             ranges[1].size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;          \
143             ranges[1].lazy = (do_lazy);                                       \
144           }                                                                   \
145         else                                                                  \
146           /* Combine processing the sections.  */                             \
147           ranges[0].size += (map)->l_info[DT_PLTRELSZ]->d_un.d_val;           \
148       }                                                                       \
149                                                                               \
150     for (ranges_index = 0; ranges_index < 2; ++ranges_index)                  \
151       elf_dynamic_do_##reloc ((map),                                          \
152                               ranges[ranges_index].start,                     \
153                               ranges[ranges_index].size,                      \
154                               ranges[ranges_index].lazy);                     \
155   } while (0)
156 #endif
157
158 #if ! ELF_MACHINE_NO_REL
159 #include "do-rel.h"
160 #define ELF_DYNAMIC_DO_REL(map, lazy) \
161   _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy)
162 #else
163 #define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do.  */
164 #endif
165
166 #if ! ELF_MACHINE_NO_RELA
167 #define DO_RELA
168 #include "do-rel.h"
169 #define ELF_DYNAMIC_DO_RELA(map, lazy) \
170   _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy)
171 #else
172 #define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do.  */
173 #endif
174
175 /* This can't just be an inline function because GCC is too dumb
176    to inline functions containing inlines themselves.  */
177 #define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
178   do {                                                                        \
179     int edr_lazy = elf_machine_runtime_setup ((map), (lazy),                  \
180                                               (consider_profile));            \
181     ELF_DYNAMIC_DO_REL ((map), edr_lazy);                                     \
182     ELF_DYNAMIC_DO_RELA ((map), edr_lazy);                                    \
183   } while (0)
184
185 #endif