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