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