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