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