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