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