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