1 /* ELF linking support for BFD.
2 Copyright 1995 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
);
37 /* This function may be called more than once. */
38 if (bfd_get_section_by_name (abfd
, ".got") != NULL
)
41 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
42 | SEC_LINKER_CREATED
);
44 s
= bfd_make_section (abfd
, ".got");
46 || !bfd_set_section_flags (abfd
, s
, flags
)
47 || !bfd_set_section_alignment (abfd
, s
, 2))
50 if (bed
->want_got_plt
)
52 s
= bfd_make_section (abfd
, ".got.plt");
54 || !bfd_set_section_flags (abfd
, s
, flags
)
55 || !bfd_set_section_alignment (abfd
, s
, 2))
59 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
60 (or .got.plt) section. We don't do this in the linker script
61 because we don't want to define the symbol if we are not creating
62 a global offset table. */
64 if (!(_bfd_generic_link_add_one_symbol
65 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
, (bfd_vma
) 0,
66 (const char *) NULL
, false, get_elf_backend_data (abfd
)->collect
,
67 (struct bfd_link_hash_entry
**) &h
)))
69 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
73 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
76 elf_hash_table (info
)->hgot
= h
;
78 /* The first three global offset table entries are reserved. */
79 s
->_raw_size
+= 3 * 4;
85 /* Create dynamic sections when linking against a dynamic object. */
88 _bfd_elf_create_dynamic_sections (abfd
, info
)
90 struct bfd_link_info
*info
;
94 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
96 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
97 .rel[a].bss sections. */
99 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
100 | SEC_LINKER_CREATED
);
102 s
= bfd_make_section (abfd
, ".plt");
104 || ! bfd_set_section_flags (abfd
, s
,
106 | (bed
->plt_readonly
? SEC_READONLY
: 0)))
107 || ! bfd_set_section_alignment (abfd
, s
, 2))
110 if (bed
->want_plt_sym
)
112 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
114 struct elf_link_hash_entry
*h
= NULL
;
115 if (! (_bfd_generic_link_add_one_symbol
116 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
117 (bfd_vma
) 0, (const char *) NULL
, false,
118 get_elf_backend_data (abfd
)->collect
,
119 (struct bfd_link_hash_entry
**) &h
)))
121 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
122 h
->type
= STT_OBJECT
;
125 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
129 s
= bfd_make_section (abfd
, bed
->use_rela_p
? ".rela.plt" : ".rel.plt");
131 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
132 || ! bfd_set_section_alignment (abfd
, s
, 2))
135 if (! _bfd_elf_create_got_section (abfd
, info
))
138 /* The .dynbss section is a place to put symbols which are defined
139 by dynamic objects, are referenced by regular objects, and are
140 not functions. We must allocate space for them in the process
141 image and use a R_*_COPY reloc to tell the dynamic linker to
142 initialize them at run time. The linker script puts the .dynbss
143 section into the .bss section of the final image. */
144 s
= bfd_make_section (abfd
, ".dynbss");
146 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
149 /* The .rel[a].bss section holds copy relocs. This section is not
150 normally needed. We need to create it here, though, so that the
151 linker will map it to an output section. We can't just create it
152 only if we need it, because we will not know whether we need it
153 until we have seen all the input files, and the first time the
154 main linker code calls BFD after examining all the input files
155 (size_dynamic_sections) the input sections have already been
156 mapped to the output sections. If the section turns out not to
157 be needed, we can discard it later. We will never need this
158 section when generating a shared object, since they do not use
162 s
= bfd_make_section (abfd
, bed
->use_rela_p
? ".rela.bss" : ".rel.bss");
164 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
165 || ! bfd_set_section_alignment (abfd
, s
, 2))
173 /* Record a new dynamic symbol. We record the dynamic symbols as we
174 read the input files, since we need to have a list of all of them
175 before we can determine the final sizes of the output sections.
176 Note that we may actually call this function even though we are not
177 going to output any dynamic symbols; in some cases we know that a
178 symbol should be in the dynamic symbol table, but only if there is
182 _bfd_elf_link_record_dynamic_symbol (info
, h
)
183 struct bfd_link_info
*info
;
184 struct elf_link_hash_entry
*h
;
186 if (h
->dynindx
== -1)
188 struct bfd_strtab_hash
*dynstr
;
190 h
->dynindx
= elf_hash_table (info
)->dynsymcount
;
191 ++elf_hash_table (info
)->dynsymcount
;
193 dynstr
= elf_hash_table (info
)->dynstr
;
196 /* Create a strtab to hold the dynamic symbol names. */
197 elf_hash_table (info
)->dynstr
= dynstr
= _bfd_elf_stringtab_init ();
202 h
->dynstr_index
= ((unsigned long)
203 _bfd_stringtab_add (dynstr
, h
->root
.root
.string
,
205 if (h
->dynstr_index
== (unsigned long) -1)
212 /* Create a special linker section, or return a pointer to a linker section already created */
214 elf_linker_section_t
*
215 _bfd_elf_create_linker_section (abfd
, info
, which
, defaults
)
217 struct bfd_link_info
*info
;
218 enum elf_linker_section_enum which
;
219 elf_linker_section_t
*defaults
;
221 bfd
*dynobj
= elf_hash_table (info
)->dynobj
;
222 elf_linker_section_t
*lsect
;
224 /* Record the first bfd section that needs the special section */
226 dynobj
= elf_hash_table (info
)->dynobj
= abfd
;
228 /* If this is the first time, create the section */
229 lsect
= elf_linker_section (dynobj
, which
);
234 lsect
= (elf_linker_section_t
*)
235 bfd_alloc (dynobj
, sizeof (elf_linker_section_t
));
238 elf_linker_section (dynobj
, which
) = lsect
;
239 lsect
->which
= which
;
240 lsect
->hole_written_p
= false;
242 /* See if the sections already exist */
243 lsect
->section
= s
= bfd_get_section_by_name (dynobj
, lsect
->name
);
244 if (!s
|| (s
->flags
& defaults
->flags
) != defaults
->flags
)
246 lsect
->section
= s
= bfd_make_section_anyway (dynobj
, lsect
->name
);
249 return (elf_linker_section_t
*)0;
251 bfd_set_section_flags (dynobj
, s
, defaults
->flags
);
252 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
254 else if (bfd_get_section_alignment (dynobj
, s
) < lsect
->alignment
)
255 bfd_set_section_alignment (dynobj
, s
, lsect
->alignment
);
257 s
->_raw_size
= align_power (s
->_raw_size
, lsect
->alignment
);
259 /* Is there a hole we have to provide? If so check whether the segment is
261 if (lsect
->hole_size
)
263 lsect
->hole_offset
= s
->_raw_size
;
264 s
->_raw_size
+= lsect
->hole_size
;
265 if (lsect
->hole_offset
> lsect
->max_hole_offset
)
267 (*_bfd_error_handler
) ("%s: Section %s is already to large to put hole of %ld bytes in",
268 bfd_get_filename (abfd
),
270 (long)lsect
->hole_size
);
272 bfd_set_error (bfd_error_bad_value
);
273 return (elf_linker_section_t
*)0;
278 fprintf (stderr
, "Creating section %s, current size = %ld\n",
279 lsect
->name
, (long)s
->_raw_size
);
284 struct elf_link_hash_entry
*h
= NULL
;
286 fprintf (stderr
, "Adding %s to section %s\n",
290 h
= (struct elf_link_hash_entry
*)
291 bfd_link_hash_lookup (info
->hash
, lsect
->sym_name
, false, false, false);
293 if ((h
== NULL
|| h
->root
.type
== bfd_link_hash_undefined
)
294 && !(_bfd_generic_link_add_one_symbol (info
,
300 ? s
->_raw_size
- lsect
->hole_size
+ lsect
->sym_offset
301 : lsect
->sym_offset
),
304 get_elf_backend_data (abfd
)->collect
,
305 (struct bfd_link_hash_entry
**) &h
)))
306 return (elf_linker_section_t
*)0;
308 if ((defaults
->which
!= LINKER_SECTION_SDATA
)
309 && (defaults
->which
!= LINKER_SECTION_SDATA2
))
310 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_DYNAMIC
;
312 h
->type
= STT_OBJECT
;
316 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
317 return (elf_linker_section_t
*)0;
321 /* Find the related sections if they have been created */
322 if (lsect
->bss_name
&& !lsect
->bss_section
)
323 lsect
->bss_section
= bfd_get_section_by_name (dynobj
, lsect
->bss_name
);
325 if (lsect
->rel_name
&& !lsect
->rel_section
)
326 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
332 /* Find a linker generated pointer with a given addend and type. */
334 elf_linker_section_pointers_t
*
335 _bfd_elf_find_pointer_linker_section (linker_pointers
, addend
, which
)
336 elf_linker_section_pointers_t
*linker_pointers
;
337 bfd_signed_vma addend
;
338 elf_linker_section_enum_t which
;
340 for ( ; linker_pointers
!= NULL
; linker_pointers
= linker_pointers
->next
)
342 if (which
== linker_pointers
->which
&& addend
== linker_pointers
->addend
)
343 return linker_pointers
;
346 return (elf_linker_section_pointers_t
*)0;
350 /* Make the .rela section corresponding to the generated linker section. */
353 _bfd_elf_make_linker_section_rela (dynobj
, lsect
, alignment
)
355 elf_linker_section_t
*lsect
;
358 if (lsect
->rel_section
)
361 lsect
->rel_section
= bfd_get_section_by_name (dynobj
, lsect
->rel_name
);
362 if (lsect
->rel_section
== NULL
)
364 lsect
->rel_section
= bfd_make_section (dynobj
, lsect
->rel_name
);
365 if (lsect
->rel_section
== NULL
366 || ! bfd_set_section_flags (dynobj
,
374 || ! bfd_set_section_alignment (dynobj
, lsect
->rel_section
, alignment
))