Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF linking support for BFD. |
305c7206 | 2 | Copyright 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc. |
252b5132 RH |
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., 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 | ||
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 | int ptralign; | |
37 | ||
38 | /* This function may be called more than once. */ | |
39 | if (bfd_get_section_by_name (abfd, ".got") != NULL) | |
40 | return true; | |
41 | ||
42 | switch (bed->s->arch_size) | |
43 | { | |
bb0deeff AO |
44 | case 32: |
45 | ptralign = 2; | |
46 | break; | |
47 | ||
48 | case 64: | |
49 | ptralign = 3; | |
50 | break; | |
51 | ||
52 | default: | |
53 | bfd_set_error (bfd_error_bad_value); | |
54 | return false; | |
252b5132 RH |
55 | } |
56 | ||
57 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
58 | | SEC_LINKER_CREATED); | |
59 | ||
60 | s = bfd_make_section (abfd, ".got"); | |
61 | if (s == NULL | |
62 | || !bfd_set_section_flags (abfd, s, flags) | |
63 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
64 | return false; | |
65 | ||
66 | if (bed->want_got_plt) | |
67 | { | |
68 | s = bfd_make_section (abfd, ".got.plt"); | |
69 | if (s == NULL | |
70 | || !bfd_set_section_flags (abfd, s, flags) | |
71 | || !bfd_set_section_alignment (abfd, s, ptralign)) | |
72 | return false; | |
73 | } | |
74 | ||
75 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
76 | (or .got.plt) section. We don't do this in the linker script | |
77 | because we don't want to define the symbol if we are not creating | |
78 | a global offset table. */ | |
79 | h = NULL; | |
80 | if (!(_bfd_generic_link_add_one_symbol | |
81 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, | |
82 | bed->got_symbol_offset, (const char *) NULL, false, | |
83 | bed->collect, (struct bfd_link_hash_entry **) &h))) | |
84 | return false; | |
85 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
86 | h->type = STT_OBJECT; | |
87 | ||
88 | if (info->shared | |
89 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
90 | return false; | |
91 | ||
92 | elf_hash_table (info)->hgot = h; | |
93 | ||
94 | /* The first bit of the global offset table is the header. */ | |
95 | s->_raw_size += bed->got_header_size + bed->got_symbol_offset; | |
96 | ||
97 | return true; | |
98 | } | |
99 | \f | |
100 | ||
101 | /* Create dynamic sections when linking against a dynamic object. */ | |
102 | ||
103 | boolean | |
104 | _bfd_elf_create_dynamic_sections (abfd, info) | |
105 | bfd *abfd; | |
106 | struct bfd_link_info *info; | |
107 | { | |
108 | flagword flags, pltflags; | |
109 | register asection *s; | |
110 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7442e600 | 111 | int ptralign = 0; |
252b5132 RH |
112 | |
113 | switch (bed->s->arch_size) | |
114 | { | |
bb0deeff AO |
115 | case 32: |
116 | ptralign = 2; | |
117 | break; | |
118 | ||
119 | case 64: | |
120 | ptralign = 3; | |
121 | break; | |
122 | ||
123 | default: | |
124 | bfd_set_error (bfd_error_bad_value); | |
125 | return false; | |
252b5132 RH |
126 | } |
127 | ||
128 | /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and | |
129 | .rel[a].bss sections. */ | |
130 | ||
131 | flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | |
132 | | SEC_LINKER_CREATED); | |
133 | ||
134 | pltflags = flags; | |
135 | pltflags |= SEC_CODE; | |
136 | if (bed->plt_not_loaded) | |
137 | pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS); | |
138 | if (bed->plt_readonly) | |
139 | pltflags |= SEC_READONLY; | |
140 | ||
141 | s = bfd_make_section (abfd, ".plt"); | |
142 | if (s == NULL | |
143 | || ! bfd_set_section_flags (abfd, s, pltflags) | |
144 | || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) | |
145 | return false; | |
146 | ||
147 | if (bed->want_plt_sym) | |
148 | { | |
149 | /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the | |
150 | .plt section. */ | |
151 | struct elf_link_hash_entry *h = NULL; | |
152 | if (! (_bfd_generic_link_add_one_symbol | |
153 | (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, | |
154 | (bfd_vma) 0, (const char *) NULL, false, | |
155 | get_elf_backend_data (abfd)->collect, | |
156 | (struct bfd_link_hash_entry **) &h))) | |
157 | return false; | |
158 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
159 | h->type = STT_OBJECT; | |
160 | ||
161 | if (info->shared | |
162 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
163 | return false; | |
164 | } | |
165 | ||
bf572ba0 MM |
166 | s = bfd_make_section (abfd, |
167 | bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); | |
252b5132 RH |
168 | if (s == NULL |
169 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
170 | || ! bfd_set_section_alignment (abfd, s, ptralign)) | |
171 | return false; | |
172 | ||
173 | if (! _bfd_elf_create_got_section (abfd, info)) | |
174 | return false; | |
175 | ||
3018b441 RH |
176 | if (bed->want_dynbss) |
177 | { | |
178 | /* The .dynbss section is a place to put symbols which are defined | |
179 | by dynamic objects, are referenced by regular objects, and are | |
180 | not functions. We must allocate space for them in the process | |
181 | image and use a R_*_COPY reloc to tell the dynamic linker to | |
182 | initialize them at run time. The linker script puts the .dynbss | |
183 | section into the .bss section of the final image. */ | |
184 | s = bfd_make_section (abfd, ".dynbss"); | |
185 | if (s == NULL | |
186 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) | |
187 | return false; | |
252b5132 | 188 | |
3018b441 | 189 | /* The .rel[a].bss section holds copy relocs. This section is not |
252b5132 RH |
190 | normally needed. We need to create it here, though, so that the |
191 | linker will map it to an output section. We can't just create it | |
192 | only if we need it, because we will not know whether we need it | |
193 | until we have seen all the input files, and the first time the | |
194 | main linker code calls BFD after examining all the input files | |
195 | (size_dynamic_sections) the input sections have already been | |
196 | mapped to the output sections. If the section turns out not to | |
197 | be needed, we can discard it later. We will never need this | |
198 | section when generating a shared object, since they do not use | |
199 | copy relocs. */ | |
3018b441 RH |
200 | if (! info->shared) |
201 | { | |
202 | s = bfd_make_section (abfd, | |
203 | (bed->default_use_rela_p | |
204 | ? ".rela.bss" : ".rel.bss")); | |
205 | if (s == NULL | |
206 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
207 | || ! bfd_set_section_alignment (abfd, s, ptralign)) | |
208 | return false; | |
209 | } | |
252b5132 RH |
210 | } |
211 | ||
212 | return true; | |
213 | } | |
214 | \f | |
215 | ||
216 | /* Record a new dynamic symbol. We record the dynamic symbols as we | |
217 | read the input files, since we need to have a list of all of them | |
218 | before we can determine the final sizes of the output sections. | |
219 | Note that we may actually call this function even though we are not | |
220 | going to output any dynamic symbols; in some cases we know that a | |
221 | symbol should be in the dynamic symbol table, but only if there is | |
222 | one. */ | |
223 | ||
224 | boolean | |
225 | _bfd_elf_link_record_dynamic_symbol (info, h) | |
226 | struct bfd_link_info *info; | |
227 | struct elf_link_hash_entry *h; | |
228 | { | |
229 | if (h->dynindx == -1) | |
230 | { | |
231 | struct bfd_strtab_hash *dynstr; | |
232 | char *p, *alc; | |
233 | const char *name; | |
234 | boolean copy; | |
235 | bfd_size_type indx; | |
236 | ||
7a13edea NC |
237 | /* XXX: The ABI draft says the linker must turn hidden and |
238 | internal symbols into STB_LOCAL symbols when producing the | |
239 | DSO. However, if ld.so honors st_other in the dynamic table, | |
240 | this would not be necessary. */ | |
241 | switch (ELF_ST_VISIBILITY (h->other)) | |
242 | { | |
243 | case STV_INTERNAL: | |
244 | case STV_HIDDEN: | |
9d6eee78 L |
245 | if (h->root.type != bfd_link_hash_undefined |
246 | && h->root.type != bfd_link_hash_undefweak) | |
38048eb9 L |
247 | { |
248 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; | |
249 | return true; | |
7a13edea | 250 | } |
0444bdd4 | 251 | |
7a13edea NC |
252 | default: |
253 | break; | |
254 | } | |
255 | ||
252b5132 RH |
256 | h->dynindx = elf_hash_table (info)->dynsymcount; |
257 | ++elf_hash_table (info)->dynsymcount; | |
258 | ||
259 | dynstr = elf_hash_table (info)->dynstr; | |
260 | if (dynstr == NULL) | |
261 | { | |
262 | /* Create a strtab to hold the dynamic symbol names. */ | |
263 | elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init (); | |
264 | if (dynstr == NULL) | |
265 | return false; | |
266 | } | |
267 | ||
268 | /* We don't put any version information in the dynamic string | |
269 | table. */ | |
270 | name = h->root.root.string; | |
271 | p = strchr (name, ELF_VER_CHR); | |
272 | if (p == NULL) | |
273 | { | |
274 | alc = NULL; | |
275 | copy = false; | |
276 | } | |
277 | else | |
278 | { | |
279 | alc = bfd_malloc (p - name + 1); | |
280 | if (alc == NULL) | |
281 | return false; | |
282 | strncpy (alc, name, p - name); | |
283 | alc[p - name] = '\0'; | |
284 | name = alc; | |
285 | copy = true; | |
286 | } | |
287 | ||
288 | indx = _bfd_stringtab_add (dynstr, name, true, copy); | |
289 | ||
290 | if (alc != NULL) | |
291 | free (alc); | |
292 | ||
293 | if (indx == (bfd_size_type) -1) | |
294 | return false; | |
295 | h->dynstr_index = indx; | |
296 | } | |
297 | ||
298 | return true; | |
299 | } | |
42751cf3 | 300 | |
30b30c21 | 301 | /* Return the dynindex of a local dynamic symbol. */ |
42751cf3 | 302 | |
30b30c21 RH |
303 | long |
304 | _bfd_elf_link_lookup_local_dynindx (info, input_bfd, input_indx) | |
305 | struct bfd_link_info *info; | |
306 | bfd *input_bfd; | |
307 | long input_indx; | |
308 | { | |
309 | struct elf_link_local_dynamic_entry *e; | |
310 | ||
311 | for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) | |
312 | if (e->input_bfd == input_bfd && e->input_indx == input_indx) | |
313 | return e->dynindx; | |
314 | return -1; | |
315 | } | |
316 | ||
317 | /* This function is used to renumber the dynamic symbols, if some of | |
318 | them are removed because they are marked as local. This is called | |
319 | via elf_link_hash_traverse. */ | |
320 | ||
321 | static boolean elf_link_renumber_hash_table_dynsyms | |
322 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
323 | ||
324 | static boolean | |
325 | elf_link_renumber_hash_table_dynsyms (h, data) | |
42751cf3 | 326 | struct elf_link_hash_entry *h; |
30b30c21 | 327 | PTR data; |
42751cf3 | 328 | { |
30b30c21 RH |
329 | size_t *count = (size_t *) data; |
330 | ||
42751cf3 | 331 | if (h->dynindx != -1) |
30b30c21 RH |
332 | h->dynindx = ++(*count); |
333 | ||
42751cf3 MM |
334 | return true; |
335 | } | |
30b30c21 RH |
336 | |
337 | /* Assign dynsym indicies. In a shared library we generate a section | |
338 | symbol for each output section, which come first. Next come all of | |
339 | the back-end allocated local dynamic syms, followed by the rest of | |
340 | the global symbols. */ | |
341 | ||
342 | unsigned long | |
343 | _bfd_elf_link_renumber_dynsyms (output_bfd, info) | |
344 | bfd *output_bfd; | |
345 | struct bfd_link_info *info; | |
346 | { | |
347 | unsigned long dynsymcount = 0; | |
348 | ||
349 | if (info->shared) | |
350 | { | |
351 | asection *p; | |
352 | for (p = output_bfd->sections; p ; p = p->next) | |
353 | elf_section_data (p)->dynindx = ++dynsymcount; | |
354 | } | |
355 | ||
356 | if (elf_hash_table (info)->dynlocal) | |
357 | { | |
358 | struct elf_link_local_dynamic_entry *p; | |
359 | for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) | |
360 | p->dynindx = ++dynsymcount; | |
361 | } | |
362 | ||
363 | elf_link_hash_traverse (elf_hash_table (info), | |
364 | elf_link_renumber_hash_table_dynsyms, | |
365 | &dynsymcount); | |
366 | ||
367 | /* There is an unused NULL entry at the head of the table which | |
368 | we must account for in our count. Unless there weren't any | |
369 | symbols, which means we'll have no table at all. */ | |
370 | if (dynsymcount != 0) | |
371 | ++dynsymcount; | |
372 | ||
373 | return elf_hash_table (info)->dynsymcount = dynsymcount; | |
374 | } | |
252b5132 | 375 | \f |
30b30c21 RH |
376 | /* Create a special linker section, or return a pointer to a linker |
377 | section already created */ | |
252b5132 RH |
378 | |
379 | elf_linker_section_t * | |
380 | _bfd_elf_create_linker_section (abfd, info, which, defaults) | |
381 | bfd *abfd; | |
382 | struct bfd_link_info *info; | |
383 | enum elf_linker_section_enum which; | |
384 | elf_linker_section_t *defaults; | |
385 | { | |
386 | bfd *dynobj = elf_hash_table (info)->dynobj; | |
387 | elf_linker_section_t *lsect; | |
388 | ||
389 | /* Record the first bfd section that needs the special section */ | |
390 | if (!dynobj) | |
391 | dynobj = elf_hash_table (info)->dynobj = abfd; | |
392 | ||
393 | /* If this is the first time, create the section */ | |
394 | lsect = elf_linker_section (dynobj, which); | |
395 | if (!lsect) | |
396 | { | |
397 | asection *s; | |
398 | ||
399 | lsect = (elf_linker_section_t *) | |
400 | bfd_alloc (dynobj, sizeof (elf_linker_section_t)); | |
401 | ||
402 | *lsect = *defaults; | |
403 | elf_linker_section (dynobj, which) = lsect; | |
404 | lsect->which = which; | |
405 | lsect->hole_written_p = false; | |
406 | ||
407 | /* See if the sections already exist */ | |
408 | lsect->section = s = bfd_get_section_by_name (dynobj, lsect->name); | |
409 | if (!s || (s->flags & defaults->flags) != defaults->flags) | |
410 | { | |
411 | lsect->section = s = bfd_make_section_anyway (dynobj, lsect->name); | |
412 | ||
413 | if (s == NULL) | |
414 | return (elf_linker_section_t *)0; | |
415 | ||
416 | bfd_set_section_flags (dynobj, s, defaults->flags); | |
417 | bfd_set_section_alignment (dynobj, s, lsect->alignment); | |
418 | } | |
419 | else if (bfd_get_section_alignment (dynobj, s) < lsect->alignment) | |
420 | bfd_set_section_alignment (dynobj, s, lsect->alignment); | |
421 | ||
422 | s->_raw_size = align_power (s->_raw_size, lsect->alignment); | |
423 | ||
424 | /* Is there a hole we have to provide? If so check whether the segment is | |
425 | too big already */ | |
426 | if (lsect->hole_size) | |
427 | { | |
428 | lsect->hole_offset = s->_raw_size; | |
429 | s->_raw_size += lsect->hole_size; | |
430 | if (lsect->hole_offset > lsect->max_hole_offset) | |
431 | { | |
432 | (*_bfd_error_handler) (_("%s: Section %s is already to large to put hole of %ld bytes in"), | |
433 | bfd_get_filename (abfd), | |
434 | lsect->name, | |
435 | (long)lsect->hole_size); | |
436 | ||
437 | bfd_set_error (bfd_error_bad_value); | |
438 | return (elf_linker_section_t *)0; | |
439 | } | |
440 | } | |
441 | ||
442 | #ifdef DEBUG | |
443 | fprintf (stderr, "Creating section %s, current size = %ld\n", | |
444 | lsect->name, (long)s->_raw_size); | |
445 | #endif | |
446 | ||
447 | if (lsect->sym_name) | |
448 | { | |
449 | struct elf_link_hash_entry *h = NULL; | |
450 | #ifdef DEBUG | |
451 | fprintf (stderr, "Adding %s to section %s\n", | |
452 | lsect->sym_name, | |
453 | lsect->name); | |
454 | #endif | |
455 | h = (struct elf_link_hash_entry *) | |
456 | bfd_link_hash_lookup (info->hash, lsect->sym_name, false, false, false); | |
457 | ||
458 | if ((h == NULL || h->root.type == bfd_link_hash_undefined) | |
459 | && !(_bfd_generic_link_add_one_symbol (info, | |
460 | abfd, | |
461 | lsect->sym_name, | |
462 | BSF_GLOBAL, | |
463 | s, | |
464 | ((lsect->hole_size) | |
465 | ? s->_raw_size - lsect->hole_size + lsect->sym_offset | |
466 | : lsect->sym_offset), | |
467 | (const char *) NULL, | |
468 | false, | |
469 | get_elf_backend_data (abfd)->collect, | |
470 | (struct bfd_link_hash_entry **) &h))) | |
471 | return (elf_linker_section_t *)0; | |
472 | ||
473 | if ((defaults->which != LINKER_SECTION_SDATA) | |
474 | && (defaults->which != LINKER_SECTION_SDATA2)) | |
475 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_DYNAMIC; | |
476 | ||
477 | h->type = STT_OBJECT; | |
478 | lsect->sym_hash = h; | |
479 | ||
480 | if (info->shared | |
481 | && ! _bfd_elf_link_record_dynamic_symbol (info, h)) | |
482 | return (elf_linker_section_t *)0; | |
483 | } | |
484 | } | |
485 | ||
486 | #if 0 | |
487 | /* This does not make sense. The sections which may exist in the | |
488 | object file have nothing to do with the sections we want to | |
489 | create. */ | |
490 | ||
491 | /* Find the related sections if they have been created */ | |
492 | if (lsect->bss_name && !lsect->bss_section) | |
493 | lsect->bss_section = bfd_get_section_by_name (dynobj, lsect->bss_name); | |
494 | ||
495 | if (lsect->rel_name && !lsect->rel_section) | |
496 | lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); | |
497 | #endif | |
498 | ||
499 | return lsect; | |
500 | } | |
501 | ||
502 | \f | |
503 | /* Find a linker generated pointer with a given addend and type. */ | |
504 | ||
505 | elf_linker_section_pointers_t * | |
506 | _bfd_elf_find_pointer_linker_section (linker_pointers, addend, which) | |
507 | elf_linker_section_pointers_t *linker_pointers; | |
508 | bfd_signed_vma addend; | |
509 | elf_linker_section_enum_t which; | |
510 | { | |
511 | for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next) | |
512 | { | |
513 | if (which == linker_pointers->which && addend == linker_pointers->addend) | |
514 | return linker_pointers; | |
515 | } | |
516 | ||
517 | return (elf_linker_section_pointers_t *)0; | |
518 | } | |
519 | ||
520 | \f | |
521 | /* Make the .rela section corresponding to the generated linker section. */ | |
522 | ||
523 | boolean | |
524 | _bfd_elf_make_linker_section_rela (dynobj, lsect, alignment) | |
525 | bfd *dynobj; | |
526 | elf_linker_section_t *lsect; | |
527 | int alignment; | |
528 | { | |
529 | if (lsect->rel_section) | |
530 | return true; | |
531 | ||
532 | lsect->rel_section = bfd_get_section_by_name (dynobj, lsect->rel_name); | |
533 | if (lsect->rel_section == NULL) | |
534 | { | |
535 | lsect->rel_section = bfd_make_section (dynobj, lsect->rel_name); | |
536 | if (lsect->rel_section == NULL | |
537 | || ! bfd_set_section_flags (dynobj, | |
538 | lsect->rel_section, | |
539 | (SEC_ALLOC | |
540 | | SEC_LOAD | |
541 | | SEC_HAS_CONTENTS | |
542 | | SEC_IN_MEMORY | |
543 | | SEC_LINKER_CREATED | |
544 | | SEC_READONLY)) | |
545 | || ! bfd_set_section_alignment (dynobj, lsect->rel_section, alignment)) | |
546 | return false; | |
547 | } | |
548 | ||
549 | return true; | |
550 | } |