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