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