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