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