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