Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* ELF linking support for BFD. |
2 | Copyright 1995, 1996, 1997 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program 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 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program 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 this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | #include "bfd.h" | |
21 | #include "sysdep.h" | |
22 | #include "bfdlink.h" | |
23 | #include "libbfd.h" | |
24 | #define ARCH_SIZE 0 | |
25 | #include "elf-bfd.h" | |
26 | ||
27 | boolean | |
28 | _bfd_elf_create_got_section (abfd, info) | |
29 | bfd *abfd; | |
30 | struct bfd_link_info *info; | |
31 | { | |
32 | flagword flags; | |
33 | register asection *s; | |
34 | struct elf_link_hash_entry *h; | |
35 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
36 | int ptralign; | |
37 | ||
38 | /* This function may be called more than once. */ | |
39 | if (bfd_get_section_by_name (abfd, ".got") != NULL) | |
40 | return true; | |
41 | ||
42 | switch (bed->s->arch_size) | |
43 | { | |
44 | case 32: ptralign = 2; break; | |
45 | case 64: ptralign = 3; break; | |
46 | default: abort(); | |
47 | } | |
48 | ||
49 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
50 | | SEC_LINKER_CREATED); | |
51 | ||
52 | s = bfd_make_section (abfd, ".got"); | |
53 | if (s == NULL | |
54 | || !bfd_set_section_flags (abfd, s, flags) | |
55 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
56 | return false; | |
57 | ||
58 | if (bed->want_got_plt) | |
59 | { | |
60 | s = bfd_make_section (abfd, ".got.plt"); | |
61 | if (s == NULL | |
62 | || !bfd_set_section_flags (abfd, s, flags) | |
63 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
64 | return false; | |
65 | } | |
66 | ||
67 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
68 | (or .got.plt) section. We don't do this in the linker script | |
69 | because we don't want to define the symbol if we are not creating | |
70 | a global offset table. */ | |
71 | h = NULL; | |
72 | if (!(_bfd_generic_link_add_one_symbol | |
73 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
74 | bed->got_symbol_offset, (const char *) NULL, false, | |
75 | bed->collect, (struct bfd_link_hash_entry **) &h))) | |
76 | return false; | |
77 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
78 | h->type = STT_OBJECT; | |
79 | ||
80 | if (info->shared | |
81 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
82 | return false; | |
83 | ||
84 | elf_hash_table (info)->hgot = h; | |
85 | ||
86 | /* The first bit of the global offset table is the header. */ | |
87 | s->_raw_size += bed->got_header_size + bed->got_symbol_offset; | |
88 | ||
89 | return true; | |
90 | } | |
91 | \f | |
92 | ||
93 | /* Create dynamic sections when linking against a dynamic object. */ | |
94 | ||
95 | boolean | |
96 | _bfd_elf_create_dynamic_sections (abfd, info) | |
97 | bfd *abfd; | |
98 | struct bfd_link_info *info; | |
99 | { | |
100 | flagword flags, pltflags; | |
101 | register asection *s; | |
102 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
103 | int ptralign; | |
104 | ||
105 | switch (bed->s->arch_size) | |
106 | { | |
107 | case 32: ptralign = 2; break; | |
108 | case 64: ptralign = 3; break; | |
109 | default: abort(); | |
110 | } | |
111 | ||
112 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and | |
113 | .rel[a].bss sections. */ | |
114 | ||
115 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
116 | | SEC_LINKER_CREATED); | |
117 | ||
118 | pltflags = flags; | |
119 | pltflags |= SEC_CODE; | |
120 | if (bed->plt_not_loaded) | |
121 | pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS); | |
122 | if (bed->plt_readonly) | |
123 | pltflags |= SEC_READONLY; | |
124 | ||
125 | s = bfd_make_section (abfd, ".plt"); | |
126 | if (s == NULL | |
127 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
128 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
129 | return false; | |
130 | ||
131 | if (bed->want_plt_sym) | |
132 | { | |
133 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
134 | .plt section. */ | |
135 | struct elf_link_hash_entry *h = NULL; | |
136 | if (! (_bfd_generic_link_add_one_symbol | |
137 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, | |
138 | (bfd_vma) 0, (const char *) NULL, false, | |
139 | get_elf_backend_data (abfd)->collect, | |
140 | (struct bfd_link_hash_entry **) &h))) | |
141 | return false; | |
142 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
143 | h->type = STT_OBJECT; | |
144 | ||
145 | if (info->shared | |
146 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
147 | return false; | |
148 | } | |
149 | ||
150 | s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.plt" : ".rel.plt"); | |
151 | if (s == NULL | |
152 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
153 | || ! bfd_set_section_alignment (abfd, s, ptralign)) | |
154 | return false; | |
155 | ||
156 | if (! _bfd_elf_create_got_section (abfd, info)) | |
157 | return false; | |
158 | ||
159 | /* The .dynbss section is a place to put symbols which are defined | |
160 | by dynamic objects, are referenced by regular objects, and are | |
161 | not functions. We must allocate space for them in the process | |
162 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
163 | initialize them at run time. The linker script puts the .dynbss | |
164 | section into the .bss section of the final image. */ | |
165 | s = bfd_make_section (abfd, ".dynbss"); | |
166 | if (s == NULL | |
167 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) | |
168 | return false; | |
169 | ||
170 | /* The .rel[a].bss section holds copy relocs. This section is not | |
171 | normally needed. We need to create it here, though, so that the | |
172 | linker will map it to an output section. We can't just create it | |
173 | only if we need it, because we will not know whether we need it | |
174 | until we have seen all the input files, and the first time the | |
175 | main linker code calls BFD after examining all the input files | |
176 | (size_dynamic_sections) the input sections have already been | |
177 | mapped to the output sections. If the section turns out not to | |
178 | be needed, we can discard it later. We will never need this | |
179 | section when generating a shared object, since they do not use | |
180 | copy relocs. */ | |
181 | if (! info->shared) | |
182 | { | |
183 | s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.bss" : ".rel.bss"); | |
184 | if (s == NULL | |
185 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
186 | || ! bfd_set_section_alignment (abfd, s, ptralign)) | |
187 | return false; | |
188 | } | |
189 | ||
190 | return true; | |
191 | } | |
192 | \f | |
193 | ||
194 | /* Record a new dynamic symbol. We record the dynamic symbols as we | |
195 | read the input files, since we need to have a list of all of them | |
196 | before we can determine the final sizes of the output sections. | |
197 | Note that we may actually call this function even though we are not | |
198 | going to output any dynamic symbols; in some cases we know that a | |
199 | symbol should be in the dynamic symbol table, but only if there is | |
200 | one. */ | |
201 | ||
202 | boolean | |
203 | _bfd_elf_link_record_dynamic_symbol (info, h) | |
204 | struct bfd_link_info *info; | |
205 | struct elf_link_hash_entry *h; | |
206 | { | |
207 | if (h->dynindx == -1) | |
208 | { | |
209 | struct bfd_strtab_hash *dynstr; | |
210 | char *p, *alc; | |
211 | const char *name; | |
212 | boolean copy; | |
213 | bfd_size_type indx; | |
214 | ||
215 | h->dynindx = elf_hash_table (info)->dynsymcount; | |
216 | ++elf_hash_table (info)->dynsymcount; | |
217 | ||
218 | dynstr = elf_hash_table (info)->dynstr; | |
219 | if (dynstr == NULL) | |
220 | { | |
221 | /* Create a strtab to hold the dynamic symbol names. */ | |
222 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init (); | |
223 | if (dynstr == NULL) | |
224 | return false; | |
225 | } | |
226 | ||
227 | /* We don't put any version information in the dynamic string | |
228 | table. */ | |
229 | name = h->root.root.string; | |
230 | p = strchr (name, ELF_VER_CHR); | |
231 | if (p == NULL) | |
232 | { | |
233 | alc = NULL; | |
234 | copy = false; | |
235 | } | |
236 | else | |
237 | { | |
238 | alc = bfd_malloc (p - name + 1); | |
239 | if (alc == NULL) | |
240 | return false; | |
241 | strncpy (alc, name, p - name); | |
242 | alc[p - name] = '\0'; | |
243 | name = alc; | |
244 | copy = true; | |
245 | } | |
246 | ||
247 | indx = _bfd_stringtab_add (dynstr, name, true, copy); | |
248 | ||
249 | if (alc != NULL) | |
250 | free (alc); | |
251 | ||
252 | if (indx == (bfd_size_type) -1) | |
253 | return false; | |
254 | h->dynstr_index = indx; | |
255 | } | |
256 | ||
257 | return true; | |
258 | } | |
259 | \f | |
260 | /* Create a special linker section, or return a pointer to a linker section already created */ | |
261 | ||
262 | elf_linker_section_t * | |
263 | _bfd_elf_create_linker_section (abfd, info, which, defaults) | |
264 | bfd *abfd; | |
265 | struct bfd_link_info *info; | |
266 | enum elf_linker_section_enum which; | |
267 | elf_linker_section_t *defaults; | |
268 | { | |
269 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
270 | elf_linker_section_t *lsect; | |
271 | ||
272 | /* Record the first bfd section that needs the special section */ | |
273 | if (!dynobj) | |
274 | dynobj = elf_hash_table (info)->dynobj = abfd; | |
275 | ||
276 | /* If this is the first time, create the section */ | |
277 | lsect = elf_linker_section (dynobj, which); | |
278 | if (!lsect) | |
279 | { | |
280 | asection *s; | |
281 | ||
282 | lsect = (elf_linker_section_t *) | |
283 | bfd_alloc (dynobj, sizeof (elf_linker_section_t)); | |
284 | ||
285 | *lsect = *defaults; | |
286 | elf_linker_section (dynobj, which) = lsect; | |
287 | lsect->which = which; | |
288 | lsect->hole_written_p = false; | |
289 | ||
290 | /* See if the sections already exist */ | |
291 | lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name); | |
292 | if (!s || (s->flags & defaults->flags) != defaults->flags) | |
293 | { | |
294 | lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name); | |
295 | ||
296 | if (s == NULL) | |
297 | return (elf_linker_section_t *)0; | |
298 | ||
299 | bfd_set_section_flags (dynobj, s, defaults->flags); | |
300 | bfd_set_section_alignment (dynobj, s, lsect->alignment); | |
301 | } | |
302 | else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment) | |
303 | bfd_set_section_alignment (dynobj, s, lsect->alignment); | |
304 | ||
305 | s->_raw_size = align_power (s->_raw_size, lsect->alignment); | |
306 | ||
307 | /* Is there a hole we have to provide? If so check whether the segment is | |
308 | too big already */ | |
309 | if (lsect->hole_size) | |
310 | { | |
311 | lsect->hole_offset = s->_raw_size; | |
312 | s->_raw_size += lsect->hole_size; | |
313 | if (lsect->hole_offset > lsect->max_hole_offset) | |
314 | { | |
315 | (*_bfd_error_handler) (_("%s: Section %s is already to large to put hole of %ld bytes in"), | |
316 | bfd_get_filename (abfd), | |
317 | lsect->name, | |
318 | (long)lsect->hole_size); | |
319 | ||
320 | bfd_set_error (bfd_error_bad_value); | |
321 | return (elf_linker_section_t *)0; | |
322 | } | |
323 | } | |
324 | ||
325 | #ifdef DEBUG | |
326 | fprintf (stderr, "Creating section %s, current size = %ld\n", | |
327 | lsect->name, (long)s->_raw_size); | |
328 | #endif | |
329 | ||
330 | if (lsect->sym_name) | |
331 | { | |
332 | struct elf_link_hash_entry *h = NULL; | |
333 | #ifdef DEBUG | |
334 | fprintf (stderr, "Adding %s to section %s\n", | |
335 | lsect->sym_name, | |
336 | lsect->name); | |
337 | #endif | |
338 | h = (struct elf_link_hash_entry *) | |
339 | bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false); | |
340 | ||
341 | if ((h == NULL || h->root.type == bfd_link_hash_undefined) | |
342 | && !(_bfd_generic_link_add_one_symbol (info, | |
343 | abfd, | |
344 | lsect->sym_name, | |
345 | BSF_GLOBAL, | |
346 | s, | |
347 | ((lsect->hole_size) | |
348 | ? s->_raw_size - lsect->hole_size + lsect->sym_offset | |
349 | : lsect->sym_offset), | |
350 | (const char *) NULL, | |
351 | false, | |
352 | get_elf_backend_data (abfd)->collect, | |
353 | (struct bfd_link_hash_entry **) &h))) | |
354 | return (elf_linker_section_t *)0; | |
355 | ||
356 | if ((defaults->which != LINKER_SECTION_SDATA) | |
357 | && (defaults->which != LINKER_SECTION_SDATA2)) | |
358 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC; | |
359 | ||
360 | h->type = STT_OBJECT; | |
361 | lsect->sym_hash = h; | |
362 | ||
363 | if (info->shared | |
364 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
365 | return (elf_linker_section_t *)0; | |
366 | } | |
367 | } | |
368 | ||
369 | #if 0 | |
370 | /* This does not make sense. The sections which may exist in the | |
371 | object file have nothing to do with the sections we want to | |
372 | create. */ | |
373 | ||
374 | /* Find the related sections if they have been created */ | |
375 | if (lsect->bss_name && !lsect->bss_section) | |
376 | lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name); | |
377 | ||
378 | if (lsect->rel_name && !lsect->rel_section) | |
379 | lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); | |
380 | #endif | |
381 | ||
382 | return lsect; | |
383 | } | |
384 | ||
385 | \f | |
386 | /* Find a linker generated pointer with a given addend and type. */ | |
387 | ||
388 | elf_linker_section_pointers_t * | |
389 | _bfd_elf_find_pointer_linker_section (linker_pointers, addend, which) | |
390 | elf_linker_section_pointers_t *linker_pointers; | |
391 | bfd_signed_vma addend; | |
392 | elf_linker_section_enum_t which; | |
393 | { | |
394 | for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next) | |
395 | { | |
396 | if (which == linker_pointers->which && addend == linker_pointers->addend) | |
397 | return linker_pointers; | |
398 | } | |
399 | ||
400 | return (elf_linker_section_pointers_t *)0; | |
401 | } | |
402 | ||
403 | \f | |
404 | /* Make the .rela section corresponding to the generated linker section. */ | |
405 | ||
406 | boolean | |
407 | _bfd_elf_make_linker_section_rela (dynobj, lsect, alignment) | |
408 | bfd *dynobj; | |
409 | elf_linker_section_t *lsect; | |
410 | int alignment; | |
411 | { | |
412 | if (lsect->rel_section) | |
413 | return true; | |
414 | ||
415 | lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); | |
416 | if (lsect->rel_section == NULL) | |
417 | { | |
418 | lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name); | |
419 | if (lsect->rel_section == NULL | |
420 | || ! bfd_set_section_flags (dynobj, | |
421 | lsect->rel_section, | |
422 | (SEC_ALLOC | |
423 | | SEC_LOAD | |
424 | | SEC_HAS_CONTENTS | |
425 | | SEC_IN_MEMORY | |
426 | | SEC_LINKER_CREATED | |
427 | | SEC_READONLY)) | |
428 | || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment)) | |
429 | return false; | |
430 | } | |
431 | ||
432 | return true; | |
433 | } |