source: branches/libc-0.6/src/binutils/bfd/elflink.c

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 18.6 KB
Line 
1/* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "bfdlink.h"
24#include "libbfd.h"
25#define ARCH_SIZE 0
26#include "elf-bfd.h"
27
28bfd_boolean
29_bfd_elf_create_got_section (abfd, info)
30 bfd *abfd;
31 struct bfd_link_info *info;
32{
33 flagword flags;
34 asection *s;
35 struct elf_link_hash_entry *h;
36 struct bfd_link_hash_entry *bh;
37 struct elf_backend_data *bed = get_elf_backend_data (abfd);
38 int ptralign;
39
40 /* This function may be called more than once. */
41 s = bfd_get_section_by_name (abfd, ".got");
42 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0)
43 return TRUE;
44
45 switch (bed->s->arch_size)
46 {
47 case 32:
48 ptralign = 2;
49 break;
50
51 case 64:
52 ptralign = 3;
53 break;
54
55 default:
56 bfd_set_error (bfd_error_bad_value);
57 return FALSE;
58 }
59
60 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
61 | SEC_LINKER_CREATED);
62
63 s = bfd_make_section (abfd, ".got");
64 if (s == NULL
65 || !bfd_set_section_flags (abfd, s, flags)
66 || !bfd_set_section_alignment (abfd, s, ptralign))
67 return FALSE;
68
69 if (bed->want_got_plt)
70 {
71 s = bfd_make_section (abfd, ".got.plt");
72 if (s == NULL
73 || !bfd_set_section_flags (abfd, s, flags)
74 || !bfd_set_section_alignment (abfd, s, ptralign))
75 return FALSE;
76 }
77
78 if (bed->want_got_sym)
79 {
80 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
81 (or .got.plt) section. We don't do this in the linker script
82 because we don't want to define the symbol if we are not creating
83 a global offset table. */
84 bh = NULL;
85 if (!(_bfd_generic_link_add_one_symbol
86 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
87 bed->got_symbol_offset, (const char *) NULL, FALSE,
88 bed->collect, &bh)))
89 return FALSE;
90 h = (struct elf_link_hash_entry *) bh;
91 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
92 h->type = STT_OBJECT;
93
94 if (info->shared
95 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
96 return FALSE;
97
98 elf_hash_table (info)->hgot = h;
99 }
100
101 /* The first bit of the global offset table is the header. */
102 s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
103
104 return TRUE;
105}
106
107
108/* Create dynamic sections when linking against a dynamic object. */
109
110bfd_boolean
111_bfd_elf_create_dynamic_sections (abfd, info)
112 bfd *abfd;
113 struct bfd_link_info *info;
114{
115 flagword flags, pltflags;
116 asection *s;
117 struct elf_backend_data *bed = get_elf_backend_data (abfd);
118 int ptralign;
119
120 switch (bed->s->arch_size)
121 {
122 case 32:
123 ptralign = 2;
124 break;
125
126 case 64:
127 ptralign = 3;
128 break;
129
130 default:
131 bfd_set_error (bfd_error_bad_value);
132 return FALSE;
133 }
134
135 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
136 .rel[a].bss sections. */
137
138 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
139 | SEC_LINKER_CREATED);
140
141 pltflags = flags;
142 pltflags |= SEC_CODE;
143 if (bed->plt_not_loaded)
144 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
145 if (bed->plt_readonly)
146 pltflags |= SEC_READONLY;
147
148 s = bfd_make_section (abfd, ".plt");
149 if (s == NULL
150 || ! bfd_set_section_flags (abfd, s, pltflags)
151 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
152 return FALSE;
153
154 if (bed->want_plt_sym)
155 {
156 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
157 .plt section. */
158 struct elf_link_hash_entry *h;
159 struct bfd_link_hash_entry *bh = NULL;
160
161 if (! (_bfd_generic_link_add_one_symbol
162 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
163 (bfd_vma) 0, (const char *) NULL, FALSE,
164 get_elf_backend_data (abfd)->collect, &bh)))
165 return FALSE;
166 h = (struct elf_link_hash_entry *) bh;
167 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
168 h->type = STT_OBJECT;
169
170 if (info->shared
171 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
172 return FALSE;
173 }
174
175 s = bfd_make_section (abfd,
176 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
177 if (s == NULL
178 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
179 || ! bfd_set_section_alignment (abfd, s, ptralign))
180 return FALSE;
181
182 if (! _bfd_elf_create_got_section (abfd, info))
183 return FALSE;
184
185 if (bed->want_dynbss)
186 {
187 /* The .dynbss section is a place to put symbols which are defined
188 by dynamic objects, are referenced by regular objects, and are
189 not functions. We must allocate space for them in the process
190 image and use a R_*_COPY reloc to tell the dynamic linker to
191 initialize them at run time. The linker script puts the .dynbss
192 section into the .bss section of the final image. */
193 s = bfd_make_section (abfd, ".dynbss");
194 if (s == NULL
195 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
196 return FALSE;
197
198 /* The .rel[a].bss section holds copy relocs. This section is not
199 normally needed. We need to create it here, though, so that the
200 linker will map it to an output section. We can't just create it
201 only if we need it, because we will not know whether we need it
202 until we have seen all the input files, and the first time the
203 main linker code calls BFD after examining all the input files
204 (size_dynamic_sections) the input sections have already been
205 mapped to the output sections. If the section turns out not to
206 be needed, we can discard it later. We will never need this
207 section when generating a shared object, since they do not use
208 copy relocs. */
209 if (! info->shared)
210 {
211 s = bfd_make_section (abfd,
212 (bed->default_use_rela_p
213 ? ".rela.bss" : ".rel.bss"));
214 if (s == NULL
215 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
216 || ! bfd_set_section_alignment (abfd, s, ptralign))
217 return FALSE;
218 }
219 }
220
221 return TRUE;
222}
223
224
225/* Record a new dynamic symbol. We record the dynamic symbols as we
226 read the input files, since we need to have a list of all of them
227 before we can determine the final sizes of the output sections.
228 Note that we may actually call this function even though we are not
229 going to output any dynamic symbols; in some cases we know that a
230 symbol should be in the dynamic symbol table, but only if there is
231 one. */
232
233bfd_boolean
234_bfd_elf_link_record_dynamic_symbol (info, h)
235 struct bfd_link_info *info;
236 struct elf_link_hash_entry *h;
237{
238 if (h->dynindx == -1)
239 {
240 struct elf_strtab_hash *dynstr;
241 char *p, *alc;
242 const char *name;
243 bfd_boolean copy;
244 bfd_size_type indx;
245
246 /* XXX: The ABI draft says the linker must turn hidden and
247 internal symbols into STB_LOCAL symbols when producing the
248 DSO. However, if ld.so honors st_other in the dynamic table,
249 this would not be necessary. */
250 switch (ELF_ST_VISIBILITY (h->other))
251 {
252 case STV_INTERNAL:
253 case STV_HIDDEN:
254 if (h->root.type != bfd_link_hash_undefined
255 && h->root.type != bfd_link_hash_undefweak)
256 {
257 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
258 return TRUE;
259 }
260
261 default:
262 break;
263 }
264
265 h->dynindx = elf_hash_table (info)->dynsymcount;
266 ++elf_hash_table (info)->dynsymcount;
267
268 dynstr = elf_hash_table (info)->dynstr;
269 if (dynstr == NULL)
270 {
271 /* Create a strtab to hold the dynamic symbol names. */
272 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
273 if (dynstr == NULL)
274 return FALSE;
275 }
276
277 /* We don't put any version information in the dynamic string
278 table. */
279 name = h->root.root.string;
280 p = strchr (name, ELF_VER_CHR);
281 if (p == NULL)
282 {
283 alc = NULL;
284 copy = FALSE;
285 }
286 else
287 {
288 size_t len = p - name + 1;
289
290 alc = bfd_malloc ((bfd_size_type) len);
291 if (alc == NULL)
292 return FALSE;
293 memcpy (alc, name, len - 1);
294 alc[len - 1] = '\0';
295 name = alc;
296 copy = TRUE;
297 }
298
299 indx = _bfd_elf_strtab_add (dynstr, name, copy);
300
301 if (alc != NULL)
302 free (alc);
303
304 if (indx == (bfd_size_type) -1)
305 return FALSE;
306 h->dynstr_index = indx;
307 }
308
309 return TRUE;
310}
311
312/* Record a new local dynamic symbol. Returns 0 on failure, 1 on
313 success, and 2 on a failure caused by attempting to record a symbol
314 in a discarded section, eg. a discarded link-once section symbol. */
315
316int
317elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
318 struct bfd_link_info *info;
319 bfd *input_bfd;
320 long input_indx;
321{
322 bfd_size_type amt;
323 struct elf_link_local_dynamic_entry *entry;
324 struct elf_link_hash_table *eht;
325 struct elf_strtab_hash *dynstr;
326 unsigned long dynstr_index;
327 char *name;
328 Elf_External_Sym_Shndx eshndx;
329 char esym[sizeof (Elf64_External_Sym)];
330
331 if (! is_elf_hash_table (info))
332 return 0;
333
334 /* See if the entry exists already. */
335 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
336 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
337 return 1;
338
339 amt = sizeof (*entry);
340 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
341 if (entry == NULL)
342 return 0;
343
344 /* Go find the symbol, so that we can find it's name. */
345 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
346 (size_t) 1, (size_t) input_indx,
347 &entry->isym, esym, &eshndx))
348 {
349 bfd_release (input_bfd, entry);
350 return 0;
351 }
352
353 if (entry->isym.st_shndx != SHN_UNDEF
354 && (entry->isym.st_shndx < SHN_LORESERVE
355 || entry->isym.st_shndx > SHN_HIRESERVE))
356 {
357 asection *s;
358
359 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
360 if (s == NULL || bfd_is_abs_section (s->output_section))
361 {
362 /* We can still bfd_release here as nothing has done another
363 bfd_alloc. We can't do this later in this function. */
364 bfd_release (input_bfd, entry);
365 return 2;
366 }
367 }
368
369 name = (bfd_elf_string_from_elf_section
370 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
371 entry->isym.st_name));
372
373 dynstr = elf_hash_table (info)->dynstr;
374 if (dynstr == NULL)
375 {
376 /* Create a strtab to hold the dynamic symbol names. */
377 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
378 if (dynstr == NULL)
379 return 0;
380 }
381
382 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE);
383 if (dynstr_index == (unsigned long) -1)
384 return 0;
385 entry->isym.st_name = dynstr_index;
386
387 eht = elf_hash_table (info);
388
389 entry->next = eht->dynlocal;
390 eht->dynlocal = entry;
391 entry->input_bfd = input_bfd;
392 entry->input_indx = input_indx;
393 eht->dynsymcount++;
394
395 /* Whatever binding the symbol had before, it's now local. */
396 entry->isym.st_info
397 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
398
399 /* The dynindx will be set at the end of size_dynamic_sections. */
400
401 return 1;
402}
403
404/* Return the dynindex of a local dynamic symbol. */
405
406long
407_bfd_elf_link_lookup_local_dynindx (info, input_bfd, input_indx)
408 struct bfd_link_info *info;
409 bfd *input_bfd;
410 long input_indx;
411{
412 struct elf_link_local_dynamic_entry *e;
413
414 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
415 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
416 return e->dynindx;
417 return -1;
418}
419
420/* This function is used to renumber the dynamic symbols, if some of
421 them are removed because they are marked as local. This is called
422 via elf_link_hash_traverse. */
423
424static bfd_boolean elf_link_renumber_hash_table_dynsyms
425 PARAMS ((struct elf_link_hash_entry *, PTR));
426
427static bfd_boolean
428elf_link_renumber_hash_table_dynsyms (h, data)
429 struct elf_link_hash_entry *h;
430 PTR data;
431{
432 size_t *count = (size_t *) data;
433
434 if (h->root.type == bfd_link_hash_warning)
435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
436
437 if (h->dynindx != -1)
438 h->dynindx = ++(*count);
439
440 return TRUE;
441}
442
443/* Assign dynsym indices. In a shared library we generate a section
444 symbol for each output section, which come first. Next come all of
445 the back-end allocated local dynamic syms, followed by the rest of
446 the global symbols. */
447
448unsigned long
449_bfd_elf_link_renumber_dynsyms (output_bfd, info)
450 bfd *output_bfd;
451 struct bfd_link_info *info;
452{
453 unsigned long dynsymcount = 0;
454
455 if (info->shared)
456 {
457 asection *p;
458 for (p = output_bfd->sections; p ; p = p->next)
459 if ((p->flags & SEC_EXCLUDE) == 0)
460 elf_section_data (p)->dynindx = ++dynsymcount;
461 }
462
463 if (elf_hash_table (info)->dynlocal)
464 {
465 struct elf_link_local_dynamic_entry *p;
466 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
467 p->dynindx = ++dynsymcount;
468 }
469
470 elf_link_hash_traverse (elf_hash_table (info),
471 elf_link_renumber_hash_table_dynsyms,
472 &dynsymcount);
473
474 /* There is an unused NULL entry at the head of the table which
475 we must account for in our count. Unless there weren't any
476 symbols, which means we'll have no table at all. */
477 if (dynsymcount != 0)
478 ++dynsymcount;
479
480 return elf_hash_table (info)->dynsymcount = dynsymcount;
481}
482
483
484/* Create a special linker section, or return a pointer to a linker
485 section already created */
486
487elf_linker_section_t *
488_bfd_elf_create_linker_section (abfd, info, which, defaults)
489 bfd *abfd;
490 struct bfd_link_info *info;
491 enum elf_linker_section_enum which;
492 elf_linker_section_t *defaults;
493{
494 bfd *dynobj = elf_hash_table (info)->dynobj;
495 elf_linker_section_t *lsect;
496
497 /* Record the first bfd section that needs the special section */
498 if (!dynobj)
499 dynobj = elf_hash_table (info)->dynobj = abfd;
500
501 /* If this is the first time, create the section */
502 lsect = elf_linker_section (dynobj, which);
503 if (!lsect)
504 {
505 asection *s;
506 bfd_size_type amt = sizeof (elf_linker_section_t);
507
508 lsect = (elf_linker_section_t *) bfd_alloc (dynobj, amt);
509
510 *lsect = *defaults;
511 elf_linker_section (dynobj, which) = lsect;
512 lsect->which = which;
513 lsect->hole_written_p = FALSE;
514
515 /* See if the sections already exist */
516 lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name);
517 if (!s || (s->flags & defaults->flags) != defaults->flags)
518 {
519 lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name);
520
521 if (s == NULL)
522 return (elf_linker_section_t *)0;
523
524 bfd_set_section_flags (dynobj, s, defaults->flags);
525 bfd_set_section_alignment (dynobj, s, lsect->alignment);
526 }
527 else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment)
528 bfd_set_section_alignment (dynobj, s, lsect->alignment);
529
530 s->_raw_size = align_power (s->_raw_size, lsect->alignment);
531
532 /* Is there a hole we have to provide? If so check whether the
533 segment is too big already */
534 if (lsect->hole_size)
535 {
536 lsect->hole_offset = s->_raw_size;
537 s->_raw_size += lsect->hole_size;
538 if (lsect->hole_offset > lsect->max_hole_offset)
539 {
540 (*_bfd_error_handler)
541 (_("%s: Section %s is too large to add hole of %ld bytes"),
542 bfd_get_filename (abfd),
543 lsect->name,
544 (long) lsect->hole_size);
545
546 bfd_set_error (bfd_error_bad_value);
547 return (elf_linker_section_t *)0;
548 }
549 }
550
551#ifdef DEBUG
552 fprintf (stderr, "Creating section %s, current size = %ld\n",
553 lsect->name, (long)s->_raw_size);
554#endif
555
556 if (lsect->sym_name)
557 {
558 struct elf_link_hash_entry *h;
559 struct bfd_link_hash_entry *bh;
560
561#ifdef DEBUG
562 fprintf (stderr, "Adding %s to section %s\n",
563 lsect->sym_name,
564 lsect->name);
565#endif
566 bh = bfd_link_hash_lookup (info->hash, lsect->sym_name,
567 FALSE, FALSE, FALSE);
568
569 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
570 && !(_bfd_generic_link_add_one_symbol
571 (info, abfd, lsect->sym_name, BSF_GLOBAL, s,
572 (lsect->hole_size
573 ? s->_raw_size - lsect->hole_size + lsect->sym_offset
574 : lsect->sym_offset),
575 (const char *) NULL, FALSE,
576 get_elf_backend_data (abfd)->collect, &bh)))
577 return (elf_linker_section_t *) 0;
578 h = (struct elf_link_hash_entry *) bh;
579
580 if ((defaults->which != LINKER_SECTION_SDATA)
581 && (defaults->which != LINKER_SECTION_SDATA2))
582 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC;
583
584 h->type = STT_OBJECT;
585 lsect->sym_hash = h;
586
587 if (info->shared
588 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
589 return (elf_linker_section_t *) 0;
590 }
591 }
592
593#if 0
594 /* This does not make sense. The sections which may exist in the
595 object file have nothing to do with the sections we want to
596 create. */
597
598 /* Find the related sections if they have been created */
599 if (lsect->bss_name && !lsect->bss_section)
600 lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name);
601
602 if (lsect->rel_name && !lsect->rel_section)
603 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
604#endif
605
606 return lsect;
607}
608
609
610/* Find a linker generated pointer with a given addend and type. */
611
612elf_linker_section_pointers_t *
613_bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
614 elf_linker_section_pointers_t *linker_pointers;
615 bfd_vma addend;
616 elf_linker_section_enum_t which;
617{
618 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
619 {
620 if (which == linker_pointers->which && addend == linker_pointers->addend)
621 return linker_pointers;
622 }
623
624 return (elf_linker_section_pointers_t *)0;
625}
626
627
628/* Make the .rela section corresponding to the generated linker section. */
629
630bfd_boolean
631_bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
632 bfd *dynobj;
633 elf_linker_section_t *lsect;
634 int alignment;
635{
636 if (lsect->rel_section)
637 return TRUE;
638
639 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
640 if (lsect->rel_section == NULL)
641 {
642 lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name);
643 if (lsect->rel_section == NULL
644 || ! bfd_set_section_flags (dynobj,
645 lsect->rel_section,
646 (SEC_ALLOC
647 | SEC_LOAD
648 | SEC_HAS_CONTENTS
649 | SEC_IN_MEMORY
650 | SEC_LINKER_CREATED
651 | SEC_READONLY))
652 || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment))
653 return FALSE;
654 }
655
656 return TRUE;
657}
Note: See TracBrowser for help on using the repository browser.