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