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