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