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