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