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