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