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