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