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