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