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