]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
* elfxx-mips.c (_bfd_mips_elf_modify_segment_map): Use the
[thirdparty/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
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
252b5132 11
5e8d7549
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
5e8d7549
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
252b5132 20
661a3fd4 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 2445 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
2446 {
2447 this_hdr->sh_flags |= SHF_TLS;
2448 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2449 {
2450 struct bfd_link_order *o;
2451
2452 this_hdr->sh_size = 0;
2453 for (o = asect->link_order_head; o != NULL; o = o->next)
2454 if (this_hdr->sh_size < o->offset + o->size)
2455 this_hdr->sh_size = o->offset + o->size;
2456 if (this_hdr->sh_size)
2457 this_hdr->sh_type = SHT_NOBITS;
2458 }
2459 }
252b5132
RH
2460
2461 /* Check for processor-specific section types. */
e1fddb6b
AO
2462 if (bed->elf_backend_fake_sections
2463 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2464 *failedptr = true;
252b5132
RH
2465
2466 /* If the section has relocs, set up a section header for the
23bc299b
MM
2467 SHT_REL[A] section. If two relocation sections are required for
2468 this section, it is up to the processor-specific back-end to
c044fabd 2469 create the other. */
23bc299b 2470 if ((asect->flags & SEC_RELOC) != 0
c044fabd 2471 && !_bfd_elf_init_reloc_shdr (abfd,
23bc299b 2472 &elf_section_data (asect)->rel_hdr,
c044fabd 2473 asect,
23bc299b
MM
2474 elf_section_data (asect)->use_rela_p))
2475 *failedptr = true;
252b5132
RH
2476}
2477
dbb410c3
AM
2478/* Fill in the contents of a SHT_GROUP section. */
2479
1126897b
AM
2480void
2481bfd_elf_set_group_contents (abfd, sec, failedptrarg)
dbb410c3
AM
2482 bfd *abfd;
2483 asection *sec;
1126897b 2484 PTR failedptrarg;
dbb410c3
AM
2485{
2486 boolean *failedptr = (boolean *) failedptrarg;
2487 unsigned long symindx;
9dce4196 2488 asection *elt, *first;
dbb410c3
AM
2489 unsigned char *loc;
2490 struct bfd_link_order *l;
9dce4196 2491 boolean gas;
dbb410c3
AM
2492
2493 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2494 || *failedptr)
2495 return;
2496
1126897b
AM
2497 symindx = 0;
2498 if (elf_group_id (sec) != NULL)
2499 symindx = elf_group_id (sec)->udata.i;
2500
2501 if (symindx == 0)
2502 {
2503 /* If called from the assembler, swap_out_syms will have set up
2504 elf_section_syms; If called for "ld -r", use target_index. */
2505 if (elf_section_syms (abfd) != NULL)
2506 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2507 else
2508 symindx = sec->target_index;
2509 }
dbb410c3
AM
2510 elf_section_data (sec)->this_hdr.sh_info = symindx;
2511
1126897b 2512 /* The contents won't be allocated for "ld -r" or objcopy. */
9dce4196 2513 gas = true;
dbb410c3
AM
2514 if (sec->contents == NULL)
2515 {
9dce4196 2516 gas = false;
dbb410c3 2517 sec->contents = bfd_alloc (abfd, sec->_raw_size);
9dce4196
AM
2518
2519 /* Arrange for the section to be written out. */
2520 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
2521 if (sec->contents == NULL)
2522 {
2523 *failedptr = true;
2524 return;
2525 }
2526 }
2527
2528 loc = sec->contents + sec->_raw_size;
2529
9dce4196
AM
2530 /* Get the pointer to the first section in the group that gas
2531 squirreled away here. objcopy arranges for this to be set to the
2532 start of the input section group. */
2533 first = elt = elf_next_in_group (sec);
dbb410c3
AM
2534
2535 /* First element is a flag word. Rest of section is elf section
2536 indices for all the sections of the group. Write them backwards
2537 just to keep the group in the same order as given in .section
2538 directives, not that it matters. */
2539 while (elt != NULL)
2540 {
9dce4196
AM
2541 asection *s;
2542 unsigned int idx;
2543
dbb410c3 2544 loc -= 4;
9dce4196
AM
2545 s = elt;
2546 if (!gas)
2547 s = s->output_section;
2548 idx = 0;
2549 if (s != NULL)
2550 idx = elf_section_data (s)->this_idx;
2551 H_PUT_32 (abfd, idx, loc);
945906ff 2552 elt = elf_next_in_group (elt);
9dce4196
AM
2553 if (elt == first)
2554 break;
dbb410c3
AM
2555 }
2556
2557 /* If this is a relocatable link, then the above did nothing because
2558 SEC is the output section. Look through the input sections
2559 instead. */
2560 for (l = sec->link_order_head; l != NULL; l = l->next)
2561 if (l->type == bfd_indirect_link_order
945906ff 2562 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
dbb410c3
AM
2563 do
2564 {
2565 loc -= 4;
2566 H_PUT_32 (abfd,
2567 elf_section_data (elt->output_section)->this_idx, loc);
945906ff 2568 elt = elf_next_in_group (elt);
dbb410c3
AM
2569 /* During a relocatable link, the lists are circular. */
2570 }
945906ff 2571 while (elt != elf_next_in_group (l->u.indirect.section));
dbb410c3 2572
9dce4196
AM
2573 /* With ld -r, merging SHT_GROUP sections results in wasted space
2574 due to allowing for the flag word on each input. We may well
2575 duplicate entries too. */
2576 while ((loc -= 4) > sec->contents)
2577 H_PUT_32 (abfd, 0, loc);
2578
2579 if (loc != sec->contents)
2580 abort ();
dbb410c3 2581
9dce4196 2582 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
2583}
2584
252b5132
RH
2585/* Assign all ELF section numbers. The dummy first section is handled here
2586 too. The link/info pointers for the standard section types are filled
2587 in here too, while we're at it. */
2588
2589static boolean
2590assign_section_numbers (abfd)
2591 bfd *abfd;
2592{
2593 struct elf_obj_tdata *t = elf_tdata (abfd);
2594 asection *sec;
2b0f7ef9 2595 unsigned int section_number, secn;
252b5132 2596 Elf_Internal_Shdr **i_shdrp;
dc810e39 2597 bfd_size_type amt;
252b5132
RH
2598
2599 section_number = 1;
2600
2b0f7ef9
JJ
2601 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2602
252b5132
RH
2603 for (sec = abfd->sections; sec; sec = sec->next)
2604 {
2605 struct bfd_elf_section_data *d = elf_section_data (sec);
2606
9ad5cbcf
AM
2607 if (section_number == SHN_LORESERVE)
2608 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2609 d->this_idx = section_number++;
2b0f7ef9 2610 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
252b5132
RH
2611 if ((sec->flags & SEC_RELOC) == 0)
2612 d->rel_idx = 0;
2613 else
2b0f7ef9 2614 {
9ad5cbcf
AM
2615 if (section_number == SHN_LORESERVE)
2616 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2b0f7ef9
JJ
2617 d->rel_idx = section_number++;
2618 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2619 }
23bc299b
MM
2620
2621 if (d->rel_hdr2)
2b0f7ef9 2622 {
9ad5cbcf
AM
2623 if (section_number == SHN_LORESERVE)
2624 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2b0f7ef9
JJ
2625 d->rel_idx2 = section_number++;
2626 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2627 }
23bc299b
MM
2628 else
2629 d->rel_idx2 = 0;
252b5132
RH
2630 }
2631
9ad5cbcf
AM
2632 if (section_number == SHN_LORESERVE)
2633 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2634 t->shstrtab_section = section_number++;
2b0f7ef9 2635 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
252b5132 2636 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
252b5132
RH
2637
2638 if (bfd_get_symcount (abfd) > 0)
2639 {
9ad5cbcf
AM
2640 if (section_number == SHN_LORESERVE)
2641 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2642 t->symtab_section = section_number++;
2b0f7ef9 2643 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
9ad5cbcf
AM
2644 if (section_number > SHN_LORESERVE - 2)
2645 {
2646 if (section_number == SHN_LORESERVE)
2647 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2648 t->symtab_shndx_section = section_number++;
2649 t->symtab_shndx_hdr.sh_name
2650 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2651 ".symtab_shndx", false);
2652 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2653 return false;
2654 }
2655 if (section_number == SHN_LORESERVE)
2656 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132 2657 t->strtab_section = section_number++;
2b0f7ef9 2658 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
2659 }
2660
2b0f7ef9
JJ
2661 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2662 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
9ad5cbcf
AM
2663
2664 elf_numsections (abfd) = section_number;
252b5132 2665 elf_elfheader (abfd)->e_shnum = section_number;
9ad5cbcf
AM
2666 if (section_number > SHN_LORESERVE)
2667 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
2668
2669 /* Set up the list of section header pointers, in agreement with the
2670 indices. */
dc810e39
AM
2671 amt = section_number * sizeof (Elf_Internal_Shdr *);
2672 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
252b5132
RH
2673 if (i_shdrp == NULL)
2674 return false;
2675
dc810e39
AM
2676 amt = sizeof (Elf_Internal_Shdr);
2677 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
252b5132
RH
2678 if (i_shdrp[0] == NULL)
2679 {
2680 bfd_release (abfd, i_shdrp);
2681 return false;
2682 }
2683 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2684
2685 elf_elfsections (abfd) = i_shdrp;
2686
2687 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2688 if (bfd_get_symcount (abfd) > 0)
2689 {
2690 i_shdrp[t->symtab_section] = &t->symtab_hdr;
9ad5cbcf
AM
2691 if (elf_numsections (abfd) > SHN_LORESERVE)
2692 {
2693 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2694 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2695 }
252b5132
RH
2696 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2697 t->symtab_hdr.sh_link = t->strtab_section;
2698 }
2699 for (sec = abfd->sections; sec; sec = sec->next)
2700 {
2701 struct bfd_elf_section_data *d = elf_section_data (sec);
2702 asection *s;
2703 const char *name;
2704
2705 i_shdrp[d->this_idx] = &d->this_hdr;
2706 if (d->rel_idx != 0)
2707 i_shdrp[d->rel_idx] = &d->rel_hdr;
23bc299b
MM
2708 if (d->rel_idx2 != 0)
2709 i_shdrp[d->rel_idx2] = d->rel_hdr2;
252b5132
RH
2710
2711 /* Fill in the sh_link and sh_info fields while we're at it. */
2712
2713 /* sh_link of a reloc section is the section index of the symbol
2714 table. sh_info is the section index of the section to which
2715 the relocation entries apply. */
2716 if (d->rel_idx != 0)
2717 {
2718 d->rel_hdr.sh_link = t->symtab_section;
2719 d->rel_hdr.sh_info = d->this_idx;
2720 }
23bc299b
MM
2721 if (d->rel_idx2 != 0)
2722 {
2723 d->rel_hdr2->sh_link = t->symtab_section;
2724 d->rel_hdr2->sh_info = d->this_idx;
2725 }
252b5132
RH
2726
2727 switch (d->this_hdr.sh_type)
2728 {
2729 case SHT_REL:
2730 case SHT_RELA:
2731 /* A reloc section which we are treating as a normal BFD
2732 section. sh_link is the section index of the symbol
2733 table. sh_info is the section index of the section to
2734 which the relocation entries apply. We assume that an
2735 allocated reloc section uses the dynamic symbol table.
2736 FIXME: How can we be sure? */
2737 s = bfd_get_section_by_name (abfd, ".dynsym");
2738 if (s != NULL)
2739 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2740
2741 /* We look up the section the relocs apply to by name. */
2742 name = sec->name;
2743 if (d->this_hdr.sh_type == SHT_REL)
2744 name += 4;
2745 else
2746 name += 5;
2747 s = bfd_get_section_by_name (abfd, name);
2748 if (s != NULL)
2749 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2750 break;
2751
2752 case SHT_STRTAB:
2753 /* We assume that a section named .stab*str is a stabs
2754 string section. We look for a section with the same name
2755 but without the trailing ``str'', and set its sh_link
2756 field to point to this section. */
2757 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2758 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2759 {
2760 size_t len;
2761 char *alc;
2762
2763 len = strlen (sec->name);
d4c88bbb 2764 alc = (char *) bfd_malloc ((bfd_size_type) (len - 2));
252b5132
RH
2765 if (alc == NULL)
2766 return false;
d4c88bbb 2767 memcpy (alc, sec->name, len - 3);
252b5132
RH
2768 alc[len - 3] = '\0';
2769 s = bfd_get_section_by_name (abfd, alc);
2770 free (alc);
2771 if (s != NULL)
2772 {
2773 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2774
2775 /* This is a .stab section. */
0594c12d
AM
2776 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2777 elf_section_data (s)->this_hdr.sh_entsize
2778 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
2779 }
2780 }
2781 break;
2782
2783 case SHT_DYNAMIC:
2784 case SHT_DYNSYM:
2785 case SHT_GNU_verneed:
2786 case SHT_GNU_verdef:
2787 /* sh_link is the section header index of the string table
2788 used for the dynamic entries, or the symbol table, or the
2789 version strings. */
2790 s = bfd_get_section_by_name (abfd, ".dynstr");
2791 if (s != NULL)
2792 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2793 break;
2794
2795 case SHT_HASH:
2796 case SHT_GNU_versym:
2797 /* sh_link is the section header index of the symbol table
2798 this hash table or version table is for. */
2799 s = bfd_get_section_by_name (abfd, ".dynsym");
2800 if (s != NULL)
2801 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2802 break;
dbb410c3
AM
2803
2804 case SHT_GROUP:
2805 d->this_hdr.sh_link = t->symtab_section;
252b5132
RH
2806 }
2807 }
2808
2b0f7ef9 2809 for (secn = 1; secn < section_number; ++secn)
9ad5cbcf
AM
2810 if (i_shdrp[secn] == NULL)
2811 i_shdrp[secn] = i_shdrp[0];
2812 else
2813 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2814 i_shdrp[secn]->sh_name);
252b5132
RH
2815 return true;
2816}
2817
2818/* Map symbol from it's internal number to the external number, moving
2819 all local symbols to be at the head of the list. */
2820
2821static INLINE int
2822sym_is_global (abfd, sym)
2823 bfd *abfd;
2824 asymbol *sym;
2825{
2826 /* If the backend has a special mapping, use it. */
2827 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2828 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2829 (abfd, sym));
2830
2831 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2832 || bfd_is_und_section (bfd_get_section (sym))
2833 || bfd_is_com_section (bfd_get_section (sym)));
2834}
2835
2836static boolean
2837elf_map_symbols (abfd)
2838 bfd *abfd;
2839{
dc810e39 2840 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
2841 asymbol **syms = bfd_get_outsymbols (abfd);
2842 asymbol **sect_syms;
dc810e39
AM
2843 unsigned int num_locals = 0;
2844 unsigned int num_globals = 0;
2845 unsigned int num_locals2 = 0;
2846 unsigned int num_globals2 = 0;
252b5132 2847 int max_index = 0;
dc810e39 2848 unsigned int idx;
252b5132
RH
2849 asection *asect;
2850 asymbol **new_syms;
dc810e39 2851 bfd_size_type amt;
252b5132
RH
2852
2853#ifdef DEBUG
2854 fprintf (stderr, "elf_map_symbols\n");
2855 fflush (stderr);
2856#endif
2857
252b5132
RH
2858 for (asect = abfd->sections; asect; asect = asect->next)
2859 {
2860 if (max_index < asect->index)
2861 max_index = asect->index;
2862 }
2863
2864 max_index++;
dc810e39
AM
2865 amt = max_index * sizeof (asymbol *);
2866 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
252b5132
RH
2867 if (sect_syms == NULL)
2868 return false;
2869 elf_section_syms (abfd) = sect_syms;
4e89ac30 2870 elf_num_section_syms (abfd) = max_index;
252b5132 2871
079e9a2f
AM
2872 /* Init sect_syms entries for any section symbols we have already
2873 decided to output. */
252b5132
RH
2874 for (idx = 0; idx < symcount; idx++)
2875 {
dc810e39 2876 asymbol *sym = syms[idx];
c044fabd 2877
252b5132
RH
2878 if ((sym->flags & BSF_SECTION_SYM) != 0
2879 && sym->value == 0)
2880 {
2881 asection *sec;
2882
2883 sec = sym->section;
2884
2885 if (sec->owner != NULL)
2886 {
2887 if (sec->owner != abfd)
2888 {
2889 if (sec->output_offset != 0)
2890 continue;
c044fabd 2891
252b5132
RH
2892 sec = sec->output_section;
2893
079e9a2f
AM
2894 /* Empty sections in the input files may have had a
2895 section symbol created for them. (See the comment
2896 near the end of _bfd_generic_link_output_symbols in
2897 linker.c). If the linker script discards such
2898 sections then we will reach this point. Since we know
2899 that we cannot avoid this case, we detect it and skip
2900 the abort and the assignment to the sect_syms array.
2901 To reproduce this particular case try running the
2902 linker testsuite test ld-scripts/weak.exp for an ELF
2903 port that uses the generic linker. */
252b5132
RH
2904 if (sec->owner == NULL)
2905 continue;
2906
2907 BFD_ASSERT (sec->owner == abfd);
2908 }
2909 sect_syms[sec->index] = syms[idx];
2910 }
2911 }
2912 }
2913
252b5132
RH
2914 /* Classify all of the symbols. */
2915 for (idx = 0; idx < symcount; idx++)
2916 {
2917 if (!sym_is_global (abfd, syms[idx]))
2918 num_locals++;
2919 else
2920 num_globals++;
2921 }
079e9a2f
AM
2922
2923 /* We will be adding a section symbol for each BFD section. Most normal
2924 sections will already have a section symbol in outsymbols, but
2925 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2926 at least in that case. */
252b5132
RH
2927 for (asect = abfd->sections; asect; asect = asect->next)
2928 {
079e9a2f 2929 if (sect_syms[asect->index] == NULL)
252b5132 2930 {
079e9a2f 2931 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
2932 num_locals++;
2933 else
2934 num_globals++;
252b5132
RH
2935 }
2936 }
2937
2938 /* Now sort the symbols so the local symbols are first. */
dc810e39
AM
2939 amt = (num_locals + num_globals) * sizeof (asymbol *);
2940 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2941
252b5132
RH
2942 if (new_syms == NULL)
2943 return false;
2944
2945 for (idx = 0; idx < symcount; idx++)
2946 {
2947 asymbol *sym = syms[idx];
dc810e39 2948 unsigned int i;
252b5132
RH
2949
2950 if (!sym_is_global (abfd, sym))
2951 i = num_locals2++;
2952 else
2953 i = num_locals + num_globals2++;
2954 new_syms[i] = sym;
2955 sym->udata.i = i + 1;
2956 }
2957 for (asect = abfd->sections; asect; asect = asect->next)
2958 {
079e9a2f 2959 if (sect_syms[asect->index] == NULL)
252b5132 2960 {
079e9a2f 2961 asymbol *sym = asect->symbol;
dc810e39 2962 unsigned int i;
252b5132 2963
079e9a2f 2964 sect_syms[asect->index] = sym;
252b5132
RH
2965 if (!sym_is_global (abfd, sym))
2966 i = num_locals2++;
2967 else
2968 i = num_locals + num_globals2++;
2969 new_syms[i] = sym;
2970 sym->udata.i = i + 1;
2971 }
2972 }
2973
2974 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2975
2976 elf_num_locals (abfd) = num_locals;
2977 elf_num_globals (abfd) = num_globals;
2978 return true;
2979}
2980
2981/* Align to the maximum file alignment that could be required for any
2982 ELF data structure. */
2983
2984static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2985static INLINE file_ptr
2986align_file_position (off, align)
2987 file_ptr off;
2988 int align;
2989{
2990 return (off + align - 1) & ~(align - 1);
2991}
2992
2993/* Assign a file position to a section, optionally aligning to the
2994 required section alignment. */
2995
2996INLINE file_ptr
2997_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2998 Elf_Internal_Shdr *i_shdrp;
2999 file_ptr offset;
3000 boolean align;
3001{
3002 if (align)
3003 {
3004 unsigned int al;
3005
3006 al = i_shdrp->sh_addralign;
3007 if (al > 1)
3008 offset = BFD_ALIGN (offset, al);
3009 }
3010 i_shdrp->sh_offset = offset;
3011 if (i_shdrp->bfd_section != NULL)
3012 i_shdrp->bfd_section->filepos = offset;
3013 if (i_shdrp->sh_type != SHT_NOBITS)
3014 offset += i_shdrp->sh_size;
3015 return offset;
3016}
3017
3018/* Compute the file positions we are going to put the sections at, and
3019 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3020 is not NULL, this is being called by the ELF backend linker. */
3021
3022boolean
3023_bfd_elf_compute_section_file_positions (abfd, link_info)
3024 bfd *abfd;
3025 struct bfd_link_info *link_info;
3026{
3027 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3028 boolean failed;
3029 struct bfd_strtab_hash *strtab;
3030 Elf_Internal_Shdr *shstrtab_hdr;
3031
3032 if (abfd->output_has_begun)
3033 return true;
3034
3035 /* Do any elf backend specific processing first. */
3036 if (bed->elf_backend_begin_write_processing)
3037 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3038
3039 if (! prep_headers (abfd))
3040 return false;
3041
e6c51ed4
NC
3042 /* Post process the headers if necessary. */
3043 if (bed->elf_backend_post_process_headers)
3044 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3045
252b5132
RH
3046 failed = false;
3047 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3048 if (failed)
3049 return false;
3050
3051 if (!assign_section_numbers (abfd))
3052 return false;
3053
3054 /* The backend linker builds symbol table information itself. */
3055 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3056 {
3057 /* Non-zero if doing a relocatable link. */
3058 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3059
3060 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3061 return false;
3062 }
3063
1126897b 3064 if (link_info == NULL)
dbb410c3 3065 {
1126897b 3066 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3
AM
3067 if (failed)
3068 return false;
3069 }
3070
252b5132
RH
3071 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3072 /* sh_name was set in prep_headers. */
3073 shstrtab_hdr->sh_type = SHT_STRTAB;
3074 shstrtab_hdr->sh_flags = 0;
3075 shstrtab_hdr->sh_addr = 0;
2b0f7ef9 3076 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
252b5132
RH
3077 shstrtab_hdr->sh_entsize = 0;
3078 shstrtab_hdr->sh_link = 0;
3079 shstrtab_hdr->sh_info = 0;
3080 /* sh_offset is set in assign_file_positions_except_relocs. */
3081 shstrtab_hdr->sh_addralign = 1;
3082
3083 if (!assign_file_positions_except_relocs (abfd))
3084 return false;
3085
3086 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3087 {
3088 file_ptr off;
3089 Elf_Internal_Shdr *hdr;
3090
3091 off = elf_tdata (abfd)->next_file_pos;
3092
3093 hdr = &elf_tdata (abfd)->symtab_hdr;
3094 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3095
9ad5cbcf
AM
3096 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3097 if (hdr->sh_size != 0)
3098 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3099
252b5132
RH
3100 hdr = &elf_tdata (abfd)->strtab_hdr;
3101 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3102
3103 elf_tdata (abfd)->next_file_pos = off;
3104
3105 /* Now that we know where the .strtab section goes, write it
3106 out. */
3107 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3108 || ! _bfd_stringtab_emit (abfd, strtab))
3109 return false;
3110 _bfd_stringtab_free (strtab);
3111 }
3112
3113 abfd->output_has_begun = true;
3114
3115 return true;
3116}
3117
3118/* Create a mapping from a set of sections to a program segment. */
3119
3120static INLINE struct elf_segment_map *
3121make_mapping (abfd, sections, from, to, phdr)
3122 bfd *abfd;
3123 asection **sections;
3124 unsigned int from;
3125 unsigned int to;
3126 boolean phdr;
3127{
3128 struct elf_segment_map *m;
3129 unsigned int i;
3130 asection **hdrpp;
dc810e39 3131 bfd_size_type amt;
252b5132 3132
dc810e39
AM
3133 amt = sizeof (struct elf_segment_map);
3134 amt += (to - from - 1) * sizeof (asection *);
3135 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3136 if (m == NULL)
3137 return NULL;
3138 m->next = NULL;
3139 m->p_type = PT_LOAD;
3140 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3141 m->sections[i - from] = *hdrpp;
3142 m->count = to - from;
3143
3144 if (from == 0 && phdr)
3145 {
3146 /* Include the headers in the first PT_LOAD segment. */
3147 m->includes_filehdr = 1;
3148 m->includes_phdrs = 1;
3149 }
3150
3151 return m;
3152}
3153
3154/* Set up a mapping from BFD sections to program segments. */
3155
3156static boolean
3157map_sections_to_segments (abfd)
3158 bfd *abfd;
3159{
3160 asection **sections = NULL;
3161 asection *s;
3162 unsigned int i;
3163 unsigned int count;
3164 struct elf_segment_map *mfirst;
3165 struct elf_segment_map **pm;
3166 struct elf_segment_map *m;
3167 asection *last_hdr;
3168 unsigned int phdr_index;
3169 bfd_vma maxpagesize;
3170 asection **hdrpp;
3171 boolean phdr_in_segment = true;
3172 boolean writable;
13ae64f3
JJ
3173 int tls_count = 0;
3174 asection *first_tls = NULL;
65765700 3175 asection *dynsec, *eh_frame_hdr;
dc810e39 3176 bfd_size_type amt;
252b5132
RH
3177
3178 if (elf_tdata (abfd)->segment_map != NULL)
3179 return true;
3180
3181 if (bfd_count_sections (abfd) == 0)
3182 return true;
3183
3184 /* Select the allocated sections, and sort them. */
3185
dc810e39
AM
3186 amt = bfd_count_sections (abfd) * sizeof (asection *);
3187 sections = (asection **) bfd_malloc (amt);
252b5132
RH
3188 if (sections == NULL)
3189 goto error_return;
3190
3191 i = 0;
3192 for (s = abfd->sections; s != NULL; s = s->next)
3193 {
3194 if ((s->flags & SEC_ALLOC) != 0)
3195 {
3196 sections[i] = s;
3197 ++i;
3198 }
3199 }
3200 BFD_ASSERT (i <= bfd_count_sections (abfd));
3201 count = i;
3202
3203 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3204
3205 /* Build the mapping. */
3206
3207 mfirst = NULL;
3208 pm = &mfirst;
3209
3210 /* If we have a .interp section, then create a PT_PHDR segment for
3211 the program headers and a PT_INTERP segment for the .interp
3212 section. */
3213 s = bfd_get_section_by_name (abfd, ".interp");
3214 if (s != NULL && (s->flags & SEC_LOAD) != 0)
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_PHDR;
3222 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3223 m->p_flags = PF_R | PF_X;
3224 m->p_flags_valid = 1;
3225 m->includes_phdrs = 1;
3226
3227 *pm = m;
3228 pm = &m->next;
3229
dc810e39
AM
3230 amt = sizeof (struct elf_segment_map);
3231 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3232 if (m == NULL)
3233 goto error_return;
3234 m->next = NULL;
3235 m->p_type = PT_INTERP;
3236 m->count = 1;
3237 m->sections[0] = s;
3238
3239 *pm = m;
3240 pm = &m->next;
3241 }
3242
3243 /* Look through the sections. We put sections in the same program
3244 segment when the start of the second section can be placed within
3245 a few bytes of the end of the first section. */
3246 last_hdr = NULL;
3247 phdr_index = 0;
3248 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3249 writable = false;
3250 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3251 if (dynsec != NULL
3252 && (dynsec->flags & SEC_LOAD) == 0)
3253 dynsec = NULL;
3254
3255 /* Deal with -Ttext or something similar such that the first section
3256 is not adjacent to the program headers. This is an
3257 approximation, since at this point we don't know exactly how many
3258 program headers we will need. */
3259 if (count > 0)
3260 {
3261 bfd_size_type phdr_size;
3262
3263 phdr_size = elf_tdata (abfd)->program_header_size;
3264 if (phdr_size == 0)
3265 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3266 if ((abfd->flags & D_PAGED) == 0
3267 || sections[0]->lma < phdr_size
3268 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3269 phdr_in_segment = false;
3270 }
3271
3272 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3273 {
3274 asection *hdr;
3275 boolean new_segment;
3276
3277 hdr = *hdrpp;
3278
3279 /* See if this section and the last one will fit in the same
3280 segment. */
3281
3282 if (last_hdr == NULL)
3283 {
3284 /* If we don't have a segment yet, then we don't need a new
3285 one (we build the last one after this loop). */
3286 new_segment = false;
3287 }
3288 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3289 {
3290 /* If this section has a different relation between the
3291 virtual address and the load address, then we need a new
3292 segment. */
3293 new_segment = true;
3294 }
3295 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3296 < BFD_ALIGN (hdr->lma, maxpagesize))
3297 {
3298 /* If putting this section in this segment would force us to
3299 skip a page in the segment, then we need a new segment. */
3300 new_segment = true;
3301 }
3302 else if ((last_hdr->flags & SEC_LOAD) == 0
3303 && (hdr->flags & SEC_LOAD) != 0)
3304 {
3305 /* We don't want to put a loadable section after a
3306 nonloadable section in the same segment. */
3307 new_segment = true;
3308 }
3309 else if ((abfd->flags & D_PAGED) == 0)
3310 {
3311 /* If the file is not demand paged, which means that we
3312 don't require the sections to be correctly aligned in the
3313 file, then there is no other reason for a new segment. */
3314 new_segment = false;
3315 }
3316 else if (! writable
3317 && (hdr->flags & SEC_READONLY) == 0
3318 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3319 == hdr->lma))
3320 {
3321 /* We don't want to put a writable section in a read only
3322 segment, unless they are on the same page in memory
3323 anyhow. We already know that the last section does not
3324 bring us past the current section on the page, so the
3325 only case in which the new section is not on the same
3326 page as the previous section is when the previous section
3327 ends precisely on a page boundary. */
3328 new_segment = true;
3329 }
3330 else
3331 {
3332 /* Otherwise, we can use the same segment. */
3333 new_segment = false;
3334 }
3335
3336 if (! new_segment)
3337 {
3338 if ((hdr->flags & SEC_READONLY) == 0)
3339 writable = true;
3340 last_hdr = hdr;
3341 continue;
3342 }
3343
3344 /* We need a new program segment. We must create a new program
3345 header holding all the sections from phdr_index until hdr. */
3346
3347 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3348 if (m == NULL)
3349 goto error_return;
3350
3351 *pm = m;
3352 pm = &m->next;
3353
3354 if ((hdr->flags & SEC_READONLY) == 0)
3355 writable = true;
3356 else
3357 writable = false;
3358
3359 last_hdr = hdr;
3360 phdr_index = i;
3361 phdr_in_segment = false;
3362 }
3363
3364 /* Create a final PT_LOAD program segment. */
3365 if (last_hdr != NULL)
3366 {
3367 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3368 if (m == NULL)
3369 goto error_return;
3370
3371 *pm = m;
3372 pm = &m->next;
3373 }
3374
3375 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3376 if (dynsec != NULL)
3377 {
dc810e39
AM
3378 amt = sizeof (struct elf_segment_map);
3379 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3380 if (m == NULL)
3381 goto error_return;
3382 m->next = NULL;
3383 m->p_type = PT_DYNAMIC;
3384 m->count = 1;
3385 m->sections[0] = dynsec;
3386
3387 *pm = m;
3388 pm = &m->next;
3389 }
3390
3391 /* For each loadable .note section, add a PT_NOTE segment. We don't
3392 use bfd_get_section_by_name, because if we link together
3393 nonloadable .note sections and loadable .note sections, we will
3394 generate two .note sections in the output file. FIXME: Using
3395 names for section types is bogus anyhow. */
3396 for (s = abfd->sections; s != NULL; s = s->next)
3397 {
3398 if ((s->flags & SEC_LOAD) != 0
3399 && strncmp (s->name, ".note", 5) == 0)
3400 {
dc810e39
AM
3401 amt = sizeof (struct elf_segment_map);
3402 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
3403 if (m == NULL)
3404 goto error_return;
3405 m->next = NULL;
3406 m->p_type = PT_NOTE;
3407 m->count = 1;
3408 m->sections[0] = s;
3409
3410 *pm = m;
3411 pm = &m->next;
3412 }
13ae64f3
JJ
3413 if (s->flags & SEC_THREAD_LOCAL)
3414 {
3415 if (! tls_count)
3416 first_tls = s;
3417 tls_count++;
3418 }
3419 }
3420
3421 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3422 if (tls_count > 0)
3423 {
3424 int i;
3425
3426 amt = sizeof (struct elf_segment_map);
3427 amt += (tls_count - 1) * sizeof (asection *);
3428 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3429 if (m == NULL)
3430 goto error_return;
3431 m->next = NULL;
3432 m->p_type = PT_TLS;
3433 m->count = tls_count;
3434 /* Mandated PF_R. */
3435 m->p_flags = PF_R;
3436 m->p_flags_valid = 1;
3437 for (i = 0; i < tls_count; ++i)
3438 {
3439 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3440 m->sections[i] = first_tls;
3441 first_tls = first_tls->next;
3442 }
3443
3444 *pm = m;
3445 pm = &m->next;
252b5132
RH
3446 }
3447
65765700
JJ
3448 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3449 segment. */
9ad5cbcf
AM
3450 eh_frame_hdr = NULL;
3451 if (elf_tdata (abfd)->eh_frame_hdr)
3452 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
65765700
JJ
3453 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3454 {
3455 amt = sizeof (struct elf_segment_map);
3456 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3457 if (m == NULL)
3458 goto error_return;
3459 m->next = NULL;
3460 m->p_type = PT_GNU_EH_FRAME;
3461 m->count = 1;
3462 m->sections[0] = eh_frame_hdr;
3463
3464 *pm = m;
3465 pm = &m->next;
3466 }
3467
252b5132
RH
3468 free (sections);
3469 sections = NULL;
3470
3471 elf_tdata (abfd)->segment_map = mfirst;
3472 return true;
3473
3474 error_return:
3475 if (sections != NULL)
3476 free (sections);
3477 return false;
3478}
3479
3480/* Sort sections by address. */
3481
3482static int
3483elf_sort_sections (arg1, arg2)
3484 const PTR arg1;
3485 const PTR arg2;
3486{
3487 const asection *sec1 = *(const asection **) arg1;
3488 const asection *sec2 = *(const asection **) arg2;
3489
3490 /* Sort by LMA first, since this is the address used to
3491 place the section into a segment. */
3492 if (sec1->lma < sec2->lma)
3493 return -1;
3494 else if (sec1->lma > sec2->lma)
3495 return 1;
3496
3497 /* Then sort by VMA. Normally the LMA and the VMA will be
3498 the same, and this will do nothing. */
3499 if (sec1->vma < sec2->vma)
3500 return -1;
3501 else if (sec1->vma > sec2->vma)
3502 return 1;
3503
3504 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3505
3506#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3507
3508 if (TOEND (sec1))
3509 {
3510 if (TOEND (sec2))
00a7cdc5
NC
3511 {
3512 /* If the indicies are the same, do not return 0
3513 here, but continue to try the next comparison. */
3514 if (sec1->target_index - sec2->target_index != 0)
3515 return sec1->target_index - sec2->target_index;
3516 }
252b5132
RH
3517 else
3518 return 1;
3519 }
00a7cdc5 3520 else if (TOEND (sec2))
252b5132
RH
3521 return -1;
3522
3523#undef TOEND
3524
00a7cdc5
NC
3525 /* Sort by size, to put zero sized sections
3526 before others at the same address. */
252b5132
RH
3527
3528 if (sec1->_raw_size < sec2->_raw_size)
3529 return -1;
3530 if (sec1->_raw_size > sec2->_raw_size)
3531 return 1;
3532
3533 return sec1->target_index - sec2->target_index;
3534}
3535
3536/* Assign file positions to the sections based on the mapping from
3537 sections to segments. This function also sets up some fields in
3538 the file header, and writes out the program headers. */
3539
3540static boolean
3541assign_file_positions_for_segments (abfd)
3542 bfd *abfd;
3543{
3544 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3545 unsigned int count;
3546 struct elf_segment_map *m;
3547 unsigned int alloc;
3548 Elf_Internal_Phdr *phdrs;
3549 file_ptr off, voff;
3550 bfd_vma filehdr_vaddr, filehdr_paddr;
3551 bfd_vma phdrs_vaddr, phdrs_paddr;
3552 Elf_Internal_Phdr *p;
dc810e39 3553 bfd_size_type amt;
252b5132
RH
3554
3555 if (elf_tdata (abfd)->segment_map == NULL)
3556 {
3557 if (! map_sections_to_segments (abfd))
3558 return false;
3559 }
1ed89aa9
NC
3560 else
3561 {
3562 /* The placement algorithm assumes that non allocated sections are
3563 not in PT_LOAD segments. We ensure this here by removing such
3564 sections from the segment map. */
3565 for (m = elf_tdata (abfd)->segment_map;
3566 m != NULL;
3567 m = m->next)
3568 {
3569 unsigned int new_count;
3570 unsigned int i;
3571
3572 if (m->p_type != PT_LOAD)
3573 continue;
3574
3575 new_count = 0;
3576 for (i = 0; i < m->count; i ++)
3577 {
3578 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3579 {
47d9a591 3580 if (i != new_count)
1ed89aa9
NC
3581 m->sections[new_count] = m->sections[i];
3582
3583 new_count ++;
3584 }
3585 }
3586
3587 if (new_count != m->count)
3588 m->count = new_count;
3589 }
3590 }
252b5132
RH
3591
3592 if (bed->elf_backend_modify_segment_map)
3593 {
3594 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3595 return false;
3596 }
3597
3598 count = 0;
3599 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3600 ++count;
3601
3602 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3603 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3604 elf_elfheader (abfd)->e_phnum = count;
3605
3606 if (count == 0)
3607 return true;
3608
3609 /* If we already counted the number of program segments, make sure
3610 that we allocated enough space. This happens when SIZEOF_HEADERS
3611 is used in a linker script. */
3612 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3613 if (alloc != 0 && count > alloc)
3614 {
3615 ((*_bfd_error_handler)
3616 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3617 bfd_get_filename (abfd), alloc, count));
3618 bfd_set_error (bfd_error_bad_value);
3619 return false;
3620 }
3621
3622 if (alloc == 0)
3623 alloc = count;
3624
dc810e39
AM
3625 amt = alloc * sizeof (Elf_Internal_Phdr);
3626 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
252b5132
RH
3627 if (phdrs == NULL)
3628 return false;
3629
3630 off = bed->s->sizeof_ehdr;
3631 off += alloc * bed->s->sizeof_phdr;
3632
3633 filehdr_vaddr = 0;
3634 filehdr_paddr = 0;
3635 phdrs_vaddr = 0;
3636 phdrs_paddr = 0;
3637
3638 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3639 m != NULL;
3640 m = m->next, p++)
3641 {
3642 unsigned int i;
3643 asection **secpp;
3644
3645 /* If elf_segment_map is not from map_sections_to_segments, the
47d9a591 3646 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
3647 not be done to the PT_NOTE section of a corefile, which may
3648 contain several pseudo-sections artificially created by bfd.
3649 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
3650 if (m->count > 1
3651 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 3652 && m->p_type == PT_NOTE))
252b5132
RH
3653 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3654 elf_sort_sections);
3655
3656 p->p_type = m->p_type;
28a7f3e7 3657 p->p_flags = m->p_flags;
252b5132
RH
3658
3659 if (p->p_type == PT_LOAD
3660 && m->count > 0
3661 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3662 {
3663 if ((abfd->flags & D_PAGED) != 0)
3664 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3665 else
3666 {
3667 bfd_size_type align;
3668
3669 align = 0;
3670 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3671 {
3672 bfd_size_type secalign;
3673
3674 secalign = bfd_get_section_alignment (abfd, *secpp);
3675 if (secalign > align)
3676 align = secalign;
3677 }
3678
3679 off += (m->sections[0]->vma - off) % (1 << align);
3680 }
3681 }
3682
3683 if (m->count == 0)
3684 p->p_vaddr = 0;
3685 else
3686 p->p_vaddr = m->sections[0]->vma;
3687
3688 if (m->p_paddr_valid)
3689 p->p_paddr = m->p_paddr;
3690 else if (m->count == 0)
3691 p->p_paddr = 0;
3692 else
3693 p->p_paddr = m->sections[0]->lma;
3694
3695 if (p->p_type == PT_LOAD
3696 && (abfd->flags & D_PAGED) != 0)
3697 p->p_align = bed->maxpagesize;
3698 else if (m->count == 0)
3699 p->p_align = bed->s->file_align;
3700 else
3701 p->p_align = 0;
3702
3703 p->p_offset = 0;
3704 p->p_filesz = 0;
3705 p->p_memsz = 0;
3706
3707 if (m->includes_filehdr)
3708 {
3709 if (! m->p_flags_valid)
3710 p->p_flags |= PF_R;
3711 p->p_offset = 0;
3712 p->p_filesz = bed->s->sizeof_ehdr;
3713 p->p_memsz = bed->s->sizeof_ehdr;
3714 if (m->count > 0)
3715 {
3716 BFD_ASSERT (p->p_type == PT_LOAD);
3717
3718 if (p->p_vaddr < (bfd_vma) off)
3719 {
caf47ea6
AM
3720 (*_bfd_error_handler)
3721 (_("%s: Not enough room for program headers, try linking with -N"),
3722 bfd_get_filename (abfd));
252b5132
RH
3723 bfd_set_error (bfd_error_bad_value);
3724 return false;
3725 }
3726
3727 p->p_vaddr -= off;
3728 if (! m->p_paddr_valid)
3729 p->p_paddr -= off;
3730 }
3731 if (p->p_type == PT_LOAD)
3732 {
3733 filehdr_vaddr = p->p_vaddr;
3734 filehdr_paddr = p->p_paddr;
3735 }
3736 }
3737
3738 if (m->includes_phdrs)
3739 {
3740 if (! m->p_flags_valid)
3741 p->p_flags |= PF_R;
3742
3743 if (m->includes_filehdr)
3744 {
3745 if (p->p_type == PT_LOAD)
3746 {
3747 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3748 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3749 }
3750 }
3751 else
3752 {
3753 p->p_offset = bed->s->sizeof_ehdr;
3754
3755 if (m->count > 0)
3756 {
3757 BFD_ASSERT (p->p_type == PT_LOAD);
3758 p->p_vaddr -= off - p->p_offset;
3759 if (! m->p_paddr_valid)
3760 p->p_paddr -= off - p->p_offset;
3761 }
3762
3763 if (p->p_type == PT_LOAD)
3764 {
3765 phdrs_vaddr = p->p_vaddr;
3766 phdrs_paddr = p->p_paddr;
3767 }
3768 else
3769 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3770 }
3771
3772 p->p_filesz += alloc * bed->s->sizeof_phdr;
3773 p->p_memsz += alloc * bed->s->sizeof_phdr;
3774 }
3775
3776 if (p->p_type == PT_LOAD
3777 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3778 {
3779 if (! m->includes_filehdr && ! m->includes_phdrs)
3780 p->p_offset = off;
3781 else
3782 {
3783 file_ptr adjust;
3784
3785 adjust = off - (p->p_offset + p->p_filesz);
3786 p->p_filesz += adjust;
3787 p->p_memsz += adjust;
3788 }
3789 }
3790
3791 voff = off;
3792
3793 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3794 {
3795 asection *sec;
3796 flagword flags;
3797 bfd_size_type align;
3798
3799 sec = *secpp;
3800 flags = sec->flags;
3801 align = 1 << bfd_get_section_alignment (abfd, sec);
3802
3803 /* The section may have artificial alignment forced by a
3804 link script. Notice this case by the gap between the
f5ffc919
NC
3805 cumulative phdr lma and the section's lma. */
3806 if (p->p_paddr + p->p_memsz < sec->lma)
252b5132 3807 {
f5ffc919 3808 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
252b5132
RH
3809
3810 p->p_memsz += adjust;
3811 off += adjust;
3812 voff += adjust;
3813 if ((flags & SEC_LOAD) != 0)
3814 p->p_filesz += adjust;
3815 }
3816
3817 if (p->p_type == PT_LOAD)
3818 {
3819 bfd_signed_vma adjust;
3820
3821 if ((flags & SEC_LOAD) != 0)
3822 {
3823 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3824 if (adjust < 0)
3825 adjust = 0;
3826 }
3827 else if ((flags & SEC_ALLOC) != 0)
3828 {
3829 /* The section VMA must equal the file position
3830 modulo the page size. FIXME: I'm not sure if
3831 this adjustment is really necessary. We used to
3832 not have the SEC_LOAD case just above, and then
3833 this was necessary, but now I'm not sure. */
3834 if ((abfd->flags & D_PAGED) != 0)
3835 adjust = (sec->vma - voff) % bed->maxpagesize;
3836 else
3837 adjust = (sec->vma - voff) % align;
3838 }
3839 else
3840 adjust = 0;
3841
3842 if (adjust != 0)
3843 {
3844 if (i == 0)
3845 {
cdc7c09f
NC
3846 (* _bfd_error_handler) (_("\
3847Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3848 bfd_section_name (abfd, sec),
3849 sec->lma,
3850 p->p_paddr);
252b5132
RH
3851 return false;
3852 }
3853 p->p_memsz += adjust;
3854 off += adjust;
3855 voff += adjust;
3856 if ((flags & SEC_LOAD) != 0)
3857 p->p_filesz += adjust;
3858 }
3859
3860 sec->filepos = off;
3861
3862 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3863 used in a linker script we may have a section with
3864 SEC_LOAD clear but which is supposed to have
3865 contents. */
3866 if ((flags & SEC_LOAD) != 0
3867 || (flags & SEC_HAS_CONTENTS) != 0)
3868 off += sec->_raw_size;
3869
3870 if ((flags & SEC_ALLOC) != 0)
3871 voff += sec->_raw_size;
3872 }
3873
3874 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3875 {
4a938328
MS
3876 /* The actual "note" segment has i == 0.
3877 This is the one that actually contains everything. */
3878 if (i == 0)
3879 {
252b5132
RH
3880 sec->filepos = off;
3881 p->p_filesz = sec->_raw_size;
3882 off += sec->_raw_size;
3883 voff = off;
3884 }
4a938328 3885 else
252b5132 3886 {
4a938328 3887 /* Fake sections -- don't need to be written. */
252b5132
RH
3888 sec->filepos = 0;
3889 sec->_raw_size = 0;
4a938328 3890 flags = sec->flags = 0;
252b5132
RH
3891 }
3892 p->p_memsz = 0;
3893 p->p_align = 1;
3894 }
3895 else
3896 {
3897 p->p_memsz += sec->_raw_size;
3898
3899 if ((flags & SEC_LOAD) != 0)
3900 p->p_filesz += sec->_raw_size;
3901
13ae64f3
JJ
3902 if (p->p_type == PT_TLS
3903 && sec->_raw_size == 0
3904 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3905 {
3906 struct bfd_link_order *o;
3907 bfd_vma tbss_size = 0;
3908
3909 for (o = sec->link_order_head; o != NULL; o = o->next)
3910 if (tbss_size < o->offset + o->size)
3911 tbss_size = o->offset + o->size;
3912
3913 p->p_memsz += tbss_size;
3914 }
3915
252b5132
RH
3916 if (align > p->p_align
3917 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3918 p->p_align = align;
3919 }
3920
3921 if (! m->p_flags_valid)
3922 {
3923 p->p_flags |= PF_R;
3924 if ((flags & SEC_CODE) != 0)
3925 p->p_flags |= PF_X;
3926 if ((flags & SEC_READONLY) == 0)
3927 p->p_flags |= PF_W;
3928 }
3929 }
3930 }
3931
3932 /* Now that we have set the section file positions, we can set up
3933 the file positions for the non PT_LOAD segments. */
3934 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3935 m != NULL;
3936 m = m->next, p++)
3937 {
3938 if (p->p_type != PT_LOAD && m->count > 0)
3939 {
3940 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3941 p->p_offset = m->sections[0]->filepos;
3942 }
3943 if (m->count == 0)
3944 {
3945 if (m->includes_filehdr)
3946 {
3947 p->p_vaddr = filehdr_vaddr;
3948 if (! m->p_paddr_valid)
3949 p->p_paddr = filehdr_paddr;
3950 }
3951 else if (m->includes_phdrs)
3952 {
3953 p->p_vaddr = phdrs_vaddr;
3954 if (! m->p_paddr_valid)
3955 p->p_paddr = phdrs_paddr;
3956 }
3957 }
3958 }
3959
caf47ea6
AM
3960 /* If additional nonloadable filepos adjustments are required,
3961 do them now. */
3962 if (bed->set_nonloadable_filepos)
3963 (*bed->set_nonloadable_filepos) (abfd, phdrs);
3964
252b5132
RH
3965 /* Clear out any program headers we allocated but did not use. */
3966 for (; count < alloc; count++, p++)
3967 {
3968 memset (p, 0, sizeof *p);
3969 p->p_type = PT_NULL;
3970 }
3971
3972 elf_tdata (abfd)->phdr = phdrs;
3973
3974 elf_tdata (abfd)->next_file_pos = off;
3975
3976 /* Write out the program headers. */
dc810e39 3977 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
252b5132
RH
3978 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3979 return false;
3980
3981 return true;
3982}
3983
3984/* Get the size of the program header.
3985
3986 If this is called by the linker before any of the section VMA's are set, it
3987 can't calculate the correct value for a strange memory layout. This only
3988 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3989 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3990 data segment (exclusive of .interp and .dynamic).
3991
3992 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3993 will be two segments. */
3994
3995static bfd_size_type
3996get_program_header_size (abfd)
3997 bfd *abfd;
3998{
3999 size_t segs;
4000 asection *s;
4001 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4002
4003 /* We can't return a different result each time we're called. */
4004 if (elf_tdata (abfd)->program_header_size != 0)
4005 return elf_tdata (abfd)->program_header_size;
4006
4007 if (elf_tdata (abfd)->segment_map != NULL)
4008 {
4009 struct elf_segment_map *m;
4010
4011 segs = 0;
4012 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4013 ++segs;
4014 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4015 return elf_tdata (abfd)->program_header_size;
4016 }
4017
4018 /* Assume we will need exactly two PT_LOAD segments: one for text
4019 and one for data. */
4020 segs = 2;
4021
4022 s = bfd_get_section_by_name (abfd, ".interp");
4023 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4024 {
4025 /* If we have a loadable interpreter section, we need a
4026 PT_INTERP segment. In this case, assume we also need a
4027 PT_PHDR segment, although that may not be true for all
4028 targets. */
4029 segs += 2;
4030 }
4031
4032 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4033 {
4034 /* We need a PT_DYNAMIC segment. */
4035 ++segs;
4036 }
4037
65765700
JJ
4038 if (elf_tdata (abfd)->eh_frame_hdr
4039 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
4040 {
4041 /* We need a PT_GNU_EH_FRAME segment. */
4042 ++segs;
4043 }
4044
252b5132
RH
4045 for (s = abfd->sections; s != NULL; s = s->next)
4046 {
4047 if ((s->flags & SEC_LOAD) != 0
4048 && strncmp (s->name, ".note", 5) == 0)
4049 {
4050 /* We need a PT_NOTE segment. */
4051 ++segs;
4052 }
4053 }
4054
13ae64f3
JJ
4055 for (s = abfd->sections; s != NULL; s = s->next)
4056 {
4057 if (s->flags & SEC_THREAD_LOCAL)
4058 {
4059 /* We need a PT_TLS segment. */
4060 ++segs;
4061 break;
4062 }
4063 }
4064
252b5132
RH
4065 /* Let the backend count up any program headers it might need. */
4066 if (bed->elf_backend_additional_program_headers)
4067 {
4068 int a;
4069
4070 a = (*bed->elf_backend_additional_program_headers) (abfd);
4071 if (a == -1)
4072 abort ();
4073 segs += a;
4074 }
4075
4076 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4077 return elf_tdata (abfd)->program_header_size;
4078}
4079
4080/* Work out the file positions of all the sections. This is called by
4081 _bfd_elf_compute_section_file_positions. All the section sizes and
4082 VMAs must be known before this is called.
4083
4084 We do not consider reloc sections at this point, unless they form
4085 part of the loadable image. Reloc sections are assigned file
4086 positions in assign_file_positions_for_relocs, which is called by
4087 write_object_contents and final_link.
4088
4089 We also don't set the positions of the .symtab and .strtab here. */
4090
4091static boolean
4092assign_file_positions_except_relocs (abfd)
4093 bfd *abfd;
4094{
4095 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4096 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4097 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
9ad5cbcf 4098 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
4099 file_ptr off;
4100 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4101
4102 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4103 && bfd_get_format (abfd) != bfd_core)
4104 {
4105 Elf_Internal_Shdr **hdrpp;
4106 unsigned int i;
4107
4108 /* Start after the ELF header. */
4109 off = i_ehdrp->e_ehsize;
4110
4111 /* We are not creating an executable, which means that we are
4112 not creating a program header, and that the actual order of
4113 the sections in the file is unimportant. */
9ad5cbcf 4114 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
4115 {
4116 Elf_Internal_Shdr *hdr;
4117
4118 hdr = *hdrpp;
9ad5cbcf
AM
4119 if (hdr->sh_type == SHT_REL
4120 || hdr->sh_type == SHT_RELA
4121 || i == tdata->symtab_section
4122 || i == tdata->symtab_shndx_section
252b5132
RH
4123 || i == tdata->strtab_section)
4124 {
4125 hdr->sh_offset = -1;
252b5132 4126 }
9ad5cbcf
AM
4127 else
4128 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
252b5132 4129
9ad5cbcf
AM
4130 if (i == SHN_LORESERVE - 1)
4131 {
4132 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4133 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4134 }
252b5132
RH
4135 }
4136 }
4137 else
4138 {
4139 unsigned int i;
4140 Elf_Internal_Shdr **hdrpp;
4141
4142 /* Assign file positions for the loaded sections based on the
4143 assignment of sections to segments. */
4144 if (! assign_file_positions_for_segments (abfd))
4145 return false;
4146
4147 /* Assign file positions for the other sections. */
4148
4149 off = elf_tdata (abfd)->next_file_pos;
9ad5cbcf 4150 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
4151 {
4152 Elf_Internal_Shdr *hdr;
4153
4154 hdr = *hdrpp;
4155 if (hdr->bfd_section != NULL
4156 && hdr->bfd_section->filepos != 0)
4157 hdr->sh_offset = hdr->bfd_section->filepos;
4158 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4159 {
4160 ((*_bfd_error_handler)
4161 (_("%s: warning: allocated section `%s' not in segment"),
4162 bfd_get_filename (abfd),
4163 (hdr->bfd_section == NULL
4164 ? "*unknown*"
4165 : hdr->bfd_section->name)));
4166 if ((abfd->flags & D_PAGED) != 0)
4167 off += (hdr->sh_addr - off) % bed->maxpagesize;
4168 else
4169 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4170 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4171 false);
4172 }
4173 else if (hdr->sh_type == SHT_REL
4174 || hdr->sh_type == SHT_RELA
4175 || hdr == i_shdrpp[tdata->symtab_section]
9ad5cbcf 4176 || hdr == i_shdrpp[tdata->symtab_shndx_section]
252b5132
RH
4177 || hdr == i_shdrpp[tdata->strtab_section])
4178 hdr->sh_offset = -1;
4179 else
4180 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
9ad5cbcf
AM
4181
4182 if (i == SHN_LORESERVE - 1)
4183 {
4184 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4185 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4186 }
252b5132
RH
4187 }
4188 }
4189
4190 /* Place the section headers. */
4191 off = align_file_position (off, bed->s->file_align);
4192 i_ehdrp->e_shoff = off;
4193 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4194
4195 elf_tdata (abfd)->next_file_pos = off;
4196
4197 return true;
4198}
4199
4200static boolean
4201prep_headers (abfd)
4202 bfd *abfd;
4203{
4204 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4205 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4206 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2b0f7ef9 4207 struct elf_strtab_hash *shstrtab;
252b5132
RH
4208 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4209
4210 i_ehdrp = elf_elfheader (abfd);
4211 i_shdrp = elf_elfsections (abfd);
4212
2b0f7ef9 4213 shstrtab = _bfd_elf_strtab_init ();
252b5132
RH
4214 if (shstrtab == NULL)
4215 return false;
4216
4217 elf_shstrtab (abfd) = shstrtab;
4218
4219 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4220 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4221 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4222 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4223
4224 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4225 i_ehdrp->e_ident[EI_DATA] =
4226 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4227 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4228
252b5132
RH
4229 if ((abfd->flags & DYNAMIC) != 0)
4230 i_ehdrp->e_type = ET_DYN;
4231 else if ((abfd->flags & EXEC_P) != 0)
4232 i_ehdrp->e_type = ET_EXEC;
4233 else if (bfd_get_format (abfd) == bfd_core)
4234 i_ehdrp->e_type = ET_CORE;
4235 else
4236 i_ehdrp->e_type = ET_REL;
4237
4238 switch (bfd_get_arch (abfd))
4239 {
4240 case bfd_arch_unknown:
4241 i_ehdrp->e_machine = EM_NONE;
4242 break;
aa4f99bb
AO
4243
4244 /* There used to be a long list of cases here, each one setting
4245 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4246 in the corresponding bfd definition. To avoid duplication,
4247 the switch was removed. Machines that need special handling
4248 can generally do it in elf_backend_final_write_processing(),
4249 unless they need the information earlier than the final write.
4250 Such need can generally be supplied by replacing the tests for
4251 e_machine with the conditions used to determine it. */
252b5132 4252 default:
aa4f99bb
AO
4253 if (get_elf_backend_data (abfd) != NULL)
4254 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4255 else
4256 i_ehdrp->e_machine = EM_NONE;
4257 }
4258
252b5132
RH
4259 i_ehdrp->e_version = bed->s->ev_current;
4260 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4261
c044fabd 4262 /* No program header, for now. */
252b5132
RH
4263 i_ehdrp->e_phoff = 0;
4264 i_ehdrp->e_phentsize = 0;
4265 i_ehdrp->e_phnum = 0;
4266
c044fabd 4267 /* Each bfd section is section header entry. */
252b5132
RH
4268 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4269 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4270
c044fabd 4271 /* If we're building an executable, we'll need a program header table. */
252b5132
RH
4272 if (abfd->flags & EXEC_P)
4273 {
c044fabd 4274 /* It all happens later. */
252b5132
RH
4275#if 0
4276 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4277
4278 /* elf_build_phdrs() returns a (NULL-terminated) array of
c044fabd 4279 Elf_Internal_Phdrs. */
252b5132
RH
4280 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4281 i_ehdrp->e_phoff = outbase;
4282 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4283#endif
4284 }
4285 else
4286 {
4287 i_ehdrp->e_phentsize = 0;
4288 i_phdrp = 0;
4289 i_ehdrp->e_phoff = 0;
4290 }
4291
4292 elf_tdata (abfd)->symtab_hdr.sh_name =
2b0f7ef9 4293 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
252b5132 4294 elf_tdata (abfd)->strtab_hdr.sh_name =
2b0f7ef9 4295 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
252b5132 4296 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2b0f7ef9 4297 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
252b5132
RH
4298 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4299 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4300 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4301 return false;
4302
4303 return true;
4304}
4305
4306/* Assign file positions for all the reloc sections which are not part
4307 of the loadable file image. */
4308
4309void
4310_bfd_elf_assign_file_positions_for_relocs (abfd)
4311 bfd *abfd;
4312{
4313 file_ptr off;
9ad5cbcf 4314 unsigned int i, num_sec;
252b5132
RH
4315 Elf_Internal_Shdr **shdrpp;
4316
4317 off = elf_tdata (abfd)->next_file_pos;
4318
9ad5cbcf
AM
4319 num_sec = elf_numsections (abfd);
4320 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
252b5132
RH
4321 {
4322 Elf_Internal_Shdr *shdrp;
4323
4324 shdrp = *shdrpp;
4325 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4326 && shdrp->sh_offset == -1)
4327 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
4328 }
4329
4330 elf_tdata (abfd)->next_file_pos = off;
4331}
4332
4333boolean
4334_bfd_elf_write_object_contents (abfd)
4335 bfd *abfd;
4336{
4337 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4338 Elf_Internal_Ehdr *i_ehdrp;
4339 Elf_Internal_Shdr **i_shdrp;
4340 boolean failed;
9ad5cbcf 4341 unsigned int count, num_sec;
252b5132
RH
4342
4343 if (! abfd->output_has_begun
4344 && ! _bfd_elf_compute_section_file_positions
4345 (abfd, (struct bfd_link_info *) NULL))
4346 return false;
4347
4348 i_shdrp = elf_elfsections (abfd);
4349 i_ehdrp = elf_elfheader (abfd);
4350
4351 failed = false;
4352 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4353 if (failed)
4354 return false;
4355
4356 _bfd_elf_assign_file_positions_for_relocs (abfd);
4357
c044fabd 4358 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
4359 num_sec = elf_numsections (abfd);
4360 for (count = 1; count < num_sec; count++)
252b5132
RH
4361 {
4362 if (bed->elf_backend_section_processing)
4363 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4364 if (i_shdrp[count]->contents)
4365 {
dc810e39
AM
4366 bfd_size_type amt = i_shdrp[count]->sh_size;
4367
252b5132 4368 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 4369 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
252b5132
RH
4370 return false;
4371 }
9ad5cbcf
AM
4372 if (count == SHN_LORESERVE - 1)
4373 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
252b5132
RH
4374 }
4375
4376 /* Write out the section header names. */
4377 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2b0f7ef9 4378 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
252b5132
RH
4379 return false;
4380
4381 if (bed->elf_backend_final_write_processing)
4382 (*bed->elf_backend_final_write_processing) (abfd,
4383 elf_tdata (abfd)->linker);
4384
4385 return bed->s->write_shdrs_and_ehdr (abfd);
4386}
4387
4388boolean
4389_bfd_elf_write_corefile_contents (abfd)
4390 bfd *abfd;
4391{
c044fabd 4392 /* Hopefully this can be done just like an object file. */
252b5132
RH
4393 return _bfd_elf_write_object_contents (abfd);
4394}
c044fabd
KH
4395
4396/* Given a section, search the header to find them. */
4397
252b5132
RH
4398int
4399_bfd_elf_section_from_bfd_section (abfd, asect)
4400 bfd *abfd;
4401 struct sec *asect;
4402{
af746e92 4403 struct elf_backend_data *bed;
252b5132 4404 int index;
252b5132 4405
9ad5cbcf
AM
4406 if (elf_section_data (asect) != NULL
4407 && elf_section_data (asect)->this_idx != 0)
4408 return elf_section_data (asect)->this_idx;
4409
4410 if (bfd_is_abs_section (asect))
af746e92
AM
4411 index = SHN_ABS;
4412 else if (bfd_is_com_section (asect))
4413 index = SHN_COMMON;
4414 else if (bfd_is_und_section (asect))
4415 index = SHN_UNDEF;
4416 else
252b5132 4417 {
af746e92
AM
4418 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4419 int maxindex = elf_numsections (abfd);
4420
4421 for (index = 1; index < maxindex; index++)
4422 {
4423 Elf_Internal_Shdr *hdr = i_shdrp[index];
4424
4425 if (hdr != NULL && hdr->bfd_section == asect)
4426 return index;
4427 }
4428 index = -1;
252b5132
RH
4429 }
4430
af746e92 4431 bed = get_elf_backend_data (abfd);
252b5132
RH
4432 if (bed->elf_backend_section_from_bfd_section)
4433 {
af746e92 4434 int retval = index;
9ad5cbcf 4435
af746e92
AM
4436 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4437 return retval;
252b5132
RH
4438 }
4439
af746e92
AM
4440 if (index == -1)
4441 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 4442
af746e92 4443 return index;
252b5132
RH
4444}
4445
4446/* Given a BFD symbol, return the index in the ELF symbol table, or -1
4447 on error. */
4448
4449int
4450_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4451 bfd *abfd;
4452 asymbol **asym_ptr_ptr;
4453{
4454 asymbol *asym_ptr = *asym_ptr_ptr;
4455 int idx;
4456 flagword flags = asym_ptr->flags;
4457
4458 /* When gas creates relocations against local labels, it creates its
4459 own symbol for the section, but does put the symbol into the
4460 symbol chain, so udata is 0. When the linker is generating
4461 relocatable output, this section symbol may be for one of the
4462 input sections rather than the output section. */
4463 if (asym_ptr->udata.i == 0
4464 && (flags & BSF_SECTION_SYM)
4465 && asym_ptr->section)
4466 {
4467 int indx;
4468
4469 if (asym_ptr->section->output_section != NULL)
4470 indx = asym_ptr->section->output_section->index;
4471 else
4472 indx = asym_ptr->section->index;
4e89ac30
L
4473 if (indx < elf_num_section_syms (abfd)
4474 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
4475 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4476 }
4477
4478 idx = asym_ptr->udata.i;
4479
4480 if (idx == 0)
4481 {
4482 /* This case can occur when using --strip-symbol on a symbol
4483 which is used in a relocation entry. */
4484 (*_bfd_error_handler)
4485 (_("%s: symbol `%s' required but not present"),
8f615d07 4486 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
252b5132
RH
4487 bfd_set_error (bfd_error_no_symbols);
4488 return -1;
4489 }
4490
4491#if DEBUG & 4
4492 {
4493 fprintf (stderr,
661a3fd4 4494 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
252b5132
RH
4495 (long) asym_ptr, asym_ptr->name, idx, flags,
4496 elf_symbol_flags (flags));
4497 fflush (stderr);
4498 }
4499#endif
4500
4501 return idx;
4502}
4503
4504/* Copy private BFD data. This copies any program header information. */
4505
4506static boolean
4507copy_private_bfd_data (ibfd, obfd)
4508 bfd *ibfd;
4509 bfd *obfd;
4510{
bc67d8a6
NC
4511 Elf_Internal_Ehdr * iehdr;
4512 struct elf_segment_map * map;
4513 struct elf_segment_map * map_first;
4514 struct elf_segment_map ** pointer_to_map;
4515 Elf_Internal_Phdr * segment;
4516 asection * section;
4517 unsigned int i;
4518 unsigned int num_segments;
4519 boolean phdr_included = false;
4520 bfd_vma maxpagesize;
4521 struct elf_segment_map * phdr_adjust_seg = NULL;
4522 unsigned int phdr_adjust_num = 0;
caf47ea6 4523 struct elf_backend_data * bed;
bc67d8a6 4524
c044fabd 4525 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
252b5132
RH
4526 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4527 return true;
4528
4529 if (elf_tdata (ibfd)->phdr == NULL)
4530 return true;
4531
caf47ea6 4532 bed = get_elf_backend_data (ibfd);
252b5132
RH
4533 iehdr = elf_elfheader (ibfd);
4534
bc67d8a6 4535 map_first = NULL;
c044fabd 4536 pointer_to_map = &map_first;
252b5132
RH
4537
4538 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
4539 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4540
4541 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
4542#define SEGMENT_END(segment, start) \
4543 (start + (segment->p_memsz > segment->p_filesz \
4544 ? segment->p_memsz : segment->p_filesz))
bc67d8a6
NC
4545
4546 /* Returns true if the given section is contained within
4547 the given segment. VMA addresses are compared. */
aecc8f8a
AM
4548#define IS_CONTAINED_BY_VMA(section, segment) \
4549 (section->vma >= segment->p_vaddr \
4550 && (section->vma + section->_raw_size \
4551 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 4552
bc67d8a6
NC
4553 /* Returns true if the given section is contained within
4554 the given segment. LMA addresses are compared. */
aecc8f8a
AM
4555#define IS_CONTAINED_BY_LMA(section, segment, base) \
4556 (section->lma >= base \
4557 && (section->lma + section->_raw_size \
4558 <= SEGMENT_END (segment, base)))
252b5132 4559
caf47ea6
AM
4560 /* Returns true if the given section is contained within the
4561 given segment. Filepos addresses are compared in an elf
4562 backend function. */
aecc8f8a
AM
4563#define IS_CONTAINED_BY_FILEPOS(sec, seg, bed) \
4564 (bed->is_contained_by_filepos \
caf47ea6
AM
4565 && (*bed->is_contained_by_filepos) (sec, seg))
4566
c044fabd 4567 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
aecc8f8a
AM
4568#define IS_COREFILE_NOTE(p, s) \
4569 (p->p_type == PT_NOTE \
4570 && bfd_get_format (ibfd) == bfd_core \
4571 && s->vma == 0 && s->lma == 0 \
4572 && (bfd_vma) s->filepos >= p->p_offset \
4573 && ((bfd_vma) s->filepos + s->_raw_size \
4574 <= p->p_offset + p->p_filesz))
252b5132
RH
4575
4576 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4577 linker, which generates a PT_INTERP section with p_vaddr and
4578 p_memsz set to 0. */
aecc8f8a
AM
4579#define IS_SOLARIS_PT_INTERP(p, s) \
4580 (p->p_vaddr == 0 \
4581 && p->p_paddr == 0 \
4582 && p->p_memsz == 0 \
4583 && p->p_filesz > 0 \
4584 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4585 && s->_raw_size > 0 \
4586 && (bfd_vma) s->filepos >= p->p_offset \
4587 && ((bfd_vma) s->filepos + s->_raw_size \
4588 <= p->p_offset + p->p_filesz))
5c440b1e 4589
bc67d8a6
NC
4590 /* Decide if the given section should be included in the given segment.
4591 A section will be included if:
f5ffc919
NC
4592 1. It is within the address space of the segment -- we use the LMA
4593 if that is set for the segment and the VMA otherwise,
bc67d8a6
NC
4594 2. It is an allocated segment,
4595 3. There is an output section associated with it,
4596 4. The section has not already been allocated to a previous segment. */
caf47ea6 4597#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
aecc8f8a
AM
4598 ((((segment->p_paddr \
4599 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4600 : IS_CONTAINED_BY_VMA (section, segment)) \
f5ffc919 4601 && (section->flags & SEC_ALLOC) != 0) \
caf47ea6
AM
4602 || IS_COREFILE_NOTE (segment, section) \
4603 || (IS_CONTAINED_BY_FILEPOS (section, segment, bed) \
4604 && (section->flags & SEC_ALLOC) == 0)) \
f5ffc919 4605 && section->output_section != NULL \
82e51918 4606 && ! section->segment_mark)
bc67d8a6
NC
4607
4608 /* Returns true iff seg1 starts after the end of seg2. */
aecc8f8a
AM
4609#define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4610 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
bc67d8a6
NC
4611
4612 /* Returns true iff seg1 and seg2 overlap. */
aecc8f8a
AM
4613#define SEGMENT_OVERLAPS(seg1, seg2) \
4614 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) \
4615 || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
bc67d8a6
NC
4616
4617 /* Initialise the segment mark field. */
4618 for (section = ibfd->sections; section != NULL; section = section->next)
4619 section->segment_mark = false;
4620
252b5132 4621 /* Scan through the segments specified in the program header
bc67d8a6 4622 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 4623 in the loadable segments. These can be created by weird
aecc8f8a 4624 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
4625 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4626 i < num_segments;
c044fabd 4627 i++, segment++)
252b5132 4628 {
252b5132 4629 unsigned int j;
c044fabd 4630 Elf_Internal_Phdr *segment2;
252b5132 4631
aecc8f8a
AM
4632 if (segment->p_type == PT_INTERP)
4633 for (section = ibfd->sections; section; section = section->next)
4634 if (IS_SOLARIS_PT_INTERP (segment, section))
4635 {
4636 /* Mininal change so that the normal section to segment
4637 assigment code will work. */
4638 segment->p_vaddr = section->vma;
4639 break;
4640 }
4641
bc67d8a6
NC
4642 if (segment->p_type != PT_LOAD)
4643 continue;
c044fabd 4644
bc67d8a6 4645 /* Determine if this segment overlaps any previous segments. */
c044fabd 4646 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
bc67d8a6
NC
4647 {
4648 bfd_signed_vma extra_length;
c044fabd 4649
bc67d8a6
NC
4650 if (segment2->p_type != PT_LOAD
4651 || ! SEGMENT_OVERLAPS (segment, segment2))
4652 continue;
c044fabd 4653
bc67d8a6
NC
4654 /* Merge the two segments together. */
4655 if (segment2->p_vaddr < segment->p_vaddr)
4656 {
c044fabd
KH
4657 /* Extend SEGMENT2 to include SEGMENT and then delete
4658 SEGMENT. */
bc67d8a6
NC
4659 extra_length =
4660 SEGMENT_END (segment, segment->p_vaddr)
4661 - SEGMENT_END (segment2, segment2->p_vaddr);
c044fabd 4662
bc67d8a6
NC
4663 if (extra_length > 0)
4664 {
4665 segment2->p_memsz += extra_length;
4666 segment2->p_filesz += extra_length;
4667 }
c044fabd 4668
bc67d8a6 4669 segment->p_type = PT_NULL;
c044fabd 4670
bc67d8a6
NC
4671 /* Since we have deleted P we must restart the outer loop. */
4672 i = 0;
4673 segment = elf_tdata (ibfd)->phdr;
4674 break;
4675 }
4676 else
4677 {
c044fabd
KH
4678 /* Extend SEGMENT to include SEGMENT2 and then delete
4679 SEGMENT2. */
bc67d8a6
NC
4680 extra_length =
4681 SEGMENT_END (segment2, segment2->p_vaddr)
4682 - SEGMENT_END (segment, segment->p_vaddr);
c044fabd 4683
bc67d8a6
NC
4684 if (extra_length > 0)
4685 {
4686 segment->p_memsz += extra_length;
4687 segment->p_filesz += extra_length;
4688 }
c044fabd 4689
bc67d8a6
NC
4690 segment2->p_type = PT_NULL;
4691 }
4692 }
4693 }
c044fabd 4694
bc67d8a6
NC
4695 /* The second scan attempts to assign sections to segments. */
4696 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4697 i < num_segments;
4698 i ++, segment ++)
4699 {
4700 unsigned int section_count;
4701 asection ** sections;
4702 asection * output_section;
4703 unsigned int isec;
4704 bfd_vma matching_lma;
4705 bfd_vma suggested_lma;
4706 unsigned int j;
dc810e39 4707 bfd_size_type amt;
bc67d8a6
NC
4708
4709 if (segment->p_type == PT_NULL)
4710 continue;
c044fabd 4711
bc67d8a6
NC
4712 /* Compute how many sections might be placed into this segment. */
4713 section_count = 0;
4714 for (section = ibfd->sections; section != NULL; section = section->next)
caf47ea6 4715 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c044fabd 4716 ++section_count;
252b5132
RH
4717
4718 /* Allocate a segment map big enough to contain all of the
4719 sections we have selected. */
dc810e39
AM
4720 amt = sizeof (struct elf_segment_map);
4721 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4722 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 4723 if (map == NULL)
252b5132
RH
4724 return false;
4725
4726 /* Initialise the fields of the segment map. Default to
4727 using the physical address of the segment in the input BFD. */
bc67d8a6
NC
4728 map->next = NULL;
4729 map->p_type = segment->p_type;
4730 map->p_flags = segment->p_flags;
4731 map->p_flags_valid = 1;
4732 map->p_paddr = segment->p_paddr;
4733 map->p_paddr_valid = 1;
252b5132
RH
4734
4735 /* Determine if this segment contains the ELF file header
4736 and if it contains the program headers themselves. */
bc67d8a6
NC
4737 map->includes_filehdr = (segment->p_offset == 0
4738 && segment->p_filesz >= iehdr->e_ehsize);
252b5132 4739
bc67d8a6 4740 map->includes_phdrs = 0;
252b5132 4741
bc67d8a6 4742 if (! phdr_included || segment->p_type != PT_LOAD)
252b5132 4743 {
bc67d8a6
NC
4744 map->includes_phdrs =
4745 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4746 && (segment->p_offset + segment->p_filesz
252b5132
RH
4747 >= ((bfd_vma) iehdr->e_phoff
4748 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 4749
bc67d8a6 4750 if (segment->p_type == PT_LOAD && map->includes_phdrs)
252b5132
RH
4751 phdr_included = true;
4752 }
4753
bc67d8a6 4754 if (section_count == 0)
252b5132
RH
4755 {
4756 /* Special segments, such as the PT_PHDR segment, may contain
4757 no sections, but ordinary, loadable segments should contain
1ed89aa9
NC
4758 something. They are allowed by the ELF spec however, so only
4759 a warning is produced. */
bc67d8a6 4760 if (segment->p_type == PT_LOAD)
caf47ea6 4761 (*_bfd_error_handler)
1ed89aa9 4762 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
caf47ea6 4763 bfd_archive_filename (ibfd));
252b5132 4764
bc67d8a6 4765 map->count = 0;
c044fabd
KH
4766 *pointer_to_map = map;
4767 pointer_to_map = &map->next;
252b5132
RH
4768
4769 continue;
4770 }
4771
4772 /* Now scan the sections in the input BFD again and attempt
4773 to add their corresponding output sections to the segment map.
4774 The problem here is how to handle an output section which has
4775 been moved (ie had its LMA changed). There are four possibilities:
4776
4777 1. None of the sections have been moved.
4778 In this case we can continue to use the segment LMA from the
4779 input BFD.
4780
4781 2. All of the sections have been moved by the same amount.
4782 In this case we can change the segment's LMA to match the LMA
4783 of the first section.
4784
4785 3. Some of the sections have been moved, others have not.
4786 In this case those sections which have not been moved can be
4787 placed in the current segment which will have to have its size,
4788 and possibly its LMA changed, and a new segment or segments will
4789 have to be created to contain the other sections.
4790
4791 4. The sections have been moved, but not be the same amount.
4792 In this case we can change the segment's LMA to match the LMA
4793 of the first section and we will have to create a new segment
4794 or segments to contain the other sections.
4795
4796 In order to save time, we allocate an array to hold the section
4797 pointers that we are interested in. As these sections get assigned
4798 to a segment, they are removed from this array. */
4799
0b14c2aa
L
4800 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4801 to work around this long long bug. */
4802 amt = section_count * sizeof (asection *);
dc810e39 4803 sections = (asection **) bfd_malloc (amt);
252b5132
RH
4804 if (sections == NULL)
4805 return false;
4806
4807 /* Step One: Scan for segment vs section LMA conflicts.
4808 Also add the sections to the section array allocated above.
4809 Also add the sections to the current segment. In the common
4810 case, where the sections have not been moved, this means that
4811 we have completely filled the segment, and there is nothing
4812 more to do. */
252b5132 4813 isec = 0;
72730e0c 4814 matching_lma = 0;
252b5132
RH
4815 suggested_lma = 0;
4816
bc67d8a6
NC
4817 for (j = 0, section = ibfd->sections;
4818 section != NULL;
4819 section = section->next)
252b5132 4820 {
caf47ea6 4821 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 4822 {
bc67d8a6
NC
4823 output_section = section->output_section;
4824
4825 sections[j ++] = section;
252b5132
RH
4826
4827 /* The Solaris native linker always sets p_paddr to 0.
4828 We try to catch that case here, and set it to the
5e8d7549
NC
4829 correct value. Note - some backends require that
4830 p_paddr be left as zero. */
bc67d8a6 4831 if (segment->p_paddr == 0
4455705d 4832 && segment->p_vaddr != 0
5e8d7549 4833 && (! bed->want_p_paddr_set_to_zero)
252b5132 4834 && isec == 0
bc67d8a6
NC
4835 && output_section->lma != 0
4836 && (output_section->vma == (segment->p_vaddr
4837 + (map->includes_filehdr
4838 ? iehdr->e_ehsize
4839 : 0)
4840 + (map->includes_phdrs
079e9a2f
AM
4841 ? (iehdr->e_phnum
4842 * iehdr->e_phentsize)
bc67d8a6
NC
4843 : 0))))
4844 map->p_paddr = segment->p_vaddr;
252b5132
RH
4845
4846 /* Match up the physical address of the segment with the
4847 LMA address of the output section. */
bc67d8a6 4848 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
caf47ea6 4849 || IS_CONTAINED_BY_FILEPOS (section, segment, bed)
5e8d7549
NC
4850 || IS_COREFILE_NOTE (segment, section)
4851 || (bed->want_p_paddr_set_to_zero &&
4852 IS_CONTAINED_BY_VMA (output_section, segment))
4853 )
252b5132
RH
4854 {
4855 if (matching_lma == 0)
bc67d8a6 4856 matching_lma = output_section->lma;
252b5132
RH
4857
4858 /* We assume that if the section fits within the segment
bc67d8a6 4859 then it does not overlap any other section within that
252b5132 4860 segment. */
bc67d8a6 4861 map->sections[isec ++] = output_section;
252b5132
RH
4862 }
4863 else if (suggested_lma == 0)
bc67d8a6 4864 suggested_lma = output_section->lma;
252b5132
RH
4865 }
4866 }
4867
bc67d8a6 4868 BFD_ASSERT (j == section_count);
252b5132
RH
4869
4870 /* Step Two: Adjust the physical address of the current segment,
4871 if necessary. */
bc67d8a6 4872 if (isec == section_count)
252b5132
RH
4873 {
4874 /* All of the sections fitted within the segment as currently
4875 specified. This is the default case. Add the segment to
4876 the list of built segments and carry on to process the next
4877 program header in the input BFD. */
bc67d8a6 4878 map->count = section_count;
c044fabd
KH
4879 *pointer_to_map = map;
4880 pointer_to_map = &map->next;
252b5132
RH
4881
4882 free (sections);
4883 continue;
4884 }
252b5132
RH
4885 else
4886 {
72730e0c
AM
4887 if (matching_lma != 0)
4888 {
4889 /* At least one section fits inside the current segment.
4890 Keep it, but modify its physical address to match the
4891 LMA of the first section that fitted. */
bc67d8a6 4892 map->p_paddr = matching_lma;
72730e0c
AM
4893 }
4894 else
4895 {
4896 /* None of the sections fitted inside the current segment.
4897 Change the current segment's physical address to match
4898 the LMA of the first section. */
bc67d8a6 4899 map->p_paddr = suggested_lma;
72730e0c
AM
4900 }
4901
bc67d8a6
NC
4902 /* Offset the segment physical address from the lma
4903 to allow for space taken up by elf headers. */
4904 if (map->includes_filehdr)
4905 map->p_paddr -= iehdr->e_ehsize;
252b5132 4906
bc67d8a6
NC
4907 if (map->includes_phdrs)
4908 {
4909 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4910
4911 /* iehdr->e_phnum is just an estimate of the number
4912 of program headers that we will need. Make a note
4913 here of the number we used and the segment we chose
4914 to hold these headers, so that we can adjust the
4915 offset when we know the correct value. */
4916 phdr_adjust_num = iehdr->e_phnum;
4917 phdr_adjust_seg = map;
4918 }
252b5132
RH
4919 }
4920
4921 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 4922 those that fit to the current segment and removing them from the
252b5132
RH
4923 sections array; but making sure not to leave large gaps. Once all
4924 possible sections have been assigned to the current segment it is
4925 added to the list of built segments and if sections still remain
4926 to be assigned, a new segment is constructed before repeating
4927 the loop. */
4928 isec = 0;
4929 do
4930 {
bc67d8a6 4931 map->count = 0;
252b5132
RH
4932 suggested_lma = 0;
4933
4934 /* Fill the current segment with sections that fit. */
bc67d8a6 4935 for (j = 0; j < section_count; j++)
252b5132 4936 {
bc67d8a6 4937 section = sections[j];
252b5132 4938
bc67d8a6 4939 if (section == NULL)
252b5132
RH
4940 continue;
4941
bc67d8a6 4942 output_section = section->output_section;
252b5132 4943
bc67d8a6 4944 BFD_ASSERT (output_section != NULL);
c044fabd 4945
bc67d8a6
NC
4946 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4947 || IS_COREFILE_NOTE (segment, section))
252b5132 4948 {
bc67d8a6 4949 if (map->count == 0)
252b5132
RH
4950 {
4951 /* If the first section in a segment does not start at
bc67d8a6
NC
4952 the beginning of the segment, then something is
4953 wrong. */
4954 if (output_section->lma !=
4955 (map->p_paddr
4956 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4957 + (map->includes_phdrs
4958 ? iehdr->e_phnum * iehdr->e_phentsize
4959 : 0)))
252b5132
RH
4960 abort ();
4961 }
4962 else
4963 {
4964 asection * prev_sec;
252b5132 4965
bc67d8a6 4966 prev_sec = map->sections[map->count - 1];
252b5132
RH
4967
4968 /* If the gap between the end of the previous section
bc67d8a6
NC
4969 and the start of this section is more than
4970 maxpagesize then we need to start a new segment. */
079e9a2f
AM
4971 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4972 maxpagesize)
caf47ea6 4973 < BFD_ALIGN (output_section->lma, maxpagesize))
079e9a2f
AM
4974 || ((prev_sec->lma + prev_sec->_raw_size)
4975 > output_section->lma))
252b5132
RH
4976 {
4977 if (suggested_lma == 0)
bc67d8a6 4978 suggested_lma = output_section->lma;
252b5132
RH
4979
4980 continue;
4981 }
4982 }
4983
bc67d8a6 4984 map->sections[map->count++] = output_section;
252b5132
RH
4985 ++isec;
4986 sections[j] = NULL;
bc67d8a6 4987 section->segment_mark = true;
252b5132
RH
4988 }
4989 else if (suggested_lma == 0)
bc67d8a6 4990 suggested_lma = output_section->lma;
252b5132
RH
4991 }
4992
bc67d8a6 4993 BFD_ASSERT (map->count > 0);
252b5132
RH
4994
4995 /* Add the current segment to the list of built segments. */
c044fabd
KH
4996 *pointer_to_map = map;
4997 pointer_to_map = &map->next;
252b5132 4998
bc67d8a6 4999 if (isec < section_count)
252b5132
RH
5000 {
5001 /* We still have not allocated all of the sections to
5002 segments. Create a new segment here, initialise it
5003 and carry on looping. */
dc810e39
AM
5004 amt = sizeof (struct elf_segment_map);
5005 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5006 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
bc67d8a6 5007 if (map == NULL)
252b5132
RH
5008 return false;
5009
5010 /* Initialise the fields of the segment map. Set the physical
5011 physical address to the LMA of the first section that has
5012 not yet been assigned. */
bc67d8a6
NC
5013 map->next = NULL;
5014 map->p_type = segment->p_type;
5015 map->p_flags = segment->p_flags;
5016 map->p_flags_valid = 1;
5017 map->p_paddr = suggested_lma;
5018 map->p_paddr_valid = 1;
5019 map->includes_filehdr = 0;
5020 map->includes_phdrs = 0;
252b5132
RH
5021 }
5022 }
bc67d8a6 5023 while (isec < section_count);
252b5132
RH
5024
5025 free (sections);
5026 }
5027
5028 /* The Solaris linker creates program headers in which all the
5029 p_paddr fields are zero. When we try to objcopy or strip such a
5030 file, we get confused. Check for this case, and if we find it
5031 reset the p_paddr_valid fields. */
bc67d8a6
NC
5032 for (map = map_first; map != NULL; map = map->next)
5033 if (map->p_paddr != 0)
252b5132 5034 break;
bc67d8a6 5035 if (map == NULL)
252b5132 5036 {
bc67d8a6
NC
5037 for (map = map_first; map != NULL; map = map->next)
5038 map->p_paddr_valid = 0;
252b5132
RH
5039 }
5040
bc67d8a6
NC
5041 elf_tdata (obfd)->segment_map = map_first;
5042
5043 /* If we had to estimate the number of program headers that were
9ad5cbcf 5044 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
5045 the offset if necessary. */
5046 if (phdr_adjust_seg != NULL)
5047 {
5048 unsigned int count;
c044fabd 5049
bc67d8a6 5050 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 5051 count++;
252b5132 5052
bc67d8a6
NC
5053 if (count > phdr_adjust_num)
5054 phdr_adjust_seg->p_paddr
5055 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5056 }
c044fabd 5057
252b5132 5058#if 0
c044fabd
KH
5059 /* Final Step: Sort the segments into ascending order of physical
5060 address. */
bc67d8a6 5061 if (map_first != NULL)
252b5132 5062 {
c044fabd 5063 struct elf_segment_map *prev;
252b5132 5064
bc67d8a6
NC
5065 prev = map_first;
5066 for (map = map_first->next; map != NULL; prev = map, map = map->next)
252b5132 5067 {
bc67d8a6
NC
5068 /* Yes I know - its a bubble sort.... */
5069 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
252b5132 5070 {
bc67d8a6
NC
5071 /* Swap map and map->next. */
5072 prev->next = map->next;
5073 map->next = map->next->next;
5074 prev->next->next = map;
252b5132 5075
bc67d8a6
NC
5076 /* Restart loop. */
5077 map = map_first;
252b5132
RH
5078 }
5079 }
5080 }
5081#endif
5082
bc67d8a6
NC
5083#undef SEGMENT_END
5084#undef IS_CONTAINED_BY_VMA
5085#undef IS_CONTAINED_BY_LMA
caf47ea6 5086#undef IS_CONTAINED_BY_FILEPOS
252b5132 5087#undef IS_COREFILE_NOTE
bc67d8a6
NC
5088#undef IS_SOLARIS_PT_INTERP
5089#undef INCLUDE_SECTION_IN_SEGMENT
5090#undef SEGMENT_AFTER_SEGMENT
5091#undef SEGMENT_OVERLAPS
252b5132
RH
5092 return true;
5093}
5094
5095/* Copy private section information. This copies over the entsize
5096 field, and sometimes the info field. */
5097
5098boolean
5099_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
5100 bfd *ibfd;
5101 asection *isec;
5102 bfd *obfd;
5103 asection *osec;
5104{
5105 Elf_Internal_Shdr *ihdr, *ohdr;
caf47ea6 5106 const struct elf_backend_data *bed = get_elf_backend_data (ibfd);
252b5132
RH
5107
5108 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5109 || obfd->xvec->flavour != bfd_target_elf_flavour)
5110 return true;
5111
5112 /* Copy over private BFD data if it has not already been copied.
5113 This must be done here, rather than in the copy_private_bfd_data
5114 entry point, because the latter is called after the section
5115 contents have been set, which means that the program headers have
47d9a591
AM
5116 already been worked out. The backend function provides a way to
5117 override the test conditions and code path for the call to
caf47ea6
AM
5118 copy_private_bfd_data. */
5119 if (bed->copy_private_bfd_data_p)
252b5132 5120 {
caf47ea6
AM
5121 if ((*bed->copy_private_bfd_data_p) (ibfd, isec, obfd, osec))
5122 if (! copy_private_bfd_data (ibfd, obfd))
5123 return false;
47d9a591 5124 }
caf47ea6
AM
5125 else if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5126 {
5127 asection *s;
5128
5129 /* Only set up the segments if there are no more SEC_ALLOC
5130 sections. FIXME: This won't do the right thing if objcopy is
5131 used to remove the last SEC_ALLOC section, since objcopy
5132 won't call this routine in that case. */
5133 for (s = isec->next; s != NULL; s = s->next)
5134 if ((s->flags & SEC_ALLOC) != 0)
5135 break;
5136 if (s == NULL)
5137 {
5138 if (! copy_private_bfd_data (ibfd, obfd))
5139 return false;
5140 }
252b5132
RH
5141 }
5142
5143 ihdr = &elf_section_data (isec)->this_hdr;
5144 ohdr = &elf_section_data (osec)->this_hdr;
5145
5146 ohdr->sh_entsize = ihdr->sh_entsize;
5147
5148 if (ihdr->sh_type == SHT_SYMTAB
5149 || ihdr->sh_type == SHT_DYNSYM
5150 || ihdr->sh_type == SHT_GNU_verneed
5151 || ihdr->sh_type == SHT_GNU_verdef)
5152 ohdr->sh_info = ihdr->sh_info;
5153
9dce4196
AM
5154 /* Set things up for objcopy. The output SHT_GROUP section will
5155 have its elf_next_in_group pointing back to the input group
5156 members. */
5157 elf_next_in_group (osec) = elf_next_in_group (isec);
5158 elf_group_name (osec) = elf_group_name (isec);
5159
bf572ba0
MM
5160 elf_section_data (osec)->use_rela_p
5161 = elf_section_data (isec)->use_rela_p;
5162
252b5132
RH
5163 return true;
5164}
5165
5166/* Copy private symbol information. If this symbol is in a section
5167 which we did not map into a BFD section, try to map the section
5168 index correctly. We use special macro definitions for the mapped
5169 section indices; these definitions are interpreted by the
5170 swap_out_syms function. */
5171
9ad5cbcf
AM
5172#define MAP_ONESYMTAB (SHN_HIOS + 1)
5173#define MAP_DYNSYMTAB (SHN_HIOS + 2)
5174#define MAP_STRTAB (SHN_HIOS + 3)
5175#define MAP_SHSTRTAB (SHN_HIOS + 4)
5176#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132
RH
5177
5178boolean
5179_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5180 bfd *ibfd;
5181 asymbol *isymarg;
5182 bfd *obfd;
5183 asymbol *osymarg;
5184{
5185 elf_symbol_type *isym, *osym;
5186
5187 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5188 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5189 return true;
5190
5191 isym = elf_symbol_from (ibfd, isymarg);
5192 osym = elf_symbol_from (obfd, osymarg);
5193
5194 if (isym != NULL
5195 && osym != NULL
5196 && bfd_is_abs_section (isym->symbol.section))
5197 {
5198 unsigned int shndx;
5199
5200 shndx = isym->internal_elf_sym.st_shndx;
5201 if (shndx == elf_onesymtab (ibfd))
5202 shndx = MAP_ONESYMTAB;
5203 else if (shndx == elf_dynsymtab (ibfd))
5204 shndx = MAP_DYNSYMTAB;
5205 else if (shndx == elf_tdata (ibfd)->strtab_section)
5206 shndx = MAP_STRTAB;
5207 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5208 shndx = MAP_SHSTRTAB;
9ad5cbcf
AM
5209 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5210 shndx = MAP_SYM_SHNDX;
252b5132
RH
5211 osym->internal_elf_sym.st_shndx = shndx;
5212 }
5213
5214 return true;
5215}
5216
5217/* Swap out the symbols. */
5218
5219static boolean
5220swap_out_syms (abfd, sttp, relocatable_p)
5221 bfd *abfd;
5222 struct bfd_strtab_hash **sttp;
5223 int relocatable_p;
5224{
079e9a2f
AM
5225 struct elf_backend_data *bed;
5226 int symcount;
5227 asymbol **syms;
5228 struct bfd_strtab_hash *stt;
5229 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 5230 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f
AM
5231 Elf_Internal_Shdr *symstrtab_hdr;
5232 char *outbound_syms;
9ad5cbcf 5233 char *outbound_shndx;
079e9a2f
AM
5234 int idx;
5235 bfd_size_type amt;
252b5132
RH
5236
5237 if (!elf_map_symbols (abfd))
5238 return false;
5239
c044fabd 5240 /* Dump out the symtabs. */
079e9a2f
AM
5241 stt = _bfd_elf_stringtab_init ();
5242 if (stt == NULL)
5243 return false;
252b5132 5244
079e9a2f
AM
5245 bed = get_elf_backend_data (abfd);
5246 symcount = bfd_get_symcount (abfd);
5247 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5248 symtab_hdr->sh_type = SHT_SYMTAB;
5249 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5250 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5251 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5252 symtab_hdr->sh_addralign = bed->s->file_align;
5253
5254 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5255 symstrtab_hdr->sh_type = SHT_STRTAB;
5256
5257 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5258 outbound_syms = bfd_alloc (abfd, amt);
5259 if (outbound_syms == NULL)
5260 return false;
5261 symtab_hdr->contents = (PTR) outbound_syms;
252b5132 5262
9ad5cbcf
AM
5263 outbound_shndx = NULL;
5264 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5265 if (symtab_shndx_hdr->sh_name != 0)
5266 {
5267 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
1126897b 5268 outbound_shndx = bfd_zalloc (abfd, amt);
9ad5cbcf
AM
5269 if (outbound_shndx == NULL)
5270 return false;
9ad5cbcf
AM
5271 symtab_shndx_hdr->contents = outbound_shndx;
5272 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5273 symtab_shndx_hdr->sh_size = amt;
5274 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5275 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5276 }
5277
079e9a2f
AM
5278 /* now generate the data (for "contents") */
5279 {
5280 /* Fill in zeroth symbol and swap it out. */
5281 Elf_Internal_Sym sym;
5282 sym.st_name = 0;
5283 sym.st_value = 0;
5284 sym.st_size = 0;
5285 sym.st_info = 0;
5286 sym.st_other = 0;
5287 sym.st_shndx = SHN_UNDEF;
9ad5cbcf 5288 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5289 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5290 if (outbound_shndx != NULL)
5291 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5292 }
252b5132 5293
079e9a2f
AM
5294 syms = bfd_get_outsymbols (abfd);
5295 for (idx = 0; idx < symcount; idx++)
252b5132 5296 {
252b5132 5297 Elf_Internal_Sym sym;
079e9a2f
AM
5298 bfd_vma value = syms[idx]->value;
5299 elf_symbol_type *type_ptr;
5300 flagword flags = syms[idx]->flags;
5301 int type;
252b5132 5302
079e9a2f
AM
5303 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5304 {
5305 /* Local section symbols have no name. */
5306 sym.st_name = 0;
5307 }
5308 else
5309 {
5310 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5311 syms[idx]->name,
5312 true, false);
5313 if (sym.st_name == (unsigned long) -1)
5314 return false;
5315 }
252b5132 5316
079e9a2f 5317 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 5318
079e9a2f
AM
5319 if ((flags & BSF_SECTION_SYM) == 0
5320 && bfd_is_com_section (syms[idx]->section))
5321 {
5322 /* ELF common symbols put the alignment into the `value' field,
5323 and the size into the `size' field. This is backwards from
5324 how BFD handles it, so reverse it here. */
5325 sym.st_size = value;
5326 if (type_ptr == NULL
5327 || type_ptr->internal_elf_sym.st_value == 0)
5328 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5329 else
5330 sym.st_value = type_ptr->internal_elf_sym.st_value;
5331 sym.st_shndx = _bfd_elf_section_from_bfd_section
5332 (abfd, syms[idx]->section);
5333 }
5334 else
5335 {
5336 asection *sec = syms[idx]->section;
5337 int shndx;
252b5132 5338
079e9a2f
AM
5339 if (sec->output_section)
5340 {
5341 value += sec->output_offset;
5342 sec = sec->output_section;
5343 }
5344 /* Don't add in the section vma for relocatable output. */
5345 if (! relocatable_p)
5346 value += sec->vma;
5347 sym.st_value = value;
5348 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5349
5350 if (bfd_is_abs_section (sec)
5351 && type_ptr != NULL
5352 && type_ptr->internal_elf_sym.st_shndx != 0)
5353 {
5354 /* This symbol is in a real ELF section which we did
5355 not create as a BFD section. Undo the mapping done
5356 by copy_private_symbol_data. */
5357 shndx = type_ptr->internal_elf_sym.st_shndx;
5358 switch (shndx)
5359 {
5360 case MAP_ONESYMTAB:
5361 shndx = elf_onesymtab (abfd);
5362 break;
5363 case MAP_DYNSYMTAB:
5364 shndx = elf_dynsymtab (abfd);
5365 break;
5366 case MAP_STRTAB:
5367 shndx = elf_tdata (abfd)->strtab_section;
5368 break;
5369 case MAP_SHSTRTAB:
5370 shndx = elf_tdata (abfd)->shstrtab_section;
5371 break;
9ad5cbcf
AM
5372 case MAP_SYM_SHNDX:
5373 shndx = elf_tdata (abfd)->symtab_shndx_section;
5374 break;
079e9a2f
AM
5375 default:
5376 break;
5377 }
5378 }
5379 else
5380 {
5381 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 5382
079e9a2f
AM
5383 if (shndx == -1)
5384 {
5385 asection *sec2;
5386
5387 /* Writing this would be a hell of a lot easier if
5388 we had some decent documentation on bfd, and
5389 knew what to expect of the library, and what to
5390 demand of applications. For example, it
5391 appears that `objcopy' might not set the
5392 section of a symbol to be a section that is
5393 actually in the output file. */
5394 sec2 = bfd_get_section_by_name (abfd, sec->name);
5395 BFD_ASSERT (sec2 != 0);
5396 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5397 BFD_ASSERT (shndx != -1);
5398 }
5399 }
252b5132 5400
079e9a2f
AM
5401 sym.st_shndx = shndx;
5402 }
252b5132 5403
13ae64f3
JJ
5404 if ((flags & BSF_THREAD_LOCAL) != 0)
5405 type = STT_TLS;
5406 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
5407 type = STT_FUNC;
5408 else if ((flags & BSF_OBJECT) != 0)
5409 type = STT_OBJECT;
5410 else
5411 type = STT_NOTYPE;
252b5132 5412
13ae64f3
JJ
5413 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5414 type = STT_TLS;
5415
079e9a2f
AM
5416 /* Processor-specific types */
5417 if (type_ptr != NULL
5418 && bed->elf_backend_get_symbol_type)
5419 type = ((*bed->elf_backend_get_symbol_type)
5420 (&type_ptr->internal_elf_sym, type));
252b5132 5421
079e9a2f
AM
5422 if (flags & BSF_SECTION_SYM)
5423 {
5424 if (flags & BSF_GLOBAL)
5425 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5426 else
5427 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5428 }
5429 else if (bfd_is_com_section (syms[idx]->section))
5430 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5431 else if (bfd_is_und_section (syms[idx]->section))
5432 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5433 ? STB_WEAK
5434 : STB_GLOBAL),
5435 type);
5436 else if (flags & BSF_FILE)
5437 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5438 else
5439 {
5440 int bind = STB_LOCAL;
252b5132 5441
079e9a2f
AM
5442 if (flags & BSF_LOCAL)
5443 bind = STB_LOCAL;
5444 else if (flags & BSF_WEAK)
5445 bind = STB_WEAK;
5446 else if (flags & BSF_GLOBAL)
5447 bind = STB_GLOBAL;
252b5132 5448
079e9a2f
AM
5449 sym.st_info = ELF_ST_INFO (bind, type);
5450 }
252b5132 5451
079e9a2f
AM
5452 if (type_ptr != NULL)
5453 sym.st_other = type_ptr->internal_elf_sym.st_other;
5454 else
5455 sym.st_other = 0;
252b5132 5456
9ad5cbcf 5457 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5458 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5459 if (outbound_shndx != NULL)
5460 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5461 }
252b5132 5462
079e9a2f
AM
5463 *sttp = stt;
5464 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5465 symstrtab_hdr->sh_type = SHT_STRTAB;
252b5132 5466
079e9a2f
AM
5467 symstrtab_hdr->sh_flags = 0;
5468 symstrtab_hdr->sh_addr = 0;
5469 symstrtab_hdr->sh_entsize = 0;
5470 symstrtab_hdr->sh_link = 0;
5471 symstrtab_hdr->sh_info = 0;
5472 symstrtab_hdr->sh_addralign = 1;
252b5132
RH
5473
5474 return true;
5475}
5476
5477/* Return the number of bytes required to hold the symtab vector.
5478
5479 Note that we base it on the count plus 1, since we will null terminate
5480 the vector allocated based on this size. However, the ELF symbol table
5481 always has a dummy entry as symbol #0, so it ends up even. */
5482
5483long
5484_bfd_elf_get_symtab_upper_bound (abfd)
5485 bfd *abfd;
5486{
5487 long symcount;
5488 long symtab_size;
5489 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5490
5491 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5492 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5493 if (symcount > 0)
5494 symtab_size -= sizeof (asymbol *);
252b5132
RH
5495
5496 return symtab_size;
5497}
5498
5499long
5500_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5501 bfd *abfd;
5502{
5503 long symcount;
5504 long symtab_size;
5505 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5506
5507 if (elf_dynsymtab (abfd) == 0)
5508 {
5509 bfd_set_error (bfd_error_invalid_operation);
5510 return -1;
5511 }
5512
5513 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5514 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5515 if (symcount > 0)
5516 symtab_size -= sizeof (asymbol *);
252b5132
RH
5517
5518 return symtab_size;
5519}
5520
5521long
5522_bfd_elf_get_reloc_upper_bound (abfd, asect)
7442e600 5523 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5524 sec_ptr asect;
5525{
5526 return (asect->reloc_count + 1) * sizeof (arelent *);
5527}
5528
5529/* Canonicalize the relocs. */
5530
5531long
5532_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5533 bfd *abfd;
5534 sec_ptr section;
5535 arelent **relptr;
5536 asymbol **symbols;
5537{
5538 arelent *tblptr;
5539 unsigned int i;
dbb410c3 5540 struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5541
dbb410c3 5542 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
252b5132
RH
5543 return -1;
5544
5545 tblptr = section->relocation;
5546 for (i = 0; i < section->reloc_count; i++)
5547 *relptr++ = tblptr++;
5548
5549 *relptr = NULL;
5550
5551 return section->reloc_count;
5552}
5553
5554long
5555_bfd_elf_get_symtab (abfd, alocation)
5556 bfd *abfd;
5557 asymbol **alocation;
5558{
dbb410c3
AM
5559 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5560 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
252b5132
RH
5561
5562 if (symcount >= 0)
5563 bfd_get_symcount (abfd) = symcount;
5564 return symcount;
5565}
5566
5567long
5568_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5569 bfd *abfd;
5570 asymbol **alocation;
5571{
dbb410c3
AM
5572 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5573 return bed->s->slurp_symbol_table (abfd, alocation, true);
252b5132
RH
5574}
5575
5576/* Return the size required for the dynamic reloc entries. Any
5577 section that was actually installed in the BFD, and has type
5578 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5579 considered to be a dynamic reloc section. */
5580
5581long
5582_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5583 bfd *abfd;
5584{
5585 long ret;
5586 asection *s;
5587
5588 if (elf_dynsymtab (abfd) == 0)
5589 {
5590 bfd_set_error (bfd_error_invalid_operation);
5591 return -1;
5592 }
5593
5594 ret = sizeof (arelent *);
5595 for (s = abfd->sections; s != NULL; s = s->next)
5596 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5597 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5598 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5599 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5600 * sizeof (arelent *));
5601
5602 return ret;
5603}
5604
5605/* Canonicalize the dynamic relocation entries. Note that we return
5606 the dynamic relocations as a single block, although they are
5607 actually associated with particular sections; the interface, which
5608 was designed for SunOS style shared libraries, expects that there
5609 is only one set of dynamic relocs. Any section that was actually
5610 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5611 the dynamic symbol table, is considered to be a dynamic reloc
5612 section. */
5613
5614long
5615_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5616 bfd *abfd;
5617 arelent **storage;
5618 asymbol **syms;
5619{
5620 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5621 asection *s;
5622 long ret;
5623
5624 if (elf_dynsymtab (abfd) == 0)
5625 {
5626 bfd_set_error (bfd_error_invalid_operation);
5627 return -1;
5628 }
5629
5630 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5631 ret = 0;
5632 for (s = abfd->sections; s != NULL; s = s->next)
5633 {
5634 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5635 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5636 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5637 {
5638 arelent *p;
5639 long count, i;
5640
5641 if (! (*slurp_relocs) (abfd, s, syms, true))
5642 return -1;
5643 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5644 p = s->relocation;
5645 for (i = 0; i < count; i++)
5646 *storage++ = p++;
5647 ret += count;
5648 }
5649 }
5650
5651 *storage = NULL;
5652
5653 return ret;
5654}
5655\f
5656/* Read in the version information. */
5657
5658boolean
5659_bfd_elf_slurp_version_tables (abfd)
5660 bfd *abfd;
5661{
5662 bfd_byte *contents = NULL;
dc810e39 5663 bfd_size_type amt;
252b5132
RH
5664
5665 if (elf_dynverdef (abfd) != 0)
5666 {
5667 Elf_Internal_Shdr *hdr;
5668 Elf_External_Verdef *everdef;
5669 Elf_Internal_Verdef *iverdef;
f631889e
UD
5670 Elf_Internal_Verdef *iverdefarr;
5671 Elf_Internal_Verdef iverdefmem;
252b5132 5672 unsigned int i;
062e2358 5673 unsigned int maxidx;
252b5132
RH
5674
5675 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5676
252b5132
RH
5677 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5678 if (contents == NULL)
5679 goto error_return;
5680 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5681 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5682 goto error_return;
5683
f631889e
UD
5684 /* We know the number of entries in the section but not the maximum
5685 index. Therefore we have to run through all entries and find
5686 the maximum. */
252b5132 5687 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
5688 maxidx = 0;
5689 for (i = 0; i < hdr->sh_info; ++i)
5690 {
5691 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5692
062e2358
AM
5693 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5694 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e
UD
5695
5696 everdef = ((Elf_External_Verdef *)
5697 ((bfd_byte *) everdef + iverdefmem.vd_next));
5698 }
5699
dc810e39
AM
5700 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5701 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
f631889e
UD
5702 if (elf_tdata (abfd)->verdef == NULL)
5703 goto error_return;
5704
5705 elf_tdata (abfd)->cverdefs = maxidx;
5706
5707 everdef = (Elf_External_Verdef *) contents;
5708 iverdefarr = elf_tdata (abfd)->verdef;
5709 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
5710 {
5711 Elf_External_Verdaux *everdaux;
5712 Elf_Internal_Verdaux *iverdaux;
5713 unsigned int j;
5714
f631889e
UD
5715 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5716
5717 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5718 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
252b5132
RH
5719
5720 iverdef->vd_bfd = abfd;
5721
dc810e39
AM
5722 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5723 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
252b5132
RH
5724 if (iverdef->vd_auxptr == NULL)
5725 goto error_return;
5726
5727 everdaux = ((Elf_External_Verdaux *)
5728 ((bfd_byte *) everdef + iverdef->vd_aux));
5729 iverdaux = iverdef->vd_auxptr;
5730 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5731 {
5732 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5733
5734 iverdaux->vda_nodename =
5735 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5736 iverdaux->vda_name);
5737 if (iverdaux->vda_nodename == NULL)
5738 goto error_return;
5739
5740 if (j + 1 < iverdef->vd_cnt)
5741 iverdaux->vda_nextptr = iverdaux + 1;
5742 else
5743 iverdaux->vda_nextptr = NULL;
5744
5745 everdaux = ((Elf_External_Verdaux *)
5746 ((bfd_byte *) everdaux + iverdaux->vda_next));
5747 }
5748
5749 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5750
5751 if (i + 1 < hdr->sh_info)
5752 iverdef->vd_nextdef = iverdef + 1;
5753 else
5754 iverdef->vd_nextdef = NULL;
5755
5756 everdef = ((Elf_External_Verdef *)
5757 ((bfd_byte *) everdef + iverdef->vd_next));
5758 }
5759
5760 free (contents);
5761 contents = NULL;
5762 }
5763
5764 if (elf_dynverref (abfd) != 0)
5765 {
5766 Elf_Internal_Shdr *hdr;
5767 Elf_External_Verneed *everneed;
5768 Elf_Internal_Verneed *iverneed;
5769 unsigned int i;
5770
5771 hdr = &elf_tdata (abfd)->dynverref_hdr;
5772
dc810e39 5773 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
252b5132 5774 elf_tdata (abfd)->verref =
dc810e39 5775 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
252b5132
RH
5776 if (elf_tdata (abfd)->verref == NULL)
5777 goto error_return;
5778
5779 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5780
5781 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5782 if (contents == NULL)
5783 goto error_return;
5784 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
dc810e39 5785 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
5786 goto error_return;
5787
5788 everneed = (Elf_External_Verneed *) contents;
5789 iverneed = elf_tdata (abfd)->verref;
5790 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5791 {
5792 Elf_External_Vernaux *evernaux;
5793 Elf_Internal_Vernaux *ivernaux;
5794 unsigned int j;
5795
5796 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5797
5798 iverneed->vn_bfd = abfd;
5799
5800 iverneed->vn_filename =
5801 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5802 iverneed->vn_file);
5803 if (iverneed->vn_filename == NULL)
5804 goto error_return;
5805
dc810e39
AM
5806 amt = iverneed->vn_cnt;
5807 amt *= sizeof (Elf_Internal_Vernaux);
5808 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
252b5132
RH
5809
5810 evernaux = ((Elf_External_Vernaux *)
5811 ((bfd_byte *) everneed + iverneed->vn_aux));
5812 ivernaux = iverneed->vn_auxptr;
5813 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5814 {
5815 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5816
5817 ivernaux->vna_nodename =
5818 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5819 ivernaux->vna_name);
5820 if (ivernaux->vna_nodename == NULL)
5821 goto error_return;
5822
5823 if (j + 1 < iverneed->vn_cnt)
5824 ivernaux->vna_nextptr = ivernaux + 1;
5825 else
5826 ivernaux->vna_nextptr = NULL;
5827
5828 evernaux = ((Elf_External_Vernaux *)
5829 ((bfd_byte *) evernaux + ivernaux->vna_next));
5830 }
5831
5832 if (i + 1 < hdr->sh_info)
5833 iverneed->vn_nextref = iverneed + 1;
5834 else
5835 iverneed->vn_nextref = NULL;
5836
5837 everneed = ((Elf_External_Verneed *)
5838 ((bfd_byte *) everneed + iverneed->vn_next));
5839 }
5840
5841 free (contents);
5842 contents = NULL;
5843 }
5844
5845 return true;
5846
5847 error_return:
5848 if (contents == NULL)
5849 free (contents);
5850 return false;
5851}
5852\f
5853asymbol *
5854_bfd_elf_make_empty_symbol (abfd)
5855 bfd *abfd;
5856{
5857 elf_symbol_type *newsym;
dc810e39 5858 bfd_size_type amt = sizeof (elf_symbol_type);
252b5132 5859
dc810e39 5860 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
252b5132
RH
5861 if (!newsym)
5862 return NULL;
5863 else
5864 {
5865 newsym->symbol.the_bfd = abfd;
5866 return &newsym->symbol;
5867 }
5868}
5869
5870void
5871_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
7442e600 5872 bfd *ignore_abfd ATTRIBUTE_UNUSED;
252b5132
RH
5873 asymbol *symbol;
5874 symbol_info *ret;
5875{
5876 bfd_symbol_info (symbol, ret);
5877}
5878
5879/* Return whether a symbol name implies a local symbol. Most targets
5880 use this function for the is_local_label_name entry point, but some
5881 override it. */
5882
5883boolean
5884_bfd_elf_is_local_label_name (abfd, name)
7442e600 5885 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5886 const char *name;
5887{
5888 /* Normal local symbols start with ``.L''. */
5889 if (name[0] == '.' && name[1] == 'L')
5890 return true;
5891
5892 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5893 DWARF debugging symbols starting with ``..''. */
5894 if (name[0] == '.' && name[1] == '.')
5895 return true;
5896
5897 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5898 emitting DWARF debugging output. I suspect this is actually a
5899 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5900 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5901 underscore to be emitted on some ELF targets). For ease of use,
5902 we treat such symbols as local. */
5903 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5904 return true;
5905
5906 return false;
5907}
5908
5909alent *
5910_bfd_elf_get_lineno (ignore_abfd, symbol)
7442e600
ILT
5911 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5912 asymbol *symbol ATTRIBUTE_UNUSED;
252b5132
RH
5913{
5914 abort ();
5915 return NULL;
5916}
5917
5918boolean
5919_bfd_elf_set_arch_mach (abfd, arch, machine)
5920 bfd *abfd;
5921 enum bfd_architecture arch;
5922 unsigned long machine;
5923{
5924 /* If this isn't the right architecture for this backend, and this
5925 isn't the generic backend, fail. */
5926 if (arch != get_elf_backend_data (abfd)->arch
5927 && arch != bfd_arch_unknown
5928 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5929 return false;
5930
5931 return bfd_default_set_arch_mach (abfd, arch, machine);
5932}
5933
d1fad7c6
NC
5934/* Find the function to a particular section and offset,
5935 for error reporting. */
252b5132 5936
d1fad7c6
NC
5937static boolean
5938elf_find_function (abfd, section, symbols, offset,
4e8a9624 5939 filename_ptr, functionname_ptr)
d1fad7c6 5940 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
5941 asection *section;
5942 asymbol **symbols;
5943 bfd_vma offset;
4e8a9624
AM
5944 const char **filename_ptr;
5945 const char **functionname_ptr;
252b5132 5946{
252b5132
RH
5947 const char *filename;
5948 asymbol *func;
5949 bfd_vma low_func;
5950 asymbol **p;
5951
252b5132
RH
5952 filename = NULL;
5953 func = NULL;
5954 low_func = 0;
5955
5956 for (p = symbols; *p != NULL; p++)
5957 {
5958 elf_symbol_type *q;
5959
5960 q = (elf_symbol_type *) *p;
5961
5962 if (bfd_get_section (&q->symbol) != section)
5963 continue;
5964
5965 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5966 {
5967 default:
5968 break;
5969 case STT_FILE:
5970 filename = bfd_asymbol_name (&q->symbol);
5971 break;
5972 case STT_NOTYPE:
5973 case STT_FUNC:
5974 if (q->symbol.section == section
5975 && q->symbol.value >= low_func
5976 && q->symbol.value <= offset)
5977 {
5978 func = (asymbol *) q;
5979 low_func = q->symbol.value;
5980 }
5981 break;
5982 }
5983 }
5984
5985 if (func == NULL)
5986 return false;
5987
d1fad7c6
NC
5988 if (filename_ptr)
5989 *filename_ptr = filename;
5990 if (functionname_ptr)
5991 *functionname_ptr = bfd_asymbol_name (func);
5992
5993 return true;
5994}
5995
5996/* Find the nearest line to a particular section and offset,
5997 for error reporting. */
5998
5999boolean
6000_bfd_elf_find_nearest_line (abfd, section, symbols, offset,
4e8a9624 6001 filename_ptr, functionname_ptr, line_ptr)
d1fad7c6
NC
6002 bfd *abfd;
6003 asection *section;
6004 asymbol **symbols;
6005 bfd_vma offset;
4e8a9624
AM
6006 const char **filename_ptr;
6007 const char **functionname_ptr;
d1fad7c6
NC
6008 unsigned int *line_ptr;
6009{
6010 boolean found;
6011
6012 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
6013 filename_ptr, functionname_ptr,
6014 line_ptr))
d1fad7c6
NC
6015 {
6016 if (!*functionname_ptr)
4e8a9624
AM
6017 elf_find_function (abfd, section, symbols, offset,
6018 *filename_ptr ? NULL : filename_ptr,
6019 functionname_ptr);
6020
d1fad7c6
NC
6021 return true;
6022 }
6023
6024 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
6025 filename_ptr, functionname_ptr,
6026 line_ptr, 0,
6027 &elf_tdata (abfd)->dwarf2_find_line_info))
d1fad7c6
NC
6028 {
6029 if (!*functionname_ptr)
4e8a9624
AM
6030 elf_find_function (abfd, section, symbols, offset,
6031 *filename_ptr ? NULL : filename_ptr,
6032 functionname_ptr);
6033
d1fad7c6
NC
6034 return true;
6035 }
6036
6037 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
6038 &found, filename_ptr,
6039 functionname_ptr, line_ptr,
6040 &elf_tdata (abfd)->line_info))
d1fad7c6
NC
6041 return false;
6042 if (found)
6043 return true;
6044
6045 if (symbols == NULL)
6046 return false;
6047
6048 if (! elf_find_function (abfd, section, symbols, offset,
4e8a9624 6049 filename_ptr, functionname_ptr))
d1fad7c6
NC
6050 return false;
6051
252b5132
RH
6052 *line_ptr = 0;
6053 return true;
6054}
6055
6056int
6057_bfd_elf_sizeof_headers (abfd, reloc)
6058 bfd *abfd;
6059 boolean reloc;
6060{
6061 int ret;
6062
6063 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6064 if (! reloc)
6065 ret += get_program_header_size (abfd);
6066 return ret;
6067}
6068
6069boolean
6070_bfd_elf_set_section_contents (abfd, section, location, offset, count)
6071 bfd *abfd;
6072 sec_ptr section;
6073 PTR location;
6074 file_ptr offset;
6075 bfd_size_type count;
6076{
6077 Elf_Internal_Shdr *hdr;
dc810e39 6078 bfd_signed_vma pos;
252b5132
RH
6079
6080 if (! abfd->output_has_begun
82e51918
AM
6081 && ! (_bfd_elf_compute_section_file_positions
6082 (abfd, (struct bfd_link_info *) NULL)))
252b5132
RH
6083 return false;
6084
6085 hdr = &elf_section_data (section)->this_hdr;
dc810e39
AM
6086 pos = hdr->sh_offset + offset;
6087 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6088 || bfd_bwrite (location, count, abfd) != count)
252b5132
RH
6089 return false;
6090
6091 return true;
6092}
6093
6094void
6095_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
7442e600
ILT
6096 bfd *abfd ATTRIBUTE_UNUSED;
6097 arelent *cache_ptr ATTRIBUTE_UNUSED;
6098 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
252b5132
RH
6099{
6100 abort ();
6101}
6102
6103#if 0
6104void
6105_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
6106 bfd *abfd;
6107 arelent *cache_ptr;
6108 Elf_Internal_Rel *dst;
6109{
6110 abort ();
6111}
6112#endif
6113
6114/* Try to convert a non-ELF reloc into an ELF one. */
6115
6116boolean
6117_bfd_elf_validate_reloc (abfd, areloc)
6118 bfd *abfd;
6119 arelent *areloc;
6120{
c044fabd 6121 /* Check whether we really have an ELF howto. */
252b5132
RH
6122
6123 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6124 {
6125 bfd_reloc_code_real_type code;
6126 reloc_howto_type *howto;
6127
6128 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 6129 equivalent ELF reloc. */
252b5132
RH
6130
6131 if (areloc->howto->pc_relative)
6132 {
6133 switch (areloc->howto->bitsize)
6134 {
6135 case 8:
6136 code = BFD_RELOC_8_PCREL;
6137 break;
6138 case 12:
6139 code = BFD_RELOC_12_PCREL;
6140 break;
6141 case 16:
6142 code = BFD_RELOC_16_PCREL;
6143 break;
6144 case 24:
6145 code = BFD_RELOC_24_PCREL;
6146 break;
6147 case 32:
6148 code = BFD_RELOC_32_PCREL;
6149 break;
6150 case 64:
6151 code = BFD_RELOC_64_PCREL;
6152 break;
6153 default:
6154 goto fail;
6155 }
6156
6157 howto = bfd_reloc_type_lookup (abfd, code);
6158
6159 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6160 {
6161 if (howto->pcrel_offset)
6162 areloc->addend += areloc->address;
6163 else
6164 areloc->addend -= areloc->address; /* addend is unsigned!! */
6165 }
6166 }
6167 else
6168 {
6169 switch (areloc->howto->bitsize)
6170 {
6171 case 8:
6172 code = BFD_RELOC_8;
6173 break;
6174 case 14:
6175 code = BFD_RELOC_14;
6176 break;
6177 case 16:
6178 code = BFD_RELOC_16;
6179 break;
6180 case 26:
6181 code = BFD_RELOC_26;
6182 break;
6183 case 32:
6184 code = BFD_RELOC_32;
6185 break;
6186 case 64:
6187 code = BFD_RELOC_64;
6188 break;
6189 default:
6190 goto fail;
6191 }
6192
6193 howto = bfd_reloc_type_lookup (abfd, code);
6194 }
6195
6196 if (howto)
6197 areloc->howto = howto;
6198 else
6199 goto fail;
6200 }
6201
6202 return true;
6203
6204 fail:
6205 (*_bfd_error_handler)
6206 (_("%s: unsupported relocation type %s"),
8f615d07 6207 bfd_archive_filename (abfd), areloc->howto->name);
252b5132
RH
6208 bfd_set_error (bfd_error_bad_value);
6209 return false;
6210}
6211
6212boolean
6213_bfd_elf_close_and_cleanup (abfd)
6214 bfd *abfd;
6215{
6216 if (bfd_get_format (abfd) == bfd_object)
6217 {
6218 if (elf_shstrtab (abfd) != NULL)
2b0f7ef9 6219 _bfd_elf_strtab_free (elf_shstrtab (abfd));
252b5132
RH
6220 }
6221
6222 return _bfd_generic_close_and_cleanup (abfd);
6223}
6224
6225/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6226 in the relocation's offset. Thus we cannot allow any sort of sanity
6227 range-checking to interfere. There is nothing else to do in processing
6228 this reloc. */
6229
6230bfd_reloc_status_type
6231_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
7442e600
ILT
6232 bfd *abfd ATTRIBUTE_UNUSED;
6233 arelent *re ATTRIBUTE_UNUSED;
6234 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6235 PTR data ATTRIBUTE_UNUSED;
6236 asection *is ATTRIBUTE_UNUSED;
6237 bfd *obfd ATTRIBUTE_UNUSED;
6238 char **errmsg ATTRIBUTE_UNUSED;
252b5132
RH
6239{
6240 return bfd_reloc_ok;
6241}
252b5132
RH
6242\f
6243/* Elf core file support. Much of this only works on native
6244 toolchains, since we rely on knowing the
6245 machine-dependent procfs structure in order to pick
c044fabd 6246 out details about the corefile. */
252b5132
RH
6247
6248#ifdef HAVE_SYS_PROCFS_H
6249# include <sys/procfs.h>
6250#endif
6251
c044fabd 6252/* FIXME: this is kinda wrong, but it's what gdb wants. */
252b5132
RH
6253
6254static int
6255elfcore_make_pid (abfd)
c044fabd 6256 bfd *abfd;
252b5132
RH
6257{
6258 return ((elf_tdata (abfd)->core_lwpid << 16)
6259 + (elf_tdata (abfd)->core_pid));
6260}
6261
252b5132
RH
6262/* If there isn't a section called NAME, make one, using
6263 data from SECT. Note, this function will generate a
6264 reference to NAME, so you shouldn't deallocate or
c044fabd 6265 overwrite it. */
252b5132
RH
6266
6267static boolean
6268elfcore_maybe_make_sect (abfd, name, sect)
c044fabd
KH
6269 bfd *abfd;
6270 char *name;
6271 asection *sect;
252b5132 6272{
c044fabd 6273 asection *sect2;
252b5132
RH
6274
6275 if (bfd_get_section_by_name (abfd, name) != NULL)
6276 return true;
6277
6278 sect2 = bfd_make_section (abfd, name);
6279 if (sect2 == NULL)
6280 return false;
6281
6282 sect2->_raw_size = sect->_raw_size;
6283 sect2->filepos = sect->filepos;
6284 sect2->flags = sect->flags;
6285 sect2->alignment_power = sect->alignment_power;
6286 return true;
6287}
6288
bb0082d6
AM
6289/* Create a pseudosection containing SIZE bytes at FILEPOS. This
6290 actually creates up to two pseudosections:
6291 - For the single-threaded case, a section named NAME, unless
6292 such a section already exists.
6293 - For the multi-threaded case, a section named "NAME/PID", where
6294 PID is elfcore_make_pid (abfd).
6295 Both pseudosections have identical contents. */
6296boolean
6297_bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6298 bfd *abfd;
6299 char *name;
dc810e39
AM
6300 size_t size;
6301 ufile_ptr filepos;
bb0082d6
AM
6302{
6303 char buf[100];
6304 char *threaded_name;
d4c88bbb 6305 size_t len;
bb0082d6
AM
6306 asection *sect;
6307
6308 /* Build the section name. */
6309
6310 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb
AM
6311 len = strlen (buf) + 1;
6312 threaded_name = bfd_alloc (abfd, (bfd_size_type) len);
bb0082d6
AM
6313 if (threaded_name == NULL)
6314 return false;
d4c88bbb 6315 memcpy (threaded_name, buf, len);
bb0082d6
AM
6316
6317 sect = bfd_make_section (abfd, threaded_name);
6318 if (sect == NULL)
6319 return false;
6320 sect->_raw_size = size;
6321 sect->filepos = filepos;
6322 sect->flags = SEC_HAS_CONTENTS;
6323 sect->alignment_power = 2;
6324
936e320b 6325 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
6326}
6327
252b5132 6328/* prstatus_t exists on:
4a938328 6329 solaris 2.5+
252b5132
RH
6330 linux 2.[01] + glibc
6331 unixware 4.2
6332*/
6333
6334#if defined (HAVE_PRSTATUS_T)
a7b97311
AM
6335static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
6336
252b5132
RH
6337static boolean
6338elfcore_grok_prstatus (abfd, note)
c044fabd
KH
6339 bfd *abfd;
6340 Elf_Internal_Note *note;
252b5132 6341{
dc810e39 6342 size_t raw_size;
7ee38065 6343 int offset;
252b5132 6344
4a938328
MS
6345 if (note->descsz == sizeof (prstatus_t))
6346 {
6347 prstatus_t prstat;
252b5132 6348
e0ebfc61 6349 raw_size = sizeof (prstat.pr_reg);
7ee38065 6350 offset = offsetof (prstatus_t, pr_reg);
4a938328 6351 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 6352
fa49d224
NC
6353 /* Do not overwrite the core signal if it
6354 has already been set by another thread. */
6355 if (elf_tdata (abfd)->core_signal == 0)
6356 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328 6357 elf_tdata (abfd)->core_pid = prstat.pr_pid;
252b5132 6358
4a938328
MS
6359 /* pr_who exists on:
6360 solaris 2.5+
6361 unixware 4.2
6362 pr_who doesn't exist on:
6363 linux 2.[01]
6364 */
252b5132 6365#if defined (HAVE_PRSTATUS_T_PR_WHO)
4a938328 6366 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
252b5132 6367#endif
4a938328 6368 }
7ee38065 6369#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
6370 else if (note->descsz == sizeof (prstatus32_t))
6371 {
6372 /* 64-bit host, 32-bit corefile */
6373 prstatus32_t prstat;
6374
e0ebfc61 6375 raw_size = sizeof (prstat.pr_reg);
7ee38065 6376 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
6377 memcpy (&prstat, note->descdata, sizeof (prstat));
6378
fa49d224
NC
6379 /* Do not overwrite the core signal if it
6380 has already been set by another thread. */
6381 if (elf_tdata (abfd)->core_signal == 0)
6382 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328
MS
6383 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6384
6385 /* pr_who exists on:
6386 solaris 2.5+
6387 unixware 4.2
6388 pr_who doesn't exist on:
6389 linux 2.[01]
6390 */
7ee38065 6391#if defined (HAVE_PRSTATUS32_T_PR_WHO)
4a938328
MS
6392 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6393#endif
6394 }
7ee38065 6395#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
6396 else
6397 {
6398 /* Fail - we don't know how to handle any other
6399 note size (ie. data object type). */
6400 return true;
6401 }
252b5132 6402
bb0082d6 6403 /* Make a ".reg/999" section and a ".reg" section. */
936e320b
AM
6404 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6405 raw_size, note->descpos + offset);
252b5132
RH
6406}
6407#endif /* defined (HAVE_PRSTATUS_T) */
6408
bb0082d6 6409/* Create a pseudosection containing the exact contents of NOTE. */
252b5132 6410static boolean
ff08c6bb 6411elfcore_make_note_pseudosection (abfd, name, note)
c044fabd 6412 bfd *abfd;
ff08c6bb 6413 char *name;
c044fabd 6414 Elf_Internal_Note *note;
252b5132 6415{
936e320b
AM
6416 return _bfd_elfcore_make_pseudosection (abfd, name,
6417 note->descsz, note->descpos);
252b5132
RH
6418}
6419
ff08c6bb
JB
6420/* There isn't a consistent prfpregset_t across platforms,
6421 but it doesn't matter, because we don't have to pick this
c044fabd
KH
6422 data structure apart. */
6423
ff08c6bb
JB
6424static boolean
6425elfcore_grok_prfpreg (abfd, note)
c044fabd
KH
6426 bfd *abfd;
6427 Elf_Internal_Note *note;
ff08c6bb
JB
6428{
6429 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6430}
6431
ff08c6bb
JB
6432/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6433 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6434 literally. */
c044fabd 6435
ff08c6bb
JB
6436static boolean
6437elfcore_grok_prxfpreg (abfd, note)
c044fabd
KH
6438 bfd *abfd;
6439 Elf_Internal_Note *note;
ff08c6bb
JB
6440{
6441 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6442}
6443
252b5132 6444#if defined (HAVE_PRPSINFO_T)
4a938328 6445typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 6446#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6447typedef prpsinfo32_t elfcore_psinfo32_t;
6448#endif
252b5132
RH
6449#endif
6450
6451#if defined (HAVE_PSINFO_T)
4a938328 6452typedef psinfo_t elfcore_psinfo_t;
7ee38065 6453#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6454typedef psinfo32_t elfcore_psinfo32_t;
6455#endif
252b5132
RH
6456#endif
6457
252b5132
RH
6458/* return a malloc'ed copy of a string at START which is at
6459 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 6460 the copy will always have a terminating '\0'. */
252b5132 6461
936e320b 6462char *
bb0082d6 6463_bfd_elfcore_strndup (abfd, start, max)
c044fabd
KH
6464 bfd *abfd;
6465 char *start;
dc810e39 6466 size_t max;
252b5132 6467{
dc810e39 6468 char *dups;
c044fabd 6469 char *end = memchr (start, '\0', max);
dc810e39 6470 size_t len;
252b5132
RH
6471
6472 if (end == NULL)
6473 len = max;
6474 else
6475 len = end - start;
6476
dc810e39
AM
6477 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6478 if (dups == NULL)
252b5132
RH
6479 return NULL;
6480
dc810e39
AM
6481 memcpy (dups, start, len);
6482 dups[len] = '\0';
252b5132 6483
dc810e39 6484 return dups;
252b5132
RH
6485}
6486
bb0082d6 6487#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
a7b97311 6488static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
bb0082d6 6489
252b5132
RH
6490static boolean
6491elfcore_grok_psinfo (abfd, note)
c044fabd
KH
6492 bfd *abfd;
6493 Elf_Internal_Note *note;
252b5132 6494{
4a938328
MS
6495 if (note->descsz == sizeof (elfcore_psinfo_t))
6496 {
6497 elfcore_psinfo_t psinfo;
252b5132 6498
7ee38065 6499 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6500
4a938328 6501 elf_tdata (abfd)->core_program
936e320b
AM
6502 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6503 sizeof (psinfo.pr_fname));
252b5132 6504
4a938328 6505 elf_tdata (abfd)->core_command
936e320b
AM
6506 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6507 sizeof (psinfo.pr_psargs));
4a938328 6508 }
7ee38065 6509#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
6510 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6511 {
6512 /* 64-bit host, 32-bit corefile */
6513 elfcore_psinfo32_t psinfo;
6514
7ee38065 6515 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6516
4a938328 6517 elf_tdata (abfd)->core_program
936e320b
AM
6518 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6519 sizeof (psinfo.pr_fname));
4a938328
MS
6520
6521 elf_tdata (abfd)->core_command
936e320b
AM
6522 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6523 sizeof (psinfo.pr_psargs));
4a938328
MS
6524 }
6525#endif
6526
6527 else
6528 {
6529 /* Fail - we don't know how to handle any other
6530 note size (ie. data object type). */
6531 return true;
6532 }
252b5132
RH
6533
6534 /* Note that for some reason, a spurious space is tacked
6535 onto the end of the args in some (at least one anyway)
c044fabd 6536 implementations, so strip it off if it exists. */
252b5132
RH
6537
6538 {
c044fabd 6539 char *command = elf_tdata (abfd)->core_command;
252b5132
RH
6540 int n = strlen (command);
6541
6542 if (0 < n && command[n - 1] == ' ')
6543 command[n - 1] = '\0';
6544 }
6545
6546 return true;
6547}
6548#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6549
252b5132 6550#if defined (HAVE_PSTATUS_T)
51316059
MS
6551static boolean elfcore_grok_pstatus PARAMS ((bfd *, Elf_Internal_Note *));
6552
252b5132
RH
6553static boolean
6554elfcore_grok_pstatus (abfd, note)
c044fabd
KH
6555 bfd *abfd;
6556 Elf_Internal_Note *note;
252b5132 6557{
f572a39d
AM
6558 if (note->descsz == sizeof (pstatus_t)
6559#if defined (HAVE_PXSTATUS_T)
6560 || note->descsz == sizeof (pxstatus_t)
6561#endif
6562 )
4a938328
MS
6563 {
6564 pstatus_t pstat;
252b5132 6565
4a938328 6566 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6567
4a938328
MS
6568 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6569 }
7ee38065 6570#if defined (HAVE_PSTATUS32_T)
4a938328
MS
6571 else if (note->descsz == sizeof (pstatus32_t))
6572 {
6573 /* 64-bit host, 32-bit corefile */
6574 pstatus32_t pstat;
252b5132 6575
4a938328 6576 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6577
4a938328
MS
6578 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6579 }
6580#endif
252b5132
RH
6581 /* Could grab some more details from the "representative"
6582 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 6583 NT_LWPSTATUS note, presumably. */
252b5132
RH
6584
6585 return true;
6586}
6587#endif /* defined (HAVE_PSTATUS_T) */
6588
252b5132 6589#if defined (HAVE_LWPSTATUS_T)
51316059
MS
6590static boolean elfcore_grok_lwpstatus PARAMS ((bfd *, Elf_Internal_Note *));
6591
252b5132
RH
6592static boolean
6593elfcore_grok_lwpstatus (abfd, note)
c044fabd
KH
6594 bfd *abfd;
6595 Elf_Internal_Note *note;
252b5132
RH
6596{
6597 lwpstatus_t lwpstat;
6598 char buf[100];
c044fabd 6599 char *name;
d4c88bbb 6600 size_t len;
c044fabd 6601 asection *sect;
252b5132 6602
f572a39d
AM
6603 if (note->descsz != sizeof (lwpstat)
6604#if defined (HAVE_LWPXSTATUS_T)
6605 && note->descsz != sizeof (lwpxstatus_t)
6606#endif
6607 )
252b5132
RH
6608 return true;
6609
6610 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6611
6612 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6613 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6614
c044fabd 6615 /* Make a ".reg/999" section. */
252b5132
RH
6616
6617 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb
AM
6618 len = strlen (buf) + 1;
6619 name = bfd_alloc (abfd, (bfd_size_type) len);
252b5132
RH
6620 if (name == NULL)
6621 return false;
d4c88bbb 6622 memcpy (name, buf, len);
252b5132
RH
6623
6624 sect = bfd_make_section (abfd, name);
6625 if (sect == NULL)
6626 return false;
6627
6628#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6629 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6630 sect->filepos = note->descpos
6631 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6632#endif
6633
6634#if defined (HAVE_LWPSTATUS_T_PR_REG)
6635 sect->_raw_size = sizeof (lwpstat.pr_reg);
6636 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6637#endif
6638
6639 sect->flags = SEC_HAS_CONTENTS;
6640 sect->alignment_power = 2;
6641
6642 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6643 return false;
6644
6645 /* Make a ".reg2/999" section */
6646
6647 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb
AM
6648 len = strlen (buf) + 1;
6649 name = bfd_alloc (abfd, (bfd_size_type) len);
252b5132
RH
6650 if (name == NULL)
6651 return false;
d4c88bbb 6652 memcpy (name, buf, len);
252b5132
RH
6653
6654 sect = bfd_make_section (abfd, name);
6655 if (sect == NULL)
6656 return false;
6657
6658#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6659 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6660 sect->filepos = note->descpos
6661 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6662#endif
6663
6664#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6665 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6666 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6667#endif
6668
6669 sect->flags = SEC_HAS_CONTENTS;
6670 sect->alignment_power = 2;
6671
936e320b 6672 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
6673}
6674#endif /* defined (HAVE_LWPSTATUS_T) */
6675
16e9c715
NC
6676#if defined (HAVE_WIN32_PSTATUS_T)
6677static boolean
6678elfcore_grok_win32pstatus (abfd, note)
c044fabd
KH
6679 bfd *abfd;
6680 Elf_Internal_Note *note;
16e9c715
NC
6681{
6682 char buf[30];
c044fabd 6683 char *name;
d4c88bbb 6684 size_t len;
c044fabd 6685 asection *sect;
16e9c715
NC
6686 win32_pstatus_t pstatus;
6687
6688 if (note->descsz < sizeof (pstatus))
6689 return true;
6690
e8eab623 6691 memcpy (&pstatus, note->descdata, sizeof (pstatus));
c044fabd
KH
6692
6693 switch (pstatus.data_type)
16e9c715
NC
6694 {
6695 case NOTE_INFO_PROCESS:
6696 /* FIXME: need to add ->core_command. */
6697 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6698 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
c044fabd 6699 break;
16e9c715
NC
6700
6701 case NOTE_INFO_THREAD:
6702 /* Make a ".reg/999" section. */
6703 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
c044fabd 6704
d4c88bbb
AM
6705 len = strlen (buf) + 1;
6706 name = bfd_alloc (abfd, (bfd_size_type) len);
16e9c715 6707 if (name == NULL)
c044fabd
KH
6708 return false;
6709
d4c88bbb 6710 memcpy (name, buf, len);
16e9c715
NC
6711
6712 sect = bfd_make_section (abfd, name);
6713 if (sect == NULL)
c044fabd
KH
6714 return false;
6715
16e9c715 6716 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
079e9a2f
AM
6717 sect->filepos = (note->descpos
6718 + offsetof (struct win32_pstatus,
6719 data.thread_info.thread_context));
16e9c715
NC
6720 sect->flags = SEC_HAS_CONTENTS;
6721 sect->alignment_power = 2;
6722
6723 if (pstatus.data.thread_info.is_active_thread)
6724 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6725 return false;
6726 break;
6727
6728 case NOTE_INFO_MODULE:
6729 /* Make a ".module/xxxxxxxx" section. */
c044fabd
KH
6730 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6731
d4c88bbb
AM
6732 len = strlen (buf) + 1;
6733 name = bfd_alloc (abfd, (bfd_size_type) len);
16e9c715
NC
6734 if (name == NULL)
6735 return false;
c044fabd 6736
d4c88bbb 6737 memcpy (name, buf, len);
252b5132 6738
16e9c715 6739 sect = bfd_make_section (abfd, name);
c044fabd 6740
16e9c715
NC
6741 if (sect == NULL)
6742 return false;
c044fabd 6743
16e9c715
NC
6744 sect->_raw_size = note->descsz;
6745 sect->filepos = note->descpos;
6746 sect->flags = SEC_HAS_CONTENTS;
6747 sect->alignment_power = 2;
6748 break;
6749
6750 default:
6751 return true;
6752 }
6753
6754 return true;
6755}
6756#endif /* HAVE_WIN32_PSTATUS_T */
252b5132
RH
6757
6758static boolean
6759elfcore_grok_note (abfd, note)
c044fabd
KH
6760 bfd *abfd;
6761 Elf_Internal_Note *note;
252b5132 6762{
bb0082d6
AM
6763 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6764
252b5132
RH
6765 switch (note->type)
6766 {
6767 default:
6768 return true;
6769
252b5132 6770 case NT_PRSTATUS:
bb0082d6
AM
6771 if (bed->elf_backend_grok_prstatus)
6772 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6773 return true;
6774#if defined (HAVE_PRSTATUS_T)
252b5132 6775 return elfcore_grok_prstatus (abfd, note);
bb0082d6
AM
6776#else
6777 return true;
252b5132
RH
6778#endif
6779
6780#if defined (HAVE_PSTATUS_T)
6781 case NT_PSTATUS:
6782 return elfcore_grok_pstatus (abfd, note);
6783#endif
6784
6785#if defined (HAVE_LWPSTATUS_T)
6786 case NT_LWPSTATUS:
6787 return elfcore_grok_lwpstatus (abfd, note);
6788#endif
6789
6790 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6791 return elfcore_grok_prfpreg (abfd, note);
6792
16e9c715 6793#if defined (HAVE_WIN32_PSTATUS_T)
c044fabd 6794 case NT_WIN32PSTATUS:
16e9c715
NC
6795 return elfcore_grok_win32pstatus (abfd, note);
6796#endif
6797
c044fabd 6798 case NT_PRXFPREG: /* Linux SSE extension */
ff08c6bb
JB
6799 if (note->namesz == 5
6800 && ! strcmp (note->namedata, "LINUX"))
6801 return elfcore_grok_prxfpreg (abfd, note);
6802 else
6803 return true;
6804
252b5132
RH
6805 case NT_PRPSINFO:
6806 case NT_PSINFO:
bb0082d6
AM
6807 if (bed->elf_backend_grok_psinfo)
6808 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6809 return true;
6810#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 6811 return elfcore_grok_psinfo (abfd, note);
bb0082d6
AM
6812#else
6813 return true;
252b5132
RH
6814#endif
6815 }
6816}
6817
50b2bdb7
AM
6818static boolean
6819elfcore_netbsd_get_lwpid (note, lwpidp)
6820 Elf_Internal_Note *note;
6821 int *lwpidp;
6822{
6823 char *cp;
6824
6825 cp = strchr (note->namedata, '@');
6826 if (cp != NULL)
6827 {
d2b64500 6828 *lwpidp = atoi(cp + 1);
50b2bdb7
AM
6829 return true;
6830 }
6831 return false;
6832}
6833
6834static boolean
6835elfcore_grok_netbsd_procinfo (abfd, note)
6836 bfd *abfd;
6837 Elf_Internal_Note *note;
6838{
6839
6840 /* Signal number at offset 0x08. */
6841 elf_tdata (abfd)->core_signal
6842 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6843
6844 /* Process ID at offset 0x50. */
6845 elf_tdata (abfd)->core_pid
6846 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6847
6848 /* Command name at 0x7c (max 32 bytes, including nul). */
6849 elf_tdata (abfd)->core_command
6850 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6851
6852 return true;
6853}
6854
6855static boolean
6856elfcore_grok_netbsd_note (abfd, note)
6857 bfd *abfd;
6858 Elf_Internal_Note *note;
6859{
6860 int lwp;
6861
6862 if (elfcore_netbsd_get_lwpid (note, &lwp))
6863 elf_tdata (abfd)->core_lwpid = lwp;
6864
b4db1224 6865 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
6866 {
6867 /* NetBSD-specific core "procinfo". Note that we expect to
6868 find this note before any of the others, which is fine,
6869 since the kernel writes this note out first when it
6870 creates a core file. */
47d9a591 6871
50b2bdb7
AM
6872 return elfcore_grok_netbsd_procinfo (abfd, note);
6873 }
6874
b4db1224
JT
6875 /* As of Jan 2002 there are no other machine-independent notes
6876 defined for NetBSD core files. If the note type is less
6877 than the start of the machine-dependent note types, we don't
6878 understand it. */
47d9a591 6879
b4db1224 6880 if (note->type < NT_NETBSDCORE_FIRSTMACH)
50b2bdb7
AM
6881 return true;
6882
6883
6884 switch (bfd_get_arch (abfd))
6885 {
6886 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6887 PT_GETFPREGS == mach+2. */
6888
6889 case bfd_arch_alpha:
6890 case bfd_arch_sparc:
6891 switch (note->type)
6892 {
b4db1224 6893 case NT_NETBSDCORE_FIRSTMACH+0:
50b2bdb7
AM
6894 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6895
b4db1224 6896 case NT_NETBSDCORE_FIRSTMACH+2:
50b2bdb7
AM
6897 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6898
6899 default:
6900 return true;
6901 }
6902
6903 /* On all other arch's, PT_GETREGS == mach+1 and
6904 PT_GETFPREGS == mach+3. */
6905
6906 default:
6907 switch (note->type)
6908 {
b4db1224 6909 case NT_NETBSDCORE_FIRSTMACH+1:
50b2bdb7
AM
6910 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6911
b4db1224 6912 case NT_NETBSDCORE_FIRSTMACH+3:
50b2bdb7
AM
6913 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6914
6915 default:
6916 return true;
6917 }
6918 }
6919 /* NOTREACHED */
6920}
6921
7c76fa91
MS
6922/* Function: elfcore_write_note
6923
47d9a591 6924 Inputs:
7c76fa91
MS
6925 buffer to hold note
6926 name of note
6927 type of note
6928 data for note
6929 size of data for note
6930
6931 Return:
6932 End of buffer containing note. */
6933
6934char *
6935elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6936 bfd *abfd;
6937 char *buf;
6938 int *bufsiz;
d4c88bbb 6939 const char *name;
7c76fa91 6940 int type;
d4c88bbb 6941 const PTR input;
7c76fa91
MS
6942 int size;
6943{
6944 Elf_External_Note *xnp;
d4c88bbb
AM
6945 size_t namesz;
6946 size_t pad;
6947 size_t newspace;
7c76fa91
MS
6948 char *p, *dest;
6949
d4c88bbb
AM
6950 namesz = 0;
6951 pad = 0;
6952 if (name != NULL)
6953 {
6954 struct elf_backend_data *bed;
6955
6956 namesz = strlen (name) + 1;
6957 bed = get_elf_backend_data (abfd);
6958 pad = -namesz & (bed->s->file_align - 1);
6959 }
6960
6961 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;
6962
7c76fa91
MS
6963 p = realloc (buf, *bufsiz + newspace);
6964 dest = p + *bufsiz;
6965 *bufsiz += newspace;
6966 xnp = (Elf_External_Note *) dest;
6967 H_PUT_32 (abfd, namesz, xnp->namesz);
6968 H_PUT_32 (abfd, size, xnp->descsz);
6969 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
6970 dest = xnp->name;
6971 if (name != NULL)
6972 {
6973 memcpy (dest, name, namesz);
6974 dest += namesz;
6975 while (pad != 0)
6976 {
6977 *dest++ = '\0';
6978 --pad;
6979 }
6980 }
6981 memcpy (dest, input, size);
7c76fa91
MS
6982 return p;
6983}
6984
6985#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6986char *
6987elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6988 bfd *abfd;
6989 char *buf;
6990 int *bufsiz;
47d9a591 6991 const char *fname;
d4c88bbb 6992 const char *psargs;
7c76fa91
MS
6993{
6994 int note_type;
6995 char *note_name = "CORE";
6996
6997#if defined (HAVE_PSINFO_T)
6998 psinfo_t data;
6999 note_type = NT_PSINFO;
7000#else
7001 prpsinfo_t data;
7002 note_type = NT_PRPSINFO;
7003#endif
7004
7005 memset (&data, 0, sizeof (data));
7006 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7007 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
47d9a591 7008 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7009 note_name, note_type, &data, sizeof (data));
7010}
7011#endif /* PSINFO_T or PRPSINFO_T */
7012
7013#if defined (HAVE_PRSTATUS_T)
7014char *
7015elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7016 bfd *abfd;
7017 char *buf;
7018 int *bufsiz;
b87011e9 7019 long pid;
7c76fa91 7020 int cursig;
d4c88bbb 7021 const PTR gregs;
7c76fa91
MS
7022{
7023 prstatus_t prstat;
7024 char *note_name = "CORE";
7025
7026 memset (&prstat, 0, sizeof (prstat));
7027 prstat.pr_pid = pid;
7028 prstat.pr_cursig = cursig;
c106e334 7029 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
47d9a591 7030 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7031 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7032}
7033#endif /* HAVE_PRSTATUS_T */
7034
51316059
MS
7035#if defined (HAVE_LWPSTATUS_T)
7036char *
7037elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7038 bfd *abfd;
7039 char *buf;
7040 int *bufsiz;
7041 long pid;
7042 int cursig;
d4c88bbb 7043 const PTR gregs;
51316059
MS
7044{
7045 lwpstatus_t lwpstat;
7046 char *note_name = "CORE";
7047
7048 memset (&lwpstat, 0, sizeof (lwpstat));
7049 lwpstat.pr_lwpid = pid >> 16;
7050 lwpstat.pr_cursig = cursig;
7051#if defined (HAVE_LWPSTATUS_T_PR_REG)
7052 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7053#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7054#if !defined(gregs)
7055 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7056 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7057#else
7058 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7059 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7060#endif
7061#endif
47d9a591 7062 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
7063 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7064}
7065#endif /* HAVE_LWPSTATUS_T */
7066
7c76fa91
MS
7067#if defined (HAVE_PSTATUS_T)
7068char *
7069elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7070 bfd *abfd;
7071 char *buf;
7072 int *bufsiz;
b87011e9 7073 long pid;
7c76fa91 7074 int cursig;
d4c88bbb 7075 const PTR gregs;
7c76fa91
MS
7076{
7077 pstatus_t pstat;
7078 char *note_name = "CORE";
7079
51316059
MS
7080 memset (&pstat, 0, sizeof (pstat));
7081 pstat.pr_pid = pid & 0xffff;
47d9a591 7082 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
7083 NT_PSTATUS, &pstat, sizeof (pstat));
7084 return buf;
7c76fa91
MS
7085}
7086#endif /* HAVE_PSTATUS_T */
7087
7088char *
7089elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
7090 bfd *abfd;
7091 char *buf;
7092 int *bufsiz;
d4c88bbb 7093 const PTR fpregs;
7c76fa91
MS
7094 int size;
7095{
7096 char *note_name = "CORE";
47d9a591 7097 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7098 note_name, NT_FPREGSET, fpregs, size);
7099}
7100
7101char *
7102elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
7103 bfd *abfd;
7104 char *buf;
7105 int *bufsiz;
d4c88bbb 7106 const PTR xfpregs;
7c76fa91
MS
7107 int size;
7108{
7109 char *note_name = "LINUX";
47d9a591 7110 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7111 note_name, NT_PRXFPREG, xfpregs, size);
7112}
7113
252b5132
RH
7114static boolean
7115elfcore_read_notes (abfd, offset, size)
c044fabd 7116 bfd *abfd;
dc810e39
AM
7117 file_ptr offset;
7118 bfd_size_type size;
252b5132 7119{
c044fabd
KH
7120 char *buf;
7121 char *p;
252b5132
RH
7122
7123 if (size <= 0)
7124 return true;
7125
dc810e39 7126 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
252b5132
RH
7127 return false;
7128
dc810e39 7129 buf = bfd_malloc (size);
252b5132
RH
7130 if (buf == NULL)
7131 return false;
7132
dc810e39 7133 if (bfd_bread (buf, size, abfd) != size)
252b5132
RH
7134 {
7135 error:
7136 free (buf);
7137 return false;
7138 }
7139
7140 p = buf;
7141 while (p < buf + size)
7142 {
c044fabd
KH
7143 /* FIXME: bad alignment assumption. */
7144 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
7145 Elf_Internal_Note in;
7146
dc810e39 7147 in.type = H_GET_32 (abfd, xnp->type);
252b5132 7148
dc810e39 7149 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132
RH
7150 in.namedata = xnp->name;
7151
dc810e39 7152 in.descsz = H_GET_32 (abfd, xnp->descsz);
252b5132
RH
7153 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7154 in.descpos = offset + (in.descdata - buf);
7155
50b2bdb7
AM
7156 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7157 {
7158 if (! elfcore_grok_netbsd_note (abfd, &in))
7159 goto error;
7160 }
7161 else
7162 {
7163 if (! elfcore_grok_note (abfd, &in))
7164 goto error;
7165 }
252b5132
RH
7166
7167 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7168 }
7169
7170 free (buf);
7171 return true;
7172}
98d8431c
JB
7173\f
7174/* Providing external access to the ELF program header table. */
7175
7176/* Return an upper bound on the number of bytes required to store a
7177 copy of ABFD's program header table entries. Return -1 if an error
7178 occurs; bfd_get_error will return an appropriate code. */
c044fabd 7179
98d8431c
JB
7180long
7181bfd_get_elf_phdr_upper_bound (abfd)
7182 bfd *abfd;
7183{
7184 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7185 {
7186 bfd_set_error (bfd_error_wrong_format);
7187 return -1;
7188 }
7189
936e320b 7190 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
7191}
7192
98d8431c
JB
7193/* Copy ABFD's program header table entries to *PHDRS. The entries
7194 will be stored as an array of Elf_Internal_Phdr structures, as
7195 defined in include/elf/internal.h. To find out how large the
7196 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7197
7198 Return the number of program header table entries read, or -1 if an
7199 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 7200
98d8431c
JB
7201int
7202bfd_get_elf_phdrs (abfd, phdrs)
7203 bfd *abfd;
7204 void *phdrs;
7205{
7206 int num_phdrs;
7207
7208 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7209 {
7210 bfd_set_error (bfd_error_wrong_format);
7211 return -1;
7212 }
7213
7214 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 7215 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
7216 num_phdrs * sizeof (Elf_Internal_Phdr));
7217
7218 return num_phdrs;
7219}
ae4221d7
L
7220
7221void
4e771d61 7222_bfd_elf_sprintf_vma (abfd, buf, value)
cc55aec9 7223 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
7224 char *buf;
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 sprintf_vma (buf, value);
7233 else
7234 {
7235 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7236 {
ae4221d7 7237#if BFD_HOST_64BIT_LONG
cc55aec9 7238 sprintf (buf, "%016lx", value);
ae4221d7 7239#else
cc55aec9
AM
7240 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7241 _bfd_int64_low (value));
ae4221d7 7242#endif
cc55aec9 7243 }
ae4221d7
L
7244 else
7245 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7246 }
d3b05f8d
L
7247#else
7248 sprintf_vma (buf, value);
7249#endif
ae4221d7
L
7250}
7251
7252void
4e771d61 7253_bfd_elf_fprintf_vma (abfd, stream, value)
cc55aec9 7254 bfd *abfd ATTRIBUTE_UNUSED;
ae4221d7
L
7255 PTR stream;
7256 bfd_vma value;
7257{
d3b05f8d 7258#ifdef BFD64
ae4221d7
L
7259 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7260
7261 i_ehdrp = elf_elfheader (abfd);
7262 if (i_ehdrp == NULL)
7263 fprintf_vma ((FILE *) stream, value);
7264 else
7265 {
7266 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7267 {
ae4221d7 7268#if BFD_HOST_64BIT_LONG
cc55aec9 7269 fprintf ((FILE *) stream, "%016lx", value);
ae4221d7 7270#else
cc55aec9
AM
7271 fprintf ((FILE *) stream, "%08lx%08lx",
7272 _bfd_int64_high (value), _bfd_int64_low (value));
ae4221d7 7273#endif
cc55aec9 7274 }
ae4221d7
L
7275 else
7276 fprintf ((FILE *) stream, "%08lx",
7277 (unsigned long) (value & 0xffffffff));
7278 }
d3b05f8d
L
7279#else
7280 fprintf_vma ((FILE *) stream, value);
7281#endif
ae4221d7 7282}
db6751f2
JJ
7283
7284enum elf_reloc_type_class
f51e552e
AM
7285_bfd_elf_reloc_type_class (rela)
7286 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
db6751f2
JJ
7287{
7288 return reloc_class_normal;
7289}
f8df10f4 7290
47d9a591 7291/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
7292 relocation against a local symbol. */
7293
7294bfd_vma
7295_bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7296 bfd *abfd;
7297 Elf_Internal_Sym *sym;
7298 asection *sec;
7299 Elf_Internal_Rela *rel;
7300{
7301 bfd_vma relocation;
7302
7303 relocation = (sec->output_section->vma
7304 + sec->output_offset
7305 + sym->st_value);
7306 if ((sec->flags & SEC_MERGE)
c629eae0 7307 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
65765700 7308 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
f8df10f4
JJ
7309 {
7310 asection *msec;
7311
7312 msec = sec;
7313 rel->r_addend =
7314 _bfd_merged_section_offset (abfd, &msec,
65765700 7315 elf_section_data (sec)->sec_info,
f8df10f4
JJ
7316 sym->st_value + rel->r_addend,
7317 (bfd_vma) 0)
7318 - relocation;
7319 rel->r_addend += msec->output_section->vma + msec->output_offset;
7320 }
7321 return relocation;
7322}
c629eae0
JJ
7323
7324bfd_vma
7325_bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7326 bfd *abfd;
7327 Elf_Internal_Sym *sym;
7328 asection **psec;
7329 bfd_vma addend;
47d9a591 7330{
c629eae0
JJ
7331 asection *sec = *psec;
7332
65765700 7333 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
c629eae0
JJ
7334 return sym->st_value + addend;
7335
7336 return _bfd_merged_section_offset (abfd, psec,
65765700 7337 elf_section_data (sec)->sec_info,
c629eae0
JJ
7338 sym->st_value + addend, (bfd_vma) 0);
7339}
7340
7341bfd_vma
7342_bfd_elf_section_offset (abfd, info, sec, offset)
7343 bfd *abfd;
7344 struct bfd_link_info *info;
7345 asection *sec;
7346 bfd_vma offset;
7347{
7348 struct bfd_elf_section_data *sec_data;
7349
7350 sec_data = elf_section_data (sec);
65765700
JJ
7351 switch (sec_data->sec_info_type)
7352 {
7353 case ELF_INFO_TYPE_STABS:
7354 return _bfd_stab_section_offset
7355 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
7356 offset);
7357 case ELF_INFO_TYPE_EH_FRAME:
7358 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7359 default:
7360 return offset;
7361 }
c629eae0 7362}