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