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