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