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