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