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