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