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