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