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