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