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