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