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