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