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