]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / bfd / elf.c
CommitLineData
32090b8e 1/* ELF executable support for BFD.
f6727b90 2 Copyright 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
32090b8e
KR
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
6f904fce 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32090b8e 19
d1b44e83
ILT
20/*
21
22SECTION
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
31 haven't bothered yet.
32 */
33
32090b8e
KR
34#include "bfd.h"
35#include "sysdep.h"
013dec1a 36#include "bfdlink.h"
32090b8e
KR
37#include "libbfd.h"
38#define ARCH_SIZE 0
6ab826bd 39#include "elf-bfd.h"
32090b8e 40
fd0198f0 41static INLINE struct elf_segment_map *make_mapping
edf3fe48 42 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
191d910c 43static boolean map_sections_to_segments PARAMS ((bfd *));
fd0198f0
ILT
44static int elf_sort_sections PARAMS ((const PTR, const PTR));
45static boolean assign_file_positions_for_segments PARAMS ((bfd *));
46static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
ede4eed4 47static boolean prep_headers PARAMS ((bfd *));
37fcafe6 48static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
3dbf33ee 49static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
ea3f0585
FF
50static char *elf_read PARAMS ((bfd *, long, unsigned int));
51static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
52static boolean assign_section_numbers PARAMS ((bfd *));
53static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
54static boolean elf_map_symbols PARAMS ((bfd *));
55static bfd_size_type get_program_header_size PARAMS ((bfd *));
ede4eed4 56
a66a61a0
ILT
57/* Swap version information in and out. The version information is
58 currently size independent. If that ever changes, this code will
59 need to move into elfcode.h. */
60
61/* Swap in a Verdef structure. */
62
63void
64_bfd_elf_swap_verdef_in (abfd, src, dst)
65 bfd *abfd;
66 const Elf_External_Verdef *src;
67 Elf_Internal_Verdef *dst;
68{
69 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
70 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
71 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
72 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
73 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
74 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
75 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
76}
77
78/* Swap out a Verdef structure. */
79
80void
81_bfd_elf_swap_verdef_out (abfd, src, dst)
82 bfd *abfd;
83 const Elf_Internal_Verdef *src;
84 Elf_External_Verdef *dst;
85{
86 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
87 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
88 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
91 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
92 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
93}
94
95/* Swap in a Verdaux structure. */
96
97void
98_bfd_elf_swap_verdaux_in (abfd, src, dst)
99 bfd *abfd;
100 const Elf_External_Verdaux *src;
101 Elf_Internal_Verdaux *dst;
102{
103 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
104 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
105}
106
107/* Swap out a Verdaux structure. */
108
109void
110_bfd_elf_swap_verdaux_out (abfd, src, dst)
111 bfd *abfd;
112 const Elf_Internal_Verdaux *src;
113 Elf_External_Verdaux *dst;
114{
115 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
116 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
117}
118
119/* Swap in a Verneed structure. */
120
121void
122_bfd_elf_swap_verneed_in (abfd, src, dst)
123 bfd *abfd;
124 const Elf_External_Verneed *src;
125 Elf_Internal_Verneed *dst;
126{
127 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
128 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
129 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
130 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
131 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
132}
133
134/* Swap out a Verneed structure. */
135
136void
137_bfd_elf_swap_verneed_out (abfd, src, dst)
138 bfd *abfd;
139 const Elf_Internal_Verneed *src;
140 Elf_External_Verneed *dst;
141{
142 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
143 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
144 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
145 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
146 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
147}
148
149/* Swap in a Vernaux structure. */
150
151void
152_bfd_elf_swap_vernaux_in (abfd, src, dst)
153 bfd *abfd;
154 const Elf_External_Vernaux *src;
155 Elf_Internal_Vernaux *dst;
156{
157 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
158 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
159 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
160 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
161 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
162}
163
164/* Swap out a Vernaux structure. */
165
166void
167_bfd_elf_swap_vernaux_out (abfd, src, dst)
168 bfd *abfd;
169 const Elf_Internal_Vernaux *src;
170 Elf_External_Vernaux *dst;
171{
172 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
173 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
174 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
175 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
176 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
177}
178
179/* Swap in a Versym structure. */
180
181void
182_bfd_elf_swap_versym_in (abfd, src, dst)
183 bfd *abfd;
184 const Elf_External_Versym *src;
185 Elf_Internal_Versym *dst;
186{
187 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
188}
189
190/* Swap out a Versym structure. */
191
192void
193_bfd_elf_swap_versym_out (abfd, src, dst)
194 bfd *abfd;
195 const Elf_Internal_Versym *src;
196 Elf_External_Versym *dst;
197{
198 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
199}
200
32090b8e 201/* Standard ELF hash function. Do not change this function; you will
eaa57a10 202 cause invalid hash tables to be generated. */
32090b8e 203unsigned long
013dec1a
ILT
204bfd_elf_hash (name)
205 CONST unsigned char *name;
32090b8e
KR
206{
207 unsigned long h = 0;
208 unsigned long g;
209 int ch;
210
211 while ((ch = *name++) != '\0')
212 {
213 h = (h << 4) + ch;
214 if ((g = (h & 0xf0000000)) != 0)
215 {
216 h ^= g >> 24;
eaa57a10
UD
217 /* The ELF ABI says `h &= ~g', but this is equivalent in
218 this case and on some machines one insn instead of two. */
219 h ^= g;
32090b8e
KR
220 }
221 }
222 return h;
223}
224
225/* Read a specified number of bytes at a specified offset in an ELF
226 file, into a newly allocated buffer, and return a pointer to the
227 buffer. */
228
229static char *
013dec1a
ILT
230elf_read (abfd, offset, size)
231 bfd * abfd;
232 long offset;
ae115e51 233 unsigned int size;
32090b8e
KR
234{
235 char *buf;
236
237 if ((buf = bfd_alloc (abfd, size)) == NULL)
a9713b91 238 return NULL;
32090b8e 239 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
013dec1a 240 return NULL;
32090b8e
KR
241 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
242 {
013dec1a
ILT
243 if (bfd_get_error () != bfd_error_system_call)
244 bfd_set_error (bfd_error_file_truncated);
32090b8e
KR
245 return NULL;
246 }
247 return buf;
248}
249
250boolean
ff12f303 251bfd_elf_mkobject (abfd)
013dec1a 252 bfd * abfd;
32090b8e
KR
253{
254 /* this just does initialization */
255 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
256 elf_tdata (abfd) = (struct elf_obj_tdata *)
257 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
258 if (elf_tdata (abfd) == 0)
a9713b91 259 return false;
32090b8e
KR
260 /* since everything is done at close time, do we need any
261 initialization? */
262
263 return true;
264}
265
b64beb2d
MS
266boolean
267bfd_elf_mkcorefile (abfd)
268 bfd * abfd;
269{
270 /* I think this can be done just like an object file. */
271 return bfd_elf_mkobject (abfd);
272}
273
32090b8e 274char *
ede4eed4 275bfd_elf_get_str_section (abfd, shindex)
013dec1a
ILT
276 bfd * abfd;
277 unsigned int shindex;
32090b8e
KR
278{
279 Elf_Internal_Shdr **i_shdrp;
280 char *shstrtab = NULL;
281 unsigned int offset;
282 unsigned int shstrtabsize;
283
284 i_shdrp = elf_elfsections (abfd);
285 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
286 return 0;
287
b176e1e9 288 shstrtab = (char *) i_shdrp[shindex]->contents;
32090b8e
KR
289 if (shstrtab == NULL)
290 {
291 /* No cached one, attempt to read, and cache what we read. */
292 offset = i_shdrp[shindex]->sh_offset;
293 shstrtabsize = i_shdrp[shindex]->sh_size;
294 shstrtab = elf_read (abfd, offset, shstrtabsize);
b176e1e9 295 i_shdrp[shindex]->contents = (PTR) shstrtab;
32090b8e
KR
296 }
297 return shstrtab;
298}
299
300char *
ede4eed4 301bfd_elf_string_from_elf_section (abfd, shindex, strindex)
013dec1a
ILT
302 bfd * abfd;
303 unsigned int shindex;
304 unsigned int strindex;
32090b8e
KR
305{
306 Elf_Internal_Shdr *hdr;
307
308 if (strindex == 0)
309 return "";
310
311 hdr = elf_elfsections (abfd)[shindex];
312
b176e1e9 313 if (hdr->contents == NULL
ede4eed4 314 && bfd_elf_get_str_section (abfd, shindex) == NULL)
32090b8e
KR
315 return NULL;
316
20db2495
ILT
317 if (strindex >= hdr->sh_size)
318 {
319 (*_bfd_error_handler)
53d3ce37 320 (_("%s: invalid string offset %u >= %lu for section `%s'"),
20db2495
ILT
321 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
322 ((shindex == elf_elfheader(abfd)->e_shstrndx
323 && strindex == hdr->sh_name)
324 ? ".shstrtab"
325 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
326 return "";
327 }
328
b176e1e9 329 return ((char *) hdr->contents) + strindex;
32090b8e
KR
330}
331
497c5434 332/* Make a BFD section from an ELF section. We store a pointer to the
b176e1e9 333 BFD section in the bfd_section field of the header. */
497c5434
ILT
334
335boolean
336_bfd_elf_make_section_from_shdr (abfd, hdr, name)
337 bfd *abfd;
338 Elf_Internal_Shdr *hdr;
339 const char *name;
340{
341 asection *newsect;
342 flagword flags;
343
b176e1e9 344 if (hdr->bfd_section != NULL)
497c5434 345 {
b176e1e9
ILT
346 BFD_ASSERT (strcmp (name,
347 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
497c5434
ILT
348 return true;
349 }
350
351 newsect = bfd_make_section_anyway (abfd, name);
352 if (newsect == NULL)
353 return false;
354
355 newsect->filepos = hdr->sh_offset;
356
357 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
358 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
359 || ! bfd_set_section_alignment (abfd, newsect,
360 bfd_log2 (hdr->sh_addralign)))
361 return false;
362
363 flags = SEC_NO_FLAGS;
364 if (hdr->sh_type != SHT_NOBITS)
365 flags |= SEC_HAS_CONTENTS;
366 if ((hdr->sh_flags & SHF_ALLOC) != 0)
367 {
368 flags |= SEC_ALLOC;
369 if (hdr->sh_type != SHT_NOBITS)
370 flags |= SEC_LOAD;
371 }
372 if ((hdr->sh_flags & SHF_WRITE) == 0)
373 flags |= SEC_READONLY;
374 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
375 flags |= SEC_CODE;
7c6da9ca 376 else if ((flags & SEC_LOAD) != 0)
497c5434
ILT
377 flags |= SEC_DATA;
378
379 /* The debugging sections appear to be recognized only by name, not
380 any sort of flag. */
381 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
382 || strncmp (name, ".line", sizeof ".line" - 1) == 0
383 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
384 flags |= SEC_DEBUGGING;
385
f0c12b73
DE
386 /* As a GNU extension, if the name begins with .gnu.linkonce, we
387 only link a single copy of the section. This is used to support
388 g++. g++ will emit each template expansion in its own section.
389 The symbols will be defined as weak, so that multiple definitions
390 are permitted. The GNU linker extension is to actually discard
391 all but one of the sections. */
392 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
393 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
394
497c5434
ILT
395 if (! bfd_set_section_flags (abfd, newsect, flags))
396 return false;
397
fd0198f0
ILT
398 if ((flags & SEC_ALLOC) != 0)
399 {
400 Elf_Internal_Phdr *phdr;
401 unsigned int i;
402
b64beb2d
MS
403 /* Look through the phdrs to see if we need to adjust the lma.
404 If all the p_paddr fields are zero, we ignore them, since
405 some ELF linkers produce such output. */
fd0198f0
ILT
406 phdr = elf_tdata (abfd)->phdr;
407 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
408 {
b64beb2d
MS
409 if (phdr->p_paddr != 0)
410 break;
411 }
412 if (i < elf_elfheader (abfd)->e_phnum)
413 {
414 phdr = elf_tdata (abfd)->phdr;
415 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
fd0198f0 416 {
b64beb2d
MS
417 if (phdr->p_type == PT_LOAD
418 && phdr->p_vaddr != phdr->p_paddr
419 && phdr->p_vaddr <= hdr->sh_addr
420 && (phdr->p_vaddr + phdr->p_memsz
421 >= hdr->sh_addr + hdr->sh_size)
422 && ((flags & SEC_LOAD) == 0
423 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
424 && (phdr->p_offset + phdr->p_filesz
425 >= hdr->sh_offset + hdr->sh_size))))
426 {
427 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
428 break;
429 }
fd0198f0
ILT
430 }
431 }
432 }
433
b176e1e9 434 hdr->bfd_section = newsect;
497c5434
ILT
435 elf_section_data (newsect)->this_hdr = *hdr;
436
437 return true;
438}
439
32090b8e
KR
440/*
441INTERNAL_FUNCTION
442 bfd_elf_find_section
443
444SYNOPSIS
445 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
446
447DESCRIPTION
448 Helper functions for GDB to locate the string tables.
449 Since BFD hides string tables from callers, GDB needs to use an
450 internal hook to find them. Sun's .stabstr, in particular,
451 isn't even pointed to by the .stab section, so ordinary
452 mechanisms wouldn't work to find it, even if we had some.
453*/
454
455struct elf_internal_shdr *
013dec1a
ILT
456bfd_elf_find_section (abfd, name)
457 bfd * abfd;
458 char *name;
32090b8e
KR
459{
460 Elf_Internal_Shdr **i_shdrp;
461 char *shstrtab;
462 unsigned int max;
463 unsigned int i;
464
465 i_shdrp = elf_elfsections (abfd);
466 if (i_shdrp != NULL)
467 {
9e7e5d5e
NC
468 shstrtab = bfd_elf_get_str_section
469 (abfd, elf_elfheader (abfd)->e_shstrndx);
32090b8e
KR
470 if (shstrtab != NULL)
471 {
472 max = elf_elfheader (abfd)->e_shnum;
473 for (i = 1; i < max; i++)
474 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
475 return i_shdrp[i];
476 }
477 }
478 return 0;
479}
480
32090b8e
KR
481const char *const bfd_elf_section_type_names[] = {
482 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
483 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
484 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
485};
486
487/* ELF relocs are against symbols. If we are producing relocateable
488 output, and the reloc is against an external symbol, and nothing
489 has given us any additional addend, the resulting reloc will also
490 be against the same symbol. In such a case, we don't want to
491 change anything about the way the reloc is handled, since it will
492 all be done at final link time. Rather than put special case code
493 into bfd_perform_relocation, all the reloc types use this howto
494 function. It just short circuits the reloc if producing
495 relocateable output against an external symbol. */
496
013dec1a 497/*ARGSUSED*/
32090b8e
KR
498bfd_reloc_status_type
499bfd_elf_generic_reloc (abfd,
500 reloc_entry,
501 symbol,
502 data,
503 input_section,
4c3721d5
ILT
504 output_bfd,
505 error_message)
32090b8e
KR
506 bfd *abfd;
507 arelent *reloc_entry;
508 asymbol *symbol;
509 PTR data;
510 asection *input_section;
511 bfd *output_bfd;
4c3721d5 512 char **error_message;
32090b8e
KR
513{
514 if (output_bfd != (bfd *) NULL
515 && (symbol->flags & BSF_SECTION_SYM) == 0
d1b44e83
ILT
516 && (! reloc_entry->howto->partial_inplace
517 || reloc_entry->addend == 0))
32090b8e
KR
518 {
519 reloc_entry->address += input_section->output_offset;
520 return bfd_reloc_ok;
521 }
522
523 return bfd_reloc_continue;
524}
013dec1a 525\f
27fb8f29
ILT
526/* Print out the program headers. */
527
528boolean
529_bfd_elf_print_private_bfd_data (abfd, farg)
530 bfd *abfd;
531 PTR farg;
532{
533 FILE *f = (FILE *) farg;
534 Elf_Internal_Phdr *p;
02fcd126
ILT
535 asection *s;
536 bfd_byte *dynbuf = NULL;
27fb8f29
ILT
537
538 p = elf_tdata (abfd)->phdr;
02fcd126 539 if (p != NULL)
27fb8f29 540 {
02fcd126 541 unsigned int i, c;
27fb8f29 542
53d3ce37 543 fprintf (f, _("\nProgram Header:\n"));
02fcd126
ILT
544 c = elf_elfheader (abfd)->e_phnum;
545 for (i = 0; i < c; i++, p++)
27fb8f29 546 {
02fcd126
ILT
547 const char *s;
548 char buf[20];
549
550 switch (p->p_type)
551 {
552 case PT_NULL: s = "NULL"; break;
553 case PT_LOAD: s = "LOAD"; break;
554 case PT_DYNAMIC: s = "DYNAMIC"; break;
555 case PT_INTERP: s = "INTERP"; break;
556 case PT_NOTE: s = "NOTE"; break;
557 case PT_SHLIB: s = "SHLIB"; break;
558 case PT_PHDR: s = "PHDR"; break;
559 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
560 }
561 fprintf (f, "%8s off 0x", s);
562 fprintf_vma (f, p->p_offset);
563 fprintf (f, " vaddr 0x");
564 fprintf_vma (f, p->p_vaddr);
565 fprintf (f, " paddr 0x");
566 fprintf_vma (f, p->p_paddr);
567 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
568 fprintf (f, " filesz 0x");
569 fprintf_vma (f, p->p_filesz);
570 fprintf (f, " memsz 0x");
571 fprintf_vma (f, p->p_memsz);
572 fprintf (f, " flags %c%c%c",
573 (p->p_flags & PF_R) != 0 ? 'r' : '-',
574 (p->p_flags & PF_W) != 0 ? 'w' : '-',
575 (p->p_flags & PF_X) != 0 ? 'x' : '-');
576 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
577 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
578 fprintf (f, "\n");
579 }
580 }
581
582 s = bfd_get_section_by_name (abfd, ".dynamic");
583 if (s != NULL)
584 {
585 int elfsec;
586 unsigned long link;
587 bfd_byte *extdyn, *extdynend;
588 size_t extdynsize;
589 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
590
53d3ce37 591 fprintf (f, _("\nDynamic Section:\n"));
02fcd126
ILT
592
593 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
594 if (dynbuf == NULL)
595 goto error_return;
596 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
597 s->_raw_size))
598 goto error_return;
599
600 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
601 if (elfsec == -1)
602 goto error_return;
603 link = elf_elfsections (abfd)[elfsec]->sh_link;
604
605 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
606 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
607
608 extdyn = dynbuf;
609 extdynend = extdyn + s->_raw_size;
610 for (; extdyn < extdynend; extdyn += extdynsize)
611 {
612 Elf_Internal_Dyn dyn;
613 const char *name;
614 char ab[20];
615 boolean stringp;
616
617 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
618
619 if (dyn.d_tag == DT_NULL)
620 break;
621
622 stringp = false;
623 switch (dyn.d_tag)
624 {
625 default:
927d05b5 626 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
02fcd126
ILT
627 name = ab;
628 break;
629
630 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
631 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
632 case DT_PLTGOT: name = "PLTGOT"; break;
633 case DT_HASH: name = "HASH"; break;
634 case DT_STRTAB: name = "STRTAB"; break;
635 case DT_SYMTAB: name = "SYMTAB"; break;
636 case DT_RELA: name = "RELA"; break;
637 case DT_RELASZ: name = "RELASZ"; break;
638 case DT_RELAENT: name = "RELAENT"; break;
639 case DT_STRSZ: name = "STRSZ"; break;
640 case DT_SYMENT: name = "SYMENT"; break;
641 case DT_INIT: name = "INIT"; break;
642 case DT_FINI: name = "FINI"; break;
643 case DT_SONAME: name = "SONAME"; stringp = true; break;
644 case DT_RPATH: name = "RPATH"; stringp = true; break;
645 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
646 case DT_REL: name = "REL"; break;
647 case DT_RELSZ: name = "RELSZ"; break;
648 case DT_RELENT: name = "RELENT"; break;
649 case DT_PLTREL: name = "PLTREL"; break;
650 case DT_DEBUG: name = "DEBUG"; break;
651 case DT_TEXTREL: name = "TEXTREL"; break;
652 case DT_JMPREL: name = "JMPREL"; break;
148437ec
ILT
653 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
654 case DT_FILTER: name = "FILTER"; stringp = true; break;
a66a61a0
ILT
655 case DT_VERSYM: name = "VERSYM"; break;
656 case DT_VERDEF: name = "VERDEF"; break;
657 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
658 case DT_VERNEED: name = "VERNEED"; break;
659 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
02fcd126
ILT
660 }
661
662 fprintf (f, " %-11s ", name);
663 if (! stringp)
927d05b5 664 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
02fcd126
ILT
665 else
666 {
667 const char *string;
668
669 string = bfd_elf_string_from_elf_section (abfd, link,
670 dyn.d_un.d_val);
671 if (string == NULL)
672 goto error_return;
673 fprintf (f, "%s", string);
674 }
675 fprintf (f, "\n");
27fb8f29 676 }
02fcd126
ILT
677
678 free (dynbuf);
679 dynbuf = NULL;
27fb8f29
ILT
680 }
681
a66a61a0
ILT
682 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
683 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
684 {
685 if (! _bfd_elf_slurp_version_tables (abfd))
686 return false;
687 }
688
689 if (elf_dynverdef (abfd) != 0)
690 {
691 Elf_Internal_Verdef *t;
692
53d3ce37 693 fprintf (f, _("\nVersion definitions:\n"));
a66a61a0
ILT
694 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
695 {
696 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
697 t->vd_flags, t->vd_hash, t->vd_nodename);
698 if (t->vd_auxptr->vda_nextptr != NULL)
699 {
700 Elf_Internal_Verdaux *a;
701
702 fprintf (f, "\t");
703 for (a = t->vd_auxptr->vda_nextptr;
704 a != NULL;
705 a = a->vda_nextptr)
706 fprintf (f, "%s ", a->vda_nodename);
707 fprintf (f, "\n");
708 }
709 }
710 }
711
712 if (elf_dynverref (abfd) != 0)
713 {
714 Elf_Internal_Verneed *t;
715
53d3ce37 716 fprintf (f, _("\nVersion References:\n"));
a66a61a0
ILT
717 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
718 {
719 Elf_Internal_Vernaux *a;
720
53d3ce37 721 fprintf (f, _(" required from %s:\n"), t->vn_filename);
a66a61a0
ILT
722 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
723 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
724 a->vna_flags, a->vna_other, a->vna_nodename);
725 }
726 }
727
27fb8f29 728 return true;
02fcd126
ILT
729
730 error_return:
731 if (dynbuf != NULL)
732 free (dynbuf);
733 return false;
27fb8f29
ILT
734}
735
b176e1e9 736/* Display ELF-specific fields of a symbol. */
d6bfcdb5 737
b176e1e9 738void
d6bfcdb5
ILT
739bfd_elf_print_symbol (abfd, filep, symbol, how)
740 bfd *abfd;
b176e1e9
ILT
741 PTR filep;
742 asymbol *symbol;
743 bfd_print_symbol_type how;
744{
745 FILE *file = (FILE *) filep;
746 switch (how)
747 {
748 case bfd_print_symbol_name:
749 fprintf (file, "%s", symbol->name);
750 break;
751 case bfd_print_symbol_more:
752 fprintf (file, "elf ");
753 fprintf_vma (file, symbol->value);
754 fprintf (file, " %lx", (long) symbol->flags);
755 break;
756 case bfd_print_symbol_all:
757 {
758 CONST char *section_name;
759 section_name = symbol->section ? symbol->section->name : "(*none*)";
760 bfd_print_symbol_vandf ((PTR) file, symbol);
761 fprintf (file, " %s\t", section_name);
762 /* Print the "other" value for a symbol. For common symbols,
763 we've already printed the size; now print the alignment.
764 For other symbols, we have no specified alignment, and
765 we've printed the address; now print the size. */
766 fprintf_vma (file,
767 (bfd_is_com_section (symbol->section)
768 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
769 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
d6bfcdb5
ILT
770
771 /* If we have version information, print it. */
772 if (elf_tdata (abfd)->dynversym_section != 0
773 && (elf_tdata (abfd)->dynverdef_section != 0
774 || elf_tdata (abfd)->dynverref_section != 0))
775 {
776 unsigned int vernum;
777 const char *version_string;
778
779 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
780
781 if (vernum == 0)
782 version_string = "";
783 else if (vernum == 1)
784 version_string = "Base";
20db2495 785 else if (vernum <= elf_tdata (abfd)->cverdefs)
d6bfcdb5
ILT
786 version_string =
787 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
788 else
789 {
790 Elf_Internal_Verneed *t;
791
792 version_string = "";
793 for (t = elf_tdata (abfd)->verref;
794 t != NULL;
795 t = t->vn_nextref)
796 {
797 Elf_Internal_Vernaux *a;
798
799 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
800 {
801 if (a->vna_other == vernum)
802 {
803 version_string = a->vna_nodename;
804 break;
805 }
806 }
807 }
808 }
809
810 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
20db2495 811 fprintf (file, " %-11s", version_string);
d6bfcdb5
ILT
812 else
813 {
814 int i;
815
816 fprintf (file, " (%s)", version_string);
20db2495 817 for (i = 10 - strlen (version_string); i > 0; --i)
d6bfcdb5
ILT
818 putc (' ', file);
819 }
820 }
821
69e2ff18
ILT
822 /* If the st_other field is not zero, print it. */
823 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
824 fprintf (file, " 0x%02x",
825 ((unsigned int)
826 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
d6bfcdb5 827
b176e1e9
ILT
828 fprintf (file, " %s", symbol->name);
829 }
830 break;
831 }
832}
833\f
013dec1a
ILT
834/* Create an entry in an ELF linker hash table. */
835
5315c428
ILT
836struct bfd_hash_entry *
837_bfd_elf_link_hash_newfunc (entry, table, string)
013dec1a
ILT
838 struct bfd_hash_entry *entry;
839 struct bfd_hash_table *table;
840 const char *string;
841{
842 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
843
844 /* Allocate the structure if it has not already been allocated by a
845 subclass. */
846 if (ret == (struct elf_link_hash_entry *) NULL)
847 ret = ((struct elf_link_hash_entry *)
848 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
849 if (ret == (struct elf_link_hash_entry *) NULL)
a9713b91 850 return (struct bfd_hash_entry *) ret;
013dec1a
ILT
851
852 /* Call the allocation method of the superclass. */
853 ret = ((struct elf_link_hash_entry *)
854 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
855 table, string));
856 if (ret != (struct elf_link_hash_entry *) NULL)
857 {
858 /* Set local fields. */
859 ret->indx = -1;
860 ret->size = 0;
013dec1a
ILT
861 ret->dynindx = -1;
862 ret->dynstr_index = 0;
863 ret->weakdef = NULL;
303b4cc6
RH
864 ret->got.offset = (bfd_vma) -1;
865 ret->plt.offset = (bfd_vma) -1;
86aac8ea 866 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
a66a61a0 867 ret->verinfo.verdef = NULL;
303b4cc6
RH
868 ret->vtable_entries_used = NULL;
869 ret->vtable_parent = NULL;
013dec1a 870 ret->type = STT_NOTYPE;
80be821d 871 ret->other = 0;
869b7d80
ILT
872 /* Assume that we have been called by a non-ELF symbol reader.
873 This flag is then reset by the code which reads an ELF input
874 file. This ensures that a symbol created by a non-ELF symbol
875 reader will have the flag set correctly. */
876 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
013dec1a
ILT
877 }
878
879 return (struct bfd_hash_entry *) ret;
880}
881
5315c428
ILT
882/* Initialize an ELF linker hash table. */
883
884boolean
885_bfd_elf_link_hash_table_init (table, abfd, newfunc)
886 struct elf_link_hash_table *table;
887 bfd *abfd;
888 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
889 struct bfd_hash_table *,
890 const char *));
891{
b176e1e9 892 table->dynamic_sections_created = false;
5315c428 893 table->dynobj = NULL;
b176e1e9
ILT
894 /* The first dynamic symbol is a dummy. */
895 table->dynsymcount = 1;
5315c428
ILT
896 table->dynstr = NULL;
897 table->bucketcount = 0;
b176e1e9 898 table->needed = NULL;
19bfbcbe 899 table->hgot = NULL;
d1bf45aa 900 table->stab_info = NULL;
5315c428
ILT
901 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
902}
903
013dec1a
ILT
904/* Create an ELF linker hash table. */
905
906struct bfd_link_hash_table *
907_bfd_elf_link_hash_table_create (abfd)
908 bfd *abfd;
909{
910 struct elf_link_hash_table *ret;
911
912 ret = ((struct elf_link_hash_table *)
913 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
914 if (ret == (struct elf_link_hash_table *) NULL)
a9713b91 915 return NULL;
5315c428
ILT
916
917 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
013dec1a
ILT
918 {
919 bfd_release (abfd, ret);
920 return NULL;
921 }
922
013dec1a
ILT
923 return &ret->root;
924}
7c6da9ca
ILT
925
926/* This is a hook for the ELF emulation code in the generic linker to
927 tell the backend linker what file name to use for the DT_NEEDED
b176e1e9
ILT
928 entry for a dynamic object. The generic linker passes name as an
929 empty string to indicate that no DT_NEEDED entry should be made. */
7c6da9ca
ILT
930
931void
932bfd_elf_set_dt_needed_name (abfd, name)
933 bfd *abfd;
934 const char *name;
935{
053ae1d7
ILT
936 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
937 && bfd_get_format (abfd) == bfd_object)
938 elf_dt_name (abfd) = name;
7c6da9ca 939}
b176e1e9 940
053ae1d7 941/* Get the list of DT_NEEDED entries for a link. This is a hook for
0d3887ba 942 the linker ELF emulation code. */
b176e1e9 943
5fe14a9f 944struct bfd_link_needed_list *
b176e1e9
ILT
945bfd_elf_get_needed_list (abfd, info)
946 bfd *abfd;
947 struct bfd_link_info *info;
948{
b2193cc5
ILT
949 if (info->hash->creator->flavour != bfd_target_elf_flavour)
950 return NULL;
b176e1e9
ILT
951 return elf_hash_table (info)->needed;
952}
053ae1d7
ILT
953
954/* Get the name actually used for a dynamic object for a link. This
955 is the SONAME entry if there is one. Otherwise, it is the string
956 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
957
958const char *
959bfd_elf_get_dt_soname (abfd)
960 bfd *abfd;
961{
962 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
963 && bfd_get_format (abfd) == bfd_object)
964 return elf_dt_name (abfd);
965 return NULL;
966}
0d3887ba
ILT
967
968/* Get the list of DT_NEEDED entries from a BFD. This is a hook for
969 the ELF linker emulation code. */
970
971boolean
972bfd_elf_get_bfd_needed_list (abfd, pneeded)
973 bfd *abfd;
974 struct bfd_link_needed_list **pneeded;
975{
976 asection *s;
977 bfd_byte *dynbuf = NULL;
978 int elfsec;
979 unsigned long link;
980 bfd_byte *extdyn, *extdynend;
981 size_t extdynsize;
982 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
983
984 *pneeded = NULL;
985
986 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
987 || bfd_get_format (abfd) != bfd_object)
988 return true;
989
990 s = bfd_get_section_by_name (abfd, ".dynamic");
991 if (s == NULL || s->_raw_size == 0)
992 return true;
993
994 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
995 if (dynbuf == NULL)
996 goto error_return;
997
998 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
999 s->_raw_size))
1000 goto error_return;
1001
1002 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1003 if (elfsec == -1)
1004 goto error_return;
1005
1006 link = elf_elfsections (abfd)[elfsec]->sh_link;
1007
1008 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1009 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1010
1011 extdyn = dynbuf;
1012 extdynend = extdyn + s->_raw_size;
1013 for (; extdyn < extdynend; extdyn += extdynsize)
1014 {
1015 Elf_Internal_Dyn dyn;
1016
1017 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1018
1019 if (dyn.d_tag == DT_NULL)
1020 break;
1021
1022 if (dyn.d_tag == DT_NEEDED)
1023 {
1024 const char *string;
1025 struct bfd_link_needed_list *l;
1026
1027 string = bfd_elf_string_from_elf_section (abfd, link,
1028 dyn.d_un.d_val);
1029 if (string == NULL)
1030 goto error_return;
1031
1032 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1033 if (l == NULL)
1034 goto error_return;
1035
1036 l->by = abfd;
1037 l->name = string;
1038 l->next = *pneeded;
1039 *pneeded = l;
1040 }
1041 }
1042
1043 free (dynbuf);
1044
1045 return true;
1046
1047 error_return:
1048 if (dynbuf != NULL)
1049 free (dynbuf);
1050 return false;
1051}
ede4eed4
KR
1052\f
1053/* Allocate an ELF string table--force the first byte to be zero. */
1054
1055struct bfd_strtab_hash *
1056_bfd_elf_stringtab_init ()
1057{
1058 struct bfd_strtab_hash *ret;
1059
1060 ret = _bfd_stringtab_init ();
1061 if (ret != NULL)
1062 {
1063 bfd_size_type loc;
1064
1065 loc = _bfd_stringtab_add (ret, "", true, false);
1066 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1067 if (loc == (bfd_size_type) -1)
1068 {
1069 _bfd_stringtab_free (ret);
1070 ret = NULL;
1071 }
1072 }
1073 return ret;
1074}
1075\f
1076/* ELF .o/exec file reading */
1077
1078/* Create a new bfd section from an ELF section header. */
1079
1080boolean
1081bfd_section_from_shdr (abfd, shindex)
1082 bfd *abfd;
1083 unsigned int shindex;
1084{
1085 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1086 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1087 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1088 char *name;
1089
1090 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1091
1092 switch (hdr->sh_type)
1093 {
1094 case SHT_NULL:
1095 /* Inactive section. Throw it away. */
1096 return true;
1097
1098 case SHT_PROGBITS: /* Normal section with contents. */
1099 case SHT_DYNAMIC: /* Dynamic linking information. */
1100 case SHT_NOBITS: /* .bss section. */
1101 case SHT_HASH: /* .hash section. */
5b3b9ff6 1102 case SHT_NOTE: /* .note section. */
ede4eed4
KR
1103 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1104
1105 case SHT_SYMTAB: /* A symbol table */
1106 if (elf_onesymtab (abfd) == shindex)
1107 return true;
1108
1109 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1110 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1111 elf_onesymtab (abfd) = shindex;
1112 elf_tdata (abfd)->symtab_hdr = *hdr;
fd0198f0 1113 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
ede4eed4
KR
1114 abfd->flags |= HAS_SYMS;
1115
1116 /* Sometimes a shared object will map in the symbol table. If
1117 SHF_ALLOC is set, and this is a shared object, then we also
1118 treat this section as a BFD section. We can not base the
1119 decision purely on SHF_ALLOC, because that flag is sometimes
1120 set in a relocateable object file, which would confuse the
1121 linker. */
1122 if ((hdr->sh_flags & SHF_ALLOC) != 0
1123 && (abfd->flags & DYNAMIC) != 0
1124 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1125 return false;
1126
1127 return true;
1128
1129 case SHT_DYNSYM: /* A dynamic symbol table */
1130 if (elf_dynsymtab (abfd) == shindex)
1131 return true;
1132
1133 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1134 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1135 elf_dynsymtab (abfd) = shindex;
1136 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
fd0198f0 1137 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
ede4eed4
KR
1138 abfd->flags |= HAS_SYMS;
1139
1140 /* Besides being a symbol table, we also treat this as a regular
1141 section, so that objcopy can handle it. */
1142 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1143
1144 case SHT_STRTAB: /* A string table */
1145 if (hdr->bfd_section != NULL)
1146 return true;
1147 if (ehdr->e_shstrndx == shindex)
1148 {
1149 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1150 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1151 return true;
1152 }
1153 {
1154 unsigned int i;
1155
1156 for (i = 1; i < ehdr->e_shnum; i++)
1157 {
1158 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1159 if (hdr2->sh_link == shindex)
1160 {
1161 if (! bfd_section_from_shdr (abfd, i))
1162 return false;
1163 if (elf_onesymtab (abfd) == i)
1164 {
1165 elf_tdata (abfd)->strtab_hdr = *hdr;
1166 elf_elfsections (abfd)[shindex] =
1167 &elf_tdata (abfd)->strtab_hdr;
1168 return true;
1169 }
1170 if (elf_dynsymtab (abfd) == i)
1171 {
1172 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
fd0198f0 1173 elf_elfsections (abfd)[shindex] = hdr =
ede4eed4
KR
1174 &elf_tdata (abfd)->dynstrtab_hdr;
1175 /* We also treat this as a regular section, so
1176 that objcopy can handle it. */
1177 break;
1178 }
1179#if 0 /* Not handling other string tables specially right now. */
1180 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1181 /* We have a strtab for some random other section. */
1182 newsect = (asection *) hdr2->bfd_section;
1183 if (!newsect)
1184 break;
1185 hdr->bfd_section = newsect;
1186 hdr2 = &elf_section_data (newsect)->str_hdr;
1187 *hdr2 = *hdr;
1188 elf_elfsections (abfd)[shindex] = hdr2;
1189#endif
1190 }
1191 }
1192 }
1193
1194 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1195
1196 case SHT_REL:
1197 case SHT_RELA:
1198 /* *These* do a lot of work -- but build no sections! */
1199 {
1200 asection *target_sect;
1201 Elf_Internal_Shdr *hdr2;
ede4eed4 1202
ae115e51
ILT
1203 /* For some incomprehensible reason Oracle distributes
1204 libraries for Solaris in which some of the objects have
1205 bogus sh_link fields. It would be nice if we could just
1206 reject them, but, unfortunately, some people need to use
1207 them. We scan through the section headers; if we find only
1208 one suitable symbol table, we clobber the sh_link to point
1209 to it. I hope this doesn't break anything. */
1210 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1211 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1212 {
1213 int scan;
1214 int found;
1215
1216 found = 0;
1217 for (scan = 1; scan < ehdr->e_shnum; scan++)
1218 {
1219 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1220 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1221 {
1222 if (found != 0)
1223 {
1224 found = 0;
1225 break;
1226 }
1227 found = scan;
1228 }
1229 }
1230 if (found != 0)
1231 hdr->sh_link = found;
1232 }
1233
ede4eed4 1234 /* Get the symbol table. */
ae115e51
ILT
1235 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1236 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
ede4eed4
KR
1237 return false;
1238
1239 /* If this reloc section does not use the main symbol table we
1240 don't treat it as a reloc section. BFD can't adequately
1241 represent such a section, so at least for now, we don't
1242 try. We just present it as a normal section. */
1243 if (hdr->sh_link != elf_onesymtab (abfd))
e85f2fbd 1244 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
ede4eed4 1245
ede4eed4
KR
1246 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1247 return false;
1248 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1249 if (target_sect == NULL)
1250 return false;
1251
d1bf45aa
ILT
1252 if ((target_sect->flags & SEC_RELOC) == 0
1253 || target_sect->reloc_count == 0)
1254 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1255 else
1256 {
1257 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1258 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1259 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1260 }
ede4eed4
KR
1261 *hdr2 = *hdr;
1262 elf_elfsections (abfd)[shindex] = hdr2;
d1bf45aa 1263 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
ede4eed4
KR
1264 target_sect->flags |= SEC_RELOC;
1265 target_sect->relocation = NULL;
1266 target_sect->rel_filepos = hdr->sh_offset;
1267 abfd->flags |= HAS_RELOC;
1268 return true;
1269 }
1270 break;
1271
a66a61a0
ILT
1272 case SHT_GNU_verdef:
1273 elf_dynverdef (abfd) = shindex;
1274 elf_tdata (abfd)->dynverdef_hdr = *hdr;
d6bfcdb5 1275 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
a66a61a0
ILT
1276 break;
1277
1278 case SHT_GNU_versym:
1279 elf_dynversym (abfd) = shindex;
1280 elf_tdata (abfd)->dynversym_hdr = *hdr;
d6bfcdb5 1281 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
a66a61a0
ILT
1282 break;
1283
1284 case SHT_GNU_verneed:
1285 elf_dynverref (abfd) = shindex;
1286 elf_tdata (abfd)->dynverref_hdr = *hdr;
d6bfcdb5 1287 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
a66a61a0
ILT
1288 break;
1289
ede4eed4 1290 case SHT_SHLIB:
ede4eed4
KR
1291 return true;
1292
1293 default:
1294 /* Check for any processor-specific section types. */
1295 {
1296 if (bed->elf_backend_section_from_shdr)
1297 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1298 }
1299 break;
1300 }
1301
1302 return true;
1303}
1304
1305/* Given an ELF section number, retrieve the corresponding BFD
1306 section. */
1307
1308asection *
1309bfd_section_from_elf_index (abfd, index)
1310 bfd *abfd;
1311 unsigned int index;
1312{
1313 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1314 if (index >= elf_elfheader (abfd)->e_shnum)
1315 return NULL;
1316 return elf_elfsections (abfd)[index]->bfd_section;
1317}
1318
1319boolean
1320_bfd_elf_new_section_hook (abfd, sec)
1321 bfd *abfd;
1322 asection *sec;
1323{
1324 struct bfd_elf_section_data *sdata;
1325
1326 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
1327 if (!sdata)
a9713b91 1328 return false;
ede4eed4
KR
1329 sec->used_by_bfd = (PTR) sdata;
1330 memset (sdata, 0, sizeof (*sdata));
1331 return true;
1332}
1333
1334/* Create a new bfd section from an ELF program header.
1335
1336 Since program segments have no names, we generate a synthetic name
1337 of the form segment<NUM>, where NUM is generally the index in the
1338 program header table. For segments that are split (see below) we
1339 generate the names segment<NUM>a and segment<NUM>b.
1340
1341 Note that some program segments may have a file size that is different than
1342 (less than) the memory size. All this means is that at execution the
1343 system must allocate the amount of memory specified by the memory size,
1344 but only initialize it with the first "file size" bytes read from the
1345 file. This would occur for example, with program segments consisting
1346 of combined data+bss.
1347
1348 To handle the above situation, this routine generates TWO bfd sections
1349 for the single program segment. The first has the length specified by
1350 the file size of the segment, and the second has the length specified
1351 by the difference between the two sizes. In effect, the segment is split
1352 into it's initialized and uninitialized parts.
1353
1354 */
1355
1356boolean
1357bfd_section_from_phdr (abfd, hdr, index)
1358 bfd *abfd;
1359 Elf_Internal_Phdr *hdr;
1360 int index;
1361{
1362 asection *newsect;
1363 char *name;
1364 char namebuf[64];
1365 int split;
1366
a6ac3b6b
MS
1367 split = ((hdr->p_memsz > 0)
1368 && (hdr->p_filesz > 0)
1369 && (hdr->p_memsz > hdr->p_filesz));
ede4eed4
KR
1370 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
1371 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1372 if (!name)
a9713b91 1373 return false;
ede4eed4
KR
1374 strcpy (name, namebuf);
1375 newsect = bfd_make_section (abfd, name);
1376 if (newsect == NULL)
1377 return false;
1378 newsect->vma = hdr->p_vaddr;
ae115e51 1379 newsect->lma = hdr->p_paddr;
ede4eed4
KR
1380 newsect->_raw_size = hdr->p_filesz;
1381 newsect->filepos = hdr->p_offset;
1382 newsect->flags |= SEC_HAS_CONTENTS;
1383 if (hdr->p_type == PT_LOAD)
1384 {
1385 newsect->flags |= SEC_ALLOC;
1386 newsect->flags |= SEC_LOAD;
1387 if (hdr->p_flags & PF_X)
1388 {
1389 /* FIXME: all we known is that it has execute PERMISSION,
1390 may be data. */
1391 newsect->flags |= SEC_CODE;
1392 }
1393 }
1394 if (!(hdr->p_flags & PF_W))
1395 {
1396 newsect->flags |= SEC_READONLY;
1397 }
1398
1399 if (split)
1400 {
1401 sprintf (namebuf, "segment%db", index);
1402 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1403 if (!name)
a9713b91 1404 return false;
ede4eed4
KR
1405 strcpy (name, namebuf);
1406 newsect = bfd_make_section (abfd, name);
1407 if (newsect == NULL)
1408 return false;
1409 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
ae115e51 1410 newsect->lma = hdr->p_paddr + hdr->p_filesz;
ede4eed4
KR
1411 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1412 if (hdr->p_type == PT_LOAD)
1413 {
1414 newsect->flags |= SEC_ALLOC;
1415 if (hdr->p_flags & PF_X)
1416 newsect->flags |= SEC_CODE;
1417 }
1418 if (!(hdr->p_flags & PF_W))
1419 newsect->flags |= SEC_READONLY;
1420 }
1421
1422 return true;
1423}
1424
1425/* Set up an ELF internal section header for a section. */
1426
1427/*ARGSUSED*/
1428static void
1429elf_fake_sections (abfd, asect, failedptrarg)
1430 bfd *abfd;
1431 asection *asect;
1432 PTR failedptrarg;
1433{
1434 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1435 boolean *failedptr = (boolean *) failedptrarg;
1436 Elf_Internal_Shdr *this_hdr;
1437
1438 if (*failedptr)
1439 {
1440 /* We already failed; just get out of the bfd_map_over_sections
1441 loop. */
1442 return;
1443 }
1444
1445 this_hdr = &elf_section_data (asect)->this_hdr;
1446
1447 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1448 asect->name,
1449 true, false);
1450 if (this_hdr->sh_name == (unsigned long) -1)
1451 {
1452 *failedptr = true;
1453 return;
1454 }
1455
1456 this_hdr->sh_flags = 0;
ae115e51 1457
50bd50d4
MH
1458 if ((asect->flags & SEC_ALLOC) != 0
1459 || asect->user_set_vma)
fd0198f0 1460 this_hdr->sh_addr = asect->vma;
ede4eed4
KR
1461 else
1462 this_hdr->sh_addr = 0;
ae115e51 1463
ede4eed4
KR
1464 this_hdr->sh_offset = 0;
1465 this_hdr->sh_size = asect->_raw_size;
1466 this_hdr->sh_link = 0;
ede4eed4 1467 this_hdr->sh_addralign = 1 << asect->alignment_power;
fd0198f0
ILT
1468 /* The sh_entsize and sh_info fields may have been set already by
1469 copy_private_section_data. */
ede4eed4
KR
1470
1471 this_hdr->bfd_section = asect;
1472 this_hdr->contents = NULL;
1473
1474 /* FIXME: This should not be based on section names. */
1475 if (strcmp (asect->name, ".dynstr") == 0)
1476 this_hdr->sh_type = SHT_STRTAB;
1477 else if (strcmp (asect->name, ".hash") == 0)
1478 {
1479 this_hdr->sh_type = SHT_HASH;
1480 this_hdr->sh_entsize = bed->s->arch_size / 8;
1481 }
1482 else if (strcmp (asect->name, ".dynsym") == 0)
1483 {
1484 this_hdr->sh_type = SHT_DYNSYM;
1485 this_hdr->sh_entsize = bed->s->sizeof_sym;
1486 }
1487 else if (strcmp (asect->name, ".dynamic") == 0)
1488 {
1489 this_hdr->sh_type = SHT_DYNAMIC;
1490 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1491 }
1492 else if (strncmp (asect->name, ".rela", 5) == 0
1493 && get_elf_backend_data (abfd)->use_rela_p)
1494 {
1495 this_hdr->sh_type = SHT_RELA;
1496 this_hdr->sh_entsize = bed->s->sizeof_rela;
1497 }
1498 else if (strncmp (asect->name, ".rel", 4) == 0
1499 && ! get_elf_backend_data (abfd)->use_rela_p)
1500 {
1501 this_hdr->sh_type = SHT_REL;
1502 this_hdr->sh_entsize = bed->s->sizeof_rel;
1503 }
a66a61a0 1504 else if (strncmp (asect->name, ".note", 5) == 0)
ede4eed4
KR
1505 this_hdr->sh_type = SHT_NOTE;
1506 else if (strncmp (asect->name, ".stab", 5) == 0
1507 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1508 this_hdr->sh_type = SHT_STRTAB;
a66a61a0
ILT
1509 else if (strcmp (asect->name, ".gnu.version") == 0)
1510 {
1511 this_hdr->sh_type = SHT_GNU_versym;
1512 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1513 }
1514 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1515 {
1516 this_hdr->sh_type = SHT_GNU_verdef;
1517 this_hdr->sh_entsize = 0;
d6bfcdb5
ILT
1518 /* objcopy or strip will copy over sh_info, but may not set
1519 cverdefs. The linker will set cverdefs, but sh_info will be
1520 zero. */
1521 if (this_hdr->sh_info == 0)
1522 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1523 else
1524 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1525 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
a66a61a0
ILT
1526 }
1527 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1528 {
1529 this_hdr->sh_type = SHT_GNU_verneed;
1530 this_hdr->sh_entsize = 0;
d6bfcdb5
ILT
1531 /* objcopy or strip will copy over sh_info, but may not set
1532 cverrefs. The linker will set cverrefs, but sh_info will be
1533 zero. */
1534 if (this_hdr->sh_info == 0)
1535 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1536 else
1537 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1538 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
a66a61a0 1539 }
ede4eed4
KR
1540 else if ((asect->flags & SEC_ALLOC) != 0
1541 && (asect->flags & SEC_LOAD) != 0)
1542 this_hdr->sh_type = SHT_PROGBITS;
1543 else if ((asect->flags & SEC_ALLOC) != 0
1544 && ((asect->flags & SEC_LOAD) == 0))
5fe14a9f 1545 this_hdr->sh_type = SHT_NOBITS;
ede4eed4
KR
1546 else
1547 {
1548 /* Who knows? */
1549 this_hdr->sh_type = SHT_PROGBITS;
1550 }
1551
1552 if ((asect->flags & SEC_ALLOC) != 0)
1553 this_hdr->sh_flags |= SHF_ALLOC;
1554 if ((asect->flags & SEC_READONLY) == 0)
1555 this_hdr->sh_flags |= SHF_WRITE;
1556 if ((asect->flags & SEC_CODE) != 0)
1557 this_hdr->sh_flags |= SHF_EXECINSTR;
1558
1559 /* Check for processor-specific section types. */
1560 {
1561 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1562
1563 if (bed->elf_backend_fake_sections)
1564 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1565 }
1566
1567 /* If the section has relocs, set up a section header for the
1568 SHT_REL[A] section. */
1569 if ((asect->flags & SEC_RELOC) != 0)
1570 {
1571 Elf_Internal_Shdr *rela_hdr;
1572 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1573 char *name;
1574
1575 rela_hdr = &elf_section_data (asect)->rel_hdr;
1576 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1577 if (name == NULL)
1578 {
ede4eed4
KR
1579 *failedptr = true;
1580 return;
1581 }
1582 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1583 rela_hdr->sh_name =
1584 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1585 true, false);
1586 if (rela_hdr->sh_name == (unsigned int) -1)
1587 {
1588 *failedptr = true;
1589 return;
1590 }
1591 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1592 rela_hdr->sh_entsize = (use_rela_p
1593 ? bed->s->sizeof_rela
1594 : bed->s->sizeof_rel);
1595 rela_hdr->sh_addralign = bed->s->file_align;
1596 rela_hdr->sh_flags = 0;
1597 rela_hdr->sh_addr = 0;
1598 rela_hdr->sh_size = 0;
1599 rela_hdr->sh_offset = 0;
1600 }
1601}
1602
1603/* Assign all ELF section numbers. The dummy first section is handled here
1604 too. The link/info pointers for the standard section types are filled
1605 in here too, while we're at it. */
1606
1607static boolean
1608assign_section_numbers (abfd)
1609 bfd *abfd;
1610{
1611 struct elf_obj_tdata *t = elf_tdata (abfd);
1612 asection *sec;
1613 unsigned int section_number;
1614 Elf_Internal_Shdr **i_shdrp;
1615 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1616
1617 section_number = 1;
1618
1619 for (sec = abfd->sections; sec; sec = sec->next)
1620 {
1621 struct bfd_elf_section_data *d = elf_section_data (sec);
1622
1623 d->this_idx = section_number++;
1624 if ((sec->flags & SEC_RELOC) == 0)
1625 d->rel_idx = 0;
1626 else
1627 d->rel_idx = section_number++;
1628 }
1629
1630 t->shstrtab_section = section_number++;
1631 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1632 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1633
1634 if (abfd->symcount > 0)
1635 {
1636 t->symtab_section = section_number++;
1637 t->strtab_section = section_number++;
1638 }
1639
1640 elf_elfheader (abfd)->e_shnum = section_number;
1641
1642 /* Set up the list of section header pointers, in agreement with the
1643 indices. */
1644 i_shdrp = ((Elf_Internal_Shdr **)
1645 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1646 if (i_shdrp == NULL)
a9713b91 1647 return false;
ede4eed4
KR
1648
1649 i_shdrp[0] = ((Elf_Internal_Shdr *)
1650 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1651 if (i_shdrp[0] == NULL)
1652 {
1653 bfd_release (abfd, i_shdrp);
ede4eed4
KR
1654 return false;
1655 }
1656 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1657
1658 elf_elfsections (abfd) = i_shdrp;
1659
1660 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1661 if (abfd->symcount > 0)
1662 {
1663 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1664 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1665 t->symtab_hdr.sh_link = t->strtab_section;
1666 }
1667 for (sec = abfd->sections; sec; sec = sec->next)
1668 {
1669 struct bfd_elf_section_data *d = elf_section_data (sec);
1670 asection *s;
1671 const char *name;
1672
1673 i_shdrp[d->this_idx] = &d->this_hdr;
1674 if (d->rel_idx != 0)
1675 i_shdrp[d->rel_idx] = &d->rel_hdr;
1676
1677 /* Fill in the sh_link and sh_info fields while we're at it. */
1678
1679 /* sh_link of a reloc section is the section index of the symbol
1680 table. sh_info is the section index of the section to which
1681 the relocation entries apply. */
1682 if (d->rel_idx != 0)
1683 {
1684 d->rel_hdr.sh_link = t->symtab_section;
1685 d->rel_hdr.sh_info = d->this_idx;
1686 }
1687
1688 switch (d->this_hdr.sh_type)
1689 {
1690 case SHT_REL:
1691 case SHT_RELA:
1692 /* A reloc section which we are treating as a normal BFD
1693 section. sh_link is the section index of the symbol
1694 table. sh_info is the section index of the section to
1695 which the relocation entries apply. We assume that an
1696 allocated reloc section uses the dynamic symbol table.
1697 FIXME: How can we be sure? */
1698 s = bfd_get_section_by_name (abfd, ".dynsym");
1699 if (s != NULL)
1700 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1701
1702 /* We look up the section the relocs apply to by name. */
1703 name = sec->name;
1704 if (d->this_hdr.sh_type == SHT_REL)
1705 name += 4;
1706 else
1707 name += 5;
1708 s = bfd_get_section_by_name (abfd, name);
1709 if (s != NULL)
1710 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1711 break;
1712
1713 case SHT_STRTAB:
1714 /* We assume that a section named .stab*str is a stabs
1715 string section. We look for a section with the same name
1716 but without the trailing ``str'', and set its sh_link
1717 field to point to this section. */
1718 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1719 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1720 {
1721 size_t len;
1722 char *alc;
1723
1724 len = strlen (sec->name);
58142f10 1725 alc = (char *) bfd_malloc (len - 2);
ede4eed4 1726 if (alc == NULL)
58142f10 1727 return false;
ede4eed4
KR
1728 strncpy (alc, sec->name, len - 3);
1729 alc[len - 3] = '\0';
1730 s = bfd_get_section_by_name (abfd, alc);
1731 free (alc);
1732 if (s != NULL)
1733 {
1734 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1735
1736 /* This is a .stab section. */
1737 elf_section_data (s)->this_hdr.sh_entsize =
1738 4 + 2 * (bed->s->arch_size / 8);
1739 }
1740 }
1741 break;
1742
1743 case SHT_DYNAMIC:
1744 case SHT_DYNSYM:
a66a61a0
ILT
1745 case SHT_GNU_verneed:
1746 case SHT_GNU_verdef:
ede4eed4 1747 /* sh_link is the section header index of the string table
a66a61a0
ILT
1748 used for the dynamic entries, or the symbol table, or the
1749 version strings. */
ede4eed4
KR
1750 s = bfd_get_section_by_name (abfd, ".dynstr");
1751 if (s != NULL)
1752 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1753 break;
1754
1755 case SHT_HASH:
a66a61a0 1756 case SHT_GNU_versym:
ede4eed4 1757 /* sh_link is the section header index of the symbol table
a66a61a0 1758 this hash table or version table is for. */
ede4eed4
KR
1759 s = bfd_get_section_by_name (abfd, ".dynsym");
1760 if (s != NULL)
1761 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1762 break;
1763 }
1764 }
1765
1766 return true;
1767}
1768
1769/* Map symbol from it's internal number to the external number, moving
1770 all local symbols to be at the head of the list. */
1771
1772static INLINE int
1773sym_is_global (abfd, sym)
1774 bfd *abfd;
1775 asymbol *sym;
1776{
1777 /* If the backend has a special mapping, use it. */
1778 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1779 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1780 (abfd, sym));
1781
1782 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1783 || bfd_is_und_section (bfd_get_section (sym))
1784 || bfd_is_com_section (bfd_get_section (sym)));
1785}
1786
1787static boolean
1788elf_map_symbols (abfd)
1789 bfd *abfd;
1790{
1791 int symcount = bfd_get_symcount (abfd);
1792 asymbol **syms = bfd_get_outsymbols (abfd);
1793 asymbol **sect_syms;
1794 int num_locals = 0;
1795 int num_globals = 0;
1796 int num_locals2 = 0;
1797 int num_globals2 = 0;
1798 int max_index = 0;
1799 int num_sections = 0;
1800 int idx;
1801 asection *asect;
1802 asymbol **new_syms;
1803
1804#ifdef DEBUG
1805 fprintf (stderr, "elf_map_symbols\n");
1806 fflush (stderr);
1807#endif
1808
1809 /* Add a section symbol for each BFD section. FIXME: Is this really
1810 necessary? */
1811 for (asect = abfd->sections; asect; asect = asect->next)
1812 {
1813 if (max_index < asect->index)
1814 max_index = asect->index;
1815 }
1816
1817 max_index++;
1818 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1819 if (sect_syms == NULL)
a9713b91 1820 return false;
ede4eed4
KR
1821 elf_section_syms (abfd) = sect_syms;
1822
1823 for (idx = 0; idx < symcount; idx++)
1824 {
1825 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
5e3da1b0 1826 && syms[idx]->value == 0)
ede4eed4
KR
1827 {
1828 asection *sec;
1829
1830 sec = syms[idx]->section;
1831 if (sec->owner != NULL)
1832 {
1833 if (sec->owner != abfd)
1834 {
1835 if (sec->output_offset != 0)
1836 continue;
1837 sec = sec->output_section;
1838 BFD_ASSERT (sec->owner == abfd);
1839 }
1840 sect_syms[sec->index] = syms[idx];
1841 }
1842 }
1843 }
1844
1845 for (asect = abfd->sections; asect; asect = asect->next)
1846 {
1847 asymbol *sym;
1848
1849 if (sect_syms[asect->index] != NULL)
1850 continue;
1851
1852 sym = bfd_make_empty_symbol (abfd);
1853 if (sym == NULL)
1854 return false;
1855 sym->the_bfd = abfd;
1856 sym->name = asect->name;
1857 sym->value = 0;
1858 /* Set the flags to 0 to indicate that this one was newly added. */
1859 sym->flags = 0;
1860 sym->section = asect;
1861 sect_syms[asect->index] = sym;
1862 num_sections++;
1863#ifdef DEBUG
1864 fprintf (stderr,
9e7e5d5e 1865 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
ede4eed4
KR
1866 asect->name, (long) asect->vma, asect->index, (long) asect);
1867#endif
1868 }
1869
1870 /* Classify all of the symbols. */
1871 for (idx = 0; idx < symcount; idx++)
1872 {
1873 if (!sym_is_global (abfd, syms[idx]))
1874 num_locals++;
1875 else
1876 num_globals++;
1877 }
1878 for (asect = abfd->sections; asect; asect = asect->next)
1879 {
1880 if (sect_syms[asect->index] != NULL
1881 && sect_syms[asect->index]->flags == 0)
1882 {
1883 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1884 if (!sym_is_global (abfd, sect_syms[asect->index]))
1885 num_locals++;
1886 else
1887 num_globals++;
1888 sect_syms[asect->index]->flags = 0;
1889 }
1890 }
1891
1892 /* Now sort the symbols so the local symbols are first. */
1893 new_syms = ((asymbol **)
1894 bfd_alloc (abfd,
1895 (num_locals + num_globals) * sizeof (asymbol *)));
1896 if (new_syms == NULL)
a9713b91 1897 return false;
ede4eed4
KR
1898
1899 for (idx = 0; idx < symcount; idx++)
1900 {
1901 asymbol *sym = syms[idx];
1902 int i;
1903
1904 if (!sym_is_global (abfd, sym))
1905 i = num_locals2++;
1906 else
1907 i = num_locals + num_globals2++;
1908 new_syms[i] = sym;
1909 sym->udata.i = i + 1;
1910 }
1911 for (asect = abfd->sections; asect; asect = asect->next)
1912 {
1913 if (sect_syms[asect->index] != NULL
1914 && sect_syms[asect->index]->flags == 0)
1915 {
1916 asymbol *sym = sect_syms[asect->index];
1917 int i;
1918
1919 sym->flags = BSF_SECTION_SYM;
1920 if (!sym_is_global (abfd, sym))
1921 i = num_locals2++;
1922 else
1923 i = num_locals + num_globals2++;
1924 new_syms[i] = sym;
1925 sym->udata.i = i + 1;
1926 }
1927 }
1928
1929 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1930
1931 elf_num_locals (abfd) = num_locals;
1932 elf_num_globals (abfd) = num_globals;
1933 return true;
1934}
1935
fd0198f0
ILT
1936/* Align to the maximum file alignment that could be required for any
1937 ELF data structure. */
1938
1939static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1940static INLINE file_ptr
1941align_file_position (off, align)
1942 file_ptr off;
1943 int align;
1944{
1945 return (off + align - 1) & ~(align - 1);
1946}
1947
1948/* Assign a file position to a section, optionally aligning to the
1949 required section alignment. */
1950
1951INLINE file_ptr
1952_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1953 Elf_Internal_Shdr *i_shdrp;
1954 file_ptr offset;
1955 boolean align;
1956{
1957 if (align)
1958 {
1959 unsigned int al;
1960
1961 al = i_shdrp->sh_addralign;
1962 if (al > 1)
1963 offset = BFD_ALIGN (offset, al);
1964 }
1965 i_shdrp->sh_offset = offset;
1966 if (i_shdrp->bfd_section != NULL)
1967 i_shdrp->bfd_section->filepos = offset;
1968 if (i_shdrp->sh_type != SHT_NOBITS)
1969 offset += i_shdrp->sh_size;
1970 return offset;
1971}
1972
ede4eed4
KR
1973/* Compute the file positions we are going to put the sections at, and
1974 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1975 is not NULL, this is being called by the ELF backend linker. */
1976
1977boolean
1978_bfd_elf_compute_section_file_positions (abfd, link_info)
1979 bfd *abfd;
1980 struct bfd_link_info *link_info;
1981{
1982 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1983 boolean failed;
1984 struct bfd_strtab_hash *strtab;
1985 Elf_Internal_Shdr *shstrtab_hdr;
1986
1987 if (abfd->output_has_begun)
1988 return true;
1989
1990 /* Do any elf backend specific processing first. */
1991 if (bed->elf_backend_begin_write_processing)
1992 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1993
1994 if (! prep_headers (abfd))
1995 return false;
1996
1997 failed = false;
1998 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1999 if (failed)
2000 return false;
2001
2002 if (!assign_section_numbers (abfd))
2003 return false;
2004
2005 /* The backend linker builds symbol table information itself. */
fd0198f0 2006 if (link_info == NULL && abfd->symcount > 0)
ede4eed4 2007 {
37fcafe6
DE
2008 /* Non-zero if doing a relocatable link. */
2009 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2010
2011 if (! swap_out_syms (abfd, &strtab, relocatable_p))
ede4eed4
KR
2012 return false;
2013 }
2014
2015 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2016 /* sh_name was set in prep_headers. */
2017 shstrtab_hdr->sh_type = SHT_STRTAB;
2018 shstrtab_hdr->sh_flags = 0;
2019 shstrtab_hdr->sh_addr = 0;
2020 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2021 shstrtab_hdr->sh_entsize = 0;
2022 shstrtab_hdr->sh_link = 0;
2023 shstrtab_hdr->sh_info = 0;
fd0198f0 2024 /* sh_offset is set in assign_file_positions_except_relocs. */
ede4eed4
KR
2025 shstrtab_hdr->sh_addralign = 1;
2026
fd0198f0 2027 if (!assign_file_positions_except_relocs (abfd))
ede4eed4
KR
2028 return false;
2029
fd0198f0 2030 if (link_info == NULL && abfd->symcount > 0)
ede4eed4 2031 {
fd0198f0
ILT
2032 file_ptr off;
2033 Elf_Internal_Shdr *hdr;
2034
2035 off = elf_tdata (abfd)->next_file_pos;
2036
2037 hdr = &elf_tdata (abfd)->symtab_hdr;
2038 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2039
2040 hdr = &elf_tdata (abfd)->strtab_hdr;
2041 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2042
2043 elf_tdata (abfd)->next_file_pos = off;
2044
ede4eed4
KR
2045 /* Now that we know where the .strtab section goes, write it
2046 out. */
fd0198f0 2047 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ede4eed4
KR
2048 || ! _bfd_stringtab_emit (abfd, strtab))
2049 return false;
2050 _bfd_stringtab_free (strtab);
2051 }
2052
2053 abfd->output_has_begun = true;
2054
2055 return true;
2056}
2057
fd0198f0 2058/* Create a mapping from a set of sections to a program segment. */
ede4eed4 2059
fd0198f0 2060static INLINE struct elf_segment_map *
edf3fe48 2061make_mapping (abfd, sections, from, to, phdr)
fd0198f0
ILT
2062 bfd *abfd;
2063 asection **sections;
2064 unsigned int from;
2065 unsigned int to;
edf3fe48 2066 boolean phdr;
ede4eed4 2067{
fd0198f0
ILT
2068 struct elf_segment_map *m;
2069 unsigned int i;
2070 asection **hdrpp;
2071
2072 m = ((struct elf_segment_map *)
2073 bfd_zalloc (abfd,
2074 (sizeof (struct elf_segment_map)
2075 + (to - from - 1) * sizeof (asection *))));
2076 if (m == NULL)
a9713b91 2077 return NULL;
fd0198f0
ILT
2078 m->next = NULL;
2079 m->p_type = PT_LOAD;
2080 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2081 m->sections[i - from] = *hdrpp;
2082 m->count = to - from;
2083
edf3fe48 2084 if (from == 0 && phdr)
6933148a
ILT
2085 {
2086 /* Include the headers in the first PT_LOAD segment. */
2087 m->includes_filehdr = 1;
2088 m->includes_phdrs = 1;
2089 }
2090
fd0198f0 2091 return m;
ede4eed4
KR
2092}
2093
fd0198f0 2094/* Set up a mapping from BFD sections to program segments. */
ede4eed4 2095
fd0198f0
ILT
2096static boolean
2097map_sections_to_segments (abfd)
2098 bfd *abfd;
ede4eed4 2099{
fd0198f0
ILT
2100 asection **sections = NULL;
2101 asection *s;
2102 unsigned int i;
2103 unsigned int count;
2104 struct elf_segment_map *mfirst;
2105 struct elf_segment_map **pm;
2106 struct elf_segment_map *m;
2107 asection *last_hdr;
2108 unsigned int phdr_index;
2109 bfd_vma maxpagesize;
2110 asection **hdrpp;
9e7e5d5e 2111 boolean phdr_in_segment = true;
edf3fe48
ILT
2112 boolean writable;
2113 asection *dynsec;
fd0198f0
ILT
2114
2115 if (elf_tdata (abfd)->segment_map != NULL)
2116 return true;
2117
2118 if (bfd_count_sections (abfd) == 0)
2119 return true;
2120
2121 /* Select the allocated sections, and sort them. */
2122
58142f10
ILT
2123 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2124 * sizeof (asection *));
fd0198f0 2125 if (sections == NULL)
58142f10 2126 goto error_return;
ede4eed4 2127
fd0198f0
ILT
2128 i = 0;
2129 for (s = abfd->sections; s != NULL; s = s->next)
2130 {
2131 if ((s->flags & SEC_ALLOC) != 0)
2132 {
2133 sections[i] = s;
2134 ++i;
2135 }
5fe14a9f 2136 }
fd0198f0
ILT
2137 BFD_ASSERT (i <= bfd_count_sections (abfd));
2138 count = i;
ede4eed4 2139
fd0198f0 2140 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
ede4eed4 2141
fd0198f0 2142 /* Build the mapping. */
ede4eed4 2143
fd0198f0
ILT
2144 mfirst = NULL;
2145 pm = &mfirst;
ede4eed4 2146
fd0198f0
ILT
2147 /* If we have a .interp section, then create a PT_PHDR segment for
2148 the program headers and a PT_INTERP segment for the .interp
2149 section. */
2150 s = bfd_get_section_by_name (abfd, ".interp");
2151 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2152 {
2153 m = ((struct elf_segment_map *)
2154 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2155 if (m == NULL)
a9713b91 2156 goto error_return;
fd0198f0
ILT
2157 m->next = NULL;
2158 m->p_type = PT_PHDR;
2159 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2160 m->p_flags = PF_R | PF_X;
2161 m->p_flags_valid = 1;
6933148a 2162 m->includes_phdrs = 1;
ede4eed4 2163
fd0198f0
ILT
2164 *pm = m;
2165 pm = &m->next;
ede4eed4 2166
fd0198f0
ILT
2167 m = ((struct elf_segment_map *)
2168 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2169 if (m == NULL)
a9713b91 2170 goto error_return;
fd0198f0
ILT
2171 m->next = NULL;
2172 m->p_type = PT_INTERP;
2173 m->count = 1;
2174 m->sections[0] = s;
ede4eed4 2175
fd0198f0
ILT
2176 *pm = m;
2177 pm = &m->next;
2178 }
ede4eed4 2179
fd0198f0
ILT
2180 /* Look through the sections. We put sections in the same program
2181 segment when the start of the second section can be placed within
2182 a few bytes of the end of the first section. */
2183 last_hdr = NULL;
2184 phdr_index = 0;
2185 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
edf3fe48
ILT
2186 writable = false;
2187 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2188 if (dynsec != NULL
2189 && (dynsec->flags & SEC_LOAD) == 0)
2190 dynsec = NULL;
2191
7fc6a16a
ILT
2192 /* Deal with -Ttext or something similar such that the first section
2193 is not adjacent to the program headers. This is an
2194 approximation, since at this point we don't know exactly how many
2195 program headers we will need. */
2196 if (count > 0)
2197 {
2198 bfd_size_type phdr_size;
2199
2200 phdr_size = elf_tdata (abfd)->program_header_size;
2201 if (phdr_size == 0)
2202 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
cdb88e87 2203 if ((abfd->flags & D_PAGED) == 0
303b4cc6 2204 || sections[0]->lma < phdr_size
cdb88e87 2205 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
9e7e5d5e 2206 phdr_in_segment = false;
7fc6a16a 2207 }
edf3fe48 2208
fd0198f0 2209 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
ede4eed4 2210 {
fd0198f0 2211 asection *hdr;
191d910c 2212 boolean new_segment;
ede4eed4 2213
fd0198f0 2214 hdr = *hdrpp;
ede4eed4 2215
fd0198f0 2216 /* See if this section and the last one will fit in the same
191d910c
ILT
2217 segment. */
2218
2219 if (last_hdr == NULL)
2220 {
2221 /* If we don't have a segment yet, then we don't need a new
2222 one (we build the last one after this loop). */
2223 new_segment = false;
2224 }
2225 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2226 {
2227 /* If this section has a different relation between the
2228 virtual address and the load address, then we need a new
2229 segment. */
2230 new_segment = true;
2231 }
191d910c 2232 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
76af94b9 2233 < BFD_ALIGN (hdr->lma, maxpagesize))
191d910c
ILT
2234 {
2235 /* If putting this section in this segment would force us to
2236 skip a page in the segment, then we need a new segment. */
2237 new_segment = true;
2238 }
2239 else if ((last_hdr->flags & SEC_LOAD) == 0
2240 && (hdr->flags & SEC_LOAD) != 0)
2241 {
2242 /* We don't want to put a loadable section after a
2243 nonloadable section in the same segment. */
2244 new_segment = true;
2245 }
c63729b5
ILT
2246 else if ((abfd->flags & D_PAGED) == 0)
2247 {
2248 /* If the file is not demand paged, which means that we
2249 don't require the sections to be correctly aligned in the
2250 file, then there is no other reason for a new segment. */
2251 new_segment = false;
2252 }
191d910c
ILT
2253 else if (! writable
2254 && (hdr->flags & SEC_READONLY) == 0
2255 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2256 == hdr->lma))
2257 {
2258 /* We don't want to put a writable section in a read only
2259 segment, unless they are on the same page in memory
2260 anyhow. We already know that the last section does not
2261 bring us past the current section on the page, so the
2262 only case in which the new section is not on the same
2263 page as the previous section is when the previous section
2264 ends precisely on a page boundary. */
2265 new_segment = true;
2266 }
2267 else
2268 {
2269 /* Otherwise, we can use the same segment. */
2270 new_segment = false;
2271 }
2272
2273 if (! new_segment)
fd0198f0 2274 {
50bd50d4
MH
2275 if ((hdr->flags & SEC_READONLY) == 0)
2276 writable = true;
fd0198f0
ILT
2277 last_hdr = hdr;
2278 continue;
2279 }
ede4eed4 2280
191d910c
ILT
2281 /* We need a new program segment. We must create a new program
2282 header holding all the sections from phdr_index until hdr. */
ede4eed4 2283
9e7e5d5e 2284 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
fd0198f0
ILT
2285 if (m == NULL)
2286 goto error_return;
ede4eed4 2287
fd0198f0
ILT
2288 *pm = m;
2289 pm = &m->next;
ede4eed4 2290
edf3fe48
ILT
2291 if ((hdr->flags & SEC_READONLY) == 0)
2292 writable = true;
50bd50d4
MH
2293 else
2294 writable = false;
edf3fe48 2295
fd0198f0
ILT
2296 last_hdr = hdr;
2297 phdr_index = i;
9e7e5d5e 2298 phdr_in_segment = false;
ede4eed4 2299 }
fd0198f0
ILT
2300
2301 /* Create a final PT_LOAD program segment. */
2302 if (last_hdr != NULL)
ede4eed4 2303 {
9e7e5d5e 2304 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
fd0198f0
ILT
2305 if (m == NULL)
2306 goto error_return;
2307
2308 *pm = m;
2309 pm = &m->next;
ede4eed4
KR
2310 }
2311
fd0198f0 2312 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
edf3fe48 2313 if (dynsec != NULL)
ede4eed4 2314 {
fd0198f0
ILT
2315 m = ((struct elf_segment_map *)
2316 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2317 if (m == NULL)
a9713b91 2318 goto error_return;
fd0198f0
ILT
2319 m->next = NULL;
2320 m->p_type = PT_DYNAMIC;
2321 m->count = 1;
edf3fe48 2322 m->sections[0] = dynsec;
ede4eed4 2323
fd0198f0
ILT
2324 *pm = m;
2325 pm = &m->next;
ede4eed4
KR
2326 }
2327
a66a61a0
ILT
2328 /* For each loadable .note section, add a PT_NOTE segment. We don't
2329 use bfd_get_section_by_name, because if we link together
2330 nonloadable .note sections and loadable .note sections, we will
2331 generate two .note sections in the output file. FIXME: Using
2332 names for section types is bogus anyhow. */
2333 for (s = abfd->sections; s != NULL; s = s->next)
2334 {
2335 if ((s->flags & SEC_LOAD) != 0
2336 && strncmp (s->name, ".note", 5) == 0)
2337 {
2338 m = ((struct elf_segment_map *)
2339 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2340 if (m == NULL)
2341 goto error_return;
2342 m->next = NULL;
2343 m->p_type = PT_NOTE;
2344 m->count = 1;
2345 m->sections[0] = s;
2346
2347 *pm = m;
2348 pm = &m->next;
2349 }
2350 }
2351
fd0198f0
ILT
2352 free (sections);
2353 sections = NULL;
ae115e51 2354
fd0198f0
ILT
2355 elf_tdata (abfd)->segment_map = mfirst;
2356 return true;
2357
2358 error_return:
2359 if (sections != NULL)
2360 free (sections);
2361 return false;
ede4eed4
KR
2362}
2363
9e7e5d5e 2364/* Sort sections by address. */
ede4eed4 2365
fd0198f0
ILT
2366static int
2367elf_sort_sections (arg1, arg2)
2368 const PTR arg1;
2369 const PTR arg2;
ede4eed4 2370{
fd0198f0
ILT
2371 const asection *sec1 = *(const asection **) arg1;
2372 const asection *sec2 = *(const asection **) arg2;
ede4eed4 2373
40afcc4c
NC
2374 /* Sort by LMA first, since this is the address used to
2375 place the section into a segment. */
2376 if (sec1->lma < sec2->lma)
fd0198f0 2377 return -1;
40afcc4c 2378 else if (sec1->lma > sec2->lma)
fd0198f0 2379 return 1;
ede4eed4 2380
9e7e5d5e
NC
2381 /* Then sort by VMA. Normally the LMA and the VMA will be
2382 the same, and this will do nothing. */
40afcc4c 2383 if (sec1->vma < sec2->vma)
cdb88e87 2384 return -1;
40afcc4c 2385 else if (sec1->vma > sec2->vma)
cdb88e87
ILT
2386 return 1;
2387
fd0198f0 2388 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
ede4eed4 2389
fd0198f0 2390#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
ede4eed4 2391
fd0198f0 2392 if (TOEND (sec1))
f6727b90
ILT
2393 {
2394 if (TOEND (sec2))
2395 return sec1->target_index - sec2->target_index;
eaa57a10 2396 else
f6727b90
ILT
2397 return 1;
2398 }
ede4eed4 2399
fd0198f0
ILT
2400 if (TOEND (sec2))
2401 return -1;
ede4eed4 2402
fd0198f0 2403#undef TOEND
ede4eed4 2404
fd0198f0
ILT
2405 /* Sort by size, to put zero sized sections before others at the
2406 same address. */
ede4eed4 2407
fd0198f0
ILT
2408 if (sec1->_raw_size < sec2->_raw_size)
2409 return -1;
2410 if (sec1->_raw_size > sec2->_raw_size)
2411 return 1;
ede4eed4 2412
fd0198f0
ILT
2413 return sec1->target_index - sec2->target_index;
2414}
ede4eed4 2415
fd0198f0
ILT
2416/* Assign file positions to the sections based on the mapping from
2417 sections to segments. This function also sets up some fields in
2418 the file header, and writes out the program headers. */
ede4eed4 2419
fd0198f0
ILT
2420static boolean
2421assign_file_positions_for_segments (abfd)
2422 bfd *abfd;
2423{
2424 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2425 unsigned int count;
2426 struct elf_segment_map *m;
2427 unsigned int alloc;
2428 Elf_Internal_Phdr *phdrs;
64f808f9 2429 file_ptr off, voff;
6933148a
ILT
2430 bfd_vma filehdr_vaddr, filehdr_paddr;
2431 bfd_vma phdrs_vaddr, phdrs_paddr;
fd0198f0
ILT
2432 Elf_Internal_Phdr *p;
2433
2434 if (elf_tdata (abfd)->segment_map == NULL)
2435 {
2436 if (! map_sections_to_segments (abfd))
2437 return false;
2438 }
ede4eed4 2439
5b3b9ff6
ILT
2440 if (bed->elf_backend_modify_segment_map)
2441 {
2442 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2443 return false;
2444 }
2445
fd0198f0
ILT
2446 count = 0;
2447 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2448 ++count;
ede4eed4 2449
fd0198f0
ILT
2450 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2451 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2452 elf_elfheader (abfd)->e_phnum = count;
ede4eed4 2453
fd0198f0
ILT
2454 if (count == 0)
2455 return true;
ede4eed4 2456
fd0198f0
ILT
2457 /* If we already counted the number of program segments, make sure
2458 that we allocated enough space. This happens when SIZEOF_HEADERS
2459 is used in a linker script. */
2460 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2461 if (alloc != 0 && count > alloc)
2462 {
2463 ((*_bfd_error_handler)
53d3ce37 2464 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
fd0198f0
ILT
2465 bfd_get_filename (abfd), alloc, count));
2466 bfd_set_error (bfd_error_bad_value);
2467 return false;
ede4eed4
KR
2468 }
2469
fd0198f0
ILT
2470 if (alloc == 0)
2471 alloc = count;
2472
2473 phdrs = ((Elf_Internal_Phdr *)
2474 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2475 if (phdrs == NULL)
a9713b91 2476 return false;
ede4eed4 2477
fd0198f0
ILT
2478 off = bed->s->sizeof_ehdr;
2479 off += alloc * bed->s->sizeof_phdr;
ede4eed4 2480
6933148a
ILT
2481 filehdr_vaddr = 0;
2482 filehdr_paddr = 0;
2483 phdrs_vaddr = 0;
2484 phdrs_paddr = 0;
eaa57a10 2485
fd0198f0
ILT
2486 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2487 m != NULL;
2488 m = m->next, p++)
2489 {
2490 unsigned int i;
2491 asection **secpp;
fd0198f0 2492
3b950780
ILT
2493 /* If elf_segment_map is not from map_sections_to_segments, the
2494 sections may not be correctly ordered. */
2495 if (m->count > 0)
2496 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2497 elf_sort_sections);
2498
fd0198f0
ILT
2499 p->p_type = m->p_type;
2500
2501 if (m->p_flags_valid)
2502 p->p_flags = m->p_flags;
14899eb7
ILT
2503 else
2504 p->p_flags = 0;
fd0198f0 2505
d49ddb85
ILT
2506 if (p->p_type == PT_LOAD
2507 && m->count > 0
d7775b43 2508 && (m->sections[0]->flags & SEC_ALLOC) != 0)
cdb88e87
ILT
2509 {
2510 if ((abfd->flags & D_PAGED) != 0)
2511 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2512 else
2513 off += ((m->sections[0]->vma - off)
2514 % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2515 }
44ef8897 2516
fd0198f0
ILT
2517 if (m->count == 0)
2518 p->p_vaddr = 0;
2519 else
2520 p->p_vaddr = m->sections[0]->vma;
ede4eed4 2521
fd0198f0
ILT
2522 if (m->p_paddr_valid)
2523 p->p_paddr = m->p_paddr;
2524 else if (m->count == 0)
2525 p->p_paddr = 0;
2526 else
2527 p->p_paddr = m->sections[0]->lma;
2528
cdb88e87
ILT
2529 if (p->p_type == PT_LOAD
2530 && (abfd->flags & D_PAGED) != 0)
fd0198f0
ILT
2531 p->p_align = bed->maxpagesize;
2532 else if (m->count == 0)
2533 p->p_align = bed->s->file_align;
2534 else
2535 p->p_align = 0;
2536
6933148a 2537 p->p_offset = 0;
fd0198f0
ILT
2538 p->p_filesz = 0;
2539 p->p_memsz = 0;
eaa57a10 2540
6933148a 2541 if (m->includes_filehdr)
ede4eed4 2542 {
14899eb7
ILT
2543 if (! m->p_flags_valid)
2544 p->p_flags |= PF_R;
6933148a
ILT
2545 p->p_offset = 0;
2546 p->p_filesz = bed->s->sizeof_ehdr;
2547 p->p_memsz = bed->s->sizeof_ehdr;
2548 if (m->count > 0)
2549 {
2550 BFD_ASSERT (p->p_type == PT_LOAD);
20b2c808 2551
f6727b90 2552 if (p->p_vaddr < (bfd_vma) off)
20b2c808 2553 {
53d3ce37 2554 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
20b2c808
DE
2555 bfd_get_filename (abfd));
2556 bfd_set_error (bfd_error_bad_value);
2557 return false;
2558 }
eaa57a10 2559
6933148a
ILT
2560 p->p_vaddr -= off;
2561 if (! m->p_paddr_valid)
2562 p->p_paddr -= off;
2563 }
2564 if (p->p_type == PT_LOAD)
2565 {
2566 filehdr_vaddr = p->p_vaddr;
2567 filehdr_paddr = p->p_paddr;
2568 }
2569 }
fd0198f0 2570
6933148a
ILT
2571 if (m->includes_phdrs)
2572 {
14899eb7
ILT
2573 if (! m->p_flags_valid)
2574 p->p_flags |= PF_R;
eaa57a10 2575
6933148a 2576 if (m->includes_filehdr)
fd0198f0 2577 {
6933148a 2578 if (p->p_type == PT_LOAD)
fd0198f0 2579 {
6933148a
ILT
2580 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2581 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
fd0198f0 2582 }
6933148a
ILT
2583 }
2584 else
2585 {
2586 p->p_offset = bed->s->sizeof_ehdr;
eaa57a10 2587
6933148a
ILT
2588 if (m->count > 0)
2589 {
2590 BFD_ASSERT (p->p_type == PT_LOAD);
2591 p->p_vaddr -= off - p->p_offset;
2592 if (! m->p_paddr_valid)
2593 p->p_paddr -= off - p->p_offset;
2594 }
eaa57a10 2595
6933148a 2596 if (p->p_type == PT_LOAD)
fd0198f0 2597 {
6933148a
ILT
2598 phdrs_vaddr = p->p_vaddr;
2599 phdrs_paddr = p->p_paddr;
fd0198f0 2600 }
9e7e5d5e
NC
2601 else
2602 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
6933148a 2603 }
eaa57a10 2604
6933148a
ILT
2605 p->p_filesz += alloc * bed->s->sizeof_phdr;
2606 p->p_memsz += alloc * bed->s->sizeof_phdr;
2607 }
2608
eaa57a10 2609 if (p->p_type == PT_LOAD
a6ac3b6b 2610 || (p->p_type == PT_NOTE && abfd->format == bfd_core))
6933148a
ILT
2611 {
2612 if (! m->includes_filehdr && ! m->includes_phdrs)
2613 p->p_offset = off;
2614 else
2615 {
2616 file_ptr adjust;
fd0198f0 2617
6933148a
ILT
2618 adjust = off - (p->p_offset + p->p_filesz);
2619 p->p_filesz += adjust;
2620 p->p_memsz += adjust;
fd0198f0 2621 }
ede4eed4
KR
2622 }
2623
64f808f9 2624 voff = off;
eaa57a10 2625
fd0198f0 2626 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
ede4eed4 2627 {
fd0198f0
ILT
2628 asection *sec;
2629 flagword flags;
2630 bfd_size_type align;
2631
2632 sec = *secpp;
2633 flags = sec->flags;
cdb88e87 2634 align = 1 << bfd_get_section_alignment (abfd, sec);
fd0198f0 2635
eaa57a10 2636 /* The section may have artificial alignment forced by a
e5fc7809
NC
2637 link script. Notice this case by the gap between the
2638 cumulative phdr vma and the section's vma. */
2639 if (p->p_vaddr + p->p_memsz < sec->vma)
2640 {
2641 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2642
2643 p->p_memsz += adjust;
2644 off += adjust;
2645 voff += adjust;
2646 if ((flags & SEC_LOAD) != 0)
2647 p->p_filesz += adjust;
2648 }
2649
fd0198f0
ILT
2650 if (p->p_type == PT_LOAD)
2651 {
9e7e5d5e 2652 bfd_signed_vma adjust;
eaa57a10 2653
c63729b5 2654 if ((flags & SEC_LOAD) != 0)
9e7e5d5e
NC
2655 {
2656 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2657 if (adjust < 0)
2658 adjust = 0;
2659 }
c63729b5 2660 else if ((flags & SEC_ALLOC) != 0)
fd0198f0 2661 {
c63729b5
ILT
2662 /* The section VMA must equal the file position
2663 modulo the page size. FIXME: I'm not sure if
2664 this adjustment is really necessary. We used to
2665 not have the SEC_LOAD case just above, and then
2666 this was necessary, but now I'm not sure. */
cdb88e87
ILT
2667 if ((abfd->flags & D_PAGED) != 0)
2668 adjust = (sec->vma - voff) % bed->maxpagesize;
2669 else
2670 adjust = (sec->vma - voff) % align;
c63729b5 2671 }
f6727b90
ILT
2672 else
2673 adjust = 0;
c63729b5
ILT
2674
2675 if (adjust != 0)
2676 {
2677 if (i == 0)
9e7e5d5e
NC
2678 {
2679 (* _bfd_error_handler)
2680 (_("Error: First section in segment (%s) starts at 0x%x"),
2681 bfd_section_name (abfd, sec), sec->lma);
2682 (* _bfd_error_handler)
2683 (_(" whereas segment starts at 0x%x"),
2684 p->p_paddr);
eaa57a10 2685
9e7e5d5e
NC
2686 return false;
2687 }
c63729b5
ILT
2688 p->p_memsz += adjust;
2689 off += adjust;
2690 voff += adjust;
2691 if ((flags & SEC_LOAD) != 0)
2692 p->p_filesz += adjust;
fd0198f0
ILT
2693 }
2694
2695 sec->filepos = off;
2696
c63729b5
ILT
2697 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2698 used in a linker script we may have a section with
2699 SEC_LOAD clear but which is supposed to have
2700 contents. */
2701 if ((flags & SEC_LOAD) != 0
2702 || (flags & SEC_HAS_CONTENTS) != 0)
fd0198f0 2703 off += sec->_raw_size;
eaa57a10 2704
64f808f9
ILT
2705 if ((flags & SEC_ALLOC) != 0)
2706 voff += sec->_raw_size;
fd0198f0
ILT
2707 }
2708
a6ac3b6b 2709 if (p->p_type == PT_NOTE && abfd->format == bfd_core)
b64beb2d
MS
2710 {
2711 if (i == 0) /* the actual "note" segment */
2712 { /* this one actually contains everything. */
2713 sec->filepos = off;
2714 p->p_filesz = sec->_raw_size;
2715 off += sec->_raw_size;
2716 voff = off;
2717 }
2718 else /* fake sections -- don't need to be written */
2719 {
2720 sec->filepos = 0;
2721 sec->_raw_size = 0;
2722 flags = sec->flags = 0; /* no contents */
2723 }
2724 p->p_memsz = 0;
2725 p->p_align = 1;
2726 }
2727 else
2728 {
2729 p->p_memsz += sec->_raw_size;
fd0198f0 2730
b64beb2d
MS
2731 if ((flags & SEC_LOAD) != 0)
2732 p->p_filesz += sec->_raw_size;
fd0198f0 2733
b64beb2d
MS
2734 if (align > p->p_align)
2735 p->p_align = align;
2736 }
fd0198f0
ILT
2737
2738 if (! m->p_flags_valid)
2739 {
14899eb7 2740 p->p_flags |= PF_R;
fd0198f0
ILT
2741 if ((flags & SEC_CODE) != 0)
2742 p->p_flags |= PF_X;
2743 if ((flags & SEC_READONLY) == 0)
2744 p->p_flags |= PF_W;
2745 }
ede4eed4 2746 }
fd0198f0 2747 }
ede4eed4 2748
fd0198f0
ILT
2749 /* Now that we have set the section file positions, we can set up
2750 the file positions for the non PT_LOAD segments. */
2751 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2752 m != NULL;
2753 m = m->next, p++)
2754 {
2755 if (p->p_type != PT_LOAD && m->count > 0)
ede4eed4 2756 {
6933148a
ILT
2757 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2758 p->p_offset = m->sections[0]->filepos;
2759 }
2760 if (m->count == 0)
2761 {
2762 if (m->includes_filehdr)
2763 {
2764 p->p_vaddr = filehdr_vaddr;
2765 if (! m->p_paddr_valid)
2766 p->p_paddr = filehdr_paddr;
2767 }
2768 else if (m->includes_phdrs)
2769 {
2770 p->p_vaddr = phdrs_vaddr;
2771 if (! m->p_paddr_valid)
2772 p->p_paddr = phdrs_paddr;
2773 }
ede4eed4 2774 }
ede4eed4
KR
2775 }
2776
fd0198f0
ILT
2777 /* Clear out any program headers we allocated but did not use. */
2778 for (; count < alloc; count++, p++)
ede4eed4 2779 {
fd0198f0
ILT
2780 memset (p, 0, sizeof *p);
2781 p->p_type = PT_NULL;
ede4eed4
KR
2782 }
2783
fd0198f0 2784 elf_tdata (abfd)->phdr = phdrs;
ede4eed4 2785
fd0198f0 2786 elf_tdata (abfd)->next_file_pos = off;
ede4eed4 2787
fd0198f0
ILT
2788 /* Write out the program headers. */
2789 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2790 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2791 return false;
2792
2793 return true;
2794}
2795
2796/* Get the size of the program header.
2797
2798 If this is called by the linker before any of the section VMA's are set, it
2799 can't calculate the correct value for a strange memory layout. This only
2800 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2801 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2802 data segment (exclusive of .interp and .dynamic).
2803
2804 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2805 will be two segments. */
2806
2807static bfd_size_type
2808get_program_header_size (abfd)
2809 bfd *abfd;
2810{
2811 size_t segs;
2812 asection *s;
2813 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2814
2815 /* We can't return a different result each time we're called. */
2816 if (elf_tdata (abfd)->program_header_size != 0)
2817 return elf_tdata (abfd)->program_header_size;
ae115e51 2818
3b950780
ILT
2819 if (elf_tdata (abfd)->segment_map != NULL)
2820 {
2821 struct elf_segment_map *m;
2822
2823 segs = 0;
2824 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2825 ++segs;
2826 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2827 return elf_tdata (abfd)->program_header_size;
2828 }
2829
fd0198f0
ILT
2830 /* Assume we will need exactly two PT_LOAD segments: one for text
2831 and one for data. */
2832 segs = 2;
2833
2834 s = bfd_get_section_by_name (abfd, ".interp");
2835 if (s != NULL && (s->flags & SEC_LOAD) != 0)
ede4eed4 2836 {
fd0198f0
ILT
2837 /* If we have a loadable interpreter section, we need a
2838 PT_INTERP segment. In this case, assume we also need a
2839 PT_PHDR segment, although that may not be true for all
2840 targets. */
2841 segs += 2;
ede4eed4
KR
2842 }
2843
fd0198f0 2844 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
ede4eed4 2845 {
fd0198f0
ILT
2846 /* We need a PT_DYNAMIC segment. */
2847 ++segs;
ede4eed4 2848 }
ede4eed4 2849
a66a61a0
ILT
2850 for (s = abfd->sections; s != NULL; s = s->next)
2851 {
2852 if ((s->flags & SEC_LOAD) != 0
2853 && strncmp (s->name, ".note", 5) == 0)
2854 {
2855 /* We need a PT_NOTE segment. */
2856 ++segs;
2857 }
2858 }
2859
fd0198f0 2860 /* Let the backend count up any program headers it might need. */
5b3b9ff6
ILT
2861 if (bed->elf_backend_additional_program_headers)
2862 {
2863 int a;
2864
2865 a = (*bed->elf_backend_additional_program_headers) (abfd);
2866 if (a == -1)
2867 abort ();
2868 segs += a;
2869 }
ede4eed4 2870
fd0198f0
ILT
2871 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2872 return elf_tdata (abfd)->program_header_size;
ede4eed4
KR
2873}
2874
2875/* Work out the file positions of all the sections. This is called by
2876 _bfd_elf_compute_section_file_positions. All the section sizes and
2877 VMAs must be known before this is called.
2878
2879 We do not consider reloc sections at this point, unless they form
2880 part of the loadable image. Reloc sections are assigned file
2881 positions in assign_file_positions_for_relocs, which is called by
2882 write_object_contents and final_link.
2883
fd0198f0 2884 We also don't set the positions of the .symtab and .strtab here. */
ede4eed4
KR
2885
2886static boolean
fd0198f0 2887assign_file_positions_except_relocs (abfd)
ede4eed4 2888 bfd *abfd;
ede4eed4
KR
2889{
2890 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2891 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2892 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2893 file_ptr off;
2894 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2895
a6ac3b6b
MS
2896 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
2897 && abfd->format != bfd_core)
ede4eed4
KR
2898 {
2899 Elf_Internal_Shdr **hdrpp;
2900 unsigned int i;
2901
fd0198f0
ILT
2902 /* Start after the ELF header. */
2903 off = i_ehdrp->e_ehsize;
2904
ede4eed4
KR
2905 /* We are not creating an executable, which means that we are
2906 not creating a program header, and that the actual order of
2907 the sections in the file is unimportant. */
2908 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2909 {
2910 Elf_Internal_Shdr *hdr;
2911
2912 hdr = *hdrpp;
2913 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2914 {
2915 hdr->sh_offset = -1;
2916 continue;
2917 }
fd0198f0
ILT
2918 if (i == tdata->symtab_section
2919 || i == tdata->strtab_section)
ede4eed4
KR
2920 {
2921 hdr->sh_offset = -1;
2922 continue;
2923 }
eaa57a10 2924
5fe14a9f 2925 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
ede4eed4
KR
2926 }
2927 }
2928 else
2929 {
ede4eed4 2930 unsigned int i;
fd0198f0 2931 Elf_Internal_Shdr **hdrpp;
ede4eed4 2932
fd0198f0
ILT
2933 /* Assign file positions for the loaded sections based on the
2934 assignment of sections to segments. */
2935 if (! assign_file_positions_for_segments (abfd))
ede4eed4
KR
2936 return false;
2937
fd0198f0
ILT
2938 /* Assign file positions for the other sections. */
2939
2940 off = elf_tdata (abfd)->next_file_pos;
2941 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
ede4eed4
KR
2942 {
2943 Elf_Internal_Shdr *hdr;
2944
2945 hdr = *hdrpp;
fd0198f0
ILT
2946 if (hdr->bfd_section != NULL
2947 && hdr->bfd_section->filepos != 0)
2948 hdr->sh_offset = hdr->bfd_section->filepos;
2949 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
ede4eed4 2950 {
fd0198f0 2951 ((*_bfd_error_handler)
53d3ce37 2952 (_("%s: warning: allocated section `%s' not in segment"),
fd0198f0
ILT
2953 bfd_get_filename (abfd),
2954 (hdr->bfd_section == NULL
2955 ? "*unknown*"
2956 : hdr->bfd_section->name)));
cdb88e87
ILT
2957 if ((abfd->flags & D_PAGED) != 0)
2958 off += (hdr->sh_addr - off) % bed->maxpagesize;
2959 else
2960 off += (hdr->sh_addr - off) % hdr->sh_addralign;
5fe14a9f
ILT
2961 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2962 false);
ede4eed4 2963 }
fd0198f0
ILT
2964 else if (hdr->sh_type == SHT_REL
2965 || hdr->sh_type == SHT_RELA
2966 || hdr == i_shdrpp[tdata->symtab_section]
2967 || hdr == i_shdrpp[tdata->strtab_section])
2968 hdr->sh_offset = -1;
2969 else
2970 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
eaa57a10 2971 }
ede4eed4
KR
2972 }
2973
2974 /* Place the section headers. */
2975 off = align_file_position (off, bed->s->file_align);
2976 i_ehdrp->e_shoff = off;
2977 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2978
2979 elf_tdata (abfd)->next_file_pos = off;
2980
2981 return true;
2982}
2983
ede4eed4
KR
2984static boolean
2985prep_headers (abfd)
2986 bfd *abfd;
2987{
2988 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
9e7e5d5e 2989 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
ede4eed4
KR
2990 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2991 int count;
2992 struct bfd_strtab_hash *shstrtab;
2993 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2994
2995 i_ehdrp = elf_elfheader (abfd);
2996 i_shdrp = elf_elfsections (abfd);
2997
2998 shstrtab = _bfd_elf_stringtab_init ();
2999 if (shstrtab == NULL)
3000 return false;
3001
3002 elf_shstrtab (abfd) = shstrtab;
3003
3004 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3005 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3006 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3007 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3008
3009 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3010 i_ehdrp->e_ident[EI_DATA] =
86587dd4 3011 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
ede4eed4
KR
3012 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3013
3014 for (count = EI_PAD; count < EI_NIDENT; count++)
3015 i_ehdrp->e_ident[count] = 0;
3016
3017 if ((abfd->flags & DYNAMIC) != 0)
3018 i_ehdrp->e_type = ET_DYN;
3019 else if ((abfd->flags & EXEC_P) != 0)
3020 i_ehdrp->e_type = ET_EXEC;
b64beb2d
MS
3021 else if (bfd_get_format (abfd) == bfd_core)
3022 i_ehdrp->e_type = ET_CORE;
ede4eed4
KR
3023 else
3024 i_ehdrp->e_type = ET_REL;
3025
3026 switch (bfd_get_arch (abfd))
3027 {
3028 case bfd_arch_unknown:
3029 i_ehdrp->e_machine = EM_NONE;
3030 break;
3031 case bfd_arch_sparc:
3032 if (bed->s->arch_size == 64)
b356d4af 3033 i_ehdrp->e_machine = EM_SPARCV9;
ede4eed4
KR
3034 else
3035 i_ehdrp->e_machine = EM_SPARC;
3036 break;
3037 case bfd_arch_i386:
3038 i_ehdrp->e_machine = EM_386;
3039 break;
3040 case bfd_arch_m68k:
3041 i_ehdrp->e_machine = EM_68K;
3042 break;
3043 case bfd_arch_m88k:
3044 i_ehdrp->e_machine = EM_88K;
3045 break;
3046 case bfd_arch_i860:
3047 i_ehdrp->e_machine = EM_860;
3048 break;
3049 case bfd_arch_mips: /* MIPS Rxxxx */
3050 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3051 break;
3052 case bfd_arch_hppa:
3053 i_ehdrp->e_machine = EM_PARISC;
3054 break;
3055 case bfd_arch_powerpc:
3056 i_ehdrp->e_machine = EM_PPC;
3057 break;
50bd50d4
MH
3058 case bfd_arch_alpha:
3059 i_ehdrp->e_machine = EM_ALPHA;
3060 break;
f0c12b73
DE
3061 case bfd_arch_sh:
3062 i_ehdrp->e_machine = EM_SH;
3063 break;
50bd50d4
MH
3064 case bfd_arch_d10v:
3065 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3066 break;
fd8d7c31
MH
3067 case bfd_arch_d30v:
3068 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3069 break;
ac0442dd
NC
3070 case bfd_arch_fr30:
3071 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3072 break;
f0c12b73 3073 case bfd_arch_v850:
8988d935
NC
3074 switch (bfd_get_mach (abfd))
3075 {
3076 default:
3077 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
8988d935 3078 }
f0c12b73 3079 break;
8988d935 3080 case bfd_arch_arc:
ede4eed4
KR
3081 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3082 break;
b64beb2d
MS
3083 case bfd_arch_arm:
3084 i_ehdrp->e_machine = EM_ARM;
3085 break;
f0c12b73
DE
3086 case bfd_arch_m32r:
3087 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3088 break;
80be821d
ILT
3089 case bfd_arch_mn10200:
3090 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3091 break;
3092 case bfd_arch_mn10300:
3093 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
efc2b064 3094 break;
ede4eed4
KR
3095 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3096 default:
3097 i_ehdrp->e_machine = EM_NONE;
3098 }
3099 i_ehdrp->e_version = bed->s->ev_current;
3100 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3101
3102 /* no program header, for now. */
3103 i_ehdrp->e_phoff = 0;
3104 i_ehdrp->e_phentsize = 0;
3105 i_ehdrp->e_phnum = 0;
3106
3107 /* each bfd section is section header entry */
3108 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3109 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3110
3111 /* if we're building an executable, we'll need a program header table */
3112 if (abfd->flags & EXEC_P)
3113 {
3114 /* it all happens later */
3115#if 0
3116 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3117
3118 /* elf_build_phdrs() returns a (NULL-terminated) array of
3119 Elf_Internal_Phdrs */
3120 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3121 i_ehdrp->e_phoff = outbase;
3122 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3123#endif
3124 }
3125 else
3126 {
3127 i_ehdrp->e_phentsize = 0;
3128 i_phdrp = 0;
3129 i_ehdrp->e_phoff = 0;
3130 }
3131
3132 elf_tdata (abfd)->symtab_hdr.sh_name =
3133 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3134 elf_tdata (abfd)->strtab_hdr.sh_name =
3135 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3136 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3137 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3138 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3139 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3140 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3141 return false;
3142
3143 return true;
3144}
3145
3146/* Assign file positions for all the reloc sections which are not part
3147 of the loadable file image. */
3148
3149void
3150_bfd_elf_assign_file_positions_for_relocs (abfd)
3151 bfd *abfd;
3152{
3153 file_ptr off;
3154 unsigned int i;
3155 Elf_Internal_Shdr **shdrpp;
3156
3157 off = elf_tdata (abfd)->next_file_pos;
3158
3159 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3160 i < elf_elfheader (abfd)->e_shnum;
3161 i++, shdrpp++)
3162 {
3163 Elf_Internal_Shdr *shdrp;
3164
3165 shdrp = *shdrpp;
3166 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3167 && shdrp->sh_offset == -1)
5fe14a9f 3168 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
ede4eed4
KR
3169 }
3170
3171 elf_tdata (abfd)->next_file_pos = off;
3172}
3173
3174boolean
3175_bfd_elf_write_object_contents (abfd)
3176 bfd *abfd;
3177{
3178 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3179 Elf_Internal_Ehdr *i_ehdrp;
3180 Elf_Internal_Shdr **i_shdrp;
3181 boolean failed;
3182 unsigned int count;
3183
3184 if (! abfd->output_has_begun
9e7e5d5e
NC
3185 && ! _bfd_elf_compute_section_file_positions
3186 (abfd, (struct bfd_link_info *) NULL))
ede4eed4
KR
3187 return false;
3188
3189 i_shdrp = elf_elfsections (abfd);
3190 i_ehdrp = elf_elfheader (abfd);
3191
3192 failed = false;
3193 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3194 if (failed)
3195 return false;
9e7e5d5e 3196
ede4eed4
KR
3197 _bfd_elf_assign_file_positions_for_relocs (abfd);
3198
3199 /* After writing the headers, we need to write the sections too... */
3200 for (count = 1; count < i_ehdrp->e_shnum; count++)
3201 {
3202 if (bed->elf_backend_section_processing)
3203 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3204 if (i_shdrp[count]->contents)
3205 {
3206 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3207 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3208 1, abfd)
3209 != i_shdrp[count]->sh_size))
3210 return false;
3211 }
3212 }
3213
3214 /* Write out the section header names. */
3215 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3216 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3217 return false;
3218
3219 if (bed->elf_backend_final_write_processing)
3220 (*bed->elf_backend_final_write_processing) (abfd,
3221 elf_tdata (abfd)->linker);
3222
3223 return bed->s->write_shdrs_and_ehdr (abfd);
3224}
3225
b64beb2d
MS
3226boolean
3227_bfd_elf_write_corefile_contents (abfd)
3228 bfd *abfd;
3229{
3230 /* Hopefully this can be done just like an object file. */
3231 return _bfd_elf_write_object_contents (abfd);
3232}
ede4eed4
KR
3233/* given a section, search the header to find them... */
3234int
3235_bfd_elf_section_from_bfd_section (abfd, asect)
3236 bfd *abfd;
3237 struct sec *asect;
3238{
3239 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3240 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3241 int index;
3242 Elf_Internal_Shdr *hdr;
3243 int maxindex = elf_elfheader (abfd)->e_shnum;
3244
3245 for (index = 0; index < maxindex; index++)
3246 {
3247 hdr = i_shdrp[index];
3248 if (hdr->bfd_section == asect)
3249 return index;
3250 }
3251
3252 if (bed->elf_backend_section_from_bfd_section)
3253 {
3254 for (index = 0; index < maxindex; index++)
3255 {
3256 int retval;
3257
3258 hdr = i_shdrp[index];
3259 retval = index;
3260 if ((*bed->elf_backend_section_from_bfd_section)
3261 (abfd, hdr, asect, &retval))
3262 return retval;
3263 }
3264 }
3265
3266 if (bfd_is_abs_section (asect))
3267 return SHN_ABS;
3268 if (bfd_is_com_section (asect))
3269 return SHN_COMMON;
3270 if (bfd_is_und_section (asect))
3271 return SHN_UNDEF;
3272
22b17c74
ILT
3273 bfd_set_error (bfd_error_nonrepresentable_section);
3274
ede4eed4
KR
3275 return -1;
3276}
3277
cb84f028
ILT
3278/* Given a BFD symbol, return the index in the ELF symbol table, or -1
3279 on error. */
3280
3281int
ede4eed4
KR
3282_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3283 bfd *abfd;
7fc6a16a 3284 asymbol **asym_ptr_ptr;
ede4eed4 3285{
7fc6a16a 3286 asymbol *asym_ptr = *asym_ptr_ptr;
ede4eed4
KR
3287 int idx;
3288 flagword flags = asym_ptr->flags;
3289
3290 /* When gas creates relocations against local labels, it creates its
3291 own symbol for the section, but does put the symbol into the
3292 symbol chain, so udata is 0. When the linker is generating
3293 relocatable output, this section symbol may be for one of the
3294 input sections rather than the output section. */
3295 if (asym_ptr->udata.i == 0
3296 && (flags & BSF_SECTION_SYM)
3297 && asym_ptr->section)
3298 {
3299 int indx;
3300
3301 if (asym_ptr->section->output_section != NULL)
3302 indx = asym_ptr->section->output_section->index;
3303 else
3304 indx = asym_ptr->section->index;
3305 if (elf_section_syms (abfd)[indx])
3306 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3307 }
3308
3309 idx = asym_ptr->udata.i;
cb84f028
ILT
3310
3311 if (idx == 0)
3312 {
3313 /* This case can occur when using --strip-symbol on a symbol
3314 which is used in a relocation entry. */
3315 (*_bfd_error_handler)
53d3ce37 3316 (_("%s: symbol `%s' required but not present"),
cb84f028
ILT
3317 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3318 bfd_set_error (bfd_error_no_symbols);
3319 return -1;
3320 }
ede4eed4
KR
3321
3322#if DEBUG & 4
3323 {
3324 fprintf (stderr,
53d3ce37 3325 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
cb84f028
ILT
3326 (long) asym_ptr, asym_ptr->name, idx, flags,
3327 elf_symbol_flags (flags));
ede4eed4
KR
3328 fflush (stderr);
3329 }
3330#endif
3331
3332 return idx;
3333}
3334
3dbf33ee
ILT
3335/* Copy private BFD data. This copies any program header information. */
3336
3337static boolean
3338copy_private_bfd_data (ibfd, obfd)
3339 bfd *ibfd;
3340 bfd *obfd;
3341{
6933148a 3342 Elf_Internal_Ehdr *iehdr;
3dbf33ee
ILT
3343 struct elf_segment_map *mfirst;
3344 struct elf_segment_map **pm;
53d3ce37 3345 struct elf_segment_map *m;
3dbf33ee 3346 Elf_Internal_Phdr *p;
9e7e5d5e
NC
3347 unsigned int i;
3348 unsigned int num_segments;
b64beb2d 3349 boolean phdr_included = false;
eaa57a10 3350
3dbf33ee
ILT
3351 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3352 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3353 return true;
3354
3355 if (elf_tdata (ibfd)->phdr == NULL)
3356 return true;
3357
6933148a
ILT
3358 iehdr = elf_elfheader (ibfd);
3359
3dbf33ee
ILT
3360 mfirst = NULL;
3361 pm = &mfirst;
3362
9e7e5d5e
NC
3363 num_segments = elf_elfheader (ibfd)->e_phnum;
3364
3365#define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3366 ((addr) >= (bottom) \
3367 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3368 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3369
b64beb2d
MS
3370 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3371
a6ac3b6b
MS
3372#define IS_COREFILE_NOTE(p, s) \
3373 (p->p_type == PT_NOTE \
3374 && ibfd->format == bfd_core \
3375 && s->vma == 0 && s->lma == 0 \
3376 && (bfd_vma) s->filepos >= p->p_offset \
3377 && (bfd_vma) s->filepos + s->_raw_size \
3378 <= p->p_offset + p->p_filesz)
b64beb2d 3379
9e7e5d5e
NC
3380 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3381 linker, which generates a PT_INTERP section with p_vaddr and
3382 p_memsz set to 0. */
b64beb2d 3383
9e7e5d5e
NC
3384#define IS_SOLARIS_PT_INTERP(p, s) \
3385 (p->p_vaddr == 0 \
3386 && p->p_filesz > 0 \
3387 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3388 && s->_raw_size > 0 \
3389 && (bfd_vma) s->filepos >= p->p_offset \
3390 && ((bfd_vma) s->filepos + s->_raw_size \
3391 <= p->p_offset + p->p_filesz))
3392
9e7e5d5e
NC
3393 /* Scan through the segments specified in the program header
3394 of the input BFD. */
3395 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3dbf33ee 3396 {
3dbf33ee 3397 unsigned int csecs;
6933148a 3398 asection *s;
9e7e5d5e
NC
3399 asection **sections;
3400 asection *os;
6933148a 3401 unsigned int isec;
9e7e5d5e
NC
3402 bfd_vma matching_lma;
3403 bfd_vma suggested_lma;
3404 unsigned int j;
3405
b64beb2d
MS
3406 /* For each section in the input BFD, decide if it should be
3407 included in the current segment. A section will be included
3408 if it is within the address space of the segment, and it is
3409 an allocated segment, and there is an output section
3410 associated with it. */
3dbf33ee 3411 csecs = 0;
6933148a 3412 for (s = ibfd->sections; s != NULL; s = s->next)
b64beb2d
MS
3413 if (s->output_section != NULL)
3414 {
a6ac3b6b
MS
3415 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3416 || IS_SOLARIS_PT_INTERP (p, s))
3417 && (s->flags & SEC_ALLOC) != 0)
b64beb2d
MS
3418 ++csecs;
3419 else if (IS_COREFILE_NOTE (p, s))
3420 ++csecs;
3421 }
3dbf33ee 3422
9e7e5d5e
NC
3423 /* Allocate a segment map big enough to contain all of the
3424 sections we have selected. */
3dbf33ee
ILT
3425 m = ((struct elf_segment_map *)
3426 bfd_alloc (obfd,
3427 (sizeof (struct elf_segment_map)
20db2495 3428 + ((size_t) csecs - 1) * sizeof (asection *))));
3dbf33ee 3429 if (m == NULL)
a9713b91 3430 return false;
3dbf33ee 3431
9e7e5d5e
NC
3432 /* Initialise the fields of the segment map. Default to
3433 using the physical address of the segment in the input BFD. */
3434 m->next = NULL;
3435 m->p_type = p->p_type;
3436 m->p_flags = p->p_flags;
3dbf33ee 3437 m->p_flags_valid = 1;
9e7e5d5e 3438 m->p_paddr = p->p_paddr;
3dbf33ee
ILT
3439 m->p_paddr_valid = 1;
3440
9e7e5d5e
NC
3441 /* Determine if this segment contains the ELF file header
3442 and if it contains the program headers themselves. */
6933148a
ILT
3443 m->includes_filehdr = (p->p_offset == 0
3444 && p->p_filesz >= iehdr->e_ehsize);
3445
b64beb2d 3446 if (! phdr_included || p->p_type != PT_LOAD)
9e7e5d5e 3447 {
b64beb2d 3448 m->includes_phdrs =
9e7e5d5e
NC
3449 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3450 && (p->p_offset + p->p_filesz
3451 >= ((bfd_vma) iehdr->e_phoff
3452 + iehdr->e_phnum * iehdr->e_phentsize)));
b64beb2d
MS
3453 if (p->p_type == PT_LOAD && m->includes_phdrs)
3454 phdr_included = true;
9e7e5d5e 3455 }
3dbf33ee 3456
9e7e5d5e 3457 if (csecs == 0)
6933148a 3458 {
9e7e5d5e
NC
3459 /* Special segments, such as the PT_PHDR segment, may contain
3460 no sections, but ordinary, loadable segments should contain
3461 something. */
eaa57a10 3462
9e7e5d5e
NC
3463 if (p->p_type == PT_LOAD)
3464 _bfd_error_handler
3465 (_("%s: warning: Empty loadable segment detected\n"),
3466 bfd_get_filename (ibfd));
eaa57a10 3467
9e7e5d5e
NC
3468 m->count = 0;
3469 *pm = m;
3470 pm = &m->next;
eaa57a10 3471
9e7e5d5e
NC
3472 continue;
3473 }
eaa57a10 3474
9e7e5d5e
NC
3475 /* Now scan the sections in the input BFD again and attempt
3476 to add their corresponding output sections to the segment map.
3477 The problem here is how to handle an output section which has
3478 been moved (ie had its LMA changed). There are four possibilities:
3479
3480 1. None of the sections have been moved.
3481 In this case we can continue to use the segment LMA from the
3482 input BFD.
eaa57a10 3483
9e7e5d5e
NC
3484 2. All of the sections have been moved by the same amount.
3485 In this case we can change the segment's LMA to match the LMA
3486 of the first section.
3487
3488 3. Some of the sections have been moved, others have not.
3489 In this case those sections which have not been moved can be
3490 placed in the current segment which will have to have its size,
3491 and possibly its LMA changed, and a new segment or segments will
3492 have to be created to contain the other sections.
3493
3494 4. The sections have been moved, but not be the same amount.
3495 In this case we can change the segment's LMA to match the LMA
3496 of the first section and we will have to create a new segment
eaa57a10
UD
3497 or segments to contain the other sections.
3498
9e7e5d5e
NC
3499 In order to save time, we allocate an array to hold the section
3500 pointers that we are interested in. As these sections get assigned
3501 to a segment, they are removed from this array. */
eaa57a10 3502
9e7e5d5e
NC
3503 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3504 if (sections == NULL)
3505 return false;
eaa57a10 3506
9e7e5d5e
NC
3507 /* Step One: Scan for segment vs section LMA conflicts.
3508 Also add the sections to the section array allocated above.
3509 Also add the sections to the current segment. In the common
3510 case, where the sections have not been moved, this means that
3511 we have completely filled the segment, and there is nothing
3512 more to do. */
eaa57a10 3513
9e7e5d5e
NC
3514 isec = 0;
3515 matching_lma = false;
3516 suggested_lma = 0;
3517
3518 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3519 {
e5fc7809 3520 os = s->output_section;
eaa57a10 3521
b64beb2d
MS
3522 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3523 || IS_SOLARIS_PT_INTERP (p, s))
eaa57a10 3524 && (s->flags & SEC_ALLOC) != 0)
b64beb2d 3525 || IS_COREFILE_NOTE (p, s))
e5fc7809 3526 && os != NULL)
3dbf33ee 3527 {
9e7e5d5e 3528 sections[j++] = s;
eaa57a10 3529
22b17c74
ILT
3530 /* The Solaris native linker always sets p_paddr to 0.
3531 We try to catch that case here, and set it to the
3532 correct value. */
3533 if (p->p_paddr == 0
3534 && p->p_vaddr != 0
3535 && isec == 0
3536 && os->lma != 0
3537 && (os->vma == (p->p_vaddr
3538 + (m->includes_filehdr
3539 ? iehdr->e_ehsize
3540 : 0)
3541 + (m->includes_phdrs
3542 ? iehdr->e_phnum * iehdr->e_phentsize
3543 : 0))))
3544 m->p_paddr = p->p_vaddr;
3545
e5fc7809 3546 /* Match up the physical address of the segment with the
9e7e5d5e 3547 LMA address of the output section. */
a6ac3b6b
MS
3548 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3549 || IS_COREFILE_NOTE (p, s))
9e7e5d5e
NC
3550 {
3551 if (matching_lma == 0)
3552 matching_lma = os->lma;
eaa57a10 3553
9e7e5d5e
NC
3554 /* We assume that if the section fits within the segment
3555 that it does not overlap any other section within that
3556 segment. */
3557 m->sections[isec++] = os;
3558 }
e5fc7809
NC
3559 else if (suggested_lma == 0)
3560 suggested_lma = os->lma;
e5fc7809 3561 }
9e7e5d5e
NC
3562 }
3563
3564 BFD_ASSERT (j == csecs);
3565
3566 /* Step Two: Adjust the physical address of the current segment,
3567 if necessary. */
3568 if (isec == csecs)
3569 {
3570 /* All of the sections fitted within the segment as currently
3571 specified. This is the default case. Add the segment to
3572 the list of built segments and carry on to process the next
3573 program header in the input BFD. */
3574 m->count = csecs;
3575 *pm = m;
3576 pm = &m->next;
3577
3578 free (sections);
3579 continue;
3580 }
3581 else if (matching_lma != 0)
3582 {
3583 /* At least one section fits inside the current segment.
3584 Keep it, but modify its physical address to match the
3585 LMA of the first section that fitted. */
3586
3587 m->p_paddr = matching_lma;
3588 }
eaa57a10 3589 else
9e7e5d5e
NC
3590 {
3591 /* None of the sections fitted inside the current segment.
3592 Change the current segment's physical address to match
3593 the LMA of the first section. */
3594
3595 m->p_paddr = suggested_lma;
3596 }
3597
3598 /* Step Three: Loop over the sections again, this time assigning
3599 those that fit to the current segment and remvoing them from the
3600 sections array; but making sure not to leave large gaps. Once all
3601 possible sections have been assigned to the current segment it is
3602 added to the list of built segments and if sections still remain
3603 to be assigned, a new segment is constructed before repeating
3604 the loop. */
3605 isec = 0;
3606 do
3607 {
3608 m->count = 0;
3609 suggested_lma = 0;
eaa57a10 3610
9e7e5d5e
NC
3611 /* Fill the current segment with sections that fit. */
3612 for (j = 0; j < csecs; j++)
e5fc7809 3613 {
9e7e5d5e 3614 s = sections[j];
eaa57a10 3615
9e7e5d5e
NC
3616 if (s == NULL)
3617 continue;
eaa57a10 3618
9e7e5d5e 3619 os = s->output_section;
eaa57a10 3620
a6ac3b6b
MS
3621 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3622 || IS_COREFILE_NOTE (p, s))
9e7e5d5e
NC
3623 {
3624 if (m->count == 0)
3625 {
3626 /* If the first section in a segment does not start at
3627 the beginning of the segment, then something is wrong. */
3628 if (os->lma != m->p_paddr)
3629 abort ();
3630 }
eaa57a10 3631 else
9e7e5d5e
NC
3632 {
3633 asection * prev_sec;
3634 bfd_vma maxpagesize;
eaa57a10 3635
9e7e5d5e
NC
3636 prev_sec = m->sections[m->count - 1];
3637 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3638
3639 /* If the gap between the end of the previous section
3640 and the start of this section is more than maxpagesize
3641 then we need to start a new segment. */
3642 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3643 < BFD_ALIGN (os->lma, maxpagesize))
3644 {
3645 if (suggested_lma == 0)
3646 suggested_lma = os->lma;
eaa57a10 3647
9e7e5d5e
NC
3648 continue;
3649 }
3650 }
3651
3652 m->sections[m->count++] = os;
3653 ++isec;
3654 sections[j] = NULL;
3655 }
3656 else if (suggested_lma == 0)
3657 suggested_lma = os->lma;
e5fc7809 3658 }
9e7e5d5e
NC
3659
3660 BFD_ASSERT (m->count > 0);
eaa57a10 3661
9e7e5d5e
NC
3662 /* Add the current segment to the list of built segments. */
3663 *pm = m;
3664 pm = &m->next;
3665
3666 if (isec < csecs)
e5fc7809 3667 {
9e7e5d5e
NC
3668 /* We still have not allocated all of the sections to
3669 segments. Create a new segment here, initialise it
3670 and carry on looping. */
3671
3672 m = ((struct elf_segment_map *)
3673 bfd_alloc (obfd,
3674 (sizeof (struct elf_segment_map)
3675 + ((size_t) csecs - 1) * sizeof (asection *))));
3676 if (m == NULL)
3677 return false;
3678
3679 /* Initialise the fields of the segment map. Set the physical
3680 physical address to the LMA of the first section that has
3681 not yet been assigned. */
eaa57a10 3682
9e7e5d5e
NC
3683 m->next = NULL;
3684 m->p_type = p->p_type;
3685 m->p_flags = p->p_flags;
3686 m->p_flags_valid = 1;
3687 m->p_paddr = suggested_lma;
3688 m->p_paddr_valid = 1;
3689 m->includes_filehdr = 0;
3690 m->includes_phdrs = 0;
3dbf33ee 3691 }
3dbf33ee 3692 }
9e7e5d5e 3693 while (isec < csecs);
3dbf33ee 3694
9e7e5d5e 3695 free (sections);
3dbf33ee
ILT
3696 }
3697
53d3ce37
TT
3698 /* The Solaris linker creates program headers in which all the
3699 p_paddr fields are zero. When we try to objcopy or strip such a
3700 file, we get confused. Check for this case, and if we find it
3701 reset the p_paddr_valid fields. */
3702 for (m = mfirst; m != NULL; m = m->next)
3703 if (m->p_paddr != 0)
3704 break;
3705 if (m == NULL)
3706 {
3707 for (m = mfirst; m != NULL; m = m->next)
3708 m->p_paddr_valid = 0;
3709 }
3710
3dbf33ee
ILT
3711 elf_tdata (obfd)->segment_map = mfirst;
3712
9e7e5d5e
NC
3713#if 0
3714 /* Final Step: Sort the segments into ascending order of physical address. */
3715 if (mfirst != NULL)
3716 {
3717 struct elf_segment_map* prev;
eaa57a10 3718
9e7e5d5e
NC
3719 prev = mfirst;
3720 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3721 {
3722 /* Yes I know - its a bubble sort....*/
3723 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3724 {
3725 /* swap m and m->next */
3726 prev->next = m->next;
3727 m->next = m->next->next;
3728 prev->next->next = m;
3729
3730 /* restart loop. */
3731 m = mfirst;
3732 }
3733 }
3734 }
3735#endif
eaa57a10 3736
9e7e5d5e
NC
3737#undef IS_CONTAINED_BY
3738#undef IS_SOLARIS_PT_INTERP
b64beb2d 3739#undef IS_COREFILE_NOTE
3dbf33ee
ILT
3740 return true;
3741}
3742
fd0198f0
ILT
3743/* Copy private section information. This copies over the entsize
3744 field, and sometimes the info field. */
3745
3746boolean
3747_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3748 bfd *ibfd;
3749 asection *isec;
3750 bfd *obfd;
3751 asection *osec;
3752{
3753 Elf_Internal_Shdr *ihdr, *ohdr;
3754
3755 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3756 || obfd->xvec->flavour != bfd_target_elf_flavour)
3757 return true;
3758
3dbf33ee
ILT
3759 /* Copy over private BFD data if it has not already been copied.
3760 This must be done here, rather than in the copy_private_bfd_data
3761 entry point, because the latter is called after the section
3762 contents have been set, which means that the program headers have
3763 already been worked out. */
3764 if (elf_tdata (obfd)->segment_map == NULL
3765 && elf_tdata (ibfd)->phdr != NULL)
3766 {
3767 asection *s;
3768
b356d4af
RH
3769 /* Only set up the segments if there are no more SEC_ALLOC
3770 sections. FIXME: This won't do the right thing if objcopy is
3771 used to remove the last SEC_ALLOC section, since objcopy
3772 won't call this routine in that case. */
3773 for (s = isec->next; s != NULL; s = s->next)
3774 if ((s->flags & SEC_ALLOC) != 0)
3dbf33ee
ILT
3775 break;
3776 if (s == NULL)
3777 {
3778 if (! copy_private_bfd_data (ibfd, obfd))
3779 return false;
3780 }
3781 }
3782
fd0198f0
ILT
3783 ihdr = &elf_section_data (isec)->this_hdr;
3784 ohdr = &elf_section_data (osec)->this_hdr;
3785
3786 ohdr->sh_entsize = ihdr->sh_entsize;
3787
3788 if (ihdr->sh_type == SHT_SYMTAB
d6bfcdb5
ILT
3789 || ihdr->sh_type == SHT_DYNSYM
3790 || ihdr->sh_type == SHT_GNU_verneed
3791 || ihdr->sh_type == SHT_GNU_verdef)
fd0198f0
ILT
3792 ohdr->sh_info = ihdr->sh_info;
3793
3794 return true;
3795}
3796
3797/* Copy private symbol information. If this symbol is in a section
3798 which we did not map into a BFD section, try to map the section
3799 index correctly. We use special macro definitions for the mapped
3800 section indices; these definitions are interpreted by the
3801 swap_out_syms function. */
3802
3803#define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3804#define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3805#define MAP_STRTAB (SHN_LORESERVE - 3)
3806#define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3807
3808boolean
3809_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3810 bfd *ibfd;
3811 asymbol *isymarg;
3812 bfd *obfd;
3813 asymbol *osymarg;
3814{
3815 elf_symbol_type *isym, *osym;
3816
efc2b064
JL
3817 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3818 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3819 return true;
3820
fd0198f0
ILT
3821 isym = elf_symbol_from (ibfd, isymarg);
3822 osym = elf_symbol_from (obfd, osymarg);
3823
3824 if (isym != NULL
3825 && osym != NULL
3826 && bfd_is_abs_section (isym->symbol.section))
3827 {
3828 unsigned int shndx;
3829
3830 shndx = isym->internal_elf_sym.st_shndx;
3831 if (shndx == elf_onesymtab (ibfd))
3832 shndx = MAP_ONESYMTAB;
3833 else if (shndx == elf_dynsymtab (ibfd))
3834 shndx = MAP_DYNSYMTAB;
3835 else if (shndx == elf_tdata (ibfd)->strtab_section)
3836 shndx = MAP_STRTAB;
3837 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3838 shndx = MAP_SHSTRTAB;
3839 osym->internal_elf_sym.st_shndx = shndx;
3840 }
3841
3842 return true;
3843}
3844
3845/* Swap out the symbols. */
3846
ede4eed4 3847static boolean
37fcafe6 3848swap_out_syms (abfd, sttp, relocatable_p)
ede4eed4
KR
3849 bfd *abfd;
3850 struct bfd_strtab_hash **sttp;
37fcafe6 3851 int relocatable_p;
ede4eed4
KR
3852{
3853 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3854
3855 if (!elf_map_symbols (abfd))
3856 return false;
3857
3858 /* Dump out the symtabs. */
3859 {
3860 int symcount = bfd_get_symcount (abfd);
3861 asymbol **syms = bfd_get_outsymbols (abfd);
3862 struct bfd_strtab_hash *stt;
3863 Elf_Internal_Shdr *symtab_hdr;
3864 Elf_Internal_Shdr *symstrtab_hdr;
3865 char *outbound_syms;
3866 int idx;
3867
3868 stt = _bfd_elf_stringtab_init ();
3869 if (stt == NULL)
3870 return false;
3871
3872 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3873 symtab_hdr->sh_type = SHT_SYMTAB;
3874 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
3875 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
3876 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
3877 symtab_hdr->sh_addralign = bed->s->file_align;
3878
3879 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3880 symstrtab_hdr->sh_type = SHT_STRTAB;
3881
3882 outbound_syms = bfd_alloc (abfd,
3883 (1 + symcount) * bed->s->sizeof_sym);
3884 if (outbound_syms == NULL)
a9713b91 3885 return false;
ede4eed4
KR
3886 symtab_hdr->contents = (PTR) outbound_syms;
3887
3888 /* now generate the data (for "contents") */
3889 {
3890 /* Fill in zeroth symbol and swap it out. */
3891 Elf_Internal_Sym sym;
3892 sym.st_name = 0;
3893 sym.st_value = 0;
3894 sym.st_size = 0;
3895 sym.st_info = 0;
3896 sym.st_other = 0;
3897 sym.st_shndx = SHN_UNDEF;
cf9fb9f2 3898 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
ede4eed4
KR
3899 outbound_syms += bed->s->sizeof_sym;
3900 }
3901 for (idx = 0; idx < symcount; idx++)
3902 {
3903 Elf_Internal_Sym sym;
3904 bfd_vma value = syms[idx]->value;
3905 elf_symbol_type *type_ptr;
3906 flagword flags = syms[idx]->flags;
052b35d2 3907 int type;
ede4eed4
KR
3908
3909 if (flags & BSF_SECTION_SYM)
3910 /* Section symbols have no names. */
3911 sym.st_name = 0;
3912 else
3913 {
3914 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3915 syms[idx]->name,
3916 true, false);
3917 if (sym.st_name == (unsigned long) -1)
3918 return false;
3919 }
3920
3921 type_ptr = elf_symbol_from (abfd, syms[idx]);
3922
eaa57a10
UD
3923 if ((flags & BSF_SECTION_SYM) == 0
3924 && bfd_is_com_section (syms[idx]->section))
ede4eed4
KR
3925 {
3926 /* ELF common symbols put the alignment into the `value' field,
3927 and the size into the `size' field. This is backwards from
3928 how BFD handles it, so reverse it here. */
3929 sym.st_size = value;
3930 if (type_ptr == NULL
3931 || type_ptr->internal_elf_sym.st_value == 0)
3932 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3933 else
3934 sym.st_value = type_ptr->internal_elf_sym.st_value;
9e7e5d5e
NC
3935 sym.st_shndx = _bfd_elf_section_from_bfd_section
3936 (abfd, syms[idx]->section);
ede4eed4
KR
3937 }
3938 else
3939 {
3940 asection *sec = syms[idx]->section;
3941 int shndx;
3942
3943 if (sec->output_section)
3944 {
3945 value += sec->output_offset;
3946 sec = sec->output_section;
3947 }
37fcafe6
DE
3948 /* Don't add in the section vma for relocatable output. */
3949 if (! relocatable_p)
3950 value += sec->vma;
ede4eed4
KR
3951 sym.st_value = value;
3952 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
fd0198f0
ILT
3953
3954 if (bfd_is_abs_section (sec)
3955 && type_ptr != NULL
3956 && type_ptr->internal_elf_sym.st_shndx != 0)
ede4eed4 3957 {
fd0198f0
ILT
3958 /* This symbol is in a real ELF section which we did
3959 not create as a BFD section. Undo the mapping done
3960 by copy_private_symbol_data. */
3961 shndx = type_ptr->internal_elf_sym.st_shndx;
3962 switch (shndx)
3963 {
3964 case MAP_ONESYMTAB:
3965 shndx = elf_onesymtab (abfd);
3966 break;
3967 case MAP_DYNSYMTAB:
3968 shndx = elf_dynsymtab (abfd);
3969 break;
3970 case MAP_STRTAB:
3971 shndx = elf_tdata (abfd)->strtab_section;
3972 break;
3973 case MAP_SHSTRTAB:
3974 shndx = elf_tdata (abfd)->shstrtab_section;
3975 break;
3976 default:
3977 break;
3978 }
3979 }
3980 else
3981 {
3982 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3983
3984 if (shndx == -1)
3985 {
3986 asection *sec2;
3987
3988 /* Writing this would be a hell of a lot easier if
3989 we had some decent documentation on bfd, and
3990 knew what to expect of the library, and what to
3991 demand of applications. For example, it
3992 appears that `objcopy' might not set the
3993 section of a symbol to be a section that is
3994 actually in the output file. */
3995 sec2 = bfd_get_section_by_name (abfd, sec->name);
3996 BFD_ASSERT (sec2 != 0);
3997 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3998 BFD_ASSERT (shndx != -1);
3999 }
ede4eed4 4000 }
fd0198f0
ILT
4001
4002 sym.st_shndx = shndx;
ede4eed4
KR
4003 }
4004
052b35d2
ILT
4005 if ((flags & BSF_FUNCTION) != 0)
4006 type = STT_FUNC;
4007 else if ((flags & BSF_OBJECT) != 0)
4008 type = STT_OBJECT;
4009 else
4010 type = STT_NOTYPE;
4011
bb3b4377
CM
4012 /* Processor-specific types */
4013 if (bed->elf_backend_get_symbol_type)
eaa57a10 4014 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
bb3b4377 4015
eaa57a10
UD
4016 if (flags & BSF_SECTION_SYM)
4017 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4018 else if (bfd_is_com_section (syms[idx]->section))
052b35d2 4019 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
ede4eed4
KR
4020 else if (bfd_is_und_section (syms[idx]->section))
4021 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4022 ? STB_WEAK
4023 : STB_GLOBAL),
052b35d2 4024 type);
ede4eed4
KR
4025 else if (flags & BSF_FILE)
4026 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4027 else
4028 {
4029 int bind = STB_LOCAL;
ede4eed4
KR
4030
4031 if (flags & BSF_LOCAL)
4032 bind = STB_LOCAL;
4033 else if (flags & BSF_WEAK)
4034 bind = STB_WEAK;
4035 else if (flags & BSF_GLOBAL)
4036 bind = STB_GLOBAL;
4037
ede4eed4
KR
4038 sym.st_info = ELF_ST_INFO (bind, type);
4039 }
4040
80be821d
ILT
4041 if (type_ptr != NULL)
4042 sym.st_other = type_ptr->internal_elf_sym.st_other;
4043 else
4044 sym.st_other = 0;
4045
cf9fb9f2 4046 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
ede4eed4
KR
4047 outbound_syms += bed->s->sizeof_sym;
4048 }
4049
4050 *sttp = stt;
4051 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4052 symstrtab_hdr->sh_type = SHT_STRTAB;
4053
4054 symstrtab_hdr->sh_flags = 0;
4055 symstrtab_hdr->sh_addr = 0;
4056 symstrtab_hdr->sh_entsize = 0;
4057 symstrtab_hdr->sh_link = 0;
4058 symstrtab_hdr->sh_info = 0;
4059 symstrtab_hdr->sh_addralign = 1;
4060 }
4061
4062 return true;
4063}
4064
4065/* Return the number of bytes required to hold the symtab vector.
4066
4067 Note that we base it on the count plus 1, since we will null terminate
4068 the vector allocated based on this size. However, the ELF symbol table
4069 always has a dummy entry as symbol #0, so it ends up even. */
4070
4071long
4072_bfd_elf_get_symtab_upper_bound (abfd)
4073 bfd *abfd;
4074{
4075 long symcount;
4076 long symtab_size;
4077 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4078
4079 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4080 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4081
4082 return symtab_size;
4083}
4084
4085long
4086_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4087 bfd *abfd;
4088{
4089 long symcount;
4090 long symtab_size;
4091 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4092
4093 if (elf_dynsymtab (abfd) == 0)
4094 {
4095 bfd_set_error (bfd_error_invalid_operation);
4096 return -1;
4097 }
4098
4099 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4100 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4101
4102 return symtab_size;
4103}
4104
4105long
4106_bfd_elf_get_reloc_upper_bound (abfd, asect)
4107 bfd *abfd;
4108 sec_ptr asect;
4109{
4110 return (asect->reloc_count + 1) * sizeof (arelent *);
4111}
4112
4113/* Canonicalize the relocs. */
4114
4115long
4116_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4117 bfd *abfd;
4118 sec_ptr section;
4119 arelent **relptr;
4120 asymbol **symbols;
4121{
4122 arelent *tblptr;
4123 unsigned int i;
4124
e35765a9
ILT
4125 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4126 section,
4127 symbols,
4128 false))
ede4eed4
KR
4129 return -1;
4130
4131 tblptr = section->relocation;
4132 for (i = 0; i < section->reloc_count; i++)
4133 *relptr++ = tblptr++;
4134
4135 *relptr = NULL;
4136
4137 return section->reloc_count;
4138}
4139
4140long
4141_bfd_elf_get_symtab (abfd, alocation)
4142 bfd *abfd;
4143 asymbol **alocation;
4144{
9e7e5d5e
NC
4145 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4146 (abfd, alocation, false);
ede4eed4
KR
4147
4148 if (symcount >= 0)
4149 bfd_get_symcount (abfd) = symcount;
4150 return symcount;
4151}
4152
4153long
4154_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4155 bfd *abfd;
4156 asymbol **alocation;
4157{
9e7e5d5e
NC
4158 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4159 (abfd, alocation, true);
ede4eed4
KR
4160}
4161
e35765a9
ILT
4162/* Return the size required for the dynamic reloc entries. Any
4163 section that was actually installed in the BFD, and has type
4164 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4165 considered to be a dynamic reloc section. */
4166
4167long
4168_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4169 bfd *abfd;
4170{
4171 long ret;
4172 asection *s;
4173
4174 if (elf_dynsymtab (abfd) == 0)
4175 {
4176 bfd_set_error (bfd_error_invalid_operation);
4177 return -1;
4178 }
4179
4180 ret = sizeof (arelent *);
4181 for (s = abfd->sections; s != NULL; s = s->next)
4182 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4183 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4184 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4185 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4186 * sizeof (arelent *));
4187
4188 return ret;
4189}
4190
4191/* Canonicalize the dynamic relocation entries. Note that we return
4192 the dynamic relocations as a single block, although they are
4193 actually associated with particular sections; the interface, which
4194 was designed for SunOS style shared libraries, expects that there
4195 is only one set of dynamic relocs. Any section that was actually
4196 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4197 the dynamic symbol table, is considered to be a dynamic reloc
4198 section. */
4199
4200long
4201_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4202 bfd *abfd;
4203 arelent **storage;
4204 asymbol **syms;
4205{
4206 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4207 asection *s;
4208 long ret;
4209
4210 if (elf_dynsymtab (abfd) == 0)
4211 {
4212 bfd_set_error (bfd_error_invalid_operation);
4213 return -1;
4214 }
4215
4216 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4217 ret = 0;
4218 for (s = abfd->sections; s != NULL; s = s->next)
4219 {
4220 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4221 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4222 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4223 {
4224 arelent *p;
4225 long count, i;
4226
4227 if (! (*slurp_relocs) (abfd, s, syms, true))
4228 return -1;
4229 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4230 p = s->relocation;
4231 for (i = 0; i < count; i++)
4232 *storage++ = p++;
4233 ret += count;
4234 }
4235 }
4236
4237 *storage = NULL;
4238
4239 return ret;
4240}
a66a61a0
ILT
4241\f
4242/* Read in the version information. */
4243
4244boolean
4245_bfd_elf_slurp_version_tables (abfd)
4246 bfd *abfd;
4247{
4248 bfd_byte *contents = NULL;
4249
4250 if (elf_dynverdef (abfd) != 0)
4251 {
4252 Elf_Internal_Shdr *hdr;
4253 Elf_External_Verdef *everdef;
4254 Elf_Internal_Verdef *iverdef;
4255 unsigned int i;
4256
4257 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4258
4259 elf_tdata (abfd)->verdef =
4260 ((Elf_Internal_Verdef *)
4261 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4262 if (elf_tdata (abfd)->verdef == NULL)
4263 goto error_return;
4264
4265 elf_tdata (abfd)->cverdefs = hdr->sh_info;
e35765a9 4266
a66a61a0
ILT
4267 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4268 if (contents == NULL)
4269 goto error_return;
4270 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4271 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4272 goto error_return;
4273
4274 everdef = (Elf_External_Verdef *) contents;
4275 iverdef = elf_tdata (abfd)->verdef;
4276 for (i = 0; i < hdr->sh_info; i++, iverdef++)
4277 {
4278 Elf_External_Verdaux *everdaux;
4279 Elf_Internal_Verdaux *iverdaux;
4280 unsigned int j;
4281
4282 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4283
4284 iverdef->vd_bfd = abfd;
4285
4286 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4287 bfd_alloc (abfd,
4288 (iverdef->vd_cnt
4289 * sizeof (Elf_Internal_Verdaux))));
4290 if (iverdef->vd_auxptr == NULL)
4291 goto error_return;
4292
4293 everdaux = ((Elf_External_Verdaux *)
4294 ((bfd_byte *) everdef + iverdef->vd_aux));
4295 iverdaux = iverdef->vd_auxptr;
4296 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4297 {
4298 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4299
4300 iverdaux->vda_nodename =
4301 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4302 iverdaux->vda_name);
4303 if (iverdaux->vda_nodename == NULL)
4304 goto error_return;
4305
4306 if (j + 1 < iverdef->vd_cnt)
4307 iverdaux->vda_nextptr = iverdaux + 1;
4308 else
4309 iverdaux->vda_nextptr = NULL;
4310
4311 everdaux = ((Elf_External_Verdaux *)
4312 ((bfd_byte *) everdaux + iverdaux->vda_next));
4313 }
4314
4315 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4316
4317 if (i + 1 < hdr->sh_info)
4318 iverdef->vd_nextdef = iverdef + 1;
4319 else
4320 iverdef->vd_nextdef = NULL;
4321
4322 everdef = ((Elf_External_Verdef *)
4323 ((bfd_byte *) everdef + iverdef->vd_next));
4324 }
4325
4326 free (contents);
4327 contents = NULL;
4328 }
4329
4330 if (elf_dynverref (abfd) != 0)
4331 {
4332 Elf_Internal_Shdr *hdr;
4333 Elf_External_Verneed *everneed;
4334 Elf_Internal_Verneed *iverneed;
4335 unsigned int i;
4336
4337 hdr = &elf_tdata (abfd)->dynverref_hdr;
4338
4339 elf_tdata (abfd)->verref =
4340 ((Elf_Internal_Verneed *)
4341 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4342 if (elf_tdata (abfd)->verref == NULL)
4343 goto error_return;
4344
4345 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4346
4347 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4348 if (contents == NULL)
4349 goto error_return;
4350 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4351 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4352 goto error_return;
4353
4354 everneed = (Elf_External_Verneed *) contents;
4355 iverneed = elf_tdata (abfd)->verref;
4356 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4357 {
4358 Elf_External_Vernaux *evernaux;
4359 Elf_Internal_Vernaux *ivernaux;
4360 unsigned int j;
4361
4362 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4363
4364 iverneed->vn_bfd = abfd;
4365
4366 iverneed->vn_filename =
4367 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4368 iverneed->vn_file);
4369 if (iverneed->vn_filename == NULL)
4370 goto error_return;
4371
4372 iverneed->vn_auxptr =
4373 ((Elf_Internal_Vernaux *)
4374 bfd_alloc (abfd,
4375 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4376
4377 evernaux = ((Elf_External_Vernaux *)
4378 ((bfd_byte *) everneed + iverneed->vn_aux));
4379 ivernaux = iverneed->vn_auxptr;
4380 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4381 {
4382 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4383
4384 ivernaux->vna_nodename =
4385 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4386 ivernaux->vna_name);
4387 if (ivernaux->vna_nodename == NULL)
4388 goto error_return;
4389
4390 if (j + 1 < iverneed->vn_cnt)
4391 ivernaux->vna_nextptr = ivernaux + 1;
4392 else
4393 ivernaux->vna_nextptr = NULL;
4394
4395 evernaux = ((Elf_External_Vernaux *)
4396 ((bfd_byte *) evernaux + ivernaux->vna_next));
4397 }
4398
4399 if (i + 1 < hdr->sh_info)
4400 iverneed->vn_nextref = iverneed + 1;
4401 else
4402 iverneed->vn_nextref = NULL;
4403
4404 everneed = ((Elf_External_Verneed *)
4405 ((bfd_byte *) everneed + iverneed->vn_next));
4406 }
4407
4408 free (contents);
4409 contents = NULL;
4410 }
4411
4412 return true;
4413
4414 error_return:
4415 if (contents == NULL)
4416 free (contents);
4417 return false;
4418}
4419\f
ede4eed4
KR
4420asymbol *
4421_bfd_elf_make_empty_symbol (abfd)
4422 bfd *abfd;
4423{
4424 elf_symbol_type *newsym;
4425
4426 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4427 if (!newsym)
a9713b91 4428 return NULL;
ede4eed4
KR
4429 else
4430 {
4431 newsym->symbol.the_bfd = abfd;
4432 return &newsym->symbol;
4433 }
4434}
4435
4436void
4437_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4438 bfd *ignore_abfd;
4439 asymbol *symbol;
4440 symbol_info *ret;
4441{
4442 bfd_symbol_info (symbol, ret);
4443}
4444
d6bfcdb5
ILT
4445/* Return whether a symbol name implies a local symbol. Most targets
4446 use this function for the is_local_label_name entry point, but some
4447 override it. */
a66a61a0
ILT
4448
4449boolean
4450_bfd_elf_is_local_label_name (abfd, name)
4451 bfd *abfd;
4452 const char *name;
4453{
d6bfcdb5
ILT
4454 /* Normal local symbols start with ``.L''. */
4455 if (name[0] == '.' && name[1] == 'L')
4456 return true;
4457
4458 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4459 DWARF debugging symbols starting with ``..''. */
4460 if (name[0] == '.' && name[1] == '.')
4461 return true;
4462
4463 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4464 emitting DWARF debugging output. I suspect this is actually a
4465 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4466 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4467 underscore to be emitted on some ELF targets). For ease of use,
4468 we treat such symbols as local. */
4469 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4470 return true;
4471
4472 return false;
a66a61a0
ILT
4473}
4474
ede4eed4
KR
4475alent *
4476_bfd_elf_get_lineno (ignore_abfd, symbol)
4477 bfd *ignore_abfd;
4478 asymbol *symbol;
4479{
8cd2f4fe 4480 abort ();
ede4eed4
KR
4481 return NULL;
4482}
4483
4484boolean
4485_bfd_elf_set_arch_mach (abfd, arch, machine)
4486 bfd *abfd;
4487 enum bfd_architecture arch;
4488 unsigned long machine;
4489{
4490 /* If this isn't the right architecture for this backend, and this
4491 isn't the generic backend, fail. */
4492 if (arch != get_elf_backend_data (abfd)->arch
4493 && arch != bfd_arch_unknown
4494 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4495 return false;
4496
4497 return bfd_default_set_arch_mach (abfd, arch, machine);
4498}
4499
6f904fce
ILT
4500/* Find the nearest line to a particular section and offset, for error
4501 reporting. */
4502
ede4eed4
KR
4503boolean
4504_bfd_elf_find_nearest_line (abfd,
6f904fce
ILT
4505 section,
4506 symbols,
4507 offset,
4508 filename_ptr,
4509 functionname_ptr,
4510 line_ptr)
ede4eed4
KR
4511 bfd *abfd;
4512 asection *section;
4513 asymbol **symbols;
4514 bfd_vma offset;
4515 CONST char **filename_ptr;
4516 CONST char **functionname_ptr;
4517 unsigned int *line_ptr;
4518{
86aac8ea 4519 boolean found;
6f904fce
ILT
4520 const char *filename;
4521 asymbol *func;
86aac8ea 4522 bfd_vma low_func;
6f904fce
ILT
4523 asymbol **p;
4524
eaa57a10 4525 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
eae43516
GRK
4526 filename_ptr, functionname_ptr,
4527 line_ptr))
4528 return true;
4529
eaa57a10
UD
4530 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4531 filename_ptr, functionname_ptr,
4532 line_ptr))
4533 return true;
4534
86aac8ea
ILT
4535 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4536 &found, filename_ptr,
4537 functionname_ptr, line_ptr,
4538 &elf_tdata (abfd)->line_info))
4539 return false;
4540 if (found)
4541 return true;
4542
6f904fce
ILT
4543 if (symbols == NULL)
4544 return false;
4545
4546 filename = NULL;
4547 func = NULL;
86aac8ea 4548 low_func = 0;
6f904fce
ILT
4549
4550 for (p = symbols; *p != NULL; p++)
4551 {
4552 elf_symbol_type *q;
4553
4554 q = (elf_symbol_type *) *p;
4555
4556 if (bfd_get_section (&q->symbol) != section)
4557 continue;
4558
4559 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4560 {
4561 default:
4562 break;
4563 case STT_FILE:
4564 filename = bfd_asymbol_name (&q->symbol);
4565 break;
b64beb2d 4566 case STT_NOTYPE:
6f904fce 4567 case STT_FUNC:
86aac8ea
ILT
4568 if (q->symbol.section == section
4569 && q->symbol.value >= low_func
4570 && q->symbol.value <= offset)
4571 {
4572 func = (asymbol *) q;
4573 low_func = q->symbol.value;
4574 }
6f904fce
ILT
4575 break;
4576 }
4577 }
4578
4579 if (func == NULL)
4580 return false;
4581
4582 *filename_ptr = filename;
4583 *functionname_ptr = bfd_asymbol_name (func);
4584 *line_ptr = 0;
4585 return true;
ede4eed4
KR
4586}
4587
4588int
4589_bfd_elf_sizeof_headers (abfd, reloc)
4590 bfd *abfd;
4591 boolean reloc;
4592{
4593 int ret;
4594
4595 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4596 if (! reloc)
fd0198f0 4597 ret += get_program_header_size (abfd);
ede4eed4
KR
4598 return ret;
4599}
4600
4601boolean
4602_bfd_elf_set_section_contents (abfd, section, location, offset, count)
4603 bfd *abfd;
4604 sec_ptr section;
4605 PTR location;
4606 file_ptr offset;
4607 bfd_size_type count;
4608{
4609 Elf_Internal_Shdr *hdr;
4610
4611 if (! abfd->output_has_begun
9e7e5d5e
NC
4612 && ! _bfd_elf_compute_section_file_positions
4613 (abfd, (struct bfd_link_info *) NULL))
ede4eed4
KR
4614 return false;
4615
4616 hdr = &elf_section_data (section)->this_hdr;
4617
4618 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4619 return false;
4620 if (bfd_write (location, 1, count, abfd) != count)
4621 return false;
4622
4623 return true;
4624}
4625
4626void
4627_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4628 bfd *abfd;
4629 arelent *cache_ptr;
4630 Elf_Internal_Rela *dst;
4631{
8cd2f4fe 4632 abort ();
ede4eed4
KR
4633}
4634
4635#if 0
4636void
4637_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4638 bfd *abfd;
4639 arelent *cache_ptr;
4640 Elf_Internal_Rel *dst;
4641{
8cd2f4fe 4642 abort ();
ede4eed4
KR
4643}
4644#endif
7fc6a16a
ILT
4645
4646/* Try to convert a non-ELF reloc into an ELF one. */
4647
4648boolean
4649_bfd_elf_validate_reloc (abfd, areloc)
4650 bfd *abfd;
4651 arelent *areloc;
4652{
4653 /* Check whether we really have an ELF howto. */
4654
eaa57a10 4655 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7fc6a16a
ILT
4656 {
4657 bfd_reloc_code_real_type code;
4658 reloc_howto_type *howto;
eaa57a10 4659
7fc6a16a
ILT
4660 /* Alien reloc: Try to determine its type to replace it with an
4661 equivalent ELF reloc. */
4662
4663 if (areloc->howto->pc_relative)
4664 {
4665 switch (areloc->howto->bitsize)
4666 {
4667 case 8:
eaa57a10 4668 code = BFD_RELOC_8_PCREL;
7fc6a16a
ILT
4669 break;
4670 case 12:
eaa57a10 4671 code = BFD_RELOC_12_PCREL;
7fc6a16a
ILT
4672 break;
4673 case 16:
eaa57a10 4674 code = BFD_RELOC_16_PCREL;
7fc6a16a
ILT
4675 break;
4676 case 24:
eaa57a10 4677 code = BFD_RELOC_24_PCREL;
7fc6a16a
ILT
4678 break;
4679 case 32:
eaa57a10 4680 code = BFD_RELOC_32_PCREL;
7fc6a16a
ILT
4681 break;
4682 case 64:
eaa57a10 4683 code = BFD_RELOC_64_PCREL;
7fc6a16a
ILT
4684 break;
4685 default:
4686 goto fail;
4687 }
4688
4689 howto = bfd_reloc_type_lookup (abfd, code);
4690
4691 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4692 {
4693 if (howto->pcrel_offset)
4694 areloc->addend += areloc->address;
4695 else
4696 areloc->addend -= areloc->address; /* addend is unsigned!! */
4697 }
4698 }
4699 else
4700 {
4701 switch (areloc->howto->bitsize)
4702 {
4703 case 8:
eaa57a10 4704 code = BFD_RELOC_8;
7fc6a16a
ILT
4705 break;
4706 case 14:
eaa57a10 4707 code = BFD_RELOC_14;
7fc6a16a
ILT
4708 break;
4709 case 16:
eaa57a10 4710 code = BFD_RELOC_16;
7fc6a16a
ILT
4711 break;
4712 case 26:
eaa57a10 4713 code = BFD_RELOC_26;
7fc6a16a
ILT
4714 break;
4715 case 32:
eaa57a10 4716 code = BFD_RELOC_32;
7fc6a16a
ILT
4717 break;
4718 case 64:
eaa57a10 4719 code = BFD_RELOC_64;
7fc6a16a
ILT
4720 break;
4721 default:
4722 goto fail;
4723 }
4724
4725 howto = bfd_reloc_type_lookup (abfd, code);
4726 }
4727
4728 if (howto)
4729 areloc->howto = howto;
4730 else
4731 goto fail;
4732 }
4733
4734 return true;
4735
4736 fail:
4737 (*_bfd_error_handler)
53d3ce37 4738 (_("%s: unsupported relocation type %s"),
7fc6a16a
ILT
4739 bfd_get_filename (abfd), areloc->howto->name);
4740 bfd_set_error (bfd_error_bad_value);
4741 return false;
4742}
16ce6205
RH
4743
4744boolean
4745_bfd_elf_close_and_cleanup (abfd)
4746 bfd *abfd;
4747{
e6e3d4bd
ILT
4748 if (bfd_get_format (abfd) == bfd_object)
4749 {
4750 if (elf_shstrtab (abfd) != NULL)
4751 _bfd_stringtab_free (elf_shstrtab (abfd));
4752 }
4753
16ce6205
RH
4754 return _bfd_generic_close_and_cleanup (abfd);
4755}
09e5322a
RH
4756
4757/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4758 in the relocation's offset. Thus we cannot allow any sort of sanity
4759 range-checking to interfere. There is nothing else to do in processing
4760 this reloc. */
4761
4762bfd_reloc_status_type
4763_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
4764 bfd *abfd;
4765 arelent *re;
4766 struct symbol_cache_entry *symbol;
4767 PTR data;
4768 asection *is;
4769 bfd *obfd;
4770 char **errmsg;
4771{
4772 return bfd_reloc_ok;
4773}
eaa57a10
UD
4774
4775\f
4776/* Elf core file support. Much of this only works on native
4777 toolchains, since we rely on knowing the
4778 machine-dependent procfs structure in order to pick
4779 out details about the corefile. */
4780
4781#ifdef HAVE_SYS_PROCFS_H
4782# include <sys/procfs.h>
4783#endif
4784
4785
4786/* Define offsetof for those systems which lack it. */
4787
4788#ifndef offsetof
4789# define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
4790#endif
4791
4792
4793/* FIXME: this is kinda wrong, but it's what gdb wants. */
4794
4795static int
4796elfcore_make_pid (abfd)
4797 bfd* abfd;
4798{
4799 return ((elf_tdata (abfd)->core_lwpid << 16)
4800 + (elf_tdata (abfd)->core_pid));
4801}
4802
4803
4804/* If there isn't a section called NAME, make one, using
4805 data from SECT. Note, this function will generate a
4806 reference to NAME, so you shouldn't deallocate or
4807 overwrite it. */
4808
4809static boolean
4810elfcore_maybe_make_sect (abfd, name, sect)
4811 bfd* abfd;
4812 char* name;
4813 asection* sect;
4814{
4815 asection* sect2;
4816
4817 if (bfd_get_section_by_name (abfd, name) != NULL)
4818 return true;
4819
4820 sect2 = bfd_make_section (abfd, name);
4821 if (sect2 == NULL)
4822 return false;
4823
4824 sect2->_raw_size = sect->_raw_size;
4825 sect2->filepos = sect->filepos;
4826 sect2->flags = sect->flags;
4827 sect2->alignment_power = sect->alignment_power;
4828 return true;
4829}
4830
4831
4832/* prstatus_t exists on:
4833 solaris 2.[567]
4834 linux 2.[01] + glibc
4835 unixware 4.2
4836*/
4837
4838#if defined (HAVE_PRSTATUS_T)
4839static boolean
4840elfcore_grok_prstatus (abfd, note)
4841 bfd* abfd;
4842 Elf_Internal_Note* note;
4843{
4844 prstatus_t prstat;
4845 char buf[100];
4846 char* name;
4847 asection* sect;
4848
4849 if (note->descsz != sizeof (prstat))
4850 return true;
4851
4852 memcpy (&prstat, note->descdata, sizeof (prstat));
4853
4854 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4855 elf_tdata (abfd)->core_pid = prstat.pr_pid;
4856
4857 /* pr_who exists on:
4858 solaris 2.[567]
4859 unixware 4.2
4860 pr_who doesn't exist on:
4861 linux 2.[01]
4862 */
4863#if defined (HAVE_PRSTATUS_T_PR_WHO)
4864 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
4865#endif
4866
4867 /* Make a ".reg/999" section. */
4868
4869 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
4870 name = bfd_alloc (abfd, strlen (buf) + 1);
4871 if (name == NULL)
4872 return false;
4873 strcpy (name, buf);
4874
4875 sect = bfd_make_section (abfd, name);
4876 if (sect == NULL)
4877 return false;
4878 sect->_raw_size = sizeof (prstat.pr_reg);
4879 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
4880 sect->flags = SEC_HAS_CONTENTS;
4881 sect->alignment_power = 2;
4882
4883 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
4884 return false;
4885
4886 return true;
4887}
4888#endif /* defined (HAVE_PRSTATUS_T) */
4889
4890
4891/* There isn't a consistent prfpregset_t across platforms,
4892 but it doesn't matter, because we don't have to pick this
4893 data structure apart. */
4894
4895static boolean
4896elfcore_grok_prfpreg (abfd, note)
4897 bfd* abfd;
4898 Elf_Internal_Note* note;
4899{
4900 char buf[100];
4901 char* name;
4902 asection* sect;
4903
4904 /* Make a ".reg2/999" section. */
4905
4906 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
4907 name = bfd_alloc (abfd, strlen (buf) + 1);
4908 if (name == NULL)
4909 return false;
4910 strcpy (name, buf);
4911
4912 sect = bfd_make_section (abfd, name);
4913 if (sect == NULL)
4914 return false;
4915 sect->_raw_size = note->descsz;
4916 sect->filepos = note->descpos;
4917 sect->flags = SEC_HAS_CONTENTS;
4918 sect->alignment_power = 2;
4919
4920 if (! elfcore_maybe_make_sect (abfd, ".reg2", sect))
4921 return false;
4922
4923 return true;
4924}
4925
4926
4927/* return a malloc'ed copy of a string at START which is at
4928 most MAX bytes long, possibly without a terminating '\0'.
4929 the copy will always have a terminating '\0'. */
4930
4931static char*
4932elfcore_strndup (abfd, start, max)
4933 bfd* abfd;
4934 char* start;
4935 int max;
4936{
4937 char* dup;
4938 char* end = memchr (start, '\0', max);
4939 int len;
4940
4941 if (end == NULL)
4942 len = max;
4943 else
4944 len = end - start;
4945
4946 dup = bfd_alloc (abfd, len + 1);
4947 if (dup == NULL)
4948 return NULL;
4949
4950 memcpy (dup, start, len);
4951 dup[len] = '\0';
4952
4953 return dup;
4954}
4955
4956
4957#if defined (HAVE_PRPSINFO_T)
4958# define elfcore_psinfo_t prpsinfo_t
4959#endif
4960
4961#if defined (HAVE_PSINFO_T)
4962# define elfcore_psinfo_t psinfo_t
4963#endif
4964
4965
4966#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
4967static boolean
4968elfcore_grok_psinfo (abfd, note)
4969 bfd* abfd;
4970 Elf_Internal_Note* note;
4971{
4972 elfcore_psinfo_t psinfo;
4973
4974 if (note->descsz != sizeof (elfcore_psinfo_t))
4975 return true;
4976
4977 memcpy (&psinfo, note->descdata, note->descsz);
4978
4979 elf_tdata (abfd)->core_program
4980 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
4981
4982 elf_tdata (abfd)->core_command
4983 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
4984
4985 /* Note that for some reason, a spurious space is tacked
4986 onto the end of the args in some (at least one anyway)
4987 implementations, so strip it off if it exists. */
4988
4989 {
4990 char* command = elf_tdata (abfd)->core_command;
4991 int n = strlen (command);
4992
4993 if (0 < n && command[n - 1] == ' ')
4994 command[n - 1] = '\0';
4995 }
4996
4997 return true;
4998}
4999#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5000
5001
5002#if defined (HAVE_PSTATUS_T)
5003static boolean
5004elfcore_grok_pstatus (abfd, note)
5005 bfd* abfd;
5006 Elf_Internal_Note* note;
5007{
5008 pstatus_t pstat;
5009
5010 if (note->descsz != sizeof (pstat))
5011 return true;
5012
5013 memcpy (&pstat, note->descdata, sizeof (pstat));
5014
5015 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5016
5017 /* Could grab some more details from the "representative"
5018 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5019 NT_LWPSTATUS note, presumably. */
5020
5021 return true;
5022}
5023#endif /* defined (HAVE_PSTATUS_T) */
5024
5025
5026#if defined (HAVE_LWPSTATUS_T)
5027static boolean
5028elfcore_grok_lwpstatus (abfd, note)
5029 bfd* abfd;
5030 Elf_Internal_Note* note;
5031{
5032 lwpstatus_t lwpstat;
5033 char buf[100];
5034 char* name;
5035 asection* sect;
5036
5037 if (note->descsz != sizeof (lwpstat))
5038 return true;
5039
5040 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5041
5042 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5043 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5044
5045 /* Make a ".reg/999" section. */
5046
5047 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5048 name = bfd_alloc (abfd, strlen (buf) + 1);
5049 if (name == NULL)
5050 return false;
5051 strcpy (name, buf);
5052
5053 sect = bfd_make_section (abfd, name);
5054 if (sect == NULL)
5055 return false;
5056
5057#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5058 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5059 sect->filepos = note->descpos
5060 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5061#endif
5062
5063#if defined (HAVE_LWPSTATUS_T_PR_REG)
5064 sect->_raw_size = sizeof (lwpstat.pr_reg);
5065 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5066#endif
5067
5068 sect->flags = SEC_HAS_CONTENTS;
5069 sect->alignment_power = 2;
5070
5071 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5072 return false;
5073
5074 /* Make a ".reg2/999" section */
5075
5076 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5077 name = bfd_alloc (abfd, strlen (buf) + 1);
5078 if (name == NULL)
5079 return false;
5080 strcpy (name, buf);
5081
5082 sect = bfd_make_section (abfd, name);
5083 if (sect == NULL)
5084 return false;
5085
5086#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5087 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5088 sect->filepos = note->descpos
5089 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5090#endif
5091
5092#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5093 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5094 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5095#endif
5096
5097 sect->flags = SEC_HAS_CONTENTS;
5098 sect->alignment_power = 2;
5099
5100 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5101 return false;
5102
5103 return true;
5104}
5105#endif /* defined (HAVE_LWPSTATUS_T) */
5106
5107
5108
5109static boolean
5110elfcore_grok_note (abfd, note)
5111 bfd* abfd;
5112 Elf_Internal_Note* note;
5113{
5114 switch (note->type)
5115 {
5116 default:
5117 return true;
5118
5119#if defined (HAVE_PRSTATUS_T)
5120 case NT_PRSTATUS:
5121 return elfcore_grok_prstatus (abfd, note);
5122#endif
5123
5124#if defined (HAVE_PSTATUS_T)
5125 case NT_PSTATUS:
5126 return elfcore_grok_pstatus (abfd, note);
5127#endif
5128
5129#if defined (HAVE_LWPSTATUS_T)
5130 case NT_LWPSTATUS:
5131 return elfcore_grok_lwpstatus (abfd, note);
5132#endif
5133
5134 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5135 return elfcore_grok_prfpreg (abfd, note);
5136
5137#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5138 case NT_PRPSINFO:
5139 case NT_PSINFO:
5140 return elfcore_grok_psinfo (abfd, note);
5141#endif
5142 }
5143}
5144
5145
5146static boolean
5147elfcore_read_notes (abfd, offset, size)
5148 bfd* abfd;
5149 bfd_vma offset;
5150 bfd_vma size;
5151{
5152 char* buf;
5153 char* p;
5154
5155 if (size <= 0)
5156 return true;
5157
5158 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5159 return false;
5160
5161 buf = bfd_malloc ((size_t) size);
5162 if (buf == NULL)
5163 return false;
5164
5165 if (bfd_read (buf, size, 1, abfd) != size)
5166 {
5167 error:
5168 free (buf);
5169 return false;
5170 }
5171
5172 p = buf;
5173 while (p < buf + size)
5174 {
5175 /* FIXME: bad alignment assumption. */
5176 Elf_External_Note* xnp = (Elf_External_Note*) p;
5177 Elf_Internal_Note in;
5178
5179 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5180
5181 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5182 in.namedata = xnp->name;
5183
5184 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5185 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5186 in.descpos = offset + (in.descdata - buf);
5187
5188 if (! elfcore_grok_note (abfd, &in))
5189 goto error;
5190
5191 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5192 }
5193
5194 free (buf);
5195 return true;
5196}
5197
5198
5199
5200boolean
5201_bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5202 bfd* abfd;
5203 Elf_Internal_Phdr* phdr;
5204 int sec_num;
5205{
5206 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5207 return false;
5208
5209 if (phdr->p_type == PT_NOTE
5210 && ! elfcore_read_notes (abfd, phdr->p_offset, phdr->p_filesz))
5211 return false;
5212
5213 return true;
5214}
5215