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