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