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