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