Update.
[platform/upstream/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, lazy) \
85   do {                                                                        \
86     ElfW(Addr) r_addr, r_size, p_addr, p_size;                                \
87     if ((map)->l_info[DT_##RELOC])                                            \
88       {                                                                       \
89         r_addr = (map)->l_info[DT_##RELOC]->d_un.d_ptr;                       \
90         r_size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;                   \
91         if ((map)->l_info[DT_PLTREL] &&                                       \
92             (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)               \
93           {                                                                   \
94             p_addr = (map)->l_info[DT_JMPREL]->d_un.d_ptr;                    \
95             p_size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;                  \
96             if (r_addr <= p_addr && r_addr+r_size > p_addr)                   \
97               {                                                               \
98                 ElfW(Addr) r2_addr, r2_size;                                  \
99                 r2_addr = p_addr + p_size;                                    \
100                 if (r2_addr < r_addr + r_size)                                \
101                   {                                                           \
102                     r2_size = r_addr + r_size - r2_addr;                      \
103                     elf_dynamic_do_##reloc ((map), r2_addr, r2_size, 0);      \
104                   }                                                           \
105                 r_size = p_addr - r_addr;                                     \
106               }                                                               \
107           }                                                                   \
108                                                                               \
109         elf_dynamic_do_##reloc ((map), r_addr, r_size, 0);                    \
110         if (p_addr)                                                           \
111           elf_dynamic_do_##reloc ((map), p_addr, p_size, (lazy));             \
112       }                                                                       \
113     else if ((map)->l_info[DT_PLTREL] &&                                      \
114              (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)              \
115       {                                                                       \
116         p_addr = (map)->l_info[DT_JMPREL]->d_un.d_ptr;                        \
117         p_size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;                      \
118                                                                               \
119         elf_dynamic_do_##reloc ((map), p_addr, p_size, (lazy));               \
120       }                                                                       \
121   } while (0)
122 #else
123 #define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, lazy) \
124   do {                                                                        \
125     if ((map)->l_info[DT_##RELOC])                                            \
126       {                                                                       \
127         ElfW(Addr) r_addr, r_size;                                            \
128         r_addr = (map)->l_info[DT_##RELOC]->d_un.d_ptr;                       \
129         r_size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;                   \
130         elf_dynamic_do_##reloc ((map), r_addr, r_size, 0);                    \
131       }                                                                       \
132     if ((map)->l_info[DT_PLTREL] &&                                           \
133         (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)                   \
134       {                                                                       \
135         ElfW(Addr) p_addr, p_size;                                            \
136         p_addr = (map)->l_info[DT_JMPREL]->d_un.d_ptr;                        \
137         p_size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;                      \
138         elf_dynamic_do_##reloc ((map), p_addr, p_size, (lazy));               \
139       }                                                                       \
140   } while (0)
141 #endif
142
143 #if ! ELF_MACHINE_NO_REL
144 #include "do-rel.h"
145 #define ELF_DYNAMIC_DO_REL(map, lazy) \
146   _ELF_DYNAMIC_DO_RELOC (REL, rel, map, lazy)
147 #else
148 #define ELF_DYNAMIC_DO_REL(map, lazy) /* Nothing to do.  */
149 #endif
150
151 #if ! ELF_MACHINE_NO_RELA
152 #define DO_RELA
153 #include "do-rel.h"
154 #define ELF_DYNAMIC_DO_RELA(map, lazy) \
155   _ELF_DYNAMIC_DO_RELOC (RELA, rela, map, lazy)
156 #else
157 #define ELF_DYNAMIC_DO_RELA(map, lazy) /* Nothing to do.  */
158 #endif
159
160 /* This can't just be an inline function because GCC is too dumb
161    to inline functions containing inlines themselves.  */
162 #define ELF_DYNAMIC_RELOCATE(map, lazy, consider_profile) \
163   do {                                                                        \
164     int edr_lazy = elf_machine_runtime_setup ((map), (lazy),                  \
165                                               (consider_profile));            \
166     ELF_DYNAMIC_DO_REL ((map), edr_lazy);                                     \
167     ELF_DYNAMIC_DO_RELA ((map), edr_lazy);                                    \
168   } while (0)
169
170 #endif