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