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