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