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