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