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