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