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