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