elflink.c revision 33965
1/* ELF linking support for BFD.
2   Copyright 1995, 1996, 1997 Free Software Foundation, Inc.
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
37  /* This function may be called more than once.  */
38  if (bfd_get_section_by_name (abfd, ".got") != NULL)
39    return true;
40
41  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
42	   | SEC_LINKER_CREATED);
43
44  s = bfd_make_section (abfd, ".got");
45  if (s == NULL
46      || !bfd_set_section_flags (abfd, s, flags)
47      || !bfd_set_section_alignment (abfd, s, 2))
48    return false;
49
50  if (bed->want_got_plt)
51    {
52      s = bfd_make_section (abfd, ".got.plt");
53      if (s == NULL
54	  || !bfd_set_section_flags (abfd, s, flags)
55	  || !bfd_set_section_alignment (abfd, s, 2))
56	return false;
57    }
58
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.  */
63  h = NULL;
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)))
68    return false;
69  h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
70  h->type = STT_OBJECT;
71
72  if (info->shared
73      && ! _bfd_elf_link_record_dynamic_symbol (info, h))
74    return false;
75
76  elf_hash_table (info)->hgot = h;
77
78  /* The first three global offset table entries are reserved.  */
79  s->_raw_size += 3 * 4;
80
81  return true;
82}
83
84
85/* Create dynamic sections when linking against a dynamic object.  */
86
87boolean
88_bfd_elf_create_dynamic_sections (abfd, info)
89     bfd *abfd;
90     struct bfd_link_info *info;
91{
92  flagword flags;
93  register asection *s;
94  struct elf_backend_data *bed = get_elf_backend_data (abfd);
95
96  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
97     .rel[a].bss sections.  */
98
99  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
100	   | SEC_LINKER_CREATED);
101
102  s = bfd_make_section (abfd, ".plt");
103  if (s == NULL
104      || ! bfd_set_section_flags (abfd, s,
105				  (flags | SEC_CODE
106				   | (bed->plt_readonly ? SEC_READONLY : 0)))
107      || ! bfd_set_section_alignment (abfd, s, 2))
108    return false;
109
110  if (bed->want_plt_sym)
111    {
112      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
113	 .plt section.  */
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)))
120	return false;
121      h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
122      h->type = STT_OBJECT;
123
124      if (info->shared
125	  && ! _bfd_elf_link_record_dynamic_symbol (info, h))
126	return false;
127    }
128
129  s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.plt" : ".rel.plt");
130  if (s == NULL
131      || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
132      || ! bfd_set_section_alignment (abfd, s, 2))
133    return false;
134
135  if (! _bfd_elf_create_got_section (abfd, info))
136    return false;
137
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");
145  if (s == NULL
146      || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
147    return false;
148
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
159     copy relocs.  */
160  if (! info->shared)
161    {
162      s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.bss" : ".rel.bss");
163      if (s == NULL
164	  || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
165	  || ! bfd_set_section_alignment (abfd, s, 2))
166	return false;
167    }
168
169  return true;
170}
171
172
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
179   one.  */
180
181boolean
182_bfd_elf_link_record_dynamic_symbol (info, h)
183     struct bfd_link_info *info;
184     struct elf_link_hash_entry *h;
185{
186  if (h->dynindx == -1)
187    {
188      struct bfd_strtab_hash *dynstr;
189      char *p, *alc;
190      const char *name;
191      boolean copy;
192      bfd_size_type indx;
193
194      h->dynindx = elf_hash_table (info)->dynsymcount;
195      ++elf_hash_table (info)->dynsymcount;
196
197      dynstr = elf_hash_table (info)->dynstr;
198      if (dynstr == NULL)
199	{
200	  /* Create a strtab to hold the dynamic symbol names.  */
201	  elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
202	  if (dynstr == NULL)
203	    return false;
204	}
205
206      /* We don't put any version information in the dynamic string
207         table.  */
208      name = h->root.root.string;
209      p = strchr (name, ELF_VER_CHR);
210      if (p == NULL)
211	{
212	  alc = NULL;
213	  copy = false;
214	}
215      else
216	{
217	  alc = bfd_malloc (p - name + 1);
218	  if (alc == NULL)
219	    return false;
220	  strncpy (alc, name, p - name);
221	  alc[p - name] = '\0';
222	  name = alc;
223	  copy = true;
224	}
225
226      indx = _bfd_stringtab_add (dynstr, name, true, copy);
227
228      if (alc != NULL)
229	free (alc);
230
231      if (indx == (bfd_size_type) -1)
232	return false;
233      h->dynstr_index = indx;
234    }
235
236  return true;
237}
238
239/* Create a special linker section, or return a pointer to a linker section already created  */
240
241elf_linker_section_t *
242_bfd_elf_create_linker_section (abfd, info, which, defaults)
243     bfd *abfd;
244     struct bfd_link_info *info;
245     enum elf_linker_section_enum which;
246     elf_linker_section_t *defaults;
247{
248  bfd *dynobj = elf_hash_table (info)->dynobj;
249  elf_linker_section_t *lsect;
250
251  /* Record the first bfd section that needs the special section */
252  if (!dynobj)
253    dynobj = elf_hash_table (info)->dynobj = abfd;
254
255  /* If this is the first time, create the section */
256  lsect = elf_linker_section (dynobj, which);
257  if (!lsect)
258    {
259      asection *s;
260
261      lsect = (elf_linker_section_t *)
262	bfd_alloc (dynobj, sizeof (elf_linker_section_t));
263
264      *lsect = *defaults;
265      elf_linker_section (dynobj, which) = lsect;
266      lsect->which = which;
267      lsect->hole_written_p = false;
268
269      /* See if the sections already exist */
270      lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name);
271      if (!s || (s->flags & defaults->flags) != defaults->flags)
272	{
273	  lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name);
274
275	  if (s == NULL)
276	    return (elf_linker_section_t *)0;
277
278	  bfd_set_section_flags (dynobj, s, defaults->flags);
279	  bfd_set_section_alignment (dynobj, s, lsect->alignment);
280	}
281      else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment)
282	bfd_set_section_alignment (dynobj, s, lsect->alignment);
283
284      s->_raw_size = align_power (s->_raw_size, lsect->alignment);
285
286      /* Is there a hole we have to provide?  If so check whether the segment is
287	 too big already */
288      if (lsect->hole_size)
289	{
290	  lsect->hole_offset = s->_raw_size;
291	  s->_raw_size += lsect->hole_size;
292	  if (lsect->hole_offset > lsect->max_hole_offset)
293	    {
294	      (*_bfd_error_handler) ("%s: Section %s is already to large to put hole of %ld bytes in",
295				     bfd_get_filename (abfd),
296				     lsect->name,
297				     (long)lsect->hole_size);
298
299	      bfd_set_error (bfd_error_bad_value);
300	      return (elf_linker_section_t *)0;
301	    }
302	}
303
304#ifdef DEBUG
305      fprintf (stderr, "Creating section %s, current size = %ld\n",
306	       lsect->name, (long)s->_raw_size);
307#endif
308
309      if (lsect->sym_name)
310	{
311	  struct elf_link_hash_entry *h = NULL;
312#ifdef DEBUG
313	  fprintf (stderr, "Adding %s to section %s\n",
314		   lsect->sym_name,
315		   lsect->name);
316#endif
317	  h = (struct elf_link_hash_entry *)
318	    bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false);
319
320	  if ((h == NULL || h->root.type == bfd_link_hash_undefined)
321	      && !(_bfd_generic_link_add_one_symbol (info,
322						     abfd,
323						     lsect->sym_name,
324						     BSF_GLOBAL,
325						     s,
326						     ((lsect->hole_size)
327						      ? s->_raw_size - lsect->hole_size + lsect->sym_offset
328						      : lsect->sym_offset),
329						     (const char *) NULL,
330						     false,
331						     get_elf_backend_data (abfd)->collect,
332						     (struct bfd_link_hash_entry **) &h)))
333	    return (elf_linker_section_t *)0;
334
335	  if ((defaults->which != LINKER_SECTION_SDATA)
336	      && (defaults->which != LINKER_SECTION_SDATA2))
337	    h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC;
338
339	  h->type = STT_OBJECT;
340	  lsect->sym_hash = h;
341
342	  if (info->shared
343	      && ! _bfd_elf_link_record_dynamic_symbol (info, h))
344	    return (elf_linker_section_t *)0;
345	}
346    }
347
348#if 0
349  /* This does not make sense.  The sections which may exist in the
350     object file have nothing to do with the sections we want to
351     create.  */
352
353  /* Find the related sections if they have been created */
354  if (lsect->bss_name && !lsect->bss_section)
355    lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name);
356
357  if (lsect->rel_name && !lsect->rel_section)
358    lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
359#endif
360
361  return lsect;
362}
363
364
365/* Find a linker generated pointer with a given addend and type.  */
366
367elf_linker_section_pointers_t *
368_bfd_elf_find_pointer_linker_section (linker_pointers, addend, which)
369     elf_linker_section_pointers_t *linker_pointers;
370     bfd_signed_vma addend;
371     elf_linker_section_enum_t which;
372{
373  for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
374    {
375      if (which == linker_pointers->which && addend == linker_pointers->addend)
376	return linker_pointers;
377    }
378
379  return (elf_linker_section_pointers_t *)0;
380}
381
382
383/* Make the .rela section corresponding to the generated linker section.  */
384
385boolean
386_bfd_elf_make_linker_section_rela (dynobj, lsect, alignment)
387     bfd *dynobj;
388     elf_linker_section_t *lsect;
389     int alignment;
390{
391  if (lsect->rel_section)
392    return true;
393
394  lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name);
395  if (lsect->rel_section == NULL)
396    {
397      lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name);
398      if (lsect->rel_section == NULL
399	  || ! bfd_set_section_flags (dynobj,
400				      lsect->rel_section,
401				      (SEC_ALLOC
402				       | SEC_LOAD
403				       | SEC_HAS_CONTENTS
404				       | SEC_IN_MEMORY
405				       | SEC_LINKER_CREATED
406				       | SEC_READONLY))
407	  || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment))
408	return false;
409    }
410
411  return true;
412}
413