]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elflink.c
*** empty log message ***
[thirdparty/binutils-gdb.git] / bfd / elflink.c
CommitLineData
252b5132 1/* ELF linking support for BFD.
7898deda
NC
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
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
76 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
77 (or .got.plt) section. We don't do this in the linker script
78 because we don't want to define the symbol if we are not creating
79 a global offset table. */
80 h = NULL;
81 if (!(_bfd_generic_link_add_one_symbol
82 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
83 bed->got_symbol_offset, (const char *) NULL, false,
84 bed->collect, (struct bfd_link_hash_entry **) &h)))
85 return false;
86 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
87 h->type = STT_OBJECT;
88
89 if (info->shared
90 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
91 return false;
92
93 elf_hash_table (info)->hgot = h;
94
95 /* The first bit of the global offset table is the header. */
96 s->_raw_size += bed->got_header_size + bed->got_symbol_offset;
97
98 return true;
99}
100\f
252b5132
RH
101/* Create dynamic sections when linking against a dynamic object. */
102
103boolean
104_bfd_elf_create_dynamic_sections (abfd, info)
105 bfd *abfd;
106 struct bfd_link_info *info;
107{
108 flagword flags, pltflags;
109 register asection *s;
110 struct elf_backend_data *bed = get_elf_backend_data (abfd);
7442e600 111 int ptralign = 0;
252b5132
RH
112
113 switch (bed->s->arch_size)
114 {
bb0deeff
AO
115 case 32:
116 ptralign = 2;
117 break;
118
119 case 64:
120 ptralign = 3;
121 break;
122
123 default:
124 bfd_set_error (bfd_error_bad_value);
125 return false;
252b5132
RH
126 }
127
128 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
129 .rel[a].bss sections. */
130
131 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
132 | SEC_LINKER_CREATED);
133
134 pltflags = flags;
135 pltflags |= SEC_CODE;
136 if (bed->plt_not_loaded)
137 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
138 if (bed->plt_readonly)
139 pltflags |= SEC_READONLY;
140
141 s = bfd_make_section (abfd, ".plt");
142 if (s == NULL
143 || ! bfd_set_section_flags (abfd, s, pltflags)
144 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
145 return false;
146
147 if (bed->want_plt_sym)
148 {
149 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
150 .plt section. */
151 struct elf_link_hash_entry *h = NULL;
152 if (! (_bfd_generic_link_add_one_symbol
153 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
154 (bfd_vma) 0, (const char *) NULL, false,
155 get_elf_backend_data (abfd)->collect,
156 (struct bfd_link_hash_entry **) &h)))
157 return false;
158 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
159 h->type = STT_OBJECT;
160
161 if (info->shared
162 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
163 return false;
164 }
165
3e932841 166 s = bfd_make_section (abfd,
bf572ba0 167 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
252b5132
RH
168 if (s == NULL
169 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
170 || ! bfd_set_section_alignment (abfd, s, ptralign))
171 return false;
172
173 if (! _bfd_elf_create_got_section (abfd, info))
174 return false;
175
3018b441
RH
176 if (bed->want_dynbss)
177 {
178 /* The .dynbss section is a place to put symbols which are defined
179 by dynamic objects, are referenced by regular objects, and are
180 not functions. We must allocate space for them in the process
181 image and use a R_*_COPY reloc to tell the dynamic linker to
182 initialize them at run time. The linker script puts the .dynbss
183 section into the .bss section of the final image. */
184 s = bfd_make_section (abfd, ".dynbss");
185 if (s == NULL
186 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
187 return false;
252b5132 188
3018b441 189 /* The .rel[a].bss section holds copy relocs. This section is not
252b5132
RH
190 normally needed. We need to create it here, though, so that the
191 linker will map it to an output section. We can't just create it
192 only if we need it, because we will not know whether we need it
193 until we have seen all the input files, and the first time the
194 main linker code calls BFD after examining all the input files
195 (size_dynamic_sections) the input sections have already been
196 mapped to the output sections. If the section turns out not to
197 be needed, we can discard it later. We will never need this
198 section when generating a shared object, since they do not use
199 copy relocs. */
3018b441
RH
200 if (! info->shared)
201 {
3e932841
KH
202 s = bfd_make_section (abfd,
203 (bed->default_use_rela_p
204 ? ".rela.bss" : ".rel.bss"));
3018b441
RH
205 if (s == NULL
206 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
207 || ! bfd_set_section_alignment (abfd, s, ptralign))
208 return false;
209 }
252b5132
RH
210 }
211
212 return true;
213}
214\f
252b5132
RH
215/* Record a new dynamic symbol. We record the dynamic symbols as we
216 read the input files, since we need to have a list of all of them
217 before we can determine the final sizes of the output sections.
218 Note that we may actually call this function even though we are not
219 going to output any dynamic symbols; in some cases we know that a
220 symbol should be in the dynamic symbol table, but only if there is
221 one. */
222
223boolean
224_bfd_elf_link_record_dynamic_symbol (info, h)
225 struct bfd_link_info *info;
226 struct elf_link_hash_entry *h;
227{
228 if (h->dynindx == -1)
229 {
230 struct bfd_strtab_hash *dynstr;
231 char *p, *alc;
232 const char *name;
233 boolean copy;
234 bfd_size_type indx;
235
7a13edea
NC
236 /* XXX: The ABI draft says the linker must turn hidden and
237 internal symbols into STB_LOCAL symbols when producing the
238 DSO. However, if ld.so honors st_other in the dynamic table,
239 this would not be necessary. */
240 switch (ELF_ST_VISIBILITY (h->other))
241 {
242 case STV_INTERNAL:
243 case STV_HIDDEN:
9d6eee78
L
244 if (h->root.type != bfd_link_hash_undefined
245 && h->root.type != bfd_link_hash_undefweak)
38048eb9
L
246 {
247 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
248 return true;
7a13edea 249 }
0444bdd4 250
7a13edea
NC
251 default:
252 break;
253 }
254
252b5132
RH
255 h->dynindx = elf_hash_table (info)->dynsymcount;
256 ++elf_hash_table (info)->dynsymcount;
257
258 dynstr = elf_hash_table (info)->dynstr;
259 if (dynstr == NULL)
260 {
261 /* Create a strtab to hold the dynamic symbol names. */
262 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
263 if (dynstr == NULL)
264 return false;
265 }
266
267 /* We don't put any version information in the dynamic string
268 table. */
269 name = h->root.root.string;
270 p = strchr (name, ELF_VER_CHR);
271 if (p == NULL)
272 {
273 alc = NULL;
274 copy = false;
275 }
276 else
277 {
278 alc = bfd_malloc (p - name + 1);
279 if (alc == NULL)
280 return false;
281 strncpy (alc, name, p - name);
282 alc[p - name] = '\0';
283 name = alc;
284 copy = true;
285 }
286
287 indx = _bfd_stringtab_add (dynstr, name, true, copy);
288
289 if (alc != NULL)
290 free (alc);
291
292 if (indx == (bfd_size_type) -1)
293 return false;
294 h->dynstr_index = indx;
295 }
296
297 return true;
298}
42751cf3 299
30b30c21 300/* Return the dynindex of a local dynamic symbol. */
42751cf3 301
30b30c21
RH
302long
303_bfd_elf_link_lookup_local_dynindx (info, input_bfd, input_indx)
304 struct bfd_link_info *info;
305 bfd *input_bfd;
306 long input_indx;
307{
308 struct elf_link_local_dynamic_entry *e;
309
310 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
311 if (e->input_bfd == input_bfd && e->input_indx == input_indx)
312 return e->dynindx;
313 return -1;
314}
315
316/* This function is used to renumber the dynamic symbols, if some of
317 them are removed because they are marked as local. This is called
318 via elf_link_hash_traverse. */
319
320static boolean elf_link_renumber_hash_table_dynsyms
321 PARAMS ((struct elf_link_hash_entry *, PTR));
322
323static boolean
324elf_link_renumber_hash_table_dynsyms (h, data)
42751cf3 325 struct elf_link_hash_entry *h;
30b30c21 326 PTR data;
42751cf3 327{
30b30c21
RH
328 size_t *count = (size_t *) data;
329
42751cf3 330 if (h->dynindx != -1)
30b30c21
RH
331 h->dynindx = ++(*count);
332
42751cf3
MM
333 return true;
334}
30b30c21
RH
335
336/* Assign dynsym indicies. In a shared library we generate a section
337 symbol for each output section, which come first. Next come all of
338 the back-end allocated local dynamic syms, followed by the rest of
339 the global symbols. */
340
341unsigned long
342_bfd_elf_link_renumber_dynsyms (output_bfd, info)
343 bfd *output_bfd;
344 struct bfd_link_info *info;
345{
346 unsigned long dynsymcount = 0;
347
348 if (info->shared)
349 {
350 asection *p;
351 for (p = output_bfd->sections; p ; p = p->next)
352 elf_section_data (p)->dynindx = ++dynsymcount;
353 }
354
355 if (elf_hash_table (info)->dynlocal)
356 {
357 struct elf_link_local_dynamic_entry *p;
358 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
359 p->dynindx = ++dynsymcount;
360 }
361
362 elf_link_hash_traverse (elf_hash_table (info),
363 elf_link_renumber_hash_table_dynsyms,
364 &dynsymcount);
365
366 /* There is an unused NULL entry at the head of the table which
367 we must account for in our count. Unless there weren't any
368 symbols, which means we'll have no table at all. */
369 if (dynsymcount != 0)
370 ++dynsymcount;
371
372 return elf_hash_table (info)->dynsymcount = dynsymcount;
373}
252b5132 374\f
30b30c21
RH
375/* Create a special linker section, or return a pointer to a linker
376 section already created */
252b5132
RH
377
378elf_linker_section_t *
379_bfd_elf_create_linker_section (abfd, info, which, defaults)
380 bfd *abfd;
381 struct bfd_link_info *info;
382 enum elf_linker_section_enum which;
383 elf_linker_section_t *defaults;
384{
385 bfd *dynobj = elf_hash_table (info)->dynobj;
386 elf_linker_section_t *lsect;
387
388 /* Record the first bfd section that needs the special section */
389 if (!dynobj)
390 dynobj = elf_hash_table (info)->dynobj = abfd;
391
392 /* If this is the first time, create the section */
393 lsect = elf_linker_section (dynobj, which);
394 if (!lsect)
395 {
396 asection *s;
397
398 lsect = (elf_linker_section_t *)
399 bfd_alloc (dynobj, sizeof (elf_linker_section_t));
400
401 *lsect = *defaults;
402 elf_linker_section (dynobj, which) = lsect;
403 lsect->which = which;
404 lsect->hole_written_p = false;
405
406 /* See if the sections already exist */
407 lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name);
408 if (!s || (s->flags & defaults->flags) != defaults->flags)
409 {
410 lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name);
411
412 if (s == NULL)
413 return (elf_linker_section_t *)0;
414
415 bfd_set_section_flags (dynobj, s, defaults->flags);
416 bfd_set_section_alignment (dynobj, s, lsect->alignment);
417 }
418 else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment)
419 bfd_set_section_alignment (dynobj, s, lsect->alignment);
420
421 s->_raw_size = align_power (s->_raw_size, lsect->alignment);
422
423 /* Is there a hole we have to provide? If so check whether the segment is
424 too big already */
425 if (lsect->hole_size)
426 {
427 lsect->hole_offset = s->_raw_size;
428 s->_raw_size += lsect->hole_size;
429 if (lsect->hole_offset > lsect->max_hole_offset)
430 {
431 (*_bfd_error_handler) (_("%s: Section %s is already to large to put hole of %ld bytes in"),
432 bfd_get_filename (abfd),
433 lsect->name,
434 (long)lsect->hole_size);
435
436 bfd_set_error (bfd_error_bad_value);
437 return (elf_linker_section_t *)0;
438 }
439 }
440
441#ifdef DEBUG
442 fprintf (stderr, "Creating section %s, current size = %ld\n",
443 lsect->name, (long)s->_raw_size);
444#endif
445
446 if (lsect->sym_name)
447 {
448 struct elf_link_hash_entry *h = NULL;
449#ifdef DEBUG
450 fprintf (stderr, "Adding %s to section %s\n",
451 lsect->sym_name,
452 lsect->name);
453#endif
454 h = (struct elf_link_hash_entry *)
455 bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false);
456
457 if ((h == NULL || h->root.type == bfd_link_hash_undefined)
458 && !(_bfd_generic_link_add_one_symbol (info,
459 abfd,
460 lsect->sym_name,
461 BSF_GLOBAL,
462 s,
463 ((lsect->hole_size)
464 ? s->_raw_size - lsect->hole_size + lsect->sym_offset
465 : lsect->sym_offset),
466 (const char *) NULL,
467 false,
468 get_elf_backend_data (abfd)->collect,
469 (struct bfd_link_hash_entry **) &h)))
470 return (elf_linker_section_t *)0;
471
472 if ((defaults->which != LINKER_SECTION_SDATA)
473 && (defaults->which != LINKER_SECTION_SDATA2))
474 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC;
475
476 h->type = STT_OBJECT;
477 lsect->sym_hash = h;
478
479 if (info->shared
480 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
481 return (elf_linker_section_t *)0;
482 }
483 }
484
485#if 0
486 /* This does not make sense. The sections which may exist in the
487 object file have nothing to do with the sections we want to
488 create. */
489
490 /* Find the related sections if they have been created */
491 if (lsect->bss_name && !lsect->bss_section)
492 lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name);
493
494 if (lsect->rel_name && !lsect->rel_section)
495 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
496#endif
497
498 return lsect;
499}
252b5132
RH
500\f
501/* Find a linker generated pointer with a given addend and type. */
502
503elf_linker_section_pointers_t *
504_bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
505 elf_linker_section_pointers_t *linker_pointers;
506 bfd_signed_vma addend;
507 elf_linker_section_enum_t which;
508{
509 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
510 {
511 if (which == linker_pointers->which && addend == linker_pointers->addend)
512 return linker_pointers;
513 }
514
515 return (elf_linker_section_pointers_t *)0;
516}
252b5132
RH
517\f
518/* Make the .rela section corresponding to the generated linker section. */
519
520boolean
521_bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
522 bfd *dynobj;
523 elf_linker_section_t *lsect;
524 int alignment;
525{
526 if (lsect->rel_section)
527 return true;
528
529 lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
530 if (lsect->rel_section == NULL)
531 {
532 lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name);
533 if (lsect->rel_section == NULL
534 || ! bfd_set_section_flags (dynobj,
535 lsect->rel_section,
536 (SEC_ALLOC
537 | SEC_LOAD
538 | SEC_HAS_CONTENTS
539 | SEC_IN_MEMORY
540 | SEC_LINKER_CREATED
541 | SEC_READONLY))
542 || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment))
543 return false;
544 }
545
546 return true;
547}