source: trunk/src/binutils/bfd/elf.c@ 524

Last change on this file since 524 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 160.3 KB
Line 
1/* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/*
22
23SECTION
24 ELF backends
25
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
29
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet.
33 */
34
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
37#include "bfd.h"
38#include "sysdep.h"
39#include "bfdlink.h"
40#include "libbfd.h"
41#define ARCH_SIZE 0
42#include "elf-bfd.h"
43
44static INLINE struct elf_segment_map *make_mapping
45 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
46static boolean map_sections_to_segments PARAMS ((bfd *));
47static int elf_sort_sections PARAMS ((const PTR, const PTR));
48static boolean assign_file_positions_for_segments PARAMS ((bfd *));
49static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
50static boolean prep_headers PARAMS ((bfd *));
51static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
52static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
53static char *elf_read PARAMS ((bfd *, long, unsigned int));
54static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
55static boolean assign_section_numbers PARAMS ((bfd *));
56static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
57static boolean elf_map_symbols PARAMS ((bfd *));
58static bfd_size_type get_program_header_size PARAMS ((bfd *));
59static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
60static boolean elf_find_function PARAMS ((bfd *, asection *,
61 asymbol **,
62 bfd_vma, const char **,
63 const char **));
64
65/* Swap version information in and out. The version information is
66 currently size independent. If that ever changes, this code will
67 need to move into elfcode.h. */
68
69/* Swap in a Verdef structure. */
70
71void
72_bfd_elf_swap_verdef_in (abfd, src, dst)
73 bfd *abfd;
74 const Elf_External_Verdef *src;
75 Elf_Internal_Verdef *dst;
76{
77 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
78 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
79 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
80 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
81 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
82 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
83 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
84}
85
86/* Swap out a Verdef structure. */
87
88void
89_bfd_elf_swap_verdef_out (abfd, src, dst)
90 bfd *abfd;
91 const Elf_Internal_Verdef *src;
92 Elf_External_Verdef *dst;
93{
94 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
95 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
96 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
97 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
98 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
99 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
100 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
101}
102
103/* Swap in a Verdaux structure. */
104
105void
106_bfd_elf_swap_verdaux_in (abfd, src, dst)
107 bfd *abfd;
108 const Elf_External_Verdaux *src;
109 Elf_Internal_Verdaux *dst;
110{
111 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
112 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
113}
114
115/* Swap out a Verdaux structure. */
116
117void
118_bfd_elf_swap_verdaux_out (abfd, src, dst)
119 bfd *abfd;
120 const Elf_Internal_Verdaux *src;
121 Elf_External_Verdaux *dst;
122{
123 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
124 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
125}
126
127/* Swap in a Verneed structure. */
128
129void
130_bfd_elf_swap_verneed_in (abfd, src, dst)
131 bfd *abfd;
132 const Elf_External_Verneed *src;
133 Elf_Internal_Verneed *dst;
134{
135 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
136 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
137 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
138 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
139 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
140}
141
142/* Swap out a Verneed structure. */
143
144void
145_bfd_elf_swap_verneed_out (abfd, src, dst)
146 bfd *abfd;
147 const Elf_Internal_Verneed *src;
148 Elf_External_Verneed *dst;
149{
150 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
151 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
152 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
153 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
154 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
155}
156
157/* Swap in a Vernaux structure. */
158
159void
160_bfd_elf_swap_vernaux_in (abfd, src, dst)
161 bfd *abfd;
162 const Elf_External_Vernaux *src;
163 Elf_Internal_Vernaux *dst;
164{
165 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
166 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
167 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
168 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
169 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
170}
171
172/* Swap out a Vernaux structure. */
173
174void
175_bfd_elf_swap_vernaux_out (abfd, src, dst)
176 bfd *abfd;
177 const Elf_Internal_Vernaux *src;
178 Elf_External_Vernaux *dst;
179{
180 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
181 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
182 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
183 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
184 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
185}
186
187/* Swap in a Versym structure. */
188
189void
190_bfd_elf_swap_versym_in (abfd, src, dst)
191 bfd *abfd;
192 const Elf_External_Versym *src;
193 Elf_Internal_Versym *dst;
194{
195 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
196}
197
198/* Swap out a Versym structure. */
199
200void
201_bfd_elf_swap_versym_out (abfd, src, dst)
202 bfd *abfd;
203 const Elf_Internal_Versym *src;
204 Elf_External_Versym *dst;
205{
206 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
207}
208
209/* Standard ELF hash function. Do not change this function; you will
210 cause invalid hash tables to be generated. */
211
212unsigned long
213bfd_elf_hash (namearg)
214 const char *namearg;
215{
216 const unsigned char *name = (const unsigned char *) namearg;
217 unsigned long h = 0;
218 unsigned long g;
219 int ch;
220
221 while ((ch = *name++) != '\0')
222 {
223 h = (h << 4) + ch;
224 if ((g = (h & 0xf0000000)) != 0)
225 {
226 h ^= g >> 24;
227 /* The ELF ABI says `h &= ~g', but this is equivalent in
228 this case and on some machines one insn instead of two. */
229 h ^= g;
230 }
231 }
232 return h;
233}
234
235/* Read a specified number of bytes at a specified offset in an ELF
236 file, into a newly allocated buffer, and return a pointer to the
237 buffer. */
238
239static char *
240elf_read (abfd, offset, size)
241 bfd *abfd;
242 long offset;
243 unsigned int size;
244{
245 char *buf;
246
247 if ((buf = bfd_alloc (abfd, size)) == NULL)
248 return NULL;
249 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
250 return NULL;
251 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
252 {
253 if (bfd_get_error () != bfd_error_system_call)
254 bfd_set_error (bfd_error_file_truncated);
255 return NULL;
256 }
257 return buf;
258}
259
260boolean
261bfd_elf_mkobject (abfd)
262 bfd *abfd;
263{
264 /* This just does initialization. */
265 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
266 elf_tdata (abfd) = (struct elf_obj_tdata *)
267 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
268 if (elf_tdata (abfd) == 0)
269 return false;
270 /* Since everything is done at close time, do we need any
271 initialization? */
272
273 return true;
274}
275
276boolean
277bfd_elf_mkcorefile (abfd)
278 bfd *abfd;
279{
280 /* I think this can be done just like an object file. */
281 return bfd_elf_mkobject (abfd);
282}
283
284char *
285bfd_elf_get_str_section (abfd, shindex)
286 bfd *abfd;
287 unsigned int shindex;
288{
289 Elf_Internal_Shdr **i_shdrp;
290 char *shstrtab = NULL;
291 unsigned int offset;
292 unsigned int shstrtabsize;
293
294 i_shdrp = elf_elfsections (abfd);
295 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
296 return 0;
297
298 shstrtab = (char *) i_shdrp[shindex]->contents;
299 if (shstrtab == NULL)
300 {
301 /* No cached one, attempt to read, and cache what we read. */
302 offset = i_shdrp[shindex]->sh_offset;
303 shstrtabsize = i_shdrp[shindex]->sh_size;
304 shstrtab = elf_read (abfd, offset, shstrtabsize);
305 i_shdrp[shindex]->contents = (PTR) shstrtab;
306 }
307 return shstrtab;
308}
309
310char *
311bfd_elf_string_from_elf_section (abfd, shindex, strindex)
312 bfd *abfd;
313 unsigned int shindex;
314 unsigned int strindex;
315{
316 Elf_Internal_Shdr *hdr;
317
318 if (strindex == 0)
319 return "";
320
321 hdr = elf_elfsections (abfd)[shindex];
322
323 if (hdr->contents == NULL
324 && bfd_elf_get_str_section (abfd, shindex) == NULL)
325 return NULL;
326
327 if (strindex >= hdr->sh_size)
328 {
329 (*_bfd_error_handler)
330 (_("%s: invalid string offset %u >= %lu for section `%s'"),
331 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
332 ((shindex == elf_elfheader(abfd)->e_shstrndx
333 && strindex == hdr->sh_name)
334 ? ".shstrtab"
335 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
336 return "";
337 }
338
339 return ((char *) hdr->contents) + strindex;
340}
341
342/* Make a BFD section from an ELF section. We store a pointer to the
343 BFD section in the bfd_section field of the header. */
344
345boolean
346_bfd_elf_make_section_from_shdr (abfd, hdr, name)
347 bfd *abfd;
348 Elf_Internal_Shdr *hdr;
349 const char *name;
350{
351 asection *newsect;
352 flagword flags;
353 struct elf_backend_data *bed;
354
355 if (hdr->bfd_section != NULL)
356 {
357 BFD_ASSERT (strcmp (name,
358 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
359 return true;
360 }
361
362 newsect = bfd_make_section_anyway (abfd, name);
363 if (newsect == NULL)
364 return false;
365
366 newsect->filepos = hdr->sh_offset;
367
368 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
369 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
370 || ! bfd_set_section_alignment (abfd, newsect,
371 bfd_log2 (hdr->sh_addralign)))
372 return false;
373
374 flags = SEC_NO_FLAGS;
375 if (hdr->sh_type != SHT_NOBITS)
376 flags |= SEC_HAS_CONTENTS;
377 if ((hdr->sh_flags & SHF_ALLOC) != 0)
378 {
379 flags |= SEC_ALLOC;
380 if (hdr->sh_type != SHT_NOBITS)
381 flags |= SEC_LOAD;
382 }
383 if ((hdr->sh_flags & SHF_WRITE) == 0)
384 flags |= SEC_READONLY;
385 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
386 flags |= SEC_CODE;
387 else if ((flags & SEC_LOAD) != 0)
388 flags |= SEC_DATA;
389
390 /* The debugging sections appear to be recognized only by name, not
391 any sort of flag. */
392 {
393 static const char *debug_sec_names [] =
394 {
395 ".debug",
396 ".gnu.linkonce.wi.",
397 ".line",
398 ".stab"
399 };
400 int i;
401
402 for (i = sizeof (debug_sec_names) / sizeof (debug_sec_names[0]); i--;)
403 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
404 break;
405
406 if (i >= 0)
407 flags |= SEC_DEBUGGING;
408 }
409
410 /* As a GNU extension, if the name begins with .gnu.linkonce, we
411 only link a single copy of the section. This is used to support
412 g++. g++ will emit each template expansion in its own section.
413 The symbols will be defined as weak, so that multiple definitions
414 are permitted. The GNU linker extension is to actually discard
415 all but one of the sections. */
416 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
417 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
418
419 bed = get_elf_backend_data (abfd);
420 if (bed->elf_backend_section_flags)
421 if (! bed->elf_backend_section_flags (&flags, hdr))
422 return false;
423
424 if (! bfd_set_section_flags (abfd, newsect, flags))
425 return false;
426
427 if ((flags & SEC_ALLOC) != 0)
428 {
429 Elf_Internal_Phdr *phdr;
430 unsigned int i;
431
432 /* Look through the phdrs to see if we need to adjust the lma.
433 If all the p_paddr fields are zero, we ignore them, since
434 some ELF linkers produce such output. */
435 phdr = elf_tdata (abfd)->phdr;
436 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
437 {
438 if (phdr->p_paddr != 0)
439 break;
440 }
441 if (i < elf_elfheader (abfd)->e_phnum)
442 {
443 phdr = elf_tdata (abfd)->phdr;
444 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
445 {
446 if (phdr->p_type == PT_LOAD
447 && phdr->p_vaddr != phdr->p_paddr
448 && phdr->p_vaddr <= hdr->sh_addr
449 && (phdr->p_vaddr + phdr->p_memsz
450 >= hdr->sh_addr + hdr->sh_size)
451 && ((flags & SEC_LOAD) == 0
452 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
453 && (phdr->p_offset + phdr->p_filesz
454 >= hdr->sh_offset + hdr->sh_size))))
455 {
456 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
457 break;
458 }
459 }
460 }
461 }
462
463 hdr->bfd_section = newsect;
464 elf_section_data (newsect)->this_hdr = *hdr;
465
466 return true;
467}
468
469/*
470INTERNAL_FUNCTION
471 bfd_elf_find_section
472
473SYNOPSIS
474 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
475
476DESCRIPTION
477 Helper functions for GDB to locate the string tables.
478 Since BFD hides string tables from callers, GDB needs to use an
479 internal hook to find them. Sun's .stabstr, in particular,
480 isn't even pointed to by the .stab section, so ordinary
481 mechanisms wouldn't work to find it, even if we had some.
482*/
483
484struct elf_internal_shdr *
485bfd_elf_find_section (abfd, name)
486 bfd *abfd;
487 char *name;
488{
489 Elf_Internal_Shdr **i_shdrp;
490 char *shstrtab;
491 unsigned int max;
492 unsigned int i;
493
494 i_shdrp = elf_elfsections (abfd);
495 if (i_shdrp != NULL)
496 {
497 shstrtab = bfd_elf_get_str_section
498 (abfd, elf_elfheader (abfd)->e_shstrndx);
499 if (shstrtab != NULL)
500 {
501 max = elf_elfheader (abfd)->e_shnum;
502 for (i = 1; i < max; i++)
503 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
504 return i_shdrp[i];
505 }
506 }
507 return 0;
508}
509
510const char *const bfd_elf_section_type_names[] = {
511 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
512 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
513 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
514};
515
516/* ELF relocs are against symbols. If we are producing relocateable
517 output, and the reloc is against an external symbol, and nothing
518 has given us any additional addend, the resulting reloc will also
519 be against the same symbol. In such a case, we don't want to
520 change anything about the way the reloc is handled, since it will
521 all be done at final link time. Rather than put special case code
522 into bfd_perform_relocation, all the reloc types use this howto
523 function. It just short circuits the reloc if producing
524 relocateable output against an external symbol. */
525
526bfd_reloc_status_type
527bfd_elf_generic_reloc (abfd,
528 reloc_entry,
529 symbol,
530 data,
531 input_section,
532 output_bfd,
533 error_message)
534 bfd *abfd ATTRIBUTE_UNUSED;
535 arelent *reloc_entry;
536 asymbol *symbol;
537 PTR data ATTRIBUTE_UNUSED;
538 asection *input_section;
539 bfd *output_bfd;
540 char **error_message ATTRIBUTE_UNUSED;
541{
542 if (output_bfd != (bfd *) NULL
543 && (symbol->flags & BSF_SECTION_SYM) == 0
544 && (! reloc_entry->howto->partial_inplace
545 || reloc_entry->addend == 0))
546 {
547 reloc_entry->address += input_section->output_offset;
548 return bfd_reloc_ok;
549 }
550
551 return bfd_reloc_continue;
552}
553
554
555/* Print out the program headers. */
556
557boolean
558_bfd_elf_print_private_bfd_data (abfd, farg)
559 bfd *abfd;
560 PTR farg;
561{
562 FILE *f = (FILE *) farg;
563 Elf_Internal_Phdr *p;
564 asection *s;
565 bfd_byte *dynbuf = NULL;
566
567 p = elf_tdata (abfd)->phdr;
568 if (p != NULL)
569 {
570 unsigned int i, c;
571
572 fprintf (f, _("\nProgram Header:\n"));
573 c = elf_elfheader (abfd)->e_phnum;
574 for (i = 0; i < c; i++, p++)
575 {
576 const char *s;
577 char buf[20];
578
579 switch (p->p_type)
580 {
581 case PT_NULL: s = "NULL"; break;
582 case PT_LOAD: s = "LOAD"; break;
583 case PT_DYNAMIC: s = "DYNAMIC"; break;
584 case PT_INTERP: s = "INTERP"; break;
585 case PT_NOTE: s = "NOTE"; break;
586 case PT_SHLIB: s = "SHLIB"; break;
587 case PT_PHDR: s = "PHDR"; break;
588 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
589 }
590 fprintf (f, "%8s off 0x", s);
591 fprintf_vma (f, p->p_offset);
592 fprintf (f, " vaddr 0x");
593 fprintf_vma (f, p->p_vaddr);
594 fprintf (f, " paddr 0x");
595 fprintf_vma (f, p->p_paddr);
596 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
597 fprintf (f, " filesz 0x");
598 fprintf_vma (f, p->p_filesz);
599 fprintf (f, " memsz 0x");
600 fprintf_vma (f, p->p_memsz);
601 fprintf (f, " flags %c%c%c",
602 (p->p_flags & PF_R) != 0 ? 'r' : '-',
603 (p->p_flags & PF_W) != 0 ? 'w' : '-',
604 (p->p_flags & PF_X) != 0 ? 'x' : '-');
605 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
606 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
607 fprintf (f, "\n");
608 }
609 }
610
611 s = bfd_get_section_by_name (abfd, ".dynamic");
612 if (s != NULL)
613 {
614 int elfsec;
615 unsigned long link;
616 bfd_byte *extdyn, *extdynend;
617 size_t extdynsize;
618 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
619
620 fprintf (f, _("\nDynamic Section:\n"));
621
622 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
623 if (dynbuf == NULL)
624 goto error_return;
625 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
626 s->_raw_size))
627 goto error_return;
628
629 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
630 if (elfsec == -1)
631 goto error_return;
632 link = elf_elfsections (abfd)[elfsec]->sh_link;
633
634 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
635 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
636
637 extdyn = dynbuf;
638 extdynend = extdyn + s->_raw_size;
639 for (; extdyn < extdynend; extdyn += extdynsize)
640 {
641 Elf_Internal_Dyn dyn;
642 const char *name;
643 char ab[20];
644 boolean stringp;
645
646 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
647
648 if (dyn.d_tag == DT_NULL)
649 break;
650
651 stringp = false;
652 switch (dyn.d_tag)
653 {
654 default:
655 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
656 name = ab;
657 break;
658
659 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
660 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
661 case DT_PLTGOT: name = "PLTGOT"; break;
662 case DT_HASH: name = "HASH"; break;
663 case DT_STRTAB: name = "STRTAB"; break;
664 case DT_SYMTAB: name = "SYMTAB"; break;
665 case DT_RELA: name = "RELA"; break;
666 case DT_RELASZ: name = "RELASZ"; break;
667 case DT_RELAENT: name = "RELAENT"; break;
668 case DT_STRSZ: name = "STRSZ"; break;
669 case DT_SYMENT: name = "SYMENT"; break;
670 case DT_INIT: name = "INIT"; break;
671 case DT_FINI: name = "FINI"; break;
672 case DT_SONAME: name = "SONAME"; stringp = true; break;
673 case DT_RPATH: name = "RPATH"; stringp = true; break;
674 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
675 case DT_REL: name = "REL"; break;
676 case DT_RELSZ: name = "RELSZ"; break;
677 case DT_RELENT: name = "RELENT"; break;
678 case DT_PLTREL: name = "PLTREL"; break;
679 case DT_DEBUG: name = "DEBUG"; break;
680 case DT_TEXTREL: name = "TEXTREL"; break;
681 case DT_JMPREL: name = "JMPREL"; break;
682 case DT_BIND_NOW: name = "BIND_NOW"; break;
683 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
684 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
685 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
686 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
687 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
688 case DT_FLAGS: name = "FLAGS"; break;
689 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
690 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
691 case DT_CHECKSUM: name = "CHECKSUM"; break;
692 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
693 case DT_MOVEENT: name = "MOVEENT"; break;
694 case DT_MOVESZ: name = "MOVESZ"; break;
695 case DT_FEATURE: name = "FEATURE"; break;
696 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
697 case DT_SYMINSZ: name = "SYMINSZ"; break;
698 case DT_SYMINENT: name = "SYMINENT"; break;
699 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
700 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
701 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
702 case DT_PLTPAD: name = "PLTPAD"; break;
703 case DT_MOVETAB: name = "MOVETAB"; break;
704 case DT_SYMINFO: name = "SYMINFO"; break;
705 case DT_RELACOUNT: name = "RELACOUNT"; break;
706 case DT_RELCOUNT: name = "RELCOUNT"; break;
707 case DT_FLAGS_1: name = "FLAGS_1"; break;
708 case DT_VERSYM: name = "VERSYM"; break;
709 case DT_VERDEF: name = "VERDEF"; break;
710 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
711 case DT_VERNEED: name = "VERNEED"; break;
712 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
713 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
714 case DT_USED: name = "USED"; break;
715 case DT_FILTER: name = "FILTER"; stringp = true; break;
716 }
717
718 fprintf (f, " %-11s ", name);
719 if (! stringp)
720 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
721 else
722 {
723 const char *string;
724
725 string = bfd_elf_string_from_elf_section (abfd, link,
726 dyn.d_un.d_val);
727 if (string == NULL)
728 goto error_return;
729 fprintf (f, "%s", string);
730 }
731 fprintf (f, "\n");
732 }
733
734 free (dynbuf);
735 dynbuf = NULL;
736 }
737
738 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
739 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
740 {
741 if (! _bfd_elf_slurp_version_tables (abfd))
742 return false;
743 }
744
745 if (elf_dynverdef (abfd) != 0)
746 {
747 Elf_Internal_Verdef *t;
748
749 fprintf (f, _("\nVersion definitions:\n"));
750 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
751 {
752 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
753 t->vd_flags, t->vd_hash, t->vd_nodename);
754 if (t->vd_auxptr->vda_nextptr != NULL)
755 {
756 Elf_Internal_Verdaux *a;
757
758 fprintf (f, "\t");
759 for (a = t->vd_auxptr->vda_nextptr;
760 a != NULL;
761 a = a->vda_nextptr)
762 fprintf (f, "%s ", a->vda_nodename);
763 fprintf (f, "\n");
764 }
765 }
766 }
767
768 if (elf_dynverref (abfd) != 0)
769 {
770 Elf_Internal_Verneed *t;
771
772 fprintf (f, _("\nVersion References:\n"));
773 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
774 {
775 Elf_Internal_Vernaux *a;
776
777 fprintf (f, _(" required from %s:\n"), t->vn_filename);
778 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
779 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
780 a->vna_flags, a->vna_other, a->vna_nodename);
781 }
782 }
783
784 return true;
785
786 error_return:
787 if (dynbuf != NULL)
788 free (dynbuf);
789 return false;
790}
791
792/* Display ELF-specific fields of a symbol. */
793
794void
795bfd_elf_print_symbol (abfd, filep, symbol, how)
796 bfd *abfd;
797 PTR filep;
798 asymbol *symbol;
799 bfd_print_symbol_type how;
800{
801 FILE *file = (FILE *) filep;
802 switch (how)
803 {
804 case bfd_print_symbol_name:
805 fprintf (file, "%s", symbol->name);
806 break;
807 case bfd_print_symbol_more:
808 fprintf (file, "elf ");
809 fprintf_vma (file, symbol->value);
810 fprintf (file, " %lx", (long) symbol->flags);
811 break;
812 case bfd_print_symbol_all:
813 {
814 const char *section_name;
815 const char *name = NULL;
816 struct elf_backend_data *bed;
817 unsigned char st_other;
818
819 section_name = symbol->section ? symbol->section->name : "(*none*)";
820
821 bed = get_elf_backend_data (abfd);
822 if (bed->elf_backend_print_symbol_all)
823 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
824
825 if (name == NULL)
826 {
827 name = symbol->name;
828 bfd_print_symbol_vandf ((PTR) file, symbol);
829 }
830
831 fprintf (file, " %s\t", section_name);
832 /* Print the "other" value for a symbol. For common symbols,
833 we've already printed the size; now print the alignment.
834 For other symbols, we have no specified alignment, and
835 we've printed the address; now print the size. */
836 fprintf_vma (file,
837 (bfd_is_com_section (symbol->section)
838 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
839 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
840
841 /* If we have version information, print it. */
842 if (elf_tdata (abfd)->dynversym_section != 0
843 && (elf_tdata (abfd)->dynverdef_section != 0
844 || elf_tdata (abfd)->dynverref_section != 0))
845 {
846 unsigned int vernum;
847 const char *version_string;
848
849 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
850
851 if (vernum == 0)
852 version_string = "";
853 else if (vernum == 1)
854 version_string = "Base";
855 else if (vernum <= elf_tdata (abfd)->cverdefs)
856 version_string =
857 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
858 else
859 {
860 Elf_Internal_Verneed *t;
861
862 version_string = "";
863 for (t = elf_tdata (abfd)->verref;
864 t != NULL;
865 t = t->vn_nextref)
866 {
867 Elf_Internal_Vernaux *a;
868
869 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
870 {
871 if (a->vna_other == vernum)
872 {
873 version_string = a->vna_nodename;
874 break;
875 }
876 }
877 }
878 }
879
880 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
881 fprintf (file, " %-11s", version_string);
882 else
883 {
884 int i;
885
886 fprintf (file, " (%s)", version_string);
887 for (i = 10 - strlen (version_string); i > 0; --i)
888 putc (' ', file);
889 }
890 }
891
892 /* If the st_other field is not zero, print it. */
893 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
894
895 switch (st_other)
896 {
897 case 0: break;
898 case STV_INTERNAL: fprintf (file, " .internal"); break;
899 case STV_HIDDEN: fprintf (file, " .hidden"); break;
900 case STV_PROTECTED: fprintf (file, " .protected"); break;
901 default:
902 /* Some other non-defined flags are also present, so print
903 everything hex. */
904 fprintf (file, " 0x%02x", (unsigned int) st_other);
905 }
906
907 fprintf (file, " %s", name);
908 }
909 break;
910 }
911}
912
913
914/* Create an entry in an ELF linker hash table. */
915
916struct bfd_hash_entry *
917_bfd_elf_link_hash_newfunc (entry, table, string)
918 struct bfd_hash_entry *entry;
919 struct bfd_hash_table *table;
920 const char *string;
921{
922 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
923
924 /* Allocate the structure if it has not already been allocated by a
925 subclass. */
926 if (ret == (struct elf_link_hash_entry *) NULL)
927 ret = ((struct elf_link_hash_entry *)
928 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
929 if (ret == (struct elf_link_hash_entry *) NULL)
930 return (struct bfd_hash_entry *) ret;
931
932 /* Call the allocation method of the superclass. */
933 ret = ((struct elf_link_hash_entry *)
934 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
935 table, string));
936 if (ret != (struct elf_link_hash_entry *) NULL)
937 {
938 /* Set local fields. */
939 ret->indx = -1;
940 ret->size = 0;
941 ret->dynindx = -1;
942 ret->dynstr_index = 0;
943 ret->weakdef = NULL;
944 ret->got.offset = (bfd_vma) -1;
945 ret->plt.offset = (bfd_vma) -1;
946 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
947 ret->verinfo.verdef = NULL;
948 ret->vtable_entries_used = NULL;
949 ret->vtable_entries_size = 0;
950 ret->vtable_parent = NULL;
951 ret->type = STT_NOTYPE;
952 ret->other = 0;
953 /* Assume that we have been called by a non-ELF symbol reader.
954 This flag is then reset by the code which reads an ELF input
955 file. This ensures that a symbol created by a non-ELF symbol
956 reader will have the flag set correctly. */
957 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
958 }
959
960 return (struct bfd_hash_entry *) ret;
961}
962
963/* Copy data from an indirect symbol to its direct symbol, hiding the
964 old indirect symbol. */
965
966void
967_bfd_elf_link_hash_copy_indirect (dir, ind)
968 struct elf_link_hash_entry *dir, *ind;
969{
970 /* Copy down any references that we may have already seen to the
971 symbol which just became indirect. */
972
973 dir->elf_link_hash_flags |=
974 (ind->elf_link_hash_flags
975 & (ELF_LINK_HASH_REF_DYNAMIC
976 | ELF_LINK_HASH_REF_REGULAR
977 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
978 | ELF_LINK_NON_GOT_REF));
979
980 /* Copy over the global and procedure linkage table offset entries.
981 These may have been already set up by a check_relocs routine. */
982 if (dir->got.offset == (bfd_vma) -1)
983 {
984 dir->got.offset = ind->got.offset;
985 ind->got.offset = (bfd_vma) -1;
986 }
987 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
988
989 if (dir->plt.offset == (bfd_vma) -1)
990 {
991 dir->plt.offset = ind->plt.offset;
992 ind->plt.offset = (bfd_vma) -1;
993 }
994 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
995
996 if (dir->dynindx == -1)
997 {
998 dir->dynindx = ind->dynindx;
999 dir->dynstr_index = ind->dynstr_index;
1000 ind->dynindx = -1;
1001 ind->dynstr_index = 0;
1002 }
1003 BFD_ASSERT (ind->dynindx == -1);
1004}
1005
1006void
1007_bfd_elf_link_hash_hide_symbol (info, h)
1008 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1009 struct elf_link_hash_entry *h;
1010{
1011 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1012 h->plt.offset = (bfd_vma) -1;
1013 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1014 h->dynindx = -1;
1015}
1016
1017/* Initialize an ELF linker hash table. */
1018
1019boolean
1020_bfd_elf_link_hash_table_init (table, abfd, newfunc)
1021 struct elf_link_hash_table *table;
1022 bfd *abfd;
1023 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1024 struct bfd_hash_table *,
1025 const char *));
1026{
1027 table->dynamic_sections_created = false;
1028 table->dynobj = NULL;
1029 /* The first dynamic symbol is a dummy. */
1030 table->dynsymcount = 1;
1031 table->dynstr = NULL;
1032 table->bucketcount = 0;
1033 table->needed = NULL;
1034 table->runpath = NULL;
1035 table->hgot = NULL;
1036 table->stab_info = NULL;
1037 table->dynlocal = NULL;
1038 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1039}
1040
1041/* Create an ELF linker hash table. */
1042
1043struct bfd_link_hash_table *
1044_bfd_elf_link_hash_table_create (abfd)
1045 bfd *abfd;
1046{
1047 struct elf_link_hash_table *ret;
1048
1049 ret = ((struct elf_link_hash_table *)
1050 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
1051 if (ret == (struct elf_link_hash_table *) NULL)
1052 return NULL;
1053
1054 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1055 {
1056 bfd_release (abfd, ret);
1057 return NULL;
1058 }
1059
1060 return &ret->root;
1061}
1062
1063/* This is a hook for the ELF emulation code in the generic linker to
1064 tell the backend linker what file name to use for the DT_NEEDED
1065 entry for a dynamic object. The generic linker passes name as an
1066 empty string to indicate that no DT_NEEDED entry should be made. */
1067
1068void
1069bfd_elf_set_dt_needed_name (abfd, name)
1070 bfd *abfd;
1071 const char *name;
1072{
1073 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1074 && bfd_get_format (abfd) == bfd_object)
1075 elf_dt_name (abfd) = name;
1076}
1077
1078void
1079bfd_elf_set_dt_needed_soname (abfd, name)
1080 bfd *abfd;
1081 const char *name;
1082{
1083 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1084 && bfd_get_format (abfd) == bfd_object)
1085 elf_dt_soname (abfd) = name;
1086}
1087
1088/* Get the list of DT_NEEDED entries for a link. This is a hook for
1089 the linker ELF emulation code. */
1090
1091struct bfd_link_needed_list *
1092bfd_elf_get_needed_list (abfd, info)
1093 bfd *abfd ATTRIBUTE_UNUSED;
1094 struct bfd_link_info *info;
1095{
1096 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1097 return NULL;
1098 return elf_hash_table (info)->needed;
1099}
1100
1101/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1102 hook for the linker ELF emulation code. */
1103
1104struct bfd_link_needed_list *
1105bfd_elf_get_runpath_list (abfd, info)
1106 bfd *abfd ATTRIBUTE_UNUSED;
1107 struct bfd_link_info *info;
1108{
1109 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1110 return NULL;
1111 return elf_hash_table (info)->runpath;
1112}
1113
1114/* Get the name actually used for a dynamic object for a link. This
1115 is the SONAME entry if there is one. Otherwise, it is the string
1116 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1117
1118const char *
1119bfd_elf_get_dt_soname (abfd)
1120 bfd *abfd;
1121{
1122 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1123 && bfd_get_format (abfd) == bfd_object)
1124 return elf_dt_name (abfd);
1125 return NULL;
1126}
1127
1128/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1129 the ELF linker emulation code. */
1130
1131boolean
1132bfd_elf_get_bfd_needed_list (abfd, pneeded)
1133 bfd *abfd;
1134 struct bfd_link_needed_list **pneeded;
1135{
1136 asection *s;
1137 bfd_byte *dynbuf = NULL;
1138 int elfsec;
1139 unsigned long link;
1140 bfd_byte *extdyn, *extdynend;
1141 size_t extdynsize;
1142 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1143
1144 *pneeded = NULL;
1145
1146 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1147 || bfd_get_format (abfd) != bfd_object)
1148 return true;
1149
1150 s = bfd_get_section_by_name (abfd, ".dynamic");
1151 if (s == NULL || s->_raw_size == 0)
1152 return true;
1153
1154 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1155 if (dynbuf == NULL)
1156 goto error_return;
1157
1158 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1159 s->_raw_size))
1160 goto error_return;
1161
1162 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1163 if (elfsec == -1)
1164 goto error_return;
1165
1166 link = elf_elfsections (abfd)[elfsec]->sh_link;
1167
1168 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1169 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1170
1171 extdyn = dynbuf;
1172 extdynend = extdyn + s->_raw_size;
1173 for (; extdyn < extdynend; extdyn += extdynsize)
1174 {
1175 Elf_Internal_Dyn dyn;
1176
1177 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1178
1179 if (dyn.d_tag == DT_NULL)
1180 break;
1181
1182 if (dyn.d_tag == DT_NEEDED)
1183 {
1184 const char *string;
1185 struct bfd_link_needed_list *l;
1186
1187 string = bfd_elf_string_from_elf_section (abfd, link,
1188 dyn.d_un.d_val);
1189 if (string == NULL)
1190 goto error_return;
1191
1192 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1193 if (l == NULL)
1194 goto error_return;
1195
1196 l->by = abfd;
1197 l->name = string;
1198 l->next = *pneeded;
1199 *pneeded = l;
1200 }
1201 }
1202
1203 free (dynbuf);
1204
1205 return true;
1206
1207 error_return:
1208 if (dynbuf != NULL)
1209 free (dynbuf);
1210 return false;
1211}
1212
1213
1214/* Allocate an ELF string table--force the first byte to be zero. */
1215
1216struct bfd_strtab_hash *
1217_bfd_elf_stringtab_init ()
1218{
1219 struct bfd_strtab_hash *ret;
1220
1221 ret = _bfd_stringtab_init ();
1222 if (ret != NULL)
1223 {
1224 bfd_size_type loc;
1225
1226 loc = _bfd_stringtab_add (ret, "", true, false);
1227 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1228 if (loc == (bfd_size_type) -1)
1229 {
1230 _bfd_stringtab_free (ret);
1231 ret = NULL;
1232 }
1233 }
1234 return ret;
1235}
1236
1237
1238/* ELF .o/exec file reading */
1239
1240/* Create a new bfd section from an ELF section header. */
1241
1242boolean
1243bfd_section_from_shdr (abfd, shindex)
1244 bfd *abfd;
1245 unsigned int shindex;
1246{
1247 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1248 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1249 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1250 char *name;
1251
1252 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1253
1254 switch (hdr->sh_type)
1255 {
1256 case SHT_NULL:
1257 /* Inactive section. Throw it away. */
1258 return true;
1259
1260 case SHT_PROGBITS: /* Normal section with contents. */
1261 case SHT_DYNAMIC: /* Dynamic linking information. */
1262 case SHT_NOBITS: /* .bss section. */
1263 case SHT_HASH: /* .hash section. */
1264 case SHT_NOTE: /* .note section. */
1265 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1266
1267 case SHT_SYMTAB: /* A symbol table */
1268 if (elf_onesymtab (abfd) == shindex)
1269 return true;
1270
1271 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1272 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1273 elf_onesymtab (abfd) = shindex;
1274 elf_tdata (abfd)->symtab_hdr = *hdr;
1275 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1276 abfd->flags |= HAS_SYMS;
1277
1278 /* Sometimes a shared object will map in the symbol table. If
1279 SHF_ALLOC is set, and this is a shared object, then we also
1280 treat this section as a BFD section. We can not base the
1281 decision purely on SHF_ALLOC, because that flag is sometimes
1282 set in a relocateable object file, which would confuse the
1283 linker. */
1284 if ((hdr->sh_flags & SHF_ALLOC) != 0
1285 && (abfd->flags & DYNAMIC) != 0
1286 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1287 return false;
1288
1289 return true;
1290
1291 case SHT_DYNSYM: /* A dynamic symbol table */
1292 if (elf_dynsymtab (abfd) == shindex)
1293 return true;
1294
1295 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1296 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1297 elf_dynsymtab (abfd) = shindex;
1298 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1299 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1300 abfd->flags |= HAS_SYMS;
1301
1302 /* Besides being a symbol table, we also treat this as a regular
1303 section, so that objcopy can handle it. */
1304 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1305
1306 case SHT_STRTAB: /* A string table */
1307 if (hdr->bfd_section != NULL)
1308 return true;
1309 if (ehdr->e_shstrndx == shindex)
1310 {
1311 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1312 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1313 return true;
1314 }
1315 {
1316 unsigned int i;
1317
1318 for (i = 1; i < ehdr->e_shnum; i++)
1319 {
1320 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1321 if (hdr2->sh_link == shindex)
1322 {
1323 if (! bfd_section_from_shdr (abfd, i))
1324 return false;
1325 if (elf_onesymtab (abfd) == i)
1326 {
1327 elf_tdata (abfd)->strtab_hdr = *hdr;
1328 elf_elfsections (abfd)[shindex] =
1329 &elf_tdata (abfd)->strtab_hdr;
1330 return true;
1331 }
1332 if (elf_dynsymtab (abfd) == i)
1333 {
1334 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1335 elf_elfsections (abfd)[shindex] = hdr =
1336 &elf_tdata (abfd)->dynstrtab_hdr;
1337 /* We also treat this as a regular section, so
1338 that objcopy can handle it. */
1339 break;
1340 }
1341#if 0 /* Not handling other string tables specially right now. */
1342 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1343 /* We have a strtab for some random other section. */
1344 newsect = (asection *) hdr2->bfd_section;
1345 if (!newsect)
1346 break;
1347 hdr->bfd_section = newsect;
1348 hdr2 = &elf_section_data (newsect)->str_hdr;
1349 *hdr2 = *hdr;
1350 elf_elfsections (abfd)[shindex] = hdr2;
1351#endif
1352 }
1353 }
1354 }
1355
1356 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1357
1358 case SHT_REL:
1359 case SHT_RELA:
1360 /* *These* do a lot of work -- but build no sections! */
1361 {
1362 asection *target_sect;
1363 Elf_Internal_Shdr *hdr2;
1364
1365 /* Check for a bogus link to avoid crashing. */
1366 if (hdr->sh_link >= ehdr->e_shnum)
1367 {
1368 ((*_bfd_error_handler)
1369 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1370 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1371 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1372 }
1373
1374 /* For some incomprehensible reason Oracle distributes
1375 libraries for Solaris in which some of the objects have
1376 bogus sh_link fields. It would be nice if we could just
1377 reject them, but, unfortunately, some people need to use
1378 them. We scan through the section headers; if we find only
1379 one suitable symbol table, we clobber the sh_link to point
1380 to it. I hope this doesn't break anything. */
1381 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1382 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1383 {
1384 int scan;
1385 int found;
1386
1387 found = 0;
1388 for (scan = 1; scan < ehdr->e_shnum; scan++)
1389 {
1390 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1391 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1392 {
1393 if (found != 0)
1394 {
1395 found = 0;
1396 break;
1397 }
1398 found = scan;
1399 }
1400 }
1401 if (found != 0)
1402 hdr->sh_link = found;
1403 }
1404
1405 /* Get the symbol table. */
1406 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1407 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1408 return false;
1409
1410 /* If this reloc section does not use the main symbol table we
1411 don't treat it as a reloc section. BFD can't adequately
1412 represent such a section, so at least for now, we don't
1413 try. We just present it as a normal section. We also
1414 can't use it as a reloc section if it points to the null
1415 section. */
1416 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1417 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1418
1419 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1420 return false;
1421 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1422 if (target_sect == NULL)
1423 return false;
1424
1425 if ((target_sect->flags & SEC_RELOC) == 0
1426 || target_sect->reloc_count == 0)
1427 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1428 else
1429 {
1430 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1431 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1432 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1433 }
1434 *hdr2 = *hdr;
1435 elf_elfsections (abfd)[shindex] = hdr2;
1436 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1437 target_sect->flags |= SEC_RELOC;
1438 target_sect->relocation = NULL;
1439 target_sect->rel_filepos = hdr->sh_offset;
1440 /* In the section to which the relocations apply, mark whether
1441 its relocations are of the REL or RELA variety. */
1442 if (hdr->sh_size != 0)
1443 elf_section_data (target_sect)->use_rela_p
1444 = (hdr->sh_type == SHT_RELA);
1445 abfd->flags |= HAS_RELOC;
1446 return true;
1447 }
1448 break;
1449
1450 case SHT_GNU_verdef:
1451 elf_dynverdef (abfd) = shindex;
1452 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1453 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1454 break;
1455
1456 case SHT_GNU_versym:
1457 elf_dynversym (abfd) = shindex;
1458 elf_tdata (abfd)->dynversym_hdr = *hdr;
1459 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1460 break;
1461
1462 case SHT_GNU_verneed:
1463 elf_dynverref (abfd) = shindex;
1464 elf_tdata (abfd)->dynverref_hdr = *hdr;
1465 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1466 break;
1467
1468 case SHT_SHLIB:
1469 return true;
1470
1471 default:
1472 /* Check for any processor-specific section types. */
1473 {
1474 if (bed->elf_backend_section_from_shdr)
1475 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1476 }
1477 break;
1478 }
1479
1480 return true;
1481}
1482
1483/* Given an ELF section number, retrieve the corresponding BFD
1484 section. */
1485
1486asection *
1487bfd_section_from_elf_index (abfd, index)
1488 bfd *abfd;
1489 unsigned int index;
1490{
1491 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1492 if (index >= elf_elfheader (abfd)->e_shnum)
1493 return NULL;
1494 return elf_elfsections (abfd)[index]->bfd_section;
1495}
1496
1497boolean
1498_bfd_elf_new_section_hook (abfd, sec)
1499 bfd *abfd;
1500 asection *sec;
1501{
1502 struct bfd_elf_section_data *sdata;
1503
1504 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1505 if (!sdata)
1506 return false;
1507 sec->used_by_bfd = (PTR) sdata;
1508
1509 /* Indicate whether or not this section should use RELA relocations. */
1510 sdata->use_rela_p
1511 = get_elf_backend_data (abfd)->default_use_rela_p;
1512
1513 return true;
1514}
1515
1516/* Create a new bfd section from an ELF program header.
1517
1518 Since program segments have no names, we generate a synthetic name
1519 of the form segment<NUM>, where NUM is generally the index in the
1520 program header table. For segments that are split (see below) we
1521 generate the names segment<NUM>a and segment<NUM>b.
1522
1523 Note that some program segments may have a file size that is different than
1524 (less than) the memory size. All this means is that at execution the
1525 system must allocate the amount of memory specified by the memory size,
1526 but only initialize it with the first "file size" bytes read from the
1527 file. This would occur for example, with program segments consisting
1528 of combined data+bss.
1529
1530 To handle the above situation, this routine generates TWO bfd sections
1531 for the single program segment. The first has the length specified by
1532 the file size of the segment, and the second has the length specified
1533 by the difference between the two sizes. In effect, the segment is split
1534 into it's initialized and uninitialized parts.
1535
1536 */
1537
1538boolean
1539_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1540 bfd *abfd;
1541 Elf_Internal_Phdr *hdr;
1542 int index;
1543 const char *typename;
1544{
1545 asection *newsect;
1546 char *name;
1547 char namebuf[64];
1548 int split;
1549
1550 split = ((hdr->p_memsz > 0)
1551 && (hdr->p_filesz > 0)
1552 && (hdr->p_memsz > hdr->p_filesz));
1553 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1554 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1555 if (!name)
1556 return false;
1557 strcpy (name, namebuf);
1558 newsect = bfd_make_section (abfd, name);
1559 if (newsect == NULL)
1560 return false;
1561 newsect->vma = hdr->p_vaddr;
1562 newsect->lma = hdr->p_paddr;
1563 newsect->_raw_size = hdr->p_filesz;
1564 newsect->filepos = hdr->p_offset;
1565 newsect->flags |= SEC_HAS_CONTENTS;
1566 if (hdr->p_type == PT_LOAD)
1567 {
1568 newsect->flags |= SEC_ALLOC;
1569 newsect->flags |= SEC_LOAD;
1570 if (hdr->p_flags & PF_X)
1571 {
1572 /* FIXME: all we known is that it has execute PERMISSION,
1573 may be data. */
1574 newsect->flags |= SEC_CODE;
1575 }
1576 }
1577 if (!(hdr->p_flags & PF_W))
1578 {
1579 newsect->flags |= SEC_READONLY;
1580 }
1581
1582 if (split)
1583 {
1584 sprintf (namebuf, "%s%db", typename, index);
1585 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1586 if (!name)
1587 return false;
1588 strcpy (name, namebuf);
1589 newsect = bfd_make_section (abfd, name);
1590 if (newsect == NULL)
1591 return false;
1592 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1593 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1594 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1595 if (hdr->p_type == PT_LOAD)
1596 {
1597 newsect->flags |= SEC_ALLOC;
1598 if (hdr->p_flags & PF_X)
1599 newsect->flags |= SEC_CODE;
1600 }
1601 if (!(hdr->p_flags & PF_W))
1602 newsect->flags |= SEC_READONLY;
1603 }
1604
1605 return true;
1606}
1607
1608boolean
1609bfd_section_from_phdr (abfd, hdr, index)
1610 bfd *abfd;
1611 Elf_Internal_Phdr *hdr;
1612 int index;
1613{
1614 struct elf_backend_data *bed;
1615
1616 switch (hdr->p_type)
1617 {
1618 case PT_NULL:
1619 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1620
1621 case PT_LOAD:
1622 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1623
1624 case PT_DYNAMIC:
1625 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1626
1627 case PT_INTERP:
1628 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1629
1630 case PT_NOTE:
1631 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1632 return false;
1633 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1634 return false;
1635 return true;
1636
1637 case PT_SHLIB:
1638 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1639
1640 case PT_PHDR:
1641 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1642
1643 default:
1644 /* Check for any processor-specific program segment types.
1645 If no handler for them, default to making "segment" sections. */
1646 bed = get_elf_backend_data (abfd);
1647 if (bed->elf_backend_section_from_phdr)
1648 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1649 else
1650 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1651 }
1652}
1653
1654/* Initialize REL_HDR, the section-header for new section, containing
1655 relocations against ASECT. If USE_RELA_P is true, we use RELA
1656 relocations; otherwise, we use REL relocations. */
1657
1658boolean
1659_bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1660 bfd *abfd;
1661 Elf_Internal_Shdr *rel_hdr;
1662 asection *asect;
1663 boolean use_rela_p;
1664{
1665 char *name;
1666 struct elf_backend_data *bed;
1667
1668 bed = get_elf_backend_data (abfd);
1669 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1670 if (name == NULL)
1671 return false;
1672 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1673 rel_hdr->sh_name =
1674 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1675 true, false);
1676 if (rel_hdr->sh_name == (unsigned int) -1)
1677 return false;
1678 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1679 rel_hdr->sh_entsize = (use_rela_p
1680 ? bed->s->sizeof_rela
1681 : bed->s->sizeof_rel);
1682 rel_hdr->sh_addralign = bed->s->file_align;
1683 rel_hdr->sh_flags = 0;
1684 rel_hdr->sh_addr = 0;
1685 rel_hdr->sh_size = 0;
1686 rel_hdr->sh_offset = 0;
1687
1688 return true;
1689}
1690
1691/* Set up an ELF internal section header for a section. */
1692
1693static void
1694elf_fake_sections (abfd, asect, failedptrarg)
1695 bfd *abfd;
1696 asection *asect;
1697 PTR failedptrarg;
1698{
1699 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1700 boolean *failedptr = (boolean *) failedptrarg;
1701 Elf_Internal_Shdr *this_hdr;
1702
1703 if (*failedptr)
1704 {
1705 /* We already failed; just get out of the bfd_map_over_sections
1706 loop. */
1707 return;
1708 }
1709
1710 this_hdr = &elf_section_data (asect)->this_hdr;
1711
1712 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1713 asect->name,
1714 true, false);
1715 if (this_hdr->sh_name == (unsigned long) -1)
1716 {
1717 *failedptr = true;
1718 return;
1719 }
1720
1721 this_hdr->sh_flags = 0;
1722
1723 if ((asect->flags & SEC_ALLOC) != 0
1724 || asect->user_set_vma)
1725 this_hdr->sh_addr = asect->vma;
1726 else
1727 this_hdr->sh_addr = 0;
1728
1729 this_hdr->sh_offset = 0;
1730 this_hdr->sh_size = asect->_raw_size;
1731 this_hdr->sh_link = 0;
1732 this_hdr->sh_addralign = 1 << asect->alignment_power;
1733 /* The sh_entsize and sh_info fields may have been set already by
1734 copy_private_section_data. */
1735
1736 this_hdr->bfd_section = asect;
1737 this_hdr->contents = NULL;
1738
1739 /* FIXME: This should not be based on section names. */
1740 if (strcmp (asect->name, ".dynstr") == 0)
1741 this_hdr->sh_type = SHT_STRTAB;
1742 else if (strcmp (asect->name, ".hash") == 0)
1743 {
1744 this_hdr->sh_type = SHT_HASH;
1745 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1746 }
1747 else if (strcmp (asect->name, ".dynsym") == 0)
1748 {
1749 this_hdr->sh_type = SHT_DYNSYM;
1750 this_hdr->sh_entsize = bed->s->sizeof_sym;
1751 }
1752 else if (strcmp (asect->name, ".dynamic") == 0)
1753 {
1754 this_hdr->sh_type = SHT_DYNAMIC;
1755 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1756 }
1757 else if (strncmp (asect->name, ".rela", 5) == 0
1758 && get_elf_backend_data (abfd)->may_use_rela_p)
1759 {
1760 this_hdr->sh_type = SHT_RELA;
1761 this_hdr->sh_entsize = bed->s->sizeof_rela;
1762 }
1763 else if (strncmp (asect->name, ".rel", 4) == 0
1764 && get_elf_backend_data (abfd)->may_use_rel_p)
1765 {
1766 this_hdr->sh_type = SHT_REL;
1767 this_hdr->sh_entsize = bed->s->sizeof_rel;
1768 }
1769 else if (strncmp (asect->name, ".note", 5) == 0)
1770 this_hdr->sh_type = SHT_NOTE;
1771 else if (strncmp (asect->name, ".stab", 5) == 0
1772 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1773 this_hdr->sh_type = SHT_STRTAB;
1774 else if (strcmp (asect->name, ".gnu.version") == 0)
1775 {
1776 this_hdr->sh_type = SHT_GNU_versym;
1777 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1778 }
1779 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1780 {
1781 this_hdr->sh_type = SHT_GNU_verdef;
1782 this_hdr->sh_entsize = 0;
1783 /* objcopy or strip will copy over sh_info, but may not set
1784 cverdefs. The linker will set cverdefs, but sh_info will be
1785 zero. */
1786 if (this_hdr->sh_info == 0)
1787 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1788 else
1789 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1790 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1791 }
1792 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1793 {
1794 this_hdr->sh_type = SHT_GNU_verneed;
1795 this_hdr->sh_entsize = 0;
1796 /* objcopy or strip will copy over sh_info, but may not set
1797 cverrefs. The linker will set cverrefs, but sh_info will be
1798 zero. */
1799 if (this_hdr->sh_info == 0)
1800 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1801 else
1802 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1803 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1804 }
1805 else if ((asect->flags & SEC_ALLOC) != 0
1806 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
1807 this_hdr->sh_type = SHT_NOBITS;
1808 else
1809 this_hdr->sh_type = SHT_PROGBITS;
1810
1811 if ((asect->flags & SEC_ALLOC) != 0)
1812 this_hdr->sh_flags |= SHF_ALLOC;
1813 if ((asect->flags & SEC_READONLY) == 0)
1814 this_hdr->sh_flags |= SHF_WRITE;
1815 if ((asect->flags & SEC_CODE) != 0)
1816 this_hdr->sh_flags |= SHF_EXECINSTR;
1817
1818 /* Check for processor-specific section types. */
1819 if (bed->elf_backend_fake_sections)
1820 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1821
1822 /* If the section has relocs, set up a section header for the
1823 SHT_REL[A] section. If two relocation sections are required for
1824 this section, it is up to the processor-specific back-end to
1825 create the other. */
1826 if ((asect->flags & SEC_RELOC) != 0
1827 && !_bfd_elf_init_reloc_shdr (abfd,
1828 &elf_section_data (asect)->rel_hdr,
1829 asect,
1830 elf_section_data (asect)->use_rela_p))
1831 *failedptr = true;
1832}
1833
1834/* Assign all ELF section numbers. The dummy first section is handled here
1835 too. The link/info pointers for the standard section types are filled
1836 in here too, while we're at it. */
1837
1838static boolean
1839assign_section_numbers (abfd)
1840 bfd *abfd;
1841{
1842 struct elf_obj_tdata *t = elf_tdata (abfd);
1843 asection *sec;
1844 unsigned int section_number;
1845 Elf_Internal_Shdr **i_shdrp;
1846
1847 section_number = 1;
1848
1849 for (sec = abfd->sections; sec; sec = sec->next)
1850 {
1851 struct bfd_elf_section_data *d = elf_section_data (sec);
1852
1853 d->this_idx = section_number++;
1854 if ((sec->flags & SEC_RELOC) == 0)
1855 d->rel_idx = 0;
1856 else
1857 d->rel_idx = section_number++;
1858
1859 if (d->rel_hdr2)
1860 d->rel_idx2 = section_number++;
1861 else
1862 d->rel_idx2 = 0;
1863 }
1864
1865 t->shstrtab_section = section_number++;
1866 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1867 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1868
1869 if (bfd_get_symcount (abfd) > 0)
1870 {
1871 t->symtab_section = section_number++;
1872 t->strtab_section = section_number++;
1873 }
1874
1875 elf_elfheader (abfd)->e_shnum = section_number;
1876
1877 /* Set up the list of section header pointers, in agreement with the
1878 indices. */
1879 i_shdrp = ((Elf_Internal_Shdr **)
1880 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1881 if (i_shdrp == NULL)
1882 return false;
1883
1884 i_shdrp[0] = ((Elf_Internal_Shdr *)
1885 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1886 if (i_shdrp[0] == NULL)
1887 {
1888 bfd_release (abfd, i_shdrp);
1889 return false;
1890 }
1891 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1892
1893 elf_elfsections (abfd) = i_shdrp;
1894
1895 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1896 if (bfd_get_symcount (abfd) > 0)
1897 {
1898 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1899 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1900 t->symtab_hdr.sh_link = t->strtab_section;
1901 }
1902 for (sec = abfd->sections; sec; sec = sec->next)
1903 {
1904 struct bfd_elf_section_data *d = elf_section_data (sec);
1905 asection *s;
1906 const char *name;
1907
1908 i_shdrp[d->this_idx] = &d->this_hdr;
1909 if (d->rel_idx != 0)
1910 i_shdrp[d->rel_idx] = &d->rel_hdr;
1911 if (d->rel_idx2 != 0)
1912 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1913
1914 /* Fill in the sh_link and sh_info fields while we're at it. */
1915
1916 /* sh_link of a reloc section is the section index of the symbol
1917 table. sh_info is the section index of the section to which
1918 the relocation entries apply. */
1919 if (d->rel_idx != 0)
1920 {
1921 d->rel_hdr.sh_link = t->symtab_section;
1922 d->rel_hdr.sh_info = d->this_idx;
1923 }
1924 if (d->rel_idx2 != 0)
1925 {
1926 d->rel_hdr2->sh_link = t->symtab_section;
1927 d->rel_hdr2->sh_info = d->this_idx;
1928 }
1929
1930 switch (d->this_hdr.sh_type)
1931 {
1932 case SHT_REL:
1933 case SHT_RELA:
1934 /* A reloc section which we are treating as a normal BFD
1935 section. sh_link is the section index of the symbol
1936 table. sh_info is the section index of the section to
1937 which the relocation entries apply. We assume that an
1938 allocated reloc section uses the dynamic symbol table.
1939 FIXME: How can we be sure? */
1940 s = bfd_get_section_by_name (abfd, ".dynsym");
1941 if (s != NULL)
1942 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1943
1944 /* We look up the section the relocs apply to by name. */
1945 name = sec->name;
1946 if (d->this_hdr.sh_type == SHT_REL)
1947 name += 4;
1948 else
1949 name += 5;
1950 s = bfd_get_section_by_name (abfd, name);
1951 if (s != NULL)
1952 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1953 break;
1954
1955 case SHT_STRTAB:
1956 /* We assume that a section named .stab*str is a stabs
1957 string section. We look for a section with the same name
1958 but without the trailing ``str'', and set its sh_link
1959 field to point to this section. */
1960 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1961 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1962 {
1963 size_t len;
1964 char *alc;
1965
1966 len = strlen (sec->name);
1967 alc = (char *) bfd_malloc (len - 2);
1968 if (alc == NULL)
1969 return false;
1970 strncpy (alc, sec->name, len - 3);
1971 alc[len - 3] = '\0';
1972 s = bfd_get_section_by_name (abfd, alc);
1973 free (alc);
1974 if (s != NULL)
1975 {
1976 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1977
1978 /* This is a .stab section. */
1979 elf_section_data (s)->this_hdr.sh_entsize =
1980 4 + 2 * bfd_get_arch_size (abfd) / 8;
1981 }
1982 }
1983 break;
1984
1985 case SHT_DYNAMIC:
1986 case SHT_DYNSYM:
1987 case SHT_GNU_verneed:
1988 case SHT_GNU_verdef:
1989 /* sh_link is the section header index of the string table
1990 used for the dynamic entries, or the symbol table, or the
1991 version strings. */
1992 s = bfd_get_section_by_name (abfd, ".dynstr");
1993 if (s != NULL)
1994 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1995 break;
1996
1997 case SHT_HASH:
1998 case SHT_GNU_versym:
1999 /* sh_link is the section header index of the symbol table
2000 this hash table or version table is for. */
2001 s = bfd_get_section_by_name (abfd, ".dynsym");
2002 if (s != NULL)
2003 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2004 break;
2005 }
2006 }
2007
2008 return true;
2009}
2010
2011/* Map symbol from it's internal number to the external number, moving
2012 all local symbols to be at the head of the list. */
2013
2014static INLINE int
2015sym_is_global (abfd, sym)
2016 bfd *abfd;
2017 asymbol *sym;
2018{
2019 /* If the backend has a special mapping, use it. */
2020 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2021 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2022 (abfd, sym));
2023
2024 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2025 || bfd_is_und_section (bfd_get_section (sym))
2026 || bfd_is_com_section (bfd_get_section (sym)));
2027}
2028
2029static boolean
2030elf_map_symbols (abfd)
2031 bfd *abfd;
2032{
2033 int symcount = bfd_get_symcount (abfd);
2034 asymbol **syms = bfd_get_outsymbols (abfd);
2035 asymbol **sect_syms;
2036 int num_locals = 0;
2037 int num_globals = 0;
2038 int num_locals2 = 0;
2039 int num_globals2 = 0;
2040 int max_index = 0;
2041 int num_sections = 0;
2042 int idx;
2043 asection *asect;
2044 asymbol **new_syms;
2045 asymbol *sym;
2046
2047#ifdef DEBUG
2048 fprintf (stderr, "elf_map_symbols\n");
2049 fflush (stderr);
2050#endif
2051
2052 /* Add a section symbol for each BFD section. FIXME: Is this really
2053 necessary? */
2054 for (asect = abfd->sections; asect; asect = asect->next)
2055 {
2056 if (max_index < asect->index)
2057 max_index = asect->index;
2058 }
2059
2060 max_index++;
2061 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2062 if (sect_syms == NULL)
2063 return false;
2064 elf_section_syms (abfd) = sect_syms;
2065
2066 for (idx = 0; idx < symcount; idx++)
2067 {
2068 sym = syms[idx];
2069
2070 if ((sym->flags & BSF_SECTION_SYM) != 0
2071 && sym->value == 0)
2072 {
2073 asection *sec;
2074
2075 sec = sym->section;
2076
2077 if (sec->owner != NULL)
2078 {
2079 if (sec->owner != abfd)
2080 {
2081 if (sec->output_offset != 0)
2082 continue;
2083
2084 sec = sec->output_section;
2085
2086 /* Empty sections in the input files may have had a section
2087 symbol created for them. (See the comment near the end of
2088 _bfd_generic_link_output_symbols in linker.c). If the linker
2089 script discards such sections then we will reach this point.
2090 Since we know that we cannot avoid this case, we detect it
2091 and skip the abort and the assignment to the sect_syms array.
2092 To reproduce this particular case try running the linker
2093 testsuite test ld-scripts/weak.exp for an ELF port that uses
2094 the generic linker. */
2095 if (sec->owner == NULL)
2096 continue;
2097
2098 BFD_ASSERT (sec->owner == abfd);
2099 }
2100 sect_syms[sec->index] = syms[idx];
2101 }
2102 }
2103 }
2104
2105 for (asect = abfd->sections; asect; asect = asect->next)
2106 {
2107 if (sect_syms[asect->index] != NULL)
2108 continue;
2109
2110 sym = bfd_make_empty_symbol (abfd);
2111 if (sym == NULL)
2112 return false;
2113 sym->the_bfd = abfd;
2114 sym->name = asect->name;
2115 sym->value = 0;
2116 /* Set the flags to 0 to indicate that this one was newly added. */
2117 sym->flags = 0;
2118 sym->section = asect;
2119 sect_syms[asect->index] = sym;
2120 num_sections++;
2121#ifdef DEBUG
2122 fprintf (stderr,
2123 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2124 asect->name, (long) asect->vma, asect->index, (long) asect);
2125#endif
2126 }
2127
2128 /* Classify all of the symbols. */
2129 for (idx = 0; idx < symcount; idx++)
2130 {
2131 if (!sym_is_global (abfd, syms[idx]))
2132 num_locals++;
2133 else
2134 num_globals++;
2135 }
2136 for (asect = abfd->sections; asect; asect = asect->next)
2137 {
2138 if (sect_syms[asect->index] != NULL
2139 && sect_syms[asect->index]->flags == 0)
2140 {
2141 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2142 if (!sym_is_global (abfd, sect_syms[asect->index]))
2143 num_locals++;
2144 else
2145 num_globals++;
2146 sect_syms[asect->index]->flags = 0;
2147 }
2148 }
2149
2150 /* Now sort the symbols so the local symbols are first. */
2151 new_syms = ((asymbol **)
2152 bfd_alloc (abfd,
2153 (num_locals + num_globals) * sizeof (asymbol *)));
2154 if (new_syms == NULL)
2155 return false;
2156
2157 for (idx = 0; idx < symcount; idx++)
2158 {
2159 asymbol *sym = syms[idx];
2160 int i;
2161
2162 if (!sym_is_global (abfd, sym))
2163 i = num_locals2++;
2164 else
2165 i = num_locals + num_globals2++;
2166 new_syms[i] = sym;
2167 sym->udata.i = i + 1;
2168 }
2169 for (asect = abfd->sections; asect; asect = asect->next)
2170 {
2171 if (sect_syms[asect->index] != NULL
2172 && sect_syms[asect->index]->flags == 0)
2173 {
2174 asymbol *sym = sect_syms[asect->index];
2175 int i;
2176
2177 sym->flags = BSF_SECTION_SYM;
2178 if (!sym_is_global (abfd, sym))
2179 i = num_locals2++;
2180 else
2181 i = num_locals + num_globals2++;
2182 new_syms[i] = sym;
2183 sym->udata.i = i + 1;
2184 }
2185 }
2186
2187 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2188
2189 elf_num_locals (abfd) = num_locals;
2190 elf_num_globals (abfd) = num_globals;
2191 return true;
2192}
2193
2194/* Align to the maximum file alignment that could be required for any
2195 ELF data structure. */
2196
2197static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2198static INLINE file_ptr
2199align_file_position (off, align)
2200 file_ptr off;
2201 int align;
2202{
2203 return (off + align - 1) & ~(align - 1);
2204}
2205
2206/* Assign a file position to a section, optionally aligning to the
2207 required section alignment. */
2208
2209INLINE file_ptr
2210_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2211 Elf_Internal_Shdr *i_shdrp;
2212 file_ptr offset;
2213 boolean align;
2214{
2215 if (align)
2216 {
2217 unsigned int al;
2218
2219 al = i_shdrp->sh_addralign;
2220 if (al > 1)
2221 offset = BFD_ALIGN (offset, al);
2222 }
2223 i_shdrp->sh_offset = offset;
2224 if (i_shdrp->bfd_section != NULL)
2225 i_shdrp->bfd_section->filepos = offset;
2226 if (i_shdrp->sh_type != SHT_NOBITS)
2227 offset += i_shdrp->sh_size;
2228 return offset;
2229}
2230
2231/* Compute the file positions we are going to put the sections at, and
2232 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2233 is not NULL, this is being called by the ELF backend linker. */
2234
2235boolean
2236_bfd_elf_compute_section_file_positions (abfd, link_info)
2237 bfd *abfd;
2238 struct bfd_link_info *link_info;
2239{
2240 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2241 boolean failed;
2242 struct bfd_strtab_hash *strtab;
2243 Elf_Internal_Shdr *shstrtab_hdr;
2244
2245 if (abfd->output_has_begun)
2246 return true;
2247
2248 /* Do any elf backend specific processing first. */
2249 if (bed->elf_backend_begin_write_processing)
2250 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2251
2252 if (! prep_headers (abfd))
2253 return false;
2254
2255 /* Post process the headers if necessary. */
2256 if (bed->elf_backend_post_process_headers)
2257 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2258
2259 failed = false;
2260 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2261 if (failed)
2262 return false;
2263
2264 if (!assign_section_numbers (abfd))
2265 return false;
2266
2267 /* The backend linker builds symbol table information itself. */
2268 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2269 {
2270 /* Non-zero if doing a relocatable link. */
2271 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2272
2273 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2274 return false;
2275 }
2276
2277 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2278 /* sh_name was set in prep_headers. */
2279 shstrtab_hdr->sh_type = SHT_STRTAB;
2280 shstrtab_hdr->sh_flags = 0;
2281 shstrtab_hdr->sh_addr = 0;
2282 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2283 shstrtab_hdr->sh_entsize = 0;
2284 shstrtab_hdr->sh_link = 0;
2285 shstrtab_hdr->sh_info = 0;
2286 /* sh_offset is set in assign_file_positions_except_relocs. */
2287 shstrtab_hdr->sh_addralign = 1;
2288
2289 if (!assign_file_positions_except_relocs (abfd))
2290 return false;
2291
2292 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2293 {
2294 file_ptr off;
2295 Elf_Internal_Shdr *hdr;
2296
2297 off = elf_tdata (abfd)->next_file_pos;
2298
2299 hdr = &elf_tdata (abfd)->symtab_hdr;
2300 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2301
2302 hdr = &elf_tdata (abfd)->strtab_hdr;
2303 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2304
2305 elf_tdata (abfd)->next_file_pos = off;
2306
2307 /* Now that we know where the .strtab section goes, write it
2308 out. */
2309 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2310 || ! _bfd_stringtab_emit (abfd, strtab))
2311 return false;
2312 _bfd_stringtab_free (strtab);
2313 }
2314
2315 abfd->output_has_begun = true;
2316
2317 return true;
2318}
2319
2320/* Create a mapping from a set of sections to a program segment. */
2321
2322static INLINE struct elf_segment_map *
2323make_mapping (abfd, sections, from, to, phdr)
2324 bfd *abfd;
2325 asection **sections;
2326 unsigned int from;
2327 unsigned int to;
2328 boolean phdr;
2329{
2330 struct elf_segment_map *m;
2331 unsigned int i;
2332 asection **hdrpp;
2333
2334 m = ((struct elf_segment_map *)
2335 bfd_zalloc (abfd,
2336 (sizeof (struct elf_segment_map)
2337 + (to - from - 1) * sizeof (asection *))));
2338 if (m == NULL)
2339 return NULL;
2340 m->next = NULL;
2341 m->p_type = PT_LOAD;
2342 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2343 m->sections[i - from] = *hdrpp;
2344 m->count = to - from;
2345
2346 if (from == 0 && phdr)
2347 {
2348 /* Include the headers in the first PT_LOAD segment. */
2349 m->includes_filehdr = 1;
2350 m->includes_phdrs = 1;
2351 }
2352
2353 return m;
2354}
2355
2356/* Set up a mapping from BFD sections to program segments. */
2357
2358static boolean
2359map_sections_to_segments (abfd)
2360 bfd *abfd;
2361{
2362 asection **sections = NULL;
2363 asection *s;
2364 unsigned int i;
2365 unsigned int count;
2366 struct elf_segment_map *mfirst;
2367 struct elf_segment_map **pm;
2368 struct elf_segment_map *m;
2369 asection *last_hdr;
2370 unsigned int phdr_index;
2371 bfd_vma maxpagesize;
2372 asection **hdrpp;
2373 boolean phdr_in_segment = true;
2374 boolean writable;
2375 asection *dynsec;
2376
2377 if (elf_tdata (abfd)->segment_map != NULL)
2378 return true;
2379
2380 if (bfd_count_sections (abfd) == 0)
2381 return true;
2382
2383 /* Select the allocated sections, and sort them. */
2384
2385 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2386 * sizeof (asection *));
2387 if (sections == NULL)
2388 goto error_return;
2389
2390 i = 0;
2391 for (s = abfd->sections; s != NULL; s = s->next)
2392 {
2393 if ((s->flags & SEC_ALLOC) != 0)
2394 {
2395 sections[i] = s;
2396 ++i;
2397 }
2398 }
2399 BFD_ASSERT (i <= bfd_count_sections (abfd));
2400 count = i;
2401
2402 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2403
2404 /* Build the mapping. */
2405
2406 mfirst = NULL;
2407 pm = &mfirst;
2408
2409 /* If we have a .interp section, then create a PT_PHDR segment for
2410 the program headers and a PT_INTERP segment for the .interp
2411 section. */
2412 s = bfd_get_section_by_name (abfd, ".interp");
2413 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2414 {
2415 m = ((struct elf_segment_map *)
2416 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2417 if (m == NULL)
2418 goto error_return;
2419 m->next = NULL;
2420 m->p_type = PT_PHDR;
2421 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2422 m->p_flags = PF_R | PF_X;
2423 m->p_flags_valid = 1;
2424 m->includes_phdrs = 1;
2425
2426 *pm = m;
2427 pm = &m->next;
2428
2429 m = ((struct elf_segment_map *)
2430 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2431 if (m == NULL)
2432 goto error_return;
2433 m->next = NULL;
2434 m->p_type = PT_INTERP;
2435 m->count = 1;
2436 m->sections[0] = s;
2437
2438 *pm = m;
2439 pm = &m->next;
2440 }
2441
2442 /* Look through the sections. We put sections in the same program
2443 segment when the start of the second section can be placed within
2444 a few bytes of the end of the first section. */
2445 last_hdr = NULL;
2446 phdr_index = 0;
2447 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2448 writable = false;
2449 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2450 if (dynsec != NULL
2451 && (dynsec->flags & SEC_LOAD) == 0)
2452 dynsec = NULL;
2453
2454 /* Deal with -Ttext or something similar such that the first section
2455 is not adjacent to the program headers. This is an
2456 approximation, since at this point we don't know exactly how many
2457 program headers we will need. */
2458 if (count > 0)
2459 {
2460 bfd_size_type phdr_size;
2461
2462 phdr_size = elf_tdata (abfd)->program_header_size;
2463 if (phdr_size == 0)
2464 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2465 if ((abfd->flags & D_PAGED) == 0
2466 || sections[0]->lma < phdr_size
2467 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2468 phdr_in_segment = false;
2469 }
2470
2471 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2472 {
2473 asection *hdr;
2474 boolean new_segment;
2475
2476 hdr = *hdrpp;
2477
2478 /* See if this section and the last one will fit in the same
2479 segment. */
2480
2481 if (last_hdr == NULL)
2482 {
2483 /* If we don't have a segment yet, then we don't need a new
2484 one (we build the last one after this loop). */
2485 new_segment = false;
2486 }
2487 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2488 {
2489 /* If this section has a different relation between the
2490 virtual address and the load address, then we need a new
2491 segment. */
2492 new_segment = true;
2493 }
2494 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2495 < BFD_ALIGN (hdr->lma, maxpagesize))
2496 {
2497 /* If putting this section in this segment would force us to
2498 skip a page in the segment, then we need a new segment. */
2499 new_segment = true;
2500 }
2501 else if ((last_hdr->flags & SEC_LOAD) == 0
2502 && (hdr->flags & SEC_LOAD) != 0)
2503 {
2504 /* We don't want to put a loadable section after a
2505 nonloadable section in the same segment. */
2506 new_segment = true;
2507 }
2508 else if ((abfd->flags & D_PAGED) == 0)
2509 {
2510 /* If the file is not demand paged, which means that we
2511 don't require the sections to be correctly aligned in the
2512 file, then there is no other reason for a new segment. */
2513 new_segment = false;
2514 }
2515 else if (! writable
2516 && (hdr->flags & SEC_READONLY) == 0
2517 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2518 == hdr->lma))
2519 {
2520 /* We don't want to put a writable section in a read only
2521 segment, unless they are on the same page in memory
2522 anyhow. We already know that the last section does not
2523 bring us past the current section on the page, so the
2524 only case in which the new section is not on the same
2525 page as the previous section is when the previous section
2526 ends precisely on a page boundary. */
2527 new_segment = true;
2528 }
2529 else
2530 {
2531 /* Otherwise, we can use the same segment. */
2532 new_segment = false;
2533 }
2534
2535 if (! new_segment)
2536 {
2537 if ((hdr->flags & SEC_READONLY) == 0)
2538 writable = true;
2539 last_hdr = hdr;
2540 continue;
2541 }
2542
2543 /* We need a new program segment. We must create a new program
2544 header holding all the sections from phdr_index until hdr. */
2545
2546 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2547 if (m == NULL)
2548 goto error_return;
2549
2550 *pm = m;
2551 pm = &m->next;
2552
2553 if ((hdr->flags & SEC_READONLY) == 0)
2554 writable = true;
2555 else
2556 writable = false;
2557
2558 last_hdr = hdr;
2559 phdr_index = i;
2560 phdr_in_segment = false;
2561 }
2562
2563 /* Create a final PT_LOAD program segment. */
2564 if (last_hdr != NULL)
2565 {
2566 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2567 if (m == NULL)
2568 goto error_return;
2569
2570 *pm = m;
2571 pm = &m->next;
2572 }
2573
2574 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2575 if (dynsec != NULL)
2576 {
2577 m = ((struct elf_segment_map *)
2578 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2579 if (m == NULL)
2580 goto error_return;
2581 m->next = NULL;
2582 m->p_type = PT_DYNAMIC;
2583 m->count = 1;
2584 m->sections[0] = dynsec;
2585
2586 *pm = m;
2587 pm = &m->next;
2588 }
2589
2590 /* For each loadable .note section, add a PT_NOTE segment. We don't
2591 use bfd_get_section_by_name, because if we link together
2592 nonloadable .note sections and loadable .note sections, we will
2593 generate two .note sections in the output file. FIXME: Using
2594 names for section types is bogus anyhow. */
2595 for (s = abfd->sections; s != NULL; s = s->next)
2596 {
2597 if ((s->flags & SEC_LOAD) != 0
2598 && strncmp (s->name, ".note", 5) == 0)
2599 {
2600 m = ((struct elf_segment_map *)
2601 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2602 if (m == NULL)
2603 goto error_return;
2604 m->next = NULL;
2605 m->p_type = PT_NOTE;
2606 m->count = 1;
2607 m->sections[0] = s;
2608
2609 *pm = m;
2610 pm = &m->next;
2611 }
2612 }
2613
2614 free (sections);
2615 sections = NULL;
2616
2617 elf_tdata (abfd)->segment_map = mfirst;
2618 return true;
2619
2620 error_return:
2621 if (sections != NULL)
2622 free (sections);
2623 return false;
2624}
2625
2626/* Sort sections by address. */
2627
2628static int
2629elf_sort_sections (arg1, arg2)
2630 const PTR arg1;
2631 const PTR arg2;
2632{
2633 const asection *sec1 = *(const asection **) arg1;
2634 const asection *sec2 = *(const asection **) arg2;
2635
2636 /* Sort by LMA first, since this is the address used to
2637 place the section into a segment. */
2638 if (sec1->lma < sec2->lma)
2639 return -1;
2640 else if (sec1->lma > sec2->lma)
2641 return 1;
2642
2643 /* Then sort by VMA. Normally the LMA and the VMA will be
2644 the same, and this will do nothing. */
2645 if (sec1->vma < sec2->vma)
2646 return -1;
2647 else if (sec1->vma > sec2->vma)
2648 return 1;
2649
2650 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2651
2652#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2653
2654 if (TOEND (sec1))
2655 {
2656 if (TOEND (sec2))
2657 return sec1->target_index - sec2->target_index;
2658 else
2659 return 1;
2660 }
2661
2662 if (TOEND (sec2))
2663 return -1;
2664
2665#undef TOEND
2666
2667 /* Sort by size, to put zero sized sections before others at the
2668 same address. */
2669
2670 if (sec1->_raw_size < sec2->_raw_size)
2671 return -1;
2672 if (sec1->_raw_size > sec2->_raw_size)
2673 return 1;
2674
2675 return sec1->target_index - sec2->target_index;
2676}
2677
2678/* Assign file positions to the sections based on the mapping from
2679 sections to segments. This function also sets up some fields in
2680 the file header, and writes out the program headers. */
2681
2682static boolean
2683assign_file_positions_for_segments (abfd)
2684 bfd *abfd;
2685{
2686 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2687 unsigned int count;
2688 struct elf_segment_map *m;
2689 unsigned int alloc;
2690 Elf_Internal_Phdr *phdrs;
2691 file_ptr off, voff;
2692 bfd_vma filehdr_vaddr, filehdr_paddr;
2693 bfd_vma phdrs_vaddr, phdrs_paddr;
2694 Elf_Internal_Phdr *p;
2695
2696 if (elf_tdata (abfd)->segment_map == NULL)
2697 {
2698 if (! map_sections_to_segments (abfd))
2699 return false;
2700 }
2701
2702 if (bed->elf_backend_modify_segment_map)
2703 {
2704 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2705 return false;
2706 }
2707
2708 count = 0;
2709 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2710 ++count;
2711
2712 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2713 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2714 elf_elfheader (abfd)->e_phnum = count;
2715
2716 if (count == 0)
2717 return true;
2718
2719 /* If we already counted the number of program segments, make sure
2720 that we allocated enough space. This happens when SIZEOF_HEADERS
2721 is used in a linker script. */
2722 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2723 if (alloc != 0 && count > alloc)
2724 {
2725 ((*_bfd_error_handler)
2726 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2727 bfd_get_filename (abfd), alloc, count));
2728 bfd_set_error (bfd_error_bad_value);
2729 return false;
2730 }
2731
2732 if (alloc == 0)
2733 alloc = count;
2734
2735 phdrs = ((Elf_Internal_Phdr *)
2736 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2737 if (phdrs == NULL)
2738 return false;
2739
2740 off = bed->s->sizeof_ehdr;
2741 off += alloc * bed->s->sizeof_phdr;
2742
2743 filehdr_vaddr = 0;
2744 filehdr_paddr = 0;
2745 phdrs_vaddr = 0;
2746 phdrs_paddr = 0;
2747
2748 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2749 m != NULL;
2750 m = m->next, p++)
2751 {
2752 unsigned int i;
2753 asection **secpp;
2754
2755 /* If elf_segment_map is not from map_sections_to_segments, the
2756 sections may not be correctly ordered. */
2757 if (m->count > 0)
2758 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2759 elf_sort_sections);
2760
2761 p->p_type = m->p_type;
2762 p->p_flags = m->p_flags;
2763
2764 if (p->p_type == PT_LOAD
2765 && m->count > 0
2766 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2767 {
2768 if ((abfd->flags & D_PAGED) != 0)
2769 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2770 else
2771 {
2772 bfd_size_type align;
2773
2774 align = 0;
2775 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2776 {
2777 bfd_size_type secalign;
2778
2779 secalign = bfd_get_section_alignment (abfd, *secpp);
2780 if (secalign > align)
2781 align = secalign;
2782 }
2783
2784 off += (m->sections[0]->vma - off) % (1 << align);
2785 }
2786 }
2787
2788 if (m->count == 0)
2789 p->p_vaddr = 0;
2790 else
2791 p->p_vaddr = m->sections[0]->vma;
2792
2793 if (m->p_paddr_valid)
2794 p->p_paddr = m->p_paddr;
2795 else if (m->count == 0)
2796 p->p_paddr = 0;
2797 else
2798 p->p_paddr = m->sections[0]->lma;
2799
2800 if (p->p_type == PT_LOAD
2801 && (abfd->flags & D_PAGED) != 0)
2802 p->p_align = bed->maxpagesize;
2803 else if (m->count == 0)
2804 p->p_align = bed->s->file_align;
2805 else
2806 p->p_align = 0;
2807
2808 p->p_offset = 0;
2809 p->p_filesz = 0;
2810 p->p_memsz = 0;
2811
2812 if (m->includes_filehdr)
2813 {
2814 if (! m->p_flags_valid)
2815 p->p_flags |= PF_R;
2816 p->p_offset = 0;
2817 p->p_filesz = bed->s->sizeof_ehdr;
2818 p->p_memsz = bed->s->sizeof_ehdr;
2819 if (m->count > 0)
2820 {
2821 BFD_ASSERT (p->p_type == PT_LOAD);
2822
2823 if (p->p_vaddr < (bfd_vma) off)
2824 {
2825 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2826 bfd_get_filename (abfd));
2827 bfd_set_error (bfd_error_bad_value);
2828 return false;
2829 }
2830
2831 p->p_vaddr -= off;
2832 if (! m->p_paddr_valid)
2833 p->p_paddr -= off;
2834 }
2835 if (p->p_type == PT_LOAD)
2836 {
2837 filehdr_vaddr = p->p_vaddr;
2838 filehdr_paddr = p->p_paddr;
2839 }
2840 }
2841
2842 if (m->includes_phdrs)
2843 {
2844 if (! m->p_flags_valid)
2845 p->p_flags |= PF_R;
2846
2847 if (m->includes_filehdr)
2848 {
2849 if (p->p_type == PT_LOAD)
2850 {
2851 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2852 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2853 }
2854 }
2855 else
2856 {
2857 p->p_offset = bed->s->sizeof_ehdr;
2858
2859 if (m->count > 0)
2860 {
2861 BFD_ASSERT (p->p_type == PT_LOAD);
2862 p->p_vaddr -= off - p->p_offset;
2863 if (! m->p_paddr_valid)
2864 p->p_paddr -= off - p->p_offset;
2865 }
2866
2867 if (p->p_type == PT_LOAD)
2868 {
2869 phdrs_vaddr = p->p_vaddr;
2870 phdrs_paddr = p->p_paddr;
2871 }
2872 else
2873 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2874 }
2875
2876 p->p_filesz += alloc * bed->s->sizeof_phdr;
2877 p->p_memsz += alloc * bed->s->sizeof_phdr;
2878 }
2879
2880 if (p->p_type == PT_LOAD
2881 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2882 {
2883 if (! m->includes_filehdr && ! m->includes_phdrs)
2884 p->p_offset = off;
2885 else
2886 {
2887 file_ptr adjust;
2888
2889 adjust = off - (p->p_offset + p->p_filesz);
2890 p->p_filesz += adjust;
2891 p->p_memsz += adjust;
2892 }
2893 }
2894
2895 voff = off;
2896
2897 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2898 {
2899 asection *sec;
2900 flagword flags;
2901 bfd_size_type align;
2902
2903 sec = *secpp;
2904 flags = sec->flags;
2905 align = 1 << bfd_get_section_alignment (abfd, sec);
2906
2907 /* The section may have artificial alignment forced by a
2908 link script. Notice this case by the gap between the
2909 cumulative phdr vma and the section's vma. */
2910 if (p->p_vaddr + p->p_memsz < sec->vma)
2911 {
2912 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2913
2914 p->p_memsz += adjust;
2915 off += adjust;
2916 voff += adjust;
2917 if ((flags & SEC_LOAD) != 0)
2918 p->p_filesz += adjust;
2919 }
2920
2921 if (p->p_type == PT_LOAD)
2922 {
2923 bfd_signed_vma adjust;
2924
2925 if ((flags & SEC_LOAD) != 0)
2926 {
2927 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2928 if (adjust < 0)
2929 adjust = 0;
2930 }
2931 else if ((flags & SEC_ALLOC) != 0)
2932 {
2933 /* The section VMA must equal the file position
2934 modulo the page size. FIXME: I'm not sure if
2935 this adjustment is really necessary. We used to
2936 not have the SEC_LOAD case just above, and then
2937 this was necessary, but now I'm not sure. */
2938 if ((abfd->flags & D_PAGED) != 0)
2939 adjust = (sec->vma - voff) % bed->maxpagesize;
2940 else
2941 adjust = (sec->vma - voff) % align;
2942 }
2943 else
2944 adjust = 0;
2945
2946 if (adjust != 0)
2947 {
2948 if (i == 0)
2949 {
2950 (* _bfd_error_handler)
2951 (_("Error: First section in segment (%s) starts at 0x%x"),
2952 bfd_section_name (abfd, sec), sec->lma);
2953 (* _bfd_error_handler)
2954 (_(" whereas segment starts at 0x%x"),
2955 p->p_paddr);
2956
2957 return false;
2958 }
2959 p->p_memsz += adjust;
2960 off += adjust;
2961 voff += adjust;
2962 if ((flags & SEC_LOAD) != 0)
2963 p->p_filesz += adjust;
2964 }
2965
2966 sec->filepos = off;
2967
2968 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2969 used in a linker script we may have a section with
2970 SEC_LOAD clear but which is supposed to have
2971 contents. */
2972 if ((flags & SEC_LOAD) != 0
2973 || (flags & SEC_HAS_CONTENTS) != 0)
2974 off += sec->_raw_size;
2975
2976 if ((flags & SEC_ALLOC) != 0)
2977 voff += sec->_raw_size;
2978 }
2979
2980 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2981 {
2982 /* The actual "note" segment has i == 0.
2983 This is the one that actually contains everything. */
2984 if (i == 0)
2985 {
2986 sec->filepos = off;
2987 p->p_filesz = sec->_raw_size;
2988 off += sec->_raw_size;
2989 voff = off;
2990 }
2991 else
2992 {
2993 /* Fake sections -- don't need to be written. */
2994 sec->filepos = 0;
2995 sec->_raw_size = 0;
2996 flags = sec->flags = 0;
2997 }
2998 p->p_memsz = 0;
2999 p->p_align = 1;
3000 }
3001 else
3002 {
3003 p->p_memsz += sec->_raw_size;
3004
3005 if ((flags & SEC_LOAD) != 0)
3006 p->p_filesz += sec->_raw_size;
3007
3008 if (align > p->p_align
3009 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3010 p->p_align = align;
3011 }
3012
3013 if (! m->p_flags_valid)
3014 {
3015 p->p_flags |= PF_R;
3016 if ((flags & SEC_CODE) != 0)
3017 p->p_flags |= PF_X;
3018 if ((flags & SEC_READONLY) == 0)
3019 p->p_flags |= PF_W;
3020 }
3021 }
3022 }
3023
3024 /* Now that we have set the section file positions, we can set up
3025 the file positions for the non PT_LOAD segments. */
3026 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3027 m != NULL;
3028 m = m->next, p++)
3029 {
3030 if (p->p_type != PT_LOAD && m->count > 0)
3031 {
3032 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3033 p->p_offset = m->sections[0]->filepos;
3034 }
3035 if (m->count == 0)
3036 {
3037 if (m->includes_filehdr)
3038 {
3039 p->p_vaddr = filehdr_vaddr;
3040 if (! m->p_paddr_valid)
3041 p->p_paddr = filehdr_paddr;
3042 }
3043 else if (m->includes_phdrs)
3044 {
3045 p->p_vaddr = phdrs_vaddr;
3046 if (! m->p_paddr_valid)
3047 p->p_paddr = phdrs_paddr;
3048 }
3049 }
3050 }
3051
3052 /* Clear out any program headers we allocated but did not use. */
3053 for (; count < alloc; count++, p++)
3054 {
3055 memset (p, 0, sizeof *p);
3056 p->p_type = PT_NULL;
3057 }
3058
3059 elf_tdata (abfd)->phdr = phdrs;
3060
3061 elf_tdata (abfd)->next_file_pos = off;
3062
3063 /* Write out the program headers. */
3064 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3065 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3066 return false;
3067
3068 return true;
3069}
3070
3071/* Get the size of the program header.
3072
3073 If this is called by the linker before any of the section VMA's are set, it
3074 can't calculate the correct value for a strange memory layout. This only
3075 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3076 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3077 data segment (exclusive of .interp and .dynamic).
3078
3079 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3080 will be two segments. */
3081
3082static bfd_size_type
3083get_program_header_size (abfd)
3084 bfd *abfd;
3085{
3086 size_t segs;
3087 asection *s;
3088 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3089
3090 /* We can't return a different result each time we're called. */
3091 if (elf_tdata (abfd)->program_header_size != 0)
3092 return elf_tdata (abfd)->program_header_size;
3093
3094 if (elf_tdata (abfd)->segment_map != NULL)
3095 {
3096 struct elf_segment_map *m;
3097
3098 segs = 0;
3099 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3100 ++segs;
3101 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3102 return elf_tdata (abfd)->program_header_size;
3103 }
3104
3105 /* Assume we will need exactly two PT_LOAD segments: one for text
3106 and one for data. */
3107 segs = 2;
3108
3109 s = bfd_get_section_by_name (abfd, ".interp");
3110 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3111 {
3112 /* If we have a loadable interpreter section, we need a
3113 PT_INTERP segment. In this case, assume we also need a
3114 PT_PHDR segment, although that may not be true for all
3115 targets. */
3116 segs += 2;
3117 }
3118
3119 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3120 {
3121 /* We need a PT_DYNAMIC segment. */
3122 ++segs;
3123 }
3124
3125 for (s = abfd->sections; s != NULL; s = s->next)
3126 {
3127 if ((s->flags & SEC_LOAD) != 0
3128 && strncmp (s->name, ".note", 5) == 0)
3129 {
3130 /* We need a PT_NOTE segment. */
3131 ++segs;
3132 }
3133 }
3134
3135 /* Let the backend count up any program headers it might need. */
3136 if (bed->elf_backend_additional_program_headers)
3137 {
3138 int a;
3139
3140 a = (*bed->elf_backend_additional_program_headers) (abfd);
3141 if (a == -1)
3142 abort ();
3143 segs += a;
3144 }
3145
3146 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3147 return elf_tdata (abfd)->program_header_size;
3148}
3149
3150/* Work out the file positions of all the sections. This is called by
3151 _bfd_elf_compute_section_file_positions. All the section sizes and
3152 VMAs must be known before this is called.
3153
3154 We do not consider reloc sections at this point, unless they form
3155 part of the loadable image. Reloc sections are assigned file
3156 positions in assign_file_positions_for_relocs, which is called by
3157 write_object_contents and final_link.
3158
3159 We also don't set the positions of the .symtab and .strtab here. */
3160
3161static boolean
3162assign_file_positions_except_relocs (abfd)
3163 bfd *abfd;
3164{
3165 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3166 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3167 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3168 file_ptr off;
3169 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3170
3171 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3172 && bfd_get_format (abfd) != bfd_core)
3173 {
3174 Elf_Internal_Shdr **hdrpp;
3175 unsigned int i;
3176
3177 /* Start after the ELF header. */
3178 off = i_ehdrp->e_ehsize;
3179
3180 /* We are not creating an executable, which means that we are
3181 not creating a program header, and that the actual order of
3182 the sections in the file is unimportant. */
3183 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3184 {
3185 Elf_Internal_Shdr *hdr;
3186
3187 hdr = *hdrpp;
3188 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3189 {
3190 hdr->sh_offset = -1;
3191 continue;
3192 }
3193 if (i == tdata->symtab_section
3194 || i == tdata->strtab_section)
3195 {
3196 hdr->sh_offset = -1;
3197 continue;
3198 }
3199
3200 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3201 }
3202 }
3203 else
3204 {
3205 unsigned int i;
3206 Elf_Internal_Shdr **hdrpp;
3207
3208 /* Assign file positions for the loaded sections based on the
3209 assignment of sections to segments. */
3210 if (! assign_file_positions_for_segments (abfd))
3211 return false;
3212
3213 /* Assign file positions for the other sections. */
3214
3215 off = elf_tdata (abfd)->next_file_pos;
3216 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3217 {
3218 Elf_Internal_Shdr *hdr;
3219
3220 hdr = *hdrpp;
3221 if (hdr->bfd_section != NULL
3222 && hdr->bfd_section->filepos != 0)
3223 hdr->sh_offset = hdr->bfd_section->filepos;
3224 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3225 {
3226 ((*_bfd_error_handler)
3227 (_("%s: warning: allocated section `%s' not in segment"),
3228 bfd_get_filename (abfd),
3229 (hdr->bfd_section == NULL
3230 ? "*unknown*"
3231 : hdr->bfd_section->name)));
3232 if ((abfd->flags & D_PAGED) != 0)
3233 off += (hdr->sh_addr - off) % bed->maxpagesize;
3234 else
3235 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3236 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3237 false);
3238 }
3239 else if (hdr->sh_type == SHT_REL
3240 || hdr->sh_type == SHT_RELA
3241 || hdr == i_shdrpp[tdata->symtab_section]
3242 || hdr == i_shdrpp[tdata->strtab_section])
3243 hdr->sh_offset = -1;
3244 else
3245 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3246 }
3247 }
3248
3249 /* Place the section headers. */
3250 off = align_file_position (off, bed->s->file_align);
3251 i_ehdrp->e_shoff = off;
3252 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3253
3254 elf_tdata (abfd)->next_file_pos = off;
3255
3256 return true;
3257}
3258
3259static boolean
3260prep_headers (abfd)
3261 bfd *abfd;
3262{
3263 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3264 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3265 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3266 int count;
3267 struct bfd_strtab_hash *shstrtab;
3268 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3269
3270 i_ehdrp = elf_elfheader (abfd);
3271 i_shdrp = elf_elfsections (abfd);
3272
3273 shstrtab = _bfd_elf_stringtab_init ();
3274 if (shstrtab == NULL)
3275 return false;
3276
3277 elf_shstrtab (abfd) = shstrtab;
3278
3279 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3280 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3281 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3282 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3283
3284 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3285 i_ehdrp->e_ident[EI_DATA] =
3286 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3287 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3288
3289 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3290 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3291
3292 for (count = EI_PAD; count < EI_NIDENT; count++)
3293 i_ehdrp->e_ident[count] = 0;
3294
3295 if ((abfd->flags & DYNAMIC) != 0)
3296 i_ehdrp->e_type = ET_DYN;
3297 else if ((abfd->flags & EXEC_P) != 0)
3298 i_ehdrp->e_type = ET_EXEC;
3299 else if (bfd_get_format (abfd) == bfd_core)
3300 i_ehdrp->e_type = ET_CORE;
3301 else
3302 i_ehdrp->e_type = ET_REL;
3303
3304 switch (bfd_get_arch (abfd))
3305 {
3306 case bfd_arch_unknown:
3307 i_ehdrp->e_machine = EM_NONE;
3308 break;
3309 case bfd_arch_sparc:
3310 if (bfd_get_arch_size (abfd) == 64)
3311 i_ehdrp->e_machine = EM_SPARCV9;
3312 else
3313 i_ehdrp->e_machine = EM_SPARC;
3314 break;
3315 case bfd_arch_i370:
3316 i_ehdrp->e_machine = EM_S370;
3317 break;
3318 case bfd_arch_i386:
3319 if (bfd_get_arch_size (abfd) == 64)
3320 i_ehdrp->e_machine = EM_X86_64;
3321 else
3322 i_ehdrp->e_machine = EM_386;
3323 break;
3324 case bfd_arch_ia64:
3325 i_ehdrp->e_machine = EM_IA_64;
3326 break;
3327 case bfd_arch_m68hc11:
3328 i_ehdrp->e_machine = EM_68HC11;
3329 break;
3330 case bfd_arch_m68hc12:
3331 i_ehdrp->e_machine = EM_68HC12;
3332 break;
3333 case bfd_arch_m68k:
3334 i_ehdrp->e_machine = EM_68K;
3335 break;
3336 case bfd_arch_m88k:
3337 i_ehdrp->e_machine = EM_88K;
3338 break;
3339 case bfd_arch_i860:
3340 i_ehdrp->e_machine = EM_860;
3341 break;
3342 case bfd_arch_i960:
3343 i_ehdrp->e_machine = EM_960;
3344 break;
3345 case bfd_arch_mips: /* MIPS Rxxxx */
3346 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3347 break;
3348 case bfd_arch_hppa:
3349 i_ehdrp->e_machine = EM_PARISC;
3350 break;
3351 case bfd_arch_powerpc:
3352 i_ehdrp->e_machine = EM_PPC;
3353 break;
3354 case bfd_arch_alpha:
3355 i_ehdrp->e_machine = EM_ALPHA;
3356 break;
3357 case bfd_arch_sh:
3358 i_ehdrp->e_machine = EM_SH;
3359 break;
3360 case bfd_arch_d10v:
3361 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3362 break;
3363 case bfd_arch_d30v:
3364 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3365 break;
3366 case bfd_arch_fr30:
3367 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3368 break;
3369 case bfd_arch_mcore:
3370 i_ehdrp->e_machine = EM_MCORE;
3371 break;
3372 case bfd_arch_avr:
3373 i_ehdrp->e_machine = EM_AVR;
3374 break;
3375 case bfd_arch_v850:
3376 switch (bfd_get_mach (abfd))
3377 {
3378 default:
3379 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3380 }
3381 break;
3382 case bfd_arch_arc:
3383 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3384 break;
3385 case bfd_arch_arm:
3386 i_ehdrp->e_machine = EM_ARM;
3387 break;
3388 case bfd_arch_m32r:
3389 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3390 break;
3391 case bfd_arch_mn10200:
3392 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3393 break;
3394 case bfd_arch_mn10300:
3395 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3396 break;
3397 case bfd_arch_pj:
3398 i_ehdrp->e_machine = EM_PJ;
3399 break;
3400 case bfd_arch_cris:
3401 i_ehdrp->e_machine = EM_CRIS;
3402 break;
3403 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3404 default:
3405 i_ehdrp->e_machine = EM_NONE;
3406 }
3407 i_ehdrp->e_version = bed->s->ev_current;
3408 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3409
3410 /* No program header, for now. */
3411 i_ehdrp->e_phoff = 0;
3412 i_ehdrp->e_phentsize = 0;
3413 i_ehdrp->e_phnum = 0;
3414
3415 /* Each bfd section is section header entry. */
3416 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3417 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3418
3419 /* If we're building an executable, we'll need a program header table. */
3420 if (abfd->flags & EXEC_P)
3421 {
3422 /* It all happens later. */
3423#if 0
3424 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3425
3426 /* elf_build_phdrs() returns a (NULL-terminated) array of
3427 Elf_Internal_Phdrs. */
3428 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3429 i_ehdrp->e_phoff = outbase;
3430 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3431#endif
3432 }
3433 else
3434 {
3435 i_ehdrp->e_phentsize = 0;
3436 i_phdrp = 0;
3437 i_ehdrp->e_phoff = 0;
3438 }
3439
3440 elf_tdata (abfd)->symtab_hdr.sh_name =
3441 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3442 elf_tdata (abfd)->strtab_hdr.sh_name =
3443 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3444 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3445 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3446 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3447 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3448 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3449 return false;
3450
3451 return true;
3452}
3453
3454/* Assign file positions for all the reloc sections which are not part
3455 of the loadable file image. */
3456
3457void
3458_bfd_elf_assign_file_positions_for_relocs (abfd)
3459 bfd *abfd;
3460{
3461 file_ptr off;
3462 unsigned int i;
3463 Elf_Internal_Shdr **shdrpp;
3464
3465 off = elf_tdata (abfd)->next_file_pos;
3466
3467 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3468 i < elf_elfheader (abfd)->e_shnum;
3469 i++, shdrpp++)
3470 {
3471 Elf_Internal_Shdr *shdrp;
3472
3473 shdrp = *shdrpp;
3474 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3475 && shdrp->sh_offset == -1)
3476 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3477 }
3478
3479 elf_tdata (abfd)->next_file_pos = off;
3480}
3481
3482boolean
3483_bfd_elf_write_object_contents (abfd)
3484 bfd *abfd;
3485{
3486 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3487 Elf_Internal_Ehdr *i_ehdrp;
3488 Elf_Internal_Shdr **i_shdrp;
3489 boolean failed;
3490 unsigned int count;
3491
3492 if (! abfd->output_has_begun
3493 && ! _bfd_elf_compute_section_file_positions
3494 (abfd, (struct bfd_link_info *) NULL))
3495 return false;
3496
3497 i_shdrp = elf_elfsections (abfd);
3498 i_ehdrp = elf_elfheader (abfd);
3499
3500 failed = false;
3501 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3502 if (failed)
3503 return false;
3504
3505 _bfd_elf_assign_file_positions_for_relocs (abfd);
3506
3507 /* After writing the headers, we need to write the sections too... */
3508 for (count = 1; count < i_ehdrp->e_shnum; count++)
3509 {
3510 if (bed->elf_backend_section_processing)
3511 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3512 if (i_shdrp[count]->contents)
3513 {
3514 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3515 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3516 1, abfd)
3517 != i_shdrp[count]->sh_size))
3518 return false;
3519 }
3520 }
3521
3522 /* Write out the section header names. */
3523 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3524 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3525 return false;
3526
3527 if (bed->elf_backend_final_write_processing)
3528 (*bed->elf_backend_final_write_processing) (abfd,
3529 elf_tdata (abfd)->linker);
3530
3531 return bed->s->write_shdrs_and_ehdr (abfd);
3532}
3533
3534boolean
3535_bfd_elf_write_corefile_contents (abfd)
3536 bfd *abfd;
3537{
3538 /* Hopefully this can be done just like an object file. */
3539 return _bfd_elf_write_object_contents (abfd);
3540}
3541
3542/* Given a section, search the header to find them. */
3543
3544int
3545_bfd_elf_section_from_bfd_section (abfd, asect)
3546 bfd *abfd;
3547 struct sec *asect;
3548{
3549 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3550 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3551 int index;
3552 Elf_Internal_Shdr *hdr;
3553 int maxindex = elf_elfheader (abfd)->e_shnum;
3554
3555 for (index = 0; index < maxindex; index++)
3556 {
3557 hdr = i_shdrp[index];
3558 if (hdr->bfd_section == asect)
3559 return index;
3560 }
3561
3562 if (bed->elf_backend_section_from_bfd_section)
3563 {
3564 for (index = 0; index < maxindex; index++)
3565 {
3566 int retval;
3567
3568 hdr = i_shdrp[index];
3569 retval = index;
3570 if ((*bed->elf_backend_section_from_bfd_section)
3571 (abfd, hdr, asect, &retval))
3572 return retval;
3573 }
3574 }
3575
3576 if (bfd_is_abs_section (asect))
3577 return SHN_ABS;
3578 if (bfd_is_com_section (asect))
3579 return SHN_COMMON;
3580 if (bfd_is_und_section (asect))
3581 return SHN_UNDEF;
3582
3583 bfd_set_error (bfd_error_nonrepresentable_section);
3584
3585 return -1;
3586}
3587
3588/* Given a BFD symbol, return the index in the ELF symbol table, or -1
3589 on error. */
3590
3591int
3592_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3593 bfd *abfd;
3594 asymbol **asym_ptr_ptr;
3595{
3596 asymbol *asym_ptr = *asym_ptr_ptr;
3597 int idx;
3598 flagword flags = asym_ptr->flags;
3599
3600 /* When gas creates relocations against local labels, it creates its
3601 own symbol for the section, but does put the symbol into the
3602 symbol chain, so udata is 0. When the linker is generating
3603 relocatable output, this section symbol may be for one of the
3604 input sections rather than the output section. */
3605 if (asym_ptr->udata.i == 0
3606 && (flags & BSF_SECTION_SYM)
3607 && asym_ptr->section)
3608 {
3609 int indx;
3610
3611 if (asym_ptr->section->output_section != NULL)
3612 indx = asym_ptr->section->output_section->index;
3613 else
3614 indx = asym_ptr->section->index;
3615 if (elf_section_syms (abfd)[indx])
3616 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3617 }
3618
3619 idx = asym_ptr->udata.i;
3620
3621 if (idx == 0)
3622 {
3623 /* This case can occur when using --strip-symbol on a symbol
3624 which is used in a relocation entry. */
3625 (*_bfd_error_handler)
3626 (_("%s: symbol `%s' required but not present"),
3627 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3628 bfd_set_error (bfd_error_no_symbols);
3629 return -1;
3630 }
3631
3632#if DEBUG & 4
3633 {
3634 fprintf (stderr,
3635 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3636 (long) asym_ptr, asym_ptr->name, idx, flags,
3637 elf_symbol_flags (flags));
3638 fflush (stderr);
3639 }
3640#endif
3641
3642 return idx;
3643}
3644
3645/* Copy private BFD data. This copies any program header information. */
3646
3647static boolean
3648copy_private_bfd_data (ibfd, obfd)
3649 bfd *ibfd;
3650 bfd *obfd;
3651{
3652 Elf_Internal_Ehdr * iehdr;
3653 struct elf_segment_map * map;
3654 struct elf_segment_map * map_first;
3655 struct elf_segment_map ** pointer_to_map;
3656 Elf_Internal_Phdr * segment;
3657 asection * section;
3658 unsigned int i;
3659 unsigned int num_segments;
3660 boolean phdr_included = false;
3661 bfd_vma maxpagesize;
3662 struct elf_segment_map * phdr_adjust_seg = NULL;
3663 unsigned int phdr_adjust_num = 0;
3664
3665 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3666 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3667 return true;
3668
3669 if (elf_tdata (ibfd)->phdr == NULL)
3670 return true;
3671
3672 iehdr = elf_elfheader (ibfd);
3673
3674 map_first = NULL;
3675 pointer_to_map = &map_first;
3676
3677 num_segments = elf_elfheader (ibfd)->e_phnum;
3678 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3679
3680 /* Returns the end address of the segment + 1. */
3681#define SEGMENT_END(segment, start) \
3682 (start + (segment->p_memsz > segment->p_filesz \
3683 ? segment->p_memsz : segment->p_filesz))
3684
3685 /* Returns true if the given section is contained within
3686 the given segment. VMA addresses are compared. */
3687#define IS_CONTAINED_BY_VMA(section, segment) \
3688 (section->vma >= segment->p_vaddr \
3689 && (section->vma + section->_raw_size) \
3690 <= (SEGMENT_END (segment, segment->p_vaddr)))
3691
3692 /* Returns true if the given section is contained within
3693 the given segment. LMA addresses are compared. */
3694#define IS_CONTAINED_BY_LMA(section, segment, base) \
3695 (section->lma >= base \
3696 && (section->lma + section->_raw_size) \
3697 <= SEGMENT_END (segment, base))
3698
3699 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3700#define IS_COREFILE_NOTE(p, s) \
3701 (p->p_type == PT_NOTE \
3702 && bfd_get_format (ibfd) == bfd_core \
3703 && s->vma == 0 && s->lma == 0 \
3704 && (bfd_vma) s->filepos >= p->p_offset \
3705 && (bfd_vma) s->filepos + s->_raw_size \
3706 <= p->p_offset + p->p_filesz)
3707
3708 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3709 linker, which generates a PT_INTERP section with p_vaddr and
3710 p_memsz set to 0. */
3711#define IS_SOLARIS_PT_INTERP(p, s) \
3712 ( p->p_vaddr == 0 \
3713 && p->p_filesz > 0 \
3714 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3715 && s->_raw_size > 0 \
3716 && (bfd_vma) s->filepos >= p->p_offset \
3717 && ((bfd_vma) s->filepos + s->_raw_size \
3718 <= p->p_offset + p->p_filesz))
3719
3720 /* Decide if the given section should be included in the given segment.
3721 A section will be included if:
3722 1. It is within the address space of the segment,
3723 2. It is an allocated segment,
3724 3. There is an output section associated with it,
3725 4. The section has not already been allocated to a previous segment. */
3726#define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3727 ((((IS_CONTAINED_BY_VMA (section, segment) \
3728 || IS_SOLARIS_PT_INTERP (segment, section)) \
3729 && (section->flags & SEC_ALLOC) != 0) \
3730 || IS_COREFILE_NOTE (segment, section)) \
3731 && section->output_section != NULL \
3732 && section->segment_mark == false)
3733
3734 /* Returns true iff seg1 starts after the end of seg2. */
3735#define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3736 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3737
3738 /* Returns true iff seg1 and seg2 overlap. */
3739#define SEGMENT_OVERLAPS(seg1, seg2) \
3740 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3741
3742 /* Initialise the segment mark field. */
3743 for (section = ibfd->sections; section != NULL; section = section->next)
3744 section->segment_mark = false;
3745
3746 /* Scan through the segments specified in the program header
3747 of the input BFD. For this first scan we look for overlaps
3748 in the loadable segments. These can be created by wierd
3749 parameters to objcopy. */
3750 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3751 i < num_segments;
3752 i++, segment++)
3753 {
3754 unsigned int j;
3755 Elf_Internal_Phdr *segment2;
3756
3757 if (segment->p_type != PT_LOAD)
3758 continue;
3759
3760 /* Determine if this segment overlaps any previous segments. */
3761 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3762 {
3763 bfd_signed_vma extra_length;
3764
3765 if (segment2->p_type != PT_LOAD
3766 || ! SEGMENT_OVERLAPS (segment, segment2))
3767 continue;
3768
3769 /* Merge the two segments together. */
3770 if (segment2->p_vaddr < segment->p_vaddr)
3771 {
3772 /* Extend SEGMENT2 to include SEGMENT and then delete
3773 SEGMENT. */
3774 extra_length =
3775 SEGMENT_END (segment, segment->p_vaddr)
3776 - SEGMENT_END (segment2, segment2->p_vaddr);
3777
3778 if (extra_length > 0)
3779 {
3780 segment2->p_memsz += extra_length;
3781 segment2->p_filesz += extra_length;
3782 }
3783
3784 segment->p_type = PT_NULL;
3785
3786 /* Since we have deleted P we must restart the outer loop. */
3787 i = 0;
3788 segment = elf_tdata (ibfd)->phdr;
3789 break;
3790 }
3791 else
3792 {
3793 /* Extend SEGMENT to include SEGMENT2 and then delete
3794 SEGMENT2. */
3795 extra_length =
3796 SEGMENT_END (segment2, segment2->p_vaddr)
3797 - SEGMENT_END (segment, segment->p_vaddr);
3798
3799 if (extra_length > 0)
3800 {
3801 segment->p_memsz += extra_length;
3802 segment->p_filesz += extra_length;
3803 }
3804
3805 segment2->p_type = PT_NULL;
3806 }
3807 }
3808 }
3809
3810 /* The second scan attempts to assign sections to segments. */
3811 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3812 i < num_segments;
3813 i ++, segment ++)
3814 {
3815 unsigned int section_count;
3816 asection ** sections;
3817 asection * output_section;
3818 unsigned int isec;
3819 bfd_vma matching_lma;
3820 bfd_vma suggested_lma;
3821 unsigned int j;
3822
3823 if (segment->p_type == PT_NULL)
3824 continue;
3825
3826 /* Compute how many sections might be placed into this segment. */
3827 section_count = 0;
3828 for (section = ibfd->sections; section != NULL; section = section->next)
3829 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3830 ++section_count;
3831
3832 /* Allocate a segment map big enough to contain all of the
3833 sections we have selected. */
3834 map = ((struct elf_segment_map *)
3835 bfd_alloc (obfd,
3836 (sizeof (struct elf_segment_map)
3837 + ((size_t) section_count - 1) * sizeof (asection *))));
3838 if (map == NULL)
3839 return false;
3840
3841 /* Initialise the fields of the segment map. Default to
3842 using the physical address of the segment in the input BFD. */
3843 map->next = NULL;
3844 map->p_type = segment->p_type;
3845 map->p_flags = segment->p_flags;
3846 map->p_flags_valid = 1;
3847 map->p_paddr = segment->p_paddr;
3848 map->p_paddr_valid = 1;
3849
3850 /* Determine if this segment contains the ELF file header
3851 and if it contains the program headers themselves. */
3852 map->includes_filehdr = (segment->p_offset == 0
3853 && segment->p_filesz >= iehdr->e_ehsize);
3854
3855 map->includes_phdrs = 0;
3856
3857 if (! phdr_included || segment->p_type != PT_LOAD)
3858 {
3859 map->includes_phdrs =
3860 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3861 && (segment->p_offset + segment->p_filesz
3862 >= ((bfd_vma) iehdr->e_phoff
3863 + iehdr->e_phnum * iehdr->e_phentsize)));
3864
3865 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3866 phdr_included = true;
3867 }
3868
3869 if (section_count == 0)
3870 {
3871 /* Special segments, such as the PT_PHDR segment, may contain
3872 no sections, but ordinary, loadable segments should contain
3873 something. */
3874 if (segment->p_type == PT_LOAD)
3875 _bfd_error_handler
3876 (_("%s: warning: Empty loadable segment detected\n"),
3877 bfd_get_filename (ibfd));
3878
3879 map->count = 0;
3880 *pointer_to_map = map;
3881 pointer_to_map = &map->next;
3882
3883 continue;
3884 }
3885
3886 /* Now scan the sections in the input BFD again and attempt
3887 to add their corresponding output sections to the segment map.
3888 The problem here is how to handle an output section which has
3889 been moved (ie had its LMA changed). There are four possibilities:
3890
3891 1. None of the sections have been moved.
3892 In this case we can continue to use the segment LMA from the
3893 input BFD.
3894
3895 2. All of the sections have been moved by the same amount.
3896 In this case we can change the segment's LMA to match the LMA
3897 of the first section.
3898
3899 3. Some of the sections have been moved, others have not.
3900 In this case those sections which have not been moved can be
3901 placed in the current segment which will have to have its size,
3902 and possibly its LMA changed, and a new segment or segments will
3903 have to be created to contain the other sections.
3904
3905 4. The sections have been moved, but not be the same amount.
3906 In this case we can change the segment's LMA to match the LMA
3907 of the first section and we will have to create a new segment
3908 or segments to contain the other sections.
3909
3910 In order to save time, we allocate an array to hold the section
3911 pointers that we are interested in. As these sections get assigned
3912 to a segment, they are removed from this array. */
3913
3914 sections = (asection **) bfd_malloc
3915 (sizeof (asection *) * section_count);
3916 if (sections == NULL)
3917 return false;
3918
3919 /* Step One: Scan for segment vs section LMA conflicts.
3920 Also add the sections to the section array allocated above.
3921 Also add the sections to the current segment. In the common
3922 case, where the sections have not been moved, this means that
3923 we have completely filled the segment, and there is nothing
3924 more to do. */
3925 isec = 0;
3926 matching_lma = 0;
3927 suggested_lma = 0;
3928
3929 for (j = 0, section = ibfd->sections;
3930 section != NULL;
3931 section = section->next)
3932 {
3933 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3934 {
3935 output_section = section->output_section;
3936
3937 sections[j ++] = section;
3938
3939 /* The Solaris native linker always sets p_paddr to 0.
3940 We try to catch that case here, and set it to the
3941 correct value. */
3942 if (segment->p_paddr == 0
3943 && segment->p_vaddr != 0
3944 && isec == 0
3945 && output_section->lma != 0
3946 && (output_section->vma == (segment->p_vaddr
3947 + (map->includes_filehdr
3948 ? iehdr->e_ehsize
3949 : 0)
3950 + (map->includes_phdrs
3951 ? iehdr->e_phnum * iehdr->e_phentsize
3952 : 0))))
3953 map->p_paddr = segment->p_vaddr;
3954
3955 /* Match up the physical address of the segment with the
3956 LMA address of the output section. */
3957 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3958 || IS_COREFILE_NOTE (segment, section))
3959 {
3960 if (matching_lma == 0)
3961 matching_lma = output_section->lma;
3962
3963 /* We assume that if the section fits within the segment
3964 then it does not overlap any other section within that
3965 segment. */
3966 map->sections[isec ++] = output_section;
3967 }
3968 else if (suggested_lma == 0)
3969 suggested_lma = output_section->lma;
3970 }
3971 }
3972
3973 BFD_ASSERT (j == section_count);
3974
3975 /* Step Two: Adjust the physical address of the current segment,
3976 if necessary. */
3977 if (isec == section_count)
3978 {
3979 /* All of the sections fitted within the segment as currently
3980 specified. This is the default case. Add the segment to
3981 the list of built segments and carry on to process the next
3982 program header in the input BFD. */
3983 map->count = section_count;
3984 *pointer_to_map = map;
3985 pointer_to_map = &map->next;
3986
3987 free (sections);
3988 continue;
3989 }
3990 else
3991 {
3992 if (matching_lma != 0)
3993 {
3994 /* At least one section fits inside the current segment.
3995 Keep it, but modify its physical address to match the
3996 LMA of the first section that fitted. */
3997 map->p_paddr = matching_lma;
3998 }
3999 else
4000 {
4001 /* None of the sections fitted inside the current segment.
4002 Change the current segment's physical address to match
4003 the LMA of the first section. */
4004 map->p_paddr = suggested_lma;
4005 }
4006
4007 /* Offset the segment physical address from the lma
4008 to allow for space taken up by elf headers. */
4009 if (map->includes_filehdr)
4010 map->p_paddr -= iehdr->e_ehsize;
4011
4012 if (map->includes_phdrs)
4013 {
4014 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4015
4016 /* iehdr->e_phnum is just an estimate of the number
4017 of program headers that we will need. Make a note
4018 here of the number we used and the segment we chose
4019 to hold these headers, so that we can adjust the
4020 offset when we know the correct value. */
4021 phdr_adjust_num = iehdr->e_phnum;
4022 phdr_adjust_seg = map;
4023 }
4024 }
4025
4026 /* Step Three: Loop over the sections again, this time assigning
4027 those that fit to the current segment and remvoing them from the
4028 sections array; but making sure not to leave large gaps. Once all
4029 possible sections have been assigned to the current segment it is
4030 added to the list of built segments and if sections still remain
4031 to be assigned, a new segment is constructed before repeating
4032 the loop. */
4033 isec = 0;
4034 do
4035 {
4036 map->count = 0;
4037 suggested_lma = 0;
4038
4039 /* Fill the current segment with sections that fit. */
4040 for (j = 0; j < section_count; j++)
4041 {
4042 section = sections[j];
4043
4044 if (section == NULL)
4045 continue;
4046
4047 output_section = section->output_section;
4048
4049 BFD_ASSERT (output_section != NULL);
4050
4051 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4052 || IS_COREFILE_NOTE (segment, section))
4053 {
4054 if (map->count == 0)
4055 {
4056 /* If the first section in a segment does not start at
4057 the beginning of the segment, then something is
4058 wrong. */
4059 if (output_section->lma !=
4060 (map->p_paddr
4061 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4062 + (map->includes_phdrs
4063 ? iehdr->e_phnum * iehdr->e_phentsize
4064 : 0)))
4065 abort ();
4066 }
4067 else
4068 {
4069 asection * prev_sec;
4070
4071 prev_sec = map->sections[map->count - 1];
4072
4073 /* If the gap between the end of the previous section
4074 and the start of this section is more than
4075 maxpagesize then we need to start a new segment. */
4076 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4077 < BFD_ALIGN (output_section->lma, maxpagesize))
4078 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4079 {
4080 if (suggested_lma == 0)
4081 suggested_lma = output_section->lma;
4082
4083 continue;
4084 }
4085 }
4086
4087 map->sections[map->count++] = output_section;
4088 ++isec;
4089 sections[j] = NULL;
4090 section->segment_mark = true;
4091 }
4092 else if (suggested_lma == 0)
4093 suggested_lma = output_section->lma;
4094 }
4095
4096 BFD_ASSERT (map->count > 0);
4097
4098 /* Add the current segment to the list of built segments. */
4099 *pointer_to_map = map;
4100 pointer_to_map = &map->next;
4101
4102 if (isec < section_count)
4103 {
4104 /* We still have not allocated all of the sections to
4105 segments. Create a new segment here, initialise it
4106 and carry on looping. */
4107 map = ((struct elf_segment_map *)
4108 bfd_alloc (obfd,
4109 (sizeof (struct elf_segment_map)
4110 + ((size_t) section_count - 1)
4111 * sizeof (asection *))));
4112 if (map == NULL)
4113 return false;
4114
4115 /* Initialise the fields of the segment map. Set the physical
4116 physical address to the LMA of the first section that has
4117 not yet been assigned. */
4118 map->next = NULL;
4119 map->p_type = segment->p_type;
4120 map->p_flags = segment->p_flags;
4121 map->p_flags_valid = 1;
4122 map->p_paddr = suggested_lma;
4123 map->p_paddr_valid = 1;
4124 map->includes_filehdr = 0;
4125 map->includes_phdrs = 0;
4126 }
4127 }
4128 while (isec < section_count);
4129
4130 free (sections);
4131 }
4132
4133 /* The Solaris linker creates program headers in which all the
4134 p_paddr fields are zero. When we try to objcopy or strip such a
4135 file, we get confused. Check for this case, and if we find it
4136 reset the p_paddr_valid fields. */
4137 for (map = map_first; map != NULL; map = map->next)
4138 if (map->p_paddr != 0)
4139 break;
4140 if (map == NULL)
4141 {
4142 for (map = map_first; map != NULL; map = map->next)
4143 map->p_paddr_valid = 0;
4144 }
4145
4146 elf_tdata (obfd)->segment_map = map_first;
4147
4148 /* If we had to estimate the number of program headers that were
4149 going to be needed, then check our estimate know and adjust
4150 the offset if necessary. */
4151 if (phdr_adjust_seg != NULL)
4152 {
4153 unsigned int count;
4154
4155 for (count = 0, map = map_first; map != NULL; map = map->next)
4156 count++;
4157
4158 if (count > phdr_adjust_num)
4159 phdr_adjust_seg->p_paddr
4160 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4161 }
4162
4163#if 0
4164 /* Final Step: Sort the segments into ascending order of physical
4165 address. */
4166 if (map_first != NULL)
4167 {
4168 struct elf_segment_map *prev;
4169
4170 prev = map_first;
4171 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4172 {
4173 /* Yes I know - its a bubble sort.... */
4174 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4175 {
4176 /* Swap map and map->next. */
4177 prev->next = map->next;
4178 map->next = map->next->next;
4179 prev->next->next = map;
4180
4181 /* Restart loop. */
4182 map = map_first;
4183 }
4184 }
4185 }
4186#endif
4187
4188#undef SEGMENT_END
4189#undef IS_CONTAINED_BY_VMA
4190#undef IS_CONTAINED_BY_LMA
4191#undef IS_COREFILE_NOTE
4192#undef IS_SOLARIS_PT_INTERP
4193#undef INCLUDE_SECTION_IN_SEGMENT
4194#undef SEGMENT_AFTER_SEGMENT
4195#undef SEGMENT_OVERLAPS
4196 return true;
4197}
4198
4199/* Copy private section information. This copies over the entsize
4200 field, and sometimes the info field. */
4201
4202boolean
4203_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4204 bfd *ibfd;
4205 asection *isec;
4206 bfd *obfd;
4207 asection *osec;
4208{
4209 Elf_Internal_Shdr *ihdr, *ohdr;
4210
4211 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4212 || obfd->xvec->flavour != bfd_target_elf_flavour)
4213 return true;
4214
4215 /* Copy over private BFD data if it has not already been copied.
4216 This must be done here, rather than in the copy_private_bfd_data
4217 entry point, because the latter is called after the section
4218 contents have been set, which means that the program headers have
4219 already been worked out. */
4220 if (elf_tdata (obfd)->segment_map == NULL
4221 && elf_tdata (ibfd)->phdr != NULL)
4222 {
4223 asection *s;
4224
4225 /* Only set up the segments if there are no more SEC_ALLOC
4226 sections. FIXME: This won't do the right thing if objcopy is
4227 used to remove the last SEC_ALLOC section, since objcopy
4228 won't call this routine in that case. */
4229 for (s = isec->next; s != NULL; s = s->next)
4230 if ((s->flags & SEC_ALLOC) != 0)
4231 break;
4232 if (s == NULL)
4233 {
4234 if (! copy_private_bfd_data (ibfd, obfd))
4235 return false;
4236 }
4237 }
4238
4239 ihdr = &elf_section_data (isec)->this_hdr;
4240 ohdr = &elf_section_data (osec)->this_hdr;
4241
4242 ohdr->sh_entsize = ihdr->sh_entsize;
4243
4244 if (ihdr->sh_type == SHT_SYMTAB
4245 || ihdr->sh_type == SHT_DYNSYM
4246 || ihdr->sh_type == SHT_GNU_verneed
4247 || ihdr->sh_type == SHT_GNU_verdef)
4248 ohdr->sh_info = ihdr->sh_info;
4249
4250 elf_section_data (osec)->use_rela_p
4251 = elf_section_data (isec)->use_rela_p;
4252
4253 return true;
4254}
4255
4256/* Copy private symbol information. If this symbol is in a section
4257 which we did not map into a BFD section, try to map the section
4258 index correctly. We use special macro definitions for the mapped
4259 section indices; these definitions are interpreted by the
4260 swap_out_syms function. */
4261
4262#define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4263#define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4264#define MAP_STRTAB (SHN_LORESERVE - 3)
4265#define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4266
4267boolean
4268_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4269 bfd *ibfd;
4270 asymbol *isymarg;
4271 bfd *obfd;
4272 asymbol *osymarg;
4273{
4274 elf_symbol_type *isym, *osym;
4275
4276 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4277 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4278 return true;
4279
4280 isym = elf_symbol_from (ibfd, isymarg);
4281 osym = elf_symbol_from (obfd, osymarg);
4282
4283 if (isym != NULL
4284 && osym != NULL
4285 && bfd_is_abs_section (isym->symbol.section))
4286 {
4287 unsigned int shndx;
4288
4289 shndx = isym->internal_elf_sym.st_shndx;
4290 if (shndx == elf_onesymtab (ibfd))
4291 shndx = MAP_ONESYMTAB;
4292 else if (shndx == elf_dynsymtab (ibfd))
4293 shndx = MAP_DYNSYMTAB;
4294 else if (shndx == elf_tdata (ibfd)->strtab_section)
4295 shndx = MAP_STRTAB;
4296 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4297 shndx = MAP_SHSTRTAB;
4298 osym->internal_elf_sym.st_shndx = shndx;
4299 }
4300
4301 return true;
4302}
4303
4304/* Swap out the symbols. */
4305
4306static boolean
4307swap_out_syms (abfd, sttp, relocatable_p)
4308 bfd *abfd;
4309 struct bfd_strtab_hash **sttp;
4310 int relocatable_p;
4311{
4312 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4313
4314 if (!elf_map_symbols (abfd))
4315 return false;
4316
4317 /* Dump out the symtabs. */
4318 {
4319 int symcount = bfd_get_symcount (abfd);
4320 asymbol **syms = bfd_get_outsymbols (abfd);
4321 struct bfd_strtab_hash *stt;
4322 Elf_Internal_Shdr *symtab_hdr;
4323 Elf_Internal_Shdr *symstrtab_hdr;
4324 char *outbound_syms;
4325 int idx;
4326
4327 stt = _bfd_elf_stringtab_init ();
4328 if (stt == NULL)
4329 return false;
4330
4331 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4332 symtab_hdr->sh_type = SHT_SYMTAB;
4333 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4334 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4335 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4336 symtab_hdr->sh_addralign = bed->s->file_align;
4337
4338 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4339 symstrtab_hdr->sh_type = SHT_STRTAB;
4340
4341 outbound_syms = bfd_alloc (abfd,
4342 (1 + symcount) * bed->s->sizeof_sym);
4343 if (outbound_syms == NULL)
4344 return false;
4345 symtab_hdr->contents = (PTR) outbound_syms;
4346
4347 /* now generate the data (for "contents") */
4348 {
4349 /* Fill in zeroth symbol and swap it out. */
4350 Elf_Internal_Sym sym;
4351 sym.st_name = 0;
4352 sym.st_value = 0;
4353 sym.st_size = 0;
4354 sym.st_info = 0;
4355 sym.st_other = 0;
4356 sym.st_shndx = SHN_UNDEF;
4357 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4358 outbound_syms += bed->s->sizeof_sym;
4359 }
4360 for (idx = 0; idx < symcount; idx++)
4361 {
4362 Elf_Internal_Sym sym;
4363 bfd_vma value = syms[idx]->value;
4364 elf_symbol_type *type_ptr;
4365 flagword flags = syms[idx]->flags;
4366 int type;
4367
4368 if ((flags & BSF_SECTION_SYM) != 0)
4369 {
4370 /* Section symbols have no name. */
4371 sym.st_name = 0;
4372 }
4373 else
4374 {
4375 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4376 syms[idx]->name,
4377 true, false);
4378 if (sym.st_name == (unsigned long) -1)
4379 return false;
4380 }
4381
4382 type_ptr = elf_symbol_from (abfd, syms[idx]);
4383
4384 if ((flags & BSF_SECTION_SYM) == 0
4385 && bfd_is_com_section (syms[idx]->section))
4386 {
4387 /* ELF common symbols put the alignment into the `value' field,
4388 and the size into the `size' field. This is backwards from
4389 how BFD handles it, so reverse it here. */
4390 sym.st_size = value;
4391 if (type_ptr == NULL
4392 || type_ptr->internal_elf_sym.st_value == 0)
4393 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4394 else
4395 sym.st_value = type_ptr->internal_elf_sym.st_value;
4396 sym.st_shndx = _bfd_elf_section_from_bfd_section
4397 (abfd, syms[idx]->section);
4398 }
4399 else
4400 {
4401 asection *sec = syms[idx]->section;
4402 int shndx;
4403
4404 if (sec->output_section)
4405 {
4406 value += sec->output_offset;
4407 sec = sec->output_section;
4408 }
4409 /* Don't add in the section vma for relocatable output. */
4410 if (! relocatable_p)
4411 value += sec->vma;
4412 sym.st_value = value;
4413 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4414
4415 if (bfd_is_abs_section (sec)
4416 && type_ptr != NULL
4417 && type_ptr->internal_elf_sym.st_shndx != 0)
4418 {
4419 /* This symbol is in a real ELF section which we did
4420 not create as a BFD section. Undo the mapping done
4421 by copy_private_symbol_data. */
4422 shndx = type_ptr->internal_elf_sym.st_shndx;
4423 switch (shndx)
4424 {
4425 case MAP_ONESYMTAB:
4426 shndx = elf_onesymtab (abfd);
4427 break;
4428 case MAP_DYNSYMTAB:
4429 shndx = elf_dynsymtab (abfd);
4430 break;
4431 case MAP_STRTAB:
4432 shndx = elf_tdata (abfd)->strtab_section;
4433 break;
4434 case MAP_SHSTRTAB:
4435 shndx = elf_tdata (abfd)->shstrtab_section;
4436 break;
4437 default:
4438 break;
4439 }
4440 }
4441 else
4442 {
4443 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4444
4445 if (shndx == -1)
4446 {
4447 asection *sec2;
4448
4449 /* Writing this would be a hell of a lot easier if
4450 we had some decent documentation on bfd, and
4451 knew what to expect of the library, and what to
4452 demand of applications. For example, it
4453 appears that `objcopy' might not set the
4454 section of a symbol to be a section that is
4455 actually in the output file. */
4456 sec2 = bfd_get_section_by_name (abfd, sec->name);
4457 BFD_ASSERT (sec2 != 0);
4458 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4459 BFD_ASSERT (shndx != -1);
4460 }
4461 }
4462
4463 sym.st_shndx = shndx;
4464 }
4465
4466 if ((flags & BSF_FUNCTION) != 0)
4467 type = STT_FUNC;
4468 else if ((flags & BSF_OBJECT) != 0)
4469 type = STT_OBJECT;
4470 else
4471 type = STT_NOTYPE;
4472
4473 /* Processor-specific types */
4474 if (type_ptr != NULL
4475 && bed->elf_backend_get_symbol_type)
4476 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4477
4478 if (flags & BSF_SECTION_SYM)
4479 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4480 else if (bfd_is_com_section (syms[idx]->section))
4481 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4482 else if (bfd_is_und_section (syms[idx]->section))
4483 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4484 ? STB_WEAK
4485 : STB_GLOBAL),
4486 type);
4487 else if (flags & BSF_FILE)
4488 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4489 else
4490 {
4491 int bind = STB_LOCAL;
4492
4493 if (flags & BSF_LOCAL)
4494 bind = STB_LOCAL;
4495 else if (flags & BSF_WEAK)
4496 bind = STB_WEAK;
4497 else if (flags & BSF_GLOBAL)
4498 bind = STB_GLOBAL;
4499
4500 sym.st_info = ELF_ST_INFO (bind, type);
4501 }
4502
4503 if (type_ptr != NULL)
4504 sym.st_other = type_ptr->internal_elf_sym.st_other;
4505 else
4506 sym.st_other = 0;
4507
4508 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4509 outbound_syms += bed->s->sizeof_sym;
4510 }
4511
4512 *sttp = stt;
4513 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4514 symstrtab_hdr->sh_type = SHT_STRTAB;
4515
4516 symstrtab_hdr->sh_flags = 0;
4517 symstrtab_hdr->sh_addr = 0;
4518 symstrtab_hdr->sh_entsize = 0;
4519 symstrtab_hdr->sh_link = 0;
4520 symstrtab_hdr->sh_info = 0;
4521 symstrtab_hdr->sh_addralign = 1;
4522 }
4523
4524 return true;
4525}
4526
4527/* Return the number of bytes required to hold the symtab vector.
4528
4529 Note that we base it on the count plus 1, since we will null terminate
4530 the vector allocated based on this size. However, the ELF symbol table
4531 always has a dummy entry as symbol #0, so it ends up even. */
4532
4533long
4534_bfd_elf_get_symtab_upper_bound (abfd)
4535 bfd *abfd;
4536{
4537 long symcount;
4538 long symtab_size;
4539 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4540
4541 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4542 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4543
4544 return symtab_size;
4545}
4546
4547long
4548_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4549 bfd *abfd;
4550{
4551 long symcount;
4552 long symtab_size;
4553 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4554
4555 if (elf_dynsymtab (abfd) == 0)
4556 {
4557 bfd_set_error (bfd_error_invalid_operation);
4558 return -1;
4559 }
4560
4561 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4562 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4563
4564 return symtab_size;
4565}
4566
4567long
4568_bfd_elf_get_reloc_upper_bound (abfd, asect)
4569 bfd *abfd ATTRIBUTE_UNUSED;
4570 sec_ptr asect;
4571{
4572 return (asect->reloc_count + 1) * sizeof (arelent *);
4573}
4574
4575/* Canonicalize the relocs. */
4576
4577long
4578_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4579 bfd *abfd;
4580 sec_ptr section;
4581 arelent **relptr;
4582 asymbol **symbols;
4583{
4584 arelent *tblptr;
4585 unsigned int i;
4586
4587 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4588 section,
4589 symbols,
4590 false))
4591 return -1;
4592
4593 tblptr = section->relocation;
4594 for (i = 0; i < section->reloc_count; i++)
4595 *relptr++ = tblptr++;
4596
4597 *relptr = NULL;
4598
4599 return section->reloc_count;
4600}
4601
4602long
4603_bfd_elf_get_symtab (abfd, alocation)
4604 bfd *abfd;
4605 asymbol **alocation;
4606{
4607 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4608 (abfd, alocation, false);
4609
4610 if (symcount >= 0)
4611 bfd_get_symcount (abfd) = symcount;
4612 return symcount;
4613}
4614
4615long
4616_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4617 bfd *abfd;
4618 asymbol **alocation;
4619{
4620 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4621 (abfd, alocation, true);
4622}
4623
4624/* Return the size required for the dynamic reloc entries. Any
4625 section that was actually installed in the BFD, and has type
4626 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4627 considered to be a dynamic reloc section. */
4628
4629long
4630_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4631 bfd *abfd;
4632{
4633 long ret;
4634 asection *s;
4635
4636 if (elf_dynsymtab (abfd) == 0)
4637 {
4638 bfd_set_error (bfd_error_invalid_operation);
4639 return -1;
4640 }
4641
4642 ret = sizeof (arelent *);
4643 for (s = abfd->sections; s != NULL; s = s->next)
4644 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4645 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4646 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4647 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4648 * sizeof (arelent *));
4649
4650 return ret;
4651}
4652
4653/* Canonicalize the dynamic relocation entries. Note that we return
4654 the dynamic relocations as a single block, although they are
4655 actually associated with particular sections; the interface, which
4656 was designed for SunOS style shared libraries, expects that there
4657 is only one set of dynamic relocs. Any section that was actually
4658 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4659 the dynamic symbol table, is considered to be a dynamic reloc
4660 section. */
4661
4662long
4663_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4664 bfd *abfd;
4665 arelent **storage;
4666 asymbol **syms;
4667{
4668 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4669 asection *s;
4670 long ret;
4671
4672 if (elf_dynsymtab (abfd) == 0)
4673 {
4674 bfd_set_error (bfd_error_invalid_operation);
4675 return -1;
4676 }
4677
4678 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4679 ret = 0;
4680 for (s = abfd->sections; s != NULL; s = s->next)
4681 {
4682 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4683 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4684 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4685 {
4686 arelent *p;
4687 long count, i;
4688
4689 if (! (*slurp_relocs) (abfd, s, syms, true))
4690 return -1;
4691 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4692 p = s->relocation;
4693 for (i = 0; i < count; i++)
4694 *storage++ = p++;
4695 ret += count;
4696 }
4697 }
4698
4699 *storage = NULL;
4700
4701 return ret;
4702}
4703
4704
4705/* Read in the version information. */
4706
4707boolean
4708_bfd_elf_slurp_version_tables (abfd)
4709 bfd *abfd;
4710{
4711 bfd_byte *contents = NULL;
4712
4713 if (elf_dynverdef (abfd) != 0)
4714 {
4715 Elf_Internal_Shdr *hdr;
4716 Elf_External_Verdef *everdef;
4717 Elf_Internal_Verdef *iverdef;
4718 Elf_Internal_Verdef *iverdefarr;
4719 Elf_Internal_Verdef iverdefmem;
4720 unsigned int i;
4721 unsigned int maxidx;
4722
4723 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4724
4725 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4726 if (contents == NULL)
4727 goto error_return;
4728 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4729 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4730 goto error_return;
4731
4732 /* We know the number of entries in the section but not the maximum
4733 index. Therefore we have to run through all entries and find
4734 the maximum. */
4735 everdef = (Elf_External_Verdef *) contents;
4736 maxidx = 0;
4737 for (i = 0; i < hdr->sh_info; ++i)
4738 {
4739 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4740
4741 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
4742 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
4743
4744 everdef = ((Elf_External_Verdef *)
4745 ((bfd_byte *) everdef + iverdefmem.vd_next));
4746 }
4747
4748 elf_tdata (abfd)->verdef =
4749 ((Elf_Internal_Verdef *)
4750 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4751 if (elf_tdata (abfd)->verdef == NULL)
4752 goto error_return;
4753
4754 elf_tdata (abfd)->cverdefs = maxidx;
4755
4756 everdef = (Elf_External_Verdef *) contents;
4757 iverdefarr = elf_tdata (abfd)->verdef;
4758 for (i = 0; i < hdr->sh_info; i++)
4759 {
4760 Elf_External_Verdaux *everdaux;
4761 Elf_Internal_Verdaux *iverdaux;
4762 unsigned int j;
4763
4764 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4765
4766 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4767 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4768
4769 iverdef->vd_bfd = abfd;
4770
4771 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4772 bfd_alloc (abfd,
4773 (iverdef->vd_cnt
4774 * sizeof (Elf_Internal_Verdaux))));
4775 if (iverdef->vd_auxptr == NULL)
4776 goto error_return;
4777
4778 everdaux = ((Elf_External_Verdaux *)
4779 ((bfd_byte *) everdef + iverdef->vd_aux));
4780 iverdaux = iverdef->vd_auxptr;
4781 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4782 {
4783 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4784
4785 iverdaux->vda_nodename =
4786 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4787 iverdaux->vda_name);
4788 if (iverdaux->vda_nodename == NULL)
4789 goto error_return;
4790
4791 if (j + 1 < iverdef->vd_cnt)
4792 iverdaux->vda_nextptr = iverdaux + 1;
4793 else
4794 iverdaux->vda_nextptr = NULL;
4795
4796 everdaux = ((Elf_External_Verdaux *)
4797 ((bfd_byte *) everdaux + iverdaux->vda_next));
4798 }
4799
4800 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4801
4802 if (i + 1 < hdr->sh_info)
4803 iverdef->vd_nextdef = iverdef + 1;
4804 else
4805 iverdef->vd_nextdef = NULL;
4806
4807 everdef = ((Elf_External_Verdef *)
4808 ((bfd_byte *) everdef + iverdef->vd_next));
4809 }
4810
4811 free (contents);
4812 contents = NULL;
4813 }
4814
4815 if (elf_dynverref (abfd) != 0)
4816 {
4817 Elf_Internal_Shdr *hdr;
4818 Elf_External_Verneed *everneed;
4819 Elf_Internal_Verneed *iverneed;
4820 unsigned int i;
4821
4822 hdr = &elf_tdata (abfd)->dynverref_hdr;
4823
4824 elf_tdata (abfd)->verref =
4825 ((Elf_Internal_Verneed *)
4826 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4827 if (elf_tdata (abfd)->verref == NULL)
4828 goto error_return;
4829
4830 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4831
4832 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4833 if (contents == NULL)
4834 goto error_return;
4835 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4836 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4837 goto error_return;
4838
4839 everneed = (Elf_External_Verneed *) contents;
4840 iverneed = elf_tdata (abfd)->verref;
4841 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4842 {
4843 Elf_External_Vernaux *evernaux;
4844 Elf_Internal_Vernaux *ivernaux;
4845 unsigned int j;
4846
4847 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4848
4849 iverneed->vn_bfd = abfd;
4850
4851 iverneed->vn_filename =
4852 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4853 iverneed->vn_file);
4854 if (iverneed->vn_filename == NULL)
4855 goto error_return;
4856
4857 iverneed->vn_auxptr =
4858 ((Elf_Internal_Vernaux *)
4859 bfd_alloc (abfd,
4860 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4861
4862 evernaux = ((Elf_External_Vernaux *)
4863 ((bfd_byte *) everneed + iverneed->vn_aux));
4864 ivernaux = iverneed->vn_auxptr;
4865 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4866 {
4867 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4868
4869 ivernaux->vna_nodename =
4870 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4871 ivernaux->vna_name);
4872 if (ivernaux->vna_nodename == NULL)
4873 goto error_return;
4874
4875 if (j + 1 < iverneed->vn_cnt)
4876 ivernaux->vna_nextptr = ivernaux + 1;
4877 else
4878 ivernaux->vna_nextptr = NULL;
4879
4880 evernaux = ((Elf_External_Vernaux *)
4881 ((bfd_byte *) evernaux + ivernaux->vna_next));
4882 }
4883
4884 if (i + 1 < hdr->sh_info)
4885 iverneed->vn_nextref = iverneed + 1;
4886 else
4887 iverneed->vn_nextref = NULL;
4888
4889 everneed = ((Elf_External_Verneed *)
4890 ((bfd_byte *) everneed + iverneed->vn_next));
4891 }
4892
4893 free (contents);
4894 contents = NULL;
4895 }
4896
4897 return true;
4898
4899 error_return:
4900 if (contents == NULL)
4901 free (contents);
4902 return false;
4903}
4904
4905
4906asymbol *
4907_bfd_elf_make_empty_symbol (abfd)
4908 bfd *abfd;
4909{
4910 elf_symbol_type *newsym;
4911
4912 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4913 if (!newsym)
4914 return NULL;
4915 else
4916 {
4917 newsym->symbol.the_bfd = abfd;
4918 return &newsym->symbol;
4919 }
4920}
4921
4922void
4923_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4924 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4925 asymbol *symbol;
4926 symbol_info *ret;
4927{
4928 bfd_symbol_info (symbol, ret);
4929}
4930
4931/* Return whether a symbol name implies a local symbol. Most targets
4932 use this function for the is_local_label_name entry point, but some
4933 override it. */
4934
4935boolean
4936_bfd_elf_is_local_label_name (abfd, name)
4937 bfd *abfd ATTRIBUTE_UNUSED;
4938 const char *name;
4939{
4940 /* Normal local symbols start with ``.L''. */
4941 if (name[0] == '.' && name[1] == 'L')
4942 return true;
4943
4944 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4945 DWARF debugging symbols starting with ``..''. */
4946 if (name[0] == '.' && name[1] == '.')
4947 return true;
4948
4949 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4950 emitting DWARF debugging output. I suspect this is actually a
4951 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4952 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4953 underscore to be emitted on some ELF targets). For ease of use,
4954 we treat such symbols as local. */
4955 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4956 return true;
4957
4958 return false;
4959}
4960
4961alent *
4962_bfd_elf_get_lineno (ignore_abfd, symbol)
4963 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4964 asymbol *symbol ATTRIBUTE_UNUSED;
4965{
4966 abort ();
4967 return NULL;
4968}
4969
4970boolean
4971_bfd_elf_set_arch_mach (abfd, arch, machine)
4972 bfd *abfd;
4973 enum bfd_architecture arch;
4974 unsigned long machine;
4975{
4976 /* If this isn't the right architecture for this backend, and this
4977 isn't the generic backend, fail. */
4978 if (arch != get_elf_backend_data (abfd)->arch
4979 && arch != bfd_arch_unknown
4980 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4981 return false;
4982
4983 return bfd_default_set_arch_mach (abfd, arch, machine);
4984}
4985
4986/* Find the function to a particular section and offset,
4987 for error reporting. */
4988
4989static boolean
4990elf_find_function (abfd, section, symbols, offset,
4991 filename_ptr, functionname_ptr)
4992 bfd *abfd ATTRIBUTE_UNUSED;
4993 asection *section;
4994 asymbol **symbols;
4995 bfd_vma offset;
4996 const char **filename_ptr;
4997 const char **functionname_ptr;
4998{
4999 const char *filename;
5000 asymbol *func;
5001 bfd_vma low_func;
5002 asymbol **p;
5003
5004 filename = NULL;
5005 func = NULL;
5006 low_func = 0;
5007
5008 for (p = symbols; *p != NULL; p++)
5009 {
5010 elf_symbol_type *q;
5011
5012 q = (elf_symbol_type *) *p;
5013
5014 if (bfd_get_section (&q->symbol) != section)
5015 continue;
5016
5017 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5018 {
5019 default:
5020 break;
5021 case STT_FILE:
5022 filename = bfd_asymbol_name (&q->symbol);
5023 break;
5024 case STT_NOTYPE:
5025 case STT_FUNC:
5026 if (q->symbol.section == section
5027 && q->symbol.value >= low_func
5028 && q->symbol.value <= offset)
5029 {
5030 func = (asymbol *) q;
5031 low_func = q->symbol.value;
5032 }
5033 break;
5034 }
5035 }
5036
5037 if (func == NULL)
5038 return false;
5039
5040 if (filename_ptr)
5041 *filename_ptr = filename;
5042 if (functionname_ptr)
5043 *functionname_ptr = bfd_asymbol_name (func);
5044
5045 return true;
5046}
5047
5048/* Find the nearest line to a particular section and offset,
5049 for error reporting. */
5050
5051boolean
5052_bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5053 filename_ptr, functionname_ptr, line_ptr)
5054 bfd *abfd;
5055 asection *section;
5056 asymbol **symbols;
5057 bfd_vma offset;
5058 const char **filename_ptr;
5059 const char **functionname_ptr;
5060 unsigned int *line_ptr;
5061{
5062 boolean found;
5063
5064 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5065 filename_ptr, functionname_ptr,
5066 line_ptr))
5067 {
5068 if (!*functionname_ptr)
5069 elf_find_function (abfd, section, symbols, offset,
5070 *filename_ptr ? NULL : filename_ptr,
5071 functionname_ptr);
5072
5073 return true;
5074 }
5075
5076 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5077 filename_ptr, functionname_ptr,
5078 line_ptr, 0,
5079 &elf_tdata (abfd)->dwarf2_find_line_info))
5080 {
5081 if (!*functionname_ptr)
5082 elf_find_function (abfd, section, symbols, offset,
5083 *filename_ptr ? NULL : filename_ptr,
5084 functionname_ptr);
5085
5086 return true;
5087 }
5088
5089 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5090 &found, filename_ptr,
5091 functionname_ptr, line_ptr,
5092 &elf_tdata (abfd)->line_info))
5093 return false;
5094 if (found)
5095 return true;
5096
5097 if (symbols == NULL)
5098 return false;
5099
5100 if (! elf_find_function (abfd, section, symbols, offset,
5101 filename_ptr, functionname_ptr))
5102 return false;
5103
5104 *line_ptr = 0;
5105 return true;
5106}
5107
5108int
5109_bfd_elf_sizeof_headers (abfd, reloc)
5110 bfd *abfd;
5111 boolean reloc;
5112{
5113 int ret;
5114
5115 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5116 if (! reloc)
5117 ret += get_program_header_size (abfd);
5118 return ret;
5119}
5120
5121boolean
5122_bfd_elf_set_section_contents (abfd, section, location, offset, count)
5123 bfd *abfd;
5124 sec_ptr section;
5125 PTR location;
5126 file_ptr offset;
5127 bfd_size_type count;
5128{
5129 Elf_Internal_Shdr *hdr;
5130
5131 if (! abfd->output_has_begun
5132 && ! _bfd_elf_compute_section_file_positions
5133 (abfd, (struct bfd_link_info *) NULL))
5134 return false;
5135
5136 hdr = &elf_section_data (section)->this_hdr;
5137
5138 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5139 return false;
5140 if (bfd_write (location, 1, count, abfd) != count)
5141 return false;
5142
5143 return true;
5144}
5145
5146void
5147_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5148 bfd *abfd ATTRIBUTE_UNUSED;
5149 arelent *cache_ptr ATTRIBUTE_UNUSED;
5150 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5151{
5152 abort ();
5153}
5154
5155#if 0
5156void
5157_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5158 bfd *abfd;
5159 arelent *cache_ptr;
5160 Elf_Internal_Rel *dst;
5161{
5162 abort ();
5163}
5164#endif
5165
5166/* Try to convert a non-ELF reloc into an ELF one. */
5167
5168boolean
5169_bfd_elf_validate_reloc (abfd, areloc)
5170 bfd *abfd;
5171 arelent *areloc;
5172{
5173 /* Check whether we really have an ELF howto. */
5174
5175 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5176 {
5177 bfd_reloc_code_real_type code;
5178 reloc_howto_type *howto;
5179
5180 /* Alien reloc: Try to determine its type to replace it with an
5181 equivalent ELF reloc. */
5182
5183 if (areloc->howto->pc_relative)
5184 {
5185 switch (areloc->howto->bitsize)
5186 {
5187 case 8:
5188 code = BFD_RELOC_8_PCREL;
5189 break;
5190 case 12:
5191 code = BFD_RELOC_12_PCREL;
5192 break;
5193 case 16:
5194 code = BFD_RELOC_16_PCREL;
5195 break;
5196 case 24:
5197 code = BFD_RELOC_24_PCREL;
5198 break;
5199 case 32:
5200 code = BFD_RELOC_32_PCREL;
5201 break;
5202 case 64:
5203 code = BFD_RELOC_64_PCREL;
5204 break;
5205 default:
5206 goto fail;
5207 }
5208
5209 howto = bfd_reloc_type_lookup (abfd, code);
5210
5211 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5212 {
5213 if (howto->pcrel_offset)
5214 areloc->addend += areloc->address;
5215 else
5216 areloc->addend -= areloc->address; /* addend is unsigned!! */
5217 }
5218 }
5219 else
5220 {
5221 switch (areloc->howto->bitsize)
5222 {
5223 case 8:
5224 code = BFD_RELOC_8;
5225 break;
5226 case 14:
5227 code = BFD_RELOC_14;
5228 break;
5229 case 16:
5230 code = BFD_RELOC_16;
5231 break;
5232 case 26:
5233 code = BFD_RELOC_26;
5234 break;
5235 case 32:
5236 code = BFD_RELOC_32;
5237 break;
5238 case 64:
5239 code = BFD_RELOC_64;
5240 break;
5241 default:
5242 goto fail;
5243 }
5244
5245 howto = bfd_reloc_type_lookup (abfd, code);
5246 }
5247
5248 if (howto)
5249 areloc->howto = howto;
5250 else
5251 goto fail;
5252 }
5253
5254 return true;
5255
5256 fail:
5257 (*_bfd_error_handler)
5258 (_("%s: unsupported relocation type %s"),
5259 bfd_get_filename (abfd), areloc->howto->name);
5260 bfd_set_error (bfd_error_bad_value);
5261 return false;
5262}
5263
5264boolean
5265_bfd_elf_close_and_cleanup (abfd)
5266 bfd *abfd;
5267{
5268 if (bfd_get_format (abfd) == bfd_object)
5269 {
5270 if (elf_shstrtab (abfd) != NULL)
5271 _bfd_stringtab_free (elf_shstrtab (abfd));
5272 }
5273
5274 return _bfd_generic_close_and_cleanup (abfd);
5275}
5276
5277/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5278 in the relocation's offset. Thus we cannot allow any sort of sanity
5279 range-checking to interfere. There is nothing else to do in processing
5280 this reloc. */
5281
5282bfd_reloc_status_type
5283_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5284 bfd *abfd ATTRIBUTE_UNUSED;
5285 arelent *re ATTRIBUTE_UNUSED;
5286 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5287 PTR data ATTRIBUTE_UNUSED;
5288 asection *is ATTRIBUTE_UNUSED;
5289 bfd *obfd ATTRIBUTE_UNUSED;
5290 char **errmsg ATTRIBUTE_UNUSED;
5291{
5292 return bfd_reloc_ok;
5293}
5294
5295
5296/* Elf core file support. Much of this only works on native
5297 toolchains, since we rely on knowing the
5298 machine-dependent procfs structure in order to pick
5299 out details about the corefile. */
5300
5301#ifdef HAVE_SYS_PROCFS_H
5302# include <sys/procfs.h>
5303#endif
5304
5305/* Define offsetof for those systems which lack it. */
5306
5307#ifndef offsetof
5308# define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5309#endif
5310
5311/* FIXME: this is kinda wrong, but it's what gdb wants. */
5312
5313static int
5314elfcore_make_pid (abfd)
5315 bfd *abfd;
5316{
5317 return ((elf_tdata (abfd)->core_lwpid << 16)
5318 + (elf_tdata (abfd)->core_pid));
5319}
5320
5321/* If there isn't a section called NAME, make one, using
5322 data from SECT. Note, this function will generate a
5323 reference to NAME, so you shouldn't deallocate or
5324 overwrite it. */
5325
5326static boolean
5327elfcore_maybe_make_sect (abfd, name, sect)
5328 bfd *abfd;
5329 char *name;
5330 asection *sect;
5331{
5332 asection *sect2;
5333
5334 if (bfd_get_section_by_name (abfd, name) != NULL)
5335 return true;
5336
5337 sect2 = bfd_make_section (abfd, name);
5338 if (sect2 == NULL)
5339 return false;
5340
5341 sect2->_raw_size = sect->_raw_size;
5342 sect2->filepos = sect->filepos;
5343 sect2->flags = sect->flags;
5344 sect2->alignment_power = sect->alignment_power;
5345 return true;
5346}
5347
5348/* prstatus_t exists on:
5349 solaris 2.5+
5350 linux 2.[01] + glibc
5351 unixware 4.2
5352*/
5353
5354#if defined (HAVE_PRSTATUS_T)
5355static boolean
5356elfcore_grok_prstatus (abfd, note)
5357 bfd *abfd;
5358 Elf_Internal_Note *note;
5359{
5360 char buf[100];
5361 char *name;
5362 asection *sect;
5363 int raw_size;
5364 int offset;
5365
5366 if (note->descsz == sizeof (prstatus_t))
5367 {
5368 prstatus_t prstat;
5369
5370 raw_size = sizeof (prstat.pr_reg);
5371 offset = offsetof (prstatus_t, pr_reg);
5372 memcpy (&prstat, note->descdata, sizeof (prstat));
5373
5374 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5375 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5376
5377 /* pr_who exists on:
5378 solaris 2.5+
5379 unixware 4.2
5380 pr_who doesn't exist on:
5381 linux 2.[01]
5382 */
5383#if defined (HAVE_PRSTATUS_T_PR_WHO)
5384 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5385#endif
5386 }
5387#if defined (HAVE_PRSTATUS32_T)
5388 else if (note->descsz == sizeof (prstatus32_t))
5389 {
5390 /* 64-bit host, 32-bit corefile */
5391 prstatus32_t prstat;
5392
5393 raw_size = sizeof (prstat.pr_reg);
5394 offset = offsetof (prstatus32_t, pr_reg);
5395 memcpy (&prstat, note->descdata, sizeof (prstat));
5396
5397 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5398 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5399
5400 /* pr_who exists on:
5401 solaris 2.5+
5402 unixware 4.2
5403 pr_who doesn't exist on:
5404 linux 2.[01]
5405 */
5406#if defined (HAVE_PRSTATUS32_T_PR_WHO)
5407 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5408#endif
5409 }
5410#endif /* HAVE_PRSTATUS32_T */
5411 else
5412 {
5413 /* Fail - we don't know how to handle any other
5414 note size (ie. data object type). */
5415 return true;
5416 }
5417
5418 /* Make a ".reg/999" section. */
5419
5420 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5421 name = bfd_alloc (abfd, strlen (buf) + 1);
5422 if (name == NULL)
5423 return false;
5424 strcpy (name, buf);
5425
5426 sect = bfd_make_section (abfd, name);
5427 if (sect == NULL)
5428 return false;
5429
5430 sect->_raw_size = raw_size;
5431 sect->filepos = note->descpos + offset;
5432
5433 sect->flags = SEC_HAS_CONTENTS;
5434 sect->alignment_power = 2;
5435
5436 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5437 return false;
5438
5439 return true;
5440}
5441#endif /* defined (HAVE_PRSTATUS_T) */
5442
5443/* Create a pseudosection containing the exact contents of NOTE. This
5444 actually creates up to two pseudosections:
5445 - For the single-threaded case, a section named NAME, unless
5446 such a section already exists.
5447 - For the multi-threaded case, a section named "NAME/PID", where
5448 PID is elfcore_make_pid (abfd).
5449 Both pseudosections have identical contents: the contents of NOTE. */
5450
5451static boolean
5452elfcore_make_note_pseudosection (abfd, name, note)
5453 bfd *abfd;
5454 char *name;
5455 Elf_Internal_Note *note;
5456{
5457 char buf[100];
5458 char *threaded_name;
5459 asection *sect;
5460
5461 /* Build the section name. */
5462
5463 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5464 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5465 if (threaded_name == NULL)
5466 return false;
5467 strcpy (threaded_name, buf);
5468
5469 sect = bfd_make_section (abfd, threaded_name);
5470 if (sect == NULL)
5471 return false;
5472 sect->_raw_size = note->descsz;
5473 sect->filepos = note->descpos;
5474 sect->flags = SEC_HAS_CONTENTS;
5475 sect->alignment_power = 2;
5476
5477 if (! elfcore_maybe_make_sect (abfd, name, sect))
5478 return false;
5479
5480 return true;
5481}
5482
5483/* There isn't a consistent prfpregset_t across platforms,
5484 but it doesn't matter, because we don't have to pick this
5485 data structure apart. */
5486
5487static boolean
5488elfcore_grok_prfpreg (abfd, note)
5489 bfd *abfd;
5490 Elf_Internal_Note *note;
5491{
5492 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5493}
5494
5495/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5496 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5497 literally. */
5498
5499static boolean
5500elfcore_grok_prxfpreg (abfd, note)
5501 bfd *abfd;
5502 Elf_Internal_Note *note;
5503{
5504 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5505}
5506
5507#if defined (HAVE_PRPSINFO_T)
5508typedef prpsinfo_t elfcore_psinfo_t;
5509#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5510typedef prpsinfo32_t elfcore_psinfo32_t;
5511#endif
5512#endif
5513
5514#if defined (HAVE_PSINFO_T)
5515typedef psinfo_t elfcore_psinfo_t;
5516#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5517typedef psinfo32_t elfcore_psinfo32_t;
5518#endif
5519#endif
5520
5521#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5522
5523/* return a malloc'ed copy of a string at START which is at
5524 most MAX bytes long, possibly without a terminating '\0'.
5525 the copy will always have a terminating '\0'. */
5526
5527static char*
5528elfcore_strndup (abfd, start, max)
5529 bfd *abfd;
5530 char *start;
5531 int max;
5532{
5533 char *dup;
5534 char *end = memchr (start, '\0', max);
5535 int len;
5536
5537 if (end == NULL)
5538 len = max;
5539 else
5540 len = end - start;
5541
5542 dup = bfd_alloc (abfd, len + 1);
5543 if (dup == NULL)
5544 return NULL;
5545
5546 memcpy (dup, start, len);
5547 dup[len] = '\0';
5548
5549 return dup;
5550}
5551
5552static boolean
5553elfcore_grok_psinfo (abfd, note)
5554 bfd *abfd;
5555 Elf_Internal_Note *note;
5556{
5557 if (note->descsz == sizeof (elfcore_psinfo_t))
5558 {
5559 elfcore_psinfo_t psinfo;
5560
5561 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5562
5563 elf_tdata (abfd)->core_program
5564 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5565
5566 elf_tdata (abfd)->core_command
5567 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5568 }
5569#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5570 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5571 {
5572 /* 64-bit host, 32-bit corefile */
5573 elfcore_psinfo32_t psinfo;
5574
5575 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5576
5577 elf_tdata (abfd)->core_program
5578 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5579
5580 elf_tdata (abfd)->core_command
5581 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5582 }
5583#endif
5584
5585 else
5586 {
5587 /* Fail - we don't know how to handle any other
5588 note size (ie. data object type). */
5589 return true;
5590 }
5591
5592 /* Note that for some reason, a spurious space is tacked
5593 onto the end of the args in some (at least one anyway)
5594 implementations, so strip it off if it exists. */
5595
5596 {
5597 char *command = elf_tdata (abfd)->core_command;
5598 int n = strlen (command);
5599
5600 if (0 < n && command[n - 1] == ' ')
5601 command[n - 1] = '\0';
5602 }
5603
5604 return true;
5605}
5606#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5607
5608#if defined (HAVE_PSTATUS_T)
5609static boolean
5610elfcore_grok_pstatus (abfd, note)
5611 bfd *abfd;
5612 Elf_Internal_Note *note;
5613{
5614 if (note->descsz == sizeof (pstatus_t)
5615#if defined (HAVE_PXSTATUS_T)
5616 || note->descsz == sizeof (pxstatus_t)
5617#endif
5618 )
5619 {
5620 pstatus_t pstat;
5621
5622 memcpy (&pstat, note->descdata, sizeof (pstat));
5623
5624 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5625 }
5626#if defined (HAVE_PSTATUS32_T)
5627 else if (note->descsz == sizeof (pstatus32_t))
5628 {
5629 /* 64-bit host, 32-bit corefile */
5630 pstatus32_t pstat;
5631
5632 memcpy (&pstat, note->descdata, sizeof (pstat));
5633
5634 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5635 }
5636#endif
5637 /* Could grab some more details from the "representative"
5638 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5639 NT_LWPSTATUS note, presumably. */
5640
5641 return true;
5642}
5643#endif /* defined (HAVE_PSTATUS_T) */
5644
5645#if defined (HAVE_LWPSTATUS_T)
5646static boolean
5647elfcore_grok_lwpstatus (abfd, note)
5648 bfd *abfd;
5649 Elf_Internal_Note *note;
5650{
5651 lwpstatus_t lwpstat;
5652 char buf[100];
5653 char *name;
5654 asection *sect;
5655
5656 if (note->descsz != sizeof (lwpstat)
5657#if defined (HAVE_LWPXSTATUS_T)
5658 && note->descsz != sizeof (lwpxstatus_t)
5659#endif
5660 )
5661 return true;
5662
5663 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5664
5665 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5666 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5667
5668 /* Make a ".reg/999" section. */
5669
5670 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5671 name = bfd_alloc (abfd, strlen (buf) + 1);
5672 if (name == NULL)
5673 return false;
5674 strcpy (name, buf);
5675
5676 sect = bfd_make_section (abfd, name);
5677 if (sect == NULL)
5678 return false;
5679
5680#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5681 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5682 sect->filepos = note->descpos
5683 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5684#endif
5685
5686#if defined (HAVE_LWPSTATUS_T_PR_REG)
5687 sect->_raw_size = sizeof (lwpstat.pr_reg);
5688 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5689#endif
5690
5691 sect->flags = SEC_HAS_CONTENTS;
5692 sect->alignment_power = 2;
5693
5694 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5695 return false;
5696
5697 /* Make a ".reg2/999" section */
5698
5699 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5700 name = bfd_alloc (abfd, strlen (buf) + 1);
5701 if (name == NULL)
5702 return false;
5703 strcpy (name, buf);
5704
5705 sect = bfd_make_section (abfd, name);
5706 if (sect == NULL)
5707 return false;
5708
5709#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5710 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5711 sect->filepos = note->descpos
5712 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5713#endif
5714
5715#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5716 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5717 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5718#endif
5719
5720 sect->flags = SEC_HAS_CONTENTS;
5721 sect->alignment_power = 2;
5722
5723 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5724 return false;
5725
5726 return true;
5727}
5728#endif /* defined (HAVE_LWPSTATUS_T) */
5729
5730#if defined (HAVE_WIN32_PSTATUS_T)
5731static boolean
5732elfcore_grok_win32pstatus (abfd, note)
5733 bfd *abfd;
5734 Elf_Internal_Note *note;
5735{
5736 char buf[30];
5737 char *name;
5738 asection *sect;
5739 win32_pstatus_t pstatus;
5740
5741 if (note->descsz < sizeof (pstatus))
5742 return true;
5743
5744 memcpy (&pstatus, note->descdata, note->descsz);
5745
5746 switch (pstatus.data_type)
5747 {
5748 case NOTE_INFO_PROCESS:
5749 /* FIXME: need to add ->core_command. */
5750 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5751 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5752 break;
5753
5754 case NOTE_INFO_THREAD:
5755 /* Make a ".reg/999" section. */
5756 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5757
5758 name = bfd_alloc (abfd, strlen (buf) + 1);
5759 if (name == NULL)
5760 return false;
5761
5762 strcpy (name, buf);
5763
5764 sect = bfd_make_section (abfd, name);
5765 if (sect == NULL)
5766 return false;
5767
5768 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5769 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5770 data.thread_info.thread_context);
5771 sect->flags = SEC_HAS_CONTENTS;
5772 sect->alignment_power = 2;
5773
5774 if (pstatus.data.thread_info.is_active_thread)
5775 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5776 return false;
5777 break;
5778
5779 case NOTE_INFO_MODULE:
5780 /* Make a ".module/xxxxxxxx" section. */
5781 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5782
5783 name = bfd_alloc (abfd, strlen (buf) + 1);
5784 if (name == NULL)
5785 return false;
5786
5787 strcpy (name, buf);
5788
5789 sect = bfd_make_section (abfd, name);
5790
5791 if (sect == NULL)
5792 return false;
5793
5794 sect->_raw_size = note->descsz;
5795 sect->filepos = note->descpos;
5796 sect->flags = SEC_HAS_CONTENTS;
5797 sect->alignment_power = 2;
5798 break;
5799
5800 default:
5801 return true;
5802 }
5803
5804 return true;
5805}
5806#endif /* HAVE_WIN32_PSTATUS_T */
5807
5808static boolean
5809elfcore_grok_note (abfd, note)
5810 bfd *abfd;
5811 Elf_Internal_Note *note;
5812{
5813 switch (note->type)
5814 {
5815 default:
5816 return true;
5817
5818#if defined (HAVE_PRSTATUS_T)
5819 case NT_PRSTATUS:
5820 return elfcore_grok_prstatus (abfd, note);
5821#endif
5822
5823#if defined (HAVE_PSTATUS_T)
5824 case NT_PSTATUS:
5825 return elfcore_grok_pstatus (abfd, note);
5826#endif
5827
5828#if defined (HAVE_LWPSTATUS_T)
5829 case NT_LWPSTATUS:
5830 return elfcore_grok_lwpstatus (abfd, note);
5831#endif
5832
5833 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5834 return elfcore_grok_prfpreg (abfd, note);
5835
5836#if defined (HAVE_WIN32_PSTATUS_T)
5837 case NT_WIN32PSTATUS:
5838 return elfcore_grok_win32pstatus (abfd, note);
5839#endif
5840
5841 case NT_PRXFPREG: /* Linux SSE extension */
5842 if (note->namesz == 5
5843 && ! strcmp (note->namedata, "LINUX"))
5844 return elfcore_grok_prxfpreg (abfd, note);
5845 else
5846 return true;
5847
5848#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5849 case NT_PRPSINFO:
5850 case NT_PSINFO:
5851 return elfcore_grok_psinfo (abfd, note);
5852#endif
5853 }
5854}
5855
5856static boolean
5857elfcore_read_notes (abfd, offset, size)
5858 bfd *abfd;
5859 bfd_vma offset;
5860 bfd_vma size;
5861{
5862 char *buf;
5863 char *p;
5864
5865 if (size <= 0)
5866 return true;
5867
5868 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5869 return false;
5870
5871 buf = bfd_malloc ((size_t) size);
5872 if (buf == NULL)
5873 return false;
5874
5875 if (bfd_read (buf, size, 1, abfd) != size)
5876 {
5877 error:
5878 free (buf);
5879 return false;
5880 }
5881
5882 p = buf;
5883 while (p < buf + size)
5884 {
5885 /* FIXME: bad alignment assumption. */
5886 Elf_External_Note *xnp = (Elf_External_Note *) p;
5887 Elf_Internal_Note in;
5888
5889 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5890
5891 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5892 in.namedata = xnp->name;
5893
5894 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5895 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5896 in.descpos = offset + (in.descdata - buf);
5897
5898 if (! elfcore_grok_note (abfd, &in))
5899 goto error;
5900
5901 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5902 }
5903
5904 free (buf);
5905 return true;
5906}
5907
5908/* FIXME: This function is now unnecessary. Callers can just call
5909 bfd_section_from_phdr directly. */
5910
5911boolean
5912_bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5913 bfd *abfd;
5914 Elf_Internal_Phdr* phdr;
5915 int sec_num;
5916{
5917 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5918 return false;
5919
5920 return true;
5921}
5922
5923
5924/* Providing external access to the ELF program header table. */
5925
5926/* Return an upper bound on the number of bytes required to store a
5927 copy of ABFD's program header table entries. Return -1 if an error
5928 occurs; bfd_get_error will return an appropriate code. */
5929
5930long
5931bfd_get_elf_phdr_upper_bound (abfd)
5932 bfd *abfd;
5933{
5934 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5935 {
5936 bfd_set_error (bfd_error_wrong_format);
5937 return -1;
5938 }
5939
5940 return (elf_elfheader (abfd)->e_phnum
5941 * sizeof (Elf_Internal_Phdr));
5942}
5943
5944/* Copy ABFD's program header table entries to *PHDRS. The entries
5945 will be stored as an array of Elf_Internal_Phdr structures, as
5946 defined in include/elf/internal.h. To find out how large the
5947 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5948
5949 Return the number of program header table entries read, or -1 if an
5950 error occurs; bfd_get_error will return an appropriate code. */
5951
5952int
5953bfd_get_elf_phdrs (abfd, phdrs)
5954 bfd *abfd;
5955 void *phdrs;
5956{
5957 int num_phdrs;
5958
5959 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5960 {
5961 bfd_set_error (bfd_error_wrong_format);
5962 return -1;
5963 }
5964
5965 num_phdrs = elf_elfheader (abfd)->e_phnum;
5966 memcpy (phdrs, elf_tdata (abfd)->phdr,
5967 num_phdrs * sizeof (Elf_Internal_Phdr));
5968
5969 return num_phdrs;
5970}
Note: See TracBrowser for help on using the repository browser.