source: branches/libc-0.6/src/binutils/bfd/elf.c

Last change on this file was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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