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