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