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