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