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