copyright notices
[deliverable/binutils-gdb.git] / bfd / elflink.c
1 /* ELF linking support for BFD.
2 Copyright 1995 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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., 675 Mass Ave, Cambridge, MA 02139, 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 "libelf.h"
26
27 boolean
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
43 s = bfd_make_section (abfd, ".got");
44 if (s == NULL
45 || !bfd_set_section_flags (abfd, s, flags)
46 || !bfd_set_section_alignment (abfd, s, 2))
47 return false;
48
49 if (bed->want_got_plt)
50 {
51 s = bfd_make_section (abfd, ".got.plt");
52 if (s == NULL
53 || !bfd_set_section_flags (abfd, s, flags)
54 || !bfd_set_section_alignment (abfd, s, 2))
55 return false;
56 }
57
58 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
59 (or .got.plt) section. We don't do this in the linker script
60 because we don't want to define the symbol if we are not creating
61 a global offset table. */
62 h = NULL;
63 if (!(_bfd_generic_link_add_one_symbol
64 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0,
65 (const char *) NULL, false, get_elf_backend_data (abfd)->collect,
66 (struct bfd_link_hash_entry **) &h)))
67 return false;
68 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
69 h->type = STT_OBJECT;
70
71 if (info->shared
72 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
73 return false;
74
75 /* The first three global offset table entries are reserved. */
76 s->_raw_size += 3 * 4;
77
78 return true;
79 }
80
81 /* Create dynamic sections when linking against a dynamic object. */
82
83 boolean
84 _bfd_elf_create_dynamic_sections (abfd, info)
85 bfd *abfd;
86 struct bfd_link_info *info;
87 {
88 flagword flags;
89 register asection *s;
90 struct elf_backend_data *bed = get_elf_backend_data (abfd);
91
92 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
93 .rel[a].bss sections. */
94
95 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
96
97 s = bfd_make_section (abfd, ".plt");
98 if (s == NULL
99 || ! bfd_set_section_flags (abfd, s,
100 (flags | SEC_CODE
101 | (bed->plt_readonly ? SEC_READONLY : 0)))
102 || ! bfd_set_section_alignment (abfd, s, 2))
103 return false;
104
105 if (bed->want_plt_sym)
106 {
107 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
108 .plt section. */
109 struct elf_link_hash_entry *h = NULL;
110 if (! (_bfd_generic_link_add_one_symbol
111 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
112 (bfd_vma) 0, (const char *) NULL, false,
113 get_elf_backend_data (abfd)->collect,
114 (struct bfd_link_hash_entry **) &h)))
115 return false;
116 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
117 h->type = STT_OBJECT;
118
119 if (info->shared
120 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
121 return false;
122 }
123
124 s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.plt" : ".rel.plt");
125 if (s == NULL
126 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
127 || ! bfd_set_section_alignment (abfd, s, 2))
128 return false;
129
130 if (! _bfd_elf_create_got_section (abfd, info))
131 return false;
132
133 /* The .dynbss section is a place to put symbols which are defined
134 by dynamic objects, are referenced by regular objects, and are
135 not functions. We must allocate space for them in the process
136 image and use a R_*_COPY reloc to tell the dynamic linker to
137 initialize them at run time. The linker script puts the .dynbss
138 section into the .bss section of the final image. */
139 s = bfd_make_section (abfd, ".dynbss");
140 if (s == NULL
141 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
142 return false;
143
144 /* The .rel[a].bss section holds copy relocs. This section is not
145 normally needed. We need to create it here, though, so that the
146 linker will map it to an output section. We can't just create it
147 only if we need it, because we will not know whether we need it
148 until we have seen all the input files, and the first time the
149 main linker code calls BFD after examining all the input files
150 (size_dynamic_sections) the input sections have already been
151 mapped to the output sections. If the section turns out not to
152 be needed, we can discard it later. We will never need this
153 section when generating a shared object, since they do not use
154 copy relocs. */
155 if (! info->shared)
156 {
157 s = bfd_make_section (abfd, bed->use_rela_p ? ".rela.bss" : ".rel.bss");
158 if (s == NULL
159 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
160 || ! bfd_set_section_alignment (abfd, s, 2))
161 return false;
162 }
163
164 return true;
165 }
166
167 /* Record a new dynamic symbol. We record the dynamic symbols as we
168 read the input files, since we need to have a list of all of them
169 before we can determine the final sizes of the output sections.
170 Note that we may actually call this function even though we are not
171 going to output any dynamic symbols; in some cases we know that a
172 symbol should be in the dynamic symbol table, but only if there is
173 one. */
174
175 boolean
176 _bfd_elf_link_record_dynamic_symbol (info, h)
177 struct bfd_link_info *info;
178 struct elf_link_hash_entry *h;
179 {
180 if (h->dynindx == -1)
181 {
182 struct bfd_strtab_hash *dynstr;
183
184 h->dynindx = elf_hash_table (info)->dynsymcount;
185 ++elf_hash_table (info)->dynsymcount;
186
187 dynstr = elf_hash_table (info)->dynstr;
188 if (dynstr == NULL)
189 {
190 /* Create a strtab to hold the dynamic symbol names. */
191 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
192 if (dynstr == NULL)
193 return false;
194 }
195
196 h->dynstr_index = ((unsigned long)
197 _bfd_stringtab_add (dynstr, h->root.root.string,
198 true, false));
199 if (h->dynstr_index == (unsigned long) -1)
200 return false;
201 }
202
203 return true;
204 }
This page took 0.034259 seconds and 4 git commands to generate.