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