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