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