1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 _bfd_elf_create_got_section (abfd
, info
)
31 struct bfd_link_info
*info
;
35 struct elf_link_hash_entry
*h
;
36 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
39 /* This function may be called more than once. */
40 if (bfd_get_section_by_name (abfd
, ".got") != NULL
)
43 switch (bed
->s
->arch_size
)
54 bfd_set_error (bfd_error_bad_value
);
58 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
59 | SEC_LINKER_CREATED
);
61 s
= bfd_make_section (abfd
, ".got");
63 || !bfd_set_section_flags (abfd
, s
, flags
)
64 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
67 if (bed
->want_got_plt
)
69 s
= bfd_make_section (abfd
, ".got.plt");
71 || !bfd_set_section_flags (abfd
, s
, flags
)
72 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
76 if (bed
->want_got_sym
)
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. */
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
)))
88 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
92 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
95 elf_hash_table (info
)->hgot
= h
;
98 /* The first bit of the global offset table is the header. */
99 s
->_raw_size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
104 /* Create dynamic sections when linking against a dynamic object. */
107 _bfd_elf_create_dynamic_sections (abfd
, info
)
109 struct bfd_link_info
*info
;
111 flagword flags
, pltflags
;
112 register asection
*s
;
113 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
116 switch (bed
->s
->arch_size
)
127 bfd_set_error (bfd_error_bad_value
);
131 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
132 .rel[a].bss sections. */
134 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
135 | SEC_LINKER_CREATED
);
138 pltflags
|= SEC_CODE
;
139 if (bed
->plt_not_loaded
)
140 pltflags
&= ~ (SEC_CODE
| SEC_LOAD
| SEC_HAS_CONTENTS
);
141 if (bed
->plt_readonly
)
142 pltflags
|= SEC_READONLY
;
144 s
= bfd_make_section (abfd
, ".plt");
146 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
147 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
150 if (bed
->want_plt_sym
)
152 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
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
)))
161 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
162 h
->type
= STT_OBJECT
;
165 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
169 s
= bfd_make_section (abfd
,
170 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
172 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
173 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
176 if (! _bfd_elf_create_got_section (abfd
, info
))
179 if (bed
->want_dynbss
)
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");
189 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
192 /* The .rel[a].bss section holds copy relocs. This section is not
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
205 s
= bfd_make_section (abfd
,
206 (bed
->default_use_rela_p
207 ? ".rela.bss" : ".rel.bss"));
209 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
210 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
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
227 _bfd_elf_link_record_dynamic_symbol (info
, h
)
228 struct bfd_link_info
*info
;
229 struct elf_link_hash_entry
*h
;
231 if (h
->dynindx
== -1)
233 struct elf_strtab_hash
*dynstr
;
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
))
247 if (h
->root
.type
!= bfd_link_hash_undefined
248 && h
->root
.type
!= bfd_link_hash_undefweak
)
250 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
258 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
259 ++elf_hash_table (info
)->dynsymcount
;
261 dynstr
= elf_hash_table (info
)->dynstr
;
264 /* Create a strtab to hold the dynamic symbol names. */
265 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_strtab_init ();
270 /* We don't put any version information in the dynamic string
272 name
= h
->root
.root
.string
;
273 p
= strchr (name
, ELF_VER_CHR
);
281 alc
= bfd_malloc ((bfd_size_type
) (p
- name
+ 1));
284 strncpy (alc
, name
, (size_t) (p
- name
));
285 alc
[p
- name
] = '\0';
290 indx
= _bfd_elf_strtab_add (dynstr
, name
, copy
);
295 if (indx
== (bfd_size_type
) -1)
297 h
->dynstr_index
= indx
;
303 /* Return the dynindex of a local dynamic symbol. */
306 _bfd_elf_link_lookup_local_dynindx (info
, input_bfd
, input_indx
)
307 struct bfd_link_info
*info
;
311 struct elf_link_local_dynamic_entry
*e
;
313 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
314 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
319 /* This function is used to renumber the dynamic symbols, if some of
320 them are removed because they are marked as local. This is called
321 via elf_link_hash_traverse. */
323 static boolean elf_link_renumber_hash_table_dynsyms
324 PARAMS ((struct elf_link_hash_entry
*, PTR
));
327 elf_link_renumber_hash_table_dynsyms (h
, data
)
328 struct elf_link_hash_entry
*h
;
331 size_t *count
= (size_t *) data
;
333 if (h
->root
.type
== bfd_link_hash_warning
)
334 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
336 if (h
->dynindx
!= -1)
337 h
->dynindx
= ++(*count
);
342 /* Assign dynsym indices. In a shared library we generate a section
343 symbol for each output section, which come first. Next come all of
344 the back-end allocated local dynamic syms, followed by the rest of
345 the global symbols. */
348 _bfd_elf_link_renumber_dynsyms (output_bfd
, info
)
350 struct bfd_link_info
*info
;
352 unsigned long dynsymcount
= 0;
357 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
358 elf_section_data (p
)->dynindx
= ++dynsymcount
;
361 if (elf_hash_table (info
)->dynlocal
)
363 struct elf_link_local_dynamic_entry
*p
;
364 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
365 p
->dynindx
= ++dynsymcount
;
368 elf_link_hash_traverse (elf_hash_table (info
),
369 elf_link_renumber_hash_table_dynsyms
,
372 /* There is an unused NULL entry at the head of the table which
373 we must account for in our count. Unless there weren't any
374 symbols, which means we'll have no table at all. */
375 if (dynsymcount
!= 0)
378 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
381 /* Create a special linker section, or return a pointer to a linker
382 section already created */
384 elf_linker_section_t
*
385 _bfd_elf_create_linker_section (abfd
, info
, which
, defaults
)
387 struct bfd_link_info
*info
;
388 enum elf_linker_section_enum which
;
389 elf_linker_section_t
*defaults
;
391 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
392 elf_linker_section_t
*lsect
;
394 /* Record the first bfd section that needs the special section */
396 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
398 /* If this is the first time, create the section */
399 lsect
= elf_linker_section (dynobj
, which
);
403 bfd_size_type amt
= sizeof (elf_linker_section_t
);
405 lsect
= (elf_linker_section_t
*) bfd_alloc (dynobj
, amt
);
408 elf_linker_section (dynobj
, which
) = lsect
;
409 lsect
->which
= which
;
410 lsect
->hole_written_p
= false;
412 /* See if the sections already exist */
413 lsect
->section
= s
= bfd_get_section_by_name (dynobj
, lsect
->name
);
414 if (!s
|| (s
->flags
& defaults
->flags
) != defaults
->flags
)
416 lsect
->section
= s
= bfd_make_section_anyway (dynobj
, lsect
->name
);
419 return (elf_linker_section_t
*)0;
421 bfd_set_section_flags (dynobj
, s
, defaults
->flags
);
422 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
424 else if (bfd_get_section_alignment (dynobj
, s
) < lsect
->alignment
)
425 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
427 s
->_raw_size
= align_power (s
->_raw_size
, lsect
->alignment
);
429 /* Is there a hole we have to provide? If so check whether the segment is
431 if (lsect
->hole_size
)
433 lsect
->hole_offset
= s
->_raw_size
;
434 s
->_raw_size
+= lsect
->hole_size
;
435 if (lsect
->hole_offset
> lsect
->max_hole_offset
)
437 (*_bfd_error_handler
) (_("%s: Section %s is too large to add hole of %ld bytes"),
438 bfd_get_filename (abfd
),
440 (long) lsect
->hole_size
);
442 bfd_set_error (bfd_error_bad_value
);
443 return (elf_linker_section_t
*)0;
448 fprintf (stderr
, "Creating section %s, current size = %ld\n",
449 lsect
->name
, (long)s
->_raw_size
);
454 struct elf_link_hash_entry
*h
= NULL
;
456 fprintf (stderr
, "Adding %s to section %s\n",
460 h
= (struct elf_link_hash_entry
*)
461 bfd_link_hash_lookup (info
->hash
, lsect
->sym_name
, false, false, false);
463 if ((h
== NULL
|| h
->root
.type
== bfd_link_hash_undefined
)
464 && !(_bfd_generic_link_add_one_symbol (info
,
470 ? s
->_raw_size
- lsect
->hole_size
+ lsect
->sym_offset
471 : lsect
->sym_offset
),
474 get_elf_backend_data (abfd
)->collect
,
475 (struct bfd_link_hash_entry
**) &h
)))
476 return (elf_linker_section_t
*)0;
478 if ((defaults
->which
!= LINKER_SECTION_SDATA
)
479 && (defaults
->which
!= LINKER_SECTION_SDATA2
))
480 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_DYNAMIC
;
482 h
->type
= STT_OBJECT
;
486 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
487 return (elf_linker_section_t
*)0;
492 /* This does not make sense. The sections which may exist in the
493 object file have nothing to do with the sections we want to
496 /* Find the related sections if they have been created */
497 if (lsect
->bss_name
&& !lsect
->bss_section
)
498 lsect
->bss_section
= bfd_get_section_by_name (dynobj
, lsect
->bss_name
);
500 if (lsect
->rel_name
&& !lsect
->rel_section
)
501 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
507 /* Find a linker generated pointer with a given addend and type. */
509 elf_linker_section_pointers_t
*
510 _bfd_elf_find_pointer_linker_section (linker_pointers
, addend
, which
)
511 elf_linker_section_pointers_t
*linker_pointers
;
513 elf_linker_section_enum_t which
;
515 for ( ; linker_pointers
!= NULL
; linker_pointers
= linker_pointers
->next
)
517 if (which
== linker_pointers
->which
&& addend
== linker_pointers
->addend
)
518 return linker_pointers
;
521 return (elf_linker_section_pointers_t
*)0;
524 /* Make the .rela section corresponding to the generated linker section. */
527 _bfd_elf_make_linker_section_rela (dynobj
, lsect
, alignment
)
529 elf_linker_section_t
*lsect
;
532 if (lsect
->rel_section
)
535 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
536 if (lsect
->rel_section
== NULL
)
538 lsect
->rel_section
= bfd_make_section (dynobj
, lsect
->rel_name
);
539 if (lsect
->rel_section
== NULL
540 || ! bfd_set_section_flags (dynobj
,
548 || ! bfd_set_section_alignment (dynobj
, lsect
->rel_section
, alignment
))