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