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