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 Free Software Foundation, Inc.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 /* SECTION
23
24 ELF backends
25
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
29
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet. */
33
34 /* For sparc64-cross-sparc32. */
35 #define _SYSCALL32
36 #include "bfd.h"
37 #include "sysdep.h"
38 #include "bfdlink.h"
39 #include "libbfd.h"
40 #define ARCH_SIZE 0
41 #include "elf-bfd.h"
42 #include "libiberty.h"
43
44 static int elf_sort_sections (const void *, const void *);
45 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
46 static bfd_boolean prep_headers (bfd *);
47 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
48 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ;
49
50 /* Swap version information in and out. The version information is
51 currently size independent. If that ever changes, this code will
52 need to move into elfcode.h. */
53
54 /* Swap in a Verdef structure. */
55
56 void
57 _bfd_elf_swap_verdef_in (bfd *abfd,
58 const Elf_External_Verdef *src,
59 Elf_Internal_Verdef *dst)
60 {
61 dst->vd_version = H_GET_16 (abfd, src->vd_version);
62 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
63 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
64 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
65 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
66 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
67 dst->vd_next = H_GET_32 (abfd, src->vd_next);
68 }
69
70 /* Swap out a Verdef structure. */
71
72 void
73 _bfd_elf_swap_verdef_out (bfd *abfd,
74 const Elf_Internal_Verdef *src,
75 Elf_External_Verdef *dst)
76 {
77 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
78 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
79 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
80 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
81 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
82 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
83 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
84 }
85
86 /* Swap in a Verdaux structure. */
87
88 void
89 _bfd_elf_swap_verdaux_in (bfd *abfd,
90 const Elf_External_Verdaux *src,
91 Elf_Internal_Verdaux *dst)
92 {
93 dst->vda_name = H_GET_32 (abfd, src->vda_name);
94 dst->vda_next = H_GET_32 (abfd, src->vda_next);
95 }
96
97 /* Swap out a Verdaux structure. */
98
99 void
100 _bfd_elf_swap_verdaux_out (bfd *abfd,
101 const Elf_Internal_Verdaux *src,
102 Elf_External_Verdaux *dst)
103 {
104 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
105 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
106 }
107
108 /* Swap in a Verneed structure. */
109
110 void
111 _bfd_elf_swap_verneed_in (bfd *abfd,
112 const Elf_External_Verneed *src,
113 Elf_Internal_Verneed *dst)
114 {
115 dst->vn_version = H_GET_16 (abfd, src->vn_version);
116 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
117 dst->vn_file = H_GET_32 (abfd, src->vn_file);
118 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
119 dst->vn_next = H_GET_32 (abfd, src->vn_next);
120 }
121
122 /* Swap out a Verneed structure. */
123
124 void
125 _bfd_elf_swap_verneed_out (bfd *abfd,
126 const Elf_Internal_Verneed *src,
127 Elf_External_Verneed *dst)
128 {
129 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
130 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
131 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
132 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
133 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
134 }
135
136 /* Swap in a Vernaux structure. */
137
138 void
139 _bfd_elf_swap_vernaux_in (bfd *abfd,
140 const Elf_External_Vernaux *src,
141 Elf_Internal_Vernaux *dst)
142 {
143 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
144 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
145 dst->vna_other = H_GET_16 (abfd, src->vna_other);
146 dst->vna_name = H_GET_32 (abfd, src->vna_name);
147 dst->vna_next = H_GET_32 (abfd, src->vna_next);
148 }
149
150 /* Swap out a Vernaux structure. */
151
152 void
153 _bfd_elf_swap_vernaux_out (bfd *abfd,
154 const Elf_Internal_Vernaux *src,
155 Elf_External_Vernaux *dst)
156 {
157 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
158 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
159 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
160 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
161 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
162 }
163
164 /* Swap in a Versym structure. */
165
166 void
167 _bfd_elf_swap_versym_in (bfd *abfd,
168 const Elf_External_Versym *src,
169 Elf_Internal_Versym *dst)
170 {
171 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
172 }
173
174 /* Swap out a Versym structure. */
175
176 void
177 _bfd_elf_swap_versym_out (bfd *abfd,
178 const Elf_Internal_Versym *src,
179 Elf_External_Versym *dst)
180 {
181 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
182 }
183
184 /* Standard ELF hash function. Do not change this function; you will
185 cause invalid hash tables to be generated. */
186
187 unsigned long
188 bfd_elf_hash (const char *namearg)
189 {
190 const unsigned char *name = (const unsigned char *) namearg;
191 unsigned long h = 0;
192 unsigned long g;
193 int ch;
194
195 while ((ch = *name++) != '\0')
196 {
197 h = (h << 4) + ch;
198 if ((g = (h & 0xf0000000)) != 0)
199 {
200 h ^= g >> 24;
201 /* The ELF ABI says `h &= ~g', but this is equivalent in
202 this case and on some machines one insn instead of two. */
203 h ^= g;
204 }
205 }
206 return h & 0xffffffff;
207 }
208
209 /* Read a specified number of bytes at a specified offset in an ELF
210 file, into a newly allocated buffer, and return a pointer to the
211 buffer. */
212
213 static char *
214 elf_read (bfd *abfd, file_ptr offset, bfd_size_type size)
215 {
216 char *buf;
217
218 if ((buf = bfd_alloc (abfd, size)) == NULL)
219 return NULL;
220 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
221 return NULL;
222 if (bfd_bread (buf, size, abfd) != size)
223 {
224 if (bfd_get_error () != bfd_error_system_call)
225 bfd_set_error (bfd_error_file_truncated);
226 return NULL;
227 }
228 return buf;
229 }
230
231 bfd_boolean
232 bfd_elf_mkobject (bfd *abfd)
233 {
234 /* This just does initialization. */
235 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
236 elf_tdata (abfd) = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
237 if (elf_tdata (abfd) == 0)
238 return FALSE;
239 /* Since everything is done at close time, do we need any
240 initialization? */
241
242 return TRUE;
243 }
244
245 bfd_boolean
246 bfd_elf_mkcorefile (bfd *abfd)
247 {
248 /* I think this can be done just like an object file. */
249 return bfd_elf_mkobject (abfd);
250 }
251
252 char *
253 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
254 {
255 Elf_Internal_Shdr **i_shdrp;
256 char *shstrtab = NULL;
257 file_ptr offset;
258 bfd_size_type shstrtabsize;
259
260 i_shdrp = elf_elfsections (abfd);
261 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
262 return 0;
263
264 shstrtab = (char *) i_shdrp[shindex]->contents;
265 if (shstrtab == NULL)
266 {
267 /* No cached one, attempt to read, and cache what we read. */
268 offset = i_shdrp[shindex]->sh_offset;
269 shstrtabsize = i_shdrp[shindex]->sh_size;
270 shstrtab = elf_read (abfd, offset, shstrtabsize);
271 i_shdrp[shindex]->contents = shstrtab;
272 }
273 return shstrtab;
274 }
275
276 char *
277 bfd_elf_string_from_elf_section (bfd *abfd,
278 unsigned int shindex,
279 unsigned int strindex)
280 {
281 Elf_Internal_Shdr *hdr;
282
283 if (strindex == 0)
284 return "";
285
286 hdr = elf_elfsections (abfd)[shindex];
287
288 if (hdr->contents == NULL
289 && bfd_elf_get_str_section (abfd, shindex) == NULL)
290 return NULL;
291
292 if (strindex >= hdr->sh_size)
293 {
294 (*_bfd_error_handler)
295 (_("%s: invalid string offset %u >= %lu for section `%s'"),
296 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
297 ((shindex == elf_elfheader(abfd)->e_shstrndx
298 && strindex == hdr->sh_name)
299 ? ".shstrtab"
300 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
301 return "";
302 }
303
304 return ((char *) hdr->contents) + strindex;
305 }
306
307 /* Read and convert symbols to internal format.
308 SYMCOUNT specifies the number of symbols to read, starting from
309 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
310 are non-NULL, they are used to store the internal symbols, external
311 symbols, and symbol section index extensions, respectively. */
312
313 Elf_Internal_Sym *
314 bfd_elf_get_elf_syms (bfd *ibfd,
315 Elf_Internal_Shdr *symtab_hdr,
316 size_t symcount,
317 size_t symoffset,
318 Elf_Internal_Sym *intsym_buf,
319 void *extsym_buf,
320 Elf_External_Sym_Shndx *extshndx_buf)
321 {
322 Elf_Internal_Shdr *shndx_hdr;
323 void *alloc_ext;
324 const bfd_byte *esym;
325 Elf_External_Sym_Shndx *alloc_extshndx;
326 Elf_External_Sym_Shndx *shndx;
327 Elf_Internal_Sym *isym;
328 Elf_Internal_Sym *isymend;
329 const struct elf_backend_data *bed;
330 size_t extsym_size;
331 bfd_size_type amt;
332 file_ptr pos;
333
334 if (symcount == 0)
335 return intsym_buf;
336
337 /* Normal syms might have section extension entries. */
338 shndx_hdr = NULL;
339 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
340 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
341
342 /* Read the symbols. */
343 alloc_ext = NULL;
344 alloc_extshndx = NULL;
345 bed = get_elf_backend_data (ibfd);
346 extsym_size = bed->s->sizeof_sym;
347 amt = symcount * extsym_size;
348 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
349 if (extsym_buf == NULL)
350 {
351 alloc_ext = bfd_malloc (amt);
352 extsym_buf = alloc_ext;
353 }
354 if (extsym_buf == NULL
355 || bfd_seek (ibfd, pos, SEEK_SET) != 0
356 || bfd_bread (extsym_buf, amt, ibfd) != amt)
357 {
358 intsym_buf = NULL;
359 goto out;
360 }
361
362 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
363 extshndx_buf = NULL;
364 else
365 {
366 amt = symcount * sizeof (Elf_External_Sym_Shndx);
367 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
368 if (extshndx_buf == NULL)
369 {
370 alloc_extshndx = bfd_malloc (amt);
371 extshndx_buf = alloc_extshndx;
372 }
373 if (extshndx_buf == NULL
374 || bfd_seek (ibfd, pos, SEEK_SET) != 0
375 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
376 {
377 intsym_buf = NULL;
378 goto out;
379 }
380 }
381
382 if (intsym_buf == NULL)
383 {
384 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
385 intsym_buf = bfd_malloc (amt);
386 if (intsym_buf == NULL)
387 goto out;
388 }
389
390 /* Convert the symbols to internal form. */
391 isymend = intsym_buf + symcount;
392 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
393 isym < isymend;
394 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
395 (*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym);
396
397 out:
398 if (alloc_ext != NULL)
399 free (alloc_ext);
400 if (alloc_extshndx != NULL)
401 free (alloc_extshndx);
402
403 return intsym_buf;
404 }
405
406 /* Look up a symbol name. */
407 const char *
408 bfd_elf_local_sym_name (bfd *abfd, Elf_Internal_Sym *isym)
409 {
410 unsigned int iname = isym->st_name;
411 unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link;
412 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION)
413 {
414 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
415 shindex = elf_elfheader (abfd)->e_shstrndx;
416 }
417
418 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
419 }
420
421 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
422 sections. The first element is the flags, the rest are section
423 pointers. */
424
425 typedef union elf_internal_group {
426 Elf_Internal_Shdr *shdr;
427 unsigned int flags;
428 } Elf_Internal_Group;
429
430 /* Return the name of the group signature symbol. Why isn't the
431 signature just a string? */
432
433 static const char *
434 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
435 {
436 Elf_Internal_Shdr *hdr;
437 unsigned char esym[sizeof (Elf64_External_Sym)];
438 Elf_External_Sym_Shndx eshndx;
439 Elf_Internal_Sym isym;
440
441 /* First we need to ensure the symbol table is available. */
442 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
443 return NULL;
444
445 /* Go read the symbol. */
446 hdr = &elf_tdata (abfd)->symtab_hdr;
447 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
448 &isym, esym, &eshndx) == NULL)
449 return NULL;
450
451 return bfd_elf_local_sym_name (abfd, &isym);
452 }
453
454 /* Set next_in_group list pointer, and group name for NEWSECT. */
455
456 static bfd_boolean
457 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
458 {
459 unsigned int num_group = elf_tdata (abfd)->num_group;
460
461 /* If num_group is zero, read in all SHT_GROUP sections. The count
462 is set to -1 if there are no SHT_GROUP sections. */
463 if (num_group == 0)
464 {
465 unsigned int i, shnum;
466
467 /* First count the number of groups. If we have a SHT_GROUP
468 section with just a flag word (ie. sh_size is 4), ignore it. */
469 shnum = elf_numsections (abfd);
470 num_group = 0;
471 for (i = 0; i < shnum; i++)
472 {
473 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
474 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
475 num_group += 1;
476 }
477
478 if (num_group == 0)
479 num_group = (unsigned) -1;
480 elf_tdata (abfd)->num_group = num_group;
481
482 if (num_group > 0)
483 {
484 /* We keep a list of elf section headers for group sections,
485 so we can find them quickly. */
486 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
487 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
488 if (elf_tdata (abfd)->group_sect_ptr == NULL)
489 return FALSE;
490
491 num_group = 0;
492 for (i = 0; i < shnum; i++)
493 {
494 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
495 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
496 {
497 unsigned char *src;
498 Elf_Internal_Group *dest;
499
500 /* Add to list of sections. */
501 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
502 num_group += 1;
503
504 /* Read the raw contents. */
505 BFD_ASSERT (sizeof (*dest) >= 4);
506 amt = shdr->sh_size * sizeof (*dest) / 4;
507 shdr->contents = bfd_alloc (abfd, amt);
508 if (shdr->contents == NULL
509 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
510 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
511 != shdr->sh_size))
512 return FALSE;
513
514 /* Translate raw contents, a flag word followed by an
515 array of elf section indices all in target byte order,
516 to the flag word followed by an array of elf section
517 pointers. */
518 src = shdr->contents + shdr->sh_size;
519 dest = (Elf_Internal_Group *) (shdr->contents + amt);
520 while (1)
521 {
522 unsigned int idx;
523
524 src -= 4;
525 --dest;
526 idx = H_GET_32 (abfd, src);
527 if (src == shdr->contents)
528 {
529 dest->flags = idx;
530 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
531 shdr->bfd_section->flags
532 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
533 break;
534 }
535 if (idx >= shnum)
536 {
537 ((*_bfd_error_handler)
538 (_("%s: invalid SHT_GROUP entry"),
539 bfd_archive_filename (abfd)));
540 idx = 0;
541 }
542 dest->shdr = elf_elfsections (abfd)[idx];
543 }
544 }
545 }
546 }
547 }
548
549 if (num_group != (unsigned) -1)
550 {
551 unsigned int i;
552
553 for (i = 0; i < num_group; i++)
554 {
555 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
556 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
557 unsigned int n_elt = shdr->sh_size / 4;
558
559 /* Look through this group's sections to see if current
560 section is a member. */
561 while (--n_elt != 0)
562 if ((++idx)->shdr == hdr)
563 {
564 asection *s = NULL;
565
566 /* We are a member of this group. Go looking through
567 other members to see if any others are linked via
568 next_in_group. */
569 idx = (Elf_Internal_Group *) shdr->contents;
570 n_elt = shdr->sh_size / 4;
571 while (--n_elt != 0)
572 if ((s = (++idx)->shdr->bfd_section) != NULL
573 && elf_next_in_group (s) != NULL)
574 break;
575 if (n_elt != 0)
576 {
577 /* Snarf the group name from other member, and
578 insert current section in circular list. */
579 elf_group_name (newsect) = elf_group_name (s);
580 elf_next_in_group (newsect) = elf_next_in_group (s);
581 elf_next_in_group (s) = newsect;
582 }
583 else
584 {
585 const char *gname;
586
587 gname = group_signature (abfd, shdr);
588 if (gname == NULL)
589 return FALSE;
590 elf_group_name (newsect) = gname;
591
592 /* Start a circular list with one element. */
593 elf_next_in_group (newsect) = newsect;
594 }
595
596 /* If the group section has been created, point to the
597 new member. */
598 if (shdr->bfd_section != NULL)
599 elf_next_in_group (shdr->bfd_section) = newsect;
600
601 i = num_group - 1;
602 break;
603 }
604 }
605 }
606
607 if (elf_group_name (newsect) == NULL)
608 {
609 (*_bfd_error_handler) (_("%s: no group info for section %s"),
610 bfd_archive_filename (abfd), newsect->name);
611 }
612 return TRUE;
613 }
614
615 bfd_boolean
616 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
617 {
618 return elf_next_in_group (sec) != NULL;
619 }
620
621 bfd_boolean
622 bfd_elf_discard_group (bfd *abfd ATTRIBUTE_UNUSED, asection *group)
623 {
624 asection *first = elf_next_in_group (group);
625 asection *s = first;
626
627 while (s != NULL)
628 {
629 s->output_section = bfd_abs_section_ptr;
630 s = elf_next_in_group (s);
631 /* These lists are circular. */
632 if (s == first)
633 break;
634 }
635 return TRUE;
636 }
637
638 /* Make a BFD section from an ELF section. We store a pointer to the
639 BFD section in the bfd_section field of the header. */
640
641 bfd_boolean
642 _bfd_elf_make_section_from_shdr (bfd *abfd,
643 Elf_Internal_Shdr *hdr,
644 const char *name)
645 {
646 asection *newsect;
647 flagword flags;
648 const struct elf_backend_data *bed;
649
650 if (hdr->bfd_section != NULL)
651 {
652 BFD_ASSERT (strcmp (name,
653 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
654 return TRUE;
655 }
656
657 newsect = bfd_make_section_anyway (abfd, name);
658 if (newsect == NULL)
659 return FALSE;
660
661 hdr->bfd_section = newsect;
662 elf_section_data (newsect)->this_hdr = *hdr;
663
664 /* Always use the real type/flags. */
665 elf_section_type (newsect) = hdr->sh_type;
666 elf_section_flags (newsect) = hdr->sh_flags;
667
668 newsect->filepos = hdr->sh_offset;
669
670 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
671 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
672 || ! bfd_set_section_alignment (abfd, newsect,
673 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
674 return FALSE;
675
676 flags = SEC_NO_FLAGS;
677 if (hdr->sh_type != SHT_NOBITS)
678 flags |= SEC_HAS_CONTENTS;
679 if (hdr->sh_type == SHT_GROUP)
680 flags |= SEC_GROUP | SEC_EXCLUDE;
681 if ((hdr->sh_flags & SHF_ALLOC) != 0)
682 {
683 flags |= SEC_ALLOC;
684 if (hdr->sh_type != SHT_NOBITS)
685 flags |= SEC_LOAD;
686 }
687 if ((hdr->sh_flags & SHF_WRITE) == 0)
688 flags |= SEC_READONLY;
689 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
690 flags |= SEC_CODE;
691 else if ((flags & SEC_LOAD) != 0)
692 flags |= SEC_DATA;
693 if ((hdr->sh_flags & SHF_MERGE) != 0)
694 {
695 flags |= SEC_MERGE;
696 newsect->entsize = hdr->sh_entsize;
697 if ((hdr->sh_flags & SHF_STRINGS) != 0)
698 flags |= SEC_STRINGS;
699 }
700 if (hdr->sh_flags & SHF_GROUP)
701 if (!setup_group (abfd, hdr, newsect))
702 return FALSE;
703 if ((hdr->sh_flags & SHF_TLS) != 0)
704 flags |= SEC_THREAD_LOCAL;
705
706 /* The debugging sections appear to be recognized only by name, not
707 any sort of flag. */
708 {
709 static const char *debug_sec_names [] =
710 {
711 ".debug",
712 ".gnu.linkonce.wi.",
713 ".line",
714 ".stab"
715 };
716 int i;
717
718 for (i = ARRAY_SIZE (debug_sec_names); i--;)
719 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
720 break;
721
722 if (i >= 0)
723 flags |= SEC_DEBUGGING;
724 }
725
726 /* As a GNU extension, if the name begins with .gnu.linkonce, we
727 only link a single copy of the section. This is used to support
728 g++. g++ will emit each template expansion in its own section.
729 The symbols will be defined as weak, so that multiple definitions
730 are permitted. The GNU linker extension is to actually discard
731 all but one of the sections. */
732 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
733 && elf_next_in_group (newsect) == NULL)
734 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
735
736 bed = get_elf_backend_data (abfd);
737 if (bed->elf_backend_section_flags)
738 if (! bed->elf_backend_section_flags (&flags, hdr))
739 return FALSE;
740
741 if (! bfd_set_section_flags (abfd, newsect, flags))
742 return FALSE;
743
744 if ((flags & SEC_ALLOC) != 0)
745 {
746 Elf_Internal_Phdr *phdr;
747 unsigned int i;
748
749 /* Look through the phdrs to see if we need to adjust the lma.
750 If all the p_paddr fields are zero, we ignore them, since
751 some ELF linkers produce such output. */
752 phdr = elf_tdata (abfd)->phdr;
753 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
754 {
755 if (phdr->p_paddr != 0)
756 break;
757 }
758 if (i < elf_elfheader (abfd)->e_phnum)
759 {
760 phdr = elf_tdata (abfd)->phdr;
761 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
762 {
763 /* This section is part of this segment if its file
764 offset plus size lies within the segment's memory
765 span and, if the section is loaded, the extent of the
766 loaded data lies within the extent of the segment.
767
768 Note - we used to check the p_paddr field as well, and
769 refuse to set the LMA if it was 0. This is wrong
770 though, as a perfectly valid initialised segment can
771 have a p_paddr of zero. Some architectures, eg ARM,
772 place special significance on the address 0 and
773 executables need to be able to have a segment which
774 covers this address. */
775 if (phdr->p_type == PT_LOAD
776 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
777 && (hdr->sh_offset + hdr->sh_size
778 <= phdr->p_offset + phdr->p_memsz)
779 && ((flags & SEC_LOAD) == 0
780 || (hdr->sh_offset + hdr->sh_size
781 <= phdr->p_offset + phdr->p_filesz)))
782 {
783 if ((flags & SEC_LOAD) == 0)
784 newsect->lma = (phdr->p_paddr
785 + hdr->sh_addr - phdr->p_vaddr);
786 else
787 /* We used to use the same adjustment for SEC_LOAD
788 sections, but that doesn't work if the segment
789 is packed with code from multiple VMAs.
790 Instead we calculate the section LMA based on
791 the segment LMA. It is assumed that the
792 segment will contain sections with contiguous
793 LMAs, even if the VMAs are not. */
794 newsect->lma = (phdr->p_paddr
795 + hdr->sh_offset - phdr->p_offset);
796
797 /* With contiguous segments, we can't tell from file
798 offsets whether a section with zero size should
799 be placed at the end of one segment or the
800 beginning of the next. Decide based on vaddr. */
801 if (hdr->sh_addr >= phdr->p_vaddr
802 && (hdr->sh_addr + hdr->sh_size
803 <= phdr->p_vaddr + phdr->p_memsz))
804 break;
805 }
806 }
807 }
808 }
809
810 return TRUE;
811 }
812
813 /*
814 INTERNAL_FUNCTION
815 bfd_elf_find_section
816
817 SYNOPSIS
818 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
819
820 DESCRIPTION
821 Helper functions for GDB to locate the string tables.
822 Since BFD hides string tables from callers, GDB needs to use an
823 internal hook to find them. Sun's .stabstr, in particular,
824 isn't even pointed to by the .stab section, so ordinary
825 mechanisms wouldn't work to find it, even if we had some.
826 */
827
828 struct elf_internal_shdr *
829 bfd_elf_find_section (bfd *abfd, char *name)
830 {
831 Elf_Internal_Shdr **i_shdrp;
832 char *shstrtab;
833 unsigned int max;
834 unsigned int i;
835
836 i_shdrp = elf_elfsections (abfd);
837 if (i_shdrp != NULL)
838 {
839 shstrtab = bfd_elf_get_str_section (abfd,
840 elf_elfheader (abfd)->e_shstrndx);
841 if (shstrtab != NULL)
842 {
843 max = elf_numsections (abfd);
844 for (i = 1; i < max; i++)
845 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
846 return i_shdrp[i];
847 }
848 }
849 return 0;
850 }
851
852 const char *const bfd_elf_section_type_names[] = {
853 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
854 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
855 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
856 };
857
858 /* ELF relocs are against symbols. If we are producing relocatable
859 output, and the reloc is against an external symbol, and nothing
860 has given us any additional addend, the resulting reloc will also
861 be against the same symbol. In such a case, we don't want to
862 change anything about the way the reloc is handled, since it will
863 all be done at final link time. Rather than put special case code
864 into bfd_perform_relocation, all the reloc types use this howto
865 function. It just short circuits the reloc if producing
866 relocatable output against an external symbol. */
867
868 bfd_reloc_status_type
869 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
870 arelent *reloc_entry,
871 asymbol *symbol,
872 void *data ATTRIBUTE_UNUSED,
873 asection *input_section,
874 bfd *output_bfd,
875 char **error_message ATTRIBUTE_UNUSED)
876 {
877 if (output_bfd != NULL
878 && (symbol->flags & BSF_SECTION_SYM) == 0
879 && (! reloc_entry->howto->partial_inplace
880 || reloc_entry->addend == 0))
881 {
882 reloc_entry->address += input_section->output_offset;
883 return bfd_reloc_ok;
884 }
885
886 return bfd_reloc_continue;
887 }
888 \f
889 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
890
891 static void
892 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
893 asection *sec)
894 {
895 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE);
896 sec->sec_info_type = ELF_INFO_TYPE_NONE;
897 }
898
899 /* Finish SHF_MERGE section merging. */
900
901 bfd_boolean
902 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info)
903 {
904 bfd *ibfd;
905 asection *sec;
906
907 if (!is_elf_hash_table (info->hash))
908 return FALSE;
909
910 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
911 if ((ibfd->flags & DYNAMIC) == 0)
912 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
913 if ((sec->flags & SEC_MERGE) != 0
914 && !bfd_is_abs_section (sec->output_section))
915 {
916 struct bfd_elf_section_data *secdata;
917
918 secdata = elf_section_data (sec);
919 if (! _bfd_add_merge_section (abfd,
920 &elf_hash_table (info)->merge_info,
921 sec, &secdata->sec_info))
922 return FALSE;
923 else if (secdata->sec_info)
924 sec->sec_info_type = ELF_INFO_TYPE_MERGE;
925 }
926
927 if (elf_hash_table (info)->merge_info != NULL)
928 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info,
929 merge_sections_remove_hook);
930 return TRUE;
931 }
932
933 void
934 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
935 {
936 sec->output_section = bfd_abs_section_ptr;
937 sec->output_offset = sec->vma;
938 if (!is_elf_hash_table (info->hash))
939 return;
940
941 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
942 }
943 \f
944 /* Copy the program header and other data from one object module to
945 another. */
946
947 bfd_boolean
948 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
949 {
950 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
951 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
952 return TRUE;
953
954 BFD_ASSERT (!elf_flags_init (obfd)
955 || (elf_elfheader (obfd)->e_flags
956 == elf_elfheader (ibfd)->e_flags));
957
958 elf_gp (obfd) = elf_gp (ibfd);
959 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
960 elf_flags_init (obfd) = TRUE;
961 return TRUE;
962 }
963
964 /* Print out the program headers. */
965
966 bfd_boolean
967 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
968 {
969 FILE *f = farg;
970 Elf_Internal_Phdr *p;
971 asection *s;
972 bfd_byte *dynbuf = NULL;
973
974 p = elf_tdata (abfd)->phdr;
975 if (p != NULL)
976 {
977 unsigned int i, c;
978
979 fprintf (f, _("\nProgram Header:\n"));
980 c = elf_elfheader (abfd)->e_phnum;
981 for (i = 0; i < c; i++, p++)
982 {
983 const char *pt;
984 char buf[20];
985
986 switch (p->p_type)
987 {
988 case PT_NULL: pt = "NULL"; break;
989 case PT_LOAD: pt = "LOAD"; break;
990 case PT_DYNAMIC: pt = "DYNAMIC"; break;
991 case PT_INTERP: pt = "INTERP"; break;
992 case PT_NOTE: pt = "NOTE"; break;
993 case PT_SHLIB: pt = "SHLIB"; break;
994 case PT_PHDR: pt = "PHDR"; break;
995 case PT_TLS: pt = "TLS"; break;
996 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
997 case PT_GNU_STACK: pt = "STACK"; break;
998 case PT_GNU_RELRO: pt = "RELRO"; break;
999 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1000 }
1001 fprintf (f, "%8s off 0x", pt);
1002 bfd_fprintf_vma (abfd, f, p->p_offset);
1003 fprintf (f, " vaddr 0x");
1004 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1005 fprintf (f, " paddr 0x");
1006 bfd_fprintf_vma (abfd, f, p->p_paddr);
1007 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1008 fprintf (f, " filesz 0x");
1009 bfd_fprintf_vma (abfd, f, p->p_filesz);
1010 fprintf (f, " memsz 0x");
1011 bfd_fprintf_vma (abfd, f, p->p_memsz);
1012 fprintf (f, " flags %c%c%c",
1013 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1014 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1015 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1016 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1017 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1018 fprintf (f, "\n");
1019 }
1020 }
1021
1022 s = bfd_get_section_by_name (abfd, ".dynamic");
1023 if (s != NULL)
1024 {
1025 int elfsec;
1026 unsigned long shlink;
1027 bfd_byte *extdyn, *extdynend;
1028 size_t extdynsize;
1029 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1030
1031 fprintf (f, _("\nDynamic Section:\n"));
1032
1033 dynbuf = bfd_malloc (s->_raw_size);
1034 if (dynbuf == NULL)
1035 goto error_return;
1036 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
1037 goto error_return;
1038
1039 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1040 if (elfsec == -1)
1041 goto error_return;
1042 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1043
1044 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1045 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1046
1047 extdyn = dynbuf;
1048 extdynend = extdyn + s->_raw_size;
1049 for (; extdyn < extdynend; extdyn += extdynsize)
1050 {
1051 Elf_Internal_Dyn dyn;
1052 const char *name;
1053 char ab[20];
1054 bfd_boolean stringp;
1055
1056 (*swap_dyn_in) (abfd, extdyn, &dyn);
1057
1058 if (dyn.d_tag == DT_NULL)
1059 break;
1060
1061 stringp = FALSE;
1062 switch (dyn.d_tag)
1063 {
1064 default:
1065 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1066 name = ab;
1067 break;
1068
1069 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1070 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1071 case DT_PLTGOT: name = "PLTGOT"; break;
1072 case DT_HASH: name = "HASH"; break;
1073 case DT_STRTAB: name = "STRTAB"; break;
1074 case DT_SYMTAB: name = "SYMTAB"; break;
1075 case DT_RELA: name = "RELA"; break;
1076 case DT_RELASZ: name = "RELASZ"; break;
1077 case DT_RELAENT: name = "RELAENT"; break;
1078 case DT_STRSZ: name = "STRSZ"; break;
1079 case DT_SYMENT: name = "SYMENT"; break;
1080 case DT_INIT: name = "INIT"; break;
1081 case DT_FINI: name = "FINI"; break;
1082 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1083 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1084 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1085 case DT_REL: name = "REL"; break;
1086 case DT_RELSZ: name = "RELSZ"; break;
1087 case DT_RELENT: name = "RELENT"; break;
1088 case DT_PLTREL: name = "PLTREL"; break;
1089 case DT_DEBUG: name = "DEBUG"; break;
1090 case DT_TEXTREL: name = "TEXTREL"; break;
1091 case DT_JMPREL: name = "JMPREL"; break;
1092 case DT_BIND_NOW: name = "BIND_NOW"; break;
1093 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1094 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1095 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1096 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1097 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1098 case DT_FLAGS: name = "FLAGS"; break;
1099 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1100 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1101 case DT_CHECKSUM: name = "CHECKSUM"; break;
1102 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1103 case DT_MOVEENT: name = "MOVEENT"; break;
1104 case DT_MOVESZ: name = "MOVESZ"; break;
1105 case DT_FEATURE: name = "FEATURE"; break;
1106 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1107 case DT_SYMINSZ: name = "SYMINSZ"; break;
1108 case DT_SYMINENT: name = "SYMINENT"; break;
1109 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1110 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1111 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1112 case DT_PLTPAD: name = "PLTPAD"; break;
1113 case DT_MOVETAB: name = "MOVETAB"; break;
1114 case DT_SYMINFO: name = "SYMINFO"; break;
1115 case DT_RELACOUNT: name = "RELACOUNT"; break;
1116 case DT_RELCOUNT: name = "RELCOUNT"; break;
1117 case DT_FLAGS_1: name = "FLAGS_1"; break;
1118 case DT_VERSYM: name = "VERSYM"; break;
1119 case DT_VERDEF: name = "VERDEF"; break;
1120 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1121 case DT_VERNEED: name = "VERNEED"; break;
1122 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1123 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1124 case DT_USED: name = "USED"; break;
1125 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1126 }
1127
1128 fprintf (f, " %-11s ", name);
1129 if (! stringp)
1130 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1131 else
1132 {
1133 const char *string;
1134 unsigned int tagv = dyn.d_un.d_val;
1135
1136 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1137 if (string == NULL)
1138 goto error_return;
1139 fprintf (f, "%s", string);
1140 }
1141 fprintf (f, "\n");
1142 }
1143
1144 free (dynbuf);
1145 dynbuf = NULL;
1146 }
1147
1148 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1149 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1150 {
1151 if (! _bfd_elf_slurp_version_tables (abfd))
1152 return FALSE;
1153 }
1154
1155 if (elf_dynverdef (abfd) != 0)
1156 {
1157 Elf_Internal_Verdef *t;
1158
1159 fprintf (f, _("\nVersion definitions:\n"));
1160 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1161 {
1162 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1163 t->vd_flags, t->vd_hash, t->vd_nodename);
1164 if (t->vd_auxptr->vda_nextptr != NULL)
1165 {
1166 Elf_Internal_Verdaux *a;
1167
1168 fprintf (f, "\t");
1169 for (a = t->vd_auxptr->vda_nextptr;
1170 a != NULL;
1171 a = a->vda_nextptr)
1172 fprintf (f, "%s ", a->vda_nodename);
1173 fprintf (f, "\n");
1174 }
1175 }
1176 }
1177
1178 if (elf_dynverref (abfd) != 0)
1179 {
1180 Elf_Internal_Verneed *t;
1181
1182 fprintf (f, _("\nVersion References:\n"));
1183 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1184 {
1185 Elf_Internal_Vernaux *a;
1186
1187 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1188 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1189 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1190 a->vna_flags, a->vna_other, a->vna_nodename);
1191 }
1192 }
1193
1194 return TRUE;
1195
1196 error_return:
1197 if (dynbuf != NULL)
1198 free (dynbuf);
1199 return FALSE;
1200 }
1201
1202 /* Display ELF-specific fields of a symbol. */
1203
1204 void
1205 bfd_elf_print_symbol (bfd *abfd,
1206 void *filep,
1207 asymbol *symbol,
1208 bfd_print_symbol_type how)
1209 {
1210 FILE *file = filep;
1211 switch (how)
1212 {
1213 case bfd_print_symbol_name:
1214 fprintf (file, "%s", symbol->name);
1215 break;
1216 case bfd_print_symbol_more:
1217 fprintf (file, "elf ");
1218 bfd_fprintf_vma (abfd, file, symbol->value);
1219 fprintf (file, " %lx", (long) symbol->flags);
1220 break;
1221 case bfd_print_symbol_all:
1222 {
1223 const char *section_name;
1224 const char *name = NULL;
1225 const struct elf_backend_data *bed;
1226 unsigned char st_other;
1227 bfd_vma val;
1228
1229 section_name = symbol->section ? symbol->section->name : "(*none*)";
1230
1231 bed = get_elf_backend_data (abfd);
1232 if (bed->elf_backend_print_symbol_all)
1233 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1234
1235 if (name == NULL)
1236 {
1237 name = symbol->name;
1238 bfd_print_symbol_vandf (abfd, file, symbol);
1239 }
1240
1241 fprintf (file, " %s\t", section_name);
1242 /* Print the "other" value for a symbol. For common symbols,
1243 we've already printed the size; now print the alignment.
1244 For other symbols, we have no specified alignment, and
1245 we've printed the address; now print the size. */
1246 if (bfd_is_com_section (symbol->section))
1247 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1248 else
1249 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1250 bfd_fprintf_vma (abfd, file, val);
1251
1252 /* If we have version information, print it. */
1253 if (elf_tdata (abfd)->dynversym_section != 0
1254 && (elf_tdata (abfd)->dynverdef_section != 0
1255 || elf_tdata (abfd)->dynverref_section != 0))
1256 {
1257 unsigned int vernum;
1258 const char *version_string;
1259
1260 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1261
1262 if (vernum == 0)
1263 version_string = "";
1264 else if (vernum == 1)
1265 version_string = "Base";
1266 else if (vernum <= elf_tdata (abfd)->cverdefs)
1267 version_string =
1268 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1269 else
1270 {
1271 Elf_Internal_Verneed *t;
1272
1273 version_string = "";
1274 for (t = elf_tdata (abfd)->verref;
1275 t != NULL;
1276 t = t->vn_nextref)
1277 {
1278 Elf_Internal_Vernaux *a;
1279
1280 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1281 {
1282 if (a->vna_other == vernum)
1283 {
1284 version_string = a->vna_nodename;
1285 break;
1286 }
1287 }
1288 }
1289 }
1290
1291 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1292 fprintf (file, " %-11s", version_string);
1293 else
1294 {
1295 int i;
1296
1297 fprintf (file, " (%s)", version_string);
1298 for (i = 10 - strlen (version_string); i > 0; --i)
1299 putc (' ', file);
1300 }
1301 }
1302
1303 /* If the st_other field is not zero, print it. */
1304 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1305
1306 switch (st_other)
1307 {
1308 case 0: break;
1309 case STV_INTERNAL: fprintf (file, " .internal"); break;
1310 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1311 case STV_PROTECTED: fprintf (file, " .protected"); break;
1312 default:
1313 /* Some other non-defined flags are also present, so print
1314 everything hex. */
1315 fprintf (file, " 0x%02x", (unsigned int) st_other);
1316 }
1317
1318 fprintf (file, " %s", name);
1319 }
1320 break;
1321 }
1322 }
1323 \f
1324 /* Create an entry in an ELF linker hash table. */
1325
1326 struct bfd_hash_entry *
1327 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
1328 struct bfd_hash_table *table,
1329 const char *string)
1330 {
1331 /* Allocate the structure if it has not already been allocated by a
1332 subclass. */
1333 if (entry == NULL)
1334 {
1335 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1336 if (entry == NULL)
1337 return entry;
1338 }
1339
1340 /* Call the allocation method of the superclass. */
1341 entry = _bfd_link_hash_newfunc (entry, table, string);
1342 if (entry != NULL)
1343 {
1344 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1345 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1346
1347 /* Set local fields. */
1348 ret->indx = -1;
1349 ret->dynindx = -1;
1350 ret->dynstr_index = 0;
1351 ret->elf_hash_value = 0;
1352 ret->weakdef = NULL;
1353 ret->verinfo.verdef = NULL;
1354 ret->vtable_entries_size = 0;
1355 ret->vtable_entries_used = NULL;
1356 ret->vtable_parent = NULL;
1357 ret->got = htab->init_refcount;
1358 ret->plt = htab->init_refcount;
1359 ret->size = 0;
1360 ret->type = STT_NOTYPE;
1361 ret->other = 0;
1362 /* Assume that we have been called by a non-ELF symbol reader.
1363 This flag is then reset by the code which reads an ELF input
1364 file. This ensures that a symbol created by a non-ELF symbol
1365 reader will have the flag set correctly. */
1366 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1367 }
1368
1369 return entry;
1370 }
1371
1372 /* Copy data from an indirect symbol to its direct symbol, hiding the
1373 old indirect symbol. Also used for copying flags to a weakdef. */
1374
1375 void
1376 _bfd_elf_link_hash_copy_indirect (const struct elf_backend_data *bed,
1377 struct elf_link_hash_entry *dir,
1378 struct elf_link_hash_entry *ind)
1379 {
1380 bfd_signed_vma tmp;
1381 bfd_signed_vma lowest_valid = bed->can_refcount;
1382
1383 /* Copy down any references that we may have already seen to the
1384 symbol which just became indirect. */
1385
1386 dir->elf_link_hash_flags
1387 |= ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
1388 | ELF_LINK_HASH_REF_REGULAR
1389 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1390 | ELF_LINK_NON_GOT_REF
1391 | ELF_LINK_HASH_NEEDS_PLT
1392 | ELF_LINK_POINTER_EQUALITY_NEEDED);
1393
1394 if (ind->root.type != bfd_link_hash_indirect)
1395 return;
1396
1397 /* Copy over the global and procedure linkage table refcount entries.
1398 These may have been already set up by a check_relocs routine. */
1399 tmp = dir->got.refcount;
1400 if (tmp < lowest_valid)
1401 {
1402 dir->got.refcount = ind->got.refcount;
1403 ind->got.refcount = tmp;
1404 }
1405 else
1406 BFD_ASSERT (ind->got.refcount < lowest_valid);
1407
1408 tmp = dir->plt.refcount;
1409 if (tmp < lowest_valid)
1410 {
1411 dir->plt.refcount = ind->plt.refcount;
1412 ind->plt.refcount = tmp;
1413 }
1414 else
1415 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1416
1417 if (dir->dynindx == -1)
1418 {
1419 dir->dynindx = ind->dynindx;
1420 dir->dynstr_index = ind->dynstr_index;
1421 ind->dynindx = -1;
1422 ind->dynstr_index = 0;
1423 }
1424 else
1425 BFD_ASSERT (ind->dynindx == -1);
1426 }
1427
1428 void
1429 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
1430 struct elf_link_hash_entry *h,
1431 bfd_boolean force_local)
1432 {
1433 h->plt = elf_hash_table (info)->init_offset;
1434 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1435 if (force_local)
1436 {
1437 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1438 if (h->dynindx != -1)
1439 {
1440 h->dynindx = -1;
1441 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1442 h->dynstr_index);
1443 }
1444 }
1445 }
1446
1447 /* Initialize an ELF linker hash table. */
1448
1449 bfd_boolean
1450 _bfd_elf_link_hash_table_init
1451 (struct elf_link_hash_table *table,
1452 bfd *abfd,
1453 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
1454 struct bfd_hash_table *,
1455 const char *))
1456 {
1457 bfd_boolean ret;
1458
1459 table->dynamic_sections_created = FALSE;
1460 table->dynobj = NULL;
1461 /* Make sure can_refcount is extended to the width and signedness of
1462 init_refcount before we subtract one from it. */
1463 table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount;
1464 table->init_refcount.refcount -= 1;
1465 table->init_offset.offset = -(bfd_vma) 1;
1466 /* The first dynamic symbol is a dummy. */
1467 table->dynsymcount = 1;
1468 table->dynstr = NULL;
1469 table->bucketcount = 0;
1470 table->needed = NULL;
1471 table->hgot = NULL;
1472 table->stab_info = NULL;
1473 table->merge_info = NULL;
1474 memset (&table->eh_info, 0, sizeof (table->eh_info));
1475 table->dynlocal = NULL;
1476 table->runpath = NULL;
1477 table->tls_sec = NULL;
1478 table->tls_size = 0;
1479 table->loaded = NULL;
1480
1481 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1482 table->root.type = bfd_link_elf_hash_table;
1483
1484 return ret;
1485 }
1486
1487 /* Create an ELF linker hash table. */
1488
1489 struct bfd_link_hash_table *
1490 _bfd_elf_link_hash_table_create (bfd *abfd)
1491 {
1492 struct elf_link_hash_table *ret;
1493 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1494
1495 ret = bfd_malloc (amt);
1496 if (ret == NULL)
1497 return NULL;
1498
1499 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1500 {
1501 free (ret);
1502 return NULL;
1503 }
1504
1505 return &ret->root;
1506 }
1507
1508 /* This is a hook for the ELF emulation code in the generic linker to
1509 tell the backend linker what file name to use for the DT_NEEDED
1510 entry for a dynamic object. */
1511
1512 void
1513 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
1514 {
1515 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1516 && bfd_get_format (abfd) == bfd_object)
1517 elf_dt_name (abfd) = name;
1518 }
1519
1520 void
1521 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class)
1522 {
1523 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1524 && bfd_get_format (abfd) == bfd_object)
1525 elf_dyn_lib_class (abfd) = lib_class;
1526 }
1527
1528 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1529 the linker ELF emulation code. */
1530
1531 struct bfd_link_needed_list *
1532 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
1533 struct bfd_link_info *info)
1534 {
1535 if (! is_elf_hash_table (info->hash))
1536 return NULL;
1537 return elf_hash_table (info)->needed;
1538 }
1539
1540 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1541 hook for the linker ELF emulation code. */
1542
1543 struct bfd_link_needed_list *
1544 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
1545 struct bfd_link_info *info)
1546 {
1547 if (! is_elf_hash_table (info->hash))
1548 return NULL;
1549 return elf_hash_table (info)->runpath;
1550 }
1551
1552 /* Get the name actually used for a dynamic object for a link. This
1553 is the SONAME entry if there is one. Otherwise, it is the string
1554 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1555
1556 const char *
1557 bfd_elf_get_dt_soname (bfd *abfd)
1558 {
1559 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1560 && bfd_get_format (abfd) == bfd_object)
1561 return elf_dt_name (abfd);
1562 return NULL;
1563 }
1564
1565 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1566 the ELF linker emulation code. */
1567
1568 bfd_boolean
1569 bfd_elf_get_bfd_needed_list (bfd *abfd,
1570 struct bfd_link_needed_list **pneeded)
1571 {
1572 asection *s;
1573 bfd_byte *dynbuf = NULL;
1574 int elfsec;
1575 unsigned long shlink;
1576 bfd_byte *extdyn, *extdynend;
1577 size_t extdynsize;
1578 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1579
1580 *pneeded = NULL;
1581
1582 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1583 || bfd_get_format (abfd) != bfd_object)
1584 return TRUE;
1585
1586 s = bfd_get_section_by_name (abfd, ".dynamic");
1587 if (s == NULL || s->_raw_size == 0)
1588 return TRUE;
1589
1590 dynbuf = bfd_malloc (s->_raw_size);
1591 if (dynbuf == NULL)
1592 goto error_return;
1593
1594 if (! bfd_get_section_contents (abfd, s, dynbuf, 0, s->_raw_size))
1595 goto error_return;
1596
1597 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1598 if (elfsec == -1)
1599 goto error_return;
1600
1601 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1602
1603 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1604 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1605
1606 extdyn = dynbuf;
1607 extdynend = extdyn + s->_raw_size;
1608 for (; extdyn < extdynend; extdyn += extdynsize)
1609 {
1610 Elf_Internal_Dyn dyn;
1611
1612 (*swap_dyn_in) (abfd, extdyn, &dyn);
1613
1614 if (dyn.d_tag == DT_NULL)
1615 break;
1616
1617 if (dyn.d_tag == DT_NEEDED)
1618 {
1619 const char *string;
1620 struct bfd_link_needed_list *l;
1621 unsigned int tagv = dyn.d_un.d_val;
1622 bfd_size_type amt;
1623
1624 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1625 if (string == NULL)
1626 goto error_return;
1627
1628 amt = sizeof *l;
1629 l = bfd_alloc (abfd, amt);
1630 if (l == NULL)
1631 goto error_return;
1632
1633 l->by = abfd;
1634 l->name = string;
1635 l->next = *pneeded;
1636 *pneeded = l;
1637 }
1638 }
1639
1640 free (dynbuf);
1641
1642 return TRUE;
1643
1644 error_return:
1645 if (dynbuf != NULL)
1646 free (dynbuf);
1647 return FALSE;
1648 }
1649 \f
1650 /* Allocate an ELF string table--force the first byte to be zero. */
1651
1652 struct bfd_strtab_hash *
1653 _bfd_elf_stringtab_init (void)
1654 {
1655 struct bfd_strtab_hash *ret;
1656
1657 ret = _bfd_stringtab_init ();
1658 if (ret != NULL)
1659 {
1660 bfd_size_type loc;
1661
1662 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1663 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1664 if (loc == (bfd_size_type) -1)
1665 {
1666 _bfd_stringtab_free (ret);
1667 ret = NULL;
1668 }
1669 }
1670 return ret;
1671 }
1672 \f
1673 /* ELF .o/exec file reading */
1674
1675 /* Create a new bfd section from an ELF section header. */
1676
1677 bfd_boolean
1678 bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1679 {
1680 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1681 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1682 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1683 const char *name;
1684
1685 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1686
1687 switch (hdr->sh_type)
1688 {
1689 case SHT_NULL:
1690 /* Inactive section. Throw it away. */
1691 return TRUE;
1692
1693 case SHT_PROGBITS: /* Normal section with contents. */
1694 case SHT_NOBITS: /* .bss section. */
1695 case SHT_HASH: /* .hash section. */
1696 case SHT_NOTE: /* .note section. */
1697 case SHT_INIT_ARRAY: /* .init_array section. */
1698 case SHT_FINI_ARRAY: /* .fini_array section. */
1699 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1700 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1701
1702 case SHT_DYNAMIC: /* Dynamic linking information. */
1703 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1704 return FALSE;
1705 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1706 {
1707 Elf_Internal_Shdr *dynsymhdr;
1708
1709 /* The shared libraries distributed with hpux11 have a bogus
1710 sh_link field for the ".dynamic" section. Find the
1711 string table for the ".dynsym" section instead. */
1712 if (elf_dynsymtab (abfd) != 0)
1713 {
1714 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1715 hdr->sh_link = dynsymhdr->sh_link;
1716 }
1717 else
1718 {
1719 unsigned int i, num_sec;
1720
1721 num_sec = elf_numsections (abfd);
1722 for (i = 1; i < num_sec; i++)
1723 {
1724 dynsymhdr = elf_elfsections (abfd)[i];
1725 if (dynsymhdr->sh_type == SHT_DYNSYM)
1726 {
1727 hdr->sh_link = dynsymhdr->sh_link;
1728 break;
1729 }
1730 }
1731 }
1732 }
1733 break;
1734
1735 case SHT_SYMTAB: /* A symbol table */
1736 if (elf_onesymtab (abfd) == shindex)
1737 return TRUE;
1738
1739 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1740 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1741 elf_onesymtab (abfd) = shindex;
1742 elf_tdata (abfd)->symtab_hdr = *hdr;
1743 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1744 abfd->flags |= HAS_SYMS;
1745
1746 /* Sometimes a shared object will map in the symbol table. If
1747 SHF_ALLOC is set, and this is a shared object, then we also
1748 treat this section as a BFD section. We can not base the
1749 decision purely on SHF_ALLOC, because that flag is sometimes
1750 set in a relocatable object file, which would confuse the
1751 linker. */
1752 if ((hdr->sh_flags & SHF_ALLOC) != 0
1753 && (abfd->flags & DYNAMIC) != 0
1754 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1755 return FALSE;
1756
1757 return TRUE;
1758
1759 case SHT_DYNSYM: /* A dynamic symbol table */
1760 if (elf_dynsymtab (abfd) == shindex)
1761 return TRUE;
1762
1763 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1764 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1765 elf_dynsymtab (abfd) = shindex;
1766 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1767 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1768 abfd->flags |= HAS_SYMS;
1769
1770 /* Besides being a symbol table, we also treat this as a regular
1771 section, so that objcopy can handle it. */
1772 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1773
1774 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1775 if (elf_symtab_shndx (abfd) == shindex)
1776 return TRUE;
1777
1778 /* Get the associated symbol table. */
1779 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1780 || hdr->sh_link != elf_onesymtab (abfd))
1781 return FALSE;
1782
1783 elf_symtab_shndx (abfd) = shindex;
1784 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1785 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1786 return TRUE;
1787
1788 case SHT_STRTAB: /* A string table */
1789 if (hdr->bfd_section != NULL)
1790 return TRUE;
1791 if (ehdr->e_shstrndx == shindex)
1792 {
1793 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1794 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1795 return TRUE;
1796 }
1797 {
1798 unsigned int i, num_sec;
1799
1800 num_sec = elf_numsections (abfd);
1801 for (i = 1; i < num_sec; i++)
1802 {
1803 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1804 if (hdr2->sh_link == shindex)
1805 {
1806 if (! bfd_section_from_shdr (abfd, i))
1807 return FALSE;
1808 if (elf_onesymtab (abfd) == i)
1809 {
1810 elf_tdata (abfd)->strtab_hdr = *hdr;
1811 elf_elfsections (abfd)[shindex] =
1812 &elf_tdata (abfd)->strtab_hdr;
1813 return TRUE;
1814 }
1815 if (elf_dynsymtab (abfd) == i)
1816 {
1817 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1818 elf_elfsections (abfd)[shindex] = hdr =
1819 &elf_tdata (abfd)->dynstrtab_hdr;
1820 /* We also treat this as a regular section, so
1821 that objcopy can handle it. */
1822 break;
1823 }
1824 #if 0 /* Not handling other string tables specially right now. */
1825 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1826 /* We have a strtab for some random other section. */
1827 newsect = (asection *) hdr2->bfd_section;
1828 if (!newsect)
1829 break;
1830 hdr->bfd_section = newsect;
1831 hdr2 = &elf_section_data (newsect)->str_hdr;
1832 *hdr2 = *hdr;
1833 elf_elfsections (abfd)[shindex] = hdr2;
1834 #endif
1835 }
1836 }
1837 }
1838
1839 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1840
1841 case SHT_REL:
1842 case SHT_RELA:
1843 /* *These* do a lot of work -- but build no sections! */
1844 {
1845 asection *target_sect;
1846 Elf_Internal_Shdr *hdr2;
1847 unsigned int num_sec = elf_numsections (abfd);
1848
1849 /* Check for a bogus link to avoid crashing. */
1850 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1851 || hdr->sh_link >= num_sec)
1852 {
1853 ((*_bfd_error_handler)
1854 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1855 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1856 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1857 }
1858
1859 /* For some incomprehensible reason Oracle distributes
1860 libraries for Solaris in which some of the objects have
1861 bogus sh_link fields. It would be nice if we could just
1862 reject them, but, unfortunately, some people need to use
1863 them. We scan through the section headers; if we find only
1864 one suitable symbol table, we clobber the sh_link to point
1865 to it. I hope this doesn't break anything. */
1866 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1867 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1868 {
1869 unsigned int scan;
1870 int found;
1871
1872 found = 0;
1873 for (scan = 1; scan < num_sec; scan++)
1874 {
1875 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1876 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1877 {
1878 if (found != 0)
1879 {
1880 found = 0;
1881 break;
1882 }
1883 found = scan;
1884 }
1885 }
1886 if (found != 0)
1887 hdr->sh_link = found;
1888 }
1889
1890 /* Get the symbol table. */
1891 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1892 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1893 return FALSE;
1894
1895 /* If this reloc section does not use the main symbol table we
1896 don't treat it as a reloc section. BFD can't adequately
1897 represent such a section, so at least for now, we don't
1898 try. We just present it as a normal section. We also
1899 can't use it as a reloc section if it points to the null
1900 section. */
1901 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1902 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1903
1904 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1905 return FALSE;
1906 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1907 if (target_sect == NULL)
1908 return FALSE;
1909
1910 if ((target_sect->flags & SEC_RELOC) == 0
1911 || target_sect->reloc_count == 0)
1912 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1913 else
1914 {
1915 bfd_size_type amt;
1916 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1917 amt = sizeof (*hdr2);
1918 hdr2 = bfd_alloc (abfd, amt);
1919 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1920 }
1921 *hdr2 = *hdr;
1922 elf_elfsections (abfd)[shindex] = hdr2;
1923 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1924 target_sect->flags |= SEC_RELOC;
1925 target_sect->relocation = NULL;
1926 target_sect->rel_filepos = hdr->sh_offset;
1927 /* In the section to which the relocations apply, mark whether
1928 its relocations are of the REL or RELA variety. */
1929 if (hdr->sh_size != 0)
1930 target_sect->use_rela_p = hdr->sh_type == SHT_RELA;
1931 abfd->flags |= HAS_RELOC;
1932 return TRUE;
1933 }
1934 break;
1935
1936 case SHT_GNU_verdef:
1937 elf_dynverdef (abfd) = shindex;
1938 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1939 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1940 break;
1941
1942 case SHT_GNU_versym:
1943 elf_dynversym (abfd) = shindex;
1944 elf_tdata (abfd)->dynversym_hdr = *hdr;
1945 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1946 break;
1947
1948 case SHT_GNU_verneed:
1949 elf_dynverref (abfd) = shindex;
1950 elf_tdata (abfd)->dynverref_hdr = *hdr;
1951 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1952 break;
1953
1954 case SHT_SHLIB:
1955 return TRUE;
1956
1957 case SHT_GROUP:
1958 /* We need a BFD section for objcopy and relocatable linking,
1959 and it's handy to have the signature available as the section
1960 name. */
1961 name = group_signature (abfd, hdr);
1962 if (name == NULL)
1963 return FALSE;
1964 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1965 return FALSE;
1966 if (hdr->contents != NULL)
1967 {
1968 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1969 unsigned int n_elt = hdr->sh_size / 4;
1970 asection *s;
1971
1972 if (idx->flags & GRP_COMDAT)
1973 hdr->bfd_section->flags
1974 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1975
1976 /* We try to keep the same section order as it comes in. */
1977 idx += n_elt;
1978 while (--n_elt != 0)
1979 if ((s = (--idx)->shdr->bfd_section) != NULL
1980 && elf_next_in_group (s) != NULL)
1981 {
1982 elf_next_in_group (hdr->bfd_section) = s;
1983 break;
1984 }
1985 }
1986 break;
1987
1988 default:
1989 /* Check for any processor-specific section types. */
1990 {
1991 if (bed->elf_backend_section_from_shdr)
1992 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1993 }
1994 break;
1995 }
1996
1997 return TRUE;
1998 }
1999
2000 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2001 Return SEC for sections that have no elf section, and NULL on error. */
2002
2003 asection *
2004 bfd_section_from_r_symndx (bfd *abfd,
2005 struct sym_sec_cache *cache,
2006 asection *sec,
2007 unsigned long r_symndx)
2008 {
2009 Elf_Internal_Shdr *symtab_hdr;
2010 unsigned char esym[sizeof (Elf64_External_Sym)];
2011 Elf_External_Sym_Shndx eshndx;
2012 Elf_Internal_Sym isym;
2013 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2014
2015 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2016 return cache->sec[ent];
2017
2018 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2019 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2020 &isym, esym, &eshndx) == NULL)
2021 return NULL;
2022
2023 if (cache->abfd != abfd)
2024 {
2025 memset (cache->indx, -1, sizeof (cache->indx));
2026 cache->abfd = abfd;
2027 }
2028 cache->indx[ent] = r_symndx;
2029 cache->sec[ent] = sec;
2030 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE)
2031 || isym.st_shndx > SHN_HIRESERVE)
2032 {
2033 asection *s;
2034 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2035 if (s != NULL)
2036 cache->sec[ent] = s;
2037 }
2038 return cache->sec[ent];
2039 }
2040
2041 /* Given an ELF section number, retrieve the corresponding BFD
2042 section. */
2043
2044 asection *
2045 bfd_section_from_elf_index (bfd *abfd, unsigned int index)
2046 {
2047 if (index >= elf_numsections (abfd))
2048 return NULL;
2049 return elf_elfsections (abfd)[index]->bfd_section;
2050 }
2051
2052 static struct bfd_elf_special_section const special_sections[] =
2053 {
2054 { ".bss", 4, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2055 { ".comment", 8, 0, SHT_PROGBITS, 0 },
2056 { ".data", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2057 { ".data1", 6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2058 { ".debug", 6, 0, SHT_PROGBITS, 0 },
2059 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2060 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2061 { ".line", 5, 0, SHT_PROGBITS, 0 },
2062 { ".rodata", 7, -2, SHT_PROGBITS, SHF_ALLOC },
2063 { ".rodata1", 8, 0, SHT_PROGBITS, SHF_ALLOC },
2064 { ".tbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2065 { ".tdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2066 { ".text", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2067 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2068 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2069 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2070 { ".debug_line", 11, 0, SHT_PROGBITS, 0 },
2071 { ".debug_info", 11, 0, SHT_PROGBITS, 0 },
2072 { ".debug_abbrev", 13, 0, SHT_PROGBITS, 0 },
2073 { ".debug_aranges", 14, 0, SHT_PROGBITS, 0 },
2074 { ".dynamic", 8, 0, SHT_DYNAMIC, SHF_ALLOC },
2075 { ".dynstr", 7, 0, SHT_STRTAB, SHF_ALLOC },
2076 { ".dynsym", 7, 0, SHT_DYNSYM, SHF_ALLOC },
2077 { ".got", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2078 { ".hash", 5, 0, SHT_HASH, SHF_ALLOC },
2079 { ".interp", 7, 0, SHT_PROGBITS, 0 },
2080 { ".plt", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2081 { ".shstrtab", 9, 0, SHT_STRTAB, 0 },
2082 { ".strtab", 7, 0, SHT_STRTAB, 0 },
2083 { ".symtab", 7, 0, SHT_SYMTAB, 0 },
2084 { ".gnu.version", 12, 0, SHT_GNU_versym, 0 },
2085 { ".gnu.version_d", 14, 0, SHT_GNU_verdef, 0 },
2086 { ".gnu.version_r", 14, 0, SHT_GNU_verneed, 0 },
2087 { ".note.GNU-stack",15, 0, SHT_PROGBITS, 0 },
2088 { ".note", 5, -1, SHT_NOTE, 0 },
2089 { ".rela", 5, -1, SHT_RELA, 0 },
2090 { ".rel", 4, -1, SHT_REL, 0 },
2091 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
2092 { NULL, 0, 0, 0, 0 }
2093 };
2094
2095 static const struct bfd_elf_special_section *
2096 get_special_section (const char *name,
2097 const struct bfd_elf_special_section *special_sections,
2098 unsigned int rela)
2099 {
2100 int i;
2101 int len = strlen (name);
2102
2103 for (i = 0; special_sections[i].prefix != NULL; i++)
2104 {
2105 int suffix_len;
2106 int prefix_len = special_sections[i].prefix_length;
2107
2108 if (len < prefix_len)
2109 continue;
2110 if (memcmp (name, special_sections[i].prefix, prefix_len) != 0)
2111 continue;
2112
2113 suffix_len = special_sections[i].suffix_length;
2114 if (suffix_len <= 0)
2115 {
2116 if (name[prefix_len] != 0)
2117 {
2118 if (suffix_len == 0)
2119 continue;
2120 if (name[prefix_len] != '.'
2121 && (suffix_len == -2
2122 || (rela && special_sections[i].type == SHT_REL)))
2123 continue;
2124 }
2125 }
2126 else
2127 {
2128 if (len < prefix_len + suffix_len)
2129 continue;
2130 if (memcmp (name + len - suffix_len,
2131 special_sections[i].prefix + prefix_len,
2132 suffix_len) != 0)
2133 continue;
2134 }
2135 return &special_sections[i];
2136 }
2137
2138 return NULL;
2139 }
2140
2141 const struct bfd_elf_special_section *
2142 _bfd_elf_get_sec_type_attr (bfd *abfd, const char *name)
2143 {
2144 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2145 const struct bfd_elf_special_section *ssect = NULL;
2146
2147 /* See if this is one of the special sections. */
2148 if (name)
2149 {
2150 unsigned int rela = bed->default_use_rela_p;
2151
2152 if (bed->special_sections)
2153 ssect = get_special_section (name, bed->special_sections, rela);
2154
2155 if (! ssect)
2156 ssect = get_special_section (name, special_sections, rela);
2157 }
2158
2159 return ssect;
2160 }
2161
2162 bfd_boolean
2163 _bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2164 {
2165 struct bfd_elf_section_data *sdata;
2166 const struct bfd_elf_special_section *ssect;
2167
2168 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2169 if (sdata == NULL)
2170 {
2171 sdata = bfd_zalloc (abfd, sizeof (*sdata));
2172 if (sdata == NULL)
2173 return FALSE;
2174 sec->used_by_bfd = sdata;
2175 }
2176
2177 elf_section_type (sec) = SHT_NULL;
2178 ssect = _bfd_elf_get_sec_type_attr (abfd, sec->name);
2179 if (ssect != NULL)
2180 {
2181 elf_section_type (sec) = ssect->type;
2182 elf_section_flags (sec) = ssect->attr;
2183 }
2184
2185 /* Indicate whether or not this section should use RELA relocations. */
2186 sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p;
2187
2188 return TRUE;
2189 }
2190
2191 /* Create a new bfd section from an ELF program header.
2192
2193 Since program segments have no names, we generate a synthetic name
2194 of the form segment<NUM>, where NUM is generally the index in the
2195 program header table. For segments that are split (see below) we
2196 generate the names segment<NUM>a and segment<NUM>b.
2197
2198 Note that some program segments may have a file size that is different than
2199 (less than) the memory size. All this means is that at execution the
2200 system must allocate the amount of memory specified by the memory size,
2201 but only initialize it with the first "file size" bytes read from the
2202 file. This would occur for example, with program segments consisting
2203 of combined data+bss.
2204
2205 To handle the above situation, this routine generates TWO bfd sections
2206 for the single program segment. The first has the length specified by
2207 the file size of the segment, and the second has the length specified
2208 by the difference between the two sizes. In effect, the segment is split
2209 into it's initialized and uninitialized parts.
2210
2211 */
2212
2213 bfd_boolean
2214 _bfd_elf_make_section_from_phdr (bfd *abfd,
2215 Elf_Internal_Phdr *hdr,
2216 int index,
2217 const char *typename)
2218 {
2219 asection *newsect;
2220 char *name;
2221 char namebuf[64];
2222 size_t len;
2223 int split;
2224
2225 split = ((hdr->p_memsz > 0)
2226 && (hdr->p_filesz > 0)
2227 && (hdr->p_memsz > hdr->p_filesz));
2228 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2229 len = strlen (namebuf) + 1;
2230 name = bfd_alloc (abfd, len);
2231 if (!name)
2232 return FALSE;
2233 memcpy (name, namebuf, len);
2234 newsect = bfd_make_section (abfd, name);
2235 if (newsect == NULL)
2236 return FALSE;
2237 newsect->vma = hdr->p_vaddr;
2238 newsect->lma = hdr->p_paddr;
2239 newsect->_raw_size = hdr->p_filesz;
2240 newsect->filepos = hdr->p_offset;
2241 newsect->flags |= SEC_HAS_CONTENTS;
2242 newsect->alignment_power = bfd_log2 (hdr->p_align);
2243 if (hdr->p_type == PT_LOAD)
2244 {
2245 newsect->flags |= SEC_ALLOC;
2246 newsect->flags |= SEC_LOAD;
2247 if (hdr->p_flags & PF_X)
2248 {
2249 /* FIXME: all we known is that it has execute PERMISSION,
2250 may be data. */
2251 newsect->flags |= SEC_CODE;
2252 }
2253 }
2254 if (!(hdr->p_flags & PF_W))
2255 {
2256 newsect->flags |= SEC_READONLY;
2257 }
2258
2259 if (split)
2260 {
2261 sprintf (namebuf, "%s%db", typename, index);
2262 len = strlen (namebuf) + 1;
2263 name = bfd_alloc (abfd, len);
2264 if (!name)
2265 return FALSE;
2266 memcpy (name, namebuf, len);
2267 newsect = bfd_make_section (abfd, name);
2268 if (newsect == NULL)
2269 return FALSE;
2270 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2271 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2272 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2273 if (hdr->p_type == PT_LOAD)
2274 {
2275 newsect->flags |= SEC_ALLOC;
2276 if (hdr->p_flags & PF_X)
2277 newsect->flags |= SEC_CODE;
2278 }
2279 if (!(hdr->p_flags & PF_W))
2280 newsect->flags |= SEC_READONLY;
2281 }
2282
2283 return TRUE;
2284 }
2285
2286 bfd_boolean
2287 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index)
2288 {
2289 const struct elf_backend_data *bed;
2290
2291 switch (hdr->p_type)
2292 {
2293 case PT_NULL:
2294 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2295
2296 case PT_LOAD:
2297 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2298
2299 case PT_DYNAMIC:
2300 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2301
2302 case PT_INTERP:
2303 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2304
2305 case PT_NOTE:
2306 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2307 return FALSE;
2308 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2309 return FALSE;
2310 return TRUE;
2311
2312 case PT_SHLIB:
2313 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2314
2315 case PT_PHDR:
2316 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2317
2318 case PT_GNU_EH_FRAME:
2319 return _bfd_elf_make_section_from_phdr (abfd, hdr, index,
2320 "eh_frame_hdr");
2321
2322 case PT_GNU_STACK:
2323 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack");
2324
2325 case PT_GNU_RELRO:
2326 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro");
2327
2328 default:
2329 /* Check for any processor-specific program segment types.
2330 If no handler for them, default to making "segment" sections. */
2331 bed = get_elf_backend_data (abfd);
2332 if (bed->elf_backend_section_from_phdr)
2333 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2334 else
2335 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2336 }
2337 }
2338
2339 /* Initialize REL_HDR, the section-header for new section, containing
2340 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2341 relocations; otherwise, we use REL relocations. */
2342
2343 bfd_boolean
2344 _bfd_elf_init_reloc_shdr (bfd *abfd,
2345 Elf_Internal_Shdr *rel_hdr,
2346 asection *asect,
2347 bfd_boolean use_rela_p)
2348 {
2349 char *name;
2350 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2351 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2352
2353 name = bfd_alloc (abfd, amt);
2354 if (name == NULL)
2355 return FALSE;
2356 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2357 rel_hdr->sh_name =
2358 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2359 FALSE);
2360 if (rel_hdr->sh_name == (unsigned int) -1)
2361 return FALSE;
2362 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2363 rel_hdr->sh_entsize = (use_rela_p
2364 ? bed->s->sizeof_rela
2365 : bed->s->sizeof_rel);
2366 rel_hdr->sh_addralign = 1 << bed->s->log_file_align;
2367 rel_hdr->sh_flags = 0;
2368 rel_hdr->sh_addr = 0;
2369 rel_hdr->sh_size = 0;
2370 rel_hdr->sh_offset = 0;
2371
2372 return TRUE;
2373 }
2374
2375 /* Set up an ELF internal section header for a section. */
2376
2377 static void
2378 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg)
2379 {
2380 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2381 bfd_boolean *failedptr = failedptrarg;
2382 Elf_Internal_Shdr *this_hdr;
2383
2384 if (*failedptr)
2385 {
2386 /* We already failed; just get out of the bfd_map_over_sections
2387 loop. */
2388 return;
2389 }
2390
2391 this_hdr = &elf_section_data (asect)->this_hdr;
2392
2393 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2394 asect->name, FALSE);
2395 if (this_hdr->sh_name == (unsigned int) -1)
2396 {
2397 *failedptr = TRUE;
2398 return;
2399 }
2400
2401 this_hdr->sh_flags = 0;
2402
2403 if ((asect->flags & SEC_ALLOC) != 0
2404 || asect->user_set_vma)
2405 this_hdr->sh_addr = asect->vma;
2406 else
2407 this_hdr->sh_addr = 0;
2408
2409 this_hdr->sh_offset = 0;
2410 this_hdr->sh_size = asect->_raw_size;
2411 this_hdr->sh_link = 0;
2412 this_hdr->sh_addralign = 1 << asect->alignment_power;
2413 /* The sh_entsize and sh_info fields may have been set already by
2414 copy_private_section_data. */
2415
2416 this_hdr->bfd_section = asect;
2417 this_hdr->contents = NULL;
2418
2419 /* If the section type is unspecified, we set it based on
2420 asect->flags. */
2421 if (this_hdr->sh_type == SHT_NULL)
2422 {
2423 if ((asect->flags & SEC_GROUP) != 0)
2424 {
2425 /* We also need to mark SHF_GROUP here for relocatable
2426 link. */
2427 struct bfd_link_order *l;
2428 asection *elt;
2429
2430 for (l = asect->link_order_head; l != NULL; l = l->next)
2431 if (l->type == bfd_indirect_link_order
2432 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2433 do
2434 {
2435 /* The name is not important. Anything will do. */
2436 elf_group_name (elt->output_section) = "G";
2437 elf_section_flags (elt->output_section) |= SHF_GROUP;
2438
2439 elt = elf_next_in_group (elt);
2440 /* During a relocatable link, the lists are
2441 circular. */
2442 }
2443 while (elt != elf_next_in_group (l->u.indirect.section));
2444
2445 this_hdr->sh_type = SHT_GROUP;
2446 }
2447 else if ((asect->flags & SEC_ALLOC) != 0
2448 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2449 || (asect->flags & SEC_NEVER_LOAD) != 0))
2450 this_hdr->sh_type = SHT_NOBITS;
2451 else
2452 this_hdr->sh_type = SHT_PROGBITS;
2453 }
2454
2455 switch (this_hdr->sh_type)
2456 {
2457 default:
2458 break;
2459
2460 case SHT_STRTAB:
2461 case SHT_INIT_ARRAY:
2462 case SHT_FINI_ARRAY:
2463 case SHT_PREINIT_ARRAY:
2464 case SHT_NOTE:
2465 case SHT_NOBITS:
2466 case SHT_PROGBITS:
2467 break;
2468
2469 case SHT_HASH:
2470 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2471 break;
2472
2473 case SHT_DYNSYM:
2474 this_hdr->sh_entsize = bed->s->sizeof_sym;
2475 break;
2476
2477 case SHT_DYNAMIC:
2478 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2479 break;
2480
2481 case SHT_RELA:
2482 if (get_elf_backend_data (abfd)->may_use_rela_p)
2483 this_hdr->sh_entsize = bed->s->sizeof_rela;
2484 break;
2485
2486 case SHT_REL:
2487 if (get_elf_backend_data (abfd)->may_use_rel_p)
2488 this_hdr->sh_entsize = bed->s->sizeof_rel;
2489 break;
2490
2491 case SHT_GNU_versym:
2492 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2493 break;
2494
2495 case SHT_GNU_verdef:
2496 this_hdr->sh_entsize = 0;
2497 /* objcopy or strip will copy over sh_info, but may not set
2498 cverdefs. The linker will set cverdefs, but sh_info will be
2499 zero. */
2500 if (this_hdr->sh_info == 0)
2501 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2502 else
2503 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2504 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2505 break;
2506
2507 case SHT_GNU_verneed:
2508 this_hdr->sh_entsize = 0;
2509 /* objcopy or strip will copy over sh_info, but may not set
2510 cverrefs. The linker will set cverrefs, but sh_info will be
2511 zero. */
2512 if (this_hdr->sh_info == 0)
2513 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2514 else
2515 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2516 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2517 break;
2518
2519 case SHT_GROUP:
2520 this_hdr->sh_entsize = 4;
2521 break;
2522 }
2523
2524 if ((asect->flags & SEC_ALLOC) != 0)
2525 this_hdr->sh_flags |= SHF_ALLOC;
2526 if ((asect->flags & SEC_READONLY) == 0)
2527 this_hdr->sh_flags |= SHF_WRITE;
2528 if ((asect->flags & SEC_CODE) != 0)
2529 this_hdr->sh_flags |= SHF_EXECINSTR;
2530 if ((asect->flags & SEC_MERGE) != 0)
2531 {
2532 this_hdr->sh_flags |= SHF_MERGE;
2533 this_hdr->sh_entsize = asect->entsize;
2534 if ((asect->flags & SEC_STRINGS) != 0)
2535 this_hdr->sh_flags |= SHF_STRINGS;
2536 }
2537 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2538 this_hdr->sh_flags |= SHF_GROUP;
2539 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2540 {
2541 this_hdr->sh_flags |= SHF_TLS;
2542 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2543 {
2544 struct bfd_link_order *o;
2545
2546 this_hdr->sh_size = 0;
2547 for (o = asect->link_order_head; o != NULL; o = o->next)
2548 if (this_hdr->sh_size < o->offset + o->size)
2549 this_hdr->sh_size = o->offset + o->size;
2550 if (this_hdr->sh_size)
2551 this_hdr->sh_type = SHT_NOBITS;
2552 }
2553 }
2554
2555 /* Check for processor-specific section types. */
2556 if (bed->elf_backend_fake_sections
2557 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2558 *failedptr = TRUE;
2559
2560 /* If the section has relocs, set up a section header for the
2561 SHT_REL[A] section. If two relocation sections are required for
2562 this section, it is up to the processor-specific back-end to
2563 create the other. */
2564 if ((asect->flags & SEC_RELOC) != 0
2565 && !_bfd_elf_init_reloc_shdr (abfd,
2566 &elf_section_data (asect)->rel_hdr,
2567 asect,
2568 asect->use_rela_p))
2569 *failedptr = TRUE;
2570 }
2571
2572 /* Fill in the contents of a SHT_GROUP section. */
2573
2574 void
2575 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2576 {
2577 bfd_boolean *failedptr = failedptrarg;
2578 unsigned long symindx;
2579 asection *elt, *first;
2580 unsigned char *loc;
2581 struct bfd_link_order *l;
2582 bfd_boolean gas;
2583
2584 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2585 || *failedptr)
2586 return;
2587
2588 symindx = 0;
2589 if (elf_group_id (sec) != NULL)
2590 symindx = elf_group_id (sec)->udata.i;
2591
2592 if (symindx == 0)
2593 {
2594 /* If called from the assembler, swap_out_syms will have set up
2595 elf_section_syms; If called for "ld -r", use target_index. */
2596 if (elf_section_syms (abfd) != NULL)
2597 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2598 else
2599 symindx = sec->target_index;
2600 }
2601 elf_section_data (sec)->this_hdr.sh_info = symindx;
2602
2603 /* The contents won't be allocated for "ld -r" or objcopy. */
2604 gas = TRUE;
2605 if (sec->contents == NULL)
2606 {
2607 gas = FALSE;
2608 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2609
2610 /* Arrange for the section to be written out. */
2611 elf_section_data (sec)->this_hdr.contents = sec->contents;
2612 if (sec->contents == NULL)
2613 {
2614 *failedptr = TRUE;
2615 return;
2616 }
2617 }
2618
2619 loc = sec->contents + sec->_raw_size;
2620
2621 /* Get the pointer to the first section in the group that gas
2622 squirreled away here. objcopy arranges for this to be set to the
2623 start of the input section group. */
2624 first = elt = elf_next_in_group (sec);
2625
2626 /* First element is a flag word. Rest of section is elf section
2627 indices for all the sections of the group. Write them backwards
2628 just to keep the group in the same order as given in .section
2629 directives, not that it matters. */
2630 while (elt != NULL)
2631 {
2632 asection *s;
2633 unsigned int idx;
2634
2635 loc -= 4;
2636 s = elt;
2637 if (!gas)
2638 s = s->output_section;
2639 idx = 0;
2640 if (s != NULL)
2641 idx = elf_section_data (s)->this_idx;
2642 H_PUT_32 (abfd, idx, loc);
2643 elt = elf_next_in_group (elt);
2644 if (elt == first)
2645 break;
2646 }
2647
2648 /* If this is a relocatable link, then the above did nothing because
2649 SEC is the output section. Look through the input sections
2650 instead. */
2651 for (l = sec->link_order_head; l != NULL; l = l->next)
2652 if (l->type == bfd_indirect_link_order
2653 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2654 do
2655 {
2656 loc -= 4;
2657 H_PUT_32 (abfd,
2658 elf_section_data (elt->output_section)->this_idx, loc);
2659 elt = elf_next_in_group (elt);
2660 /* During a relocatable link, the lists are circular. */
2661 }
2662 while (elt != elf_next_in_group (l->u.indirect.section));
2663
2664 /* With ld -r, merging SHT_GROUP sections results in wasted space
2665 due to allowing for the flag word on each input. We may well
2666 duplicate entries too. */
2667 while ((loc -= 4) > sec->contents)
2668 H_PUT_32 (abfd, 0, loc);
2669
2670 if (loc != sec->contents)
2671 abort ();
2672
2673 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2674 }
2675
2676 /* Assign all ELF section numbers. The dummy first section is handled here
2677 too. The link/info pointers for the standard section types are filled
2678 in here too, while we're at it. */
2679
2680 static bfd_boolean
2681 assign_section_numbers (bfd *abfd)
2682 {
2683 struct elf_obj_tdata *t = elf_tdata (abfd);
2684 asection *sec;
2685 unsigned int section_number, secn;
2686 Elf_Internal_Shdr **i_shdrp;
2687 bfd_size_type amt;
2688
2689 section_number = 1;
2690
2691 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2692
2693 for (sec = abfd->sections; sec; sec = sec->next)
2694 {
2695 struct bfd_elf_section_data *d = elf_section_data (sec);
2696
2697 if (section_number == SHN_LORESERVE)
2698 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2699 d->this_idx = section_number++;
2700 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2701 if ((sec->flags & SEC_RELOC) == 0)
2702 d->rel_idx = 0;
2703 else
2704 {
2705 if (section_number == SHN_LORESERVE)
2706 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2707 d->rel_idx = section_number++;
2708 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2709 }
2710
2711 if (d->rel_hdr2)
2712 {
2713 if (section_number == SHN_LORESERVE)
2714 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2715 d->rel_idx2 = section_number++;
2716 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2717 }
2718 else
2719 d->rel_idx2 = 0;
2720 }
2721
2722 if (section_number == SHN_LORESERVE)
2723 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2724 t->shstrtab_section = section_number++;
2725 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2726 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2727
2728 if (bfd_get_symcount (abfd) > 0)
2729 {
2730 if (section_number == SHN_LORESERVE)
2731 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2732 t->symtab_section = section_number++;
2733 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2734 if (section_number > SHN_LORESERVE - 2)
2735 {
2736 if (section_number == SHN_LORESERVE)
2737 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2738 t->symtab_shndx_section = section_number++;
2739 t->symtab_shndx_hdr.sh_name
2740 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2741 ".symtab_shndx", FALSE);
2742 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2743 return FALSE;
2744 }
2745 if (section_number == SHN_LORESERVE)
2746 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2747 t->strtab_section = section_number++;
2748 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2749 }
2750
2751 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2752 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2753
2754 elf_numsections (abfd) = section_number;
2755 elf_elfheader (abfd)->e_shnum = section_number;
2756 if (section_number > SHN_LORESERVE)
2757 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2758
2759 /* Set up the list of section header pointers, in agreement with the
2760 indices. */
2761 amt = section_number * sizeof (Elf_Internal_Shdr *);
2762 i_shdrp = bfd_zalloc (abfd, amt);
2763 if (i_shdrp == NULL)
2764 return FALSE;
2765
2766 amt = sizeof (Elf_Internal_Shdr);
2767 i_shdrp[0] = bfd_zalloc (abfd, amt);
2768 if (i_shdrp[0] == NULL)
2769 {
2770 bfd_release (abfd, i_shdrp);
2771 return FALSE;
2772 }
2773
2774 elf_elfsections (abfd) = i_shdrp;
2775
2776 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2777 if (bfd_get_symcount (abfd) > 0)
2778 {
2779 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2780 if (elf_numsections (abfd) > SHN_LORESERVE)
2781 {
2782 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2783 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2784 }
2785 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2786 t->symtab_hdr.sh_link = t->strtab_section;
2787 }
2788 for (sec = abfd->sections; sec; sec = sec->next)
2789 {
2790 struct bfd_elf_section_data *d = elf_section_data (sec);
2791 asection *s;
2792 const char *name;
2793
2794 i_shdrp[d->this_idx] = &d->this_hdr;
2795 if (d->rel_idx != 0)
2796 i_shdrp[d->rel_idx] = &d->rel_hdr;
2797 if (d->rel_idx2 != 0)
2798 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2799
2800 /* Fill in the sh_link and sh_info fields while we're at it. */
2801
2802 /* sh_link of a reloc section is the section index of the symbol
2803 table. sh_info is the section index of the section to which
2804 the relocation entries apply. */
2805 if (d->rel_idx != 0)
2806 {
2807 d->rel_hdr.sh_link = t->symtab_section;
2808 d->rel_hdr.sh_info = d->this_idx;
2809 }
2810 if (d->rel_idx2 != 0)
2811 {
2812 d->rel_hdr2->sh_link = t->symtab_section;
2813 d->rel_hdr2->sh_info = d->this_idx;
2814 }
2815
2816 switch (d->this_hdr.sh_type)
2817 {
2818 case SHT_REL:
2819 case SHT_RELA:
2820 /* A reloc section which we are treating as a normal BFD
2821 section. sh_link is the section index of the symbol
2822 table. sh_info is the section index of the section to
2823 which the relocation entries apply. We assume that an
2824 allocated reloc section uses the dynamic symbol table.
2825 FIXME: How can we be sure? */
2826 s = bfd_get_section_by_name (abfd, ".dynsym");
2827 if (s != NULL)
2828 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2829
2830 /* We look up the section the relocs apply to by name. */
2831 name = sec->name;
2832 if (d->this_hdr.sh_type == SHT_REL)
2833 name += 4;
2834 else
2835 name += 5;
2836 s = bfd_get_section_by_name (abfd, name);
2837 if (s != NULL)
2838 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2839 break;
2840
2841 case SHT_STRTAB:
2842 /* We assume that a section named .stab*str is a stabs
2843 string section. We look for a section with the same name
2844 but without the trailing ``str'', and set its sh_link
2845 field to point to this section. */
2846 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2847 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2848 {
2849 size_t len;
2850 char *alc;
2851
2852 len = strlen (sec->name);
2853 alc = bfd_malloc (len - 2);
2854 if (alc == NULL)
2855 return FALSE;
2856 memcpy (alc, sec->name, len - 3);
2857 alc[len - 3] = '\0';
2858 s = bfd_get_section_by_name (abfd, alc);
2859 free (alc);
2860 if (s != NULL)
2861 {
2862 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2863
2864 /* This is a .stab section. */
2865 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2866 elf_section_data (s)->this_hdr.sh_entsize
2867 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2868 }
2869 }
2870 break;
2871
2872 case SHT_DYNAMIC:
2873 case SHT_DYNSYM:
2874 case SHT_GNU_verneed:
2875 case SHT_GNU_verdef:
2876 /* sh_link is the section header index of the string table
2877 used for the dynamic entries, or the symbol table, or the
2878 version strings. */
2879 s = bfd_get_section_by_name (abfd, ".dynstr");
2880 if (s != NULL)
2881 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2882 break;
2883
2884 case SHT_HASH:
2885 case SHT_GNU_versym:
2886 /* sh_link is the section header index of the symbol table
2887 this hash table or version table is for. */
2888 s = bfd_get_section_by_name (abfd, ".dynsym");
2889 if (s != NULL)
2890 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2891 break;
2892
2893 case SHT_GROUP:
2894 d->this_hdr.sh_link = t->symtab_section;
2895 }
2896 }
2897
2898 for (secn = 1; secn < section_number; ++secn)
2899 if (i_shdrp[secn] == NULL)
2900 i_shdrp[secn] = i_shdrp[0];
2901 else
2902 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2903 i_shdrp[secn]->sh_name);
2904 return TRUE;
2905 }
2906
2907 /* Map symbol from it's internal number to the external number, moving
2908 all local symbols to be at the head of the list. */
2909
2910 static int
2911 sym_is_global (bfd *abfd, asymbol *sym)
2912 {
2913 /* If the backend has a special mapping, use it. */
2914 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2915 if (bed->elf_backend_sym_is_global)
2916 return (*bed->elf_backend_sym_is_global) (abfd, sym);
2917
2918 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2919 || bfd_is_und_section (bfd_get_section (sym))
2920 || bfd_is_com_section (bfd_get_section (sym)));
2921 }
2922
2923 static bfd_boolean
2924 elf_map_symbols (bfd *abfd)
2925 {
2926 unsigned int symcount = bfd_get_symcount (abfd);
2927 asymbol **syms = bfd_get_outsymbols (abfd);
2928 asymbol **sect_syms;
2929 unsigned int num_locals = 0;
2930 unsigned int num_globals = 0;
2931 unsigned int num_locals2 = 0;
2932 unsigned int num_globals2 = 0;
2933 int max_index = 0;
2934 unsigned int idx;
2935 asection *asect;
2936 asymbol **new_syms;
2937 bfd_size_type amt;
2938
2939 #ifdef DEBUG
2940 fprintf (stderr, "elf_map_symbols\n");
2941 fflush (stderr);
2942 #endif
2943
2944 for (asect = abfd->sections; asect; asect = asect->next)
2945 {
2946 if (max_index < asect->index)
2947 max_index = asect->index;
2948 }
2949
2950 max_index++;
2951 amt = max_index * sizeof (asymbol *);
2952 sect_syms = bfd_zalloc (abfd, amt);
2953 if (sect_syms == NULL)
2954 return FALSE;
2955 elf_section_syms (abfd) = sect_syms;
2956 elf_num_section_syms (abfd) = max_index;
2957
2958 /* Init sect_syms entries for any section symbols we have already
2959 decided to output. */
2960 for (idx = 0; idx < symcount; idx++)
2961 {
2962 asymbol *sym = syms[idx];
2963
2964 if ((sym->flags & BSF_SECTION_SYM) != 0
2965 && sym->value == 0)
2966 {
2967 asection *sec;
2968
2969 sec = sym->section;
2970
2971 if (sec->owner != NULL)
2972 {
2973 if (sec->owner != abfd)
2974 {
2975 if (sec->output_offset != 0)
2976 continue;
2977
2978 sec = sec->output_section;
2979
2980 /* Empty sections in the input files may have had a
2981 section symbol created for them. (See the comment
2982 near the end of _bfd_generic_link_output_symbols in
2983 linker.c). If the linker script discards such
2984 sections then we will reach this point. Since we know
2985 that we cannot avoid this case, we detect it and skip
2986 the abort and the assignment to the sect_syms array.
2987 To reproduce this particular case try running the
2988 linker testsuite test ld-scripts/weak.exp for an ELF
2989 port that uses the generic linker. */
2990 if (sec->owner == NULL)
2991 continue;
2992
2993 BFD_ASSERT (sec->owner == abfd);
2994 }
2995 sect_syms[sec->index] = syms[idx];
2996 }
2997 }
2998 }
2999
3000 /* Classify all of the symbols. */
3001 for (idx = 0; idx < symcount; idx++)
3002 {
3003 if (!sym_is_global (abfd, syms[idx]))
3004 num_locals++;
3005 else
3006 num_globals++;
3007 }
3008
3009 /* We will be adding a section symbol for each BFD section. Most normal
3010 sections will already have a section symbol in outsymbols, but
3011 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3012 at least in that case. */
3013 for (asect = abfd->sections; asect; asect = asect->next)
3014 {
3015 if (sect_syms[asect->index] == NULL)
3016 {
3017 if (!sym_is_global (abfd, asect->symbol))
3018 num_locals++;
3019 else
3020 num_globals++;
3021 }
3022 }
3023
3024 /* Now sort the symbols so the local symbols are first. */
3025 amt = (num_locals + num_globals) * sizeof (asymbol *);
3026 new_syms = bfd_alloc (abfd, amt);
3027
3028 if (new_syms == NULL)
3029 return FALSE;
3030
3031 for (idx = 0; idx < symcount; idx++)
3032 {
3033 asymbol *sym = syms[idx];
3034 unsigned int i;
3035
3036 if (!sym_is_global (abfd, sym))
3037 i = num_locals2++;
3038 else
3039 i = num_locals + num_globals2++;
3040 new_syms[i] = sym;
3041 sym->udata.i = i + 1;
3042 }
3043 for (asect = abfd->sections; asect; asect = asect->next)
3044 {
3045 if (sect_syms[asect->index] == NULL)
3046 {
3047 asymbol *sym = asect->symbol;
3048 unsigned int i;
3049
3050 sect_syms[asect->index] = sym;
3051 if (!sym_is_global (abfd, sym))
3052 i = num_locals2++;
3053 else
3054 i = num_locals + num_globals2++;
3055 new_syms[i] = sym;
3056 sym->udata.i = i + 1;
3057 }
3058 }
3059
3060 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3061
3062 elf_num_locals (abfd) = num_locals;
3063 elf_num_globals (abfd) = num_globals;
3064 return TRUE;
3065 }
3066
3067 /* Align to the maximum file alignment that could be required for any
3068 ELF data structure. */
3069
3070 static inline file_ptr
3071 align_file_position (file_ptr off, int align)
3072 {
3073 return (off + align - 1) & ~(align - 1);
3074 }
3075
3076 /* Assign a file position to a section, optionally aligning to the
3077 required section alignment. */
3078
3079 file_ptr
3080 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3081 file_ptr offset,
3082 bfd_boolean align)
3083 {
3084 if (align)
3085 {
3086 unsigned int al;
3087
3088 al = i_shdrp->sh_addralign;
3089 if (al > 1)
3090 offset = BFD_ALIGN (offset, al);
3091 }
3092 i_shdrp->sh_offset = offset;
3093 if (i_shdrp->bfd_section != NULL)
3094 i_shdrp->bfd_section->filepos = offset;
3095 if (i_shdrp->sh_type != SHT_NOBITS)
3096 offset += i_shdrp->sh_size;
3097 return offset;
3098 }
3099
3100 /* Compute the file positions we are going to put the sections at, and
3101 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3102 is not NULL, this is being called by the ELF backend linker. */
3103
3104 bfd_boolean
3105 _bfd_elf_compute_section_file_positions (bfd *abfd,
3106 struct bfd_link_info *link_info)
3107 {
3108 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3109 bfd_boolean failed;
3110 struct bfd_strtab_hash *strtab;
3111 Elf_Internal_Shdr *shstrtab_hdr;
3112
3113 if (abfd->output_has_begun)
3114 return TRUE;
3115
3116 /* Do any elf backend specific processing first. */
3117 if (bed->elf_backend_begin_write_processing)
3118 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3119
3120 if (! prep_headers (abfd))
3121 return FALSE;
3122
3123 /* Post process the headers if necessary. */
3124 if (bed->elf_backend_post_process_headers)
3125 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3126
3127 failed = FALSE;
3128 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3129 if (failed)
3130 return FALSE;
3131
3132 if (!assign_section_numbers (abfd))
3133 return FALSE;
3134
3135 /* The backend linker builds symbol table information itself. */
3136 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3137 {
3138 /* Non-zero if doing a relocatable link. */
3139 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3140
3141 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3142 return FALSE;
3143 }
3144
3145 if (link_info == NULL)
3146 {
3147 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3148 if (failed)
3149 return FALSE;
3150 }
3151
3152 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3153 /* sh_name was set in prep_headers. */
3154 shstrtab_hdr->sh_type = SHT_STRTAB;
3155 shstrtab_hdr->sh_flags = 0;
3156 shstrtab_hdr->sh_addr = 0;
3157 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3158 shstrtab_hdr->sh_entsize = 0;
3159 shstrtab_hdr->sh_link = 0;
3160 shstrtab_hdr->sh_info = 0;
3161 /* sh_offset is set in assign_file_positions_except_relocs. */
3162 shstrtab_hdr->sh_addralign = 1;
3163
3164 if (!assign_file_positions_except_relocs (abfd, link_info))
3165 return FALSE;
3166
3167 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3168 {
3169 file_ptr off;
3170 Elf_Internal_Shdr *hdr;
3171
3172 off = elf_tdata (abfd)->next_file_pos;
3173
3174 hdr = &elf_tdata (abfd)->symtab_hdr;
3175 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3176
3177 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3178 if (hdr->sh_size != 0)
3179 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3180
3181 hdr = &elf_tdata (abfd)->strtab_hdr;
3182 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3183
3184 elf_tdata (abfd)->next_file_pos = off;
3185
3186 /* Now that we know where the .strtab section goes, write it
3187 out. */
3188 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3189 || ! _bfd_stringtab_emit (abfd, strtab))
3190 return FALSE;
3191 _bfd_stringtab_free (strtab);
3192 }
3193
3194 abfd->output_has_begun = TRUE;
3195
3196 return TRUE;
3197 }
3198
3199 /* Create a mapping from a set of sections to a program segment. */
3200
3201 static struct elf_segment_map *
3202 make_mapping (bfd *abfd,
3203 asection **sections,
3204 unsigned int from,
3205 unsigned int to,
3206 bfd_boolean phdr)
3207 {
3208 struct elf_segment_map *m;
3209 unsigned int i;
3210 asection **hdrpp;
3211 bfd_size_type amt;
3212
3213 amt = sizeof (struct elf_segment_map);
3214 amt += (to - from - 1) * sizeof (asection *);
3215 m = bfd_zalloc (abfd, amt);
3216 if (m == NULL)
3217 return NULL;
3218 m->next = NULL;
3219 m->p_type = PT_LOAD;
3220 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3221 m->sections[i - from] = *hdrpp;
3222 m->count = to - from;
3223
3224 if (from == 0 && phdr)
3225 {
3226 /* Include the headers in the first PT_LOAD segment. */
3227 m->includes_filehdr = 1;
3228 m->includes_phdrs = 1;
3229 }
3230
3231 return m;
3232 }
3233
3234 /* Set up a mapping from BFD sections to program segments. */
3235
3236 static bfd_boolean
3237 map_sections_to_segments (bfd *abfd)
3238 {
3239 asection **sections = NULL;
3240 asection *s;
3241 unsigned int i;
3242 unsigned int count;
3243 struct elf_segment_map *mfirst;
3244 struct elf_segment_map **pm;
3245 struct elf_segment_map *m;
3246 asection *last_hdr;
3247 bfd_vma last_size;
3248 unsigned int phdr_index;
3249 bfd_vma maxpagesize;
3250 asection **hdrpp;
3251 bfd_boolean phdr_in_segment = TRUE;
3252 bfd_boolean writable;
3253 int tls_count = 0;
3254 asection *first_tls = NULL;
3255 asection *dynsec, *eh_frame_hdr;
3256 bfd_size_type amt;
3257
3258 if (elf_tdata (abfd)->segment_map != NULL)
3259 return TRUE;
3260
3261 if (bfd_count_sections (abfd) == 0)
3262 return TRUE;
3263
3264 /* Select the allocated sections, and sort them. */
3265
3266 amt = bfd_count_sections (abfd) * sizeof (asection *);
3267 sections = bfd_malloc (amt);
3268 if (sections == NULL)
3269 goto error_return;
3270
3271 i = 0;
3272 for (s = abfd->sections; s != NULL; s = s->next)
3273 {
3274 if ((s->flags & SEC_ALLOC) != 0)
3275 {
3276 sections[i] = s;
3277 ++i;
3278 }
3279 }
3280 BFD_ASSERT (i <= bfd_count_sections (abfd));
3281 count = i;
3282
3283 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3284
3285 /* Build the mapping. */
3286
3287 mfirst = NULL;
3288 pm = &mfirst;
3289
3290 /* If we have a .interp section, then create a PT_PHDR segment for
3291 the program headers and a PT_INTERP segment for the .interp
3292 section. */
3293 s = bfd_get_section_by_name (abfd, ".interp");
3294 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3295 {
3296 amt = sizeof (struct elf_segment_map);
3297 m = bfd_zalloc (abfd, amt);
3298 if (m == NULL)
3299 goto error_return;
3300 m->next = NULL;
3301 m->p_type = PT_PHDR;
3302 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3303 m->p_flags = PF_R | PF_X;
3304 m->p_flags_valid = 1;
3305 m->includes_phdrs = 1;
3306
3307 *pm = m;
3308 pm = &m->next;
3309
3310 amt = sizeof (struct elf_segment_map);
3311 m = bfd_zalloc (abfd, amt);
3312 if (m == NULL)
3313 goto error_return;
3314 m->next = NULL;
3315 m->p_type = PT_INTERP;
3316 m->count = 1;
3317 m->sections[0] = s;
3318
3319 *pm = m;
3320 pm = &m->next;
3321 }
3322
3323 /* Look through the sections. We put sections in the same program
3324 segment when the start of the second section can be placed within
3325 a few bytes of the end of the first section. */
3326 last_hdr = NULL;
3327 last_size = 0;
3328 phdr_index = 0;
3329 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3330 writable = FALSE;
3331 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3332 if (dynsec != NULL
3333 && (dynsec->flags & SEC_LOAD) == 0)
3334 dynsec = NULL;
3335
3336 /* Deal with -Ttext or something similar such that the first section
3337 is not adjacent to the program headers. This is an
3338 approximation, since at this point we don't know exactly how many
3339 program headers we will need. */
3340 if (count > 0)
3341 {
3342 bfd_size_type phdr_size;
3343
3344 phdr_size = elf_tdata (abfd)->program_header_size;
3345 if (phdr_size == 0)
3346 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3347 if ((abfd->flags & D_PAGED) == 0
3348 || sections[0]->lma < phdr_size
3349 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3350 phdr_in_segment = FALSE;
3351 }
3352
3353 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3354 {
3355 asection *hdr;
3356 bfd_boolean new_segment;
3357
3358 hdr = *hdrpp;
3359
3360 /* See if this section and the last one will fit in the same
3361 segment. */
3362
3363 if (last_hdr == NULL)
3364 {
3365 /* If we don't have a segment yet, then we don't need a new
3366 one (we build the last one after this loop). */
3367 new_segment = FALSE;
3368 }
3369 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3370 {
3371 /* If this section has a different relation between the
3372 virtual address and the load address, then we need a new
3373 segment. */
3374 new_segment = TRUE;
3375 }
3376 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
3377 < BFD_ALIGN (hdr->lma, maxpagesize))
3378 {
3379 /* If putting this section in this segment would force us to
3380 skip a page in the segment, then we need a new segment. */
3381 new_segment = TRUE;
3382 }
3383 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3384 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
3385 {
3386 /* We don't want to put a loadable section after a
3387 nonloadable section in the same segment.
3388 Consider .tbss sections as loadable for this purpose. */
3389 new_segment = TRUE;
3390 }
3391 else if ((abfd->flags & D_PAGED) == 0)
3392 {
3393 /* If the file is not demand paged, which means that we
3394 don't require the sections to be correctly aligned in the
3395 file, then there is no other reason for a new segment. */
3396 new_segment = FALSE;
3397 }
3398 else if (! writable
3399 && (hdr->flags & SEC_READONLY) == 0
3400 && (((last_hdr->lma + last_size - 1)
3401 & ~(maxpagesize - 1))
3402 != (hdr->lma & ~(maxpagesize - 1))))
3403 {
3404 /* We don't want to put a writable section in a read only
3405 segment, unless they are on the same page in memory
3406 anyhow. We already know that the last section does not
3407 bring us past the current section on the page, so the
3408 only case in which the new section is not on the same
3409 page as the previous section is when the previous section
3410 ends precisely on a page boundary. */
3411 new_segment = TRUE;
3412 }
3413 else
3414 {
3415 /* Otherwise, we can use the same segment. */
3416 new_segment = FALSE;
3417 }
3418
3419 if (! new_segment)
3420 {
3421 if ((hdr->flags & SEC_READONLY) == 0)
3422 writable = TRUE;
3423 last_hdr = hdr;
3424 /* .tbss sections effectively have zero size. */
3425 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3426 last_size = hdr->_raw_size;
3427 else
3428 last_size = 0;
3429 continue;
3430 }
3431
3432 /* We need a new program segment. We must create a new program
3433 header holding all the sections from phdr_index until hdr. */
3434
3435 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3436 if (m == NULL)
3437 goto error_return;
3438
3439 *pm = m;
3440 pm = &m->next;
3441
3442 if ((hdr->flags & SEC_READONLY) == 0)
3443 writable = TRUE;
3444 else
3445 writable = FALSE;
3446
3447 last_hdr = hdr;
3448 /* .tbss sections effectively have zero size. */
3449 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
3450 last_size = hdr->_raw_size;
3451 else
3452 last_size = 0;
3453 phdr_index = i;
3454 phdr_in_segment = FALSE;
3455 }
3456
3457 /* Create a final PT_LOAD program segment. */
3458 if (last_hdr != NULL)
3459 {
3460 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3461 if (m == NULL)
3462 goto error_return;
3463
3464 *pm = m;
3465 pm = &m->next;
3466 }
3467
3468 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3469 if (dynsec != NULL)
3470 {
3471 amt = sizeof (struct elf_segment_map);
3472 m = bfd_zalloc (abfd, amt);
3473 if (m == NULL)
3474 goto error_return;
3475 m->next = NULL;
3476 m->p_type = PT_DYNAMIC;
3477 m->count = 1;
3478 m->sections[0] = dynsec;
3479
3480 *pm = m;
3481 pm = &m->next;
3482 }
3483
3484 /* For each loadable .note section, add a PT_NOTE segment. We don't
3485 use bfd_get_section_by_name, because if we link together
3486 nonloadable .note sections and loadable .note sections, we will
3487 generate two .note sections in the output file. FIXME: Using
3488 names for section types is bogus anyhow. */
3489 for (s = abfd->sections; s != NULL; s = s->next)
3490 {
3491 if ((s->flags & SEC_LOAD) != 0
3492 && strncmp (s->name, ".note", 5) == 0)
3493 {
3494 amt = sizeof (struct elf_segment_map);
3495 m = bfd_zalloc (abfd, amt);
3496 if (m == NULL)
3497 goto error_return;
3498 m->next = NULL;
3499 m->p_type = PT_NOTE;
3500 m->count = 1;
3501 m->sections[0] = s;
3502
3503 *pm = m;
3504 pm = &m->next;
3505 }
3506 if (s->flags & SEC_THREAD_LOCAL)
3507 {
3508 if (! tls_count)
3509 first_tls = s;
3510 tls_count++;
3511 }
3512 }
3513
3514 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3515 if (tls_count > 0)
3516 {
3517 int i;
3518
3519 amt = sizeof (struct elf_segment_map);
3520 amt += (tls_count - 1) * sizeof (asection *);
3521 m = bfd_zalloc (abfd, amt);
3522 if (m == NULL)
3523 goto error_return;
3524 m->next = NULL;
3525 m->p_type = PT_TLS;
3526 m->count = tls_count;
3527 /* Mandated PF_R. */
3528 m->p_flags = PF_R;
3529 m->p_flags_valid = 1;
3530 for (i = 0; i < tls_count; ++i)
3531 {
3532 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3533 m->sections[i] = first_tls;
3534 first_tls = first_tls->next;
3535 }
3536
3537 *pm = m;
3538 pm = &m->next;
3539 }
3540
3541 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3542 segment. */
3543 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3544 if (eh_frame_hdr != NULL
3545 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3546 {
3547 amt = sizeof (struct elf_segment_map);
3548 m = bfd_zalloc (abfd, amt);
3549 if (m == NULL)
3550 goto error_return;
3551 m->next = NULL;
3552 m->p_type = PT_GNU_EH_FRAME;
3553 m->count = 1;
3554 m->sections[0] = eh_frame_hdr->output_section;
3555
3556 *pm = m;
3557 pm = &m->next;
3558 }
3559
3560 if (elf_tdata (abfd)->stack_flags)
3561 {
3562 amt = sizeof (struct elf_segment_map);
3563 m = bfd_zalloc (abfd, amt);
3564 if (m == NULL)
3565 goto error_return;
3566 m->next = NULL;
3567 m->p_type = PT_GNU_STACK;
3568 m->p_flags = elf_tdata (abfd)->stack_flags;
3569 m->p_flags_valid = 1;
3570
3571 *pm = m;
3572 pm = &m->next;
3573 }
3574
3575 if (elf_tdata (abfd)->relro)
3576 {
3577 amt = sizeof (struct elf_segment_map);
3578 m = bfd_zalloc (abfd, amt);
3579 if (m == NULL)
3580 goto error_return;
3581 m->next = NULL;
3582 m->p_type = PT_GNU_RELRO;
3583 m->p_flags = PF_R;
3584 m->p_flags_valid = 1;
3585
3586 *pm = m;
3587 pm = &m->next;
3588 }
3589
3590 free (sections);
3591 sections = NULL;
3592
3593 elf_tdata (abfd)->segment_map = mfirst;
3594 return TRUE;
3595
3596 error_return:
3597 if (sections != NULL)
3598 free (sections);
3599 return FALSE;
3600 }
3601
3602 /* Sort sections by address. */
3603
3604 static int
3605 elf_sort_sections (const void *arg1, const void *arg2)
3606 {
3607 const asection *sec1 = *(const asection **) arg1;
3608 const asection *sec2 = *(const asection **) arg2;
3609 bfd_size_type size1, size2;
3610
3611 /* Sort by LMA first, since this is the address used to
3612 place the section into a segment. */
3613 if (sec1->lma < sec2->lma)
3614 return -1;
3615 else if (sec1->lma > sec2->lma)
3616 return 1;
3617
3618 /* Then sort by VMA. Normally the LMA and the VMA will be
3619 the same, and this will do nothing. */
3620 if (sec1->vma < sec2->vma)
3621 return -1;
3622 else if (sec1->vma > sec2->vma)
3623 return 1;
3624
3625 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3626
3627 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
3628
3629 if (TOEND (sec1))
3630 {
3631 if (TOEND (sec2))
3632 {
3633 /* If the indicies are the same, do not return 0
3634 here, but continue to try the next comparison. */
3635 if (sec1->target_index - sec2->target_index != 0)
3636 return sec1->target_index - sec2->target_index;
3637 }
3638 else
3639 return 1;
3640 }
3641 else if (TOEND (sec2))
3642 return -1;
3643
3644 #undef TOEND
3645
3646 /* Sort by size, to put zero sized sections
3647 before others at the same address. */
3648
3649 size1 = (sec1->flags & SEC_LOAD) ? sec1->_raw_size : 0;
3650 size2 = (sec2->flags & SEC_LOAD) ? sec2->_raw_size : 0;
3651
3652 if (size1 < size2)
3653 return -1;
3654 if (size1 > size2)
3655 return 1;
3656
3657 return sec1->target_index - sec2->target_index;
3658 }
3659
3660 /* Ian Lance Taylor writes:
3661
3662 We shouldn't be using % with a negative signed number. That's just
3663 not good. We have to make sure either that the number is not
3664 negative, or that the number has an unsigned type. When the types
3665 are all the same size they wind up as unsigned. When file_ptr is a
3666 larger signed type, the arithmetic winds up as signed long long,
3667 which is wrong.
3668
3669 What we're trying to say here is something like ``increase OFF by
3670 the least amount that will cause it to be equal to the VMA modulo
3671 the page size.'' */
3672 /* In other words, something like:
3673
3674 vma_offset = m->sections[0]->vma % bed->maxpagesize;
3675 off_offset = off % bed->maxpagesize;
3676 if (vma_offset < off_offset)
3677 adjustment = vma_offset + bed->maxpagesize - off_offset;
3678 else
3679 adjustment = vma_offset - off_offset;
3680
3681 which can can be collapsed into the expression below. */
3682
3683 static file_ptr
3684 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
3685 {
3686 return ((vma - off) % maxpagesize);
3687 }
3688
3689 /* Assign file positions to the sections based on the mapping from
3690 sections to segments. This function also sets up some fields in
3691 the file header, and writes out the program headers. */
3692
3693 static bfd_boolean
3694 assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info)
3695 {
3696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3697 unsigned int count;
3698 struct elf_segment_map *m;
3699 unsigned int alloc;
3700 Elf_Internal_Phdr *phdrs;
3701 file_ptr off, voff;
3702 bfd_vma filehdr_vaddr, filehdr_paddr;
3703 bfd_vma phdrs_vaddr, phdrs_paddr;
3704 Elf_Internal_Phdr *p;
3705 bfd_size_type amt;
3706
3707 if (elf_tdata (abfd)->segment_map == NULL)
3708 {
3709 if (! map_sections_to_segments (abfd))
3710 return FALSE;
3711 }
3712 else
3713 {
3714 /* The placement algorithm assumes that non allocated sections are
3715 not in PT_LOAD segments. We ensure this here by removing such
3716 sections from the segment map. */
3717 for (m = elf_tdata (abfd)->segment_map;
3718 m != NULL;
3719 m = m->next)
3720 {
3721 unsigned int new_count;
3722 unsigned int i;
3723
3724 if (m->p_type != PT_LOAD)
3725 continue;
3726
3727 new_count = 0;
3728 for (i = 0; i < m->count; i ++)
3729 {
3730 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3731 {
3732 if (i != new_count)
3733 m->sections[new_count] = m->sections[i];
3734
3735 new_count ++;
3736 }
3737 }
3738
3739 if (new_count != m->count)
3740 m->count = new_count;
3741 }
3742 }
3743
3744 if (bed->elf_backend_modify_segment_map)
3745 {
3746 if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info))
3747 return FALSE;
3748 }
3749
3750 count = 0;
3751 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3752 ++count;
3753
3754 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3755 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3756 elf_elfheader (abfd)->e_phnum = count;
3757
3758 if (count == 0)
3759 return TRUE;
3760
3761 /* If we already counted the number of program segments, make sure
3762 that we allocated enough space. This happens when SIZEOF_HEADERS
3763 is used in a linker script. */
3764 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3765 if (alloc != 0 && count > alloc)
3766 {
3767 ((*_bfd_error_handler)
3768 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3769 bfd_get_filename (abfd), alloc, count));
3770 bfd_set_error (bfd_error_bad_value);
3771 return FALSE;
3772 }
3773
3774 if (alloc == 0)
3775 alloc = count;
3776
3777 amt = alloc * sizeof (Elf_Internal_Phdr);
3778 phdrs = bfd_alloc (abfd, amt);
3779 if (phdrs == NULL)
3780 return FALSE;
3781
3782 off = bed->s->sizeof_ehdr;
3783 off += alloc * bed->s->sizeof_phdr;
3784
3785 filehdr_vaddr = 0;
3786 filehdr_paddr = 0;
3787 phdrs_vaddr = 0;
3788 phdrs_paddr = 0;
3789
3790 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3791 m != NULL;
3792 m = m->next, p++)
3793 {
3794 unsigned int i;
3795 asection **secpp;
3796
3797 /* If elf_segment_map is not from map_sections_to_segments, the
3798 sections may not be correctly ordered. NOTE: sorting should
3799 not be done to the PT_NOTE section of a corefile, which may
3800 contain several pseudo-sections artificially created by bfd.
3801 Sorting these pseudo-sections breaks things badly. */
3802 if (m->count > 1
3803 && !(elf_elfheader (abfd)->e_type == ET_CORE
3804 && m->p_type == PT_NOTE))
3805 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3806 elf_sort_sections);
3807
3808 p->p_type = m->p_type;
3809 p->p_flags = m->p_flags;
3810
3811 if (p->p_type == PT_LOAD
3812 && m->count > 0
3813 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3814 {
3815 if ((abfd->flags & D_PAGED) != 0)
3816 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3817 bed->maxpagesize);
3818 else
3819 {
3820 bfd_size_type align;
3821
3822 align = 0;
3823 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3824 {
3825 bfd_size_type secalign;
3826
3827 secalign = bfd_get_section_alignment (abfd, *secpp);
3828 if (secalign > align)
3829 align = secalign;
3830 }
3831
3832 off += vma_page_aligned_bias (m->sections[0]->vma, off,
3833 1 << align);
3834 }
3835 }
3836
3837 if (m->count == 0)
3838 p->p_vaddr = 0;
3839 else
3840 p->p_vaddr = m->sections[0]->vma;
3841
3842 if (m->p_paddr_valid)
3843 p->p_paddr = m->p_paddr;
3844 else if (m->count == 0)
3845 p->p_paddr = 0;
3846 else
3847 p->p_paddr = m->sections[0]->lma;
3848
3849 if (p->p_type == PT_LOAD
3850 && (abfd->flags & D_PAGED) != 0)
3851 p->p_align = bed->maxpagesize;
3852 else if (m->count == 0)
3853 p->p_align = 1 << bed->s->log_file_align;
3854 else
3855 p->p_align = 0;
3856
3857 p->p_offset = 0;
3858 p->p_filesz = 0;
3859 p->p_memsz = 0;
3860
3861 if (m->includes_filehdr)
3862 {
3863 if (! m->p_flags_valid)
3864 p->p_flags |= PF_R;
3865 p->p_offset = 0;
3866 p->p_filesz = bed->s->sizeof_ehdr;
3867 p->p_memsz = bed->s->sizeof_ehdr;
3868 if (m->count > 0)
3869 {
3870 BFD_ASSERT (p->p_type == PT_LOAD);
3871
3872 if (p->p_vaddr < (bfd_vma) off)
3873 {
3874 (*_bfd_error_handler)
3875 (_("%s: Not enough room for program headers, try linking with -N"),
3876 bfd_get_filename (abfd));
3877 bfd_set_error (bfd_error_bad_value);
3878 return FALSE;
3879 }
3880
3881 p->p_vaddr -= off;
3882 if (! m->p_paddr_valid)
3883 p->p_paddr -= off;
3884 }
3885 if (p->p_type == PT_LOAD)
3886 {
3887 filehdr_vaddr = p->p_vaddr;
3888 filehdr_paddr = p->p_paddr;
3889 }
3890 }
3891
3892 if (m->includes_phdrs)
3893 {
3894 if (! m->p_flags_valid)
3895 p->p_flags |= PF_R;
3896
3897 if (m->includes_filehdr)
3898 {
3899 if (p->p_type == PT_LOAD)
3900 {
3901 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3902 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3903 }
3904 }
3905 else
3906 {
3907 p->p_offset = bed->s->sizeof_ehdr;
3908
3909 if (m->count > 0)
3910 {
3911 BFD_ASSERT (p->p_type == PT_LOAD);
3912 p->p_vaddr -= off - p->p_offset;
3913 if (! m->p_paddr_valid)
3914 p->p_paddr -= off - p->p_offset;
3915 }
3916
3917 if (p->p_type == PT_LOAD)
3918 {
3919 phdrs_vaddr = p->p_vaddr;
3920 phdrs_paddr = p->p_paddr;
3921 }
3922 else
3923 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3924 }
3925
3926 p->p_filesz += alloc * bed->s->sizeof_phdr;
3927 p->p_memsz += alloc * bed->s->sizeof_phdr;
3928 }
3929
3930 if (p->p_type == PT_LOAD
3931 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3932 {
3933 if (! m->includes_filehdr && ! m->includes_phdrs)
3934 p->p_offset = off;
3935 else
3936 {
3937 file_ptr adjust;
3938
3939 adjust = off - (p->p_offset + p->p_filesz);
3940 p->p_filesz += adjust;
3941 p->p_memsz += adjust;
3942 }
3943 }
3944
3945 voff = off;
3946
3947 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3948 {
3949 asection *sec;
3950 flagword flags;
3951 bfd_size_type align;
3952
3953 sec = *secpp;
3954 flags = sec->flags;
3955 align = 1 << bfd_get_section_alignment (abfd, sec);
3956
3957 /* The section may have artificial alignment forced by a
3958 link script. Notice this case by the gap between the
3959 cumulative phdr lma and the section's lma. */
3960 if (p->p_paddr + p->p_memsz < sec->lma)
3961 {
3962 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3963
3964 p->p_memsz += adjust;
3965 if (p->p_type == PT_LOAD
3966 || (p->p_type == PT_NOTE
3967 && bfd_get_format (abfd) == bfd_core))
3968 {
3969 off += adjust;
3970 voff += adjust;
3971 }
3972 if ((flags & SEC_LOAD) != 0
3973 || (flags & SEC_THREAD_LOCAL) != 0)
3974 p->p_filesz += adjust;
3975 }
3976
3977 if (p->p_type == PT_LOAD)
3978 {
3979 bfd_signed_vma adjust;
3980
3981 if ((flags & SEC_LOAD) != 0)
3982 {
3983 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3984 if (adjust < 0)
3985 adjust = 0;
3986 }
3987 else if ((flags & SEC_ALLOC) != 0)
3988 {
3989 /* The section VMA must equal the file position
3990 modulo the page size. FIXME: I'm not sure if
3991 this adjustment is really necessary. We used to
3992 not have the SEC_LOAD case just above, and then
3993 this was necessary, but now I'm not sure. */
3994 if ((abfd->flags & D_PAGED) != 0)
3995 adjust = vma_page_aligned_bias (sec->vma, voff,
3996 bed->maxpagesize);
3997 else
3998 adjust = vma_page_aligned_bias (sec->vma, voff,
3999 align);
4000 }
4001 else
4002 adjust = 0;
4003
4004 if (adjust != 0)
4005 {
4006 if (i == 0)
4007 {
4008 (* _bfd_error_handler) (_("\
4009 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
4010 bfd_section_name (abfd, sec),
4011 sec->lma,
4012 p->p_paddr);
4013 return FALSE;
4014 }
4015 p->p_memsz += adjust;
4016 off += adjust;
4017 voff += adjust;
4018 if ((flags & SEC_LOAD) != 0)
4019 p->p_filesz += adjust;
4020 }
4021
4022 sec->filepos = off;
4023
4024 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
4025 used in a linker script we may have a section with
4026 SEC_LOAD clear but which is supposed to have
4027 contents. */
4028 if ((flags & SEC_LOAD) != 0
4029 || (flags & SEC_HAS_CONTENTS) != 0)
4030 off += sec->_raw_size;
4031
4032 if ((flags & SEC_ALLOC) != 0
4033 && ((flags & SEC_LOAD) != 0
4034 || (flags & SEC_THREAD_LOCAL) == 0))
4035 voff += sec->_raw_size;
4036 }
4037
4038 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4039 {
4040 /* The actual "note" segment has i == 0.
4041 This is the one that actually contains everything. */
4042 if (i == 0)
4043 {
4044 sec->filepos = off;
4045 p->p_filesz = sec->_raw_size;
4046 off += sec->_raw_size;
4047 voff = off;
4048 }
4049 else
4050 {
4051 /* Fake sections -- don't need to be written. */
4052 sec->filepos = 0;
4053 sec->_raw_size = 0;
4054 flags = sec->flags = 0;
4055 }
4056 p->p_memsz = 0;
4057 p->p_align = 1;
4058 }
4059 else
4060 {
4061 if ((sec->flags & SEC_LOAD) != 0
4062 || (sec->flags & SEC_THREAD_LOCAL) == 0
4063 || p->p_type == PT_TLS)
4064 p->p_memsz += sec->_raw_size;
4065
4066 if ((flags & SEC_LOAD) != 0)
4067 p->p_filesz += sec->_raw_size;
4068
4069 if (p->p_type == PT_TLS
4070 && sec->_raw_size == 0
4071 && (sec->flags & SEC_HAS_CONTENTS) == 0)
4072 {
4073 struct bfd_link_order *o;
4074 bfd_vma tbss_size = 0;
4075
4076 for (o = sec->link_order_head; o != NULL; o = o->next)
4077 if (tbss_size < o->offset + o->size)
4078 tbss_size = o->offset + o->size;
4079
4080 p->p_memsz += tbss_size;
4081 }
4082
4083 if (align > p->p_align
4084 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
4085 p->p_align = align;
4086 }
4087
4088 if (! m->p_flags_valid)
4089 {
4090 p->p_flags |= PF_R;
4091 if ((flags & SEC_CODE) != 0)
4092 p->p_flags |= PF_X;
4093 if ((flags & SEC_READONLY) == 0)
4094 p->p_flags |= PF_W;
4095 }
4096 }
4097 }
4098
4099 /* Now that we have set the section file positions, we can set up
4100 the file positions for the non PT_LOAD segments. */
4101 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
4102 m != NULL;
4103 m = m->next, p++)
4104 {
4105 if (p->p_type != PT_LOAD && m->count > 0)
4106 {
4107 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
4108 p->p_offset = m->sections[0]->filepos;
4109 }
4110 if (m->count == 0)
4111 {
4112 if (m->includes_filehdr)
4113 {
4114 p->p_vaddr = filehdr_vaddr;
4115 if (! m->p_paddr_valid)
4116 p->p_paddr = filehdr_paddr;
4117 }
4118 else if (m->includes_phdrs)
4119 {
4120 p->p_vaddr = phdrs_vaddr;
4121 if (! m->p_paddr_valid)
4122 p->p_paddr = phdrs_paddr;
4123 }
4124 else if (p->p_type == PT_GNU_RELRO)
4125 {
4126 Elf_Internal_Phdr *lp;
4127
4128 for (lp = phdrs; lp < phdrs + count; ++lp)
4129 {
4130 if (lp->p_type == PT_LOAD
4131 && lp->p_vaddr <= link_info->relro_end
4132 && lp->p_vaddr >= link_info->relro_start
4133 && lp->p_vaddr + lp->p_filesz
4134 >= link_info->relro_end)
4135 break;
4136 }
4137
4138 if (lp < phdrs + count
4139 && link_info->relro_end > lp->p_vaddr)
4140 {
4141 p->p_vaddr = lp->p_vaddr;
4142 p->p_paddr = lp->p_paddr;
4143 p->p_offset = lp->p_offset;
4144 p->p_filesz = link_info->relro_end - lp->p_vaddr;
4145 p->p_memsz = p->p_filesz;
4146 p->p_align = 1;
4147 p->p_flags = (lp->p_flags & ~PF_W);
4148 }
4149 else
4150 {
4151 memset (p, 0, sizeof *p);
4152 p->p_type = PT_NULL;
4153 }
4154 }
4155 }
4156 }
4157
4158 /* Clear out any program headers we allocated but did not use. */
4159 for (; count < alloc; count++, p++)
4160 {
4161 memset (p, 0, sizeof *p);
4162 p->p_type = PT_NULL;
4163 }
4164
4165 elf_tdata (abfd)->phdr = phdrs;
4166
4167 elf_tdata (abfd)->next_file_pos = off;
4168
4169 /* Write out the program headers. */
4170 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4171 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4172 return FALSE;
4173
4174 return TRUE;
4175 }
4176
4177 /* Get the size of the program header.
4178
4179 If this is called by the linker before any of the section VMA's are set, it
4180 can't calculate the correct value for a strange memory layout. This only
4181 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4182 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4183 data segment (exclusive of .interp and .dynamic).
4184
4185 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4186 will be two segments. */
4187
4188 static bfd_size_type
4189 get_program_header_size (bfd *abfd)
4190 {
4191 size_t segs;
4192 asection *s;
4193 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4194
4195 /* We can't return a different result each time we're called. */
4196 if (elf_tdata (abfd)->program_header_size != 0)
4197 return elf_tdata (abfd)->program_header_size;
4198
4199 if (elf_tdata (abfd)->segment_map != NULL)
4200 {
4201 struct elf_segment_map *m;
4202
4203 segs = 0;
4204 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4205 ++segs;
4206 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4207 return elf_tdata (abfd)->program_header_size;
4208 }
4209
4210 /* Assume we will need exactly two PT_LOAD segments: one for text
4211 and one for data. */
4212 segs = 2;
4213
4214 s = bfd_get_section_by_name (abfd, ".interp");
4215 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4216 {
4217 /* If we have a loadable interpreter section, we need a
4218 PT_INTERP segment. In this case, assume we also need a
4219 PT_PHDR segment, although that may not be true for all
4220 targets. */
4221 segs += 2;
4222 }
4223
4224 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4225 {
4226 /* We need a PT_DYNAMIC segment. */
4227 ++segs;
4228 }
4229
4230 if (elf_tdata (abfd)->eh_frame_hdr)
4231 {
4232 /* We need a PT_GNU_EH_FRAME segment. */
4233 ++segs;
4234 }
4235
4236 if (elf_tdata (abfd)->stack_flags)
4237 {
4238 /* We need a PT_GNU_STACK segment. */
4239 ++segs;
4240 }
4241
4242 if (elf_tdata (abfd)->relro)
4243 {
4244 /* We need a PT_GNU_RELRO segment. */
4245 ++segs;
4246 }
4247
4248 for (s = abfd->sections; s != NULL; s = s->next)
4249 {
4250 if ((s->flags & SEC_LOAD) != 0
4251 && strncmp (s->name, ".note", 5) == 0)
4252 {
4253 /* We need a PT_NOTE segment. */
4254 ++segs;
4255 }
4256 }
4257
4258 for (s = abfd->sections; s != NULL; s = s->next)
4259 {
4260 if (s->flags & SEC_THREAD_LOCAL)
4261 {
4262 /* We need a PT_TLS segment. */
4263 ++segs;
4264 break;
4265 }
4266 }
4267
4268 /* Let the backend count up any program headers it might need. */
4269 if (bed->elf_backend_additional_program_headers)
4270 {
4271 int a;
4272
4273 a = (*bed->elf_backend_additional_program_headers) (abfd);
4274 if (a == -1)
4275 abort ();
4276 segs += a;
4277 }
4278
4279 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4280 return elf_tdata (abfd)->program_header_size;
4281 }
4282
4283 /* Work out the file positions of all the sections. This is called by
4284 _bfd_elf_compute_section_file_positions. All the section sizes and
4285 VMAs must be known before this is called.
4286
4287 We do not consider reloc sections at this point, unless they form
4288 part of the loadable image. Reloc sections are assigned file
4289 positions in assign_file_positions_for_relocs, which is called by
4290 write_object_contents and final_link.
4291
4292 We also don't set the positions of the .symtab and .strtab here. */
4293
4294 static bfd_boolean
4295 assign_file_positions_except_relocs (bfd *abfd,
4296 struct bfd_link_info *link_info)
4297 {
4298 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4299 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4300 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4301 unsigned int num_sec = elf_numsections (abfd);
4302 file_ptr off;
4303 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4304
4305 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4306 && bfd_get_format (abfd) != bfd_core)
4307 {
4308 Elf_Internal_Shdr **hdrpp;
4309 unsigned int i;
4310
4311 /* Start after the ELF header. */
4312 off = i_ehdrp->e_ehsize;
4313
4314 /* We are not creating an executable, which means that we are
4315 not creating a program header, and that the actual order of
4316 the sections in the file is unimportant. */
4317 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4318 {
4319 Elf_Internal_Shdr *hdr;
4320
4321 hdr = *hdrpp;
4322 if (hdr->sh_type == SHT_REL
4323 || hdr->sh_type == SHT_RELA
4324 || i == tdata->symtab_section
4325 || i == tdata->symtab_shndx_section
4326 || i == tdata->strtab_section)
4327 {
4328 hdr->sh_offset = -1;
4329 }
4330 else
4331 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4332
4333 if (i == SHN_LORESERVE - 1)
4334 {
4335 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4336 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4337 }
4338 }
4339 }
4340 else
4341 {
4342 unsigned int i;
4343 Elf_Internal_Shdr **hdrpp;
4344
4345 /* Assign file positions for the loaded sections based on the
4346 assignment of sections to segments. */
4347 if (! assign_file_positions_for_segments (abfd, link_info))
4348 return FALSE;
4349
4350 /* Assign file positions for the other sections. */
4351
4352 off = elf_tdata (abfd)->next_file_pos;
4353 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4354 {
4355 Elf_Internal_Shdr *hdr;
4356
4357 hdr = *hdrpp;
4358 if (hdr->bfd_section != NULL
4359 && hdr->bfd_section->filepos != 0)
4360 hdr->sh_offset = hdr->bfd_section->filepos;
4361 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4362 {
4363 ((*_bfd_error_handler)
4364 (_("%s: warning: allocated section `%s' not in segment"),
4365 bfd_get_filename (abfd),
4366 (hdr->bfd_section == NULL
4367 ? "*unknown*"
4368 : hdr->bfd_section->name)));
4369 if ((abfd->flags & D_PAGED) != 0)
4370 off += vma_page_aligned_bias (hdr->sh_addr, off,
4371 bed->maxpagesize);
4372 else
4373 off += vma_page_aligned_bias (hdr->sh_addr, off,
4374 hdr->sh_addralign);
4375 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4376 FALSE);
4377 }
4378 else if (hdr->sh_type == SHT_REL
4379 || hdr->sh_type == SHT_RELA
4380 || hdr == i_shdrpp[tdata->symtab_section]
4381 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4382 || hdr == i_shdrpp[tdata->strtab_section])
4383 hdr->sh_offset = -1;
4384 else
4385 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4386
4387 if (i == SHN_LORESERVE - 1)
4388 {
4389 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4390 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4391 }
4392 }
4393 }
4394
4395 /* Place the section headers. */
4396 off = align_file_position (off, 1 << bed->s->log_file_align);
4397 i_ehdrp->e_shoff = off;
4398 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4399
4400 elf_tdata (abfd)->next_file_pos = off;
4401
4402 return TRUE;
4403 }
4404
4405 static bfd_boolean
4406 prep_headers (bfd *abfd)
4407 {
4408 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4409 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4410 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4411 struct elf_strtab_hash *shstrtab;
4412 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4413
4414 i_ehdrp = elf_elfheader (abfd);
4415 i_shdrp = elf_elfsections (abfd);
4416
4417 shstrtab = _bfd_elf_strtab_init ();
4418 if (shstrtab == NULL)
4419 return FALSE;
4420
4421 elf_shstrtab (abfd) = shstrtab;
4422
4423 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4424 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4425 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4426 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4427
4428 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4429 i_ehdrp->e_ident[EI_DATA] =
4430 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4431 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4432
4433 if ((abfd->flags & DYNAMIC) != 0)
4434 i_ehdrp->e_type = ET_DYN;
4435 else if ((abfd->flags & EXEC_P) != 0)
4436 i_ehdrp->e_type = ET_EXEC;
4437 else if (bfd_get_format (abfd) == bfd_core)
4438 i_ehdrp->e_type = ET_CORE;
4439 else
4440 i_ehdrp->e_type = ET_REL;
4441
4442 switch (bfd_get_arch (abfd))
4443 {
4444 case bfd_arch_unknown:
4445 i_ehdrp->e_machine = EM_NONE;
4446 break;
4447
4448 /* There used to be a long list of cases here, each one setting
4449 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4450 in the corresponding bfd definition. To avoid duplication,
4451 the switch was removed. Machines that need special handling
4452 can generally do it in elf_backend_final_write_processing(),
4453 unless they need the information earlier than the final write.
4454 Such need can generally be supplied by replacing the tests for
4455 e_machine with the conditions used to determine it. */
4456 default:
4457 i_ehdrp->e_machine = bed->elf_machine_code;
4458 }
4459
4460 i_ehdrp->e_version = bed->s->ev_current;
4461 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4462
4463 /* No program header, for now. */
4464 i_ehdrp->e_phoff = 0;
4465 i_ehdrp->e_phentsize = 0;
4466 i_ehdrp->e_phnum = 0;
4467
4468 /* Each bfd section is section header entry. */
4469 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4470 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4471
4472 /* If we're building an executable, we'll need a program header table. */
4473 if (abfd->flags & EXEC_P)
4474 {
4475 /* It all happens later. */
4476 #if 0
4477 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4478
4479 /* elf_build_phdrs() returns a (NULL-terminated) array of
4480 Elf_Internal_Phdrs. */
4481 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4482 i_ehdrp->e_phoff = outbase;
4483 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4484 #endif
4485 }
4486 else
4487 {
4488 i_ehdrp->e_phentsize = 0;
4489 i_phdrp = 0;
4490 i_ehdrp->e_phoff = 0;
4491 }
4492
4493 elf_tdata (abfd)->symtab_hdr.sh_name =
4494 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4495 elf_tdata (abfd)->strtab_hdr.sh_name =
4496 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4497 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4498 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4499 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4500 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4501 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4502 return FALSE;
4503
4504 return TRUE;
4505 }
4506
4507 /* Assign file positions for all the reloc sections which are not part
4508 of the loadable file image. */
4509
4510 void
4511 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
4512 {
4513 file_ptr off;
4514 unsigned int i, num_sec;
4515 Elf_Internal_Shdr **shdrpp;
4516
4517 off = elf_tdata (abfd)->next_file_pos;
4518
4519 num_sec = elf_numsections (abfd);
4520 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4521 {
4522 Elf_Internal_Shdr *shdrp;
4523
4524 shdrp = *shdrpp;
4525 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4526 && shdrp->sh_offset == -1)
4527 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4528 }
4529
4530 elf_tdata (abfd)->next_file_pos = off;
4531 }
4532
4533 bfd_boolean
4534 _bfd_elf_write_object_contents (bfd *abfd)
4535 {
4536 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4537 Elf_Internal_Ehdr *i_ehdrp;
4538 Elf_Internal_Shdr **i_shdrp;
4539 bfd_boolean failed;
4540 unsigned int count, num_sec;
4541
4542 if (! abfd->output_has_begun
4543 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
4544 return FALSE;
4545
4546 i_shdrp = elf_elfsections (abfd);
4547 i_ehdrp = elf_elfheader (abfd);
4548
4549 failed = FALSE;
4550 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4551 if (failed)
4552 return FALSE;
4553
4554 _bfd_elf_assign_file_positions_for_relocs (abfd);
4555
4556 /* After writing the headers, we need to write the sections too... */
4557 num_sec = elf_numsections (abfd);
4558 for (count = 1; count < num_sec; count++)
4559 {
4560 if (bed->elf_backend_section_processing)
4561 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4562 if (i_shdrp[count]->contents)
4563 {
4564 bfd_size_type amt = i_shdrp[count]->sh_size;
4565
4566 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4567 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4568 return FALSE;
4569 }
4570 if (count == SHN_LORESERVE - 1)
4571 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4572 }
4573
4574 /* Write out the section header names. */
4575 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4576 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4577 return FALSE;
4578
4579 if (bed->elf_backend_final_write_processing)
4580 (*bed->elf_backend_final_write_processing) (abfd,
4581 elf_tdata (abfd)->linker);
4582
4583 return bed->s->write_shdrs_and_ehdr (abfd);
4584 }
4585
4586 bfd_boolean
4587 _bfd_elf_write_corefile_contents (bfd *abfd)
4588 {
4589 /* Hopefully this can be done just like an object file. */
4590 return _bfd_elf_write_object_contents (abfd);
4591 }
4592
4593 /* Given a section, search the header to find them. */
4594
4595 int
4596 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
4597 {
4598 const struct elf_backend_data *bed;
4599 int index;
4600
4601 if (elf_section_data (asect) != NULL
4602 && elf_section_data (asect)->this_idx != 0)
4603 return elf_section_data (asect)->this_idx;
4604
4605 if (bfd_is_abs_section (asect))
4606 index = SHN_ABS;
4607 else if (bfd_is_com_section (asect))
4608 index = SHN_COMMON;
4609 else if (bfd_is_und_section (asect))
4610 index = SHN_UNDEF;
4611 else
4612 {
4613 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4614 int maxindex = elf_numsections (abfd);
4615
4616 for (index = 1; index < maxindex; index++)
4617 {
4618 Elf_Internal_Shdr *hdr = i_shdrp[index];
4619
4620 if (hdr != NULL && hdr->bfd_section == asect)
4621 return index;
4622 }
4623 index = -1;
4624 }
4625
4626 bed = get_elf_backend_data (abfd);
4627 if (bed->elf_backend_section_from_bfd_section)
4628 {
4629 int retval = index;
4630
4631 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4632 return retval;
4633 }
4634
4635 if (index == -1)
4636 bfd_set_error (bfd_error_nonrepresentable_section);
4637
4638 return index;
4639 }
4640
4641 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4642 on error. */
4643
4644 int
4645 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
4646 {
4647 asymbol *asym_ptr = *asym_ptr_ptr;
4648 int idx;
4649 flagword flags = asym_ptr->flags;
4650
4651 /* When gas creates relocations against local labels, it creates its
4652 own symbol for the section, but does put the symbol into the
4653 symbol chain, so udata is 0. When the linker is generating
4654 relocatable output, this section symbol may be for one of the
4655 input sections rather than the output section. */
4656 if (asym_ptr->udata.i == 0
4657 && (flags & BSF_SECTION_SYM)
4658 && asym_ptr->section)
4659 {
4660 int indx;
4661
4662 if (asym_ptr->section->output_section != NULL)
4663 indx = asym_ptr->section->output_section->index;
4664 else
4665 indx = asym_ptr->section->index;
4666 if (indx < elf_num_section_syms (abfd)
4667 && elf_section_syms (abfd)[indx] != NULL)
4668 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4669 }
4670
4671 idx = asym_ptr->udata.i;
4672
4673 if (idx == 0)
4674 {
4675 /* This case can occur when using --strip-symbol on a symbol
4676 which is used in a relocation entry. */
4677 (*_bfd_error_handler)
4678 (_("%s: symbol `%s' required but not present"),
4679 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4680 bfd_set_error (bfd_error_no_symbols);
4681 return -1;
4682 }
4683
4684 #if DEBUG & 4
4685 {
4686 fprintf (stderr,
4687 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4688 (long) asym_ptr, asym_ptr->name, idx, flags,
4689 elf_symbol_flags (flags));
4690 fflush (stderr);
4691 }
4692 #endif
4693
4694 return idx;
4695 }
4696
4697 /* Copy private BFD data. This copies any program header information. */
4698
4699 static bfd_boolean
4700 copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4701 {
4702 Elf_Internal_Ehdr *iehdr;
4703 struct elf_segment_map *map;
4704 struct elf_segment_map *map_first;
4705 struct elf_segment_map **pointer_to_map;
4706 Elf_Internal_Phdr *segment;
4707 asection *section;
4708 unsigned int i;
4709 unsigned int num_segments;
4710 bfd_boolean phdr_included = FALSE;
4711 bfd_vma maxpagesize;
4712 struct elf_segment_map *phdr_adjust_seg = NULL;
4713 unsigned int phdr_adjust_num = 0;
4714 const struct elf_backend_data *bed;
4715
4716 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4717 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4718 return TRUE;
4719
4720 if (elf_tdata (ibfd)->phdr == NULL)
4721 return TRUE;
4722
4723 bed = get_elf_backend_data (ibfd);
4724 iehdr = elf_elfheader (ibfd);
4725
4726 map_first = NULL;
4727 pointer_to_map = &map_first;
4728
4729 num_segments = elf_elfheader (ibfd)->e_phnum;
4730 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4731
4732 /* Returns the end address of the segment + 1. */
4733 #define SEGMENT_END(segment, start) \
4734 (start + (segment->p_memsz > segment->p_filesz \
4735 ? segment->p_memsz : segment->p_filesz))
4736
4737 #define SECTION_SIZE(section, segment) \
4738 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
4739 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
4740 ? section->_raw_size : 0)
4741
4742 /* Returns TRUE if the given section is contained within
4743 the given segment. VMA addresses are compared. */
4744 #define IS_CONTAINED_BY_VMA(section, segment) \
4745 (section->vma >= segment->p_vaddr \
4746 && (section->vma + SECTION_SIZE (section, segment) \
4747 <= (SEGMENT_END (segment, segment->p_vaddr))))
4748
4749 /* Returns TRUE if the given section is contained within
4750 the given segment. LMA addresses are compared. */
4751 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4752 (section->lma >= base \
4753 && (section->lma + SECTION_SIZE (section, segment) \
4754 <= SEGMENT_END (segment, base)))
4755
4756 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4757 #define IS_COREFILE_NOTE(p, s) \
4758 (p->p_type == PT_NOTE \
4759 && bfd_get_format (ibfd) == bfd_core \
4760 && s->vma == 0 && s->lma == 0 \
4761 && (bfd_vma) s->filepos >= p->p_offset \
4762 && ((bfd_vma) s->filepos + s->_raw_size \
4763 <= p->p_offset + p->p_filesz))
4764
4765 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4766 linker, which generates a PT_INTERP section with p_vaddr and
4767 p_memsz set to 0. */
4768 #define IS_SOLARIS_PT_INTERP(p, s) \
4769 (p->p_vaddr == 0 \
4770 && p->p_paddr == 0 \
4771 && p->p_memsz == 0 \
4772 && p->p_filesz > 0 \
4773 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4774 && s->_raw_size > 0 \
4775 && (bfd_vma) s->filepos >= p->p_offset \
4776 && ((bfd_vma) s->filepos + s->_raw_size \
4777 <= p->p_offset + p->p_filesz))
4778
4779 /* Decide if the given section should be included in the given segment.
4780 A section will be included if:
4781 1. It is within the address space of the segment -- we use the LMA
4782 if that is set for the segment and the VMA otherwise,
4783 2. It is an allocated segment,
4784 3. There is an output section associated with it,
4785 4. The section has not already been allocated to a previous segment.
4786 5. PT_GNU_STACK segments do not include any sections.
4787 6. PT_TLS segment includes only SHF_TLS sections.
4788 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */
4789 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4790 ((((segment->p_paddr \
4791 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4792 : IS_CONTAINED_BY_VMA (section, segment)) \
4793 && (section->flags & SEC_ALLOC) != 0) \
4794 || IS_COREFILE_NOTE (segment, section)) \
4795 && section->output_section != NULL \
4796 && segment->p_type != PT_GNU_STACK \
4797 && (segment->p_type != PT_TLS \
4798 || (section->flags & SEC_THREAD_LOCAL)) \
4799 && (segment->p_type == PT_LOAD \
4800 || segment->p_type == PT_TLS \
4801 || (section->flags & SEC_THREAD_LOCAL) == 0) \
4802 && ! section->segment_mark)
4803
4804 /* Returns TRUE iff seg1 starts after the end of seg2. */
4805 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4806 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4807
4808 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4809 their VMA address ranges and their LMA address ranges overlap.
4810 It is possible to have overlapping VMA ranges without overlapping LMA
4811 ranges. RedBoot images for example can have both .data and .bss mapped
4812 to the same VMA range, but with the .data section mapped to a different
4813 LMA. */
4814 #define SEGMENT_OVERLAPS(seg1, seg2) \
4815 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
4816 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
4817 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
4818 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
4819
4820 /* Initialise the segment mark field. */
4821 for (section = ibfd->sections; section != NULL; section = section->next)
4822 section->segment_mark = FALSE;
4823
4824 /* Scan through the segments specified in the program header
4825 of the input BFD. For this first scan we look for overlaps
4826 in the loadable segments. These can be created by weird
4827 parameters to objcopy. Also, fix some solaris weirdness. */
4828 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4829 i < num_segments;
4830 i++, segment++)
4831 {
4832 unsigned int j;
4833 Elf_Internal_Phdr *segment2;
4834
4835 if (segment->p_type == PT_INTERP)
4836 for (section = ibfd->sections; section; section = section->next)
4837 if (IS_SOLARIS_PT_INTERP (segment, section))
4838 {
4839 /* Mininal change so that the normal section to segment
4840 assignment code will work. */
4841 segment->p_vaddr = section->vma;
4842 break;
4843 }
4844
4845 if (segment->p_type != PT_LOAD)
4846 continue;
4847
4848 /* Determine if this segment overlaps any previous segments. */
4849 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4850 {
4851 bfd_signed_vma extra_length;
4852
4853 if (segment2->p_type != PT_LOAD
4854 || ! SEGMENT_OVERLAPS (segment, segment2))
4855 continue;
4856
4857 /* Merge the two segments together. */
4858 if (segment2->p_vaddr < segment->p_vaddr)
4859 {
4860 /* Extend SEGMENT2 to include SEGMENT and then delete
4861 SEGMENT. */
4862 extra_length =
4863 SEGMENT_END (segment, segment->p_vaddr)
4864 - SEGMENT_END (segment2, segment2->p_vaddr);
4865
4866 if (extra_length > 0)
4867 {
4868 segment2->p_memsz += extra_length;
4869 segment2->p_filesz += extra_length;
4870 }
4871
4872 segment->p_type = PT_NULL;
4873
4874 /* Since we have deleted P we must restart the outer loop. */
4875 i = 0;
4876 segment = elf_tdata (ibfd)->phdr;
4877 break;
4878 }
4879 else
4880 {
4881 /* Extend SEGMENT to include SEGMENT2 and then delete
4882 SEGMENT2. */
4883 extra_length =
4884 SEGMENT_END (segment2, segment2->p_vaddr)
4885 - SEGMENT_END (segment, segment->p_vaddr);
4886
4887 if (extra_length > 0)
4888 {
4889 segment->p_memsz += extra_length;
4890 segment->p_filesz += extra_length;
4891 }
4892
4893 segment2->p_type = PT_NULL;
4894 }
4895 }
4896 }
4897
4898 /* The second scan attempts to assign sections to segments. */
4899 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4900 i < num_segments;
4901 i ++, segment ++)
4902 {
4903 unsigned int section_count;
4904 asection ** sections;
4905 asection * output_section;
4906 unsigned int isec;
4907 bfd_vma matching_lma;
4908 bfd_vma suggested_lma;
4909 unsigned int j;
4910 bfd_size_type amt;
4911
4912 if (segment->p_type == PT_NULL)
4913 continue;
4914
4915 /* Compute how many sections might be placed into this segment. */
4916 for (section = ibfd->sections, section_count = 0;
4917 section != NULL;
4918 section = section->next)
4919 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4920 ++section_count;
4921
4922 /* Allocate a segment map big enough to contain
4923 all of the sections we have selected. */
4924 amt = sizeof (struct elf_segment_map);
4925 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4926 map = bfd_alloc (obfd, amt);
4927 if (map == NULL)
4928 return FALSE;
4929
4930 /* Initialise the fields of the segment map. Default to
4931 using the physical address of the segment in the input BFD. */
4932 map->next = NULL;
4933 map->p_type = segment->p_type;
4934 map->p_flags = segment->p_flags;
4935 map->p_flags_valid = 1;
4936 map->p_paddr = segment->p_paddr;
4937 map->p_paddr_valid = 1;
4938
4939 /* Determine if this segment contains the ELF file header
4940 and if it contains the program headers themselves. */
4941 map->includes_filehdr = (segment->p_offset == 0
4942 && segment->p_filesz >= iehdr->e_ehsize);
4943
4944 map->includes_phdrs = 0;
4945
4946 if (! phdr_included || segment->p_type != PT_LOAD)
4947 {
4948 map->includes_phdrs =
4949 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4950 && (segment->p_offset + segment->p_filesz
4951 >= ((bfd_vma) iehdr->e_phoff
4952 + iehdr->e_phnum * iehdr->e_phentsize)));
4953
4954 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4955 phdr_included = TRUE;
4956 }
4957
4958 if (section_count == 0)
4959 {
4960 /* Special segments, such as the PT_PHDR segment, may contain
4961 no sections, but ordinary, loadable segments should contain
4962 something. They are allowed by the ELF spec however, so only
4963 a warning is produced. */
4964 if (segment->p_type == PT_LOAD)
4965 (*_bfd_error_handler)
4966 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4967 bfd_archive_filename (ibfd));
4968
4969 map->count = 0;
4970 *pointer_to_map = map;
4971 pointer_to_map = &map->next;
4972
4973 continue;
4974 }
4975
4976 /* Now scan the sections in the input BFD again and attempt
4977 to add their corresponding output sections to the segment map.
4978 The problem here is how to handle an output section which has
4979 been moved (ie had its LMA changed). There are four possibilities:
4980
4981 1. None of the sections have been moved.
4982 In this case we can continue to use the segment LMA from the
4983 input BFD.
4984
4985 2. All of the sections have been moved by the same amount.
4986 In this case we can change the segment's LMA to match the LMA
4987 of the first section.
4988
4989 3. Some of the sections have been moved, others have not.
4990 In this case those sections which have not been moved can be
4991 placed in the current segment which will have to have its size,
4992 and possibly its LMA changed, and a new segment or segments will
4993 have to be created to contain the other sections.
4994
4995 4. The sections have been moved, but not by the same amount.
4996 In this case we can change the segment's LMA to match the LMA
4997 of the first section and we will have to create a new segment
4998 or segments to contain the other sections.
4999
5000 In order to save time, we allocate an array to hold the section
5001 pointers that we are interested in. As these sections get assigned
5002 to a segment, they are removed from this array. */
5003
5004 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5005 to work around this long long bug. */
5006 amt = section_count * sizeof (asection *);
5007 sections = bfd_malloc (amt);
5008 if (sections == NULL)
5009 return FALSE;
5010
5011 /* Step One: Scan for segment vs section LMA conflicts.
5012 Also add the sections to the section array allocated above.
5013 Also add the sections to the current segment. In the common
5014 case, where the sections have not been moved, this means that
5015 we have completely filled the segment, and there is nothing
5016 more to do. */
5017 isec = 0;
5018 matching_lma = 0;
5019 suggested_lma = 0;
5020
5021 for (j = 0, section = ibfd->sections;
5022 section != NULL;
5023 section = section->next)
5024 {
5025 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5026 {
5027 output_section = section->output_section;
5028
5029 sections[j ++] = section;
5030
5031 /* The Solaris native linker always sets p_paddr to 0.
5032 We try to catch that case here, and set it to the
5033 correct value. Note - some backends require that
5034 p_paddr be left as zero. */
5035 if (segment->p_paddr == 0
5036 && segment->p_vaddr != 0
5037 && (! bed->want_p_paddr_set_to_zero)
5038 && isec == 0
5039 && output_section->lma != 0
5040 && (output_section->vma == (segment->p_vaddr
5041 + (map->includes_filehdr
5042 ? iehdr->e_ehsize
5043 : 0)
5044 + (map->includes_phdrs
5045 ? (iehdr->e_phnum
5046 * iehdr->e_phentsize)
5047 : 0))))
5048 map->p_paddr = segment->p_vaddr;
5049
5050 /* Match up the physical address of the segment with the
5051 LMA address of the output section. */
5052 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5053 || IS_COREFILE_NOTE (segment, section)
5054 || (bed->want_p_paddr_set_to_zero &&
5055 IS_CONTAINED_BY_VMA (output_section, segment))
5056 )
5057 {
5058 if (matching_lma == 0)
5059 matching_lma = output_section->lma;
5060
5061 /* We assume that if the section fits within the segment
5062 then it does not overlap any other section within that
5063 segment. */
5064 map->sections[isec ++] = output_section;
5065 }
5066 else if (suggested_lma == 0)
5067 suggested_lma = output_section->lma;
5068 }
5069 }
5070
5071 BFD_ASSERT (j == section_count);
5072
5073 /* Step Two: Adjust the physical address of the current segment,
5074 if necessary. */
5075 if (isec == section_count)
5076 {
5077 /* All of the sections fitted within the segment as currently
5078 specified. This is the default case. Add the segment to
5079 the list of built segments and carry on to process the next
5080 program header in the input BFD. */
5081 map->count = section_count;
5082 *pointer_to_map = map;
5083 pointer_to_map = &map->next;
5084
5085 free (sections);
5086 continue;
5087 }
5088 else
5089 {
5090 if (matching_lma != 0)
5091 {
5092 /* At least one section fits inside the current segment.
5093 Keep it, but modify its physical address to match the
5094 LMA of the first section that fitted. */
5095 map->p_paddr = matching_lma;
5096 }
5097 else
5098 {
5099 /* None of the sections fitted inside the current segment.
5100 Change the current segment's physical address to match
5101 the LMA of the first section. */
5102 map->p_paddr = suggested_lma;
5103 }
5104
5105 /* Offset the segment physical address from the lma
5106 to allow for space taken up by elf headers. */
5107 if (map->includes_filehdr)
5108 map->p_paddr -= iehdr->e_ehsize;
5109
5110 if (map->includes_phdrs)
5111 {
5112 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5113
5114 /* iehdr->e_phnum is just an estimate of the number
5115 of program headers that we will need. Make a note
5116 here of the number we used and the segment we chose
5117 to hold these headers, so that we can adjust the
5118 offset when we know the correct value. */
5119 phdr_adjust_num = iehdr->e_phnum;
5120 phdr_adjust_seg = map;
5121 }
5122 }
5123
5124 /* Step Three: Loop over the sections again, this time assigning
5125 those that fit to the current segment and removing them from the
5126 sections array; but making sure not to leave large gaps. Once all
5127 possible sections have been assigned to the current segment it is
5128 added to the list of built segments and if sections still remain
5129 to be assigned, a new segment is constructed before repeating
5130 the loop. */
5131 isec = 0;
5132 do
5133 {
5134 map->count = 0;
5135 suggested_lma = 0;
5136
5137 /* Fill the current segment with sections that fit. */
5138 for (j = 0; j < section_count; j++)
5139 {
5140 section = sections[j];
5141
5142 if (section == NULL)
5143 continue;
5144
5145 output_section = section->output_section;
5146
5147 BFD_ASSERT (output_section != NULL);
5148
5149 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5150 || IS_COREFILE_NOTE (segment, section))
5151 {
5152 if (map->count == 0)
5153 {
5154 /* If the first section in a segment does not start at
5155 the beginning of the segment, then something is
5156 wrong. */
5157 if (output_section->lma !=
5158 (map->p_paddr
5159 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5160 + (map->includes_phdrs
5161 ? iehdr->e_phnum * iehdr->e_phentsize
5162 : 0)))
5163 abort ();
5164 }
5165 else
5166 {
5167 asection * prev_sec;
5168
5169 prev_sec = map->sections[map->count - 1];
5170
5171 /* If the gap between the end of the previous section
5172 and the start of this section is more than
5173 maxpagesize then we need to start a new segment. */
5174 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
5175 maxpagesize)
5176 < BFD_ALIGN (output_section->lma, maxpagesize))
5177 || ((prev_sec->lma + prev_sec->_raw_size)
5178 > output_section->lma))
5179 {
5180 if (suggested_lma == 0)
5181 suggested_lma = output_section->lma;
5182
5183 continue;
5184 }
5185 }
5186
5187 map->sections[map->count++] = output_section;
5188 ++isec;
5189 sections[j] = NULL;
5190 section->segment_mark = TRUE;
5191 }
5192 else if (suggested_lma == 0)
5193 suggested_lma = output_section->lma;
5194 }
5195
5196 BFD_ASSERT (map->count > 0);
5197
5198 /* Add the current segment to the list of built segments. */
5199 *pointer_to_map = map;
5200 pointer_to_map = &map->next;
5201
5202 if (isec < section_count)
5203 {
5204 /* We still have not allocated all of the sections to
5205 segments. Create a new segment here, initialise it
5206 and carry on looping. */
5207 amt = sizeof (struct elf_segment_map);
5208 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5209 map = bfd_alloc (obfd, amt);
5210 if (map == NULL)
5211 {
5212 free (sections);
5213 return FALSE;
5214 }
5215
5216 /* Initialise the fields of the segment map. Set the physical
5217 physical address to the LMA of the first section that has
5218 not yet been assigned. */
5219 map->next = NULL;
5220 map->p_type = segment->p_type;
5221 map->p_flags = segment->p_flags;
5222 map->p_flags_valid = 1;
5223 map->p_paddr = suggested_lma;
5224 map->p_paddr_valid = 1;
5225 map->includes_filehdr = 0;
5226 map->includes_phdrs = 0;
5227 }
5228 }
5229 while (isec < section_count);
5230
5231 free (sections);
5232 }
5233
5234 /* The Solaris linker creates program headers in which all the
5235 p_paddr fields are zero. When we try to objcopy or strip such a
5236 file, we get confused. Check for this case, and if we find it
5237 reset the p_paddr_valid fields. */
5238 for (map = map_first; map != NULL; map = map->next)
5239 if (map->p_paddr != 0)
5240 break;
5241 if (map == NULL)
5242 for (map = map_first; map != NULL; map = map->next)
5243 map->p_paddr_valid = 0;
5244
5245 elf_tdata (obfd)->segment_map = map_first;
5246
5247 /* If we had to estimate the number of program headers that were
5248 going to be needed, then check our estimate now and adjust
5249 the offset if necessary. */
5250 if (phdr_adjust_seg != NULL)
5251 {
5252 unsigned int count;
5253
5254 for (count = 0, map = map_first; map != NULL; map = map->next)
5255 count++;
5256
5257 if (count > phdr_adjust_num)
5258 phdr_adjust_seg->p_paddr
5259 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5260 }
5261
5262 #if 0
5263 /* Final Step: Sort the segments into ascending order of physical
5264 address. */
5265 if (map_first != NULL)
5266 {
5267 struct elf_segment_map *prev;
5268
5269 prev = map_first;
5270 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5271 {
5272 /* Yes I know - its a bubble sort.... */
5273 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5274 {
5275 /* Swap map and map->next. */
5276 prev->next = map->next;
5277 map->next = map->next->next;
5278 prev->next->next = map;
5279
5280 /* Restart loop. */
5281 map = map_first;
5282 }
5283 }
5284 }
5285 #endif
5286
5287 #undef SEGMENT_END
5288 #undef SECTION_SIZE
5289 #undef IS_CONTAINED_BY_VMA
5290 #undef IS_CONTAINED_BY_LMA
5291 #undef IS_COREFILE_NOTE
5292 #undef IS_SOLARIS_PT_INTERP
5293 #undef INCLUDE_SECTION_IN_SEGMENT
5294 #undef SEGMENT_AFTER_SEGMENT
5295 #undef SEGMENT_OVERLAPS
5296 return TRUE;
5297 }
5298
5299 /* Copy private section information. This copies over the entsize
5300 field, and sometimes the info field. */
5301
5302 bfd_boolean
5303 _bfd_elf_copy_private_section_data (bfd *ibfd,
5304 asection *isec,
5305 bfd *obfd,
5306 asection *osec)
5307 {
5308 Elf_Internal_Shdr *ihdr, *ohdr;
5309
5310 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5311 || obfd->xvec->flavour != bfd_target_elf_flavour)
5312 return TRUE;
5313
5314 ihdr = &elf_section_data (isec)->this_hdr;
5315 ohdr = &elf_section_data (osec)->this_hdr;
5316
5317 ohdr->sh_entsize = ihdr->sh_entsize;
5318
5319 if (ihdr->sh_type == SHT_SYMTAB
5320 || ihdr->sh_type == SHT_DYNSYM
5321 || ihdr->sh_type == SHT_GNU_verneed
5322 || ihdr->sh_type == SHT_GNU_verdef)
5323 ohdr->sh_info = ihdr->sh_info;
5324
5325 /* Set things up for objcopy. The output SHT_GROUP section will
5326 have its elf_next_in_group pointing back to the input group
5327 members. */
5328 elf_next_in_group (osec) = elf_next_in_group (isec);
5329 elf_group_name (osec) = elf_group_name (isec);
5330
5331 osec->use_rela_p = isec->use_rela_p;
5332
5333 return TRUE;
5334 }
5335
5336 /* Copy private header information. */
5337
5338 bfd_boolean
5339 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5340 {
5341 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5342 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5343 return TRUE;
5344
5345 /* Copy over private BFD data if it has not already been copied.
5346 This must be done here, rather than in the copy_private_bfd_data
5347 entry point, because the latter is called after the section
5348 contents have been set, which means that the program headers have
5349 already been worked out. */
5350 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5351 {
5352 if (! copy_private_bfd_data (ibfd, obfd))
5353 return FALSE;
5354 }
5355
5356 return TRUE;
5357 }
5358
5359 /* Copy private symbol information. If this symbol is in a section
5360 which we did not map into a BFD section, try to map the section
5361 index correctly. We use special macro definitions for the mapped
5362 section indices; these definitions are interpreted by the
5363 swap_out_syms function. */
5364
5365 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5366 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5367 #define MAP_STRTAB (SHN_HIOS + 3)
5368 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5369 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5370
5371 bfd_boolean
5372 _bfd_elf_copy_private_symbol_data (bfd *ibfd,
5373 asymbol *isymarg,
5374 bfd *obfd,
5375 asymbol *osymarg)
5376 {
5377 elf_symbol_type *isym, *osym;
5378
5379 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5380 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5381 return TRUE;
5382
5383 isym = elf_symbol_from (ibfd, isymarg);
5384 osym = elf_symbol_from (obfd, osymarg);
5385
5386 if (isym != NULL
5387 && osym != NULL
5388 && bfd_is_abs_section (isym->symbol.section))
5389 {
5390 unsigned int shndx;
5391
5392 shndx = isym->internal_elf_sym.st_shndx;
5393 if (shndx == elf_onesymtab (ibfd))
5394 shndx = MAP_ONESYMTAB;
5395 else if (shndx == elf_dynsymtab (ibfd))
5396 shndx = MAP_DYNSYMTAB;
5397 else if (shndx == elf_tdata (ibfd)->strtab_section)
5398 shndx = MAP_STRTAB;
5399 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5400 shndx = MAP_SHSTRTAB;
5401 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5402 shndx = MAP_SYM_SHNDX;
5403 osym->internal_elf_sym.st_shndx = shndx;
5404 }
5405
5406 return TRUE;
5407 }
5408
5409 /* Swap out the symbols. */
5410
5411 static bfd_boolean
5412 swap_out_syms (bfd *abfd,
5413 struct bfd_strtab_hash **sttp,
5414 int relocatable_p)
5415 {
5416 const struct elf_backend_data *bed;
5417 int symcount;
5418 asymbol **syms;
5419 struct bfd_strtab_hash *stt;
5420 Elf_Internal_Shdr *symtab_hdr;
5421 Elf_Internal_Shdr *symtab_shndx_hdr;
5422 Elf_Internal_Shdr *symstrtab_hdr;
5423 char *outbound_syms;
5424 char *outbound_shndx;
5425 int idx;
5426 bfd_size_type amt;
5427 bfd_boolean name_local_sections;
5428
5429 if (!elf_map_symbols (abfd))
5430 return FALSE;
5431
5432 /* Dump out the symtabs. */
5433 stt = _bfd_elf_stringtab_init ();
5434 if (stt == NULL)
5435 return FALSE;
5436
5437 bed = get_elf_backend_data (abfd);
5438 symcount = bfd_get_symcount (abfd);
5439 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5440 symtab_hdr->sh_type = SHT_SYMTAB;
5441 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5442 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5443 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5444 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
5445
5446 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5447 symstrtab_hdr->sh_type = SHT_STRTAB;
5448
5449 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5450 outbound_syms = bfd_alloc (abfd, amt);
5451 if (outbound_syms == NULL)
5452 {
5453 _bfd_stringtab_free (stt);
5454 return FALSE;
5455 }
5456 symtab_hdr->contents = outbound_syms;
5457
5458 outbound_shndx = NULL;
5459 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5460 if (symtab_shndx_hdr->sh_name != 0)
5461 {
5462 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5463 outbound_shndx = bfd_zalloc (abfd, amt);
5464 if (outbound_shndx == NULL)
5465 {
5466 _bfd_stringtab_free (stt);
5467 return FALSE;
5468 }
5469
5470 symtab_shndx_hdr->contents = outbound_shndx;
5471 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5472 symtab_shndx_hdr->sh_size = amt;
5473 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5474 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5475 }
5476
5477 /* Now generate the data (for "contents"). */
5478 {
5479 /* Fill in zeroth symbol and swap it out. */
5480 Elf_Internal_Sym sym;
5481 sym.st_name = 0;
5482 sym.st_value = 0;
5483 sym.st_size = 0;
5484 sym.st_info = 0;
5485 sym.st_other = 0;
5486 sym.st_shndx = SHN_UNDEF;
5487 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5488 outbound_syms += bed->s->sizeof_sym;
5489 if (outbound_shndx != NULL)
5490 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5491 }
5492
5493 name_local_sections
5494 = (bed->elf_backend_name_local_section_symbols
5495 && bed->elf_backend_name_local_section_symbols (abfd));
5496
5497 syms = bfd_get_outsymbols (abfd);
5498 for (idx = 0; idx < symcount; idx++)
5499 {
5500 Elf_Internal_Sym sym;
5501 bfd_vma value = syms[idx]->value;
5502 elf_symbol_type *type_ptr;
5503 flagword flags = syms[idx]->flags;
5504 int type;
5505
5506 if (!name_local_sections
5507 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5508 {
5509 /* Local section symbols have no name. */
5510 sym.st_name = 0;
5511 }
5512 else
5513 {
5514 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5515 syms[idx]->name,
5516 TRUE, FALSE);
5517 if (sym.st_name == (unsigned long) -1)
5518 {
5519 _bfd_stringtab_free (stt);
5520 return FALSE;
5521 }
5522 }
5523
5524 type_ptr = elf_symbol_from (abfd, syms[idx]);
5525
5526 if ((flags & BSF_SECTION_SYM) == 0
5527 && bfd_is_com_section (syms[idx]->section))
5528 {
5529 /* ELF common symbols put the alignment into the `value' field,
5530 and the size into the `size' field. This is backwards from
5531 how BFD handles it, so reverse it here. */
5532 sym.st_size = value;
5533 if (type_ptr == NULL
5534 || type_ptr->internal_elf_sym.st_value == 0)
5535 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5536 else
5537 sym.st_value = type_ptr->internal_elf_sym.st_value;
5538 sym.st_shndx = _bfd_elf_section_from_bfd_section
5539 (abfd, syms[idx]->section);
5540 }
5541 else
5542 {
5543 asection *sec = syms[idx]->section;
5544 int shndx;
5545
5546 if (sec->output_section)
5547 {
5548 value += sec->output_offset;
5549 sec = sec->output_section;
5550 }
5551
5552 /* Don't add in the section vma for relocatable output. */
5553 if (! relocatable_p)
5554 value += sec->vma;
5555 sym.st_value = value;
5556 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5557
5558 if (bfd_is_abs_section (sec)
5559 && type_ptr != NULL
5560 && type_ptr->internal_elf_sym.st_shndx != 0)
5561 {
5562 /* This symbol is in a real ELF section which we did
5563 not create as a BFD section. Undo the mapping done
5564 by copy_private_symbol_data. */
5565 shndx = type_ptr->internal_elf_sym.st_shndx;
5566 switch (shndx)
5567 {
5568 case MAP_ONESYMTAB:
5569 shndx = elf_onesymtab (abfd);
5570 break;
5571 case MAP_DYNSYMTAB:
5572 shndx = elf_dynsymtab (abfd);
5573 break;
5574 case MAP_STRTAB:
5575 shndx = elf_tdata (abfd)->strtab_section;
5576 break;
5577 case MAP_SHSTRTAB:
5578 shndx = elf_tdata (abfd)->shstrtab_section;
5579 break;
5580 case MAP_SYM_SHNDX:
5581 shndx = elf_tdata (abfd)->symtab_shndx_section;
5582 break;
5583 default:
5584 break;
5585 }
5586 }
5587 else
5588 {
5589 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5590
5591 if (shndx == -1)
5592 {
5593 asection *sec2;
5594
5595 /* Writing this would be a hell of a lot easier if
5596 we had some decent documentation on bfd, and
5597 knew what to expect of the library, and what to
5598 demand of applications. For example, it
5599 appears that `objcopy' might not set the
5600 section of a symbol to be a section that is
5601 actually in the output file. */
5602 sec2 = bfd_get_section_by_name (abfd, sec->name);
5603 if (sec2 == NULL)
5604 {
5605 _bfd_error_handler (_("\
5606 Unable to find equivalent output section for symbol '%s' from section '%s'"),
5607 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5608 sec->name);
5609 bfd_set_error (bfd_error_invalid_operation);
5610 _bfd_stringtab_free (stt);
5611 return FALSE;
5612 }
5613
5614 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5615 BFD_ASSERT (shndx != -1);
5616 }
5617 }
5618
5619 sym.st_shndx = shndx;
5620 }
5621
5622 if ((flags & BSF_THREAD_LOCAL) != 0)
5623 type = STT_TLS;
5624 else if ((flags & BSF_FUNCTION) != 0)
5625 type = STT_FUNC;
5626 else if ((flags & BSF_OBJECT) != 0)
5627 type = STT_OBJECT;
5628 else
5629 type = STT_NOTYPE;
5630
5631 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5632 type = STT_TLS;
5633
5634 /* Processor-specific types. */
5635 if (type_ptr != NULL
5636 && bed->elf_backend_get_symbol_type)
5637 type = ((*bed->elf_backend_get_symbol_type)
5638 (&type_ptr->internal_elf_sym, type));
5639
5640 if (flags & BSF_SECTION_SYM)
5641 {
5642 if (flags & BSF_GLOBAL)
5643 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5644 else
5645 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5646 }
5647 else if (bfd_is_com_section (syms[idx]->section))
5648 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5649 else if (bfd_is_und_section (syms[idx]->section))
5650 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5651 ? STB_WEAK
5652 : STB_GLOBAL),
5653 type);
5654 else if (flags & BSF_FILE)
5655 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5656 else
5657 {
5658 int bind = STB_LOCAL;
5659
5660 if (flags & BSF_LOCAL)
5661 bind = STB_LOCAL;
5662 else if (flags & BSF_WEAK)
5663 bind = STB_WEAK;
5664 else if (flags & BSF_GLOBAL)
5665 bind = STB_GLOBAL;
5666
5667 sym.st_info = ELF_ST_INFO (bind, type);
5668 }
5669
5670 if (type_ptr != NULL)
5671 sym.st_other = type_ptr->internal_elf_sym.st_other;
5672 else
5673 sym.st_other = 0;
5674
5675 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5676 outbound_syms += bed->s->sizeof_sym;
5677 if (outbound_shndx != NULL)
5678 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5679 }
5680
5681 *sttp = stt;
5682 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5683 symstrtab_hdr->sh_type = SHT_STRTAB;
5684
5685 symstrtab_hdr->sh_flags = 0;
5686 symstrtab_hdr->sh_addr = 0;
5687 symstrtab_hdr->sh_entsize = 0;
5688 symstrtab_hdr->sh_link = 0;
5689 symstrtab_hdr->sh_info = 0;
5690 symstrtab_hdr->sh_addralign = 1;
5691
5692 return TRUE;
5693 }
5694
5695 /* Return the number of bytes required to hold the symtab vector.
5696
5697 Note that we base it on the count plus 1, since we will null terminate
5698 the vector allocated based on this size. However, the ELF symbol table
5699 always has a dummy entry as symbol #0, so it ends up even. */
5700
5701 long
5702 _bfd_elf_get_symtab_upper_bound (bfd *abfd)
5703 {
5704 long symcount;
5705 long symtab_size;
5706 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5707
5708 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5709 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5710 if (symcount > 0)
5711 symtab_size -= sizeof (asymbol *);
5712
5713 return symtab_size;
5714 }
5715
5716 long
5717 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
5718 {
5719 long symcount;
5720 long symtab_size;
5721 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5722
5723 if (elf_dynsymtab (abfd) == 0)
5724 {
5725 bfd_set_error (bfd_error_invalid_operation);
5726 return -1;
5727 }
5728
5729 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5730 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5731 if (symcount > 0)
5732 symtab_size -= sizeof (asymbol *);
5733
5734 return symtab_size;
5735 }
5736
5737 long
5738 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5739 sec_ptr asect)
5740 {
5741 return (asect->reloc_count + 1) * sizeof (arelent *);
5742 }
5743
5744 /* Canonicalize the relocs. */
5745
5746 long
5747 _bfd_elf_canonicalize_reloc (bfd *abfd,
5748 sec_ptr section,
5749 arelent **relptr,
5750 asymbol **symbols)
5751 {
5752 arelent *tblptr;
5753 unsigned int i;
5754 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5755
5756 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5757 return -1;
5758
5759 tblptr = section->relocation;
5760 for (i = 0; i < section->reloc_count; i++)
5761 *relptr++ = tblptr++;
5762
5763 *relptr = NULL;
5764
5765 return section->reloc_count;
5766 }
5767
5768 long
5769 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
5770 {
5771 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5772 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
5773
5774 if (symcount >= 0)
5775 bfd_get_symcount (abfd) = symcount;
5776 return symcount;
5777 }
5778
5779 long
5780 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5781 asymbol **allocation)
5782 {
5783 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5784 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
5785
5786 if (symcount >= 0)
5787 bfd_get_dynamic_symcount (abfd) = symcount;
5788 return symcount;
5789 }
5790
5791 /* Return the size required for the dynamic reloc entries. Any
5792 section that was actually installed in the BFD, and has type
5793 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5794 considered to be a dynamic reloc section. */
5795
5796 long
5797 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
5798 {
5799 long ret;
5800 asection *s;
5801
5802 if (elf_dynsymtab (abfd) == 0)
5803 {
5804 bfd_set_error (bfd_error_invalid_operation);
5805 return -1;
5806 }
5807
5808 ret = sizeof (arelent *);
5809 for (s = abfd->sections; s != NULL; s = s->next)
5810 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5811 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5812 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5813 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5814 * sizeof (arelent *));
5815
5816 return ret;
5817 }
5818
5819 /* Canonicalize the dynamic relocation entries. Note that we return
5820 the dynamic relocations as a single block, although they are
5821 actually associated with particular sections; the interface, which
5822 was designed for SunOS style shared libraries, expects that there
5823 is only one set of dynamic relocs. Any section that was actually
5824 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5825 the dynamic symbol table, is considered to be a dynamic reloc
5826 section. */
5827
5828 long
5829 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
5830 arelent **storage,
5831 asymbol **syms)
5832 {
5833 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
5834 asection *s;
5835 long ret;
5836
5837 if (elf_dynsymtab (abfd) == 0)
5838 {
5839 bfd_set_error (bfd_error_invalid_operation);
5840 return -1;
5841 }
5842
5843 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5844 ret = 0;
5845 for (s = abfd->sections; s != NULL; s = s->next)
5846 {
5847 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5848 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5849 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5850 {
5851 arelent *p;
5852 long count, i;
5853
5854 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5855 return -1;
5856 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5857 p = s->relocation;
5858 for (i = 0; i < count; i++)
5859 *storage++ = p++;
5860 ret += count;
5861 }
5862 }
5863
5864 *storage = NULL;
5865
5866 return ret;
5867 }
5868 \f
5869 /* Read in the version information. */
5870
5871 bfd_boolean
5872 _bfd_elf_slurp_version_tables (bfd *abfd)
5873 {
5874 bfd_byte *contents = NULL;
5875 bfd_size_type amt;
5876
5877 if (elf_dynverdef (abfd) != 0)
5878 {
5879 Elf_Internal_Shdr *hdr;
5880 Elf_External_Verdef *everdef;
5881 Elf_Internal_Verdef *iverdef;
5882 Elf_Internal_Verdef *iverdefarr;
5883 Elf_Internal_Verdef iverdefmem;
5884 unsigned int i;
5885 unsigned int maxidx;
5886
5887 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5888
5889 contents = bfd_malloc (hdr->sh_size);
5890 if (contents == NULL)
5891 goto error_return;
5892 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5893 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5894 goto error_return;
5895
5896 /* We know the number of entries in the section but not the maximum
5897 index. Therefore we have to run through all entries and find
5898 the maximum. */
5899 everdef = (Elf_External_Verdef *) contents;
5900 maxidx = 0;
5901 for (i = 0; i < hdr->sh_info; ++i)
5902 {
5903 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5904
5905 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5906 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5907
5908 everdef = ((Elf_External_Verdef *)
5909 ((bfd_byte *) everdef + iverdefmem.vd_next));
5910 }
5911
5912 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5913 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
5914 if (elf_tdata (abfd)->verdef == NULL)
5915 goto error_return;
5916
5917 elf_tdata (abfd)->cverdefs = maxidx;
5918
5919 everdef = (Elf_External_Verdef *) contents;
5920 iverdefarr = elf_tdata (abfd)->verdef;
5921 for (i = 0; i < hdr->sh_info; i++)
5922 {
5923 Elf_External_Verdaux *everdaux;
5924 Elf_Internal_Verdaux *iverdaux;
5925 unsigned int j;
5926
5927 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5928
5929 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5930 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5931
5932 iverdef->vd_bfd = abfd;
5933
5934 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5935 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
5936 if (iverdef->vd_auxptr == NULL)
5937 goto error_return;
5938
5939 everdaux = ((Elf_External_Verdaux *)
5940 ((bfd_byte *) everdef + iverdef->vd_aux));
5941 iverdaux = iverdef->vd_auxptr;
5942 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5943 {
5944 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5945
5946 iverdaux->vda_nodename =
5947 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5948 iverdaux->vda_name);
5949 if (iverdaux->vda_nodename == NULL)
5950 goto error_return;
5951
5952 if (j + 1 < iverdef->vd_cnt)
5953 iverdaux->vda_nextptr = iverdaux + 1;
5954 else
5955 iverdaux->vda_nextptr = NULL;
5956
5957 everdaux = ((Elf_External_Verdaux *)
5958 ((bfd_byte *) everdaux + iverdaux->vda_next));
5959 }
5960
5961 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5962
5963 if (i + 1 < hdr->sh_info)
5964 iverdef->vd_nextdef = iverdef + 1;
5965 else
5966 iverdef->vd_nextdef = NULL;
5967
5968 everdef = ((Elf_External_Verdef *)
5969 ((bfd_byte *) everdef + iverdef->vd_next));
5970 }
5971
5972 free (contents);
5973 contents = NULL;
5974 }
5975
5976 if (elf_dynverref (abfd) != 0)
5977 {
5978 Elf_Internal_Shdr *hdr;
5979 Elf_External_Verneed *everneed;
5980 Elf_Internal_Verneed *iverneed;
5981 unsigned int i;
5982
5983 hdr = &elf_tdata (abfd)->dynverref_hdr;
5984
5985 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5986 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
5987 if (elf_tdata (abfd)->verref == NULL)
5988 goto error_return;
5989
5990 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5991
5992 contents = bfd_malloc (hdr->sh_size);
5993 if (contents == NULL)
5994 goto error_return;
5995 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5996 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
5997 goto error_return;
5998
5999 everneed = (Elf_External_Verneed *) contents;
6000 iverneed = elf_tdata (abfd)->verref;
6001 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6002 {
6003 Elf_External_Vernaux *evernaux;
6004 Elf_Internal_Vernaux *ivernaux;
6005 unsigned int j;
6006
6007 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6008
6009 iverneed->vn_bfd = abfd;
6010
6011 iverneed->vn_filename =
6012 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6013 iverneed->vn_file);
6014 if (iverneed->vn_filename == NULL)
6015 goto error_return;
6016
6017 amt = iverneed->vn_cnt;
6018 amt *= sizeof (Elf_Internal_Vernaux);
6019 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
6020
6021 evernaux = ((Elf_External_Vernaux *)
6022 ((bfd_byte *) everneed + iverneed->vn_aux));
6023 ivernaux = iverneed->vn_auxptr;
6024 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6025 {
6026 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6027
6028 ivernaux->vna_nodename =
6029 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6030 ivernaux->vna_name);
6031 if (ivernaux->vna_nodename == NULL)
6032 goto error_return;
6033
6034 if (j + 1 < iverneed->vn_cnt)
6035 ivernaux->vna_nextptr = ivernaux + 1;
6036 else
6037 ivernaux->vna_nextptr = NULL;
6038
6039 evernaux = ((Elf_External_Vernaux *)
6040 ((bfd_byte *) evernaux + ivernaux->vna_next));
6041 }
6042
6043 if (i + 1 < hdr->sh_info)
6044 iverneed->vn_nextref = iverneed + 1;
6045 else
6046 iverneed->vn_nextref = NULL;
6047
6048 everneed = ((Elf_External_Verneed *)
6049 ((bfd_byte *) everneed + iverneed->vn_next));
6050 }
6051
6052 free (contents);
6053 contents = NULL;
6054 }
6055
6056 return TRUE;
6057
6058 error_return:
6059 if (contents != NULL)
6060 free (contents);
6061 return FALSE;
6062 }
6063 \f
6064 asymbol *
6065 _bfd_elf_make_empty_symbol (bfd *abfd)
6066 {
6067 elf_symbol_type *newsym;
6068 bfd_size_type amt = sizeof (elf_symbol_type);
6069
6070 newsym = bfd_zalloc (abfd, amt);
6071 if (!newsym)
6072 return NULL;
6073 else
6074 {
6075 newsym->symbol.the_bfd = abfd;
6076 return &newsym->symbol;
6077 }
6078 }
6079
6080 void
6081 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6082 asymbol *symbol,
6083 symbol_info *ret)
6084 {
6085 bfd_symbol_info (symbol, ret);
6086 }
6087
6088 /* Return whether a symbol name implies a local symbol. Most targets
6089 use this function for the is_local_label_name entry point, but some
6090 override it. */
6091
6092 bfd_boolean
6093 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6094 const char *name)
6095 {
6096 /* Normal local symbols start with ``.L''. */
6097 if (name[0] == '.' && name[1] == 'L')
6098 return TRUE;
6099
6100 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6101 DWARF debugging symbols starting with ``..''. */
6102 if (name[0] == '.' && name[1] == '.')
6103 return TRUE;
6104
6105 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6106 emitting DWARF debugging output. I suspect this is actually a
6107 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6108 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6109 underscore to be emitted on some ELF targets). For ease of use,
6110 we treat such symbols as local. */
6111 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
6112 return TRUE;
6113
6114 return FALSE;
6115 }
6116
6117 alent *
6118 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6119 asymbol *symbol ATTRIBUTE_UNUSED)
6120 {
6121 abort ();
6122 return NULL;
6123 }
6124
6125 bfd_boolean
6126 _bfd_elf_set_arch_mach (bfd *abfd,
6127 enum bfd_architecture arch,
6128 unsigned long machine)
6129 {
6130 /* If this isn't the right architecture for this backend, and this
6131 isn't the generic backend, fail. */
6132 if (arch != get_elf_backend_data (abfd)->arch
6133 && arch != bfd_arch_unknown
6134 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
6135 return FALSE;
6136
6137 return bfd_default_set_arch_mach (abfd, arch, machine);
6138 }
6139
6140 /* Find the function to a particular section and offset,
6141 for error reporting. */
6142
6143 static bfd_boolean
6144 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6145 asection *section,
6146 asymbol **symbols,
6147 bfd_vma offset,
6148 const char **filename_ptr,
6149 const char **functionname_ptr)
6150 {
6151 const char *filename;
6152 asymbol *func;
6153 bfd_vma low_func;
6154 asymbol **p;
6155
6156 filename = NULL;
6157 func = NULL;
6158 low_func = 0;
6159
6160 for (p = symbols; *p != NULL; p++)
6161 {
6162 elf_symbol_type *q;
6163
6164 q = (elf_symbol_type *) *p;
6165
6166 if (bfd_get_section (&q->symbol) != section)
6167 continue;
6168
6169 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6170 {
6171 default:
6172 break;
6173 case STT_FILE:
6174 filename = bfd_asymbol_name (&q->symbol);
6175 break;
6176 case STT_NOTYPE:
6177 case STT_FUNC:
6178 if (q->symbol.section == section
6179 && q->symbol.value >= low_func
6180 && q->symbol.value <= offset)
6181 {
6182 func = (asymbol *) q;
6183 low_func = q->symbol.value;
6184 }
6185 break;
6186 }
6187 }
6188
6189 if (func == NULL)
6190 return FALSE;
6191
6192 if (filename_ptr)
6193 *filename_ptr = filename;
6194 if (functionname_ptr)
6195 *functionname_ptr = bfd_asymbol_name (func);
6196
6197 return TRUE;
6198 }
6199
6200 /* Find the nearest line to a particular section and offset,
6201 for error reporting. */
6202
6203 bfd_boolean
6204 _bfd_elf_find_nearest_line (bfd *abfd,
6205 asection *section,
6206 asymbol **symbols,
6207 bfd_vma offset,
6208 const char **filename_ptr,
6209 const char **functionname_ptr,
6210 unsigned int *line_ptr)
6211 {
6212 bfd_boolean found;
6213
6214 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6215 filename_ptr, functionname_ptr,
6216 line_ptr))
6217 {
6218 if (!*functionname_ptr)
6219 elf_find_function (abfd, section, symbols, offset,
6220 *filename_ptr ? NULL : filename_ptr,
6221 functionname_ptr);
6222
6223 return TRUE;
6224 }
6225
6226 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6227 filename_ptr, functionname_ptr,
6228 line_ptr, 0,
6229 &elf_tdata (abfd)->dwarf2_find_line_info))
6230 {
6231 if (!*functionname_ptr)
6232 elf_find_function (abfd, section, symbols, offset,
6233 *filename_ptr ? NULL : filename_ptr,
6234 functionname_ptr);
6235
6236 return TRUE;
6237 }
6238
6239 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6240 &found, filename_ptr,
6241 functionname_ptr, line_ptr,
6242 &elf_tdata (abfd)->line_info))
6243 return FALSE;
6244 if (found && (*functionname_ptr || *line_ptr))
6245 return TRUE;
6246
6247 if (symbols == NULL)
6248 return FALSE;
6249
6250 if (! elf_find_function (abfd, section, symbols, offset,
6251 filename_ptr, functionname_ptr))
6252 return FALSE;
6253
6254 *line_ptr = 0;
6255 return TRUE;
6256 }
6257
6258 int
6259 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
6260 {
6261 int ret;
6262
6263 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6264 if (! reloc)
6265 ret += get_program_header_size (abfd);
6266 return ret;
6267 }
6268
6269 bfd_boolean
6270 _bfd_elf_set_section_contents (bfd *abfd,
6271 sec_ptr section,
6272 const void *location,
6273 file_ptr offset,
6274 bfd_size_type count)
6275 {
6276 Elf_Internal_Shdr *hdr;
6277 bfd_signed_vma pos;
6278
6279 if (! abfd->output_has_begun
6280 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
6281 return FALSE;
6282
6283 hdr = &elf_section_data (section)->this_hdr;
6284 pos = hdr->sh_offset + offset;
6285 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6286 || bfd_bwrite (location, count, abfd) != count)
6287 return FALSE;
6288
6289 return TRUE;
6290 }
6291
6292 void
6293 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6294 arelent *cache_ptr ATTRIBUTE_UNUSED,
6295 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
6296 {
6297 abort ();
6298 }
6299
6300 /* Try to convert a non-ELF reloc into an ELF one. */
6301
6302 bfd_boolean
6303 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
6304 {
6305 /* Check whether we really have an ELF howto. */
6306
6307 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6308 {
6309 bfd_reloc_code_real_type code;
6310 reloc_howto_type *howto;
6311
6312 /* Alien reloc: Try to determine its type to replace it with an
6313 equivalent ELF reloc. */
6314
6315 if (areloc->howto->pc_relative)
6316 {
6317 switch (areloc->howto->bitsize)
6318 {
6319 case 8:
6320 code = BFD_RELOC_8_PCREL;
6321 break;
6322 case 12:
6323 code = BFD_RELOC_12_PCREL;
6324 break;
6325 case 16:
6326 code = BFD_RELOC_16_PCREL;
6327 break;
6328 case 24:
6329 code = BFD_RELOC_24_PCREL;
6330 break;
6331 case 32:
6332 code = BFD_RELOC_32_PCREL;
6333 break;
6334 case 64:
6335 code = BFD_RELOC_64_PCREL;
6336 break;
6337 default:
6338 goto fail;
6339 }
6340
6341 howto = bfd_reloc_type_lookup (abfd, code);
6342
6343 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6344 {
6345 if (howto->pcrel_offset)
6346 areloc->addend += areloc->address;
6347 else
6348 areloc->addend -= areloc->address; /* addend is unsigned!! */
6349 }
6350 }
6351 else
6352 {
6353 switch (areloc->howto->bitsize)
6354 {
6355 case 8:
6356 code = BFD_RELOC_8;
6357 break;
6358 case 14:
6359 code = BFD_RELOC_14;
6360 break;
6361 case 16:
6362 code = BFD_RELOC_16;
6363 break;
6364 case 26:
6365 code = BFD_RELOC_26;
6366 break;
6367 case 32:
6368 code = BFD_RELOC_32;
6369 break;
6370 case 64:
6371 code = BFD_RELOC_64;
6372 break;
6373 default:
6374 goto fail;
6375 }
6376
6377 howto = bfd_reloc_type_lookup (abfd, code);
6378 }
6379
6380 if (howto)
6381 areloc->howto = howto;
6382 else
6383 goto fail;
6384 }
6385
6386 return TRUE;
6387
6388 fail:
6389 (*_bfd_error_handler)
6390 (_("%s: unsupported relocation type %s"),
6391 bfd_archive_filename (abfd), areloc->howto->name);
6392 bfd_set_error (bfd_error_bad_value);
6393 return FALSE;
6394 }
6395
6396 bfd_boolean
6397 _bfd_elf_close_and_cleanup (bfd *abfd)
6398 {
6399 if (bfd_get_format (abfd) == bfd_object)
6400 {
6401 if (elf_shstrtab (abfd) != NULL)
6402 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6403 }
6404
6405 return _bfd_generic_close_and_cleanup (abfd);
6406 }
6407
6408 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6409 in the relocation's offset. Thus we cannot allow any sort of sanity
6410 range-checking to interfere. There is nothing else to do in processing
6411 this reloc. */
6412
6413 bfd_reloc_status_type
6414 _bfd_elf_rel_vtable_reloc_fn
6415 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
6416 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
6417 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6418 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
6419 {
6420 return bfd_reloc_ok;
6421 }
6422 \f
6423 /* Elf core file support. Much of this only works on native
6424 toolchains, since we rely on knowing the
6425 machine-dependent procfs structure in order to pick
6426 out details about the corefile. */
6427
6428 #ifdef HAVE_SYS_PROCFS_H
6429 # include <sys/procfs.h>
6430 #endif
6431
6432 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6433
6434 static int
6435 elfcore_make_pid (bfd *abfd)
6436 {
6437 return ((elf_tdata (abfd)->core_lwpid << 16)
6438 + (elf_tdata (abfd)->core_pid));
6439 }
6440
6441 /* If there isn't a section called NAME, make one, using
6442 data from SECT. Note, this function will generate a
6443 reference to NAME, so you shouldn't deallocate or
6444 overwrite it. */
6445
6446 static bfd_boolean
6447 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
6448 {
6449 asection *sect2;
6450
6451 if (bfd_get_section_by_name (abfd, name) != NULL)
6452 return TRUE;
6453
6454 sect2 = bfd_make_section (abfd, name);
6455 if (sect2 == NULL)
6456 return FALSE;
6457
6458 sect2->_raw_size = sect->_raw_size;
6459 sect2->filepos = sect->filepos;
6460 sect2->flags = sect->flags;
6461 sect2->alignment_power = sect->alignment_power;
6462 return TRUE;
6463 }
6464
6465 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6466 actually creates up to two pseudosections:
6467 - For the single-threaded case, a section named NAME, unless
6468 such a section already exists.
6469 - For the multi-threaded case, a section named "NAME/PID", where
6470 PID is elfcore_make_pid (abfd).
6471 Both pseudosections have identical contents. */
6472 bfd_boolean
6473 _bfd_elfcore_make_pseudosection (bfd *abfd,
6474 char *name,
6475 size_t size,
6476 ufile_ptr filepos)
6477 {
6478 char buf[100];
6479 char *threaded_name;
6480 size_t len;
6481 asection *sect;
6482
6483 /* Build the section name. */
6484
6485 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6486 len = strlen (buf) + 1;
6487 threaded_name = bfd_alloc (abfd, len);
6488 if (threaded_name == NULL)
6489 return FALSE;
6490 memcpy (threaded_name, buf, len);
6491
6492 sect = bfd_make_section_anyway (abfd, threaded_name);
6493 if (sect == NULL)
6494 return FALSE;
6495 sect->_raw_size = size;
6496 sect->filepos = filepos;
6497 sect->flags = SEC_HAS_CONTENTS;
6498 sect->alignment_power = 2;
6499
6500 return elfcore_maybe_make_sect (abfd, name, sect);
6501 }
6502
6503 /* prstatus_t exists on:
6504 solaris 2.5+
6505 linux 2.[01] + glibc
6506 unixware 4.2
6507 */
6508
6509 #if defined (HAVE_PRSTATUS_T)
6510
6511 static bfd_boolean
6512 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6513 {
6514 size_t raw_size;
6515 int offset;
6516
6517 if (note->descsz == sizeof (prstatus_t))
6518 {
6519 prstatus_t prstat;
6520
6521 raw_size = sizeof (prstat.pr_reg);
6522 offset = offsetof (prstatus_t, pr_reg);
6523 memcpy (&prstat, note->descdata, sizeof (prstat));
6524
6525 /* Do not overwrite the core signal if it
6526 has already been set by another thread. */
6527 if (elf_tdata (abfd)->core_signal == 0)
6528 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6529 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6530
6531 /* pr_who exists on:
6532 solaris 2.5+
6533 unixware 4.2
6534 pr_who doesn't exist on:
6535 linux 2.[01]
6536 */
6537 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6538 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6539 #endif
6540 }
6541 #if defined (HAVE_PRSTATUS32_T)
6542 else if (note->descsz == sizeof (prstatus32_t))
6543 {
6544 /* 64-bit host, 32-bit corefile */
6545 prstatus32_t prstat;
6546
6547 raw_size = sizeof (prstat.pr_reg);
6548 offset = offsetof (prstatus32_t, pr_reg);
6549 memcpy (&prstat, note->descdata, sizeof (prstat));
6550
6551 /* Do not overwrite the core signal if it
6552 has already been set by another thread. */
6553 if (elf_tdata (abfd)->core_signal == 0)
6554 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6555 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6556
6557 /* pr_who exists on:
6558 solaris 2.5+
6559 unixware 4.2
6560 pr_who doesn't exist on:
6561 linux 2.[01]
6562 */
6563 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6564 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6565 #endif
6566 }
6567 #endif /* HAVE_PRSTATUS32_T */
6568 else
6569 {
6570 /* Fail - we don't know how to handle any other
6571 note size (ie. data object type). */
6572 return TRUE;
6573 }
6574
6575 /* Make a ".reg/999" section and a ".reg" section. */
6576 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6577 raw_size, note->descpos + offset);
6578 }
6579 #endif /* defined (HAVE_PRSTATUS_T) */
6580
6581 /* Create a pseudosection containing the exact contents of NOTE. */
6582 static bfd_boolean
6583 elfcore_make_note_pseudosection (bfd *abfd,
6584 char *name,
6585 Elf_Internal_Note *note)
6586 {
6587 return _bfd_elfcore_make_pseudosection (abfd, name,
6588 note->descsz, note->descpos);
6589 }
6590
6591 /* There isn't a consistent prfpregset_t across platforms,
6592 but it doesn't matter, because we don't have to pick this
6593 data structure apart. */
6594
6595 static bfd_boolean
6596 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
6597 {
6598 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6599 }
6600
6601 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6602 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6603 literally. */
6604
6605 static bfd_boolean
6606 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
6607 {
6608 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6609 }
6610
6611 #if defined (HAVE_PRPSINFO_T)
6612 typedef prpsinfo_t elfcore_psinfo_t;
6613 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6614 typedef prpsinfo32_t elfcore_psinfo32_t;
6615 #endif
6616 #endif
6617
6618 #if defined (HAVE_PSINFO_T)
6619 typedef psinfo_t elfcore_psinfo_t;
6620 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6621 typedef psinfo32_t elfcore_psinfo32_t;
6622 #endif
6623 #endif
6624
6625 /* return a malloc'ed copy of a string at START which is at
6626 most MAX bytes long, possibly without a terminating '\0'.
6627 the copy will always have a terminating '\0'. */
6628
6629 char *
6630 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
6631 {
6632 char *dups;
6633 char *end = memchr (start, '\0', max);
6634 size_t len;
6635
6636 if (end == NULL)
6637 len = max;
6638 else
6639 len = end - start;
6640
6641 dups = bfd_alloc (abfd, len + 1);
6642 if (dups == NULL)
6643 return NULL;
6644
6645 memcpy (dups, start, len);
6646 dups[len] = '\0';
6647
6648 return dups;
6649 }
6650
6651 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6652 static bfd_boolean
6653 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6654 {
6655 if (note->descsz == sizeof (elfcore_psinfo_t))
6656 {
6657 elfcore_psinfo_t psinfo;
6658
6659 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6660
6661 elf_tdata (abfd)->core_program
6662 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6663 sizeof (psinfo.pr_fname));
6664
6665 elf_tdata (abfd)->core_command
6666 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6667 sizeof (psinfo.pr_psargs));
6668 }
6669 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6670 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6671 {
6672 /* 64-bit host, 32-bit corefile */
6673 elfcore_psinfo32_t psinfo;
6674
6675 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6676
6677 elf_tdata (abfd)->core_program
6678 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6679 sizeof (psinfo.pr_fname));
6680
6681 elf_tdata (abfd)->core_command
6682 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6683 sizeof (psinfo.pr_psargs));
6684 }
6685 #endif
6686
6687 else
6688 {
6689 /* Fail - we don't know how to handle any other
6690 note size (ie. data object type). */
6691 return TRUE;
6692 }
6693
6694 /* Note that for some reason, a spurious space is tacked
6695 onto the end of the args in some (at least one anyway)
6696 implementations, so strip it off if it exists. */
6697
6698 {
6699 char *command = elf_tdata (abfd)->core_command;
6700 int n = strlen (command);
6701
6702 if (0 < n && command[n - 1] == ' ')
6703 command[n - 1] = '\0';
6704 }
6705
6706 return TRUE;
6707 }
6708 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6709
6710 #if defined (HAVE_PSTATUS_T)
6711 static bfd_boolean
6712 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
6713 {
6714 if (note->descsz == sizeof (pstatus_t)
6715 #if defined (HAVE_PXSTATUS_T)
6716 || note->descsz == sizeof (pxstatus_t)
6717 #endif
6718 )
6719 {
6720 pstatus_t pstat;
6721
6722 memcpy (&pstat, note->descdata, sizeof (pstat));
6723
6724 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6725 }
6726 #if defined (HAVE_PSTATUS32_T)
6727 else if (note->descsz == sizeof (pstatus32_t))
6728 {
6729 /* 64-bit host, 32-bit corefile */
6730 pstatus32_t pstat;
6731
6732 memcpy (&pstat, note->descdata, sizeof (pstat));
6733
6734 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6735 }
6736 #endif
6737 /* Could grab some more details from the "representative"
6738 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6739 NT_LWPSTATUS note, presumably. */
6740
6741 return TRUE;
6742 }
6743 #endif /* defined (HAVE_PSTATUS_T) */
6744
6745 #if defined (HAVE_LWPSTATUS_T)
6746 static bfd_boolean
6747 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
6748 {
6749 lwpstatus_t lwpstat;
6750 char buf[100];
6751 char *name;
6752 size_t len;
6753 asection *sect;
6754
6755 if (note->descsz != sizeof (lwpstat)
6756 #if defined (HAVE_LWPXSTATUS_T)
6757 && note->descsz != sizeof (lwpxstatus_t)
6758 #endif
6759 )
6760 return TRUE;
6761
6762 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6763
6764 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6765 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6766
6767 /* Make a ".reg/999" section. */
6768
6769 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6770 len = strlen (buf) + 1;
6771 name = bfd_alloc (abfd, len);
6772 if (name == NULL)
6773 return FALSE;
6774 memcpy (name, buf, len);
6775
6776 sect = bfd_make_section_anyway (abfd, name);
6777 if (sect == NULL)
6778 return FALSE;
6779
6780 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6781 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6782 sect->filepos = note->descpos
6783 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6784 #endif
6785
6786 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6787 sect->_raw_size = sizeof (lwpstat.pr_reg);
6788 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6789 #endif
6790
6791 sect->flags = SEC_HAS_CONTENTS;
6792 sect->alignment_power = 2;
6793
6794 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6795 return FALSE;
6796
6797 /* Make a ".reg2/999" section */
6798
6799 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6800 len = strlen (buf) + 1;
6801 name = bfd_alloc (abfd, len);
6802 if (name == NULL)
6803 return FALSE;
6804 memcpy (name, buf, len);
6805
6806 sect = bfd_make_section_anyway (abfd, name);
6807 if (sect == NULL)
6808 return FALSE;
6809
6810 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6811 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6812 sect->filepos = note->descpos
6813 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6814 #endif
6815
6816 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6817 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6818 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6819 #endif
6820
6821 sect->flags = SEC_HAS_CONTENTS;
6822 sect->alignment_power = 2;
6823
6824 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6825 }
6826 #endif /* defined (HAVE_LWPSTATUS_T) */
6827
6828 #if defined (HAVE_WIN32_PSTATUS_T)
6829 static bfd_boolean
6830 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
6831 {
6832 char buf[30];
6833 char *name;
6834 size_t len;
6835 asection *sect;
6836 win32_pstatus_t pstatus;
6837
6838 if (note->descsz < sizeof (pstatus))
6839 return TRUE;
6840
6841 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6842
6843 switch (pstatus.data_type)
6844 {
6845 case NOTE_INFO_PROCESS:
6846 /* FIXME: need to add ->core_command. */
6847 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6848 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6849 break;
6850
6851 case NOTE_INFO_THREAD:
6852 /* Make a ".reg/999" section. */
6853 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6854
6855 len = strlen (buf) + 1;
6856 name = bfd_alloc (abfd, len);
6857 if (name == NULL)
6858 return FALSE;
6859
6860 memcpy (name, buf, len);
6861
6862 sect = bfd_make_section_anyway (abfd, name);
6863 if (sect == NULL)
6864 return FALSE;
6865
6866 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6867 sect->filepos = (note->descpos
6868 + offsetof (struct win32_pstatus,
6869 data.thread_info.thread_context));
6870 sect->flags = SEC_HAS_CONTENTS;
6871 sect->alignment_power = 2;
6872
6873 if (pstatus.data.thread_info.is_active_thread)
6874 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6875 return FALSE;
6876 break;
6877
6878 case NOTE_INFO_MODULE:
6879 /* Make a ".module/xxxxxxxx" section. */
6880 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6881
6882 len = strlen (buf) + 1;
6883 name = bfd_alloc (abfd, len);
6884 if (name == NULL)
6885 return FALSE;
6886
6887 memcpy (name, buf, len);
6888
6889 sect = bfd_make_section_anyway (abfd, name);
6890
6891 if (sect == NULL)
6892 return FALSE;
6893
6894 sect->_raw_size = note->descsz;
6895 sect->filepos = note->descpos;
6896 sect->flags = SEC_HAS_CONTENTS;
6897 sect->alignment_power = 2;
6898 break;
6899
6900 default:
6901 return TRUE;
6902 }
6903
6904 return TRUE;
6905 }
6906 #endif /* HAVE_WIN32_PSTATUS_T */
6907
6908 static bfd_boolean
6909 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
6910 {
6911 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6912
6913 switch (note->type)
6914 {
6915 default:
6916 return TRUE;
6917
6918 case NT_PRSTATUS:
6919 if (bed->elf_backend_grok_prstatus)
6920 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6921 return TRUE;
6922 #if defined (HAVE_PRSTATUS_T)
6923 return elfcore_grok_prstatus (abfd, note);
6924 #else
6925 return TRUE;
6926 #endif
6927
6928 #if defined (HAVE_PSTATUS_T)
6929 case NT_PSTATUS:
6930 return elfcore_grok_pstatus (abfd, note);
6931 #endif
6932
6933 #if defined (HAVE_LWPSTATUS_T)
6934 case NT_LWPSTATUS:
6935 return elfcore_grok_lwpstatus (abfd, note);
6936 #endif
6937
6938 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6939 return elfcore_grok_prfpreg (abfd, note);
6940
6941 #if defined (HAVE_WIN32_PSTATUS_T)
6942 case NT_WIN32PSTATUS:
6943 return elfcore_grok_win32pstatus (abfd, note);
6944 #endif
6945
6946 case NT_PRXFPREG: /* Linux SSE extension */
6947 if (note->namesz == 6
6948 && strcmp (note->namedata, "LINUX") == 0)
6949 return elfcore_grok_prxfpreg (abfd, note);
6950 else
6951 return TRUE;
6952
6953 case NT_PRPSINFO:
6954 case NT_PSINFO:
6955 if (bed->elf_backend_grok_psinfo)
6956 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6957 return TRUE;
6958 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6959 return elfcore_grok_psinfo (abfd, note);
6960 #else
6961 return TRUE;
6962 #endif
6963
6964 case NT_AUXV:
6965 {
6966 asection *sect = bfd_make_section_anyway (abfd, ".auxv");
6967
6968 if (sect == NULL)
6969 return FALSE;
6970 sect->_raw_size = note->descsz;
6971 sect->filepos = note->descpos;
6972 sect->flags = SEC_HAS_CONTENTS;
6973 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
6974
6975 return TRUE;
6976 }
6977 }
6978 }
6979
6980 static bfd_boolean
6981 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
6982 {
6983 char *cp;
6984
6985 cp = strchr (note->namedata, '@');
6986 if (cp != NULL)
6987 {
6988 *lwpidp = atoi(cp + 1);
6989 return TRUE;
6990 }
6991 return FALSE;
6992 }
6993
6994 static bfd_boolean
6995 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
6996 {
6997
6998 /* Signal number at offset 0x08. */
6999 elf_tdata (abfd)->core_signal
7000 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7001
7002 /* Process ID at offset 0x50. */
7003 elf_tdata (abfd)->core_pid
7004 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7005
7006 /* Command name at 0x7c (max 32 bytes, including nul). */
7007 elf_tdata (abfd)->core_command
7008 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7009
7010 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7011 note);
7012 }
7013
7014 static bfd_boolean
7015 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
7016 {
7017 int lwp;
7018
7019 if (elfcore_netbsd_get_lwpid (note, &lwp))
7020 elf_tdata (abfd)->core_lwpid = lwp;
7021
7022 if (note->type == NT_NETBSDCORE_PROCINFO)
7023 {
7024 /* NetBSD-specific core "procinfo". Note that we expect to
7025 find this note before any of the others, which is fine,
7026 since the kernel writes this note out first when it
7027 creates a core file. */
7028
7029 return elfcore_grok_netbsd_procinfo (abfd, note);
7030 }
7031
7032 /* As of Jan 2002 there are no other machine-independent notes
7033 defined for NetBSD core files. If the note type is less
7034 than the start of the machine-dependent note types, we don't
7035 understand it. */
7036
7037 if (note->type < NT_NETBSDCORE_FIRSTMACH)
7038 return TRUE;
7039
7040
7041 switch (bfd_get_arch (abfd))
7042 {
7043 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7044 PT_GETFPREGS == mach+2. */
7045
7046 case bfd_arch_alpha:
7047 case bfd_arch_sparc:
7048 switch (note->type)
7049 {
7050 case NT_NETBSDCORE_FIRSTMACH+0:
7051 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7052
7053 case NT_NETBSDCORE_FIRSTMACH+2:
7054 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7055
7056 default:
7057 return TRUE;
7058 }
7059
7060 /* On all other arch's, PT_GETREGS == mach+1 and
7061 PT_GETFPREGS == mach+3. */
7062
7063 default:
7064 switch (note->type)
7065 {
7066 case NT_NETBSDCORE_FIRSTMACH+1:
7067 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7068
7069 case NT_NETBSDCORE_FIRSTMACH+3:
7070 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7071
7072 default:
7073 return TRUE;
7074 }
7075 }
7076 /* NOTREACHED */
7077 }
7078
7079 static bfd_boolean
7080 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
7081 {
7082 void *ddata = note->descdata;
7083 char buf[100];
7084 char *name;
7085 asection *sect;
7086 short sig;
7087 unsigned flags;
7088
7089 /* nto_procfs_status 'pid' field is at offset 0. */
7090 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7091
7092 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7093 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7094
7095 /* nto_procfs_status 'flags' field is at offset 8. */
7096 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
7097
7098 /* nto_procfs_status 'what' field is at offset 14. */
7099 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7100 {
7101 elf_tdata (abfd)->core_signal = sig;
7102 elf_tdata (abfd)->core_lwpid = *tid;
7103 }
7104
7105 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7106 do not come from signals so we make sure we set the current
7107 thread just in case. */
7108 if (flags & 0x00000080)
7109 elf_tdata (abfd)->core_lwpid = *tid;
7110
7111 /* Make a ".qnx_core_status/%d" section. */
7112 sprintf (buf, ".qnx_core_status/%d", *tid);
7113
7114 name = bfd_alloc (abfd, strlen (buf) + 1);
7115 if (name == NULL)
7116 return FALSE;
7117 strcpy (name, buf);
7118
7119 sect = bfd_make_section_anyway (abfd, name);
7120 if (sect == NULL)
7121 return FALSE;
7122
7123 sect->_raw_size = note->descsz;
7124 sect->filepos = note->descpos;
7125 sect->flags = SEC_HAS_CONTENTS;
7126 sect->alignment_power = 2;
7127
7128 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7129 }
7130
7131 static bfd_boolean
7132 elfcore_grok_nto_gregs (bfd *abfd, Elf_Internal_Note *note, pid_t tid)
7133 {
7134 char buf[100];
7135 char *name;
7136 asection *sect;
7137
7138 /* Make a ".reg/%d" section. */
7139 sprintf (buf, ".reg/%d", tid);
7140
7141 name = bfd_alloc (abfd, strlen (buf) + 1);
7142 if (name == NULL)
7143 return FALSE;
7144 strcpy (name, buf);
7145
7146 sect = bfd_make_section_anyway (abfd, name);
7147 if (sect == NULL)
7148 return FALSE;
7149
7150 sect->_raw_size = note->descsz;
7151 sect->filepos = note->descpos;
7152 sect->flags = SEC_HAS_CONTENTS;
7153 sect->alignment_power = 2;
7154
7155 /* This is the current thread. */
7156 if (elf_tdata (abfd)->core_lwpid == tid)
7157 return elfcore_maybe_make_sect (abfd, ".reg", sect);
7158
7159 return TRUE;
7160 }
7161
7162 #define BFD_QNT_CORE_INFO 7
7163 #define BFD_QNT_CORE_STATUS 8
7164 #define BFD_QNT_CORE_GREG 9
7165 #define BFD_QNT_CORE_FPREG 10
7166
7167 static bfd_boolean
7168 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
7169 {
7170 /* Every GREG section has a STATUS section before it. Store the
7171 tid from the previous call to pass down to the next gregs
7172 function. */
7173 static pid_t tid = 1;
7174
7175 switch (note->type)
7176 {
7177 case BFD_QNT_CORE_INFO: return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7178 case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid);
7179 case BFD_QNT_CORE_GREG: return elfcore_grok_nto_gregs (abfd, note, tid);
7180 case BFD_QNT_CORE_FPREG: return elfcore_grok_prfpreg (abfd, note);
7181 default: return TRUE;
7182 }
7183 }
7184
7185 /* Function: elfcore_write_note
7186
7187 Inputs:
7188 buffer to hold note
7189 name of note
7190 type of note
7191 data for note
7192 size of data for note
7193
7194 Return:
7195 End of buffer containing note. */
7196
7197 char *
7198 elfcore_write_note (bfd *abfd,
7199 char *buf,
7200 int *bufsiz,
7201 const char *name,
7202 int type,
7203 const void *input,
7204 int size)
7205 {
7206 Elf_External_Note *xnp;
7207 size_t namesz;
7208 size_t pad;
7209 size_t newspace;
7210 char *p, *dest;
7211
7212 namesz = 0;
7213 pad = 0;
7214 if (name != NULL)
7215 {
7216 const struct elf_backend_data *bed;
7217
7218 namesz = strlen (name) + 1;
7219 bed = get_elf_backend_data (abfd);
7220 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
7221 }
7222
7223 newspace = 12 + namesz + pad + size;
7224
7225 p = realloc (buf, *bufsiz + newspace);
7226 dest = p + *bufsiz;
7227 *bufsiz += newspace;
7228 xnp = (Elf_External_Note *) dest;
7229 H_PUT_32 (abfd, namesz, xnp->namesz);
7230 H_PUT_32 (abfd, size, xnp->descsz);
7231 H_PUT_32 (abfd, type, xnp->type);
7232 dest = xnp->name;
7233 if (name != NULL)
7234 {
7235 memcpy (dest, name, namesz);
7236 dest += namesz;
7237 while (pad != 0)
7238 {
7239 *dest++ = '\0';
7240 --pad;
7241 }
7242 }
7243 memcpy (dest, input, size);
7244 return p;
7245 }
7246
7247 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7248 char *
7249 elfcore_write_prpsinfo (bfd *abfd,
7250 char *buf,
7251 int *bufsiz,
7252 const char *fname,
7253 const char *psargs)
7254 {
7255 int note_type;
7256 char *note_name = "CORE";
7257
7258 #if defined (HAVE_PSINFO_T)
7259 psinfo_t data;
7260 note_type = NT_PSINFO;
7261 #else
7262 prpsinfo_t data;
7263 note_type = NT_PRPSINFO;
7264 #endif
7265
7266 memset (&data, 0, sizeof (data));
7267 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7268 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7269 return elfcore_write_note (abfd, buf, bufsiz,
7270 note_name, note_type, &data, sizeof (data));
7271 }
7272 #endif /* PSINFO_T or PRPSINFO_T */
7273
7274 #if defined (HAVE_PRSTATUS_T)
7275 char *
7276 elfcore_write_prstatus (bfd *abfd,
7277 char *buf,
7278 int *bufsiz,
7279 long pid,
7280 int cursig,
7281 const void *gregs)
7282 {
7283 prstatus_t prstat;
7284 char *note_name = "CORE";
7285
7286 memset (&prstat, 0, sizeof (prstat));
7287 prstat.pr_pid = pid;
7288 prstat.pr_cursig = cursig;
7289 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7290 return elfcore_write_note (abfd, buf, bufsiz,
7291 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7292 }
7293 #endif /* HAVE_PRSTATUS_T */
7294
7295 #if defined (HAVE_LWPSTATUS_T)
7296 char *
7297 elfcore_write_lwpstatus (bfd *abfd,
7298 char *buf,
7299 int *bufsiz,
7300 long pid,
7301 int cursig,
7302 const void *gregs)
7303 {
7304 lwpstatus_t lwpstat;
7305 char *note_name = "CORE";
7306
7307 memset (&lwpstat, 0, sizeof (lwpstat));
7308 lwpstat.pr_lwpid = pid >> 16;
7309 lwpstat.pr_cursig = cursig;
7310 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7311 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7312 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7313 #if !defined(gregs)
7314 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7315 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7316 #else
7317 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7318 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7319 #endif
7320 #endif
7321 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7322 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7323 }
7324 #endif /* HAVE_LWPSTATUS_T */
7325
7326 #if defined (HAVE_PSTATUS_T)
7327 char *
7328 elfcore_write_pstatus (bfd *abfd,
7329 char *buf,
7330 int *bufsiz,
7331 long pid,
7332 int cursig,
7333 const void *gregs)
7334 {
7335 pstatus_t pstat;
7336 char *note_name = "CORE";
7337
7338 memset (&pstat, 0, sizeof (pstat));
7339 pstat.pr_pid = pid & 0xffff;
7340 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7341 NT_PSTATUS, &pstat, sizeof (pstat));
7342 return buf;
7343 }
7344 #endif /* HAVE_PSTATUS_T */
7345
7346 char *
7347 elfcore_write_prfpreg (bfd *abfd,
7348 char *buf,
7349 int *bufsiz,
7350 const void *fpregs,
7351 int size)
7352 {
7353 char *note_name = "CORE";
7354 return elfcore_write_note (abfd, buf, bufsiz,
7355 note_name, NT_FPREGSET, fpregs, size);
7356 }
7357
7358 char *
7359 elfcore_write_prxfpreg (bfd *abfd,
7360 char *buf,
7361 int *bufsiz,
7362 const void *xfpregs,
7363 int size)
7364 {
7365 char *note_name = "LINUX";
7366 return elfcore_write_note (abfd, buf, bufsiz,
7367 note_name, NT_PRXFPREG, xfpregs, size);
7368 }
7369
7370 static bfd_boolean
7371 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
7372 {
7373 char *buf;
7374 char *p;
7375
7376 if (size <= 0)
7377 return TRUE;
7378
7379 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7380 return FALSE;
7381
7382 buf = bfd_malloc (size);
7383 if (buf == NULL)
7384 return FALSE;
7385
7386 if (bfd_bread (buf, size, abfd) != size)
7387 {
7388 error:
7389 free (buf);
7390 return FALSE;
7391 }
7392
7393 p = buf;
7394 while (p < buf + size)
7395 {
7396 /* FIXME: bad alignment assumption. */
7397 Elf_External_Note *xnp = (Elf_External_Note *) p;
7398 Elf_Internal_Note in;
7399
7400 in.type = H_GET_32 (abfd, xnp->type);
7401
7402 in.namesz = H_GET_32 (abfd, xnp->namesz);
7403 in.namedata = xnp->name;
7404
7405 in.descsz = H_GET_32 (abfd, xnp->descsz);
7406 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7407 in.descpos = offset + (in.descdata - buf);
7408
7409 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7410 {
7411 if (! elfcore_grok_netbsd_note (abfd, &in))
7412 goto error;
7413 }
7414 else if (strncmp (in.namedata, "QNX", 3) == 0)
7415 {
7416 if (! elfcore_grok_nto_note (abfd, &in))
7417 goto error;
7418 }
7419 else
7420 {
7421 if (! elfcore_grok_note (abfd, &in))
7422 goto error;
7423 }
7424
7425 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7426 }
7427
7428 free (buf);
7429 return TRUE;
7430 }
7431 \f
7432 /* Providing external access to the ELF program header table. */
7433
7434 /* Return an upper bound on the number of bytes required to store a
7435 copy of ABFD's program header table entries. Return -1 if an error
7436 occurs; bfd_get_error will return an appropriate code. */
7437
7438 long
7439 bfd_get_elf_phdr_upper_bound (bfd *abfd)
7440 {
7441 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7442 {
7443 bfd_set_error (bfd_error_wrong_format);
7444 return -1;
7445 }
7446
7447 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7448 }
7449
7450 /* Copy ABFD's program header table entries to *PHDRS. The entries
7451 will be stored as an array of Elf_Internal_Phdr structures, as
7452 defined in include/elf/internal.h. To find out how large the
7453 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7454
7455 Return the number of program header table entries read, or -1 if an
7456 error occurs; bfd_get_error will return an appropriate code. */
7457
7458 int
7459 bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
7460 {
7461 int num_phdrs;
7462
7463 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7464 {
7465 bfd_set_error (bfd_error_wrong_format);
7466 return -1;
7467 }
7468
7469 num_phdrs = elf_elfheader (abfd)->e_phnum;
7470 memcpy (phdrs, elf_tdata (abfd)->phdr,
7471 num_phdrs * sizeof (Elf_Internal_Phdr));
7472
7473 return num_phdrs;
7474 }
7475
7476 void
7477 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
7478 {
7479 #ifdef BFD64
7480 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7481
7482 i_ehdrp = elf_elfheader (abfd);
7483 if (i_ehdrp == NULL)
7484 sprintf_vma (buf, value);
7485 else
7486 {
7487 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7488 {
7489 #if BFD_HOST_64BIT_LONG
7490 sprintf (buf, "%016lx", value);
7491 #else
7492 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7493 _bfd_int64_low (value));
7494 #endif
7495 }
7496 else
7497 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7498 }
7499 #else
7500 sprintf_vma (buf, value);
7501 #endif
7502 }
7503
7504 void
7505 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
7506 {
7507 #ifdef BFD64
7508 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7509
7510 i_ehdrp = elf_elfheader (abfd);
7511 if (i_ehdrp == NULL)
7512 fprintf_vma ((FILE *) stream, value);
7513 else
7514 {
7515 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7516 {
7517 #if BFD_HOST_64BIT_LONG
7518 fprintf ((FILE *) stream, "%016lx", value);
7519 #else
7520 fprintf ((FILE *) stream, "%08lx%08lx",
7521 _bfd_int64_high (value), _bfd_int64_low (value));
7522 #endif
7523 }
7524 else
7525 fprintf ((FILE *) stream, "%08lx",
7526 (unsigned long) (value & 0xffffffff));
7527 }
7528 #else
7529 fprintf_vma ((FILE *) stream, value);
7530 #endif
7531 }
7532
7533 enum elf_reloc_type_class
7534 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
7535 {
7536 return reloc_class_normal;
7537 }
7538
7539 /* For RELA architectures, return the relocation value for a
7540 relocation against a local symbol. */
7541
7542 bfd_vma
7543 _bfd_elf_rela_local_sym (bfd *abfd,
7544 Elf_Internal_Sym *sym,
7545 asection **psec,
7546 Elf_Internal_Rela *rel)
7547 {
7548 asection *sec = *psec;
7549 bfd_vma relocation;
7550
7551 relocation = (sec->output_section->vma
7552 + sec->output_offset
7553 + sym->st_value);
7554 if ((sec->flags & SEC_MERGE)
7555 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7556 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
7557 {
7558 rel->r_addend =
7559 _bfd_merged_section_offset (abfd, psec,
7560 elf_section_data (sec)->sec_info,
7561 sym->st_value + rel->r_addend);
7562 if (sec != *psec)
7563 {
7564 /* If we have changed the section, and our original section is
7565 marked with SEC_EXCLUDE, it means that the original
7566 SEC_MERGE section has been completely subsumed in some
7567 other SEC_MERGE section. In this case, we need to leave
7568 some info around for --emit-relocs. */
7569 if ((sec->flags & SEC_EXCLUDE) != 0)
7570 sec->kept_section = *psec;
7571 sec = *psec;
7572 }
7573 rel->r_addend -= relocation;
7574 rel->r_addend += sec->output_section->vma + sec->output_offset;
7575 }
7576 return relocation;
7577 }
7578
7579 bfd_vma
7580 _bfd_elf_rel_local_sym (bfd *abfd,
7581 Elf_Internal_Sym *sym,
7582 asection **psec,
7583 bfd_vma addend)
7584 {
7585 asection *sec = *psec;
7586
7587 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
7588 return sym->st_value + addend;
7589
7590 return _bfd_merged_section_offset (abfd, psec,
7591 elf_section_data (sec)->sec_info,
7592 sym->st_value + addend);
7593 }
7594
7595 bfd_vma
7596 _bfd_elf_section_offset (bfd *abfd,
7597 struct bfd_link_info *info,
7598 asection *sec,
7599 bfd_vma offset)
7600 {
7601 struct bfd_elf_section_data *sec_data;
7602
7603 sec_data = elf_section_data (sec);
7604 switch (sec->sec_info_type)
7605 {
7606 case ELF_INFO_TYPE_STABS:
7607 return _bfd_stab_section_offset (abfd,
7608 &elf_hash_table (info)->merge_info,
7609 sec, &sec_data->sec_info, offset);
7610 case ELF_INFO_TYPE_EH_FRAME:
7611 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7612 default:
7613 return offset;
7614 }
7615 }
7616 \f
7617 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
7618 reconstruct an ELF file by reading the segments out of remote memory
7619 based on the ELF file header at EHDR_VMA and the ELF program headers it
7620 points to. If not null, *LOADBASEP is filled in with the difference
7621 between the VMAs from which the segments were read, and the VMAs the
7622 file headers (and hence BFD's idea of each section's VMA) put them at.
7623
7624 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7625 remote memory at target address VMA into the local buffer at MYADDR; it
7626 should return zero on success or an `errno' code on failure. TEMPL must
7627 be a BFD for an ELF target with the word size and byte order found in
7628 the remote memory. */
7629
7630 bfd *
7631 bfd_elf_bfd_from_remote_memory
7632 (bfd *templ,
7633 bfd_vma ehdr_vma,
7634 bfd_vma *loadbasep,
7635 int (*target_read_memory) (bfd_vma, char *, int))
7636 {
7637 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7638 (templ, ehdr_vma, loadbasep, target_read_memory);
7639 }
7640 \f
7641 long
7642 _bfd_elf_get_synthetic_symtab (bfd *abfd, asymbol **dynsyms, asymbol **ret)
7643 {
7644 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7645 asection *relplt;
7646 asymbol *s;
7647 const char *relplt_name;
7648 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7649 arelent *p;
7650 long count, i, n;
7651 size_t size;
7652 Elf_Internal_Shdr *hdr;
7653 char *names;
7654 asection *plt;
7655
7656 *ret = NULL;
7657 if (!bed->plt_sym_val)
7658 return 0;
7659
7660 relplt_name = bed->relplt_name;
7661 if (relplt_name == NULL)
7662 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
7663 relplt = bfd_get_section_by_name (abfd, relplt_name);
7664 if (relplt == NULL)
7665 return 0;
7666
7667 hdr = &elf_section_data (relplt)->this_hdr;
7668 if (hdr->sh_link != elf_dynsymtab (abfd)
7669 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
7670 return 0;
7671
7672 plt = bfd_get_section_by_name (abfd, ".plt");
7673 if (plt == NULL)
7674 return 0;
7675
7676 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7677 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
7678 return -1;
7679
7680 count = relplt->_raw_size / hdr->sh_entsize;
7681 size = count * sizeof (asymbol);
7682 p = relplt->relocation;
7683 for (i = 0; i < count; i++, s++, p++)
7684 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
7685
7686 s = *ret = bfd_malloc (size);
7687 if (s == NULL)
7688 return -1;
7689
7690 names = (char *) (s + count);
7691 p = relplt->relocation;
7692 n = 0;
7693 for (i = 0; i < count; i++, s++, p++)
7694 {
7695 size_t len;
7696 bfd_vma addr;
7697
7698 addr = bed->plt_sym_val (i, plt, p);
7699 if (addr == (bfd_vma) -1)
7700 continue;
7701
7702 *s = **p->sym_ptr_ptr;
7703 s->section = plt;
7704 s->value = addr - plt->vma;
7705 s->name = names;
7706 len = strlen ((*p->sym_ptr_ptr)->name);
7707 memcpy (names, (*p->sym_ptr_ptr)->name, len);
7708 names += len;
7709 memcpy (names, "@plt", sizeof ("@plt"));
7710 names += sizeof ("@plt");
7711 ++n;
7712 }
7713
7714 return n;
7715 }
This page took 0.294865 seconds and 5 git commands to generate.