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