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