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