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