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