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