* gdb.texinfo: Add some credits, mention bug monitor.
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
32090b8e 1/* ELF executable support for BFD.
f6727b90 2 Copyright 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
32090b8e
KR
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
6f904fce 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32090b8e 19
d1b44e83
ILT
20/*
21
22SECTION
23 ELF backends
24
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
28
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet.
32 */
33
32090b8e
KR
34#include "bfd.h"
35#include "sysdep.h"
013dec1a 36#include "bfdlink.h"
32090b8e
KR
37#include "libbfd.h"
38#define ARCH_SIZE 0
6ab826bd 39#include "elf-bfd.h"
32090b8e 40
fd0198f0 41static INLINE struct elf_segment_map *make_mapping
edf3fe48 42 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
191d910c 43static boolean map_sections_to_segments PARAMS ((bfd *));
fd0198f0
ILT
44static int elf_sort_sections PARAMS ((const PTR, const PTR));
45static boolean assign_file_positions_for_segments PARAMS ((bfd *));
46static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
ede4eed4
KR
47static boolean prep_headers PARAMS ((bfd *));
48static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **));
3dbf33ee 49static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
ea3f0585
FF
50static char *elf_read PARAMS ((bfd *, long, unsigned int));
51static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
52static boolean assign_section_numbers PARAMS ((bfd *));
53static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
54static boolean elf_map_symbols PARAMS ((bfd *));
55static bfd_size_type get_program_header_size PARAMS ((bfd *));
ede4eed4 56
a66a61a0
ILT
57/* Swap version information in and out. The version information is
58 currently size independent. If that ever changes, this code will
59 need to move into elfcode.h. */
60
61/* Swap in a Verdef structure. */
62
63void
64_bfd_elf_swap_verdef_in (abfd, src, dst)
65 bfd *abfd;
66 const Elf_External_Verdef *src;
67 Elf_Internal_Verdef *dst;
68{
69 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
70 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
71 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
72 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
73 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
74 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
75 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
76}
77
78/* Swap out a Verdef structure. */
79
80void
81_bfd_elf_swap_verdef_out (abfd, src, dst)
82 bfd *abfd;
83 const Elf_Internal_Verdef *src;
84 Elf_External_Verdef *dst;
85{
86 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
87 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
88 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
91 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
92 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
93}
94
95/* Swap in a Verdaux structure. */
96
97void
98_bfd_elf_swap_verdaux_in (abfd, src, dst)
99 bfd *abfd;
100 const Elf_External_Verdaux *src;
101 Elf_Internal_Verdaux *dst;
102{
103 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
104 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
105}
106
107/* Swap out a Verdaux structure. */
108
109void
110_bfd_elf_swap_verdaux_out (abfd, src, dst)
111 bfd *abfd;
112 const Elf_Internal_Verdaux *src;
113 Elf_External_Verdaux *dst;
114{
115 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
116 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
117}
118
119/* Swap in a Verneed structure. */
120
121void
122_bfd_elf_swap_verneed_in (abfd, src, dst)
123 bfd *abfd;
124 const Elf_External_Verneed *src;
125 Elf_Internal_Verneed *dst;
126{
127 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
128 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
129 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
130 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
131 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
132}
133
134/* Swap out a Verneed structure. */
135
136void
137_bfd_elf_swap_verneed_out (abfd, src, dst)
138 bfd *abfd;
139 const Elf_Internal_Verneed *src;
140 Elf_External_Verneed *dst;
141{
142 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
143 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
144 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
145 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
146 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
147}
148
149/* Swap in a Vernaux structure. */
150
151void
152_bfd_elf_swap_vernaux_in (abfd, src, dst)
153 bfd *abfd;
154 const Elf_External_Vernaux *src;
155 Elf_Internal_Vernaux *dst;
156{
157 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
158 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
159 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
160 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
161 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
162}
163
164/* Swap out a Vernaux structure. */
165
166void
167_bfd_elf_swap_vernaux_out (abfd, src, dst)
168 bfd *abfd;
169 const Elf_Internal_Vernaux *src;
170 Elf_External_Vernaux *dst;
171{
172 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
173 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
174 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
175 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
176 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
177}
178
179/* Swap in a Versym structure. */
180
181void
182_bfd_elf_swap_versym_in (abfd, src, dst)
183 bfd *abfd;
184 const Elf_External_Versym *src;
185 Elf_Internal_Versym *dst;
186{
187 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
188}
189
190/* Swap out a Versym structure. */
191
192void
193_bfd_elf_swap_versym_out (abfd, src, dst)
194 bfd *abfd;
195 const Elf_Internal_Versym *src;
196 Elf_External_Versym *dst;
197{
198 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
199}
200
32090b8e
KR
201/* Standard ELF hash function. Do not change this function; you will
202 cause invalid hash tables to be generated. (Well, you would if this
203 were being used yet.) */
204unsigned long
013dec1a
ILT
205bfd_elf_hash (name)
206 CONST unsigned char *name;
32090b8e
KR
207{
208 unsigned long h = 0;
209 unsigned long g;
210 int ch;
211
212 while ((ch = *name++) != '\0')
213 {
214 h = (h << 4) + ch;
215 if ((g = (h & 0xf0000000)) != 0)
216 {
217 h ^= g >> 24;
218 h &= ~g;
219 }
220 }
221 return h;
222}
223
224/* Read a specified number of bytes at a specified offset in an ELF
225 file, into a newly allocated buffer, and return a pointer to the
226 buffer. */
227
228static char *
013dec1a
ILT
229elf_read (abfd, offset, size)
230 bfd * abfd;
231 long offset;
ae115e51 232 unsigned int size;
32090b8e
KR
233{
234 char *buf;
235
236 if ((buf = bfd_alloc (abfd, size)) == NULL)
a9713b91 237 return NULL;
32090b8e 238 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
013dec1a 239 return NULL;
32090b8e
KR
240 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
241 {
013dec1a
ILT
242 if (bfd_get_error () != bfd_error_system_call)
243 bfd_set_error (bfd_error_file_truncated);
32090b8e
KR
244 return NULL;
245 }
246 return buf;
247}
248
249boolean
ff12f303 250bfd_elf_mkobject (abfd)
013dec1a 251 bfd * abfd;
32090b8e
KR
252{
253 /* this just does initialization */
254 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
255 elf_tdata (abfd) = (struct elf_obj_tdata *)
256 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
257 if (elf_tdata (abfd) == 0)
a9713b91 258 return false;
32090b8e
KR
259 /* since everything is done at close time, do we need any
260 initialization? */
261
262 return true;
263}
264
265char *
ede4eed4 266bfd_elf_get_str_section (abfd, shindex)
013dec1a
ILT
267 bfd * abfd;
268 unsigned int shindex;
32090b8e
KR
269{
270 Elf_Internal_Shdr **i_shdrp;
271 char *shstrtab = NULL;
272 unsigned int offset;
273 unsigned int shstrtabsize;
274
275 i_shdrp = elf_elfsections (abfd);
276 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
277 return 0;
278
b176e1e9 279 shstrtab = (char *) i_shdrp[shindex]->contents;
32090b8e
KR
280 if (shstrtab == NULL)
281 {
282 /* No cached one, attempt to read, and cache what we read. */
283 offset = i_shdrp[shindex]->sh_offset;
284 shstrtabsize = i_shdrp[shindex]->sh_size;
285 shstrtab = elf_read (abfd, offset, shstrtabsize);
b176e1e9 286 i_shdrp[shindex]->contents = (PTR) shstrtab;
32090b8e
KR
287 }
288 return shstrtab;
289}
290
291char *
ede4eed4 292bfd_elf_string_from_elf_section (abfd, shindex, strindex)
013dec1a
ILT
293 bfd * abfd;
294 unsigned int shindex;
295 unsigned int strindex;
32090b8e
KR
296{
297 Elf_Internal_Shdr *hdr;
298
299 if (strindex == 0)
300 return "";
301
302 hdr = elf_elfsections (abfd)[shindex];
303
b176e1e9 304 if (hdr->contents == NULL
ede4eed4 305 && bfd_elf_get_str_section (abfd, shindex) == NULL)
32090b8e
KR
306 return NULL;
307
20db2495
ILT
308 if (strindex >= hdr->sh_size)
309 {
310 (*_bfd_error_handler)
311 ("%s: invalid string offset %u >= %lu for section `%s'",
312 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
313 ((shindex == elf_elfheader(abfd)->e_shstrndx
314 && strindex == hdr->sh_name)
315 ? ".shstrtab"
316 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
317 return "";
318 }
319
b176e1e9 320 return ((char *) hdr->contents) + strindex;
32090b8e
KR
321}
322
497c5434 323/* Make a BFD section from an ELF section. We store a pointer to the
b176e1e9 324 BFD section in the bfd_section field of the header. */
497c5434
ILT
325
326boolean
327_bfd_elf_make_section_from_shdr (abfd, hdr, name)
328 bfd *abfd;
329 Elf_Internal_Shdr *hdr;
330 const char *name;
331{
332 asection *newsect;
333 flagword flags;
334
b176e1e9 335 if (hdr->bfd_section != NULL)
497c5434 336 {
b176e1e9
ILT
337 BFD_ASSERT (strcmp (name,
338 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
497c5434
ILT
339 return true;
340 }
341
342 newsect = bfd_make_section_anyway (abfd, name);
343 if (newsect == NULL)
344 return false;
345
346 newsect->filepos = hdr->sh_offset;
347
348 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
349 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
350 || ! bfd_set_section_alignment (abfd, newsect,
351 bfd_log2 (hdr->sh_addralign)))
352 return false;
353
354 flags = SEC_NO_FLAGS;
355 if (hdr->sh_type != SHT_NOBITS)
356 flags |= SEC_HAS_CONTENTS;
357 if ((hdr->sh_flags & SHF_ALLOC) != 0)
358 {
359 flags |= SEC_ALLOC;
360 if (hdr->sh_type != SHT_NOBITS)
361 flags |= SEC_LOAD;
362 }
363 if ((hdr->sh_flags & SHF_WRITE) == 0)
364 flags |= SEC_READONLY;
365 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
366 flags |= SEC_CODE;
7c6da9ca 367 else if ((flags & SEC_LOAD) != 0)
497c5434
ILT
368 flags |= SEC_DATA;
369
370 /* The debugging sections appear to be recognized only by name, not
371 any sort of flag. */
372 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
373 || strncmp (name, ".line", sizeof ".line" - 1) == 0
374 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
375 flags |= SEC_DEBUGGING;
376
f0c12b73
DE
377 /* As a GNU extension, if the name begins with .gnu.linkonce, we
378 only link a single copy of the section. This is used to support
379 g++. g++ will emit each template expansion in its own section.
380 The symbols will be defined as weak, so that multiple definitions
381 are permitted. The GNU linker extension is to actually discard
382 all but one of the sections. */
383 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
384 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
385
497c5434
ILT
386 if (! bfd_set_section_flags (abfd, newsect, flags))
387 return false;
388
fd0198f0
ILT
389 if ((flags & SEC_ALLOC) != 0)
390 {
391 Elf_Internal_Phdr *phdr;
392 unsigned int i;
393
394 /* Look through the phdrs to see if we need to adjust the lma. */
395 phdr = elf_tdata (abfd)->phdr;
396 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
397 {
398 if (phdr->p_type == PT_LOAD
6933148a 399 && phdr->p_paddr != 0
fd0198f0
ILT
400 && phdr->p_vaddr != phdr->p_paddr
401 && phdr->p_vaddr <= hdr->sh_addr
b944e7e8
ILT
402 && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size
403 && ((flags & SEC_LOAD) == 0
f6727b90 404 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
b944e7e8
ILT
405 && (phdr->p_offset + phdr->p_filesz
406 >= hdr->sh_offset + hdr->sh_size))))
fd0198f0
ILT
407 {
408 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
409 break;
410 }
411 }
412 }
413
b176e1e9 414 hdr->bfd_section = newsect;
497c5434
ILT
415 elf_section_data (newsect)->this_hdr = *hdr;
416
417 return true;
418}
419
32090b8e
KR
420/*
421INTERNAL_FUNCTION
422 bfd_elf_find_section
423
424SYNOPSIS
425 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
426
427DESCRIPTION
428 Helper functions for GDB to locate the string tables.
429 Since BFD hides string tables from callers, GDB needs to use an
430 internal hook to find them. Sun's .stabstr, in particular,
431 isn't even pointed to by the .stab section, so ordinary
432 mechanisms wouldn't work to find it, even if we had some.
433*/
434
435struct elf_internal_shdr *
013dec1a
ILT
436bfd_elf_find_section (abfd, name)
437 bfd * abfd;
438 char *name;
32090b8e
KR
439{
440 Elf_Internal_Shdr **i_shdrp;
441 char *shstrtab;
442 unsigned int max;
443 unsigned int i;
444
445 i_shdrp = elf_elfsections (abfd);
446 if (i_shdrp != NULL)
447 {
ede4eed4 448 shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
32090b8e
KR
449 if (shstrtab != NULL)
450 {
451 max = elf_elfheader (abfd)->e_shnum;
452 for (i = 1; i < max; i++)
453 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
454 return i_shdrp[i];
455 }
456 }
457 return 0;
458}
459
32090b8e
KR
460const char *const bfd_elf_section_type_names[] = {
461 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
462 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
463 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
464};
465
466/* ELF relocs are against symbols. If we are producing relocateable
467 output, and the reloc is against an external symbol, and nothing
468 has given us any additional addend, the resulting reloc will also
469 be against the same symbol. In such a case, we don't want to
470 change anything about the way the reloc is handled, since it will
471 all be done at final link time. Rather than put special case code
472 into bfd_perform_relocation, all the reloc types use this howto
473 function. It just short circuits the reloc if producing
474 relocateable output against an external symbol. */
475
013dec1a 476/*ARGSUSED*/
32090b8e
KR
477bfd_reloc_status_type
478bfd_elf_generic_reloc (abfd,
479 reloc_entry,
480 symbol,
481 data,
482 input_section,
4c3721d5
ILT
483 output_bfd,
484 error_message)
32090b8e
KR
485 bfd *abfd;
486 arelent *reloc_entry;
487 asymbol *symbol;
488 PTR data;
489 asection *input_section;
490 bfd *output_bfd;
4c3721d5 491 char **error_message;
32090b8e
KR
492{
493 if (output_bfd != (bfd *) NULL
494 && (symbol->flags & BSF_SECTION_SYM) == 0
d1b44e83
ILT
495 && (! reloc_entry->howto->partial_inplace
496 || reloc_entry->addend == 0))
32090b8e
KR
497 {
498 reloc_entry->address += input_section->output_offset;
499 return bfd_reloc_ok;
500 }
501
502 return bfd_reloc_continue;
503}
013dec1a 504\f
27fb8f29
ILT
505/* Print out the program headers. */
506
507boolean
508_bfd_elf_print_private_bfd_data (abfd, farg)
509 bfd *abfd;
510 PTR farg;
511{
512 FILE *f = (FILE *) farg;
513 Elf_Internal_Phdr *p;
02fcd126
ILT
514 asection *s;
515 bfd_byte *dynbuf = NULL;
27fb8f29
ILT
516
517 p = elf_tdata (abfd)->phdr;
02fcd126 518 if (p != NULL)
27fb8f29 519 {
02fcd126 520 unsigned int i, c;
27fb8f29 521
02fcd126
ILT
522 fprintf (f, "\nProgram Header:\n");
523 c = elf_elfheader (abfd)->e_phnum;
524 for (i = 0; i < c; i++, p++)
27fb8f29 525 {
02fcd126
ILT
526 const char *s;
527 char buf[20];
528
529 switch (p->p_type)
530 {
531 case PT_NULL: s = "NULL"; break;
532 case PT_LOAD: s = "LOAD"; break;
533 case PT_DYNAMIC: s = "DYNAMIC"; break;
534 case PT_INTERP: s = "INTERP"; break;
535 case PT_NOTE: s = "NOTE"; break;
536 case PT_SHLIB: s = "SHLIB"; break;
537 case PT_PHDR: s = "PHDR"; break;
538 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
539 }
540 fprintf (f, "%8s off 0x", s);
541 fprintf_vma (f, p->p_offset);
542 fprintf (f, " vaddr 0x");
543 fprintf_vma (f, p->p_vaddr);
544 fprintf (f, " paddr 0x");
545 fprintf_vma (f, p->p_paddr);
546 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
547 fprintf (f, " filesz 0x");
548 fprintf_vma (f, p->p_filesz);
549 fprintf (f, " memsz 0x");
550 fprintf_vma (f, p->p_memsz);
551 fprintf (f, " flags %c%c%c",
552 (p->p_flags & PF_R) != 0 ? 'r' : '-',
553 (p->p_flags & PF_W) != 0 ? 'w' : '-',
554 (p->p_flags & PF_X) != 0 ? 'x' : '-');
555 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
556 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
557 fprintf (f, "\n");
558 }
559 }
560
561 s = bfd_get_section_by_name (abfd, ".dynamic");
562 if (s != NULL)
563 {
564 int elfsec;
565 unsigned long link;
566 bfd_byte *extdyn, *extdynend;
567 size_t extdynsize;
568 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
569
570 fprintf (f, "\nDynamic Section:\n");
571
572 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
573 if (dynbuf == NULL)
574 goto error_return;
575 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
576 s->_raw_size))
577 goto error_return;
578
579 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
580 if (elfsec == -1)
581 goto error_return;
582 link = elf_elfsections (abfd)[elfsec]->sh_link;
583
584 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
585 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
586
587 extdyn = dynbuf;
588 extdynend = extdyn + s->_raw_size;
589 for (; extdyn < extdynend; extdyn += extdynsize)
590 {
591 Elf_Internal_Dyn dyn;
592 const char *name;
593 char ab[20];
594 boolean stringp;
595
596 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
597
598 if (dyn.d_tag == DT_NULL)
599 break;
600
601 stringp = false;
602 switch (dyn.d_tag)
603 {
604 default:
927d05b5 605 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
02fcd126
ILT
606 name = ab;
607 break;
608
609 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
610 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
611 case DT_PLTGOT: name = "PLTGOT"; break;
612 case DT_HASH: name = "HASH"; break;
613 case DT_STRTAB: name = "STRTAB"; break;
614 case DT_SYMTAB: name = "SYMTAB"; break;
615 case DT_RELA: name = "RELA"; break;
616 case DT_RELASZ: name = "RELASZ"; break;
617 case DT_RELAENT: name = "RELAENT"; break;
618 case DT_STRSZ: name = "STRSZ"; break;
619 case DT_SYMENT: name = "SYMENT"; break;
620 case DT_INIT: name = "INIT"; break;
621 case DT_FINI: name = "FINI"; break;
622 case DT_SONAME: name = "SONAME"; stringp = true; break;
623 case DT_RPATH: name = "RPATH"; stringp = true; break;
624 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
625 case DT_REL: name = "REL"; break;
626 case DT_RELSZ: name = "RELSZ"; break;
627 case DT_RELENT: name = "RELENT"; break;
628 case DT_PLTREL: name = "PLTREL"; break;
629 case DT_DEBUG: name = "DEBUG"; break;
630 case DT_TEXTREL: name = "TEXTREL"; break;
631 case DT_JMPREL: name = "JMPREL"; break;
148437ec
ILT
632 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
633 case DT_FILTER: name = "FILTER"; stringp = true; break;
a66a61a0
ILT
634 case DT_VERSYM: name = "VERSYM"; break;
635 case DT_VERDEF: name = "VERDEF"; break;
636 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
637 case DT_VERNEED: name = "VERNEED"; break;
638 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
02fcd126
ILT
639 }
640
641 fprintf (f, " %-11s ", name);
642 if (! stringp)
927d05b5 643 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
02fcd126
ILT
644 else
645 {
646 const char *string;
647
648 string = bfd_elf_string_from_elf_section (abfd, link,
649 dyn.d_un.d_val);
650 if (string == NULL)
651 goto error_return;
652 fprintf (f, "%s", string);
653 }
654 fprintf (f, "\n");
27fb8f29 655 }
02fcd126
ILT
656
657 free (dynbuf);
658 dynbuf = NULL;
27fb8f29
ILT
659 }
660
a66a61a0
ILT
661 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
662 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
663 {
664 if (! _bfd_elf_slurp_version_tables (abfd))
665 return false;
666 }
667
668 if (elf_dynverdef (abfd) != 0)
669 {
670 Elf_Internal_Verdef *t;
671
672 fprintf (f, "\nVersion definitions:\n");
673 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
674 {
675 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
676 t->vd_flags, t->vd_hash, t->vd_nodename);
677 if (t->vd_auxptr->vda_nextptr != NULL)
678 {
679 Elf_Internal_Verdaux *a;
680
681 fprintf (f, "\t");
682 for (a = t->vd_auxptr->vda_nextptr;
683 a != NULL;
684 a = a->vda_nextptr)
685 fprintf (f, "%s ", a->vda_nodename);
686 fprintf (f, "\n");
687 }
688 }
689 }
690
691 if (elf_dynverref (abfd) != 0)
692 {
693 Elf_Internal_Verneed *t;
694
695 fprintf (f, "\nVersion References:\n");
696 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
697 {
698 Elf_Internal_Vernaux *a;
699
700 fprintf (f, " required from %s:\n", t->vn_filename);
701 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
702 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
703 a->vna_flags, a->vna_other, a->vna_nodename);
704 }
705 }
706
27fb8f29 707 return true;
02fcd126
ILT
708
709 error_return:
710 if (dynbuf != NULL)
711 free (dynbuf);
712 return false;
27fb8f29
ILT
713}
714
b176e1e9 715/* Display ELF-specific fields of a symbol. */
d6bfcdb5 716
b176e1e9 717void
d6bfcdb5
ILT
718bfd_elf_print_symbol (abfd, filep, symbol, how)
719 bfd *abfd;
b176e1e9
ILT
720 PTR filep;
721 asymbol *symbol;
722 bfd_print_symbol_type how;
723{
724 FILE *file = (FILE *) filep;
725 switch (how)
726 {
727 case bfd_print_symbol_name:
728 fprintf (file, "%s", symbol->name);
729 break;
730 case bfd_print_symbol_more:
731 fprintf (file, "elf ");
732 fprintf_vma (file, symbol->value);
733 fprintf (file, " %lx", (long) symbol->flags);
734 break;
735 case bfd_print_symbol_all:
736 {
737 CONST char *section_name;
738 section_name = symbol->section ? symbol->section->name : "(*none*)";
739 bfd_print_symbol_vandf ((PTR) file, symbol);
740 fprintf (file, " %s\t", section_name);
741 /* Print the "other" value for a symbol. For common symbols,
742 we've already printed the size; now print the alignment.
743 For other symbols, we have no specified alignment, and
744 we've printed the address; now print the size. */
745 fprintf_vma (file,
746 (bfd_is_com_section (symbol->section)
747 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
748 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
d6bfcdb5
ILT
749
750 /* If we have version information, print it. */
751 if (elf_tdata (abfd)->dynversym_section != 0
752 && (elf_tdata (abfd)->dynverdef_section != 0
753 || elf_tdata (abfd)->dynverref_section != 0))
754 {
755 unsigned int vernum;
756 const char *version_string;
757
758 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
759
760 if (vernum == 0)
761 version_string = "";
762 else if (vernum == 1)
763 version_string = "Base";
20db2495 764 else if (vernum <= elf_tdata (abfd)->cverdefs)
d6bfcdb5
ILT
765 version_string =
766 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
767 else
768 {
769 Elf_Internal_Verneed *t;
770
771 version_string = "";
772 for (t = elf_tdata (abfd)->verref;
773 t != NULL;
774 t = t->vn_nextref)
775 {
776 Elf_Internal_Vernaux *a;
777
778 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
779 {
780 if (a->vna_other == vernum)
781 {
782 version_string = a->vna_nodename;
783 break;
784 }
785 }
786 }
787 }
788
789 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
20db2495 790 fprintf (file, " %-11s", version_string);
d6bfcdb5
ILT
791 else
792 {
793 int i;
794
795 fprintf (file, " (%s)", version_string);
20db2495 796 for (i = 10 - strlen (version_string); i > 0; --i)
d6bfcdb5
ILT
797 putc (' ', file);
798 }
799 }
800
69e2ff18
ILT
801 /* If the st_other field is not zero, print it. */
802 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
803 fprintf (file, " 0x%02x",
804 ((unsigned int)
805 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
d6bfcdb5 806
b176e1e9
ILT
807 fprintf (file, " %s", symbol->name);
808 }
809 break;
810 }
811}
812\f
013dec1a
ILT
813/* Create an entry in an ELF linker hash table. */
814
5315c428
ILT
815struct bfd_hash_entry *
816_bfd_elf_link_hash_newfunc (entry, table, string)
013dec1a
ILT
817 struct bfd_hash_entry *entry;
818 struct bfd_hash_table *table;
819 const char *string;
820{
821 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
822
823 /* Allocate the structure if it has not already been allocated by a
824 subclass. */
825 if (ret == (struct elf_link_hash_entry *) NULL)
826 ret = ((struct elf_link_hash_entry *)
827 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
828 if (ret == (struct elf_link_hash_entry *) NULL)
a9713b91 829 return (struct bfd_hash_entry *) ret;
013dec1a
ILT
830
831 /* Call the allocation method of the superclass. */
832 ret = ((struct elf_link_hash_entry *)
833 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
834 table, string));
835 if (ret != (struct elf_link_hash_entry *) NULL)
836 {
837 /* Set local fields. */
838 ret->indx = -1;
839 ret->size = 0;
013dec1a
ILT
840 ret->dynindx = -1;
841 ret->dynstr_index = 0;
842 ret->weakdef = NULL;
b176e1e9
ILT
843 ret->got_offset = (bfd_vma) -1;
844 ret->plt_offset = (bfd_vma) -1;
86aac8ea 845 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
a66a61a0 846 ret->verinfo.verdef = NULL;
013dec1a 847 ret->type = STT_NOTYPE;
80be821d 848 ret->other = 0;
869b7d80
ILT
849 /* Assume that we have been called by a non-ELF symbol reader.
850 This flag is then reset by the code which reads an ELF input
851 file. This ensures that a symbol created by a non-ELF symbol
852 reader will have the flag set correctly. */
853 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
013dec1a
ILT
854 }
855
856 return (struct bfd_hash_entry *) ret;
857}
858
5315c428
ILT
859/* Initialize an ELF linker hash table. */
860
861boolean
862_bfd_elf_link_hash_table_init (table, abfd, newfunc)
863 struct elf_link_hash_table *table;
864 bfd *abfd;
865 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
866 struct bfd_hash_table *,
867 const char *));
868{
b176e1e9 869 table->dynamic_sections_created = false;
5315c428 870 table->dynobj = NULL;
b176e1e9
ILT
871 /* The first dynamic symbol is a dummy. */
872 table->dynsymcount = 1;
5315c428
ILT
873 table->dynstr = NULL;
874 table->bucketcount = 0;
b176e1e9 875 table->needed = NULL;
19bfbcbe 876 table->hgot = NULL;
d1bf45aa 877 table->stab_info = NULL;
5315c428
ILT
878 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
879}
880
013dec1a
ILT
881/* Create an ELF linker hash table. */
882
883struct bfd_link_hash_table *
884_bfd_elf_link_hash_table_create (abfd)
885 bfd *abfd;
886{
887 struct elf_link_hash_table *ret;
888
889 ret = ((struct elf_link_hash_table *)
890 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
891 if (ret == (struct elf_link_hash_table *) NULL)
a9713b91 892 return NULL;
5315c428
ILT
893
894 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
013dec1a
ILT
895 {
896 bfd_release (abfd, ret);
897 return NULL;
898 }
899
013dec1a
ILT
900 return &ret->root;
901}
7c6da9ca
ILT
902
903/* This is a hook for the ELF emulation code in the generic linker to
904 tell the backend linker what file name to use for the DT_NEEDED
b176e1e9
ILT
905 entry for a dynamic object. The generic linker passes name as an
906 empty string to indicate that no DT_NEEDED entry should be made. */
7c6da9ca
ILT
907
908void
909bfd_elf_set_dt_needed_name (abfd, name)
910 bfd *abfd;
911 const char *name;
912{
053ae1d7
ILT
913 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
914 && bfd_get_format (abfd) == bfd_object)
915 elf_dt_name (abfd) = name;
7c6da9ca 916}
b176e1e9 917
053ae1d7
ILT
918/* Get the list of DT_NEEDED entries for a link. This is a hook for
919 the ELF emulation code. */
b176e1e9 920
5fe14a9f 921struct bfd_link_needed_list *
b176e1e9
ILT
922bfd_elf_get_needed_list (abfd, info)
923 bfd *abfd;
924 struct bfd_link_info *info;
925{
b2193cc5
ILT
926 if (info->hash->creator->flavour != bfd_target_elf_flavour)
927 return NULL;
b176e1e9
ILT
928 return elf_hash_table (info)->needed;
929}
053ae1d7
ILT
930
931/* Get the name actually used for a dynamic object for a link. This
932 is the SONAME entry if there is one. Otherwise, it is the string
933 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
934
935const char *
936bfd_elf_get_dt_soname (abfd)
937 bfd *abfd;
938{
939 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
940 && bfd_get_format (abfd) == bfd_object)
941 return elf_dt_name (abfd);
942 return NULL;
943}
ede4eed4
KR
944\f
945/* Allocate an ELF string table--force the first byte to be zero. */
946
947struct bfd_strtab_hash *
948_bfd_elf_stringtab_init ()
949{
950 struct bfd_strtab_hash *ret;
951
952 ret = _bfd_stringtab_init ();
953 if (ret != NULL)
954 {
955 bfd_size_type loc;
956
957 loc = _bfd_stringtab_add (ret, "", true, false);
958 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
959 if (loc == (bfd_size_type) -1)
960 {
961 _bfd_stringtab_free (ret);
962 ret = NULL;
963 }
964 }
965 return ret;
966}
967\f
968/* ELF .o/exec file reading */
969
970/* Create a new bfd section from an ELF section header. */
971
972boolean
973bfd_section_from_shdr (abfd, shindex)
974 bfd *abfd;
975 unsigned int shindex;
976{
977 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
978 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
979 struct elf_backend_data *bed = get_elf_backend_data (abfd);
980 char *name;
981
982 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
983
984 switch (hdr->sh_type)
985 {
986 case SHT_NULL:
987 /* Inactive section. Throw it away. */
988 return true;
989
990 case SHT_PROGBITS: /* Normal section with contents. */
991 case SHT_DYNAMIC: /* Dynamic linking information. */
992 case SHT_NOBITS: /* .bss section. */
993 case SHT_HASH: /* .hash section. */
5b3b9ff6 994 case SHT_NOTE: /* .note section. */
ede4eed4
KR
995 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
996
997 case SHT_SYMTAB: /* A symbol table */
998 if (elf_onesymtab (abfd) == shindex)
999 return true;
1000
1001 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1002 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1003 elf_onesymtab (abfd) = shindex;
1004 elf_tdata (abfd)->symtab_hdr = *hdr;
fd0198f0 1005 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
ede4eed4
KR
1006 abfd->flags |= HAS_SYMS;
1007
1008 /* Sometimes a shared object will map in the symbol table. If
1009 SHF_ALLOC is set, and this is a shared object, then we also
1010 treat this section as a BFD section. We can not base the
1011 decision purely on SHF_ALLOC, because that flag is sometimes
1012 set in a relocateable object file, which would confuse the
1013 linker. */
1014 if ((hdr->sh_flags & SHF_ALLOC) != 0
1015 && (abfd->flags & DYNAMIC) != 0
1016 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1017 return false;
1018
1019 return true;
1020
1021 case SHT_DYNSYM: /* A dynamic symbol table */
1022 if (elf_dynsymtab (abfd) == shindex)
1023 return true;
1024
1025 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1026 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1027 elf_dynsymtab (abfd) = shindex;
1028 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
fd0198f0 1029 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
ede4eed4
KR
1030 abfd->flags |= HAS_SYMS;
1031
1032 /* Besides being a symbol table, we also treat this as a regular
1033 section, so that objcopy can handle it. */
1034 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1035
1036 case SHT_STRTAB: /* A string table */
1037 if (hdr->bfd_section != NULL)
1038 return true;
1039 if (ehdr->e_shstrndx == shindex)
1040 {
1041 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1042 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1043 return true;
1044 }
1045 {
1046 unsigned int i;
1047
1048 for (i = 1; i < ehdr->e_shnum; i++)
1049 {
1050 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1051 if (hdr2->sh_link == shindex)
1052 {
1053 if (! bfd_section_from_shdr (abfd, i))
1054 return false;
1055 if (elf_onesymtab (abfd) == i)
1056 {
1057 elf_tdata (abfd)->strtab_hdr = *hdr;
1058 elf_elfsections (abfd)[shindex] =
1059 &elf_tdata (abfd)->strtab_hdr;
1060 return true;
1061 }
1062 if (elf_dynsymtab (abfd) == i)
1063 {
1064 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
fd0198f0 1065 elf_elfsections (abfd)[shindex] = hdr =
ede4eed4
KR
1066 &elf_tdata (abfd)->dynstrtab_hdr;
1067 /* We also treat this as a regular section, so
1068 that objcopy can handle it. */
1069 break;
1070 }
1071#if 0 /* Not handling other string tables specially right now. */
1072 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1073 /* We have a strtab for some random other section. */
1074 newsect = (asection *) hdr2->bfd_section;
1075 if (!newsect)
1076 break;
1077 hdr->bfd_section = newsect;
1078 hdr2 = &elf_section_data (newsect)->str_hdr;
1079 *hdr2 = *hdr;
1080 elf_elfsections (abfd)[shindex] = hdr2;
1081#endif
1082 }
1083 }
1084 }
1085
1086 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1087
1088 case SHT_REL:
1089 case SHT_RELA:
1090 /* *These* do a lot of work -- but build no sections! */
1091 {
1092 asection *target_sect;
1093 Elf_Internal_Shdr *hdr2;
ede4eed4 1094
ae115e51
ILT
1095 /* For some incomprehensible reason Oracle distributes
1096 libraries for Solaris in which some of the objects have
1097 bogus sh_link fields. It would be nice if we could just
1098 reject them, but, unfortunately, some people need to use
1099 them. We scan through the section headers; if we find only
1100 one suitable symbol table, we clobber the sh_link to point
1101 to it. I hope this doesn't break anything. */
1102 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1103 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1104 {
1105 int scan;
1106 int found;
1107
1108 found = 0;
1109 for (scan = 1; scan < ehdr->e_shnum; scan++)
1110 {
1111 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1112 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1113 {
1114 if (found != 0)
1115 {
1116 found = 0;
1117 break;
1118 }
1119 found = scan;
1120 }
1121 }
1122 if (found != 0)
1123 hdr->sh_link = found;
1124 }
1125
ede4eed4 1126 /* Get the symbol table. */
ae115e51
ILT
1127 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1128 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
ede4eed4
KR
1129 return false;
1130
1131 /* If this reloc section does not use the main symbol table we
1132 don't treat it as a reloc section. BFD can't adequately
1133 represent such a section, so at least for now, we don't
1134 try. We just present it as a normal section. */
1135 if (hdr->sh_link != elf_onesymtab (abfd))
e85f2fbd 1136 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
ede4eed4 1137
ede4eed4
KR
1138 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1139 return false;
1140 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1141 if (target_sect == NULL)
1142 return false;
1143
d1bf45aa
ILT
1144 if ((target_sect->flags & SEC_RELOC) == 0
1145 || target_sect->reloc_count == 0)
1146 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1147 else
1148 {
1149 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1150 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1151 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1152 }
ede4eed4
KR
1153 *hdr2 = *hdr;
1154 elf_elfsections (abfd)[shindex] = hdr2;
d1bf45aa 1155 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
ede4eed4
KR
1156 target_sect->flags |= SEC_RELOC;
1157 target_sect->relocation = NULL;
1158 target_sect->rel_filepos = hdr->sh_offset;
1159 abfd->flags |= HAS_RELOC;
1160 return true;
1161 }
1162 break;
1163
a66a61a0
ILT
1164 case SHT_GNU_verdef:
1165 elf_dynverdef (abfd) = shindex;
1166 elf_tdata (abfd)->dynverdef_hdr = *hdr;
d6bfcdb5 1167 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
a66a61a0
ILT
1168 break;
1169
1170 case SHT_GNU_versym:
1171 elf_dynversym (abfd) = shindex;
1172 elf_tdata (abfd)->dynversym_hdr = *hdr;
d6bfcdb5 1173 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
a66a61a0
ILT
1174 break;
1175
1176 case SHT_GNU_verneed:
1177 elf_dynverref (abfd) = shindex;
1178 elf_tdata (abfd)->dynverref_hdr = *hdr;
d6bfcdb5 1179 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
a66a61a0
ILT
1180 break;
1181
ede4eed4 1182 case SHT_SHLIB:
ede4eed4
KR
1183 return true;
1184
1185 default:
1186 /* Check for any processor-specific section types. */
1187 {
1188 if (bed->elf_backend_section_from_shdr)
1189 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1190 }
1191 break;
1192 }
1193
1194 return true;
1195}
1196
1197/* Given an ELF section number, retrieve the corresponding BFD
1198 section. */
1199
1200asection *
1201bfd_section_from_elf_index (abfd, index)
1202 bfd *abfd;
1203 unsigned int index;
1204{
1205 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1206 if (index >= elf_elfheader (abfd)->e_shnum)
1207 return NULL;
1208 return elf_elfsections (abfd)[index]->bfd_section;
1209}
1210
1211boolean
1212_bfd_elf_new_section_hook (abfd, sec)
1213 bfd *abfd;
1214 asection *sec;
1215{
1216 struct bfd_elf_section_data *sdata;
1217
1218 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
1219 if (!sdata)
a9713b91 1220 return false;
ede4eed4
KR
1221 sec->used_by_bfd = (PTR) sdata;
1222 memset (sdata, 0, sizeof (*sdata));
1223 return true;
1224}
1225
1226/* Create a new bfd section from an ELF program header.
1227
1228 Since program segments have no names, we generate a synthetic name
1229 of the form segment<NUM>, where NUM is generally the index in the
1230 program header table. For segments that are split (see below) we
1231 generate the names segment<NUM>a and segment<NUM>b.
1232
1233 Note that some program segments may have a file size that is different than
1234 (less than) the memory size. All this means is that at execution the
1235 system must allocate the amount of memory specified by the memory size,
1236 but only initialize it with the first "file size" bytes read from the
1237 file. This would occur for example, with program segments consisting
1238 of combined data+bss.
1239
1240 To handle the above situation, this routine generates TWO bfd sections
1241 for the single program segment. The first has the length specified by
1242 the file size of the segment, and the second has the length specified
1243 by the difference between the two sizes. In effect, the segment is split
1244 into it's initialized and uninitialized parts.
1245
1246 */
1247
1248boolean
1249bfd_section_from_phdr (abfd, hdr, index)
1250 bfd *abfd;
1251 Elf_Internal_Phdr *hdr;
1252 int index;
1253{
1254 asection *newsect;
1255 char *name;
1256 char namebuf[64];
1257 int split;
1258
1259 split = ((hdr->p_memsz > 0) &&
1260 (hdr->p_filesz > 0) &&
1261 (hdr->p_memsz > hdr->p_filesz));
1262 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
1263 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1264 if (!name)
a9713b91 1265 return false;
ede4eed4
KR
1266 strcpy (name, namebuf);
1267 newsect = bfd_make_section (abfd, name);
1268 if (newsect == NULL)
1269 return false;
1270 newsect->vma = hdr->p_vaddr;
ae115e51 1271 newsect->lma = hdr->p_paddr;
ede4eed4
KR
1272 newsect->_raw_size = hdr->p_filesz;
1273 newsect->filepos = hdr->p_offset;
1274 newsect->flags |= SEC_HAS_CONTENTS;
1275 if (hdr->p_type == PT_LOAD)
1276 {
1277 newsect->flags |= SEC_ALLOC;
1278 newsect->flags |= SEC_LOAD;
1279 if (hdr->p_flags & PF_X)
1280 {
1281 /* FIXME: all we known is that it has execute PERMISSION,
1282 may be data. */
1283 newsect->flags |= SEC_CODE;
1284 }
1285 }
1286 if (!(hdr->p_flags & PF_W))
1287 {
1288 newsect->flags |= SEC_READONLY;
1289 }
1290
1291 if (split)
1292 {
1293 sprintf (namebuf, "segment%db", index);
1294 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1295 if (!name)
a9713b91 1296 return false;
ede4eed4
KR
1297 strcpy (name, namebuf);
1298 newsect = bfd_make_section (abfd, name);
1299 if (newsect == NULL)
1300 return false;
1301 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
ae115e51 1302 newsect->lma = hdr->p_paddr + hdr->p_filesz;
ede4eed4
KR
1303 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1304 if (hdr->p_type == PT_LOAD)
1305 {
1306 newsect->flags |= SEC_ALLOC;
1307 if (hdr->p_flags & PF_X)
1308 newsect->flags |= SEC_CODE;
1309 }
1310 if (!(hdr->p_flags & PF_W))
1311 newsect->flags |= SEC_READONLY;
1312 }
1313
1314 return true;
1315}
1316
1317/* Set up an ELF internal section header for a section. */
1318
1319/*ARGSUSED*/
1320static void
1321elf_fake_sections (abfd, asect, failedptrarg)
1322 bfd *abfd;
1323 asection *asect;
1324 PTR failedptrarg;
1325{
1326 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1327 boolean *failedptr = (boolean *) failedptrarg;
1328 Elf_Internal_Shdr *this_hdr;
1329
1330 if (*failedptr)
1331 {
1332 /* We already failed; just get out of the bfd_map_over_sections
1333 loop. */
1334 return;
1335 }
1336
1337 this_hdr = &elf_section_data (asect)->this_hdr;
1338
1339 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1340 asect->name,
1341 true, false);
1342 if (this_hdr->sh_name == (unsigned long) -1)
1343 {
1344 *failedptr = true;
1345 return;
1346 }
1347
1348 this_hdr->sh_flags = 0;
ae115e51 1349
50bd50d4
MH
1350 if ((asect->flags & SEC_ALLOC) != 0
1351 || asect->user_set_vma)
fd0198f0 1352 this_hdr->sh_addr = asect->vma;
ede4eed4
KR
1353 else
1354 this_hdr->sh_addr = 0;
ae115e51 1355
ede4eed4
KR
1356 this_hdr->sh_offset = 0;
1357 this_hdr->sh_size = asect->_raw_size;
1358 this_hdr->sh_link = 0;
ede4eed4 1359 this_hdr->sh_addralign = 1 << asect->alignment_power;
fd0198f0
ILT
1360 /* The sh_entsize and sh_info fields may have been set already by
1361 copy_private_section_data. */
ede4eed4
KR
1362
1363 this_hdr->bfd_section = asect;
1364 this_hdr->contents = NULL;
1365
1366 /* FIXME: This should not be based on section names. */
1367 if (strcmp (asect->name, ".dynstr") == 0)
1368 this_hdr->sh_type = SHT_STRTAB;
1369 else if (strcmp (asect->name, ".hash") == 0)
1370 {
1371 this_hdr->sh_type = SHT_HASH;
1372 this_hdr->sh_entsize = bed->s->arch_size / 8;
1373 }
1374 else if (strcmp (asect->name, ".dynsym") == 0)
1375 {
1376 this_hdr->sh_type = SHT_DYNSYM;
1377 this_hdr->sh_entsize = bed->s->sizeof_sym;
1378 }
1379 else if (strcmp (asect->name, ".dynamic") == 0)
1380 {
1381 this_hdr->sh_type = SHT_DYNAMIC;
1382 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1383 }
1384 else if (strncmp (asect->name, ".rela", 5) == 0
1385 && get_elf_backend_data (abfd)->use_rela_p)
1386 {
1387 this_hdr->sh_type = SHT_RELA;
1388 this_hdr->sh_entsize = bed->s->sizeof_rela;
1389 }
1390 else if (strncmp (asect->name, ".rel", 4) == 0
1391 && ! get_elf_backend_data (abfd)->use_rela_p)
1392 {
1393 this_hdr->sh_type = SHT_REL;
1394 this_hdr->sh_entsize = bed->s->sizeof_rel;
1395 }
a66a61a0 1396 else if (strncmp (asect->name, ".note", 5) == 0)
ede4eed4
KR
1397 this_hdr->sh_type = SHT_NOTE;
1398 else if (strncmp (asect->name, ".stab", 5) == 0
1399 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1400 this_hdr->sh_type = SHT_STRTAB;
a66a61a0
ILT
1401 else if (strcmp (asect->name, ".gnu.version") == 0)
1402 {
1403 this_hdr->sh_type = SHT_GNU_versym;
1404 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1405 }
1406 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1407 {
1408 this_hdr->sh_type = SHT_GNU_verdef;
1409 this_hdr->sh_entsize = 0;
d6bfcdb5
ILT
1410 /* objcopy or strip will copy over sh_info, but may not set
1411 cverdefs. The linker will set cverdefs, but sh_info will be
1412 zero. */
1413 if (this_hdr->sh_info == 0)
1414 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1415 else
1416 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1417 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
a66a61a0
ILT
1418 }
1419 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1420 {
1421 this_hdr->sh_type = SHT_GNU_verneed;
1422 this_hdr->sh_entsize = 0;
d6bfcdb5
ILT
1423 /* objcopy or strip will copy over sh_info, but may not set
1424 cverrefs. The linker will set cverrefs, but sh_info will be
1425 zero. */
1426 if (this_hdr->sh_info == 0)
1427 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1428 else
1429 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1430 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
a66a61a0 1431 }
ede4eed4
KR
1432 else if ((asect->flags & SEC_ALLOC) != 0
1433 && (asect->flags & SEC_LOAD) != 0)
1434 this_hdr->sh_type = SHT_PROGBITS;
1435 else if ((asect->flags & SEC_ALLOC) != 0
1436 && ((asect->flags & SEC_LOAD) == 0))
5fe14a9f 1437 this_hdr->sh_type = SHT_NOBITS;
ede4eed4
KR
1438 else
1439 {
1440 /* Who knows? */
1441 this_hdr->sh_type = SHT_PROGBITS;
1442 }
1443
1444 if ((asect->flags & SEC_ALLOC) != 0)
1445 this_hdr->sh_flags |= SHF_ALLOC;
1446 if ((asect->flags & SEC_READONLY) == 0)
1447 this_hdr->sh_flags |= SHF_WRITE;
1448 if ((asect->flags & SEC_CODE) != 0)
1449 this_hdr->sh_flags |= SHF_EXECINSTR;
1450
1451 /* Check for processor-specific section types. */
1452 {
1453 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1454
1455 if (bed->elf_backend_fake_sections)
1456 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1457 }
1458
1459 /* If the section has relocs, set up a section header for the
1460 SHT_REL[A] section. */
1461 if ((asect->flags & SEC_RELOC) != 0)
1462 {
1463 Elf_Internal_Shdr *rela_hdr;
1464 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1465 char *name;
1466
1467 rela_hdr = &elf_section_data (asect)->rel_hdr;
1468 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1469 if (name == NULL)
1470 {
ede4eed4
KR
1471 *failedptr = true;
1472 return;
1473 }
1474 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1475 rela_hdr->sh_name =
1476 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1477 true, false);
1478 if (rela_hdr->sh_name == (unsigned int) -1)
1479 {
1480 *failedptr = true;
1481 return;
1482 }
1483 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1484 rela_hdr->sh_entsize = (use_rela_p
1485 ? bed->s->sizeof_rela
1486 : bed->s->sizeof_rel);
1487 rela_hdr->sh_addralign = bed->s->file_align;
1488 rela_hdr->sh_flags = 0;
1489 rela_hdr->sh_addr = 0;
1490 rela_hdr->sh_size = 0;
1491 rela_hdr->sh_offset = 0;
1492 }
1493}
1494
1495/* Assign all ELF section numbers. The dummy first section is handled here
1496 too. The link/info pointers for the standard section types are filled
1497 in here too, while we're at it. */
1498
1499static boolean
1500assign_section_numbers (abfd)
1501 bfd *abfd;
1502{
1503 struct elf_obj_tdata *t = elf_tdata (abfd);
1504 asection *sec;
1505 unsigned int section_number;
1506 Elf_Internal_Shdr **i_shdrp;
1507 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1508
1509 section_number = 1;
1510
1511 for (sec = abfd->sections; sec; sec = sec->next)
1512 {
1513 struct bfd_elf_section_data *d = elf_section_data (sec);
1514
1515 d->this_idx = section_number++;
1516 if ((sec->flags & SEC_RELOC) == 0)
1517 d->rel_idx = 0;
1518 else
1519 d->rel_idx = section_number++;
1520 }
1521
1522 t->shstrtab_section = section_number++;
1523 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1524 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1525
1526 if (abfd->symcount > 0)
1527 {
1528 t->symtab_section = section_number++;
1529 t->strtab_section = section_number++;
1530 }
1531
1532 elf_elfheader (abfd)->e_shnum = section_number;
1533
1534 /* Set up the list of section header pointers, in agreement with the
1535 indices. */
1536 i_shdrp = ((Elf_Internal_Shdr **)
1537 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1538 if (i_shdrp == NULL)
a9713b91 1539 return false;
ede4eed4
KR
1540
1541 i_shdrp[0] = ((Elf_Internal_Shdr *)
1542 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1543 if (i_shdrp[0] == NULL)
1544 {
1545 bfd_release (abfd, i_shdrp);
ede4eed4
KR
1546 return false;
1547 }
1548 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1549
1550 elf_elfsections (abfd) = i_shdrp;
1551
1552 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1553 if (abfd->symcount > 0)
1554 {
1555 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1556 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1557 t->symtab_hdr.sh_link = t->strtab_section;
1558 }
1559 for (sec = abfd->sections; sec; sec = sec->next)
1560 {
1561 struct bfd_elf_section_data *d = elf_section_data (sec);
1562 asection *s;
1563 const char *name;
1564
1565 i_shdrp[d->this_idx] = &d->this_hdr;
1566 if (d->rel_idx != 0)
1567 i_shdrp[d->rel_idx] = &d->rel_hdr;
1568
1569 /* Fill in the sh_link and sh_info fields while we're at it. */
1570
1571 /* sh_link of a reloc section is the section index of the symbol
1572 table. sh_info is the section index of the section to which
1573 the relocation entries apply. */
1574 if (d->rel_idx != 0)
1575 {
1576 d->rel_hdr.sh_link = t->symtab_section;
1577 d->rel_hdr.sh_info = d->this_idx;
1578 }
1579
1580 switch (d->this_hdr.sh_type)
1581 {
1582 case SHT_REL:
1583 case SHT_RELA:
1584 /* A reloc section which we are treating as a normal BFD
1585 section. sh_link is the section index of the symbol
1586 table. sh_info is the section index of the section to
1587 which the relocation entries apply. We assume that an
1588 allocated reloc section uses the dynamic symbol table.
1589 FIXME: How can we be sure? */
1590 s = bfd_get_section_by_name (abfd, ".dynsym");
1591 if (s != NULL)
1592 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1593
1594 /* We look up the section the relocs apply to by name. */
1595 name = sec->name;
1596 if (d->this_hdr.sh_type == SHT_REL)
1597 name += 4;
1598 else
1599 name += 5;
1600 s = bfd_get_section_by_name (abfd, name);
1601 if (s != NULL)
1602 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1603 break;
1604
1605 case SHT_STRTAB:
1606 /* We assume that a section named .stab*str is a stabs
1607 string section. We look for a section with the same name
1608 but without the trailing ``str'', and set its sh_link
1609 field to point to this section. */
1610 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1611 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1612 {
1613 size_t len;
1614 char *alc;
1615
1616 len = strlen (sec->name);
58142f10 1617 alc = (char *) bfd_malloc (len - 2);
ede4eed4 1618 if (alc == NULL)
58142f10 1619 return false;
ede4eed4
KR
1620 strncpy (alc, sec->name, len - 3);
1621 alc[len - 3] = '\0';
1622 s = bfd_get_section_by_name (abfd, alc);
1623 free (alc);
1624 if (s != NULL)
1625 {
1626 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1627
1628 /* This is a .stab section. */
1629 elf_section_data (s)->this_hdr.sh_entsize =
1630 4 + 2 * (bed->s->arch_size / 8);
1631 }
1632 }
1633 break;
1634
1635 case SHT_DYNAMIC:
1636 case SHT_DYNSYM:
a66a61a0
ILT
1637 case SHT_GNU_verneed:
1638 case SHT_GNU_verdef:
ede4eed4 1639 /* sh_link is the section header index of the string table
a66a61a0
ILT
1640 used for the dynamic entries, or the symbol table, or the
1641 version strings. */
ede4eed4
KR
1642 s = bfd_get_section_by_name (abfd, ".dynstr");
1643 if (s != NULL)
1644 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1645 break;
1646
1647 case SHT_HASH:
a66a61a0 1648 case SHT_GNU_versym:
ede4eed4 1649 /* sh_link is the section header index of the symbol table
a66a61a0 1650 this hash table or version table is for. */
ede4eed4
KR
1651 s = bfd_get_section_by_name (abfd, ".dynsym");
1652 if (s != NULL)
1653 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1654 break;
1655 }
1656 }
1657
1658 return true;
1659}
1660
1661/* Map symbol from it's internal number to the external number, moving
1662 all local symbols to be at the head of the list. */
1663
1664static INLINE int
1665sym_is_global (abfd, sym)
1666 bfd *abfd;
1667 asymbol *sym;
1668{
1669 /* If the backend has a special mapping, use it. */
1670 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1671 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1672 (abfd, sym));
1673
1674 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1675 || bfd_is_und_section (bfd_get_section (sym))
1676 || bfd_is_com_section (bfd_get_section (sym)));
1677}
1678
1679static boolean
1680elf_map_symbols (abfd)
1681 bfd *abfd;
1682{
1683 int symcount = bfd_get_symcount (abfd);
1684 asymbol **syms = bfd_get_outsymbols (abfd);
1685 asymbol **sect_syms;
1686 int num_locals = 0;
1687 int num_globals = 0;
1688 int num_locals2 = 0;
1689 int num_globals2 = 0;
1690 int max_index = 0;
1691 int num_sections = 0;
1692 int idx;
1693 asection *asect;
1694 asymbol **new_syms;
1695
1696#ifdef DEBUG
1697 fprintf (stderr, "elf_map_symbols\n");
1698 fflush (stderr);
1699#endif
1700
1701 /* Add a section symbol for each BFD section. FIXME: Is this really
1702 necessary? */
1703 for (asect = abfd->sections; asect; asect = asect->next)
1704 {
1705 if (max_index < asect->index)
1706 max_index = asect->index;
1707 }
1708
1709 max_index++;
1710 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1711 if (sect_syms == NULL)
a9713b91 1712 return false;
ede4eed4
KR
1713 elf_section_syms (abfd) = sect_syms;
1714
1715 for (idx = 0; idx < symcount; idx++)
1716 {
1717 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
fd0198f0 1718 && (syms[idx]->value + syms[idx]->section->vma) == 0)
ede4eed4
KR
1719 {
1720 asection *sec;
1721
1722 sec = syms[idx]->section;
1723 if (sec->owner != NULL)
1724 {
1725 if (sec->owner != abfd)
1726 {
1727 if (sec->output_offset != 0)
1728 continue;
1729 sec = sec->output_section;
1730 BFD_ASSERT (sec->owner == abfd);
1731 }
1732 sect_syms[sec->index] = syms[idx];
1733 }
1734 }
1735 }
1736
1737 for (asect = abfd->sections; asect; asect = asect->next)
1738 {
1739 asymbol *sym;
1740
1741 if (sect_syms[asect->index] != NULL)
1742 continue;
1743
1744 sym = bfd_make_empty_symbol (abfd);
1745 if (sym == NULL)
1746 return false;
1747 sym->the_bfd = abfd;
1748 sym->name = asect->name;
1749 sym->value = 0;
1750 /* Set the flags to 0 to indicate that this one was newly added. */
1751 sym->flags = 0;
1752 sym->section = asect;
1753 sect_syms[asect->index] = sym;
1754 num_sections++;
1755#ifdef DEBUG
1756 fprintf (stderr,
1757 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1758 asect->name, (long) asect->vma, asect->index, (long) asect);
1759#endif
1760 }
1761
1762 /* Classify all of the symbols. */
1763 for (idx = 0; idx < symcount; idx++)
1764 {
1765 if (!sym_is_global (abfd, syms[idx]))
1766 num_locals++;
1767 else
1768 num_globals++;
1769 }
1770 for (asect = abfd->sections; asect; asect = asect->next)
1771 {
1772 if (sect_syms[asect->index] != NULL
1773 && sect_syms[asect->index]->flags == 0)
1774 {
1775 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1776 if (!sym_is_global (abfd, sect_syms[asect->index]))
1777 num_locals++;
1778 else
1779 num_globals++;
1780 sect_syms[asect->index]->flags = 0;
1781 }
1782 }
1783
1784 /* Now sort the symbols so the local symbols are first. */
1785 new_syms = ((asymbol **)
1786 bfd_alloc (abfd,
1787 (num_locals + num_globals) * sizeof (asymbol *)));
1788 if (new_syms == NULL)
a9713b91 1789 return false;
ede4eed4
KR
1790
1791 for (idx = 0; idx < symcount; idx++)
1792 {
1793 asymbol *sym = syms[idx];
1794 int i;
1795
1796 if (!sym_is_global (abfd, sym))
1797 i = num_locals2++;
1798 else
1799 i = num_locals + num_globals2++;
1800 new_syms[i] = sym;
1801 sym->udata.i = i + 1;
1802 }
1803 for (asect = abfd->sections; asect; asect = asect->next)
1804 {
1805 if (sect_syms[asect->index] != NULL
1806 && sect_syms[asect->index]->flags == 0)
1807 {
1808 asymbol *sym = sect_syms[asect->index];
1809 int i;
1810
1811 sym->flags = BSF_SECTION_SYM;
1812 if (!sym_is_global (abfd, sym))
1813 i = num_locals2++;
1814 else
1815 i = num_locals + num_globals2++;
1816 new_syms[i] = sym;
1817 sym->udata.i = i + 1;
1818 }
1819 }
1820
1821 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1822
1823 elf_num_locals (abfd) = num_locals;
1824 elf_num_globals (abfd) = num_globals;
1825 return true;
1826}
1827
fd0198f0
ILT
1828/* Align to the maximum file alignment that could be required for any
1829 ELF data structure. */
1830
1831static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1832static INLINE file_ptr
1833align_file_position (off, align)
1834 file_ptr off;
1835 int align;
1836{
1837 return (off + align - 1) & ~(align - 1);
1838}
1839
1840/* Assign a file position to a section, optionally aligning to the
1841 required section alignment. */
1842
1843INLINE file_ptr
1844_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1845 Elf_Internal_Shdr *i_shdrp;
1846 file_ptr offset;
1847 boolean align;
1848{
1849 if (align)
1850 {
1851 unsigned int al;
1852
1853 al = i_shdrp->sh_addralign;
1854 if (al > 1)
1855 offset = BFD_ALIGN (offset, al);
1856 }
1857 i_shdrp->sh_offset = offset;
1858 if (i_shdrp->bfd_section != NULL)
1859 i_shdrp->bfd_section->filepos = offset;
1860 if (i_shdrp->sh_type != SHT_NOBITS)
1861 offset += i_shdrp->sh_size;
1862 return offset;
1863}
1864
ede4eed4
KR
1865/* Compute the file positions we are going to put the sections at, and
1866 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1867 is not NULL, this is being called by the ELF backend linker. */
1868
1869boolean
1870_bfd_elf_compute_section_file_positions (abfd, link_info)
1871 bfd *abfd;
1872 struct bfd_link_info *link_info;
1873{
1874 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1875 boolean failed;
1876 struct bfd_strtab_hash *strtab;
1877 Elf_Internal_Shdr *shstrtab_hdr;
1878
1879 if (abfd->output_has_begun)
1880 return true;
1881
1882 /* Do any elf backend specific processing first. */
1883 if (bed->elf_backend_begin_write_processing)
1884 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1885
1886 if (! prep_headers (abfd))
1887 return false;
1888
1889 failed = false;
1890 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1891 if (failed)
1892 return false;
1893
1894 if (!assign_section_numbers (abfd))
1895 return false;
1896
1897 /* The backend linker builds symbol table information itself. */
fd0198f0 1898 if (link_info == NULL && abfd->symcount > 0)
ede4eed4
KR
1899 {
1900 if (! swap_out_syms (abfd, &strtab))
1901 return false;
1902 }
1903
1904 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1905 /* sh_name was set in prep_headers. */
1906 shstrtab_hdr->sh_type = SHT_STRTAB;
1907 shstrtab_hdr->sh_flags = 0;
1908 shstrtab_hdr->sh_addr = 0;
1909 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1910 shstrtab_hdr->sh_entsize = 0;
1911 shstrtab_hdr->sh_link = 0;
1912 shstrtab_hdr->sh_info = 0;
fd0198f0 1913 /* sh_offset is set in assign_file_positions_except_relocs. */
ede4eed4
KR
1914 shstrtab_hdr->sh_addralign = 1;
1915
fd0198f0 1916 if (!assign_file_positions_except_relocs (abfd))
ede4eed4
KR
1917 return false;
1918
fd0198f0 1919 if (link_info == NULL && abfd->symcount > 0)
ede4eed4 1920 {
fd0198f0
ILT
1921 file_ptr off;
1922 Elf_Internal_Shdr *hdr;
1923
1924 off = elf_tdata (abfd)->next_file_pos;
1925
1926 hdr = &elf_tdata (abfd)->symtab_hdr;
1927 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1928
1929 hdr = &elf_tdata (abfd)->strtab_hdr;
1930 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1931
1932 elf_tdata (abfd)->next_file_pos = off;
1933
ede4eed4
KR
1934 /* Now that we know where the .strtab section goes, write it
1935 out. */
fd0198f0 1936 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ede4eed4
KR
1937 || ! _bfd_stringtab_emit (abfd, strtab))
1938 return false;
1939 _bfd_stringtab_free (strtab);
1940 }
1941
1942 abfd->output_has_begun = true;
1943
1944 return true;
1945}
1946
fd0198f0 1947/* Create a mapping from a set of sections to a program segment. */
ede4eed4 1948
fd0198f0 1949static INLINE struct elf_segment_map *
edf3fe48 1950make_mapping (abfd, sections, from, to, phdr)
fd0198f0
ILT
1951 bfd *abfd;
1952 asection **sections;
1953 unsigned int from;
1954 unsigned int to;
edf3fe48 1955 boolean phdr;
ede4eed4 1956{
fd0198f0
ILT
1957 struct elf_segment_map *m;
1958 unsigned int i;
1959 asection **hdrpp;
1960
1961 m = ((struct elf_segment_map *)
1962 bfd_zalloc (abfd,
1963 (sizeof (struct elf_segment_map)
1964 + (to - from - 1) * sizeof (asection *))));
1965 if (m == NULL)
a9713b91 1966 return NULL;
fd0198f0
ILT
1967 m->next = NULL;
1968 m->p_type = PT_LOAD;
1969 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1970 m->sections[i - from] = *hdrpp;
1971 m->count = to - from;
1972
edf3fe48 1973 if (from == 0 && phdr)
6933148a
ILT
1974 {
1975 /* Include the headers in the first PT_LOAD segment. */
1976 m->includes_filehdr = 1;
1977 m->includes_phdrs = 1;
1978 }
1979
fd0198f0 1980 return m;
ede4eed4
KR
1981}
1982
fd0198f0 1983/* Set up a mapping from BFD sections to program segments. */
ede4eed4 1984
fd0198f0
ILT
1985static boolean
1986map_sections_to_segments (abfd)
1987 bfd *abfd;
ede4eed4 1988{
fd0198f0
ILT
1989 asection **sections = NULL;
1990 asection *s;
1991 unsigned int i;
1992 unsigned int count;
1993 struct elf_segment_map *mfirst;
1994 struct elf_segment_map **pm;
1995 struct elf_segment_map *m;
1996 asection *last_hdr;
1997 unsigned int phdr_index;
1998 bfd_vma maxpagesize;
1999 asection **hdrpp;
edf3fe48
ILT
2000 boolean phdr_in_section = true;
2001 boolean writable;
2002 asection *dynsec;
fd0198f0
ILT
2003
2004 if (elf_tdata (abfd)->segment_map != NULL)
2005 return true;
2006
2007 if (bfd_count_sections (abfd) == 0)
2008 return true;
2009
2010 /* Select the allocated sections, and sort them. */
2011
58142f10
ILT
2012 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2013 * sizeof (asection *));
fd0198f0 2014 if (sections == NULL)
58142f10 2015 goto error_return;
ede4eed4 2016
fd0198f0
ILT
2017 i = 0;
2018 for (s = abfd->sections; s != NULL; s = s->next)
2019 {
2020 if ((s->flags & SEC_ALLOC) != 0)
2021 {
2022 sections[i] = s;
2023 ++i;
2024 }
5fe14a9f 2025 }
fd0198f0
ILT
2026 BFD_ASSERT (i <= bfd_count_sections (abfd));
2027 count = i;
ede4eed4 2028
fd0198f0 2029 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
ede4eed4 2030
fd0198f0 2031 /* Build the mapping. */
ede4eed4 2032
fd0198f0
ILT
2033 mfirst = NULL;
2034 pm = &mfirst;
ede4eed4 2035
fd0198f0
ILT
2036 /* If we have a .interp section, then create a PT_PHDR segment for
2037 the program headers and a PT_INTERP segment for the .interp
2038 section. */
2039 s = bfd_get_section_by_name (abfd, ".interp");
2040 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2041 {
2042 m = ((struct elf_segment_map *)
2043 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2044 if (m == NULL)
a9713b91 2045 goto error_return;
fd0198f0
ILT
2046 m->next = NULL;
2047 m->p_type = PT_PHDR;
2048 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2049 m->p_flags = PF_R | PF_X;
2050 m->p_flags_valid = 1;
6933148a 2051 m->includes_phdrs = 1;
ede4eed4 2052
fd0198f0
ILT
2053 *pm = m;
2054 pm = &m->next;
ede4eed4 2055
fd0198f0
ILT
2056 m = ((struct elf_segment_map *)
2057 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2058 if (m == NULL)
a9713b91 2059 goto error_return;
fd0198f0
ILT
2060 m->next = NULL;
2061 m->p_type = PT_INTERP;
2062 m->count = 1;
2063 m->sections[0] = s;
ede4eed4 2064
fd0198f0
ILT
2065 *pm = m;
2066 pm = &m->next;
2067 }
ede4eed4 2068
fd0198f0
ILT
2069 /* Look through the sections. We put sections in the same program
2070 segment when the start of the second section can be placed within
2071 a few bytes of the end of the first section. */
2072 last_hdr = NULL;
2073 phdr_index = 0;
2074 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
edf3fe48
ILT
2075 writable = false;
2076 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2077 if (dynsec != NULL
2078 && (dynsec->flags & SEC_LOAD) == 0)
2079 dynsec = NULL;
2080
7fc6a16a
ILT
2081 /* Deal with -Ttext or something similar such that the first section
2082 is not adjacent to the program headers. This is an
2083 approximation, since at this point we don't know exactly how many
2084 program headers we will need. */
2085 if (count > 0)
2086 {
2087 bfd_size_type phdr_size;
2088
2089 phdr_size = elf_tdata (abfd)->program_header_size;
2090 if (phdr_size == 0)
2091 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
cdb88e87
ILT
2092 if ((abfd->flags & D_PAGED) == 0
2093 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
7fc6a16a
ILT
2094 phdr_in_section = false;
2095 }
edf3fe48 2096
fd0198f0 2097 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
ede4eed4 2098 {
fd0198f0 2099 asection *hdr;
191d910c 2100 boolean new_segment;
ede4eed4 2101
fd0198f0 2102 hdr = *hdrpp;
ede4eed4 2103
fd0198f0 2104 /* See if this section and the last one will fit in the same
191d910c
ILT
2105 segment. */
2106
2107 if (last_hdr == NULL)
2108 {
2109 /* If we don't have a segment yet, then we don't need a new
2110 one (we build the last one after this loop). */
2111 new_segment = false;
2112 }
2113 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2114 {
2115 /* If this section has a different relation between the
2116 virtual address and the load address, then we need a new
2117 segment. */
2118 new_segment = true;
2119 }
191d910c 2120 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
76af94b9 2121 < BFD_ALIGN (hdr->lma, maxpagesize))
191d910c
ILT
2122 {
2123 /* If putting this section in this segment would force us to
2124 skip a page in the segment, then we need a new segment. */
2125 new_segment = true;
2126 }
2127 else if ((last_hdr->flags & SEC_LOAD) == 0
2128 && (hdr->flags & SEC_LOAD) != 0)
2129 {
2130 /* We don't want to put a loadable section after a
2131 nonloadable section in the same segment. */
2132 new_segment = true;
2133 }
c63729b5
ILT
2134 else if ((abfd->flags & D_PAGED) == 0)
2135 {
2136 /* If the file is not demand paged, which means that we
2137 don't require the sections to be correctly aligned in the
2138 file, then there is no other reason for a new segment. */
2139 new_segment = false;
2140 }
191d910c
ILT
2141 else if (! writable
2142 && (hdr->flags & SEC_READONLY) == 0
2143 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2144 == hdr->lma))
2145 {
2146 /* We don't want to put a writable section in a read only
2147 segment, unless they are on the same page in memory
2148 anyhow. We already know that the last section does not
2149 bring us past the current section on the page, so the
2150 only case in which the new section is not on the same
2151 page as the previous section is when the previous section
2152 ends precisely on a page boundary. */
2153 new_segment = true;
2154 }
2155 else
2156 {
2157 /* Otherwise, we can use the same segment. */
2158 new_segment = false;
2159 }
2160
2161 if (! new_segment)
fd0198f0 2162 {
50bd50d4
MH
2163 if ((hdr->flags & SEC_READONLY) == 0)
2164 writable = true;
fd0198f0
ILT
2165 last_hdr = hdr;
2166 continue;
2167 }
ede4eed4 2168
191d910c
ILT
2169 /* We need a new program segment. We must create a new program
2170 header holding all the sections from phdr_index until hdr. */
ede4eed4 2171
edf3fe48 2172 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
fd0198f0
ILT
2173 if (m == NULL)
2174 goto error_return;
ede4eed4 2175
fd0198f0
ILT
2176 *pm = m;
2177 pm = &m->next;
ede4eed4 2178
edf3fe48
ILT
2179 if ((hdr->flags & SEC_READONLY) == 0)
2180 writable = true;
50bd50d4
MH
2181 else
2182 writable = false;
edf3fe48 2183
fd0198f0
ILT
2184 last_hdr = hdr;
2185 phdr_index = i;
edf3fe48 2186 phdr_in_section = false;
ede4eed4 2187 }
fd0198f0
ILT
2188
2189 /* Create a final PT_LOAD program segment. */
2190 if (last_hdr != NULL)
ede4eed4 2191 {
edf3fe48 2192 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
fd0198f0
ILT
2193 if (m == NULL)
2194 goto error_return;
2195
2196 *pm = m;
2197 pm = &m->next;
ede4eed4
KR
2198 }
2199
fd0198f0 2200 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
edf3fe48 2201 if (dynsec != NULL)
ede4eed4 2202 {
fd0198f0
ILT
2203 m = ((struct elf_segment_map *)
2204 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2205 if (m == NULL)
a9713b91 2206 goto error_return;
fd0198f0
ILT
2207 m->next = NULL;
2208 m->p_type = PT_DYNAMIC;
2209 m->count = 1;
edf3fe48 2210 m->sections[0] = dynsec;
ede4eed4 2211
fd0198f0
ILT
2212 *pm = m;
2213 pm = &m->next;
ede4eed4
KR
2214 }
2215
a66a61a0
ILT
2216 /* For each loadable .note section, add a PT_NOTE segment. We don't
2217 use bfd_get_section_by_name, because if we link together
2218 nonloadable .note sections and loadable .note sections, we will
2219 generate two .note sections in the output file. FIXME: Using
2220 names for section types is bogus anyhow. */
2221 for (s = abfd->sections; s != NULL; s = s->next)
2222 {
2223 if ((s->flags & SEC_LOAD) != 0
2224 && strncmp (s->name, ".note", 5) == 0)
2225 {
2226 m = ((struct elf_segment_map *)
2227 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2228 if (m == NULL)
2229 goto error_return;
2230 m->next = NULL;
2231 m->p_type = PT_NOTE;
2232 m->count = 1;
2233 m->sections[0] = s;
2234
2235 *pm = m;
2236 pm = &m->next;
2237 }
2238 }
2239
fd0198f0
ILT
2240 free (sections);
2241 sections = NULL;
ae115e51 2242
fd0198f0
ILT
2243 elf_tdata (abfd)->segment_map = mfirst;
2244 return true;
2245
2246 error_return:
2247 if (sections != NULL)
2248 free (sections);
2249 return false;
ede4eed4
KR
2250}
2251
fd0198f0 2252/* Sort sections by VMA. */
ede4eed4 2253
fd0198f0
ILT
2254static int
2255elf_sort_sections (arg1, arg2)
2256 const PTR arg1;
2257 const PTR arg2;
ede4eed4 2258{
fd0198f0
ILT
2259 const asection *sec1 = *(const asection **) arg1;
2260 const asection *sec2 = *(const asection **) arg2;
ede4eed4 2261
fd0198f0
ILT
2262 if (sec1->vma < sec2->vma)
2263 return -1;
2264 else if (sec1->vma > sec2->vma)
2265 return 1;
ede4eed4 2266
cdb88e87
ILT
2267 /* Sort by LMA. Normally the LMA and the VMA will be the same, and
2268 this will do nothing. */
2269 if (sec1->lma < sec2->lma)
2270 return -1;
2271 else if (sec1->lma > sec2->lma)
2272 return 1;
2273
fd0198f0 2274 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
ede4eed4 2275
fd0198f0 2276#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
ede4eed4 2277
fd0198f0 2278 if (TOEND (sec1))
f6727b90
ILT
2279 {
2280 if (TOEND (sec2))
2281 return sec1->target_index - sec2->target_index;
2282 else
2283 return 1;
2284 }
ede4eed4 2285
fd0198f0
ILT
2286 if (TOEND (sec2))
2287 return -1;
ede4eed4 2288
fd0198f0 2289#undef TOEND
ede4eed4 2290
fd0198f0
ILT
2291 /* Sort by size, to put zero sized sections before others at the
2292 same address. */
ede4eed4 2293
fd0198f0
ILT
2294 if (sec1->_raw_size < sec2->_raw_size)
2295 return -1;
2296 if (sec1->_raw_size > sec2->_raw_size)
2297 return 1;
ede4eed4 2298
fd0198f0
ILT
2299 return sec1->target_index - sec2->target_index;
2300}
ede4eed4 2301
fd0198f0
ILT
2302/* Assign file positions to the sections based on the mapping from
2303 sections to segments. This function also sets up some fields in
2304 the file header, and writes out the program headers. */
ede4eed4 2305
fd0198f0
ILT
2306static boolean
2307assign_file_positions_for_segments (abfd)
2308 bfd *abfd;
2309{
2310 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2311 unsigned int count;
2312 struct elf_segment_map *m;
2313 unsigned int alloc;
2314 Elf_Internal_Phdr *phdrs;
64f808f9 2315 file_ptr off, voff;
6933148a
ILT
2316 bfd_vma filehdr_vaddr, filehdr_paddr;
2317 bfd_vma phdrs_vaddr, phdrs_paddr;
fd0198f0
ILT
2318 Elf_Internal_Phdr *p;
2319
2320 if (elf_tdata (abfd)->segment_map == NULL)
2321 {
2322 if (! map_sections_to_segments (abfd))
2323 return false;
2324 }
ede4eed4 2325
5b3b9ff6
ILT
2326 if (bed->elf_backend_modify_segment_map)
2327 {
2328 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2329 return false;
2330 }
2331
fd0198f0
ILT
2332 count = 0;
2333 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2334 ++count;
ede4eed4 2335
fd0198f0
ILT
2336 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2337 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2338 elf_elfheader (abfd)->e_phnum = count;
ede4eed4 2339
fd0198f0
ILT
2340 if (count == 0)
2341 return true;
ede4eed4 2342
fd0198f0
ILT
2343 /* If we already counted the number of program segments, make sure
2344 that we allocated enough space. This happens when SIZEOF_HEADERS
2345 is used in a linker script. */
2346 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2347 if (alloc != 0 && count > alloc)
2348 {
2349 ((*_bfd_error_handler)
2350 ("%s: Not enough room for program headers (allocated %u, need %u)",
2351 bfd_get_filename (abfd), alloc, count));
2352 bfd_set_error (bfd_error_bad_value);
2353 return false;
ede4eed4
KR
2354 }
2355
fd0198f0
ILT
2356 if (alloc == 0)
2357 alloc = count;
2358
2359 phdrs = ((Elf_Internal_Phdr *)
2360 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2361 if (phdrs == NULL)
a9713b91 2362 return false;
ede4eed4 2363
fd0198f0
ILT
2364 off = bed->s->sizeof_ehdr;
2365 off += alloc * bed->s->sizeof_phdr;
ede4eed4 2366
6933148a
ILT
2367 filehdr_vaddr = 0;
2368 filehdr_paddr = 0;
2369 phdrs_vaddr = 0;
2370 phdrs_paddr = 0;
fd0198f0
ILT
2371 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2372 m != NULL;
2373 m = m->next, p++)
2374 {
2375 unsigned int i;
2376 asection **secpp;
fd0198f0 2377
3b950780
ILT
2378 /* If elf_segment_map is not from map_sections_to_segments, the
2379 sections may not be correctly ordered. */
2380 if (m->count > 0)
2381 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2382 elf_sort_sections);
2383
fd0198f0
ILT
2384 p->p_type = m->p_type;
2385
2386 if (m->p_flags_valid)
2387 p->p_flags = m->p_flags;
14899eb7
ILT
2388 else
2389 p->p_flags = 0;
fd0198f0 2390
d49ddb85
ILT
2391 if (p->p_type == PT_LOAD
2392 && m->count > 0
d7775b43 2393 && (m->sections[0]->flags & SEC_ALLOC) != 0)
cdb88e87
ILT
2394 {
2395 if ((abfd->flags & D_PAGED) != 0)
2396 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2397 else
2398 off += ((m->sections[0]->vma - off)
2399 % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2400 }
44ef8897 2401
fd0198f0
ILT
2402 if (m->count == 0)
2403 p->p_vaddr = 0;
2404 else
2405 p->p_vaddr = m->sections[0]->vma;
ede4eed4 2406
fd0198f0
ILT
2407 if (m->p_paddr_valid)
2408 p->p_paddr = m->p_paddr;
2409 else if (m->count == 0)
2410 p->p_paddr = 0;
2411 else
2412 p->p_paddr = m->sections[0]->lma;
2413
cdb88e87
ILT
2414 if (p->p_type == PT_LOAD
2415 && (abfd->flags & D_PAGED) != 0)
fd0198f0
ILT
2416 p->p_align = bed->maxpagesize;
2417 else if (m->count == 0)
2418 p->p_align = bed->s->file_align;
2419 else
2420 p->p_align = 0;
2421
6933148a 2422 p->p_offset = 0;
fd0198f0
ILT
2423 p->p_filesz = 0;
2424 p->p_memsz = 0;
2425
6933148a 2426 if (m->includes_filehdr)
ede4eed4 2427 {
14899eb7
ILT
2428 if (! m->p_flags_valid)
2429 p->p_flags |= PF_R;
6933148a
ILT
2430 p->p_offset = 0;
2431 p->p_filesz = bed->s->sizeof_ehdr;
2432 p->p_memsz = bed->s->sizeof_ehdr;
2433 if (m->count > 0)
2434 {
2435 BFD_ASSERT (p->p_type == PT_LOAD);
20b2c808 2436
f6727b90 2437 if (p->p_vaddr < (bfd_vma) off)
20b2c808
DE
2438 {
2439 _bfd_error_handler ("%s: Not enough room for program headers, try linking with -N",
2440 bfd_get_filename (abfd));
2441 bfd_set_error (bfd_error_bad_value);
2442 return false;
2443 }
2444
6933148a
ILT
2445 p->p_vaddr -= off;
2446 if (! m->p_paddr_valid)
2447 p->p_paddr -= off;
2448 }
2449 if (p->p_type == PT_LOAD)
2450 {
2451 filehdr_vaddr = p->p_vaddr;
2452 filehdr_paddr = p->p_paddr;
2453 }
2454 }
fd0198f0 2455
6933148a
ILT
2456 if (m->includes_phdrs)
2457 {
14899eb7
ILT
2458 if (! m->p_flags_valid)
2459 p->p_flags |= PF_R;
6933148a 2460 if (m->includes_filehdr)
fd0198f0 2461 {
6933148a 2462 if (p->p_type == PT_LOAD)
fd0198f0 2463 {
6933148a
ILT
2464 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2465 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
fd0198f0 2466 }
6933148a
ILT
2467 }
2468 else
2469 {
2470 p->p_offset = bed->s->sizeof_ehdr;
2471 if (m->count > 0)
2472 {
2473 BFD_ASSERT (p->p_type == PT_LOAD);
2474 p->p_vaddr -= off - p->p_offset;
2475 if (! m->p_paddr_valid)
2476 p->p_paddr -= off - p->p_offset;
2477 }
2478 if (p->p_type == PT_LOAD)
fd0198f0 2479 {
6933148a
ILT
2480 phdrs_vaddr = p->p_vaddr;
2481 phdrs_paddr = p->p_paddr;
fd0198f0 2482 }
6933148a
ILT
2483 }
2484 p->p_filesz += alloc * bed->s->sizeof_phdr;
2485 p->p_memsz += alloc * bed->s->sizeof_phdr;
2486 }
2487
2488 if (p->p_type == PT_LOAD)
2489 {
2490 if (! m->includes_filehdr && ! m->includes_phdrs)
2491 p->p_offset = off;
2492 else
2493 {
2494 file_ptr adjust;
fd0198f0 2495
6933148a
ILT
2496 adjust = off - (p->p_offset + p->p_filesz);
2497 p->p_filesz += adjust;
2498 p->p_memsz += adjust;
fd0198f0 2499 }
ede4eed4
KR
2500 }
2501
64f808f9 2502 voff = off;
fd0198f0 2503 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
ede4eed4 2504 {
fd0198f0
ILT
2505 asection *sec;
2506 flagword flags;
2507 bfd_size_type align;
2508
2509 sec = *secpp;
2510 flags = sec->flags;
cdb88e87 2511 align = 1 << bfd_get_section_alignment (abfd, sec);
fd0198f0
ILT
2512
2513 if (p->p_type == PT_LOAD)
2514 {
2515 bfd_vma adjust;
2516
c63729b5
ILT
2517 if ((flags & SEC_LOAD) != 0)
2518 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2519 else if ((flags & SEC_ALLOC) != 0)
fd0198f0 2520 {
c63729b5
ILT
2521 /* The section VMA must equal the file position
2522 modulo the page size. FIXME: I'm not sure if
2523 this adjustment is really necessary. We used to
2524 not have the SEC_LOAD case just above, and then
2525 this was necessary, but now I'm not sure. */
cdb88e87
ILT
2526 if ((abfd->flags & D_PAGED) != 0)
2527 adjust = (sec->vma - voff) % bed->maxpagesize;
2528 else
2529 adjust = (sec->vma - voff) % align;
c63729b5 2530 }
f6727b90
ILT
2531 else
2532 adjust = 0;
c63729b5
ILT
2533
2534 if (adjust != 0)
2535 {
2536 if (i == 0)
2537 abort ();
2538 p->p_memsz += adjust;
2539 off += adjust;
2540 voff += adjust;
2541 if ((flags & SEC_LOAD) != 0)
2542 p->p_filesz += adjust;
fd0198f0
ILT
2543 }
2544
2545 sec->filepos = off;
2546
c63729b5
ILT
2547 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2548 used in a linker script we may have a section with
2549 SEC_LOAD clear but which is supposed to have
2550 contents. */
2551 if ((flags & SEC_LOAD) != 0
2552 || (flags & SEC_HAS_CONTENTS) != 0)
fd0198f0 2553 off += sec->_raw_size;
64f808f9
ILT
2554 if ((flags & SEC_ALLOC) != 0)
2555 voff += sec->_raw_size;
fd0198f0
ILT
2556 }
2557
2558 p->p_memsz += sec->_raw_size;
2559
2560 if ((flags & SEC_LOAD) != 0)
2561 p->p_filesz += sec->_raw_size;
2562
fd0198f0
ILT
2563 if (align > p->p_align)
2564 p->p_align = align;
2565
2566 if (! m->p_flags_valid)
2567 {
14899eb7 2568 p->p_flags |= PF_R;
fd0198f0
ILT
2569 if ((flags & SEC_CODE) != 0)
2570 p->p_flags |= PF_X;
2571 if ((flags & SEC_READONLY) == 0)
2572 p->p_flags |= PF_W;
2573 }
ede4eed4 2574 }
fd0198f0 2575 }
ede4eed4 2576
fd0198f0
ILT
2577 /* Now that we have set the section file positions, we can set up
2578 the file positions for the non PT_LOAD segments. */
2579 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2580 m != NULL;
2581 m = m->next, p++)
2582 {
2583 if (p->p_type != PT_LOAD && m->count > 0)
ede4eed4 2584 {
6933148a
ILT
2585 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2586 p->p_offset = m->sections[0]->filepos;
2587 }
2588 if (m->count == 0)
2589 {
2590 if (m->includes_filehdr)
2591 {
2592 p->p_vaddr = filehdr_vaddr;
2593 if (! m->p_paddr_valid)
2594 p->p_paddr = filehdr_paddr;
2595 }
2596 else if (m->includes_phdrs)
2597 {
2598 p->p_vaddr = phdrs_vaddr;
2599 if (! m->p_paddr_valid)
2600 p->p_paddr = phdrs_paddr;
2601 }
ede4eed4 2602 }
ede4eed4
KR
2603 }
2604
fd0198f0
ILT
2605 /* Clear out any program headers we allocated but did not use. */
2606 for (; count < alloc; count++, p++)
ede4eed4 2607 {
fd0198f0
ILT
2608 memset (p, 0, sizeof *p);
2609 p->p_type = PT_NULL;
ede4eed4
KR
2610 }
2611
fd0198f0 2612 elf_tdata (abfd)->phdr = phdrs;
ede4eed4 2613
fd0198f0 2614 elf_tdata (abfd)->next_file_pos = off;
ede4eed4 2615
fd0198f0
ILT
2616 /* Write out the program headers. */
2617 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2618 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2619 return false;
2620
2621 return true;
2622}
2623
2624/* Get the size of the program header.
2625
2626 If this is called by the linker before any of the section VMA's are set, it
2627 can't calculate the correct value for a strange memory layout. This only
2628 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2629 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2630 data segment (exclusive of .interp and .dynamic).
2631
2632 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2633 will be two segments. */
2634
2635static bfd_size_type
2636get_program_header_size (abfd)
2637 bfd *abfd;
2638{
2639 size_t segs;
2640 asection *s;
2641 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2642
2643 /* We can't return a different result each time we're called. */
2644 if (elf_tdata (abfd)->program_header_size != 0)
2645 return elf_tdata (abfd)->program_header_size;
ae115e51 2646
3b950780
ILT
2647 if (elf_tdata (abfd)->segment_map != NULL)
2648 {
2649 struct elf_segment_map *m;
2650
2651 segs = 0;
2652 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2653 ++segs;
2654 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2655 return elf_tdata (abfd)->program_header_size;
2656 }
2657
fd0198f0
ILT
2658 /* Assume we will need exactly two PT_LOAD segments: one for text
2659 and one for data. */
2660 segs = 2;
2661
2662 s = bfd_get_section_by_name (abfd, ".interp");
2663 if (s != NULL && (s->flags & SEC_LOAD) != 0)
ede4eed4 2664 {
fd0198f0
ILT
2665 /* If we have a loadable interpreter section, we need a
2666 PT_INTERP segment. In this case, assume we also need a
2667 PT_PHDR segment, although that may not be true for all
2668 targets. */
2669 segs += 2;
ede4eed4
KR
2670 }
2671
fd0198f0 2672 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
ede4eed4 2673 {
fd0198f0
ILT
2674 /* We need a PT_DYNAMIC segment. */
2675 ++segs;
ede4eed4 2676 }
ede4eed4 2677
a66a61a0
ILT
2678 for (s = abfd->sections; s != NULL; s = s->next)
2679 {
2680 if ((s->flags & SEC_LOAD) != 0
2681 && strncmp (s->name, ".note", 5) == 0)
2682 {
2683 /* We need a PT_NOTE segment. */
2684 ++segs;
2685 }
2686 }
2687
fd0198f0 2688 /* Let the backend count up any program headers it might need. */
5b3b9ff6
ILT
2689 if (bed->elf_backend_additional_program_headers)
2690 {
2691 int a;
2692
2693 a = (*bed->elf_backend_additional_program_headers) (abfd);
2694 if (a == -1)
2695 abort ();
2696 segs += a;
2697 }
ede4eed4 2698
fd0198f0
ILT
2699 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2700 return elf_tdata (abfd)->program_header_size;
ede4eed4
KR
2701}
2702
2703/* Work out the file positions of all the sections. This is called by
2704 _bfd_elf_compute_section_file_positions. All the section sizes and
2705 VMAs must be known before this is called.
2706
2707 We do not consider reloc sections at this point, unless they form
2708 part of the loadable image. Reloc sections are assigned file
2709 positions in assign_file_positions_for_relocs, which is called by
2710 write_object_contents and final_link.
2711
fd0198f0 2712 We also don't set the positions of the .symtab and .strtab here. */
ede4eed4
KR
2713
2714static boolean
fd0198f0 2715assign_file_positions_except_relocs (abfd)
ede4eed4 2716 bfd *abfd;
ede4eed4
KR
2717{
2718 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2719 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2720 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2721 file_ptr off;
2722 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2723
ede4eed4
KR
2724 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2725 {
2726 Elf_Internal_Shdr **hdrpp;
2727 unsigned int i;
2728
fd0198f0
ILT
2729 /* Start after the ELF header. */
2730 off = i_ehdrp->e_ehsize;
2731
ede4eed4
KR
2732 /* We are not creating an executable, which means that we are
2733 not creating a program header, and that the actual order of
2734 the sections in the file is unimportant. */
2735 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2736 {
2737 Elf_Internal_Shdr *hdr;
2738
2739 hdr = *hdrpp;
2740 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2741 {
2742 hdr->sh_offset = -1;
2743 continue;
2744 }
fd0198f0
ILT
2745 if (i == tdata->symtab_section
2746 || i == tdata->strtab_section)
ede4eed4
KR
2747 {
2748 hdr->sh_offset = -1;
2749 continue;
2750 }
2751
5fe14a9f 2752 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
ede4eed4
KR
2753 }
2754 }
2755 else
2756 {
ede4eed4 2757 unsigned int i;
fd0198f0 2758 Elf_Internal_Shdr **hdrpp;
ede4eed4 2759
fd0198f0
ILT
2760 /* Assign file positions for the loaded sections based on the
2761 assignment of sections to segments. */
2762 if (! assign_file_positions_for_segments (abfd))
ede4eed4
KR
2763 return false;
2764
fd0198f0
ILT
2765 /* Assign file positions for the other sections. */
2766
2767 off = elf_tdata (abfd)->next_file_pos;
2768 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
ede4eed4
KR
2769 {
2770 Elf_Internal_Shdr *hdr;
2771
2772 hdr = *hdrpp;
fd0198f0
ILT
2773 if (hdr->bfd_section != NULL
2774 && hdr->bfd_section->filepos != 0)
2775 hdr->sh_offset = hdr->bfd_section->filepos;
2776 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
ede4eed4 2777 {
fd0198f0
ILT
2778 ((*_bfd_error_handler)
2779 ("%s: warning: allocated section `%s' not in segment",
2780 bfd_get_filename (abfd),
2781 (hdr->bfd_section == NULL
2782 ? "*unknown*"
2783 : hdr->bfd_section->name)));
cdb88e87
ILT
2784 if ((abfd->flags & D_PAGED) != 0)
2785 off += (hdr->sh_addr - off) % bed->maxpagesize;
2786 else
2787 off += (hdr->sh_addr - off) % hdr->sh_addralign;
5fe14a9f
ILT
2788 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2789 false);
ede4eed4 2790 }
fd0198f0
ILT
2791 else if (hdr->sh_type == SHT_REL
2792 || hdr->sh_type == SHT_RELA
2793 || hdr == i_shdrpp[tdata->symtab_section]
2794 || hdr == i_shdrpp[tdata->strtab_section])
2795 hdr->sh_offset = -1;
2796 else
2797 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2798 }
ede4eed4
KR
2799 }
2800
2801 /* Place the section headers. */
2802 off = align_file_position (off, bed->s->file_align);
2803 i_ehdrp->e_shoff = off;
2804 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2805
2806 elf_tdata (abfd)->next_file_pos = off;
2807
2808 return true;
2809}
2810
ede4eed4
KR
2811static boolean
2812prep_headers (abfd)
2813 bfd *abfd;
2814{
2815 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2816 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2817 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2818 int count;
2819 struct bfd_strtab_hash *shstrtab;
2820 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2821
2822 i_ehdrp = elf_elfheader (abfd);
2823 i_shdrp = elf_elfsections (abfd);
2824
2825 shstrtab = _bfd_elf_stringtab_init ();
2826 if (shstrtab == NULL)
2827 return false;
2828
2829 elf_shstrtab (abfd) = shstrtab;
2830
2831 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2832 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2833 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2834 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2835
2836 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2837 i_ehdrp->e_ident[EI_DATA] =
86587dd4 2838 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
ede4eed4
KR
2839 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2840
2841 for (count = EI_PAD; count < EI_NIDENT; count++)
2842 i_ehdrp->e_ident[count] = 0;
2843
2844 if ((abfd->flags & DYNAMIC) != 0)
2845 i_ehdrp->e_type = ET_DYN;
2846 else if ((abfd->flags & EXEC_P) != 0)
2847 i_ehdrp->e_type = ET_EXEC;
2848 else
2849 i_ehdrp->e_type = ET_REL;
2850
2851 switch (bfd_get_arch (abfd))
2852 {
2853 case bfd_arch_unknown:
2854 i_ehdrp->e_machine = EM_NONE;
2855 break;
2856 case bfd_arch_sparc:
2857 if (bed->s->arch_size == 64)
2858 i_ehdrp->e_machine = EM_SPARC64;
2859 else
2860 i_ehdrp->e_machine = EM_SPARC;
2861 break;
2862 case bfd_arch_i386:
2863 i_ehdrp->e_machine = EM_386;
2864 break;
2865 case bfd_arch_m68k:
2866 i_ehdrp->e_machine = EM_68K;
2867 break;
2868 case bfd_arch_m88k:
2869 i_ehdrp->e_machine = EM_88K;
2870 break;
2871 case bfd_arch_i860:
2872 i_ehdrp->e_machine = EM_860;
2873 break;
2874 case bfd_arch_mips: /* MIPS Rxxxx */
2875 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2876 break;
2877 case bfd_arch_hppa:
2878 i_ehdrp->e_machine = EM_PARISC;
2879 break;
2880 case bfd_arch_powerpc:
2881 i_ehdrp->e_machine = EM_PPC;
2882 break;
50bd50d4
MH
2883 case bfd_arch_alpha:
2884 i_ehdrp->e_machine = EM_ALPHA;
2885 break;
f0c12b73
DE
2886 case bfd_arch_sh:
2887 i_ehdrp->e_machine = EM_SH;
2888 break;
50bd50d4
MH
2889 case bfd_arch_d10v:
2890 i_ehdrp->e_machine = EM_CYGNUS_D10V;
2891 break;
fd8d7c31
MH
2892/* start-sanitize-d30v */
2893 case bfd_arch_d30v:
2894 i_ehdrp->e_machine = EM_CYGNUS_D30V;
2895 break;
2896/* end-sanitize-d30v */
f0c12b73 2897 case bfd_arch_v850:
8988d935
NC
2898 switch (bfd_get_mach (abfd))
2899 {
2900 default:
2901 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
8988d935 2902 }
f0c12b73 2903 break;
8988d935 2904 case bfd_arch_arc:
ede4eed4
KR
2905 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2906 break;
f0c12b73
DE
2907 case bfd_arch_m32r:
2908 i_ehdrp->e_machine = EM_CYGNUS_M32R;
2909 break;
80be821d
ILT
2910 case bfd_arch_mn10200:
2911 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
2912 break;
2913 case bfd_arch_mn10300:
2914 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
efc2b064 2915 break;
ede4eed4
KR
2916 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2917 default:
2918 i_ehdrp->e_machine = EM_NONE;
2919 }
2920 i_ehdrp->e_version = bed->s->ev_current;
2921 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2922
2923 /* no program header, for now. */
2924 i_ehdrp->e_phoff = 0;
2925 i_ehdrp->e_phentsize = 0;
2926 i_ehdrp->e_phnum = 0;
2927
2928 /* each bfd section is section header entry */
2929 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2930 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2931
2932 /* if we're building an executable, we'll need a program header table */
2933 if (abfd->flags & EXEC_P)
2934 {
2935 /* it all happens later */
2936#if 0
2937 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2938
2939 /* elf_build_phdrs() returns a (NULL-terminated) array of
2940 Elf_Internal_Phdrs */
2941 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2942 i_ehdrp->e_phoff = outbase;
2943 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2944#endif
2945 }
2946 else
2947 {
2948 i_ehdrp->e_phentsize = 0;
2949 i_phdrp = 0;
2950 i_ehdrp->e_phoff = 0;
2951 }
2952
2953 elf_tdata (abfd)->symtab_hdr.sh_name =
2954 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2955 elf_tdata (abfd)->strtab_hdr.sh_name =
2956 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2957 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2958 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2959 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2960 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2961 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2962 return false;
2963
2964 return true;
2965}
2966
2967/* Assign file positions for all the reloc sections which are not part
2968 of the loadable file image. */
2969
2970void
2971_bfd_elf_assign_file_positions_for_relocs (abfd)
2972 bfd *abfd;
2973{
2974 file_ptr off;
2975 unsigned int i;
2976 Elf_Internal_Shdr **shdrpp;
2977
2978 off = elf_tdata (abfd)->next_file_pos;
2979
2980 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2981 i < elf_elfheader (abfd)->e_shnum;
2982 i++, shdrpp++)
2983 {
2984 Elf_Internal_Shdr *shdrp;
2985
2986 shdrp = *shdrpp;
2987 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2988 && shdrp->sh_offset == -1)
5fe14a9f 2989 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
ede4eed4
KR
2990 }
2991
2992 elf_tdata (abfd)->next_file_pos = off;
2993}
2994
2995boolean
2996_bfd_elf_write_object_contents (abfd)
2997 bfd *abfd;
2998{
2999 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3000 Elf_Internal_Ehdr *i_ehdrp;
3001 Elf_Internal_Shdr **i_shdrp;
3002 boolean failed;
3003 unsigned int count;
3004
3005 if (! abfd->output_has_begun
3006 && ! _bfd_elf_compute_section_file_positions (abfd,
3007 (struct bfd_link_info *) NULL))
3008 return false;
3009
3010 i_shdrp = elf_elfsections (abfd);
3011 i_ehdrp = elf_elfheader (abfd);
3012
3013 failed = false;
3014 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3015 if (failed)
3016 return false;
3017 _bfd_elf_assign_file_positions_for_relocs (abfd);
3018
3019 /* After writing the headers, we need to write the sections too... */
3020 for (count = 1; count < i_ehdrp->e_shnum; count++)
3021 {
3022 if (bed->elf_backend_section_processing)
3023 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3024 if (i_shdrp[count]->contents)
3025 {
3026 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3027 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3028 1, abfd)
3029 != i_shdrp[count]->sh_size))
3030 return false;
3031 }
3032 }
3033
3034 /* Write out the section header names. */
3035 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3036 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3037 return false;
3038
3039 if (bed->elf_backend_final_write_processing)
3040 (*bed->elf_backend_final_write_processing) (abfd,
3041 elf_tdata (abfd)->linker);
3042
3043 return bed->s->write_shdrs_and_ehdr (abfd);
3044}
3045
3046/* given a section, search the header to find them... */
3047int
3048_bfd_elf_section_from_bfd_section (abfd, asect)
3049 bfd *abfd;
3050 struct sec *asect;
3051{
3052 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3053 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3054 int index;
3055 Elf_Internal_Shdr *hdr;
3056 int maxindex = elf_elfheader (abfd)->e_shnum;
3057
3058 for (index = 0; index < maxindex; index++)
3059 {
3060 hdr = i_shdrp[index];
3061 if (hdr->bfd_section == asect)
3062 return index;
3063 }
3064
3065 if (bed->elf_backend_section_from_bfd_section)
3066 {
3067 for (index = 0; index < maxindex; index++)
3068 {
3069 int retval;
3070
3071 hdr = i_shdrp[index];
3072 retval = index;
3073 if ((*bed->elf_backend_section_from_bfd_section)
3074 (abfd, hdr, asect, &retval))
3075 return retval;
3076 }
3077 }
3078
3079 if (bfd_is_abs_section (asect))
3080 return SHN_ABS;
3081 if (bfd_is_com_section (asect))
3082 return SHN_COMMON;
3083 if (bfd_is_und_section (asect))
3084 return SHN_UNDEF;
3085
3086 return -1;
3087}
3088
cb84f028
ILT
3089/* Given a BFD symbol, return the index in the ELF symbol table, or -1
3090 on error. */
3091
3092int
ede4eed4
KR
3093_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3094 bfd *abfd;
7fc6a16a 3095 asymbol **asym_ptr_ptr;
ede4eed4 3096{
7fc6a16a 3097 asymbol *asym_ptr = *asym_ptr_ptr;
ede4eed4
KR
3098 int idx;
3099 flagword flags = asym_ptr->flags;
3100
3101 /* When gas creates relocations against local labels, it creates its
3102 own symbol for the section, but does put the symbol into the
3103 symbol chain, so udata is 0. When the linker is generating
3104 relocatable output, this section symbol may be for one of the
3105 input sections rather than the output section. */
3106 if (asym_ptr->udata.i == 0
3107 && (flags & BSF_SECTION_SYM)
3108 && asym_ptr->section)
3109 {
3110 int indx;
3111
3112 if (asym_ptr->section->output_section != NULL)
3113 indx = asym_ptr->section->output_section->index;
3114 else
3115 indx = asym_ptr->section->index;
3116 if (elf_section_syms (abfd)[indx])
3117 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3118 }
3119
3120 idx = asym_ptr->udata.i;
cb84f028
ILT
3121
3122 if (idx == 0)
3123 {
3124 /* This case can occur when using --strip-symbol on a symbol
3125 which is used in a relocation entry. */
3126 (*_bfd_error_handler)
3127 ("%s: symbol `%s' required but not present",
3128 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3129 bfd_set_error (bfd_error_no_symbols);
3130 return -1;
3131 }
ede4eed4
KR
3132
3133#if DEBUG & 4
3134 {
3135 fprintf (stderr,
3136 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
cb84f028
ILT
3137 (long) asym_ptr, asym_ptr->name, idx, flags,
3138 elf_symbol_flags (flags));
ede4eed4
KR
3139 fflush (stderr);
3140 }
3141#endif
3142
3143 return idx;
3144}
3145
3dbf33ee
ILT
3146/* Copy private BFD data. This copies any program header information. */
3147
3148static boolean
3149copy_private_bfd_data (ibfd, obfd)
3150 bfd *ibfd;
3151 bfd *obfd;
3152{
6933148a 3153 Elf_Internal_Ehdr *iehdr;
3dbf33ee
ILT
3154 struct elf_segment_map *mfirst;
3155 struct elf_segment_map **pm;
3156 Elf_Internal_Phdr *p;
3157 unsigned int i, c;
3158
3159 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3160 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3161 return true;
3162
3163 if (elf_tdata (ibfd)->phdr == NULL)
3164 return true;
3165
6933148a
ILT
3166 iehdr = elf_elfheader (ibfd);
3167
3dbf33ee
ILT
3168 mfirst = NULL;
3169 pm = &mfirst;
3170
3171 c = elf_elfheader (ibfd)->e_phnum;
3172 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
3173 {
3dbf33ee 3174 unsigned int csecs;
6933148a
ILT
3175 asection *s;
3176 struct elf_segment_map *m;
3177 unsigned int isec;
3dbf33ee
ILT
3178
3179 csecs = 0;
3dbf33ee 3180
6933148a
ILT
3181 /* The complicated case when p_vaddr is 0 is to handle the
3182 Solaris linker, which generates a PT_INTERP section with
3183 p_vaddr and p_memsz set to 0. */
3184 for (s = ibfd->sections; s != NULL; s = s->next)
3185 if (((s->vma >= p->p_vaddr
3186 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
3187 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
3188 || (p->p_vaddr == 0
3189 && p->p_filesz > 0
3190 && (s->flags & SEC_HAS_CONTENTS) != 0
3191 && (bfd_vma) s->filepos >= p->p_offset
3192 && ((bfd_vma) s->filepos + s->_raw_size
3193 <= p->p_offset + p->p_filesz)))
86587dd4 3194 && (s->flags & SEC_ALLOC) != 0
6933148a
ILT
3195 && s->output_section != NULL)
3196 ++csecs;
3dbf33ee
ILT
3197
3198 m = ((struct elf_segment_map *)
3199 bfd_alloc (obfd,
3200 (sizeof (struct elf_segment_map)
20db2495 3201 + ((size_t) csecs - 1) * sizeof (asection *))));
3dbf33ee 3202 if (m == NULL)
a9713b91 3203 return false;
3dbf33ee
ILT
3204
3205 m->next = NULL;
3206 m->p_type = p->p_type;
3207 m->p_flags = p->p_flags;
3208 m->p_flags_valid = 1;
3209 m->p_paddr = p->p_paddr;
3210 m->p_paddr_valid = 1;
3211
6933148a
ILT
3212 m->includes_filehdr = (p->p_offset == 0
3213 && p->p_filesz >= iehdr->e_ehsize);
3214
3215 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
3216 && (p->p_offset + p->p_filesz
3217 >= ((bfd_vma) iehdr->e_phoff
3218 + iehdr->e_phnum * iehdr->e_phentsize)));
3dbf33ee 3219
6933148a
ILT
3220 isec = 0;
3221 for (s = ibfd->sections; s != NULL; s = s->next)
3222 {
3223 if (((s->vma >= p->p_vaddr
3224 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
3225 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
3226 || (p->p_vaddr == 0
3227 && p->p_filesz > 0
3228 && (s->flags & SEC_HAS_CONTENTS) != 0
3229 && (bfd_vma) s->filepos >= p->p_offset
3230 && ((bfd_vma) s->filepos + s->_raw_size
3231 <= p->p_offset + p->p_filesz)))
86587dd4 3232 && (s->flags & SEC_ALLOC) != 0
6933148a 3233 && s->output_section != NULL)
3dbf33ee 3234 {
6933148a
ILT
3235 m->sections[isec] = s->output_section;
3236 ++isec;
3dbf33ee 3237 }
3dbf33ee 3238 }
6933148a 3239 BFD_ASSERT (isec == csecs);
6933148a 3240 m->count = csecs;
3dbf33ee
ILT
3241
3242 *pm = m;
3243 pm = &m->next;
3244 }
3245
3246 elf_tdata (obfd)->segment_map = mfirst;
3247
3248 return true;
3249}
3250
fd0198f0
ILT
3251/* Copy private section information. This copies over the entsize
3252 field, and sometimes the info field. */
3253
3254boolean
3255_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3256 bfd *ibfd;
3257 asection *isec;
3258 bfd *obfd;
3259 asection *osec;
3260{
3261 Elf_Internal_Shdr *ihdr, *ohdr;
3262
3263 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3264 || obfd->xvec->flavour != bfd_target_elf_flavour)
3265 return true;
3266
3dbf33ee
ILT
3267 /* Copy over private BFD data if it has not already been copied.
3268 This must be done here, rather than in the copy_private_bfd_data
3269 entry point, because the latter is called after the section
3270 contents have been set, which means that the program headers have
3271 already been worked out. */
3272 if (elf_tdata (obfd)->segment_map == NULL
3273 && elf_tdata (ibfd)->phdr != NULL)
3274 {
3275 asection *s;
3276
3277 /* Only set up the segments when all the sections have been set
3278 up. */
3279 for (s = ibfd->sections; s != NULL; s = s->next)
3280 if (s->output_section == NULL)
3281 break;
3282 if (s == NULL)
3283 {
3284 if (! copy_private_bfd_data (ibfd, obfd))
3285 return false;
3286 }
3287 }
3288
fd0198f0
ILT
3289 ihdr = &elf_section_data (isec)->this_hdr;
3290 ohdr = &elf_section_data (osec)->this_hdr;
3291
3292 ohdr->sh_entsize = ihdr->sh_entsize;
3293
3294 if (ihdr->sh_type == SHT_SYMTAB
d6bfcdb5
ILT
3295 || ihdr->sh_type == SHT_DYNSYM
3296 || ihdr->sh_type == SHT_GNU_verneed
3297 || ihdr->sh_type == SHT_GNU_verdef)
fd0198f0
ILT
3298 ohdr->sh_info = ihdr->sh_info;
3299
3300 return true;
3301}
3302
3303/* Copy private symbol information. If this symbol is in a section
3304 which we did not map into a BFD section, try to map the section
3305 index correctly. We use special macro definitions for the mapped
3306 section indices; these definitions are interpreted by the
3307 swap_out_syms function. */
3308
3309#define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3310#define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3311#define MAP_STRTAB (SHN_LORESERVE - 3)
3312#define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3313
3314boolean
3315_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3316 bfd *ibfd;
3317 asymbol *isymarg;
3318 bfd *obfd;
3319 asymbol *osymarg;
3320{
3321 elf_symbol_type *isym, *osym;
3322
efc2b064
JL
3323 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3324 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3325 return true;
3326
fd0198f0
ILT
3327 isym = elf_symbol_from (ibfd, isymarg);
3328 osym = elf_symbol_from (obfd, osymarg);
3329
3330 if (isym != NULL
3331 && osym != NULL
3332 && bfd_is_abs_section (isym->symbol.section))
3333 {
3334 unsigned int shndx;
3335
3336 shndx = isym->internal_elf_sym.st_shndx;
3337 if (shndx == elf_onesymtab (ibfd))
3338 shndx = MAP_ONESYMTAB;
3339 else if (shndx == elf_dynsymtab (ibfd))
3340 shndx = MAP_DYNSYMTAB;
3341 else if (shndx == elf_tdata (ibfd)->strtab_section)
3342 shndx = MAP_STRTAB;
3343 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3344 shndx = MAP_SHSTRTAB;
3345 osym->internal_elf_sym.st_shndx = shndx;
3346 }
3347
3348 return true;
3349}
3350
3351/* Swap out the symbols. */
3352
ede4eed4
KR
3353static boolean
3354swap_out_syms (abfd, sttp)
3355 bfd *abfd;
3356 struct bfd_strtab_hash **sttp;
3357{
3358 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3359
3360 if (!elf_map_symbols (abfd))
3361 return false;
3362
3363 /* Dump out the symtabs. */
3364 {
3365 int symcount = bfd_get_symcount (abfd);
3366 asymbol **syms = bfd_get_outsymbols (abfd);
3367 struct bfd_strtab_hash *stt;
3368 Elf_Internal_Shdr *symtab_hdr;
3369 Elf_Internal_Shdr *symstrtab_hdr;
3370 char *outbound_syms;
3371 int idx;
3372
3373 stt = _bfd_elf_stringtab_init ();
3374 if (stt == NULL)
3375 return false;
3376
3377 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3378 symtab_hdr->sh_type = SHT_SYMTAB;
3379 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
3380 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
3381 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
3382 symtab_hdr->sh_addralign = bed->s->file_align;
3383
3384 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3385 symstrtab_hdr->sh_type = SHT_STRTAB;
3386
3387 outbound_syms = bfd_alloc (abfd,
3388 (1 + symcount) * bed->s->sizeof_sym);
3389 if (outbound_syms == NULL)
a9713b91 3390 return false;
ede4eed4
KR
3391 symtab_hdr->contents = (PTR) outbound_syms;
3392
3393 /* now generate the data (for "contents") */
3394 {
3395 /* Fill in zeroth symbol and swap it out. */
3396 Elf_Internal_Sym sym;
3397 sym.st_name = 0;
3398 sym.st_value = 0;
3399 sym.st_size = 0;
3400 sym.st_info = 0;
3401 sym.st_other = 0;
3402 sym.st_shndx = SHN_UNDEF;
cf9fb9f2 3403 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
ede4eed4
KR
3404 outbound_syms += bed->s->sizeof_sym;
3405 }
3406 for (idx = 0; idx < symcount; idx++)
3407 {
3408 Elf_Internal_Sym sym;
3409 bfd_vma value = syms[idx]->value;
3410 elf_symbol_type *type_ptr;
3411 flagword flags = syms[idx]->flags;
052b35d2 3412 int type;
ede4eed4
KR
3413
3414 if (flags & BSF_SECTION_SYM)
3415 /* Section symbols have no names. */
3416 sym.st_name = 0;
3417 else
3418 {
3419 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3420 syms[idx]->name,
3421 true, false);
3422 if (sym.st_name == (unsigned long) -1)
3423 return false;
3424 }
3425
3426 type_ptr = elf_symbol_from (abfd, syms[idx]);
3427
3428 if (bfd_is_com_section (syms[idx]->section))
3429 {
3430 /* ELF common symbols put the alignment into the `value' field,
3431 and the size into the `size' field. This is backwards from
3432 how BFD handles it, so reverse it here. */
3433 sym.st_size = value;
3434 if (type_ptr == NULL
3435 || type_ptr->internal_elf_sym.st_value == 0)
3436 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3437 else
3438 sym.st_value = type_ptr->internal_elf_sym.st_value;
3439 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
3440 syms[idx]->section);
3441 }
3442 else
3443 {
3444 asection *sec = syms[idx]->section;
3445 int shndx;
3446
3447 if (sec->output_section)
3448 {
3449 value += sec->output_offset;
3450 sec = sec->output_section;
3451 }
3452 value += sec->vma;
3453 sym.st_value = value;
3454 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
fd0198f0
ILT
3455
3456 if (bfd_is_abs_section (sec)
3457 && type_ptr != NULL
3458 && type_ptr->internal_elf_sym.st_shndx != 0)
ede4eed4 3459 {
fd0198f0
ILT
3460 /* This symbol is in a real ELF section which we did
3461 not create as a BFD section. Undo the mapping done
3462 by copy_private_symbol_data. */
3463 shndx = type_ptr->internal_elf_sym.st_shndx;
3464 switch (shndx)
3465 {
3466 case MAP_ONESYMTAB:
3467 shndx = elf_onesymtab (abfd);
3468 break;
3469 case MAP_DYNSYMTAB:
3470 shndx = elf_dynsymtab (abfd);
3471 break;
3472 case MAP_STRTAB:
3473 shndx = elf_tdata (abfd)->strtab_section;
3474 break;
3475 case MAP_SHSTRTAB:
3476 shndx = elf_tdata (abfd)->shstrtab_section;
3477 break;
3478 default:
3479 break;
3480 }
3481 }
3482 else
3483 {
3484 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3485
3486 if (shndx == -1)
3487 {
3488 asection *sec2;
3489
3490 /* Writing this would be a hell of a lot easier if
3491 we had some decent documentation on bfd, and
3492 knew what to expect of the library, and what to
3493 demand of applications. For example, it
3494 appears that `objcopy' might not set the
3495 section of a symbol to be a section that is
3496 actually in the output file. */
3497 sec2 = bfd_get_section_by_name (abfd, sec->name);
3498 BFD_ASSERT (sec2 != 0);
3499 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3500 BFD_ASSERT (shndx != -1);
3501 }
ede4eed4 3502 }
fd0198f0
ILT
3503
3504 sym.st_shndx = shndx;
ede4eed4
KR
3505 }
3506
052b35d2
ILT
3507 if ((flags & BSF_FUNCTION) != 0)
3508 type = STT_FUNC;
3509 else if ((flags & BSF_OBJECT) != 0)
3510 type = STT_OBJECT;
3511 else
3512 type = STT_NOTYPE;
3513
ede4eed4 3514 if (bfd_is_com_section (syms[idx]->section))
052b35d2 3515 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
ede4eed4
KR
3516 else if (bfd_is_und_section (syms[idx]->section))
3517 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
3518 ? STB_WEAK
3519 : STB_GLOBAL),
052b35d2 3520 type);
ede4eed4
KR
3521 else if (flags & BSF_SECTION_SYM)
3522 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3523 else if (flags & BSF_FILE)
3524 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3525 else
3526 {
3527 int bind = STB_LOCAL;
ede4eed4
KR
3528
3529 if (flags & BSF_LOCAL)
3530 bind = STB_LOCAL;
3531 else if (flags & BSF_WEAK)
3532 bind = STB_WEAK;
3533 else if (flags & BSF_GLOBAL)
3534 bind = STB_GLOBAL;
3535
ede4eed4
KR
3536 sym.st_info = ELF_ST_INFO (bind, type);
3537 }
3538
80be821d
ILT
3539 if (type_ptr != NULL)
3540 sym.st_other = type_ptr->internal_elf_sym.st_other;
3541 else
3542 sym.st_other = 0;
3543
cf9fb9f2 3544 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
ede4eed4
KR
3545 outbound_syms += bed->s->sizeof_sym;
3546 }
3547
3548 *sttp = stt;
3549 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
3550 symstrtab_hdr->sh_type = SHT_STRTAB;
3551
3552 symstrtab_hdr->sh_flags = 0;
3553 symstrtab_hdr->sh_addr = 0;
3554 symstrtab_hdr->sh_entsize = 0;
3555 symstrtab_hdr->sh_link = 0;
3556 symstrtab_hdr->sh_info = 0;
3557 symstrtab_hdr->sh_addralign = 1;
3558 }
3559
3560 return true;
3561}
3562
3563/* Return the number of bytes required to hold the symtab vector.
3564
3565 Note that we base it on the count plus 1, since we will null terminate
3566 the vector allocated based on this size. However, the ELF symbol table
3567 always has a dummy entry as symbol #0, so it ends up even. */
3568
3569long
3570_bfd_elf_get_symtab_upper_bound (abfd)
3571 bfd *abfd;
3572{
3573 long symcount;
3574 long symtab_size;
3575 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
3576
3577 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3578 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3579
3580 return symtab_size;
3581}
3582
3583long
3584_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
3585 bfd *abfd;
3586{
3587 long symcount;
3588 long symtab_size;
3589 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3590
3591 if (elf_dynsymtab (abfd) == 0)
3592 {
3593 bfd_set_error (bfd_error_invalid_operation);
3594 return -1;
3595 }
3596
3597 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3598 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3599
3600 return symtab_size;
3601}
3602
3603long
3604_bfd_elf_get_reloc_upper_bound (abfd, asect)
3605 bfd *abfd;
3606 sec_ptr asect;
3607{
3608 return (asect->reloc_count + 1) * sizeof (arelent *);
3609}
3610
3611/* Canonicalize the relocs. */
3612
3613long
3614_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
3615 bfd *abfd;
3616 sec_ptr section;
3617 arelent **relptr;
3618 asymbol **symbols;
3619{
3620 arelent *tblptr;
3621 unsigned int i;
3622
e35765a9
ILT
3623 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
3624 section,
3625 symbols,
3626 false))
ede4eed4
KR
3627 return -1;
3628
3629 tblptr = section->relocation;
3630 for (i = 0; i < section->reloc_count; i++)
3631 *relptr++ = tblptr++;
3632
3633 *relptr = NULL;
3634
3635 return section->reloc_count;
3636}
3637
3638long
3639_bfd_elf_get_symtab (abfd, alocation)
3640 bfd *abfd;
3641 asymbol **alocation;
3642{
3643 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
3644
3645 if (symcount >= 0)
3646 bfd_get_symcount (abfd) = symcount;
3647 return symcount;
3648}
3649
3650long
3651_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
3652 bfd *abfd;
3653 asymbol **alocation;
3654{
3655 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
3656}
3657
e35765a9
ILT
3658/* Return the size required for the dynamic reloc entries. Any
3659 section that was actually installed in the BFD, and has type
3660 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
3661 considered to be a dynamic reloc section. */
3662
3663long
3664_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
3665 bfd *abfd;
3666{
3667 long ret;
3668 asection *s;
3669
3670 if (elf_dynsymtab (abfd) == 0)
3671 {
3672 bfd_set_error (bfd_error_invalid_operation);
3673 return -1;
3674 }
3675
3676 ret = sizeof (arelent *);
3677 for (s = abfd->sections; s != NULL; s = s->next)
3678 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
3679 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
3680 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3681 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
3682 * sizeof (arelent *));
3683
3684 return ret;
3685}
3686
3687/* Canonicalize the dynamic relocation entries. Note that we return
3688 the dynamic relocations as a single block, although they are
3689 actually associated with particular sections; the interface, which
3690 was designed for SunOS style shared libraries, expects that there
3691 is only one set of dynamic relocs. Any section that was actually
3692 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
3693 the dynamic symbol table, is considered to be a dynamic reloc
3694 section. */
3695
3696long
3697_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
3698 bfd *abfd;
3699 arelent **storage;
3700 asymbol **syms;
3701{
3702 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
3703 asection *s;
3704 long ret;
3705
3706 if (elf_dynsymtab (abfd) == 0)
3707 {
3708 bfd_set_error (bfd_error_invalid_operation);
3709 return -1;
3710 }
3711
3712 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3713 ret = 0;
3714 for (s = abfd->sections; s != NULL; s = s->next)
3715 {
3716 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
3717 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
3718 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3719 {
3720 arelent *p;
3721 long count, i;
3722
3723 if (! (*slurp_relocs) (abfd, s, syms, true))
3724 return -1;
3725 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
3726 p = s->relocation;
3727 for (i = 0; i < count; i++)
3728 *storage++ = p++;
3729 ret += count;
3730 }
3731 }
3732
3733 *storage = NULL;
3734
3735 return ret;
3736}
a66a61a0
ILT
3737\f
3738/* Read in the version information. */
3739
3740boolean
3741_bfd_elf_slurp_version_tables (abfd)
3742 bfd *abfd;
3743{
3744 bfd_byte *contents = NULL;
3745
3746 if (elf_dynverdef (abfd) != 0)
3747 {
3748 Elf_Internal_Shdr *hdr;
3749 Elf_External_Verdef *everdef;
3750 Elf_Internal_Verdef *iverdef;
3751 unsigned int i;
3752
3753 hdr = &elf_tdata (abfd)->dynverdef_hdr;
3754
3755 elf_tdata (abfd)->verdef =
3756 ((Elf_Internal_Verdef *)
3757 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
3758 if (elf_tdata (abfd)->verdef == NULL)
3759 goto error_return;
3760
3761 elf_tdata (abfd)->cverdefs = hdr->sh_info;
e35765a9 3762
a66a61a0
ILT
3763 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3764 if (contents == NULL)
3765 goto error_return;
3766 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3767 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
3768 goto error_return;
3769
3770 everdef = (Elf_External_Verdef *) contents;
3771 iverdef = elf_tdata (abfd)->verdef;
3772 for (i = 0; i < hdr->sh_info; i++, iverdef++)
3773 {
3774 Elf_External_Verdaux *everdaux;
3775 Elf_Internal_Verdaux *iverdaux;
3776 unsigned int j;
3777
3778 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
3779
3780 iverdef->vd_bfd = abfd;
3781
3782 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
3783 bfd_alloc (abfd,
3784 (iverdef->vd_cnt
3785 * sizeof (Elf_Internal_Verdaux))));
3786 if (iverdef->vd_auxptr == NULL)
3787 goto error_return;
3788
3789 everdaux = ((Elf_External_Verdaux *)
3790 ((bfd_byte *) everdef + iverdef->vd_aux));
3791 iverdaux = iverdef->vd_auxptr;
3792 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
3793 {
3794 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
3795
3796 iverdaux->vda_nodename =
3797 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3798 iverdaux->vda_name);
3799 if (iverdaux->vda_nodename == NULL)
3800 goto error_return;
3801
3802 if (j + 1 < iverdef->vd_cnt)
3803 iverdaux->vda_nextptr = iverdaux + 1;
3804 else
3805 iverdaux->vda_nextptr = NULL;
3806
3807 everdaux = ((Elf_External_Verdaux *)
3808 ((bfd_byte *) everdaux + iverdaux->vda_next));
3809 }
3810
3811 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
3812
3813 if (i + 1 < hdr->sh_info)
3814 iverdef->vd_nextdef = iverdef + 1;
3815 else
3816 iverdef->vd_nextdef = NULL;
3817
3818 everdef = ((Elf_External_Verdef *)
3819 ((bfd_byte *) everdef + iverdef->vd_next));
3820 }
3821
3822 free (contents);
3823 contents = NULL;
3824 }
3825
3826 if (elf_dynverref (abfd) != 0)
3827 {
3828 Elf_Internal_Shdr *hdr;
3829 Elf_External_Verneed *everneed;
3830 Elf_Internal_Verneed *iverneed;
3831 unsigned int i;
3832
3833 hdr = &elf_tdata (abfd)->dynverref_hdr;
3834
3835 elf_tdata (abfd)->verref =
3836 ((Elf_Internal_Verneed *)
3837 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
3838 if (elf_tdata (abfd)->verref == NULL)
3839 goto error_return;
3840
3841 elf_tdata (abfd)->cverrefs = hdr->sh_info;
3842
3843 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3844 if (contents == NULL)
3845 goto error_return;
3846 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3847 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
3848 goto error_return;
3849
3850 everneed = (Elf_External_Verneed *) contents;
3851 iverneed = elf_tdata (abfd)->verref;
3852 for (i = 0; i < hdr->sh_info; i++, iverneed++)
3853 {
3854 Elf_External_Vernaux *evernaux;
3855 Elf_Internal_Vernaux *ivernaux;
3856 unsigned int j;
3857
3858 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
3859
3860 iverneed->vn_bfd = abfd;
3861
3862 iverneed->vn_filename =
3863 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3864 iverneed->vn_file);
3865 if (iverneed->vn_filename == NULL)
3866 goto error_return;
3867
3868 iverneed->vn_auxptr =
3869 ((Elf_Internal_Vernaux *)
3870 bfd_alloc (abfd,
3871 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
3872
3873 evernaux = ((Elf_External_Vernaux *)
3874 ((bfd_byte *) everneed + iverneed->vn_aux));
3875 ivernaux = iverneed->vn_auxptr;
3876 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
3877 {
3878 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
3879
3880 ivernaux->vna_nodename =
3881 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3882 ivernaux->vna_name);
3883 if (ivernaux->vna_nodename == NULL)
3884 goto error_return;
3885
3886 if (j + 1 < iverneed->vn_cnt)
3887 ivernaux->vna_nextptr = ivernaux + 1;
3888 else
3889 ivernaux->vna_nextptr = NULL;
3890
3891 evernaux = ((Elf_External_Vernaux *)
3892 ((bfd_byte *) evernaux + ivernaux->vna_next));
3893 }
3894
3895 if (i + 1 < hdr->sh_info)
3896 iverneed->vn_nextref = iverneed + 1;
3897 else
3898 iverneed->vn_nextref = NULL;
3899
3900 everneed = ((Elf_External_Verneed *)
3901 ((bfd_byte *) everneed + iverneed->vn_next));
3902 }
3903
3904 free (contents);
3905 contents = NULL;
3906 }
3907
3908 return true;
3909
3910 error_return:
3911 if (contents == NULL)
3912 free (contents);
3913 return false;
3914}
3915\f
ede4eed4
KR
3916asymbol *
3917_bfd_elf_make_empty_symbol (abfd)
3918 bfd *abfd;
3919{
3920 elf_symbol_type *newsym;
3921
3922 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3923 if (!newsym)
a9713b91 3924 return NULL;
ede4eed4
KR
3925 else
3926 {
3927 newsym->symbol.the_bfd = abfd;
3928 return &newsym->symbol;
3929 }
3930}
3931
3932void
3933_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
3934 bfd *ignore_abfd;
3935 asymbol *symbol;
3936 symbol_info *ret;
3937{
3938 bfd_symbol_info (symbol, ret);
3939}
3940
d6bfcdb5
ILT
3941/* Return whether a symbol name implies a local symbol. Most targets
3942 use this function for the is_local_label_name entry point, but some
3943 override it. */
a66a61a0
ILT
3944
3945boolean
3946_bfd_elf_is_local_label_name (abfd, name)
3947 bfd *abfd;
3948 const char *name;
3949{
d6bfcdb5
ILT
3950 /* Normal local symbols start with ``.L''. */
3951 if (name[0] == '.' && name[1] == 'L')
3952 return true;
3953
3954 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
3955 DWARF debugging symbols starting with ``..''. */
3956 if (name[0] == '.' && name[1] == '.')
3957 return true;
3958
3959 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
3960 emitting DWARF debugging output. I suspect this is actually a
3961 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
3962 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
3963 underscore to be emitted on some ELF targets). For ease of use,
3964 we treat such symbols as local. */
3965 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
3966 return true;
3967
3968 return false;
a66a61a0
ILT
3969}
3970
ede4eed4
KR
3971alent *
3972_bfd_elf_get_lineno (ignore_abfd, symbol)
3973 bfd *ignore_abfd;
3974 asymbol *symbol;
3975{
8cd2f4fe 3976 abort ();
ede4eed4
KR
3977 return NULL;
3978}
3979
3980boolean
3981_bfd_elf_set_arch_mach (abfd, arch, machine)
3982 bfd *abfd;
3983 enum bfd_architecture arch;
3984 unsigned long machine;
3985{
3986 /* If this isn't the right architecture for this backend, and this
3987 isn't the generic backend, fail. */
3988 if (arch != get_elf_backend_data (abfd)->arch
3989 && arch != bfd_arch_unknown
3990 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3991 return false;
3992
3993 return bfd_default_set_arch_mach (abfd, arch, machine);
3994}
3995
6f904fce
ILT
3996/* Find the nearest line to a particular section and offset, for error
3997 reporting. */
3998
ede4eed4
KR
3999boolean
4000_bfd_elf_find_nearest_line (abfd,
6f904fce
ILT
4001 section,
4002 symbols,
4003 offset,
4004 filename_ptr,
4005 functionname_ptr,
4006 line_ptr)
ede4eed4
KR
4007 bfd *abfd;
4008 asection *section;
4009 asymbol **symbols;
4010 bfd_vma offset;
4011 CONST char **filename_ptr;
4012 CONST char **functionname_ptr;
4013 unsigned int *line_ptr;
4014{
86aac8ea 4015 boolean found;
6f904fce
ILT
4016 const char *filename;
4017 asymbol *func;
86aac8ea 4018 bfd_vma low_func;
6f904fce
ILT
4019 asymbol **p;
4020
eae43516
GRK
4021 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4022 filename_ptr, functionname_ptr,
4023 line_ptr))
4024 return true;
4025
86aac8ea
ILT
4026 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4027 &found, filename_ptr,
4028 functionname_ptr, line_ptr,
4029 &elf_tdata (abfd)->line_info))
4030 return false;
4031 if (found)
4032 return true;
4033
6f904fce
ILT
4034 if (symbols == NULL)
4035 return false;
4036
4037 filename = NULL;
4038 func = NULL;
86aac8ea 4039 low_func = 0;
6f904fce
ILT
4040
4041 for (p = symbols; *p != NULL; p++)
4042 {
4043 elf_symbol_type *q;
4044
4045 q = (elf_symbol_type *) *p;
4046
4047 if (bfd_get_section (&q->symbol) != section)
4048 continue;
4049
4050 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4051 {
4052 default:
4053 break;
4054 case STT_FILE:
4055 filename = bfd_asymbol_name (&q->symbol);
4056 break;
4057 case STT_FUNC:
86aac8ea
ILT
4058 if (q->symbol.section == section
4059 && q->symbol.value >= low_func
4060 && q->symbol.value <= offset)
4061 {
4062 func = (asymbol *) q;
4063 low_func = q->symbol.value;
4064 }
6f904fce
ILT
4065 break;
4066 }
4067 }
4068
4069 if (func == NULL)
4070 return false;
4071
4072 *filename_ptr = filename;
4073 *functionname_ptr = bfd_asymbol_name (func);
4074 *line_ptr = 0;
4075 return true;
ede4eed4
KR
4076}
4077
4078int
4079_bfd_elf_sizeof_headers (abfd, reloc)
4080 bfd *abfd;
4081 boolean reloc;
4082{
4083 int ret;
4084
4085 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4086 if (! reloc)
fd0198f0 4087 ret += get_program_header_size (abfd);
ede4eed4
KR
4088 return ret;
4089}
4090
4091boolean
4092_bfd_elf_set_section_contents (abfd, section, location, offset, count)
4093 bfd *abfd;
4094 sec_ptr section;
4095 PTR location;
4096 file_ptr offset;
4097 bfd_size_type count;
4098{
4099 Elf_Internal_Shdr *hdr;
4100
4101 if (! abfd->output_has_begun
4102 && ! _bfd_elf_compute_section_file_positions (abfd,
4103 (struct bfd_link_info *) NULL))
4104 return false;
4105
4106 hdr = &elf_section_data (section)->this_hdr;
4107
4108 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4109 return false;
4110 if (bfd_write (location, 1, count, abfd) != count)
4111 return false;
4112
4113 return true;
4114}
4115
4116void
4117_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4118 bfd *abfd;
4119 arelent *cache_ptr;
4120 Elf_Internal_Rela *dst;
4121{
8cd2f4fe 4122 abort ();
ede4eed4
KR
4123}
4124
4125#if 0
4126void
4127_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4128 bfd *abfd;
4129 arelent *cache_ptr;
4130 Elf_Internal_Rel *dst;
4131{
8cd2f4fe 4132 abort ();
ede4eed4
KR
4133}
4134#endif
7fc6a16a
ILT
4135
4136/* Try to convert a non-ELF reloc into an ELF one. */
4137
4138boolean
4139_bfd_elf_validate_reloc (abfd, areloc)
4140 bfd *abfd;
4141 arelent *areloc;
4142{
4143 /* Check whether we really have an ELF howto. */
4144
4145 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4146 {
4147 bfd_reloc_code_real_type code;
4148 reloc_howto_type *howto;
4149
4150 /* Alien reloc: Try to determine its type to replace it with an
4151 equivalent ELF reloc. */
4152
4153 if (areloc->howto->pc_relative)
4154 {
4155 switch (areloc->howto->bitsize)
4156 {
4157 case 8:
4158 code = BFD_RELOC_8_PCREL;
4159 break;
4160 case 12:
4161 code = BFD_RELOC_12_PCREL;
4162 break;
4163 case 16:
4164 code = BFD_RELOC_16_PCREL;
4165 break;
4166 case 24:
4167 code = BFD_RELOC_24_PCREL;
4168 break;
4169 case 32:
4170 code = BFD_RELOC_32_PCREL;
4171 break;
4172 case 64:
4173 code = BFD_RELOC_64_PCREL;
4174 break;
4175 default:
4176 goto fail;
4177 }
4178
4179 howto = bfd_reloc_type_lookup (abfd, code);
4180
4181 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4182 {
4183 if (howto->pcrel_offset)
4184 areloc->addend += areloc->address;
4185 else
4186 areloc->addend -= areloc->address; /* addend is unsigned!! */
4187 }
4188 }
4189 else
4190 {
4191 switch (areloc->howto->bitsize)
4192 {
4193 case 8:
4194 code = BFD_RELOC_8;
4195 break;
4196 case 14:
4197 code = BFD_RELOC_14;
4198 break;
4199 case 16:
4200 code = BFD_RELOC_16;
4201 break;
4202 case 26:
4203 code = BFD_RELOC_26;
4204 break;
4205 case 32:
4206 code = BFD_RELOC_32;
4207 break;
4208 case 64:
4209 code = BFD_RELOC_64;
4210 break;
4211 default:
4212 goto fail;
4213 }
4214
4215 howto = bfd_reloc_type_lookup (abfd, code);
4216 }
4217
4218 if (howto)
4219 areloc->howto = howto;
4220 else
4221 goto fail;
4222 }
4223
4224 return true;
4225
4226 fail:
4227 (*_bfd_error_handler)
4228 ("%s: unsupported relocation type %s",
4229 bfd_get_filename (abfd), areloc->howto->name);
4230 bfd_set_error (bfd_error_bad_value);
4231 return false;
4232}
16ce6205
RH
4233
4234boolean
4235_bfd_elf_close_and_cleanup (abfd)
4236 bfd *abfd;
4237{
e6e3d4bd
ILT
4238 if (bfd_get_format (abfd) == bfd_object)
4239 {
4240 if (elf_shstrtab (abfd) != NULL)
4241 _bfd_stringtab_free (elf_shstrtab (abfd));
4242 }
4243
16ce6205
RH
4244 return _bfd_generic_close_and_cleanup (abfd);
4245}
This page took 0.405803 seconds and 4 git commands to generate.