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