1 /* ELF linking support for BFD.
2 Copyright 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 _bfd_elf_create_got_section (abfd
, info
)
30 struct bfd_link_info
*info
;
34 struct elf_link_hash_entry
*h
;
35 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
38 /* This function may be called more than once. */
39 if (bfd_get_section_by_name (abfd
, ".got") != NULL
)
42 switch (bed
->s
->arch_size
)
44 case 32: ptralign
= 2; break;
45 case 64: ptralign
= 3; break;
49 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
50 | SEC_LINKER_CREATED
);
52 s
= bfd_make_section (abfd
, ".got");
54 || !bfd_set_section_flags (abfd
, s
, flags
)
55 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
58 if (bed
->want_got_plt
)
60 s
= bfd_make_section (abfd
, ".got.plt");
62 || !bfd_set_section_flags (abfd
, s
, flags
)
63 || !bfd_set_section_alignment (abfd
, s
, ptralign
))
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. */
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
)))
77 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
81 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
84 elf_hash_table (info
)->hgot
= h
;
86 /* The first bit of the global offset table is the header. */
87 s
->_raw_size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
93 /* Create dynamic sections when linking against a dynamic object. */
96 _bfd_elf_create_dynamic_sections (abfd
, info
)
98 struct bfd_link_info
*info
;
100 flagword flags
, pltflags
;
101 register asection
*s
;
102 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
105 switch (bed
->s
->arch_size
)
107 case 32: ptralign
= 2; break;
108 case 64: ptralign
= 3; break;
112 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
113 .rel[a].bss sections. */
115 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
116 | SEC_LINKER_CREATED
);
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
;
125 s
= bfd_make_section (abfd
, ".plt");
127 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
128 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
131 if (bed
->want_plt_sym
)
133 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
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
)))
142 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
143 h
->type
= STT_OBJECT
;
146 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
150 s
= bfd_make_section (abfd
,
151 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
153 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
154 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
157 if (! _bfd_elf_create_got_section (abfd
, info
))
160 if (bed
->want_dynbss
)
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");
170 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
173 /* The .rel[a].bss section holds copy relocs. This section is not
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
186 s
= bfd_make_section (abfd
,
187 (bed
->default_use_rela_p
188 ? ".rela.bss" : ".rel.bss"));
190 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
191 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
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
209 _bfd_elf_link_record_dynamic_symbol (info
, h
)
210 struct bfd_link_info
*info
;
211 struct elf_link_hash_entry
*h
;
213 if (h
->dynindx
== -1)
215 struct bfd_strtab_hash
*dynstr
;
221 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
222 ++elf_hash_table (info
)->dynsymcount
;
224 dynstr
= elf_hash_table (info
)->dynstr
;
227 /* Create a strtab to hold the dynamic symbol names. */
228 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_stringtab_init ();
233 /* We don't put any version information in the dynamic string
235 name
= h
->root
.root
.string
;
236 p
= strchr (name
, ELF_VER_CHR
);
244 alc
= bfd_malloc (p
- name
+ 1);
247 strncpy (alc
, name
, p
- name
);
248 alc
[p
- name
] = '\0';
253 indx
= _bfd_stringtab_add (dynstr
, name
, true, copy
);
258 if (indx
== (bfd_size_type
) -1)
260 h
->dynstr_index
= indx
;
266 /* Return the dynindex of a local dynamic symbol. */
269 _bfd_elf_link_lookup_local_dynindx (info
, input_bfd
, input_indx
)
270 struct bfd_link_info
*info
;
274 struct elf_link_local_dynamic_entry
*e
;
276 for (e
= elf_hash_table (info
)->dynlocal
; e
; e
= e
->next
)
277 if (e
->input_bfd
== input_bfd
&& e
->input_indx
== input_indx
)
282 /* This function is used to renumber the dynamic symbols, if some of
283 them are removed because they are marked as local. This is called
284 via elf_link_hash_traverse. */
286 static boolean elf_link_renumber_hash_table_dynsyms
287 PARAMS ((struct elf_link_hash_entry
*, PTR
));
290 elf_link_renumber_hash_table_dynsyms (h
, data
)
291 struct elf_link_hash_entry
*h
;
294 size_t *count
= (size_t *) data
;
296 if (h
->dynindx
!= -1)
297 h
->dynindx
= ++(*count
);
302 /* Assign dynsym indicies. In a shared library we generate a section
303 symbol for each output section, which come first. Next come all of
304 the back-end allocated local dynamic syms, followed by the rest of
305 the global symbols. */
308 _bfd_elf_link_renumber_dynsyms (output_bfd
, info
)
310 struct bfd_link_info
*info
;
312 unsigned long dynsymcount
= 0;
317 for (p
= output_bfd
->sections
; p
; p
= p
->next
)
318 elf_section_data (p
)->dynindx
= ++dynsymcount
;
321 if (elf_hash_table (info
)->dynlocal
)
323 struct elf_link_local_dynamic_entry
*p
;
324 for (p
= elf_hash_table (info
)->dynlocal
; p
; p
= p
->next
)
325 p
->dynindx
= ++dynsymcount
;
328 elf_link_hash_traverse (elf_hash_table (info
),
329 elf_link_renumber_hash_table_dynsyms
,
332 /* There is an unused NULL entry at the head of the table which
333 we must account for in our count. Unless there weren't any
334 symbols, which means we'll have no table at all. */
335 if (dynsymcount
!= 0)
338 return elf_hash_table (info
)->dynsymcount
= dynsymcount
;
341 /* Create a special linker section, or return a pointer to a linker
342 section already created */
344 elf_linker_section_t
*
345 _bfd_elf_create_linker_section (abfd
, info
, which
, defaults
)
347 struct bfd_link_info
*info
;
348 enum elf_linker_section_enum which
;
349 elf_linker_section_t
*defaults
;
351 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
352 elf_linker_section_t
*lsect
;
354 /* Record the first bfd section that needs the special section */
356 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
358 /* If this is the first time, create the section */
359 lsect
= elf_linker_section (dynobj
, which
);
364 lsect
= (elf_linker_section_t
*)
365 bfd_alloc (dynobj
, sizeof (elf_linker_section_t
));
368 elf_linker_section (dynobj
, which
) = lsect
;
369 lsect
->which
= which
;
370 lsect
->hole_written_p
= false;
372 /* See if the sections already exist */
373 lsect
->section
= s
= bfd_get_section_by_name (dynobj
, lsect
->name
);
374 if (!s
|| (s
->flags
& defaults
->flags
) != defaults
->flags
)
376 lsect
->section
= s
= bfd_make_section_anyway (dynobj
, lsect
->name
);
379 return (elf_linker_section_t
*)0;
381 bfd_set_section_flags (dynobj
, s
, defaults
->flags
);
382 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
384 else if (bfd_get_section_alignment (dynobj
, s
) < lsect
->alignment
)
385 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
387 s
->_raw_size
= align_power (s
->_raw_size
, lsect
->alignment
);
389 /* Is there a hole we have to provide? If so check whether the segment is
391 if (lsect
->hole_size
)
393 lsect
->hole_offset
= s
->_raw_size
;
394 s
->_raw_size
+= lsect
->hole_size
;
395 if (lsect
->hole_offset
> lsect
->max_hole_offset
)
397 (*_bfd_error_handler
) (_("%s: Section %s is already to large to put hole of %ld bytes in"),
398 bfd_get_filename (abfd
),
400 (long)lsect
->hole_size
);
402 bfd_set_error (bfd_error_bad_value
);
403 return (elf_linker_section_t
*)0;
408 fprintf (stderr
, "Creating section %s, current size = %ld\n",
409 lsect
->name
, (long)s
->_raw_size
);
414 struct elf_link_hash_entry
*h
= NULL
;
416 fprintf (stderr
, "Adding %s to section %s\n",
420 h
= (struct elf_link_hash_entry
*)
421 bfd_link_hash_lookup (info
->hash
, lsect
->sym_name
, false, false, false);
423 if ((h
== NULL
|| h
->root
.type
== bfd_link_hash_undefined
)
424 && !(_bfd_generic_link_add_one_symbol (info
,
430 ? s
->_raw_size
- lsect
->hole_size
+ lsect
->sym_offset
431 : lsect
->sym_offset
),
434 get_elf_backend_data (abfd
)->collect
,
435 (struct bfd_link_hash_entry
**) &h
)))
436 return (elf_linker_section_t
*)0;
438 if ((defaults
->which
!= LINKER_SECTION_SDATA
)
439 && (defaults
->which
!= LINKER_SECTION_SDATA2
))
440 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_DYNAMIC
;
442 h
->type
= STT_OBJECT
;
446 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
447 return (elf_linker_section_t
*)0;
452 /* This does not make sense. The sections which may exist in the
453 object file have nothing to do with the sections we want to
456 /* Find the related sections if they have been created */
457 if (lsect
->bss_name
&& !lsect
->bss_section
)
458 lsect
->bss_section
= bfd_get_section_by_name (dynobj
, lsect
->bss_name
);
460 if (lsect
->rel_name
&& !lsect
->rel_section
)
461 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
468 /* Find a linker generated pointer with a given addend and type. */
470 elf_linker_section_pointers_t
*
471 _bfd_elf_find_pointer_linker_section (linker_pointers
, addend
, which
)
472 elf_linker_section_pointers_t
*linker_pointers
;
473 bfd_signed_vma addend
;
474 elf_linker_section_enum_t which
;
476 for ( ; linker_pointers
!= NULL
; linker_pointers
= linker_pointers
->next
)
478 if (which
== linker_pointers
->which
&& addend
== linker_pointers
->addend
)
479 return linker_pointers
;
482 return (elf_linker_section_pointers_t
*)0;
486 /* Make the .rela section corresponding to the generated linker section. */
489 _bfd_elf_make_linker_section_rela (dynobj
, lsect
, alignment
)
491 elf_linker_section_t
*lsect
;
494 if (lsect
->rel_section
)
497 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
498 if (lsect
->rel_section
== NULL
)
500 lsect
->rel_section
= bfd_make_section (dynobj
, lsect
->rel_name
);
501 if (lsect
->rel_section
== NULL
502 || ! bfd_set_section_flags (dynobj
,
510 || ! bfd_set_section_alignment (dynobj
, lsect
->rel_section
, alignment
))