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