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