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