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