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