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