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