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