source: trunk/src/binutils/bfd/elflink.c@ 10

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

Initial revision

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