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