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