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