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