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