]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
Fix compile time warning introduced by previous delta.
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
252b5132 1/* ELF executable support for BFD.
340b6d91 2
219d1afa 3 Copyright (C) 1993-2018 Free Software Foundation, Inc.
252b5132 4
5e8d7549 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
5e8d7549
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
5e8d7549 10 (at your option) any later version.
252b5132 11
5e8d7549
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
5e8d7549 17 You should have received a copy of the GNU General Public License
b34976b6 18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132 22
1b74d094
BW
23/*
24SECTION
252b5132
RH
25 ELF backends
26
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
30
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
661a3fd4 33 haven't bothered yet. */
252b5132 34
7ee38065
MS
35/* For sparc64-cross-sparc32. */
36#define _SYSCALL32
252b5132 37#include "sysdep.h"
3db64b00 38#include "bfd.h"
252b5132
RH
39#include "bfdlink.h"
40#include "libbfd.h"
41#define ARCH_SIZE 0
42#include "elf-bfd.h"
e0e8c97f 43#include "libiberty.h"
ff59fc36 44#include "safe-ctype.h"
de64ce13 45#include "elf-linux-core.h"
252b5132 46
8bc7f138
L
47#ifdef CORE_HEADER
48#include CORE_HEADER
49#endif
50
217aa764 51static int elf_sort_sections (const void *, const void *);
c84fca4d 52static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
217aa764 53static bfd_boolean prep_headers (bfd *);
ef10c3ac 54static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
276da9b3
L
55static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type,
56 size_t align) ;
718175fa 57static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
276da9b3 58 file_ptr offset, size_t align);
50b2bdb7 59
252b5132
RH
60/* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
63
64/* Swap in a Verdef structure. */
65
66void
217aa764
AM
67_bfd_elf_swap_verdef_in (bfd *abfd,
68 const Elf_External_Verdef *src,
69 Elf_Internal_Verdef *dst)
252b5132 70{
dc810e39
AM
71 dst->vd_version = H_GET_16 (abfd, src->vd_version);
72 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
73 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
74 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
75 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
76 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
77 dst->vd_next = H_GET_32 (abfd, src->vd_next);
252b5132
RH
78}
79
80/* Swap out a Verdef structure. */
81
82void
217aa764
AM
83_bfd_elf_swap_verdef_out (bfd *abfd,
84 const Elf_Internal_Verdef *src,
85 Elf_External_Verdef *dst)
252b5132 86{
dc810e39
AM
87 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
88 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
89 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
92 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
93 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
252b5132
RH
94}
95
96/* Swap in a Verdaux structure. */
97
98void
217aa764
AM
99_bfd_elf_swap_verdaux_in (bfd *abfd,
100 const Elf_External_Verdaux *src,
101 Elf_Internal_Verdaux *dst)
252b5132 102{
dc810e39
AM
103 dst->vda_name = H_GET_32 (abfd, src->vda_name);
104 dst->vda_next = H_GET_32 (abfd, src->vda_next);
252b5132
RH
105}
106
107/* Swap out a Verdaux structure. */
108
109void
217aa764
AM
110_bfd_elf_swap_verdaux_out (bfd *abfd,
111 const Elf_Internal_Verdaux *src,
112 Elf_External_Verdaux *dst)
252b5132 113{
dc810e39
AM
114 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
115 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
252b5132
RH
116}
117
118/* Swap in a Verneed structure. */
119
120void
217aa764
AM
121_bfd_elf_swap_verneed_in (bfd *abfd,
122 const Elf_External_Verneed *src,
123 Elf_Internal_Verneed *dst)
252b5132 124{
dc810e39
AM
125 dst->vn_version = H_GET_16 (abfd, src->vn_version);
126 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
127 dst->vn_file = H_GET_32 (abfd, src->vn_file);
128 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
129 dst->vn_next = H_GET_32 (abfd, src->vn_next);
252b5132
RH
130}
131
132/* Swap out a Verneed structure. */
133
134void
217aa764
AM
135_bfd_elf_swap_verneed_out (bfd *abfd,
136 const Elf_Internal_Verneed *src,
137 Elf_External_Verneed *dst)
252b5132 138{
dc810e39
AM
139 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
140 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
141 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
142 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
143 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
252b5132
RH
144}
145
146/* Swap in a Vernaux structure. */
147
148void
217aa764
AM
149_bfd_elf_swap_vernaux_in (bfd *abfd,
150 const Elf_External_Vernaux *src,
151 Elf_Internal_Vernaux *dst)
252b5132 152{
dc810e39
AM
153 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
154 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
155 dst->vna_other = H_GET_16 (abfd, src->vna_other);
156 dst->vna_name = H_GET_32 (abfd, src->vna_name);
157 dst->vna_next = H_GET_32 (abfd, src->vna_next);
252b5132
RH
158}
159
160/* Swap out a Vernaux structure. */
161
162void
217aa764
AM
163_bfd_elf_swap_vernaux_out (bfd *abfd,
164 const Elf_Internal_Vernaux *src,
165 Elf_External_Vernaux *dst)
252b5132 166{
dc810e39
AM
167 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
168 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
169 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
170 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
171 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
252b5132
RH
172}
173
174/* Swap in a Versym structure. */
175
176void
217aa764
AM
177_bfd_elf_swap_versym_in (bfd *abfd,
178 const Elf_External_Versym *src,
179 Elf_Internal_Versym *dst)
252b5132 180{
dc810e39 181 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252b5132
RH
182}
183
184/* Swap out a Versym structure. */
185
186void
217aa764
AM
187_bfd_elf_swap_versym_out (bfd *abfd,
188 const Elf_Internal_Versym *src,
189 Elf_External_Versym *dst)
252b5132 190{
dc810e39 191 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
252b5132
RH
192}
193
194/* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
3a99b017 196
252b5132 197unsigned long
217aa764 198bfd_elf_hash (const char *namearg)
252b5132 199{
3a99b017 200 const unsigned char *name = (const unsigned char *) namearg;
252b5132
RH
201 unsigned long h = 0;
202 unsigned long g;
203 int ch;
204
205 while ((ch = *name++) != '\0')
206 {
207 h = (h << 4) + ch;
208 if ((g = (h & 0xf0000000)) != 0)
209 {
210 h ^= g >> 24;
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
213 h ^= g;
214 }
215 }
32dfa85d 216 return h & 0xffffffff;
252b5132
RH
217}
218
fdc90cb4
JJ
219/* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
221
222unsigned long
223bfd_elf_gnu_hash (const char *namearg)
224{
225 const unsigned char *name = (const unsigned char *) namearg;
226 unsigned long h = 5381;
227 unsigned char ch;
228
229 while ((ch = *name++) != '\0')
230 h = (h << 5) + h + ch;
231 return h & 0xffffffff;
232}
233
0c8d6e5c
AM
234/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
b34976b6 236bfd_boolean
0c8d6e5c 237bfd_elf_allocate_object (bfd *abfd,
0ffa91dd 238 size_t object_size,
4dfe6ac6 239 enum elf_target_id object_id)
252b5132 240{
0ffa91dd
NC
241 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
242 abfd->tdata.any = bfd_zalloc (abfd, object_size);
243 if (abfd->tdata.any == NULL)
244 return FALSE;
252b5132 245
0ffa91dd 246 elf_object_id (abfd) = object_id;
c0355132
AM
247 if (abfd->direction != read_direction)
248 {
249 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
250 if (o == NULL)
251 return FALSE;
252 elf_tdata (abfd)->o = o;
253 elf_program_header_size (abfd) = (bfd_size_type) -1;
254 }
b34976b6 255 return TRUE;
252b5132
RH
256}
257
0ffa91dd
NC
258
259bfd_boolean
ae95ffa6 260bfd_elf_make_object (bfd *abfd)
0ffa91dd 261{
ae95ffa6 262 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
0ffa91dd 263 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
ae95ffa6 264 bed->target_id);
0ffa91dd
NC
265}
266
b34976b6 267bfd_boolean
217aa764 268bfd_elf_mkcorefile (bfd *abfd)
252b5132 269{
c044fabd 270 /* I think this can be done just like an object file. */
228e534f
AM
271 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
272 return FALSE;
273 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
274 return elf_tdata (abfd)->core != NULL;
252b5132
RH
275}
276
72a80a16 277static char *
217aa764 278bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
252b5132
RH
279{
280 Elf_Internal_Shdr **i_shdrp;
f075ee0c 281 bfd_byte *shstrtab = NULL;
dc810e39
AM
282 file_ptr offset;
283 bfd_size_type shstrtabsize;
252b5132
RH
284
285 i_shdrp = elf_elfsections (abfd);
74f2e02b
AM
286 if (i_shdrp == 0
287 || shindex >= elf_numsections (abfd)
288 || i_shdrp[shindex] == 0)
f075ee0c 289 return NULL;
252b5132 290
f075ee0c 291 shstrtab = i_shdrp[shindex]->contents;
252b5132
RH
292 if (shstrtab == NULL)
293 {
c044fabd 294 /* No cached one, attempt to read, and cache what we read. */
252b5132
RH
295 offset = i_shdrp[shindex]->sh_offset;
296 shstrtabsize = i_shdrp[shindex]->sh_size;
c6c60d09
JJ
297
298 /* Allocate and clear an extra byte at the end, to prevent crashes
299 in case the string table is not terminated. */
3471d59d 300 if (shstrtabsize + 1 <= 1
06614111
NC
301 || bfd_seek (abfd, offset, SEEK_SET) != 0
302 || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
c6c60d09
JJ
303 shstrtab = NULL;
304 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
305 {
306 if (bfd_get_error () != bfd_error_system_call)
307 bfd_set_error (bfd_error_file_truncated);
06614111 308 bfd_release (abfd, shstrtab);
c6c60d09 309 shstrtab = NULL;
3471d59d
CC
310 /* Once we've failed to read it, make sure we don't keep
311 trying. Otherwise, we'll keep allocating space for
312 the string table over and over. */
313 i_shdrp[shindex]->sh_size = 0;
c6c60d09
JJ
314 }
315 else
316 shstrtab[shstrtabsize] = '\0';
217aa764 317 i_shdrp[shindex]->contents = shstrtab;
252b5132 318 }
f075ee0c 319 return (char *) shstrtab;
252b5132
RH
320}
321
322char *
217aa764
AM
323bfd_elf_string_from_elf_section (bfd *abfd,
324 unsigned int shindex,
325 unsigned int strindex)
252b5132
RH
326{
327 Elf_Internal_Shdr *hdr;
328
329 if (strindex == 0)
330 return "";
331
74f2e02b
AM
332 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
333 return NULL;
334
252b5132
RH
335 hdr = elf_elfsections (abfd)[shindex];
336
06614111
NC
337 if (hdr->contents == NULL)
338 {
339 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
340 {
341 /* PR 17512: file: f057ec89. */
695344c0 342 /* xgettext:c-format */
63a5468a
AM
343 _bfd_error_handler (_("%B: attempt to load strings from"
344 " a non-string section (number %d)"),
06614111
NC
345 abfd, shindex);
346 return NULL;
347 }
b1fa9dd6 348
06614111
NC
349 if (bfd_elf_get_str_section (abfd, shindex) == NULL)
350 return NULL;
351 }
252b5132
RH
352
353 if (strindex >= hdr->sh_size)
354 {
1b3a8575 355 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
4eca0228 356 _bfd_error_handler
695344c0 357 /* xgettext:c-format */
d42c267e
AM
358 (_("%B: invalid string offset %u >= %Lu for section `%s'"),
359 abfd, strindex, hdr->sh_size,
1b3a8575 360 (shindex == shstrndx && strindex == hdr->sh_name
252b5132 361 ? ".shstrtab"
1b3a8575 362 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
45b222d6 363 return NULL;
252b5132
RH
364 }
365
366 return ((char *) hdr->contents) + strindex;
367}
368
6cdc0ccc
AM
369/* Read and convert symbols to internal format.
370 SYMCOUNT specifies the number of symbols to read, starting from
371 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
372 are non-NULL, they are used to store the internal symbols, external
b7c368d0
NC
373 symbols, and symbol section index extensions, respectively.
374 Returns a pointer to the internal symbol buffer (malloced if necessary)
375 or NULL if there were no symbols or some kind of problem. */
6cdc0ccc
AM
376
377Elf_Internal_Sym *
217aa764
AM
378bfd_elf_get_elf_syms (bfd *ibfd,
379 Elf_Internal_Shdr *symtab_hdr,
380 size_t symcount,
381 size_t symoffset,
382 Elf_Internal_Sym *intsym_buf,
383 void *extsym_buf,
384 Elf_External_Sym_Shndx *extshndx_buf)
6cdc0ccc
AM
385{
386 Elf_Internal_Shdr *shndx_hdr;
217aa764 387 void *alloc_ext;
df622259 388 const bfd_byte *esym;
6cdc0ccc
AM
389 Elf_External_Sym_Shndx *alloc_extshndx;
390 Elf_External_Sym_Shndx *shndx;
4dd07732 391 Elf_Internal_Sym *alloc_intsym;
6cdc0ccc
AM
392 Elf_Internal_Sym *isym;
393 Elf_Internal_Sym *isymend;
9c5bfbb7 394 const struct elf_backend_data *bed;
6cdc0ccc
AM
395 size_t extsym_size;
396 bfd_size_type amt;
397 file_ptr pos;
398
e44a2c9c
AM
399 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
400 abort ();
401
6cdc0ccc
AM
402 if (symcount == 0)
403 return intsym_buf;
404
405 /* Normal syms might have section extension entries. */
406 shndx_hdr = NULL;
6a40cf0c
NC
407 if (elf_symtab_shndx_list (ibfd) != NULL)
408 {
409 elf_section_list * entry;
410 Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
411
412 /* Find an index section that is linked to this symtab section. */
413 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
315350be
NC
414 {
415 /* PR 20063. */
416 if (entry->hdr.sh_link >= elf_numsections (ibfd))
417 continue;
418
419 if (sections[entry->hdr.sh_link] == symtab_hdr)
420 {
421 shndx_hdr = & entry->hdr;
422 break;
423 };
424 }
6a40cf0c
NC
425
426 if (shndx_hdr == NULL)
427 {
428 if (symtab_hdr == & elf_symtab_hdr (ibfd))
429 /* Not really accurate, but this was how the old code used to work. */
430 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
431 /* Otherwise we do nothing. The assumption is that
432 the index table will not be needed. */
433 }
434 }
6cdc0ccc
AM
435
436 /* Read the symbols. */
437 alloc_ext = NULL;
438 alloc_extshndx = NULL;
4dd07732 439 alloc_intsym = NULL;
6cdc0ccc
AM
440 bed = get_elf_backend_data (ibfd);
441 extsym_size = bed->s->sizeof_sym;
ef53be89 442 amt = (bfd_size_type) symcount * extsym_size;
6cdc0ccc
AM
443 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
444 if (extsym_buf == NULL)
445 {
d0fb9a8d 446 alloc_ext = bfd_malloc2 (symcount, extsym_size);
6cdc0ccc
AM
447 extsym_buf = alloc_ext;
448 }
449 if (extsym_buf == NULL
450 || bfd_seek (ibfd, pos, SEEK_SET) != 0
451 || bfd_bread (extsym_buf, amt, ibfd) != amt)
452 {
453 intsym_buf = NULL;
454 goto out;
455 }
456
457 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
458 extshndx_buf = NULL;
459 else
460 {
ef53be89 461 amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx);
6cdc0ccc
AM
462 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
463 if (extshndx_buf == NULL)
464 {
a50b1753 465 alloc_extshndx = (Elf_External_Sym_Shndx *)
07d6d2b8 466 bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
6cdc0ccc
AM
467 extshndx_buf = alloc_extshndx;
468 }
469 if (extshndx_buf == NULL
470 || bfd_seek (ibfd, pos, SEEK_SET) != 0
471 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
472 {
473 intsym_buf = NULL;
474 goto out;
475 }
476 }
477
478 if (intsym_buf == NULL)
479 {
a50b1753 480 alloc_intsym = (Elf_Internal_Sym *)
07d6d2b8 481 bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
4dd07732 482 intsym_buf = alloc_intsym;
6cdc0ccc
AM
483 if (intsym_buf == NULL)
484 goto out;
485 }
486
487 /* Convert the symbols to internal form. */
488 isymend = intsym_buf + symcount;
a50b1753 489 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
07d6d2b8 490 shndx = extshndx_buf;
6cdc0ccc
AM
491 isym < isymend;
492 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
8384fb8f
AM
493 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
494 {
495 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
695344c0 496 /* xgettext:c-format */
63a5468a
AM
497 _bfd_error_handler (_("%B symbol number %lu references"
498 " nonexistent SHT_SYMTAB_SHNDX section"),
4eca0228 499 ibfd, (unsigned long) symoffset);
4dd07732
AM
500 if (alloc_intsym != NULL)
501 free (alloc_intsym);
8384fb8f
AM
502 intsym_buf = NULL;
503 goto out;
504 }
6cdc0ccc
AM
505
506 out:
507 if (alloc_ext != NULL)
508 free (alloc_ext);
509 if (alloc_extshndx != NULL)
510 free (alloc_extshndx);
511
512 return intsym_buf;
513}
514
5cab59f6
AM
515/* Look up a symbol name. */
516const char *
be8dd2ca
AM
517bfd_elf_sym_name (bfd *abfd,
518 Elf_Internal_Shdr *symtab_hdr,
26c61ae5
L
519 Elf_Internal_Sym *isym,
520 asection *sym_sec)
5cab59f6 521{
26c61ae5 522 const char *name;
5cab59f6 523 unsigned int iname = isym->st_name;
be8dd2ca 524 unsigned int shindex = symtab_hdr->sh_link;
26c61ae5 525
138f35cc
JJ
526 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
527 /* Check for a bogus st_shndx to avoid crashing. */
4fbb74a6 528 && isym->st_shndx < elf_numsections (abfd))
5cab59f6
AM
529 {
530 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
531 shindex = elf_elfheader (abfd)->e_shstrndx;
532 }
533
26c61ae5
L
534 name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
535 if (name == NULL)
536 name = "(null)";
537 else if (sym_sec && *name == '\0')
538 name = bfd_section_name (abfd, sym_sec);
539
540 return name;
5cab59f6
AM
541}
542
dbb410c3
AM
543/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
544 sections. The first element is the flags, the rest are section
545 pointers. */
546
547typedef union elf_internal_group {
548 Elf_Internal_Shdr *shdr;
549 unsigned int flags;
550} Elf_Internal_Group;
551
b885599b
AM
552/* Return the name of the group signature symbol. Why isn't the
553 signature just a string? */
554
555static const char *
217aa764 556group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
b885599b 557{
9dce4196 558 Elf_Internal_Shdr *hdr;
9dce4196
AM
559 unsigned char esym[sizeof (Elf64_External_Sym)];
560 Elf_External_Sym_Shndx eshndx;
561 Elf_Internal_Sym isym;
b885599b 562
13792e9d
L
563 /* First we need to ensure the symbol table is available. Make sure
564 that it is a symbol table section. */
4fbb74a6
AM
565 if (ghdr->sh_link >= elf_numsections (abfd))
566 return NULL;
13792e9d
L
567 hdr = elf_elfsections (abfd) [ghdr->sh_link];
568 if (hdr->sh_type != SHT_SYMTAB
569 || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
b885599b
AM
570 return NULL;
571
9dce4196
AM
572 /* Go read the symbol. */
573 hdr = &elf_tdata (abfd)->symtab_hdr;
6cdc0ccc
AM
574 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
575 &isym, esym, &eshndx) == NULL)
b885599b 576 return NULL;
9dce4196 577
26c61ae5 578 return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
b885599b
AM
579}
580
dbb410c3
AM
581/* Set next_in_group list pointer, and group name for NEWSECT. */
582
b34976b6 583static bfd_boolean
217aa764 584setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
dbb410c3
AM
585{
586 unsigned int num_group = elf_tdata (abfd)->num_group;
587
588 /* If num_group is zero, read in all SHT_GROUP sections. The count
589 is set to -1 if there are no SHT_GROUP sections. */
590 if (num_group == 0)
591 {
592 unsigned int i, shnum;
593
594 /* First count the number of groups. If we have a SHT_GROUP
595 section with just a flag word (ie. sh_size is 4), ignore it. */
9ad5cbcf 596 shnum = elf_numsections (abfd);
dbb410c3 597 num_group = 0;
08a40648 598
44534af3 599#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
1783205a 600 ( (shdr)->sh_type == SHT_GROUP \
44534af3 601 && (shdr)->sh_size >= minsize \
1783205a
NC
602 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
603 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
08a40648 604
dbb410c3
AM
605 for (i = 0; i < shnum; i++)
606 {
607 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 608
44534af3 609 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3
AM
610 num_group += 1;
611 }
612
613 if (num_group == 0)
20dbb49d
L
614 {
615 num_group = (unsigned) -1;
616 elf_tdata (abfd)->num_group = num_group;
ce497010 617 elf_tdata (abfd)->group_sect_ptr = NULL;
20dbb49d
L
618 }
619 else
dbb410c3
AM
620 {
621 /* We keep a list of elf section headers for group sections,
622 so we can find them quickly. */
20dbb49d 623 bfd_size_type amt;
d0fb9a8d 624
20dbb49d 625 elf_tdata (abfd)->num_group = num_group;
a50b1753 626 elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
07d6d2b8 627 bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
dbb410c3 628 if (elf_tdata (abfd)->group_sect_ptr == NULL)
b34976b6 629 return FALSE;
ce497010 630 memset (elf_tdata (abfd)->group_sect_ptr, 0, num_group * sizeof (Elf_Internal_Shdr *));
dbb410c3 631 num_group = 0;
ce497010 632
dbb410c3
AM
633 for (i = 0; i < shnum; i++)
634 {
635 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
1783205a 636
44534af3 637 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
dbb410c3 638 {
973ffd63 639 unsigned char *src;
dbb410c3
AM
640 Elf_Internal_Group *dest;
641
07d6d2b8
AM
642 /* Make sure the group section has a BFD section
643 attached to it. */
644 if (!bfd_section_from_shdr (abfd, i))
645 return FALSE;
646
dbb410c3
AM
647 /* Add to list of sections. */
648 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
649 num_group += 1;
650
651 /* Read the raw contents. */
652 BFD_ASSERT (sizeof (*dest) >= 4);
653 amt = shdr->sh_size * sizeof (*dest) / 4;
a50b1753 654 shdr->contents = (unsigned char *)
07d6d2b8 655 bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
1783205a
NC
656 /* PR binutils/4110: Handle corrupt group headers. */
657 if (shdr->contents == NULL)
658 {
659 _bfd_error_handler
695344c0 660 /* xgettext:c-format */
63a5468a 661 (_("%B: corrupt size field in group section"
76cfced5 662 " header: %#Lx"), abfd, shdr->sh_size);
1783205a 663 bfd_set_error (bfd_error_bad_value);
493a3386
NC
664 -- num_group;
665 continue;
1783205a
NC
666 }
667
668 memset (shdr->contents, 0, amt);
669
670 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
dbb410c3
AM
671 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
672 != shdr->sh_size))
493a3386
NC
673 {
674 _bfd_error_handler
695344c0 675 /* xgettext:c-format */
63a5468a 676 (_("%B: invalid size field in group section"
76cfced5 677 " header: %#Lx"), abfd, shdr->sh_size);
493a3386
NC
678 bfd_set_error (bfd_error_bad_value);
679 -- num_group;
63a5468a
AM
680 /* PR 17510: If the group contents are even
681 partially corrupt, do not allow any of the
682 contents to be used. */
493a3386
NC
683 memset (shdr->contents, 0, amt);
684 continue;
685 }
708d7d0d 686
dbb410c3
AM
687 /* Translate raw contents, a flag word followed by an
688 array of elf section indices all in target byte order,
689 to the flag word followed by an array of elf section
690 pointers. */
691 src = shdr->contents + shdr->sh_size;
692 dest = (Elf_Internal_Group *) (shdr->contents + amt);
06614111 693
dbb410c3
AM
694 while (1)
695 {
696 unsigned int idx;
697
698 src -= 4;
699 --dest;
700 idx = H_GET_32 (abfd, src);
701 if (src == shdr->contents)
702 {
703 dest->flags = idx;
b885599b
AM
704 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
705 shdr->bfd_section->flags
706 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
dbb410c3
AM
707 break;
708 }
709 if (idx >= shnum)
710 {
4eca0228
AM
711 _bfd_error_handler
712 (_("%B: invalid SHT_GROUP entry"), abfd);
dbb410c3
AM
713 idx = 0;
714 }
715 dest->shdr = elf_elfsections (abfd)[idx];
716 }
717 }
718 }
493a3386
NC
719
720 /* PR 17510: Corrupt binaries might contain invalid groups. */
721 if (num_group != (unsigned) elf_tdata (abfd)->num_group)
722 {
723 elf_tdata (abfd)->num_group = num_group;
724
725 /* If all groups are invalid then fail. */
726 if (num_group == 0)
727 {
728 elf_tdata (abfd)->group_sect_ptr = NULL;
729 elf_tdata (abfd)->num_group = num_group = -1;
4eca0228
AM
730 _bfd_error_handler
731 (_("%B: no valid group sections found"), abfd);
493a3386
NC
732 bfd_set_error (bfd_error_bad_value);
733 }
734 }
dbb410c3
AM
735 }
736 }
737
738 if (num_group != (unsigned) -1)
739 {
740 unsigned int i;
741
742 for (i = 0; i < num_group; i++)
743 {
744 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
ce497010 745 Elf_Internal_Group *idx;
0c54f692 746 bfd_size_type n_elt;
ce497010
NC
747
748 if (shdr == NULL)
749 continue;
750
751 idx = (Elf_Internal_Group *) shdr->contents;
0c54f692
NC
752 if (idx == NULL || shdr->sh_size < 4)
753 {
754 /* See PR 21957 for a reproducer. */
755 /* xgettext:c-format */
756 _bfd_error_handler (_("%B: group section '%A' has no contents"),
757 abfd, shdr->bfd_section);
758 elf_tdata (abfd)->group_sect_ptr[i] = NULL;
759 bfd_set_error (bfd_error_bad_value);
760 return FALSE;
761 }
ce497010 762 n_elt = shdr->sh_size / 4;
dbb410c3
AM
763
764 /* Look through this group's sections to see if current
765 section is a member. */
766 while (--n_elt != 0)
767 if ((++idx)->shdr == hdr)
768 {
e0e8c97f 769 asection *s = NULL;
dbb410c3
AM
770
771 /* We are a member of this group. Go looking through
772 other members to see if any others are linked via
773 next_in_group. */
774 idx = (Elf_Internal_Group *) shdr->contents;
775 n_elt = shdr->sh_size / 4;
776 while (--n_elt != 0)
777 if ((s = (++idx)->shdr->bfd_section) != NULL
945906ff 778 && elf_next_in_group (s) != NULL)
dbb410c3
AM
779 break;
780 if (n_elt != 0)
781 {
dbb410c3
AM
782 /* Snarf the group name from other member, and
783 insert current section in circular list. */
945906ff
AM
784 elf_group_name (newsect) = elf_group_name (s);
785 elf_next_in_group (newsect) = elf_next_in_group (s);
786 elf_next_in_group (s) = newsect;
dbb410c3
AM
787 }
788 else
789 {
dbb410c3
AM
790 const char *gname;
791
b885599b
AM
792 gname = group_signature (abfd, shdr);
793 if (gname == NULL)
b34976b6 794 return FALSE;
945906ff 795 elf_group_name (newsect) = gname;
dbb410c3
AM
796
797 /* Start a circular list with one element. */
945906ff 798 elf_next_in_group (newsect) = newsect;
dbb410c3 799 }
b885599b 800
9dce4196
AM
801 /* If the group section has been created, point to the
802 new member. */
dbb410c3 803 if (shdr->bfd_section != NULL)
945906ff 804 elf_next_in_group (shdr->bfd_section) = newsect;
b885599b 805
dbb410c3
AM
806 i = num_group - 1;
807 break;
808 }
809 }
810 }
811
945906ff 812 if (elf_group_name (newsect) == NULL)
dbb410c3 813 {
695344c0 814 /* xgettext:c-format */
0c54f692 815 _bfd_error_handler (_("%B: no group info for section '%A'"),
4eca0228 816 abfd, newsect);
493a3386 817 return FALSE;
dbb410c3 818 }
b34976b6 819 return TRUE;
dbb410c3
AM
820}
821
3d7f7666 822bfd_boolean
dd863624 823_bfd_elf_setup_sections (bfd *abfd)
3d7f7666
L
824{
825 unsigned int i;
826 unsigned int num_group = elf_tdata (abfd)->num_group;
827 bfd_boolean result = TRUE;
dd863624
L
828 asection *s;
829
830 /* Process SHF_LINK_ORDER. */
831 for (s = abfd->sections; s != NULL; s = s->next)
832 {
833 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
834 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
835 {
836 unsigned int elfsec = this_hdr->sh_link;
837 /* FIXME: The old Intel compiler and old strip/objcopy may
838 not set the sh_link or sh_info fields. Hence we could
839 get the situation where elfsec is 0. */
840 if (elfsec == 0)
841 {
4fbb74a6 842 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
dd863624
L
843 if (bed->link_order_error_handler)
844 bed->link_order_error_handler
695344c0 845 /* xgettext:c-format */
dd863624
L
846 (_("%B: warning: sh_link not set for section `%A'"),
847 abfd, s);
848 }
849 else
850 {
91d6fa6a 851 asection *linksec = NULL;
25bbc984 852
4fbb74a6
AM
853 if (elfsec < elf_numsections (abfd))
854 {
855 this_hdr = elf_elfsections (abfd)[elfsec];
91d6fa6a 856 linksec = this_hdr->bfd_section;
4fbb74a6 857 }
25bbc984
L
858
859 /* PR 1991, 2008:
860 Some strip/objcopy may leave an incorrect value in
861 sh_link. We don't want to proceed. */
91d6fa6a 862 if (linksec == NULL)
25bbc984 863 {
4eca0228 864 _bfd_error_handler
695344c0 865 /* xgettext:c-format */
25bbc984 866 (_("%B: sh_link [%d] in section `%A' is incorrect"),
c08bb8dd 867 s->owner, elfsec, s);
25bbc984
L
868 result = FALSE;
869 }
870
91d6fa6a 871 elf_linked_to_section (s) = linksec;
dd863624
L
872 }
873 }
53720c49
AM
874 else if (this_hdr->sh_type == SHT_GROUP
875 && elf_next_in_group (s) == NULL)
876 {
4eca0228 877 _bfd_error_handler
695344c0 878 /* xgettext:c-format */
53720c49
AM
879 (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
880 abfd, elf_section_data (s)->this_idx);
881 result = FALSE;
882 }
dd863624 883 }
3d7f7666 884
dd863624 885 /* Process section groups. */
3d7f7666
L
886 if (num_group == (unsigned) -1)
887 return result;
888
889 for (i = 0; i < num_group; i++)
890 {
891 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
4b0e8a5f
NC
892 Elf_Internal_Group *idx;
893 unsigned int n_elt;
3d7f7666 894
4b0e8a5f
NC
895 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
896 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
897 {
4eca0228 898 _bfd_error_handler
695344c0 899 /* xgettext:c-format */
4b0e8a5f
NC
900 (_("%B: section group entry number %u is corrupt"),
901 abfd, i);
902 result = FALSE;
903 continue;
904 }
905
906 idx = (Elf_Internal_Group *) shdr->contents;
907 n_elt = shdr->sh_size / 4;
1b786873 908
3d7f7666 909 while (--n_elt != 0)
24d3e51b
NC
910 {
911 ++ idx;
912
913 if (idx->shdr == NULL)
914 continue;
915 else if (idx->shdr->bfd_section)
916 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
db4677b8
AM
917 else if (idx->shdr->sh_type != SHT_RELA
918 && idx->shdr->sh_type != SHT_REL)
24d3e51b
NC
919 {
920 /* There are some unknown sections in the group. */
921 _bfd_error_handler
922 /* xgettext:c-format */
923 (_("%B: unknown type [%#x] section `%s' in group [%A]"),
924 abfd,
925 idx->shdr->sh_type,
926 bfd_elf_string_from_elf_section (abfd,
927 (elf_elfheader (abfd)
928 ->e_shstrndx),
929 idx->shdr->sh_name),
930 shdr->bfd_section);
931 result = FALSE;
932 }
933 }
3d7f7666 934 }
24d3e51b 935
3d7f7666
L
936 return result;
937}
938
72adc230
AM
939bfd_boolean
940bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
941{
942 return elf_next_in_group (sec) != NULL;
943}
944
f6fe1ccd
L
945static char *
946convert_debug_to_zdebug (bfd *abfd, const char *name)
947{
948 unsigned int len = strlen (name);
949 char *new_name = bfd_alloc (abfd, len + 2);
950 if (new_name == NULL)
951 return NULL;
952 new_name[0] = '.';
953 new_name[1] = 'z';
954 memcpy (new_name + 2, name + 1, len);
955 return new_name;
956}
957
958static char *
959convert_zdebug_to_debug (bfd *abfd, const char *name)
960{
961 unsigned int len = strlen (name);
962 char *new_name = bfd_alloc (abfd, len);
963 if (new_name == NULL)
964 return NULL;
965 new_name[0] = '.';
966 memcpy (new_name + 1, name + 2, len - 1);
967 return new_name;
968}
969
252b5132
RH
970/* Make a BFD section from an ELF section. We store a pointer to the
971 BFD section in the bfd_section field of the header. */
972
b34976b6 973bfd_boolean
217aa764
AM
974_bfd_elf_make_section_from_shdr (bfd *abfd,
975 Elf_Internal_Shdr *hdr,
6dc132d9
L
976 const char *name,
977 int shindex)
252b5132
RH
978{
979 asection *newsect;
980 flagword flags;
9c5bfbb7 981 const struct elf_backend_data *bed;
252b5132
RH
982
983 if (hdr->bfd_section != NULL)
4e011fb5 984 return TRUE;
252b5132
RH
985
986 newsect = bfd_make_section_anyway (abfd, name);
987 if (newsect == NULL)
b34976b6 988 return FALSE;
252b5132 989
1829f4b2
AM
990 hdr->bfd_section = newsect;
991 elf_section_data (newsect)->this_hdr = *hdr;
6dc132d9 992 elf_section_data (newsect)->this_idx = shindex;
1829f4b2 993
2f89ff8d
L
994 /* Always use the real type/flags. */
995 elf_section_type (newsect) = hdr->sh_type;
996 elf_section_flags (newsect) = hdr->sh_flags;
997
252b5132
RH
998 newsect->filepos = hdr->sh_offset;
999
1000 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
1001 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
1002 || ! bfd_set_section_alignment (abfd, newsect,
72de5009 1003 bfd_log2 (hdr->sh_addralign)))
b34976b6 1004 return FALSE;
252b5132
RH
1005
1006 flags = SEC_NO_FLAGS;
1007 if (hdr->sh_type != SHT_NOBITS)
1008 flags |= SEC_HAS_CONTENTS;
dbb410c3 1009 if (hdr->sh_type == SHT_GROUP)
7bdf4127 1010 flags |= SEC_GROUP;
252b5132
RH
1011 if ((hdr->sh_flags & SHF_ALLOC) != 0)
1012 {
1013 flags |= SEC_ALLOC;
1014 if (hdr->sh_type != SHT_NOBITS)
1015 flags |= SEC_LOAD;
1016 }
1017 if ((hdr->sh_flags & SHF_WRITE) == 0)
1018 flags |= SEC_READONLY;
1019 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
1020 flags |= SEC_CODE;
1021 else if ((flags & SEC_LOAD) != 0)
1022 flags |= SEC_DATA;
f5fa8ca2
JJ
1023 if ((hdr->sh_flags & SHF_MERGE) != 0)
1024 {
1025 flags |= SEC_MERGE;
1026 newsect->entsize = hdr->sh_entsize;
f5fa8ca2 1027 }
84865015
NC
1028 if ((hdr->sh_flags & SHF_STRINGS) != 0)
1029 flags |= SEC_STRINGS;
dbb410c3
AM
1030 if (hdr->sh_flags & SHF_GROUP)
1031 if (!setup_group (abfd, hdr, newsect))
b34976b6 1032 return FALSE;
13ae64f3
JJ
1033 if ((hdr->sh_flags & SHF_TLS) != 0)
1034 flags |= SEC_THREAD_LOCAL;
18ae9cc1
L
1035 if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
1036 flags |= SEC_EXCLUDE;
252b5132 1037
3d2b39cf 1038 if ((flags & SEC_ALLOC) == 0)
7a6cc5fb 1039 {
3d2b39cf
L
1040 /* The debugging sections appear to be recognized only by name,
1041 not any sort of flag. Their SEC_ALLOC bits are cleared. */
3d2b39cf
L
1042 if (name [0] == '.')
1043 {
f073ced3
AM
1044 const char *p;
1045 int n;
1046 if (name[1] == 'd')
1047 p = ".debug", n = 6;
1048 else if (name[1] == 'g' && name[2] == 'n')
1049 p = ".gnu.linkonce.wi.", n = 17;
1050 else if (name[1] == 'g' && name[2] == 'd')
1051 p = ".gdb_index", n = 11; /* yes we really do mean 11. */
1052 else if (name[1] == 'l')
1053 p = ".line", n = 5;
1054 else if (name[1] == 's')
1055 p = ".stab", n = 5;
1056 else if (name[1] == 'z')
1057 p = ".zdebug", n = 7;
1058 else
1059 p = NULL, n = 0;
1060 if (p != NULL && strncmp (name, p, n) == 0)
3d2b39cf
L
1061 flags |= SEC_DEBUGGING;
1062 }
1063 }
252b5132
RH
1064
1065 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1066 only link a single copy of the section. This is used to support
1067 g++. g++ will emit each template expansion in its own section.
1068 The symbols will be defined as weak, so that multiple definitions
1069 are permitted. The GNU linker extension is to actually discard
1070 all but one of the sections. */
0112cd26 1071 if (CONST_STRNEQ (name, ".gnu.linkonce")
b885599b 1072 && elf_next_in_group (newsect) == NULL)
252b5132
RH
1073 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1074
fa152c49
JW
1075 bed = get_elf_backend_data (abfd);
1076 if (bed->elf_backend_section_flags)
1077 if (! bed->elf_backend_section_flags (&flags, hdr))
b34976b6 1078 return FALSE;
fa152c49 1079
252b5132 1080 if (! bfd_set_section_flags (abfd, newsect, flags))
b34976b6 1081 return FALSE;
252b5132 1082
718175fa
JK
1083 /* We do not parse the PT_NOTE segments as we are interested even in the
1084 separate debug info files which may have the segments offsets corrupted.
1085 PT_NOTEs from the core files are currently not parsed using BFD. */
1086 if (hdr->sh_type == SHT_NOTE)
1087 {
baea7ef1 1088 bfd_byte *contents;
718175fa 1089
baea7ef1 1090 if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
718175fa
JK
1091 return FALSE;
1092
276da9b3
L
1093 elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1094 hdr->sh_offset, hdr->sh_addralign);
718175fa
JK
1095 free (contents);
1096 }
1097
252b5132
RH
1098 if ((flags & SEC_ALLOC) != 0)
1099 {
1100 Elf_Internal_Phdr *phdr;
6ffd7900
AM
1101 unsigned int i, nload;
1102
1103 /* Some ELF linkers produce binaries with all the program header
1104 p_paddr fields zero. If we have such a binary with more than
1105 one PT_LOAD header, then leave the section lma equal to vma
1106 so that we don't create sections with overlapping lma. */
1107 phdr = elf_tdata (abfd)->phdr;
1108 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1109 if (phdr->p_paddr != 0)
1110 break;
1111 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1112 ++nload;
1113 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1114 return TRUE;
252b5132 1115
252b5132
RH
1116 phdr = elf_tdata (abfd)->phdr;
1117 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1118 {
86b2281f
AM
1119 if (((phdr->p_type == PT_LOAD
1120 && (hdr->sh_flags & SHF_TLS) == 0)
1121 || phdr->p_type == PT_TLS)
9a83a553 1122 && ELF_SECTION_IN_SEGMENT (hdr, phdr))
252b5132 1123 {
88967714
AM
1124 if ((flags & SEC_LOAD) == 0)
1125 newsect->lma = (phdr->p_paddr
1126 + hdr->sh_addr - phdr->p_vaddr);
1127 else
1128 /* We used to use the same adjustment for SEC_LOAD
1129 sections, but that doesn't work if the segment
1130 is packed with code from multiple VMAs.
1131 Instead we calculate the section LMA based on
1132 the segment LMA. It is assumed that the
1133 segment will contain sections with contiguous
1134 LMAs, even if the VMAs are not. */
1135 newsect->lma = (phdr->p_paddr
1136 + hdr->sh_offset - phdr->p_offset);
1137
1138 /* With contiguous segments, we can't tell from file
1139 offsets whether a section with zero size should
1140 be placed at the end of one segment or the
1141 beginning of the next. Decide based on vaddr. */
1142 if (hdr->sh_addr >= phdr->p_vaddr
1143 && (hdr->sh_addr + hdr->sh_size
1144 <= phdr->p_vaddr + phdr->p_memsz))
1145 break;
252b5132
RH
1146 }
1147 }
1148 }
1149
4a114e3e
L
1150 /* Compress/decompress DWARF debug sections with names: .debug_* and
1151 .zdebug_*, after the section flags is set. */
1152 if ((flags & SEC_DEBUGGING)
1153 && ((name[1] == 'd' && name[6] == '_')
1154 || (name[1] == 'z' && name[7] == '_')))
1155 {
1156 enum { nothing, compress, decompress } action = nothing;
151411f8 1157 int compression_header_size;
dab394de 1158 bfd_size_type uncompressed_size;
151411f8
L
1159 bfd_boolean compressed
1160 = bfd_is_section_compressed_with_header (abfd, newsect,
dab394de
L
1161 &compression_header_size,
1162 &uncompressed_size);
4a114e3e 1163
151411f8 1164 if (compressed)
4a114e3e
L
1165 {
1166 /* Compressed section. Check if we should decompress. */
1167 if ((abfd->flags & BFD_DECOMPRESS))
1168 action = decompress;
1169 }
151411f8
L
1170
1171 /* Compress the uncompressed section or convert from/to .zdebug*
1172 section. Check if we should compress. */
1173 if (action == nothing)
4a114e3e 1174 {
151411f8
L
1175 if (newsect->size != 0
1176 && (abfd->flags & BFD_COMPRESS)
1177 && compression_header_size >= 0
dab394de 1178 && uncompressed_size > 0
151411f8
L
1179 && (!compressed
1180 || ((compression_header_size > 0)
1181 != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
4a114e3e 1182 action = compress;
151411f8
L
1183 else
1184 return TRUE;
4a114e3e
L
1185 }
1186
151411f8 1187 if (action == compress)
4a114e3e 1188 {
4a114e3e
L
1189 if (!bfd_init_section_compress_status (abfd, newsect))
1190 {
4eca0228 1191 _bfd_error_handler
695344c0 1192 /* xgettext:c-format */
bc823199 1193 (_("%B: unable to initialize compress status for section %s"),
4a114e3e
L
1194 abfd, name);
1195 return FALSE;
1196 }
151411f8
L
1197 }
1198 else
1199 {
4a114e3e
L
1200 if (!bfd_init_section_decompress_status (abfd, newsect))
1201 {
4eca0228 1202 _bfd_error_handler
695344c0 1203 /* xgettext:c-format */
bc823199 1204 (_("%B: unable to initialize decompress status for section %s"),
4a114e3e
L
1205 abfd, name);
1206 return FALSE;
1207 }
151411f8
L
1208 }
1209
f6fe1ccd 1210 if (abfd->is_linker_input)
151411f8 1211 {
f6fe1ccd
L
1212 if (name[1] == 'z'
1213 && (action == decompress
1214 || (action == compress
1215 && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
4e011fb5 1216 {
f6fe1ccd
L
1217 /* Convert section name from .zdebug_* to .debug_* so
1218 that linker will consider this section as a debug
1219 section. */
1220 char *new_name = convert_zdebug_to_debug (abfd, name);
151411f8
L
1221 if (new_name == NULL)
1222 return FALSE;
f6fe1ccd 1223 bfd_rename_section (abfd, newsect, new_name);
151411f8 1224 }
4a114e3e 1225 }
f6fe1ccd
L
1226 else
1227 /* For objdump, don't rename the section. For objcopy, delay
1228 section rename to elf_fake_sections. */
1229 newsect->flags |= SEC_ELF_RENAME;
4a114e3e
L
1230 }
1231
b34976b6 1232 return TRUE;
252b5132
RH
1233}
1234
84865015
NC
1235const char *const bfd_elf_section_type_names[] =
1236{
252b5132
RH
1237 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1238 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1239 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1240};
1241
1049f94e 1242/* ELF relocs are against symbols. If we are producing relocatable
252b5132
RH
1243 output, and the reloc is against an external symbol, and nothing
1244 has given us any additional addend, the resulting reloc will also
1245 be against the same symbol. In such a case, we don't want to
1246 change anything about the way the reloc is handled, since it will
1247 all be done at final link time. Rather than put special case code
1248 into bfd_perform_relocation, all the reloc types use this howto
1249 function. It just short circuits the reloc if producing
1049f94e 1250 relocatable output against an external symbol. */
252b5132 1251
252b5132 1252bfd_reloc_status_type
217aa764
AM
1253bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1254 arelent *reloc_entry,
1255 asymbol *symbol,
1256 void *data ATTRIBUTE_UNUSED,
1257 asection *input_section,
1258 bfd *output_bfd,
1259 char **error_message ATTRIBUTE_UNUSED)
1260{
1261 if (output_bfd != NULL
252b5132
RH
1262 && (symbol->flags & BSF_SECTION_SYM) == 0
1263 && (! reloc_entry->howto->partial_inplace
1264 || reloc_entry->addend == 0))
1265 {
1266 reloc_entry->address += input_section->output_offset;
1267 return bfd_reloc_ok;
1268 }
1269
1270 return bfd_reloc_continue;
1271}
1272\f
84865015
NC
1273/* Returns TRUE if section A matches section B.
1274 Names, addresses and links may be different, but everything else
1275 should be the same. */
1276
1277static bfd_boolean
5522f910
NC
1278section_match (const Elf_Internal_Shdr * a,
1279 const Elf_Internal_Shdr * b)
84865015
NC
1280{
1281 return
07d6d2b8 1282 a->sh_type == b->sh_type
5522f910
NC
1283 && (a->sh_flags & ~ SHF_INFO_LINK)
1284 == (b->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1285 && a->sh_addralign == b->sh_addralign
1286 && a->sh_size == b->sh_size
1287 && a->sh_entsize == b->sh_entsize
1288 /* FIXME: Check sh_addr ? */
1289 ;
1290}
1291
1292/* Find a section in OBFD that has the same characteristics
1293 as IHEADER. Return the index of this section or SHN_UNDEF if
1294 none can be found. Check's section HINT first, as this is likely
1295 to be the correct section. */
1296
1297static unsigned int
5cc4ca83
ST
1298find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1299 const unsigned int hint)
84865015
NC
1300{
1301 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1302 unsigned int i;
1303
a55c9876
NC
1304 BFD_ASSERT (iheader != NULL);
1305
1306 /* See PR 20922 for a reproducer of the NULL test. */
5cc4ca83
ST
1307 if (hint < elf_numsections (obfd)
1308 && oheaders[hint] != NULL
a55c9876 1309 && section_match (oheaders[hint], iheader))
84865015
NC
1310 return hint;
1311
1312 for (i = 1; i < elf_numsections (obfd); i++)
1313 {
1314 Elf_Internal_Shdr * oheader = oheaders[i];
1315
a55c9876
NC
1316 if (oheader == NULL)
1317 continue;
84865015
NC
1318 if (section_match (oheader, iheader))
1319 /* FIXME: Do we care if there is a potential for
1320 multiple matches ? */
1321 return i;
1322 }
1323
1324 return SHN_UNDEF;
1325}
1326
5522f910
NC
1327/* PR 19938: Attempt to set the ELF section header fields of an OS or
1328 Processor specific section, based upon a matching input section.
1329 Returns TRUE upon success, FALSE otherwise. */
07d6d2b8 1330
5522f910
NC
1331static bfd_boolean
1332copy_special_section_fields (const bfd *ibfd,
1333 bfd *obfd,
1334 const Elf_Internal_Shdr *iheader,
1335 Elf_Internal_Shdr *oheader,
1336 const unsigned int secnum)
1337{
1338 const struct elf_backend_data *bed = get_elf_backend_data (obfd);
1339 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1340 bfd_boolean changed = FALSE;
1341 unsigned int sh_link;
1342
1343 if (oheader->sh_type == SHT_NOBITS)
1344 {
1345 /* This is a feature for objcopy --only-keep-debug:
1346 When a section's type is changed to NOBITS, we preserve
1347 the sh_link and sh_info fields so that they can be
1348 matched up with the original.
1349
1350 Note: Strictly speaking these assignments are wrong.
1351 The sh_link and sh_info fields should point to the
1352 relevent sections in the output BFD, which may not be in
1353 the same location as they were in the input BFD. But
1354 the whole point of this action is to preserve the
1355 original values of the sh_link and sh_info fields, so
1356 that they can be matched up with the section headers in
1357 the original file. So strictly speaking we may be
1358 creating an invalid ELF file, but it is only for a file
1359 that just contains debug info and only for sections
1360 without any contents. */
1361 if (oheader->sh_link == 0)
1362 oheader->sh_link = iheader->sh_link;
1363 if (oheader->sh_info == 0)
1364 oheader->sh_info = iheader->sh_info;
1365 return TRUE;
1366 }
1367
1368 /* Allow the target a chance to decide how these fields should be set. */
1369 if (bed->elf_backend_copy_special_section_fields != NULL
1370 && bed->elf_backend_copy_special_section_fields
1371 (ibfd, obfd, iheader, oheader))
1372 return TRUE;
1373
1374 /* We have an iheader which might match oheader, and which has non-zero
1375 sh_info and/or sh_link fields. Attempt to follow those links and find
1376 the section in the output bfd which corresponds to the linked section
1377 in the input bfd. */
1378 if (iheader->sh_link != SHN_UNDEF)
1379 {
4f3ca05b
NC
1380 /* See PR 20931 for a reproducer. */
1381 if (iheader->sh_link >= elf_numsections (ibfd))
1382 {
76cfced5 1383 _bfd_error_handler
4f3ca05b
NC
1384 /* xgettext:c-format */
1385 (_("%B: Invalid sh_link field (%d) in section number %d"),
1386 ibfd, iheader->sh_link, secnum);
1387 return FALSE;
1388 }
1389
5522f910
NC
1390 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1391 if (sh_link != SHN_UNDEF)
1392 {
1393 oheader->sh_link = sh_link;
1394 changed = TRUE;
1395 }
1396 else
1397 /* FIXME: Should we install iheader->sh_link
1398 if we could not find a match ? */
76cfced5 1399 _bfd_error_handler
695344c0 1400 /* xgettext:c-format */
5522f910
NC
1401 (_("%B: Failed to find link section for section %d"), obfd, secnum);
1402 }
1403
1404 if (iheader->sh_info)
1405 {
1406 /* The sh_info field can hold arbitrary information, but if the
1407 SHF_LINK_INFO flag is set then it should be interpreted as a
1408 section index. */
1409 if (iheader->sh_flags & SHF_INFO_LINK)
1410 {
1411 sh_link = find_link (obfd, iheaders[iheader->sh_info],
1412 iheader->sh_info);
1413 if (sh_link != SHN_UNDEF)
1414 oheader->sh_flags |= SHF_INFO_LINK;
1415 }
1416 else
1417 /* No idea what it means - just copy it. */
1418 sh_link = iheader->sh_info;
1419
1420 if (sh_link != SHN_UNDEF)
1421 {
1422 oheader->sh_info = sh_link;
1423 changed = TRUE;
1424 }
1425 else
76cfced5 1426 _bfd_error_handler
695344c0 1427 /* xgettext:c-format */
5522f910
NC
1428 (_("%B: Failed to find info section for section %d"), obfd, secnum);
1429 }
1430
1431 return changed;
1432}
07d6d2b8 1433
0ac4564e
L
1434/* Copy the program header and other data from one object module to
1435 another. */
252b5132 1436
b34976b6 1437bfd_boolean
217aa764 1438_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
2d502050 1439{
5522f910
NC
1440 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1441 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd);
1442 const struct elf_backend_data *bed;
84865015
NC
1443 unsigned int i;
1444
2d502050 1445 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
84865015 1446 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 1447 return TRUE;
2d502050 1448
57b828ef
L
1449 if (!elf_flags_init (obfd))
1450 {
1451 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1452 elf_flags_init (obfd) = TRUE;
1453 }
2d502050 1454
0ac4564e 1455 elf_gp (obfd) = elf_gp (ibfd);
57b828ef
L
1456
1457 /* Also copy the EI_OSABI field. */
1458 elf_elfheader (obfd)->e_ident[EI_OSABI] =
1459 elf_elfheader (ibfd)->e_ident[EI_OSABI];
104d59d1 1460
5522f910
NC
1461 /* If set, copy the EI_ABIVERSION field. */
1462 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1463 elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1464 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
07d6d2b8 1465
104d59d1
JM
1466 /* Copy object attributes. */
1467 _bfd_elf_copy_obj_attributes (ibfd, obfd);
63b9bbb7 1468
84865015
NC
1469 if (iheaders == NULL || oheaders == NULL)
1470 return TRUE;
63b9bbb7 1471
5522f910
NC
1472 bed = get_elf_backend_data (obfd);
1473
1474 /* Possibly copy other fields in the section header. */
84865015 1475 for (i = 1; i < elf_numsections (obfd); i++)
63b9bbb7 1476 {
84865015
NC
1477 unsigned int j;
1478 Elf_Internal_Shdr * oheader = oheaders[i];
63b9bbb7 1479
5522f910
NC
1480 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1481 because of a special case need for generating separate debug info
1482 files. See below for more details. */
84865015
NC
1483 if (oheader == NULL
1484 || (oheader->sh_type != SHT_NOBITS
5522f910
NC
1485 && oheader->sh_type < SHT_LOOS))
1486 continue;
1487
1488 /* Ignore empty sections, and sections whose
1489 fields have already been initialised. */
1490 if (oheader->sh_size == 0
84865015
NC
1491 || (oheader->sh_info != 0 && oheader->sh_link != 0))
1492 continue;
63b9bbb7 1493
84865015 1494 /* Scan for the matching section in the input bfd.
5522f910
NC
1495 First we try for a direct mapping between the input and output sections. */
1496 for (j = 1; j < elf_numsections (ibfd); j++)
1497 {
1498 const Elf_Internal_Shdr * iheader = iheaders[j];
1499
1500 if (iheader == NULL)
1501 continue;
1502
1503 if (oheader->bfd_section != NULL
1504 && iheader->bfd_section != NULL
1505 && iheader->bfd_section->output_section != NULL
1506 && iheader->bfd_section->output_section == oheader->bfd_section)
1507 {
1508 /* We have found a connection from the input section to the
1509 output section. Attempt to copy the header fields. If
1510 this fails then do not try any further sections - there
1511 should only be a one-to-one mapping between input and output. */
1512 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1513 j = elf_numsections (ibfd);
1514 break;
1515 }
1516 }
1517
1518 if (j < elf_numsections (ibfd))
1519 continue;
1520
1521 /* That failed. So try to deduce the corresponding input section.
84865015
NC
1522 Unfortunately we cannot compare names as the output string table
1523 is empty, so instead we check size, address and type. */
1524 for (j = 1; j < elf_numsections (ibfd); j++)
1525 {
5522f910 1526 const Elf_Internal_Shdr * iheader = iheaders[j];
84865015 1527
5522f910
NC
1528 if (iheader == NULL)
1529 continue;
1530
1531 /* Try matching fields in the input section's header.
1532 Since --only-keep-debug turns all non-debug sections into
84865015
NC
1533 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1534 input type. */
1535 if ((oheader->sh_type == SHT_NOBITS
1536 || iheader->sh_type == oheader->sh_type)
5522f910
NC
1537 && (iheader->sh_flags & ~ SHF_INFO_LINK)
1538 == (oheader->sh_flags & ~ SHF_INFO_LINK)
84865015
NC
1539 && iheader->sh_addralign == oheader->sh_addralign
1540 && iheader->sh_entsize == oheader->sh_entsize
1541 && iheader->sh_size == oheader->sh_size
1542 && iheader->sh_addr == oheader->sh_addr
1543 && (iheader->sh_info != oheader->sh_info
1544 || iheader->sh_link != oheader->sh_link))
63b9bbb7 1545 {
5522f910
NC
1546 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1547 break;
63b9bbb7
NC
1548 }
1549 }
5522f910
NC
1550
1551 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1552 {
1553 /* Final attempt. Call the backend copy function
1554 with a NULL input section. */
1555 if (bed->elf_backend_copy_special_section_fields != NULL)
1556 bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader);
1557 }
63b9bbb7
NC
1558 }
1559
b34976b6 1560 return TRUE;
2d502050
L
1561}
1562
cedc298e
L
1563static const char *
1564get_segment_type (unsigned int p_type)
1565{
1566 const char *pt;
1567 switch (p_type)
1568 {
1569 case PT_NULL: pt = "NULL"; break;
1570 case PT_LOAD: pt = "LOAD"; break;
1571 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1572 case PT_INTERP: pt = "INTERP"; break;
1573 case PT_NOTE: pt = "NOTE"; break;
1574 case PT_SHLIB: pt = "SHLIB"; break;
1575 case PT_PHDR: pt = "PHDR"; break;
1576 case PT_TLS: pt = "TLS"; break;
1577 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
2b05f1b7 1578 case PT_GNU_STACK: pt = "STACK"; break;
cedc298e
L
1579 case PT_GNU_RELRO: pt = "RELRO"; break;
1580 default: pt = NULL; break;
1581 }
1582 return pt;
1583}
1584
f0b79d91
L
1585/* Print out the program headers. */
1586
b34976b6 1587bfd_boolean
217aa764 1588_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
252b5132 1589{
a50b1753 1590 FILE *f = (FILE *) farg;
252b5132
RH
1591 Elf_Internal_Phdr *p;
1592 asection *s;
1593 bfd_byte *dynbuf = NULL;
1594
1595 p = elf_tdata (abfd)->phdr;
1596 if (p != NULL)
1597 {
1598 unsigned int i, c;
1599
1600 fprintf (f, _("\nProgram Header:\n"));
1601 c = elf_elfheader (abfd)->e_phnum;
1602 for (i = 0; i < c; i++, p++)
1603 {
cedc298e 1604 const char *pt = get_segment_type (p->p_type);
252b5132
RH
1605 char buf[20];
1606
cedc298e 1607 if (pt == NULL)
252b5132 1608 {
cedc298e
L
1609 sprintf (buf, "0x%lx", p->p_type);
1610 pt = buf;
252b5132 1611 }
dc810e39 1612 fprintf (f, "%8s off 0x", pt);
60b89a18 1613 bfd_fprintf_vma (abfd, f, p->p_offset);
252b5132 1614 fprintf (f, " vaddr 0x");
60b89a18 1615 bfd_fprintf_vma (abfd, f, p->p_vaddr);
252b5132 1616 fprintf (f, " paddr 0x");
60b89a18 1617 bfd_fprintf_vma (abfd, f, p->p_paddr);
252b5132
RH
1618 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1619 fprintf (f, " filesz 0x");
60b89a18 1620 bfd_fprintf_vma (abfd, f, p->p_filesz);
252b5132 1621 fprintf (f, " memsz 0x");
60b89a18 1622 bfd_fprintf_vma (abfd, f, p->p_memsz);
252b5132
RH
1623 fprintf (f, " flags %c%c%c",
1624 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1625 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1626 (p->p_flags & PF_X) != 0 ? 'x' : '-');
dc810e39
AM
1627 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1628 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
252b5132
RH
1629 fprintf (f, "\n");
1630 }
1631 }
1632
1633 s = bfd_get_section_by_name (abfd, ".dynamic");
1634 if (s != NULL)
1635 {
cb33740c 1636 unsigned int elfsec;
dc810e39 1637 unsigned long shlink;
252b5132
RH
1638 bfd_byte *extdyn, *extdynend;
1639 size_t extdynsize;
217aa764 1640 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
252b5132
RH
1641
1642 fprintf (f, _("\nDynamic Section:\n"));
1643
eea6121a 1644 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
252b5132
RH
1645 goto error_return;
1646
1647 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
cb33740c 1648 if (elfsec == SHN_BAD)
252b5132 1649 goto error_return;
dc810e39 1650 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
252b5132
RH
1651
1652 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1653 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1654
1655 extdyn = dynbuf;
06614111
NC
1656 /* PR 17512: file: 6f427532. */
1657 if (s->size < extdynsize)
1658 goto error_return;
eea6121a 1659 extdynend = extdyn + s->size;
1036838a 1660 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
07d6d2b8 1661 Fix range check. */
1036838a 1662 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
252b5132
RH
1663 {
1664 Elf_Internal_Dyn dyn;
ad9563d6 1665 const char *name = "";
252b5132 1666 char ab[20];
b34976b6 1667 bfd_boolean stringp;
ad9563d6 1668 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 1669
217aa764 1670 (*swap_dyn_in) (abfd, extdyn, &dyn);
252b5132
RH
1671
1672 if (dyn.d_tag == DT_NULL)
1673 break;
1674
b34976b6 1675 stringp = FALSE;
252b5132
RH
1676 switch (dyn.d_tag)
1677 {
1678 default:
ad9563d6
CM
1679 if (bed->elf_backend_get_target_dtag)
1680 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1681
1682 if (!strcmp (name, ""))
1683 {
cd9af601 1684 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag);
ad9563d6
CM
1685 name = ab;
1686 }
252b5132
RH
1687 break;
1688
b34976b6 1689 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
252b5132
RH
1690 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1691 case DT_PLTGOT: name = "PLTGOT"; break;
1692 case DT_HASH: name = "HASH"; break;
1693 case DT_STRTAB: name = "STRTAB"; break;
1694 case DT_SYMTAB: name = "SYMTAB"; break;
1695 case DT_RELA: name = "RELA"; break;
1696 case DT_RELASZ: name = "RELASZ"; break;
1697 case DT_RELAENT: name = "RELAENT"; break;
1698 case DT_STRSZ: name = "STRSZ"; break;
1699 case DT_SYMENT: name = "SYMENT"; break;
1700 case DT_INIT: name = "INIT"; break;
1701 case DT_FINI: name = "FINI"; break;
b34976b6
AM
1702 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1703 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
252b5132
RH
1704 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1705 case DT_REL: name = "REL"; break;
1706 case DT_RELSZ: name = "RELSZ"; break;
1707 case DT_RELENT: name = "RELENT"; break;
1708 case DT_PLTREL: name = "PLTREL"; break;
1709 case DT_DEBUG: name = "DEBUG"; break;
1710 case DT_TEXTREL: name = "TEXTREL"; break;
1711 case DT_JMPREL: name = "JMPREL"; break;
94558834
L
1712 case DT_BIND_NOW: name = "BIND_NOW"; break;
1713 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1714 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1715 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1716 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
b34976b6 1717 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
94558834
L
1718 case DT_FLAGS: name = "FLAGS"; break;
1719 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1720 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
d48188b9 1721 case DT_CHECKSUM: name = "CHECKSUM"; break;
94558834
L
1722 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1723 case DT_MOVEENT: name = "MOVEENT"; break;
1724 case DT_MOVESZ: name = "MOVESZ"; break;
1725 case DT_FEATURE: name = "FEATURE"; break;
1726 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1727 case DT_SYMINSZ: name = "SYMINSZ"; break;
1728 case DT_SYMINENT: name = "SYMINENT"; break;
b34976b6
AM
1729 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1730 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1731 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
94558834
L
1732 case DT_PLTPAD: name = "PLTPAD"; break;
1733 case DT_MOVETAB: name = "MOVETAB"; break;
1734 case DT_SYMINFO: name = "SYMINFO"; break;
1735 case DT_RELACOUNT: name = "RELACOUNT"; break;
1736 case DT_RELCOUNT: name = "RELCOUNT"; break;
1737 case DT_FLAGS_1: name = "FLAGS_1"; break;
252b5132
RH
1738 case DT_VERSYM: name = "VERSYM"; break;
1739 case DT_VERDEF: name = "VERDEF"; break;
1740 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1741 case DT_VERNEED: name = "VERNEED"; break;
1742 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
b34976b6 1743 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
94558834 1744 case DT_USED: name = "USED"; break;
b34976b6 1745 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
fdc90cb4 1746 case DT_GNU_HASH: name = "GNU_HASH"; break;
252b5132
RH
1747 }
1748
ad9563d6 1749 fprintf (f, " %-20s ", name);
252b5132 1750 if (! stringp)
a1f3c56e
AN
1751 {
1752 fprintf (f, "0x");
1753 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1754 }
252b5132
RH
1755 else
1756 {
1757 const char *string;
dc810e39 1758 unsigned int tagv = dyn.d_un.d_val;
252b5132 1759
dc810e39 1760 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
252b5132
RH
1761 if (string == NULL)
1762 goto error_return;
1763 fprintf (f, "%s", string);
1764 }
1765 fprintf (f, "\n");
1766 }
1767
1768 free (dynbuf);
1769 dynbuf = NULL;
1770 }
1771
1772 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1773 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1774 {
fc0e6df6 1775 if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
b34976b6 1776 return FALSE;
252b5132
RH
1777 }
1778
1779 if (elf_dynverdef (abfd) != 0)
1780 {
1781 Elf_Internal_Verdef *t;
1782
1783 fprintf (f, _("\nVersion definitions:\n"));
1784 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1785 {
1786 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
d0fb9a8d
JJ
1787 t->vd_flags, t->vd_hash,
1788 t->vd_nodename ? t->vd_nodename : "<corrupt>");
1789 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
252b5132
RH
1790 {
1791 Elf_Internal_Verdaux *a;
1792
1793 fprintf (f, "\t");
1794 for (a = t->vd_auxptr->vda_nextptr;
1795 a != NULL;
1796 a = a->vda_nextptr)
d0fb9a8d
JJ
1797 fprintf (f, "%s ",
1798 a->vda_nodename ? a->vda_nodename : "<corrupt>");
252b5132
RH
1799 fprintf (f, "\n");
1800 }
1801 }
1802 }
1803
1804 if (elf_dynverref (abfd) != 0)
1805 {
1806 Elf_Internal_Verneed *t;
1807
1808 fprintf (f, _("\nVersion References:\n"));
1809 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1810 {
1811 Elf_Internal_Vernaux *a;
1812
d0fb9a8d
JJ
1813 fprintf (f, _(" required from %s:\n"),
1814 t->vn_filename ? t->vn_filename : "<corrupt>");
252b5132
RH
1815 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1816 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
d0fb9a8d
JJ
1817 a->vna_flags, a->vna_other,
1818 a->vna_nodename ? a->vna_nodename : "<corrupt>");
252b5132
RH
1819 }
1820 }
1821
b34976b6 1822 return TRUE;
252b5132
RH
1823
1824 error_return:
1825 if (dynbuf != NULL)
1826 free (dynbuf);
b34976b6 1827 return FALSE;
252b5132
RH
1828}
1829
bb4d2ac2
L
1830/* Get version string. */
1831
1832const char *
60bb06bc
L
1833_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
1834 bfd_boolean *hidden)
bb4d2ac2
L
1835{
1836 const char *version_string = NULL;
1837 if (elf_dynversym (abfd) != 0
1838 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
1839 {
1840 unsigned int vernum = ((elf_symbol_type *) symbol)->version;
1841
1842 *hidden = (vernum & VERSYM_HIDDEN) != 0;
1843 vernum &= VERSYM_VERSION;
1844
1845 if (vernum == 0)
1846 version_string = "";
1847 else if (vernum == 1)
1848 version_string = "Base";
1849 else if (vernum <= elf_tdata (abfd)->cverdefs)
1850 version_string =
1851 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1852 else
1853 {
1854 Elf_Internal_Verneed *t;
1855
1856 version_string = "";
1857 for (t = elf_tdata (abfd)->verref;
1858 t != NULL;
1859 t = t->vn_nextref)
1860 {
1861 Elf_Internal_Vernaux *a;
1862
1863 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1864 {
1865 if (a->vna_other == vernum)
1866 {
1867 version_string = a->vna_nodename;
1868 break;
1869 }
1870 }
1871 }
1872 }
1873 }
1874 return version_string;
1875}
1876
252b5132
RH
1877/* Display ELF-specific fields of a symbol. */
1878
1879void
217aa764
AM
1880bfd_elf_print_symbol (bfd *abfd,
1881 void *filep,
1882 asymbol *symbol,
1883 bfd_print_symbol_type how)
252b5132 1884{
a50b1753 1885 FILE *file = (FILE *) filep;
252b5132
RH
1886 switch (how)
1887 {
1888 case bfd_print_symbol_name:
1889 fprintf (file, "%s", symbol->name);
1890 break;
1891 case bfd_print_symbol_more:
1892 fprintf (file, "elf ");
60b89a18 1893 bfd_fprintf_vma (abfd, file, symbol->value);
cd9af601 1894 fprintf (file, " %x", symbol->flags);
252b5132
RH
1895 break;
1896 case bfd_print_symbol_all:
1897 {
4e8a9624
AM
1898 const char *section_name;
1899 const char *name = NULL;
9c5bfbb7 1900 const struct elf_backend_data *bed;
7a13edea 1901 unsigned char st_other;
dbb410c3 1902 bfd_vma val;
bb4d2ac2
L
1903 const char *version_string;
1904 bfd_boolean hidden;
c044fabd 1905
252b5132 1906 section_name = symbol->section ? symbol->section->name : "(*none*)";
587ff49e
RH
1907
1908 bed = get_elf_backend_data (abfd);
1909 if (bed->elf_backend_print_symbol_all)
c044fabd 1910 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
587ff49e
RH
1911
1912 if (name == NULL)
1913 {
7ee38065 1914 name = symbol->name;
217aa764 1915 bfd_print_symbol_vandf (abfd, file, symbol);
587ff49e
RH
1916 }
1917
252b5132
RH
1918 fprintf (file, " %s\t", section_name);
1919 /* Print the "other" value for a symbol. For common symbols,
1920 we've already printed the size; now print the alignment.
1921 For other symbols, we have no specified alignment, and
1922 we've printed the address; now print the size. */
dcf6c779 1923 if (symbol->section && bfd_is_com_section (symbol->section))
dbb410c3
AM
1924 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1925 else
1926 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1927 bfd_fprintf_vma (abfd, file, val);
252b5132
RH
1928
1929 /* If we have version information, print it. */
60bb06bc
L
1930 version_string = _bfd_elf_get_symbol_version_string (abfd,
1931 symbol,
1932 &hidden);
bb4d2ac2 1933 if (version_string)
252b5132 1934 {
bb4d2ac2 1935 if (!hidden)
252b5132
RH
1936 fprintf (file, " %-11s", version_string);
1937 else
1938 {
1939 int i;
1940
1941 fprintf (file, " (%s)", version_string);
1942 for (i = 10 - strlen (version_string); i > 0; --i)
1943 putc (' ', file);
1944 }
1945 }
1946
1947 /* If the st_other field is not zero, print it. */
7a13edea 1948 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
c044fabd 1949
7a13edea
NC
1950 switch (st_other)
1951 {
1952 case 0: break;
1953 case STV_INTERNAL: fprintf (file, " .internal"); break;
1954 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1955 case STV_PROTECTED: fprintf (file, " .protected"); break;
1956 default:
1957 /* Some other non-defined flags are also present, so print
1958 everything hex. */
1959 fprintf (file, " 0x%02x", (unsigned int) st_other);
1960 }
252b5132 1961
587ff49e 1962 fprintf (file, " %s", name);
252b5132
RH
1963 }
1964 break;
1965 }
1966}
252b5132
RH
1967\f
1968/* ELF .o/exec file reading */
1969
c044fabd 1970/* Create a new bfd section from an ELF section header. */
252b5132 1971
b34976b6 1972bfd_boolean
217aa764 1973bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
252b5132 1974{
4fbb74a6
AM
1975 Elf_Internal_Shdr *hdr;
1976 Elf_Internal_Ehdr *ehdr;
1977 const struct elf_backend_data *bed;
90937f86 1978 const char *name;
bf67003b
NC
1979 bfd_boolean ret = TRUE;
1980 static bfd_boolean * sections_being_created = NULL;
5a4b0ccc 1981 static bfd * sections_being_created_abfd = NULL;
bf67003b 1982 static unsigned int nesting = 0;
252b5132 1983
4fbb74a6
AM
1984 if (shindex >= elf_numsections (abfd))
1985 return FALSE;
1986
bf67003b
NC
1987 if (++ nesting > 3)
1988 {
1989 /* PR17512: A corrupt ELF binary might contain a recursive group of
06614111 1990 sections, with each the string indicies pointing to the next in the
bf67003b
NC
1991 loop. Detect this here, by refusing to load a section that we are
1992 already in the process of loading. We only trigger this test if
1993 we have nested at least three sections deep as normal ELF binaries
5a4b0ccc
NC
1994 can expect to recurse at least once.
1995
1996 FIXME: It would be better if this array was attached to the bfd,
1997 rather than being held in a static pointer. */
1998
1999 if (sections_being_created_abfd != abfd)
2000 sections_being_created = NULL;
bf67003b
NC
2001 if (sections_being_created == NULL)
2002 {
2003 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
2004 sections_being_created = (bfd_boolean *)
2005 bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
5a4b0ccc 2006 sections_being_created_abfd = abfd;
bf67003b
NC
2007 }
2008 if (sections_being_created [shindex])
2009 {
4eca0228 2010 _bfd_error_handler
bf67003b
NC
2011 (_("%B: warning: loop in section dependencies detected"), abfd);
2012 return FALSE;
2013 }
2014 sections_being_created [shindex] = TRUE;
2015 }
2016
4fbb74a6
AM
2017 hdr = elf_elfsections (abfd)[shindex];
2018 ehdr = elf_elfheader (abfd);
2019 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1b3a8575 2020 hdr->sh_name);
933d961a 2021 if (name == NULL)
bf67003b 2022 goto fail;
252b5132 2023
4fbb74a6 2024 bed = get_elf_backend_data (abfd);
252b5132
RH
2025 switch (hdr->sh_type)
2026 {
2027 case SHT_NULL:
2028 /* Inactive section. Throw it away. */
bf67003b 2029 goto success;
252b5132 2030
bf67003b
NC
2031 case SHT_PROGBITS: /* Normal section with contents. */
2032 case SHT_NOBITS: /* .bss section. */
2033 case SHT_HASH: /* .hash section. */
2034 case SHT_NOTE: /* .note section. */
25e27870
L
2035 case SHT_INIT_ARRAY: /* .init_array section. */
2036 case SHT_FINI_ARRAY: /* .fini_array section. */
2037 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
7f1204bb 2038 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */
fdc90cb4 2039 case SHT_GNU_HASH: /* .gnu.hash section. */
bf67003b
NC
2040 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2041 goto success;
252b5132 2042
797fc050 2043 case SHT_DYNAMIC: /* Dynamic linking information. */
6dc132d9 2044 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2045 goto fail;
2046
cfcac11d
NC
2047 if (hdr->sh_link > elf_numsections (abfd))
2048 {
caa83f8b 2049 /* PR 10478: Accept Solaris binaries with a sh_link
cfcac11d
NC
2050 field set to SHN_BEFORE or SHN_AFTER. */
2051 switch (bfd_get_arch (abfd))
2052 {
caa83f8b 2053 case bfd_arch_i386:
cfcac11d
NC
2054 case bfd_arch_sparc:
2055 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
2056 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
2057 break;
2058 /* Otherwise fall through. */
2059 default:
bf67003b 2060 goto fail;
cfcac11d
NC
2061 }
2062 }
2063 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
bf67003b 2064 goto fail;
cfcac11d 2065 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
797fc050
AM
2066 {
2067 Elf_Internal_Shdr *dynsymhdr;
2068
2069 /* The shared libraries distributed with hpux11 have a bogus
2070 sh_link field for the ".dynamic" section. Find the
2071 string table for the ".dynsym" section instead. */
2072 if (elf_dynsymtab (abfd) != 0)
2073 {
2074 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2075 hdr->sh_link = dynsymhdr->sh_link;
2076 }
2077 else
2078 {
2079 unsigned int i, num_sec;
2080
2081 num_sec = elf_numsections (abfd);
2082 for (i = 1; i < num_sec; i++)
2083 {
2084 dynsymhdr = elf_elfsections (abfd)[i];
2085 if (dynsymhdr->sh_type == SHT_DYNSYM)
2086 {
2087 hdr->sh_link = dynsymhdr->sh_link;
2088 break;
2089 }
2090 }
2091 }
2092 }
bf67003b 2093 goto success;
797fc050 2094
bf67003b 2095 case SHT_SYMTAB: /* A symbol table. */
252b5132 2096 if (elf_onesymtab (abfd) == shindex)
bf67003b 2097 goto success;
252b5132 2098
a50b2160 2099 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2100 goto fail;
2101
3337c1e5 2102 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
eee3b786
AM
2103 {
2104 if (hdr->sh_size != 0)
bf67003b 2105 goto fail;
eee3b786
AM
2106 /* Some assemblers erroneously set sh_info to one with a
2107 zero sh_size. ld sees this as a global symbol count
2108 of (unsigned) -1. Fix it here. */
2109 hdr->sh_info = 0;
bf67003b 2110 goto success;
eee3b786 2111 }
bf67003b 2112
16ad13ec
NC
2113 /* PR 18854: A binary might contain more than one symbol table.
2114 Unusual, but possible. Warn, but continue. */
2115 if (elf_onesymtab (abfd) != 0)
2116 {
4eca0228 2117 _bfd_error_handler
695344c0 2118 /* xgettext:c-format */
63a5468a
AM
2119 (_("%B: warning: multiple symbol tables detected"
2120 " - ignoring the table in section %u"),
16ad13ec
NC
2121 abfd, shindex);
2122 goto success;
2123 }
252b5132 2124 elf_onesymtab (abfd) = shindex;
6a40cf0c
NC
2125 elf_symtab_hdr (abfd) = *hdr;
2126 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
252b5132
RH
2127 abfd->flags |= HAS_SYMS;
2128
2129 /* Sometimes a shared object will map in the symbol table. If
08a40648
AM
2130 SHF_ALLOC is set, and this is a shared object, then we also
2131 treat this section as a BFD section. We can not base the
2132 decision purely on SHF_ALLOC, because that flag is sometimes
2133 set in a relocatable object file, which would confuse the
2134 linker. */
252b5132
RH
2135 if ((hdr->sh_flags & SHF_ALLOC) != 0
2136 && (abfd->flags & DYNAMIC) != 0
6dc132d9
L
2137 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2138 shindex))
bf67003b 2139 goto fail;
252b5132 2140
1b3a8575
AM
2141 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2142 can't read symbols without that section loaded as well. It
2143 is most likely specified by the next section header. */
6a40cf0c
NC
2144 {
2145 elf_section_list * entry;
2146 unsigned int i, num_sec;
1b3a8575 2147
6a40cf0c
NC
2148 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2149 if (entry->hdr.sh_link == shindex)
2150 goto success;
2151
2152 num_sec = elf_numsections (abfd);
2153 for (i = shindex + 1; i < num_sec; i++)
2154 {
2155 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2156
2157 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2158 && hdr2->sh_link == shindex)
2159 break;
2160 }
2161
2162 if (i == num_sec)
2163 for (i = 1; i < shindex; i++)
1b3a8575
AM
2164 {
2165 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
6a40cf0c 2166
1b3a8575
AM
2167 if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2168 && hdr2->sh_link == shindex)
2169 break;
2170 }
6a40cf0c
NC
2171
2172 if (i != shindex)
2173 ret = bfd_section_from_shdr (abfd, i);
2174 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2175 goto success;
2176 }
252b5132 2177
bf67003b 2178 case SHT_DYNSYM: /* A dynamic symbol table. */
252b5132 2179 if (elf_dynsymtab (abfd) == shindex)
bf67003b 2180 goto success;
252b5132 2181
a50b2160 2182 if (hdr->sh_entsize != bed->s->sizeof_sym)
bf67003b
NC
2183 goto fail;
2184
eee3b786
AM
2185 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2186 {
2187 if (hdr->sh_size != 0)
bf67003b
NC
2188 goto fail;
2189
eee3b786
AM
2190 /* Some linkers erroneously set sh_info to one with a
2191 zero sh_size. ld sees this as a global symbol count
2192 of (unsigned) -1. Fix it here. */
2193 hdr->sh_info = 0;
bf67003b 2194 goto success;
eee3b786 2195 }
bf67003b 2196
16ad13ec
NC
2197 /* PR 18854: A binary might contain more than one dynamic symbol table.
2198 Unusual, but possible. Warn, but continue. */
2199 if (elf_dynsymtab (abfd) != 0)
2200 {
4eca0228 2201 _bfd_error_handler
695344c0 2202 /* xgettext:c-format */
63a5468a
AM
2203 (_("%B: warning: multiple dynamic symbol tables detected"
2204 " - ignoring the table in section %u"),
16ad13ec
NC
2205 abfd, shindex);
2206 goto success;
2207 }
252b5132
RH
2208 elf_dynsymtab (abfd) = shindex;
2209 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2210 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2211 abfd->flags |= HAS_SYMS;
2212
2213 /* Besides being a symbol table, we also treat this as a regular
2214 section, so that objcopy can handle it. */
bf67003b
NC
2215 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2216 goto success;
252b5132 2217
bf67003b 2218 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */
6a40cf0c
NC
2219 {
2220 elf_section_list * entry;
9ad5cbcf 2221
6a40cf0c
NC
2222 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
2223 if (entry->ndx == shindex)
2224 goto success;
07d6d2b8 2225
6a40cf0c
NC
2226 entry = bfd_alloc (abfd, sizeof * entry);
2227 if (entry == NULL)
2228 goto fail;
2229 entry->ndx = shindex;
2230 entry->hdr = * hdr;
2231 entry->next = elf_symtab_shndx_list (abfd);
2232 elf_symtab_shndx_list (abfd) = entry;
2233 elf_elfsections (abfd)[shindex] = & entry->hdr;
2234 goto success;
2235 }
9ad5cbcf 2236
bf67003b 2237 case SHT_STRTAB: /* A string table. */
252b5132 2238 if (hdr->bfd_section != NULL)
bf67003b
NC
2239 goto success;
2240
252b5132
RH
2241 if (ehdr->e_shstrndx == shindex)
2242 {
2243 elf_tdata (abfd)->shstrtab_hdr = *hdr;
2244 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
bf67003b 2245 goto success;
252b5132 2246 }
bf67003b 2247
1b3a8575
AM
2248 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2249 {
2250 symtab_strtab:
2251 elf_tdata (abfd)->strtab_hdr = *hdr;
2252 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
bf67003b 2253 goto success;
1b3a8575 2254 }
bf67003b 2255
1b3a8575
AM
2256 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2257 {
2258 dynsymtab_strtab:
2259 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2260 hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2261 elf_elfsections (abfd)[shindex] = hdr;
2262 /* We also treat this as a regular section, so that objcopy
2263 can handle it. */
bf67003b
NC
2264 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2265 shindex);
2266 goto success;
1b3a8575 2267 }
252b5132 2268
1b3a8575
AM
2269 /* If the string table isn't one of the above, then treat it as a
2270 regular section. We need to scan all the headers to be sure,
2271 just in case this strtab section appeared before the above. */
2272 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2273 {
2274 unsigned int i, num_sec;
252b5132 2275
1b3a8575
AM
2276 num_sec = elf_numsections (abfd);
2277 for (i = 1; i < num_sec; i++)
2278 {
2279 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2280 if (hdr2->sh_link == shindex)
2281 {
933d961a
JJ
2282 /* Prevent endless recursion on broken objects. */
2283 if (i == shindex)
bf67003b 2284 goto fail;
1b3a8575 2285 if (! bfd_section_from_shdr (abfd, i))
bf67003b 2286 goto fail;
1b3a8575
AM
2287 if (elf_onesymtab (abfd) == i)
2288 goto symtab_strtab;
2289 if (elf_dynsymtab (abfd) == i)
2290 goto dynsymtab_strtab;
2291 }
2292 }
2293 }
bf67003b
NC
2294 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2295 goto success;
252b5132
RH
2296
2297 case SHT_REL:
2298 case SHT_RELA:
2299 /* *These* do a lot of work -- but build no sections! */
2300 {
2301 asection *target_sect;
d4730f92 2302 Elf_Internal_Shdr *hdr2, **p_hdr;
9ad5cbcf 2303 unsigned int num_sec = elf_numsections (abfd);
d4730f92 2304 struct bfd_elf_section_data *esdt;
252b5132 2305
aa2ca951
JJ
2306 if (hdr->sh_entsize
2307 != (bfd_size_type) (hdr->sh_type == SHT_REL
a50b2160 2308 ? bed->s->sizeof_rel : bed->s->sizeof_rela))
bf67003b 2309 goto fail;
a50b2160 2310
03ae5f59 2311 /* Check for a bogus link to avoid crashing. */
4fbb74a6 2312 if (hdr->sh_link >= num_sec)
03ae5f59 2313 {
4eca0228 2314 _bfd_error_handler
695344c0 2315 /* xgettext:c-format */
76cfced5 2316 (_("%B: invalid link %u for reloc section %s (index %u)"),
4eca0228 2317 abfd, hdr->sh_link, name, shindex);
bf67003b
NC
2318 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2319 shindex);
2320 goto success;
03ae5f59
ILT
2321 }
2322
252b5132
RH
2323 /* For some incomprehensible reason Oracle distributes
2324 libraries for Solaris in which some of the objects have
2325 bogus sh_link fields. It would be nice if we could just
2326 reject them, but, unfortunately, some people need to use
2327 them. We scan through the section headers; if we find only
2328 one suitable symbol table, we clobber the sh_link to point
83b89087
L
2329 to it. I hope this doesn't break anything.
2330
2331 Don't do it on executable nor shared library. */
2332 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2333 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
252b5132
RH
2334 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2335 {
9ad5cbcf 2336 unsigned int scan;
252b5132
RH
2337 int found;
2338
2339 found = 0;
9ad5cbcf 2340 for (scan = 1; scan < num_sec; scan++)
252b5132
RH
2341 {
2342 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2343 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2344 {
2345 if (found != 0)
2346 {
2347 found = 0;
2348 break;
2349 }
2350 found = scan;
2351 }
2352 }
2353 if (found != 0)
2354 hdr->sh_link = found;
2355 }
2356
2357 /* Get the symbol table. */
1b3a8575
AM
2358 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2359 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
252b5132 2360 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
bf67003b 2361 goto fail;
252b5132
RH
2362
2363 /* If this reloc section does not use the main symbol table we
2364 don't treat it as a reloc section. BFD can't adequately
2365 represent such a section, so at least for now, we don't
c044fabd 2366 try. We just present it as a normal section. We also
60bcf0fa 2367 can't use it as a reloc section if it points to the null
83b89087
L
2368 section, an invalid section, another reloc section, or its
2369 sh_link points to the null section. */
185ef66d 2370 if (hdr->sh_link != elf_onesymtab (abfd)
83b89087 2371 || hdr->sh_link == SHN_UNDEF
185ef66d 2372 || hdr->sh_info == SHN_UNDEF
185ef66d
AM
2373 || hdr->sh_info >= num_sec
2374 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2375 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
bf67003b
NC
2376 {
2377 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2378 shindex);
2379 goto success;
2380 }
252b5132
RH
2381
2382 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
bf67003b
NC
2383 goto fail;
2384
252b5132
RH
2385 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2386 if (target_sect == NULL)
bf67003b 2387 goto fail;
252b5132 2388
d4730f92
BS
2389 esdt = elf_section_data (target_sect);
2390 if (hdr->sh_type == SHT_RELA)
2391 p_hdr = &esdt->rela.hdr;
252b5132 2392 else
d4730f92
BS
2393 p_hdr = &esdt->rel.hdr;
2394
06614111
NC
2395 /* PR 17512: file: 0b4f81b7. */
2396 if (*p_hdr != NULL)
2397 goto fail;
ef53be89 2398 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
d4730f92 2399 if (hdr2 == NULL)
bf67003b 2400 goto fail;
252b5132 2401 *hdr2 = *hdr;
d4730f92 2402 *p_hdr = hdr2;
252b5132 2403 elf_elfsections (abfd)[shindex] = hdr2;
056bafd4
MR
2404 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2405 * bed->s->int_rels_per_ext_rel);
252b5132
RH
2406 target_sect->flags |= SEC_RELOC;
2407 target_sect->relocation = NULL;
2408 target_sect->rel_filepos = hdr->sh_offset;
bf572ba0
MM
2409 /* In the section to which the relocations apply, mark whether
2410 its relocations are of the REL or RELA variety. */
72730e0c 2411 if (hdr->sh_size != 0)
d4730f92
BS
2412 {
2413 if (hdr->sh_type == SHT_RELA)
2414 target_sect->use_rela_p = 1;
2415 }
252b5132 2416 abfd->flags |= HAS_RELOC;
bf67003b 2417 goto success;
252b5132 2418 }
252b5132
RH
2419
2420 case SHT_GNU_verdef:
2421 elf_dynverdef (abfd) = shindex;
2422 elf_tdata (abfd)->dynverdef_hdr = *hdr;
bf67003b
NC
2423 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2424 goto success;
252b5132
RH
2425
2426 case SHT_GNU_versym:
a50b2160 2427 if (hdr->sh_entsize != sizeof (Elf_External_Versym))
bf67003b
NC
2428 goto fail;
2429
252b5132
RH
2430 elf_dynversym (abfd) = shindex;
2431 elf_tdata (abfd)->dynversym_hdr = *hdr;
bf67003b
NC
2432 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2433 goto success;
252b5132
RH
2434
2435 case SHT_GNU_verneed:
2436 elf_dynverref (abfd) = shindex;
2437 elf_tdata (abfd)->dynverref_hdr = *hdr;
bf67003b
NC
2438 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2439 goto success;
252b5132
RH
2440
2441 case SHT_SHLIB:
bf67003b 2442 goto success;
252b5132 2443
dbb410c3 2444 case SHT_GROUP:
44534af3 2445 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
bf67003b
NC
2446 goto fail;
2447
6dc132d9 2448 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b
NC
2449 goto fail;
2450
bf67003b 2451 goto success;
dbb410c3 2452
252b5132 2453 default:
104d59d1
JM
2454 /* Possibly an attributes section. */
2455 if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2456 || hdr->sh_type == bed->obj_attrs_section_type)
2457 {
2458 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2459 goto fail;
104d59d1 2460 _bfd_elf_parse_attributes (abfd, hdr);
bf67003b 2461 goto success;
104d59d1
JM
2462 }
2463
252b5132 2464 /* Check for any processor-specific section types. */
3eb70a79 2465 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
bf67003b 2466 goto success;
3eb70a79
L
2467
2468 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2469 {
2470 if ((hdr->sh_flags & SHF_ALLOC) != 0)
2471 /* FIXME: How to properly handle allocated section reserved
2472 for applications? */
4eca0228 2473 _bfd_error_handler
695344c0 2474 /* xgettext:c-format */
76cfced5
AM
2475 (_("%B: unknown type [%#x] section `%s'"),
2476 abfd, hdr->sh_type, name);
3eb70a79 2477 else
bf67003b
NC
2478 {
2479 /* Allow sections reserved for applications. */
2480 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2481 shindex);
2482 goto success;
2483 }
3eb70a79
L
2484 }
2485 else if (hdr->sh_type >= SHT_LOPROC
2486 && hdr->sh_type <= SHT_HIPROC)
2487 /* FIXME: We should handle this section. */
4eca0228 2488 _bfd_error_handler
695344c0 2489 /* xgettext:c-format */
76cfced5
AM
2490 (_("%B: unknown type [%#x] section `%s'"),
2491 abfd, hdr->sh_type, name);
3eb70a79 2492 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
ff15b240
NC
2493 {
2494 /* Unrecognised OS-specific sections. */
2495 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2496 /* SHF_OS_NONCONFORMING indicates that special knowledge is
08a40648 2497 required to correctly process the section and the file should
ff15b240 2498 be rejected with an error message. */
4eca0228 2499 _bfd_error_handler
695344c0 2500 /* xgettext:c-format */
76cfced5
AM
2501 (_("%B: unknown type [%#x] section `%s'"),
2502 abfd, hdr->sh_type, name);
ff15b240 2503 else
bf67003b
NC
2504 {
2505 /* Otherwise it should be processed. */
2506 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2507 goto success;
2508 }
ff15b240 2509 }
3eb70a79
L
2510 else
2511 /* FIXME: We should handle this section. */
4eca0228 2512 _bfd_error_handler
695344c0 2513 /* xgettext:c-format */
76cfced5
AM
2514 (_("%B: unknown type [%#x] section `%s'"),
2515 abfd, hdr->sh_type, name);
3eb70a79 2516
bf67003b 2517 goto fail;
252b5132
RH
2518 }
2519
bf67003b
NC
2520 fail:
2521 ret = FALSE;
2522 success:
e5b470e2 2523 if (sections_being_created && sections_being_created_abfd == abfd)
bf67003b
NC
2524 sections_being_created [shindex] = FALSE;
2525 if (-- nesting == 0)
5a4b0ccc
NC
2526 {
2527 sections_being_created = NULL;
2528 sections_being_created_abfd = abfd;
2529 }
bf67003b 2530 return ret;
252b5132
RH
2531}
2532
87d72d41 2533/* Return the local symbol specified by ABFD, R_SYMNDX. */
ec338859 2534
87d72d41
AM
2535Elf_Internal_Sym *
2536bfd_sym_from_r_symndx (struct sym_cache *cache,
2537 bfd *abfd,
2538 unsigned long r_symndx)
ec338859 2539{
ec338859
AM
2540 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2541
a5d1b3b5
AM
2542 if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2543 {
2544 Elf_Internal_Shdr *symtab_hdr;
2545 unsigned char esym[sizeof (Elf64_External_Sym)];
2546 Elf_External_Sym_Shndx eshndx;
ec338859 2547
a5d1b3b5
AM
2548 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2549 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
87d72d41 2550 &cache->sym[ent], esym, &eshndx) == NULL)
a5d1b3b5 2551 return NULL;
9ad5cbcf 2552
a5d1b3b5
AM
2553 if (cache->abfd != abfd)
2554 {
2555 memset (cache->indx, -1, sizeof (cache->indx));
2556 cache->abfd = abfd;
2557 }
2558 cache->indx[ent] = r_symndx;
ec338859 2559 }
a5d1b3b5 2560
87d72d41 2561 return &cache->sym[ent];
ec338859
AM
2562}
2563
252b5132
RH
2564/* Given an ELF section number, retrieve the corresponding BFD
2565 section. */
2566
2567asection *
91d6fa6a 2568bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
252b5132 2569{
91d6fa6a 2570 if (sec_index >= elf_numsections (abfd))
252b5132 2571 return NULL;
91d6fa6a 2572 return elf_elfsections (abfd)[sec_index]->bfd_section;
252b5132
RH
2573}
2574
b35d266b 2575static const struct bfd_elf_special_section special_sections_b[] =
2f89ff8d 2576{
0112cd26 2577 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2578 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2579};
2580
b35d266b 2581static const struct bfd_elf_special_section special_sections_c[] =
7f4d3958 2582{
0112cd26 2583 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2584 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2585};
2586
b35d266b 2587static const struct bfd_elf_special_section special_sections_d[] =
7f4d3958 2588{
07d6d2b8
AM
2589 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2590 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
a9a72a65
DE
2591 /* There are more DWARF sections than these, but they needn't be added here
2592 unless you have to cope with broken compilers that don't emit section
2593 attributes or you want to help the user writing assembler. */
07d6d2b8
AM
2594 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 },
2595 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 },
2596 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 },
2597 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 },
0112cd26 2598 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2599 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC },
2600 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC },
2601 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC },
2602 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2603};
2604
b35d266b 2605static const struct bfd_elf_special_section special_sections_f[] =
7f4d3958 2606{
07d6d2b8 2607 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2608 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8 2609 { NULL, 0 , 0, 0, 0 }
7f4d3958
L
2610};
2611
b35d266b 2612static const struct bfd_elf_special_section special_sections_g[] =
7f4d3958 2613{
0112cd26 2614 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2615 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE },
2616 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2617 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 },
0112cd26
NC
2618 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 },
2619 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 },
07d6d2b8
AM
2620 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC },
2621 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC },
2622 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC },
2623 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2624};
2625
b35d266b 2626static const struct bfd_elf_special_section special_sections_h[] =
7f4d3958 2627{
07d6d2b8
AM
2628 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC },
2629 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2630};
2631
b35d266b 2632static const struct bfd_elf_special_section special_sections_i[] =
7f4d3958 2633{
07d6d2b8 2634 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
6f9dbcd4 2635 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2636 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 },
2637 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2638};
2639
b35d266b 2640static const struct bfd_elf_special_section special_sections_l[] =
7f4d3958 2641{
0112cd26 2642 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2643 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2644};
2645
b35d266b 2646static const struct bfd_elf_special_section special_sections_n[] =
7f4d3958 2647{
0112cd26 2648 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
07d6d2b8
AM
2649 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 },
2650 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2651};
2652
b35d266b 2653static const struct bfd_elf_special_section special_sections_p[] =
7f4d3958 2654{
6f9dbcd4 2655 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
07d6d2b8
AM
2656 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2657 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2658};
2659
b35d266b 2660static const struct bfd_elf_special_section special_sections_r[] =
7f4d3958 2661{
0112cd26
NC
2662 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2663 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8
AM
2664 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 },
2665 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 },
2666 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2667};
2668
b35d266b 2669static const struct bfd_elf_special_section special_sections_s[] =
7f4d3958 2670{
0112cd26
NC
2671 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2672 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 },
2673 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 },
60ff4dc4
HPN
2674 /* See struct bfd_elf_special_section declaration for the semantics of
2675 this special case where .prefix_length != strlen (.prefix). */
2676 { ".stabstr", 5, 3, SHT_STRTAB, 0 },
07d6d2b8 2677 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2678};
2679
b35d266b 2680static const struct bfd_elf_special_section special_sections_t[] =
7f4d3958 2681{
07d6d2b8
AM
2682 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2683 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
0112cd26 2684 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
07d6d2b8 2685 { NULL, 0, 0, 0, 0 }
7f4d3958
L
2686};
2687
1b315056
CS
2688static const struct bfd_elf_special_section special_sections_z[] =
2689{
07d6d2b8
AM
2690 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 },
2691 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 },
1b315056
CS
2692 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 },
2693 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
07d6d2b8 2694 { NULL, 0, 0, 0, 0 }
1b315056
CS
2695};
2696
e4c93b56 2697static const struct bfd_elf_special_section * const special_sections[] =
7f4d3958 2698{
7f4d3958 2699 special_sections_b, /* 'b' */
98ece1b3 2700 special_sections_c, /* 'c' */
7f4d3958
L
2701 special_sections_d, /* 'd' */
2702 NULL, /* 'e' */
2703 special_sections_f, /* 'f' */
2704 special_sections_g, /* 'g' */
2705 special_sections_h, /* 'h' */
2706 special_sections_i, /* 'i' */
2707 NULL, /* 'j' */
2708 NULL, /* 'k' */
2709 special_sections_l, /* 'l' */
2710 NULL, /* 'm' */
2711 special_sections_n, /* 'n' */
2712 NULL, /* 'o' */
2713 special_sections_p, /* 'p' */
2714 NULL, /* 'q' */
2715 special_sections_r, /* 'r' */
2716 special_sections_s, /* 's' */
2717 special_sections_t, /* 't' */
1b315056
CS
2718 NULL, /* 'u' */
2719 NULL, /* 'v' */
2720 NULL, /* 'w' */
2721 NULL, /* 'x' */
2722 NULL, /* 'y' */
2723 special_sections_z /* 'z' */
7f4d3958
L
2724};
2725
551b43fd
AM
2726const struct bfd_elf_special_section *
2727_bfd_elf_get_special_section (const char *name,
2728 const struct bfd_elf_special_section *spec,
2729 unsigned int rela)
2f89ff8d
L
2730{
2731 int i;
7f4d3958 2732 int len;
7f4d3958 2733
551b43fd 2734 len = strlen (name);
7f4d3958 2735
551b43fd 2736 for (i = 0; spec[i].prefix != NULL; i++)
7dcb9820
AM
2737 {
2738 int suffix_len;
551b43fd 2739 int prefix_len = spec[i].prefix_length;
7dcb9820
AM
2740
2741 if (len < prefix_len)
2742 continue;
551b43fd 2743 if (memcmp (name, spec[i].prefix, prefix_len) != 0)
7dcb9820
AM
2744 continue;
2745
551b43fd 2746 suffix_len = spec[i].suffix_length;
7dcb9820
AM
2747 if (suffix_len <= 0)
2748 {
2749 if (name[prefix_len] != 0)
2750 {
2751 if (suffix_len == 0)
2752 continue;
2753 if (name[prefix_len] != '.'
2754 && (suffix_len == -2
551b43fd 2755 || (rela && spec[i].type == SHT_REL)))
7dcb9820
AM
2756 continue;
2757 }
2758 }
2759 else
2760 {
2761 if (len < prefix_len + suffix_len)
2762 continue;
2763 if (memcmp (name + len - suffix_len,
551b43fd 2764 spec[i].prefix + prefix_len,
7dcb9820
AM
2765 suffix_len) != 0)
2766 continue;
2767 }
551b43fd 2768 return &spec[i];
7dcb9820 2769 }
2f89ff8d
L
2770
2771 return NULL;
2772}
2773
7dcb9820 2774const struct bfd_elf_special_section *
29ef7005 2775_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2f89ff8d 2776{
551b43fd
AM
2777 int i;
2778 const struct bfd_elf_special_section *spec;
29ef7005 2779 const struct elf_backend_data *bed;
2f89ff8d
L
2780
2781 /* See if this is one of the special sections. */
551b43fd
AM
2782 if (sec->name == NULL)
2783 return NULL;
2f89ff8d 2784
29ef7005
L
2785 bed = get_elf_backend_data (abfd);
2786 spec = bed->special_sections;
2787 if (spec)
2788 {
2789 spec = _bfd_elf_get_special_section (sec->name,
2790 bed->special_sections,
2791 sec->use_rela_p);
2792 if (spec != NULL)
2793 return spec;
2794 }
2795
551b43fd
AM
2796 if (sec->name[0] != '.')
2797 return NULL;
2f89ff8d 2798
551b43fd 2799 i = sec->name[1] - 'b';
1b315056 2800 if (i < 0 || i > 'z' - 'b')
551b43fd
AM
2801 return NULL;
2802
2803 spec = special_sections[i];
2f89ff8d 2804
551b43fd
AM
2805 if (spec == NULL)
2806 return NULL;
2807
2808 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2f89ff8d
L
2809}
2810
b34976b6 2811bfd_boolean
217aa764 2812_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
252b5132
RH
2813{
2814 struct bfd_elf_section_data *sdata;
551b43fd 2815 const struct elf_backend_data *bed;
7dcb9820 2816 const struct bfd_elf_special_section *ssect;
252b5132 2817
f0abc2a1
AM
2818 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2819 if (sdata == NULL)
2820 {
a50b1753 2821 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
07d6d2b8 2822 sizeof (*sdata));
f0abc2a1
AM
2823 if (sdata == NULL)
2824 return FALSE;
217aa764 2825 sec->used_by_bfd = sdata;
f0abc2a1 2826 }
bf572ba0 2827
551b43fd
AM
2828 /* Indicate whether or not this section should use RELA relocations. */
2829 bed = get_elf_backend_data (abfd);
2830 sec->use_rela_p = bed->default_use_rela_p;
2831
e843e0f8
L
2832 /* When we read a file, we don't need to set ELF section type and
2833 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2834 anyway. We will set ELF section type and flags for all linker
2835 created sections. If user specifies BFD section flags, we will
2836 set ELF section type and flags based on BFD section flags in
02ecc8e9
L
2837 elf_fake_sections. Special handling for .init_array/.fini_array
2838 output sections since they may contain .ctors/.dtors input
2839 sections. We don't want _bfd_elf_init_private_section_data to
2840 copy ELF section type from .ctors/.dtors input sections. */
2841 if (abfd->direction != read_direction
3496cb2a 2842 || (sec->flags & SEC_LINKER_CREATED) != 0)
2f89ff8d 2843 {
551b43fd 2844 ssect = (*bed->get_sec_type_attr) (abfd, sec);
02ecc8e9
L
2845 if (ssect != NULL
2846 && (!sec->flags
2847 || (sec->flags & SEC_LINKER_CREATED) != 0
2848 || ssect->type == SHT_INIT_ARRAY
2849 || ssect->type == SHT_FINI_ARRAY))
a31501e9
L
2850 {
2851 elf_section_type (sec) = ssect->type;
2852 elf_section_flags (sec) = ssect->attr;
2853 }
2f89ff8d
L
2854 }
2855
f592407e 2856 return _bfd_generic_new_section_hook (abfd, sec);
252b5132
RH
2857}
2858
2859/* Create a new bfd section from an ELF program header.
2860
2861 Since program segments have no names, we generate a synthetic name
2862 of the form segment<NUM>, where NUM is generally the index in the
2863 program header table. For segments that are split (see below) we
2864 generate the names segment<NUM>a and segment<NUM>b.
2865
2866 Note that some program segments may have a file size that is different than
2867 (less than) the memory size. All this means is that at execution the
2868 system must allocate the amount of memory specified by the memory size,
2869 but only initialize it with the first "file size" bytes read from the
2870 file. This would occur for example, with program segments consisting
2871 of combined data+bss.
2872
2873 To handle the above situation, this routine generates TWO bfd sections
2874 for the single program segment. The first has the length specified by
2875 the file size of the segment, and the second has the length specified
2876 by the difference between the two sizes. In effect, the segment is split
d5191d0c 2877 into its initialized and uninitialized parts.
252b5132
RH
2878
2879 */
2880
b34976b6 2881bfd_boolean
217aa764
AM
2882_bfd_elf_make_section_from_phdr (bfd *abfd,
2883 Elf_Internal_Phdr *hdr,
91d6fa6a 2884 int hdr_index,
a50b1753 2885 const char *type_name)
252b5132
RH
2886{
2887 asection *newsect;
2888 char *name;
2889 char namebuf[64];
d4c88bbb 2890 size_t len;
252b5132
RH
2891 int split;
2892
2893 split = ((hdr->p_memsz > 0)
2894 && (hdr->p_filesz > 0)
2895 && (hdr->p_memsz > hdr->p_filesz));
d5191d0c
AM
2896
2897 if (hdr->p_filesz > 0)
252b5132 2898 {
91d6fa6a 2899 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
d5191d0c 2900 len = strlen (namebuf) + 1;
a50b1753 2901 name = (char *) bfd_alloc (abfd, len);
d5191d0c
AM
2902 if (!name)
2903 return FALSE;
2904 memcpy (name, namebuf, len);
2905 newsect = bfd_make_section (abfd, name);
2906 if (newsect == NULL)
2907 return FALSE;
2908 newsect->vma = hdr->p_vaddr;
2909 newsect->lma = hdr->p_paddr;
2910 newsect->size = hdr->p_filesz;
2911 newsect->filepos = hdr->p_offset;
2912 newsect->flags |= SEC_HAS_CONTENTS;
2913 newsect->alignment_power = bfd_log2 (hdr->p_align);
2914 if (hdr->p_type == PT_LOAD)
252b5132 2915 {
d5191d0c
AM
2916 newsect->flags |= SEC_ALLOC;
2917 newsect->flags |= SEC_LOAD;
2918 if (hdr->p_flags & PF_X)
2919 {
2920 /* FIXME: all we known is that it has execute PERMISSION,
2921 may be data. */
2922 newsect->flags |= SEC_CODE;
2923 }
2924 }
2925 if (!(hdr->p_flags & PF_W))
2926 {
2927 newsect->flags |= SEC_READONLY;
252b5132 2928 }
252b5132
RH
2929 }
2930
d5191d0c 2931 if (hdr->p_memsz > hdr->p_filesz)
252b5132 2932 {
d5191d0c
AM
2933 bfd_vma align;
2934
91d6fa6a 2935 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
d4c88bbb 2936 len = strlen (namebuf) + 1;
a50b1753 2937 name = (char *) bfd_alloc (abfd, len);
252b5132 2938 if (!name)
b34976b6 2939 return FALSE;
d4c88bbb 2940 memcpy (name, namebuf, len);
252b5132
RH
2941 newsect = bfd_make_section (abfd, name);
2942 if (newsect == NULL)
b34976b6 2943 return FALSE;
252b5132
RH
2944 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2945 newsect->lma = hdr->p_paddr + hdr->p_filesz;
eea6121a 2946 newsect->size = hdr->p_memsz - hdr->p_filesz;
d5191d0c
AM
2947 newsect->filepos = hdr->p_offset + hdr->p_filesz;
2948 align = newsect->vma & -newsect->vma;
2949 if (align == 0 || align > hdr->p_align)
2950 align = hdr->p_align;
2951 newsect->alignment_power = bfd_log2 (align);
252b5132
RH
2952 if (hdr->p_type == PT_LOAD)
2953 {
d5191d0c
AM
2954 /* Hack for gdb. Segments that have not been modified do
2955 not have their contents written to a core file, on the
2956 assumption that a debugger can find the contents in the
2957 executable. We flag this case by setting the fake
2958 section size to zero. Note that "real" bss sections will
2959 always have their contents dumped to the core file. */
2960 if (bfd_get_format (abfd) == bfd_core)
2961 newsect->size = 0;
252b5132
RH
2962 newsect->flags |= SEC_ALLOC;
2963 if (hdr->p_flags & PF_X)
2964 newsect->flags |= SEC_CODE;
2965 }
2966 if (!(hdr->p_flags & PF_W))
2967 newsect->flags |= SEC_READONLY;
2968 }
2969
b34976b6 2970 return TRUE;
252b5132
RH
2971}
2972
b34976b6 2973bfd_boolean
91d6fa6a 2974bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
20cfcaae 2975{
9c5bfbb7 2976 const struct elf_backend_data *bed;
20cfcaae
NC
2977
2978 switch (hdr->p_type)
2979 {
2980 case PT_NULL:
91d6fa6a 2981 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
20cfcaae
NC
2982
2983 case PT_LOAD:
91d6fa6a 2984 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
20cfcaae
NC
2985
2986 case PT_DYNAMIC:
91d6fa6a 2987 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
20cfcaae
NC
2988
2989 case PT_INTERP:
91d6fa6a 2990 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
20cfcaae
NC
2991
2992 case PT_NOTE:
91d6fa6a 2993 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
b34976b6 2994 return FALSE;
276da9b3
L
2995 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
2996 hdr->p_align))
b34976b6
AM
2997 return FALSE;
2998 return TRUE;
20cfcaae
NC
2999
3000 case PT_SHLIB:
91d6fa6a 3001 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
20cfcaae
NC
3002
3003 case PT_PHDR:
91d6fa6a 3004 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
20cfcaae 3005
811072d8 3006 case PT_GNU_EH_FRAME:
91d6fa6a 3007 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
811072d8
RM
3008 "eh_frame_hdr");
3009
2b05f1b7 3010 case PT_GNU_STACK:
91d6fa6a 3011 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
9ee5e499 3012
8c37241b 3013 case PT_GNU_RELRO:
91d6fa6a 3014 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
8c37241b 3015
20cfcaae 3016 default:
8c1acd09 3017 /* Check for any processor-specific program segment types. */
20cfcaae 3018 bed = get_elf_backend_data (abfd);
91d6fa6a 3019 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
20cfcaae
NC
3020 }
3021}
3022
d4730f92
BS
3023/* Return the REL_HDR for SEC, assuming there is only a single one, either
3024 REL or RELA. */
3025
3026Elf_Internal_Shdr *
3027_bfd_elf_single_rel_hdr (asection *sec)
3028{
3029 if (elf_section_data (sec)->rel.hdr)
3030 {
3031 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3032 return elf_section_data (sec)->rel.hdr;
3033 }
3034 else
3035 return elf_section_data (sec)->rela.hdr;
3036}
3037
3e19fb8f
L
3038static bfd_boolean
3039_bfd_elf_set_reloc_sh_name (bfd *abfd,
3040 Elf_Internal_Shdr *rel_hdr,
3041 const char *sec_name,
3042 bfd_boolean use_rela_p)
3043{
3044 char *name = (char *) bfd_alloc (abfd,
3045 sizeof ".rela" + strlen (sec_name));
3046 if (name == NULL)
3047 return FALSE;
3048
3049 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3050 rel_hdr->sh_name =
3051 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3052 FALSE);
3053 if (rel_hdr->sh_name == (unsigned int) -1)
3054 return FALSE;
3055
3056 return TRUE;
3057}
3058
d4730f92
BS
3059/* Allocate and initialize a section-header for a new reloc section,
3060 containing relocations against ASECT. It is stored in RELDATA. If
3061 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3062 relocations. */
23bc299b 3063
5d13b3b3 3064static bfd_boolean
217aa764 3065_bfd_elf_init_reloc_shdr (bfd *abfd,
d4730f92 3066 struct bfd_elf_section_reloc_data *reldata,
f6fe1ccd 3067 const char *sec_name,
3e19fb8f
L
3068 bfd_boolean use_rela_p,
3069 bfd_boolean delay_st_name_p)
23bc299b 3070{
d4730f92 3071 Elf_Internal_Shdr *rel_hdr;
9c5bfbb7 3072 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3073
d4730f92 3074 BFD_ASSERT (reldata->hdr == NULL);
ef53be89 3075 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
d4730f92 3076 reldata->hdr = rel_hdr;
23bc299b 3077
3e19fb8f
L
3078 if (delay_st_name_p)
3079 rel_hdr->sh_name = (unsigned int) -1;
3080 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3081 use_rela_p))
b34976b6 3082 return FALSE;
23bc299b
MM
3083 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3084 rel_hdr->sh_entsize = (use_rela_p
3085 ? bed->s->sizeof_rela
3086 : bed->s->sizeof_rel);
72de5009 3087 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
28e07a05 3088 rel_hdr->sh_flags = 0;
23bc299b
MM
3089 rel_hdr->sh_addr = 0;
3090 rel_hdr->sh_size = 0;
3091 rel_hdr->sh_offset = 0;
3092
b34976b6 3093 return TRUE;
23bc299b
MM
3094}
3095
94be91de
JB
3096/* Return the default section type based on the passed in section flags. */
3097
3098int
3099bfd_elf_get_default_section_type (flagword flags)
3100{
3101 if ((flags & SEC_ALLOC) != 0
2e76e85a 3102 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
94be91de
JB
3103 return SHT_NOBITS;
3104 return SHT_PROGBITS;
3105}
3106
d4730f92
BS
3107struct fake_section_arg
3108{
3109 struct bfd_link_info *link_info;
3110 bfd_boolean failed;
3111};
3112
252b5132
RH
3113/* Set up an ELF internal section header for a section. */
3114
252b5132 3115static void
d4730f92 3116elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
252b5132 3117{
d4730f92 3118 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
9c5bfbb7 3119 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 3120 struct bfd_elf_section_data *esd = elf_section_data (asect);
252b5132 3121 Elf_Internal_Shdr *this_hdr;
0414f35b 3122 unsigned int sh_type;
0ce398f1 3123 const char *name = asect->name;
3e19fb8f 3124 bfd_boolean delay_st_name_p = FALSE;
252b5132 3125
d4730f92 3126 if (arg->failed)
252b5132
RH
3127 {
3128 /* We already failed; just get out of the bfd_map_over_sections
08a40648 3129 loop. */
252b5132
RH
3130 return;
3131 }
3132
d4730f92 3133 this_hdr = &esd->this_hdr;
252b5132 3134
f6fe1ccd 3135 if (arg->link_info)
0ce398f1 3136 {
f6fe1ccd
L
3137 /* ld: compress DWARF debug sections with names: .debug_*. */
3138 if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
3139 && (asect->flags & SEC_DEBUGGING)
3140 && name[1] == 'd'
3141 && name[6] == '_')
3142 {
3143 /* Set SEC_ELF_COMPRESS to indicate this section should be
3144 compressed. */
3145 asect->flags |= SEC_ELF_COMPRESS;
0ce398f1 3146
dd905818 3147 /* If this section will be compressed, delay adding section
3e19fb8f
L
3148 name to section name section after it is compressed in
3149 _bfd_elf_assign_file_positions_for_non_load. */
3150 delay_st_name_p = TRUE;
f6fe1ccd
L
3151 }
3152 }
3153 else if ((asect->flags & SEC_ELF_RENAME))
3154 {
3155 /* objcopy: rename output DWARF debug section. */
3156 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
3157 {
3158 /* When we decompress or compress with SHF_COMPRESSED,
3159 convert section name from .zdebug_* to .debug_* if
3160 needed. */
3161 if (name[1] == 'z')
3162 {
3163 char *new_name = convert_zdebug_to_debug (abfd, name);
3164 if (new_name == NULL)
3165 {
3166 arg->failed = TRUE;
3167 return;
3168 }
3169 name = new_name;
3170 }
3171 }
3172 else if (asect->compress_status == COMPRESS_SECTION_DONE)
0ce398f1 3173 {
f6fe1ccd
L
3174 /* PR binutils/18087: Compression does not always make a
3175 section smaller. So only rename the section when
3176 compression has actually taken place. If input section
3177 name is .zdebug_*, we should never compress it again. */
3178 char *new_name = convert_debug_to_zdebug (abfd, name);
0ce398f1
L
3179 if (new_name == NULL)
3180 {
3181 arg->failed = TRUE;
3182 return;
3183 }
f6fe1ccd
L
3184 BFD_ASSERT (name[1] != 'z');
3185 name = new_name;
0ce398f1
L
3186 }
3187 }
3188
3e19fb8f
L
3189 if (delay_st_name_p)
3190 this_hdr->sh_name = (unsigned int) -1;
3191 else
252b5132 3192 {
3e19fb8f
L
3193 this_hdr->sh_name
3194 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3195 name, FALSE);
3196 if (this_hdr->sh_name == (unsigned int) -1)
3197 {
3198 arg->failed = TRUE;
3199 return;
3200 }
252b5132
RH
3201 }
3202
a4d8e49b 3203 /* Don't clear sh_flags. Assembler may set additional bits. */
252b5132
RH
3204
3205 if ((asect->flags & SEC_ALLOC) != 0
3206 || asect->user_set_vma)
3207 this_hdr->sh_addr = asect->vma;
3208 else
3209 this_hdr->sh_addr = 0;
3210
3211 this_hdr->sh_offset = 0;
eea6121a 3212 this_hdr->sh_size = asect->size;
252b5132 3213 this_hdr->sh_link = 0;
c86934ce
NC
3214 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3215 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3216 {
4eca0228 3217 _bfd_error_handler
695344c0 3218 /* xgettext:c-format */
c86934ce 3219 (_("%B: error: Alignment power %d of section `%A' is too big"),
c08bb8dd 3220 abfd, asect->alignment_power, asect);
c86934ce
NC
3221 arg->failed = TRUE;
3222 return;
3223 }
72de5009 3224 this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
252b5132
RH
3225 /* The sh_entsize and sh_info fields may have been set already by
3226 copy_private_section_data. */
3227
3228 this_hdr->bfd_section = asect;
3229 this_hdr->contents = NULL;
3230
3cddba1e
L
3231 /* If the section type is unspecified, we set it based on
3232 asect->flags. */
98ece1b3
AM
3233 if ((asect->flags & SEC_GROUP) != 0)
3234 sh_type = SHT_GROUP;
98ece1b3 3235 else
94be91de 3236 sh_type = bfd_elf_get_default_section_type (asect->flags);
98ece1b3 3237
3cddba1e 3238 if (this_hdr->sh_type == SHT_NULL)
98ece1b3
AM
3239 this_hdr->sh_type = sh_type;
3240 else if (this_hdr->sh_type == SHT_NOBITS
3241 && sh_type == SHT_PROGBITS
3242 && (asect->flags & SEC_ALLOC) != 0)
3cddba1e 3243 {
98ece1b3
AM
3244 /* Warn if we are changing a NOBITS section to PROGBITS, but
3245 allow the link to proceed. This can happen when users link
3246 non-bss input sections to bss output sections, or emit data
3247 to a bss output section via a linker script. */
4eca0228 3248 _bfd_error_handler
58f0869b 3249 (_("warning: section `%A' type changed to PROGBITS"), asect);
98ece1b3 3250 this_hdr->sh_type = sh_type;
3cddba1e
L
3251 }
3252
2f89ff8d 3253 switch (this_hdr->sh_type)
252b5132 3254 {
2f89ff8d 3255 default:
2f89ff8d
L
3256 break;
3257
3258 case SHT_STRTAB:
2f89ff8d
L
3259 case SHT_NOTE:
3260 case SHT_NOBITS:
3261 case SHT_PROGBITS:
3262 break;
606851fb
AM
3263
3264 case SHT_INIT_ARRAY:
3265 case SHT_FINI_ARRAY:
3266 case SHT_PREINIT_ARRAY:
3267 this_hdr->sh_entsize = bed->s->arch_size / 8;
3268 break;
2f89ff8d
L
3269
3270 case SHT_HASH:
c7ac6ff8 3271 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2f89ff8d 3272 break;
5de3bf90 3273
2f89ff8d 3274 case SHT_DYNSYM:
252b5132 3275 this_hdr->sh_entsize = bed->s->sizeof_sym;
2f89ff8d
L
3276 break;
3277
3278 case SHT_DYNAMIC:
252b5132 3279 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2f89ff8d
L
3280 break;
3281
3282 case SHT_RELA:
3283 if (get_elf_backend_data (abfd)->may_use_rela_p)
3284 this_hdr->sh_entsize = bed->s->sizeof_rela;
3285 break;
3286
3287 case SHT_REL:
3288 if (get_elf_backend_data (abfd)->may_use_rel_p)
3289 this_hdr->sh_entsize = bed->s->sizeof_rel;
3290 break;
3291
3292 case SHT_GNU_versym:
252b5132 3293 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2f89ff8d
L
3294 break;
3295
3296 case SHT_GNU_verdef:
252b5132
RH
3297 this_hdr->sh_entsize = 0;
3298 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3299 cverdefs. The linker will set cverdefs, but sh_info will be
3300 zero. */
252b5132
RH
3301 if (this_hdr->sh_info == 0)
3302 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3303 else
3304 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3305 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2f89ff8d
L
3306 break;
3307
3308 case SHT_GNU_verneed:
252b5132
RH
3309 this_hdr->sh_entsize = 0;
3310 /* objcopy or strip will copy over sh_info, but may not set
08a40648
AM
3311 cverrefs. The linker will set cverrefs, but sh_info will be
3312 zero. */
252b5132
RH
3313 if (this_hdr->sh_info == 0)
3314 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3315 else
3316 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3317 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2f89ff8d
L
3318 break;
3319
3320 case SHT_GROUP:
1783205a 3321 this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2f89ff8d 3322 break;
fdc90cb4
JJ
3323
3324 case SHT_GNU_HASH:
3325 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3326 break;
dbb410c3 3327 }
252b5132
RH
3328
3329 if ((asect->flags & SEC_ALLOC) != 0)
3330 this_hdr->sh_flags |= SHF_ALLOC;
3331 if ((asect->flags & SEC_READONLY) == 0)
3332 this_hdr->sh_flags |= SHF_WRITE;
3333 if ((asect->flags & SEC_CODE) != 0)
3334 this_hdr->sh_flags |= SHF_EXECINSTR;
f5fa8ca2
JJ
3335 if ((asect->flags & SEC_MERGE) != 0)
3336 {
3337 this_hdr->sh_flags |= SHF_MERGE;
3338 this_hdr->sh_entsize = asect->entsize;
f5fa8ca2 3339 }
84865015
NC
3340 if ((asect->flags & SEC_STRINGS) != 0)
3341 this_hdr->sh_flags |= SHF_STRINGS;
1126897b 3342 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
dbb410c3 3343 this_hdr->sh_flags |= SHF_GROUP;
13ae64f3 3344 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
704afa60
JJ
3345 {
3346 this_hdr->sh_flags |= SHF_TLS;
3a800eb9
AM
3347 if (asect->size == 0
3348 && (asect->flags & SEC_HAS_CONTENTS) == 0)
704afa60 3349 {
3a800eb9 3350 struct bfd_link_order *o = asect->map_tail.link_order;
b34976b6 3351
704afa60 3352 this_hdr->sh_size = 0;
3a800eb9
AM
3353 if (o != NULL)
3354 {
704afa60 3355 this_hdr->sh_size = o->offset + o->size;
3a800eb9
AM
3356 if (this_hdr->sh_size != 0)
3357 this_hdr->sh_type = SHT_NOBITS;
3358 }
704afa60
JJ
3359 }
3360 }
18ae9cc1
L
3361 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3362 this_hdr->sh_flags |= SHF_EXCLUDE;
252b5132 3363
d4730f92
BS
3364 /* If the section has relocs, set up a section header for the
3365 SHT_REL[A] section. If two relocation sections are required for
3366 this section, it is up to the processor-specific back-end to
3367 create the other. */
3368 if ((asect->flags & SEC_RELOC) != 0)
3369 {
3370 /* When doing a relocatable link, create both REL and RELA sections if
3371 needed. */
3372 if (arg->link_info
3373 /* Do the normal setup if we wouldn't create any sections here. */
3374 && esd->rel.count + esd->rela.count > 0
0e1862bb
L
3375 && (bfd_link_relocatable (arg->link_info)
3376 || arg->link_info->emitrelocations))
d4730f92
BS
3377 {
3378 if (esd->rel.count && esd->rel.hdr == NULL
28e07a05 3379 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
db4677b8 3380 FALSE, delay_st_name_p))
d4730f92
BS
3381 {
3382 arg->failed = TRUE;
3383 return;
3384 }
3385 if (esd->rela.count && esd->rela.hdr == NULL
28e07a05 3386 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
db4677b8 3387 TRUE, delay_st_name_p))
d4730f92
BS
3388 {
3389 arg->failed = TRUE;
3390 return;
3391 }
3392 }
3393 else if (!_bfd_elf_init_reloc_shdr (abfd,
3394 (asect->use_rela_p
3395 ? &esd->rela : &esd->rel),
f6fe1ccd 3396 name,
3e19fb8f
L
3397 asect->use_rela_p,
3398 delay_st_name_p))
db4677b8 3399 {
d4730f92 3400 arg->failed = TRUE;
db4677b8
AM
3401 return;
3402 }
d4730f92
BS
3403 }
3404
252b5132 3405 /* Check for processor-specific section types. */
0414f35b 3406 sh_type = this_hdr->sh_type;
e1fddb6b
AO
3407 if (bed->elf_backend_fake_sections
3408 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
db4677b8
AM
3409 {
3410 arg->failed = TRUE;
3411 return;
3412 }
252b5132 3413
42bb2e33 3414 if (sh_type == SHT_NOBITS && asect->size != 0)
0414f35b
AM
3415 {
3416 /* Don't change the header type from NOBITS if we are being
42bb2e33 3417 called for objcopy --only-keep-debug. */
0414f35b
AM
3418 this_hdr->sh_type = sh_type;
3419 }
252b5132
RH
3420}
3421
bcacc0f5
AM
3422/* Fill in the contents of a SHT_GROUP section. Called from
3423 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3424 when ELF targets use the generic linker, ld. Called for ld -r
3425 from bfd_elf_final_link. */
dbb410c3 3426
1126897b 3427void
217aa764 3428bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
dbb410c3 3429{
a50b1753 3430 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
9dce4196 3431 asection *elt, *first;
dbb410c3 3432 unsigned char *loc;
b34976b6 3433 bfd_boolean gas;
dbb410c3 3434
7e4111ad
L
3435 /* Ignore linker created group section. See elfNN_ia64_object_p in
3436 elfxx-ia64.c. */
3437 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
dbb410c3
AM
3438 || *failedptr)
3439 return;
3440
bcacc0f5
AM
3441 if (elf_section_data (sec)->this_hdr.sh_info == 0)
3442 {
3443 unsigned long symindx = 0;
3444
3445 /* elf_group_id will have been set up by objcopy and the
3446 generic linker. */
3447 if (elf_group_id (sec) != NULL)
3448 symindx = elf_group_id (sec)->udata.i;
1126897b 3449
bcacc0f5
AM
3450 if (symindx == 0)
3451 {
3452 /* If called from the assembler, swap_out_syms will have set up
3453 elf_section_syms. */
3454 BFD_ASSERT (elf_section_syms (abfd) != NULL);
3455 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3456 }
3457 elf_section_data (sec)->this_hdr.sh_info = symindx;
3458 }
3459 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
1126897b 3460 {
bcacc0f5
AM
3461 /* The ELF backend linker sets sh_info to -2 when the group
3462 signature symbol is global, and thus the index can't be
3463 set until all local symbols are output. */
53720c49
AM
3464 asection *igroup;
3465 struct bfd_elf_section_data *sec_data;
3466 unsigned long symndx;
3467 unsigned long extsymoff;
bcacc0f5
AM
3468 struct elf_link_hash_entry *h;
3469
53720c49
AM
3470 /* The point of this little dance to the first SHF_GROUP section
3471 then back to the SHT_GROUP section is that this gets us to
3472 the SHT_GROUP in the input object. */
3473 igroup = elf_sec_group (elf_next_in_group (sec));
3474 sec_data = elf_section_data (igroup);
3475 symndx = sec_data->this_hdr.sh_info;
3476 extsymoff = 0;
bcacc0f5
AM
3477 if (!elf_bad_symtab (igroup->owner))
3478 {
3479 Elf_Internal_Shdr *symtab_hdr;
3480
3481 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3482 extsymoff = symtab_hdr->sh_info;
3483 }
3484 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3485 while (h->root.type == bfd_link_hash_indirect
3486 || h->root.type == bfd_link_hash_warning)
3487 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3488
3489 elf_section_data (sec)->this_hdr.sh_info = h->indx;
1126897b 3490 }
dbb410c3 3491
1126897b 3492 /* The contents won't be allocated for "ld -r" or objcopy. */
b34976b6 3493 gas = TRUE;
dbb410c3
AM
3494 if (sec->contents == NULL)
3495 {
b34976b6 3496 gas = FALSE;
a50b1753 3497 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
9dce4196
AM
3498
3499 /* Arrange for the section to be written out. */
3500 elf_section_data (sec)->this_hdr.contents = sec->contents;
dbb410c3
AM
3501 if (sec->contents == NULL)
3502 {
b34976b6 3503 *failedptr = TRUE;
dbb410c3
AM
3504 return;
3505 }
3506 }
3507
eea6121a 3508 loc = sec->contents + sec->size;
dbb410c3 3509
9dce4196
AM
3510 /* Get the pointer to the first section in the group that gas
3511 squirreled away here. objcopy arranges for this to be set to the
3512 start of the input section group. */
3513 first = elt = elf_next_in_group (sec);
dbb410c3
AM
3514
3515 /* First element is a flag word. Rest of section is elf section
3516 indices for all the sections of the group. Write them backwards
3517 just to keep the group in the same order as given in .section
3518 directives, not that it matters. */
3519 while (elt != NULL)
3520 {
9dce4196 3521 asection *s;
9dce4196 3522
9dce4196 3523 s = elt;
415f38a6
AM
3524 if (!gas)
3525 s = s->output_section;
3526 if (s != NULL
3527 && !bfd_is_abs_section (s))
01e1a5bc 3528 {
db4677b8 3529 struct bfd_elf_section_data *elf_sec = elf_section_data (s);
28e07a05
AM
3530 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
3531
3532 if (elf_sec->rel.hdr != NULL
3533 && (gas
3534 || (input_elf_sec->rel.hdr != NULL
3535 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3536 {
28e07a05 3537 elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3538 loc -= 4;
3539 H_PUT_32 (abfd, elf_sec->rel.idx, loc);
3540 }
28e07a05
AM
3541 if (elf_sec->rela.hdr != NULL
3542 && (gas
3543 || (input_elf_sec->rela.hdr != NULL
3544 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
db4677b8 3545 {
28e07a05 3546 elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
db4677b8
AM
3547 loc -= 4;
3548 H_PUT_32 (abfd, elf_sec->rela.idx, loc);
3549 }
01e1a5bc 3550 loc -= 4;
db4677b8 3551 H_PUT_32 (abfd, elf_sec->this_idx, loc);
01e1a5bc 3552 }
945906ff 3553 elt = elf_next_in_group (elt);
9dce4196
AM
3554 if (elt == first)
3555 break;
dbb410c3
AM
3556 }
3557
7bdf4127
AB
3558 loc -= 4;
3559 BFD_ASSERT (loc == sec->contents);
dbb410c3 3560
9dce4196 3561 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
dbb410c3
AM
3562}
3563
bce964aa
AM
3564/* Given NAME, the name of a relocation section stripped of its
3565 .rel/.rela prefix, return the section in ABFD to which the
3566 relocations apply. */
bd53a53a
L
3567
3568asection *
bce964aa
AM
3569_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
3570{
3571 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3572 section likely apply to .got.plt or .got section. */
3573 if (get_elf_backend_data (abfd)->want_got_plt
3574 && strcmp (name, ".plt") == 0)
3575 {
3576 asection *sec;
3577
3578 name = ".got.plt";
3579 sec = bfd_get_section_by_name (abfd, name);
3580 if (sec != NULL)
3581 return sec;
3582 name = ".got";
3583 }
3584
3585 return bfd_get_section_by_name (abfd, name);
3586}
3587
3588/* Return the section to which RELOC_SEC applies. */
3589
3590static asection *
3591elf_get_reloc_section (asection *reloc_sec)
bd53a53a
L
3592{
3593 const char *name;
3594 unsigned int type;
3595 bfd *abfd;
bce964aa 3596 const struct elf_backend_data *bed;
bd53a53a
L
3597
3598 type = elf_section_data (reloc_sec)->this_hdr.sh_type;
3599 if (type != SHT_REL && type != SHT_RELA)
3600 return NULL;
3601
3602 /* We look up the section the relocs apply to by name. */
3603 name = reloc_sec->name;
bce964aa
AM
3604 if (strncmp (name, ".rel", 4) != 0)
3605 return NULL;
3606 name += 4;
3607 if (type == SHT_RELA && *name++ != 'a')
3608 return NULL;
bd53a53a 3609
bd53a53a 3610 abfd = reloc_sec->owner;
bce964aa
AM
3611 bed = get_elf_backend_data (abfd);
3612 return bed->get_reloc_section (abfd, name);
bd53a53a
L
3613}
3614
252b5132
RH
3615/* Assign all ELF section numbers. The dummy first section is handled here
3616 too. The link/info pointers for the standard section types are filled
3617 in here too, while we're at it. */
3618
b34976b6 3619static bfd_boolean
da9f89d4 3620assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
252b5132
RH
3621{
3622 struct elf_obj_tdata *t = elf_tdata (abfd);
3623 asection *sec;
3e19fb8f 3624 unsigned int section_number;
252b5132 3625 Elf_Internal_Shdr **i_shdrp;
47cc2cf5 3626 struct bfd_elf_section_data *d;
3516e984 3627 bfd_boolean need_symtab;
252b5132
RH
3628
3629 section_number = 1;
3630
2b0f7ef9
JJ
3631 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3632
da9f89d4 3633 /* SHT_GROUP sections are in relocatable files only. */
7bdf4127 3634 if (link_info == NULL || !link_info->resolve_section_groups)
252b5132 3635 {
ef53be89 3636 size_t reloc_count = 0;
14f2c699 3637
da9f89d4 3638 /* Put SHT_GROUP sections first. */
04dd1667 3639 for (sec = abfd->sections; sec != NULL; sec = sec->next)
47cc2cf5 3640 {
5daa8fe7 3641 d = elf_section_data (sec);
da9f89d4
L
3642
3643 if (d->this_hdr.sh_type == SHT_GROUP)
08a40648 3644 {
5daa8fe7 3645 if (sec->flags & SEC_LINKER_CREATED)
da9f89d4
L
3646 {
3647 /* Remove the linker created SHT_GROUP sections. */
5daa8fe7 3648 bfd_section_list_remove (abfd, sec);
da9f89d4 3649 abfd->section_count--;
da9f89d4 3650 }
08a40648 3651 else
4fbb74a6 3652 d->this_idx = section_number++;
da9f89d4 3653 }
14f2c699
L
3654
3655 /* Count relocations. */
3656 reloc_count += sec->reloc_count;
47cc2cf5 3657 }
14f2c699
L
3658
3659 /* Clear HAS_RELOC if there are no relocations. */
3660 if (reloc_count == 0)
3661 abfd->flags &= ~HAS_RELOC;
47cc2cf5
PB
3662 }
3663
3664 for (sec = abfd->sections; sec; sec = sec->next)
3665 {
3666 d = elf_section_data (sec);
3667
3668 if (d->this_hdr.sh_type != SHT_GROUP)
4fbb74a6 3669 d->this_idx = section_number++;
3e19fb8f
L
3670 if (d->this_hdr.sh_name != (unsigned int) -1)
3671 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
d4730f92 3672 if (d->rel.hdr)
2b0f7ef9 3673 {
d4730f92 3674 d->rel.idx = section_number++;
3e19fb8f
L
3675 if (d->rel.hdr->sh_name != (unsigned int) -1)
3676 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2b0f7ef9 3677 }
d4730f92
BS
3678 else
3679 d->rel.idx = 0;
23bc299b 3680
d4730f92 3681 if (d->rela.hdr)
2b0f7ef9 3682 {
d4730f92 3683 d->rela.idx = section_number++;
3e19fb8f
L
3684 if (d->rela.hdr->sh_name != (unsigned int) -1)
3685 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2b0f7ef9 3686 }
23bc299b 3687 else
d4730f92 3688 d->rela.idx = 0;
252b5132
RH
3689 }
3690
3516e984
L
3691 need_symtab = (bfd_get_symcount (abfd) > 0
3692 || (link_info == NULL
3693 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3694 == HAS_RELOC)));
3695 if (need_symtab)
252b5132 3696 {
12bd6957 3697 elf_onesymtab (abfd) = section_number++;
2b0f7ef9 3698 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4fbb74a6 3699 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
9ad5cbcf 3700 {
6a40cf0c
NC
3701 elf_section_list * entry;
3702
3703 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
3704
3705 entry = bfd_zalloc (abfd, sizeof * entry);
3706 entry->ndx = section_number++;
3707 elf_symtab_shndx_list (abfd) = entry;
3708 entry->hdr.sh_name
9ad5cbcf 3709 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
b34976b6 3710 ".symtab_shndx", FALSE);
6a40cf0c 3711 if (entry->hdr.sh_name == (unsigned int) -1)
b34976b6 3712 return FALSE;
9ad5cbcf 3713 }
12bd6957 3714 elf_strtab_sec (abfd) = section_number++;
2b0f7ef9 3715 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
252b5132
RH
3716 }
3717
dd905818
NC
3718 elf_shstrtab_sec (abfd) = section_number++;
3719 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3720 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3721
1c52a645
L
3722 if (section_number >= SHN_LORESERVE)
3723 {
695344c0 3724 /* xgettext:c-format */
1c52a645
L
3725 _bfd_error_handler (_("%B: too many sections: %u"),
3726 abfd, section_number);
3727 return FALSE;
3728 }
3729
9ad5cbcf 3730 elf_numsections (abfd) = section_number;
252b5132
RH
3731 elf_elfheader (abfd)->e_shnum = section_number;
3732
3733 /* Set up the list of section header pointers, in agreement with the
3734 indices. */
a50b1753 3735 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
07d6d2b8 3736 sizeof (Elf_Internal_Shdr *));
252b5132 3737 if (i_shdrp == NULL)
b34976b6 3738 return FALSE;
252b5132 3739
a50b1753 3740 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
07d6d2b8 3741 sizeof (Elf_Internal_Shdr));
252b5132
RH
3742 if (i_shdrp[0] == NULL)
3743 {
3744 bfd_release (abfd, i_shdrp);
b34976b6 3745 return FALSE;
252b5132 3746 }
252b5132
RH
3747
3748 elf_elfsections (abfd) = i_shdrp;
3749
12bd6957 3750 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3516e984 3751 if (need_symtab)
252b5132 3752 {
12bd6957 3753 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4fbb74a6 3754 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
9ad5cbcf 3755 {
6a40cf0c
NC
3756 elf_section_list * entry = elf_symtab_shndx_list (abfd);
3757 BFD_ASSERT (entry != NULL);
3758 i_shdrp[entry->ndx] = & entry->hdr;
3759 entry->hdr.sh_link = elf_onesymtab (abfd);
9ad5cbcf 3760 }
12bd6957
AM
3761 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3762 t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
252b5132 3763 }
38ce5b11 3764
252b5132
RH
3765 for (sec = abfd->sections; sec; sec = sec->next)
3766 {
252b5132 3767 asection *s;
252b5132 3768
91d6fa6a
NC
3769 d = elf_section_data (sec);
3770
252b5132 3771 i_shdrp[d->this_idx] = &d->this_hdr;
d4730f92
BS
3772 if (d->rel.idx != 0)
3773 i_shdrp[d->rel.idx] = d->rel.hdr;
3774 if (d->rela.idx != 0)
3775 i_shdrp[d->rela.idx] = d->rela.hdr;
252b5132
RH
3776
3777 /* Fill in the sh_link and sh_info fields while we're at it. */
3778
3779 /* sh_link of a reloc section is the section index of the symbol
3780 table. sh_info is the section index of the section to which
3781 the relocation entries apply. */
d4730f92 3782 if (d->rel.idx != 0)
252b5132 3783 {
12bd6957 3784 d->rel.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3785 d->rel.hdr->sh_info = d->this_idx;
9ef5d938 3786 d->rel.hdr->sh_flags |= SHF_INFO_LINK;
252b5132 3787 }
d4730f92 3788 if (d->rela.idx != 0)
23bc299b 3789 {
12bd6957 3790 d->rela.hdr->sh_link = elf_onesymtab (abfd);
d4730f92 3791 d->rela.hdr->sh_info = d->this_idx;
9ef5d938 3792 d->rela.hdr->sh_flags |= SHF_INFO_LINK;
23bc299b 3793 }
252b5132 3794
38ce5b11
L
3795 /* We need to set up sh_link for SHF_LINK_ORDER. */
3796 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3797 {
3798 s = elf_linked_to_section (sec);
3799 if (s)
38ce5b11 3800 {
f2876037 3801 /* elf_linked_to_section points to the input section. */
ccd2ec6a 3802 if (link_info != NULL)
38ce5b11 3803 {
f2876037 3804 /* Check discarded linkonce section. */
dbaa2011 3805 if (discarded_section (s))
38ce5b11 3806 {
ccd2ec6a 3807 asection *kept;
4eca0228 3808 _bfd_error_handler
695344c0 3809 /* xgettext:c-format */
63a5468a
AM
3810 (_("%B: sh_link of section `%A' points to"
3811 " discarded section `%A' of `%B'"),
ccd2ec6a
L
3812 abfd, d->this_hdr.bfd_section,
3813 s, s->owner);
3814 /* Point to the kept section if it has the same
3815 size as the discarded one. */
c0f00686 3816 kept = _bfd_elf_check_kept_section (s, link_info);
ccd2ec6a 3817 if (kept == NULL)
185d09ad 3818 {
ccd2ec6a
L
3819 bfd_set_error (bfd_error_bad_value);
3820 return FALSE;
185d09ad 3821 }
ccd2ec6a 3822 s = kept;
38ce5b11 3823 }
e424ecc8 3824
ccd2ec6a
L
3825 s = s->output_section;
3826 BFD_ASSERT (s != NULL);
38ce5b11 3827 }
f2876037
L
3828 else
3829 {
3830 /* Handle objcopy. */
3831 if (s->output_section == NULL)
3832 {
4eca0228 3833 _bfd_error_handler
695344c0 3834 /* xgettext:c-format */
63a5468a
AM
3835 (_("%B: sh_link of section `%A' points to"
3836 " removed section `%A' of `%B'"),
f2876037
L
3837 abfd, d->this_hdr.bfd_section, s, s->owner);
3838 bfd_set_error (bfd_error_bad_value);
3839 return FALSE;
3840 }
3841 s = s->output_section;
3842 }
ccd2ec6a
L
3843 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3844 }
3845 else
3846 {
3847 /* PR 290:
3848 The Intel C compiler generates SHT_IA_64_UNWIND with
3849 SHF_LINK_ORDER. But it doesn't set the sh_link or
3850 sh_info fields. Hence we could get the situation
08a40648 3851 where s is NULL. */
ccd2ec6a
L
3852 const struct elf_backend_data *bed
3853 = get_elf_backend_data (abfd);
3854 if (bed->link_order_error_handler)
3855 bed->link_order_error_handler
695344c0 3856 /* xgettext:c-format */
ccd2ec6a
L
3857 (_("%B: warning: sh_link not set for section `%A'"),
3858 abfd, sec);
38ce5b11
L
3859 }
3860 }
3861
252b5132
RH
3862 switch (d->this_hdr.sh_type)
3863 {
3864 case SHT_REL:
3865 case SHT_RELA:
3866 /* A reloc section which we are treating as a normal BFD
3867 section. sh_link is the section index of the symbol
3868 table. sh_info is the section index of the section to
3869 which the relocation entries apply. We assume that an
3870 allocated reloc section uses the dynamic symbol table.
3871 FIXME: How can we be sure? */
3872 s = bfd_get_section_by_name (abfd, ".dynsym");
3873 if (s != NULL)
3874 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3875
bce964aa 3876 s = elf_get_reloc_section (sec);
252b5132 3877 if (s != NULL)
9ef5d938
L
3878 {
3879 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3880 d->this_hdr.sh_flags |= SHF_INFO_LINK;
3881 }
252b5132
RH
3882 break;
3883
3884 case SHT_STRTAB:
3885 /* We assume that a section named .stab*str is a stabs
3886 string section. We look for a section with the same name
3887 but without the trailing ``str'', and set its sh_link
3888 field to point to this section. */
0112cd26 3889 if (CONST_STRNEQ (sec->name, ".stab")
252b5132
RH
3890 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3891 {
3892 size_t len;
3893 char *alc;
3894
3895 len = strlen (sec->name);
a50b1753 3896 alc = (char *) bfd_malloc (len - 2);
252b5132 3897 if (alc == NULL)
b34976b6 3898 return FALSE;
d4c88bbb 3899 memcpy (alc, sec->name, len - 3);
252b5132
RH
3900 alc[len - 3] = '\0';
3901 s = bfd_get_section_by_name (abfd, alc);
3902 free (alc);
3903 if (s != NULL)
3904 {
3905 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3906
3907 /* This is a .stab section. */
0594c12d
AM
3908 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3909 elf_section_data (s)->this_hdr.sh_entsize
3910 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
252b5132
RH
3911 }
3912 }
3913 break;
3914
3915 case SHT_DYNAMIC:
3916 case SHT_DYNSYM:
3917 case SHT_GNU_verneed:
3918 case SHT_GNU_verdef:
3919 /* sh_link is the section header index of the string table
3920 used for the dynamic entries, or the symbol table, or the
3921 version strings. */
3922 s = bfd_get_section_by_name (abfd, ".dynstr");
3923 if (s != NULL)
3924 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3925 break;
3926
7f1204bb
JJ
3927 case SHT_GNU_LIBLIST:
3928 /* sh_link is the section header index of the prelink library
08a40648
AM
3929 list used for the dynamic entries, or the symbol table, or
3930 the version strings. */
7f1204bb
JJ
3931 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3932 ? ".dynstr" : ".gnu.libstr");
3933 if (s != NULL)
3934 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3935 break;
3936
252b5132 3937 case SHT_HASH:
fdc90cb4 3938 case SHT_GNU_HASH:
252b5132
RH
3939 case SHT_GNU_versym:
3940 /* sh_link is the section header index of the symbol table
3941 this hash table or version table is for. */
3942 s = bfd_get_section_by_name (abfd, ".dynsym");
3943 if (s != NULL)
3944 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3945 break;
dbb410c3
AM
3946
3947 case SHT_GROUP:
12bd6957 3948 d->this_hdr.sh_link = elf_onesymtab (abfd);
252b5132
RH
3949 }
3950 }
3951
3e19fb8f
L
3952 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3953 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3954 debug section name from .debug_* to .zdebug_* if needed. */
3955
b34976b6 3956 return TRUE;
252b5132
RH
3957}
3958
5372391b 3959static bfd_boolean
217aa764 3960sym_is_global (bfd *abfd, asymbol *sym)
252b5132
RH
3961{
3962 /* If the backend has a special mapping, use it. */
9c5bfbb7 3963 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764
AM
3964 if (bed->elf_backend_sym_is_global)
3965 return (*bed->elf_backend_sym_is_global) (abfd, sym);
252b5132 3966
e47bf690 3967 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
252b5132
RH
3968 || bfd_is_und_section (bfd_get_section (sym))
3969 || bfd_is_com_section (bfd_get_section (sym)));
3970}
3971
76359541
TP
3972/* Filter global symbols of ABFD to include in the import library. All
3973 SYMCOUNT symbols of ABFD can be examined from their pointers in
3974 SYMS. Pointers of symbols to keep should be stored contiguously at
3975 the beginning of that array.
3976
3977 Returns the number of symbols to keep. */
3978
3979unsigned int
3980_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
3981 asymbol **syms, long symcount)
3982{
3983 long src_count, dst_count = 0;
3984
3985 for (src_count = 0; src_count < symcount; src_count++)
3986 {
3987 asymbol *sym = syms[src_count];
3988 char *name = (char *) bfd_asymbol_name (sym);
3989 struct bfd_link_hash_entry *h;
3990
3991 if (!sym_is_global (abfd, sym))
3992 continue;
3993
3994 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
5df1bc57
AM
3995 if (h == NULL)
3996 continue;
76359541
TP
3997 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
3998 continue;
76359541
TP
3999 if (h->linker_def || h->ldscript_def)
4000 continue;
4001
4002 syms[dst_count++] = sym;
4003 }
4004
4005 syms[dst_count] = NULL;
4006
4007 return dst_count;
4008}
4009
5372391b 4010/* Don't output section symbols for sections that are not going to be
c6d8cab4 4011 output, that are duplicates or there is no BFD section. */
5372391b
AM
4012
4013static bfd_boolean
4014ignore_section_sym (bfd *abfd, asymbol *sym)
4015{
c6d8cab4
L
4016 elf_symbol_type *type_ptr;
4017
4018 if ((sym->flags & BSF_SECTION_SYM) == 0)
4019 return FALSE;
4020
4021 type_ptr = elf_symbol_from (abfd, sym);
4022 return ((type_ptr != NULL
4023 && type_ptr->internal_elf_sym.st_shndx != 0
4024 && bfd_is_abs_section (sym->section))
4025 || !(sym->section->owner == abfd
0f0a5e58 4026 || (sym->section->output_section->owner == abfd
2633a79c
AM
4027 && sym->section->output_offset == 0)
4028 || bfd_is_abs_section (sym->section)));
5372391b
AM
4029}
4030
2633a79c
AM
4031/* Map symbol from it's internal number to the external number, moving
4032 all local symbols to be at the head of the list. */
4033
b34976b6 4034static bfd_boolean
12bd6957 4035elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
252b5132 4036{
dc810e39 4037 unsigned int symcount = bfd_get_symcount (abfd);
252b5132
RH
4038 asymbol **syms = bfd_get_outsymbols (abfd);
4039 asymbol **sect_syms;
dc810e39
AM
4040 unsigned int num_locals = 0;
4041 unsigned int num_globals = 0;
4042 unsigned int num_locals2 = 0;
4043 unsigned int num_globals2 = 0;
7292b3ac 4044 unsigned int max_index = 0;
dc810e39 4045 unsigned int idx;
252b5132
RH
4046 asection *asect;
4047 asymbol **new_syms;
252b5132
RH
4048
4049#ifdef DEBUG
4050 fprintf (stderr, "elf_map_symbols\n");
4051 fflush (stderr);
4052#endif
4053
252b5132
RH
4054 for (asect = abfd->sections; asect; asect = asect->next)
4055 {
4056 if (max_index < asect->index)
4057 max_index = asect->index;
4058 }
4059
4060 max_index++;
a50b1753 4061 sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
252b5132 4062 if (sect_syms == NULL)
b34976b6 4063 return FALSE;
252b5132 4064 elf_section_syms (abfd) = sect_syms;
4e89ac30 4065 elf_num_section_syms (abfd) = max_index;
252b5132 4066
079e9a2f
AM
4067 /* Init sect_syms entries for any section symbols we have already
4068 decided to output. */
252b5132
RH
4069 for (idx = 0; idx < symcount; idx++)
4070 {
dc810e39 4071 asymbol *sym = syms[idx];
c044fabd 4072
252b5132 4073 if ((sym->flags & BSF_SECTION_SYM) != 0
0f0a5e58 4074 && sym->value == 0
2633a79c
AM
4075 && !ignore_section_sym (abfd, sym)
4076 && !bfd_is_abs_section (sym->section))
252b5132 4077 {
5372391b 4078 asection *sec = sym->section;
252b5132 4079
5372391b
AM
4080 if (sec->owner != abfd)
4081 sec = sec->output_section;
252b5132 4082
5372391b 4083 sect_syms[sec->index] = syms[idx];
252b5132
RH
4084 }
4085 }
4086
252b5132
RH
4087 /* Classify all of the symbols. */
4088 for (idx = 0; idx < symcount; idx++)
4089 {
2633a79c 4090 if (sym_is_global (abfd, syms[idx]))
252b5132 4091 num_globals++;
2633a79c
AM
4092 else if (!ignore_section_sym (abfd, syms[idx]))
4093 num_locals++;
252b5132 4094 }
079e9a2f 4095
5372391b 4096 /* We will be adding a section symbol for each normal BFD section. Most
079e9a2f
AM
4097 sections will already have a section symbol in outsymbols, but
4098 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4099 at least in that case. */
252b5132
RH
4100 for (asect = abfd->sections; asect; asect = asect->next)
4101 {
079e9a2f 4102 if (sect_syms[asect->index] == NULL)
252b5132 4103 {
079e9a2f 4104 if (!sym_is_global (abfd, asect->symbol))
252b5132
RH
4105 num_locals++;
4106 else
4107 num_globals++;
252b5132
RH
4108 }
4109 }
4110
4111 /* Now sort the symbols so the local symbols are first. */
a50b1753 4112 new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
07d6d2b8 4113 sizeof (asymbol *));
dc810e39 4114
252b5132 4115 if (new_syms == NULL)
b34976b6 4116 return FALSE;
252b5132
RH
4117
4118 for (idx = 0; idx < symcount; idx++)
4119 {
4120 asymbol *sym = syms[idx];
dc810e39 4121 unsigned int i;
252b5132 4122
2633a79c
AM
4123 if (sym_is_global (abfd, sym))
4124 i = num_locals + num_globals2++;
4125 else if (!ignore_section_sym (abfd, sym))
252b5132
RH
4126 i = num_locals2++;
4127 else
2633a79c 4128 continue;
252b5132
RH
4129 new_syms[i] = sym;
4130 sym->udata.i = i + 1;
4131 }
4132 for (asect = abfd->sections; asect; asect = asect->next)
4133 {
079e9a2f 4134 if (sect_syms[asect->index] == NULL)
252b5132 4135 {
079e9a2f 4136 asymbol *sym = asect->symbol;
dc810e39 4137 unsigned int i;
252b5132 4138
079e9a2f 4139 sect_syms[asect->index] = sym;
252b5132
RH
4140 if (!sym_is_global (abfd, sym))
4141 i = num_locals2++;
4142 else
4143 i = num_locals + num_globals2++;
4144 new_syms[i] = sym;
4145 sym->udata.i = i + 1;
4146 }
4147 }
4148
4149 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4150
12bd6957 4151 *pnum_locals = num_locals;
b34976b6 4152 return TRUE;
252b5132
RH
4153}
4154
4155/* Align to the maximum file alignment that could be required for any
4156 ELF data structure. */
4157
268b6b39 4158static inline file_ptr
217aa764 4159align_file_position (file_ptr off, int align)
252b5132
RH
4160{
4161 return (off + align - 1) & ~(align - 1);
4162}
4163
4164/* Assign a file position to a section, optionally aligning to the
4165 required section alignment. */
4166
217aa764
AM
4167file_ptr
4168_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4169 file_ptr offset,
4170 bfd_boolean align)
252b5132 4171{
72de5009
AM
4172 if (align && i_shdrp->sh_addralign > 1)
4173 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
252b5132
RH
4174 i_shdrp->sh_offset = offset;
4175 if (i_shdrp->bfd_section != NULL)
4176 i_shdrp->bfd_section->filepos = offset;
4177 if (i_shdrp->sh_type != SHT_NOBITS)
4178 offset += i_shdrp->sh_size;
4179 return offset;
4180}
4181
4182/* Compute the file positions we are going to put the sections at, and
4183 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4184 is not NULL, this is being called by the ELF backend linker. */
4185
b34976b6 4186bfd_boolean
217aa764
AM
4187_bfd_elf_compute_section_file_positions (bfd *abfd,
4188 struct bfd_link_info *link_info)
252b5132 4189{
9c5bfbb7 4190 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
d4730f92 4191 struct fake_section_arg fsargs;
b34976b6 4192 bfd_boolean failed;
ef10c3ac 4193 struct elf_strtab_hash *strtab = NULL;
252b5132 4194 Elf_Internal_Shdr *shstrtab_hdr;
3516e984 4195 bfd_boolean need_symtab;
252b5132
RH
4196
4197 if (abfd->output_has_begun)
b34976b6 4198 return TRUE;
252b5132
RH
4199
4200 /* Do any elf backend specific processing first. */
4201 if (bed->elf_backend_begin_write_processing)
4202 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4203
4204 if (! prep_headers (abfd))
b34976b6 4205 return FALSE;
252b5132 4206
e6c51ed4 4207 /* Post process the headers if necessary. */
78245035 4208 (*bed->elf_backend_post_process_headers) (abfd, link_info);
e6c51ed4 4209
d4730f92
BS
4210 fsargs.failed = FALSE;
4211 fsargs.link_info = link_info;
4212 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4213 if (fsargs.failed)
b34976b6 4214 return FALSE;
252b5132 4215
da9f89d4 4216 if (!assign_section_numbers (abfd, link_info))
b34976b6 4217 return FALSE;
252b5132
RH
4218
4219 /* The backend linker builds symbol table information itself. */
3516e984
L
4220 need_symtab = (link_info == NULL
4221 && (bfd_get_symcount (abfd) > 0
4222 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4223 == HAS_RELOC)));
4224 if (need_symtab)
252b5132
RH
4225 {
4226 /* Non-zero if doing a relocatable link. */
4227 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4228
4229 if (! swap_out_syms (abfd, &strtab, relocatable_p))
b34976b6 4230 return FALSE;
252b5132
RH
4231 }
4232
d4730f92 4233 failed = FALSE;
1126897b 4234 if (link_info == NULL)
dbb410c3 4235 {
1126897b 4236 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
dbb410c3 4237 if (failed)
b34976b6 4238 return FALSE;
dbb410c3
AM
4239 }
4240
252b5132
RH
4241 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4242 /* sh_name was set in prep_headers. */
4243 shstrtab_hdr->sh_type = SHT_STRTAB;
84865015 4244 shstrtab_hdr->sh_flags = bed->elf_strtab_flags;
252b5132 4245 shstrtab_hdr->sh_addr = 0;
946748d5 4246 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4247 shstrtab_hdr->sh_entsize = 0;
4248 shstrtab_hdr->sh_link = 0;
4249 shstrtab_hdr->sh_info = 0;
3e19fb8f 4250 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
252b5132
RH
4251 shstrtab_hdr->sh_addralign = 1;
4252
c84fca4d 4253 if (!assign_file_positions_except_relocs (abfd, link_info))
b34976b6 4254 return FALSE;
252b5132 4255
3516e984 4256 if (need_symtab)
252b5132
RH
4257 {
4258 file_ptr off;
4259 Elf_Internal_Shdr *hdr;
4260
12bd6957 4261 off = elf_next_file_pos (abfd);
252b5132 4262
6a40cf0c 4263 hdr = & elf_symtab_hdr (abfd);
b34976b6 4264 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4265
6a40cf0c
NC
4266 if (elf_symtab_shndx_list (abfd) != NULL)
4267 {
4268 hdr = & elf_symtab_shndx_list (abfd)->hdr;
4269 if (hdr->sh_size != 0)
4270 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4271 /* FIXME: What about other symtab_shndx sections in the list ? */
4272 }
9ad5cbcf 4273
252b5132 4274 hdr = &elf_tdata (abfd)->strtab_hdr;
b34976b6 4275 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 4276
12bd6957 4277 elf_next_file_pos (abfd) = off;
252b5132
RH
4278
4279 /* Now that we know where the .strtab section goes, write it
08a40648 4280 out. */
252b5132 4281 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ef10c3ac 4282 || ! _bfd_elf_strtab_emit (abfd, strtab))
b34976b6 4283 return FALSE;
ef10c3ac 4284 _bfd_elf_strtab_free (strtab);
252b5132
RH
4285 }
4286
b34976b6 4287 abfd->output_has_begun = TRUE;
252b5132 4288
b34976b6 4289 return TRUE;
252b5132
RH
4290}
4291
8ded5a0f
AM
4292/* Make an initial estimate of the size of the program header. If we
4293 get the number wrong here, we'll redo section placement. */
4294
4295static bfd_size_type
4296get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4297{
4298 size_t segs;
4299 asection *s;
2b05f1b7 4300 const struct elf_backend_data *bed;
8ded5a0f
AM
4301
4302 /* Assume we will need exactly two PT_LOAD segments: one for text
4303 and one for data. */
4304 segs = 2;
4305
4306 s = bfd_get_section_by_name (abfd, ".interp");
4307 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4308 {
4309 /* If we have a loadable interpreter section, we need a
4310 PT_INTERP segment. In this case, assume we also need a
4311 PT_PHDR segment, although that may not be true for all
4312 targets. */
e9a38e0f 4313 segs += 2;
8ded5a0f
AM
4314 }
4315
4316 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4317 {
4318 /* We need a PT_DYNAMIC segment. */
4319 ++segs;
f210dcff 4320 }
08a40648 4321
ceae84aa 4322 if (info != NULL && info->relro)
f210dcff
L
4323 {
4324 /* We need a PT_GNU_RELRO segment. */
4325 ++segs;
8ded5a0f
AM
4326 }
4327
12bd6957 4328 if (elf_eh_frame_hdr (abfd))
8ded5a0f
AM
4329 {
4330 /* We need a PT_GNU_EH_FRAME segment. */
4331 ++segs;
4332 }
4333
12bd6957 4334 if (elf_stack_flags (abfd))
8ded5a0f 4335 {
2b05f1b7
L
4336 /* We need a PT_GNU_STACK segment. */
4337 ++segs;
4338 }
94b11780 4339
2b05f1b7
L
4340 for (s = abfd->sections; s != NULL; s = s->next)
4341 {
8ded5a0f 4342 if ((s->flags & SEC_LOAD) != 0
0112cd26 4343 && CONST_STRNEQ (s->name, ".note"))
8ded5a0f
AM
4344 {
4345 /* We need a PT_NOTE segment. */
4346 ++segs;
1c5265b5
JJ
4347 /* Try to create just one PT_NOTE segment
4348 for all adjacent loadable .note* sections.
4349 gABI requires that within a PT_NOTE segment
4350 (and also inside of each SHT_NOTE section)
4351 each note is padded to a multiple of 4 size,
4352 so we check whether the sections are correctly
4353 aligned. */
4354 if (s->alignment_power == 2)
4355 while (s->next != NULL
4356 && s->next->alignment_power == 2
4357 && (s->next->flags & SEC_LOAD) != 0
4358 && CONST_STRNEQ (s->next->name, ".note"))
4359 s = s->next;
8ded5a0f
AM
4360 }
4361 }
4362
4363 for (s = abfd->sections; s != NULL; s = s->next)
4364 {
4365 if (s->flags & SEC_THREAD_LOCAL)
4366 {
4367 /* We need a PT_TLS segment. */
4368 ++segs;
4369 break;
4370 }
4371 }
4372
2b05f1b7 4373 bed = get_elf_backend_data (abfd);
a91e1603
L
4374
4375 if ((abfd->flags & D_PAGED) != 0)
4376 {
4377 /* Add a PT_GNU_MBIND segment for each mbind section. */
4378 unsigned int page_align_power = bfd_log2 (bed->commonpagesize);
4379 for (s = abfd->sections; s != NULL; s = s->next)
4380 if (elf_section_flags (s) & SHF_GNU_MBIND)
4381 {
4382 if (elf_section_data (s)->this_hdr.sh_info
4383 > PT_GNU_MBIND_NUM)
4384 {
4385 _bfd_error_handler
4386 /* xgettext:c-format */
4387 (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"),
4388 abfd, s, elf_section_data (s)->this_hdr.sh_info);
4389 continue;
4390 }
4391 /* Align mbind section to page size. */
4392 if (s->alignment_power < page_align_power)
4393 s->alignment_power = page_align_power;
4394 segs ++;
4395 }
4396 }
4397
4398 /* Let the backend count up any program headers it might need. */
4399 if (bed->elf_backend_additional_program_headers)
8ded5a0f
AM
4400 {
4401 int a;
4402
4403 a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4404 if (a == -1)
4405 abort ();
4406 segs += a;
4407 }
4408
4409 return segs * bed->s->sizeof_phdr;
4410}
4411
2ea37f1c
NC
4412/* Find the segment that contains the output_section of section. */
4413
4414Elf_Internal_Phdr *
4415_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4416{
4417 struct elf_segment_map *m;
4418 Elf_Internal_Phdr *p;
4419
12bd6957 4420 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
2ea37f1c
NC
4421 m != NULL;
4422 m = m->next, p++)
4423 {
4424 int i;
4425
4426 for (i = m->count - 1; i >= 0; i--)
4427 if (m->sections[i] == section)
4428 return p;
4429 }
4430
4431 return NULL;
4432}
4433
252b5132
RH
4434/* Create a mapping from a set of sections to a program segment. */
4435
217aa764
AM
4436static struct elf_segment_map *
4437make_mapping (bfd *abfd,
4438 asection **sections,
4439 unsigned int from,
4440 unsigned int to,
4441 bfd_boolean phdr)
252b5132
RH
4442{
4443 struct elf_segment_map *m;
4444 unsigned int i;
4445 asection **hdrpp;
dc810e39 4446 bfd_size_type amt;
252b5132 4447
dc810e39
AM
4448 amt = sizeof (struct elf_segment_map);
4449 amt += (to - from - 1) * sizeof (asection *);
a50b1753 4450 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4451 if (m == NULL)
4452 return NULL;
4453 m->next = NULL;
4454 m->p_type = PT_LOAD;
4455 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4456 m->sections[i - from] = *hdrpp;
4457 m->count = to - from;
4458
4459 if (from == 0 && phdr)
4460 {
4461 /* Include the headers in the first PT_LOAD segment. */
4462 m->includes_filehdr = 1;
4463 m->includes_phdrs = 1;
4464 }
4465
4466 return m;
4467}
4468
229fcec5
MM
4469/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4470 on failure. */
4471
4472struct elf_segment_map *
4473_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4474{
4475 struct elf_segment_map *m;
4476
a50b1753 4477 m = (struct elf_segment_map *) bfd_zalloc (abfd,
07d6d2b8 4478 sizeof (struct elf_segment_map));
229fcec5
MM
4479 if (m == NULL)
4480 return NULL;
4481 m->next = NULL;
4482 m->p_type = PT_DYNAMIC;
4483 m->count = 1;
4484 m->sections[0] = dynsec;
08a40648 4485
229fcec5
MM
4486 return m;
4487}
4488
8ded5a0f 4489/* Possibly add or remove segments from the segment map. */
252b5132 4490
b34976b6 4491static bfd_boolean
3dea8fca
AM
4492elf_modify_segment_map (bfd *abfd,
4493 struct bfd_link_info *info,
4494 bfd_boolean remove_empty_load)
252b5132 4495{
252e386e 4496 struct elf_segment_map **m;
8ded5a0f 4497 const struct elf_backend_data *bed;
252b5132 4498
8ded5a0f
AM
4499 /* The placement algorithm assumes that non allocated sections are
4500 not in PT_LOAD segments. We ensure this here by removing such
4501 sections from the segment map. We also remove excluded
252e386e
AM
4502 sections. Finally, any PT_LOAD segment without sections is
4503 removed. */
12bd6957 4504 m = &elf_seg_map (abfd);
252e386e 4505 while (*m)
8ded5a0f
AM
4506 {
4507 unsigned int i, new_count;
252b5132 4508
252e386e 4509 for (new_count = 0, i = 0; i < (*m)->count; i++)
8ded5a0f 4510 {
252e386e
AM
4511 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4512 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4513 || (*m)->p_type != PT_LOAD))
8ded5a0f 4514 {
252e386e
AM
4515 (*m)->sections[new_count] = (*m)->sections[i];
4516 new_count++;
8ded5a0f
AM
4517 }
4518 }
252e386e 4519 (*m)->count = new_count;
252b5132 4520
1a9ccd70
NC
4521 if (remove_empty_load
4522 && (*m)->p_type == PT_LOAD
4523 && (*m)->count == 0
4524 && !(*m)->includes_phdrs)
252e386e
AM
4525 *m = (*m)->next;
4526 else
4527 m = &(*m)->next;
8ded5a0f 4528 }
252b5132 4529
8ded5a0f
AM
4530 bed = get_elf_backend_data (abfd);
4531 if (bed->elf_backend_modify_segment_map != NULL)
252b5132 4532 {
252e386e 4533 if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
8ded5a0f 4534 return FALSE;
252b5132 4535 }
252b5132 4536
8ded5a0f
AM
4537 return TRUE;
4538}
252b5132 4539
8ded5a0f 4540/* Set up a mapping from BFD sections to program segments. */
252b5132 4541
8ded5a0f
AM
4542bfd_boolean
4543_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4544{
4545 unsigned int count;
4546 struct elf_segment_map *m;
4547 asection **sections = NULL;
4548 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3dea8fca 4549 bfd_boolean no_user_phdrs;
252b5132 4550
12bd6957 4551 no_user_phdrs = elf_seg_map (abfd) == NULL;
d324f6d6
RM
4552
4553 if (info != NULL)
4554 info->user_phdrs = !no_user_phdrs;
4555
3dea8fca 4556 if (no_user_phdrs && bfd_count_sections (abfd) != 0)
252b5132 4557 {
8ded5a0f
AM
4558 asection *s;
4559 unsigned int i;
4560 struct elf_segment_map *mfirst;
4561 struct elf_segment_map **pm;
4562 asection *last_hdr;
4563 bfd_vma last_size;
4564 unsigned int phdr_index;
4565 bfd_vma maxpagesize;
4566 asection **hdrpp;
4567 bfd_boolean phdr_in_segment = TRUE;
4568 bfd_boolean writable;
4569 int tls_count = 0;
4570 asection *first_tls = NULL;
a91e1603 4571 asection *first_mbind = NULL;
8ded5a0f
AM
4572 asection *dynsec, *eh_frame_hdr;
4573 bfd_size_type amt;
8d06853e 4574 bfd_vma addr_mask, wrap_to = 0;
1a9ccd70 4575 bfd_boolean linker_created_pt_phdr_segment = FALSE;
252b5132 4576
8ded5a0f 4577 /* Select the allocated sections, and sort them. */
252b5132 4578
a50b1753 4579 sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
07d6d2b8 4580 sizeof (asection *));
8ded5a0f 4581 if (sections == NULL)
252b5132 4582 goto error_return;
252b5132 4583
8d06853e
AM
4584 /* Calculate top address, avoiding undefined behaviour of shift
4585 left operator when shift count is equal to size of type
4586 being shifted. */
4587 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4588 addr_mask = (addr_mask << 1) + 1;
4589
8ded5a0f
AM
4590 i = 0;
4591 for (s = abfd->sections; s != NULL; s = s->next)
4592 {
4593 if ((s->flags & SEC_ALLOC) != 0)
4594 {
4595 sections[i] = s;
4596 ++i;
8d06853e
AM
4597 /* A wrapping section potentially clashes with header. */
4598 if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4599 wrap_to = (s->lma + s->size) & addr_mask;
8ded5a0f
AM
4600 }
4601 }
4602 BFD_ASSERT (i <= bfd_count_sections (abfd));
4603 count = i;
252b5132 4604
8ded5a0f 4605 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
252b5132 4606
8ded5a0f 4607 /* Build the mapping. */
252b5132 4608
8ded5a0f
AM
4609 mfirst = NULL;
4610 pm = &mfirst;
252b5132 4611
8ded5a0f
AM
4612 /* If we have a .interp section, then create a PT_PHDR segment for
4613 the program headers and a PT_INTERP segment for the .interp
4614 section. */
4615 s = bfd_get_section_by_name (abfd, ".interp");
4616 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4617 {
4618 amt = sizeof (struct elf_segment_map);
a50b1753 4619 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4620 if (m == NULL)
4621 goto error_return;
4622 m->next = NULL;
4623 m->p_type = PT_PHDR;
f882209d 4624 m->p_flags = PF_R;
8ded5a0f
AM
4625 m->p_flags_valid = 1;
4626 m->includes_phdrs = 1;
1a9ccd70 4627 linker_created_pt_phdr_segment = TRUE;
8ded5a0f
AM
4628 *pm = m;
4629 pm = &m->next;
252b5132 4630
8ded5a0f 4631 amt = sizeof (struct elf_segment_map);
a50b1753 4632 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4633 if (m == NULL)
4634 goto error_return;
4635 m->next = NULL;
4636 m->p_type = PT_INTERP;
4637 m->count = 1;
4638 m->sections[0] = s;
4639
4640 *pm = m;
4641 pm = &m->next;
252b5132 4642 }
8ded5a0f
AM
4643
4644 /* Look through the sections. We put sections in the same program
4645 segment when the start of the second section can be placed within
4646 a few bytes of the end of the first section. */
4647 last_hdr = NULL;
4648 last_size = 0;
4649 phdr_index = 0;
4650 maxpagesize = bed->maxpagesize;
063bb025
NC
4651 /* PR 17512: file: c8455299.
4652 Avoid divide-by-zero errors later on.
4653 FIXME: Should we abort if the maxpagesize is zero ? */
4654 if (maxpagesize == 0)
4655 maxpagesize = 1;
8ded5a0f
AM
4656 writable = FALSE;
4657 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4658 if (dynsec != NULL
4659 && (dynsec->flags & SEC_LOAD) == 0)
4660 dynsec = NULL;
4661
4662 /* Deal with -Ttext or something similar such that the first section
4663 is not adjacent to the program headers. This is an
4664 approximation, since at this point we don't know exactly how many
4665 program headers we will need. */
4666 if (count > 0)
252b5132 4667 {
12bd6957 4668 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 4669
62d7a5f6 4670 if (phdr_size == (bfd_size_type) -1)
8ded5a0f 4671 phdr_size = get_program_header_size (abfd, info);
d2bcb0d1 4672 phdr_size += bed->s->sizeof_ehdr;
8ded5a0f 4673 if ((abfd->flags & D_PAGED) == 0
8d06853e
AM
4674 || (sections[0]->lma & addr_mask) < phdr_size
4675 || ((sections[0]->lma & addr_mask) % maxpagesize
4676 < phdr_size % maxpagesize)
4677 || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
1a9ccd70
NC
4678 {
4679 /* PR 20815: The ELF standard says that a PT_PHDR segment, if
4680 present, must be included as part of the memory image of the
4681 program. Ie it must be part of a PT_LOAD segment as well.
4682 If we have had to create our own PT_PHDR segment, but it is
4683 not going to be covered by the first PT_LOAD segment, then
4684 force the inclusion if we can... */
4685 if ((abfd->flags & D_PAGED) != 0
4686 && linker_created_pt_phdr_segment)
4687 phdr_in_segment = TRUE;
4688 else
4689 phdr_in_segment = FALSE;
4690 }
252b5132
RH
4691 }
4692
8ded5a0f 4693 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
252b5132 4694 {
8ded5a0f
AM
4695 asection *hdr;
4696 bfd_boolean new_segment;
4697
4698 hdr = *hdrpp;
4699
4700 /* See if this section and the last one will fit in the same
4701 segment. */
4702
4703 if (last_hdr == NULL)
4704 {
4705 /* If we don't have a segment yet, then we don't need a new
4706 one (we build the last one after this loop). */
4707 new_segment = FALSE;
4708 }
4709 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4710 {
4711 /* If this section has a different relation between the
4712 virtual address and the load address, then we need a new
4713 segment. */
4714 new_segment = TRUE;
4715 }
b5599592
AM
4716 else if (hdr->lma < last_hdr->lma + last_size
4717 || last_hdr->lma + last_size < last_hdr->lma)
4718 {
4719 /* If this section has a load address that makes it overlap
4720 the previous section, then we need a new segment. */
4721 new_segment = TRUE;
4722 }
39948a60
NC
4723 /* In the next test we have to be careful when last_hdr->lma is close
4724 to the end of the address space. If the aligned address wraps
4725 around to the start of the address space, then there are no more
4726 pages left in memory and it is OK to assume that the current
4727 section can be included in the current segment. */
4728 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4729 > last_hdr->lma)
4730 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4ff73856 4731 <= hdr->lma))
8ded5a0f
AM
4732 {
4733 /* If putting this section in this segment would force us to
4734 skip a page in the segment, then we need a new segment. */
4735 new_segment = TRUE;
4736 }
4737 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
e5654c0f
AM
4738 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0
4739 && ((abfd->flags & D_PAGED) == 0
4740 || (((last_hdr->lma + last_size - 1) & -maxpagesize)
4741 != (hdr->lma & -maxpagesize))))
8ded5a0f 4742 {
e5654c0f
AM
4743 /* We don't want to put a loaded section after a
4744 nonloaded (ie. bss style) section in the same segment
4745 as that will force the non-loaded section to be loaded.
4746 Consider .tbss sections as loaded for this purpose.
4747 However, like the writable/non-writable case below,
4748 if they are on the same page then they must be put
4749 in the same segment. */
8ded5a0f
AM
4750 new_segment = TRUE;
4751 }
4752 else if ((abfd->flags & D_PAGED) == 0)
4753 {
4754 /* If the file is not demand paged, which means that we
4755 don't require the sections to be correctly aligned in the
4756 file, then there is no other reason for a new segment. */
4757 new_segment = FALSE;
4758 }
4759 else if (! writable
4760 && (hdr->flags & SEC_READONLY) == 0
8d06853e
AM
4761 && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4762 != (hdr->lma & -maxpagesize)))
8ded5a0f
AM
4763 {
4764 /* We don't want to put a writable section in a read only
4765 segment, unless they are on the same page in memory
4766 anyhow. We already know that the last section does not
4767 bring us past the current section on the page, so the
4768 only case in which the new section is not on the same
4769 page as the previous section is when the previous section
4770 ends precisely on a page boundary. */
4771 new_segment = TRUE;
4772 }
4773 else
4774 {
4775 /* Otherwise, we can use the same segment. */
4776 new_segment = FALSE;
4777 }
4778
2889e75b 4779 /* Allow interested parties a chance to override our decision. */
ceae84aa
AM
4780 if (last_hdr != NULL
4781 && info != NULL
4782 && info->callbacks->override_segment_assignment != NULL)
4783 new_segment
4784 = info->callbacks->override_segment_assignment (info, abfd, hdr,
4785 last_hdr,
4786 new_segment);
2889e75b 4787
8ded5a0f
AM
4788 if (! new_segment)
4789 {
4790 if ((hdr->flags & SEC_READONLY) == 0)
4791 writable = TRUE;
4792 last_hdr = hdr;
4793 /* .tbss sections effectively have zero size. */
4794 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4795 != SEC_THREAD_LOCAL)
4796 last_size = hdr->size;
4797 else
4798 last_size = 0;
4799 continue;
4800 }
4801
4802 /* We need a new program segment. We must create a new program
4803 header holding all the sections from phdr_index until hdr. */
4804
4805 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4806 if (m == NULL)
4807 goto error_return;
4808
4809 *pm = m;
4810 pm = &m->next;
4811
252b5132 4812 if ((hdr->flags & SEC_READONLY) == 0)
b34976b6 4813 writable = TRUE;
8ded5a0f
AM
4814 else
4815 writable = FALSE;
4816
baaff79e
JJ
4817 last_hdr = hdr;
4818 /* .tbss sections effectively have zero size. */
e5caec89 4819 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
eea6121a 4820 last_size = hdr->size;
baaff79e
JJ
4821 else
4822 last_size = 0;
8ded5a0f
AM
4823 phdr_index = i;
4824 phdr_in_segment = FALSE;
252b5132
RH
4825 }
4826
86b2281f
AM
4827 /* Create a final PT_LOAD program segment, but not if it's just
4828 for .tbss. */
4829 if (last_hdr != NULL
4830 && (i - phdr_index != 1
4831 || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4832 != SEC_THREAD_LOCAL)))
8ded5a0f
AM
4833 {
4834 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4835 if (m == NULL)
4836 goto error_return;
252b5132 4837
8ded5a0f
AM
4838 *pm = m;
4839 pm = &m->next;
4840 }
252b5132 4841
8ded5a0f
AM
4842 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4843 if (dynsec != NULL)
4844 {
4845 m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4846 if (m == NULL)
4847 goto error_return;
4848 *pm = m;
4849 pm = &m->next;
4850 }
252b5132 4851
1c5265b5
JJ
4852 /* For each batch of consecutive loadable .note sections,
4853 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4854 because if we link together nonloadable .note sections and
4855 loadable .note sections, we will generate two .note sections
4856 in the output file. FIXME: Using names for section types is
4857 bogus anyhow. */
8ded5a0f
AM
4858 for (s = abfd->sections; s != NULL; s = s->next)
4859 {
4860 if ((s->flags & SEC_LOAD) != 0
0112cd26 4861 && CONST_STRNEQ (s->name, ".note"))
8ded5a0f 4862 {
1c5265b5 4863 asection *s2;
91d6fa6a
NC
4864
4865 count = 1;
8ded5a0f 4866 amt = sizeof (struct elf_segment_map);
1c5265b5
JJ
4867 if (s->alignment_power == 2)
4868 for (s2 = s; s2->next != NULL; s2 = s2->next)
55b581a6
JJ
4869 {
4870 if (s2->next->alignment_power == 2
4871 && (s2->next->flags & SEC_LOAD) != 0
4872 && CONST_STRNEQ (s2->next->name, ".note")
8d06853e
AM
4873 && align_power (s2->lma + s2->size, 2)
4874 == s2->next->lma)
55b581a6
JJ
4875 count++;
4876 else
4877 break;
4878 }
1c5265b5 4879 amt += (count - 1) * sizeof (asection *);
a50b1753 4880 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4881 if (m == NULL)
4882 goto error_return;
4883 m->next = NULL;
4884 m->p_type = PT_NOTE;
1c5265b5
JJ
4885 m->count = count;
4886 while (count > 1)
4887 {
4888 m->sections[m->count - count--] = s;
4889 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4890 s = s->next;
4891 }
4892 m->sections[m->count - 1] = s;
4893 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
8ded5a0f
AM
4894 *pm = m;
4895 pm = &m->next;
4896 }
4897 if (s->flags & SEC_THREAD_LOCAL)
4898 {
4899 if (! tls_count)
4900 first_tls = s;
4901 tls_count++;
4902 }
a91e1603
L
4903 if (first_mbind == NULL
4904 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
4905 first_mbind = s;
8ded5a0f 4906 }
252b5132 4907
8ded5a0f
AM
4908 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4909 if (tls_count > 0)
4910 {
8ded5a0f
AM
4911 amt = sizeof (struct elf_segment_map);
4912 amt += (tls_count - 1) * sizeof (asection *);
a50b1753 4913 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
4914 if (m == NULL)
4915 goto error_return;
4916 m->next = NULL;
4917 m->p_type = PT_TLS;
4918 m->count = tls_count;
4919 /* Mandated PF_R. */
4920 m->p_flags = PF_R;
4921 m->p_flags_valid = 1;
d923cae0 4922 s = first_tls;
91d6fa6a 4923 for (i = 0; i < (unsigned int) tls_count; ++i)
8ded5a0f 4924 {
d923cae0
L
4925 if ((s->flags & SEC_THREAD_LOCAL) == 0)
4926 {
4927 _bfd_error_handler
4928 (_("%B: TLS sections are not adjacent:"), abfd);
4929 s = first_tls;
4930 i = 0;
4931 while (i < (unsigned int) tls_count)
4932 {
4933 if ((s->flags & SEC_THREAD_LOCAL) != 0)
4934 {
4935 _bfd_error_handler (_(" TLS: %A"), s);
4936 i++;
4937 }
4938 else
4939 _bfd_error_handler (_(" non-TLS: %A"), s);
4940 s = s->next;
4941 }
4942 bfd_set_error (bfd_error_bad_value);
4943 goto error_return;
4944 }
4945 m->sections[i] = s;
4946 s = s->next;
8ded5a0f 4947 }
252b5132 4948
8ded5a0f
AM
4949 *pm = m;
4950 pm = &m->next;
4951 }
252b5132 4952
a91e1603
L
4953 if (first_mbind && (abfd->flags & D_PAGED) != 0)
4954 for (s = first_mbind; s != NULL; s = s->next)
4955 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
4956 && (elf_section_data (s)->this_hdr.sh_info
4957 <= PT_GNU_MBIND_NUM))
4958 {
4959 /* Mandated PF_R. */
4960 unsigned long p_flags = PF_R;
4961 if ((s->flags & SEC_READONLY) == 0)
4962 p_flags |= PF_W;
4963 if ((s->flags & SEC_CODE) != 0)
4964 p_flags |= PF_X;
4965
4966 amt = sizeof (struct elf_segment_map) + sizeof (asection *);
4967 m = bfd_zalloc (abfd, amt);
4968 if (m == NULL)
4969 goto error_return;
4970 m->next = NULL;
4971 m->p_type = (PT_GNU_MBIND_LO
4972 + elf_section_data (s)->this_hdr.sh_info);
4973 m->count = 1;
4974 m->p_flags_valid = 1;
4975 m->sections[0] = s;
4976 m->p_flags = p_flags;
4977
4978 *pm = m;
4979 pm = &m->next;
4980 }
4981
8ded5a0f
AM
4982 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4983 segment. */
12bd6957 4984 eh_frame_hdr = elf_eh_frame_hdr (abfd);
8ded5a0f
AM
4985 if (eh_frame_hdr != NULL
4986 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
252b5132 4987 {
dc810e39 4988 amt = sizeof (struct elf_segment_map);
a50b1753 4989 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
252b5132
RH
4990 if (m == NULL)
4991 goto error_return;
4992 m->next = NULL;
8ded5a0f 4993 m->p_type = PT_GNU_EH_FRAME;
252b5132 4994 m->count = 1;
8ded5a0f 4995 m->sections[0] = eh_frame_hdr->output_section;
252b5132
RH
4996
4997 *pm = m;
4998 pm = &m->next;
4999 }
13ae64f3 5000
12bd6957 5001 if (elf_stack_flags (abfd))
13ae64f3 5002 {
8ded5a0f 5003 amt = sizeof (struct elf_segment_map);
a50b1753 5004 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
8ded5a0f
AM
5005 if (m == NULL)
5006 goto error_return;
5007 m->next = NULL;
2b05f1b7 5008 m->p_type = PT_GNU_STACK;
12bd6957 5009 m->p_flags = elf_stack_flags (abfd);
04c3a755 5010 m->p_align = bed->stack_align;
8ded5a0f 5011 m->p_flags_valid = 1;
04c3a755
NS
5012 m->p_align_valid = m->p_align != 0;
5013 if (info->stacksize > 0)
5014 {
5015 m->p_size = info->stacksize;
5016 m->p_size_valid = 1;
5017 }
252b5132 5018
8ded5a0f
AM
5019 *pm = m;
5020 pm = &m->next;
5021 }
65765700 5022
ceae84aa 5023 if (info != NULL && info->relro)
8ded5a0f 5024 {
f210dcff
L
5025 for (m = mfirst; m != NULL; m = m->next)
5026 {
3832a4d8
AM
5027 if (m->p_type == PT_LOAD
5028 && m->count != 0
5029 && m->sections[0]->vma >= info->relro_start
5030 && m->sections[0]->vma < info->relro_end)
f210dcff 5031 {
3832a4d8
AM
5032 i = m->count;
5033 while (--i != (unsigned) -1)
5034 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
5035 == (SEC_LOAD | SEC_HAS_CONTENTS))
5036 break;
5037
43a8475c 5038 if (i != (unsigned) -1)
f210dcff
L
5039 break;
5040 }
be01b344 5041 }
f210dcff
L
5042
5043 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5044 if (m != NULL)
5045 {
5046 amt = sizeof (struct elf_segment_map);
a50b1753 5047 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
f210dcff
L
5048 if (m == NULL)
5049 goto error_return;
5050 m->next = NULL;
5051 m->p_type = PT_GNU_RELRO;
f210dcff
L
5052 *pm = m;
5053 pm = &m->next;
5054 }
8ded5a0f 5055 }
9ee5e499 5056
8ded5a0f 5057 free (sections);
12bd6957 5058 elf_seg_map (abfd) = mfirst;
9ee5e499
JJ
5059 }
5060
3dea8fca 5061 if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
8ded5a0f 5062 return FALSE;
8c37241b 5063
12bd6957 5064 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
8ded5a0f 5065 ++count;
12bd6957 5066 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
252b5132 5067
b34976b6 5068 return TRUE;
252b5132
RH
5069
5070 error_return:
5071 if (sections != NULL)
5072 free (sections);
b34976b6 5073 return FALSE;
252b5132
RH
5074}
5075
5076/* Sort sections by address. */
5077
5078static int
217aa764 5079elf_sort_sections (const void *arg1, const void *arg2)
252b5132
RH
5080{
5081 const asection *sec1 = *(const asection **) arg1;
5082 const asection *sec2 = *(const asection **) arg2;
eecdbe52 5083 bfd_size_type size1, size2;
252b5132
RH
5084
5085 /* Sort by LMA first, since this is the address used to
5086 place the section into a segment. */
5087 if (sec1->lma < sec2->lma)
5088 return -1;
5089 else if (sec1->lma > sec2->lma)
5090 return 1;
5091
5092 /* Then sort by VMA. Normally the LMA and the VMA will be
5093 the same, and this will do nothing. */
5094 if (sec1->vma < sec2->vma)
5095 return -1;
5096 else if (sec1->vma > sec2->vma)
5097 return 1;
5098
5099 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5100
07c6e936 5101#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
252b5132
RH
5102
5103 if (TOEND (sec1))
5104 {
5105 if (TOEND (sec2))
00a7cdc5
NC
5106 {
5107 /* If the indicies are the same, do not return 0
5108 here, but continue to try the next comparison. */
5109 if (sec1->target_index - sec2->target_index != 0)
5110 return sec1->target_index - sec2->target_index;
5111 }
252b5132
RH
5112 else
5113 return 1;
5114 }
00a7cdc5 5115 else if (TOEND (sec2))
252b5132
RH
5116 return -1;
5117
5118#undef TOEND
5119
00a7cdc5
NC
5120 /* Sort by size, to put zero sized sections
5121 before others at the same address. */
252b5132 5122
eea6121a
AM
5123 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5124 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
eecdbe52
JJ
5125
5126 if (size1 < size2)
252b5132 5127 return -1;
eecdbe52 5128 if (size1 > size2)
252b5132
RH
5129 return 1;
5130
5131 return sec1->target_index - sec2->target_index;
5132}
5133
340b6d91
AC
5134/* Ian Lance Taylor writes:
5135
5136 We shouldn't be using % with a negative signed number. That's just
5137 not good. We have to make sure either that the number is not
5138 negative, or that the number has an unsigned type. When the types
5139 are all the same size they wind up as unsigned. When file_ptr is a
5140 larger signed type, the arithmetic winds up as signed long long,
5141 which is wrong.
5142
5143 What we're trying to say here is something like ``increase OFF by
5144 the least amount that will cause it to be equal to the VMA modulo
5145 the page size.'' */
5146/* In other words, something like:
5147
5148 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5149 off_offset = off % bed->maxpagesize;
5150 if (vma_offset < off_offset)
5151 adjustment = vma_offset + bed->maxpagesize - off_offset;
5152 else
5153 adjustment = vma_offset - off_offset;
08a40648 5154
de194d85 5155 which can be collapsed into the expression below. */
340b6d91
AC
5156
5157static file_ptr
5158vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5159{
dc9155b2
NC
5160 /* PR binutils/16199: Handle an alignment of zero. */
5161 if (maxpagesize == 0)
5162 maxpagesize = 1;
340b6d91
AC
5163 return ((vma - off) % maxpagesize);
5164}
5165
6d33f217
L
5166static void
5167print_segment_map (const struct elf_segment_map *m)
5168{
5169 unsigned int j;
5170 const char *pt = get_segment_type (m->p_type);
5171 char buf[32];
5172
5173 if (pt == NULL)
5174 {
5175 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5176 sprintf (buf, "LOPROC+%7.7x",
5177 (unsigned int) (m->p_type - PT_LOPROC));
5178 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5179 sprintf (buf, "LOOS+%7.7x",
5180 (unsigned int) (m->p_type - PT_LOOS));
5181 else
5182 snprintf (buf, sizeof (buf), "%8.8x",
5183 (unsigned int) m->p_type);
5184 pt = buf;
5185 }
4a97a0e5 5186 fflush (stdout);
6d33f217
L
5187 fprintf (stderr, "%s:", pt);
5188 for (j = 0; j < m->count; j++)
5189 fprintf (stderr, " %s", m->sections [j]->name);
5190 putc ('\n',stderr);
4a97a0e5 5191 fflush (stderr);
6d33f217
L
5192}
5193
32812159
AM
5194static bfd_boolean
5195write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
5196{
5197 void *buf;
5198 bfd_boolean ret;
5199
5200 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
5201 return FALSE;
5202 buf = bfd_zmalloc (len);
5203 if (buf == NULL)
5204 return FALSE;
5205 ret = bfd_bwrite (buf, len, abfd) == len;
5206 free (buf);
5207 return ret;
5208}
5209
252b5132
RH
5210/* Assign file positions to the sections based on the mapping from
5211 sections to segments. This function also sets up some fields in
f3520d2f 5212 the file header. */
252b5132 5213
b34976b6 5214static bfd_boolean
f3520d2f
AM
5215assign_file_positions_for_load_sections (bfd *abfd,
5216 struct bfd_link_info *link_info)
252b5132
RH
5217{
5218 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 5219 struct elf_segment_map *m;
252b5132 5220 Elf_Internal_Phdr *phdrs;
252b5132 5221 Elf_Internal_Phdr *p;
02bf8d82 5222 file_ptr off;
3f570048 5223 bfd_size_type maxpagesize;
a8c75b76 5224 unsigned int pt_load_count = 0;
f3520d2f 5225 unsigned int alloc;
0920dee7 5226 unsigned int i, j;
2b0bc088 5227 bfd_vma header_pad = 0;
252b5132 5228
e36284ab 5229 if (link_info == NULL
ceae84aa 5230 && !_bfd_elf_map_sections_to_segments (abfd, link_info))
8ded5a0f 5231 return FALSE;
252b5132 5232
8ded5a0f 5233 alloc = 0;
12bd6957 5234 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
2b0bc088
NC
5235 {
5236 ++alloc;
5237 if (m->header_size)
5238 header_pad = m->header_size;
5239 }
252b5132 5240
82f2dbf7
NC
5241 if (alloc)
5242 {
5243 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5244 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5245 }
5246 else
5247 {
5248 /* PR binutils/12467. */
5249 elf_elfheader (abfd)->e_phoff = 0;
5250 elf_elfheader (abfd)->e_phentsize = 0;
5251 }
d324f6d6 5252
8ded5a0f 5253 elf_elfheader (abfd)->e_phnum = alloc;
252b5132 5254
12bd6957
AM
5255 if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5256 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
8ded5a0f 5257 else
12bd6957 5258 BFD_ASSERT (elf_program_header_size (abfd)
59e0647f 5259 >= alloc * bed->s->sizeof_phdr);
252b5132
RH
5260
5261 if (alloc == 0)
f3520d2f 5262 {
12bd6957 5263 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
8ded5a0f 5264 return TRUE;
f3520d2f 5265 }
252b5132 5266
12bd6957 5267 /* We're writing the size in elf_program_header_size (abfd),
57268894
HPN
5268 see assign_file_positions_except_relocs, so make sure we have
5269 that amount allocated, with trailing space cleared.
12bd6957
AM
5270 The variable alloc contains the computed need, while
5271 elf_program_header_size (abfd) contains the size used for the
57268894
HPN
5272 layout.
5273 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5274 where the layout is forced to according to a larger size in the
5275 last iterations for the testcase ld-elf/header. */
12bd6957 5276 BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
57268894 5277 == 0);
a50b1753
NC
5278 phdrs = (Elf_Internal_Phdr *)
5279 bfd_zalloc2 (abfd,
07d6d2b8
AM
5280 (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
5281 sizeof (Elf_Internal_Phdr));
f3520d2f 5282 elf_tdata (abfd)->phdr = phdrs;
252b5132 5283 if (phdrs == NULL)
b34976b6 5284 return FALSE;
252b5132 5285
3f570048
AM
5286 maxpagesize = 1;
5287 if ((abfd->flags & D_PAGED) != 0)
5288 maxpagesize = bed->maxpagesize;
5289
252b5132
RH
5290 off = bed->s->sizeof_ehdr;
5291 off += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5292 if (header_pad < (bfd_vma) off)
5293 header_pad = 0;
5294 else
5295 header_pad -= off;
5296 off += header_pad;
252b5132 5297
12bd6957 5298 for (m = elf_seg_map (abfd), p = phdrs, j = 0;
252b5132 5299 m != NULL;
0920dee7 5300 m = m->next, p++, j++)
252b5132 5301 {
252b5132 5302 asection **secpp;
bf988460
AM
5303 bfd_vma off_adjust;
5304 bfd_boolean no_contents;
252b5132
RH
5305
5306 /* If elf_segment_map is not from map_sections_to_segments, the
08a40648 5307 sections may not be correctly ordered. NOTE: sorting should
52e9b619
MS
5308 not be done to the PT_NOTE section of a corefile, which may
5309 contain several pseudo-sections artificially created by bfd.
5310 Sorting these pseudo-sections breaks things badly. */
47d9a591
AM
5311 if (m->count > 1
5312 && !(elf_elfheader (abfd)->e_type == ET_CORE
52e9b619 5313 && m->p_type == PT_NOTE))
252b5132
RH
5314 qsort (m->sections, (size_t) m->count, sizeof (asection *),
5315 elf_sort_sections);
5316
b301b248
AM
5317 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5318 number of sections with contents contributing to both p_filesz
5319 and p_memsz, followed by a number of sections with no contents
5320 that just contribute to p_memsz. In this loop, OFF tracks next
02bf8d82 5321 available file offset for PT_LOAD and PT_NOTE segments. */
252b5132 5322 p->p_type = m->p_type;
28a7f3e7 5323 p->p_flags = m->p_flags;
252b5132 5324
3f570048
AM
5325 if (m->count == 0)
5326 p->p_vaddr = 0;
5327 else
3271a814 5328 p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
3f570048
AM
5329
5330 if (m->p_paddr_valid)
5331 p->p_paddr = m->p_paddr;
5332 else if (m->count == 0)
5333 p->p_paddr = 0;
5334 else
08a40648 5335 p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
3f570048
AM
5336
5337 if (p->p_type == PT_LOAD
5338 && (abfd->flags & D_PAGED) != 0)
5339 {
5340 /* p_align in demand paged PT_LOAD segments effectively stores
5341 the maximum page size. When copying an executable with
5342 objcopy, we set m->p_align from the input file. Use this
5343 value for maxpagesize rather than bed->maxpagesize, which
5344 may be different. Note that we use maxpagesize for PT_TLS
5345 segment alignment later in this function, so we are relying
5346 on at least one PT_LOAD segment appearing before a PT_TLS
5347 segment. */
5348 if (m->p_align_valid)
5349 maxpagesize = m->p_align;
5350
5351 p->p_align = maxpagesize;
a8c75b76 5352 pt_load_count += 1;
3f570048 5353 }
3271a814
NS
5354 else if (m->p_align_valid)
5355 p->p_align = m->p_align;
e970b90a
DJ
5356 else if (m->count == 0)
5357 p->p_align = 1 << bed->s->log_file_align;
3f570048
AM
5358 else
5359 p->p_align = 0;
5360
bf988460
AM
5361 no_contents = FALSE;
5362 off_adjust = 0;
252b5132 5363 if (p->p_type == PT_LOAD
b301b248 5364 && m->count > 0)
252b5132 5365 {
b301b248 5366 bfd_size_type align;
a49e53ed 5367 unsigned int align_power = 0;
b301b248 5368
3271a814
NS
5369 if (m->p_align_valid)
5370 align = p->p_align;
5371 else
252b5132 5372 {
3271a814
NS
5373 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5374 {
5375 unsigned int secalign;
08a40648 5376
3271a814
NS
5377 secalign = bfd_get_section_alignment (abfd, *secpp);
5378 if (secalign > align_power)
5379 align_power = secalign;
5380 }
5381 align = (bfd_size_type) 1 << align_power;
5382 if (align < maxpagesize)
5383 align = maxpagesize;
b301b248 5384 }
252b5132 5385
02bf8d82
AM
5386 for (i = 0; i < m->count; i++)
5387 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
5388 /* If we aren't making room for this section, then
5389 it must be SHT_NOBITS regardless of what we've
5390 set via struct bfd_elf_special_section. */
5391 elf_section_type (m->sections[i]) = SHT_NOBITS;
5392
bf988460 5393 /* Find out whether this segment contains any loadable
aea274d3
AM
5394 sections. */
5395 no_contents = TRUE;
5396 for (i = 0; i < m->count; i++)
5397 if (elf_section_type (m->sections[i]) != SHT_NOBITS)
5398 {
5399 no_contents = FALSE;
5400 break;
5401 }
bf988460 5402
85cfcbfb 5403 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
a8c75b76
AM
5404
5405 /* Broken hardware and/or kernel require that files do not
5406 map the same page with different permissions on some hppa
5407 processors. */
5408 if (pt_load_count > 1
5409 && bed->no_page_alias
5410 && (off & (maxpagesize - 1)) != 0
5411 && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
5412 off_adjust += maxpagesize;
bf988460
AM
5413 off += off_adjust;
5414 if (no_contents)
5415 {
5416 /* We shouldn't need to align the segment on disk since
5417 the segment doesn't need file space, but the gABI
5418 arguably requires the alignment and glibc ld.so
5419 checks it. So to comply with the alignment
5420 requirement but not waste file space, we adjust
5421 p_offset for just this segment. (OFF_ADJUST is
5422 subtracted from OFF later.) This may put p_offset
5423 past the end of file, but that shouldn't matter. */
5424 }
5425 else
5426 off_adjust = 0;
252b5132 5427 }
b1a6d0b1
NC
5428 /* Make sure the .dynamic section is the first section in the
5429 PT_DYNAMIC segment. */
5430 else if (p->p_type == PT_DYNAMIC
5431 && m->count > 1
5432 && strcmp (m->sections[0]->name, ".dynamic") != 0)
5433 {
5434 _bfd_error_handler
63a5468a
AM
5435 (_("%B: The first section in the PT_DYNAMIC segment"
5436 " is not the .dynamic section"),
b301b248 5437 abfd);
b1a6d0b1
NC
5438 bfd_set_error (bfd_error_bad_value);
5439 return FALSE;
5440 }
3f001e84
JK
5441 /* Set the note section type to SHT_NOTE. */
5442 else if (p->p_type == PT_NOTE)
5443 for (i = 0; i < m->count; i++)
5444 elf_section_type (m->sections[i]) = SHT_NOTE;
252b5132 5445
252b5132
RH
5446 p->p_offset = 0;
5447 p->p_filesz = 0;
5448 p->p_memsz = 0;
5449
5450 if (m->includes_filehdr)
5451 {
bf988460 5452 if (!m->p_flags_valid)
252b5132 5453 p->p_flags |= PF_R;
252b5132
RH
5454 p->p_filesz = bed->s->sizeof_ehdr;
5455 p->p_memsz = bed->s->sizeof_ehdr;
5456 if (m->count > 0)
5457 {
9ab82472
L
5458 if (p->p_vaddr < (bfd_vma) off
5459 || (!m->p_paddr_valid
5460 && p->p_paddr < (bfd_vma) off))
252b5132 5461 {
4eca0228 5462 _bfd_error_handler
63a5468a
AM
5463 (_("%B: Not enough room for program headers,"
5464 " try linking with -N"),
b301b248 5465 abfd);
252b5132 5466 bfd_set_error (bfd_error_bad_value);
b34976b6 5467 return FALSE;
252b5132
RH
5468 }
5469
5470 p->p_vaddr -= off;
bf988460 5471 if (!m->p_paddr_valid)
252b5132
RH
5472 p->p_paddr -= off;
5473 }
252b5132
RH
5474 }
5475
5476 if (m->includes_phdrs)
5477 {
bf988460 5478 if (!m->p_flags_valid)
252b5132
RH
5479 p->p_flags |= PF_R;
5480
f3520d2f 5481 if (!m->includes_filehdr)
252b5132
RH
5482 {
5483 p->p_offset = bed->s->sizeof_ehdr;
5484
5485 if (m->count > 0)
5486 {
252b5132 5487 p->p_vaddr -= off - p->p_offset;
bf988460 5488 if (!m->p_paddr_valid)
252b5132
RH
5489 p->p_paddr -= off - p->p_offset;
5490 }
252b5132
RH
5491 }
5492
5493 p->p_filesz += alloc * bed->s->sizeof_phdr;
5494 p->p_memsz += alloc * bed->s->sizeof_phdr;
2b0bc088
NC
5495 if (m->count)
5496 {
5497 p->p_filesz += header_pad;
5498 p->p_memsz += header_pad;
5499 }
252b5132
RH
5500 }
5501
5502 if (p->p_type == PT_LOAD
5503 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5504 {
bf988460 5505 if (!m->includes_filehdr && !m->includes_phdrs)
02bf8d82 5506 p->p_offset = off;
252b5132
RH
5507 else
5508 {
5509 file_ptr adjust;
5510
5511 adjust = off - (p->p_offset + p->p_filesz);
bf988460
AM
5512 if (!no_contents)
5513 p->p_filesz += adjust;
252b5132
RH
5514 p->p_memsz += adjust;
5515 }
5516 }
5517
1ea63fd2
AM
5518 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5519 maps. Set filepos for sections in PT_LOAD segments, and in
5520 core files, for sections in PT_NOTE segments.
5521 assign_file_positions_for_non_load_sections will set filepos
5522 for other sections and update p_filesz for other segments. */
252b5132
RH
5523 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5524 {
5525 asection *sec;
252b5132 5526 bfd_size_type align;
627b32bc 5527 Elf_Internal_Shdr *this_hdr;
252b5132
RH
5528
5529 sec = *secpp;
02bf8d82 5530 this_hdr = &elf_section_data (sec)->this_hdr;
3f570048 5531 align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
252b5132 5532
88967714
AM
5533 if ((p->p_type == PT_LOAD
5534 || p->p_type == PT_TLS)
5535 && (this_hdr->sh_type != SHT_NOBITS
5536 || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5537 && ((this_hdr->sh_flags & SHF_TLS) == 0
5538 || p->p_type == PT_TLS))))
252b5132 5539 {
b5599592
AM
5540 bfd_vma p_start = p->p_paddr;
5541 bfd_vma p_end = p_start + p->p_memsz;
5542 bfd_vma s_start = sec->lma;
5543 bfd_vma adjust = s_start - p_end;
252b5132 5544
a2d1e028
L
5545 if (adjust != 0
5546 && (s_start < p_end
5547 || p_end < p_start))
252b5132 5548 {
4eca0228 5549 _bfd_error_handler
695344c0 5550 /* xgettext:c-format */
76cfced5
AM
5551 (_("%B: section %A lma %#Lx adjusted to %#Lx"),
5552 abfd, sec, s_start, p_end);
88967714 5553 adjust = 0;
b5599592 5554 sec->lma = p_end;
1cfb7d1e 5555 }
3ac9b6c9 5556 p->p_memsz += adjust;
1cfb7d1e 5557
88967714
AM
5558 if (this_hdr->sh_type != SHT_NOBITS)
5559 {
32812159
AM
5560 if (p->p_filesz + adjust < p->p_memsz)
5561 {
5562 /* We have a PROGBITS section following NOBITS ones.
07d6d2b8 5563 Allocate file space for the NOBITS section(s) and
32812159
AM
5564 zero it. */
5565 adjust = p->p_memsz - p->p_filesz;
5566 if (!write_zeros (abfd, off, adjust))
5567 return FALSE;
5568 }
88967714
AM
5569 off += adjust;
5570 p->p_filesz += adjust;
252b5132 5571 }
252b5132
RH
5572 }
5573
5574 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5575 {
b301b248
AM
5576 /* The section at i == 0 is the one that actually contains
5577 everything. */
4a938328
MS
5578 if (i == 0)
5579 {
627b32bc 5580 this_hdr->sh_offset = sec->filepos = off;
6a3cd2b4
AM
5581 off += this_hdr->sh_size;
5582 p->p_filesz = this_hdr->sh_size;
b301b248
AM
5583 p->p_memsz = 0;
5584 p->p_align = 1;
252b5132 5585 }
4a938328 5586 else
252b5132 5587 {
b301b248 5588 /* The rest are fake sections that shouldn't be written. */
252b5132 5589 sec->filepos = 0;
eea6121a 5590 sec->size = 0;
b301b248
AM
5591 sec->flags = 0;
5592 continue;
252b5132 5593 }
252b5132
RH
5594 }
5595 else
5596 {
1e951488 5597 if (p->p_type == PT_LOAD)
b301b248 5598 {
1e951488
AM
5599 this_hdr->sh_offset = sec->filepos = off;
5600 if (this_hdr->sh_type != SHT_NOBITS)
5601 off += this_hdr->sh_size;
5602 }
5603 else if (this_hdr->sh_type == SHT_NOBITS
5604 && (this_hdr->sh_flags & SHF_TLS) != 0
5605 && this_hdr->sh_offset == 0)
5606 {
5607 /* This is a .tbss section that didn't get a PT_LOAD.
5608 (See _bfd_elf_map_sections_to_segments "Create a
5609 final PT_LOAD".) Set sh_offset to the value it
5610 would have if we had created a zero p_filesz and
5611 p_memsz PT_LOAD header for the section. This
5612 also makes the PT_TLS header have the same
5613 p_offset value. */
5614 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5615 off, align);
5616 this_hdr->sh_offset = sec->filepos = off + adjust;
b301b248 5617 }
252b5132 5618
02bf8d82 5619 if (this_hdr->sh_type != SHT_NOBITS)
b301b248 5620 {
6a3cd2b4 5621 p->p_filesz += this_hdr->sh_size;
02bf8d82
AM
5622 /* A load section without SHF_ALLOC is something like
5623 a note section in a PT_NOTE segment. These take
5624 file space but are not loaded into memory. */
5625 if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6a3cd2b4 5626 p->p_memsz += this_hdr->sh_size;
b301b248 5627 }
6a3cd2b4 5628 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
13ae64f3 5629 {
6a3cd2b4
AM
5630 if (p->p_type == PT_TLS)
5631 p->p_memsz += this_hdr->sh_size;
5632
5633 /* .tbss is special. It doesn't contribute to p_memsz of
5634 normal segments. */
5635 else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5636 p->p_memsz += this_hdr->sh_size;
13ae64f3
JJ
5637 }
5638
b10a8ae0
L
5639 if (align > p->p_align
5640 && !m->p_align_valid
5641 && (p->p_type != PT_LOAD
5642 || (abfd->flags & D_PAGED) == 0))
252b5132
RH
5643 p->p_align = align;
5644 }
5645
bf988460 5646 if (!m->p_flags_valid)
252b5132
RH
5647 {
5648 p->p_flags |= PF_R;
02bf8d82 5649 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
252b5132 5650 p->p_flags |= PF_X;
02bf8d82 5651 if ((this_hdr->sh_flags & SHF_WRITE) != 0)
252b5132
RH
5652 p->p_flags |= PF_W;
5653 }
5654 }
43a8475c 5655
bf988460 5656 off -= off_adjust;
0920dee7 5657
7c928300
AM
5658 /* Check that all sections are in a PT_LOAD segment.
5659 Don't check funky gdb generated core files. */
5660 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
9a83a553
AM
5661 {
5662 bfd_boolean check_vma = TRUE;
5663
5664 for (i = 1; i < m->count; i++)
5665 if (m->sections[i]->vma == m->sections[i - 1]->vma
5666 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5667 ->this_hdr), p) != 0
5668 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5669 ->this_hdr), p) != 0)
0920dee7 5670 {
9a83a553
AM
5671 /* Looks like we have overlays packed into the segment. */
5672 check_vma = FALSE;
5673 break;
0920dee7 5674 }
9a83a553
AM
5675
5676 for (i = 0; i < m->count; i++)
5677 {
5678 Elf_Internal_Shdr *this_hdr;
5679 asection *sec;
5680
5681 sec = m->sections[i];
5682 this_hdr = &(elf_section_data(sec)->this_hdr);
86b2281f
AM
5683 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5684 && !ELF_TBSS_SPECIAL (this_hdr, p))
9a83a553 5685 {
4eca0228 5686 _bfd_error_handler
695344c0 5687 /* xgettext:c-format */
9a83a553
AM
5688 (_("%B: section `%A' can't be allocated in segment %d"),
5689 abfd, sec, j);
5690 print_segment_map (m);
5691 }
5692 }
5693 }
252b5132
RH
5694 }
5695
12bd6957 5696 elf_next_file_pos (abfd) = off;
f3520d2f
AM
5697 return TRUE;
5698}
5699
5700/* Assign file positions for the other sections. */
5701
5702static bfd_boolean
5703assign_file_positions_for_non_load_sections (bfd *abfd,
5704 struct bfd_link_info *link_info)
5705{
5706 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5707 Elf_Internal_Shdr **i_shdrpp;
e06efbf1 5708 Elf_Internal_Shdr **hdrpp, **end_hdrpp;
f3520d2f
AM
5709 Elf_Internal_Phdr *phdrs;
5710 Elf_Internal_Phdr *p;
5711 struct elf_segment_map *m;
62655c7b 5712 struct elf_segment_map *hdrs_segment;
f3520d2f
AM
5713 bfd_vma filehdr_vaddr, filehdr_paddr;
5714 bfd_vma phdrs_vaddr, phdrs_paddr;
5715 file_ptr off;
f3520d2f
AM
5716 unsigned int count;
5717
5c182d5f 5718 i_shdrpp = elf_elfsections (abfd);
e06efbf1 5719 end_hdrpp = i_shdrpp + elf_numsections (abfd);
12bd6957 5720 off = elf_next_file_pos (abfd);
e06efbf1 5721 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
5c182d5f 5722 {
5c182d5f
AM
5723 Elf_Internal_Shdr *hdr;
5724
5725 hdr = *hdrpp;
5726 if (hdr->bfd_section != NULL
252e386e
AM
5727 && (hdr->bfd_section->filepos != 0
5728 || (hdr->sh_type == SHT_NOBITS
5729 && hdr->contents == NULL)))
627b32bc 5730 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5c182d5f
AM
5731 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5732 {
e8d2ba53 5733 if (hdr->sh_size != 0)
4eca0228 5734 _bfd_error_handler
695344c0 5735 /* xgettext:c-format */
e8d2ba53
AM
5736 (_("%B: warning: allocated section `%s' not in segment"),
5737 abfd,
5738 (hdr->bfd_section == NULL
5739 ? "*unknown*"
5740 : hdr->bfd_section->name));
3ba71138
L
5741 /* We don't need to page align empty sections. */
5742 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5c182d5f
AM
5743 off += vma_page_aligned_bias (hdr->sh_addr, off,
5744 bed->maxpagesize);
5745 else
5746 off += vma_page_aligned_bias (hdr->sh_addr, off,
5747 hdr->sh_addralign);
5748 off = _bfd_elf_assign_file_position_for_section (hdr, off,
5749 FALSE);
5750 }
5751 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5752 && hdr->bfd_section == NULL)
0ce398f1
L
5753 || (hdr->bfd_section != NULL
5754 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
5755 /* Compress DWARF debug sections. */
12bd6957 5756 || hdr == i_shdrpp[elf_onesymtab (abfd)]
6a40cf0c
NC
5757 || (elf_symtab_shndx_list (abfd) != NULL
5758 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
5759 || hdr == i_shdrpp[elf_strtab_sec (abfd)]
5760 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
5c182d5f
AM
5761 hdr->sh_offset = -1;
5762 else
5763 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5c182d5f
AM
5764 }
5765
252b5132
RH
5766 /* Now that we have set the section file positions, we can set up
5767 the file positions for the non PT_LOAD segments. */
f3520d2f
AM
5768 count = 0;
5769 filehdr_vaddr = 0;
5770 filehdr_paddr = 0;
5771 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5772 phdrs_paddr = 0;
62655c7b 5773 hdrs_segment = NULL;
f3520d2f 5774 phdrs = elf_tdata (abfd)->phdr;
12bd6957 5775 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
f3520d2f
AM
5776 {
5777 ++count;
5778 if (p->p_type != PT_LOAD)
5779 continue;
5780
5781 if (m->includes_filehdr)
5782 {
5783 filehdr_vaddr = p->p_vaddr;
5784 filehdr_paddr = p->p_paddr;
5785 }
5786 if (m->includes_phdrs)
5787 {
5788 phdrs_vaddr = p->p_vaddr;
5789 phdrs_paddr = p->p_paddr;
5790 if (m->includes_filehdr)
5791 {
62655c7b 5792 hdrs_segment = m;
f3520d2f
AM
5793 phdrs_vaddr += bed->s->sizeof_ehdr;
5794 phdrs_paddr += bed->s->sizeof_ehdr;
5795 }
5796 }
5797 }
5798
62655c7b
RM
5799 if (hdrs_segment != NULL && link_info != NULL)
5800 {
5801 /* There is a segment that contains both the file headers and the
5802 program headers, so provide a symbol __ehdr_start pointing there.
5803 A program can use this to examine itself robustly. */
5804
5805 struct elf_link_hash_entry *hash
5806 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5807 FALSE, FALSE, TRUE);
5808 /* If the symbol was referenced and not defined, define it. */
5809 if (hash != NULL
5810 && (hash->root.type == bfd_link_hash_new
5811 || hash->root.type == bfd_link_hash_undefined
5812 || hash->root.type == bfd_link_hash_undefweak
5813 || hash->root.type == bfd_link_hash_common))
5814 {
5815 asection *s = NULL;
5816 if (hdrs_segment->count != 0)
5817 /* The segment contains sections, so use the first one. */
5818 s = hdrs_segment->sections[0];
5819 else
5820 /* Use the first (i.e. lowest-addressed) section in any segment. */
12bd6957 5821 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62655c7b
RM
5822 if (m->count != 0)
5823 {
5824 s = m->sections[0];
5825 break;
5826 }
5827
5828 if (s != NULL)
5829 {
5830 hash->root.u.def.value = filehdr_vaddr - s->vma;
5831 hash->root.u.def.section = s;
5832 }
5833 else
5834 {
5835 hash->root.u.def.value = filehdr_vaddr;
5836 hash->root.u.def.section = bfd_abs_section_ptr;
5837 }
5838
5839 hash->root.type = bfd_link_hash_defined;
5840 hash->def_regular = 1;
5841 hash->non_elf = 0;
5842 }
5843 }
5844
12bd6957 5845 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
252b5132 5846 {
129af99f 5847 if (p->p_type == PT_GNU_RELRO)
252b5132 5848 {
b84a33b5 5849 const Elf_Internal_Phdr *lp;
3146fac4 5850 struct elf_segment_map *lm;
1ea63fd2 5851
129af99f 5852 if (link_info != NULL)
8c37241b 5853 {
129af99f
AS
5854 /* During linking the range of the RELRO segment is passed
5855 in link_info. */
12bd6957 5856 for (lm = elf_seg_map (abfd), lp = phdrs;
3146fac4
AM
5857 lm != NULL;
5858 lm = lm->next, lp++)
8c37241b
JJ
5859 {
5860 if (lp->p_type == PT_LOAD
b84a33b5 5861 && lp->p_vaddr < link_info->relro_end
3146fac4
AM
5862 && lm->count != 0
5863 && lm->sections[0]->vma >= link_info->relro_start)
8c37241b
JJ
5864 break;
5865 }
8981c88a 5866
3146fac4 5867 BFD_ASSERT (lm != NULL);
8c37241b 5868 }
129af99f
AS
5869 else
5870 {
5871 /* Otherwise we are copying an executable or shared
b84a33b5 5872 library, but we need to use the same linker logic. */
129af99f
AS
5873 for (lp = phdrs; lp < phdrs + count; ++lp)
5874 {
5875 if (lp->p_type == PT_LOAD
5876 && lp->p_paddr == p->p_paddr)
5877 break;
5878 }
b84a33b5
AM
5879 }
5880
5881 if (lp < phdrs + count)
5882 {
5883 p->p_vaddr = lp->p_vaddr;
5884 p->p_paddr = lp->p_paddr;
5885 p->p_offset = lp->p_offset;
5886 if (link_info != NULL)
5887 p->p_filesz = link_info->relro_end - lp->p_vaddr;
5888 else if (m->p_size_valid)
5889 p->p_filesz = m->p_size;
129af99f
AS
5890 else
5891 abort ();
b84a33b5 5892 p->p_memsz = p->p_filesz;
f3944f72 5893 /* Preserve the alignment and flags if they are valid. The
07d6d2b8 5894 gold linker generates RW/4 for the PT_GNU_RELRO section.
f3944f72
L
5895 It is better for objcopy/strip to honor these attributes
5896 otherwise gdb will choke when using separate debug files.
5897 */
5898 if (!m->p_align_valid)
5899 p->p_align = 1;
5900 if (!m->p_flags_valid)
341b8ee7 5901 p->p_flags = PF_R;
129af99f 5902 }
9433b9b1 5903 else
b84a33b5
AM
5904 {
5905 memset (p, 0, sizeof *p);
5906 p->p_type = PT_NULL;
5907 }
129af99f 5908 }
04c3a755
NS
5909 else if (p->p_type == PT_GNU_STACK)
5910 {
5911 if (m->p_size_valid)
5912 p->p_memsz = m->p_size;
5913 }
129af99f
AS
5914 else if (m->count != 0)
5915 {
e06efbf1 5916 unsigned int i;
1a9ccd70 5917
129af99f
AS
5918 if (p->p_type != PT_LOAD
5919 && (p->p_type != PT_NOTE
5920 || bfd_get_format (abfd) != bfd_core))
5921 {
1a9ccd70
NC
5922 /* A user specified segment layout may include a PHDR
5923 segment that overlaps with a LOAD segment... */
5924 if (p->p_type == PT_PHDR)
5925 {
5926 m->count = 0;
5927 continue;
5928 }
5929
c86934ce
NC
5930 if (m->includes_filehdr || m->includes_phdrs)
5931 {
b1fa9dd6 5932 /* PR 17512: file: 2195325e. */
4eca0228 5933 _bfd_error_handler
76cfced5
AM
5934 (_("%B: error: non-load segment %d includes file header "
5935 "and/or program header"),
5936 abfd, (int) (p - phdrs));
c86934ce
NC
5937 return FALSE;
5938 }
129af99f 5939
86b2281f 5940 p->p_filesz = 0;
129af99f 5941 p->p_offset = m->sections[0]->filepos;
86b2281f
AM
5942 for (i = m->count; i-- != 0;)
5943 {
5944 asection *sect = m->sections[i];
5945 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5946 if (hdr->sh_type != SHT_NOBITS)
5947 {
5948 p->p_filesz = (sect->filepos - m->sections[0]->filepos
5949 + hdr->sh_size);
5950 break;
5951 }
5952 }
129af99f
AS
5953 }
5954 }
5955 else if (m->includes_filehdr)
5956 {
5957 p->p_vaddr = filehdr_vaddr;
5958 if (! m->p_paddr_valid)
5959 p->p_paddr = filehdr_paddr;
5960 }
5961 else if (m->includes_phdrs)
5962 {
5963 p->p_vaddr = phdrs_vaddr;
5964 if (! m->p_paddr_valid)
5965 p->p_paddr = phdrs_paddr;
252b5132
RH
5966 }
5967 }
5968
12bd6957 5969 elf_next_file_pos (abfd) = off;
252b5132 5970
b34976b6 5971 return TRUE;
252b5132
RH
5972}
5973
6a40cf0c
NC
5974static elf_section_list *
5975find_section_in_list (unsigned int i, elf_section_list * list)
5976{
5977 for (;list != NULL; list = list->next)
5978 if (list->ndx == i)
5979 break;
5980 return list;
5981}
5982
252b5132
RH
5983/* Work out the file positions of all the sections. This is called by
5984 _bfd_elf_compute_section_file_positions. All the section sizes and
5985 VMAs must be known before this is called.
5986
e0638f70
AM
5987 Reloc sections come in two flavours: Those processed specially as
5988 "side-channel" data attached to a section to which they apply, and
5989 those that bfd doesn't process as relocations. The latter sort are
5990 stored in a normal bfd section by bfd_section_from_shdr. We don't
5991 consider the former sort here, unless they form part of the loadable
5992 image. Reloc sections not assigned here will be handled later by
5993 assign_file_positions_for_relocs.
252b5132
RH
5994
5995 We also don't set the positions of the .symtab and .strtab here. */
5996
b34976b6 5997static bfd_boolean
c84fca4d
AO
5998assign_file_positions_except_relocs (bfd *abfd,
5999 struct bfd_link_info *link_info)
252b5132 6000{
5c182d5f
AM
6001 struct elf_obj_tdata *tdata = elf_tdata (abfd);
6002 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
9c5bfbb7 6003 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6004
6005 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6006 && bfd_get_format (abfd) != bfd_core)
6007 {
5c182d5f
AM
6008 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6009 unsigned int num_sec = elf_numsections (abfd);
252b5132
RH
6010 Elf_Internal_Shdr **hdrpp;
6011 unsigned int i;
a485e98e 6012 file_ptr off;
252b5132
RH
6013
6014 /* Start after the ELF header. */
6015 off = i_ehdrp->e_ehsize;
6016
6017 /* We are not creating an executable, which means that we are
6018 not creating a program header, and that the actual order of
6019 the sections in the file is unimportant. */
9ad5cbcf 6020 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
252b5132
RH
6021 {
6022 Elf_Internal_Shdr *hdr;
6023
6024 hdr = *hdrpp;
e0638f70
AM
6025 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6026 && hdr->bfd_section == NULL)
0ce398f1
L
6027 || (hdr->bfd_section != NULL
6028 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
6029 /* Compress DWARF debug sections. */
12bd6957 6030 || i == elf_onesymtab (abfd)
6a40cf0c
NC
6031 || (elf_symtab_shndx_list (abfd) != NULL
6032 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
3e19fb8f
L
6033 || i == elf_strtab_sec (abfd)
6034 || i == elf_shstrtab_sec (abfd))
252b5132
RH
6035 {
6036 hdr->sh_offset = -1;
252b5132 6037 }
9ad5cbcf 6038 else
b34976b6 6039 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
252b5132 6040 }
a485e98e
AM
6041
6042 elf_next_file_pos (abfd) = off;
252b5132
RH
6043 }
6044 else
6045 {
f3520d2f
AM
6046 unsigned int alloc;
6047
252b5132 6048 /* Assign file positions for the loaded sections based on the
08a40648 6049 assignment of sections to segments. */
f3520d2f
AM
6050 if (!assign_file_positions_for_load_sections (abfd, link_info))
6051 return FALSE;
6052
6053 /* And for non-load sections. */
6054 if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6055 return FALSE;
6056
e36284ab
AM
6057 if (bed->elf_backend_modify_program_headers != NULL)
6058 {
6059 if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
6060 return FALSE;
6061 }
6062
58e7ebac 6063 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
0e1862bb 6064 if (link_info != NULL && bfd_link_pie (link_info))
58e7ebac
L
6065 {
6066 unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
6067 Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
6068 Elf_Internal_Phdr *end_segment = &segment[num_segments];
6069
6070 /* Find the lowest p_vaddr in PT_LOAD segments. */
6071 bfd_vma p_vaddr = (bfd_vma) -1;
6072 for (; segment < end_segment; segment++)
6073 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
6074 p_vaddr = segment->p_vaddr;
6075
6076 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6077 segments is non-zero. */
6078 if (p_vaddr)
6079 i_ehdrp->e_type = ET_EXEC;
6080 }
6081
f3520d2f 6082 /* Write out the program headers. */
12bd6957 6083 alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
1a9ccd70
NC
6084
6085 /* Sort the program headers into the ordering required by the ELF standard. */
6086 if (alloc == 0)
6087 return TRUE;
6088
1a9ccd70
NC
6089 /* PR ld/20815 - Check that the program header segment, if present, will
6090 be loaded into memory. FIXME: The check below is not sufficient as
6091 really all PT_LOAD segments should be checked before issuing an error
6092 message. Plus the PHDR segment does not have to be the first segment
6093 in the program header table. But this version of the check should
cd584857
NC
6094 catch all real world use cases.
6095
6096 FIXME: We used to have code here to sort the PT_LOAD segments into
6097 ascending order, as per the ELF spec. But this breaks some programs,
6098 including the Linux kernel. But really either the spec should be
07d6d2b8 6099 changed or the programs updated. */
1a9ccd70 6100 if (alloc > 1
cd584857
NC
6101 && tdata->phdr[0].p_type == PT_PHDR
6102 && ! bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, alloc)
6103 && tdata->phdr[1].p_type == PT_LOAD
6104 && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr
6105 || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz)
6106 < (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz)))
1a9ccd70
NC
6107 {
6108 /* The fix for this error is usually to edit the linker script being
6109 used and set up the program headers manually. Either that or
6110 leave room for the headers at the start of the SECTIONS. */
6111 _bfd_error_handler (_("\
6112%B: error: PHDR segment not covered by LOAD segment"),
6113 abfd);
1a9ccd70
NC
6114 return FALSE;
6115 }
6116
f3520d2f 6117 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
cd584857
NC
6118 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6119 return FALSE;
252b5132
RH
6120 }
6121
b34976b6 6122 return TRUE;
252b5132
RH
6123}
6124
b34976b6 6125static bfd_boolean
217aa764 6126prep_headers (bfd *abfd)
252b5132 6127{
3d540e93 6128 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */
2b0f7ef9 6129 struct elf_strtab_hash *shstrtab;
9c5bfbb7 6130 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132
RH
6131
6132 i_ehdrp = elf_elfheader (abfd);
252b5132 6133
2b0f7ef9 6134 shstrtab = _bfd_elf_strtab_init ();
252b5132 6135 if (shstrtab == NULL)
b34976b6 6136 return FALSE;
252b5132
RH
6137
6138 elf_shstrtab (abfd) = shstrtab;
6139
6140 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
6141 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
6142 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
6143 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
6144
6145 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
6146 i_ehdrp->e_ident[EI_DATA] =
6147 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
6148 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
6149
252b5132
RH
6150 if ((abfd->flags & DYNAMIC) != 0)
6151 i_ehdrp->e_type = ET_DYN;
6152 else if ((abfd->flags & EXEC_P) != 0)
6153 i_ehdrp->e_type = ET_EXEC;
6154 else if (bfd_get_format (abfd) == bfd_core)
6155 i_ehdrp->e_type = ET_CORE;
6156 else
6157 i_ehdrp->e_type = ET_REL;
6158
6159 switch (bfd_get_arch (abfd))
6160 {
6161 case bfd_arch_unknown:
6162 i_ehdrp->e_machine = EM_NONE;
6163 break;
aa4f99bb
AO
6164
6165 /* There used to be a long list of cases here, each one setting
6166 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6167 in the corresponding bfd definition. To avoid duplication,
6168 the switch was removed. Machines that need special handling
6169 can generally do it in elf_backend_final_write_processing(),
6170 unless they need the information earlier than the final write.
6171 Such need can generally be supplied by replacing the tests for
6172 e_machine with the conditions used to determine it. */
252b5132 6173 default:
9c5bfbb7
AM
6174 i_ehdrp->e_machine = bed->elf_machine_code;
6175 }
aa4f99bb 6176
252b5132
RH
6177 i_ehdrp->e_version = bed->s->ev_current;
6178 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
6179
c044fabd 6180 /* No program header, for now. */
252b5132
RH
6181 i_ehdrp->e_phoff = 0;
6182 i_ehdrp->e_phentsize = 0;
6183 i_ehdrp->e_phnum = 0;
6184
c044fabd 6185 /* Each bfd section is section header entry. */
252b5132
RH
6186 i_ehdrp->e_entry = bfd_get_start_address (abfd);
6187 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
6188
c044fabd 6189 /* If we're building an executable, we'll need a program header table. */
252b5132 6190 if (abfd->flags & EXEC_P)
0e71e495
BE
6191 /* It all happens later. */
6192 ;
252b5132
RH
6193 else
6194 {
6195 i_ehdrp->e_phentsize = 0;
252b5132
RH
6196 i_ehdrp->e_phoff = 0;
6197 }
6198
6199 elf_tdata (abfd)->symtab_hdr.sh_name =
b34976b6 6200 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
252b5132 6201 elf_tdata (abfd)->strtab_hdr.sh_name =
b34976b6 6202 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
252b5132 6203 elf_tdata (abfd)->shstrtab_hdr.sh_name =
b34976b6 6204 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
252b5132 6205 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
17ca87fc 6206 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
252b5132 6207 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
b34976b6 6208 return FALSE;
252b5132 6209
b34976b6 6210 return TRUE;
252b5132
RH
6211}
6212
6213/* Assign file positions for all the reloc sections which are not part
a485e98e 6214 of the loadable file image, and the file position of section headers. */
252b5132 6215
0ce398f1
L
6216static bfd_boolean
6217_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
252b5132
RH
6218{
6219 file_ptr off;
e06efbf1 6220 Elf_Internal_Shdr **shdrpp, **end_shdrpp;
3e19fb8f 6221 Elf_Internal_Shdr *shdrp;
a485e98e
AM
6222 Elf_Internal_Ehdr *i_ehdrp;
6223 const struct elf_backend_data *bed;
252b5132 6224
12bd6957 6225 off = elf_next_file_pos (abfd);
252b5132 6226
e06efbf1
L
6227 shdrpp = elf_elfsections (abfd);
6228 end_shdrpp = shdrpp + elf_numsections (abfd);
6229 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
252b5132 6230 {
252b5132 6231 shdrp = *shdrpp;
0ce398f1
L
6232 if (shdrp->sh_offset == -1)
6233 {
3e19fb8f 6234 asection *sec = shdrp->bfd_section;
0ce398f1
L
6235 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
6236 || shdrp->sh_type == SHT_RELA);
6237 if (is_rel
3e19fb8f 6238 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
0ce398f1
L
6239 {
6240 if (!is_rel)
6241 {
3e19fb8f
L
6242 const char *name = sec->name;
6243 struct bfd_elf_section_data *d;
6244
0ce398f1 6245 /* Compress DWARF debug sections. */
3e19fb8f 6246 if (!bfd_compress_section (abfd, sec,
0ce398f1
L
6247 shdrp->contents))
6248 return FALSE;
3e19fb8f
L
6249
6250 if (sec->compress_status == COMPRESS_SECTION_DONE
6251 && (abfd->flags & BFD_COMPRESS_GABI) == 0)
6252 {
6253 /* If section is compressed with zlib-gnu, convert
6254 section name from .debug_* to .zdebug_*. */
6255 char *new_name
6256 = convert_debug_to_zdebug (abfd, name);
6257 if (new_name == NULL)
6258 return FALSE;
6259 name = new_name;
6260 }
dd905818 6261 /* Add section name to section name section. */
3e19fb8f
L
6262 if (shdrp->sh_name != (unsigned int) -1)
6263 abort ();
6264 shdrp->sh_name
6265 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
6266 name, FALSE);
6267 d = elf_section_data (sec);
6268
dd905818 6269 /* Add reloc section name to section name section. */
3e19fb8f
L
6270 if (d->rel.hdr
6271 && !_bfd_elf_set_reloc_sh_name (abfd,
6272 d->rel.hdr,
6273 name, FALSE))
6274 return FALSE;
6275 if (d->rela.hdr
6276 && !_bfd_elf_set_reloc_sh_name (abfd,
6277 d->rela.hdr,
91cb26da 6278 name, TRUE))
3e19fb8f
L
6279 return FALSE;
6280
0ce398f1 6281 /* Update section size and contents. */
3e19fb8f
L
6282 shdrp->sh_size = sec->size;
6283 shdrp->contents = sec->contents;
0ce398f1
L
6284 shdrp->bfd_section->contents = NULL;
6285 }
6286 off = _bfd_elf_assign_file_position_for_section (shdrp,
6287 off,
6288 TRUE);
6289 }
6290 }
252b5132
RH
6291 }
6292
3e19fb8f
L
6293 /* Place section name section after DWARF debug sections have been
6294 compressed. */
6295 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
6296 shdrp = &elf_tdata (abfd)->shstrtab_hdr;
6297 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
6298 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
6299
6300 /* Place the section headers. */
a485e98e
AM
6301 i_ehdrp = elf_elfheader (abfd);
6302 bed = get_elf_backend_data (abfd);
6303 off = align_file_position (off, 1 << bed->s->log_file_align);
6304 i_ehdrp->e_shoff = off;
6305 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
12bd6957 6306 elf_next_file_pos (abfd) = off;
0ce398f1
L
6307
6308 return TRUE;
252b5132
RH
6309}
6310
b34976b6 6311bfd_boolean
217aa764 6312_bfd_elf_write_object_contents (bfd *abfd)
252b5132 6313{
9c5bfbb7 6314 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 6315 Elf_Internal_Shdr **i_shdrp;
b34976b6 6316 bfd_boolean failed;
9ad5cbcf 6317 unsigned int count, num_sec;
30e8ee25 6318 struct elf_obj_tdata *t;
252b5132
RH
6319
6320 if (! abfd->output_has_begun
217aa764 6321 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 6322 return FALSE;
252b5132
RH
6323
6324 i_shdrp = elf_elfsections (abfd);
252b5132 6325
b34976b6 6326 failed = FALSE;
252b5132
RH
6327 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
6328 if (failed)
b34976b6 6329 return FALSE;
252b5132 6330
0ce398f1
L
6331 if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
6332 return FALSE;
252b5132 6333
c044fabd 6334 /* After writing the headers, we need to write the sections too... */
9ad5cbcf
AM
6335 num_sec = elf_numsections (abfd);
6336 for (count = 1; count < num_sec; count++)
252b5132 6337 {
3e19fb8f
L
6338 i_shdrp[count]->sh_name
6339 = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
6340 i_shdrp[count]->sh_name);
252b5132
RH
6341 if (bed->elf_backend_section_processing)
6342 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
6343 if (i_shdrp[count]->contents)
6344 {
dc810e39
AM
6345 bfd_size_type amt = i_shdrp[count]->sh_size;
6346
252b5132 6347 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
dc810e39 6348 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
b34976b6 6349 return FALSE;
252b5132
RH
6350 }
6351 }
6352
6353 /* Write out the section header names. */
30e8ee25 6354 t = elf_tdata (abfd);
26ae6d5e 6355 if (elf_shstrtab (abfd) != NULL
30e8ee25 6356 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
08a40648 6357 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
b34976b6 6358 return FALSE;
252b5132
RH
6359
6360 if (bed->elf_backend_final_write_processing)
12bd6957 6361 (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
252b5132 6362
ff59fc36
RM
6363 if (!bed->s->write_shdrs_and_ehdr (abfd))
6364 return FALSE;
6365
6366 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
c0355132
AM
6367 if (t->o->build_id.after_write_object_contents != NULL)
6368 return (*t->o->build_id.after_write_object_contents) (abfd);
ff59fc36
RM
6369
6370 return TRUE;
252b5132
RH
6371}
6372
b34976b6 6373bfd_boolean
217aa764 6374_bfd_elf_write_corefile_contents (bfd *abfd)
252b5132 6375{
c044fabd 6376 /* Hopefully this can be done just like an object file. */
252b5132
RH
6377 return _bfd_elf_write_object_contents (abfd);
6378}
c044fabd
KH
6379
6380/* Given a section, search the header to find them. */
6381
cb33740c 6382unsigned int
198beae2 6383_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
252b5132 6384{
9c5bfbb7 6385 const struct elf_backend_data *bed;
91d6fa6a 6386 unsigned int sec_index;
252b5132 6387
9ad5cbcf
AM
6388 if (elf_section_data (asect) != NULL
6389 && elf_section_data (asect)->this_idx != 0)
6390 return elf_section_data (asect)->this_idx;
6391
6392 if (bfd_is_abs_section (asect))
91d6fa6a 6393 sec_index = SHN_ABS;
af746e92 6394 else if (bfd_is_com_section (asect))
91d6fa6a 6395 sec_index = SHN_COMMON;
af746e92 6396 else if (bfd_is_und_section (asect))
91d6fa6a 6397 sec_index = SHN_UNDEF;
af746e92 6398 else
91d6fa6a 6399 sec_index = SHN_BAD;
252b5132 6400
af746e92 6401 bed = get_elf_backend_data (abfd);
252b5132
RH
6402 if (bed->elf_backend_section_from_bfd_section)
6403 {
91d6fa6a 6404 int retval = sec_index;
9ad5cbcf 6405
af746e92
AM
6406 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
6407 return retval;
252b5132
RH
6408 }
6409
91d6fa6a 6410 if (sec_index == SHN_BAD)
af746e92 6411 bfd_set_error (bfd_error_nonrepresentable_section);
252b5132 6412
91d6fa6a 6413 return sec_index;
252b5132
RH
6414}
6415
6416/* Given a BFD symbol, return the index in the ELF symbol table, or -1
6417 on error. */
6418
6419int
217aa764 6420_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
252b5132
RH
6421{
6422 asymbol *asym_ptr = *asym_ptr_ptr;
6423 int idx;
6424 flagword flags = asym_ptr->flags;
6425
6426 /* When gas creates relocations against local labels, it creates its
6427 own symbol for the section, but does put the symbol into the
6428 symbol chain, so udata is 0. When the linker is generating
6429 relocatable output, this section symbol may be for one of the
6430 input sections rather than the output section. */
6431 if (asym_ptr->udata.i == 0
6432 && (flags & BSF_SECTION_SYM)
6433 && asym_ptr->section)
6434 {
5372391b 6435 asection *sec;
252b5132
RH
6436 int indx;
6437
5372391b
AM
6438 sec = asym_ptr->section;
6439 if (sec->owner != abfd && sec->output_section != NULL)
6440 sec = sec->output_section;
6441 if (sec->owner == abfd
6442 && (indx = sec->index) < elf_num_section_syms (abfd)
4e89ac30 6443 && elf_section_syms (abfd)[indx] != NULL)
252b5132
RH
6444 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
6445 }
6446
6447 idx = asym_ptr->udata.i;
6448
6449 if (idx == 0)
6450 {
6451 /* This case can occur when using --strip-symbol on a symbol
08a40648 6452 which is used in a relocation entry. */
4eca0228 6453 _bfd_error_handler
695344c0 6454 /* xgettext:c-format */
d003868e
AM
6455 (_("%B: symbol `%s' required but not present"),
6456 abfd, bfd_asymbol_name (asym_ptr));
252b5132
RH
6457 bfd_set_error (bfd_error_no_symbols);
6458 return -1;
6459 }
6460
6461#if DEBUG & 4
6462 {
6463 fprintf (stderr,
cd9af601
AM
6464 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6465 (long) asym_ptr, asym_ptr->name, idx, flags);
252b5132
RH
6466 fflush (stderr);
6467 }
6468#endif
6469
6470 return idx;
6471}
6472
84d1d650 6473/* Rewrite program header information. */
252b5132 6474
b34976b6 6475static bfd_boolean
84d1d650 6476rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
252b5132 6477{
b34976b6
AM
6478 Elf_Internal_Ehdr *iehdr;
6479 struct elf_segment_map *map;
6480 struct elf_segment_map *map_first;
6481 struct elf_segment_map **pointer_to_map;
6482 Elf_Internal_Phdr *segment;
6483 asection *section;
6484 unsigned int i;
6485 unsigned int num_segments;
6486 bfd_boolean phdr_included = FALSE;
5c44b38e 6487 bfd_boolean p_paddr_valid;
b34976b6
AM
6488 bfd_vma maxpagesize;
6489 struct elf_segment_map *phdr_adjust_seg = NULL;
6490 unsigned int phdr_adjust_num = 0;
9c5bfbb7 6491 const struct elf_backend_data *bed;
bc67d8a6 6492
caf47ea6 6493 bed = get_elf_backend_data (ibfd);
252b5132
RH
6494 iehdr = elf_elfheader (ibfd);
6495
bc67d8a6 6496 map_first = NULL;
c044fabd 6497 pointer_to_map = &map_first;
252b5132
RH
6498
6499 num_segments = elf_elfheader (ibfd)->e_phnum;
bc67d8a6
NC
6500 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6501
6502 /* Returns the end address of the segment + 1. */
aecc8f8a
AM
6503#define SEGMENT_END(segment, start) \
6504 (start + (segment->p_memsz > segment->p_filesz \
6505 ? segment->p_memsz : segment->p_filesz))
bc67d8a6 6506
eecdbe52
JJ
6507#define SECTION_SIZE(section, segment) \
6508 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6509 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
eea6121a 6510 ? section->size : 0)
eecdbe52 6511
b34976b6 6512 /* Returns TRUE if the given section is contained within
bc67d8a6 6513 the given segment. VMA addresses are compared. */
aecc8f8a
AM
6514#define IS_CONTAINED_BY_VMA(section, segment) \
6515 (section->vma >= segment->p_vaddr \
eecdbe52 6516 && (section->vma + SECTION_SIZE (section, segment) \
aecc8f8a 6517 <= (SEGMENT_END (segment, segment->p_vaddr))))
c044fabd 6518
b34976b6 6519 /* Returns TRUE if the given section is contained within
bc67d8a6 6520 the given segment. LMA addresses are compared. */
aecc8f8a
AM
6521#define IS_CONTAINED_BY_LMA(section, segment, base) \
6522 (section->lma >= base \
eecdbe52 6523 && (section->lma + SECTION_SIZE (section, segment) \
aecc8f8a 6524 <= SEGMENT_END (segment, base)))
252b5132 6525
0efc80c8
L
6526 /* Handle PT_NOTE segment. */
6527#define IS_NOTE(p, s) \
aecc8f8a 6528 (p->p_type == PT_NOTE \
0efc80c8 6529 && elf_section_type (s) == SHT_NOTE \
aecc8f8a 6530 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6531 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6532 <= p->p_offset + p->p_filesz))
252b5132 6533
0efc80c8
L
6534 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6535 etc. */
6536#define IS_COREFILE_NOTE(p, s) \
6537 (IS_NOTE (p, s) \
6538 && bfd_get_format (ibfd) == bfd_core \
6539 && s->vma == 0 \
6540 && s->lma == 0)
6541
252b5132
RH
6542 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6543 linker, which generates a PT_INTERP section with p_vaddr and
6544 p_memsz set to 0. */
aecc8f8a
AM
6545#define IS_SOLARIS_PT_INTERP(p, s) \
6546 (p->p_vaddr == 0 \
6547 && p->p_paddr == 0 \
6548 && p->p_memsz == 0 \
6549 && p->p_filesz > 0 \
6550 && (s->flags & SEC_HAS_CONTENTS) != 0 \
eea6121a 6551 && s->size > 0 \
aecc8f8a 6552 && (bfd_vma) s->filepos >= p->p_offset \
cb3ff1e5 6553 && ((bfd_vma) s->filepos + s->size \
aecc8f8a 6554 <= p->p_offset + p->p_filesz))
5c440b1e 6555
bc67d8a6
NC
6556 /* Decide if the given section should be included in the given segment.
6557 A section will be included if:
f5ffc919 6558 1. It is within the address space of the segment -- we use the LMA
08a40648 6559 if that is set for the segment and the VMA otherwise,
0efc80c8 6560 2. It is an allocated section or a NOTE section in a PT_NOTE
d324f6d6 6561 segment.
bc67d8a6 6562 3. There is an output section associated with it,
eecdbe52 6563 4. The section has not already been allocated to a previous segment.
2b05f1b7 6564 5. PT_GNU_STACK segments do not include any sections.
03394ac9 6565 6. PT_TLS segment includes only SHF_TLS sections.
6f79b219
JJ
6566 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6567 8. PT_DYNAMIC should not contain empty sections at the beginning
08a40648 6568 (with the possible exception of .dynamic). */
9f17e2a6 6569#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
2b05f1b7
L
6570 ((((segment->p_paddr \
6571 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6572 : IS_CONTAINED_BY_VMA (section, segment)) \
6573 && (section->flags & SEC_ALLOC) != 0) \
0efc80c8 6574 || IS_NOTE (segment, section)) \
2b05f1b7
L
6575 && segment->p_type != PT_GNU_STACK \
6576 && (segment->p_type != PT_TLS \
6577 || (section->flags & SEC_THREAD_LOCAL)) \
6578 && (segment->p_type == PT_LOAD \
6579 || segment->p_type == PT_TLS \
6580 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6581 && (segment->p_type != PT_DYNAMIC \
6582 || SECTION_SIZE (section, segment) > 0 \
6583 || (segment->p_paddr \
6584 ? segment->p_paddr != section->lma \
6585 : segment->p_vaddr != section->vma) \
6586 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6587 == 0)) \
0067a569 6588 && !section->segment_mark)
bc67d8a6 6589
9f17e2a6
L
6590/* If the output section of a section in the input segment is NULL,
6591 it is removed from the corresponding output segment. */
6592#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6593 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6594 && section->output_section != NULL)
6595
b34976b6 6596 /* Returns TRUE iff seg1 starts after the end of seg2. */
b5f852ea
NC
6597#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6598 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6599
6600 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6601 their VMA address ranges and their LMA address ranges overlap.
6602 It is possible to have overlapping VMA ranges without overlapping LMA
6603 ranges. RedBoot images for example can have both .data and .bss mapped
6604 to the same VMA range, but with the .data section mapped to a different
6605 LMA. */
aecc8f8a 6606#define SEGMENT_OVERLAPS(seg1, seg2) \
b5f852ea 6607 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
08a40648 6608 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
b5f852ea 6609 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
08a40648 6610 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
bc67d8a6
NC
6611
6612 /* Initialise the segment mark field. */
6613 for (section = ibfd->sections; section != NULL; section = section->next)
b34976b6 6614 section->segment_mark = FALSE;
bc67d8a6 6615
5c44b38e
AM
6616 /* The Solaris linker creates program headers in which all the
6617 p_paddr fields are zero. When we try to objcopy or strip such a
6618 file, we get confused. Check for this case, and if we find it
6619 don't set the p_paddr_valid fields. */
6620 p_paddr_valid = FALSE;
6621 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6622 i < num_segments;
6623 i++, segment++)
6624 if (segment->p_paddr != 0)
6625 {
6626 p_paddr_valid = TRUE;
6627 break;
6628 }
6629
252b5132 6630 /* Scan through the segments specified in the program header
bc67d8a6 6631 of the input BFD. For this first scan we look for overlaps
9ad5cbcf 6632 in the loadable segments. These can be created by weird
aecc8f8a 6633 parameters to objcopy. Also, fix some solaris weirdness. */
bc67d8a6
NC
6634 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6635 i < num_segments;
c044fabd 6636 i++, segment++)
252b5132 6637 {
252b5132 6638 unsigned int j;
c044fabd 6639 Elf_Internal_Phdr *segment2;
252b5132 6640
aecc8f8a
AM
6641 if (segment->p_type == PT_INTERP)
6642 for (section = ibfd->sections; section; section = section->next)
6643 if (IS_SOLARIS_PT_INTERP (segment, section))
6644 {
6645 /* Mininal change so that the normal section to segment
4cc11e76 6646 assignment code will work. */
aecc8f8a
AM
6647 segment->p_vaddr = section->vma;
6648 break;
6649 }
6650
bc67d8a6 6651 if (segment->p_type != PT_LOAD)
b10a8ae0
L
6652 {
6653 /* Remove PT_GNU_RELRO segment. */
6654 if (segment->p_type == PT_GNU_RELRO)
6655 segment->p_type = PT_NULL;
6656 continue;
6657 }
c044fabd 6658
bc67d8a6 6659 /* Determine if this segment overlaps any previous segments. */
0067a569 6660 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
bc67d8a6
NC
6661 {
6662 bfd_signed_vma extra_length;
c044fabd 6663
bc67d8a6 6664 if (segment2->p_type != PT_LOAD
0067a569 6665 || !SEGMENT_OVERLAPS (segment, segment2))
bc67d8a6 6666 continue;
c044fabd 6667
bc67d8a6
NC
6668 /* Merge the two segments together. */
6669 if (segment2->p_vaddr < segment->p_vaddr)
6670 {
c044fabd 6671 /* Extend SEGMENT2 to include SEGMENT and then delete
08a40648 6672 SEGMENT. */
0067a569
AM
6673 extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6674 - SEGMENT_END (segment2, segment2->p_vaddr));
c044fabd 6675
bc67d8a6
NC
6676 if (extra_length > 0)
6677 {
0067a569 6678 segment2->p_memsz += extra_length;
bc67d8a6
NC
6679 segment2->p_filesz += extra_length;
6680 }
c044fabd 6681
bc67d8a6 6682 segment->p_type = PT_NULL;
c044fabd 6683
bc67d8a6
NC
6684 /* Since we have deleted P we must restart the outer loop. */
6685 i = 0;
6686 segment = elf_tdata (ibfd)->phdr;
6687 break;
6688 }
6689 else
6690 {
c044fabd 6691 /* Extend SEGMENT to include SEGMENT2 and then delete
08a40648 6692 SEGMENT2. */
0067a569
AM
6693 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6694 - SEGMENT_END (segment, segment->p_vaddr));
c044fabd 6695
bc67d8a6
NC
6696 if (extra_length > 0)
6697 {
0067a569 6698 segment->p_memsz += extra_length;
bc67d8a6
NC
6699 segment->p_filesz += extra_length;
6700 }
c044fabd 6701
bc67d8a6
NC
6702 segment2->p_type = PT_NULL;
6703 }
6704 }
6705 }
c044fabd 6706
bc67d8a6
NC
6707 /* The second scan attempts to assign sections to segments. */
6708 for (i = 0, segment = elf_tdata (ibfd)->phdr;
6709 i < num_segments;
0067a569 6710 i++, segment++)
bc67d8a6 6711 {
0067a569
AM
6712 unsigned int section_count;
6713 asection **sections;
6714 asection *output_section;
6715 unsigned int isec;
6716 bfd_vma matching_lma;
6717 bfd_vma suggested_lma;
6718 unsigned int j;
dc810e39 6719 bfd_size_type amt;
0067a569
AM
6720 asection *first_section;
6721 bfd_boolean first_matching_lma;
6722 bfd_boolean first_suggested_lma;
bc67d8a6
NC
6723
6724 if (segment->p_type == PT_NULL)
6725 continue;
c044fabd 6726
9f17e2a6 6727 first_section = NULL;
bc67d8a6 6728 /* Compute how many sections might be placed into this segment. */
b5f852ea
NC
6729 for (section = ibfd->sections, section_count = 0;
6730 section != NULL;
6731 section = section->next)
9f17e2a6
L
6732 {
6733 /* Find the first section in the input segment, which may be
6734 removed from the corresponding output segment. */
6735 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6736 {
6737 if (first_section == NULL)
6738 first_section = section;
6739 if (section->output_section != NULL)
6740 ++section_count;
6741 }
6742 }
811072d8 6743
b5f852ea
NC
6744 /* Allocate a segment map big enough to contain
6745 all of the sections we have selected. */
dc810e39
AM
6746 amt = sizeof (struct elf_segment_map);
6747 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
a50b1753 6748 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 6749 if (map == NULL)
b34976b6 6750 return FALSE;
252b5132
RH
6751
6752 /* Initialise the fields of the segment map. Default to
6753 using the physical address of the segment in the input BFD. */
0067a569
AM
6754 map->next = NULL;
6755 map->p_type = segment->p_type;
6756 map->p_flags = segment->p_flags;
bc67d8a6 6757 map->p_flags_valid = 1;
55d55ac7 6758
9f17e2a6
L
6759 /* If the first section in the input segment is removed, there is
6760 no need to preserve segment physical address in the corresponding
6761 output segment. */
945c025a 6762 if (!first_section || first_section->output_section != NULL)
9f17e2a6
L
6763 {
6764 map->p_paddr = segment->p_paddr;
5c44b38e 6765 map->p_paddr_valid = p_paddr_valid;
9f17e2a6 6766 }
252b5132
RH
6767
6768 /* Determine if this segment contains the ELF file header
6769 and if it contains the program headers themselves. */
bc67d8a6
NC
6770 map->includes_filehdr = (segment->p_offset == 0
6771 && segment->p_filesz >= iehdr->e_ehsize);
bc67d8a6 6772 map->includes_phdrs = 0;
252b5132 6773
0067a569 6774 if (!phdr_included || segment->p_type != PT_LOAD)
252b5132 6775 {
bc67d8a6
NC
6776 map->includes_phdrs =
6777 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6778 && (segment->p_offset + segment->p_filesz
252b5132
RH
6779 >= ((bfd_vma) iehdr->e_phoff
6780 + iehdr->e_phnum * iehdr->e_phentsize)));
c044fabd 6781
bc67d8a6 6782 if (segment->p_type == PT_LOAD && map->includes_phdrs)
b34976b6 6783 phdr_included = TRUE;
252b5132
RH
6784 }
6785
bc67d8a6 6786 if (section_count == 0)
252b5132
RH
6787 {
6788 /* Special segments, such as the PT_PHDR segment, may contain
6789 no sections, but ordinary, loadable segments should contain
1ed89aa9 6790 something. They are allowed by the ELF spec however, so only
07d6d2b8 6791 a warning is produced.
f98450c6
NC
6792 There is however the valid use case of embedded systems which
6793 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6794 flash memory with zeros. No warning is shown for that case. */
6795 if (segment->p_type == PT_LOAD
6796 && (segment->p_filesz > 0 || segment->p_memsz == 0))
6797 /* xgettext:c-format */
76cfced5
AM
6798 _bfd_error_handler (_("%B: warning: Empty loadable segment detected"
6799 " at vaddr=%#Lx, is this intentional?"),
f98450c6 6800 ibfd, segment->p_vaddr);
252b5132 6801
bc67d8a6 6802 map->count = 0;
c044fabd
KH
6803 *pointer_to_map = map;
6804 pointer_to_map = &map->next;
252b5132
RH
6805
6806 continue;
6807 }
6808
6809 /* Now scan the sections in the input BFD again and attempt
6810 to add their corresponding output sections to the segment map.
6811 The problem here is how to handle an output section which has
6812 been moved (ie had its LMA changed). There are four possibilities:
6813
6814 1. None of the sections have been moved.
6815 In this case we can continue to use the segment LMA from the
6816 input BFD.
6817
6818 2. All of the sections have been moved by the same amount.
6819 In this case we can change the segment's LMA to match the LMA
6820 of the first section.
6821
6822 3. Some of the sections have been moved, others have not.
6823 In this case those sections which have not been moved can be
6824 placed in the current segment which will have to have its size,
6825 and possibly its LMA changed, and a new segment or segments will
6826 have to be created to contain the other sections.
6827
b5f852ea 6828 4. The sections have been moved, but not by the same amount.
252b5132
RH
6829 In this case we can change the segment's LMA to match the LMA
6830 of the first section and we will have to create a new segment
6831 or segments to contain the other sections.
6832
6833 In order to save time, we allocate an array to hold the section
6834 pointers that we are interested in. As these sections get assigned
6835 to a segment, they are removed from this array. */
6836
a50b1753 6837 sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
252b5132 6838 if (sections == NULL)
b34976b6 6839 return FALSE;
252b5132
RH
6840
6841 /* Step One: Scan for segment vs section LMA conflicts.
6842 Also add the sections to the section array allocated above.
6843 Also add the sections to the current segment. In the common
6844 case, where the sections have not been moved, this means that
6845 we have completely filled the segment, and there is nothing
6846 more to do. */
252b5132 6847 isec = 0;
72730e0c 6848 matching_lma = 0;
252b5132 6849 suggested_lma = 0;
0067a569
AM
6850 first_matching_lma = TRUE;
6851 first_suggested_lma = TRUE;
252b5132 6852
461c4b2e 6853 for (section = first_section, j = 0;
bc67d8a6
NC
6854 section != NULL;
6855 section = section->next)
252b5132 6856 {
caf47ea6 6857 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
c0f7859b 6858 {
bc67d8a6
NC
6859 output_section = section->output_section;
6860
0067a569 6861 sections[j++] = section;
252b5132
RH
6862
6863 /* The Solaris native linker always sets p_paddr to 0.
6864 We try to catch that case here, and set it to the
5e8d7549
NC
6865 correct value. Note - some backends require that
6866 p_paddr be left as zero. */
5c44b38e 6867 if (!p_paddr_valid
4455705d 6868 && segment->p_vaddr != 0
0067a569 6869 && !bed->want_p_paddr_set_to_zero
252b5132 6870 && isec == 0
bc67d8a6 6871 && output_section->lma != 0
0067a569
AM
6872 && output_section->vma == (segment->p_vaddr
6873 + (map->includes_filehdr
6874 ? iehdr->e_ehsize
6875 : 0)
6876 + (map->includes_phdrs
6877 ? (iehdr->e_phnum
6878 * iehdr->e_phentsize)
6879 : 0)))
bc67d8a6 6880 map->p_paddr = segment->p_vaddr;
252b5132
RH
6881
6882 /* Match up the physical address of the segment with the
6883 LMA address of the output section. */
bc67d8a6 6884 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5e8d7549 6885 || IS_COREFILE_NOTE (segment, section)
0067a569
AM
6886 || (bed->want_p_paddr_set_to_zero
6887 && IS_CONTAINED_BY_VMA (output_section, segment)))
252b5132 6888 {
0067a569
AM
6889 if (first_matching_lma || output_section->lma < matching_lma)
6890 {
6891 matching_lma = output_section->lma;
6892 first_matching_lma = FALSE;
6893 }
252b5132
RH
6894
6895 /* We assume that if the section fits within the segment
bc67d8a6 6896 then it does not overlap any other section within that
252b5132 6897 segment. */
0067a569
AM
6898 map->sections[isec++] = output_section;
6899 }
6900 else if (first_suggested_lma)
6901 {
6902 suggested_lma = output_section->lma;
6903 first_suggested_lma = FALSE;
252b5132 6904 }
147d51c2
L
6905
6906 if (j == section_count)
6907 break;
252b5132
RH
6908 }
6909 }
6910
bc67d8a6 6911 BFD_ASSERT (j == section_count);
252b5132
RH
6912
6913 /* Step Two: Adjust the physical address of the current segment,
6914 if necessary. */
bc67d8a6 6915 if (isec == section_count)
252b5132
RH
6916 {
6917 /* All of the sections fitted within the segment as currently
6918 specified. This is the default case. Add the segment to
6919 the list of built segments and carry on to process the next
6920 program header in the input BFD. */
bc67d8a6 6921 map->count = section_count;
c044fabd
KH
6922 *pointer_to_map = map;
6923 pointer_to_map = &map->next;
08a40648 6924
5c44b38e
AM
6925 if (p_paddr_valid
6926 && !bed->want_p_paddr_set_to_zero
147d51c2 6927 && matching_lma != map->p_paddr
5c44b38e
AM
6928 && !map->includes_filehdr
6929 && !map->includes_phdrs)
3271a814
NS
6930 /* There is some padding before the first section in the
6931 segment. So, we must account for that in the output
6932 segment's vma. */
6933 map->p_vaddr_offset = matching_lma - map->p_paddr;
08a40648 6934
252b5132
RH
6935 free (sections);
6936 continue;
6937 }
252b5132
RH
6938 else
6939 {
0067a569 6940 if (!first_matching_lma)
72730e0c
AM
6941 {
6942 /* At least one section fits inside the current segment.
6943 Keep it, but modify its physical address to match the
6944 LMA of the first section that fitted. */
bc67d8a6 6945 map->p_paddr = matching_lma;
72730e0c
AM
6946 }
6947 else
6948 {
6949 /* None of the sections fitted inside the current segment.
6950 Change the current segment's physical address to match
6951 the LMA of the first section. */
bc67d8a6 6952 map->p_paddr = suggested_lma;
72730e0c
AM
6953 }
6954
bc67d8a6
NC
6955 /* Offset the segment physical address from the lma
6956 to allow for space taken up by elf headers. */
6957 if (map->includes_filehdr)
010c8431
AM
6958 {
6959 if (map->p_paddr >= iehdr->e_ehsize)
6960 map->p_paddr -= iehdr->e_ehsize;
6961 else
6962 {
6963 map->includes_filehdr = FALSE;
6964 map->includes_phdrs = FALSE;
6965 }
6966 }
252b5132 6967
bc67d8a6
NC
6968 if (map->includes_phdrs)
6969 {
010c8431
AM
6970 if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6971 {
6972 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6973
6974 /* iehdr->e_phnum is just an estimate of the number
6975 of program headers that we will need. Make a note
6976 here of the number we used and the segment we chose
6977 to hold these headers, so that we can adjust the
6978 offset when we know the correct value. */
6979 phdr_adjust_num = iehdr->e_phnum;
6980 phdr_adjust_seg = map;
6981 }
6982 else
6983 map->includes_phdrs = FALSE;
bc67d8a6 6984 }
252b5132
RH
6985 }
6986
6987 /* Step Three: Loop over the sections again, this time assigning
caf47ea6 6988 those that fit to the current segment and removing them from the
252b5132
RH
6989 sections array; but making sure not to leave large gaps. Once all
6990 possible sections have been assigned to the current segment it is
6991 added to the list of built segments and if sections still remain
6992 to be assigned, a new segment is constructed before repeating
6993 the loop. */
6994 isec = 0;
6995 do
6996 {
bc67d8a6 6997 map->count = 0;
252b5132 6998 suggested_lma = 0;
0067a569 6999 first_suggested_lma = TRUE;
252b5132
RH
7000
7001 /* Fill the current segment with sections that fit. */
bc67d8a6 7002 for (j = 0; j < section_count; j++)
252b5132 7003 {
bc67d8a6 7004 section = sections[j];
252b5132 7005
bc67d8a6 7006 if (section == NULL)
252b5132
RH
7007 continue;
7008
bc67d8a6 7009 output_section = section->output_section;
252b5132 7010
bc67d8a6 7011 BFD_ASSERT (output_section != NULL);
c044fabd 7012
bc67d8a6
NC
7013 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
7014 || IS_COREFILE_NOTE (segment, section))
252b5132 7015 {
bc67d8a6 7016 if (map->count == 0)
252b5132
RH
7017 {
7018 /* If the first section in a segment does not start at
bc67d8a6
NC
7019 the beginning of the segment, then something is
7020 wrong. */
0067a569
AM
7021 if (output_section->lma
7022 != (map->p_paddr
7023 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
7024 + (map->includes_phdrs
7025 ? iehdr->e_phnum * iehdr->e_phentsize
7026 : 0)))
252b5132
RH
7027 abort ();
7028 }
7029 else
7030 {
0067a569 7031 asection *prev_sec;
252b5132 7032
bc67d8a6 7033 prev_sec = map->sections[map->count - 1];
252b5132
RH
7034
7035 /* If the gap between the end of the previous section
bc67d8a6
NC
7036 and the start of this section is more than
7037 maxpagesize then we need to start a new segment. */
eea6121a 7038 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
079e9a2f 7039 maxpagesize)
caf47ea6 7040 < BFD_ALIGN (output_section->lma, maxpagesize))
0067a569 7041 || (prev_sec->lma + prev_sec->size
079e9a2f 7042 > output_section->lma))
252b5132 7043 {
0067a569
AM
7044 if (first_suggested_lma)
7045 {
7046 suggested_lma = output_section->lma;
7047 first_suggested_lma = FALSE;
7048 }
252b5132
RH
7049
7050 continue;
7051 }
7052 }
7053
bc67d8a6 7054 map->sections[map->count++] = output_section;
252b5132
RH
7055 ++isec;
7056 sections[j] = NULL;
b34976b6 7057 section->segment_mark = TRUE;
252b5132 7058 }
0067a569
AM
7059 else if (first_suggested_lma)
7060 {
7061 suggested_lma = output_section->lma;
7062 first_suggested_lma = FALSE;
7063 }
252b5132
RH
7064 }
7065
bc67d8a6 7066 BFD_ASSERT (map->count > 0);
252b5132
RH
7067
7068 /* Add the current segment to the list of built segments. */
c044fabd
KH
7069 *pointer_to_map = map;
7070 pointer_to_map = &map->next;
252b5132 7071
bc67d8a6 7072 if (isec < section_count)
252b5132
RH
7073 {
7074 /* We still have not allocated all of the sections to
7075 segments. Create a new segment here, initialise it
7076 and carry on looping. */
dc810e39
AM
7077 amt = sizeof (struct elf_segment_map);
7078 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5964fc3a 7079 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
bc67d8a6 7080 if (map == NULL)
5ed6aba4
NC
7081 {
7082 free (sections);
7083 return FALSE;
7084 }
252b5132
RH
7085
7086 /* Initialise the fields of the segment map. Set the physical
7087 physical address to the LMA of the first section that has
7088 not yet been assigned. */
0067a569
AM
7089 map->next = NULL;
7090 map->p_type = segment->p_type;
7091 map->p_flags = segment->p_flags;
7092 map->p_flags_valid = 1;
7093 map->p_paddr = suggested_lma;
5c44b38e 7094 map->p_paddr_valid = p_paddr_valid;
bc67d8a6 7095 map->includes_filehdr = 0;
0067a569 7096 map->includes_phdrs = 0;
252b5132
RH
7097 }
7098 }
bc67d8a6 7099 while (isec < section_count);
252b5132
RH
7100
7101 free (sections);
7102 }
7103
12bd6957 7104 elf_seg_map (obfd) = map_first;
bc67d8a6
NC
7105
7106 /* If we had to estimate the number of program headers that were
9ad5cbcf 7107 going to be needed, then check our estimate now and adjust
bc67d8a6
NC
7108 the offset if necessary. */
7109 if (phdr_adjust_seg != NULL)
7110 {
7111 unsigned int count;
c044fabd 7112
bc67d8a6 7113 for (count = 0, map = map_first; map != NULL; map = map->next)
c044fabd 7114 count++;
252b5132 7115
bc67d8a6
NC
7116 if (count > phdr_adjust_num)
7117 phdr_adjust_seg->p_paddr
7118 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
7119 }
c044fabd 7120
bc67d8a6 7121#undef SEGMENT_END
eecdbe52 7122#undef SECTION_SIZE
bc67d8a6
NC
7123#undef IS_CONTAINED_BY_VMA
7124#undef IS_CONTAINED_BY_LMA
0efc80c8 7125#undef IS_NOTE
252b5132 7126#undef IS_COREFILE_NOTE
bc67d8a6 7127#undef IS_SOLARIS_PT_INTERP
9f17e2a6 7128#undef IS_SECTION_IN_INPUT_SEGMENT
bc67d8a6
NC
7129#undef INCLUDE_SECTION_IN_SEGMENT
7130#undef SEGMENT_AFTER_SEGMENT
7131#undef SEGMENT_OVERLAPS
b34976b6 7132 return TRUE;
252b5132
RH
7133}
7134
84d1d650
L
7135/* Copy ELF program header information. */
7136
7137static bfd_boolean
7138copy_elf_program_header (bfd *ibfd, bfd *obfd)
7139{
7140 Elf_Internal_Ehdr *iehdr;
7141 struct elf_segment_map *map;
7142 struct elf_segment_map *map_first;
7143 struct elf_segment_map **pointer_to_map;
7144 Elf_Internal_Phdr *segment;
7145 unsigned int i;
7146 unsigned int num_segments;
7147 bfd_boolean phdr_included = FALSE;
88967714 7148 bfd_boolean p_paddr_valid;
84d1d650
L
7149
7150 iehdr = elf_elfheader (ibfd);
7151
7152 map_first = NULL;
7153 pointer_to_map = &map_first;
7154
88967714
AM
7155 /* If all the segment p_paddr fields are zero, don't set
7156 map->p_paddr_valid. */
7157 p_paddr_valid = FALSE;
84d1d650 7158 num_segments = elf_elfheader (ibfd)->e_phnum;
88967714
AM
7159 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7160 i < num_segments;
7161 i++, segment++)
7162 if (segment->p_paddr != 0)
7163 {
7164 p_paddr_valid = TRUE;
7165 break;
7166 }
7167
84d1d650
L
7168 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7169 i < num_segments;
7170 i++, segment++)
7171 {
7172 asection *section;
7173 unsigned int section_count;
7174 bfd_size_type amt;
7175 Elf_Internal_Shdr *this_hdr;
53020534 7176 asection *first_section = NULL;
a76e6f2f 7177 asection *lowest_section;
84d1d650 7178
84d1d650
L
7179 /* Compute how many sections are in this segment. */
7180 for (section = ibfd->sections, section_count = 0;
7181 section != NULL;
7182 section = section->next)
7183 {
7184 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7185 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
3271a814 7186 {
a76e6f2f
AM
7187 if (first_section == NULL)
7188 first_section = section;
3271a814
NS
7189 section_count++;
7190 }
84d1d650
L
7191 }
7192
7193 /* Allocate a segment map big enough to contain
7194 all of the sections we have selected. */
7195 amt = sizeof (struct elf_segment_map);
7196 if (section_count != 0)
7197 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
a50b1753 7198 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
84d1d650
L
7199 if (map == NULL)
7200 return FALSE;
7201
7202 /* Initialize the fields of the output segment map with the
7203 input segment. */
7204 map->next = NULL;
7205 map->p_type = segment->p_type;
7206 map->p_flags = segment->p_flags;
7207 map->p_flags_valid = 1;
7208 map->p_paddr = segment->p_paddr;
88967714 7209 map->p_paddr_valid = p_paddr_valid;
3f570048
AM
7210 map->p_align = segment->p_align;
7211 map->p_align_valid = 1;
3271a814 7212 map->p_vaddr_offset = 0;
84d1d650 7213
04c3a755
NS
7214 if (map->p_type == PT_GNU_RELRO
7215 || map->p_type == PT_GNU_STACK)
b10a8ae0
L
7216 {
7217 /* The PT_GNU_RELRO segment may contain the first a few
7218 bytes in the .got.plt section even if the whole .got.plt
7219 section isn't in the PT_GNU_RELRO segment. We won't
04c3a755
NS
7220 change the size of the PT_GNU_RELRO segment.
7221 Similarly, PT_GNU_STACK size is significant on uclinux
7222 systems. */
9433b9b1 7223 map->p_size = segment->p_memsz;
b10a8ae0
L
7224 map->p_size_valid = 1;
7225 }
7226
84d1d650
L
7227 /* Determine if this segment contains the ELF file header
7228 and if it contains the program headers themselves. */
7229 map->includes_filehdr = (segment->p_offset == 0
7230 && segment->p_filesz >= iehdr->e_ehsize);
7231
7232 map->includes_phdrs = 0;
7233 if (! phdr_included || segment->p_type != PT_LOAD)
7234 {
7235 map->includes_phdrs =
7236 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7237 && (segment->p_offset + segment->p_filesz
7238 >= ((bfd_vma) iehdr->e_phoff
7239 + iehdr->e_phnum * iehdr->e_phentsize)));
7240
7241 if (segment->p_type == PT_LOAD && map->includes_phdrs)
7242 phdr_included = TRUE;
7243 }
7244
bbefd0a9 7245 lowest_section = NULL;
84d1d650
L
7246 if (section_count != 0)
7247 {
7248 unsigned int isec = 0;
7249
53020534 7250 for (section = first_section;
84d1d650
L
7251 section != NULL;
7252 section = section->next)
7253 {
7254 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7255 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
53020534
L
7256 {
7257 map->sections[isec++] = section->output_section;
a76e6f2f
AM
7258 if ((section->flags & SEC_ALLOC) != 0)
7259 {
7260 bfd_vma seg_off;
7261
bbefd0a9
AM
7262 if (lowest_section == NULL
7263 || section->lma < lowest_section->lma)
fb8a5684
AM
7264 lowest_section = section;
7265
a76e6f2f
AM
7266 /* Section lmas are set up from PT_LOAD header
7267 p_paddr in _bfd_elf_make_section_from_shdr.
7268 If this header has a p_paddr that disagrees
7269 with the section lma, flag the p_paddr as
7270 invalid. */
7271 if ((section->flags & SEC_LOAD) != 0)
7272 seg_off = this_hdr->sh_offset - segment->p_offset;
7273 else
7274 seg_off = this_hdr->sh_addr - segment->p_vaddr;
7275 if (section->lma - segment->p_paddr != seg_off)
7276 map->p_paddr_valid = FALSE;
7277 }
53020534
L
7278 if (isec == section_count)
7279 break;
7280 }
84d1d650
L
7281 }
7282 }
7283
a76e6f2f
AM
7284 if (map->includes_filehdr && lowest_section != NULL)
7285 /* We need to keep the space used by the headers fixed. */
7286 map->header_size = lowest_section->vma - segment->p_vaddr;
d324f6d6 7287
a76e6f2f
AM
7288 if (!map->includes_phdrs
7289 && !map->includes_filehdr
7290 && map->p_paddr_valid)
7291 /* There is some other padding before the first section. */
7292 map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
7293 - segment->p_paddr);
7294
84d1d650
L
7295 map->count = section_count;
7296 *pointer_to_map = map;
7297 pointer_to_map = &map->next;
7298 }
7299
12bd6957 7300 elf_seg_map (obfd) = map_first;
84d1d650
L
7301 return TRUE;
7302}
7303
7304/* Copy private BFD data. This copies or rewrites ELF program header
7305 information. */
7306
7307static bfd_boolean
7308copy_private_bfd_data (bfd *ibfd, bfd *obfd)
7309{
84d1d650
L
7310 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7311 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7312 return TRUE;
7313
7314 if (elf_tdata (ibfd)->phdr == NULL)
7315 return TRUE;
7316
7317 if (ibfd->xvec == obfd->xvec)
7318 {
cb3ff1e5
NC
7319 /* Check to see if any sections in the input BFD
7320 covered by ELF program header have changed. */
d55ce4e2 7321 Elf_Internal_Phdr *segment;
84d1d650
L
7322 asection *section, *osec;
7323 unsigned int i, num_segments;
7324 Elf_Internal_Shdr *this_hdr;
147d51c2
L
7325 const struct elf_backend_data *bed;
7326
7327 bed = get_elf_backend_data (ibfd);
7328
7329 /* Regenerate the segment map if p_paddr is set to 0. */
7330 if (bed->want_p_paddr_set_to_zero)
7331 goto rewrite;
84d1d650
L
7332
7333 /* Initialize the segment mark field. */
7334 for (section = obfd->sections; section != NULL;
7335 section = section->next)
7336 section->segment_mark = FALSE;
7337
7338 num_segments = elf_elfheader (ibfd)->e_phnum;
7339 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7340 i < num_segments;
7341 i++, segment++)
7342 {
5f6999aa
NC
7343 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7344 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7345 which severly confuses things, so always regenerate the segment
7346 map in this case. */
7347 if (segment->p_paddr == 0
7348 && segment->p_memsz == 0
7349 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
cb3ff1e5 7350 goto rewrite;
5f6999aa 7351
84d1d650
L
7352 for (section = ibfd->sections;
7353 section != NULL; section = section->next)
7354 {
7355 /* We mark the output section so that we know it comes
7356 from the input BFD. */
7357 osec = section->output_section;
7358 if (osec)
7359 osec->segment_mark = TRUE;
7360
7361 /* Check if this section is covered by the segment. */
7362 this_hdr = &(elf_section_data(section)->this_hdr);
f4638467 7363 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
84d1d650
L
7364 {
7365 /* FIXME: Check if its output section is changed or
7366 removed. What else do we need to check? */
7367 if (osec == NULL
7368 || section->flags != osec->flags
7369 || section->lma != osec->lma
7370 || section->vma != osec->vma
7371 || section->size != osec->size
7372 || section->rawsize != osec->rawsize
7373 || section->alignment_power != osec->alignment_power)
7374 goto rewrite;
7375 }
7376 }
7377 }
7378
cb3ff1e5 7379 /* Check to see if any output section do not come from the
84d1d650
L
7380 input BFD. */
7381 for (section = obfd->sections; section != NULL;
7382 section = section->next)
7383 {
535b785f 7384 if (!section->segment_mark)
84d1d650
L
7385 goto rewrite;
7386 else
7387 section->segment_mark = FALSE;
7388 }
7389
7390 return copy_elf_program_header (ibfd, obfd);
7391 }
7392
7393rewrite:
f1d85785
L
7394 if (ibfd->xvec == obfd->xvec)
7395 {
7396 /* When rewriting program header, set the output maxpagesize to
7397 the maximum alignment of input PT_LOAD segments. */
7398 Elf_Internal_Phdr *segment;
7399 unsigned int i;
7400 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
7401 bfd_vma maxpagesize = 0;
7402
7403 for (i = 0, segment = elf_tdata (ibfd)->phdr;
7404 i < num_segments;
7405 i++, segment++)
7406 if (segment->p_type == PT_LOAD
7407 && maxpagesize < segment->p_align)
c86934ce
NC
7408 {
7409 /* PR 17512: file: f17299af. */
7410 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
695344c0 7411 /* xgettext:c-format */
76cfced5
AM
7412 _bfd_error_handler (_("%B: warning: segment alignment of %#Lx"
7413 " is too large"),
7414 ibfd, segment->p_align);
c86934ce
NC
7415 else
7416 maxpagesize = segment->p_align;
7417 }
f1d85785
L
7418
7419 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
7420 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
7421 }
7422
84d1d650
L
7423 return rewrite_elf_program_header (ibfd, obfd);
7424}
7425
ccd2ec6a
L
7426/* Initialize private output section information from input section. */
7427
7428bfd_boolean
7429_bfd_elf_init_private_section_data (bfd *ibfd,
7430 asection *isec,
7431 bfd *obfd,
7432 asection *osec,
7433 struct bfd_link_info *link_info)
7434
7435{
7436 Elf_Internal_Shdr *ihdr, *ohdr;
0e1862bb
L
7437 bfd_boolean final_link = (link_info != NULL
7438 && !bfd_link_relocatable (link_info));
ccd2ec6a
L
7439
7440 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7441 || obfd->xvec->flavour != bfd_target_elf_flavour)
7442 return TRUE;
7443
ba85c43e
NC
7444 BFD_ASSERT (elf_section_data (osec) != NULL);
7445
dfa7b0b8
AM
7446 /* For objcopy and relocatable link, don't copy the output ELF
7447 section type from input if the output BFD section flags have been
7448 set to something different. For a final link allow some flags
7449 that the linker clears to differ. */
42bb2e33 7450 if (elf_section_type (osec) == SHT_NULL
dfa7b0b8
AM
7451 && (osec->flags == isec->flags
7452 || (final_link
7453 && ((osec->flags ^ isec->flags)
0814be7d 7454 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
42bb2e33 7455 elf_section_type (osec) = elf_section_type (isec);
d270463e
L
7456
7457 /* FIXME: Is this correct for all OS/PROC specific flags? */
7458 elf_section_flags (osec) |= (elf_section_flags (isec)
7459 & (SHF_MASKOS | SHF_MASKPROC));
ccd2ec6a 7460
a91e1603
L
7461 /* Copy sh_info from input for mbind section. */
7462 if (elf_section_flags (isec) & SHF_GNU_MBIND)
7463 elf_section_data (osec)->this_hdr.sh_info
7464 = elf_section_data (isec)->this_hdr.sh_info;
7465
ccd2ec6a
L
7466 /* Set things up for objcopy and relocatable link. The output
7467 SHT_GROUP section will have its elf_next_in_group pointing back
7468 to the input group members. Ignore linker created group section.
7469 See elfNN_ia64_object_p in elfxx-ia64.c. */
7bdf4127
AB
7470 if ((link_info == NULL
7471 || !link_info->resolve_section_groups)
7472 && (elf_sec_group (isec) == NULL
7473 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
ccd2ec6a 7474 {
7bdf4127
AB
7475 if (elf_section_flags (isec) & SHF_GROUP)
7476 elf_section_flags (osec) |= SHF_GROUP;
7477 elf_next_in_group (osec) = elf_next_in_group (isec);
7478 elf_section_data (osec)->group = elf_section_data (isec)->group;
ccd2ec6a
L
7479 }
7480
7bdf4127
AB
7481 /* If not decompress, preserve SHF_COMPRESSED. */
7482 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
7483 elf_section_flags (osec) |= (elf_section_flags (isec)
7484 & SHF_COMPRESSED);
7485
ccd2ec6a
L
7486 ihdr = &elf_section_data (isec)->this_hdr;
7487
7488 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7489 don't use the output section of the linked-to section since it
7490 may be NULL at this point. */
7491 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7492 {
7493 ohdr = &elf_section_data (osec)->this_hdr;
7494 ohdr->sh_flags |= SHF_LINK_ORDER;
7495 elf_linked_to_section (osec) = elf_linked_to_section (isec);
7496 }
7497
7498 osec->use_rela_p = isec->use_rela_p;
7499
7500 return TRUE;
7501}
7502
252b5132
RH
7503/* Copy private section information. This copies over the entsize
7504 field, and sometimes the info field. */
7505
b34976b6 7506bfd_boolean
217aa764
AM
7507_bfd_elf_copy_private_section_data (bfd *ibfd,
7508 asection *isec,
7509 bfd *obfd,
7510 asection *osec)
252b5132
RH
7511{
7512 Elf_Internal_Shdr *ihdr, *ohdr;
7513
7514 if (ibfd->xvec->flavour != bfd_target_elf_flavour
7515 || obfd->xvec->flavour != bfd_target_elf_flavour)
b34976b6 7516 return TRUE;
252b5132 7517
252b5132
RH
7518 ihdr = &elf_section_data (isec)->this_hdr;
7519 ohdr = &elf_section_data (osec)->this_hdr;
7520
7521 ohdr->sh_entsize = ihdr->sh_entsize;
7522
7523 if (ihdr->sh_type == SHT_SYMTAB
7524 || ihdr->sh_type == SHT_DYNSYM
7525 || ihdr->sh_type == SHT_GNU_verneed
7526 || ihdr->sh_type == SHT_GNU_verdef)
7527 ohdr->sh_info = ihdr->sh_info;
7528
ccd2ec6a
L
7529 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7530 NULL);
252b5132
RH
7531}
7532
d0bf826b
AM
7533/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7534 necessary if we are removing either the SHT_GROUP section or any of
7535 the group member sections. DISCARDED is the value that a section's
7536 output_section has if the section will be discarded, NULL when this
7537 function is called from objcopy, bfd_abs_section_ptr when called
7538 from the linker. */
80fccad2
BW
7539
7540bfd_boolean
d0bf826b 7541_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
80fccad2 7542{
30288845
AM
7543 asection *isec;
7544
30288845 7545 for (isec = ibfd->sections; isec != NULL; isec = isec->next)
415f38a6 7546 if (elf_section_type (isec) == SHT_GROUP)
30288845
AM
7547 {
7548 asection *first = elf_next_in_group (isec);
7549 asection *s = first;
d0bf826b
AM
7550 bfd_size_type removed = 0;
7551
30288845
AM
7552 while (s != NULL)
7553 {
415f38a6
AM
7554 /* If this member section is being output but the
7555 SHT_GROUP section is not, then clear the group info
7556 set up by _bfd_elf_copy_private_section_data. */
d0bf826b
AM
7557 if (s->output_section != discarded
7558 && isec->output_section == discarded)
30288845
AM
7559 {
7560 elf_section_flags (s->output_section) &= ~SHF_GROUP;
7561 elf_group_name (s->output_section) = NULL;
7562 }
415f38a6
AM
7563 /* Conversely, if the member section is not being output
7564 but the SHT_GROUP section is, then adjust its size. */
d0bf826b
AM
7565 else if (s->output_section == discarded
7566 && isec->output_section != discarded)
7567 removed += 4;
30288845
AM
7568 s = elf_next_in_group (s);
7569 if (s == first)
7570 break;
7571 }
d0bf826b
AM
7572 if (removed != 0)
7573 {
7574 if (discarded != NULL)
7575 {
7576 /* If we've been called for ld -r, then we need to
7577 adjust the input section size. This function may
7578 be called multiple times, so save the original
7579 size. */
7580 if (isec->rawsize == 0)
7581 isec->rawsize = isec->size;
7582 isec->size = isec->rawsize - removed;
7583 }
7584 else
7585 {
7586 /* Adjust the output section size when called from
7587 objcopy. */
7588 isec->output_section->size -= removed;
7589 }
7590 }
30288845
AM
7591 }
7592
80fccad2
BW
7593 return TRUE;
7594}
7595
d0bf826b
AM
7596/* Copy private header information. */
7597
7598bfd_boolean
7599_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7600{
7601 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7602 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7603 return TRUE;
7604
7605 /* Copy over private BFD data if it has not already been copied.
7606 This must be done here, rather than in the copy_private_bfd_data
7607 entry point, because the latter is called after the section
7608 contents have been set, which means that the program headers have
7609 already been worked out. */
12bd6957 7610 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
d0bf826b
AM
7611 {
7612 if (! copy_private_bfd_data (ibfd, obfd))
7613 return FALSE;
7614 }
7615
7616 return _bfd_elf_fixup_group_sections (ibfd, NULL);
7617}
7618
252b5132
RH
7619/* Copy private symbol information. If this symbol is in a section
7620 which we did not map into a BFD section, try to map the section
7621 index correctly. We use special macro definitions for the mapped
7622 section indices; these definitions are interpreted by the
7623 swap_out_syms function. */
7624
9ad5cbcf
AM
7625#define MAP_ONESYMTAB (SHN_HIOS + 1)
7626#define MAP_DYNSYMTAB (SHN_HIOS + 2)
7627#define MAP_STRTAB (SHN_HIOS + 3)
7628#define MAP_SHSTRTAB (SHN_HIOS + 4)
7629#define MAP_SYM_SHNDX (SHN_HIOS + 5)
252b5132 7630
b34976b6 7631bfd_boolean
217aa764
AM
7632_bfd_elf_copy_private_symbol_data (bfd *ibfd,
7633 asymbol *isymarg,
7634 bfd *obfd,
7635 asymbol *osymarg)
252b5132
RH
7636{
7637 elf_symbol_type *isym, *osym;
7638
7639 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7640 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 7641 return TRUE;
252b5132
RH
7642
7643 isym = elf_symbol_from (ibfd, isymarg);
7644 osym = elf_symbol_from (obfd, osymarg);
7645
7646 if (isym != NULL
8424d8f5 7647 && isym->internal_elf_sym.st_shndx != 0
252b5132
RH
7648 && osym != NULL
7649 && bfd_is_abs_section (isym->symbol.section))
7650 {
7651 unsigned int shndx;
7652
7653 shndx = isym->internal_elf_sym.st_shndx;
7654 if (shndx == elf_onesymtab (ibfd))
7655 shndx = MAP_ONESYMTAB;
7656 else if (shndx == elf_dynsymtab (ibfd))
7657 shndx = MAP_DYNSYMTAB;
12bd6957 7658 else if (shndx == elf_strtab_sec (ibfd))
252b5132 7659 shndx = MAP_STRTAB;
12bd6957 7660 else if (shndx == elf_shstrtab_sec (ibfd))
252b5132 7661 shndx = MAP_SHSTRTAB;
6a40cf0c 7662 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
9ad5cbcf 7663 shndx = MAP_SYM_SHNDX;
252b5132
RH
7664 osym->internal_elf_sym.st_shndx = shndx;
7665 }
7666
b34976b6 7667 return TRUE;
252b5132
RH
7668}
7669
7670/* Swap out the symbols. */
7671
b34976b6 7672static bfd_boolean
217aa764 7673swap_out_syms (bfd *abfd,
ef10c3ac 7674 struct elf_strtab_hash **sttp,
217aa764 7675 int relocatable_p)
252b5132 7676{
9c5bfbb7 7677 const struct elf_backend_data *bed;
079e9a2f
AM
7678 int symcount;
7679 asymbol **syms;
ef10c3ac 7680 struct elf_strtab_hash *stt;
079e9a2f 7681 Elf_Internal_Shdr *symtab_hdr;
9ad5cbcf 7682 Elf_Internal_Shdr *symtab_shndx_hdr;
079e9a2f 7683 Elf_Internal_Shdr *symstrtab_hdr;
ef10c3ac 7684 struct elf_sym_strtab *symstrtab;
f075ee0c
AM
7685 bfd_byte *outbound_syms;
7686 bfd_byte *outbound_shndx;
ef10c3ac
L
7687 unsigned long outbound_syms_index;
7688 unsigned long outbound_shndx_index;
079e9a2f 7689 int idx;
12bd6957 7690 unsigned int num_locals;
079e9a2f 7691 bfd_size_type amt;
174fd7f9 7692 bfd_boolean name_local_sections;
252b5132 7693
12bd6957 7694 if (!elf_map_symbols (abfd, &num_locals))
b34976b6 7695 return FALSE;
252b5132 7696
c044fabd 7697 /* Dump out the symtabs. */
ef10c3ac 7698 stt = _bfd_elf_strtab_init ();
079e9a2f 7699 if (stt == NULL)
b34976b6 7700 return FALSE;
252b5132 7701
079e9a2f
AM
7702 bed = get_elf_backend_data (abfd);
7703 symcount = bfd_get_symcount (abfd);
7704 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7705 symtab_hdr->sh_type = SHT_SYMTAB;
7706 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7707 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
12bd6957 7708 symtab_hdr->sh_info = num_locals + 1;
72de5009 7709 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
079e9a2f
AM
7710
7711 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7712 symstrtab_hdr->sh_type = SHT_STRTAB;
7713
ef10c3ac
L
7714 /* Allocate buffer to swap out the .strtab section. */
7715 symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
7716 * sizeof (*symstrtab));
7717 if (symstrtab == NULL)
7718 {
7719 _bfd_elf_strtab_free (stt);
7720 return FALSE;
7721 }
7722
a50b1753 7723 outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
07d6d2b8 7724 bed->s->sizeof_sym);
079e9a2f 7725 if (outbound_syms == NULL)
5ed6aba4 7726 {
ef10c3ac
L
7727error_return:
7728 _bfd_elf_strtab_free (stt);
7729 free (symstrtab);
5ed6aba4
NC
7730 return FALSE;
7731 }
217aa764 7732 symtab_hdr->contents = outbound_syms;
ef10c3ac 7733 outbound_syms_index = 0;
252b5132 7734
9ad5cbcf 7735 outbound_shndx = NULL;
ef10c3ac 7736 outbound_shndx_index = 0;
6a40cf0c
NC
7737
7738 if (elf_symtab_shndx_list (abfd))
9ad5cbcf 7739 {
6a40cf0c
NC
7740 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
7741 if (symtab_shndx_hdr->sh_name != 0)
7742 {
7743 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7744 outbound_shndx = (bfd_byte *)
7745 bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7746 if (outbound_shndx == NULL)
7747 goto error_return;
5ed6aba4 7748
6a40cf0c
NC
7749 symtab_shndx_hdr->contents = outbound_shndx;
7750 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7751 symtab_shndx_hdr->sh_size = amt;
7752 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7753 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7754 }
7755 /* FIXME: What about any other headers in the list ? */
9ad5cbcf
AM
7756 }
7757
589e6347 7758 /* Now generate the data (for "contents"). */
079e9a2f
AM
7759 {
7760 /* Fill in zeroth symbol and swap it out. */
7761 Elf_Internal_Sym sym;
7762 sym.st_name = 0;
7763 sym.st_value = 0;
7764 sym.st_size = 0;
7765 sym.st_info = 0;
7766 sym.st_other = 0;
7767 sym.st_shndx = SHN_UNDEF;
35fc36a8 7768 sym.st_target_internal = 0;
ef10c3ac
L
7769 symstrtab[0].sym = sym;
7770 symstrtab[0].dest_index = outbound_syms_index;
7771 symstrtab[0].destshndx_index = outbound_shndx_index;
7772 outbound_syms_index++;
9ad5cbcf 7773 if (outbound_shndx != NULL)
ef10c3ac 7774 outbound_shndx_index++;
079e9a2f 7775 }
252b5132 7776
174fd7f9
RS
7777 name_local_sections
7778 = (bed->elf_backend_name_local_section_symbols
7779 && bed->elf_backend_name_local_section_symbols (abfd));
7780
079e9a2f 7781 syms = bfd_get_outsymbols (abfd);
ef10c3ac 7782 for (idx = 0; idx < symcount;)
252b5132 7783 {
252b5132 7784 Elf_Internal_Sym sym;
079e9a2f
AM
7785 bfd_vma value = syms[idx]->value;
7786 elf_symbol_type *type_ptr;
7787 flagword flags = syms[idx]->flags;
7788 int type;
252b5132 7789
174fd7f9
RS
7790 if (!name_local_sections
7791 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
079e9a2f
AM
7792 {
7793 /* Local section symbols have no name. */
ef10c3ac 7794 sym.st_name = (unsigned long) -1;
079e9a2f
AM
7795 }
7796 else
7797 {
ef10c3ac
L
7798 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7799 to get the final offset for st_name. */
7800 sym.st_name
7801 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
7802 FALSE);
079e9a2f 7803 if (sym.st_name == (unsigned long) -1)
ef10c3ac 7804 goto error_return;
079e9a2f 7805 }
252b5132 7806
079e9a2f 7807 type_ptr = elf_symbol_from (abfd, syms[idx]);
252b5132 7808
079e9a2f
AM
7809 if ((flags & BSF_SECTION_SYM) == 0
7810 && bfd_is_com_section (syms[idx]->section))
7811 {
7812 /* ELF common symbols put the alignment into the `value' field,
7813 and the size into the `size' field. This is backwards from
7814 how BFD handles it, so reverse it here. */
7815 sym.st_size = value;
7816 if (type_ptr == NULL
7817 || type_ptr->internal_elf_sym.st_value == 0)
7818 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
7819 else
7820 sym.st_value = type_ptr->internal_elf_sym.st_value;
7821 sym.st_shndx = _bfd_elf_section_from_bfd_section
7822 (abfd, syms[idx]->section);
7823 }
7824 else
7825 {
7826 asection *sec = syms[idx]->section;
cb33740c 7827 unsigned int shndx;
252b5132 7828
079e9a2f
AM
7829 if (sec->output_section)
7830 {
7831 value += sec->output_offset;
7832 sec = sec->output_section;
7833 }
589e6347 7834
079e9a2f
AM
7835 /* Don't add in the section vma for relocatable output. */
7836 if (! relocatable_p)
7837 value += sec->vma;
7838 sym.st_value = value;
7839 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
7840
7841 if (bfd_is_abs_section (sec)
7842 && type_ptr != NULL
7843 && type_ptr->internal_elf_sym.st_shndx != 0)
7844 {
7845 /* This symbol is in a real ELF section which we did
7846 not create as a BFD section. Undo the mapping done
7847 by copy_private_symbol_data. */
7848 shndx = type_ptr->internal_elf_sym.st_shndx;
7849 switch (shndx)
7850 {
7851 case MAP_ONESYMTAB:
7852 shndx = elf_onesymtab (abfd);
7853 break;
7854 case MAP_DYNSYMTAB:
7855 shndx = elf_dynsymtab (abfd);
7856 break;
7857 case MAP_STRTAB:
12bd6957 7858 shndx = elf_strtab_sec (abfd);
079e9a2f
AM
7859 break;
7860 case MAP_SHSTRTAB:
12bd6957 7861 shndx = elf_shstrtab_sec (abfd);
079e9a2f 7862 break;
9ad5cbcf 7863 case MAP_SYM_SHNDX:
6a40cf0c
NC
7864 if (elf_symtab_shndx_list (abfd))
7865 shndx = elf_symtab_shndx_list (abfd)->ndx;
9ad5cbcf 7866 break;
079e9a2f 7867 default:
15bc576a 7868 shndx = SHN_ABS;
079e9a2f
AM
7869 break;
7870 }
7871 }
7872 else
7873 {
7874 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
252b5132 7875
cb33740c 7876 if (shndx == SHN_BAD)
079e9a2f
AM
7877 {
7878 asection *sec2;
7879
7880 /* Writing this would be a hell of a lot easier if
7881 we had some decent documentation on bfd, and
7882 knew what to expect of the library, and what to
7883 demand of applications. For example, it
7884 appears that `objcopy' might not set the
7885 section of a symbol to be a section that is
7886 actually in the output file. */
7887 sec2 = bfd_get_section_by_name (abfd, sec->name);
5df1bc57
AM
7888 if (sec2 != NULL)
7889 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
7890 if (shndx == SHN_BAD)
589e6347 7891 {
695344c0 7892 /* xgettext:c-format */
589e6347
NC
7893 _bfd_error_handler (_("\
7894Unable to find equivalent output section for symbol '%s' from section '%s'"),
7895 syms[idx]->name ? syms[idx]->name : "<Local sym>",
7896 sec->name);
811072d8 7897 bfd_set_error (bfd_error_invalid_operation);
ef10c3ac 7898 goto error_return;
589e6347 7899 }
079e9a2f
AM
7900 }
7901 }
252b5132 7902
079e9a2f
AM
7903 sym.st_shndx = shndx;
7904 }
252b5132 7905
13ae64f3
JJ
7906 if ((flags & BSF_THREAD_LOCAL) != 0)
7907 type = STT_TLS;
d8045f23
NC
7908 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
7909 type = STT_GNU_IFUNC;
13ae64f3 7910 else if ((flags & BSF_FUNCTION) != 0)
079e9a2f
AM
7911 type = STT_FUNC;
7912 else if ((flags & BSF_OBJECT) != 0)
7913 type = STT_OBJECT;
d9352518
DB
7914 else if ((flags & BSF_RELC) != 0)
7915 type = STT_RELC;
7916 else if ((flags & BSF_SRELC) != 0)
7917 type = STT_SRELC;
079e9a2f
AM
7918 else
7919 type = STT_NOTYPE;
252b5132 7920
13ae64f3
JJ
7921 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7922 type = STT_TLS;
7923
589e6347 7924 /* Processor-specific types. */
079e9a2f
AM
7925 if (type_ptr != NULL
7926 && bed->elf_backend_get_symbol_type)
7927 type = ((*bed->elf_backend_get_symbol_type)
7928 (&type_ptr->internal_elf_sym, type));
252b5132 7929
079e9a2f
AM
7930 if (flags & BSF_SECTION_SYM)
7931 {
7932 if (flags & BSF_GLOBAL)
7933 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7934 else
7935 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7936 }
7937 else if (bfd_is_com_section (syms[idx]->section))
0a40daed 7938 {
b8871f35
L
7939 if (type != STT_TLS)
7940 {
7941 if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
7942 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
7943 ? STT_COMMON : STT_OBJECT);
7944 else
7945 type = ((flags & BSF_ELF_COMMON) != 0
7946 ? STT_COMMON : STT_OBJECT);
7947 }
7948 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
0a40daed 7949 }
079e9a2f
AM
7950 else if (bfd_is_und_section (syms[idx]->section))
7951 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7952 ? STB_WEAK
7953 : STB_GLOBAL),
7954 type);
7955 else if (flags & BSF_FILE)
7956 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7957 else
7958 {
7959 int bind = STB_LOCAL;
252b5132 7960
079e9a2f
AM
7961 if (flags & BSF_LOCAL)
7962 bind = STB_LOCAL;
3e7a7d11
NC
7963 else if (flags & BSF_GNU_UNIQUE)
7964 bind = STB_GNU_UNIQUE;
079e9a2f
AM
7965 else if (flags & BSF_WEAK)
7966 bind = STB_WEAK;
7967 else if (flags & BSF_GLOBAL)
7968 bind = STB_GLOBAL;
252b5132 7969
079e9a2f
AM
7970 sym.st_info = ELF_ST_INFO (bind, type);
7971 }
252b5132 7972
079e9a2f 7973 if (type_ptr != NULL)
35fc36a8
RS
7974 {
7975 sym.st_other = type_ptr->internal_elf_sym.st_other;
7976 sym.st_target_internal
7977 = type_ptr->internal_elf_sym.st_target_internal;
7978 }
079e9a2f 7979 else
35fc36a8
RS
7980 {
7981 sym.st_other = 0;
7982 sym.st_target_internal = 0;
7983 }
252b5132 7984
ef10c3ac
L
7985 idx++;
7986 symstrtab[idx].sym = sym;
7987 symstrtab[idx].dest_index = outbound_syms_index;
7988 symstrtab[idx].destshndx_index = outbound_shndx_index;
7989
7990 outbound_syms_index++;
9ad5cbcf 7991 if (outbound_shndx != NULL)
ef10c3ac
L
7992 outbound_shndx_index++;
7993 }
7994
7995 /* Finalize the .strtab section. */
7996 _bfd_elf_strtab_finalize (stt);
7997
7998 /* Swap out the .strtab section. */
7999 for (idx = 0; idx <= symcount; idx++)
8000 {
8001 struct elf_sym_strtab *elfsym = &symstrtab[idx];
8002 if (elfsym->sym.st_name == (unsigned long) -1)
8003 elfsym->sym.st_name = 0;
8004 else
8005 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
8006 elfsym->sym.st_name);
8007 bed->s->swap_symbol_out (abfd, &elfsym->sym,
8008 (outbound_syms
8009 + (elfsym->dest_index
8010 * bed->s->sizeof_sym)),
8011 (outbound_shndx
8012 + (elfsym->destshndx_index
8013 * sizeof (Elf_External_Sym_Shndx))));
079e9a2f 8014 }
ef10c3ac 8015 free (symstrtab);
252b5132 8016
079e9a2f 8017 *sttp = stt;
ef10c3ac 8018 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
079e9a2f 8019 symstrtab_hdr->sh_type = SHT_STRTAB;
84865015 8020 symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
079e9a2f
AM
8021 symstrtab_hdr->sh_addr = 0;
8022 symstrtab_hdr->sh_entsize = 0;
8023 symstrtab_hdr->sh_link = 0;
8024 symstrtab_hdr->sh_info = 0;
8025 symstrtab_hdr->sh_addralign = 1;
252b5132 8026
b34976b6 8027 return TRUE;
252b5132
RH
8028}
8029
8030/* Return the number of bytes required to hold the symtab vector.
8031
8032 Note that we base it on the count plus 1, since we will null terminate
8033 the vector allocated based on this size. However, the ELF symbol table
8034 always has a dummy entry as symbol #0, so it ends up even. */
8035
8036long
217aa764 8037_bfd_elf_get_symtab_upper_bound (bfd *abfd)
252b5132
RH
8038{
8039 long symcount;
8040 long symtab_size;
8041 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
8042
8043 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
8044 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8045 if (symcount > 0)
8046 symtab_size -= sizeof (asymbol *);
252b5132
RH
8047
8048 return symtab_size;
8049}
8050
8051long
217aa764 8052_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
252b5132
RH
8053{
8054 long symcount;
8055 long symtab_size;
8056 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
8057
8058 if (elf_dynsymtab (abfd) == 0)
8059 {
8060 bfd_set_error (bfd_error_invalid_operation);
8061 return -1;
8062 }
8063
8064 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
b99d1833
AM
8065 symtab_size = (symcount + 1) * (sizeof (asymbol *));
8066 if (symcount > 0)
8067 symtab_size -= sizeof (asymbol *);
252b5132
RH
8068
8069 return symtab_size;
8070}
8071
8072long
217aa764
AM
8073_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
8074 sec_ptr asect)
252b5132
RH
8075{
8076 return (asect->reloc_count + 1) * sizeof (arelent *);
8077}
8078
8079/* Canonicalize the relocs. */
8080
8081long
217aa764
AM
8082_bfd_elf_canonicalize_reloc (bfd *abfd,
8083 sec_ptr section,
8084 arelent **relptr,
8085 asymbol **symbols)
252b5132
RH
8086{
8087 arelent *tblptr;
8088 unsigned int i;
9c5bfbb7 8089 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
252b5132 8090
b34976b6 8091 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
252b5132
RH
8092 return -1;
8093
8094 tblptr = section->relocation;
8095 for (i = 0; i < section->reloc_count; i++)
8096 *relptr++ = tblptr++;
8097
8098 *relptr = NULL;
8099
8100 return section->reloc_count;
8101}
8102
8103long
6cee3f79 8104_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
252b5132 8105{
9c5bfbb7 8106 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8107 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
252b5132
RH
8108
8109 if (symcount >= 0)
8110 bfd_get_symcount (abfd) = symcount;
8111 return symcount;
8112}
8113
8114long
217aa764
AM
8115_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
8116 asymbol **allocation)
252b5132 8117{
9c5bfbb7 8118 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
217aa764 8119 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
1f70368c
DJ
8120
8121 if (symcount >= 0)
8122 bfd_get_dynamic_symcount (abfd) = symcount;
8123 return symcount;
252b5132
RH
8124}
8125
8615f3f2
AM
8126/* Return the size required for the dynamic reloc entries. Any loadable
8127 section that was actually installed in the BFD, and has type SHT_REL
8128 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8129 dynamic reloc section. */
252b5132
RH
8130
8131long
217aa764 8132_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
252b5132
RH
8133{
8134 long ret;
8135 asection *s;
8136
8137 if (elf_dynsymtab (abfd) == 0)
8138 {
8139 bfd_set_error (bfd_error_invalid_operation);
8140 return -1;
8141 }
8142
8143 ret = sizeof (arelent *);
8144 for (s = abfd->sections; s != NULL; s = s->next)
266b05cf 8145 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8146 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8147 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
eea6121a 8148 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
252b5132
RH
8149 * sizeof (arelent *));
8150
8151 return ret;
8152}
8153
8615f3f2
AM
8154/* Canonicalize the dynamic relocation entries. Note that we return the
8155 dynamic relocations as a single block, although they are actually
8156 associated with particular sections; the interface, which was
8157 designed for SunOS style shared libraries, expects that there is only
8158 one set of dynamic relocs. Any loadable section that was actually
8159 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8160 dynamic symbol table, is considered to be a dynamic reloc section. */
252b5132
RH
8161
8162long
217aa764
AM
8163_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
8164 arelent **storage,
8165 asymbol **syms)
252b5132 8166{
217aa764 8167 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
252b5132
RH
8168 asection *s;
8169 long ret;
8170
8171 if (elf_dynsymtab (abfd) == 0)
8172 {
8173 bfd_set_error (bfd_error_invalid_operation);
8174 return -1;
8175 }
8176
8177 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
8178 ret = 0;
8179 for (s = abfd->sections; s != NULL; s = s->next)
8180 {
266b05cf 8181 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
252b5132
RH
8182 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
8183 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
8184 {
8185 arelent *p;
8186 long count, i;
8187
b34976b6 8188 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
252b5132 8189 return -1;
eea6121a 8190 count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
252b5132
RH
8191 p = s->relocation;
8192 for (i = 0; i < count; i++)
8193 *storage++ = p++;
8194 ret += count;
8195 }
8196 }
8197
8198 *storage = NULL;
8199
8200 return ret;
8201}
8202\f
8203/* Read in the version information. */
8204
b34976b6 8205bfd_boolean
fc0e6df6 8206_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
252b5132
RH
8207{
8208 bfd_byte *contents = NULL;
fc0e6df6
PB
8209 unsigned int freeidx = 0;
8210
8211 if (elf_dynverref (abfd) != 0)
8212 {
8213 Elf_Internal_Shdr *hdr;
8214 Elf_External_Verneed *everneed;
8215 Elf_Internal_Verneed *iverneed;
8216 unsigned int i;
d0fb9a8d 8217 bfd_byte *contents_end;
fc0e6df6
PB
8218
8219 hdr = &elf_tdata (abfd)->dynverref_hdr;
8220
bd61e135
AM
8221 if (hdr->sh_info == 0
8222 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
d0fb9a8d 8223 {
601a03ba 8224error_return_bad_verref:
4eca0228 8225 _bfd_error_handler
601a03ba
AM
8226 (_("%B: .gnu.version_r invalid entry"), abfd);
8227 bfd_set_error (bfd_error_bad_value);
d0fb9a8d
JJ
8228error_return_verref:
8229 elf_tdata (abfd)->verref = NULL;
8230 elf_tdata (abfd)->cverrefs = 0;
8231 goto error_return;
8232 }
601a03ba
AM
8233
8234 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
8235 if (contents == NULL)
8236 goto error_return_verref;
8237
fc0e6df6
PB
8238 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
8239 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
d0fb9a8d 8240 goto error_return_verref;
fc0e6df6 8241
601a03ba 8242 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
bd61e135 8243 bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
601a03ba
AM
8244
8245 if (elf_tdata (abfd)->verref == NULL)
d0fb9a8d
JJ
8246 goto error_return_verref;
8247
8248 BFD_ASSERT (sizeof (Elf_External_Verneed)
8249 == sizeof (Elf_External_Vernaux));
8250 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
fc0e6df6
PB
8251 everneed = (Elf_External_Verneed *) contents;
8252 iverneed = elf_tdata (abfd)->verref;
8253 for (i = 0; i < hdr->sh_info; i++, iverneed++)
8254 {
8255 Elf_External_Vernaux *evernaux;
8256 Elf_Internal_Vernaux *ivernaux;
8257 unsigned int j;
8258
8259 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
8260
8261 iverneed->vn_bfd = abfd;
8262
8263 iverneed->vn_filename =
8264 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8265 iverneed->vn_file);
8266 if (iverneed->vn_filename == NULL)
601a03ba 8267 goto error_return_bad_verref;
fc0e6df6 8268
d0fb9a8d
JJ
8269 if (iverneed->vn_cnt == 0)
8270 iverneed->vn_auxptr = NULL;
8271 else
8272 {
a50b1753 8273 iverneed->vn_auxptr = (struct elf_internal_vernaux *)
07d6d2b8
AM
8274 bfd_alloc2 (abfd, iverneed->vn_cnt,
8275 sizeof (Elf_Internal_Vernaux));
d0fb9a8d
JJ
8276 if (iverneed->vn_auxptr == NULL)
8277 goto error_return_verref;
8278 }
8279
8280 if (iverneed->vn_aux
8281 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8282 goto error_return_bad_verref;
fc0e6df6
PB
8283
8284 evernaux = ((Elf_External_Vernaux *)
8285 ((bfd_byte *) everneed + iverneed->vn_aux));
8286 ivernaux = iverneed->vn_auxptr;
8287 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
8288 {
8289 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
8290
8291 ivernaux->vna_nodename =
8292 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8293 ivernaux->vna_name);
8294 if (ivernaux->vna_nodename == NULL)
601a03ba 8295 goto error_return_bad_verref;
fc0e6df6 8296
25ff461f
AM
8297 if (ivernaux->vna_other > freeidx)
8298 freeidx = ivernaux->vna_other;
8299
8300 ivernaux->vna_nextptr = NULL;
8301 if (ivernaux->vna_next == 0)
8302 {
8303 iverneed->vn_cnt = j + 1;
8304 break;
8305 }
fc0e6df6
PB
8306 if (j + 1 < iverneed->vn_cnt)
8307 ivernaux->vna_nextptr = ivernaux + 1;
fc0e6df6 8308
d0fb9a8d
JJ
8309 if (ivernaux->vna_next
8310 > (size_t) (contents_end - (bfd_byte *) evernaux))
601a03ba 8311 goto error_return_bad_verref;
d0fb9a8d 8312
fc0e6df6
PB
8313 evernaux = ((Elf_External_Vernaux *)
8314 ((bfd_byte *) evernaux + ivernaux->vna_next));
fc0e6df6
PB
8315 }
8316
25ff461f
AM
8317 iverneed->vn_nextref = NULL;
8318 if (iverneed->vn_next == 0)
8319 break;
fc0e6df6
PB
8320 if (i + 1 < hdr->sh_info)
8321 iverneed->vn_nextref = iverneed + 1;
fc0e6df6 8322
d0fb9a8d
JJ
8323 if (iverneed->vn_next
8324 > (size_t) (contents_end - (bfd_byte *) everneed))
601a03ba 8325 goto error_return_bad_verref;
d0fb9a8d 8326
fc0e6df6
PB
8327 everneed = ((Elf_External_Verneed *)
8328 ((bfd_byte *) everneed + iverneed->vn_next));
8329 }
25ff461f 8330 elf_tdata (abfd)->cverrefs = i;
fc0e6df6
PB
8331
8332 free (contents);
8333 contents = NULL;
8334 }
252b5132
RH
8335
8336 if (elf_dynverdef (abfd) != 0)
8337 {
8338 Elf_Internal_Shdr *hdr;
8339 Elf_External_Verdef *everdef;
8340 Elf_Internal_Verdef *iverdef;
f631889e
UD
8341 Elf_Internal_Verdef *iverdefarr;
8342 Elf_Internal_Verdef iverdefmem;
252b5132 8343 unsigned int i;
062e2358 8344 unsigned int maxidx;
d0fb9a8d 8345 bfd_byte *contents_end_def, *contents_end_aux;
252b5132
RH
8346
8347 hdr = &elf_tdata (abfd)->dynverdef_hdr;
8348
601a03ba
AM
8349 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
8350 {
8351 error_return_bad_verdef:
4eca0228 8352 _bfd_error_handler
601a03ba
AM
8353 (_("%B: .gnu.version_d invalid entry"), abfd);
8354 bfd_set_error (bfd_error_bad_value);
8355 error_return_verdef:
8356 elf_tdata (abfd)->verdef = NULL;
8357 elf_tdata (abfd)->cverdefs = 0;
8358 goto error_return;
8359 }
8360
a50b1753 8361 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
252b5132 8362 if (contents == NULL)
601a03ba 8363 goto error_return_verdef;
252b5132 8364 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
217aa764 8365 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
601a03ba 8366 goto error_return_verdef;
d0fb9a8d
JJ
8367
8368 BFD_ASSERT (sizeof (Elf_External_Verdef)
8369 >= sizeof (Elf_External_Verdaux));
8370 contents_end_def = contents + hdr->sh_size
8371 - sizeof (Elf_External_Verdef);
8372 contents_end_aux = contents + hdr->sh_size
8373 - sizeof (Elf_External_Verdaux);
8374
f631889e
UD
8375 /* We know the number of entries in the section but not the maximum
8376 index. Therefore we have to run through all entries and find
8377 the maximum. */
252b5132 8378 everdef = (Elf_External_Verdef *) contents;
f631889e
UD
8379 maxidx = 0;
8380 for (i = 0; i < hdr->sh_info; ++i)
8381 {
8382 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8383
601a03ba
AM
8384 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
8385 goto error_return_bad_verdef;
062e2358
AM
8386 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
8387 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
f631889e 8388
25ff461f
AM
8389 if (iverdefmem.vd_next == 0)
8390 break;
8391
d0fb9a8d
JJ
8392 if (iverdefmem.vd_next
8393 > (size_t) (contents_end_def - (bfd_byte *) everdef))
601a03ba 8394 goto error_return_bad_verdef;
d0fb9a8d 8395
f631889e
UD
8396 everdef = ((Elf_External_Verdef *)
8397 ((bfd_byte *) everdef + iverdefmem.vd_next));
8398 }
8399
fc0e6df6
PB
8400 if (default_imported_symver)
8401 {
8402 if (freeidx > maxidx)
8403 maxidx = ++freeidx;
8404 else
8405 freeidx = ++maxidx;
8406 }
201159ec 8407
601a03ba
AM
8408 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8409 bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
f631889e 8410 if (elf_tdata (abfd)->verdef == NULL)
601a03ba 8411 goto error_return_verdef;
f631889e
UD
8412
8413 elf_tdata (abfd)->cverdefs = maxidx;
8414
8415 everdef = (Elf_External_Verdef *) contents;
8416 iverdefarr = elf_tdata (abfd)->verdef;
8417 for (i = 0; i < hdr->sh_info; i++)
252b5132
RH
8418 {
8419 Elf_External_Verdaux *everdaux;
8420 Elf_Internal_Verdaux *iverdaux;
8421 unsigned int j;
8422
f631889e
UD
8423 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
8424
d0fb9a8d 8425 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
601a03ba 8426 goto error_return_bad_verdef;
d0fb9a8d 8427
f631889e 8428 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
595bce75 8429 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
252b5132
RH
8430
8431 iverdef->vd_bfd = abfd;
8432
d0fb9a8d
JJ
8433 if (iverdef->vd_cnt == 0)
8434 iverdef->vd_auxptr = NULL;
8435 else
8436 {
a50b1753 8437 iverdef->vd_auxptr = (struct elf_internal_verdaux *)
07d6d2b8
AM
8438 bfd_alloc2 (abfd, iverdef->vd_cnt,
8439 sizeof (Elf_Internal_Verdaux));
d0fb9a8d
JJ
8440 if (iverdef->vd_auxptr == NULL)
8441 goto error_return_verdef;
8442 }
8443
8444 if (iverdef->vd_aux
8445 > (size_t) (contents_end_aux - (bfd_byte *) everdef))
601a03ba 8446 goto error_return_bad_verdef;
252b5132
RH
8447
8448 everdaux = ((Elf_External_Verdaux *)
8449 ((bfd_byte *) everdef + iverdef->vd_aux));
8450 iverdaux = iverdef->vd_auxptr;
8451 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
8452 {
8453 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
8454
8455 iverdaux->vda_nodename =
8456 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
8457 iverdaux->vda_name);
8458 if (iverdaux->vda_nodename == NULL)
601a03ba 8459 goto error_return_bad_verdef;
252b5132 8460
25ff461f
AM
8461 iverdaux->vda_nextptr = NULL;
8462 if (iverdaux->vda_next == 0)
8463 {
8464 iverdef->vd_cnt = j + 1;
8465 break;
8466 }
252b5132
RH
8467 if (j + 1 < iverdef->vd_cnt)
8468 iverdaux->vda_nextptr = iverdaux + 1;
252b5132 8469
d0fb9a8d
JJ
8470 if (iverdaux->vda_next
8471 > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
601a03ba 8472 goto error_return_bad_verdef;
d0fb9a8d 8473
252b5132
RH
8474 everdaux = ((Elf_External_Verdaux *)
8475 ((bfd_byte *) everdaux + iverdaux->vda_next));
8476 }
8477
595bce75 8478 iverdef->vd_nodename = NULL;
d0fb9a8d
JJ
8479 if (iverdef->vd_cnt)
8480 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
252b5132 8481
25ff461f
AM
8482 iverdef->vd_nextdef = NULL;
8483 if (iverdef->vd_next == 0)
8484 break;
d0fb9a8d 8485 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
252b5132 8486 iverdef->vd_nextdef = iverdef + 1;
252b5132
RH
8487
8488 everdef = ((Elf_External_Verdef *)
8489 ((bfd_byte *) everdef + iverdef->vd_next));
8490 }
8491
8492 free (contents);
8493 contents = NULL;
8494 }
fc0e6df6 8495 else if (default_imported_symver)
252b5132 8496 {
fc0e6df6
PB
8497 if (freeidx < 3)
8498 freeidx = 3;
8499 else
8500 freeidx++;
252b5132 8501
a50b1753 8502 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
07d6d2b8 8503 bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
fc0e6df6 8504 if (elf_tdata (abfd)->verdef == NULL)
252b5132
RH
8505 goto error_return;
8506
fc0e6df6
PB
8507 elf_tdata (abfd)->cverdefs = freeidx;
8508 }
252b5132 8509
fc0e6df6
PB
8510 /* Create a default version based on the soname. */
8511 if (default_imported_symver)
8512 {
8513 Elf_Internal_Verdef *iverdef;
8514 Elf_Internal_Verdaux *iverdaux;
252b5132 8515
5bb3703f 8516 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
252b5132 8517
fc0e6df6
PB
8518 iverdef->vd_version = VER_DEF_CURRENT;
8519 iverdef->vd_flags = 0;
8520 iverdef->vd_ndx = freeidx;
8521 iverdef->vd_cnt = 1;
252b5132 8522
fc0e6df6 8523 iverdef->vd_bfd = abfd;
252b5132 8524
fc0e6df6
PB
8525 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8526 if (iverdef->vd_nodename == NULL)
d0fb9a8d 8527 goto error_return_verdef;
fc0e6df6 8528 iverdef->vd_nextdef = NULL;
601a03ba
AM
8529 iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
8530 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
d0fb9a8d
JJ
8531 if (iverdef->vd_auxptr == NULL)
8532 goto error_return_verdef;
252b5132 8533
fc0e6df6
PB
8534 iverdaux = iverdef->vd_auxptr;
8535 iverdaux->vda_nodename = iverdef->vd_nodename;
252b5132
RH
8536 }
8537
b34976b6 8538 return TRUE;
252b5132
RH
8539
8540 error_return:
5ed6aba4 8541 if (contents != NULL)
252b5132 8542 free (contents);
b34976b6 8543 return FALSE;
252b5132
RH
8544}
8545\f
8546asymbol *
217aa764 8547_bfd_elf_make_empty_symbol (bfd *abfd)
252b5132
RH
8548{
8549 elf_symbol_type *newsym;
8550
201159ec 8551 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
252b5132
RH
8552 if (!newsym)
8553 return NULL;
201159ec
NC
8554 newsym->symbol.the_bfd = abfd;
8555 return &newsym->symbol;
252b5132
RH
8556}
8557
8558void
217aa764
AM
8559_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8560 asymbol *symbol,
8561 symbol_info *ret)
252b5132
RH
8562{
8563 bfd_symbol_info (symbol, ret);
8564}
8565
8566/* Return whether a symbol name implies a local symbol. Most targets
8567 use this function for the is_local_label_name entry point, but some
8568 override it. */
8569
b34976b6 8570bfd_boolean
217aa764
AM
8571_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8572 const char *name)
252b5132
RH
8573{
8574 /* Normal local symbols start with ``.L''. */
8575 if (name[0] == '.' && name[1] == 'L')
b34976b6 8576 return TRUE;
252b5132
RH
8577
8578 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8579 DWARF debugging symbols starting with ``..''. */
8580 if (name[0] == '.' && name[1] == '.')
b34976b6 8581 return TRUE;
252b5132
RH
8582
8583 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8584 emitting DWARF debugging output. I suspect this is actually a
8585 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8586 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8587 underscore to be emitted on some ELF targets). For ease of use,
8588 we treat such symbols as local. */
8589 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
b34976b6 8590 return TRUE;
252b5132 8591
b1fa9dd6
NC
8592 /* Treat assembler generated fake symbols, dollar local labels and
8593 forward-backward labels (aka local labels) as locals.
8594 These labels have the form:
8595
07d6d2b8 8596 L0^A.* (fake symbols)
b1fa9dd6
NC
8597
8598 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8599
8600 Versions which start with .L will have already been matched above,
8601 so we only need to match the rest. */
8602 if (name[0] == 'L' && ISDIGIT (name[1]))
8603 {
8604 bfd_boolean ret = FALSE;
8605 const char * p;
8606 char c;
8607
8608 for (p = name + 2; (c = *p); p++)
8609 {
8610 if (c == 1 || c == 2)
8611 {
8612 if (c == 1 && p == name + 2)
8613 /* A fake symbol. */
8614 return TRUE;
8615
8616 /* FIXME: We are being paranoid here and treating symbols like
8617 L0^Bfoo as if there were non-local, on the grounds that the
8618 assembler will never generate them. But can any symbol
8619 containing an ASCII value in the range 1-31 ever be anything
8620 other than some kind of local ? */
8621 ret = TRUE;
8622 }
8623
8624 if (! ISDIGIT (c))
8625 {
8626 ret = FALSE;
8627 break;
8628 }
8629 }
8630 return ret;
8631 }
ffa54770 8632
b34976b6 8633 return FALSE;
252b5132
RH
8634}
8635
8636alent *
217aa764
AM
8637_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8638 asymbol *symbol ATTRIBUTE_UNUSED)
252b5132
RH
8639{
8640 abort ();
8641 return NULL;
8642}
8643
b34976b6 8644bfd_boolean
217aa764
AM
8645_bfd_elf_set_arch_mach (bfd *abfd,
8646 enum bfd_architecture arch,
8647 unsigned long machine)
252b5132
RH
8648{
8649 /* If this isn't the right architecture for this backend, and this
8650 isn't the generic backend, fail. */
8651 if (arch != get_elf_backend_data (abfd)->arch
8652 && arch != bfd_arch_unknown
8653 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
b34976b6 8654 return FALSE;
252b5132
RH
8655
8656 return bfd_default_set_arch_mach (abfd, arch, machine);
8657}
8658
d1fad7c6
NC
8659/* Find the nearest line to a particular section and offset,
8660 for error reporting. */
8661
b34976b6 8662bfd_boolean
217aa764 8663_bfd_elf_find_nearest_line (bfd *abfd,
217aa764 8664 asymbol **symbols,
fb167eb2 8665 asection *section,
217aa764
AM
8666 bfd_vma offset,
8667 const char **filename_ptr,
8668 const char **functionname_ptr,
fb167eb2
AM
8669 unsigned int *line_ptr,
8670 unsigned int *discriminator_ptr)
d1fad7c6 8671{
b34976b6 8672 bfd_boolean found;
d1fad7c6 8673
fb167eb2 8674 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
4e8a9624 8675 filename_ptr, functionname_ptr,
fb167eb2
AM
8676 line_ptr, discriminator_ptr,
8677 dwarf_debug_sections, 0,
e00e8198
AM
8678 &elf_tdata (abfd)->dwarf2_find_line_info)
8679 || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
8680 filename_ptr, functionname_ptr,
8681 line_ptr))
d1fad7c6
NC
8682 {
8683 if (!*functionname_ptr)
e00e8198
AM
8684 _bfd_elf_find_function (abfd, symbols, section, offset,
8685 *filename_ptr ? NULL : filename_ptr,
8686 functionname_ptr);
b34976b6 8687 return TRUE;
d1fad7c6
NC
8688 }
8689
8690 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4e8a9624
AM
8691 &found, filename_ptr,
8692 functionname_ptr, line_ptr,
8693 &elf_tdata (abfd)->line_info))
b34976b6 8694 return FALSE;
dc43ada5 8695 if (found && (*functionname_ptr || *line_ptr))
b34976b6 8696 return TRUE;
d1fad7c6
NC
8697
8698 if (symbols == NULL)
b34976b6 8699 return FALSE;
d1fad7c6 8700
e00e8198
AM
8701 if (! _bfd_elf_find_function (abfd, symbols, section, offset,
8702 filename_ptr, functionname_ptr))
b34976b6 8703 return FALSE;
d1fad7c6 8704
252b5132 8705 *line_ptr = 0;
b34976b6 8706 return TRUE;
252b5132
RH
8707}
8708
5420f73d
L
8709/* Find the line for a symbol. */
8710
8711bfd_boolean
8712_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8713 const char **filename_ptr, unsigned int *line_ptr)
9b8d1a36 8714{
fb167eb2
AM
8715 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
8716 filename_ptr, NULL, line_ptr, NULL,
8717 dwarf_debug_sections, 0,
8718 &elf_tdata (abfd)->dwarf2_find_line_info);
5420f73d
L
8719}
8720
4ab527b0
FF
8721/* After a call to bfd_find_nearest_line, successive calls to
8722 bfd_find_inliner_info can be used to get source information about
8723 each level of function inlining that terminated at the address
8724 passed to bfd_find_nearest_line. Currently this is only supported
8725 for DWARF2 with appropriate DWARF3 extensions. */
8726
8727bfd_boolean
8728_bfd_elf_find_inliner_info (bfd *abfd,
8729 const char **filename_ptr,
8730 const char **functionname_ptr,
8731 unsigned int *line_ptr)
8732{
8733 bfd_boolean found;
8734 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
8735 functionname_ptr, line_ptr,
8736 & elf_tdata (abfd)->dwarf2_find_line_info);
8737 return found;
8738}
8739
252b5132 8740int
a6b96beb 8741_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132 8742{
8ded5a0f
AM
8743 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8744 int ret = bed->s->sizeof_ehdr;
252b5132 8745
0e1862bb 8746 if (!bfd_link_relocatable (info))
8ded5a0f 8747 {
12bd6957 8748 bfd_size_type phdr_size = elf_program_header_size (abfd);
8ded5a0f 8749
62d7a5f6
AM
8750 if (phdr_size == (bfd_size_type) -1)
8751 {
8752 struct elf_segment_map *m;
8753
8754 phdr_size = 0;
12bd6957 8755 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
62d7a5f6 8756 phdr_size += bed->s->sizeof_phdr;
8ded5a0f 8757
62d7a5f6
AM
8758 if (phdr_size == 0)
8759 phdr_size = get_program_header_size (abfd, info);
8760 }
8ded5a0f 8761
12bd6957 8762 elf_program_header_size (abfd) = phdr_size;
8ded5a0f
AM
8763 ret += phdr_size;
8764 }
8765
252b5132
RH
8766 return ret;
8767}
8768
b34976b6 8769bfd_boolean
217aa764
AM
8770_bfd_elf_set_section_contents (bfd *abfd,
8771 sec_ptr section,
0f867abe 8772 const void *location,
217aa764
AM
8773 file_ptr offset,
8774 bfd_size_type count)
252b5132
RH
8775{
8776 Elf_Internal_Shdr *hdr;
1b6aeedb 8777 file_ptr pos;
252b5132
RH
8778
8779 if (! abfd->output_has_begun
217aa764 8780 && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
b34976b6 8781 return FALSE;
252b5132 8782
0ce398f1
L
8783 if (!count)
8784 return TRUE;
8785
252b5132 8786 hdr = &elf_section_data (section)->this_hdr;
0ce398f1
L
8787 if (hdr->sh_offset == (file_ptr) -1)
8788 {
8789 /* We must compress this section. Write output to the buffer. */
8790 unsigned char *contents = hdr->contents;
8791 if ((offset + count) > hdr->sh_size
8792 || (section->flags & SEC_ELF_COMPRESS) == 0
8793 || contents == NULL)
8794 abort ();
8795 memcpy (contents + offset, location, count);
8796 return TRUE;
8797 }
dc810e39
AM
8798 pos = hdr->sh_offset + offset;
8799 if (bfd_seek (abfd, pos, SEEK_SET) != 0
8800 || bfd_bwrite (location, count, abfd) != count)
b34976b6 8801 return FALSE;
252b5132 8802
b34976b6 8803 return TRUE;
252b5132
RH
8804}
8805
8806void
217aa764
AM
8807_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
8808 arelent *cache_ptr ATTRIBUTE_UNUSED,
8809 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
252b5132
RH
8810{
8811 abort ();
8812}
8813
252b5132
RH
8814/* Try to convert a non-ELF reloc into an ELF one. */
8815
b34976b6 8816bfd_boolean
217aa764 8817_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
252b5132 8818{
c044fabd 8819 /* Check whether we really have an ELF howto. */
252b5132
RH
8820
8821 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
8822 {
8823 bfd_reloc_code_real_type code;
8824 reloc_howto_type *howto;
8825
8826 /* Alien reloc: Try to determine its type to replace it with an
c044fabd 8827 equivalent ELF reloc. */
252b5132
RH
8828
8829 if (areloc->howto->pc_relative)
8830 {
8831 switch (areloc->howto->bitsize)
8832 {
8833 case 8:
8834 code = BFD_RELOC_8_PCREL;
8835 break;
8836 case 12:
8837 code = BFD_RELOC_12_PCREL;
8838 break;
8839 case 16:
8840 code = BFD_RELOC_16_PCREL;
8841 break;
8842 case 24:
8843 code = BFD_RELOC_24_PCREL;
8844 break;
8845 case 32:
8846 code = BFD_RELOC_32_PCREL;
8847 break;
8848 case 64:
8849 code = BFD_RELOC_64_PCREL;
8850 break;
8851 default:
8852 goto fail;
8853 }
8854
8855 howto = bfd_reloc_type_lookup (abfd, code);
8856
8857 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
8858 {
8859 if (howto->pcrel_offset)
8860 areloc->addend += areloc->address;
8861 else
8862 areloc->addend -= areloc->address; /* addend is unsigned!! */
8863 }
8864 }
8865 else
8866 {
8867 switch (areloc->howto->bitsize)
8868 {
8869 case 8:
8870 code = BFD_RELOC_8;
8871 break;
8872 case 14:
8873 code = BFD_RELOC_14;
8874 break;
8875 case 16:
8876 code = BFD_RELOC_16;
8877 break;
8878 case 26:
8879 code = BFD_RELOC_26;
8880 break;
8881 case 32:
8882 code = BFD_RELOC_32;
8883 break;
8884 case 64:
8885 code = BFD_RELOC_64;
8886 break;
8887 default:
8888 goto fail;
8889 }
8890
8891 howto = bfd_reloc_type_lookup (abfd, code);
8892 }
8893
8894 if (howto)
8895 areloc->howto = howto;
8896 else
8897 goto fail;
8898 }
8899
b34976b6 8900 return TRUE;
252b5132
RH
8901
8902 fail:
4eca0228 8903 _bfd_error_handler
695344c0 8904 /* xgettext:c-format */
d003868e
AM
8905 (_("%B: unsupported relocation type %s"),
8906 abfd, areloc->howto->name);
252b5132 8907 bfd_set_error (bfd_error_bad_value);
b34976b6 8908 return FALSE;
252b5132
RH
8909}
8910
b34976b6 8911bfd_boolean
217aa764 8912_bfd_elf_close_and_cleanup (bfd *abfd)
252b5132 8913{
d9071b0c
TG
8914 struct elf_obj_tdata *tdata = elf_tdata (abfd);
8915 if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
252b5132 8916 {
c0355132 8917 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
2b0f7ef9 8918 _bfd_elf_strtab_free (elf_shstrtab (abfd));
d9071b0c 8919 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
252b5132
RH
8920 }
8921
8922 return _bfd_generic_close_and_cleanup (abfd);
8923}
8924
8925/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8926 in the relocation's offset. Thus we cannot allow any sort of sanity
8927 range-checking to interfere. There is nothing else to do in processing
8928 this reloc. */
8929
8930bfd_reloc_status_type
217aa764
AM
8931_bfd_elf_rel_vtable_reloc_fn
8932 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
fc0a2244 8933 struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
217aa764
AM
8934 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
8935 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
252b5132
RH
8936{
8937 return bfd_reloc_ok;
8938}
252b5132
RH
8939\f
8940/* Elf core file support. Much of this only works on native
8941 toolchains, since we rely on knowing the
8942 machine-dependent procfs structure in order to pick
c044fabd 8943 out details about the corefile. */
252b5132
RH
8944
8945#ifdef HAVE_SYS_PROCFS_H
16231b7b
DG
8946/* Needed for new procfs interface on sparc-solaris. */
8947# define _STRUCTURED_PROC 1
252b5132
RH
8948# include <sys/procfs.h>
8949#endif
8950
261b8d08
PA
8951/* Return a PID that identifies a "thread" for threaded cores, or the
8952 PID of the main process for non-threaded cores. */
252b5132
RH
8953
8954static int
217aa764 8955elfcore_make_pid (bfd *abfd)
252b5132 8956{
261b8d08
PA
8957 int pid;
8958
228e534f 8959 pid = elf_tdata (abfd)->core->lwpid;
261b8d08 8960 if (pid == 0)
228e534f 8961 pid = elf_tdata (abfd)->core->pid;
261b8d08
PA
8962
8963 return pid;
252b5132
RH
8964}
8965
252b5132
RH
8966/* If there isn't a section called NAME, make one, using
8967 data from SECT. Note, this function will generate a
8968 reference to NAME, so you shouldn't deallocate or
c044fabd 8969 overwrite it. */
252b5132 8970
b34976b6 8971static bfd_boolean
217aa764 8972elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
252b5132 8973{
c044fabd 8974 asection *sect2;
252b5132
RH
8975
8976 if (bfd_get_section_by_name (abfd, name) != NULL)
b34976b6 8977 return TRUE;
252b5132 8978
117ed4f8 8979 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
252b5132 8980 if (sect2 == NULL)
b34976b6 8981 return FALSE;
252b5132 8982
eea6121a 8983 sect2->size = sect->size;
252b5132 8984 sect2->filepos = sect->filepos;
252b5132 8985 sect2->alignment_power = sect->alignment_power;
b34976b6 8986 return TRUE;
252b5132
RH
8987}
8988
bb0082d6
AM
8989/* Create a pseudosection containing SIZE bytes at FILEPOS. This
8990 actually creates up to two pseudosections:
8991 - For the single-threaded case, a section named NAME, unless
8992 such a section already exists.
8993 - For the multi-threaded case, a section named "NAME/PID", where
8994 PID is elfcore_make_pid (abfd).
24d3e51b 8995 Both pseudosections have identical contents. */
b34976b6 8996bfd_boolean
217aa764
AM
8997_bfd_elfcore_make_pseudosection (bfd *abfd,
8998 char *name,
8999 size_t size,
9000 ufile_ptr filepos)
bb0082d6
AM
9001{
9002 char buf[100];
9003 char *threaded_name;
d4c88bbb 9004 size_t len;
bb0082d6
AM
9005 asection *sect;
9006
9007 /* Build the section name. */
9008
9009 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
d4c88bbb 9010 len = strlen (buf) + 1;
a50b1753 9011 threaded_name = (char *) bfd_alloc (abfd, len);
bb0082d6 9012 if (threaded_name == NULL)
b34976b6 9013 return FALSE;
d4c88bbb 9014 memcpy (threaded_name, buf, len);
bb0082d6 9015
117ed4f8
AM
9016 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
9017 SEC_HAS_CONTENTS);
bb0082d6 9018 if (sect == NULL)
b34976b6 9019 return FALSE;
eea6121a 9020 sect->size = size;
bb0082d6 9021 sect->filepos = filepos;
bb0082d6
AM
9022 sect->alignment_power = 2;
9023
936e320b 9024 return elfcore_maybe_make_sect (abfd, name, sect);
bb0082d6
AM
9025}
9026
252b5132 9027/* prstatus_t exists on:
4a938328 9028 solaris 2.5+
252b5132
RH
9029 linux 2.[01] + glibc
9030 unixware 4.2
9031*/
9032
9033#if defined (HAVE_PRSTATUS_T)
a7b97311 9034
b34976b6 9035static bfd_boolean
217aa764 9036elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9037{
eea6121a 9038 size_t size;
7ee38065 9039 int offset;
252b5132 9040
4a938328
MS
9041 if (note->descsz == sizeof (prstatus_t))
9042 {
9043 prstatus_t prstat;
252b5132 9044
eea6121a 9045 size = sizeof (prstat.pr_reg);
7ee38065 9046 offset = offsetof (prstatus_t, pr_reg);
4a938328 9047 memcpy (&prstat, note->descdata, sizeof (prstat));
252b5132 9048
fa49d224
NC
9049 /* Do not overwrite the core signal if it
9050 has already been set by another thread. */
228e534f
AM
9051 if (elf_tdata (abfd)->core->signal == 0)
9052 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9053 if (elf_tdata (abfd)->core->pid == 0)
9054 elf_tdata (abfd)->core->pid = prstat.pr_pid;
252b5132 9055
4a938328
MS
9056 /* pr_who exists on:
9057 solaris 2.5+
9058 unixware 4.2
9059 pr_who doesn't exist on:
9060 linux 2.[01]
9061 */
252b5132 9062#if defined (HAVE_PRSTATUS_T_PR_WHO)
228e534f 9063 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9064#else
228e534f 9065 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
252b5132 9066#endif
4a938328 9067 }
7ee38065 9068#if defined (HAVE_PRSTATUS32_T)
4a938328
MS
9069 else if (note->descsz == sizeof (prstatus32_t))
9070 {
9071 /* 64-bit host, 32-bit corefile */
9072 prstatus32_t prstat;
9073
eea6121a 9074 size = sizeof (prstat.pr_reg);
7ee38065 9075 offset = offsetof (prstatus32_t, pr_reg);
4a938328
MS
9076 memcpy (&prstat, note->descdata, sizeof (prstat));
9077
fa49d224
NC
9078 /* Do not overwrite the core signal if it
9079 has already been set by another thread. */
228e534f
AM
9080 if (elf_tdata (abfd)->core->signal == 0)
9081 elf_tdata (abfd)->core->signal = prstat.pr_cursig;
9082 if (elf_tdata (abfd)->core->pid == 0)
9083 elf_tdata (abfd)->core->pid = prstat.pr_pid;
4a938328
MS
9084
9085 /* pr_who exists on:
9086 solaris 2.5+
9087 unixware 4.2
9088 pr_who doesn't exist on:
9089 linux 2.[01]
9090 */
7ee38065 9091#if defined (HAVE_PRSTATUS32_T_PR_WHO)
228e534f 9092 elf_tdata (abfd)->core->lwpid = prstat.pr_who;
261b8d08 9093#else
228e534f 9094 elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
4a938328
MS
9095#endif
9096 }
7ee38065 9097#endif /* HAVE_PRSTATUS32_T */
4a938328
MS
9098 else
9099 {
9100 /* Fail - we don't know how to handle any other
9101 note size (ie. data object type). */
b34976b6 9102 return TRUE;
4a938328 9103 }
252b5132 9104
bb0082d6 9105 /* Make a ".reg/999" section and a ".reg" section. */
936e320b 9106 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 9107 size, note->descpos + offset);
252b5132
RH
9108}
9109#endif /* defined (HAVE_PRSTATUS_T) */
9110
bb0082d6 9111/* Create a pseudosection containing the exact contents of NOTE. */
b34976b6 9112static bfd_boolean
217aa764
AM
9113elfcore_make_note_pseudosection (bfd *abfd,
9114 char *name,
9115 Elf_Internal_Note *note)
252b5132 9116{
936e320b
AM
9117 return _bfd_elfcore_make_pseudosection (abfd, name,
9118 note->descsz, note->descpos);
252b5132
RH
9119}
9120
ff08c6bb
JB
9121/* There isn't a consistent prfpregset_t across platforms,
9122 but it doesn't matter, because we don't have to pick this
c044fabd
KH
9123 data structure apart. */
9124
b34976b6 9125static bfd_boolean
217aa764 9126elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9127{
9128 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9129}
9130
ff08c6bb 9131/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
971d4640 9132 type of NT_PRXFPREG. Just include the whole note's contents
ff08c6bb 9133 literally. */
c044fabd 9134
b34976b6 9135static bfd_boolean
217aa764 9136elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
ff08c6bb
JB
9137{
9138 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9139}
9140
4339cae0
L
9141/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9142 with a note type of NT_X86_XSTATE. Just include the whole note's
9143 contents literally. */
9144
9145static bfd_boolean
9146elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
9147{
9148 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
9149}
9150
97753bd5
AM
9151static bfd_boolean
9152elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
9153{
9154 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
9155}
9156
89eeb0bc
LM
9157static bfd_boolean
9158elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
9159{
9160 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
9161}
97753bd5 9162
0675e188
UW
9163static bfd_boolean
9164elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
9165{
9166 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
9167}
9168
d7eeb400
MS
9169static bfd_boolean
9170elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
9171{
9172 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
9173}
9174
9175static bfd_boolean
9176elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
9177{
9178 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
9179}
9180
9181static bfd_boolean
9182elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
9183{
9184 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
9185}
9186
9187static bfd_boolean
9188elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
9189{
9190 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
9191}
9192
9193static bfd_boolean
9194elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
9195{
9196 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
9197}
9198
355b81d9
UW
9199static bfd_boolean
9200elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
9201{
9202 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
9203}
9204
9205static bfd_boolean
9206elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
9207{
9208 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
9209}
9210
abb3f6cc
NC
9211static bfd_boolean
9212elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
9213{
9214 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
9215}
9216
4ef9f41a
AA
9217static bfd_boolean
9218elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
9219{
9220 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
9221}
9222
9223static bfd_boolean
9224elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
9225{
9226 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
9227}
9228
88ab90e8
AA
9229static bfd_boolean
9230elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
9231{
9232 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note);
9233}
9234
9235static bfd_boolean
9236elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
9237{
9238 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note);
9239}
9240
faa9a424
UW
9241static bfd_boolean
9242elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
9243{
9244 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
9245}
9246
652451f8
YZ
9247static bfd_boolean
9248elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
9249{
9250 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
9251}
9252
9253static bfd_boolean
9254elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
9255{
9256 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
9257}
9258
9259static bfd_boolean
9260elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
9261{
9262 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
9263}
9264
252b5132 9265#if defined (HAVE_PRPSINFO_T)
4a938328 9266typedef prpsinfo_t elfcore_psinfo_t;
7ee38065 9267#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9268typedef prpsinfo32_t elfcore_psinfo32_t;
9269#endif
252b5132
RH
9270#endif
9271
9272#if defined (HAVE_PSINFO_T)
4a938328 9273typedef psinfo_t elfcore_psinfo_t;
7ee38065 9274#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
4a938328
MS
9275typedef psinfo32_t elfcore_psinfo32_t;
9276#endif
252b5132
RH
9277#endif
9278
252b5132
RH
9279/* return a malloc'ed copy of a string at START which is at
9280 most MAX bytes long, possibly without a terminating '\0'.
c044fabd 9281 the copy will always have a terminating '\0'. */
252b5132 9282
936e320b 9283char *
217aa764 9284_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
252b5132 9285{
dc810e39 9286 char *dups;
a50b1753 9287 char *end = (char *) memchr (start, '\0', max);
dc810e39 9288 size_t len;
252b5132
RH
9289
9290 if (end == NULL)
9291 len = max;
9292 else
9293 len = end - start;
9294
a50b1753 9295 dups = (char *) bfd_alloc (abfd, len + 1);
dc810e39 9296 if (dups == NULL)
252b5132
RH
9297 return NULL;
9298
dc810e39
AM
9299 memcpy (dups, start, len);
9300 dups[len] = '\0';
252b5132 9301
dc810e39 9302 return dups;
252b5132
RH
9303}
9304
bb0082d6 9305#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
b34976b6 9306static bfd_boolean
217aa764 9307elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
252b5132 9308{
4a938328
MS
9309 if (note->descsz == sizeof (elfcore_psinfo_t))
9310 {
9311 elfcore_psinfo_t psinfo;
252b5132 9312
7ee38065 9313 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9314
335e41d4 9315#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
228e534f 9316 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9317#endif
228e534f 9318 elf_tdata (abfd)->core->program
936e320b
AM
9319 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9320 sizeof (psinfo.pr_fname));
252b5132 9321
228e534f 9322 elf_tdata (abfd)->core->command
936e320b
AM
9323 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9324 sizeof (psinfo.pr_psargs));
4a938328 9325 }
7ee38065 9326#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
4a938328
MS
9327 else if (note->descsz == sizeof (elfcore_psinfo32_t))
9328 {
9329 /* 64-bit host, 32-bit corefile */
9330 elfcore_psinfo32_t psinfo;
9331
7ee38065 9332 memcpy (&psinfo, note->descdata, sizeof (psinfo));
252b5132 9333
335e41d4 9334#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
228e534f 9335 elf_tdata (abfd)->core->pid = psinfo.pr_pid;
335e41d4 9336#endif
228e534f 9337 elf_tdata (abfd)->core->program
936e320b
AM
9338 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
9339 sizeof (psinfo.pr_fname));
4a938328 9340
228e534f 9341 elf_tdata (abfd)->core->command
936e320b
AM
9342 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
9343 sizeof (psinfo.pr_psargs));
4a938328
MS
9344 }
9345#endif
9346
9347 else
9348 {
9349 /* Fail - we don't know how to handle any other
9350 note size (ie. data object type). */
b34976b6 9351 return TRUE;
4a938328 9352 }
252b5132
RH
9353
9354 /* Note that for some reason, a spurious space is tacked
9355 onto the end of the args in some (at least one anyway)
c044fabd 9356 implementations, so strip it off if it exists. */
252b5132
RH
9357
9358 {
228e534f 9359 char *command = elf_tdata (abfd)->core->command;
252b5132
RH
9360 int n = strlen (command);
9361
9362 if (0 < n && command[n - 1] == ' ')
9363 command[n - 1] = '\0';
9364 }
9365
b34976b6 9366 return TRUE;
252b5132
RH
9367}
9368#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9369
252b5132 9370#if defined (HAVE_PSTATUS_T)
b34976b6 9371static bfd_boolean
217aa764 9372elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132 9373{
f572a39d
AM
9374 if (note->descsz == sizeof (pstatus_t)
9375#if defined (HAVE_PXSTATUS_T)
9376 || note->descsz == sizeof (pxstatus_t)
9377#endif
9378 )
4a938328
MS
9379 {
9380 pstatus_t pstat;
252b5132 9381
4a938328 9382 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9383
228e534f 9384 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328 9385 }
7ee38065 9386#if defined (HAVE_PSTATUS32_T)
4a938328
MS
9387 else if (note->descsz == sizeof (pstatus32_t))
9388 {
9389 /* 64-bit host, 32-bit corefile */
9390 pstatus32_t pstat;
252b5132 9391
4a938328 9392 memcpy (&pstat, note->descdata, sizeof (pstat));
252b5132 9393
228e534f 9394 elf_tdata (abfd)->core->pid = pstat.pr_pid;
4a938328
MS
9395 }
9396#endif
252b5132
RH
9397 /* Could grab some more details from the "representative"
9398 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
c044fabd 9399 NT_LWPSTATUS note, presumably. */
252b5132 9400
b34976b6 9401 return TRUE;
252b5132
RH
9402}
9403#endif /* defined (HAVE_PSTATUS_T) */
9404
252b5132 9405#if defined (HAVE_LWPSTATUS_T)
b34976b6 9406static bfd_boolean
217aa764 9407elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
252b5132
RH
9408{
9409 lwpstatus_t lwpstat;
9410 char buf[100];
c044fabd 9411 char *name;
d4c88bbb 9412 size_t len;
c044fabd 9413 asection *sect;
252b5132 9414
f572a39d
AM
9415 if (note->descsz != sizeof (lwpstat)
9416#if defined (HAVE_LWPXSTATUS_T)
9417 && note->descsz != sizeof (lwpxstatus_t)
9418#endif
9419 )
b34976b6 9420 return TRUE;
252b5132
RH
9421
9422 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
9423
228e534f 9424 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
a1504221
JB
9425 /* Do not overwrite the core signal if it has already been set by
9426 another thread. */
228e534f
AM
9427 if (elf_tdata (abfd)->core->signal == 0)
9428 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
252b5132 9429
c044fabd 9430 /* Make a ".reg/999" section. */
252b5132
RH
9431
9432 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
d4c88bbb 9433 len = strlen (buf) + 1;
217aa764 9434 name = bfd_alloc (abfd, len);
252b5132 9435 if (name == NULL)
b34976b6 9436 return FALSE;
d4c88bbb 9437 memcpy (name, buf, len);
252b5132 9438
117ed4f8 9439 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9440 if (sect == NULL)
b34976b6 9441 return FALSE;
252b5132
RH
9442
9443#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9444 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
252b5132
RH
9445 sect->filepos = note->descpos
9446 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
9447#endif
9448
9449#if defined (HAVE_LWPSTATUS_T_PR_REG)
eea6121a 9450 sect->size = sizeof (lwpstat.pr_reg);
252b5132
RH
9451 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
9452#endif
9453
252b5132
RH
9454 sect->alignment_power = 2;
9455
9456 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9457 return FALSE;
252b5132
RH
9458
9459 /* Make a ".reg2/999" section */
9460
9461 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
d4c88bbb 9462 len = strlen (buf) + 1;
217aa764 9463 name = bfd_alloc (abfd, len);
252b5132 9464 if (name == NULL)
b34976b6 9465 return FALSE;
d4c88bbb 9466 memcpy (name, buf, len);
252b5132 9467
117ed4f8 9468 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
252b5132 9469 if (sect == NULL)
b34976b6 9470 return FALSE;
252b5132
RH
9471
9472#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
eea6121a 9473 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
252b5132
RH
9474 sect->filepos = note->descpos
9475 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
9476#endif
9477
9478#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
eea6121a 9479 sect->size = sizeof (lwpstat.pr_fpreg);
252b5132
RH
9480 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
9481#endif
9482
252b5132
RH
9483 sect->alignment_power = 2;
9484
936e320b 9485 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
252b5132
RH
9486}
9487#endif /* defined (HAVE_LWPSTATUS_T) */
9488
b34976b6 9489static bfd_boolean
217aa764 9490elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
16e9c715
NC
9491{
9492 char buf[30];
c044fabd 9493 char *name;
d4c88bbb 9494 size_t len;
c044fabd 9495 asection *sect;
4a6636fb
PA
9496 int type;
9497 int is_active_thread;
9498 bfd_vma base_addr;
16e9c715 9499
4a6636fb 9500 if (note->descsz < 728)
b34976b6 9501 return TRUE;
16e9c715 9502
4a6636fb
PA
9503 if (! CONST_STRNEQ (note->namedata, "win32"))
9504 return TRUE;
9505
9506 type = bfd_get_32 (abfd, note->descdata);
c044fabd 9507
4a6636fb 9508 switch (type)
16e9c715 9509 {
4a6636fb 9510 case 1 /* NOTE_INFO_PROCESS */:
228e534f 9511 /* FIXME: need to add ->core->command. */
4a6636fb 9512 /* process_info.pid */
228e534f 9513 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
4a6636fb 9514 /* process_info.signal */
228e534f 9515 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
c044fabd 9516 break;
16e9c715 9517
4a6636fb 9518 case 2 /* NOTE_INFO_THREAD */:
16e9c715 9519 /* Make a ".reg/999" section. */
4a6636fb
PA
9520 /* thread_info.tid */
9521 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
c044fabd 9522
d4c88bbb 9523 len = strlen (buf) + 1;
a50b1753 9524 name = (char *) bfd_alloc (abfd, len);
16e9c715 9525 if (name == NULL)
b34976b6 9526 return FALSE;
c044fabd 9527
d4c88bbb 9528 memcpy (name, buf, len);
16e9c715 9529
117ed4f8 9530 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
16e9c715 9531 if (sect == NULL)
b34976b6 9532 return FALSE;
c044fabd 9533
4a6636fb
PA
9534 /* sizeof (thread_info.thread_context) */
9535 sect->size = 716;
9536 /* offsetof (thread_info.thread_context) */
9537 sect->filepos = note->descpos + 12;
16e9c715
NC
9538 sect->alignment_power = 2;
9539
4a6636fb
PA
9540 /* thread_info.is_active_thread */
9541 is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9542
9543 if (is_active_thread)
16e9c715 9544 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
b34976b6 9545 return FALSE;
16e9c715
NC
9546 break;
9547
4a6636fb 9548 case 3 /* NOTE_INFO_MODULE */:
16e9c715 9549 /* Make a ".module/xxxxxxxx" section. */
4a6636fb
PA
9550 /* module_info.base_address */
9551 base_addr = bfd_get_32 (abfd, note->descdata + 4);
0af1713e 9552 sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
c044fabd 9553
d4c88bbb 9554 len = strlen (buf) + 1;
a50b1753 9555 name = (char *) bfd_alloc (abfd, len);
16e9c715 9556 if (name == NULL)
b34976b6 9557 return FALSE;
c044fabd 9558
d4c88bbb 9559 memcpy (name, buf, len);
252b5132 9560
117ed4f8 9561 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
c044fabd 9562
16e9c715 9563 if (sect == NULL)
b34976b6 9564 return FALSE;
c044fabd 9565
eea6121a 9566 sect->size = note->descsz;
16e9c715 9567 sect->filepos = note->descpos;
16e9c715
NC
9568 sect->alignment_power = 2;
9569 break;
9570
9571 default:
b34976b6 9572 return TRUE;
16e9c715
NC
9573 }
9574
b34976b6 9575 return TRUE;
16e9c715 9576}
252b5132 9577
b34976b6 9578static bfd_boolean
217aa764 9579elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
252b5132 9580{
9c5bfbb7 9581 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bb0082d6 9582
252b5132
RH
9583 switch (note->type)
9584 {
9585 default:
b34976b6 9586 return TRUE;
252b5132 9587
252b5132 9588 case NT_PRSTATUS:
bb0082d6
AM
9589 if (bed->elf_backend_grok_prstatus)
9590 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
b34976b6 9591 return TRUE;
bb0082d6 9592#if defined (HAVE_PRSTATUS_T)
252b5132 9593 return elfcore_grok_prstatus (abfd, note);
bb0082d6 9594#else
b34976b6 9595 return TRUE;
252b5132
RH
9596#endif
9597
9598#if defined (HAVE_PSTATUS_T)
9599 case NT_PSTATUS:
9600 return elfcore_grok_pstatus (abfd, note);
9601#endif
9602
9603#if defined (HAVE_LWPSTATUS_T)
9604 case NT_LWPSTATUS:
9605 return elfcore_grok_lwpstatus (abfd, note);
9606#endif
9607
9608 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
9609 return elfcore_grok_prfpreg (abfd, note);
9610
c044fabd 9611 case NT_WIN32PSTATUS:
16e9c715 9612 return elfcore_grok_win32pstatus (abfd, note);
16e9c715 9613
c044fabd 9614 case NT_PRXFPREG: /* Linux SSE extension */
e377ab71
MK
9615 if (note->namesz == 6
9616 && strcmp (note->namedata, "LINUX") == 0)
ff08c6bb
JB
9617 return elfcore_grok_prxfpreg (abfd, note);
9618 else
b34976b6 9619 return TRUE;
ff08c6bb 9620
4339cae0
L
9621 case NT_X86_XSTATE: /* Linux XSAVE extension */
9622 if (note->namesz == 6
9623 && strcmp (note->namedata, "LINUX") == 0)
9624 return elfcore_grok_xstatereg (abfd, note);
9625 else
9626 return TRUE;
9627
97753bd5
AM
9628 case NT_PPC_VMX:
9629 if (note->namesz == 6
9630 && strcmp (note->namedata, "LINUX") == 0)
9631 return elfcore_grok_ppc_vmx (abfd, note);
9632 else
9633 return TRUE;
9634
89eeb0bc
LM
9635 case NT_PPC_VSX:
9636 if (note->namesz == 6
07d6d2b8
AM
9637 && strcmp (note->namedata, "LINUX") == 0)
9638 return elfcore_grok_ppc_vsx (abfd, note);
89eeb0bc 9639 else
07d6d2b8 9640 return TRUE;
89eeb0bc 9641
0675e188
UW
9642 case NT_S390_HIGH_GPRS:
9643 if (note->namesz == 6
07d6d2b8
AM
9644 && strcmp (note->namedata, "LINUX") == 0)
9645 return elfcore_grok_s390_high_gprs (abfd, note);
0675e188 9646 else
07d6d2b8 9647 return TRUE;
0675e188 9648
d7eeb400
MS
9649 case NT_S390_TIMER:
9650 if (note->namesz == 6
07d6d2b8
AM
9651 && strcmp (note->namedata, "LINUX") == 0)
9652 return elfcore_grok_s390_timer (abfd, note);
d7eeb400 9653 else
07d6d2b8 9654 return TRUE;
d7eeb400
MS
9655
9656 case NT_S390_TODCMP:
9657 if (note->namesz == 6
07d6d2b8
AM
9658 && strcmp (note->namedata, "LINUX") == 0)
9659 return elfcore_grok_s390_todcmp (abfd, note);
d7eeb400 9660 else
07d6d2b8 9661 return TRUE;
d7eeb400
MS
9662
9663 case NT_S390_TODPREG:
9664 if (note->namesz == 6
07d6d2b8
AM
9665 && strcmp (note->namedata, "LINUX") == 0)
9666 return elfcore_grok_s390_todpreg (abfd, note);
d7eeb400 9667 else
07d6d2b8 9668 return TRUE;
d7eeb400
MS
9669
9670 case NT_S390_CTRS:
9671 if (note->namesz == 6
07d6d2b8
AM
9672 && strcmp (note->namedata, "LINUX") == 0)
9673 return elfcore_grok_s390_ctrs (abfd, note);
d7eeb400 9674 else
07d6d2b8 9675 return TRUE;
d7eeb400
MS
9676
9677 case NT_S390_PREFIX:
9678 if (note->namesz == 6
07d6d2b8
AM
9679 && strcmp (note->namedata, "LINUX") == 0)
9680 return elfcore_grok_s390_prefix (abfd, note);
d7eeb400 9681 else
07d6d2b8 9682 return TRUE;
d7eeb400 9683
355b81d9
UW
9684 case NT_S390_LAST_BREAK:
9685 if (note->namesz == 6
07d6d2b8
AM
9686 && strcmp (note->namedata, "LINUX") == 0)
9687 return elfcore_grok_s390_last_break (abfd, note);
355b81d9 9688 else
07d6d2b8 9689 return TRUE;
355b81d9
UW
9690
9691 case NT_S390_SYSTEM_CALL:
9692 if (note->namesz == 6
07d6d2b8
AM
9693 && strcmp (note->namedata, "LINUX") == 0)
9694 return elfcore_grok_s390_system_call (abfd, note);
355b81d9 9695 else
07d6d2b8 9696 return TRUE;
355b81d9 9697
abb3f6cc
NC
9698 case NT_S390_TDB:
9699 if (note->namesz == 6
07d6d2b8
AM
9700 && strcmp (note->namedata, "LINUX") == 0)
9701 return elfcore_grok_s390_tdb (abfd, note);
abb3f6cc 9702 else
07d6d2b8 9703 return TRUE;
abb3f6cc 9704
4ef9f41a
AA
9705 case NT_S390_VXRS_LOW:
9706 if (note->namesz == 6
9707 && strcmp (note->namedata, "LINUX") == 0)
9708 return elfcore_grok_s390_vxrs_low (abfd, note);
9709 else
9710 return TRUE;
9711
9712 case NT_S390_VXRS_HIGH:
9713 if (note->namesz == 6
9714 && strcmp (note->namedata, "LINUX") == 0)
9715 return elfcore_grok_s390_vxrs_high (abfd, note);
9716 else
9717 return TRUE;
9718
88ab90e8
AA
9719 case NT_S390_GS_CB:
9720 if (note->namesz == 6
9721 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 9722 return elfcore_grok_s390_gs_cb (abfd, note);
88ab90e8
AA
9723 else
9724 return TRUE;
9725
9726 case NT_S390_GS_BC:
9727 if (note->namesz == 6
9728 && strcmp (note->namedata, "LINUX") == 0)
8fe09d74 9729 return elfcore_grok_s390_gs_bc (abfd, note);
88ab90e8
AA
9730 else
9731 return TRUE;
9732
faa9a424
UW
9733 case NT_ARM_VFP:
9734 if (note->namesz == 6
9735 && strcmp (note->namedata, "LINUX") == 0)
9736 return elfcore_grok_arm_vfp (abfd, note);
9737 else
9738 return TRUE;
9739
652451f8
YZ
9740 case NT_ARM_TLS:
9741 if (note->namesz == 6
9742 && strcmp (note->namedata, "LINUX") == 0)
9743 return elfcore_grok_aarch_tls (abfd, note);
9744 else
9745 return TRUE;
9746
9747 case NT_ARM_HW_BREAK:
9748 if (note->namesz == 6
9749 && strcmp (note->namedata, "LINUX") == 0)
9750 return elfcore_grok_aarch_hw_break (abfd, note);
9751 else
9752 return TRUE;
9753
9754 case NT_ARM_HW_WATCH:
9755 if (note->namesz == 6
9756 && strcmp (note->namedata, "LINUX") == 0)
9757 return elfcore_grok_aarch_hw_watch (abfd, note);
9758 else
9759 return TRUE;
9760
252b5132
RH
9761 case NT_PRPSINFO:
9762 case NT_PSINFO:
bb0082d6
AM
9763 if (bed->elf_backend_grok_psinfo)
9764 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
b34976b6 9765 return TRUE;
bb0082d6 9766#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
252b5132 9767 return elfcore_grok_psinfo (abfd, note);
bb0082d6 9768#else
b34976b6 9769 return TRUE;
252b5132 9770#endif
3333a7c3
RM
9771
9772 case NT_AUXV:
9773 {
117ed4f8
AM
9774 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9775 SEC_HAS_CONTENTS);
3333a7c3
RM
9776
9777 if (sect == NULL)
9778 return FALSE;
eea6121a 9779 sect->size = note->descsz;
3333a7c3 9780 sect->filepos = note->descpos;
3333a7c3
RM
9781 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9782
9783 return TRUE;
9784 }
9015683b 9785
451b7c33
TT
9786 case NT_FILE:
9787 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
9788 note);
9789
9015683b
TT
9790 case NT_SIGINFO:
9791 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
9792 note);
5b2c414d 9793
252b5132
RH
9794 }
9795}
9796
718175fa
JK
9797static bfd_boolean
9798elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
9799{
c74f7d1c 9800 struct bfd_build_id* build_id;
30e8ee25
AM
9801
9802 if (note->descsz == 0)
9803 return FALSE;
9804
c74f7d1c
JT
9805 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
9806 if (build_id == NULL)
718175fa
JK
9807 return FALSE;
9808
c74f7d1c
JT
9809 build_id->size = note->descsz;
9810 memcpy (build_id->data, note->descdata, note->descsz);
9811 abfd->build_id = build_id;
718175fa
JK
9812
9813 return TRUE;
9814}
9815
9816static bfd_boolean
9817elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
9818{
9819 switch (note->type)
9820 {
9821 default:
9822 return TRUE;
9823
46bed679
L
9824 case NT_GNU_PROPERTY_TYPE_0:
9825 return _bfd_elf_parse_gnu_properties (abfd, note);
9826
718175fa
JK
9827 case NT_GNU_BUILD_ID:
9828 return elfobj_grok_gnu_build_id (abfd, note);
9829 }
9830}
9831
e21e5835
NC
9832static bfd_boolean
9833elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
9834{
9835 struct sdt_note *cur =
9836 (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
9837 + note->descsz);
9838
9839 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
9840 cur->size = (bfd_size_type) note->descsz;
9841 memcpy (cur->data, note->descdata, note->descsz);
9842
9843 elf_tdata (abfd)->sdt_note_head = cur;
9844
9845 return TRUE;
9846}
9847
9848static bfd_boolean
9849elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
9850{
9851 switch (note->type)
9852 {
9853 case NT_STAPSDT:
9854 return elfobj_grok_stapsdt_note_1 (abfd, note);
9855
9856 default:
9857 return TRUE;
9858 }
9859}
9860
aa1ed4a9
JB
9861static bfd_boolean
9862elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
9863{
9864 size_t offset;
9865
b5430a3c 9866 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 9867 {
b5430a3c 9868 case ELFCLASS32:
0064d223
JB
9869 if (note->descsz < 108)
9870 return FALSE;
aa1ed4a9
JB
9871 break;
9872
b5430a3c 9873 case ELFCLASS64:
0064d223
JB
9874 if (note->descsz < 120)
9875 return FALSE;
aa1ed4a9
JB
9876 break;
9877
9878 default:
9879 return FALSE;
9880 }
9881
0064d223
JB
9882 /* Check for version 1 in pr_version. */
9883 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
9884 return FALSE;
80a04378 9885
0064d223
JB
9886 offset = 4;
9887
9888 /* Skip over pr_psinfosz. */
b5430a3c 9889 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
0064d223
JB
9890 offset += 4;
9891 else
9892 {
9893 offset += 4; /* Padding before pr_psinfosz. */
9894 offset += 8;
9895 }
9896
aa1ed4a9
JB
9897 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9898 elf_tdata (abfd)->core->program
9899 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
9900 offset += 17;
9901
9902 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9903 elf_tdata (abfd)->core->command
9904 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
0064d223
JB
9905 offset += 81;
9906
9907 /* Padding before pr_pid. */
9908 offset += 2;
9909
9910 /* The pr_pid field was added in version "1a". */
9911 if (note->descsz < offset + 4)
9912 return TRUE;
9913
9914 elf_tdata (abfd)->core->pid
9915 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
aa1ed4a9
JB
9916
9917 return TRUE;
9918}
9919
9920static bfd_boolean
9921elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
9922{
9923 size_t offset;
9924 size_t size;
24d3e51b 9925 size_t min_size;
aa1ed4a9 9926
24d3e51b
NC
9927 /* Compute offset of pr_getregsz, skipping over pr_statussz.
9928 Also compute minimum size of this note. */
b5430a3c 9929 switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
aa1ed4a9 9930 {
b5430a3c 9931 case ELFCLASS32:
24d3e51b
NC
9932 offset = 4 + 4;
9933 min_size = offset + (4 * 2) + 4 + 4 + 4;
aa1ed4a9
JB
9934 break;
9935
b5430a3c 9936 case ELFCLASS64:
24d3e51b
NC
9937 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */
9938 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
aa1ed4a9
JB
9939 break;
9940
9941 default:
9942 return FALSE;
9943 }
9944
24d3e51b
NC
9945 if (note->descsz < min_size)
9946 return FALSE;
9947
9948 /* Check for version 1 in pr_version. */
9949 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
9950 return FALSE;
aa1ed4a9 9951
24d3e51b
NC
9952 /* Extract size of pr_reg from pr_gregsetsz. */
9953 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
b5430a3c 9954 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
24d3e51b
NC
9955 {
9956 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
9957 offset += 4 * 2;
9958 }
b5430a3c 9959 else
24d3e51b
NC
9960 {
9961 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
9962 offset += 8 * 2;
9963 }
aa1ed4a9 9964
24d3e51b 9965 /* Skip over pr_osreldate. */
aa1ed4a9
JB
9966 offset += 4;
9967
24d3e51b 9968 /* Read signal from pr_cursig. */
aa1ed4a9
JB
9969 if (elf_tdata (abfd)->core->signal == 0)
9970 elf_tdata (abfd)->core->signal
9971 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
9972 offset += 4;
9973
24d3e51b 9974 /* Read TID from pr_pid. */
aa1ed4a9
JB
9975 elf_tdata (abfd)->core->lwpid
9976 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
9977 offset += 4;
9978
24d3e51b 9979 /* Padding before pr_reg. */
b5430a3c 9980 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
aa1ed4a9
JB
9981 offset += 4;
9982
24d3e51b
NC
9983 /* Make sure that there is enough data remaining in the note. */
9984 if ((note->descsz - offset) < size)
9985 return FALSE;
9986
aa1ed4a9
JB
9987 /* Make a ".reg/999" section and a ".reg" section. */
9988 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
9989 size, note->descpos + offset);
9990}
9991
9992static bfd_boolean
9993elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
9994{
544c67cd
JB
9995 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9996
aa1ed4a9
JB
9997 switch (note->type)
9998 {
9999 case NT_PRSTATUS:
544c67cd
JB
10000 if (bed->elf_backend_grok_freebsd_prstatus)
10001 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
10002 return TRUE;
aa1ed4a9
JB
10003 return elfcore_grok_freebsd_prstatus (abfd, note);
10004
10005 case NT_FPREGSET:
10006 return elfcore_grok_prfpreg (abfd, note);
10007
10008 case NT_PRPSINFO:
10009 return elfcore_grok_freebsd_psinfo (abfd, note);
10010
10011 case NT_FREEBSD_THRMISC:
10012 if (note->namesz == 8)
10013 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
10014 else
10015 return TRUE;
10016
3350c5f5
JB
10017 case NT_FREEBSD_PROCSTAT_AUXV:
10018 {
10019 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10020 SEC_HAS_CONTENTS);
10021
10022 if (sect == NULL)
10023 return FALSE;
10024 sect->size = note->descsz - 4;
10025 sect->filepos = note->descpos + 4;
10026 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10027
10028 return TRUE;
10029 }
10030
aa1ed4a9
JB
10031 case NT_X86_XSTATE:
10032 if (note->namesz == 8)
10033 return elfcore_grok_xstatereg (abfd, note);
10034 else
10035 return TRUE;
10036
e6f3b9c3
JB
10037 case NT_FREEBSD_PTLWPINFO:
10038 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
10039 note);
10040
6d5be5d6
JB
10041 case NT_ARM_VFP:
10042 return elfcore_grok_arm_vfp (abfd, note);
10043
aa1ed4a9
JB
10044 default:
10045 return TRUE;
10046 }
10047}
10048
b34976b6 10049static bfd_boolean
217aa764 10050elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
50b2bdb7
AM
10051{
10052 char *cp;
10053
10054 cp = strchr (note->namedata, '@');
10055 if (cp != NULL)
10056 {
d2b64500 10057 *lwpidp = atoi(cp + 1);
b34976b6 10058 return TRUE;
50b2bdb7 10059 }
b34976b6 10060 return FALSE;
50b2bdb7
AM
10061}
10062
b34976b6 10063static bfd_boolean
217aa764 10064elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7 10065{
80a04378
NC
10066 if (note->descsz <= 0x7c + 31)
10067 return FALSE;
10068
50b2bdb7 10069 /* Signal number at offset 0x08. */
228e534f 10070 elf_tdata (abfd)->core->signal
50b2bdb7
AM
10071 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10072
10073 /* Process ID at offset 0x50. */
228e534f 10074 elf_tdata (abfd)->core->pid
50b2bdb7
AM
10075 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
10076
10077 /* Command name at 0x7c (max 32 bytes, including nul). */
228e534f 10078 elf_tdata (abfd)->core->command
50b2bdb7
AM
10079 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
10080
7720ba9f
MK
10081 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
10082 note);
50b2bdb7
AM
10083}
10084
b34976b6 10085static bfd_boolean
217aa764 10086elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
50b2bdb7
AM
10087{
10088 int lwp;
10089
10090 if (elfcore_netbsd_get_lwpid (note, &lwp))
228e534f 10091 elf_tdata (abfd)->core->lwpid = lwp;
50b2bdb7 10092
b4db1224 10093 if (note->type == NT_NETBSDCORE_PROCINFO)
50b2bdb7
AM
10094 {
10095 /* NetBSD-specific core "procinfo". Note that we expect to
08a40648
AM
10096 find this note before any of the others, which is fine,
10097 since the kernel writes this note out first when it
10098 creates a core file. */
47d9a591 10099
50b2bdb7
AM
10100 return elfcore_grok_netbsd_procinfo (abfd, note);
10101 }
10102
b4db1224
JT
10103 /* As of Jan 2002 there are no other machine-independent notes
10104 defined for NetBSD core files. If the note type is less
10105 than the start of the machine-dependent note types, we don't
10106 understand it. */
47d9a591 10107
b4db1224 10108 if (note->type < NT_NETBSDCORE_FIRSTMACH)
b34976b6 10109 return TRUE;
50b2bdb7
AM
10110
10111
10112 switch (bfd_get_arch (abfd))
10113 {
08a40648
AM
10114 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10115 PT_GETFPREGS == mach+2. */
50b2bdb7
AM
10116
10117 case bfd_arch_alpha:
10118 case bfd_arch_sparc:
10119 switch (note->type)
08a40648
AM
10120 {
10121 case NT_NETBSDCORE_FIRSTMACH+0:
10122 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10123
08a40648
AM
10124 case NT_NETBSDCORE_FIRSTMACH+2:
10125 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10126
08a40648
AM
10127 default:
10128 return TRUE;
10129 }
50b2bdb7 10130
08a40648
AM
10131 /* On all other arch's, PT_GETREGS == mach+1 and
10132 PT_GETFPREGS == mach+3. */
50b2bdb7
AM
10133
10134 default:
10135 switch (note->type)
08a40648
AM
10136 {
10137 case NT_NETBSDCORE_FIRSTMACH+1:
10138 return elfcore_make_note_pseudosection (abfd, ".reg", note);
50b2bdb7 10139
08a40648
AM
10140 case NT_NETBSDCORE_FIRSTMACH+3:
10141 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
50b2bdb7 10142
08a40648
AM
10143 default:
10144 return TRUE;
10145 }
50b2bdb7
AM
10146 }
10147 /* NOTREACHED */
10148}
10149
67cc5033
MK
10150static bfd_boolean
10151elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
10152{
80a04378
NC
10153 if (note->descsz <= 0x48 + 31)
10154 return FALSE;
10155
67cc5033 10156 /* Signal number at offset 0x08. */
228e534f 10157 elf_tdata (abfd)->core->signal
67cc5033
MK
10158 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
10159
10160 /* Process ID at offset 0x20. */
228e534f 10161 elf_tdata (abfd)->core->pid
67cc5033
MK
10162 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
10163
10164 /* Command name at 0x48 (max 32 bytes, including nul). */
228e534f 10165 elf_tdata (abfd)->core->command
67cc5033
MK
10166 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
10167
10168 return TRUE;
10169}
10170
10171static bfd_boolean
10172elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
10173{
10174 if (note->type == NT_OPENBSD_PROCINFO)
10175 return elfcore_grok_openbsd_procinfo (abfd, note);
10176
10177 if (note->type == NT_OPENBSD_REGS)
10178 return elfcore_make_note_pseudosection (abfd, ".reg", note);
10179
10180 if (note->type == NT_OPENBSD_FPREGS)
10181 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
10182
10183 if (note->type == NT_OPENBSD_XFPREGS)
10184 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
10185
10186 if (note->type == NT_OPENBSD_AUXV)
10187 {
10188 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10189 SEC_HAS_CONTENTS);
10190
10191 if (sect == NULL)
10192 return FALSE;
10193 sect->size = note->descsz;
10194 sect->filepos = note->descpos;
10195 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10196
10197 return TRUE;
10198 }
10199
10200 if (note->type == NT_OPENBSD_WCOOKIE)
10201 {
10202 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
10203 SEC_HAS_CONTENTS);
10204
10205 if (sect == NULL)
10206 return FALSE;
10207 sect->size = note->descsz;
10208 sect->filepos = note->descpos;
10209 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10210
10211 return TRUE;
10212 }
10213
10214 return TRUE;
10215}
10216
07c6e936 10217static bfd_boolean
d3fd4074 10218elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
07c6e936
NC
10219{
10220 void *ddata = note->descdata;
10221 char buf[100];
10222 char *name;
10223 asection *sect;
f8843e87
AM
10224 short sig;
10225 unsigned flags;
07c6e936 10226
80a04378
NC
10227 if (note->descsz < 16)
10228 return FALSE;
10229
07c6e936 10230 /* nto_procfs_status 'pid' field is at offset 0. */
228e534f 10231 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
07c6e936 10232
f8843e87
AM
10233 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10234 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
10235
10236 /* nto_procfs_status 'flags' field is at offset 8. */
10237 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
07c6e936
NC
10238
10239 /* nto_procfs_status 'what' field is at offset 14. */
f8843e87
AM
10240 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
10241 {
228e534f
AM
10242 elf_tdata (abfd)->core->signal = sig;
10243 elf_tdata (abfd)->core->lwpid = *tid;
f8843e87 10244 }
07c6e936 10245
f8843e87
AM
10246 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10247 do not come from signals so we make sure we set the current
10248 thread just in case. */
10249 if (flags & 0x00000080)
228e534f 10250 elf_tdata (abfd)->core->lwpid = *tid;
07c6e936
NC
10251
10252 /* Make a ".qnx_core_status/%d" section. */
d3fd4074 10253 sprintf (buf, ".qnx_core_status/%ld", *tid);
07c6e936 10254
a50b1753 10255 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10256 if (name == NULL)
10257 return FALSE;
10258 strcpy (name, buf);
10259
117ed4f8 10260 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10261 if (sect == NULL)
10262 return FALSE;
10263
07d6d2b8
AM
10264 sect->size = note->descsz;
10265 sect->filepos = note->descpos;
07c6e936
NC
10266 sect->alignment_power = 2;
10267
10268 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
10269}
10270
10271static bfd_boolean
d69f560c
KW
10272elfcore_grok_nto_regs (bfd *abfd,
10273 Elf_Internal_Note *note,
d3fd4074 10274 long tid,
d69f560c 10275 char *base)
07c6e936
NC
10276{
10277 char buf[100];
10278 char *name;
10279 asection *sect;
10280
d69f560c 10281 /* Make a "(base)/%d" section. */
d3fd4074 10282 sprintf (buf, "%s/%ld", base, tid);
07c6e936 10283
a50b1753 10284 name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
07c6e936
NC
10285 if (name == NULL)
10286 return FALSE;
10287 strcpy (name, buf);
10288
117ed4f8 10289 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
07c6e936
NC
10290 if (sect == NULL)
10291 return FALSE;
10292
07d6d2b8
AM
10293 sect->size = note->descsz;
10294 sect->filepos = note->descpos;
07c6e936
NC
10295 sect->alignment_power = 2;
10296
f8843e87 10297 /* This is the current thread. */
228e534f 10298 if (elf_tdata (abfd)->core->lwpid == tid)
d69f560c 10299 return elfcore_maybe_make_sect (abfd, base, sect);
f8843e87
AM
10300
10301 return TRUE;
07c6e936
NC
10302}
10303
10304#define BFD_QNT_CORE_INFO 7
10305#define BFD_QNT_CORE_STATUS 8
10306#define BFD_QNT_CORE_GREG 9
10307#define BFD_QNT_CORE_FPREG 10
10308
10309static bfd_boolean
217aa764 10310elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
07c6e936
NC
10311{
10312 /* Every GREG section has a STATUS section before it. Store the
811072d8 10313 tid from the previous call to pass down to the next gregs
07c6e936 10314 function. */
d3fd4074 10315 static long tid = 1;
07c6e936
NC
10316
10317 switch (note->type)
10318 {
d69f560c
KW
10319 case BFD_QNT_CORE_INFO:
10320 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
10321 case BFD_QNT_CORE_STATUS:
10322 return elfcore_grok_nto_status (abfd, note, &tid);
10323 case BFD_QNT_CORE_GREG:
10324 return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
10325 case BFD_QNT_CORE_FPREG:
10326 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
10327 default:
10328 return TRUE;
07c6e936
NC
10329 }
10330}
10331
b15fa79e
AM
10332static bfd_boolean
10333elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
10334{
10335 char *name;
10336 asection *sect;
10337 size_t len;
10338
10339 /* Use note name as section name. */
10340 len = note->namesz;
a50b1753 10341 name = (char *) bfd_alloc (abfd, len);
b15fa79e
AM
10342 if (name == NULL)
10343 return FALSE;
10344 memcpy (name, note->namedata, len);
10345 name[len - 1] = '\0';
10346
10347 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10348 if (sect == NULL)
10349 return FALSE;
10350
07d6d2b8
AM
10351 sect->size = note->descsz;
10352 sect->filepos = note->descpos;
b15fa79e
AM
10353 sect->alignment_power = 1;
10354
10355 return TRUE;
10356}
10357
7c76fa91
MS
10358/* Function: elfcore_write_note
10359
47d9a591 10360 Inputs:
a39f3346 10361 buffer to hold note, and current size of buffer
7c76fa91
MS
10362 name of note
10363 type of note
10364 data for note
10365 size of data for note
10366
a39f3346
AM
10367 Writes note to end of buffer. ELF64 notes are written exactly as
10368 for ELF32, despite the current (as of 2006) ELF gabi specifying
10369 that they ought to have 8-byte namesz and descsz field, and have
10370 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10371
7c76fa91 10372 Return:
a39f3346 10373 Pointer to realloc'd buffer, *BUFSIZ updated. */
7c76fa91
MS
10374
10375char *
a39f3346 10376elfcore_write_note (bfd *abfd,
217aa764 10377 char *buf,
a39f3346 10378 int *bufsiz,
217aa764 10379 const char *name,
a39f3346 10380 int type,
217aa764 10381 const void *input,
a39f3346 10382 int size)
7c76fa91
MS
10383{
10384 Elf_External_Note *xnp;
d4c88bbb 10385 size_t namesz;
d4c88bbb 10386 size_t newspace;
a39f3346 10387 char *dest;
7c76fa91 10388
d4c88bbb 10389 namesz = 0;
d4c88bbb 10390 if (name != NULL)
a39f3346 10391 namesz = strlen (name) + 1;
d4c88bbb 10392
a39f3346 10393 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
d4c88bbb 10394
a50b1753 10395 buf = (char *) realloc (buf, *bufsiz + newspace);
14b1c01e
AM
10396 if (buf == NULL)
10397 return buf;
a39f3346 10398 dest = buf + *bufsiz;
7c76fa91
MS
10399 *bufsiz += newspace;
10400 xnp = (Elf_External_Note *) dest;
10401 H_PUT_32 (abfd, namesz, xnp->namesz);
10402 H_PUT_32 (abfd, size, xnp->descsz);
10403 H_PUT_32 (abfd, type, xnp->type);
d4c88bbb
AM
10404 dest = xnp->name;
10405 if (name != NULL)
10406 {
10407 memcpy (dest, name, namesz);
10408 dest += namesz;
a39f3346 10409 while (namesz & 3)
d4c88bbb
AM
10410 {
10411 *dest++ = '\0';
a39f3346 10412 ++namesz;
d4c88bbb
AM
10413 }
10414 }
10415 memcpy (dest, input, size);
a39f3346
AM
10416 dest += size;
10417 while (size & 3)
10418 {
10419 *dest++ = '\0';
10420 ++size;
10421 }
10422 return buf;
7c76fa91
MS
10423}
10424
7c76fa91 10425char *
217aa764
AM
10426elfcore_write_prpsinfo (bfd *abfd,
10427 char *buf,
10428 int *bufsiz,
10429 const char *fname,
10430 const char *psargs)
7c76fa91 10431{
183e98be
AM
10432 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10433
10434 if (bed->elf_backend_write_core_note != NULL)
10435 {
10436 char *ret;
10437 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10438 NT_PRPSINFO, fname, psargs);
10439 if (ret != NULL)
10440 return ret;
10441 }
7c76fa91 10442
1f20dca5 10443#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
183e98be
AM
10444#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10445 if (bed->s->elfclass == ELFCLASS32)
10446 {
10447#if defined (HAVE_PSINFO32_T)
10448 psinfo32_t data;
10449 int note_type = NT_PSINFO;
10450#else
10451 prpsinfo32_t data;
10452 int note_type = NT_PRPSINFO;
10453#endif
10454
10455 memset (&data, 0, sizeof (data));
10456 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10457 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10458 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 10459 "CORE", note_type, &data, sizeof (data));
183e98be
AM
10460 }
10461 else
10462#endif
10463 {
7c76fa91 10464#if defined (HAVE_PSINFO_T)
183e98be
AM
10465 psinfo_t data;
10466 int note_type = NT_PSINFO;
7c76fa91 10467#else
183e98be
AM
10468 prpsinfo_t data;
10469 int note_type = NT_PRPSINFO;
7c76fa91
MS
10470#endif
10471
183e98be
AM
10472 memset (&data, 0, sizeof (data));
10473 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
10474 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
10475 return elfcore_write_note (abfd, buf, bufsiz,
1f20dca5 10476 "CORE", note_type, &data, sizeof (data));
183e98be 10477 }
7c76fa91
MS
10478#endif /* PSINFO_T or PRPSINFO_T */
10479
1f20dca5
UW
10480 free (buf);
10481 return NULL;
10482}
10483
70a38d42
SDJ
10484char *
10485elfcore_write_linux_prpsinfo32
10486 (bfd *abfd, char *buf, int *bufsiz,
10487 const struct elf_internal_linux_prpsinfo *prpsinfo)
10488{
a2f63b2e
MR
10489 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
10490 {
10491 struct elf_external_linux_prpsinfo32_ugid16 data;
10492
10493 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
10494 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10495 &data, sizeof (data));
10496 }
10497 else
10498 {
10499 struct elf_external_linux_prpsinfo32_ugid32 data;
70a38d42 10500
a2f63b2e
MR
10501 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
10502 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
10503 &data, sizeof (data));
10504 }
70a38d42
SDJ
10505}
10506
10507char *
10508elfcore_write_linux_prpsinfo64
10509 (bfd *abfd, char *buf, int *bufsiz,
10510 const struct elf_internal_linux_prpsinfo *prpsinfo)
10511{
3c9a7b0d
MR
10512 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
10513 {
10514 struct elf_external_linux_prpsinfo64_ugid16 data;
10515
10516 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
10517 return elfcore_write_note (abfd, buf, bufsiz,
10518 "CORE", NT_PRPSINFO, &data, sizeof (data));
10519 }
10520 else
10521 {
10522 struct elf_external_linux_prpsinfo64_ugid32 data;
70a38d42 10523
3c9a7b0d
MR
10524 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
10525 return elfcore_write_note (abfd, buf, bufsiz,
10526 "CORE", NT_PRPSINFO, &data, sizeof (data));
10527 }
70a38d42
SDJ
10528}
10529
7c76fa91 10530char *
217aa764
AM
10531elfcore_write_prstatus (bfd *abfd,
10532 char *buf,
10533 int *bufsiz,
10534 long pid,
10535 int cursig,
10536 const void *gregs)
7c76fa91 10537{
183e98be 10538 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 10539
183e98be
AM
10540 if (bed->elf_backend_write_core_note != NULL)
10541 {
10542 char *ret;
10543 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
10544 NT_PRSTATUS,
10545 pid, cursig, gregs);
10546 if (ret != NULL)
10547 return ret;
10548 }
10549
1f20dca5 10550#if defined (HAVE_PRSTATUS_T)
183e98be
AM
10551#if defined (HAVE_PRSTATUS32_T)
10552 if (bed->s->elfclass == ELFCLASS32)
10553 {
10554 prstatus32_t prstat;
10555
10556 memset (&prstat, 0, sizeof (prstat));
10557 prstat.pr_pid = pid;
10558 prstat.pr_cursig = cursig;
10559 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 10560 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
10561 NT_PRSTATUS, &prstat, sizeof (prstat));
10562 }
10563 else
10564#endif
10565 {
10566 prstatus_t prstat;
10567
10568 memset (&prstat, 0, sizeof (prstat));
10569 prstat.pr_pid = pid;
10570 prstat.pr_cursig = cursig;
10571 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
1f20dca5 10572 return elfcore_write_note (abfd, buf, bufsiz, "CORE",
183e98be
AM
10573 NT_PRSTATUS, &prstat, sizeof (prstat));
10574 }
7c76fa91
MS
10575#endif /* HAVE_PRSTATUS_T */
10576
1f20dca5
UW
10577 free (buf);
10578 return NULL;
10579}
10580
51316059
MS
10581#if defined (HAVE_LWPSTATUS_T)
10582char *
217aa764
AM
10583elfcore_write_lwpstatus (bfd *abfd,
10584 char *buf,
10585 int *bufsiz,
10586 long pid,
10587 int cursig,
10588 const void *gregs)
51316059
MS
10589{
10590 lwpstatus_t lwpstat;
183e98be 10591 const char *note_name = "CORE";
51316059
MS
10592
10593 memset (&lwpstat, 0, sizeof (lwpstat));
10594 lwpstat.pr_lwpid = pid >> 16;
10595 lwpstat.pr_cursig = cursig;
10596#if defined (HAVE_LWPSTATUS_T_PR_REG)
d1e8523e 10597 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
51316059
MS
10598#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10599#if !defined(gregs)
10600 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
10601 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
10602#else
10603 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
10604 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
10605#endif
10606#endif
47d9a591 10607 return elfcore_write_note (abfd, buf, bufsiz, note_name,
51316059
MS
10608 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
10609}
10610#endif /* HAVE_LWPSTATUS_T */
10611
7c76fa91
MS
10612#if defined (HAVE_PSTATUS_T)
10613char *
217aa764
AM
10614elfcore_write_pstatus (bfd *abfd,
10615 char *buf,
10616 int *bufsiz,
10617 long pid,
6c10990d
NC
10618 int cursig ATTRIBUTE_UNUSED,
10619 const void *gregs ATTRIBUTE_UNUSED)
7c76fa91 10620{
183e98be
AM
10621 const char *note_name = "CORE";
10622#if defined (HAVE_PSTATUS32_T)
10623 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7c76fa91 10624
183e98be
AM
10625 if (bed->s->elfclass == ELFCLASS32)
10626 {
10627 pstatus32_t pstat;
10628
10629 memset (&pstat, 0, sizeof (pstat));
10630 pstat.pr_pid = pid & 0xffff;
10631 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
10632 NT_PSTATUS, &pstat, sizeof (pstat));
10633 return buf;
10634 }
10635 else
10636#endif
10637 {
10638 pstatus_t pstat;
10639
10640 memset (&pstat, 0, sizeof (pstat));
10641 pstat.pr_pid = pid & 0xffff;
10642 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
10643 NT_PSTATUS, &pstat, sizeof (pstat));
10644 return buf;
10645 }
7c76fa91
MS
10646}
10647#endif /* HAVE_PSTATUS_T */
10648
10649char *
217aa764
AM
10650elfcore_write_prfpreg (bfd *abfd,
10651 char *buf,
10652 int *bufsiz,
10653 const void *fpregs,
10654 int size)
7c76fa91 10655{
183e98be 10656 const char *note_name = "CORE";
47d9a591 10657 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
10658 note_name, NT_FPREGSET, fpregs, size);
10659}
10660
10661char *
217aa764
AM
10662elfcore_write_prxfpreg (bfd *abfd,
10663 char *buf,
10664 int *bufsiz,
10665 const void *xfpregs,
10666 int size)
7c76fa91
MS
10667{
10668 char *note_name = "LINUX";
47d9a591 10669 return elfcore_write_note (abfd, buf, bufsiz,
7c76fa91
MS
10670 note_name, NT_PRXFPREG, xfpregs, size);
10671}
10672
4339cae0
L
10673char *
10674elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
10675 const void *xfpregs, int size)
10676{
97de3545
JB
10677 char *note_name;
10678 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
10679 note_name = "FreeBSD";
10680 else
10681 note_name = "LINUX";
4339cae0
L
10682 return elfcore_write_note (abfd, buf, bufsiz,
10683 note_name, NT_X86_XSTATE, xfpregs, size);
10684}
10685
97753bd5
AM
10686char *
10687elfcore_write_ppc_vmx (bfd *abfd,
10688 char *buf,
10689 int *bufsiz,
10690 const void *ppc_vmx,
10691 int size)
10692{
10693 char *note_name = "LINUX";
10694 return elfcore_write_note (abfd, buf, bufsiz,
10695 note_name, NT_PPC_VMX, ppc_vmx, size);
10696}
10697
89eeb0bc
LM
10698char *
10699elfcore_write_ppc_vsx (bfd *abfd,
07d6d2b8
AM
10700 char *buf,
10701 int *bufsiz,
10702 const void *ppc_vsx,
10703 int size)
89eeb0bc
LM
10704{
10705 char *note_name = "LINUX";
10706 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10707 note_name, NT_PPC_VSX, ppc_vsx, size);
89eeb0bc
LM
10708}
10709
0675e188
UW
10710static char *
10711elfcore_write_s390_high_gprs (bfd *abfd,
10712 char *buf,
10713 int *bufsiz,
10714 const void *s390_high_gprs,
10715 int size)
10716{
10717 char *note_name = "LINUX";
10718 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10719 note_name, NT_S390_HIGH_GPRS,
0675e188
UW
10720 s390_high_gprs, size);
10721}
10722
d7eeb400
MS
10723char *
10724elfcore_write_s390_timer (bfd *abfd,
07d6d2b8
AM
10725 char *buf,
10726 int *bufsiz,
10727 const void *s390_timer,
10728 int size)
d7eeb400
MS
10729{
10730 char *note_name = "LINUX";
10731 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10732 note_name, NT_S390_TIMER, s390_timer, size);
d7eeb400
MS
10733}
10734
10735char *
10736elfcore_write_s390_todcmp (bfd *abfd,
07d6d2b8
AM
10737 char *buf,
10738 int *bufsiz,
10739 const void *s390_todcmp,
10740 int size)
d7eeb400
MS
10741{
10742 char *note_name = "LINUX";
10743 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10744 note_name, NT_S390_TODCMP, s390_todcmp, size);
d7eeb400
MS
10745}
10746
10747char *
10748elfcore_write_s390_todpreg (bfd *abfd,
07d6d2b8
AM
10749 char *buf,
10750 int *bufsiz,
10751 const void *s390_todpreg,
10752 int size)
d7eeb400
MS
10753{
10754 char *note_name = "LINUX";
10755 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10756 note_name, NT_S390_TODPREG, s390_todpreg, size);
d7eeb400
MS
10757}
10758
10759char *
10760elfcore_write_s390_ctrs (bfd *abfd,
07d6d2b8
AM
10761 char *buf,
10762 int *bufsiz,
10763 const void *s390_ctrs,
10764 int size)
d7eeb400
MS
10765{
10766 char *note_name = "LINUX";
10767 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10768 note_name, NT_S390_CTRS, s390_ctrs, size);
d7eeb400
MS
10769}
10770
10771char *
10772elfcore_write_s390_prefix (bfd *abfd,
07d6d2b8
AM
10773 char *buf,
10774 int *bufsiz,
10775 const void *s390_prefix,
10776 int size)
d7eeb400
MS
10777{
10778 char *note_name = "LINUX";
10779 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10780 note_name, NT_S390_PREFIX, s390_prefix, size);
d7eeb400
MS
10781}
10782
355b81d9
UW
10783char *
10784elfcore_write_s390_last_break (bfd *abfd,
10785 char *buf,
10786 int *bufsiz,
10787 const void *s390_last_break,
10788 int size)
10789{
10790 char *note_name = "LINUX";
10791 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10792 note_name, NT_S390_LAST_BREAK,
355b81d9
UW
10793 s390_last_break, size);
10794}
10795
10796char *
10797elfcore_write_s390_system_call (bfd *abfd,
10798 char *buf,
10799 int *bufsiz,
10800 const void *s390_system_call,
10801 int size)
10802{
10803 char *note_name = "LINUX";
10804 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10805 note_name, NT_S390_SYSTEM_CALL,
355b81d9
UW
10806 s390_system_call, size);
10807}
10808
abb3f6cc
NC
10809char *
10810elfcore_write_s390_tdb (bfd *abfd,
10811 char *buf,
10812 int *bufsiz,
10813 const void *s390_tdb,
10814 int size)
10815{
10816 char *note_name = "LINUX";
10817 return elfcore_write_note (abfd, buf, bufsiz,
07d6d2b8 10818 note_name, NT_S390_TDB, s390_tdb, size);
abb3f6cc
NC
10819}
10820
4ef9f41a
AA
10821char *
10822elfcore_write_s390_vxrs_low (bfd *abfd,
10823 char *buf,
10824 int *bufsiz,
10825 const void *s390_vxrs_low,
10826 int size)
10827{
10828 char *note_name = "LINUX";
10829 return elfcore_write_note (abfd, buf, bufsiz,
10830 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
10831}
10832
10833char *
10834elfcore_write_s390_vxrs_high (bfd *abfd,
10835 char *buf,
10836 int *bufsiz,
10837 const void *s390_vxrs_high,
10838 int size)
10839{
10840 char *note_name = "LINUX";
10841 return elfcore_write_note (abfd, buf, bufsiz,
10842 note_name, NT_S390_VXRS_HIGH,
10843 s390_vxrs_high, size);
10844}
10845
88ab90e8
AA
10846char *
10847elfcore_write_s390_gs_cb (bfd *abfd,
10848 char *buf,
10849 int *bufsiz,
10850 const void *s390_gs_cb,
10851 int size)
10852{
10853 char *note_name = "LINUX";
10854 return elfcore_write_note (abfd, buf, bufsiz,
10855 note_name, NT_S390_GS_CB,
10856 s390_gs_cb, size);
10857}
10858
10859char *
10860elfcore_write_s390_gs_bc (bfd *abfd,
10861 char *buf,
10862 int *bufsiz,
10863 const void *s390_gs_bc,
10864 int size)
10865{
10866 char *note_name = "LINUX";
10867 return elfcore_write_note (abfd, buf, bufsiz,
10868 note_name, NT_S390_GS_BC,
10869 s390_gs_bc, size);
10870}
10871
faa9a424
UW
10872char *
10873elfcore_write_arm_vfp (bfd *abfd,
10874 char *buf,
10875 int *bufsiz,
10876 const void *arm_vfp,
10877 int size)
10878{
10879 char *note_name = "LINUX";
10880 return elfcore_write_note (abfd, buf, bufsiz,
10881 note_name, NT_ARM_VFP, arm_vfp, size);
10882}
10883
652451f8
YZ
10884char *
10885elfcore_write_aarch_tls (bfd *abfd,
10886 char *buf,
10887 int *bufsiz,
10888 const void *aarch_tls,
10889 int size)
10890{
10891 char *note_name = "LINUX";
10892 return elfcore_write_note (abfd, buf, bufsiz,
10893 note_name, NT_ARM_TLS, aarch_tls, size);
10894}
10895
10896char *
10897elfcore_write_aarch_hw_break (bfd *abfd,
10898 char *buf,
10899 int *bufsiz,
10900 const void *aarch_hw_break,
10901 int size)
10902{
10903 char *note_name = "LINUX";
10904 return elfcore_write_note (abfd, buf, bufsiz,
10905 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
10906}
10907
10908char *
10909elfcore_write_aarch_hw_watch (bfd *abfd,
10910 char *buf,
10911 int *bufsiz,
10912 const void *aarch_hw_watch,
10913 int size)
10914{
10915 char *note_name = "LINUX";
10916 return elfcore_write_note (abfd, buf, bufsiz,
10917 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
10918}
10919
bb864ac1
CES
10920char *
10921elfcore_write_register_note (bfd *abfd,
10922 char *buf,
10923 int *bufsiz,
10924 const char *section,
10925 const void *data,
10926 int size)
10927{
10928 if (strcmp (section, ".reg2") == 0)
10929 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
10930 if (strcmp (section, ".reg-xfp") == 0)
10931 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
4339cae0
L
10932 if (strcmp (section, ".reg-xstate") == 0)
10933 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
bb864ac1
CES
10934 if (strcmp (section, ".reg-ppc-vmx") == 0)
10935 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
89eeb0bc
LM
10936 if (strcmp (section, ".reg-ppc-vsx") == 0)
10937 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
0675e188
UW
10938 if (strcmp (section, ".reg-s390-high-gprs") == 0)
10939 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
d7eeb400
MS
10940 if (strcmp (section, ".reg-s390-timer") == 0)
10941 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
10942 if (strcmp (section, ".reg-s390-todcmp") == 0)
10943 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
10944 if (strcmp (section, ".reg-s390-todpreg") == 0)
10945 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
10946 if (strcmp (section, ".reg-s390-ctrs") == 0)
10947 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
10948 if (strcmp (section, ".reg-s390-prefix") == 0)
10949 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
355b81d9
UW
10950 if (strcmp (section, ".reg-s390-last-break") == 0)
10951 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
10952 if (strcmp (section, ".reg-s390-system-call") == 0)
10953 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
abb3f6cc
NC
10954 if (strcmp (section, ".reg-s390-tdb") == 0)
10955 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
4ef9f41a
AA
10956 if (strcmp (section, ".reg-s390-vxrs-low") == 0)
10957 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
10958 if (strcmp (section, ".reg-s390-vxrs-high") == 0)
10959 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
88ab90e8
AA
10960 if (strcmp (section, ".reg-s390-gs-cb") == 0)
10961 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size);
10962 if (strcmp (section, ".reg-s390-gs-bc") == 0)
10963 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size);
faa9a424
UW
10964 if (strcmp (section, ".reg-arm-vfp") == 0)
10965 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
652451f8
YZ
10966 if (strcmp (section, ".reg-aarch-tls") == 0)
10967 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
10968 if (strcmp (section, ".reg-aarch-hw-break") == 0)
10969 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
10970 if (strcmp (section, ".reg-aarch-hw-watch") == 0)
10971 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
bb864ac1
CES
10972 return NULL;
10973}
10974
b34976b6 10975static bfd_boolean
276da9b3
L
10976elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
10977 size_t align)
252b5132 10978{
c044fabd 10979 char *p;
252b5132 10980
276da9b3
L
10981 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
10982 gABI specifies that PT_NOTE alignment should be aligned to 4
10983 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
10984 align is less than 4, we use 4 byte alignment. */
10985 if (align < 4)
10986 align = 4;
10987
252b5132
RH
10988 p = buf;
10989 while (p < buf + size)
10990 {
c044fabd 10991 Elf_External_Note *xnp = (Elf_External_Note *) p;
252b5132
RH
10992 Elf_Internal_Note in;
10993
baea7ef1
AM
10994 if (offsetof (Elf_External_Note, name) > buf - p + size)
10995 return FALSE;
10996
dc810e39 10997 in.type = H_GET_32 (abfd, xnp->type);
252b5132 10998
dc810e39 10999 in.namesz = H_GET_32 (abfd, xnp->namesz);
252b5132 11000 in.namedata = xnp->name;
baea7ef1
AM
11001 if (in.namesz > buf - in.namedata + size)
11002 return FALSE;
252b5132 11003
dc810e39 11004 in.descsz = H_GET_32 (abfd, xnp->descsz);
276da9b3 11005 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
252b5132 11006 in.descpos = offset + (in.descdata - buf);
baea7ef1
AM
11007 if (in.descsz != 0
11008 && (in.descdata >= buf + size
11009 || in.descsz > buf - in.descdata + size))
11010 return FALSE;
252b5132 11011
718175fa 11012 switch (bfd_get_format (abfd))
07d6d2b8 11013 {
718175fa
JK
11014 default:
11015 return TRUE;
11016
11017 case bfd_core:
f64e188b 11018 {
8acbedd6 11019#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
f64e188b 11020 struct
718175fa 11021 {
f64e188b 11022 const char * string;
8acbedd6 11023 size_t len;
f64e188b 11024 bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
718175fa 11025 }
f64e188b 11026 grokers[] =
b15fa79e 11027 {
8acbedd6 11028 GROKER_ELEMENT ("", elfcore_grok_note),
aa1ed4a9 11029 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note),
8acbedd6
KS
11030 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
11031 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
11032 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
11033 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
f64e188b 11034 };
8acbedd6 11035#undef GROKER_ELEMENT
f64e188b
NC
11036 int i;
11037
11038 for (i = ARRAY_SIZE (grokers); i--;)
8acbedd6
KS
11039 {
11040 if (in.namesz >= grokers[i].len
11041 && strncmp (in.namedata, grokers[i].string,
11042 grokers[i].len) == 0)
11043 {
11044 if (! grokers[i].func (abfd, & in))
11045 return FALSE;
11046 break;
11047 }
11048 }
f64e188b
NC
11049 break;
11050 }
718175fa
JK
11051
11052 case bfd_object:
11053 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
11054 {
11055 if (! elfobj_grok_gnu_note (abfd, &in))
11056 return FALSE;
11057 }
e21e5835
NC
11058 else if (in.namesz == sizeof "stapsdt"
11059 && strcmp (in.namedata, "stapsdt") == 0)
11060 {
11061 if (! elfobj_grok_stapsdt_note (abfd, &in))
11062 return FALSE;
11063 }
718175fa 11064 break;
08a40648 11065 }
252b5132 11066
276da9b3 11067 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
252b5132
RH
11068 }
11069
718175fa
JK
11070 return TRUE;
11071}
11072
11073static bfd_boolean
276da9b3
L
11074elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
11075 size_t align)
718175fa
JK
11076{
11077 char *buf;
11078
957e1fc1 11079 if (size == 0 || (size + 1) == 0)
718175fa
JK
11080 return TRUE;
11081
11082 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
11083 return FALSE;
11084
f64e188b 11085 buf = (char *) bfd_malloc (size + 1);
718175fa
JK
11086 if (buf == NULL)
11087 return FALSE;
11088
f64e188b
NC
11089 /* PR 17512: file: ec08f814
11090 0-termintate the buffer so that string searches will not overflow. */
11091 buf[size] = 0;
11092
718175fa 11093 if (bfd_bread (buf, size, abfd) != size
276da9b3 11094 || !elf_parse_notes (abfd, buf, size, offset, align))
718175fa
JK
11095 {
11096 free (buf);
11097 return FALSE;
11098 }
11099
252b5132 11100 free (buf);
b34976b6 11101 return TRUE;
252b5132 11102}
98d8431c
JB
11103\f
11104/* Providing external access to the ELF program header table. */
11105
11106/* Return an upper bound on the number of bytes required to store a
11107 copy of ABFD's program header table entries. Return -1 if an error
11108 occurs; bfd_get_error will return an appropriate code. */
c044fabd 11109
98d8431c 11110long
217aa764 11111bfd_get_elf_phdr_upper_bound (bfd *abfd)
98d8431c
JB
11112{
11113 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11114 {
11115 bfd_set_error (bfd_error_wrong_format);
11116 return -1;
11117 }
11118
936e320b 11119 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
98d8431c
JB
11120}
11121
98d8431c
JB
11122/* Copy ABFD's program header table entries to *PHDRS. The entries
11123 will be stored as an array of Elf_Internal_Phdr structures, as
11124 defined in include/elf/internal.h. To find out how large the
11125 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11126
11127 Return the number of program header table entries read, or -1 if an
11128 error occurs; bfd_get_error will return an appropriate code. */
c044fabd 11129
98d8431c 11130int
217aa764 11131bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
98d8431c
JB
11132{
11133 int num_phdrs;
11134
11135 if (abfd->xvec->flavour != bfd_target_elf_flavour)
11136 {
11137 bfd_set_error (bfd_error_wrong_format);
11138 return -1;
11139 }
11140
11141 num_phdrs = elf_elfheader (abfd)->e_phnum;
c044fabd 11142 memcpy (phdrs, elf_tdata (abfd)->phdr,
98d8431c
JB
11143 num_phdrs * sizeof (Elf_Internal_Phdr));
11144
11145 return num_phdrs;
11146}
ae4221d7 11147
db6751f2 11148enum elf_reloc_type_class
7e612e98
AM
11149_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
11150 const asection *rel_sec ATTRIBUTE_UNUSED,
11151 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
db6751f2
JJ
11152{
11153 return reloc_class_normal;
11154}
f8df10f4 11155
47d9a591 11156/* For RELA architectures, return the relocation value for a
f8df10f4
JJ
11157 relocation against a local symbol. */
11158
11159bfd_vma
217aa764
AM
11160_bfd_elf_rela_local_sym (bfd *abfd,
11161 Elf_Internal_Sym *sym,
8517fae7 11162 asection **psec,
217aa764 11163 Elf_Internal_Rela *rel)
f8df10f4 11164{
8517fae7 11165 asection *sec = *psec;
f8df10f4
JJ
11166 bfd_vma relocation;
11167
11168 relocation = (sec->output_section->vma
11169 + sec->output_offset
11170 + sym->st_value);
11171 if ((sec->flags & SEC_MERGE)
c629eae0 11172 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
dbaa2011 11173 && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
f8df10f4 11174 {
f8df10f4 11175 rel->r_addend =
8517fae7 11176 _bfd_merged_section_offset (abfd, psec,
65765700 11177 elf_section_data (sec)->sec_info,
753731ee
AM
11178 sym->st_value + rel->r_addend);
11179 if (sec != *psec)
11180 {
11181 /* If we have changed the section, and our original section is
11182 marked with SEC_EXCLUDE, it means that the original
11183 SEC_MERGE section has been completely subsumed in some
11184 other SEC_MERGE section. In this case, we need to leave
11185 some info around for --emit-relocs. */
11186 if ((sec->flags & SEC_EXCLUDE) != 0)
11187 sec->kept_section = *psec;
11188 sec = *psec;
11189 }
8517fae7
AM
11190 rel->r_addend -= relocation;
11191 rel->r_addend += sec->output_section->vma + sec->output_offset;
f8df10f4
JJ
11192 }
11193 return relocation;
11194}
c629eae0
JJ
11195
11196bfd_vma
217aa764
AM
11197_bfd_elf_rel_local_sym (bfd *abfd,
11198 Elf_Internal_Sym *sym,
11199 asection **psec,
11200 bfd_vma addend)
47d9a591 11201{
c629eae0
JJ
11202 asection *sec = *psec;
11203
dbaa2011 11204 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
c629eae0
JJ
11205 return sym->st_value + addend;
11206
11207 return _bfd_merged_section_offset (abfd, psec,
65765700 11208 elf_section_data (sec)->sec_info,
753731ee 11209 sym->st_value + addend);
c629eae0
JJ
11210}
11211
37b01f6a
DG
11212/* Adjust an address within a section. Given OFFSET within SEC, return
11213 the new offset within the section, based upon changes made to the
11214 section. Returns -1 if the offset is now invalid.
11215 The offset (in abnd out) is in target sized bytes, however big a
11216 byte may be. */
11217
c629eae0 11218bfd_vma
217aa764 11219_bfd_elf_section_offset (bfd *abfd,
92e4ec35 11220 struct bfd_link_info *info,
217aa764
AM
11221 asection *sec,
11222 bfd_vma offset)
c629eae0 11223{
68bfbfcc 11224 switch (sec->sec_info_type)
65765700 11225 {
dbaa2011 11226 case SEC_INFO_TYPE_STABS:
eea6121a
AM
11227 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
11228 offset);
dbaa2011 11229 case SEC_INFO_TYPE_EH_FRAME:
92e4ec35 11230 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
37b01f6a 11231
65765700 11232 default:
310fd250
L
11233 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
11234 {
37b01f6a 11235 /* Reverse the offset. */
310fd250
L
11236 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11237 bfd_size_type address_size = bed->s->arch_size / 8;
37b01f6a
DG
11238
11239 /* address_size and sec->size are in octets. Convert
11240 to bytes before subtracting the original offset. */
11241 offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset;
310fd250 11242 }
65765700
JJ
11243 return offset;
11244 }
c629eae0 11245}
3333a7c3
RM
11246\f
11247/* Create a new BFD as if by bfd_openr. Rather than opening a file,
11248 reconstruct an ELF file by reading the segments out of remote memory
11249 based on the ELF file header at EHDR_VMA and the ELF program headers it
11250 points to. If not null, *LOADBASEP is filled in with the difference
11251 between the VMAs from which the segments were read, and the VMAs the
11252 file headers (and hence BFD's idea of each section's VMA) put them at.
11253
11254 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11255 remote memory at target address VMA into the local buffer at MYADDR; it
11256 should return zero on success or an `errno' code on failure. TEMPL must
11257 be a BFD for an ELF target with the word size and byte order found in
11258 the remote memory. */
11259
11260bfd *
217aa764
AM
11261bfd_elf_bfd_from_remote_memory
11262 (bfd *templ,
11263 bfd_vma ehdr_vma,
f0a5d95a 11264 bfd_size_type size,
217aa764 11265 bfd_vma *loadbasep,
fe78531d 11266 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
3333a7c3
RM
11267{
11268 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
5979d6b6 11269 (templ, ehdr_vma, size, loadbasep, target_read_memory);
3333a7c3 11270}
4c45e5c9
JJ
11271\f
11272long
c9727e01
AM
11273_bfd_elf_get_synthetic_symtab (bfd *abfd,
11274 long symcount ATTRIBUTE_UNUSED,
11275 asymbol **syms ATTRIBUTE_UNUSED,
8615f3f2 11276 long dynsymcount,
c9727e01
AM
11277 asymbol **dynsyms,
11278 asymbol **ret)
4c45e5c9
JJ
11279{
11280 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
11281 asection *relplt;
11282 asymbol *s;
11283 const char *relplt_name;
11284 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
11285 arelent *p;
11286 long count, i, n;
11287 size_t size;
11288 Elf_Internal_Shdr *hdr;
11289 char *names;
11290 asection *plt;
11291
8615f3f2
AM
11292 *ret = NULL;
11293
90e3cdf2
JJ
11294 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
11295 return 0;
11296
8615f3f2
AM
11297 if (dynsymcount <= 0)
11298 return 0;
11299
4c45e5c9
JJ
11300 if (!bed->plt_sym_val)
11301 return 0;
11302
11303 relplt_name = bed->relplt_name;
11304 if (relplt_name == NULL)
d35fd659 11305 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
4c45e5c9
JJ
11306 relplt = bfd_get_section_by_name (abfd, relplt_name);
11307 if (relplt == NULL)
11308 return 0;
11309
11310 hdr = &elf_section_data (relplt)->this_hdr;
11311 if (hdr->sh_link != elf_dynsymtab (abfd)
11312 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
11313 return 0;
11314
11315 plt = bfd_get_section_by_name (abfd, ".plt");
11316 if (plt == NULL)
11317 return 0;
11318
11319 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
c9727e01 11320 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
4c45e5c9
JJ
11321 return -1;
11322
eea6121a 11323 count = relplt->size / hdr->sh_entsize;
4c45e5c9
JJ
11324 size = count * sizeof (asymbol);
11325 p = relplt->relocation;
cb53bf42 11326 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
041de40d
AM
11327 {
11328 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
11329 if (p->addend != 0)
11330 {
11331#ifdef BFD64
11332 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
11333#else
11334 size += sizeof ("+0x") - 1 + 8;
11335#endif
11336 }
11337 }
4c45e5c9 11338
a50b1753 11339 s = *ret = (asymbol *) bfd_malloc (size);
4c45e5c9
JJ
11340 if (s == NULL)
11341 return -1;
11342
11343 names = (char *) (s + count);
11344 p = relplt->relocation;
11345 n = 0;
cb53bf42 11346 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
4c45e5c9
JJ
11347 {
11348 size_t len;
11349 bfd_vma addr;
11350
11351 addr = bed->plt_sym_val (i, plt, p);
11352 if (addr == (bfd_vma) -1)
11353 continue;
11354
11355 *s = **p->sym_ptr_ptr;
65a7a66f
AM
11356 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11357 we are defining a symbol, ensure one of them is set. */
11358 if ((s->flags & BSF_LOCAL) == 0)
11359 s->flags |= BSF_GLOBAL;
6ba2a415 11360 s->flags |= BSF_SYNTHETIC;
4c45e5c9
JJ
11361 s->section = plt;
11362 s->value = addr - plt->vma;
11363 s->name = names;
8f39ba8e 11364 s->udata.p = NULL;
4c45e5c9
JJ
11365 len = strlen ((*p->sym_ptr_ptr)->name);
11366 memcpy (names, (*p->sym_ptr_ptr)->name, len);
11367 names += len;
041de40d
AM
11368 if (p->addend != 0)
11369 {
1d770845 11370 char buf[30], *a;
d324f6d6 11371
041de40d
AM
11372 memcpy (names, "+0x", sizeof ("+0x") - 1);
11373 names += sizeof ("+0x") - 1;
1d770845
L
11374 bfd_sprintf_vma (abfd, buf, p->addend);
11375 for (a = buf; *a == '0'; ++a)
11376 ;
11377 len = strlen (a);
11378 memcpy (names, a, len);
11379 names += len;
041de40d 11380 }
4c45e5c9
JJ
11381 memcpy (names, "@plt", sizeof ("@plt"));
11382 names += sizeof ("@plt");
8f39ba8e 11383 ++s, ++n;
4c45e5c9
JJ
11384 }
11385
11386 return n;
11387}
3d7f7666 11388
821e6ff6
AM
11389/* It is only used by x86-64 so far.
11390 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
7eacd66b
AM
11391 but current usage would allow all of _bfd_std_section to be zero. */
11392static const asymbol lcomm_sym
11393 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section);
3b22753a 11394asection _bfd_elf_large_com_section
7eacd66b 11395 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym,
821e6ff6 11396 "LARGE_COMMON", 0, SEC_IS_COMMON);
ecca9871 11397
d1036acb 11398void
78245035
L
11399_bfd_elf_post_process_headers (bfd * abfd,
11400 struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
d1036acb
L
11401{
11402 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
11403
11404 i_ehdrp = elf_elfheader (abfd);
11405
11406 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
d8045f23
NC
11407
11408 /* To make things simpler for the loader on Linux systems we set the
9c55345c 11409 osabi field to ELFOSABI_GNU if the binary contains symbols of
f64b2e8d 11410 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
d8045f23 11411 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
f64b2e8d 11412 && elf_tdata (abfd)->has_gnu_symbols)
9c55345c 11413 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
d1036acb 11414}
fcb93ecf
PB
11415
11416
11417/* Return TRUE for ELF symbol types that represent functions.
11418 This is the default version of this function, which is sufficient for
d8045f23 11419 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
fcb93ecf
PB
11420
11421bfd_boolean
11422_bfd_elf_is_function_type (unsigned int type)
11423{
d8045f23
NC
11424 return (type == STT_FUNC
11425 || type == STT_GNU_IFUNC);
fcb93ecf 11426}
9f296da3 11427
aef36ac1
AM
11428/* If the ELF symbol SYM might be a function in SEC, return the
11429 function size and set *CODE_OFF to the function's entry point,
11430 otherwise return zero. */
9f296da3 11431
aef36ac1
AM
11432bfd_size_type
11433_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
11434 bfd_vma *code_off)
9f296da3 11435{
aef36ac1
AM
11436 bfd_size_type size;
11437
ff9e0f5b 11438 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
aef36ac1
AM
11439 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
11440 || sym->section != sec)
11441 return 0;
ff9e0f5b 11442
ff9e0f5b 11443 *code_off = sym->value;
aef36ac1
AM
11444 size = 0;
11445 if (!(sym->flags & BSF_SYNTHETIC))
11446 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
11447 if (size == 0)
11448 size = 1;
11449 return size;
9f296da3 11450}