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