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