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