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