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