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