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