]>
Commit | Line | Data |
---|---|---|
6ade1673 ILT |
1 | /* ldctor.c -- constructor support routines |
2 | Copyright (C) 1991, 92, 93, 94 Free Software Foundation, Inc. | |
3 | By Steve Chamberlain <[email protected]> | |
fcf276c4 ILT |
4 | |
5 | This file is part of GLD, the Gnu Linker. | |
6 | ||
7 | GLD is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
6ade1673 | 9 | the Free Software Foundation; either version 2, or (at your option) |
fcf276c4 ILT |
10 | any later version. |
11 | ||
12 | GLD is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GLD; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
fcf276c4 | 21 | #include "bfd.h" |
6ade1673 ILT |
22 | #include "sysdep.h" |
23 | #include "bfdlink.h" | |
24 | ||
fcf276c4 ILT |
25 | #include "ld.h" |
26 | #include "ldexp.h" | |
27 | #include "ldlang.h" | |
fcf276c4 ILT |
28 | #include "ldmisc.h" |
29 | #include "ldgram.h" | |
6ade1673 ILT |
30 | #include "ldmain.h" |
31 | #include "ldctor.h" | |
fcf276c4 | 32 | |
6ade1673 ILT |
33 | /* The list of statements needed to handle constructors. These are |
34 | invoked by the command CONSTRUCTORS in the linker script. */ | |
fcf276c4 ILT |
35 | lang_statement_list_type constructor_list; |
36 | ||
6ade1673 | 37 | /* We keep a list of these structures for each sets we build. */ |
fcf276c4 | 38 | |
6ade1673 | 39 | struct set_info |
fcf276c4 | 40 | { |
6ade1673 ILT |
41 | struct set_info *next; /* Next set. */ |
42 | struct bfd_link_hash_entry *h; /* Hash table entry. */ | |
43 | bfd_reloc_code_real_type reloc; /* Reloc to use for an entry. */ | |
44 | size_t count; /* Number of elements. */ | |
45 | struct set_element *elements; /* Elements in set. */ | |
46 | }; | |
47 | ||
48 | struct set_element | |
fcf276c4 | 49 | { |
6ade1673 ILT |
50 | struct set_element *next; /* Next element. */ |
51 | asection *section; /* Section of value. */ | |
52 | bfd_vma value; /* Value. */ | |
53 | }; | |
fcf276c4 | 54 | |
6ade1673 | 55 | /* The sets we have seen. */ |
fcf276c4 | 56 | |
6ade1673 | 57 | static struct set_info *sets; |
fcf276c4 | 58 | |
6ade1673 ILT |
59 | /* Add an entry to a set. H is the entry in the linker hash table. |
60 | RELOC is the relocation to use for an entry in the set. SECTION | |
61 | and VALUE are the value to add. This is called during the first | |
62 | phase of the link, when we are still gathering symbols together. | |
63 | We just record the information now. The ldctor_find_constructors | |
64 | function will construct the sets. */ | |
fcf276c4 ILT |
65 | |
66 | void | |
6ade1673 ILT |
67 | ldctor_add_set_entry (h, reloc, section, value) |
68 | struct bfd_link_hash_entry *h; | |
69 | bfd_reloc_code_real_type reloc; | |
70 | asection *section; | |
71 | bfd_vma value; | |
fcf276c4 | 72 | { |
6ade1673 ILT |
73 | struct set_info *p; |
74 | struct set_element *e; | |
75 | struct set_element **epp; | |
76 | ||
77 | for (p = sets; p != (struct set_info *) NULL; p = p->next) | |
78 | if (p->h == h) | |
79 | break; | |
fcf276c4 | 80 | |
6ade1673 | 81 | if (p == (struct set_info *) NULL) |
fcf276c4 | 82 | { |
6ade1673 ILT |
83 | p = (struct set_info *) xmalloc (sizeof (struct set_info)); |
84 | p->next = sets; | |
85 | sets = p; | |
86 | p->h = h; | |
87 | p->reloc = reloc; | |
88 | p->count = 0; | |
89 | p->elements = NULL; | |
90 | } | |
91 | else | |
92 | { | |
93 | if (p->reloc != reloc) | |
94 | { | |
95 | einfo ("%P%X: Different relocs used in set %s\n", h->root.string); | |
96 | return; | |
97 | } | |
98 | ||
99 | /* Don't permit a set to be constructed from different object | |
100 | file formats. The same reloc may have different results. We | |
101 | actually could sometimes handle this, but the case is | |
102 | unlikely to ever arise. Sometimes constructor symbols are in | |
103 | unusual sections, such as the absolute section--this appears | |
104 | to be the case in Linux a.out--and in such cases we just | |
105 | assume everything is OK. */ | |
106 | if (p->elements != NULL | |
107 | && section->owner != NULL | |
108 | && p->elements->section->owner != NULL | |
109 | && strcmp (bfd_get_target (section->owner), | |
110 | bfd_get_target (p->elements->section->owner)) != 0) | |
111 | { | |
112 | einfo ("%P%X: Different object file formats composing set %s\n", | |
113 | h->root.string); | |
114 | return; | |
115 | } | |
fcf276c4 | 116 | } |
fcf276c4 | 117 | |
6ade1673 ILT |
118 | e = (struct set_element *) xmalloc (sizeof (struct set_element)); |
119 | e->next = NULL; | |
120 | e->section = section; | |
121 | e->value = value; | |
fcf276c4 | 122 | |
6ade1673 ILT |
123 | for (epp = &p->elements; *epp != NULL; epp = &(*epp)->next) |
124 | ; | |
125 | *epp = e; | |
fcf276c4 | 126 | |
6ade1673 ILT |
127 | ++p->count; |
128 | } | |
fcf276c4 | 129 | |
6ade1673 ILT |
130 | /* This function is called after the first phase of the link and |
131 | before the second phase. At this point all set information has | |
132 | been gathered. We now put the statements to build the sets | |
133 | themselves into constructor_list. */ | |
fcf276c4 | 134 | |
6ade1673 ILT |
135 | void |
136 | ldctor_build_sets () | |
137 | { | |
138 | lang_statement_list_type *old; | |
139 | struct set_info *p; | |
fcf276c4 | 140 | |
6ade1673 ILT |
141 | old = stat_ptr; |
142 | stat_ptr = &constructor_list; | |
fcf276c4 | 143 | |
6ade1673 | 144 | lang_list_init (stat_ptr); |
fcf276c4 | 145 | |
6ade1673 | 146 | for (p = sets; p != (struct set_info *) NULL; p = p->next) |
fcf276c4 | 147 | { |
6ade1673 ILT |
148 | struct set_element *e; |
149 | reloc_howto_type *howto; | |
150 | int size; | |
151 | ||
152 | /* If the symbol is defined, we may have been invoked from | |
153 | collect, and the sets may already have been built, so we do | |
154 | not do anything. */ | |
155 | if (p->h->type == bfd_link_hash_defined) | |
156 | continue; | |
157 | ||
158 | /* For each set we build: | |
159 | set: | |
160 | .long number_of_elements | |
161 | .long element0 | |
162 | ... | |
163 | .long elementN | |
164 | .long 0 | |
165 | except that we use the right size instead of .long. When | |
166 | generating relocateable output, we generate relocs instead of | |
167 | addresses. */ | |
168 | howto = bfd_reloc_type_lookup (output_bfd, p->reloc); | |
169 | if (howto == (reloc_howto_type *) NULL) | |
170 | { | |
171 | if (link_info.relocateable) | |
172 | { | |
173 | einfo ("%P%X: %s does not support reloc %s for set %s\n", | |
174 | bfd_get_target (output_bfd), | |
175 | bfd_get_reloc_code_name (p->reloc), | |
176 | p->h->root.string); | |
177 | continue; | |
178 | } | |
fcf276c4 | 179 | |
6ade1673 ILT |
180 | /* If this is not a relocateable link, all we need is the |
181 | size, which we can get from the input BFD. */ | |
182 | howto = bfd_reloc_type_lookup (p->elements->section->owner, | |
183 | p->reloc); | |
184 | if (howto == NULL) | |
fcf276c4 | 185 | { |
6ade1673 ILT |
186 | einfo ("%P%X: %s does not support reloc %s for set %s\n", |
187 | bfd_get_target (p->elements->section->owner), | |
188 | bfd_get_reloc_code_name (p->reloc), | |
189 | p->h->root.string); | |
190 | continue; | |
fcf276c4 | 191 | } |
6ade1673 ILT |
192 | } |
193 | ||
194 | switch (bfd_get_reloc_size (howto)) | |
195 | { | |
196 | case 1: size = BYTE; break; | |
197 | case 2: size = SHORT; break; | |
198 | case 4: size = LONG; break; | |
199 | case 8: size = QUAD; break; | |
200 | default: | |
201 | einfo ("%P%X: Unsupported size %d for set %s\n", | |
202 | bfd_get_reloc_size (howto), p->h->root.string); | |
203 | size = LONG; | |
204 | break; | |
205 | } | |
206 | ||
207 | lang_add_assignment (exp_assop ('=', p->h->root.string, | |
208 | exp_nameop (NAME, "."))); | |
209 | lang_add_data (size, exp_intop ((bfd_vma) p->count)); | |
210 | ||
211 | for (e = p->elements; e != (struct set_element *) NULL; e = e->next) | |
212 | { | |
213 | if (link_info.relocateable) | |
214 | lang_add_reloc (p->reloc, howto, e->section, | |
215 | (const char *) NULL, exp_intop (e->value)); | |
216 | else | |
217 | lang_add_data (size, exp_relop (e->section, e->value)); | |
218 | } | |
219 | ||
220 | lang_add_data (size, exp_intop (0)); | |
fcf276c4 | 221 | } |
fcf276c4 | 222 | |
6ade1673 ILT |
223 | stat_ptr = old; |
224 | } |