]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
Add no-c-format comment to prevent confusion when translating string.
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
e1fddb6b 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
7898deda 3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/*
22
23SECTION
24 ELF backends
25
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
29
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet.
33 */
34
7ee38065
MS
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
252b5132
RH
37#include "bfd.h"
38#include "sysdep.h"
39#include "bfdlink.h"
40#include "libbfd.h"
41#define ARCH_SIZE 0
42#include "elf-bfd.h"
e0e8c97f 43#include "libiberty.h"
252b5132
RH
44
45static INLINE struct elf_segment_map *make_mapping
46 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
47static boolean map_sections_to_segments PARAMS ((bfd *));
48static int elf_sort_sections PARAMS ((const PTR, const PTR));
49static boolean assign_file_positions_for_segments PARAMS ((bfd *));
50static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
51static boolean prep_headers PARAMS ((bfd *));
52static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
53static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
dc810e39 54static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
dbb410c3 55static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
d3c456e9 56static void merge_sections_remove_hook PARAMS ((bfd *, asection *));
252b5132 57static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
dbb410c3 58static void set_group_contents PARAMS ((bfd *, asection *, PTR));
252b5132
RH
59static boolean assign_section_numbers PARAMS ((bfd *));
60static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
61static boolean elf_map_symbols PARAMS ((bfd *));
62static bfd_size_type get_program_header_size PARAMS ((bfd *));
dc810e39 63static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
a7b97311
AM
64static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
65 bfd_vma, const char **,
66 const char **));
67static int elfcore_make_pid PARAMS ((bfd *));
68static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
69static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
70 Elf_Internal_Note *));
71static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
72static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
73static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
252b5132 74
50b2bdb7
AM
75static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *));
76static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *,
77 Elf_Internal_Note *));
78static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *));
79
252b5132
RH
80/* Swap version information in and out. The version information is
81 currently size independent. If that ever changes, this code will
82 need to move into elfcode.h. */
83
84/* Swap in a Verdef structure. */
85
86void
87_bfd_elf_swap_verdef_in (abfd, src, dst)
88 bfd *abfd;
89 const Elf_External_Verdef *src;
90 Elf_Internal_Verdef *dst;
91{
dc810e39
AM
92 dst->vd_version = H_GET_16 (abfd, src->vd_version);
93 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
94 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
95 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
96 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
97 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
98 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
99}
100
101/* Swap out a Verdef structure. */
102
103void
104_bfd_elf_swap_verdef_out (abfd, src, dst)
105 bfd *abfd;
106 const Elf_Internal_Verdef *src;
107 Elf_External_Verdef *dst;
108{
dc810e39
AM
109 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
110 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
111 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
112 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
113 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
114 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
115 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
116}
117
118/* Swap in a Verdaux structure. */
119
120void
121_bfd_elf_swap_verdaux_in (abfd, src, dst)
122 bfd *abfd;
123 const Elf_External_Verdaux *src;
124 Elf_Internal_Verdaux *dst;
125{
dc810e39
AM
126 dst->vda_name = H_GET_32 (abfd, src->vda_name);
127 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
128}
129
130/* Swap out a Verdaux structure. */
131
132void
133_bfd_elf_swap_verdaux_out (abfd, src, dst)
134 bfd *abfd;
135 const Elf_Internal_Verdaux *src;
136 Elf_External_Verdaux *dst;
137{
dc810e39
AM
138 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
139 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
140}
141
142/* Swap in a Verneed structure. */
143
144void
145_bfd_elf_swap_verneed_in (abfd, src, dst)
146 bfd *abfd;
147 const Elf_External_Verneed *src;
148 Elf_Internal_Verneed *dst;
149{
dc810e39
AM
150 dst->vn_version = H_GET_16 (abfd, src->vn_version);
151 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
152 dst->vn_file = H_GET_32 (abfd, src->vn_file);
153 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
154 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
155}
156
157/* Swap out a Verneed structure. */
158
159void
160_bfd_elf_swap_verneed_out (abfd, src, dst)
161 bfd *abfd;
162 const Elf_Internal_Verneed *src;
163 Elf_External_Verneed *dst;
164{
dc810e39
AM
165 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
166 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
167 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
168 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
169 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
170}
171
172/* Swap in a Vernaux structure. */
173
174void
175_bfd_elf_swap_vernaux_in (abfd, src, dst)
176 bfd *abfd;
177 const Elf_External_Vernaux *src;
178 Elf_Internal_Vernaux *dst;
179{
dc810e39
AM
180 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
181 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
182 dst->vna_other = H_GET_16 (abfd, src->vna_other);
183 dst->vna_name = H_GET_32 (abfd, src->vna_name);
184 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
185}
186
187/* Swap out a Vernaux structure. */
188
189void
190_bfd_elf_swap_vernaux_out (abfd, src, dst)
191 bfd *abfd;
192 const Elf_Internal_Vernaux *src;
193 Elf_External_Vernaux *dst;
194{
dc810e39
AM
195 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
196 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
197 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
198 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
199 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
200}
201
202/* Swap in a Versym structure. */
203
204void
205_bfd_elf_swap_versym_in (abfd, src, dst)
206 bfd *abfd;
207 const Elf_External_Versym *src;
208 Elf_Internal_Versym *dst;
209{
dc810e39 210 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
211}
212
213/* Swap out a Versym structure. */
214
215void
216_bfd_elf_swap_versym_out (abfd, src, dst)
217 bfd *abfd;
218 const Elf_Internal_Versym *src;
219 Elf_External_Versym *dst;
220{
dc810e39 221 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
222}
223
224/* Standard ELF hash function. Do not change this function; you will
225 cause invalid hash tables to be generated. */
3a99b017 226
252b5132 227unsigned long
3a99b017
ILT
228bfd_elf_hash (namearg)
229 const char *namearg;
252b5132 230{
3a99b017 231 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
232 unsigned long h = 0;
233 unsigned long g;
234 int ch;
235
236 while ((ch = *name++) != '\0')
237 {
238 h = (h << 4) + ch;
239 if ((g = (h & 0xf0000000)) != 0)
240 {
241 h ^= g >> 24;
242 /* The ELF ABI says `h &= ~g', but this is equivalent in
243 this case and on some machines one insn instead of two. */
244 h ^= g;
245 }
246 }
247 return h;
248}
249
250/* Read a specified number of bytes at a specified offset in an ELF
251 file, into a newly allocated buffer, and return a pointer to the
c044fabd 252 buffer. */
252b5132
RH
253
254static char *
255elf_read (abfd, offset, size)
c044fabd 256 bfd *abfd;
dc810e39
AM
257 file_ptr offset;
258 bfd_size_type size;
252b5132
RH
259{
260 char *buf;
261
262 if ((buf = bfd_alloc (abfd, size)) == NULL)
263 return NULL;
dc810e39 264 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
252b5132 265 return NULL;
dc810e39 266 if (bfd_bread ((PTR) buf, size, abfd) != size)
252b5132
RH
267 {
268 if (bfd_get_error () != bfd_error_system_call)
269 bfd_set_error (bfd_error_file_truncated);
270 return NULL;
271 }
272 return buf;
273}
274
275boolean
276bfd_elf_mkobject (abfd)
c044fabd 277 bfd *abfd;
252b5132 278{
c044fabd
KH
279 /* This just does initialization. */
280 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
dc810e39
AM
281 bfd_size_type amt = sizeof (struct elf_obj_tdata);
282 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
252b5132
RH
283 if (elf_tdata (abfd) == 0)
284 return false;
c044fabd
KH
285 /* Since everything is done at close time, do we need any
286 initialization? */
252b5132
RH
287
288 return true;
289}
290
291boolean
292bfd_elf_mkcorefile (abfd)
c044fabd 293 bfd *abfd;
252b5132 294{
c044fabd 295 /* I think this can be done just like an object file. */
252b5132
RH
296 return bfd_elf_mkobject (abfd);
297}
298
299char *
300bfd_elf_get_str_section (abfd, shindex)
c044fabd 301 bfd *abfd;
252b5132
RH
302 unsigned int shindex;
303{
304 Elf_Internal_Shdr **i_shdrp;
305 char *shstrtab = NULL;
dc810e39
AM
306 file_ptr offset;
307 bfd_size_type shstrtabsize;
252b5132
RH
308
309 i_shdrp = elf_elfsections (abfd);
310 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
311 return 0;
312
313 shstrtab = (char *) i_shdrp[shindex]->contents;
314 if (shstrtab == NULL)
315 {
c044fabd 316 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
317 offset = i_shdrp[shindex]->sh_offset;
318 shstrtabsize = i_shdrp[shindex]->sh_size;
319 shstrtab = elf_read (abfd, offset, shstrtabsize);
320 i_shdrp[shindex]->contents = (PTR) shstrtab;
321 }
322 return shstrtab;
323}
324
325char *
326bfd_elf_string_from_elf_section (abfd, shindex, strindex)
c044fabd 327 bfd *abfd;
252b5132
RH
328 unsigned int shindex;
329 unsigned int strindex;
330{
331 Elf_Internal_Shdr *hdr;
332
333 if (strindex == 0)
334 return "";
335
336 hdr = elf_elfsections (abfd)[shindex];
337
338 if (hdr->contents == NULL
339 && bfd_elf_get_str_section (abfd, shindex) == NULL)
340 return NULL;
341
342 if (strindex >= hdr->sh_size)
343 {
344 (*_bfd_error_handler)
345 (_("%s: invalid string offset %u >= %lu for section `%s'"),
8f615d07 346 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
252b5132
RH
347 ((shindex == elf_elfheader(abfd)->e_shstrndx
348 && strindex == hdr->sh_name)
349 ? ".shstrtab"
350 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
351 return "";
352 }
353
354 return ((char *) hdr->contents) + strindex;
355}
356
dbb410c3
AM
357/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
358 sections. The first element is the flags, the rest are section
359 pointers. */
360
361typedef union elf_internal_group {
362 Elf_Internal_Shdr *shdr;
363 unsigned int flags;
364} Elf_Internal_Group;
365
366/* Set next_in_group list pointer, and group name for NEWSECT. */
367
368static boolean
369setup_group (abfd, hdr, newsect)
370 bfd *abfd;
371 Elf_Internal_Shdr *hdr;
372 asection *newsect;
373{
374 unsigned int num_group = elf_tdata (abfd)->num_group;
375
376 /* If num_group is zero, read in all SHT_GROUP sections. The count
377 is set to -1 if there are no SHT_GROUP sections. */
378 if (num_group == 0)
379 {
380 unsigned int i, shnum;
381
382 /* First count the number of groups. If we have a SHT_GROUP
383 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 384 shnum = elf_numsections (abfd);
dbb410c3
AM
385 num_group = 0;
386 for (i = 0; i < shnum; i++)
387 {
388 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
389 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
390 num_group += 1;
391 }
392
393 if (num_group == 0)
973ffd63 394 num_group = (unsigned) -1;
dbb410c3
AM
395 elf_tdata (abfd)->num_group = num_group;
396
397 if (num_group > 0)
398 {
399 /* We keep a list of elf section headers for group sections,
400 so we can find them quickly. */
401 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
402 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
403 if (elf_tdata (abfd)->group_sect_ptr == NULL)
404 return false;
405
406 num_group = 0;
407 for (i = 0; i < shnum; i++)
408 {
409 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
410 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
411 {
973ffd63 412 unsigned char *src;
dbb410c3
AM
413 Elf_Internal_Group *dest;
414
415 /* Add to list of sections. */
416 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
417 num_group += 1;
418
419 /* Read the raw contents. */
420 BFD_ASSERT (sizeof (*dest) >= 4);
421 amt = shdr->sh_size * sizeof (*dest) / 4;
422 shdr->contents = bfd_alloc (abfd, amt);
423 if (shdr->contents == NULL
424 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
425 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
426 != shdr->sh_size))
427 return false;
428
429 /* Translate raw contents, a flag word followed by an
430 array of elf section indices all in target byte order,
431 to the flag word followed by an array of elf section
432 pointers. */
433 src = shdr->contents + shdr->sh_size;
434 dest = (Elf_Internal_Group *) (shdr->contents + amt);
435 while (1)
436 {
437 unsigned int idx;
438
439 src -= 4;
440 --dest;
441 idx = H_GET_32 (abfd, src);
442 if (src == shdr->contents)
443 {
444 dest->flags = idx;
445 break;
446 }
447 if (idx >= shnum)
448 {
449 ((*_bfd_error_handler)
450 (_("%s: invalid SHT_GROUP entry"),
451 bfd_archive_filename (abfd)));
452 idx = 0;
453 }
454 dest->shdr = elf_elfsections (abfd)[idx];
455 }
456 }
457 }
458 }
459 }
460
461 if (num_group != (unsigned) -1)
462 {
463 unsigned int i;
464
465 for (i = 0; i < num_group; i++)
466 {
467 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
468 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
469 unsigned int n_elt = shdr->sh_size / 4;
470
471 /* Look through this group's sections to see if current
472 section is a member. */
473 while (--n_elt != 0)
474 if ((++idx)->shdr == hdr)
475 {
e0e8c97f 476 asection *s = NULL;
dbb410c3
AM
477
478 /* We are a member of this group. Go looking through
479 other members to see if any others are linked via
480 next_in_group. */
481 idx = (Elf_Internal_Group *) shdr->contents;
482 n_elt = shdr->sh_size / 4;
483 while (--n_elt != 0)
484 if ((s = (++idx)->shdr->bfd_section) != NULL
945906ff 485 && elf_next_in_group (s) != NULL)
dbb410c3
AM
486 break;
487 if (n_elt != 0)
488 {
dbb410c3
AM
489 /* Snarf the group name from other member, and
490 insert current section in circular list. */
945906ff
AM
491 elf_group_name (newsect) = elf_group_name (s);
492 elf_next_in_group (newsect) = elf_next_in_group (s);
493 elf_next_in_group (s) = newsect;
dbb410c3
AM
494 }
495 else
496 {
497 struct elf_backend_data *bed;
498 file_ptr pos;
499 unsigned char ename[4];
500 unsigned long iname;
501 const char *gname;
502
503 /* Humbug. Get the name from the group signature
504 symbol. Why isn't the signature just a string?
505 Fortunately, the name index is at the same
506 place in the external symbol for both 32 and 64
507 bit ELF. */
508 bed = get_elf_backend_data (abfd);
509 pos = elf_tdata (abfd)->symtab_hdr.sh_offset;
510 pos += shdr->sh_info * bed->s->sizeof_sym;
511 if (bfd_seek (abfd, pos, SEEK_SET) != 0
973ffd63 512 || bfd_bread (ename, (bfd_size_type) 4, abfd) != 4)
dbb410c3
AM
513 return false;
514 iname = H_GET_32 (abfd, ename);
515 gname = elf_string_from_elf_strtab (abfd, iname);
945906ff 516 elf_group_name (newsect) = gname;
dbb410c3
AM
517
518 /* Start a circular list with one element. */
945906ff 519 elf_next_in_group (newsect) = newsect;
dbb410c3
AM
520 }
521 if (shdr->bfd_section != NULL)
945906ff 522 elf_next_in_group (shdr->bfd_section) = newsect;
dbb410c3
AM
523 i = num_group - 1;
524 break;
525 }
526 }
527 }
528
945906ff 529 if (elf_group_name (newsect) == NULL)
dbb410c3
AM
530 {
531 (*_bfd_error_handler) (_("%s: no group info for section %s"),
532 bfd_archive_filename (abfd), newsect->name);
533 }
534 return true;
535}
536
252b5132
RH
537/* Make a BFD section from an ELF section. We store a pointer to the
538 BFD section in the bfd_section field of the header. */
539
540boolean
541_bfd_elf_make_section_from_shdr (abfd, hdr, name)
542 bfd *abfd;
543 Elf_Internal_Shdr *hdr;
544 const char *name;
545{
546 asection *newsect;
547 flagword flags;
fa152c49 548 struct elf_backend_data *bed;
252b5132
RH
549
550 if (hdr->bfd_section != NULL)
551 {
552 BFD_ASSERT (strcmp (name,
553 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
554 return true;
555 }
556
557 newsect = bfd_make_section_anyway (abfd, name);
558 if (newsect == NULL)
559 return false;
560
561 newsect->filepos = hdr->sh_offset;
562
563 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
564 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
565 || ! bfd_set_section_alignment (abfd, newsect,
dc810e39 566 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
252b5132
RH
567 return false;
568
569 flags = SEC_NO_FLAGS;
570 if (hdr->sh_type != SHT_NOBITS)
571 flags |= SEC_HAS_CONTENTS;
dbb410c3
AM
572 if (hdr->sh_type == SHT_GROUP)
573 flags |= SEC_GROUP | SEC_EXCLUDE;
252b5132
RH
574 if ((hdr->sh_flags & SHF_ALLOC) != 0)
575 {
576 flags |= SEC_ALLOC;
577 if (hdr->sh_type != SHT_NOBITS)
578 flags |= SEC_LOAD;
579 }
580 if ((hdr->sh_flags & SHF_WRITE) == 0)
581 flags |= SEC_READONLY;
582 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
583 flags |= SEC_CODE;
584 else if ((flags & SEC_LOAD) != 0)
585 flags |= SEC_DATA;
f5fa8ca2
JJ
586 if ((hdr->sh_flags & SHF_MERGE) != 0)
587 {
588 flags |= SEC_MERGE;
589 newsect->entsize = hdr->sh_entsize;
590 if ((hdr->sh_flags & SHF_STRINGS) != 0)
591 flags |= SEC_STRINGS;
592 }
dbb410c3
AM
593 if (hdr->sh_flags & SHF_GROUP)
594 if (!setup_group (abfd, hdr, newsect))
595 return false;
252b5132
RH
596
597 /* The debugging sections appear to be recognized only by name, not
598 any sort of flag. */
7a6cc5fb 599 {
dbf48117 600 static const char *debug_sec_names [] =
7a6cc5fb
NC
601 {
602 ".debug",
603 ".gnu.linkonce.wi.",
604 ".line",
605 ".stab"
606 };
607 int i;
608
e0e8c97f 609 for (i = ARRAY_SIZE (debug_sec_names); i--;)
7a6cc5fb
NC
610 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
611 break;
612
613 if (i >= 0)
614 flags |= SEC_DEBUGGING;
615 }
252b5132
RH
616
617 /* As a GNU extension, if the name begins with .gnu.linkonce, we
618 only link a single copy of the section. This is used to support
619 g++. g++ will emit each template expansion in its own section.
620 The symbols will be defined as weak, so that multiple definitions
621 are permitted. The GNU linker extension is to actually discard
622 all but one of the sections. */
623 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
624 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
625
fa152c49
JW
626 bed = get_elf_backend_data (abfd);
627 if (bed->elf_backend_section_flags)
628 if (! bed->elf_backend_section_flags (&flags, hdr))
629 return false;
630
252b5132
RH
631 if (! bfd_set_section_flags (abfd, newsect, flags))
632 return false;
633
634 if ((flags & SEC_ALLOC) != 0)
635 {
636 Elf_Internal_Phdr *phdr;
637 unsigned int i;
638
639 /* Look through the phdrs to see if we need to adjust the lma.
640 If all the p_paddr fields are zero, we ignore them, since
641 some ELF linkers produce such output. */
642 phdr = elf_tdata (abfd)->phdr;
643 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
644 {
645 if (phdr->p_paddr != 0)
646 break;
647 }
648 if (i < elf_elfheader (abfd)->e_phnum)
649 {
650 phdr = elf_tdata (abfd)->phdr;
651 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
652 {
e0e8c97f
NC
653 /* This section is part of this segment if its file
654 offset plus size lies within the segment's memory
655 span and, if the section is loaded, the extent of the
656 loaded data lies within the extent of the segment.
657 If the p_paddr field is not set, we don't alter the
658 LMA. */
252b5132 659 if (phdr->p_type == PT_LOAD
e0e8c97f
NC
660 && phdr->p_paddr
661 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
662 && (hdr->sh_offset + hdr->sh_size
663 <= phdr->p_offset + phdr->p_memsz)
252b5132 664 && ((flags & SEC_LOAD) == 0
e0e8c97f
NC
665 || (phdr->p_offset + phdr->p_filesz
666 >= hdr->sh_offset + hdr->sh_size)))
252b5132 667 {
e0e8c97f
NC
668 /* We used to do a relative adjustment here, but
669 that doesn't work if the segment is packed with
670 code from multiple VMAs. Instead we calculate
671 the LMA absoultely, based on the LMA of the
672 segment (it is assumed that the segment will
673 contain sections with contiguous LMAs, even if
674 the VMAs are not). */
675 newsect->lma = phdr->p_paddr
676 + hdr->sh_offset - phdr->p_offset;
252b5132
RH
677 break;
678 }
679 }
680 }
681 }
682
683 hdr->bfd_section = newsect;
684 elf_section_data (newsect)->this_hdr = *hdr;
685
686 return true;
687}
688
689/*
690INTERNAL_FUNCTION
691 bfd_elf_find_section
692
693SYNOPSIS
694 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
695
696DESCRIPTION
697 Helper functions for GDB to locate the string tables.
698 Since BFD hides string tables from callers, GDB needs to use an
699 internal hook to find them. Sun's .stabstr, in particular,
700 isn't even pointed to by the .stab section, so ordinary
701 mechanisms wouldn't work to find it, even if we had some.
702*/
703
704struct elf_internal_shdr *
705bfd_elf_find_section (abfd, name)
c044fabd 706 bfd *abfd;
252b5132
RH
707 char *name;
708{
709 Elf_Internal_Shdr **i_shdrp;
710 char *shstrtab;
711 unsigned int max;
712 unsigned int i;
713
714 i_shdrp = elf_elfsections (abfd);
715 if (i_shdrp != NULL)
716 {
9ad5cbcf
AM
717 shstrtab = bfd_elf_get_str_section (abfd,
718 elf_elfheader (abfd)->e_shstrndx);
252b5132
RH
719 if (shstrtab != NULL)
720 {
9ad5cbcf 721 max = elf_numsections (abfd);
252b5132
RH
722 for (i = 1; i < max; i++)
723 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
724 return i_shdrp[i];
725 }
726 }
727 return 0;
728}
729
730const char *const bfd_elf_section_type_names[] = {
731 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
732 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
733 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
734};
735
736/* ELF relocs are against symbols. If we are producing relocateable
737 output, and the reloc is against an external symbol, and nothing
738 has given us any additional addend, the resulting reloc will also
739 be against the same symbol. In such a case, we don't want to
740 change anything about the way the reloc is handled, since it will
741 all be done at final link time. Rather than put special case code
742 into bfd_perform_relocation, all the reloc types use this howto
743 function. It just short circuits the reloc if producing
744 relocateable output against an external symbol. */
745
252b5132
RH
746bfd_reloc_status_type
747bfd_elf_generic_reloc (abfd,
748 reloc_entry,
749 symbol,
750 data,
751 input_section,
752 output_bfd,
753 error_message)
7442e600 754 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
755 arelent *reloc_entry;
756 asymbol *symbol;
7442e600 757 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
758 asection *input_section;
759 bfd *output_bfd;
7442e600 760 char **error_message ATTRIBUTE_UNUSED;
252b5132
RH
761{
762 if (output_bfd != (bfd *) NULL
763 && (symbol->flags & BSF_SECTION_SYM) == 0
764 && (! reloc_entry->howto->partial_inplace
765 || reloc_entry->addend == 0))
766 {
767 reloc_entry->address += input_section->output_offset;
768 return bfd_reloc_ok;
769 }
770
771 return bfd_reloc_continue;
772}
773\f
d3c456e9
JJ
774/* Make sure sec_info_type is cleared if sec_info is cleared too. */
775
776static void
777merge_sections_remove_hook (abfd, sec)
778 bfd *abfd ATTRIBUTE_UNUSED;
779 asection *sec;
780{
781 struct bfd_elf_section_data *sec_data;
782
783 sec_data = elf_section_data (sec);
784 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);
785 sec_data->sec_info_type = ELF_INFO_TYPE_NONE;
786}
787
8550eb6e
JJ
788/* Finish SHF_MERGE section merging. */
789
790boolean
791_bfd_elf_merge_sections (abfd, info)
792 bfd *abfd;
793 struct bfd_link_info *info;
794{
b0f35f36 795 if (!is_elf_hash_table (info))
8ea2e4bd 796 return false;
b0f35f36 797 if (elf_hash_table (info)->merge_info)
d3c456e9
JJ
798 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
799 merge_sections_remove_hook);
8550eb6e
JJ
800 return true;
801}
802\f
0ac4564e
L
803/* Copy the program header and other data from one object module to
804 another. */
252b5132 805
2d502050
L
806boolean
807_bfd_elf_copy_private_bfd_data (ibfd, obfd)
808 bfd *ibfd;
809 bfd *obfd;
810{
811 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
812 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
813 return true;
814
815 BFD_ASSERT (!elf_flags_init (obfd)
816 || (elf_elfheader (obfd)->e_flags
817 == elf_elfheader (ibfd)->e_flags));
818
0ac4564e 819 elf_gp (obfd) = elf_gp (ibfd);
2d502050
L
820 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
821 elf_flags_init (obfd) = true;
822 return true;
823}
824
f0b79d91
L
825/* Print out the program headers. */
826
252b5132
RH
827boolean
828_bfd_elf_print_private_bfd_data (abfd, farg)
829 bfd *abfd;
830 PTR farg;
831{
832 FILE *f = (FILE *) farg;
833 Elf_Internal_Phdr *p;
834 asection *s;
835 bfd_byte *dynbuf = NULL;
836
837 p = elf_tdata (abfd)->phdr;
838 if (p != NULL)
839 {
840 unsigned int i, c;
841
842 fprintf (f, _("\nProgram Header:\n"));
843 c = elf_elfheader (abfd)->e_phnum;
844 for (i = 0; i < c; i++, p++)
845 {
dc810e39 846 const char *pt;
252b5132
RH
847 char buf[20];
848
849 switch (p->p_type)
850 {
dc810e39
AM
851 case PT_NULL: pt = "NULL"; break;
852 case PT_LOAD: pt = "LOAD"; break;
853 case PT_DYNAMIC: pt = "DYNAMIC"; break;
854 case PT_INTERP: pt = "INTERP"; break;
855 case PT_NOTE: pt = "NOTE"; break;
856 case PT_SHLIB: pt = "SHLIB"; break;
857 case PT_PHDR: pt = "PHDR"; break;
65765700 858 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
dc810e39 859 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
252b5132 860 }
dc810e39 861 fprintf (f, "%8s off 0x", pt);
60b89a18 862 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 863 fprintf (f, " vaddr 0x");
60b89a18 864 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 865 fprintf (f, " paddr 0x");
60b89a18 866 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
867 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
868 fprintf (f, " filesz 0x");
60b89a18 869 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 870 fprintf (f, " memsz 0x");
60b89a18 871 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
872 fprintf (f, " flags %c%c%c",
873 (p->p_flags & PF_R) != 0 ? 'r' : '-',
874 (p->p_flags & PF_W) != 0 ? 'w' : '-',
875 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
876 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
877 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
878 fprintf (f, "\n");
879 }
880 }
881
882 s = bfd_get_section_by_name (abfd, ".dynamic");
883 if (s != NULL)
884 {
885 int elfsec;
dc810e39 886 unsigned long shlink;
252b5132
RH
887 bfd_byte *extdyn, *extdynend;
888 size_t extdynsize;
889 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
890
891 fprintf (f, _("\nDynamic Section:\n"));
892
893 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
894 if (dynbuf == NULL)
895 goto error_return;
896 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
897 s->_raw_size))
898 goto error_return;
899
900 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
901 if (elfsec == -1)
902 goto error_return;
dc810e39 903 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
904
905 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
906 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
907
908 extdyn = dynbuf;
909 extdynend = extdyn + s->_raw_size;
910 for (; extdyn < extdynend; extdyn += extdynsize)
911 {
912 Elf_Internal_Dyn dyn;
913 const char *name;
914 char ab[20];
915 boolean stringp;
916
917 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
918
919 if (dyn.d_tag == DT_NULL)
920 break;
921
922 stringp = false;
923 switch (dyn.d_tag)
924 {
925 default:
926 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
927 name = ab;
928 break;
929
930 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
931 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
932 case DT_PLTGOT: name = "PLTGOT"; break;
933 case DT_HASH: name = "HASH"; break;
934 case DT_STRTAB: name = "STRTAB"; break;
935 case DT_SYMTAB: name = "SYMTAB"; break;
936 case DT_RELA: name = "RELA"; break;
937 case DT_RELASZ: name = "RELASZ"; break;
938 case DT_RELAENT: name = "RELAENT"; break;
939 case DT_STRSZ: name = "STRSZ"; break;
940 case DT_SYMENT: name = "SYMENT"; break;
941 case DT_INIT: name = "INIT"; break;
942 case DT_FINI: name = "FINI"; break;
943 case DT_SONAME: name = "SONAME"; stringp = true; break;
944 case DT_RPATH: name = "RPATH"; stringp = true; break;
945 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
946 case DT_REL: name = "REL"; break;
947 case DT_RELSZ: name = "RELSZ"; break;
948 case DT_RELENT: name = "RELENT"; break;
949 case DT_PLTREL: name = "PLTREL"; break;
950 case DT_DEBUG: name = "DEBUG"; break;
951 case DT_TEXTREL: name = "TEXTREL"; break;
952 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
953 case DT_BIND_NOW: name = "BIND_NOW"; break;
954 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
955 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
956 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
957 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
958 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
959 case DT_FLAGS: name = "FLAGS"; break;
960 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
961 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 962 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
963 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
964 case DT_MOVEENT: name = "MOVEENT"; break;
965 case DT_MOVESZ: name = "MOVESZ"; break;
966 case DT_FEATURE: name = "FEATURE"; break;
967 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
968 case DT_SYMINSZ: name = "SYMINSZ"; break;
969 case DT_SYMINENT: name = "SYMINENT"; break;
36a30e65
L
970 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
971 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
972 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
94558834
L
973 case DT_PLTPAD: name = "PLTPAD"; break;
974 case DT_MOVETAB: name = "MOVETAB"; break;
975 case DT_SYMINFO: name = "SYMINFO"; break;
976 case DT_RELACOUNT: name = "RELACOUNT"; break;
977 case DT_RELCOUNT: name = "RELCOUNT"; break;
978 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
979 case DT_VERSYM: name = "VERSYM"; break;
980 case DT_VERDEF: name = "VERDEF"; break;
981 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
982 case DT_VERNEED: name = "VERNEED"; break;
983 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
94558834
L
984 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
985 case DT_USED: name = "USED"; break;
986 case DT_FILTER: name = "FILTER"; stringp = true; break;
252b5132
RH
987 }
988
989 fprintf (f, " %-11s ", name);
990 if (! stringp)
991 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
992 else
993 {
994 const char *string;
dc810e39 995 unsigned int tagv = dyn.d_un.d_val;
252b5132 996
dc810e39 997 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
998 if (string == NULL)
999 goto error_return;
1000 fprintf (f, "%s", string);
1001 }
1002 fprintf (f, "\n");
1003 }
1004
1005 free (dynbuf);
1006 dynbuf = NULL;
1007 }
1008
1009 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1010 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1011 {
1012 if (! _bfd_elf_slurp_version_tables (abfd))
1013 return false;
1014 }
1015
1016 if (elf_dynverdef (abfd) != 0)
1017 {
1018 Elf_Internal_Verdef *t;
1019
1020 fprintf (f, _("\nVersion definitions:\n"));
1021 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1022 {
1023 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1024 t->vd_flags, t->vd_hash, t->vd_nodename);
1025 if (t->vd_auxptr->vda_nextptr != NULL)
1026 {
1027 Elf_Internal_Verdaux *a;
1028
1029 fprintf (f, "\t");
1030 for (a = t->vd_auxptr->vda_nextptr;
1031 a != NULL;
1032 a = a->vda_nextptr)
1033 fprintf (f, "%s ", a->vda_nodename);
1034 fprintf (f, "\n");
1035 }
1036 }
1037 }
1038
1039 if (elf_dynverref (abfd) != 0)
1040 {
1041 Elf_Internal_Verneed *t;
1042
1043 fprintf (f, _("\nVersion References:\n"));
1044 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1045 {
1046 Elf_Internal_Vernaux *a;
1047
1048 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1049 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1050 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1051 a->vna_flags, a->vna_other, a->vna_nodename);
1052 }
1053 }
1054
1055 return true;
1056
1057 error_return:
1058 if (dynbuf != NULL)
1059 free (dynbuf);
1060 return false;
1061}
1062
1063/* Display ELF-specific fields of a symbol. */
1064
1065void
1066bfd_elf_print_symbol (abfd, filep, symbol, how)
1067 bfd *abfd;
1068 PTR filep;
1069 asymbol *symbol;
1070 bfd_print_symbol_type how;
1071{
1072 FILE *file = (FILE *) filep;
1073 switch (how)
1074 {
1075 case bfd_print_symbol_name:
1076 fprintf (file, "%s", symbol->name);
1077 break;
1078 case bfd_print_symbol_more:
1079 fprintf (file, "elf ");
60b89a18 1080 bfd_fprintf_vma (abfd, file, symbol->value);
252b5132
RH
1081 fprintf (file, " %lx", (long) symbol->flags);
1082 break;
1083 case bfd_print_symbol_all:
1084 {
4e8a9624
AM
1085 const char *section_name;
1086 const char *name = NULL;
587ff49e 1087 struct elf_backend_data *bed;
7a13edea 1088 unsigned char st_other;
dbb410c3 1089 bfd_vma val;
c044fabd 1090
252b5132 1091 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1092
1093 bed = get_elf_backend_data (abfd);
1094 if (bed->elf_backend_print_symbol_all)
c044fabd 1095 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1096
1097 if (name == NULL)
1098 {
7ee38065 1099 name = symbol->name;
60b89a18 1100 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
587ff49e
RH
1101 }
1102
252b5132
RH
1103 fprintf (file, " %s\t", section_name);
1104 /* Print the "other" value for a symbol. For common symbols,
1105 we've already printed the size; now print the alignment.
1106 For other symbols, we have no specified alignment, and
1107 we've printed the address; now print the size. */
dbb410c3
AM
1108 if (bfd_is_com_section (symbol->section))
1109 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1110 else
1111 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1112 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1113
1114 /* If we have version information, print it. */
1115 if (elf_tdata (abfd)->dynversym_section != 0
1116 && (elf_tdata (abfd)->dynverdef_section != 0
1117 || elf_tdata (abfd)->dynverref_section != 0))
1118 {
1119 unsigned int vernum;
1120 const char *version_string;
1121
1122 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1123
1124 if (vernum == 0)
1125 version_string = "";
1126 else if (vernum == 1)
1127 version_string = "Base";
1128 else if (vernum <= elf_tdata (abfd)->cverdefs)
1129 version_string =
1130 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1131 else
1132 {
1133 Elf_Internal_Verneed *t;
1134
1135 version_string = "";
1136 for (t = elf_tdata (abfd)->verref;
1137 t != NULL;
1138 t = t->vn_nextref)
1139 {
1140 Elf_Internal_Vernaux *a;
1141
1142 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1143 {
1144 if (a->vna_other == vernum)
1145 {
1146 version_string = a->vna_nodename;
1147 break;
1148 }
1149 }
1150 }
1151 }
1152
1153 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1154 fprintf (file, " %-11s", version_string);
1155 else
1156 {
1157 int i;
1158
1159 fprintf (file, " (%s)", version_string);
1160 for (i = 10 - strlen (version_string); i > 0; --i)
1161 putc (' ', file);
1162 }
1163 }
1164
1165 /* If the st_other field is not zero, print it. */
7a13edea 1166 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 1167
7a13edea
NC
1168 switch (st_other)
1169 {
1170 case 0: break;
1171 case STV_INTERNAL: fprintf (file, " .internal"); break;
1172 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1173 case STV_PROTECTED: fprintf (file, " .protected"); break;
1174 default:
1175 /* Some other non-defined flags are also present, so print
1176 everything hex. */
1177 fprintf (file, " 0x%02x", (unsigned int) st_other);
1178 }
252b5132 1179
587ff49e 1180 fprintf (file, " %s", name);
252b5132
RH
1181 }
1182 break;
1183 }
1184}
1185\f
1186/* Create an entry in an ELF linker hash table. */
1187
1188struct bfd_hash_entry *
1189_bfd_elf_link_hash_newfunc (entry, table, string)
1190 struct bfd_hash_entry *entry;
1191 struct bfd_hash_table *table;
1192 const char *string;
1193{
252b5132
RH
1194 /* Allocate the structure if it has not already been allocated by a
1195 subclass. */
51b64d56
AM
1196 if (entry == NULL)
1197 {
1198 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1199 if (entry == NULL)
1200 return entry;
1201 }
252b5132
RH
1202
1203 /* Call the allocation method of the superclass. */
51b64d56
AM
1204 entry = _bfd_link_hash_newfunc (entry, table, string);
1205 if (entry != NULL)
252b5132 1206 {
51b64d56
AM
1207 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1208 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1209
252b5132
RH
1210 /* Set local fields. */
1211 ret->indx = -1;
1212 ret->size = 0;
1213 ret->dynindx = -1;
1214 ret->dynstr_index = 0;
1215 ret->weakdef = NULL;
51b64d56
AM
1216 ret->got.refcount = htab->init_refcount;
1217 ret->plt.refcount = htab->init_refcount;
1218 ret->linker_section_pointer = NULL;
252b5132
RH
1219 ret->verinfo.verdef = NULL;
1220 ret->vtable_entries_used = NULL;
1221 ret->vtable_entries_size = 0;
1222 ret->vtable_parent = NULL;
1223 ret->type = STT_NOTYPE;
1224 ret->other = 0;
1225 /* Assume that we have been called by a non-ELF symbol reader.
1226 This flag is then reset by the code which reads an ELF input
1227 file. This ensures that a symbol created by a non-ELF symbol
1228 reader will have the flag set correctly. */
1229 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1230 }
1231
51b64d56 1232 return entry;
252b5132
RH
1233}
1234
2920b85c 1235/* Copy data from an indirect symbol to its direct symbol, hiding the
0a991dfe 1236 old indirect symbol. Also used for copying flags to a weakdef. */
2920b85c 1237
c61b8717
RH
1238void
1239_bfd_elf_link_hash_copy_indirect (dir, ind)
2920b85c
RH
1240 struct elf_link_hash_entry *dir, *ind;
1241{
3c3e9281
AM
1242 bfd_signed_vma tmp;
1243
2920b85c
RH
1244 /* Copy down any references that we may have already seen to the
1245 symbol which just became indirect. */
1246
1247 dir->elf_link_hash_flags |=
1248 (ind->elf_link_hash_flags
1249 & (ELF_LINK_HASH_REF_DYNAMIC
1250 | ELF_LINK_HASH_REF_REGULAR
1251 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1252 | ELF_LINK_NON_GOT_REF));
1253
1e370bd2 1254 if (ind->root.type != bfd_link_hash_indirect)
0a991dfe
AM
1255 return;
1256
51b64d56 1257 /* Copy over the global and procedure linkage table refcount entries.
2920b85c 1258 These may have been already set up by a check_relocs routine. */
3c3e9281
AM
1259 tmp = dir->got.refcount;
1260 if (tmp <= 0)
2920b85c 1261 {
51b64d56 1262 dir->got.refcount = ind->got.refcount;
3c3e9281 1263 ind->got.refcount = tmp;
2920b85c 1264 }
3c3e9281
AM
1265 else
1266 BFD_ASSERT (ind->got.refcount <= 0);
2920b85c 1267
3c3e9281
AM
1268 tmp = dir->plt.refcount;
1269 if (tmp <= 0)
2920b85c 1270 {
51b64d56 1271 dir->plt.refcount = ind->plt.refcount;
3c3e9281 1272 ind->plt.refcount = tmp;
2920b85c 1273 }
3c3e9281
AM
1274 else
1275 BFD_ASSERT (ind->plt.refcount <= 0);
2920b85c
RH
1276
1277 if (dir->dynindx == -1)
1278 {
1279 dir->dynindx = ind->dynindx;
1280 dir->dynstr_index = ind->dynstr_index;
1281 ind->dynindx = -1;
1282 ind->dynstr_index = 0;
1283 }
3c3e9281
AM
1284 else
1285 BFD_ASSERT (ind->dynindx == -1);
2920b85c
RH
1286}
1287
c61b8717 1288void
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{
4053 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4054 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4055 int index;
4056 Elf_Internal_Shdr *hdr;
9ad5cbcf 4057 int maxindex = elf_numsections (abfd);
252b5132 4058
9ad5cbcf
AM
4059 if (elf_section_data (asect) != NULL
4060 && elf_section_data (asect)->this_idx != 0)
4061 return elf_section_data (asect)->this_idx;
4062
4063 if (bfd_is_abs_section (asect))
4064 return SHN_ABS;
4065 if (bfd_is_com_section (asect))
4066 return SHN_COMMON;
4067 if (bfd_is_und_section (asect))
4068 return SHN_UNDEF;
4069
4070 for (index = 1; index < maxindex; index++)
252b5132
RH
4071 {
4072 hdr = i_shdrp[index];
9ad5cbcf 4073 if (hdr != NULL && hdr->bfd_section == asect)
252b5132
RH
4074 return index;
4075 }
4076
4077 if (bed->elf_backend_section_from_bfd_section)
4078 {
4079 for (index = 0; index < maxindex; index++)
4080 {
4081 int retval;
4082
4083 hdr = i_shdrp[index];
9ad5cbcf
AM
4084 if (hdr == NULL)
4085 continue;
4086
252b5132
RH
4087 retval = index;
4088 if ((*bed->elf_backend_section_from_bfd_section)
4089 (abfd, hdr, asect, &retval))
4090 return retval;
4091 }
4092 }
4093
252b5132
RH
4094 bfd_set_error (bfd_error_nonrepresentable_section);
4095
9ad5cbcf 4096 return SHN_BAD;
252b5132
RH
4097}
4098
4099/* Given a BFD symbol, return the index in the ELF symbol table, or -1
4100 on error. */
4101
4102int
4103_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4104 bfd *abfd;
4105 asymbol **asym_ptr_ptr;
4106{
4107 asymbol *asym_ptr = *asym_ptr_ptr;
4108 int idx;
4109 flagword flags = asym_ptr->flags;
4110
4111 /* When gas creates relocations against local labels, it creates its
4112 own symbol for the section, but does put the symbol into the
4113 symbol chain, so udata is 0. When the linker is generating
4114 relocatable output, this section symbol may be for one of the
4115 input sections rather than the output section. */
4116 if (asym_ptr->udata.i == 0
4117 && (flags & BSF_SECTION_SYM)
4118 && asym_ptr->section)
4119 {
4120 int indx;
4121
4122 if (asym_ptr->section->output_section != NULL)
4123 indx = asym_ptr->section->output_section->index;
4124 else
4125 indx = asym_ptr->section->index;
4e89ac30
L
4126 if (indx < elf_num_section_syms (abfd)
4127 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
4128 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4129 }
4130
4131 idx = asym_ptr->udata.i;
4132
4133 if (idx == 0)
4134 {
4135 /* This case can occur when using --strip-symbol on a symbol
4136 which is used in a relocation entry. */
4137 (*_bfd_error_handler)
4138 (_("%s: symbol `%s' required but not present"),
8f615d07 4139 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
252b5132
RH
4140 bfd_set_error (bfd_error_no_symbols);
4141 return -1;
4142 }
4143
4144#if DEBUG & 4
4145 {
4146 fprintf (stderr,
4147 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
4148 (long) asym_ptr, asym_ptr->name, idx, flags,
4149 elf_symbol_flags (flags));
4150 fflush (stderr);
4151 }
4152#endif
4153
4154 return idx;
4155}
4156
4157/* Copy private BFD data. This copies any program header information. */
4158
4159static boolean
4160copy_private_bfd_data (ibfd, obfd)
4161 bfd *ibfd;
4162 bfd *obfd;
4163{
bc67d8a6
NC
4164 Elf_Internal_Ehdr * iehdr;
4165 struct elf_segment_map * map;
4166 struct elf_segment_map * map_first;
4167 struct elf_segment_map ** pointer_to_map;
4168 Elf_Internal_Phdr * segment;
4169 asection * section;
4170 unsigned int i;
4171 unsigned int num_segments;
4172 boolean phdr_included = false;
4173 bfd_vma maxpagesize;
4174 struct elf_segment_map * phdr_adjust_seg = NULL;
4175 unsigned int phdr_adjust_num = 0;
4176
c044fabd 4177 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
252b5132
RH
4178 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4179 return true;
4180
4181 if (elf_tdata (ibfd)->phdr == NULL)
4182 return true;
4183
4184 iehdr = elf_elfheader (ibfd);
4185
bc67d8a6 4186 map_first = NULL;
c044fabd 4187 pointer_to_map = &map_first;
252b5132
RH
4188
4189 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
4190 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4191
4192 /* Returns the end address of the segment + 1. */
4193#define SEGMENT_END(segment, start) \
4194 (start + (segment->p_memsz > segment->p_filesz \
4195 ? segment->p_memsz : segment->p_filesz))
4196
4197 /* Returns true if the given section is contained within
4198 the given segment. VMA addresses are compared. */
4199#define IS_CONTAINED_BY_VMA(section, segment) \
4200 (section->vma >= segment->p_vaddr \
4201 && (section->vma + section->_raw_size) \
4202 <= (SEGMENT_END (segment, segment->p_vaddr)))
c044fabd 4203
bc67d8a6
NC
4204 /* Returns true if the given section is contained within
4205 the given segment. LMA addresses are compared. */
4206#define IS_CONTAINED_BY_LMA(section, segment, base) \
4207 (section->lma >= base \
4208 && (section->lma + section->_raw_size) \
4209 <= SEGMENT_END (segment, base))
252b5132 4210
c044fabd 4211 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
bc67d8a6
NC
4212#define IS_COREFILE_NOTE(p, s) \
4213 (p->p_type == PT_NOTE \
4214 && bfd_get_format (ibfd) == bfd_core \
4215 && s->vma == 0 && s->lma == 0 \
4216 && (bfd_vma) s->filepos >= p->p_offset \
4217 && (bfd_vma) s->filepos + s->_raw_size \
252b5132
RH
4218 <= p->p_offset + p->p_filesz)
4219
4220 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4221 linker, which generates a PT_INTERP section with p_vaddr and
4222 p_memsz set to 0. */
bc67d8a6
NC
4223#define IS_SOLARIS_PT_INTERP(p, s) \
4224 ( p->p_vaddr == 0 \
4225 && p->p_filesz > 0 \
4226 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4227 && s->_raw_size > 0 \
4228 && (bfd_vma) s->filepos >= p->p_offset \
4229 && ((bfd_vma) s->filepos + s->_raw_size \
c0f7859b 4230 <= p->p_offset + p->p_filesz))
5c440b1e 4231
bc67d8a6
NC
4232 /* Decide if the given section should be included in the given segment.
4233 A section will be included if:
f5ffc919
NC
4234 1. It is within the address space of the segment -- we use the LMA
4235 if that is set for the segment and the VMA otherwise,
bc67d8a6
NC
4236 2. It is an allocated segment,
4237 3. There is an output section associated with it,
4238 4. The section has not already been allocated to a previous segment. */
f5ffc919
NC
4239#define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4240 (((((segment->p_paddr \
4241 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4242 : IS_CONTAINED_BY_VMA (section, segment)) \
4243 || IS_SOLARIS_PT_INTERP (segment, section)) \
4244 && (section->flags & SEC_ALLOC) != 0) \
4245 || IS_COREFILE_NOTE (segment, section)) \
4246 && section->output_section != NULL \
bc67d8a6
NC
4247 && section->segment_mark == false)
4248
4249 /* Returns true iff seg1 starts after the end of seg2. */
4250#define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4251 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4252
4253 /* Returns true iff seg1 and seg2 overlap. */
4254#define SEGMENT_OVERLAPS(seg1, seg2) \
4255 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4256
4257 /* Initialise the segment mark field. */
4258 for (section = ibfd->sections; section != NULL; section = section->next)
4259 section->segment_mark = false;
4260
252b5132 4261 /* Scan through the segments specified in the program header
bc67d8a6 4262 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 4263 in the loadable segments. These can be created by weird
bc67d8a6
NC
4264 parameters to objcopy. */
4265 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4266 i < num_segments;
c044fabd 4267 i++, segment++)
252b5132 4268 {
252b5132 4269 unsigned int j;
c044fabd 4270 Elf_Internal_Phdr *segment2;
252b5132 4271
bc67d8a6
NC
4272 if (segment->p_type != PT_LOAD)
4273 continue;
c044fabd 4274
bc67d8a6 4275 /* Determine if this segment overlaps any previous segments. */
c044fabd 4276 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
bc67d8a6
NC
4277 {
4278 bfd_signed_vma extra_length;
c044fabd 4279
bc67d8a6
NC
4280 if (segment2->p_type != PT_LOAD
4281 || ! SEGMENT_OVERLAPS (segment, segment2))
4282 continue;
c044fabd 4283
bc67d8a6
NC
4284 /* Merge the two segments together. */
4285 if (segment2->p_vaddr < segment->p_vaddr)
4286 {
c044fabd
KH
4287 /* Extend SEGMENT2 to include SEGMENT and then delete
4288 SEGMENT. */
bc67d8a6
NC
4289 extra_length =
4290 SEGMENT_END (segment, segment->p_vaddr)
4291 - SEGMENT_END (segment2, segment2->p_vaddr);
c044fabd 4292
bc67d8a6
NC
4293 if (extra_length > 0)
4294 {
4295 segment2->p_memsz += extra_length;
4296 segment2->p_filesz += extra_length;
4297 }
c044fabd 4298
bc67d8a6 4299 segment->p_type = PT_NULL;
c044fabd 4300
bc67d8a6
NC
4301 /* Since we have deleted P we must restart the outer loop. */
4302 i = 0;
4303 segment = elf_tdata (ibfd)->phdr;
4304 break;
4305 }
4306 else
4307 {
c044fabd
KH
4308 /* Extend SEGMENT to include SEGMENT2 and then delete
4309 SEGMENT2. */
bc67d8a6
NC
4310 extra_length =
4311 SEGMENT_END (segment2, segment2->p_vaddr)
4312 - SEGMENT_END (segment, segment->p_vaddr);
c044fabd 4313
bc67d8a6
NC
4314 if (extra_length > 0)
4315 {
4316 segment->p_memsz += extra_length;
4317 segment->p_filesz += extra_length;
4318 }
c044fabd 4319
bc67d8a6
NC
4320 segment2->p_type = PT_NULL;
4321 }
4322 }
4323 }
c044fabd 4324
bc67d8a6
NC
4325 /* The second scan attempts to assign sections to segments. */
4326 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4327 i < num_segments;
4328 i ++, segment ++)
4329 {
4330 unsigned int section_count;
4331 asection ** sections;
4332 asection * output_section;
4333 unsigned int isec;
4334 bfd_vma matching_lma;
4335 bfd_vma suggested_lma;
4336 unsigned int j;
dc810e39 4337 bfd_size_type amt;
bc67d8a6
NC
4338
4339 if (segment->p_type == PT_NULL)
4340 continue;
c044fabd 4341
bc67d8a6
NC
4342 /* Compute how many sections might be placed into this segment. */
4343 section_count = 0;
4344 for (section = ibfd->sections; section != NULL; section = section->next)
4345 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
c044fabd 4346 ++section_count;
252b5132
RH
4347
4348 /* Allocate a segment map big enough to contain all of the
4349 sections we have selected. */
dc810e39
AM
4350 amt = sizeof (struct elf_segment_map);
4351 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4352 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 4353 if (map == NULL)
252b5132
RH
4354 return false;
4355
4356 /* Initialise the fields of the segment map. Default to
4357 using the physical address of the segment in the input BFD. */
bc67d8a6
NC
4358 map->next = NULL;
4359 map->p_type = segment->p_type;
4360 map->p_flags = segment->p_flags;
4361 map->p_flags_valid = 1;
4362 map->p_paddr = segment->p_paddr;
4363 map->p_paddr_valid = 1;
252b5132
RH
4364
4365 /* Determine if this segment contains the ELF file header
4366 and if it contains the program headers themselves. */
bc67d8a6
NC
4367 map->includes_filehdr = (segment->p_offset == 0
4368 && segment->p_filesz >= iehdr->e_ehsize);
252b5132 4369
bc67d8a6 4370 map->includes_phdrs = 0;
252b5132 4371
bc67d8a6 4372 if (! phdr_included || segment->p_type != PT_LOAD)
252b5132 4373 {
bc67d8a6
NC
4374 map->includes_phdrs =
4375 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4376 && (segment->p_offset + segment->p_filesz
252b5132
RH
4377 >= ((bfd_vma) iehdr->e_phoff
4378 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 4379
bc67d8a6 4380 if (segment->p_type == PT_LOAD && map->includes_phdrs)
252b5132
RH
4381 phdr_included = true;
4382 }
4383
bc67d8a6 4384 if (section_count == 0)
252b5132
RH
4385 {
4386 /* Special segments, such as the PT_PHDR segment, may contain
4387 no sections, but ordinary, loadable segments should contain
4388 something. */
bc67d8a6 4389 if (segment->p_type == PT_LOAD)
252b5132
RH
4390 _bfd_error_handler
4391 (_("%s: warning: Empty loadable segment detected\n"),
8f615d07 4392 bfd_archive_filename (ibfd));
252b5132 4393
bc67d8a6 4394 map->count = 0;
c044fabd
KH
4395 *pointer_to_map = map;
4396 pointer_to_map = &map->next;
252b5132
RH
4397
4398 continue;
4399 }
4400
4401 /* Now scan the sections in the input BFD again and attempt
4402 to add their corresponding output sections to the segment map.
4403 The problem here is how to handle an output section which has
4404 been moved (ie had its LMA changed). There are four possibilities:
4405
4406 1. None of the sections have been moved.
4407 In this case we can continue to use the segment LMA from the
4408 input BFD.
4409
4410 2. All of the sections have been moved by the same amount.
4411 In this case we can change the segment's LMA to match the LMA
4412 of the first section.
4413
4414 3. Some of the sections have been moved, others have not.
4415 In this case those sections which have not been moved can be
4416 placed in the current segment which will have to have its size,
4417 and possibly its LMA changed, and a new segment or segments will
4418 have to be created to contain the other sections.
4419
4420 4. The sections have been moved, but not be the same amount.
4421 In this case we can change the segment's LMA to match the LMA
4422 of the first section and we will have to create a new segment
4423 or segments to contain the other sections.
4424
4425 In order to save time, we allocate an array to hold the section
4426 pointers that we are interested in. As these sections get assigned
4427 to a segment, they are removed from this array. */
4428
dc810e39
AM
4429 amt = (bfd_size_type) section_count * sizeof (asection *);
4430 sections = (asection **) bfd_malloc (amt);
252b5132
RH
4431 if (sections == NULL)
4432 return false;
4433
4434 /* Step One: Scan for segment vs section LMA conflicts.
4435 Also add the sections to the section array allocated above.
4436 Also add the sections to the current segment. In the common
4437 case, where the sections have not been moved, this means that
4438 we have completely filled the segment, and there is nothing
4439 more to do. */
252b5132 4440 isec = 0;
72730e0c 4441 matching_lma = 0;
252b5132
RH
4442 suggested_lma = 0;
4443
bc67d8a6
NC
4444 for (j = 0, section = ibfd->sections;
4445 section != NULL;
4446 section = section->next)
252b5132 4447 {
bc67d8a6 4448 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
c0f7859b 4449 {
bc67d8a6
NC
4450 output_section = section->output_section;
4451
4452 sections[j ++] = section;
252b5132
RH
4453
4454 /* The Solaris native linker always sets p_paddr to 0.
4455 We try to catch that case here, and set it to the
4456 correct value. */
bc67d8a6
NC
4457 if (segment->p_paddr == 0
4458 && segment->p_vaddr != 0
252b5132 4459 && isec == 0
bc67d8a6
NC
4460 && output_section->lma != 0
4461 && (output_section->vma == (segment->p_vaddr
4462 + (map->includes_filehdr
4463 ? iehdr->e_ehsize
4464 : 0)
4465 + (map->includes_phdrs
079e9a2f
AM
4466 ? (iehdr->e_phnum
4467 * iehdr->e_phentsize)
bc67d8a6
NC
4468 : 0))))
4469 map->p_paddr = segment->p_vaddr;
252b5132
RH
4470
4471 /* Match up the physical address of the segment with the
4472 LMA address of the output section. */
bc67d8a6
NC
4473 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4474 || IS_COREFILE_NOTE (segment, section))
252b5132
RH
4475 {
4476 if (matching_lma == 0)
bc67d8a6 4477 matching_lma = output_section->lma;
252b5132
RH
4478
4479 /* We assume that if the section fits within the segment
bc67d8a6 4480 then it does not overlap any other section within that
252b5132 4481 segment. */
bc67d8a6 4482 map->sections[isec ++] = output_section;
252b5132
RH
4483 }
4484 else if (suggested_lma == 0)
bc67d8a6 4485 suggested_lma = output_section->lma;
252b5132
RH
4486 }
4487 }
4488
bc67d8a6 4489 BFD_ASSERT (j == section_count);
252b5132
RH
4490
4491 /* Step Two: Adjust the physical address of the current segment,
4492 if necessary. */
bc67d8a6 4493 if (isec == section_count)
252b5132
RH
4494 {
4495 /* All of the sections fitted within the segment as currently
4496 specified. This is the default case. Add the segment to
4497 the list of built segments and carry on to process the next
4498 program header in the input BFD. */
bc67d8a6 4499 map->count = section_count;
c044fabd
KH
4500 *pointer_to_map = map;
4501 pointer_to_map = &map->next;
252b5132
RH
4502
4503 free (sections);
4504 continue;
4505 }
252b5132
RH
4506 else
4507 {
72730e0c
AM
4508 if (matching_lma != 0)
4509 {
4510 /* At least one section fits inside the current segment.
4511 Keep it, but modify its physical address to match the
4512 LMA of the first section that fitted. */
bc67d8a6 4513 map->p_paddr = matching_lma;
72730e0c
AM
4514 }
4515 else
4516 {
4517 /* None of the sections fitted inside the current segment.
4518 Change the current segment's physical address to match
4519 the LMA of the first section. */
bc67d8a6 4520 map->p_paddr = suggested_lma;
72730e0c
AM
4521 }
4522
bc67d8a6
NC
4523 /* Offset the segment physical address from the lma
4524 to allow for space taken up by elf headers. */
4525 if (map->includes_filehdr)
4526 map->p_paddr -= iehdr->e_ehsize;
252b5132 4527
bc67d8a6
NC
4528 if (map->includes_phdrs)
4529 {
4530 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4531
4532 /* iehdr->e_phnum is just an estimate of the number
4533 of program headers that we will need. Make a note
4534 here of the number we used and the segment we chose
4535 to hold these headers, so that we can adjust the
4536 offset when we know the correct value. */
4537 phdr_adjust_num = iehdr->e_phnum;
4538 phdr_adjust_seg = map;
4539 }
252b5132
RH
4540 }
4541
4542 /* Step Three: Loop over the sections again, this time assigning
4543 those that fit to the current segment and remvoing them from the
4544 sections array; but making sure not to leave large gaps. Once all
4545 possible sections have been assigned to the current segment it is
4546 added to the list of built segments and if sections still remain
4547 to be assigned, a new segment is constructed before repeating
4548 the loop. */
4549 isec = 0;
4550 do
4551 {
bc67d8a6 4552 map->count = 0;
252b5132
RH
4553 suggested_lma = 0;
4554
4555 /* Fill the current segment with sections that fit. */
bc67d8a6 4556 for (j = 0; j < section_count; j++)
252b5132 4557 {
bc67d8a6 4558 section = sections[j];
252b5132 4559
bc67d8a6 4560 if (section == NULL)
252b5132
RH
4561 continue;
4562
bc67d8a6 4563 output_section = section->output_section;
252b5132 4564
bc67d8a6 4565 BFD_ASSERT (output_section != NULL);
c044fabd 4566
bc67d8a6
NC
4567 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4568 || IS_COREFILE_NOTE (segment, section))
252b5132 4569 {
bc67d8a6 4570 if (map->count == 0)
252b5132
RH
4571 {
4572 /* If the first section in a segment does not start at
bc67d8a6
NC
4573 the beginning of the segment, then something is
4574 wrong. */
4575 if (output_section->lma !=
4576 (map->p_paddr
4577 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4578 + (map->includes_phdrs
4579 ? iehdr->e_phnum * iehdr->e_phentsize
4580 : 0)))
252b5132
RH
4581 abort ();
4582 }
4583 else
4584 {
4585 asection * prev_sec;
252b5132 4586
bc67d8a6 4587 prev_sec = map->sections[map->count - 1];
252b5132
RH
4588
4589 /* If the gap between the end of the previous section
bc67d8a6
NC
4590 and the start of this section is more than
4591 maxpagesize then we need to start a new segment. */
079e9a2f
AM
4592 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4593 maxpagesize)
bc67d8a6 4594 < BFD_ALIGN (output_section->lma, maxpagesize))
079e9a2f
AM
4595 || ((prev_sec->lma + prev_sec->_raw_size)
4596 > output_section->lma))
252b5132
RH
4597 {
4598 if (suggested_lma == 0)
bc67d8a6 4599 suggested_lma = output_section->lma;
252b5132
RH
4600
4601 continue;
4602 }
4603 }
4604
bc67d8a6 4605 map->sections[map->count++] = output_section;
252b5132
RH
4606 ++isec;
4607 sections[j] = NULL;
bc67d8a6 4608 section->segment_mark = true;
252b5132
RH
4609 }
4610 else if (suggested_lma == 0)
bc67d8a6 4611 suggested_lma = output_section->lma;
252b5132
RH
4612 }
4613
bc67d8a6 4614 BFD_ASSERT (map->count > 0);
252b5132
RH
4615
4616 /* Add the current segment to the list of built segments. */
c044fabd
KH
4617 *pointer_to_map = map;
4618 pointer_to_map = &map->next;
252b5132 4619
bc67d8a6 4620 if (isec < section_count)
252b5132
RH
4621 {
4622 /* We still have not allocated all of the sections to
4623 segments. Create a new segment here, initialise it
4624 and carry on looping. */
dc810e39
AM
4625 amt = sizeof (struct elf_segment_map);
4626 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4627 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 4628 if (map == NULL)
252b5132
RH
4629 return false;
4630
4631 /* Initialise the fields of the segment map. Set the physical
4632 physical address to the LMA of the first section that has
4633 not yet been assigned. */
bc67d8a6
NC
4634 map->next = NULL;
4635 map->p_type = segment->p_type;
4636 map->p_flags = segment->p_flags;
4637 map->p_flags_valid = 1;
4638 map->p_paddr = suggested_lma;
4639 map->p_paddr_valid = 1;
4640 map->includes_filehdr = 0;
4641 map->includes_phdrs = 0;
252b5132
RH
4642 }
4643 }
bc67d8a6 4644 while (isec < section_count);
252b5132
RH
4645
4646 free (sections);
4647 }
4648
4649 /* The Solaris linker creates program headers in which all the
4650 p_paddr fields are zero. When we try to objcopy or strip such a
4651 file, we get confused. Check for this case, and if we find it
4652 reset the p_paddr_valid fields. */
bc67d8a6
NC
4653 for (map = map_first; map != NULL; map = map->next)
4654 if (map->p_paddr != 0)
252b5132 4655 break;
bc67d8a6 4656 if (map == NULL)
252b5132 4657 {
bc67d8a6
NC
4658 for (map = map_first; map != NULL; map = map->next)
4659 map->p_paddr_valid = 0;
252b5132
RH
4660 }
4661
bc67d8a6
NC
4662 elf_tdata (obfd)->segment_map = map_first;
4663
4664 /* If we had to estimate the number of program headers that were
9ad5cbcf 4665 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
4666 the offset if necessary. */
4667 if (phdr_adjust_seg != NULL)
4668 {
4669 unsigned int count;
c044fabd 4670
bc67d8a6 4671 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 4672 count++;
252b5132 4673
bc67d8a6
NC
4674 if (count > phdr_adjust_num)
4675 phdr_adjust_seg->p_paddr
4676 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4677 }
c044fabd 4678
252b5132 4679#if 0
c044fabd
KH
4680 /* Final Step: Sort the segments into ascending order of physical
4681 address. */
bc67d8a6 4682 if (map_first != NULL)
252b5132 4683 {
c044fabd 4684 struct elf_segment_map *prev;
252b5132 4685
bc67d8a6
NC
4686 prev = map_first;
4687 for (map = map_first->next; map != NULL; prev = map, map = map->next)
252b5132 4688 {
bc67d8a6
NC
4689 /* Yes I know - its a bubble sort.... */
4690 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
252b5132 4691 {
bc67d8a6
NC
4692 /* Swap map and map->next. */
4693 prev->next = map->next;
4694 map->next = map->next->next;
4695 prev->next->next = map;
252b5132 4696
bc67d8a6
NC
4697 /* Restart loop. */
4698 map = map_first;
252b5132
RH
4699 }
4700 }
4701 }
4702#endif
4703
bc67d8a6
NC
4704#undef SEGMENT_END
4705#undef IS_CONTAINED_BY_VMA
4706#undef IS_CONTAINED_BY_LMA
252b5132 4707#undef IS_COREFILE_NOTE
bc67d8a6
NC
4708#undef IS_SOLARIS_PT_INTERP
4709#undef INCLUDE_SECTION_IN_SEGMENT
4710#undef SEGMENT_AFTER_SEGMENT
4711#undef SEGMENT_OVERLAPS
252b5132
RH
4712 return true;
4713}
4714
4715/* Copy private section information. This copies over the entsize
4716 field, and sometimes the info field. */
4717
4718boolean
4719_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4720 bfd *ibfd;
4721 asection *isec;
4722 bfd *obfd;
4723 asection *osec;
4724{
4725 Elf_Internal_Shdr *ihdr, *ohdr;
4726
4727 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4728 || obfd->xvec->flavour != bfd_target_elf_flavour)
4729 return true;
4730
4731 /* Copy over private BFD data if it has not already been copied.
4732 This must be done here, rather than in the copy_private_bfd_data
4733 entry point, because the latter is called after the section
4734 contents have been set, which means that the program headers have
4735 already been worked out. */
4736 if (elf_tdata (obfd)->segment_map == NULL
4737 && elf_tdata (ibfd)->phdr != NULL)
4738 {
4739 asection *s;
4740
4741 /* Only set up the segments if there are no more SEC_ALLOC
4742 sections. FIXME: This won't do the right thing if objcopy is
4743 used to remove the last SEC_ALLOC section, since objcopy
4744 won't call this routine in that case. */
4745 for (s = isec->next; s != NULL; s = s->next)
4746 if ((s->flags & SEC_ALLOC) != 0)
4747 break;
4748 if (s == NULL)
4749 {
4750 if (! copy_private_bfd_data (ibfd, obfd))
4751 return false;
4752 }
4753 }
4754
4755 ihdr = &elf_section_data (isec)->this_hdr;
4756 ohdr = &elf_section_data (osec)->this_hdr;
4757
4758 ohdr->sh_entsize = ihdr->sh_entsize;
4759
4760 if (ihdr->sh_type == SHT_SYMTAB
4761 || ihdr->sh_type == SHT_DYNSYM
4762 || ihdr->sh_type == SHT_GNU_verneed
4763 || ihdr->sh_type == SHT_GNU_verdef)
4764 ohdr->sh_info = ihdr->sh_info;
4765
bf572ba0
MM
4766 elf_section_data (osec)->use_rela_p
4767 = elf_section_data (isec)->use_rela_p;
4768
252b5132
RH
4769 return true;
4770}
4771
4772/* Copy private symbol information. If this symbol is in a section
4773 which we did not map into a BFD section, try to map the section
4774 index correctly. We use special macro definitions for the mapped
4775 section indices; these definitions are interpreted by the
4776 swap_out_syms function. */
4777
9ad5cbcf
AM
4778#define MAP_ONESYMTAB (SHN_HIOS + 1)
4779#define MAP_DYNSYMTAB (SHN_HIOS + 2)
4780#define MAP_STRTAB (SHN_HIOS + 3)
4781#define MAP_SHSTRTAB (SHN_HIOS + 4)
4782#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132
RH
4783
4784boolean
4785_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4786 bfd *ibfd;
4787 asymbol *isymarg;
4788 bfd *obfd;
4789 asymbol *osymarg;
4790{
4791 elf_symbol_type *isym, *osym;
4792
4793 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4794 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4795 return true;
4796
4797 isym = elf_symbol_from (ibfd, isymarg);
4798 osym = elf_symbol_from (obfd, osymarg);
4799
4800 if (isym != NULL
4801 && osym != NULL
4802 && bfd_is_abs_section (isym->symbol.section))
4803 {
4804 unsigned int shndx;
4805
4806 shndx = isym->internal_elf_sym.st_shndx;
4807 if (shndx == elf_onesymtab (ibfd))
4808 shndx = MAP_ONESYMTAB;
4809 else if (shndx == elf_dynsymtab (ibfd))
4810 shndx = MAP_DYNSYMTAB;
4811 else if (shndx == elf_tdata (ibfd)->strtab_section)
4812 shndx = MAP_STRTAB;
4813 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4814 shndx = MAP_SHSTRTAB;
9ad5cbcf
AM
4815 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
4816 shndx = MAP_SYM_SHNDX;
252b5132
RH
4817 osym->internal_elf_sym.st_shndx = shndx;
4818 }
4819
4820 return true;
4821}
4822
4823/* Swap out the symbols. */
4824
4825static boolean
4826swap_out_syms (abfd, sttp, relocatable_p)
4827 bfd *abfd;
4828 struct bfd_strtab_hash **sttp;
4829 int relocatable_p;
4830{
079e9a2f
AM
4831 struct elf_backend_data *bed;
4832 int symcount;
4833 asymbol **syms;
4834 struct bfd_strtab_hash *stt;
4835 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 4836 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f
AM
4837 Elf_Internal_Shdr *symstrtab_hdr;
4838 char *outbound_syms;
9ad5cbcf 4839 char *outbound_shndx;
079e9a2f
AM
4840 int idx;
4841 bfd_size_type amt;
252b5132
RH
4842
4843 if (!elf_map_symbols (abfd))
4844 return false;
4845
c044fabd 4846 /* Dump out the symtabs. */
079e9a2f
AM
4847 stt = _bfd_elf_stringtab_init ();
4848 if (stt == NULL)
4849 return false;
252b5132 4850
079e9a2f
AM
4851 bed = get_elf_backend_data (abfd);
4852 symcount = bfd_get_symcount (abfd);
4853 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4854 symtab_hdr->sh_type = SHT_SYMTAB;
4855 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4856 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4857 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4858 symtab_hdr->sh_addralign = bed->s->file_align;
4859
4860 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4861 symstrtab_hdr->sh_type = SHT_STRTAB;
4862
4863 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
4864 outbound_syms = bfd_alloc (abfd, amt);
4865 if (outbound_syms == NULL)
4866 return false;
4867 symtab_hdr->contents = (PTR) outbound_syms;
252b5132 4868
9ad5cbcf
AM
4869 outbound_shndx = NULL;
4870 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
4871 if (symtab_shndx_hdr->sh_name != 0)
4872 {
4873 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
4874 outbound_shndx = bfd_alloc (abfd, amt);
4875 if (outbound_shndx == NULL)
4876 return false;
4877 memset (outbound_shndx, 0, (unsigned long) amt);
4878 symtab_shndx_hdr->contents = outbound_shndx;
4879 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
4880 symtab_shndx_hdr->sh_size = amt;
4881 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
4882 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
4883 }
4884
079e9a2f
AM
4885 /* now generate the data (for "contents") */
4886 {
4887 /* Fill in zeroth symbol and swap it out. */
4888 Elf_Internal_Sym sym;
4889 sym.st_name = 0;
4890 sym.st_value = 0;
4891 sym.st_size = 0;
4892 sym.st_info = 0;
4893 sym.st_other = 0;
4894 sym.st_shndx = SHN_UNDEF;
9ad5cbcf 4895 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 4896 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
4897 if (outbound_shndx != NULL)
4898 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 4899 }
252b5132 4900
079e9a2f
AM
4901 syms = bfd_get_outsymbols (abfd);
4902 for (idx = 0; idx < symcount; idx++)
252b5132 4903 {
252b5132 4904 Elf_Internal_Sym sym;
079e9a2f
AM
4905 bfd_vma value = syms[idx]->value;
4906 elf_symbol_type *type_ptr;
4907 flagword flags = syms[idx]->flags;
4908 int type;
252b5132 4909
079e9a2f
AM
4910 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4911 {
4912 /* Local section symbols have no name. */
4913 sym.st_name = 0;
4914 }
4915 else
4916 {
4917 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4918 syms[idx]->name,
4919 true, false);
4920 if (sym.st_name == (unsigned long) -1)
4921 return false;
4922 }
252b5132 4923
079e9a2f 4924 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 4925
079e9a2f
AM
4926 if ((flags & BSF_SECTION_SYM) == 0
4927 && bfd_is_com_section (syms[idx]->section))
4928 {
4929 /* ELF common symbols put the alignment into the `value' field,
4930 and the size into the `size' field. This is backwards from
4931 how BFD handles it, so reverse it here. */
4932 sym.st_size = value;
4933 if (type_ptr == NULL
4934 || type_ptr->internal_elf_sym.st_value == 0)
4935 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4936 else
4937 sym.st_value = type_ptr->internal_elf_sym.st_value;
4938 sym.st_shndx = _bfd_elf_section_from_bfd_section
4939 (abfd, syms[idx]->section);
4940 }
4941 else
4942 {
4943 asection *sec = syms[idx]->section;
4944 int shndx;
252b5132 4945
079e9a2f
AM
4946 if (sec->output_section)
4947 {
4948 value += sec->output_offset;
4949 sec = sec->output_section;
4950 }
4951 /* Don't add in the section vma for relocatable output. */
4952 if (! relocatable_p)
4953 value += sec->vma;
4954 sym.st_value = value;
4955 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4956
4957 if (bfd_is_abs_section (sec)
4958 && type_ptr != NULL
4959 && type_ptr->internal_elf_sym.st_shndx != 0)
4960 {
4961 /* This symbol is in a real ELF section which we did
4962 not create as a BFD section. Undo the mapping done
4963 by copy_private_symbol_data. */
4964 shndx = type_ptr->internal_elf_sym.st_shndx;
4965 switch (shndx)
4966 {
4967 case MAP_ONESYMTAB:
4968 shndx = elf_onesymtab (abfd);
4969 break;
4970 case MAP_DYNSYMTAB:
4971 shndx = elf_dynsymtab (abfd);
4972 break;
4973 case MAP_STRTAB:
4974 shndx = elf_tdata (abfd)->strtab_section;
4975 break;
4976 case MAP_SHSTRTAB:
4977 shndx = elf_tdata (abfd)->shstrtab_section;
4978 break;
9ad5cbcf
AM
4979 case MAP_SYM_SHNDX:
4980 shndx = elf_tdata (abfd)->symtab_shndx_section;
4981 break;
079e9a2f
AM
4982 default:
4983 break;
4984 }
4985 }
4986 else
4987 {
4988 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 4989
079e9a2f
AM
4990 if (shndx == -1)
4991 {
4992 asection *sec2;
4993
4994 /* Writing this would be a hell of a lot easier if
4995 we had some decent documentation on bfd, and
4996 knew what to expect of the library, and what to
4997 demand of applications. For example, it
4998 appears that `objcopy' might not set the
4999 section of a symbol to be a section that is
5000 actually in the output file. */
5001 sec2 = bfd_get_section_by_name (abfd, sec->name);
5002 BFD_ASSERT (sec2 != 0);
5003 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5004 BFD_ASSERT (shndx != -1);
5005 }
5006 }
252b5132 5007
079e9a2f
AM
5008 sym.st_shndx = shndx;
5009 }
252b5132 5010
079e9a2f
AM
5011 if ((flags & BSF_FUNCTION) != 0)
5012 type = STT_FUNC;
5013 else if ((flags & BSF_OBJECT) != 0)
5014 type = STT_OBJECT;
5015 else
5016 type = STT_NOTYPE;
252b5132 5017
079e9a2f
AM
5018 /* Processor-specific types */
5019 if (type_ptr != NULL
5020 && bed->elf_backend_get_symbol_type)
5021 type = ((*bed->elf_backend_get_symbol_type)
5022 (&type_ptr->internal_elf_sym, type));
252b5132 5023
079e9a2f
AM
5024 if (flags & BSF_SECTION_SYM)
5025 {
5026 if (flags & BSF_GLOBAL)
5027 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5028 else
5029 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5030 }
5031 else if (bfd_is_com_section (syms[idx]->section))
5032 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5033 else if (bfd_is_und_section (syms[idx]->section))
5034 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5035 ? STB_WEAK
5036 : STB_GLOBAL),
5037 type);
5038 else if (flags & BSF_FILE)
5039 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5040 else
5041 {
5042 int bind = STB_LOCAL;
252b5132 5043
079e9a2f
AM
5044 if (flags & BSF_LOCAL)
5045 bind = STB_LOCAL;
5046 else if (flags & BSF_WEAK)
5047 bind = STB_WEAK;
5048 else if (flags & BSF_GLOBAL)
5049 bind = STB_GLOBAL;
252b5132 5050
079e9a2f
AM
5051 sym.st_info = ELF_ST_INFO (bind, type);
5052 }
252b5132 5053
079e9a2f
AM
5054 if (type_ptr != NULL)
5055 sym.st_other = type_ptr->internal_elf_sym.st_other;
5056 else
5057 sym.st_other = 0;
252b5132 5058
9ad5cbcf 5059 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5060 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5061 if (outbound_shndx != NULL)
5062 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5063 }
252b5132 5064
079e9a2f
AM
5065 *sttp = stt;
5066 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5067 symstrtab_hdr->sh_type = SHT_STRTAB;
252b5132 5068
079e9a2f
AM
5069 symstrtab_hdr->sh_flags = 0;
5070 symstrtab_hdr->sh_addr = 0;
5071 symstrtab_hdr->sh_entsize = 0;
5072 symstrtab_hdr->sh_link = 0;
5073 symstrtab_hdr->sh_info = 0;
5074 symstrtab_hdr->sh_addralign = 1;
252b5132
RH
5075
5076 return true;
5077}
5078
5079/* Return the number of bytes required to hold the symtab vector.
5080
5081 Note that we base it on the count plus 1, since we will null terminate
5082 the vector allocated based on this size. However, the ELF symbol table
5083 always has a dummy entry as symbol #0, so it ends up even. */
5084
5085long
5086_bfd_elf_get_symtab_upper_bound (abfd)
5087 bfd *abfd;
5088{
5089 long symcount;
5090 long symtab_size;
5091 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5092
5093 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5094 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5095
5096 return symtab_size;
5097}
5098
5099long
5100_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5101 bfd *abfd;
5102{
5103 long symcount;
5104 long symtab_size;
5105 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5106
5107 if (elf_dynsymtab (abfd) == 0)
5108 {
5109 bfd_set_error (bfd_error_invalid_operation);
5110 return -1;
5111 }
5112
5113 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5114 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5115
5116 return symtab_size;
5117}
5118
5119long
5120_bfd_elf_get_reloc_upper_bound (abfd, asect)
7442e600 5121 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5122 sec_ptr asect;
5123{
5124 return (asect->reloc_count + 1) * sizeof (arelent *);
5125}
5126
5127/* Canonicalize the relocs. */
5128
5129long
5130_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5131 bfd *abfd;
5132 sec_ptr section;
5133 arelent **relptr;
5134 asymbol **symbols;
5135{
5136 arelent *tblptr;
5137 unsigned int i;
dbb410c3 5138 struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5139
dbb410c3 5140 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
5141 return -1;
5142
5143 tblptr = section->relocation;
5144 for (i = 0; i < section->reloc_count; i++)
5145 *relptr++ = tblptr++;
5146
5147 *relptr = NULL;
5148
5149 return section->reloc_count;
5150}
5151
5152long
5153_bfd_elf_get_symtab (abfd, alocation)
5154 bfd *abfd;
5155 asymbol **alocation;
5156{
dbb410c3
AM
5157 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5158 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
252b5132
RH
5159
5160 if (symcount >= 0)
5161 bfd_get_symcount (abfd) = symcount;
5162 return symcount;
5163}
5164
5165long
5166_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5167 bfd *abfd;
5168 asymbol **alocation;
5169{
dbb410c3
AM
5170 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5171 return bed->s->slurp_symbol_table (abfd, alocation, true);
252b5132
RH
5172}
5173
5174/* Return the size required for the dynamic reloc entries. Any
5175 section that was actually installed in the BFD, and has type
5176 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5177 considered to be a dynamic reloc section. */
5178
5179long
5180_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5181 bfd *abfd;
5182{
5183 long ret;
5184 asection *s;
5185
5186 if (elf_dynsymtab (abfd) == 0)
5187 {
5188 bfd_set_error (bfd_error_invalid_operation);
5189 return -1;
5190 }
5191
5192 ret = sizeof (arelent *);
5193 for (s = abfd->sections; s != NULL; s = s->next)
5194 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5195 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5196 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5197 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5198 * sizeof (arelent *));
5199
5200 return ret;
5201}
5202
5203/* Canonicalize the dynamic relocation entries. Note that we return
5204 the dynamic relocations as a single block, although they are
5205 actually associated with particular sections; the interface, which
5206 was designed for SunOS style shared libraries, expects that there
5207 is only one set of dynamic relocs. Any section that was actually
5208 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5209 the dynamic symbol table, is considered to be a dynamic reloc
5210 section. */
5211
5212long
5213_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5214 bfd *abfd;
5215 arelent **storage;
5216 asymbol **syms;
5217{
5218 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5219 asection *s;
5220 long ret;
5221
5222 if (elf_dynsymtab (abfd) == 0)
5223 {
5224 bfd_set_error (bfd_error_invalid_operation);
5225 return -1;
5226 }
5227
5228 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5229 ret = 0;
5230 for (s = abfd->sections; s != NULL; s = s->next)
5231 {
5232 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5233 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5234 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5235 {
5236 arelent *p;
5237 long count, i;
5238
5239 if (! (*slurp_relocs) (abfd, s, syms, true))
5240 return -1;
5241 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5242 p = s->relocation;
5243 for (i = 0; i < count; i++)
5244 *storage++ = p++;
5245 ret += count;
5246 }
5247 }
5248
5249 *storage = NULL;
5250
5251 return ret;
5252}
5253\f
5254/* Read in the version information. */
5255
5256boolean
5257_bfd_elf_slurp_version_tables (abfd)
5258 bfd *abfd;
5259{
5260 bfd_byte *contents = NULL;
dc810e39 5261 bfd_size_type amt;
252b5132
RH
5262
5263 if (elf_dynverdef (abfd) != 0)
5264 {
5265 Elf_Internal_Shdr *hdr;
5266 Elf_External_Verdef *everdef;
5267 Elf_Internal_Verdef *iverdef;
f631889e
UD
5268 Elf_Internal_Verdef *iverdefarr;
5269 Elf_Internal_Verdef iverdefmem;
252b5132 5270 unsigned int i;
062e2358 5271 unsigned int maxidx;
252b5132
RH
5272
5273 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5274
252b5132
RH
5275 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5276 if (contents == NULL)
5277 goto error_return;
5278 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5279 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5280 goto error_return;
5281
f631889e
UD
5282 /* We know the number of entries in the section but not the maximum
5283 index. Therefore we have to run through all entries and find
5284 the maximum. */
252b5132 5285 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
5286 maxidx = 0;
5287 for (i = 0; i < hdr->sh_info; ++i)
5288 {
5289 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5290
062e2358
AM
5291 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5292 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e
UD
5293
5294 everdef = ((Elf_External_Verdef *)
5295 ((bfd_byte *) everdef + iverdefmem.vd_next));
5296 }
5297
dc810e39
AM
5298 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5299 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e
UD
5300 if (elf_tdata (abfd)->verdef == NULL)
5301 goto error_return;
5302
5303 elf_tdata (abfd)->cverdefs = maxidx;
5304
5305 everdef = (Elf_External_Verdef *) contents;
5306 iverdefarr = elf_tdata (abfd)->verdef;
5307 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
5308 {
5309 Elf_External_Verdaux *everdaux;
5310 Elf_Internal_Verdaux *iverdaux;
5311 unsigned int j;
5312
f631889e
UD
5313 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5314
5315 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5316 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
252b5132
RH
5317
5318 iverdef->vd_bfd = abfd;
5319
dc810e39
AM
5320 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5321 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
252b5132
RH
5322 if (iverdef->vd_auxptr == NULL)
5323 goto error_return;
5324
5325 everdaux = ((Elf_External_Verdaux *)
5326 ((bfd_byte *) everdef + iverdef->vd_aux));
5327 iverdaux = iverdef->vd_auxptr;
5328 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5329 {
5330 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5331
5332 iverdaux->vda_nodename =
5333 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5334 iverdaux->vda_name);
5335 if (iverdaux->vda_nodename == NULL)
5336 goto error_return;
5337
5338 if (j + 1 < iverdef->vd_cnt)
5339 iverdaux->vda_nextptr = iverdaux + 1;
5340 else
5341 iverdaux->vda_nextptr = NULL;
5342
5343 everdaux = ((Elf_External_Verdaux *)
5344 ((bfd_byte *) everdaux + iverdaux->vda_next));
5345 }
5346
5347 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5348
5349 if (i + 1 < hdr->sh_info)
5350 iverdef->vd_nextdef = iverdef + 1;
5351 else
5352 iverdef->vd_nextdef = NULL;
5353
5354 everdef = ((Elf_External_Verdef *)
5355 ((bfd_byte *) everdef + iverdef->vd_next));
5356 }
5357
5358 free (contents);
5359 contents = NULL;
5360 }
5361
5362 if (elf_dynverref (abfd) != 0)
5363 {
5364 Elf_Internal_Shdr *hdr;
5365 Elf_External_Verneed *everneed;
5366 Elf_Internal_Verneed *iverneed;
5367 unsigned int i;
5368
5369 hdr = &elf_tdata (abfd)->dynverref_hdr;
5370
dc810e39 5371 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
252b5132 5372 elf_tdata (abfd)->verref =
dc810e39 5373 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
252b5132
RH
5374 if (elf_tdata (abfd)->verref == NULL)
5375 goto error_return;
5376
5377 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5378
5379 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5380 if (contents == NULL)
5381 goto error_return;
5382 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5383 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5384 goto error_return;
5385
5386 everneed = (Elf_External_Verneed *) contents;
5387 iverneed = elf_tdata (abfd)->verref;
5388 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5389 {
5390 Elf_External_Vernaux *evernaux;
5391 Elf_Internal_Vernaux *ivernaux;
5392 unsigned int j;
5393
5394 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5395
5396 iverneed->vn_bfd = abfd;
5397
5398 iverneed->vn_filename =
5399 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5400 iverneed->vn_file);
5401 if (iverneed->vn_filename == NULL)
5402 goto error_return;
5403
dc810e39
AM
5404 amt = iverneed->vn_cnt;
5405 amt *= sizeof (Elf_Internal_Vernaux);
5406 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
252b5132
RH
5407
5408 evernaux = ((Elf_External_Vernaux *)
5409 ((bfd_byte *) everneed + iverneed->vn_aux));
5410 ivernaux = iverneed->vn_auxptr;
5411 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5412 {
5413 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5414
5415 ivernaux->vna_nodename =
5416 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5417 ivernaux->vna_name);
5418 if (ivernaux->vna_nodename == NULL)
5419 goto error_return;
5420
5421 if (j + 1 < iverneed->vn_cnt)
5422 ivernaux->vna_nextptr = ivernaux + 1;
5423 else
5424 ivernaux->vna_nextptr = NULL;
5425
5426 evernaux = ((Elf_External_Vernaux *)
5427 ((bfd_byte *) evernaux + ivernaux->vna_next));
5428 }
5429
5430 if (i + 1 < hdr->sh_info)
5431 iverneed->vn_nextref = iverneed + 1;
5432 else
5433 iverneed->vn_nextref = NULL;
5434
5435 everneed = ((Elf_External_Verneed *)
5436 ((bfd_byte *) everneed + iverneed->vn_next));
5437 }
5438
5439 free (contents);
5440 contents = NULL;
5441 }
5442
5443 return true;
5444
5445 error_return:
5446 if (contents == NULL)
5447 free (contents);
5448 return false;
5449}
5450\f
5451asymbol *
5452_bfd_elf_make_empty_symbol (abfd)
5453 bfd *abfd;
5454{
5455 elf_symbol_type *newsym;
dc810e39 5456 bfd_size_type amt = sizeof (elf_symbol_type);
252b5132 5457
dc810e39 5458 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
252b5132
RH
5459 if (!newsym)
5460 return NULL;
5461 else
5462 {
5463 newsym->symbol.the_bfd = abfd;
5464 return &newsym->symbol;
5465 }
5466}
5467
5468void
5469_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
7442e600 5470 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
5471 asymbol *symbol;
5472 symbol_info *ret;
5473{
5474 bfd_symbol_info (symbol, ret);
5475}
5476
5477/* Return whether a symbol name implies a local symbol. Most targets
5478 use this function for the is_local_label_name entry point, but some
5479 override it. */
5480
5481boolean
5482_bfd_elf_is_local_label_name (abfd, name)
7442e600 5483 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5484 const char *name;
5485{
5486 /* Normal local symbols start with ``.L''. */
5487 if (name[0] == '.' && name[1] == 'L')
5488 return true;
5489
5490 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5491 DWARF debugging symbols starting with ``..''. */
5492 if (name[0] == '.' && name[1] == '.')
5493 return true;
5494
5495 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5496 emitting DWARF debugging output. I suspect this is actually a
5497 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5498 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5499 underscore to be emitted on some ELF targets). For ease of use,
5500 we treat such symbols as local. */
5501 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5502 return true;
5503
5504 return false;
5505}
5506
5507alent *
5508_bfd_elf_get_lineno (ignore_abfd, symbol)
7442e600
ILT
5509 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5510 asymbol *symbol ATTRIBUTE_UNUSED;
252b5132
RH
5511{
5512 abort ();
5513 return NULL;
5514}
5515
5516boolean
5517_bfd_elf_set_arch_mach (abfd, arch, machine)
5518 bfd *abfd;
5519 enum bfd_architecture arch;
5520 unsigned long machine;
5521{
5522 /* If this isn't the right architecture for this backend, and this
5523 isn't the generic backend, fail. */
5524 if (arch != get_elf_backend_data (abfd)->arch
5525 && arch != bfd_arch_unknown
5526 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5527 return false;
5528
5529 return bfd_default_set_arch_mach (abfd, arch, machine);
5530}
5531
d1fad7c6
NC
5532/* Find the function to a particular section and offset,
5533 for error reporting. */
252b5132 5534
d1fad7c6
NC
5535static boolean
5536elf_find_function (abfd, section, symbols, offset,
4e8a9624 5537 filename_ptr, functionname_ptr)
d1fad7c6 5538 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5539 asection *section;
5540 asymbol **symbols;
5541 bfd_vma offset;
4e8a9624
AM
5542 const char **filename_ptr;
5543 const char **functionname_ptr;
252b5132 5544{
252b5132
RH
5545 const char *filename;
5546 asymbol *func;
5547 bfd_vma low_func;
5548 asymbol **p;
5549
252b5132
RH
5550 filename = NULL;
5551 func = NULL;
5552 low_func = 0;
5553
5554 for (p = symbols; *p != NULL; p++)
5555 {
5556 elf_symbol_type *q;
5557
5558 q = (elf_symbol_type *) *p;
5559
5560 if (bfd_get_section (&q->symbol) != section)
5561 continue;
5562
5563 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5564 {
5565 default:
5566 break;
5567 case STT_FILE:
5568 filename = bfd_asymbol_name (&q->symbol);
5569 break;
5570 case STT_NOTYPE:
5571 case STT_FUNC:
5572 if (q->symbol.section == section
5573 && q->symbol.value >= low_func
5574 && q->symbol.value <= offset)
5575 {
5576 func = (asymbol *) q;
5577 low_func = q->symbol.value;
5578 }
5579 break;
5580 }
5581 }
5582
5583 if (func == NULL)
5584 return false;
5585
d1fad7c6
NC
5586 if (filename_ptr)
5587 *filename_ptr = filename;
5588 if (functionname_ptr)
5589 *functionname_ptr = bfd_asymbol_name (func);
5590
5591 return true;
5592}
5593
5594/* Find the nearest line to a particular section and offset,
5595 for error reporting. */
5596
5597boolean
5598_bfd_elf_find_nearest_line (abfd, section, symbols, offset,
4e8a9624 5599 filename_ptr, functionname_ptr, line_ptr)
d1fad7c6
NC
5600 bfd *abfd;
5601 asection *section;
5602 asymbol **symbols;
5603 bfd_vma offset;
4e8a9624
AM
5604 const char **filename_ptr;
5605 const char **functionname_ptr;
d1fad7c6
NC
5606 unsigned int *line_ptr;
5607{
5608 boolean found;
5609
5610 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
5611 filename_ptr, functionname_ptr,
5612 line_ptr))
d1fad7c6
NC
5613 {
5614 if (!*functionname_ptr)
4e8a9624
AM
5615 elf_find_function (abfd, section, symbols, offset,
5616 *filename_ptr ? NULL : filename_ptr,
5617 functionname_ptr);
5618
d1fad7c6
NC
5619 return true;
5620 }
5621
5622 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
5623 filename_ptr, functionname_ptr,
5624 line_ptr, 0,
5625 &elf_tdata (abfd)->dwarf2_find_line_info))
d1fad7c6
NC
5626 {
5627 if (!*functionname_ptr)
4e8a9624
AM
5628 elf_find_function (abfd, section, symbols, offset,
5629 *filename_ptr ? NULL : filename_ptr,
5630 functionname_ptr);
5631
d1fad7c6
NC
5632 return true;
5633 }
5634
5635 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
5636 &found, filename_ptr,
5637 functionname_ptr, line_ptr,
5638 &elf_tdata (abfd)->line_info))
d1fad7c6
NC
5639 return false;
5640 if (found)
5641 return true;
5642
5643 if (symbols == NULL)
5644 return false;
5645
5646 if (! elf_find_function (abfd, section, symbols, offset,
4e8a9624 5647 filename_ptr, functionname_ptr))
d1fad7c6
NC
5648 return false;
5649
252b5132
RH
5650 *line_ptr = 0;
5651 return true;
5652}
5653
5654int
5655_bfd_elf_sizeof_headers (abfd, reloc)
5656 bfd *abfd;
5657 boolean reloc;
5658{
5659 int ret;
5660
5661 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5662 if (! reloc)
5663 ret += get_program_header_size (abfd);
5664 return ret;
5665}
5666
5667boolean
5668_bfd_elf_set_section_contents (abfd, section, location, offset, count)
5669 bfd *abfd;
5670 sec_ptr section;
5671 PTR location;
5672 file_ptr offset;
5673 bfd_size_type count;
5674{
5675 Elf_Internal_Shdr *hdr;
dc810e39 5676 bfd_signed_vma pos;
252b5132
RH
5677
5678 if (! abfd->output_has_begun
5679 && ! _bfd_elf_compute_section_file_positions
5680 (abfd, (struct bfd_link_info *) NULL))
5681 return false;
5682
5683 hdr = &elf_section_data (section)->this_hdr;
dc810e39
AM
5684 pos = hdr->sh_offset + offset;
5685 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5686 || bfd_bwrite (location, count, abfd) != count)
252b5132
RH
5687 return false;
5688
5689 return true;
5690}
5691
5692void
5693_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
7442e600
ILT
5694 bfd *abfd ATTRIBUTE_UNUSED;
5695 arelent *cache_ptr ATTRIBUTE_UNUSED;
5696 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
252b5132
RH
5697{
5698 abort ();
5699}
5700
5701#if 0
5702void
5703_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5704 bfd *abfd;
5705 arelent *cache_ptr;
5706 Elf_Internal_Rel *dst;
5707{
5708 abort ();
5709}
5710#endif
5711
5712/* Try to convert a non-ELF reloc into an ELF one. */
5713
5714boolean
5715_bfd_elf_validate_reloc (abfd, areloc)
5716 bfd *abfd;
5717 arelent *areloc;
5718{
c044fabd 5719 /* Check whether we really have an ELF howto. */
252b5132
RH
5720
5721 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5722 {
5723 bfd_reloc_code_real_type code;
5724 reloc_howto_type *howto;
5725
5726 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 5727 equivalent ELF reloc. */
252b5132
RH
5728
5729 if (areloc->howto->pc_relative)
5730 {
5731 switch (areloc->howto->bitsize)
5732 {
5733 case 8:
5734 code = BFD_RELOC_8_PCREL;
5735 break;
5736 case 12:
5737 code = BFD_RELOC_12_PCREL;
5738 break;
5739 case 16:
5740 code = BFD_RELOC_16_PCREL;
5741 break;
5742 case 24:
5743 code = BFD_RELOC_24_PCREL;
5744 break;
5745 case 32:
5746 code = BFD_RELOC_32_PCREL;
5747 break;
5748 case 64:
5749 code = BFD_RELOC_64_PCREL;
5750 break;
5751 default:
5752 goto fail;
5753 }
5754
5755 howto = bfd_reloc_type_lookup (abfd, code);
5756
5757 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5758 {
5759 if (howto->pcrel_offset)
5760 areloc->addend += areloc->address;
5761 else
5762 areloc->addend -= areloc->address; /* addend is unsigned!! */
5763 }
5764 }
5765 else
5766 {
5767 switch (areloc->howto->bitsize)
5768 {
5769 case 8:
5770 code = BFD_RELOC_8;
5771 break;
5772 case 14:
5773 code = BFD_RELOC_14;
5774 break;
5775 case 16:
5776 code = BFD_RELOC_16;
5777 break;
5778 case 26:
5779 code = BFD_RELOC_26;
5780 break;
5781 case 32:
5782 code = BFD_RELOC_32;
5783 break;
5784 case 64:
5785 code = BFD_RELOC_64;
5786 break;
5787 default:
5788 goto fail;
5789 }
5790
5791 howto = bfd_reloc_type_lookup (abfd, code);
5792 }
5793
5794 if (howto)
5795 areloc->howto = howto;
5796 else
5797 goto fail;
5798 }
5799
5800 return true;
5801
5802 fail:
5803 (*_bfd_error_handler)
5804 (_("%s: unsupported relocation type %s"),
8f615d07 5805 bfd_archive_filename (abfd), areloc->howto->name);
252b5132
RH
5806 bfd_set_error (bfd_error_bad_value);
5807 return false;
5808}
5809
5810boolean
5811_bfd_elf_close_and_cleanup (abfd)
5812 bfd *abfd;
5813{
5814 if (bfd_get_format (abfd) == bfd_object)
5815 {
5816 if (elf_shstrtab (abfd) != NULL)
2b0f7ef9 5817 _bfd_elf_strtab_free (elf_shstrtab (abfd));
252b5132
RH
5818 }
5819
5820 return _bfd_generic_close_and_cleanup (abfd);
5821}
5822
5823/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5824 in the relocation's offset. Thus we cannot allow any sort of sanity
5825 range-checking to interfere. There is nothing else to do in processing
5826 this reloc. */
5827
5828bfd_reloc_status_type
5829_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
7442e600
ILT
5830 bfd *abfd ATTRIBUTE_UNUSED;
5831 arelent *re ATTRIBUTE_UNUSED;
5832 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5833 PTR data ATTRIBUTE_UNUSED;
5834 asection *is ATTRIBUTE_UNUSED;
5835 bfd *obfd ATTRIBUTE_UNUSED;
5836 char **errmsg ATTRIBUTE_UNUSED;
252b5132
RH
5837{
5838 return bfd_reloc_ok;
5839}
252b5132
RH
5840\f
5841/* Elf core file support. Much of this only works on native
5842 toolchains, since we rely on knowing the
5843 machine-dependent procfs structure in order to pick
c044fabd 5844 out details about the corefile. */
252b5132
RH
5845
5846#ifdef HAVE_SYS_PROCFS_H
5847# include <sys/procfs.h>
5848#endif
5849
c044fabd 5850/* FIXME: this is kinda wrong, but it's what gdb wants. */
252b5132
RH
5851
5852static int
5853elfcore_make_pid (abfd)
c044fabd 5854 bfd *abfd;
252b5132
RH
5855{
5856 return ((elf_tdata (abfd)->core_lwpid << 16)
5857 + (elf_tdata (abfd)->core_pid));
5858}
5859
252b5132
RH
5860/* If there isn't a section called NAME, make one, using
5861 data from SECT. Note, this function will generate a
5862 reference to NAME, so you shouldn't deallocate or
c044fabd 5863 overwrite it. */
252b5132
RH
5864
5865static boolean
5866elfcore_maybe_make_sect (abfd, name, sect)
c044fabd
KH
5867 bfd *abfd;
5868 char *name;
5869 asection *sect;
252b5132 5870{
c044fabd 5871 asection *sect2;
252b5132
RH
5872
5873 if (bfd_get_section_by_name (abfd, name) != NULL)
5874 return true;
5875
5876 sect2 = bfd_make_section (abfd, name);
5877 if (sect2 == NULL)
5878 return false;
5879
5880 sect2->_raw_size = sect->_raw_size;
5881 sect2->filepos = sect->filepos;
5882 sect2->flags = sect->flags;
5883 sect2->alignment_power = sect->alignment_power;
5884 return true;
5885}
5886
bb0082d6
AM
5887/* Create a pseudosection containing SIZE bytes at FILEPOS. This
5888 actually creates up to two pseudosections:
5889 - For the single-threaded case, a section named NAME, unless
5890 such a section already exists.
5891 - For the multi-threaded case, a section named "NAME/PID", where
5892 PID is elfcore_make_pid (abfd).
5893 Both pseudosections have identical contents. */
5894boolean
5895_bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5896 bfd *abfd;
5897 char *name;
dc810e39
AM
5898 size_t size;
5899 ufile_ptr filepos;
bb0082d6
AM
5900{
5901 char buf[100];
5902 char *threaded_name;
5903 asection *sect;
5904
5905 /* Build the section name. */
5906
5907 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
dc810e39 5908 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
bb0082d6
AM
5909 if (threaded_name == NULL)
5910 return false;
5911 strcpy (threaded_name, buf);
5912
5913 sect = bfd_make_section (abfd, threaded_name);
5914 if (sect == NULL)
5915 return false;
5916 sect->_raw_size = size;
5917 sect->filepos = filepos;
5918 sect->flags = SEC_HAS_CONTENTS;
5919 sect->alignment_power = 2;
5920
936e320b 5921 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
5922}
5923
252b5132 5924/* prstatus_t exists on:
4a938328 5925 solaris 2.5+
252b5132
RH
5926 linux 2.[01] + glibc
5927 unixware 4.2
5928*/
5929
5930#if defined (HAVE_PRSTATUS_T)
a7b97311
AM
5931static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5932
252b5132
RH
5933static boolean
5934elfcore_grok_prstatus (abfd, note)
c044fabd
KH
5935 bfd *abfd;
5936 Elf_Internal_Note *note;
252b5132 5937{
dc810e39 5938 size_t raw_size;
7ee38065 5939 int offset;
252b5132 5940
4a938328
MS
5941 if (note->descsz == sizeof (prstatus_t))
5942 {
5943 prstatus_t prstat;
252b5132 5944
e0ebfc61 5945 raw_size = sizeof (prstat.pr_reg);
7ee38065 5946 offset = offsetof (prstatus_t, pr_reg);
4a938328 5947 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 5948
fa49d224
NC
5949 /* Do not overwrite the core signal if it
5950 has already been set by another thread. */
5951 if (elf_tdata (abfd)->core_signal == 0)
5952 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328 5953 elf_tdata (abfd)->core_pid = prstat.pr_pid;
252b5132 5954
4a938328
MS
5955 /* pr_who exists on:
5956 solaris 2.5+
5957 unixware 4.2
5958 pr_who doesn't exist on:
5959 linux 2.[01]
5960 */
252b5132 5961#if defined (HAVE_PRSTATUS_T_PR_WHO)
4a938328 5962 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
252b5132 5963#endif
4a938328 5964 }
7ee38065 5965#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
5966 else if (note->descsz == sizeof (prstatus32_t))
5967 {
5968 /* 64-bit host, 32-bit corefile */
5969 prstatus32_t prstat;
5970
e0ebfc61 5971 raw_size = sizeof (prstat.pr_reg);
7ee38065 5972 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
5973 memcpy (&prstat, note->descdata, sizeof (prstat));
5974
fa49d224
NC
5975 /* Do not overwrite the core signal if it
5976 has already been set by another thread. */
5977 if (elf_tdata (abfd)->core_signal == 0)
5978 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328
MS
5979 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5980
5981 /* pr_who exists on:
5982 solaris 2.5+
5983 unixware 4.2
5984 pr_who doesn't exist on:
5985 linux 2.[01]
5986 */
7ee38065 5987#if defined (HAVE_PRSTATUS32_T_PR_WHO)
4a938328
MS
5988 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5989#endif
5990 }
7ee38065 5991#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
5992 else
5993 {
5994 /* Fail - we don't know how to handle any other
5995 note size (ie. data object type). */
5996 return true;
5997 }
252b5132 5998
bb0082d6 5999 /* Make a ".reg/999" section and a ".reg" section. */
936e320b
AM
6000 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6001 raw_size, note->descpos + offset);
252b5132
RH
6002}
6003#endif /* defined (HAVE_PRSTATUS_T) */
6004
bb0082d6 6005/* Create a pseudosection containing the exact contents of NOTE. */
252b5132 6006static boolean
ff08c6bb 6007elfcore_make_note_pseudosection (abfd, name, note)
c044fabd 6008 bfd *abfd;
ff08c6bb 6009 char *name;
c044fabd 6010 Elf_Internal_Note *note;
252b5132 6011{
936e320b
AM
6012 return _bfd_elfcore_make_pseudosection (abfd, name,
6013 note->descsz, note->descpos);
252b5132
RH
6014}
6015
ff08c6bb
JB
6016/* There isn't a consistent prfpregset_t across platforms,
6017 but it doesn't matter, because we don't have to pick this
c044fabd
KH
6018 data structure apart. */
6019
ff08c6bb
JB
6020static boolean
6021elfcore_grok_prfpreg (abfd, note)
c044fabd
KH
6022 bfd *abfd;
6023 Elf_Internal_Note *note;
ff08c6bb
JB
6024{
6025 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6026}
6027
ff08c6bb
JB
6028/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6029 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6030 literally. */
c044fabd 6031
ff08c6bb
JB
6032static boolean
6033elfcore_grok_prxfpreg (abfd, note)
c044fabd
KH
6034 bfd *abfd;
6035 Elf_Internal_Note *note;
ff08c6bb
JB
6036{
6037 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6038}
6039
252b5132 6040#if defined (HAVE_PRPSINFO_T)
4a938328 6041typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 6042#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6043typedef prpsinfo32_t elfcore_psinfo32_t;
6044#endif
252b5132
RH
6045#endif
6046
6047#if defined (HAVE_PSINFO_T)
4a938328 6048typedef psinfo_t elfcore_psinfo_t;
7ee38065 6049#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6050typedef psinfo32_t elfcore_psinfo32_t;
6051#endif
252b5132
RH
6052#endif
6053
252b5132
RH
6054/* return a malloc'ed copy of a string at START which is at
6055 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 6056 the copy will always have a terminating '\0'. */
252b5132 6057
936e320b 6058char *
bb0082d6 6059_bfd_elfcore_strndup (abfd, start, max)
c044fabd
KH
6060 bfd *abfd;
6061 char *start;
dc810e39 6062 size_t max;
252b5132 6063{
dc810e39 6064 char *dups;
c044fabd 6065 char *end = memchr (start, '\0', max);
dc810e39 6066 size_t len;
252b5132
RH
6067
6068 if (end == NULL)
6069 len = max;
6070 else
6071 len = end - start;
6072
dc810e39
AM
6073 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6074 if (dups == NULL)
252b5132
RH
6075 return NULL;
6076
dc810e39
AM
6077 memcpy (dups, start, len);
6078 dups[len] = '\0';
252b5132 6079
dc810e39 6080 return dups;
252b5132
RH
6081}
6082
bb0082d6 6083#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
a7b97311 6084static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
bb0082d6 6085
252b5132
RH
6086static boolean
6087elfcore_grok_psinfo (abfd, note)
c044fabd
KH
6088 bfd *abfd;
6089 Elf_Internal_Note *note;
252b5132 6090{
4a938328
MS
6091 if (note->descsz == sizeof (elfcore_psinfo_t))
6092 {
6093 elfcore_psinfo_t psinfo;
252b5132 6094
7ee38065 6095 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6096
4a938328 6097 elf_tdata (abfd)->core_program
936e320b
AM
6098 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6099 sizeof (psinfo.pr_fname));
252b5132 6100
4a938328 6101 elf_tdata (abfd)->core_command
936e320b
AM
6102 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6103 sizeof (psinfo.pr_psargs));
4a938328 6104 }
7ee38065 6105#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
6106 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6107 {
6108 /* 64-bit host, 32-bit corefile */
6109 elfcore_psinfo32_t psinfo;
6110
7ee38065 6111 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6112
4a938328 6113 elf_tdata (abfd)->core_program
936e320b
AM
6114 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6115 sizeof (psinfo.pr_fname));
4a938328
MS
6116
6117 elf_tdata (abfd)->core_command
936e320b
AM
6118 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6119 sizeof (psinfo.pr_psargs));
4a938328
MS
6120 }
6121#endif
6122
6123 else
6124 {
6125 /* Fail - we don't know how to handle any other
6126 note size (ie. data object type). */
6127 return true;
6128 }
252b5132
RH
6129
6130 /* Note that for some reason, a spurious space is tacked
6131 onto the end of the args in some (at least one anyway)
c044fabd 6132 implementations, so strip it off if it exists. */
252b5132
RH
6133
6134 {
c044fabd 6135 char *command = elf_tdata (abfd)->core_command;
252b5132
RH
6136 int n = strlen (command);
6137
6138 if (0 < n && command[n - 1] == ' ')
6139 command[n - 1] = '\0';
6140 }
6141
6142 return true;
6143}
6144#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6145
252b5132
RH
6146#if defined (HAVE_PSTATUS_T)
6147static boolean
6148elfcore_grok_pstatus (abfd, note)
c044fabd
KH
6149 bfd *abfd;
6150 Elf_Internal_Note *note;
252b5132 6151{
f572a39d
AM
6152 if (note->descsz == sizeof (pstatus_t)
6153#if defined (HAVE_PXSTATUS_T)
6154 || note->descsz == sizeof (pxstatus_t)
6155#endif
6156 )
4a938328
MS
6157 {
6158 pstatus_t pstat;
252b5132 6159
4a938328 6160 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6161
4a938328
MS
6162 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6163 }
7ee38065 6164#if defined (HAVE_PSTATUS32_T)
4a938328
MS
6165 else if (note->descsz == sizeof (pstatus32_t))
6166 {
6167 /* 64-bit host, 32-bit corefile */
6168 pstatus32_t pstat;
252b5132 6169
4a938328 6170 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6171
4a938328
MS
6172 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6173 }
6174#endif
252b5132
RH
6175 /* Could grab some more details from the "representative"
6176 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 6177 NT_LWPSTATUS note, presumably. */
252b5132
RH
6178
6179 return true;
6180}
6181#endif /* defined (HAVE_PSTATUS_T) */
6182
252b5132
RH
6183#if defined (HAVE_LWPSTATUS_T)
6184static boolean
6185elfcore_grok_lwpstatus (abfd, note)
c044fabd
KH
6186 bfd *abfd;
6187 Elf_Internal_Note *note;
252b5132
RH
6188{
6189 lwpstatus_t lwpstat;
6190 char buf[100];
c044fabd
KH
6191 char *name;
6192 asection *sect;
252b5132 6193
f572a39d
AM
6194 if (note->descsz != sizeof (lwpstat)
6195#if defined (HAVE_LWPXSTATUS_T)
6196 && note->descsz != sizeof (lwpxstatus_t)
6197#endif
6198 )
252b5132
RH
6199 return true;
6200
6201 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6202
6203 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6204 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6205
c044fabd 6206 /* Make a ".reg/999" section. */
252b5132
RH
6207
6208 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
dc810e39 6209 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
252b5132
RH
6210 if (name == NULL)
6211 return false;
6212 strcpy (name, buf);
6213
6214 sect = bfd_make_section (abfd, name);
6215 if (sect == NULL)
6216 return false;
6217
6218#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6219 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6220 sect->filepos = note->descpos
6221 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6222#endif
6223
6224#if defined (HAVE_LWPSTATUS_T_PR_REG)
6225 sect->_raw_size = sizeof (lwpstat.pr_reg);
6226 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6227#endif
6228
6229 sect->flags = SEC_HAS_CONTENTS;
6230 sect->alignment_power = 2;
6231
6232 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6233 return false;
6234
6235 /* Make a ".reg2/999" section */
6236
6237 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
dc810e39 6238 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
252b5132
RH
6239 if (name == NULL)
6240 return false;
6241 strcpy (name, buf);
6242
6243 sect = bfd_make_section (abfd, name);
6244 if (sect == NULL)
6245 return false;
6246
6247#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6248 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6249 sect->filepos = note->descpos
6250 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6251#endif
6252
6253#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6254 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6255 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6256#endif
6257
6258 sect->flags = SEC_HAS_CONTENTS;
6259 sect->alignment_power = 2;
6260
936e320b 6261 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
6262}
6263#endif /* defined (HAVE_LWPSTATUS_T) */
6264
16e9c715
NC
6265#if defined (HAVE_WIN32_PSTATUS_T)
6266static boolean
6267elfcore_grok_win32pstatus (abfd, note)
c044fabd
KH
6268 bfd *abfd;
6269 Elf_Internal_Note *note;
16e9c715
NC
6270{
6271 char buf[30];
c044fabd
KH
6272 char *name;
6273 asection *sect;
16e9c715
NC
6274 win32_pstatus_t pstatus;
6275
6276 if (note->descsz < sizeof (pstatus))
6277 return true;
6278
c044fabd
KH
6279 memcpy (&pstatus, note->descdata, note->descsz);
6280
6281 switch (pstatus.data_type)
16e9c715
NC
6282 {
6283 case NOTE_INFO_PROCESS:
6284 /* FIXME: need to add ->core_command. */
6285 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6286 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
c044fabd 6287 break;
16e9c715
NC
6288
6289 case NOTE_INFO_THREAD:
6290 /* Make a ".reg/999" section. */
6291 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
c044fabd 6292
dc810e39 6293 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
16e9c715 6294 if (name == NULL)
c044fabd
KH
6295 return false;
6296
16e9c715
NC
6297 strcpy (name, buf);
6298
6299 sect = bfd_make_section (abfd, name);
6300 if (sect == NULL)
c044fabd
KH
6301 return false;
6302
16e9c715 6303 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
079e9a2f
AM
6304 sect->filepos = (note->descpos
6305 + offsetof (struct win32_pstatus,
6306 data.thread_info.thread_context));
16e9c715
NC
6307 sect->flags = SEC_HAS_CONTENTS;
6308 sect->alignment_power = 2;
6309
6310 if (pstatus.data.thread_info.is_active_thread)
6311 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6312 return false;
6313 break;
6314
6315 case NOTE_INFO_MODULE:
6316 /* Make a ".module/xxxxxxxx" section. */
c044fabd
KH
6317 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6318
dc810e39 6319 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
16e9c715
NC
6320 if (name == NULL)
6321 return false;
c044fabd 6322
16e9c715 6323 strcpy (name, buf);
252b5132 6324
16e9c715 6325 sect = bfd_make_section (abfd, name);
c044fabd 6326
16e9c715
NC
6327 if (sect == NULL)
6328 return false;
c044fabd 6329
16e9c715
NC
6330 sect->_raw_size = note->descsz;
6331 sect->filepos = note->descpos;
6332 sect->flags = SEC_HAS_CONTENTS;
6333 sect->alignment_power = 2;
6334 break;
6335
6336 default:
6337 return true;
6338 }
6339
6340 return true;
6341}
6342#endif /* HAVE_WIN32_PSTATUS_T */
252b5132
RH
6343
6344static boolean
6345elfcore_grok_note (abfd, note)
c044fabd
KH
6346 bfd *abfd;
6347 Elf_Internal_Note *note;
252b5132 6348{
bb0082d6
AM
6349 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6350
252b5132
RH
6351 switch (note->type)
6352 {
6353 default:
6354 return true;
6355
252b5132 6356 case NT_PRSTATUS:
bb0082d6
AM
6357 if (bed->elf_backend_grok_prstatus)
6358 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6359 return true;
6360#if defined (HAVE_PRSTATUS_T)
252b5132 6361 return elfcore_grok_prstatus (abfd, note);
bb0082d6
AM
6362#else
6363 return true;
252b5132
RH
6364#endif
6365
6366#if defined (HAVE_PSTATUS_T)
6367 case NT_PSTATUS:
6368 return elfcore_grok_pstatus (abfd, note);
6369#endif
6370
6371#if defined (HAVE_LWPSTATUS_T)
6372 case NT_LWPSTATUS:
6373 return elfcore_grok_lwpstatus (abfd, note);
6374#endif
6375
6376 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6377 return elfcore_grok_prfpreg (abfd, note);
6378
16e9c715 6379#if defined (HAVE_WIN32_PSTATUS_T)
c044fabd 6380 case NT_WIN32PSTATUS:
16e9c715
NC
6381 return elfcore_grok_win32pstatus (abfd, note);
6382#endif
6383
c044fabd 6384 case NT_PRXFPREG: /* Linux SSE extension */
ff08c6bb
JB
6385 if (note->namesz == 5
6386 && ! strcmp (note->namedata, "LINUX"))
6387 return elfcore_grok_prxfpreg (abfd, note);
6388 else
6389 return true;
6390
252b5132
RH
6391 case NT_PRPSINFO:
6392 case NT_PSINFO:
bb0082d6
AM
6393 if (bed->elf_backend_grok_psinfo)
6394 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6395 return true;
6396#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 6397 return elfcore_grok_psinfo (abfd, note);
bb0082d6
AM
6398#else
6399 return true;
252b5132
RH
6400#endif
6401 }
6402}
6403
50b2bdb7
AM
6404static boolean
6405elfcore_netbsd_get_lwpid (note, lwpidp)
6406 Elf_Internal_Note *note;
6407 int *lwpidp;
6408{
6409 char *cp;
6410
6411 cp = strchr (note->namedata, '@');
6412 if (cp != NULL)
6413 {
6414 *lwpidp = atoi(cp);
6415 return true;
6416 }
6417 return false;
6418}
6419
6420static boolean
6421elfcore_grok_netbsd_procinfo (abfd, note)
6422 bfd *abfd;
6423 Elf_Internal_Note *note;
6424{
6425
6426 /* Signal number at offset 0x08. */
6427 elf_tdata (abfd)->core_signal
6428 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6429
6430 /* Process ID at offset 0x50. */
6431 elf_tdata (abfd)->core_pid
6432 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6433
6434 /* Command name at 0x7c (max 32 bytes, including nul). */
6435 elf_tdata (abfd)->core_command
6436 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6437
6438 return true;
6439}
6440
6441static boolean
6442elfcore_grok_netbsd_note (abfd, note)
6443 bfd *abfd;
6444 Elf_Internal_Note *note;
6445{
6446 int lwp;
6447
6448 if (elfcore_netbsd_get_lwpid (note, &lwp))
6449 elf_tdata (abfd)->core_lwpid = lwp;
6450
b4db1224 6451 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
6452 {
6453 /* NetBSD-specific core "procinfo". Note that we expect to
6454 find this note before any of the others, which is fine,
6455 since the kernel writes this note out first when it
6456 creates a core file. */
6457
6458 return elfcore_grok_netbsd_procinfo (abfd, note);
6459 }
6460
b4db1224
JT
6461 /* As of Jan 2002 there are no other machine-independent notes
6462 defined for NetBSD core files. If the note type is less
6463 than the start of the machine-dependent note types, we don't
6464 understand it. */
50b2bdb7 6465
b4db1224 6466 if (note->type < NT_NETBSDCORE_FIRSTMACH)
50b2bdb7
AM
6467 return true;
6468
6469
6470 switch (bfd_get_arch (abfd))
6471 {
6472 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6473 PT_GETFPREGS == mach+2. */
6474
6475 case bfd_arch_alpha:
6476 case bfd_arch_sparc:
6477 switch (note->type)
6478 {
b4db1224 6479 case NT_NETBSDCORE_FIRSTMACH+0:
50b2bdb7
AM
6480 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6481
b4db1224 6482 case NT_NETBSDCORE_FIRSTMACH+2:
50b2bdb7
AM
6483 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6484
6485 default:
6486 return true;
6487 }
6488
6489 /* On all other arch's, PT_GETREGS == mach+1 and
6490 PT_GETFPREGS == mach+3. */
6491
6492 default:
6493 switch (note->type)
6494 {
b4db1224 6495 case NT_NETBSDCORE_FIRSTMACH+1:
50b2bdb7
AM
6496 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6497
b4db1224 6498 case NT_NETBSDCORE_FIRSTMACH+3:
50b2bdb7
AM
6499 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6500
6501 default:
6502 return true;
6503 }
6504 }
6505 /* NOTREACHED */
6506}
6507
7c76fa91
MS
6508/* Function: elfcore_write_note
6509
6510 Inputs:
6511 buffer to hold note
6512 name of note
6513 type of note
6514 data for note
6515 size of data for note
6516
6517 Return:
6518 End of buffer containing note. */
6519
6520char *
6521elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6522 bfd *abfd;
6523 char *buf;
6524 int *bufsiz;
6525 char *name;
6526 int type;
6527 void *input;
6528 int size;
6529{
6530 Elf_External_Note *xnp;
6531 int namesz = strlen (name);
6532 int newspace = BFD_ALIGN (sizeof (Elf_External_Note) + size + namesz - 1, 4);
6533 char *p, *dest;
6534
6535 p = realloc (buf, *bufsiz + newspace);
6536 dest = p + *bufsiz;
6537 *bufsiz += newspace;
6538 xnp = (Elf_External_Note *) dest;
6539 H_PUT_32 (abfd, namesz, xnp->namesz);
6540 H_PUT_32 (abfd, size, xnp->descsz);
6541 H_PUT_32 (abfd, type, xnp->type);
6542 strcpy (xnp->name, name);
6543 memcpy (xnp->name + BFD_ALIGN (namesz, 4), input, size);
6544 return p;
6545}
6546
6547#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6548char *
6549elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6550 bfd *abfd;
6551 char *buf;
6552 int *bufsiz;
6553 char *fname;
6554 char *psargs;
6555{
6556 int note_type;
6557 char *note_name = "CORE";
6558
6559#if defined (HAVE_PSINFO_T)
6560 psinfo_t data;
6561 note_type = NT_PSINFO;
6562#else
6563 prpsinfo_t data;
6564 note_type = NT_PRPSINFO;
6565#endif
6566
6567 memset (&data, 0, sizeof (data));
6568 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
6569 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
6570 return elfcore_write_note (abfd, buf, bufsiz,
6571 note_name, note_type, &data, sizeof (data));
6572}
6573#endif /* PSINFO_T or PRPSINFO_T */
6574
6575#if defined (HAVE_PRSTATUS_T)
6576char *
6577elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6578 bfd *abfd;
6579 char *buf;
6580 int *bufsiz;
b87011e9 6581 long pid;
7c76fa91
MS
6582 int cursig;
6583 void *gregs;
6584{
6585 prstatus_t prstat;
6586 char *note_name = "CORE";
6587
6588 memset (&prstat, 0, sizeof (prstat));
6589 prstat.pr_pid = pid;
6590 prstat.pr_cursig = cursig;
6591 memcpy (prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
6592 return elfcore_write_note (abfd, buf, bufsiz,
6593 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
6594}
6595#endif /* HAVE_PRSTATUS_T */
6596
6597#if defined (HAVE_PSTATUS_T)
6598char *
6599elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6600 bfd *abfd;
6601 char *buf;
6602 int *bufsiz;
b87011e9 6603 long pid;
7c76fa91
MS
6604 int cursig;
6605 void *gregs;
6606{
6607 pstatus_t pstat;
6608 char *note_name = "CORE";
6609
6610 memset (&pstat, 0, sizeof (prstat));
6611 pstat.pr_pid = pid;
6612 memcpy (pstat.pr_reg, gregs, sizeof (pstat.pr_reg));
6613 return elfcore_write_note (abfd, buf, bufsiz,
6614 note_name, NT_PSTATUS, &pstat, sizeof (pstat));
6615}
6616#endif /* HAVE_PSTATUS_T */
6617
6618char *
6619elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
6620 bfd *abfd;
6621 char *buf;
6622 int *bufsiz;
6623 void *fpregs;
6624 int size;
6625{
6626 char *note_name = "CORE";
6627 return elfcore_write_note (abfd, buf, bufsiz,
6628 note_name, NT_FPREGSET, fpregs, size);
6629}
6630
6631char *
6632elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
6633 bfd *abfd;
6634 char *buf;
6635 int *bufsiz;
6636 void *xfpregs;
6637 int size;
6638{
6639 char *note_name = "LINUX";
6640 return elfcore_write_note (abfd, buf, bufsiz,
6641 note_name, NT_PRXFPREG, xfpregs, size);
6642}
6643
252b5132
RH
6644static boolean
6645elfcore_read_notes (abfd, offset, size)
c044fabd 6646 bfd *abfd;
dc810e39
AM
6647 file_ptr offset;
6648 bfd_size_type size;
252b5132 6649{
c044fabd
KH
6650 char *buf;
6651 char *p;
252b5132
RH
6652
6653 if (size <= 0)
6654 return true;
6655
dc810e39 6656 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
252b5132
RH
6657 return false;
6658
dc810e39 6659 buf = bfd_malloc (size);
252b5132
RH
6660 if (buf == NULL)
6661 return false;
6662
dc810e39 6663 if (bfd_bread (buf, size, abfd) != size)
252b5132
RH
6664 {
6665 error:
6666 free (buf);
6667 return false;
6668 }
6669
6670 p = buf;
6671 while (p < buf + size)
6672 {
c044fabd
KH
6673 /* FIXME: bad alignment assumption. */
6674 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
6675 Elf_Internal_Note in;
6676
dc810e39 6677 in.type = H_GET_32 (abfd, xnp->type);
252b5132 6678
dc810e39 6679 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132
RH
6680 in.namedata = xnp->name;
6681
dc810e39 6682 in.descsz = H_GET_32 (abfd, xnp->descsz);
252b5132
RH
6683 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
6684 in.descpos = offset + (in.descdata - buf);
6685
50b2bdb7
AM
6686 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
6687 {
6688 if (! elfcore_grok_netbsd_note (abfd, &in))
6689 goto error;
6690 }
6691 else
6692 {
6693 if (! elfcore_grok_note (abfd, &in))
6694 goto error;
6695 }
252b5132
RH
6696
6697 p = in.descdata + BFD_ALIGN (in.descsz, 4);
6698 }
6699
6700 free (buf);
6701 return true;
6702}
98d8431c
JB
6703\f
6704/* Providing external access to the ELF program header table. */
6705
6706/* Return an upper bound on the number of bytes required to store a
6707 copy of ABFD's program header table entries. Return -1 if an error
6708 occurs; bfd_get_error will return an appropriate code. */
c044fabd 6709
98d8431c
JB
6710long
6711bfd_get_elf_phdr_upper_bound (abfd)
6712 bfd *abfd;
6713{
6714 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6715 {
6716 bfd_set_error (bfd_error_wrong_format);
6717 return -1;
6718 }
6719
936e320b 6720 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
6721}
6722
98d8431c
JB
6723/* Copy ABFD's program header table entries to *PHDRS. The entries
6724 will be stored as an array of Elf_Internal_Phdr structures, as
6725 defined in include/elf/internal.h. To find out how large the
6726 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6727
6728 Return the number of program header table entries read, or -1 if an
6729 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 6730
98d8431c
JB
6731int
6732bfd_get_elf_phdrs (abfd, phdrs)
6733 bfd *abfd;
6734 void *phdrs;
6735{
6736 int num_phdrs;
6737
6738 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6739 {
6740 bfd_set_error (bfd_error_wrong_format);
6741 return -1;
6742 }
6743
6744 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 6745 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
6746 num_phdrs * sizeof (Elf_Internal_Phdr));
6747
6748 return num_phdrs;
6749}
ae4221d7
L
6750
6751void
4e771d61 6752_bfd_elf_sprintf_vma (abfd, buf, value)
cc55aec9 6753 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
6754 char *buf;
6755 bfd_vma value;
6756{
d3b05f8d 6757#ifdef BFD64
ae4221d7
L
6758 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6759
6760 i_ehdrp = elf_elfheader (abfd);
6761 if (i_ehdrp == NULL)
6762 sprintf_vma (buf, value);
6763 else
6764 {
6765 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 6766 {
ae4221d7 6767#if BFD_HOST_64BIT_LONG
cc55aec9 6768 sprintf (buf, "%016lx", value);
ae4221d7 6769#else
cc55aec9
AM
6770 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6771 _bfd_int64_low (value));
ae4221d7 6772#endif
cc55aec9 6773 }
ae4221d7
L
6774 else
6775 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6776 }
d3b05f8d
L
6777#else
6778 sprintf_vma (buf, value);
6779#endif
ae4221d7
L
6780}
6781
6782void
4e771d61 6783_bfd_elf_fprintf_vma (abfd, stream, value)
cc55aec9 6784 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
6785 PTR stream;
6786 bfd_vma value;
6787{
d3b05f8d 6788#ifdef BFD64
ae4221d7
L
6789 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6790
6791 i_ehdrp = elf_elfheader (abfd);
6792 if (i_ehdrp == NULL)
6793 fprintf_vma ((FILE *) stream, value);
6794 else
6795 {
6796 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 6797 {
ae4221d7 6798#if BFD_HOST_64BIT_LONG
cc55aec9 6799 fprintf ((FILE *) stream, "%016lx", value);
ae4221d7 6800#else
cc55aec9
AM
6801 fprintf ((FILE *) stream, "%08lx%08lx",
6802 _bfd_int64_high (value), _bfd_int64_low (value));
ae4221d7 6803#endif
cc55aec9 6804 }
ae4221d7
L
6805 else
6806 fprintf ((FILE *) stream, "%08lx",
6807 (unsigned long) (value & 0xffffffff));
6808 }
d3b05f8d
L
6809#else
6810 fprintf_vma ((FILE *) stream, value);
6811#endif
ae4221d7 6812}
db6751f2
JJ
6813
6814enum elf_reloc_type_class
f51e552e
AM
6815_bfd_elf_reloc_type_class (rela)
6816 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
db6751f2
JJ
6817{
6818 return reloc_class_normal;
6819}
f8df10f4
JJ
6820
6821/* For RELA architectures, return what the relocation value for
6822 relocation against a local symbol. */
6823
6824bfd_vma
6825_bfd_elf_rela_local_sym (abfd, sym, sec, rel)
6826 bfd *abfd;
6827 Elf_Internal_Sym *sym;
6828 asection *sec;
6829 Elf_Internal_Rela *rel;
6830{
6831 bfd_vma relocation;
6832
6833 relocation = (sec->output_section->vma
6834 + sec->output_offset
6835 + sym->st_value);
6836 if ((sec->flags & SEC_MERGE)
c629eae0 6837 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
65765700 6838 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
f8df10f4
JJ
6839 {
6840 asection *msec;
6841
6842 msec = sec;
6843 rel->r_addend =
6844 _bfd_merged_section_offset (abfd, &msec,
65765700 6845 elf_section_data (sec)->sec_info,
f8df10f4
JJ
6846 sym->st_value + rel->r_addend,
6847 (bfd_vma) 0)
6848 - relocation;
6849 rel->r_addend += msec->output_section->vma + msec->output_offset;
6850 }
6851 return relocation;
6852}
c629eae0
JJ
6853
6854bfd_vma
6855_bfd_elf_rel_local_sym (abfd, sym, psec, addend)
6856 bfd *abfd;
6857 Elf_Internal_Sym *sym;
6858 asection **psec;
6859 bfd_vma addend;
6860{
6861 asection *sec = *psec;
6862
65765700 6863 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
c629eae0
JJ
6864 return sym->st_value + addend;
6865
6866 return _bfd_merged_section_offset (abfd, psec,
65765700 6867 elf_section_data (sec)->sec_info,
c629eae0
JJ
6868 sym->st_value + addend, (bfd_vma) 0);
6869}
6870
6871bfd_vma
6872_bfd_elf_section_offset (abfd, info, sec, offset)
6873 bfd *abfd;
6874 struct bfd_link_info *info;
6875 asection *sec;
6876 bfd_vma offset;
6877{
6878 struct bfd_elf_section_data *sec_data;
6879
6880 sec_data = elf_section_data (sec);
65765700
JJ
6881 switch (sec_data->sec_info_type)
6882 {
6883 case ELF_INFO_TYPE_STABS:
6884 return _bfd_stab_section_offset
6885 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
6886 offset);
6887 case ELF_INFO_TYPE_EH_FRAME:
6888 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
6889 default:
6890 return offset;
6891 }
c629eae0 6892}