bfd/
[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.
6f79b219
JJ
4968 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
4969 8. PT_DYNAMIC should not contain empty sections at the beginning
4970 (with the possible exception of .dynamic). */
caf47ea6 4971#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
aecc8f8a
AM
4972 ((((segment->p_paddr \
4973 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4974 : IS_CONTAINED_BY_VMA (section, segment)) \
f5ffc919 4975 && (section->flags & SEC_ALLOC) != 0) \
b6821651 4976 || IS_COREFILE_NOTE (segment, section)) \
f5ffc919 4977 && section->output_section != NULL \
03394ac9 4978 && segment->p_type != PT_GNU_STACK \
eecdbe52
JJ
4979 && (segment->p_type != PT_TLS \
4980 || (section->flags & SEC_THREAD_LOCAL)) \
4981 && (segment->p_type == PT_LOAD \
4982 || segment->p_type == PT_TLS \
4983 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6f79b219
JJ
4984 && (segment->p_type != PT_DYNAMIC \
4985 || SECTION_SIZE (section, segment) > 0 \
4986 || (segment->p_paddr \
4987 ? segment->p_paddr != section->lma \
4988 : segment->p_vaddr != section->vma) \
4989 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
4990 == 0)) \
82e51918 4991 && ! section->segment_mark)
bc67d8a6 4992
b34976b6 4993 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
4994#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
4995 (seg1->field >= SEGMENT_END (seg2, seg2->field))
4996
4997 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
4998 their VMA address ranges and their LMA address ranges overlap.
4999 It is possible to have overlapping VMA ranges without overlapping LMA
5000 ranges. RedBoot images for example can have both .data and .bss mapped
5001 to the same VMA range, but with the .data section mapped to a different
5002 LMA. */
aecc8f8a 5003#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea
NC
5004 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5005 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5006 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5007 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
5008
5009 /* Initialise the segment mark field. */
5010 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 5011 section->segment_mark = FALSE;
bc67d8a6 5012
252b5132 5013 /* Scan through the segments specified in the program header
bc67d8a6 5014 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 5015 in the loadable segments. These can be created by weird
aecc8f8a 5016 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
5017 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5018 i < num_segments;
c044fabd 5019 i++, segment++)
252b5132 5020 {
252b5132 5021 unsigned int j;
c044fabd 5022 Elf_Internal_Phdr *segment2;
252b5132 5023
aecc8f8a
AM
5024 if (segment->p_type == PT_INTERP)
5025 for (section = ibfd->sections; section; section = section->next)
5026 if (IS_SOLARIS_PT_INTERP (segment, section))
5027 {
5028 /* Mininal change so that the normal section to segment
4cc11e76 5029 assignment code will work. */
aecc8f8a
AM
5030 segment->p_vaddr = section->vma;
5031 break;
5032 }
5033
bc67d8a6
NC
5034 if (segment->p_type != PT_LOAD)
5035 continue;
c044fabd 5036
bc67d8a6 5037 /* Determine if this segment overlaps any previous segments. */
c044fabd 5038 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
bc67d8a6
NC
5039 {
5040 bfd_signed_vma extra_length;
c044fabd 5041
bc67d8a6
NC
5042 if (segment2->p_type != PT_LOAD
5043 || ! SEGMENT_OVERLAPS (segment, segment2))
5044 continue;
c044fabd 5045
bc67d8a6
NC
5046 /* Merge the two segments together. */
5047 if (segment2->p_vaddr < segment->p_vaddr)
5048 {
c044fabd
KH
5049 /* Extend SEGMENT2 to include SEGMENT and then delete
5050 SEGMENT. */
bc67d8a6
NC
5051 extra_length =
5052 SEGMENT_END (segment, segment->p_vaddr)
5053 - SEGMENT_END (segment2, segment2->p_vaddr);
c044fabd 5054
bc67d8a6
NC
5055 if (extra_length > 0)
5056 {
5057 segment2->p_memsz += extra_length;
5058 segment2->p_filesz += extra_length;
5059 }
c044fabd 5060
bc67d8a6 5061 segment->p_type = PT_NULL;
c044fabd 5062
bc67d8a6
NC
5063 /* Since we have deleted P we must restart the outer loop. */
5064 i = 0;
5065 segment = elf_tdata (ibfd)->phdr;
5066 break;
5067 }
5068 else
5069 {
c044fabd
KH
5070 /* Extend SEGMENT to include SEGMENT2 and then delete
5071 SEGMENT2. */
bc67d8a6
NC
5072 extra_length =
5073 SEGMENT_END (segment2, segment2->p_vaddr)
5074 - SEGMENT_END (segment, segment->p_vaddr);
c044fabd 5075
bc67d8a6
NC
5076 if (extra_length > 0)
5077 {
5078 segment->p_memsz += extra_length;
5079 segment->p_filesz += extra_length;
5080 }
c044fabd 5081
bc67d8a6
NC
5082 segment2->p_type = PT_NULL;
5083 }
5084 }
5085 }
c044fabd 5086
bc67d8a6
NC
5087 /* The second scan attempts to assign sections to segments. */
5088 for (i = 0, segment = elf_tdata (ibfd)->phdr;
5089 i < num_segments;
5090 i ++, segment ++)
5091 {
5092 unsigned int section_count;
5093 asection ** sections;
5094 asection * output_section;
5095 unsigned int isec;
5096 bfd_vma matching_lma;
5097 bfd_vma suggested_lma;
5098 unsigned int j;
dc810e39 5099 bfd_size_type amt;
bc67d8a6
NC
5100
5101 if (segment->p_type == PT_NULL)
5102 continue;
c044fabd 5103
bc67d8a6 5104 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
5105 for (section = ibfd->sections, section_count = 0;
5106 section != NULL;
5107 section = section->next)
caf47ea6 5108 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c044fabd 5109 ++section_count;
811072d8 5110
b5f852ea
NC
5111 /* Allocate a segment map big enough to contain
5112 all of the sections we have selected. */
dc810e39
AM
5113 amt = sizeof (struct elf_segment_map);
5114 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
217aa764 5115 map = bfd_alloc (obfd, amt);
bc67d8a6 5116 if (map == NULL)
b34976b6 5117 return FALSE;
252b5132
RH
5118
5119 /* Initialise the fields of the segment map. Default to
5120 using the physical address of the segment in the input BFD. */
bc67d8a6
NC
5121 map->next = NULL;
5122 map->p_type = segment->p_type;
5123 map->p_flags = segment->p_flags;
5124 map->p_flags_valid = 1;
5125 map->p_paddr = segment->p_paddr;
5126 map->p_paddr_valid = 1;
252b5132
RH
5127
5128 /* Determine if this segment contains the ELF file header
5129 and if it contains the program headers themselves. */
bc67d8a6
NC
5130 map->includes_filehdr = (segment->p_offset == 0
5131 && segment->p_filesz >= iehdr->e_ehsize);
252b5132 5132
bc67d8a6 5133 map->includes_phdrs = 0;
252b5132 5134
bc67d8a6 5135 if (! phdr_included || segment->p_type != PT_LOAD)
252b5132 5136 {
bc67d8a6
NC
5137 map->includes_phdrs =
5138 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5139 && (segment->p_offset + segment->p_filesz
252b5132
RH
5140 >= ((bfd_vma) iehdr->e_phoff
5141 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 5142
bc67d8a6 5143 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 5144 phdr_included = TRUE;
252b5132
RH
5145 }
5146
bc67d8a6 5147 if (section_count == 0)
252b5132
RH
5148 {
5149 /* Special segments, such as the PT_PHDR segment, may contain
5150 no sections, but ordinary, loadable segments should contain
1ed89aa9
NC
5151 something. They are allowed by the ELF spec however, so only
5152 a warning is produced. */
bc67d8a6 5153 if (segment->p_type == PT_LOAD)
caf47ea6 5154 (*_bfd_error_handler)
d003868e
AM
5155 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"),
5156 ibfd);
252b5132 5157
bc67d8a6 5158 map->count = 0;
c044fabd
KH
5159 *pointer_to_map = map;
5160 pointer_to_map = &map->next;
252b5132
RH
5161
5162 continue;
5163 }
5164
5165 /* Now scan the sections in the input BFD again and attempt
5166 to add their corresponding output sections to the segment map.
5167 The problem here is how to handle an output section which has
5168 been moved (ie had its LMA changed). There are four possibilities:
5169
5170 1. None of the sections have been moved.
5171 In this case we can continue to use the segment LMA from the
5172 input BFD.
5173
5174 2. All of the sections have been moved by the same amount.
5175 In this case we can change the segment's LMA to match the LMA
5176 of the first section.
5177
5178 3. Some of the sections have been moved, others have not.
5179 In this case those sections which have not been moved can be
5180 placed in the current segment which will have to have its size,
5181 and possibly its LMA changed, and a new segment or segments will
5182 have to be created to contain the other sections.
5183
b5f852ea 5184 4. The sections have been moved, but not by the same amount.
252b5132
RH
5185 In this case we can change the segment's LMA to match the LMA
5186 of the first section and we will have to create a new segment
5187 or segments to contain the other sections.
5188
5189 In order to save time, we allocate an array to hold the section
5190 pointers that we are interested in. As these sections get assigned
5191 to a segment, they are removed from this array. */
5192
0b14c2aa
L
5193 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
5194 to work around this long long bug. */
5195 amt = section_count * sizeof (asection *);
217aa764 5196 sections = bfd_malloc (amt);
252b5132 5197 if (sections == NULL)
b34976b6 5198 return FALSE;
252b5132
RH
5199
5200 /* Step One: Scan for segment vs section LMA conflicts.
5201 Also add the sections to the section array allocated above.
5202 Also add the sections to the current segment. In the common
5203 case, where the sections have not been moved, this means that
5204 we have completely filled the segment, and there is nothing
5205 more to do. */
252b5132 5206 isec = 0;
72730e0c 5207 matching_lma = 0;
252b5132
RH
5208 suggested_lma = 0;
5209
bc67d8a6
NC
5210 for (j = 0, section = ibfd->sections;
5211 section != NULL;
5212 section = section->next)
252b5132 5213 {
caf47ea6 5214 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 5215 {
bc67d8a6
NC
5216 output_section = section->output_section;
5217
5218 sections[j ++] = section;
252b5132
RH
5219
5220 /* The Solaris native linker always sets p_paddr to 0.
5221 We try to catch that case here, and set it to the
5e8d7549
NC
5222 correct value. Note - some backends require that
5223 p_paddr be left as zero. */
bc67d8a6 5224 if (segment->p_paddr == 0
4455705d 5225 && segment->p_vaddr != 0
5e8d7549 5226 && (! bed->want_p_paddr_set_to_zero)
252b5132 5227 && isec == 0
bc67d8a6
NC
5228 && output_section->lma != 0
5229 && (output_section->vma == (segment->p_vaddr
5230 + (map->includes_filehdr
5231 ? iehdr->e_ehsize
5232 : 0)
5233 + (map->includes_phdrs
079e9a2f
AM
5234 ? (iehdr->e_phnum
5235 * iehdr->e_phentsize)
bc67d8a6
NC
5236 : 0))))
5237 map->p_paddr = segment->p_vaddr;
252b5132
RH
5238
5239 /* Match up the physical address of the segment with the
5240 LMA address of the output section. */
bc67d8a6 5241 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5e8d7549
NC
5242 || IS_COREFILE_NOTE (segment, section)
5243 || (bed->want_p_paddr_set_to_zero &&
5244 IS_CONTAINED_BY_VMA (output_section, segment))
5245 )
252b5132
RH
5246 {
5247 if (matching_lma == 0)
bc67d8a6 5248 matching_lma = output_section->lma;
252b5132
RH
5249
5250 /* We assume that if the section fits within the segment
bc67d8a6 5251 then it does not overlap any other section within that
252b5132 5252 segment. */
bc67d8a6 5253 map->sections[isec ++] = output_section;
252b5132
RH
5254 }
5255 else if (suggested_lma == 0)
bc67d8a6 5256 suggested_lma = output_section->lma;
252b5132
RH
5257 }
5258 }
5259
bc67d8a6 5260 BFD_ASSERT (j == section_count);
252b5132
RH
5261
5262 /* Step Two: Adjust the physical address of the current segment,
5263 if necessary. */
bc67d8a6 5264 if (isec == section_count)
252b5132
RH
5265 {
5266 /* All of the sections fitted within the segment as currently
5267 specified. This is the default case. Add the segment to
5268 the list of built segments and carry on to process the next
5269 program header in the input BFD. */
bc67d8a6 5270 map->count = section_count;
c044fabd
KH
5271 *pointer_to_map = map;
5272 pointer_to_map = &map->next;
252b5132
RH
5273
5274 free (sections);
5275 continue;
5276 }
252b5132
RH
5277 else
5278 {
72730e0c
AM
5279 if (matching_lma != 0)
5280 {
5281 /* At least one section fits inside the current segment.
5282 Keep it, but modify its physical address to match the
5283 LMA of the first section that fitted. */
bc67d8a6 5284 map->p_paddr = matching_lma;
72730e0c
AM
5285 }
5286 else
5287 {
5288 /* None of the sections fitted inside the current segment.
5289 Change the current segment's physical address to match
5290 the LMA of the first section. */
bc67d8a6 5291 map->p_paddr = suggested_lma;
72730e0c
AM
5292 }
5293
bc67d8a6
NC
5294 /* Offset the segment physical address from the lma
5295 to allow for space taken up by elf headers. */
5296 if (map->includes_filehdr)
5297 map->p_paddr -= iehdr->e_ehsize;
252b5132 5298
bc67d8a6
NC
5299 if (map->includes_phdrs)
5300 {
5301 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5302
5303 /* iehdr->e_phnum is just an estimate of the number
5304 of program headers that we will need. Make a note
5305 here of the number we used and the segment we chose
5306 to hold these headers, so that we can adjust the
5307 offset when we know the correct value. */
5308 phdr_adjust_num = iehdr->e_phnum;
5309 phdr_adjust_seg = map;
5310 }
252b5132
RH
5311 }
5312
5313 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 5314 those that fit to the current segment and removing them from the
252b5132
RH
5315 sections array; but making sure not to leave large gaps. Once all
5316 possible sections have been assigned to the current segment it is
5317 added to the list of built segments and if sections still remain
5318 to be assigned, a new segment is constructed before repeating
5319 the loop. */
5320 isec = 0;
5321 do
5322 {
bc67d8a6 5323 map->count = 0;
252b5132
RH
5324 suggested_lma = 0;
5325
5326 /* Fill the current segment with sections that fit. */
bc67d8a6 5327 for (j = 0; j < section_count; j++)
252b5132 5328 {
bc67d8a6 5329 section = sections[j];
252b5132 5330
bc67d8a6 5331 if (section == NULL)
252b5132
RH
5332 continue;
5333
bc67d8a6 5334 output_section = section->output_section;
252b5132 5335
bc67d8a6 5336 BFD_ASSERT (output_section != NULL);
c044fabd 5337
bc67d8a6
NC
5338 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5339 || IS_COREFILE_NOTE (segment, section))
252b5132 5340 {
bc67d8a6 5341 if (map->count == 0)
252b5132
RH
5342 {
5343 /* If the first section in a segment does not start at
bc67d8a6
NC
5344 the beginning of the segment, then something is
5345 wrong. */
5346 if (output_section->lma !=
5347 (map->p_paddr
5348 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5349 + (map->includes_phdrs
5350 ? iehdr->e_phnum * iehdr->e_phentsize
5351 : 0)))
252b5132
RH
5352 abort ();
5353 }
5354 else
5355 {
5356 asection * prev_sec;
252b5132 5357
bc67d8a6 5358 prev_sec = map->sections[map->count - 1];
252b5132
RH
5359
5360 /* If the gap between the end of the previous section
bc67d8a6
NC
5361 and the start of this section is more than
5362 maxpagesize then we need to start a new segment. */
eea6121a 5363 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 5364 maxpagesize)
caf47ea6 5365 < BFD_ALIGN (output_section->lma, maxpagesize))
eea6121a 5366 || ((prev_sec->lma + prev_sec->size)
079e9a2f 5367 > output_section->lma))
252b5132
RH
5368 {
5369 if (suggested_lma == 0)
bc67d8a6 5370 suggested_lma = output_section->lma;
252b5132
RH
5371
5372 continue;
5373 }
5374 }
5375
bc67d8a6 5376 map->sections[map->count++] = output_section;
252b5132
RH
5377 ++isec;
5378 sections[j] = NULL;
b34976b6 5379 section->segment_mark = TRUE;
252b5132
RH
5380 }
5381 else if (suggested_lma == 0)
bc67d8a6 5382 suggested_lma = output_section->lma;
252b5132
RH
5383 }
5384
bc67d8a6 5385 BFD_ASSERT (map->count > 0);
252b5132
RH
5386
5387 /* Add the current segment to the list of built segments. */
c044fabd
KH
5388 *pointer_to_map = map;
5389 pointer_to_map = &map->next;
252b5132 5390
bc67d8a6 5391 if (isec < section_count)
252b5132
RH
5392 {
5393 /* We still have not allocated all of the sections to
5394 segments. Create a new segment here, initialise it
5395 and carry on looping. */
dc810e39
AM
5396 amt = sizeof (struct elf_segment_map);
5397 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
217aa764 5398 map = bfd_alloc (obfd, amt);
bc67d8a6 5399 if (map == NULL)
5ed6aba4
NC
5400 {
5401 free (sections);
5402 return FALSE;
5403 }
252b5132
RH
5404
5405 /* Initialise the fields of the segment map. Set the physical
5406 physical address to the LMA of the first section that has
5407 not yet been assigned. */
bc67d8a6
NC
5408 map->next = NULL;
5409 map->p_type = segment->p_type;
5410 map->p_flags = segment->p_flags;
5411 map->p_flags_valid = 1;
5412 map->p_paddr = suggested_lma;
5413 map->p_paddr_valid = 1;
5414 map->includes_filehdr = 0;
5415 map->includes_phdrs = 0;
252b5132
RH
5416 }
5417 }
bc67d8a6 5418 while (isec < section_count);
252b5132
RH
5419
5420 free (sections);
5421 }
5422
5423 /* The Solaris linker creates program headers in which all the
5424 p_paddr fields are zero. When we try to objcopy or strip such a
5425 file, we get confused. Check for this case, and if we find it
5426 reset the p_paddr_valid fields. */
bc67d8a6
NC
5427 for (map = map_first; map != NULL; map = map->next)
5428 if (map->p_paddr != 0)
252b5132 5429 break;
bc67d8a6 5430 if (map == NULL)
b5f852ea
NC
5431 for (map = map_first; map != NULL; map = map->next)
5432 map->p_paddr_valid = 0;
252b5132 5433
bc67d8a6
NC
5434 elf_tdata (obfd)->segment_map = map_first;
5435
5436 /* If we had to estimate the number of program headers that were
9ad5cbcf 5437 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
5438 the offset if necessary. */
5439 if (phdr_adjust_seg != NULL)
5440 {
5441 unsigned int count;
c044fabd 5442
bc67d8a6 5443 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 5444 count++;
252b5132 5445
bc67d8a6
NC
5446 if (count > phdr_adjust_num)
5447 phdr_adjust_seg->p_paddr
5448 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5449 }
c044fabd 5450
252b5132 5451#if 0
c044fabd
KH
5452 /* Final Step: Sort the segments into ascending order of physical
5453 address. */
bc67d8a6 5454 if (map_first != NULL)
252b5132 5455 {
c044fabd 5456 struct elf_segment_map *prev;
252b5132 5457
bc67d8a6
NC
5458 prev = map_first;
5459 for (map = map_first->next; map != NULL; prev = map, map = map->next)
252b5132 5460 {
bc67d8a6
NC
5461 /* Yes I know - its a bubble sort.... */
5462 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
252b5132 5463 {
bc67d8a6
NC
5464 /* Swap map and map->next. */
5465 prev->next = map->next;
5466 map->next = map->next->next;
5467 prev->next->next = map;
252b5132 5468
bc67d8a6
NC
5469 /* Restart loop. */
5470 map = map_first;
252b5132
RH
5471 }
5472 }
5473 }
5474#endif
5475
bc67d8a6 5476#undef SEGMENT_END
eecdbe52 5477#undef SECTION_SIZE
bc67d8a6
NC
5478#undef IS_CONTAINED_BY_VMA
5479#undef IS_CONTAINED_BY_LMA
252b5132 5480#undef IS_COREFILE_NOTE
bc67d8a6
NC
5481#undef IS_SOLARIS_PT_INTERP
5482#undef INCLUDE_SECTION_IN_SEGMENT
5483#undef SEGMENT_AFTER_SEGMENT
5484#undef SEGMENT_OVERLAPS
b34976b6 5485 return TRUE;
252b5132
RH
5486}
5487
5488/* Copy private section information. This copies over the entsize
5489 field, and sometimes the info field. */
5490
b34976b6 5491bfd_boolean
217aa764
AM
5492_bfd_elf_copy_private_section_data (bfd *ibfd,
5493 asection *isec,
5494 bfd *obfd,
5495 asection *osec)
252b5132
RH
5496{
5497 Elf_Internal_Shdr *ihdr, *ohdr;
5498
5499 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5500 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 5501 return TRUE;
252b5132 5502
252b5132
RH
5503 ihdr = &elf_section_data (isec)->this_hdr;
5504 ohdr = &elf_section_data (osec)->this_hdr;
5505
5506 ohdr->sh_entsize = ihdr->sh_entsize;
5507
5508 if (ihdr->sh_type == SHT_SYMTAB
5509 || ihdr->sh_type == SHT_DYNSYM
5510 || ihdr->sh_type == SHT_GNU_verneed
5511 || ihdr->sh_type == SHT_GNU_verdef)
5512 ohdr->sh_info = ihdr->sh_info;
5513
9dce4196
AM
5514 /* Set things up for objcopy. The output SHT_GROUP section will
5515 have its elf_next_in_group pointing back to the input group
5516 members. */
5517 elf_next_in_group (osec) = elf_next_in_group (isec);
5518 elf_group_name (osec) = elf_group_name (isec);
5519
68bfbfcc 5520 osec->use_rela_p = isec->use_rela_p;
bf572ba0 5521
b34976b6 5522 return TRUE;
252b5132
RH
5523}
5524
80fccad2
BW
5525/* Copy private header information. */
5526
5527bfd_boolean
5528_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
5529{
5530 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5531 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5532 return TRUE;
5533
5534 /* Copy over private BFD data if it has not already been copied.
5535 This must be done here, rather than in the copy_private_bfd_data
5536 entry point, because the latter is called after the section
5537 contents have been set, which means that the program headers have
5538 already been worked out. */
5539 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5540 {
5541 if (! copy_private_bfd_data (ibfd, obfd))
5542 return FALSE;
5543 }
5544
5545 return TRUE;
5546}
5547
252b5132
RH
5548/* Copy private symbol information. If this symbol is in a section
5549 which we did not map into a BFD section, try to map the section
5550 index correctly. We use special macro definitions for the mapped
5551 section indices; these definitions are interpreted by the
5552 swap_out_syms function. */
5553
9ad5cbcf
AM
5554#define MAP_ONESYMTAB (SHN_HIOS + 1)
5555#define MAP_DYNSYMTAB (SHN_HIOS + 2)
5556#define MAP_STRTAB (SHN_HIOS + 3)
5557#define MAP_SHSTRTAB (SHN_HIOS + 4)
5558#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 5559
b34976b6 5560bfd_boolean
217aa764
AM
5561_bfd_elf_copy_private_symbol_data (bfd *ibfd,
5562 asymbol *isymarg,
5563 bfd *obfd,
5564 asymbol *osymarg)
252b5132
RH
5565{
5566 elf_symbol_type *isym, *osym;
5567
5568 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5569 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 5570 return TRUE;
252b5132
RH
5571
5572 isym = elf_symbol_from (ibfd, isymarg);
5573 osym = elf_symbol_from (obfd, osymarg);
5574
5575 if (isym != NULL
5576 && osym != NULL
5577 && bfd_is_abs_section (isym->symbol.section))
5578 {
5579 unsigned int shndx;
5580
5581 shndx = isym->internal_elf_sym.st_shndx;
5582 if (shndx == elf_onesymtab (ibfd))
5583 shndx = MAP_ONESYMTAB;
5584 else if (shndx == elf_dynsymtab (ibfd))
5585 shndx = MAP_DYNSYMTAB;
5586 else if (shndx == elf_tdata (ibfd)->strtab_section)
5587 shndx = MAP_STRTAB;
5588 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5589 shndx = MAP_SHSTRTAB;
9ad5cbcf
AM
5590 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5591 shndx = MAP_SYM_SHNDX;
252b5132
RH
5592 osym->internal_elf_sym.st_shndx = shndx;
5593 }
5594
b34976b6 5595 return TRUE;
252b5132
RH
5596}
5597
5598/* Swap out the symbols. */
5599
b34976b6 5600static bfd_boolean
217aa764
AM
5601swap_out_syms (bfd *abfd,
5602 struct bfd_strtab_hash **sttp,
5603 int relocatable_p)
252b5132 5604{
9c5bfbb7 5605 const struct elf_backend_data *bed;
079e9a2f
AM
5606 int symcount;
5607 asymbol **syms;
5608 struct bfd_strtab_hash *stt;
5609 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 5610 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f
AM
5611 Elf_Internal_Shdr *symstrtab_hdr;
5612 char *outbound_syms;
9ad5cbcf 5613 char *outbound_shndx;
079e9a2f
AM
5614 int idx;
5615 bfd_size_type amt;
174fd7f9 5616 bfd_boolean name_local_sections;
252b5132
RH
5617
5618 if (!elf_map_symbols (abfd))
b34976b6 5619 return FALSE;
252b5132 5620
c044fabd 5621 /* Dump out the symtabs. */
079e9a2f
AM
5622 stt = _bfd_elf_stringtab_init ();
5623 if (stt == NULL)
b34976b6 5624 return FALSE;
252b5132 5625
079e9a2f
AM
5626 bed = get_elf_backend_data (abfd);
5627 symcount = bfd_get_symcount (abfd);
5628 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5629 symtab_hdr->sh_type = SHT_SYMTAB;
5630 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5631 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5632 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
45d6a902 5633 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align;
079e9a2f
AM
5634
5635 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5636 symstrtab_hdr->sh_type = SHT_STRTAB;
5637
5638 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5639 outbound_syms = bfd_alloc (abfd, amt);
5640 if (outbound_syms == NULL)
5ed6aba4
NC
5641 {
5642 _bfd_stringtab_free (stt);
5643 return FALSE;
5644 }
217aa764 5645 symtab_hdr->contents = outbound_syms;
252b5132 5646
9ad5cbcf
AM
5647 outbound_shndx = NULL;
5648 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5649 if (symtab_shndx_hdr->sh_name != 0)
5650 {
5651 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
1126897b 5652 outbound_shndx = bfd_zalloc (abfd, amt);
9ad5cbcf 5653 if (outbound_shndx == NULL)
5ed6aba4
NC
5654 {
5655 _bfd_stringtab_free (stt);
5656 return FALSE;
5657 }
5658
9ad5cbcf
AM
5659 symtab_shndx_hdr->contents = outbound_shndx;
5660 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5661 symtab_shndx_hdr->sh_size = amt;
5662 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5663 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5664 }
5665
589e6347 5666 /* Now generate the data (for "contents"). */
079e9a2f
AM
5667 {
5668 /* Fill in zeroth symbol and swap it out. */
5669 Elf_Internal_Sym sym;
5670 sym.st_name = 0;
5671 sym.st_value = 0;
5672 sym.st_size = 0;
5673 sym.st_info = 0;
5674 sym.st_other = 0;
5675 sym.st_shndx = SHN_UNDEF;
9ad5cbcf 5676 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5677 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5678 if (outbound_shndx != NULL)
5679 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5680 }
252b5132 5681
174fd7f9
RS
5682 name_local_sections
5683 = (bed->elf_backend_name_local_section_symbols
5684 && bed->elf_backend_name_local_section_symbols (abfd));
5685
079e9a2f
AM
5686 syms = bfd_get_outsymbols (abfd);
5687 for (idx = 0; idx < symcount; idx++)
252b5132 5688 {
252b5132 5689 Elf_Internal_Sym sym;
079e9a2f
AM
5690 bfd_vma value = syms[idx]->value;
5691 elf_symbol_type *type_ptr;
5692 flagword flags = syms[idx]->flags;
5693 int type;
252b5132 5694
174fd7f9
RS
5695 if (!name_local_sections
5696 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
5697 {
5698 /* Local section symbols have no name. */
5699 sym.st_name = 0;
5700 }
5701 else
5702 {
5703 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5704 syms[idx]->name,
b34976b6 5705 TRUE, FALSE);
079e9a2f 5706 if (sym.st_name == (unsigned long) -1)
5ed6aba4
NC
5707 {
5708 _bfd_stringtab_free (stt);
5709 return FALSE;
5710 }
079e9a2f 5711 }
252b5132 5712
079e9a2f 5713 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 5714
079e9a2f
AM
5715 if ((flags & BSF_SECTION_SYM) == 0
5716 && bfd_is_com_section (syms[idx]->section))
5717 {
5718 /* ELF common symbols put the alignment into the `value' field,
5719 and the size into the `size' field. This is backwards from
5720 how BFD handles it, so reverse it here. */
5721 sym.st_size = value;
5722 if (type_ptr == NULL
5723 || type_ptr->internal_elf_sym.st_value == 0)
5724 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5725 else
5726 sym.st_value = type_ptr->internal_elf_sym.st_value;
5727 sym.st_shndx = _bfd_elf_section_from_bfd_section
5728 (abfd, syms[idx]->section);
5729 }
5730 else
5731 {
5732 asection *sec = syms[idx]->section;
5733 int shndx;
252b5132 5734
079e9a2f
AM
5735 if (sec->output_section)
5736 {
5737 value += sec->output_offset;
5738 sec = sec->output_section;
5739 }
589e6347 5740
079e9a2f
AM
5741 /* Don't add in the section vma for relocatable output. */
5742 if (! relocatable_p)
5743 value += sec->vma;
5744 sym.st_value = value;
5745 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5746
5747 if (bfd_is_abs_section (sec)
5748 && type_ptr != NULL
5749 && type_ptr->internal_elf_sym.st_shndx != 0)
5750 {
5751 /* This symbol is in a real ELF section which we did
5752 not create as a BFD section. Undo the mapping done
5753 by copy_private_symbol_data. */
5754 shndx = type_ptr->internal_elf_sym.st_shndx;
5755 switch (shndx)
5756 {
5757 case MAP_ONESYMTAB:
5758 shndx = elf_onesymtab (abfd);
5759 break;
5760 case MAP_DYNSYMTAB:
5761 shndx = elf_dynsymtab (abfd);
5762 break;
5763 case MAP_STRTAB:
5764 shndx = elf_tdata (abfd)->strtab_section;
5765 break;
5766 case MAP_SHSTRTAB:
5767 shndx = elf_tdata (abfd)->shstrtab_section;
5768 break;
9ad5cbcf
AM
5769 case MAP_SYM_SHNDX:
5770 shndx = elf_tdata (abfd)->symtab_shndx_section;
5771 break;
079e9a2f
AM
5772 default:
5773 break;
5774 }
5775 }
5776 else
5777 {
5778 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 5779
079e9a2f
AM
5780 if (shndx == -1)
5781 {
5782 asection *sec2;
5783
5784 /* Writing this would be a hell of a lot easier if
5785 we had some decent documentation on bfd, and
5786 knew what to expect of the library, and what to
5787 demand of applications. For example, it
5788 appears that `objcopy' might not set the
5789 section of a symbol to be a section that is
5790 actually in the output file. */
5791 sec2 = bfd_get_section_by_name (abfd, sec->name);
589e6347
NC
5792 if (sec2 == NULL)
5793 {
5794 _bfd_error_handler (_("\
5795Unable to find equivalent output section for symbol '%s' from section '%s'"),
5796 syms[idx]->name ? syms[idx]->name : "<Local sym>",
5797 sec->name);
811072d8 5798 bfd_set_error (bfd_error_invalid_operation);
5ed6aba4 5799 _bfd_stringtab_free (stt);
589e6347
NC
5800 return FALSE;
5801 }
811072d8 5802
079e9a2f
AM
5803 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5804 BFD_ASSERT (shndx != -1);
5805 }
5806 }
252b5132 5807
079e9a2f
AM
5808 sym.st_shndx = shndx;
5809 }
252b5132 5810
13ae64f3
JJ
5811 if ((flags & BSF_THREAD_LOCAL) != 0)
5812 type = STT_TLS;
5813 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
5814 type = STT_FUNC;
5815 else if ((flags & BSF_OBJECT) != 0)
5816 type = STT_OBJECT;
5817 else
5818 type = STT_NOTYPE;
252b5132 5819
13ae64f3
JJ
5820 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5821 type = STT_TLS;
5822
589e6347 5823 /* Processor-specific types. */
079e9a2f
AM
5824 if (type_ptr != NULL
5825 && bed->elf_backend_get_symbol_type)
5826 type = ((*bed->elf_backend_get_symbol_type)
5827 (&type_ptr->internal_elf_sym, type));
252b5132 5828
079e9a2f
AM
5829 if (flags & BSF_SECTION_SYM)
5830 {
5831 if (flags & BSF_GLOBAL)
5832 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5833 else
5834 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5835 }
5836 else if (bfd_is_com_section (syms[idx]->section))
5837 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5838 else if (bfd_is_und_section (syms[idx]->section))
5839 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5840 ? STB_WEAK
5841 : STB_GLOBAL),
5842 type);
5843 else if (flags & BSF_FILE)
5844 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5845 else
5846 {
5847 int bind = STB_LOCAL;
252b5132 5848
079e9a2f
AM
5849 if (flags & BSF_LOCAL)
5850 bind = STB_LOCAL;
5851 else if (flags & BSF_WEAK)
5852 bind = STB_WEAK;
5853 else if (flags & BSF_GLOBAL)
5854 bind = STB_GLOBAL;
252b5132 5855
079e9a2f
AM
5856 sym.st_info = ELF_ST_INFO (bind, type);
5857 }
252b5132 5858
079e9a2f
AM
5859 if (type_ptr != NULL)
5860 sym.st_other = type_ptr->internal_elf_sym.st_other;
5861 else
5862 sym.st_other = 0;
252b5132 5863
9ad5cbcf 5864 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
079e9a2f 5865 outbound_syms += bed->s->sizeof_sym;
9ad5cbcf
AM
5866 if (outbound_shndx != NULL)
5867 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
079e9a2f 5868 }
252b5132 5869
079e9a2f
AM
5870 *sttp = stt;
5871 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5872 symstrtab_hdr->sh_type = SHT_STRTAB;
252b5132 5873
079e9a2f
AM
5874 symstrtab_hdr->sh_flags = 0;
5875 symstrtab_hdr->sh_addr = 0;
5876 symstrtab_hdr->sh_entsize = 0;
5877 symstrtab_hdr->sh_link = 0;
5878 symstrtab_hdr->sh_info = 0;
5879 symstrtab_hdr->sh_addralign = 1;
252b5132 5880
b34976b6 5881 return TRUE;
252b5132
RH
5882}
5883
5884/* Return the number of bytes required to hold the symtab vector.
5885
5886 Note that we base it on the count plus 1, since we will null terminate
5887 the vector allocated based on this size. However, the ELF symbol table
5888 always has a dummy entry as symbol #0, so it ends up even. */
5889
5890long
217aa764 5891_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132
RH
5892{
5893 long symcount;
5894 long symtab_size;
5895 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5896
5897 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5898 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5899 if (symcount > 0)
5900 symtab_size -= sizeof (asymbol *);
252b5132
RH
5901
5902 return symtab_size;
5903}
5904
5905long
217aa764 5906_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132
RH
5907{
5908 long symcount;
5909 long symtab_size;
5910 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5911
5912 if (elf_dynsymtab (abfd) == 0)
5913 {
5914 bfd_set_error (bfd_error_invalid_operation);
5915 return -1;
5916 }
5917
5918 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
5919 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5920 if (symcount > 0)
5921 symtab_size -= sizeof (asymbol *);
252b5132
RH
5922
5923 return symtab_size;
5924}
5925
5926long
217aa764
AM
5927_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
5928 sec_ptr asect)
252b5132
RH
5929{
5930 return (asect->reloc_count + 1) * sizeof (arelent *);
5931}
5932
5933/* Canonicalize the relocs. */
5934
5935long
217aa764
AM
5936_bfd_elf_canonicalize_reloc (bfd *abfd,
5937 sec_ptr section,
5938 arelent **relptr,
5939 asymbol **symbols)
252b5132
RH
5940{
5941 arelent *tblptr;
5942 unsigned int i;
9c5bfbb7 5943 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5944
b34976b6 5945 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
5946 return -1;
5947
5948 tblptr = section->relocation;
5949 for (i = 0; i < section->reloc_count; i++)
5950 *relptr++ = tblptr++;
5951
5952 *relptr = NULL;
5953
5954 return section->reloc_count;
5955}
5956
5957long
6cee3f79 5958_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 5959{
9c5bfbb7 5960 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 5961 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
5962
5963 if (symcount >= 0)
5964 bfd_get_symcount (abfd) = symcount;
5965 return symcount;
5966}
5967
5968long
217aa764
AM
5969_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
5970 asymbol **allocation)
252b5132 5971{
9c5bfbb7 5972 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 5973 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
5974
5975 if (symcount >= 0)
5976 bfd_get_dynamic_symcount (abfd) = symcount;
5977 return symcount;
252b5132
RH
5978}
5979
5980/* Return the size required for the dynamic reloc entries. Any
5981 section that was actually installed in the BFD, and has type
5982 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5983 considered to be a dynamic reloc section. */
5984
5985long
217aa764 5986_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132
RH
5987{
5988 long ret;
5989 asection *s;
5990
5991 if (elf_dynsymtab (abfd) == 0)
5992 {
5993 bfd_set_error (bfd_error_invalid_operation);
5994 return -1;
5995 }
5996
5997 ret = sizeof (arelent *);
5998 for (s = abfd->sections; s != NULL; s = s->next)
5999 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6000 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6001 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
eea6121a 6002 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
252b5132
RH
6003 * sizeof (arelent *));
6004
6005 return ret;
6006}
6007
6008/* Canonicalize the dynamic relocation entries. Note that we return
6009 the dynamic relocations as a single block, although they are
6010 actually associated with particular sections; the interface, which
6011 was designed for SunOS style shared libraries, expects that there
6012 is only one set of dynamic relocs. Any section that was actually
6013 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
6014 the dynamic symbol table, is considered to be a dynamic reloc
6015 section. */
6016
6017long
217aa764
AM
6018_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
6019 arelent **storage,
6020 asymbol **syms)
252b5132 6021{
217aa764 6022 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
6023 asection *s;
6024 long ret;
6025
6026 if (elf_dynsymtab (abfd) == 0)
6027 {
6028 bfd_set_error (bfd_error_invalid_operation);
6029 return -1;
6030 }
6031
6032 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6033 ret = 0;
6034 for (s = abfd->sections; s != NULL; s = s->next)
6035 {
6036 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
6037 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
6038 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
6039 {
6040 arelent *p;
6041 long count, i;
6042
b34976b6 6043 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 6044 return -1;
eea6121a 6045 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
6046 p = s->relocation;
6047 for (i = 0; i < count; i++)
6048 *storage++ = p++;
6049 ret += count;
6050 }
6051 }
6052
6053 *storage = NULL;
6054
6055 return ret;
6056}
6057\f
6058/* Read in the version information. */
6059
b34976b6 6060bfd_boolean
fc0e6df6 6061_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
6062{
6063 bfd_byte *contents = NULL;
dc810e39 6064 bfd_size_type amt;
fc0e6df6
PB
6065 unsigned int freeidx = 0;
6066
6067 if (elf_dynverref (abfd) != 0)
6068 {
6069 Elf_Internal_Shdr *hdr;
6070 Elf_External_Verneed *everneed;
6071 Elf_Internal_Verneed *iverneed;
6072 unsigned int i;
6073
6074 hdr = &elf_tdata (abfd)->dynverref_hdr;
6075
6076 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
6077 elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
6078 if (elf_tdata (abfd)->verref == NULL)
6079 goto error_return;
6080
6081 elf_tdata (abfd)->cverrefs = hdr->sh_info;
6082
6083 contents = bfd_malloc (hdr->sh_size);
6084 if (contents == NULL)
6085 goto error_return;
6086 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
6087 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
6088 goto error_return;
6089
6090 everneed = (Elf_External_Verneed *) contents;
6091 iverneed = elf_tdata (abfd)->verref;
6092 for (i = 0; i < hdr->sh_info; i++, iverneed++)
6093 {
6094 Elf_External_Vernaux *evernaux;
6095 Elf_Internal_Vernaux *ivernaux;
6096 unsigned int j;
6097
6098 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
6099
6100 iverneed->vn_bfd = abfd;
6101
6102 iverneed->vn_filename =
6103 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6104 iverneed->vn_file);
6105 if (iverneed->vn_filename == NULL)
6106 goto error_return;
6107
6108 amt = iverneed->vn_cnt;
6109 amt *= sizeof (Elf_Internal_Vernaux);
6110 iverneed->vn_auxptr = bfd_alloc (abfd, amt);
6111
6112 evernaux = ((Elf_External_Vernaux *)
6113 ((bfd_byte *) everneed + iverneed->vn_aux));
6114 ivernaux = iverneed->vn_auxptr;
6115 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
6116 {
6117 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
6118
6119 ivernaux->vna_nodename =
6120 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6121 ivernaux->vna_name);
6122 if (ivernaux->vna_nodename == NULL)
6123 goto error_return;
6124
6125 if (j + 1 < iverneed->vn_cnt)
6126 ivernaux->vna_nextptr = ivernaux + 1;
6127 else
6128 ivernaux->vna_nextptr = NULL;
6129
6130 evernaux = ((Elf_External_Vernaux *)
6131 ((bfd_byte *) evernaux + ivernaux->vna_next));
6132
6133 if (ivernaux->vna_other > freeidx)
6134 freeidx = ivernaux->vna_other;
6135 }
6136
6137 if (i + 1 < hdr->sh_info)
6138 iverneed->vn_nextref = iverneed + 1;
6139 else
6140 iverneed->vn_nextref = NULL;
6141
6142 everneed = ((Elf_External_Verneed *)
6143 ((bfd_byte *) everneed + iverneed->vn_next));
6144 }
6145
6146 free (contents);
6147 contents = NULL;
6148 }
252b5132
RH
6149
6150 if (elf_dynverdef (abfd) != 0)
6151 {
6152 Elf_Internal_Shdr *hdr;
6153 Elf_External_Verdef *everdef;
6154 Elf_Internal_Verdef *iverdef;
f631889e
UD
6155 Elf_Internal_Verdef *iverdefarr;
6156 Elf_Internal_Verdef iverdefmem;
252b5132 6157 unsigned int i;
062e2358 6158 unsigned int maxidx;
252b5132
RH
6159
6160 hdr = &elf_tdata (abfd)->dynverdef_hdr;
6161
217aa764 6162 contents = bfd_malloc (hdr->sh_size);
252b5132
RH
6163 if (contents == NULL)
6164 goto error_return;
6165 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
217aa764 6166 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
252b5132
RH
6167 goto error_return;
6168
f631889e
UD
6169 /* We know the number of entries in the section but not the maximum
6170 index. Therefore we have to run through all entries and find
6171 the maximum. */
252b5132 6172 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
6173 maxidx = 0;
6174 for (i = 0; i < hdr->sh_info; ++i)
6175 {
6176 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6177
062e2358
AM
6178 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
6179 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e
UD
6180
6181 everdef = ((Elf_External_Verdef *)
6182 ((bfd_byte *) everdef + iverdefmem.vd_next));
6183 }
6184
fc0e6df6
PB
6185 if (default_imported_symver)
6186 {
6187 if (freeidx > maxidx)
6188 maxidx = ++freeidx;
6189 else
6190 freeidx = ++maxidx;
6191 }
dc810e39 6192 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
217aa764 6193 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
f631889e
UD
6194 if (elf_tdata (abfd)->verdef == NULL)
6195 goto error_return;
6196
6197 elf_tdata (abfd)->cverdefs = maxidx;
6198
6199 everdef = (Elf_External_Verdef *) contents;
6200 iverdefarr = elf_tdata (abfd)->verdef;
6201 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
6202 {
6203 Elf_External_Verdaux *everdaux;
6204 Elf_Internal_Verdaux *iverdaux;
6205 unsigned int j;
6206
f631889e
UD
6207 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
6208
6209 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
6210 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
252b5132
RH
6211
6212 iverdef->vd_bfd = abfd;
6213
dc810e39 6214 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
217aa764 6215 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
252b5132
RH
6216 if (iverdef->vd_auxptr == NULL)
6217 goto error_return;
6218
6219 everdaux = ((Elf_External_Verdaux *)
6220 ((bfd_byte *) everdef + iverdef->vd_aux));
6221 iverdaux = iverdef->vd_auxptr;
6222 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
6223 {
6224 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
6225
6226 iverdaux->vda_nodename =
6227 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
6228 iverdaux->vda_name);
6229 if (iverdaux->vda_nodename == NULL)
6230 goto error_return;
6231
6232 if (j + 1 < iverdef->vd_cnt)
6233 iverdaux->vda_nextptr = iverdaux + 1;
6234 else
6235 iverdaux->vda_nextptr = NULL;
6236
6237 everdaux = ((Elf_External_Verdaux *)
6238 ((bfd_byte *) everdaux + iverdaux->vda_next));
6239 }
6240
6241 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
6242
6243 if (i + 1 < hdr->sh_info)
6244 iverdef->vd_nextdef = iverdef + 1;
6245 else
6246 iverdef->vd_nextdef = NULL;
6247
6248 everdef = ((Elf_External_Verdef *)
6249 ((bfd_byte *) everdef + iverdef->vd_next));
6250 }
6251
6252 free (contents);
6253 contents = NULL;
6254 }
fc0e6df6 6255 else if (default_imported_symver)
252b5132 6256 {
fc0e6df6
PB
6257 if (freeidx < 3)
6258 freeidx = 3;
6259 else
6260 freeidx++;
252b5132 6261
fc0e6df6
PB
6262 amt = (bfd_size_type) freeidx * sizeof (Elf_Internal_Verdef);
6263 elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
6264 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
6265 goto error_return;
6266
fc0e6df6
PB
6267 elf_tdata (abfd)->cverdefs = freeidx;
6268 }
252b5132 6269
fc0e6df6
PB
6270 /* Create a default version based on the soname. */
6271 if (default_imported_symver)
6272 {
6273 Elf_Internal_Verdef *iverdef;
6274 Elf_Internal_Verdaux *iverdaux;
252b5132 6275
fc0e6df6 6276 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];;
252b5132 6277
fc0e6df6
PB
6278 iverdef->vd_version = VER_DEF_CURRENT;
6279 iverdef->vd_flags = 0;
6280 iverdef->vd_ndx = freeidx;
6281 iverdef->vd_cnt = 1;
252b5132 6282
fc0e6df6 6283 iverdef->vd_bfd = abfd;
252b5132 6284
fc0e6df6
PB
6285 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
6286 if (iverdef->vd_nodename == NULL)
6287 goto error_return;
6288 iverdef->vd_nextdef = NULL;
6289 amt = (bfd_size_type) sizeof (Elf_Internal_Verdaux);
6290 iverdef->vd_auxptr = bfd_alloc (abfd, amt);
252b5132 6291
fc0e6df6
PB
6292 iverdaux = iverdef->vd_auxptr;
6293 iverdaux->vda_nodename = iverdef->vd_nodename;
6294 iverdaux->vda_nextptr = NULL;
252b5132
RH
6295 }
6296
b34976b6 6297 return TRUE;
252b5132
RH
6298
6299 error_return:
5ed6aba4 6300 if (contents != NULL)
252b5132 6301 free (contents);
b34976b6 6302 return FALSE;
252b5132
RH
6303}
6304\f
6305asymbol *
217aa764 6306_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
6307{
6308 elf_symbol_type *newsym;
dc810e39 6309 bfd_size_type amt = sizeof (elf_symbol_type);
252b5132 6310
217aa764 6311 newsym = bfd_zalloc (abfd, amt);
252b5132
RH
6312 if (!newsym)
6313 return NULL;
6314 else
6315 {
6316 newsym->symbol.the_bfd = abfd;
6317 return &newsym->symbol;
6318 }
6319}
6320
6321void
217aa764
AM
6322_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
6323 asymbol *symbol,
6324 symbol_info *ret)
252b5132
RH
6325{
6326 bfd_symbol_info (symbol, ret);
6327}
6328
6329/* Return whether a symbol name implies a local symbol. Most targets
6330 use this function for the is_local_label_name entry point, but some
6331 override it. */
6332
b34976b6 6333bfd_boolean
217aa764
AM
6334_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
6335 const char *name)
252b5132
RH
6336{
6337 /* Normal local symbols start with ``.L''. */
6338 if (name[0] == '.' && name[1] == 'L')
b34976b6 6339 return TRUE;
252b5132
RH
6340
6341 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
6342 DWARF debugging symbols starting with ``..''. */
6343 if (name[0] == '.' && name[1] == '.')
b34976b6 6344 return TRUE;
252b5132
RH
6345
6346 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
6347 emitting DWARF debugging output. I suspect this is actually a
6348 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
6349 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
6350 underscore to be emitted on some ELF targets). For ease of use,
6351 we treat such symbols as local. */
6352 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 6353 return TRUE;
252b5132 6354
b34976b6 6355 return FALSE;
252b5132
RH
6356}
6357
6358alent *
217aa764
AM
6359_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
6360 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
6361{
6362 abort ();
6363 return NULL;
6364}
6365
b34976b6 6366bfd_boolean
217aa764
AM
6367_bfd_elf_set_arch_mach (bfd *abfd,
6368 enum bfd_architecture arch,
6369 unsigned long machine)
252b5132
RH
6370{
6371 /* If this isn't the right architecture for this backend, and this
6372 isn't the generic backend, fail. */
6373 if (arch != get_elf_backend_data (abfd)->arch
6374 && arch != bfd_arch_unknown
6375 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 6376 return FALSE;
252b5132
RH
6377
6378 return bfd_default_set_arch_mach (abfd, arch, machine);
6379}
6380
d1fad7c6
NC
6381/* Find the function to a particular section and offset,
6382 for error reporting. */
252b5132 6383
b34976b6 6384static bfd_boolean
217aa764
AM
6385elf_find_function (bfd *abfd ATTRIBUTE_UNUSED,
6386 asection *section,
6387 asymbol **symbols,
6388 bfd_vma offset,
6389 const char **filename_ptr,
6390 const char **functionname_ptr)
252b5132 6391{
252b5132 6392 const char *filename;
57426232 6393 asymbol *func, *file;
252b5132
RH
6394 bfd_vma low_func;
6395 asymbol **p;
57426232
JB
6396 /* ??? Given multiple file symbols, it is impossible to reliably
6397 choose the right file name for global symbols. File symbols are
6398 local symbols, and thus all file symbols must sort before any
6399 global symbols. The ELF spec may be interpreted to say that a
6400 file symbol must sort before other local symbols, but currently
6401 ld -r doesn't do this. So, for ld -r output, it is possible to
6402 make a better choice of file name for local symbols by ignoring
6403 file symbols appearing after a given local symbol. */
6404 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
252b5132 6405
252b5132
RH
6406 filename = NULL;
6407 func = NULL;
57426232 6408 file = NULL;
252b5132 6409 low_func = 0;
57426232 6410 state = nothing_seen;
252b5132
RH
6411
6412 for (p = symbols; *p != NULL; p++)
6413 {
6414 elf_symbol_type *q;
6415
6416 q = (elf_symbol_type *) *p;
6417
252b5132
RH
6418 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
6419 {
6420 default:
6421 break;
6422 case STT_FILE:
57426232
JB
6423 file = &q->symbol;
6424 if (state == symbol_seen)
6425 state = file_after_symbol_seen;
6426 continue;
6427 case STT_SECTION:
6428 continue;
252b5132
RH
6429 case STT_NOTYPE:
6430 case STT_FUNC:
6b40fcba 6431 if (bfd_get_section (&q->symbol) == section
252b5132
RH
6432 && q->symbol.value >= low_func
6433 && q->symbol.value <= offset)
6434 {
6435 func = (asymbol *) q;
6436 low_func = q->symbol.value;
57426232
JB
6437 if (file == NULL)
6438 filename = NULL;
6439 else if (ELF_ST_BIND (q->internal_elf_sym.st_info) != STB_LOCAL
6440 && state == file_after_symbol_seen)
6441 filename = NULL;
6442 else
6443 filename = bfd_asymbol_name (file);
252b5132
RH
6444 }
6445 break;
6446 }
57426232
JB
6447 if (state == nothing_seen)
6448 state = symbol_seen;
252b5132
RH
6449 }
6450
6451 if (func == NULL)
b34976b6 6452 return FALSE;
252b5132 6453
d1fad7c6
NC
6454 if (filename_ptr)
6455 *filename_ptr = filename;
6456 if (functionname_ptr)
6457 *functionname_ptr = bfd_asymbol_name (func);
6458
b34976b6 6459 return TRUE;
d1fad7c6
NC
6460}
6461
6462/* Find the nearest line to a particular section and offset,
6463 for error reporting. */
6464
b34976b6 6465bfd_boolean
217aa764
AM
6466_bfd_elf_find_nearest_line (bfd *abfd,
6467 asection *section,
6468 asymbol **symbols,
6469 bfd_vma offset,
6470 const char **filename_ptr,
6471 const char **functionname_ptr,
6472 unsigned int *line_ptr)
d1fad7c6 6473{
b34976b6 6474 bfd_boolean found;
d1fad7c6
NC
6475
6476 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
6477 filename_ptr, functionname_ptr,
6478 line_ptr))
d1fad7c6
NC
6479 {
6480 if (!*functionname_ptr)
4e8a9624
AM
6481 elf_find_function (abfd, section, symbols, offset,
6482 *filename_ptr ? NULL : filename_ptr,
6483 functionname_ptr);
6484
b34976b6 6485 return TRUE;
d1fad7c6
NC
6486 }
6487
6488 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4e8a9624
AM
6489 filename_ptr, functionname_ptr,
6490 line_ptr, 0,
6491 &elf_tdata (abfd)->dwarf2_find_line_info))
d1fad7c6
NC
6492 {
6493 if (!*functionname_ptr)
4e8a9624
AM
6494 elf_find_function (abfd, section, symbols, offset,
6495 *filename_ptr ? NULL : filename_ptr,
6496 functionname_ptr);
6497
b34976b6 6498 return TRUE;
d1fad7c6
NC
6499 }
6500
6501 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
6502 &found, filename_ptr,
6503 functionname_ptr, line_ptr,
6504 &elf_tdata (abfd)->line_info))
b34976b6 6505 return FALSE;
dc43ada5 6506 if (found && (*functionname_ptr || *line_ptr))
b34976b6 6507 return TRUE;
d1fad7c6
NC
6508
6509 if (symbols == NULL)
b34976b6 6510 return FALSE;
d1fad7c6
NC
6511
6512 if (! elf_find_function (abfd, section, symbols, offset,
4e8a9624 6513 filename_ptr, functionname_ptr))
b34976b6 6514 return FALSE;
d1fad7c6 6515
252b5132 6516 *line_ptr = 0;
b34976b6 6517 return TRUE;
252b5132
RH
6518}
6519
6520int
217aa764 6521_bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc)
252b5132
RH
6522{
6523 int ret;
6524
6525 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6526 if (! reloc)
6527 ret += get_program_header_size (abfd);
6528 return ret;
6529}
6530
b34976b6 6531bfd_boolean
217aa764
AM
6532_bfd_elf_set_section_contents (bfd *abfd,
6533 sec_ptr section,
0f867abe 6534 const void *location,
217aa764
AM
6535 file_ptr offset,
6536 bfd_size_type count)
252b5132
RH
6537{
6538 Elf_Internal_Shdr *hdr;
dc810e39 6539 bfd_signed_vma pos;
252b5132
RH
6540
6541 if (! abfd->output_has_begun
217aa764 6542 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6543 return FALSE;
252b5132
RH
6544
6545 hdr = &elf_section_data (section)->this_hdr;
dc810e39
AM
6546 pos = hdr->sh_offset + offset;
6547 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6548 || bfd_bwrite (location, count, abfd) != count)
b34976b6 6549 return FALSE;
252b5132 6550
b34976b6 6551 return TRUE;
252b5132
RH
6552}
6553
6554void
217aa764
AM
6555_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
6556 arelent *cache_ptr ATTRIBUTE_UNUSED,
6557 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
6558{
6559 abort ();
6560}
6561
252b5132
RH
6562/* Try to convert a non-ELF reloc into an ELF one. */
6563
b34976b6 6564bfd_boolean
217aa764 6565_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 6566{
c044fabd 6567 /* Check whether we really have an ELF howto. */
252b5132
RH
6568
6569 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6570 {
6571 bfd_reloc_code_real_type code;
6572 reloc_howto_type *howto;
6573
6574 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 6575 equivalent ELF reloc. */
252b5132
RH
6576
6577 if (areloc->howto->pc_relative)
6578 {
6579 switch (areloc->howto->bitsize)
6580 {
6581 case 8:
6582 code = BFD_RELOC_8_PCREL;
6583 break;
6584 case 12:
6585 code = BFD_RELOC_12_PCREL;
6586 break;
6587 case 16:
6588 code = BFD_RELOC_16_PCREL;
6589 break;
6590 case 24:
6591 code = BFD_RELOC_24_PCREL;
6592 break;
6593 case 32:
6594 code = BFD_RELOC_32_PCREL;
6595 break;
6596 case 64:
6597 code = BFD_RELOC_64_PCREL;
6598 break;
6599 default:
6600 goto fail;
6601 }
6602
6603 howto = bfd_reloc_type_lookup (abfd, code);
6604
6605 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6606 {
6607 if (howto->pcrel_offset)
6608 areloc->addend += areloc->address;
6609 else
6610 areloc->addend -= areloc->address; /* addend is unsigned!! */
6611 }
6612 }
6613 else
6614 {
6615 switch (areloc->howto->bitsize)
6616 {
6617 case 8:
6618 code = BFD_RELOC_8;
6619 break;
6620 case 14:
6621 code = BFD_RELOC_14;
6622 break;
6623 case 16:
6624 code = BFD_RELOC_16;
6625 break;
6626 case 26:
6627 code = BFD_RELOC_26;
6628 break;
6629 case 32:
6630 code = BFD_RELOC_32;
6631 break;
6632 case 64:
6633 code = BFD_RELOC_64;
6634 break;
6635 default:
6636 goto fail;
6637 }
6638
6639 howto = bfd_reloc_type_lookup (abfd, code);
6640 }
6641
6642 if (howto)
6643 areloc->howto = howto;
6644 else
6645 goto fail;
6646 }
6647
b34976b6 6648 return TRUE;
252b5132
RH
6649
6650 fail:
6651 (*_bfd_error_handler)
d003868e
AM
6652 (_("%B: unsupported relocation type %s"),
6653 abfd, areloc->howto->name);
252b5132 6654 bfd_set_error (bfd_error_bad_value);
b34976b6 6655 return FALSE;
252b5132
RH
6656}
6657
b34976b6 6658bfd_boolean
217aa764 6659_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132
RH
6660{
6661 if (bfd_get_format (abfd) == bfd_object)
6662 {
6663 if (elf_shstrtab (abfd) != NULL)
2b0f7ef9 6664 _bfd_elf_strtab_free (elf_shstrtab (abfd));
252b5132
RH
6665 }
6666
6667 return _bfd_generic_close_and_cleanup (abfd);
6668}
6669
6670/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6671 in the relocation's offset. Thus we cannot allow any sort of sanity
6672 range-checking to interfere. There is nothing else to do in processing
6673 this reloc. */
6674
6675bfd_reloc_status_type
217aa764
AM
6676_bfd_elf_rel_vtable_reloc_fn
6677 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 6678 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
6679 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
6680 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
6681{
6682 return bfd_reloc_ok;
6683}
252b5132
RH
6684\f
6685/* Elf core file support. Much of this only works on native
6686 toolchains, since we rely on knowing the
6687 machine-dependent procfs structure in order to pick
c044fabd 6688 out details about the corefile. */
252b5132
RH
6689
6690#ifdef HAVE_SYS_PROCFS_H
6691# include <sys/procfs.h>
6692#endif
6693
c044fabd 6694/* FIXME: this is kinda wrong, but it's what gdb wants. */
252b5132
RH
6695
6696static int
217aa764 6697elfcore_make_pid (bfd *abfd)
252b5132
RH
6698{
6699 return ((elf_tdata (abfd)->core_lwpid << 16)
6700 + (elf_tdata (abfd)->core_pid));
6701}
6702
252b5132
RH
6703/* If there isn't a section called NAME, make one, using
6704 data from SECT. Note, this function will generate a
6705 reference to NAME, so you shouldn't deallocate or
c044fabd 6706 overwrite it. */
252b5132 6707
b34976b6 6708static bfd_boolean
217aa764 6709elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 6710{
c044fabd 6711 asection *sect2;
252b5132
RH
6712
6713 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 6714 return TRUE;
252b5132
RH
6715
6716 sect2 = bfd_make_section (abfd, name);
6717 if (sect2 == NULL)
b34976b6 6718 return FALSE;
252b5132 6719
eea6121a 6720 sect2->size = sect->size;
252b5132
RH
6721 sect2->filepos = sect->filepos;
6722 sect2->flags = sect->flags;
6723 sect2->alignment_power = sect->alignment_power;
b34976b6 6724 return TRUE;
252b5132
RH
6725}
6726
bb0082d6
AM
6727/* Create a pseudosection containing SIZE bytes at FILEPOS. This
6728 actually creates up to two pseudosections:
6729 - For the single-threaded case, a section named NAME, unless
6730 such a section already exists.
6731 - For the multi-threaded case, a section named "NAME/PID", where
6732 PID is elfcore_make_pid (abfd).
6733 Both pseudosections have identical contents. */
b34976b6 6734bfd_boolean
217aa764
AM
6735_bfd_elfcore_make_pseudosection (bfd *abfd,
6736 char *name,
6737 size_t size,
6738 ufile_ptr filepos)
bb0082d6
AM
6739{
6740 char buf[100];
6741 char *threaded_name;
d4c88bbb 6742 size_t len;
bb0082d6
AM
6743 asection *sect;
6744
6745 /* Build the section name. */
6746
6747 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 6748 len = strlen (buf) + 1;
217aa764 6749 threaded_name = bfd_alloc (abfd, len);
bb0082d6 6750 if (threaded_name == NULL)
b34976b6 6751 return FALSE;
d4c88bbb 6752 memcpy (threaded_name, buf, len);
bb0082d6 6753
62f3bb11 6754 sect = bfd_make_section_anyway (abfd, threaded_name);
bb0082d6 6755 if (sect == NULL)
b34976b6 6756 return FALSE;
eea6121a 6757 sect->size = size;
bb0082d6
AM
6758 sect->filepos = filepos;
6759 sect->flags = SEC_HAS_CONTENTS;
6760 sect->alignment_power = 2;
6761
936e320b 6762 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
6763}
6764
252b5132 6765/* prstatus_t exists on:
4a938328 6766 solaris 2.5+
252b5132
RH
6767 linux 2.[01] + glibc
6768 unixware 4.2
6769*/
6770
6771#if defined (HAVE_PRSTATUS_T)
a7b97311 6772
b34976b6 6773static bfd_boolean
217aa764 6774elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 6775{
eea6121a 6776 size_t size;
7ee38065 6777 int offset;
252b5132 6778
4a938328
MS
6779 if (note->descsz == sizeof (prstatus_t))
6780 {
6781 prstatus_t prstat;
252b5132 6782
eea6121a 6783 size = sizeof (prstat.pr_reg);
7ee38065 6784 offset = offsetof (prstatus_t, pr_reg);
4a938328 6785 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 6786
fa49d224
NC
6787 /* Do not overwrite the core signal if it
6788 has already been set by another thread. */
6789 if (elf_tdata (abfd)->core_signal == 0)
6790 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328 6791 elf_tdata (abfd)->core_pid = prstat.pr_pid;
252b5132 6792
4a938328
MS
6793 /* pr_who exists on:
6794 solaris 2.5+
6795 unixware 4.2
6796 pr_who doesn't exist on:
6797 linux 2.[01]
6798 */
252b5132 6799#if defined (HAVE_PRSTATUS_T_PR_WHO)
4a938328 6800 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
252b5132 6801#endif
4a938328 6802 }
7ee38065 6803#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
6804 else if (note->descsz == sizeof (prstatus32_t))
6805 {
6806 /* 64-bit host, 32-bit corefile */
6807 prstatus32_t prstat;
6808
eea6121a 6809 size = sizeof (prstat.pr_reg);
7ee38065 6810 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
6811 memcpy (&prstat, note->descdata, sizeof (prstat));
6812
fa49d224
NC
6813 /* Do not overwrite the core signal if it
6814 has already been set by another thread. */
6815 if (elf_tdata (abfd)->core_signal == 0)
6816 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4a938328
MS
6817 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6818
6819 /* pr_who exists on:
6820 solaris 2.5+
6821 unixware 4.2
6822 pr_who doesn't exist on:
6823 linux 2.[01]
6824 */
7ee38065 6825#if defined (HAVE_PRSTATUS32_T_PR_WHO)
4a938328
MS
6826 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6827#endif
6828 }
7ee38065 6829#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
6830 else
6831 {
6832 /* Fail - we don't know how to handle any other
6833 note size (ie. data object type). */
b34976b6 6834 return TRUE;
4a938328 6835 }
252b5132 6836
bb0082d6 6837 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 6838 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 6839 size, note->descpos + offset);
252b5132
RH
6840}
6841#endif /* defined (HAVE_PRSTATUS_T) */
6842
bb0082d6 6843/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 6844static bfd_boolean
217aa764
AM
6845elfcore_make_note_pseudosection (bfd *abfd,
6846 char *name,
6847 Elf_Internal_Note *note)
252b5132 6848{
936e320b
AM
6849 return _bfd_elfcore_make_pseudosection (abfd, name,
6850 note->descsz, note->descpos);
252b5132
RH
6851}
6852
ff08c6bb
JB
6853/* There isn't a consistent prfpregset_t across platforms,
6854 but it doesn't matter, because we don't have to pick this
c044fabd
KH
6855 data structure apart. */
6856
b34976b6 6857static bfd_boolean
217aa764 6858elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
6859{
6860 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6861}
6862
ff08c6bb
JB
6863/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6864 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6865 literally. */
c044fabd 6866
b34976b6 6867static bfd_boolean
217aa764 6868elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
6869{
6870 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6871}
6872
252b5132 6873#if defined (HAVE_PRPSINFO_T)
4a938328 6874typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 6875#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6876typedef prpsinfo32_t elfcore_psinfo32_t;
6877#endif
252b5132
RH
6878#endif
6879
6880#if defined (HAVE_PSINFO_T)
4a938328 6881typedef psinfo_t elfcore_psinfo_t;
7ee38065 6882#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
6883typedef psinfo32_t elfcore_psinfo32_t;
6884#endif
252b5132
RH
6885#endif
6886
252b5132
RH
6887/* return a malloc'ed copy of a string at START which is at
6888 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 6889 the copy will always have a terminating '\0'. */
252b5132 6890
936e320b 6891char *
217aa764 6892_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 6893{
dc810e39 6894 char *dups;
c044fabd 6895 char *end = memchr (start, '\0', max);
dc810e39 6896 size_t len;
252b5132
RH
6897
6898 if (end == NULL)
6899 len = max;
6900 else
6901 len = end - start;
6902
217aa764 6903 dups = bfd_alloc (abfd, len + 1);
dc810e39 6904 if (dups == NULL)
252b5132
RH
6905 return NULL;
6906
dc810e39
AM
6907 memcpy (dups, start, len);
6908 dups[len] = '\0';
252b5132 6909
dc810e39 6910 return dups;
252b5132
RH
6911}
6912
bb0082d6 6913#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 6914static bfd_boolean
217aa764 6915elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 6916{
4a938328
MS
6917 if (note->descsz == sizeof (elfcore_psinfo_t))
6918 {
6919 elfcore_psinfo_t psinfo;
252b5132 6920
7ee38065 6921 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6922
4a938328 6923 elf_tdata (abfd)->core_program
936e320b
AM
6924 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6925 sizeof (psinfo.pr_fname));
252b5132 6926
4a938328 6927 elf_tdata (abfd)->core_command
936e320b
AM
6928 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6929 sizeof (psinfo.pr_psargs));
4a938328 6930 }
7ee38065 6931#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
6932 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6933 {
6934 /* 64-bit host, 32-bit corefile */
6935 elfcore_psinfo32_t psinfo;
6936
7ee38065 6937 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 6938
4a938328 6939 elf_tdata (abfd)->core_program
936e320b
AM
6940 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6941 sizeof (psinfo.pr_fname));
4a938328
MS
6942
6943 elf_tdata (abfd)->core_command
936e320b
AM
6944 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6945 sizeof (psinfo.pr_psargs));
4a938328
MS
6946 }
6947#endif
6948
6949 else
6950 {
6951 /* Fail - we don't know how to handle any other
6952 note size (ie. data object type). */
b34976b6 6953 return TRUE;
4a938328 6954 }
252b5132
RH
6955
6956 /* Note that for some reason, a spurious space is tacked
6957 onto the end of the args in some (at least one anyway)
c044fabd 6958 implementations, so strip it off if it exists. */
252b5132
RH
6959
6960 {
c044fabd 6961 char *command = elf_tdata (abfd)->core_command;
252b5132
RH
6962 int n = strlen (command);
6963
6964 if (0 < n && command[n - 1] == ' ')
6965 command[n - 1] = '\0';
6966 }
6967
b34976b6 6968 return TRUE;
252b5132
RH
6969}
6970#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6971
252b5132 6972#if defined (HAVE_PSTATUS_T)
b34976b6 6973static bfd_boolean
217aa764 6974elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 6975{
f572a39d
AM
6976 if (note->descsz == sizeof (pstatus_t)
6977#if defined (HAVE_PXSTATUS_T)
6978 || note->descsz == sizeof (pxstatus_t)
6979#endif
6980 )
4a938328
MS
6981 {
6982 pstatus_t pstat;
252b5132 6983
4a938328 6984 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6985
4a938328
MS
6986 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6987 }
7ee38065 6988#if defined (HAVE_PSTATUS32_T)
4a938328
MS
6989 else if (note->descsz == sizeof (pstatus32_t))
6990 {
6991 /* 64-bit host, 32-bit corefile */
6992 pstatus32_t pstat;
252b5132 6993
4a938328 6994 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 6995
4a938328
MS
6996 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6997 }
6998#endif
252b5132
RH
6999 /* Could grab some more details from the "representative"
7000 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 7001 NT_LWPSTATUS note, presumably. */
252b5132 7002
b34976b6 7003 return TRUE;
252b5132
RH
7004}
7005#endif /* defined (HAVE_PSTATUS_T) */
7006
252b5132 7007#if defined (HAVE_LWPSTATUS_T)
b34976b6 7008static bfd_boolean
217aa764 7009elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
7010{
7011 lwpstatus_t lwpstat;
7012 char buf[100];
c044fabd 7013 char *name;
d4c88bbb 7014 size_t len;
c044fabd 7015 asection *sect;
252b5132 7016
f572a39d
AM
7017 if (note->descsz != sizeof (lwpstat)
7018#if defined (HAVE_LWPXSTATUS_T)
7019 && note->descsz != sizeof (lwpxstatus_t)
7020#endif
7021 )
b34976b6 7022 return TRUE;
252b5132
RH
7023
7024 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
7025
7026 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
7027 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
7028
c044fabd 7029 /* Make a ".reg/999" section. */
252b5132
RH
7030
7031 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 7032 len = strlen (buf) + 1;
217aa764 7033 name = bfd_alloc (abfd, len);
252b5132 7034 if (name == NULL)
b34976b6 7035 return FALSE;
d4c88bbb 7036 memcpy (name, buf, len);
252b5132 7037
62f3bb11 7038 sect = bfd_make_section_anyway (abfd, name);
252b5132 7039 if (sect == NULL)
b34976b6 7040 return FALSE;
252b5132
RH
7041
7042#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 7043 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
7044 sect->filepos = note->descpos
7045 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
7046#endif
7047
7048#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 7049 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
7050 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
7051#endif
7052
7053 sect->flags = SEC_HAS_CONTENTS;
7054 sect->alignment_power = 2;
7055
7056 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 7057 return FALSE;
252b5132
RH
7058
7059 /* Make a ".reg2/999" section */
7060
7061 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 7062 len = strlen (buf) + 1;
217aa764 7063 name = bfd_alloc (abfd, len);
252b5132 7064 if (name == NULL)
b34976b6 7065 return FALSE;
d4c88bbb 7066 memcpy (name, buf, len);
252b5132 7067
62f3bb11 7068 sect = bfd_make_section_anyway (abfd, name);
252b5132 7069 if (sect == NULL)
b34976b6 7070 return FALSE;
252b5132
RH
7071
7072#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 7073 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
7074 sect->filepos = note->descpos
7075 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
7076#endif
7077
7078#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 7079 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
7080 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
7081#endif
7082
7083 sect->flags = SEC_HAS_CONTENTS;
7084 sect->alignment_power = 2;
7085
936e320b 7086 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
7087}
7088#endif /* defined (HAVE_LWPSTATUS_T) */
7089
16e9c715 7090#if defined (HAVE_WIN32_PSTATUS_T)
b34976b6 7091static bfd_boolean
217aa764 7092elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
7093{
7094 char buf[30];
c044fabd 7095 char *name;
d4c88bbb 7096 size_t len;
c044fabd 7097 asection *sect;
16e9c715
NC
7098 win32_pstatus_t pstatus;
7099
7100 if (note->descsz < sizeof (pstatus))
b34976b6 7101 return TRUE;
16e9c715 7102
e8eab623 7103 memcpy (&pstatus, note->descdata, sizeof (pstatus));
c044fabd
KH
7104
7105 switch (pstatus.data_type)
16e9c715
NC
7106 {
7107 case NOTE_INFO_PROCESS:
7108 /* FIXME: need to add ->core_command. */
7109 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
7110 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
c044fabd 7111 break;
16e9c715
NC
7112
7113 case NOTE_INFO_THREAD:
7114 /* Make a ".reg/999" section. */
7115 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
c044fabd 7116
d4c88bbb 7117 len = strlen (buf) + 1;
217aa764 7118 name = bfd_alloc (abfd, len);
16e9c715 7119 if (name == NULL)
b34976b6 7120 return FALSE;
c044fabd 7121
d4c88bbb 7122 memcpy (name, buf, len);
16e9c715 7123
62f3bb11 7124 sect = bfd_make_section_anyway (abfd, name);
16e9c715 7125 if (sect == NULL)
b34976b6 7126 return FALSE;
c044fabd 7127
eea6121a 7128 sect->size = sizeof (pstatus.data.thread_info.thread_context);
079e9a2f
AM
7129 sect->filepos = (note->descpos
7130 + offsetof (struct win32_pstatus,
7131 data.thread_info.thread_context));
16e9c715
NC
7132 sect->flags = SEC_HAS_CONTENTS;
7133 sect->alignment_power = 2;
7134
7135 if (pstatus.data.thread_info.is_active_thread)
7136 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 7137 return FALSE;
16e9c715
NC
7138 break;
7139
7140 case NOTE_INFO_MODULE:
7141 /* Make a ".module/xxxxxxxx" section. */
c044fabd
KH
7142 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
7143
d4c88bbb 7144 len = strlen (buf) + 1;
217aa764 7145 name = bfd_alloc (abfd, len);
16e9c715 7146 if (name == NULL)
b34976b6 7147 return FALSE;
c044fabd 7148
d4c88bbb 7149 memcpy (name, buf, len);
252b5132 7150
62f3bb11 7151 sect = bfd_make_section_anyway (abfd, name);
c044fabd 7152
16e9c715 7153 if (sect == NULL)
b34976b6 7154 return FALSE;
c044fabd 7155
eea6121a 7156 sect->size = note->descsz;
16e9c715
NC
7157 sect->filepos = note->descpos;
7158 sect->flags = SEC_HAS_CONTENTS;
7159 sect->alignment_power = 2;
7160 break;
7161
7162 default:
b34976b6 7163 return TRUE;
16e9c715
NC
7164 }
7165
b34976b6 7166 return TRUE;
16e9c715
NC
7167}
7168#endif /* HAVE_WIN32_PSTATUS_T */
252b5132 7169
b34976b6 7170static bfd_boolean
217aa764 7171elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 7172{
9c5bfbb7 7173 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 7174
252b5132
RH
7175 switch (note->type)
7176 {
7177 default:
b34976b6 7178 return TRUE;
252b5132 7179
252b5132 7180 case NT_PRSTATUS:
bb0082d6
AM
7181 if (bed->elf_backend_grok_prstatus)
7182 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 7183 return TRUE;
bb0082d6 7184#if defined (HAVE_PRSTATUS_T)
252b5132 7185 return elfcore_grok_prstatus (abfd, note);
bb0082d6 7186#else
b34976b6 7187 return TRUE;
252b5132
RH
7188#endif
7189
7190#if defined (HAVE_PSTATUS_T)
7191 case NT_PSTATUS:
7192 return elfcore_grok_pstatus (abfd, note);
7193#endif
7194
7195#if defined (HAVE_LWPSTATUS_T)
7196 case NT_LWPSTATUS:
7197 return elfcore_grok_lwpstatus (abfd, note);
7198#endif
7199
7200 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
7201 return elfcore_grok_prfpreg (abfd, note);
7202
16e9c715 7203#if defined (HAVE_WIN32_PSTATUS_T)
c044fabd 7204 case NT_WIN32PSTATUS:
16e9c715
NC
7205 return elfcore_grok_win32pstatus (abfd, note);
7206#endif
7207
c044fabd 7208 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
7209 if (note->namesz == 6
7210 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
7211 return elfcore_grok_prxfpreg (abfd, note);
7212 else
b34976b6 7213 return TRUE;
ff08c6bb 7214
252b5132
RH
7215 case NT_PRPSINFO:
7216 case NT_PSINFO:
bb0082d6
AM
7217 if (bed->elf_backend_grok_psinfo)
7218 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 7219 return TRUE;
bb0082d6 7220#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 7221 return elfcore_grok_psinfo (abfd, note);
bb0082d6 7222#else
b34976b6 7223 return TRUE;
252b5132 7224#endif
3333a7c3
RM
7225
7226 case NT_AUXV:
7227 {
62f3bb11 7228 asection *sect = bfd_make_section_anyway (abfd, ".auxv");
3333a7c3
RM
7229
7230 if (sect == NULL)
7231 return FALSE;
eea6121a 7232 sect->size = note->descsz;
3333a7c3
RM
7233 sect->filepos = note->descpos;
7234 sect->flags = SEC_HAS_CONTENTS;
7235 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
7236
7237 return TRUE;
7238 }
252b5132
RH
7239 }
7240}
7241
b34976b6 7242static bfd_boolean
217aa764 7243elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
7244{
7245 char *cp;
7246
7247 cp = strchr (note->namedata, '@');
7248 if (cp != NULL)
7249 {
d2b64500 7250 *lwpidp = atoi(cp + 1);
b34976b6 7251 return TRUE;
50b2bdb7 7252 }
b34976b6 7253 return FALSE;
50b2bdb7
AM
7254}
7255
b34976b6 7256static bfd_boolean
217aa764 7257elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
7258{
7259
7260 /* Signal number at offset 0x08. */
7261 elf_tdata (abfd)->core_signal
7262 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
7263
7264 /* Process ID at offset 0x50. */
7265 elf_tdata (abfd)->core_pid
7266 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
7267
7268 /* Command name at 0x7c (max 32 bytes, including nul). */
7269 elf_tdata (abfd)->core_command
7270 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
7271
7720ba9f
MK
7272 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
7273 note);
50b2bdb7
AM
7274}
7275
b34976b6 7276static bfd_boolean
217aa764 7277elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
7278{
7279 int lwp;
7280
7281 if (elfcore_netbsd_get_lwpid (note, &lwp))
7282 elf_tdata (abfd)->core_lwpid = lwp;
7283
b4db1224 7284 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
7285 {
7286 /* NetBSD-specific core "procinfo". Note that we expect to
7287 find this note before any of the others, which is fine,
7288 since the kernel writes this note out first when it
7289 creates a core file. */
47d9a591 7290
50b2bdb7
AM
7291 return elfcore_grok_netbsd_procinfo (abfd, note);
7292 }
7293
b4db1224
JT
7294 /* As of Jan 2002 there are no other machine-independent notes
7295 defined for NetBSD core files. If the note type is less
7296 than the start of the machine-dependent note types, we don't
7297 understand it. */
47d9a591 7298
b4db1224 7299 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 7300 return TRUE;
50b2bdb7
AM
7301
7302
7303 switch (bfd_get_arch (abfd))
7304 {
7305 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
7306 PT_GETFPREGS == mach+2. */
7307
7308 case bfd_arch_alpha:
7309 case bfd_arch_sparc:
7310 switch (note->type)
7311 {
b4db1224 7312 case NT_NETBSDCORE_FIRSTMACH+0:
50b2bdb7
AM
7313 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7314
b4db1224 7315 case NT_NETBSDCORE_FIRSTMACH+2:
50b2bdb7
AM
7316 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7317
7318 default:
b34976b6 7319 return TRUE;
50b2bdb7
AM
7320 }
7321
7322 /* On all other arch's, PT_GETREGS == mach+1 and
7323 PT_GETFPREGS == mach+3. */
7324
7325 default:
7326 switch (note->type)
7327 {
b4db1224 7328 case NT_NETBSDCORE_FIRSTMACH+1:
50b2bdb7
AM
7329 return elfcore_make_note_pseudosection (abfd, ".reg", note);
7330
b4db1224 7331 case NT_NETBSDCORE_FIRSTMACH+3:
50b2bdb7
AM
7332 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
7333
7334 default:
b34976b6 7335 return TRUE;
50b2bdb7
AM
7336 }
7337 }
7338 /* NOTREACHED */
7339}
7340
07c6e936 7341static bfd_boolean
217aa764 7342elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid)
07c6e936
NC
7343{
7344 void *ddata = note->descdata;
7345 char buf[100];
7346 char *name;
7347 asection *sect;
f8843e87
AM
7348 short sig;
7349 unsigned flags;
07c6e936
NC
7350
7351 /* nto_procfs_status 'pid' field is at offset 0. */
7352 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
7353
f8843e87
AM
7354 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
7355 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
7356
7357 /* nto_procfs_status 'flags' field is at offset 8. */
7358 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
7359
7360 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
7361 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
7362 {
7363 elf_tdata (abfd)->core_signal = sig;
7364 elf_tdata (abfd)->core_lwpid = *tid;
7365 }
07c6e936 7366
f8843e87
AM
7367 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
7368 do not come from signals so we make sure we set the current
7369 thread just in case. */
7370 if (flags & 0x00000080)
7371 elf_tdata (abfd)->core_lwpid = *tid;
07c6e936
NC
7372
7373 /* Make a ".qnx_core_status/%d" section. */
7374 sprintf (buf, ".qnx_core_status/%d", *tid);
7375
217aa764 7376 name = bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
7377 if (name == NULL)
7378 return FALSE;
7379 strcpy (name, buf);
7380
62f3bb11 7381 sect = bfd_make_section_anyway (abfd, name);
07c6e936
NC
7382 if (sect == NULL)
7383 return FALSE;
7384
eea6121a 7385 sect->size = note->descsz;
07c6e936
NC
7386 sect->filepos = note->descpos;
7387 sect->flags = SEC_HAS_CONTENTS;
7388 sect->alignment_power = 2;
7389
7390 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
7391}
7392
7393static bfd_boolean
d69f560c
KW
7394elfcore_grok_nto_regs (bfd *abfd,
7395 Elf_Internal_Note *note,
7396 pid_t tid,
7397 char *base)
07c6e936
NC
7398{
7399 char buf[100];
7400 char *name;
7401 asection *sect;
7402
d69f560c
KW
7403 /* Make a "(base)/%d" section. */
7404 sprintf (buf, "%s/%d", base, tid);
07c6e936 7405
217aa764 7406 name = bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
7407 if (name == NULL)
7408 return FALSE;
7409 strcpy (name, buf);
7410
62f3bb11 7411 sect = bfd_make_section_anyway (abfd, name);
07c6e936
NC
7412 if (sect == NULL)
7413 return FALSE;
7414
eea6121a 7415 sect->size = note->descsz;
07c6e936
NC
7416 sect->filepos = note->descpos;
7417 sect->flags = SEC_HAS_CONTENTS;
7418 sect->alignment_power = 2;
7419
f8843e87
AM
7420 /* This is the current thread. */
7421 if (elf_tdata (abfd)->core_lwpid == tid)
d69f560c 7422 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
7423
7424 return TRUE;
07c6e936
NC
7425}
7426
7427#define BFD_QNT_CORE_INFO 7
7428#define BFD_QNT_CORE_STATUS 8
7429#define BFD_QNT_CORE_GREG 9
7430#define BFD_QNT_CORE_FPREG 10
7431
7432static bfd_boolean
217aa764 7433elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
7434{
7435 /* Every GREG section has a STATUS section before it. Store the
811072d8 7436 tid from the previous call to pass down to the next gregs
07c6e936
NC
7437 function. */
7438 static pid_t tid = 1;
7439
7440 switch (note->type)
7441 {
d69f560c
KW
7442 case BFD_QNT_CORE_INFO:
7443 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
7444 case BFD_QNT_CORE_STATUS:
7445 return elfcore_grok_nto_status (abfd, note, &tid);
7446 case BFD_QNT_CORE_GREG:
7447 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
7448 case BFD_QNT_CORE_FPREG:
7449 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
7450 default:
7451 return TRUE;
07c6e936
NC
7452 }
7453}
7454
7c76fa91
MS
7455/* Function: elfcore_write_note
7456
47d9a591 7457 Inputs:
7c76fa91
MS
7458 buffer to hold note
7459 name of note
7460 type of note
7461 data for note
7462 size of data for note
7463
7464 Return:
7465 End of buffer containing note. */
7466
7467char *
217aa764
AM
7468elfcore_write_note (bfd *abfd,
7469 char *buf,
7470 int *bufsiz,
7471 const char *name,
7472 int type,
7473 const void *input,
7474 int size)
7c76fa91
MS
7475{
7476 Elf_External_Note *xnp;
d4c88bbb
AM
7477 size_t namesz;
7478 size_t pad;
7479 size_t newspace;
7c76fa91
MS
7480 char *p, *dest;
7481
d4c88bbb
AM
7482 namesz = 0;
7483 pad = 0;
7484 if (name != NULL)
7485 {
9c5bfbb7 7486 const struct elf_backend_data *bed;
d4c88bbb
AM
7487
7488 namesz = strlen (name) + 1;
7489 bed = get_elf_backend_data (abfd);
45d6a902 7490 pad = -namesz & ((1 << bed->s->log_file_align) - 1);
d4c88bbb
AM
7491 }
7492
5de3bf90 7493 newspace = 12 + namesz + pad + size;
d4c88bbb 7494
7c76fa91
MS
7495 p = realloc (buf, *bufsiz + newspace);
7496 dest = p + *bufsiz;
7497 *bufsiz += newspace;
7498 xnp = (Elf_External_Note *) dest;
7499 H_PUT_32 (abfd, namesz, xnp->namesz);
7500 H_PUT_32 (abfd, size, xnp->descsz);
7501 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
7502 dest = xnp->name;
7503 if (name != NULL)
7504 {
7505 memcpy (dest, name, namesz);
7506 dest += namesz;
7507 while (pad != 0)
7508 {
7509 *dest++ = '\0';
7510 --pad;
7511 }
7512 }
7513 memcpy (dest, input, size);
7c76fa91
MS
7514 return p;
7515}
7516
7517#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7518char *
217aa764
AM
7519elfcore_write_prpsinfo (bfd *abfd,
7520 char *buf,
7521 int *bufsiz,
7522 const char *fname,
7523 const char *psargs)
7c76fa91
MS
7524{
7525 int note_type;
7526 char *note_name = "CORE";
7527
7528#if defined (HAVE_PSINFO_T)
7529 psinfo_t data;
7530 note_type = NT_PSINFO;
7531#else
7532 prpsinfo_t data;
7533 note_type = NT_PRPSINFO;
7534#endif
7535
7536 memset (&data, 0, sizeof (data));
7537 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7538 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
47d9a591 7539 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7540 note_name, note_type, &data, sizeof (data));
7541}
7542#endif /* PSINFO_T or PRPSINFO_T */
7543
7544#if defined (HAVE_PRSTATUS_T)
7545char *
217aa764
AM
7546elfcore_write_prstatus (bfd *abfd,
7547 char *buf,
7548 int *bufsiz,
7549 long pid,
7550 int cursig,
7551 const void *gregs)
7c76fa91
MS
7552{
7553 prstatus_t prstat;
7554 char *note_name = "CORE";
7555
7556 memset (&prstat, 0, sizeof (prstat));
7557 prstat.pr_pid = pid;
7558 prstat.pr_cursig = cursig;
c106e334 7559 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
47d9a591 7560 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7561 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7562}
7563#endif /* HAVE_PRSTATUS_T */
7564
51316059
MS
7565#if defined (HAVE_LWPSTATUS_T)
7566char *
217aa764
AM
7567elfcore_write_lwpstatus (bfd *abfd,
7568 char *buf,
7569 int *bufsiz,
7570 long pid,
7571 int cursig,
7572 const void *gregs)
51316059
MS
7573{
7574 lwpstatus_t lwpstat;
7575 char *note_name = "CORE";
7576
7577 memset (&lwpstat, 0, sizeof (lwpstat));
7578 lwpstat.pr_lwpid = pid >> 16;
7579 lwpstat.pr_cursig = cursig;
7580#if defined (HAVE_LWPSTATUS_T_PR_REG)
7581 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7582#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7583#if !defined(gregs)
7584 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7585 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7586#else
7587 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7588 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7589#endif
7590#endif
47d9a591 7591 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
7592 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7593}
7594#endif /* HAVE_LWPSTATUS_T */
7595
7c76fa91
MS
7596#if defined (HAVE_PSTATUS_T)
7597char *
217aa764
AM
7598elfcore_write_pstatus (bfd *abfd,
7599 char *buf,
7600 int *bufsiz,
7601 long pid,
7602 int cursig,
7603 const void *gregs)
7c76fa91
MS
7604{
7605 pstatus_t pstat;
7606 char *note_name = "CORE";
7607
51316059
MS
7608 memset (&pstat, 0, sizeof (pstat));
7609 pstat.pr_pid = pid & 0xffff;
47d9a591 7610 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
7611 NT_PSTATUS, &pstat, sizeof (pstat));
7612 return buf;
7c76fa91
MS
7613}
7614#endif /* HAVE_PSTATUS_T */
7615
7616char *
217aa764
AM
7617elfcore_write_prfpreg (bfd *abfd,
7618 char *buf,
7619 int *bufsiz,
7620 const void *fpregs,
7621 int size)
7c76fa91
MS
7622{
7623 char *note_name = "CORE";
47d9a591 7624 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7625 note_name, NT_FPREGSET, fpregs, size);
7626}
7627
7628char *
217aa764
AM
7629elfcore_write_prxfpreg (bfd *abfd,
7630 char *buf,
7631 int *bufsiz,
7632 const void *xfpregs,
7633 int size)
7c76fa91
MS
7634{
7635 char *note_name = "LINUX";
47d9a591 7636 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
7637 note_name, NT_PRXFPREG, xfpregs, size);
7638}
7639
b34976b6 7640static bfd_boolean
217aa764 7641elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
252b5132 7642{
c044fabd
KH
7643 char *buf;
7644 char *p;
252b5132
RH
7645
7646 if (size <= 0)
b34976b6 7647 return TRUE;
252b5132 7648
dc810e39 7649 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
b34976b6 7650 return FALSE;
252b5132 7651
dc810e39 7652 buf = bfd_malloc (size);
252b5132 7653 if (buf == NULL)
b34976b6 7654 return FALSE;
252b5132 7655
dc810e39 7656 if (bfd_bread (buf, size, abfd) != size)
252b5132
RH
7657 {
7658 error:
7659 free (buf);
b34976b6 7660 return FALSE;
252b5132
RH
7661 }
7662
7663 p = buf;
7664 while (p < buf + size)
7665 {
c044fabd
KH
7666 /* FIXME: bad alignment assumption. */
7667 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
7668 Elf_Internal_Note in;
7669
dc810e39 7670 in.type = H_GET_32 (abfd, xnp->type);
252b5132 7671
dc810e39 7672 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132
RH
7673 in.namedata = xnp->name;
7674
dc810e39 7675 in.descsz = H_GET_32 (abfd, xnp->descsz);
252b5132
RH
7676 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7677 in.descpos = offset + (in.descdata - buf);
7678
50b2bdb7
AM
7679 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7680 {
7681 if (! elfcore_grok_netbsd_note (abfd, &in))
7682 goto error;
7683 }
07c6e936
NC
7684 else if (strncmp (in.namedata, "QNX", 3) == 0)
7685 {
7686 if (! elfcore_grok_nto_note (abfd, &in))
7687 goto error;
7688 }
50b2bdb7
AM
7689 else
7690 {
7691 if (! elfcore_grok_note (abfd, &in))
7692 goto error;
7693 }
252b5132
RH
7694
7695 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7696 }
7697
7698 free (buf);
b34976b6 7699 return TRUE;
252b5132 7700}
98d8431c
JB
7701\f
7702/* Providing external access to the ELF program header table. */
7703
7704/* Return an upper bound on the number of bytes required to store a
7705 copy of ABFD's program header table entries. Return -1 if an error
7706 occurs; bfd_get_error will return an appropriate code. */
c044fabd 7707
98d8431c 7708long
217aa764 7709bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
7710{
7711 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7712 {
7713 bfd_set_error (bfd_error_wrong_format);
7714 return -1;
7715 }
7716
936e320b 7717 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
7718}
7719
98d8431c
JB
7720/* Copy ABFD's program header table entries to *PHDRS. The entries
7721 will be stored as an array of Elf_Internal_Phdr structures, as
7722 defined in include/elf/internal.h. To find out how large the
7723 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7724
7725 Return the number of program header table entries read, or -1 if an
7726 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 7727
98d8431c 7728int
217aa764 7729bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
7730{
7731 int num_phdrs;
7732
7733 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7734 {
7735 bfd_set_error (bfd_error_wrong_format);
7736 return -1;
7737 }
7738
7739 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 7740 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
7741 num_phdrs * sizeof (Elf_Internal_Phdr));
7742
7743 return num_phdrs;
7744}
ae4221d7
L
7745
7746void
217aa764 7747_bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
ae4221d7 7748{
d3b05f8d 7749#ifdef BFD64
ae4221d7
L
7750 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7751
7752 i_ehdrp = elf_elfheader (abfd);
7753 if (i_ehdrp == NULL)
7754 sprintf_vma (buf, value);
7755 else
7756 {
7757 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7758 {
ae4221d7 7759#if BFD_HOST_64BIT_LONG
cc55aec9 7760 sprintf (buf, "%016lx", value);
ae4221d7 7761#else
cc55aec9
AM
7762 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7763 _bfd_int64_low (value));
ae4221d7 7764#endif
cc55aec9 7765 }
ae4221d7
L
7766 else
7767 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7768 }
d3b05f8d
L
7769#else
7770 sprintf_vma (buf, value);
7771#endif
ae4221d7
L
7772}
7773
7774void
217aa764 7775_bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
ae4221d7 7776{
d3b05f8d 7777#ifdef BFD64
ae4221d7
L
7778 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7779
7780 i_ehdrp = elf_elfheader (abfd);
7781 if (i_ehdrp == NULL)
7782 fprintf_vma ((FILE *) stream, value);
7783 else
7784 {
7785 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
cc55aec9 7786 {
ae4221d7 7787#if BFD_HOST_64BIT_LONG
cc55aec9 7788 fprintf ((FILE *) stream, "%016lx", value);
ae4221d7 7789#else
cc55aec9
AM
7790 fprintf ((FILE *) stream, "%08lx%08lx",
7791 _bfd_int64_high (value), _bfd_int64_low (value));
ae4221d7 7792#endif
cc55aec9 7793 }
ae4221d7
L
7794 else
7795 fprintf ((FILE *) stream, "%08lx",
7796 (unsigned long) (value & 0xffffffff));
7797 }
d3b05f8d
L
7798#else
7799 fprintf_vma ((FILE *) stream, value);
7800#endif
ae4221d7 7801}
db6751f2
JJ
7802
7803enum elf_reloc_type_class
217aa764 7804_bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
7805{
7806 return reloc_class_normal;
7807}
f8df10f4 7808
47d9a591 7809/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
7810 relocation against a local symbol. */
7811
7812bfd_vma
217aa764
AM
7813_bfd_elf_rela_local_sym (bfd *abfd,
7814 Elf_Internal_Sym *sym,
8517fae7 7815 asection **psec,
217aa764 7816 Elf_Internal_Rela *rel)
f8df10f4 7817{
8517fae7 7818 asection *sec = *psec;
f8df10f4
JJ
7819 bfd_vma relocation;
7820
7821 relocation = (sec->output_section->vma
7822 + sec->output_offset
7823 + sym->st_value);
7824 if ((sec->flags & SEC_MERGE)
c629eae0 7825 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
68bfbfcc 7826 && sec->sec_info_type == ELF_INFO_TYPE_MERGE)
f8df10f4 7827 {
f8df10f4 7828 rel->r_addend =
8517fae7 7829 _bfd_merged_section_offset (abfd, psec,
65765700 7830 elf_section_data (sec)->sec_info,
753731ee
AM
7831 sym->st_value + rel->r_addend);
7832 if (sec != *psec)
7833 {
7834 /* If we have changed the section, and our original section is
7835 marked with SEC_EXCLUDE, it means that the original
7836 SEC_MERGE section has been completely subsumed in some
7837 other SEC_MERGE section. In this case, we need to leave
7838 some info around for --emit-relocs. */
7839 if ((sec->flags & SEC_EXCLUDE) != 0)
7840 sec->kept_section = *psec;
7841 sec = *psec;
7842 }
8517fae7
AM
7843 rel->r_addend -= relocation;
7844 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
7845 }
7846 return relocation;
7847}
c629eae0
JJ
7848
7849bfd_vma
217aa764
AM
7850_bfd_elf_rel_local_sym (bfd *abfd,
7851 Elf_Internal_Sym *sym,
7852 asection **psec,
7853 bfd_vma addend)
47d9a591 7854{
c629eae0
JJ
7855 asection *sec = *psec;
7856
68bfbfcc 7857 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE)
c629eae0
JJ
7858 return sym->st_value + addend;
7859
7860 return _bfd_merged_section_offset (abfd, psec,
65765700 7861 elf_section_data (sec)->sec_info,
753731ee 7862 sym->st_value + addend);
c629eae0
JJ
7863}
7864
7865bfd_vma
217aa764 7866_bfd_elf_section_offset (bfd *abfd,
92e4ec35 7867 struct bfd_link_info *info,
217aa764
AM
7868 asection *sec,
7869 bfd_vma offset)
c629eae0 7870{
68bfbfcc 7871 switch (sec->sec_info_type)
65765700
JJ
7872 {
7873 case ELF_INFO_TYPE_STABS:
eea6121a
AM
7874 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
7875 offset);
65765700 7876 case ELF_INFO_TYPE_EH_FRAME:
92e4ec35 7877 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
65765700
JJ
7878 default:
7879 return offset;
7880 }
c629eae0 7881}
3333a7c3
RM
7882\f
7883/* Create a new BFD as if by bfd_openr. Rather than opening a file,
7884 reconstruct an ELF file by reading the segments out of remote memory
7885 based on the ELF file header at EHDR_VMA and the ELF program headers it
7886 points to. If not null, *LOADBASEP is filled in with the difference
7887 between the VMAs from which the segments were read, and the VMAs the
7888 file headers (and hence BFD's idea of each section's VMA) put them at.
7889
7890 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
7891 remote memory at target address VMA into the local buffer at MYADDR; it
7892 should return zero on success or an `errno' code on failure. TEMPL must
7893 be a BFD for an ELF target with the word size and byte order found in
7894 the remote memory. */
7895
7896bfd *
217aa764
AM
7897bfd_elf_bfd_from_remote_memory
7898 (bfd *templ,
7899 bfd_vma ehdr_vma,
7900 bfd_vma *loadbasep,
7901 int (*target_read_memory) (bfd_vma, char *, int))
3333a7c3
RM
7902{
7903 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
7904 (templ, ehdr_vma, loadbasep, target_read_memory);
7905}
4c45e5c9
JJ
7906\f
7907long
c9727e01
AM
7908_bfd_elf_get_synthetic_symtab (bfd *abfd,
7909 long symcount ATTRIBUTE_UNUSED,
7910 asymbol **syms ATTRIBUTE_UNUSED,
7911 long dynsymcount ATTRIBUTE_UNUSED,
7912 asymbol **dynsyms,
7913 asymbol **ret)
4c45e5c9
JJ
7914{
7915 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7916 asection *relplt;
7917 asymbol *s;
7918 const char *relplt_name;
7919 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7920 arelent *p;
7921 long count, i, n;
7922 size_t size;
7923 Elf_Internal_Shdr *hdr;
7924 char *names;
7925 asection *plt;
7926
90e3cdf2
JJ
7927 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
7928 return 0;
7929
4c45e5c9
JJ
7930 *ret = NULL;
7931 if (!bed->plt_sym_val)
7932 return 0;
7933
7934 relplt_name = bed->relplt_name;
7935 if (relplt_name == NULL)
7936 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt";
7937 relplt = bfd_get_section_by_name (abfd, relplt_name);
7938 if (relplt == NULL)
7939 return 0;
7940
7941 hdr = &elf_section_data (relplt)->this_hdr;
7942 if (hdr->sh_link != elf_dynsymtab (abfd)
7943 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
7944 return 0;
7945
7946 plt = bfd_get_section_by_name (abfd, ".plt");
7947 if (plt == NULL)
7948 return 0;
7949
7950 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 7951 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
7952 return -1;
7953
eea6121a 7954 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
7955 size = count * sizeof (asymbol);
7956 p = relplt->relocation;
7957 for (i = 0; i < count; i++, s++, p++)
7958 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
7959
7960 s = *ret = bfd_malloc (size);
7961 if (s == NULL)
7962 return -1;
7963
7964 names = (char *) (s + count);
7965 p = relplt->relocation;
7966 n = 0;
7967 for (i = 0; i < count; i++, s++, p++)
7968 {
7969 size_t len;
7970 bfd_vma addr;
7971
7972 addr = bed->plt_sym_val (i, plt, p);
7973 if (addr == (bfd_vma) -1)
7974 continue;
7975
7976 *s = **p->sym_ptr_ptr;
7977 s->section = plt;
7978 s->value = addr - plt->vma;
7979 s->name = names;
7980 len = strlen ((*p->sym_ptr_ptr)->name);
7981 memcpy (names, (*p->sym_ptr_ptr)->name, len);
7982 names += len;
7983 memcpy (names, "@plt", sizeof ("@plt"));
7984 names += sizeof ("@plt");
7985 ++n;
7986 }
7987
7988 return n;
7989}
3d7f7666
L
7990
7991/* Sort symbol by binding and section. We want to put definitions
7992 sorted by section at the beginning. */
7993
7994static int
7995elf_sort_elf_symbol (const void *arg1, const void *arg2)
7996{
7997 const Elf_Internal_Sym *s1;
7998 const Elf_Internal_Sym *s2;
7999 int shndx;
8000
8001 /* Make sure that undefined symbols are at the end. */
8002 s1 = (const Elf_Internal_Sym *) arg1;
8003 if (s1->st_shndx == SHN_UNDEF)
8004 return 1;
8005 s2 = (const Elf_Internal_Sym *) arg2;
8006 if (s2->st_shndx == SHN_UNDEF)
8007 return -1;
8008
8009 /* Sorted by section index. */
8010 shndx = s1->st_shndx - s2->st_shndx;
8011 if (shndx != 0)
8012 return shndx;
8013
8014 /* Sorted by binding. */
8015 return ELF_ST_BIND (s1->st_info) - ELF_ST_BIND (s2->st_info);
8016}
8017
8018struct elf_symbol
8019{
8020 Elf_Internal_Sym *sym;
8021 const char *name;
8022};
8023
8024static int
8025elf_sym_name_compare (const void *arg1, const void *arg2)
8026{
8027 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
8028 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
8029 return strcmp (s1->name, s2->name);
8030}
8031
8032/* Check if 2 sections define the same set of local and global
8033 symbols. */
8034
8035bfd_boolean
8036bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2)
8037{
8038 bfd *bfd1, *bfd2;
8039 const struct elf_backend_data *bed1, *bed2;
8040 Elf_Internal_Shdr *hdr1, *hdr2;
8041 bfd_size_type symcount1, symcount2;
8042 Elf_Internal_Sym *isymbuf1, *isymbuf2;
8043 Elf_Internal_Sym *isymstart1 = NULL, *isymstart2 = NULL, *isym;
8044 Elf_Internal_Sym *isymend;
8045 struct elf_symbol *symp, *symtable1 = NULL, *symtable2 = NULL;
8046 bfd_size_type count1, count2, i;
8047 int shndx1, shndx2;
8048 bfd_boolean result;
8049
8050 bfd1 = sec1->owner;
8051 bfd2 = sec2->owner;
8052
8053 /* If both are .gnu.linkonce sections, they have to have the same
8054 section name. */
8055 if (strncmp (sec1->name, ".gnu.linkonce",
8056 sizeof ".gnu.linkonce" - 1) == 0
8057 && strncmp (sec2->name, ".gnu.linkonce",
8058 sizeof ".gnu.linkonce" - 1) == 0)
8059 return strcmp (sec1->name + sizeof ".gnu.linkonce",
8060 sec2->name + sizeof ".gnu.linkonce") == 0;
8061
8062 /* Both sections have to be in ELF. */
8063 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
8064 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
8065 return FALSE;
8066
8067 if (elf_section_type (sec1) != elf_section_type (sec2))
8068 return FALSE;
8069
8070 if ((elf_section_flags (sec1) & SHF_GROUP) != 0
8071 && (elf_section_flags (sec2) & SHF_GROUP) != 0)
8072 {
8073 /* If both are members of section groups, they have to have the
8074 same group name. */
8075 if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0)
8076 return FALSE;
8077 }
8078
8079 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
8080 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
8081 if (shndx1 == -1 || shndx2 == -1)
8082 return FALSE;
8083
8084 bed1 = get_elf_backend_data (bfd1);
8085 bed2 = get_elf_backend_data (bfd2);
8086 hdr1 = &elf_tdata (bfd1)->symtab_hdr;
8087 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
8088 hdr2 = &elf_tdata (bfd2)->symtab_hdr;
8089 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
8090
8091 if (symcount1 == 0 || symcount2 == 0)
8092 return FALSE;
8093
8094 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
8095 NULL, NULL, NULL);
8096 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
8097 NULL, NULL, NULL);
8098
8099 result = FALSE;
8100 if (isymbuf1 == NULL || isymbuf2 == NULL)
8101 goto done;
8102
8103 /* Sort symbols by binding and section. Global definitions are at
8104 the beginning. */
8105 qsort (isymbuf1, symcount1, sizeof (Elf_Internal_Sym),
8106 elf_sort_elf_symbol);
8107 qsort (isymbuf2, symcount2, sizeof (Elf_Internal_Sym),
8108 elf_sort_elf_symbol);
8109
8110 /* Count definitions in the section. */
8111 count1 = 0;
8112 for (isym = isymbuf1, isymend = isym + symcount1;
8113 isym < isymend; isym++)
8114 {
8115 if (isym->st_shndx == (unsigned int) shndx1)
8116 {
8117 if (count1 == 0)
8118 isymstart1 = isym;
8119 count1++;
8120 }
8121
8122 if (count1 && isym->st_shndx != (unsigned int) shndx1)
8123 break;
8124 }
8125
8126 count2 = 0;
8127 for (isym = isymbuf2, isymend = isym + symcount2;
8128 isym < isymend; isym++)
8129 {
8130 if (isym->st_shndx == (unsigned int) shndx2)
8131 {
8132 if (count2 == 0)
8133 isymstart2 = isym;
8134 count2++;
8135 }
8136
8137 if (count2 && isym->st_shndx != (unsigned int) shndx2)
8138 break;
8139 }
8140
8141 if (count1 == 0 || count2 == 0 || count1 != count2)
8142 goto done;
8143
8144 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol));
8145 symtable2 = bfd_malloc (count1 * sizeof (struct elf_symbol));
8146
8147 if (symtable1 == NULL || symtable2 == NULL)
8148 goto done;
8149
8150 symp = symtable1;
8151 for (isym = isymstart1, isymend = isym + count1;
8152 isym < isymend; isym++)
8153 {
8154 symp->sym = isym;
8155 symp->name = bfd_elf_string_from_elf_section (bfd1,
8156 hdr1->sh_link,
8157 isym->st_name);
8158 symp++;
8159 }
8160
8161 symp = symtable2;
8162 for (isym = isymstart2, isymend = isym + count1;
8163 isym < isymend; isym++)
8164 {
8165 symp->sym = isym;
8166 symp->name = bfd_elf_string_from_elf_section (bfd2,
8167 hdr2->sh_link,
8168 isym->st_name);
8169 symp++;
8170 }
8171
8172 /* Sort symbol by name. */
8173 qsort (symtable1, count1, sizeof (struct elf_symbol),
8174 elf_sym_name_compare);
8175 qsort (symtable2, count1, sizeof (struct elf_symbol),
8176 elf_sym_name_compare);
8177
8178 for (i = 0; i < count1; i++)
8179 /* Two symbols must have the same binding, type and name. */
8180 if (symtable1 [i].sym->st_info != symtable2 [i].sym->st_info
8181 || symtable1 [i].sym->st_other != symtable2 [i].sym->st_other
8182 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
8183 goto done;
8184
8185 result = TRUE;
8186
8187done:
8188 if (symtable1)
8189 free (symtable1);
8190 if (symtable2)
8191 free (symtable2);
8192 if (isymbuf1)
8193 free (isymbuf1);
8194 if (isymbuf2)
8195 free (isymbuf2);
8196
8197 return result;
8198}
This page took 0.829354 seconds and 4 git commands to generate.