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