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