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