* xcofflink.c (_bfd_xcoff_bfd_final_link): Always allocate space
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
32090b8e 1/* ELF executable support for BFD.
fd8d7c31 2 Copyright 1993, 1994, 1995, 1996, 1997 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
KR
47static boolean prep_headers PARAMS ((bfd *));
48static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **));
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
KR
201/* Standard ELF hash function. Do not change this function; you will
202 cause invalid hash tables to be generated. (Well, you would if this
203 were being used yet.) */
204unsigned long
013dec1a
ILT
205bfd_elf_hash (name)
206 CONST unsigned char *name;
32090b8e
KR
207{
208 unsigned long h = 0;
209 unsigned long g;
210 int ch;
211
212 while ((ch = *name++) != '\0')
213 {
214 h = (h << 4) + ch;
215 if ((g = (h & 0xf0000000)) != 0)
216 {
217 h ^= g >> 24;
218 h &= ~g;
219 }
220 }
221 return h;
222}
223
224/* Read a specified number of bytes at a specified offset in an ELF
225 file, into a newly allocated buffer, and return a pointer to the
226 buffer. */
227
228static char *
013dec1a
ILT
229elf_read (abfd, offset, size)
230 bfd * abfd;
231 long offset;
ae115e51 232 unsigned int size;
32090b8e
KR
233{
234 char *buf;
235
236 if ((buf = bfd_alloc (abfd, size)) == NULL)
a9713b91 237 return NULL;
32090b8e 238 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
013dec1a 239 return NULL;
32090b8e
KR
240 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
241 {
013dec1a
ILT
242 if (bfd_get_error () != bfd_error_system_call)
243 bfd_set_error (bfd_error_file_truncated);
32090b8e
KR
244 return NULL;
245 }
246 return buf;
247}
248
249boolean
ff12f303 250bfd_elf_mkobject (abfd)
013dec1a 251 bfd * abfd;
32090b8e
KR
252{
253 /* this just does initialization */
254 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
255 elf_tdata (abfd) = (struct elf_obj_tdata *)
256 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
257 if (elf_tdata (abfd) == 0)
a9713b91 258 return false;
32090b8e
KR
259 /* since everything is done at close time, do we need any
260 initialization? */
261
262 return true;
263}
264
265char *
ede4eed4 266bfd_elf_get_str_section (abfd, shindex)
013dec1a
ILT
267 bfd * abfd;
268 unsigned int shindex;
32090b8e
KR
269{
270 Elf_Internal_Shdr **i_shdrp;
271 char *shstrtab = NULL;
272 unsigned int offset;
273 unsigned int shstrtabsize;
274
275 i_shdrp = elf_elfsections (abfd);
276 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
277 return 0;
278
b176e1e9 279 shstrtab = (char *) i_shdrp[shindex]->contents;
32090b8e
KR
280 if (shstrtab == NULL)
281 {
282 /* No cached one, attempt to read, and cache what we read. */
283 offset = i_shdrp[shindex]->sh_offset;
284 shstrtabsize = i_shdrp[shindex]->sh_size;
285 shstrtab = elf_read (abfd, offset, shstrtabsize);
b176e1e9 286 i_shdrp[shindex]->contents = (PTR) shstrtab;
32090b8e
KR
287 }
288 return shstrtab;
289}
290
291char *
ede4eed4 292bfd_elf_string_from_elf_section (abfd, shindex, strindex)
013dec1a
ILT
293 bfd * abfd;
294 unsigned int shindex;
295 unsigned int strindex;
32090b8e
KR
296{
297 Elf_Internal_Shdr *hdr;
298
299 if (strindex == 0)
300 return "";
301
302 hdr = elf_elfsections (abfd)[shindex];
303
b176e1e9 304 if (hdr->contents == NULL
ede4eed4 305 && bfd_elf_get_str_section (abfd, shindex) == NULL)
32090b8e
KR
306 return NULL;
307
b176e1e9 308 return ((char *) hdr->contents) + strindex;
32090b8e
KR
309}
310
497c5434 311/* Make a BFD section from an ELF section. We store a pointer to the
b176e1e9 312 BFD section in the bfd_section field of the header. */
497c5434
ILT
313
314boolean
315_bfd_elf_make_section_from_shdr (abfd, hdr, name)
316 bfd *abfd;
317 Elf_Internal_Shdr *hdr;
318 const char *name;
319{
320 asection *newsect;
321 flagword flags;
322
b176e1e9 323 if (hdr->bfd_section != NULL)
497c5434 324 {
b176e1e9
ILT
325 BFD_ASSERT (strcmp (name,
326 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
497c5434
ILT
327 return true;
328 }
329
330 newsect = bfd_make_section_anyway (abfd, name);
331 if (newsect == NULL)
332 return false;
333
334 newsect->filepos = hdr->sh_offset;
335
336 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
337 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
338 || ! bfd_set_section_alignment (abfd, newsect,
339 bfd_log2 (hdr->sh_addralign)))
340 return false;
341
342 flags = SEC_NO_FLAGS;
343 if (hdr->sh_type != SHT_NOBITS)
344 flags |= SEC_HAS_CONTENTS;
345 if ((hdr->sh_flags & SHF_ALLOC) != 0)
346 {
347 flags |= SEC_ALLOC;
348 if (hdr->sh_type != SHT_NOBITS)
349 flags |= SEC_LOAD;
350 }
351 if ((hdr->sh_flags & SHF_WRITE) == 0)
352 flags |= SEC_READONLY;
353 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
354 flags |= SEC_CODE;
7c6da9ca 355 else if ((flags & SEC_LOAD) != 0)
497c5434
ILT
356 flags |= SEC_DATA;
357
358 /* The debugging sections appear to be recognized only by name, not
359 any sort of flag. */
360 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
361 || strncmp (name, ".line", sizeof ".line" - 1) == 0
362 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
363 flags |= SEC_DEBUGGING;
364
f0c12b73
DE
365 /* As a GNU extension, if the name begins with .gnu.linkonce, we
366 only link a single copy of the section. This is used to support
367 g++. g++ will emit each template expansion in its own section.
368 The symbols will be defined as weak, so that multiple definitions
369 are permitted. The GNU linker extension is to actually discard
370 all but one of the sections. */
371 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
372 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
373
497c5434
ILT
374 if (! bfd_set_section_flags (abfd, newsect, flags))
375 return false;
376
fd0198f0
ILT
377 if ((flags & SEC_ALLOC) != 0)
378 {
379 Elf_Internal_Phdr *phdr;
380 unsigned int i;
381
382 /* Look through the phdrs to see if we need to adjust the lma. */
383 phdr = elf_tdata (abfd)->phdr;
384 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
385 {
386 if (phdr->p_type == PT_LOAD
6933148a 387 && phdr->p_paddr != 0
fd0198f0
ILT
388 && phdr->p_vaddr != phdr->p_paddr
389 && phdr->p_vaddr <= hdr->sh_addr
b944e7e8
ILT
390 && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size
391 && ((flags & SEC_LOAD) == 0
392 || (phdr->p_offset <= hdr->sh_offset
393 && (phdr->p_offset + phdr->p_filesz
394 >= hdr->sh_offset + hdr->sh_size))))
fd0198f0
ILT
395 {
396 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
397 break;
398 }
399 }
400 }
401
b176e1e9 402 hdr->bfd_section = newsect;
497c5434
ILT
403 elf_section_data (newsect)->this_hdr = *hdr;
404
405 return true;
406}
407
32090b8e
KR
408/*
409INTERNAL_FUNCTION
410 bfd_elf_find_section
411
412SYNOPSIS
413 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
414
415DESCRIPTION
416 Helper functions for GDB to locate the string tables.
417 Since BFD hides string tables from callers, GDB needs to use an
418 internal hook to find them. Sun's .stabstr, in particular,
419 isn't even pointed to by the .stab section, so ordinary
420 mechanisms wouldn't work to find it, even if we had some.
421*/
422
423struct elf_internal_shdr *
013dec1a
ILT
424bfd_elf_find_section (abfd, name)
425 bfd * abfd;
426 char *name;
32090b8e
KR
427{
428 Elf_Internal_Shdr **i_shdrp;
429 char *shstrtab;
430 unsigned int max;
431 unsigned int i;
432
433 i_shdrp = elf_elfsections (abfd);
434 if (i_shdrp != NULL)
435 {
ede4eed4 436 shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
32090b8e
KR
437 if (shstrtab != NULL)
438 {
439 max = elf_elfheader (abfd)->e_shnum;
440 for (i = 1; i < max; i++)
441 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
442 return i_shdrp[i];
443 }
444 }
445 return 0;
446}
447
32090b8e
KR
448const char *const bfd_elf_section_type_names[] = {
449 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
450 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
451 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
452};
453
454/* ELF relocs are against symbols. If we are producing relocateable
455 output, and the reloc is against an external symbol, and nothing
456 has given us any additional addend, the resulting reloc will also
457 be against the same symbol. In such a case, we don't want to
458 change anything about the way the reloc is handled, since it will
459 all be done at final link time. Rather than put special case code
460 into bfd_perform_relocation, all the reloc types use this howto
461 function. It just short circuits the reloc if producing
462 relocateable output against an external symbol. */
463
013dec1a 464/*ARGSUSED*/
32090b8e
KR
465bfd_reloc_status_type
466bfd_elf_generic_reloc (abfd,
467 reloc_entry,
468 symbol,
469 data,
470 input_section,
4c3721d5
ILT
471 output_bfd,
472 error_message)
32090b8e
KR
473 bfd *abfd;
474 arelent *reloc_entry;
475 asymbol *symbol;
476 PTR data;
477 asection *input_section;
478 bfd *output_bfd;
4c3721d5 479 char **error_message;
32090b8e
KR
480{
481 if (output_bfd != (bfd *) NULL
482 && (symbol->flags & BSF_SECTION_SYM) == 0
d1b44e83
ILT
483 && (! reloc_entry->howto->partial_inplace
484 || reloc_entry->addend == 0))
32090b8e
KR
485 {
486 reloc_entry->address += input_section->output_offset;
487 return bfd_reloc_ok;
488 }
489
490 return bfd_reloc_continue;
491}
013dec1a 492\f
27fb8f29
ILT
493/* Print out the program headers. */
494
495boolean
496_bfd_elf_print_private_bfd_data (abfd, farg)
497 bfd *abfd;
498 PTR farg;
499{
500 FILE *f = (FILE *) farg;
501 Elf_Internal_Phdr *p;
02fcd126
ILT
502 asection *s;
503 bfd_byte *dynbuf = NULL;
27fb8f29
ILT
504
505 p = elf_tdata (abfd)->phdr;
02fcd126 506 if (p != NULL)
27fb8f29 507 {
02fcd126 508 unsigned int i, c;
27fb8f29 509
02fcd126
ILT
510 fprintf (f, "\nProgram Header:\n");
511 c = elf_elfheader (abfd)->e_phnum;
512 for (i = 0; i < c; i++, p++)
27fb8f29 513 {
02fcd126
ILT
514 const char *s;
515 char buf[20];
516
517 switch (p->p_type)
518 {
519 case PT_NULL: s = "NULL"; break;
520 case PT_LOAD: s = "LOAD"; break;
521 case PT_DYNAMIC: s = "DYNAMIC"; break;
522 case PT_INTERP: s = "INTERP"; break;
523 case PT_NOTE: s = "NOTE"; break;
524 case PT_SHLIB: s = "SHLIB"; break;
525 case PT_PHDR: s = "PHDR"; break;
526 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
527 }
528 fprintf (f, "%8s off 0x", s);
529 fprintf_vma (f, p->p_offset);
530 fprintf (f, " vaddr 0x");
531 fprintf_vma (f, p->p_vaddr);
532 fprintf (f, " paddr 0x");
533 fprintf_vma (f, p->p_paddr);
534 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
535 fprintf (f, " filesz 0x");
536 fprintf_vma (f, p->p_filesz);
537 fprintf (f, " memsz 0x");
538 fprintf_vma (f, p->p_memsz);
539 fprintf (f, " flags %c%c%c",
540 (p->p_flags & PF_R) != 0 ? 'r' : '-',
541 (p->p_flags & PF_W) != 0 ? 'w' : '-',
542 (p->p_flags & PF_X) != 0 ? 'x' : '-');
543 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
544 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
545 fprintf (f, "\n");
546 }
547 }
548
549 s = bfd_get_section_by_name (abfd, ".dynamic");
550 if (s != NULL)
551 {
552 int elfsec;
553 unsigned long link;
554 bfd_byte *extdyn, *extdynend;
555 size_t extdynsize;
556 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
557
558 fprintf (f, "\nDynamic Section:\n");
559
560 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
561 if (dynbuf == NULL)
562 goto error_return;
563 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
564 s->_raw_size))
565 goto error_return;
566
567 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
568 if (elfsec == -1)
569 goto error_return;
570 link = elf_elfsections (abfd)[elfsec]->sh_link;
571
572 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
573 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
574
575 extdyn = dynbuf;
576 extdynend = extdyn + s->_raw_size;
577 for (; extdyn < extdynend; extdyn += extdynsize)
578 {
579 Elf_Internal_Dyn dyn;
580 const char *name;
581 char ab[20];
582 boolean stringp;
583
584 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
585
586 if (dyn.d_tag == DT_NULL)
587 break;
588
589 stringp = false;
590 switch (dyn.d_tag)
591 {
592 default:
927d05b5 593 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
02fcd126
ILT
594 name = ab;
595 break;
596
597 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
598 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
599 case DT_PLTGOT: name = "PLTGOT"; break;
600 case DT_HASH: name = "HASH"; break;
601 case DT_STRTAB: name = "STRTAB"; break;
602 case DT_SYMTAB: name = "SYMTAB"; break;
603 case DT_RELA: name = "RELA"; break;
604 case DT_RELASZ: name = "RELASZ"; break;
605 case DT_RELAENT: name = "RELAENT"; break;
606 case DT_STRSZ: name = "STRSZ"; break;
607 case DT_SYMENT: name = "SYMENT"; break;
608 case DT_INIT: name = "INIT"; break;
609 case DT_FINI: name = "FINI"; break;
610 case DT_SONAME: name = "SONAME"; stringp = true; break;
611 case DT_RPATH: name = "RPATH"; stringp = true; break;
612 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
613 case DT_REL: name = "REL"; break;
614 case DT_RELSZ: name = "RELSZ"; break;
615 case DT_RELENT: name = "RELENT"; break;
616 case DT_PLTREL: name = "PLTREL"; break;
617 case DT_DEBUG: name = "DEBUG"; break;
618 case DT_TEXTREL: name = "TEXTREL"; break;
619 case DT_JMPREL: name = "JMPREL"; break;
148437ec
ILT
620 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
621 case DT_FILTER: name = "FILTER"; stringp = true; break;
a66a61a0
ILT
622 case DT_VERSYM: name = "VERSYM"; break;
623 case DT_VERDEF: name = "VERDEF"; break;
624 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
625 case DT_VERNEED: name = "VERNEED"; break;
626 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
02fcd126
ILT
627 }
628
629 fprintf (f, " %-11s ", name);
630 if (! stringp)
927d05b5 631 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
02fcd126
ILT
632 else
633 {
634 const char *string;
635
636 string = bfd_elf_string_from_elf_section (abfd, link,
637 dyn.d_un.d_val);
638 if (string == NULL)
639 goto error_return;
640 fprintf (f, "%s", string);
641 }
642 fprintf (f, "\n");
27fb8f29 643 }
02fcd126
ILT
644
645 free (dynbuf);
646 dynbuf = NULL;
27fb8f29
ILT
647 }
648
a66a61a0
ILT
649 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
650 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
651 {
652 if (! _bfd_elf_slurp_version_tables (abfd))
653 return false;
654 }
655
656 if (elf_dynverdef (abfd) != 0)
657 {
658 Elf_Internal_Verdef *t;
659
660 fprintf (f, "\nVersion definitions:\n");
661 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
662 {
663 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
664 t->vd_flags, t->vd_hash, t->vd_nodename);
665 if (t->vd_auxptr->vda_nextptr != NULL)
666 {
667 Elf_Internal_Verdaux *a;
668
669 fprintf (f, "\t");
670 for (a = t->vd_auxptr->vda_nextptr;
671 a != NULL;
672 a = a->vda_nextptr)
673 fprintf (f, "%s ", a->vda_nodename);
674 fprintf (f, "\n");
675 }
676 }
677 }
678
679 if (elf_dynverref (abfd) != 0)
680 {
681 Elf_Internal_Verneed *t;
682
683 fprintf (f, "\nVersion References:\n");
684 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
685 {
686 Elf_Internal_Vernaux *a;
687
688 fprintf (f, " required from %s:\n", t->vn_filename);
689 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
690 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
691 a->vna_flags, a->vna_other, a->vna_nodename);
692 }
693 }
694
27fb8f29 695 return true;
02fcd126
ILT
696
697 error_return:
698 if (dynbuf != NULL)
699 free (dynbuf);
700 return false;
27fb8f29
ILT
701}
702
b176e1e9
ILT
703/* Display ELF-specific fields of a symbol. */
704void
705bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
706 bfd *ignore_abfd;
707 PTR filep;
708 asymbol *symbol;
709 bfd_print_symbol_type how;
710{
711 FILE *file = (FILE *) filep;
712 switch (how)
713 {
714 case bfd_print_symbol_name:
715 fprintf (file, "%s", symbol->name);
716 break;
717 case bfd_print_symbol_more:
718 fprintf (file, "elf ");
719 fprintf_vma (file, symbol->value);
720 fprintf (file, " %lx", (long) symbol->flags);
721 break;
722 case bfd_print_symbol_all:
723 {
724 CONST char *section_name;
725 section_name = symbol->section ? symbol->section->name : "(*none*)";
726 bfd_print_symbol_vandf ((PTR) file, symbol);
727 fprintf (file, " %s\t", section_name);
728 /* Print the "other" value for a symbol. For common symbols,
729 we've already printed the size; now print the alignment.
730 For other symbols, we have no specified alignment, and
731 we've printed the address; now print the size. */
732 fprintf_vma (file,
733 (bfd_is_com_section (symbol->section)
734 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
735 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
69e2ff18
ILT
736 /* If the st_other field is not zero, print it. */
737 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
738 fprintf (file, " 0x%02x",
739 ((unsigned int)
740 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
b176e1e9
ILT
741 fprintf (file, " %s", symbol->name);
742 }
743 break;
744 }
745}
746\f
013dec1a
ILT
747/* Create an entry in an ELF linker hash table. */
748
5315c428
ILT
749struct bfd_hash_entry *
750_bfd_elf_link_hash_newfunc (entry, table, string)
013dec1a
ILT
751 struct bfd_hash_entry *entry;
752 struct bfd_hash_table *table;
753 const char *string;
754{
755 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
756
757 /* Allocate the structure if it has not already been allocated by a
758 subclass. */
759 if (ret == (struct elf_link_hash_entry *) NULL)
760 ret = ((struct elf_link_hash_entry *)
761 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
762 if (ret == (struct elf_link_hash_entry *) NULL)
a9713b91 763 return (struct bfd_hash_entry *) ret;
013dec1a
ILT
764
765 /* Call the allocation method of the superclass. */
766 ret = ((struct elf_link_hash_entry *)
767 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
768 table, string));
769 if (ret != (struct elf_link_hash_entry *) NULL)
770 {
771 /* Set local fields. */
772 ret->indx = -1;
773 ret->size = 0;
013dec1a
ILT
774 ret->dynindx = -1;
775 ret->dynstr_index = 0;
776 ret->weakdef = NULL;
b176e1e9
ILT
777 ret->got_offset = (bfd_vma) -1;
778 ret->plt_offset = (bfd_vma) -1;
86aac8ea 779 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
a66a61a0 780 ret->verinfo.verdef = NULL;
013dec1a 781 ret->type = STT_NOTYPE;
80be821d 782 ret->other = 0;
869b7d80
ILT
783 /* Assume that we have been called by a non-ELF symbol reader.
784 This flag is then reset by the code which reads an ELF input
785 file. This ensures that a symbol created by a non-ELF symbol
786 reader will have the flag set correctly. */
787 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
013dec1a
ILT
788 }
789
790 return (struct bfd_hash_entry *) ret;
791}
792
5315c428
ILT
793/* Initialize an ELF linker hash table. */
794
795boolean
796_bfd_elf_link_hash_table_init (table, abfd, newfunc)
797 struct elf_link_hash_table *table;
798 bfd *abfd;
799 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
800 struct bfd_hash_table *,
801 const char *));
802{
b176e1e9 803 table->dynamic_sections_created = false;
5315c428 804 table->dynobj = NULL;
b176e1e9
ILT
805 /* The first dynamic symbol is a dummy. */
806 table->dynsymcount = 1;
5315c428
ILT
807 table->dynstr = NULL;
808 table->bucketcount = 0;
b176e1e9 809 table->needed = NULL;
19bfbcbe 810 table->hgot = NULL;
d1bf45aa 811 table->stab_info = NULL;
5315c428
ILT
812 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
813}
814
013dec1a
ILT
815/* Create an ELF linker hash table. */
816
817struct bfd_link_hash_table *
818_bfd_elf_link_hash_table_create (abfd)
819 bfd *abfd;
820{
821 struct elf_link_hash_table *ret;
822
823 ret = ((struct elf_link_hash_table *)
824 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
825 if (ret == (struct elf_link_hash_table *) NULL)
a9713b91 826 return NULL;
5315c428
ILT
827
828 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
013dec1a
ILT
829 {
830 bfd_release (abfd, ret);
831 return NULL;
832 }
833
013dec1a
ILT
834 return &ret->root;
835}
7c6da9ca
ILT
836
837/* This is a hook for the ELF emulation code in the generic linker to
838 tell the backend linker what file name to use for the DT_NEEDED
b176e1e9
ILT
839 entry for a dynamic object. The generic linker passes name as an
840 empty string to indicate that no DT_NEEDED entry should be made. */
7c6da9ca
ILT
841
842void
843bfd_elf_set_dt_needed_name (abfd, name)
844 bfd *abfd;
845 const char *name;
846{
053ae1d7
ILT
847 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
848 && bfd_get_format (abfd) == bfd_object)
849 elf_dt_name (abfd) = name;
7c6da9ca 850}
b176e1e9 851
053ae1d7
ILT
852/* Get the list of DT_NEEDED entries for a link. This is a hook for
853 the ELF emulation code. */
b176e1e9 854
5fe14a9f 855struct bfd_link_needed_list *
b176e1e9
ILT
856bfd_elf_get_needed_list (abfd, info)
857 bfd *abfd;
858 struct bfd_link_info *info;
859{
b2193cc5
ILT
860 if (info->hash->creator->flavour != bfd_target_elf_flavour)
861 return NULL;
b176e1e9
ILT
862 return elf_hash_table (info)->needed;
863}
053ae1d7
ILT
864
865/* Get the name actually used for a dynamic object for a link. This
866 is the SONAME entry if there is one. Otherwise, it is the string
867 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
868
869const char *
870bfd_elf_get_dt_soname (abfd)
871 bfd *abfd;
872{
873 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
874 && bfd_get_format (abfd) == bfd_object)
875 return elf_dt_name (abfd);
876 return NULL;
877}
ede4eed4
KR
878\f
879/* Allocate an ELF string table--force the first byte to be zero. */
880
881struct bfd_strtab_hash *
882_bfd_elf_stringtab_init ()
883{
884 struct bfd_strtab_hash *ret;
885
886 ret = _bfd_stringtab_init ();
887 if (ret != NULL)
888 {
889 bfd_size_type loc;
890
891 loc = _bfd_stringtab_add (ret, "", true, false);
892 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
893 if (loc == (bfd_size_type) -1)
894 {
895 _bfd_stringtab_free (ret);
896 ret = NULL;
897 }
898 }
899 return ret;
900}
901\f
902/* ELF .o/exec file reading */
903
904/* Create a new bfd section from an ELF section header. */
905
906boolean
907bfd_section_from_shdr (abfd, shindex)
908 bfd *abfd;
909 unsigned int shindex;
910{
911 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
912 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
913 struct elf_backend_data *bed = get_elf_backend_data (abfd);
914 char *name;
915
916 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
917
918 switch (hdr->sh_type)
919 {
920 case SHT_NULL:
921 /* Inactive section. Throw it away. */
922 return true;
923
924 case SHT_PROGBITS: /* Normal section with contents. */
925 case SHT_DYNAMIC: /* Dynamic linking information. */
926 case SHT_NOBITS: /* .bss section. */
927 case SHT_HASH: /* .hash section. */
5b3b9ff6 928 case SHT_NOTE: /* .note section. */
ede4eed4
KR
929 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
930
931 case SHT_SYMTAB: /* A symbol table */
932 if (elf_onesymtab (abfd) == shindex)
933 return true;
934
935 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
936 BFD_ASSERT (elf_onesymtab (abfd) == 0);
937 elf_onesymtab (abfd) = shindex;
938 elf_tdata (abfd)->symtab_hdr = *hdr;
fd0198f0 939 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
ede4eed4
KR
940 abfd->flags |= HAS_SYMS;
941
942 /* Sometimes a shared object will map in the symbol table. If
943 SHF_ALLOC is set, and this is a shared object, then we also
944 treat this section as a BFD section. We can not base the
945 decision purely on SHF_ALLOC, because that flag is sometimes
946 set in a relocateable object file, which would confuse the
947 linker. */
948 if ((hdr->sh_flags & SHF_ALLOC) != 0
949 && (abfd->flags & DYNAMIC) != 0
950 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
951 return false;
952
953 return true;
954
955 case SHT_DYNSYM: /* A dynamic symbol table */
956 if (elf_dynsymtab (abfd) == shindex)
957 return true;
958
959 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
960 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
961 elf_dynsymtab (abfd) = shindex;
962 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
fd0198f0 963 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
ede4eed4
KR
964 abfd->flags |= HAS_SYMS;
965
966 /* Besides being a symbol table, we also treat this as a regular
967 section, so that objcopy can handle it. */
968 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
969
970 case SHT_STRTAB: /* A string table */
971 if (hdr->bfd_section != NULL)
972 return true;
973 if (ehdr->e_shstrndx == shindex)
974 {
975 elf_tdata (abfd)->shstrtab_hdr = *hdr;
976 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
977 return true;
978 }
979 {
980 unsigned int i;
981
982 for (i = 1; i < ehdr->e_shnum; i++)
983 {
984 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
985 if (hdr2->sh_link == shindex)
986 {
987 if (! bfd_section_from_shdr (abfd, i))
988 return false;
989 if (elf_onesymtab (abfd) == i)
990 {
991 elf_tdata (abfd)->strtab_hdr = *hdr;
992 elf_elfsections (abfd)[shindex] =
993 &elf_tdata (abfd)->strtab_hdr;
994 return true;
995 }
996 if (elf_dynsymtab (abfd) == i)
997 {
998 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
fd0198f0 999 elf_elfsections (abfd)[shindex] = hdr =
ede4eed4
KR
1000 &elf_tdata (abfd)->dynstrtab_hdr;
1001 /* We also treat this as a regular section, so
1002 that objcopy can handle it. */
1003 break;
1004 }
1005#if 0 /* Not handling other string tables specially right now. */
1006 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1007 /* We have a strtab for some random other section. */
1008 newsect = (asection *) hdr2->bfd_section;
1009 if (!newsect)
1010 break;
1011 hdr->bfd_section = newsect;
1012 hdr2 = &elf_section_data (newsect)->str_hdr;
1013 *hdr2 = *hdr;
1014 elf_elfsections (abfd)[shindex] = hdr2;
1015#endif
1016 }
1017 }
1018 }
1019
1020 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1021
1022 case SHT_REL:
1023 case SHT_RELA:
1024 /* *These* do a lot of work -- but build no sections! */
1025 {
1026 asection *target_sect;
1027 Elf_Internal_Shdr *hdr2;
ede4eed4 1028
ae115e51
ILT
1029 /* For some incomprehensible reason Oracle distributes
1030 libraries for Solaris in which some of the objects have
1031 bogus sh_link fields. It would be nice if we could just
1032 reject them, but, unfortunately, some people need to use
1033 them. We scan through the section headers; if we find only
1034 one suitable symbol table, we clobber the sh_link to point
1035 to it. I hope this doesn't break anything. */
1036 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1037 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1038 {
1039 int scan;
1040 int found;
1041
1042 found = 0;
1043 for (scan = 1; scan < ehdr->e_shnum; scan++)
1044 {
1045 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1046 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1047 {
1048 if (found != 0)
1049 {
1050 found = 0;
1051 break;
1052 }
1053 found = scan;
1054 }
1055 }
1056 if (found != 0)
1057 hdr->sh_link = found;
1058 }
1059
ede4eed4 1060 /* Get the symbol table. */
ae115e51
ILT
1061 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1062 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
ede4eed4
KR
1063 return false;
1064
1065 /* If this reloc section does not use the main symbol table we
1066 don't treat it as a reloc section. BFD can't adequately
1067 represent such a section, so at least for now, we don't
1068 try. We just present it as a normal section. */
1069 if (hdr->sh_link != elf_onesymtab (abfd))
e85f2fbd 1070 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
ede4eed4 1071
ede4eed4
KR
1072 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1073 return false;
1074 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1075 if (target_sect == NULL)
1076 return false;
1077
d1bf45aa
ILT
1078 if ((target_sect->flags & SEC_RELOC) == 0
1079 || target_sect->reloc_count == 0)
1080 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1081 else
1082 {
1083 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1084 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1085 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1086 }
ede4eed4
KR
1087 *hdr2 = *hdr;
1088 elf_elfsections (abfd)[shindex] = hdr2;
d1bf45aa 1089 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
ede4eed4
KR
1090 target_sect->flags |= SEC_RELOC;
1091 target_sect->relocation = NULL;
1092 target_sect->rel_filepos = hdr->sh_offset;
1093 abfd->flags |= HAS_RELOC;
1094 return true;
1095 }
1096 break;
1097
a66a61a0
ILT
1098 case SHT_GNU_verdef:
1099 elf_dynverdef (abfd) = shindex;
1100 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1101 break;
1102
1103 case SHT_GNU_versym:
1104 elf_dynversym (abfd) = shindex;
1105 elf_tdata (abfd)->dynversym_hdr = *hdr;
1106 break;
1107
1108 case SHT_GNU_verneed:
1109 elf_dynverref (abfd) = shindex;
1110 elf_tdata (abfd)->dynverref_hdr = *hdr;
1111 break;
1112
ede4eed4 1113 case SHT_SHLIB:
ede4eed4
KR
1114 return true;
1115
1116 default:
1117 /* Check for any processor-specific section types. */
1118 {
1119 if (bed->elf_backend_section_from_shdr)
1120 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1121 }
1122 break;
1123 }
1124
1125 return true;
1126}
1127
1128/* Given an ELF section number, retrieve the corresponding BFD
1129 section. */
1130
1131asection *
1132bfd_section_from_elf_index (abfd, index)
1133 bfd *abfd;
1134 unsigned int index;
1135{
1136 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1137 if (index >= elf_elfheader (abfd)->e_shnum)
1138 return NULL;
1139 return elf_elfsections (abfd)[index]->bfd_section;
1140}
1141
1142boolean
1143_bfd_elf_new_section_hook (abfd, sec)
1144 bfd *abfd;
1145 asection *sec;
1146{
1147 struct bfd_elf_section_data *sdata;
1148
1149 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
1150 if (!sdata)
a9713b91 1151 return false;
ede4eed4
KR
1152 sec->used_by_bfd = (PTR) sdata;
1153 memset (sdata, 0, sizeof (*sdata));
1154 return true;
1155}
1156
1157/* Create a new bfd section from an ELF program header.
1158
1159 Since program segments have no names, we generate a synthetic name
1160 of the form segment<NUM>, where NUM is generally the index in the
1161 program header table. For segments that are split (see below) we
1162 generate the names segment<NUM>a and segment<NUM>b.
1163
1164 Note that some program segments may have a file size that is different than
1165 (less than) the memory size. All this means is that at execution the
1166 system must allocate the amount of memory specified by the memory size,
1167 but only initialize it with the first "file size" bytes read from the
1168 file. This would occur for example, with program segments consisting
1169 of combined data+bss.
1170
1171 To handle the above situation, this routine generates TWO bfd sections
1172 for the single program segment. The first has the length specified by
1173 the file size of the segment, and the second has the length specified
1174 by the difference between the two sizes. In effect, the segment is split
1175 into it's initialized and uninitialized parts.
1176
1177 */
1178
1179boolean
1180bfd_section_from_phdr (abfd, hdr, index)
1181 bfd *abfd;
1182 Elf_Internal_Phdr *hdr;
1183 int index;
1184{
1185 asection *newsect;
1186 char *name;
1187 char namebuf[64];
1188 int split;
1189
1190 split = ((hdr->p_memsz > 0) &&
1191 (hdr->p_filesz > 0) &&
1192 (hdr->p_memsz > hdr->p_filesz));
1193 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
1194 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1195 if (!name)
a9713b91 1196 return false;
ede4eed4
KR
1197 strcpy (name, namebuf);
1198 newsect = bfd_make_section (abfd, name);
1199 if (newsect == NULL)
1200 return false;
1201 newsect->vma = hdr->p_vaddr;
ae115e51 1202 newsect->lma = hdr->p_paddr;
ede4eed4
KR
1203 newsect->_raw_size = hdr->p_filesz;
1204 newsect->filepos = hdr->p_offset;
1205 newsect->flags |= SEC_HAS_CONTENTS;
1206 if (hdr->p_type == PT_LOAD)
1207 {
1208 newsect->flags |= SEC_ALLOC;
1209 newsect->flags |= SEC_LOAD;
1210 if (hdr->p_flags & PF_X)
1211 {
1212 /* FIXME: all we known is that it has execute PERMISSION,
1213 may be data. */
1214 newsect->flags |= SEC_CODE;
1215 }
1216 }
1217 if (!(hdr->p_flags & PF_W))
1218 {
1219 newsect->flags |= SEC_READONLY;
1220 }
1221
1222 if (split)
1223 {
1224 sprintf (namebuf, "segment%db", index);
1225 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1226 if (!name)
a9713b91 1227 return false;
ede4eed4
KR
1228 strcpy (name, namebuf);
1229 newsect = bfd_make_section (abfd, name);
1230 if (newsect == NULL)
1231 return false;
1232 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
ae115e51 1233 newsect->lma = hdr->p_paddr + hdr->p_filesz;
ede4eed4
KR
1234 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1235 if (hdr->p_type == PT_LOAD)
1236 {
1237 newsect->flags |= SEC_ALLOC;
1238 if (hdr->p_flags & PF_X)
1239 newsect->flags |= SEC_CODE;
1240 }
1241 if (!(hdr->p_flags & PF_W))
1242 newsect->flags |= SEC_READONLY;
1243 }
1244
1245 return true;
1246}
1247
1248/* Set up an ELF internal section header for a section. */
1249
1250/*ARGSUSED*/
1251static void
1252elf_fake_sections (abfd, asect, failedptrarg)
1253 bfd *abfd;
1254 asection *asect;
1255 PTR failedptrarg;
1256{
1257 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1258 boolean *failedptr = (boolean *) failedptrarg;
1259 Elf_Internal_Shdr *this_hdr;
1260
1261 if (*failedptr)
1262 {
1263 /* We already failed; just get out of the bfd_map_over_sections
1264 loop. */
1265 return;
1266 }
1267
1268 this_hdr = &elf_section_data (asect)->this_hdr;
1269
1270 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1271 asect->name,
1272 true, false);
1273 if (this_hdr->sh_name == (unsigned long) -1)
1274 {
1275 *failedptr = true;
1276 return;
1277 }
1278
1279 this_hdr->sh_flags = 0;
ae115e51 1280
50bd50d4
MH
1281 if ((asect->flags & SEC_ALLOC) != 0
1282 || asect->user_set_vma)
fd0198f0 1283 this_hdr->sh_addr = asect->vma;
ede4eed4
KR
1284 else
1285 this_hdr->sh_addr = 0;
ae115e51 1286
ede4eed4
KR
1287 this_hdr->sh_offset = 0;
1288 this_hdr->sh_size = asect->_raw_size;
1289 this_hdr->sh_link = 0;
ede4eed4 1290 this_hdr->sh_addralign = 1 << asect->alignment_power;
fd0198f0
ILT
1291 /* The sh_entsize and sh_info fields may have been set already by
1292 copy_private_section_data. */
ede4eed4
KR
1293
1294 this_hdr->bfd_section = asect;
1295 this_hdr->contents = NULL;
1296
1297 /* FIXME: This should not be based on section names. */
1298 if (strcmp (asect->name, ".dynstr") == 0)
1299 this_hdr->sh_type = SHT_STRTAB;
1300 else if (strcmp (asect->name, ".hash") == 0)
1301 {
1302 this_hdr->sh_type = SHT_HASH;
1303 this_hdr->sh_entsize = bed->s->arch_size / 8;
1304 }
1305 else if (strcmp (asect->name, ".dynsym") == 0)
1306 {
1307 this_hdr->sh_type = SHT_DYNSYM;
1308 this_hdr->sh_entsize = bed->s->sizeof_sym;
1309 }
1310 else if (strcmp (asect->name, ".dynamic") == 0)
1311 {
1312 this_hdr->sh_type = SHT_DYNAMIC;
1313 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1314 }
1315 else if (strncmp (asect->name, ".rela", 5) == 0
1316 && get_elf_backend_data (abfd)->use_rela_p)
1317 {
1318 this_hdr->sh_type = SHT_RELA;
1319 this_hdr->sh_entsize = bed->s->sizeof_rela;
1320 }
1321 else if (strncmp (asect->name, ".rel", 4) == 0
1322 && ! get_elf_backend_data (abfd)->use_rela_p)
1323 {
1324 this_hdr->sh_type = SHT_REL;
1325 this_hdr->sh_entsize = bed->s->sizeof_rel;
1326 }
a66a61a0 1327 else if (strncmp (asect->name, ".note", 5) == 0)
ede4eed4
KR
1328 this_hdr->sh_type = SHT_NOTE;
1329 else if (strncmp (asect->name, ".stab", 5) == 0
1330 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1331 this_hdr->sh_type = SHT_STRTAB;
a66a61a0
ILT
1332 else if (strcmp (asect->name, ".gnu.version") == 0)
1333 {
1334 this_hdr->sh_type = SHT_GNU_versym;
1335 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1336 }
1337 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1338 {
1339 this_hdr->sh_type = SHT_GNU_verdef;
1340 this_hdr->sh_entsize = 0;
1341 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1342 }
1343 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1344 {
1345 this_hdr->sh_type = SHT_GNU_verneed;
1346 this_hdr->sh_entsize = 0;
1347 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1348 }
ede4eed4
KR
1349 else if ((asect->flags & SEC_ALLOC) != 0
1350 && (asect->flags & SEC_LOAD) != 0)
1351 this_hdr->sh_type = SHT_PROGBITS;
1352 else if ((asect->flags & SEC_ALLOC) != 0
1353 && ((asect->flags & SEC_LOAD) == 0))
5fe14a9f 1354 this_hdr->sh_type = SHT_NOBITS;
ede4eed4
KR
1355 else
1356 {
1357 /* Who knows? */
1358 this_hdr->sh_type = SHT_PROGBITS;
1359 }
1360
1361 if ((asect->flags & SEC_ALLOC) != 0)
1362 this_hdr->sh_flags |= SHF_ALLOC;
1363 if ((asect->flags & SEC_READONLY) == 0)
1364 this_hdr->sh_flags |= SHF_WRITE;
1365 if ((asect->flags & SEC_CODE) != 0)
1366 this_hdr->sh_flags |= SHF_EXECINSTR;
1367
1368 /* Check for processor-specific section types. */
1369 {
1370 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1371
1372 if (bed->elf_backend_fake_sections)
1373 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1374 }
1375
1376 /* If the section has relocs, set up a section header for the
1377 SHT_REL[A] section. */
1378 if ((asect->flags & SEC_RELOC) != 0)
1379 {
1380 Elf_Internal_Shdr *rela_hdr;
1381 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1382 char *name;
1383
1384 rela_hdr = &elf_section_data (asect)->rel_hdr;
1385 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1386 if (name == NULL)
1387 {
ede4eed4
KR
1388 *failedptr = true;
1389 return;
1390 }
1391 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1392 rela_hdr->sh_name =
1393 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1394 true, false);
1395 if (rela_hdr->sh_name == (unsigned int) -1)
1396 {
1397 *failedptr = true;
1398 return;
1399 }
1400 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1401 rela_hdr->sh_entsize = (use_rela_p
1402 ? bed->s->sizeof_rela
1403 : bed->s->sizeof_rel);
1404 rela_hdr->sh_addralign = bed->s->file_align;
1405 rela_hdr->sh_flags = 0;
1406 rela_hdr->sh_addr = 0;
1407 rela_hdr->sh_size = 0;
1408 rela_hdr->sh_offset = 0;
1409 }
1410}
1411
1412/* Assign all ELF section numbers. The dummy first section is handled here
1413 too. The link/info pointers for the standard section types are filled
1414 in here too, while we're at it. */
1415
1416static boolean
1417assign_section_numbers (abfd)
1418 bfd *abfd;
1419{
1420 struct elf_obj_tdata *t = elf_tdata (abfd);
1421 asection *sec;
1422 unsigned int section_number;
1423 Elf_Internal_Shdr **i_shdrp;
1424 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1425
1426 section_number = 1;
1427
1428 for (sec = abfd->sections; sec; sec = sec->next)
1429 {
1430 struct bfd_elf_section_data *d = elf_section_data (sec);
1431
1432 d->this_idx = section_number++;
1433 if ((sec->flags & SEC_RELOC) == 0)
1434 d->rel_idx = 0;
1435 else
1436 d->rel_idx = section_number++;
1437 }
1438
1439 t->shstrtab_section = section_number++;
1440 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1441 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1442
1443 if (abfd->symcount > 0)
1444 {
1445 t->symtab_section = section_number++;
1446 t->strtab_section = section_number++;
1447 }
1448
1449 elf_elfheader (abfd)->e_shnum = section_number;
1450
1451 /* Set up the list of section header pointers, in agreement with the
1452 indices. */
1453 i_shdrp = ((Elf_Internal_Shdr **)
1454 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1455 if (i_shdrp == NULL)
a9713b91 1456 return false;
ede4eed4
KR
1457
1458 i_shdrp[0] = ((Elf_Internal_Shdr *)
1459 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1460 if (i_shdrp[0] == NULL)
1461 {
1462 bfd_release (abfd, i_shdrp);
ede4eed4
KR
1463 return false;
1464 }
1465 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1466
1467 elf_elfsections (abfd) = i_shdrp;
1468
1469 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1470 if (abfd->symcount > 0)
1471 {
1472 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1473 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1474 t->symtab_hdr.sh_link = t->strtab_section;
1475 }
1476 for (sec = abfd->sections; sec; sec = sec->next)
1477 {
1478 struct bfd_elf_section_data *d = elf_section_data (sec);
1479 asection *s;
1480 const char *name;
1481
1482 i_shdrp[d->this_idx] = &d->this_hdr;
1483 if (d->rel_idx != 0)
1484 i_shdrp[d->rel_idx] = &d->rel_hdr;
1485
1486 /* Fill in the sh_link and sh_info fields while we're at it. */
1487
1488 /* sh_link of a reloc section is the section index of the symbol
1489 table. sh_info is the section index of the section to which
1490 the relocation entries apply. */
1491 if (d->rel_idx != 0)
1492 {
1493 d->rel_hdr.sh_link = t->symtab_section;
1494 d->rel_hdr.sh_info = d->this_idx;
1495 }
1496
1497 switch (d->this_hdr.sh_type)
1498 {
1499 case SHT_REL:
1500 case SHT_RELA:
1501 /* A reloc section which we are treating as a normal BFD
1502 section. sh_link is the section index of the symbol
1503 table. sh_info is the section index of the section to
1504 which the relocation entries apply. We assume that an
1505 allocated reloc section uses the dynamic symbol table.
1506 FIXME: How can we be sure? */
1507 s = bfd_get_section_by_name (abfd, ".dynsym");
1508 if (s != NULL)
1509 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1510
1511 /* We look up the section the relocs apply to by name. */
1512 name = sec->name;
1513 if (d->this_hdr.sh_type == SHT_REL)
1514 name += 4;
1515 else
1516 name += 5;
1517 s = bfd_get_section_by_name (abfd, name);
1518 if (s != NULL)
1519 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1520 break;
1521
1522 case SHT_STRTAB:
1523 /* We assume that a section named .stab*str is a stabs
1524 string section. We look for a section with the same name
1525 but without the trailing ``str'', and set its sh_link
1526 field to point to this section. */
1527 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1528 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1529 {
1530 size_t len;
1531 char *alc;
1532
1533 len = strlen (sec->name);
58142f10 1534 alc = (char *) bfd_malloc (len - 2);
ede4eed4 1535 if (alc == NULL)
58142f10 1536 return false;
ede4eed4
KR
1537 strncpy (alc, sec->name, len - 3);
1538 alc[len - 3] = '\0';
1539 s = bfd_get_section_by_name (abfd, alc);
1540 free (alc);
1541 if (s != NULL)
1542 {
1543 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1544
1545 /* This is a .stab section. */
1546 elf_section_data (s)->this_hdr.sh_entsize =
1547 4 + 2 * (bed->s->arch_size / 8);
1548 }
1549 }
1550 break;
1551
1552 case SHT_DYNAMIC:
1553 case SHT_DYNSYM:
a66a61a0
ILT
1554 case SHT_GNU_verneed:
1555 case SHT_GNU_verdef:
ede4eed4 1556 /* sh_link is the section header index of the string table
a66a61a0
ILT
1557 used for the dynamic entries, or the symbol table, or the
1558 version strings. */
ede4eed4
KR
1559 s = bfd_get_section_by_name (abfd, ".dynstr");
1560 if (s != NULL)
1561 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1562 break;
1563
1564 case SHT_HASH:
a66a61a0 1565 case SHT_GNU_versym:
ede4eed4 1566 /* sh_link is the section header index of the symbol table
a66a61a0 1567 this hash table or version table is for. */
ede4eed4
KR
1568 s = bfd_get_section_by_name (abfd, ".dynsym");
1569 if (s != NULL)
1570 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1571 break;
1572 }
1573 }
1574
1575 return true;
1576}
1577
1578/* Map symbol from it's internal number to the external number, moving
1579 all local symbols to be at the head of the list. */
1580
1581static INLINE int
1582sym_is_global (abfd, sym)
1583 bfd *abfd;
1584 asymbol *sym;
1585{
1586 /* If the backend has a special mapping, use it. */
1587 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1588 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1589 (abfd, sym));
1590
1591 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1592 || bfd_is_und_section (bfd_get_section (sym))
1593 || bfd_is_com_section (bfd_get_section (sym)));
1594}
1595
1596static boolean
1597elf_map_symbols (abfd)
1598 bfd *abfd;
1599{
1600 int symcount = bfd_get_symcount (abfd);
1601 asymbol **syms = bfd_get_outsymbols (abfd);
1602 asymbol **sect_syms;
1603 int num_locals = 0;
1604 int num_globals = 0;
1605 int num_locals2 = 0;
1606 int num_globals2 = 0;
1607 int max_index = 0;
1608 int num_sections = 0;
1609 int idx;
1610 asection *asect;
1611 asymbol **new_syms;
1612
1613#ifdef DEBUG
1614 fprintf (stderr, "elf_map_symbols\n");
1615 fflush (stderr);
1616#endif
1617
1618 /* Add a section symbol for each BFD section. FIXME: Is this really
1619 necessary? */
1620 for (asect = abfd->sections; asect; asect = asect->next)
1621 {
1622 if (max_index < asect->index)
1623 max_index = asect->index;
1624 }
1625
1626 max_index++;
1627 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1628 if (sect_syms == NULL)
a9713b91 1629 return false;
ede4eed4
KR
1630 elf_section_syms (abfd) = sect_syms;
1631
1632 for (idx = 0; idx < symcount; idx++)
1633 {
1634 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
fd0198f0 1635 && (syms[idx]->value + syms[idx]->section->vma) == 0)
ede4eed4
KR
1636 {
1637 asection *sec;
1638
1639 sec = syms[idx]->section;
1640 if (sec->owner != NULL)
1641 {
1642 if (sec->owner != abfd)
1643 {
1644 if (sec->output_offset != 0)
1645 continue;
1646 sec = sec->output_section;
1647 BFD_ASSERT (sec->owner == abfd);
1648 }
1649 sect_syms[sec->index] = syms[idx];
1650 }
1651 }
1652 }
1653
1654 for (asect = abfd->sections; asect; asect = asect->next)
1655 {
1656 asymbol *sym;
1657
1658 if (sect_syms[asect->index] != NULL)
1659 continue;
1660
1661 sym = bfd_make_empty_symbol (abfd);
1662 if (sym == NULL)
1663 return false;
1664 sym->the_bfd = abfd;
1665 sym->name = asect->name;
1666 sym->value = 0;
1667 /* Set the flags to 0 to indicate that this one was newly added. */
1668 sym->flags = 0;
1669 sym->section = asect;
1670 sect_syms[asect->index] = sym;
1671 num_sections++;
1672#ifdef DEBUG
1673 fprintf (stderr,
1674 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1675 asect->name, (long) asect->vma, asect->index, (long) asect);
1676#endif
1677 }
1678
1679 /* Classify all of the symbols. */
1680 for (idx = 0; idx < symcount; idx++)
1681 {
1682 if (!sym_is_global (abfd, syms[idx]))
1683 num_locals++;
1684 else
1685 num_globals++;
1686 }
1687 for (asect = abfd->sections; asect; asect = asect->next)
1688 {
1689 if (sect_syms[asect->index] != NULL
1690 && sect_syms[asect->index]->flags == 0)
1691 {
1692 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1693 if (!sym_is_global (abfd, sect_syms[asect->index]))
1694 num_locals++;
1695 else
1696 num_globals++;
1697 sect_syms[asect->index]->flags = 0;
1698 }
1699 }
1700
1701 /* Now sort the symbols so the local symbols are first. */
1702 new_syms = ((asymbol **)
1703 bfd_alloc (abfd,
1704 (num_locals + num_globals) * sizeof (asymbol *)));
1705 if (new_syms == NULL)
a9713b91 1706 return false;
ede4eed4
KR
1707
1708 for (idx = 0; idx < symcount; idx++)
1709 {
1710 asymbol *sym = syms[idx];
1711 int i;
1712
1713 if (!sym_is_global (abfd, sym))
1714 i = num_locals2++;
1715 else
1716 i = num_locals + num_globals2++;
1717 new_syms[i] = sym;
1718 sym->udata.i = i + 1;
1719 }
1720 for (asect = abfd->sections; asect; asect = asect->next)
1721 {
1722 if (sect_syms[asect->index] != NULL
1723 && sect_syms[asect->index]->flags == 0)
1724 {
1725 asymbol *sym = sect_syms[asect->index];
1726 int i;
1727
1728 sym->flags = BSF_SECTION_SYM;
1729 if (!sym_is_global (abfd, sym))
1730 i = num_locals2++;
1731 else
1732 i = num_locals + num_globals2++;
1733 new_syms[i] = sym;
1734 sym->udata.i = i + 1;
1735 }
1736 }
1737
1738 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1739
1740 elf_num_locals (abfd) = num_locals;
1741 elf_num_globals (abfd) = num_globals;
1742 return true;
1743}
1744
fd0198f0
ILT
1745/* Align to the maximum file alignment that could be required for any
1746 ELF data structure. */
1747
1748static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1749static INLINE file_ptr
1750align_file_position (off, align)
1751 file_ptr off;
1752 int align;
1753{
1754 return (off + align - 1) & ~(align - 1);
1755}
1756
1757/* Assign a file position to a section, optionally aligning to the
1758 required section alignment. */
1759
1760INLINE file_ptr
1761_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1762 Elf_Internal_Shdr *i_shdrp;
1763 file_ptr offset;
1764 boolean align;
1765{
1766 if (align)
1767 {
1768 unsigned int al;
1769
1770 al = i_shdrp->sh_addralign;
1771 if (al > 1)
1772 offset = BFD_ALIGN (offset, al);
1773 }
1774 i_shdrp->sh_offset = offset;
1775 if (i_shdrp->bfd_section != NULL)
1776 i_shdrp->bfd_section->filepos = offset;
1777 if (i_shdrp->sh_type != SHT_NOBITS)
1778 offset += i_shdrp->sh_size;
1779 return offset;
1780}
1781
ede4eed4
KR
1782/* Compute the file positions we are going to put the sections at, and
1783 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1784 is not NULL, this is being called by the ELF backend linker. */
1785
1786boolean
1787_bfd_elf_compute_section_file_positions (abfd, link_info)
1788 bfd *abfd;
1789 struct bfd_link_info *link_info;
1790{
1791 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1792 boolean failed;
1793 struct bfd_strtab_hash *strtab;
1794 Elf_Internal_Shdr *shstrtab_hdr;
1795
1796 if (abfd->output_has_begun)
1797 return true;
1798
1799 /* Do any elf backend specific processing first. */
1800 if (bed->elf_backend_begin_write_processing)
1801 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1802
1803 if (! prep_headers (abfd))
1804 return false;
1805
1806 failed = false;
1807 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1808 if (failed)
1809 return false;
1810
1811 if (!assign_section_numbers (abfd))
1812 return false;
1813
1814 /* The backend linker builds symbol table information itself. */
fd0198f0 1815 if (link_info == NULL && abfd->symcount > 0)
ede4eed4
KR
1816 {
1817 if (! swap_out_syms (abfd, &strtab))
1818 return false;
1819 }
1820
1821 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1822 /* sh_name was set in prep_headers. */
1823 shstrtab_hdr->sh_type = SHT_STRTAB;
1824 shstrtab_hdr->sh_flags = 0;
1825 shstrtab_hdr->sh_addr = 0;
1826 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1827 shstrtab_hdr->sh_entsize = 0;
1828 shstrtab_hdr->sh_link = 0;
1829 shstrtab_hdr->sh_info = 0;
fd0198f0 1830 /* sh_offset is set in assign_file_positions_except_relocs. */
ede4eed4
KR
1831 shstrtab_hdr->sh_addralign = 1;
1832
fd0198f0 1833 if (!assign_file_positions_except_relocs (abfd))
ede4eed4
KR
1834 return false;
1835
fd0198f0 1836 if (link_info == NULL && abfd->symcount > 0)
ede4eed4 1837 {
fd0198f0
ILT
1838 file_ptr off;
1839 Elf_Internal_Shdr *hdr;
1840
1841 off = elf_tdata (abfd)->next_file_pos;
1842
1843 hdr = &elf_tdata (abfd)->symtab_hdr;
1844 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1845
1846 hdr = &elf_tdata (abfd)->strtab_hdr;
1847 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1848
1849 elf_tdata (abfd)->next_file_pos = off;
1850
ede4eed4
KR
1851 /* Now that we know where the .strtab section goes, write it
1852 out. */
fd0198f0 1853 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ede4eed4
KR
1854 || ! _bfd_stringtab_emit (abfd, strtab))
1855 return false;
1856 _bfd_stringtab_free (strtab);
1857 }
1858
1859 abfd->output_has_begun = true;
1860
1861 return true;
1862}
1863
fd0198f0 1864/* Create a mapping from a set of sections to a program segment. */
ede4eed4 1865
fd0198f0 1866static INLINE struct elf_segment_map *
edf3fe48 1867make_mapping (abfd, sections, from, to, phdr)
fd0198f0
ILT
1868 bfd *abfd;
1869 asection **sections;
1870 unsigned int from;
1871 unsigned int to;
edf3fe48 1872 boolean phdr;
ede4eed4 1873{
fd0198f0
ILT
1874 struct elf_segment_map *m;
1875 unsigned int i;
1876 asection **hdrpp;
1877
1878 m = ((struct elf_segment_map *)
1879 bfd_zalloc (abfd,
1880 (sizeof (struct elf_segment_map)
1881 + (to - from - 1) * sizeof (asection *))));
1882 if (m == NULL)
a9713b91 1883 return NULL;
fd0198f0
ILT
1884 m->next = NULL;
1885 m->p_type = PT_LOAD;
1886 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1887 m->sections[i - from] = *hdrpp;
1888 m->count = to - from;
1889
edf3fe48 1890 if (from == 0 && phdr)
6933148a
ILT
1891 {
1892 /* Include the headers in the first PT_LOAD segment. */
1893 m->includes_filehdr = 1;
1894 m->includes_phdrs = 1;
1895 }
1896
fd0198f0 1897 return m;
ede4eed4
KR
1898}
1899
fd0198f0 1900/* Set up a mapping from BFD sections to program segments. */
ede4eed4 1901
fd0198f0
ILT
1902static boolean
1903map_sections_to_segments (abfd)
1904 bfd *abfd;
ede4eed4 1905{
fd0198f0
ILT
1906 asection **sections = NULL;
1907 asection *s;
1908 unsigned int i;
1909 unsigned int count;
1910 struct elf_segment_map *mfirst;
1911 struct elf_segment_map **pm;
1912 struct elf_segment_map *m;
1913 asection *last_hdr;
1914 unsigned int phdr_index;
1915 bfd_vma maxpagesize;
1916 asection **hdrpp;
edf3fe48
ILT
1917 boolean phdr_in_section = true;
1918 boolean writable;
1919 asection *dynsec;
fd0198f0
ILT
1920
1921 if (elf_tdata (abfd)->segment_map != NULL)
1922 return true;
1923
1924 if (bfd_count_sections (abfd) == 0)
1925 return true;
1926
1927 /* Select the allocated sections, and sort them. */
1928
58142f10
ILT
1929 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
1930 * sizeof (asection *));
fd0198f0 1931 if (sections == NULL)
58142f10 1932 goto error_return;
ede4eed4 1933
fd0198f0
ILT
1934 i = 0;
1935 for (s = abfd->sections; s != NULL; s = s->next)
1936 {
1937 if ((s->flags & SEC_ALLOC) != 0)
1938 {
1939 sections[i] = s;
1940 ++i;
1941 }
5fe14a9f 1942 }
fd0198f0
ILT
1943 BFD_ASSERT (i <= bfd_count_sections (abfd));
1944 count = i;
ede4eed4 1945
fd0198f0 1946 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
ede4eed4 1947
fd0198f0 1948 /* Build the mapping. */
ede4eed4 1949
fd0198f0
ILT
1950 mfirst = NULL;
1951 pm = &mfirst;
ede4eed4 1952
fd0198f0
ILT
1953 /* If we have a .interp section, then create a PT_PHDR segment for
1954 the program headers and a PT_INTERP segment for the .interp
1955 section. */
1956 s = bfd_get_section_by_name (abfd, ".interp");
1957 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1958 {
1959 m = ((struct elf_segment_map *)
1960 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1961 if (m == NULL)
a9713b91 1962 goto error_return;
fd0198f0
ILT
1963 m->next = NULL;
1964 m->p_type = PT_PHDR;
1965 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1966 m->p_flags = PF_R | PF_X;
1967 m->p_flags_valid = 1;
6933148a 1968 m->includes_phdrs = 1;
ede4eed4 1969
fd0198f0
ILT
1970 *pm = m;
1971 pm = &m->next;
ede4eed4 1972
fd0198f0
ILT
1973 m = ((struct elf_segment_map *)
1974 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1975 if (m == NULL)
a9713b91 1976 goto error_return;
fd0198f0
ILT
1977 m->next = NULL;
1978 m->p_type = PT_INTERP;
1979 m->count = 1;
1980 m->sections[0] = s;
ede4eed4 1981
fd0198f0
ILT
1982 *pm = m;
1983 pm = &m->next;
1984 }
ede4eed4 1985
fd0198f0
ILT
1986 /* Look through the sections. We put sections in the same program
1987 segment when the start of the second section can be placed within
1988 a few bytes of the end of the first section. */
1989 last_hdr = NULL;
1990 phdr_index = 0;
1991 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
edf3fe48
ILT
1992 writable = false;
1993 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
1994 if (dynsec != NULL
1995 && (dynsec->flags & SEC_LOAD) == 0)
1996 dynsec = NULL;
1997
7fc6a16a
ILT
1998 /* Deal with -Ttext or something similar such that the first section
1999 is not adjacent to the program headers. This is an
2000 approximation, since at this point we don't know exactly how many
2001 program headers we will need. */
2002 if (count > 0)
2003 {
2004 bfd_size_type phdr_size;
2005
2006 phdr_size = elf_tdata (abfd)->program_header_size;
2007 if (phdr_size == 0)
2008 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
cdb88e87
ILT
2009 if ((abfd->flags & D_PAGED) == 0
2010 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
7fc6a16a
ILT
2011 phdr_in_section = false;
2012 }
edf3fe48 2013
fd0198f0 2014 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
ede4eed4 2015 {
fd0198f0 2016 asection *hdr;
191d910c 2017 boolean new_segment;
ede4eed4 2018
fd0198f0 2019 hdr = *hdrpp;
ede4eed4 2020
fd0198f0 2021 /* See if this section and the last one will fit in the same
191d910c
ILT
2022 segment. */
2023
2024 if (last_hdr == NULL)
2025 {
2026 /* If we don't have a segment yet, then we don't need a new
2027 one (we build the last one after this loop). */
2028 new_segment = false;
2029 }
2030 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2031 {
2032 /* If this section has a different relation between the
2033 virtual address and the load address, then we need a new
2034 segment. */
2035 new_segment = true;
2036 }
191d910c
ILT
2037 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2038 < hdr->lma)
2039 {
2040 /* If putting this section in this segment would force us to
2041 skip a page in the segment, then we need a new segment. */
2042 new_segment = true;
2043 }
f0c12b73
DE
2044 else if ((abfd->flags & D_PAGED) == 0)
2045 {
2046 /* If the file is not demand paged, which means that we
2047 don't require the sections to be correctly aligned in the
2048 file, then there is no other reason for a new segment. */
2049 new_segment = false;
2050 }
191d910c
ILT
2051 else if ((last_hdr->flags & SEC_LOAD) == 0
2052 && (hdr->flags & SEC_LOAD) != 0)
2053 {
2054 /* We don't want to put a loadable section after a
2055 nonloadable section in the same segment. */
2056 new_segment = true;
2057 }
2058 else if (! writable
2059 && (hdr->flags & SEC_READONLY) == 0
2060 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2061 == hdr->lma))
2062 {
2063 /* We don't want to put a writable section in a read only
2064 segment, unless they are on the same page in memory
2065 anyhow. We already know that the last section does not
2066 bring us past the current section on the page, so the
2067 only case in which the new section is not on the same
2068 page as the previous section is when the previous section
2069 ends precisely on a page boundary. */
2070 new_segment = true;
2071 }
2072 else
2073 {
2074 /* Otherwise, we can use the same segment. */
2075 new_segment = false;
2076 }
2077
2078 if (! new_segment)
fd0198f0 2079 {
50bd50d4
MH
2080 if ((hdr->flags & SEC_READONLY) == 0)
2081 writable = true;
fd0198f0
ILT
2082 last_hdr = hdr;
2083 continue;
2084 }
ede4eed4 2085
191d910c
ILT
2086 /* We need a new program segment. We must create a new program
2087 header holding all the sections from phdr_index until hdr. */
ede4eed4 2088
edf3fe48 2089 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
fd0198f0
ILT
2090 if (m == NULL)
2091 goto error_return;
ede4eed4 2092
fd0198f0
ILT
2093 *pm = m;
2094 pm = &m->next;
ede4eed4 2095
edf3fe48
ILT
2096 if ((hdr->flags & SEC_READONLY) == 0)
2097 writable = true;
50bd50d4
MH
2098 else
2099 writable = false;
edf3fe48 2100
fd0198f0
ILT
2101 last_hdr = hdr;
2102 phdr_index = i;
edf3fe48 2103 phdr_in_section = false;
ede4eed4 2104 }
fd0198f0
ILT
2105
2106 /* Create a final PT_LOAD program segment. */
2107 if (last_hdr != NULL)
ede4eed4 2108 {
edf3fe48 2109 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
fd0198f0
ILT
2110 if (m == NULL)
2111 goto error_return;
2112
2113 *pm = m;
2114 pm = &m->next;
ede4eed4
KR
2115 }
2116
fd0198f0 2117 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
edf3fe48 2118 if (dynsec != NULL)
ede4eed4 2119 {
fd0198f0
ILT
2120 m = ((struct elf_segment_map *)
2121 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2122 if (m == NULL)
a9713b91 2123 goto error_return;
fd0198f0
ILT
2124 m->next = NULL;
2125 m->p_type = PT_DYNAMIC;
2126 m->count = 1;
edf3fe48 2127 m->sections[0] = dynsec;
ede4eed4 2128
fd0198f0
ILT
2129 *pm = m;
2130 pm = &m->next;
ede4eed4
KR
2131 }
2132
a66a61a0
ILT
2133 /* For each loadable .note section, add a PT_NOTE segment. We don't
2134 use bfd_get_section_by_name, because if we link together
2135 nonloadable .note sections and loadable .note sections, we will
2136 generate two .note sections in the output file. FIXME: Using
2137 names for section types is bogus anyhow. */
2138 for (s = abfd->sections; s != NULL; s = s->next)
2139 {
2140 if ((s->flags & SEC_LOAD) != 0
2141 && strncmp (s->name, ".note", 5) == 0)
2142 {
2143 m = ((struct elf_segment_map *)
2144 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2145 if (m == NULL)
2146 goto error_return;
2147 m->next = NULL;
2148 m->p_type = PT_NOTE;
2149 m->count = 1;
2150 m->sections[0] = s;
2151
2152 *pm = m;
2153 pm = &m->next;
2154 }
2155 }
2156
fd0198f0
ILT
2157 free (sections);
2158 sections = NULL;
ae115e51 2159
fd0198f0
ILT
2160 elf_tdata (abfd)->segment_map = mfirst;
2161 return true;
2162
2163 error_return:
2164 if (sections != NULL)
2165 free (sections);
2166 return false;
ede4eed4
KR
2167}
2168
fd0198f0 2169/* Sort sections by VMA. */
ede4eed4 2170
fd0198f0
ILT
2171static int
2172elf_sort_sections (arg1, arg2)
2173 const PTR arg1;
2174 const PTR arg2;
ede4eed4 2175{
fd0198f0
ILT
2176 const asection *sec1 = *(const asection **) arg1;
2177 const asection *sec2 = *(const asection **) arg2;
ede4eed4 2178
fd0198f0
ILT
2179 if (sec1->vma < sec2->vma)
2180 return -1;
2181 else if (sec1->vma > sec2->vma)
2182 return 1;
ede4eed4 2183
cdb88e87
ILT
2184 /* Sort by LMA. Normally the LMA and the VMA will be the same, and
2185 this will do nothing. */
2186 if (sec1->lma < sec2->lma)
2187 return -1;
2188 else if (sec1->lma > sec2->lma)
2189 return 1;
2190
fd0198f0 2191 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
ede4eed4 2192
fd0198f0 2193#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
ede4eed4 2194
fd0198f0
ILT
2195 if (TOEND (sec1))
2196 if (TOEND (sec2))
2197 return sec1->target_index - sec2->target_index;
2198 else
2199 return 1;
ede4eed4 2200
fd0198f0
ILT
2201 if (TOEND (sec2))
2202 return -1;
ede4eed4 2203
fd0198f0 2204#undef TOEND
ede4eed4 2205
fd0198f0
ILT
2206 /* Sort by size, to put zero sized sections before others at the
2207 same address. */
ede4eed4 2208
fd0198f0
ILT
2209 if (sec1->_raw_size < sec2->_raw_size)
2210 return -1;
2211 if (sec1->_raw_size > sec2->_raw_size)
2212 return 1;
ede4eed4 2213
fd0198f0
ILT
2214 return sec1->target_index - sec2->target_index;
2215}
ede4eed4 2216
fd0198f0
ILT
2217/* Assign file positions to the sections based on the mapping from
2218 sections to segments. This function also sets up some fields in
2219 the file header, and writes out the program headers. */
ede4eed4 2220
fd0198f0
ILT
2221static boolean
2222assign_file_positions_for_segments (abfd)
2223 bfd *abfd;
2224{
2225 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2226 unsigned int count;
2227 struct elf_segment_map *m;
2228 unsigned int alloc;
2229 Elf_Internal_Phdr *phdrs;
64f808f9 2230 file_ptr off, voff;
6933148a
ILT
2231 bfd_vma filehdr_vaddr, filehdr_paddr;
2232 bfd_vma phdrs_vaddr, phdrs_paddr;
fd0198f0
ILT
2233 Elf_Internal_Phdr *p;
2234
2235 if (elf_tdata (abfd)->segment_map == NULL)
2236 {
2237 if (! map_sections_to_segments (abfd))
2238 return false;
2239 }
ede4eed4 2240
5b3b9ff6
ILT
2241 if (bed->elf_backend_modify_segment_map)
2242 {
2243 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2244 return false;
2245 }
2246
fd0198f0
ILT
2247 count = 0;
2248 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2249 ++count;
ede4eed4 2250
fd0198f0
ILT
2251 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2252 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2253 elf_elfheader (abfd)->e_phnum = count;
ede4eed4 2254
fd0198f0
ILT
2255 if (count == 0)
2256 return true;
ede4eed4 2257
fd0198f0
ILT
2258 /* If we already counted the number of program segments, make sure
2259 that we allocated enough space. This happens when SIZEOF_HEADERS
2260 is used in a linker script. */
2261 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2262 if (alloc != 0 && count > alloc)
2263 {
2264 ((*_bfd_error_handler)
2265 ("%s: Not enough room for program headers (allocated %u, need %u)",
2266 bfd_get_filename (abfd), alloc, count));
2267 bfd_set_error (bfd_error_bad_value);
2268 return false;
ede4eed4
KR
2269 }
2270
fd0198f0
ILT
2271 if (alloc == 0)
2272 alloc = count;
2273
2274 phdrs = ((Elf_Internal_Phdr *)
2275 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2276 if (phdrs == NULL)
a9713b91 2277 return false;
ede4eed4 2278
fd0198f0
ILT
2279 off = bed->s->sizeof_ehdr;
2280 off += alloc * bed->s->sizeof_phdr;
ede4eed4 2281
6933148a
ILT
2282 filehdr_vaddr = 0;
2283 filehdr_paddr = 0;
2284 phdrs_vaddr = 0;
2285 phdrs_paddr = 0;
fd0198f0
ILT
2286 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2287 m != NULL;
2288 m = m->next, p++)
2289 {
2290 unsigned int i;
2291 asection **secpp;
fd0198f0 2292
3b950780
ILT
2293 /* If elf_segment_map is not from map_sections_to_segments, the
2294 sections may not be correctly ordered. */
2295 if (m->count > 0)
2296 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2297 elf_sort_sections);
2298
fd0198f0
ILT
2299 p->p_type = m->p_type;
2300
2301 if (m->p_flags_valid)
2302 p->p_flags = m->p_flags;
14899eb7
ILT
2303 else
2304 p->p_flags = 0;
fd0198f0 2305
d49ddb85
ILT
2306 if (p->p_type == PT_LOAD
2307 && m->count > 0
d7775b43 2308 && (m->sections[0]->flags & SEC_ALLOC) != 0)
cdb88e87
ILT
2309 {
2310 if ((abfd->flags & D_PAGED) != 0)
2311 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2312 else
2313 off += ((m->sections[0]->vma - off)
2314 % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2315 }
44ef8897 2316
fd0198f0
ILT
2317 if (m->count == 0)
2318 p->p_vaddr = 0;
2319 else
2320 p->p_vaddr = m->sections[0]->vma;
ede4eed4 2321
fd0198f0
ILT
2322 if (m->p_paddr_valid)
2323 p->p_paddr = m->p_paddr;
2324 else if (m->count == 0)
2325 p->p_paddr = 0;
2326 else
2327 p->p_paddr = m->sections[0]->lma;
2328
cdb88e87
ILT
2329 if (p->p_type == PT_LOAD
2330 && (abfd->flags & D_PAGED) != 0)
fd0198f0
ILT
2331 p->p_align = bed->maxpagesize;
2332 else if (m->count == 0)
2333 p->p_align = bed->s->file_align;
2334 else
2335 p->p_align = 0;
2336
6933148a 2337 p->p_offset = 0;
fd0198f0
ILT
2338 p->p_filesz = 0;
2339 p->p_memsz = 0;
2340
6933148a 2341 if (m->includes_filehdr)
ede4eed4 2342 {
14899eb7
ILT
2343 if (! m->p_flags_valid)
2344 p->p_flags |= PF_R;
6933148a
ILT
2345 p->p_offset = 0;
2346 p->p_filesz = bed->s->sizeof_ehdr;
2347 p->p_memsz = bed->s->sizeof_ehdr;
2348 if (m->count > 0)
2349 {
2350 BFD_ASSERT (p->p_type == PT_LOAD);
2351 p->p_vaddr -= off;
2352 if (! m->p_paddr_valid)
2353 p->p_paddr -= off;
2354 }
2355 if (p->p_type == PT_LOAD)
2356 {
2357 filehdr_vaddr = p->p_vaddr;
2358 filehdr_paddr = p->p_paddr;
2359 }
2360 }
fd0198f0 2361
6933148a
ILT
2362 if (m->includes_phdrs)
2363 {
14899eb7
ILT
2364 if (! m->p_flags_valid)
2365 p->p_flags |= PF_R;
6933148a 2366 if (m->includes_filehdr)
fd0198f0 2367 {
6933148a 2368 if (p->p_type == PT_LOAD)
fd0198f0 2369 {
6933148a
ILT
2370 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2371 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
fd0198f0 2372 }
6933148a
ILT
2373 }
2374 else
2375 {
2376 p->p_offset = bed->s->sizeof_ehdr;
2377 if (m->count > 0)
2378 {
2379 BFD_ASSERT (p->p_type == PT_LOAD);
2380 p->p_vaddr -= off - p->p_offset;
2381 if (! m->p_paddr_valid)
2382 p->p_paddr -= off - p->p_offset;
2383 }
2384 if (p->p_type == PT_LOAD)
fd0198f0 2385 {
6933148a
ILT
2386 phdrs_vaddr = p->p_vaddr;
2387 phdrs_paddr = p->p_paddr;
fd0198f0 2388 }
6933148a
ILT
2389 }
2390 p->p_filesz += alloc * bed->s->sizeof_phdr;
2391 p->p_memsz += alloc * bed->s->sizeof_phdr;
2392 }
2393
2394 if (p->p_type == PT_LOAD)
2395 {
2396 if (! m->includes_filehdr && ! m->includes_phdrs)
2397 p->p_offset = off;
2398 else
2399 {
2400 file_ptr adjust;
fd0198f0 2401
6933148a
ILT
2402 adjust = off - (p->p_offset + p->p_filesz);
2403 p->p_filesz += adjust;
2404 p->p_memsz += adjust;
fd0198f0 2405 }
ede4eed4
KR
2406 }
2407
64f808f9 2408 voff = off;
fd0198f0 2409 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
ede4eed4 2410 {
fd0198f0
ILT
2411 asection *sec;
2412 flagword flags;
2413 bfd_size_type align;
2414
2415 sec = *secpp;
2416 flags = sec->flags;
cdb88e87 2417 align = 1 << bfd_get_section_alignment (abfd, sec);
fd0198f0
ILT
2418
2419 if (p->p_type == PT_LOAD)
2420 {
2421 bfd_vma adjust;
2422
2423 /* The section VMA must equal the file position modulo
2424 the page size. */
09609415 2425 if ((flags & SEC_ALLOC) != 0)
fd0198f0 2426 {
cdb88e87
ILT
2427 if ((abfd->flags & D_PAGED) != 0)
2428 adjust = (sec->vma - voff) % bed->maxpagesize;
2429 else
2430 adjust = (sec->vma - voff) % align;
d49ddb85
ILT
2431 if (adjust != 0)
2432 {
2433 if (i == 0)
2434 abort ();
2435 p->p_memsz += adjust;
19bfbcbe 2436 off += adjust;
64f808f9 2437 voff += adjust;
d49ddb85 2438 if ((flags & SEC_LOAD) != 0)
19bfbcbe 2439 p->p_filesz += adjust;
d49ddb85 2440 }
fd0198f0
ILT
2441 }
2442
2443 sec->filepos = off;
2444
2445 if ((flags & SEC_LOAD) != 0)
2446 off += sec->_raw_size;
64f808f9
ILT
2447 if ((flags & SEC_ALLOC) != 0)
2448 voff += sec->_raw_size;
fd0198f0
ILT
2449 }
2450
2451 p->p_memsz += sec->_raw_size;
2452
2453 if ((flags & SEC_LOAD) != 0)
2454 p->p_filesz += sec->_raw_size;
2455
fd0198f0
ILT
2456 if (align > p->p_align)
2457 p->p_align = align;
2458
2459 if (! m->p_flags_valid)
2460 {
14899eb7 2461 p->p_flags |= PF_R;
fd0198f0
ILT
2462 if ((flags & SEC_CODE) != 0)
2463 p->p_flags |= PF_X;
2464 if ((flags & SEC_READONLY) == 0)
2465 p->p_flags |= PF_W;
2466 }
ede4eed4 2467 }
fd0198f0 2468 }
ede4eed4 2469
fd0198f0
ILT
2470 /* Now that we have set the section file positions, we can set up
2471 the file positions for the non PT_LOAD segments. */
2472 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2473 m != NULL;
2474 m = m->next, p++)
2475 {
2476 if (p->p_type != PT_LOAD && m->count > 0)
ede4eed4 2477 {
6933148a
ILT
2478 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2479 p->p_offset = m->sections[0]->filepos;
2480 }
2481 if (m->count == 0)
2482 {
2483 if (m->includes_filehdr)
2484 {
2485 p->p_vaddr = filehdr_vaddr;
2486 if (! m->p_paddr_valid)
2487 p->p_paddr = filehdr_paddr;
2488 }
2489 else if (m->includes_phdrs)
2490 {
2491 p->p_vaddr = phdrs_vaddr;
2492 if (! m->p_paddr_valid)
2493 p->p_paddr = phdrs_paddr;
2494 }
ede4eed4 2495 }
ede4eed4
KR
2496 }
2497
fd0198f0
ILT
2498 /* Clear out any program headers we allocated but did not use. */
2499 for (; count < alloc; count++, p++)
ede4eed4 2500 {
fd0198f0
ILT
2501 memset (p, 0, sizeof *p);
2502 p->p_type = PT_NULL;
ede4eed4
KR
2503 }
2504
fd0198f0 2505 elf_tdata (abfd)->phdr = phdrs;
ede4eed4 2506
fd0198f0 2507 elf_tdata (abfd)->next_file_pos = off;
ede4eed4 2508
fd0198f0
ILT
2509 /* Write out the program headers. */
2510 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2511 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2512 return false;
2513
2514 return true;
2515}
2516
2517/* Get the size of the program header.
2518
2519 If this is called by the linker before any of the section VMA's are set, it
2520 can't calculate the correct value for a strange memory layout. This only
2521 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2522 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2523 data segment (exclusive of .interp and .dynamic).
2524
2525 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2526 will be two segments. */
2527
2528static bfd_size_type
2529get_program_header_size (abfd)
2530 bfd *abfd;
2531{
2532 size_t segs;
2533 asection *s;
2534 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2535
2536 /* We can't return a different result each time we're called. */
2537 if (elf_tdata (abfd)->program_header_size != 0)
2538 return elf_tdata (abfd)->program_header_size;
ae115e51 2539
3b950780
ILT
2540 if (elf_tdata (abfd)->segment_map != NULL)
2541 {
2542 struct elf_segment_map *m;
2543
2544 segs = 0;
2545 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2546 ++segs;
2547 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2548 return elf_tdata (abfd)->program_header_size;
2549 }
2550
fd0198f0
ILT
2551 /* Assume we will need exactly two PT_LOAD segments: one for text
2552 and one for data. */
2553 segs = 2;
2554
2555 s = bfd_get_section_by_name (abfd, ".interp");
2556 if (s != NULL && (s->flags & SEC_LOAD) != 0)
ede4eed4 2557 {
fd0198f0
ILT
2558 /* If we have a loadable interpreter section, we need a
2559 PT_INTERP segment. In this case, assume we also need a
2560 PT_PHDR segment, although that may not be true for all
2561 targets. */
2562 segs += 2;
ede4eed4
KR
2563 }
2564
fd0198f0 2565 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
ede4eed4 2566 {
fd0198f0
ILT
2567 /* We need a PT_DYNAMIC segment. */
2568 ++segs;
ede4eed4 2569 }
ede4eed4 2570
a66a61a0
ILT
2571 for (s = abfd->sections; s != NULL; s = s->next)
2572 {
2573 if ((s->flags & SEC_LOAD) != 0
2574 && strncmp (s->name, ".note", 5) == 0)
2575 {
2576 /* We need a PT_NOTE segment. */
2577 ++segs;
2578 }
2579 }
2580
fd0198f0 2581 /* Let the backend count up any program headers it might need. */
5b3b9ff6
ILT
2582 if (bed->elf_backend_additional_program_headers)
2583 {
2584 int a;
2585
2586 a = (*bed->elf_backend_additional_program_headers) (abfd);
2587 if (a == -1)
2588 abort ();
2589 segs += a;
2590 }
ede4eed4 2591
fd0198f0
ILT
2592 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2593 return elf_tdata (abfd)->program_header_size;
ede4eed4
KR
2594}
2595
2596/* Work out the file positions of all the sections. This is called by
2597 _bfd_elf_compute_section_file_positions. All the section sizes and
2598 VMAs must be known before this is called.
2599
2600 We do not consider reloc sections at this point, unless they form
2601 part of the loadable image. Reloc sections are assigned file
2602 positions in assign_file_positions_for_relocs, which is called by
2603 write_object_contents and final_link.
2604
fd0198f0 2605 We also don't set the positions of the .symtab and .strtab here. */
ede4eed4
KR
2606
2607static boolean
fd0198f0 2608assign_file_positions_except_relocs (abfd)
ede4eed4 2609 bfd *abfd;
ede4eed4
KR
2610{
2611 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2612 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2613 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2614 file_ptr off;
2615 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2616
ede4eed4
KR
2617 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2618 {
2619 Elf_Internal_Shdr **hdrpp;
2620 unsigned int i;
2621
fd0198f0
ILT
2622 /* Start after the ELF header. */
2623 off = i_ehdrp->e_ehsize;
2624
ede4eed4
KR
2625 /* We are not creating an executable, which means that we are
2626 not creating a program header, and that the actual order of
2627 the sections in the file is unimportant. */
2628 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2629 {
2630 Elf_Internal_Shdr *hdr;
2631
2632 hdr = *hdrpp;
2633 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2634 {
2635 hdr->sh_offset = -1;
2636 continue;
2637 }
fd0198f0
ILT
2638 if (i == tdata->symtab_section
2639 || i == tdata->strtab_section)
ede4eed4
KR
2640 {
2641 hdr->sh_offset = -1;
2642 continue;
2643 }
2644
5fe14a9f 2645 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
ede4eed4
KR
2646 }
2647 }
2648 else
2649 {
ede4eed4 2650 unsigned int i;
fd0198f0 2651 Elf_Internal_Shdr **hdrpp;
ede4eed4 2652
fd0198f0
ILT
2653 /* Assign file positions for the loaded sections based on the
2654 assignment of sections to segments. */
2655 if (! assign_file_positions_for_segments (abfd))
ede4eed4
KR
2656 return false;
2657
fd0198f0
ILT
2658 /* Assign file positions for the other sections. */
2659
2660 off = elf_tdata (abfd)->next_file_pos;
2661 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
ede4eed4
KR
2662 {
2663 Elf_Internal_Shdr *hdr;
2664
2665 hdr = *hdrpp;
fd0198f0
ILT
2666 if (hdr->bfd_section != NULL
2667 && hdr->bfd_section->filepos != 0)
2668 hdr->sh_offset = hdr->bfd_section->filepos;
2669 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
ede4eed4 2670 {
fd0198f0
ILT
2671 ((*_bfd_error_handler)
2672 ("%s: warning: allocated section `%s' not in segment",
2673 bfd_get_filename (abfd),
2674 (hdr->bfd_section == NULL
2675 ? "*unknown*"
2676 : hdr->bfd_section->name)));
cdb88e87
ILT
2677 if ((abfd->flags & D_PAGED) != 0)
2678 off += (hdr->sh_addr - off) % bed->maxpagesize;
2679 else
2680 off += (hdr->sh_addr - off) % hdr->sh_addralign;
5fe14a9f
ILT
2681 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2682 false);
ede4eed4 2683 }
fd0198f0
ILT
2684 else if (hdr->sh_type == SHT_REL
2685 || hdr->sh_type == SHT_RELA
2686 || hdr == i_shdrpp[tdata->symtab_section]
2687 || hdr == i_shdrpp[tdata->strtab_section])
2688 hdr->sh_offset = -1;
2689 else
2690 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2691 }
ede4eed4
KR
2692 }
2693
2694 /* Place the section headers. */
2695 off = align_file_position (off, bed->s->file_align);
2696 i_ehdrp->e_shoff = off;
2697 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2698
2699 elf_tdata (abfd)->next_file_pos = off;
2700
2701 return true;
2702}
2703
ede4eed4
KR
2704static boolean
2705prep_headers (abfd)
2706 bfd *abfd;
2707{
2708 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2709 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2710 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2711 int count;
2712 struct bfd_strtab_hash *shstrtab;
2713 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2714
2715 i_ehdrp = elf_elfheader (abfd);
2716 i_shdrp = elf_elfsections (abfd);
2717
2718 shstrtab = _bfd_elf_stringtab_init ();
2719 if (shstrtab == NULL)
2720 return false;
2721
2722 elf_shstrtab (abfd) = shstrtab;
2723
2724 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2725 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2726 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2727 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2728
2729 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2730 i_ehdrp->e_ident[EI_DATA] =
86587dd4 2731 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
ede4eed4
KR
2732 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2733
2734 for (count = EI_PAD; count < EI_NIDENT; count++)
2735 i_ehdrp->e_ident[count] = 0;
2736
2737 if ((abfd->flags & DYNAMIC) != 0)
2738 i_ehdrp->e_type = ET_DYN;
2739 else if ((abfd->flags & EXEC_P) != 0)
2740 i_ehdrp->e_type = ET_EXEC;
2741 else
2742 i_ehdrp->e_type = ET_REL;
2743
2744 switch (bfd_get_arch (abfd))
2745 {
2746 case bfd_arch_unknown:
2747 i_ehdrp->e_machine = EM_NONE;
2748 break;
2749 case bfd_arch_sparc:
2750 if (bed->s->arch_size == 64)
2751 i_ehdrp->e_machine = EM_SPARC64;
2752 else
2753 i_ehdrp->e_machine = EM_SPARC;
2754 break;
2755 case bfd_arch_i386:
2756 i_ehdrp->e_machine = EM_386;
2757 break;
2758 case bfd_arch_m68k:
2759 i_ehdrp->e_machine = EM_68K;
2760 break;
2761 case bfd_arch_m88k:
2762 i_ehdrp->e_machine = EM_88K;
2763 break;
2764 case bfd_arch_i860:
2765 i_ehdrp->e_machine = EM_860;
2766 break;
2767 case bfd_arch_mips: /* MIPS Rxxxx */
2768 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2769 break;
2770 case bfd_arch_hppa:
2771 i_ehdrp->e_machine = EM_PARISC;
2772 break;
2773 case bfd_arch_powerpc:
2774 i_ehdrp->e_machine = EM_PPC;
2775 break;
50bd50d4
MH
2776 case bfd_arch_alpha:
2777 i_ehdrp->e_machine = EM_ALPHA;
2778 break;
f0c12b73
DE
2779 case bfd_arch_sh:
2780 i_ehdrp->e_machine = EM_SH;
2781 break;
50bd50d4
MH
2782 case bfd_arch_d10v:
2783 i_ehdrp->e_machine = EM_CYGNUS_D10V;
2784 break;
fd8d7c31
MH
2785/* start-sanitize-d30v */
2786 case bfd_arch_d30v:
2787 i_ehdrp->e_machine = EM_CYGNUS_D30V;
2788 break;
2789/* end-sanitize-d30v */
f0c12b73
DE
2790/* start-sanitize-v850 */
2791 case bfd_arch_v850:
2792 i_ehdrp->e_machine = EM_CYGNUS_V850;
2793 break;
2794/* end-sanitize-v850 */
ede4eed4
KR
2795/* start-sanitize-arc */
2796 case bfd_arch_arc:
2797 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2798 break;
2799/* end-sanitize-arc */
f0c12b73
DE
2800/* start-sanitize-m32r */
2801 case bfd_arch_m32r:
2802 i_ehdrp->e_machine = EM_CYGNUS_M32R;
2803 break;
2804/* end-sanitize-m32r */
80be821d
ILT
2805 case bfd_arch_mn10200:
2806 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
2807 break;
2808 case bfd_arch_mn10300:
2809 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
efc2b064 2810 break;
ede4eed4
KR
2811 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2812 default:
2813 i_ehdrp->e_machine = EM_NONE;
2814 }
2815 i_ehdrp->e_version = bed->s->ev_current;
2816 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2817
2818 /* no program header, for now. */
2819 i_ehdrp->e_phoff = 0;
2820 i_ehdrp->e_phentsize = 0;
2821 i_ehdrp->e_phnum = 0;
2822
2823 /* each bfd section is section header entry */
2824 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2825 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2826
2827 /* if we're building an executable, we'll need a program header table */
2828 if (abfd->flags & EXEC_P)
2829 {
2830 /* it all happens later */
2831#if 0
2832 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2833
2834 /* elf_build_phdrs() returns a (NULL-terminated) array of
2835 Elf_Internal_Phdrs */
2836 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2837 i_ehdrp->e_phoff = outbase;
2838 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2839#endif
2840 }
2841 else
2842 {
2843 i_ehdrp->e_phentsize = 0;
2844 i_phdrp = 0;
2845 i_ehdrp->e_phoff = 0;
2846 }
2847
2848 elf_tdata (abfd)->symtab_hdr.sh_name =
2849 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2850 elf_tdata (abfd)->strtab_hdr.sh_name =
2851 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2852 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2853 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2854 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2855 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2856 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2857 return false;
2858
2859 return true;
2860}
2861
2862/* Assign file positions for all the reloc sections which are not part
2863 of the loadable file image. */
2864
2865void
2866_bfd_elf_assign_file_positions_for_relocs (abfd)
2867 bfd *abfd;
2868{
2869 file_ptr off;
2870 unsigned int i;
2871 Elf_Internal_Shdr **shdrpp;
2872
2873 off = elf_tdata (abfd)->next_file_pos;
2874
2875 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2876 i < elf_elfheader (abfd)->e_shnum;
2877 i++, shdrpp++)
2878 {
2879 Elf_Internal_Shdr *shdrp;
2880
2881 shdrp = *shdrpp;
2882 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2883 && shdrp->sh_offset == -1)
5fe14a9f 2884 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
ede4eed4
KR
2885 }
2886
2887 elf_tdata (abfd)->next_file_pos = off;
2888}
2889
2890boolean
2891_bfd_elf_write_object_contents (abfd)
2892 bfd *abfd;
2893{
2894 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2895 Elf_Internal_Ehdr *i_ehdrp;
2896 Elf_Internal_Shdr **i_shdrp;
2897 boolean failed;
2898 unsigned int count;
2899
2900 if (! abfd->output_has_begun
2901 && ! _bfd_elf_compute_section_file_positions (abfd,
2902 (struct bfd_link_info *) NULL))
2903 return false;
2904
2905 i_shdrp = elf_elfsections (abfd);
2906 i_ehdrp = elf_elfheader (abfd);
2907
2908 failed = false;
2909 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2910 if (failed)
2911 return false;
2912 _bfd_elf_assign_file_positions_for_relocs (abfd);
2913
2914 /* After writing the headers, we need to write the sections too... */
2915 for (count = 1; count < i_ehdrp->e_shnum; count++)
2916 {
2917 if (bed->elf_backend_section_processing)
2918 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2919 if (i_shdrp[count]->contents)
2920 {
2921 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2922 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2923 1, abfd)
2924 != i_shdrp[count]->sh_size))
2925 return false;
2926 }
2927 }
2928
2929 /* Write out the section header names. */
2930 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2931 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2932 return false;
2933
2934 if (bed->elf_backend_final_write_processing)
2935 (*bed->elf_backend_final_write_processing) (abfd,
2936 elf_tdata (abfd)->linker);
2937
2938 return bed->s->write_shdrs_and_ehdr (abfd);
2939}
2940
2941/* given a section, search the header to find them... */
2942int
2943_bfd_elf_section_from_bfd_section (abfd, asect)
2944 bfd *abfd;
2945 struct sec *asect;
2946{
2947 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2948 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2949 int index;
2950 Elf_Internal_Shdr *hdr;
2951 int maxindex = elf_elfheader (abfd)->e_shnum;
2952
2953 for (index = 0; index < maxindex; index++)
2954 {
2955 hdr = i_shdrp[index];
2956 if (hdr->bfd_section == asect)
2957 return index;
2958 }
2959
2960 if (bed->elf_backend_section_from_bfd_section)
2961 {
2962 for (index = 0; index < maxindex; index++)
2963 {
2964 int retval;
2965
2966 hdr = i_shdrp[index];
2967 retval = index;
2968 if ((*bed->elf_backend_section_from_bfd_section)
2969 (abfd, hdr, asect, &retval))
2970 return retval;
2971 }
2972 }
2973
2974 if (bfd_is_abs_section (asect))
2975 return SHN_ABS;
2976 if (bfd_is_com_section (asect))
2977 return SHN_COMMON;
2978 if (bfd_is_und_section (asect))
2979 return SHN_UNDEF;
2980
2981 return -1;
2982}
2983
cb84f028
ILT
2984/* Given a BFD symbol, return the index in the ELF symbol table, or -1
2985 on error. */
2986
2987int
ede4eed4
KR
2988_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2989 bfd *abfd;
7fc6a16a 2990 asymbol **asym_ptr_ptr;
ede4eed4 2991{
7fc6a16a 2992 asymbol *asym_ptr = *asym_ptr_ptr;
ede4eed4
KR
2993 int idx;
2994 flagword flags = asym_ptr->flags;
2995
2996 /* When gas creates relocations against local labels, it creates its
2997 own symbol for the section, but does put the symbol into the
2998 symbol chain, so udata is 0. When the linker is generating
2999 relocatable output, this section symbol may be for one of the
3000 input sections rather than the output section. */
3001 if (asym_ptr->udata.i == 0
3002 && (flags & BSF_SECTION_SYM)
3003 && asym_ptr->section)
3004 {
3005 int indx;
3006
3007 if (asym_ptr->section->output_section != NULL)
3008 indx = asym_ptr->section->output_section->index;
3009 else
3010 indx = asym_ptr->section->index;
3011 if (elf_section_syms (abfd)[indx])
3012 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3013 }
3014
3015 idx = asym_ptr->udata.i;
cb84f028
ILT
3016
3017 if (idx == 0)
3018 {
3019 /* This case can occur when using --strip-symbol on a symbol
3020 which is used in a relocation entry. */
3021 (*_bfd_error_handler)
3022 ("%s: symbol `%s' required but not present",
3023 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3024 bfd_set_error (bfd_error_no_symbols);
3025 return -1;
3026 }
ede4eed4
KR
3027
3028#if DEBUG & 4
3029 {
3030 fprintf (stderr,
3031 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
cb84f028
ILT
3032 (long) asym_ptr, asym_ptr->name, idx, flags,
3033 elf_symbol_flags (flags));
ede4eed4
KR
3034 fflush (stderr);
3035 }
3036#endif
3037
3038 return idx;
3039}
3040
3dbf33ee
ILT
3041/* Copy private BFD data. This copies any program header information. */
3042
3043static boolean
3044copy_private_bfd_data (ibfd, obfd)
3045 bfd *ibfd;
3046 bfd *obfd;
3047{
6933148a 3048 Elf_Internal_Ehdr *iehdr;
3dbf33ee
ILT
3049 struct elf_segment_map *mfirst;
3050 struct elf_segment_map **pm;
3051 Elf_Internal_Phdr *p;
3052 unsigned int i, c;
3053
3054 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3055 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3056 return true;
3057
3058 if (elf_tdata (ibfd)->phdr == NULL)
3059 return true;
3060
6933148a
ILT
3061 iehdr = elf_elfheader (ibfd);
3062
3dbf33ee
ILT
3063 mfirst = NULL;
3064 pm = &mfirst;
3065
3066 c = elf_elfheader (ibfd)->e_phnum;
3067 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
3068 {
3dbf33ee 3069 unsigned int csecs;
6933148a
ILT
3070 asection *s;
3071 struct elf_segment_map *m;
3072 unsigned int isec;
3dbf33ee
ILT
3073
3074 csecs = 0;
3dbf33ee 3075
6933148a
ILT
3076 /* The complicated case when p_vaddr is 0 is to handle the
3077 Solaris linker, which generates a PT_INTERP section with
3078 p_vaddr and p_memsz set to 0. */
3079 for (s = ibfd->sections; s != NULL; s = s->next)
3080 if (((s->vma >= p->p_vaddr
3081 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
3082 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
3083 || (p->p_vaddr == 0
3084 && p->p_filesz > 0
3085 && (s->flags & SEC_HAS_CONTENTS) != 0
3086 && (bfd_vma) s->filepos >= p->p_offset
3087 && ((bfd_vma) s->filepos + s->_raw_size
3088 <= p->p_offset + p->p_filesz)))
86587dd4 3089 && (s->flags & SEC_ALLOC) != 0
6933148a
ILT
3090 && s->output_section != NULL)
3091 ++csecs;
3dbf33ee
ILT
3092
3093 m = ((struct elf_segment_map *)
3094 bfd_alloc (obfd,
3095 (sizeof (struct elf_segment_map)
3096 + (csecs - 1) * sizeof (asection *))));
3097 if (m == NULL)
a9713b91 3098 return false;
3dbf33ee
ILT
3099
3100 m->next = NULL;
3101 m->p_type = p->p_type;
3102 m->p_flags = p->p_flags;
3103 m->p_flags_valid = 1;
3104 m->p_paddr = p->p_paddr;
3105 m->p_paddr_valid = 1;
3106
6933148a
ILT
3107 m->includes_filehdr = (p->p_offset == 0
3108 && p->p_filesz >= iehdr->e_ehsize);
3109
3110 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
3111 && (p->p_offset + p->p_filesz
3112 >= ((bfd_vma) iehdr->e_phoff
3113 + iehdr->e_phnum * iehdr->e_phentsize)));
3dbf33ee 3114
6933148a
ILT
3115 isec = 0;
3116 for (s = ibfd->sections; s != NULL; s = s->next)
3117 {
3118 if (((s->vma >= p->p_vaddr
3119 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
3120 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
3121 || (p->p_vaddr == 0
3122 && p->p_filesz > 0
3123 && (s->flags & SEC_HAS_CONTENTS) != 0
3124 && (bfd_vma) s->filepos >= p->p_offset
3125 && ((bfd_vma) s->filepos + s->_raw_size
3126 <= p->p_offset + p->p_filesz)))
86587dd4 3127 && (s->flags & SEC_ALLOC) != 0
6933148a 3128 && s->output_section != NULL)
3dbf33ee 3129 {
6933148a
ILT
3130 m->sections[isec] = s->output_section;
3131 ++isec;
3dbf33ee 3132 }
3dbf33ee 3133 }
6933148a 3134 BFD_ASSERT (isec == csecs);
6933148a 3135 m->count = csecs;
3dbf33ee
ILT
3136
3137 *pm = m;
3138 pm = &m->next;
3139 }
3140
3141 elf_tdata (obfd)->segment_map = mfirst;
3142
3143 return true;
3144}
3145
fd0198f0
ILT
3146/* Copy private section information. This copies over the entsize
3147 field, and sometimes the info field. */
3148
3149boolean
3150_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3151 bfd *ibfd;
3152 asection *isec;
3153 bfd *obfd;
3154 asection *osec;
3155{
3156 Elf_Internal_Shdr *ihdr, *ohdr;
3157
3158 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3159 || obfd->xvec->flavour != bfd_target_elf_flavour)
3160 return true;
3161
3dbf33ee
ILT
3162 /* Copy over private BFD data if it has not already been copied.
3163 This must be done here, rather than in the copy_private_bfd_data
3164 entry point, because the latter is called after the section
3165 contents have been set, which means that the program headers have
3166 already been worked out. */
3167 if (elf_tdata (obfd)->segment_map == NULL
3168 && elf_tdata (ibfd)->phdr != NULL)
3169 {
3170 asection *s;
3171
3172 /* Only set up the segments when all the sections have been set
3173 up. */
3174 for (s = ibfd->sections; s != NULL; s = s->next)
3175 if (s->output_section == NULL)
3176 break;
3177 if (s == NULL)
3178 {
3179 if (! copy_private_bfd_data (ibfd, obfd))
3180 return false;
3181 }
3182 }
3183
fd0198f0
ILT
3184 ihdr = &elf_section_data (isec)->this_hdr;
3185 ohdr = &elf_section_data (osec)->this_hdr;
3186
3187 ohdr->sh_entsize = ihdr->sh_entsize;
3188
3189 if (ihdr->sh_type == SHT_SYMTAB
3190 || ihdr->sh_type == SHT_DYNSYM)
3191 ohdr->sh_info = ihdr->sh_info;
3192
3193 return true;
3194}
3195
3196/* Copy private symbol information. If this symbol is in a section
3197 which we did not map into a BFD section, try to map the section
3198 index correctly. We use special macro definitions for the mapped
3199 section indices; these definitions are interpreted by the
3200 swap_out_syms function. */
3201
3202#define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3203#define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3204#define MAP_STRTAB (SHN_LORESERVE - 3)
3205#define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3206
3207boolean
3208_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3209 bfd *ibfd;
3210 asymbol *isymarg;
3211 bfd *obfd;
3212 asymbol *osymarg;
3213{
3214 elf_symbol_type *isym, *osym;
3215
efc2b064
JL
3216 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3217 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3218 return true;
3219
fd0198f0
ILT
3220 isym = elf_symbol_from (ibfd, isymarg);
3221 osym = elf_symbol_from (obfd, osymarg);
3222
3223 if (isym != NULL
3224 && osym != NULL
3225 && bfd_is_abs_section (isym->symbol.section))
3226 {
3227 unsigned int shndx;
3228
3229 shndx = isym->internal_elf_sym.st_shndx;
3230 if (shndx == elf_onesymtab (ibfd))
3231 shndx = MAP_ONESYMTAB;
3232 else if (shndx == elf_dynsymtab (ibfd))
3233 shndx = MAP_DYNSYMTAB;
3234 else if (shndx == elf_tdata (ibfd)->strtab_section)
3235 shndx = MAP_STRTAB;
3236 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3237 shndx = MAP_SHSTRTAB;
3238 osym->internal_elf_sym.st_shndx = shndx;
3239 }
3240
3241 return true;
3242}
3243
3244/* Swap out the symbols. */
3245
ede4eed4
KR
3246static boolean
3247swap_out_syms (abfd, sttp)
3248 bfd *abfd;
3249 struct bfd_strtab_hash **sttp;
3250{
3251 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3252
3253 if (!elf_map_symbols (abfd))
3254 return false;
3255
3256 /* Dump out the symtabs. */
3257 {
3258 int symcount = bfd_get_symcount (abfd);
3259 asymbol **syms = bfd_get_outsymbols (abfd);
3260 struct bfd_strtab_hash *stt;
3261 Elf_Internal_Shdr *symtab_hdr;
3262 Elf_Internal_Shdr *symstrtab_hdr;
3263 char *outbound_syms;
3264 int idx;
3265
3266 stt = _bfd_elf_stringtab_init ();
3267 if (stt == NULL)
3268 return false;
3269
3270 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3271 symtab_hdr->sh_type = SHT_SYMTAB;
3272 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
3273 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
3274 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
3275 symtab_hdr->sh_addralign = bed->s->file_align;
3276
3277 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3278 symstrtab_hdr->sh_type = SHT_STRTAB;
3279
3280 outbound_syms = bfd_alloc (abfd,
3281 (1 + symcount) * bed->s->sizeof_sym);
3282 if (outbound_syms == NULL)
a9713b91 3283 return false;
ede4eed4
KR
3284 symtab_hdr->contents = (PTR) outbound_syms;
3285
3286 /* now generate the data (for "contents") */
3287 {
3288 /* Fill in zeroth symbol and swap it out. */
3289 Elf_Internal_Sym sym;
3290 sym.st_name = 0;
3291 sym.st_value = 0;
3292 sym.st_size = 0;
3293 sym.st_info = 0;
3294 sym.st_other = 0;
3295 sym.st_shndx = SHN_UNDEF;
cf9fb9f2 3296 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
ede4eed4
KR
3297 outbound_syms += bed->s->sizeof_sym;
3298 }
3299 for (idx = 0; idx < symcount; idx++)
3300 {
3301 Elf_Internal_Sym sym;
3302 bfd_vma value = syms[idx]->value;
3303 elf_symbol_type *type_ptr;
3304 flagword flags = syms[idx]->flags;
052b35d2 3305 int type;
ede4eed4
KR
3306
3307 if (flags & BSF_SECTION_SYM)
3308 /* Section symbols have no names. */
3309 sym.st_name = 0;
3310 else
3311 {
3312 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3313 syms[idx]->name,
3314 true, false);
3315 if (sym.st_name == (unsigned long) -1)
3316 return false;
3317 }
3318
3319 type_ptr = elf_symbol_from (abfd, syms[idx]);
3320
3321 if (bfd_is_com_section (syms[idx]->section))
3322 {
3323 /* ELF common symbols put the alignment into the `value' field,
3324 and the size into the `size' field. This is backwards from
3325 how BFD handles it, so reverse it here. */
3326 sym.st_size = value;
3327 if (type_ptr == NULL
3328 || type_ptr->internal_elf_sym.st_value == 0)
3329 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3330 else
3331 sym.st_value = type_ptr->internal_elf_sym.st_value;
3332 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
3333 syms[idx]->section);
3334 }
3335 else
3336 {
3337 asection *sec = syms[idx]->section;
3338 int shndx;
3339
3340 if (sec->output_section)
3341 {
3342 value += sec->output_offset;
3343 sec = sec->output_section;
3344 }
3345 value += sec->vma;
3346 sym.st_value = value;
3347 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
fd0198f0
ILT
3348
3349 if (bfd_is_abs_section (sec)
3350 && type_ptr != NULL
3351 && type_ptr->internal_elf_sym.st_shndx != 0)
ede4eed4 3352 {
fd0198f0
ILT
3353 /* This symbol is in a real ELF section which we did
3354 not create as a BFD section. Undo the mapping done
3355 by copy_private_symbol_data. */
3356 shndx = type_ptr->internal_elf_sym.st_shndx;
3357 switch (shndx)
3358 {
3359 case MAP_ONESYMTAB:
3360 shndx = elf_onesymtab (abfd);
3361 break;
3362 case MAP_DYNSYMTAB:
3363 shndx = elf_dynsymtab (abfd);
3364 break;
3365 case MAP_STRTAB:
3366 shndx = elf_tdata (abfd)->strtab_section;
3367 break;
3368 case MAP_SHSTRTAB:
3369 shndx = elf_tdata (abfd)->shstrtab_section;
3370 break;
3371 default:
3372 break;
3373 }
3374 }
3375 else
3376 {
3377 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3378
3379 if (shndx == -1)
3380 {
3381 asection *sec2;
3382
3383 /* Writing this would be a hell of a lot easier if
3384 we had some decent documentation on bfd, and
3385 knew what to expect of the library, and what to
3386 demand of applications. For example, it
3387 appears that `objcopy' might not set the
3388 section of a symbol to be a section that is
3389 actually in the output file. */
3390 sec2 = bfd_get_section_by_name (abfd, sec->name);
3391 BFD_ASSERT (sec2 != 0);
3392 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3393 BFD_ASSERT (shndx != -1);
3394 }
ede4eed4 3395 }
fd0198f0
ILT
3396
3397 sym.st_shndx = shndx;
ede4eed4
KR
3398 }
3399
052b35d2
ILT
3400 if ((flags & BSF_FUNCTION) != 0)
3401 type = STT_FUNC;
3402 else if ((flags & BSF_OBJECT) != 0)
3403 type = STT_OBJECT;
3404 else
3405 type = STT_NOTYPE;
3406
ede4eed4 3407 if (bfd_is_com_section (syms[idx]->section))
052b35d2 3408 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
ede4eed4
KR
3409 else if (bfd_is_und_section (syms[idx]->section))
3410 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
3411 ? STB_WEAK
3412 : STB_GLOBAL),
052b35d2 3413 type);
ede4eed4
KR
3414 else if (flags & BSF_SECTION_SYM)
3415 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3416 else if (flags & BSF_FILE)
3417 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3418 else
3419 {
3420 int bind = STB_LOCAL;
ede4eed4
KR
3421
3422 if (flags & BSF_LOCAL)
3423 bind = STB_LOCAL;
3424 else if (flags & BSF_WEAK)
3425 bind = STB_WEAK;
3426 else if (flags & BSF_GLOBAL)
3427 bind = STB_GLOBAL;
3428
ede4eed4
KR
3429 sym.st_info = ELF_ST_INFO (bind, type);
3430 }
3431
80be821d
ILT
3432 if (type_ptr != NULL)
3433 sym.st_other = type_ptr->internal_elf_sym.st_other;
3434 else
3435 sym.st_other = 0;
3436
cf9fb9f2 3437 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
ede4eed4
KR
3438 outbound_syms += bed->s->sizeof_sym;
3439 }
3440
3441 *sttp = stt;
3442 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
3443 symstrtab_hdr->sh_type = SHT_STRTAB;
3444
3445 symstrtab_hdr->sh_flags = 0;
3446 symstrtab_hdr->sh_addr = 0;
3447 symstrtab_hdr->sh_entsize = 0;
3448 symstrtab_hdr->sh_link = 0;
3449 symstrtab_hdr->sh_info = 0;
3450 symstrtab_hdr->sh_addralign = 1;
3451 }
3452
3453 return true;
3454}
3455
3456/* Return the number of bytes required to hold the symtab vector.
3457
3458 Note that we base it on the count plus 1, since we will null terminate
3459 the vector allocated based on this size. However, the ELF symbol table
3460 always has a dummy entry as symbol #0, so it ends up even. */
3461
3462long
3463_bfd_elf_get_symtab_upper_bound (abfd)
3464 bfd *abfd;
3465{
3466 long symcount;
3467 long symtab_size;
3468 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
3469
3470 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3471 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3472
3473 return symtab_size;
3474}
3475
3476long
3477_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
3478 bfd *abfd;
3479{
3480 long symcount;
3481 long symtab_size;
3482 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3483
3484 if (elf_dynsymtab (abfd) == 0)
3485 {
3486 bfd_set_error (bfd_error_invalid_operation);
3487 return -1;
3488 }
3489
3490 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3491 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3492
3493 return symtab_size;
3494}
3495
3496long
3497_bfd_elf_get_reloc_upper_bound (abfd, asect)
3498 bfd *abfd;
3499 sec_ptr asect;
3500{
3501 return (asect->reloc_count + 1) * sizeof (arelent *);
3502}
3503
3504/* Canonicalize the relocs. */
3505
3506long
3507_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
3508 bfd *abfd;
3509 sec_ptr section;
3510 arelent **relptr;
3511 asymbol **symbols;
3512{
3513 arelent *tblptr;
3514 unsigned int i;
3515
e35765a9
ILT
3516 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
3517 section,
3518 symbols,
3519 false))
ede4eed4
KR
3520 return -1;
3521
3522 tblptr = section->relocation;
3523 for (i = 0; i < section->reloc_count; i++)
3524 *relptr++ = tblptr++;
3525
3526 *relptr = NULL;
3527
3528 return section->reloc_count;
3529}
3530
3531long
3532_bfd_elf_get_symtab (abfd, alocation)
3533 bfd *abfd;
3534 asymbol **alocation;
3535{
3536 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
3537
3538 if (symcount >= 0)
3539 bfd_get_symcount (abfd) = symcount;
3540 return symcount;
3541}
3542
3543long
3544_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
3545 bfd *abfd;
3546 asymbol **alocation;
3547{
3548 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
3549}
3550
e35765a9
ILT
3551/* Return the size required for the dynamic reloc entries. Any
3552 section that was actually installed in the BFD, and has type
3553 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
3554 considered to be a dynamic reloc section. */
3555
3556long
3557_bfd_elf_get_dynamic_reloc_upper_bound (abfd)
3558 bfd *abfd;
3559{
3560 long ret;
3561 asection *s;
3562
3563 if (elf_dynsymtab (abfd) == 0)
3564 {
3565 bfd_set_error (bfd_error_invalid_operation);
3566 return -1;
3567 }
3568
3569 ret = sizeof (arelent *);
3570 for (s = abfd->sections; s != NULL; s = s->next)
3571 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
3572 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
3573 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3574 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
3575 * sizeof (arelent *));
3576
3577 return ret;
3578}
3579
3580/* Canonicalize the dynamic relocation entries. Note that we return
3581 the dynamic relocations as a single block, although they are
3582 actually associated with particular sections; the interface, which
3583 was designed for SunOS style shared libraries, expects that there
3584 is only one set of dynamic relocs. Any section that was actually
3585 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
3586 the dynamic symbol table, is considered to be a dynamic reloc
3587 section. */
3588
3589long
3590_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
3591 bfd *abfd;
3592 arelent **storage;
3593 asymbol **syms;
3594{
3595 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
3596 asection *s;
3597 long ret;
3598
3599 if (elf_dynsymtab (abfd) == 0)
3600 {
3601 bfd_set_error (bfd_error_invalid_operation);
3602 return -1;
3603 }
3604
3605 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3606 ret = 0;
3607 for (s = abfd->sections; s != NULL; s = s->next)
3608 {
3609 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
3610 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
3611 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3612 {
3613 arelent *p;
3614 long count, i;
3615
3616 if (! (*slurp_relocs) (abfd, s, syms, true))
3617 return -1;
3618 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
3619 p = s->relocation;
3620 for (i = 0; i < count; i++)
3621 *storage++ = p++;
3622 ret += count;
3623 }
3624 }
3625
3626 *storage = NULL;
3627
3628 return ret;
3629}
a66a61a0
ILT
3630\f
3631/* Read in the version information. */
3632
3633boolean
3634_bfd_elf_slurp_version_tables (abfd)
3635 bfd *abfd;
3636{
3637 bfd_byte *contents = NULL;
3638
3639 if (elf_dynverdef (abfd) != 0)
3640 {
3641 Elf_Internal_Shdr *hdr;
3642 Elf_External_Verdef *everdef;
3643 Elf_Internal_Verdef *iverdef;
3644 unsigned int i;
3645
3646 hdr = &elf_tdata (abfd)->dynverdef_hdr;
3647
3648 elf_tdata (abfd)->verdef =
3649 ((Elf_Internal_Verdef *)
3650 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
3651 if (elf_tdata (abfd)->verdef == NULL)
3652 goto error_return;
3653
3654 elf_tdata (abfd)->cverdefs = hdr->sh_info;
e35765a9 3655
a66a61a0
ILT
3656 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3657 if (contents == NULL)
3658 goto error_return;
3659 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3660 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
3661 goto error_return;
3662
3663 everdef = (Elf_External_Verdef *) contents;
3664 iverdef = elf_tdata (abfd)->verdef;
3665 for (i = 0; i < hdr->sh_info; i++, iverdef++)
3666 {
3667 Elf_External_Verdaux *everdaux;
3668 Elf_Internal_Verdaux *iverdaux;
3669 unsigned int j;
3670
3671 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
3672
3673 iverdef->vd_bfd = abfd;
3674
3675 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
3676 bfd_alloc (abfd,
3677 (iverdef->vd_cnt
3678 * sizeof (Elf_Internal_Verdaux))));
3679 if (iverdef->vd_auxptr == NULL)
3680 goto error_return;
3681
3682 everdaux = ((Elf_External_Verdaux *)
3683 ((bfd_byte *) everdef + iverdef->vd_aux));
3684 iverdaux = iverdef->vd_auxptr;
3685 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
3686 {
3687 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
3688
3689 iverdaux->vda_nodename =
3690 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3691 iverdaux->vda_name);
3692 if (iverdaux->vda_nodename == NULL)
3693 goto error_return;
3694
3695 if (j + 1 < iverdef->vd_cnt)
3696 iverdaux->vda_nextptr = iverdaux + 1;
3697 else
3698 iverdaux->vda_nextptr = NULL;
3699
3700 everdaux = ((Elf_External_Verdaux *)
3701 ((bfd_byte *) everdaux + iverdaux->vda_next));
3702 }
3703
3704 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
3705
3706 if (i + 1 < hdr->sh_info)
3707 iverdef->vd_nextdef = iverdef + 1;
3708 else
3709 iverdef->vd_nextdef = NULL;
3710
3711 everdef = ((Elf_External_Verdef *)
3712 ((bfd_byte *) everdef + iverdef->vd_next));
3713 }
3714
3715 free (contents);
3716 contents = NULL;
3717 }
3718
3719 if (elf_dynverref (abfd) != 0)
3720 {
3721 Elf_Internal_Shdr *hdr;
3722 Elf_External_Verneed *everneed;
3723 Elf_Internal_Verneed *iverneed;
3724 unsigned int i;
3725
3726 hdr = &elf_tdata (abfd)->dynverref_hdr;
3727
3728 elf_tdata (abfd)->verref =
3729 ((Elf_Internal_Verneed *)
3730 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
3731 if (elf_tdata (abfd)->verref == NULL)
3732 goto error_return;
3733
3734 elf_tdata (abfd)->cverrefs = hdr->sh_info;
3735
3736 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
3737 if (contents == NULL)
3738 goto error_return;
3739 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3740 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
3741 goto error_return;
3742
3743 everneed = (Elf_External_Verneed *) contents;
3744 iverneed = elf_tdata (abfd)->verref;
3745 for (i = 0; i < hdr->sh_info; i++, iverneed++)
3746 {
3747 Elf_External_Vernaux *evernaux;
3748 Elf_Internal_Vernaux *ivernaux;
3749 unsigned int j;
3750
3751 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
3752
3753 iverneed->vn_bfd = abfd;
3754
3755 iverneed->vn_filename =
3756 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3757 iverneed->vn_file);
3758 if (iverneed->vn_filename == NULL)
3759 goto error_return;
3760
3761 iverneed->vn_auxptr =
3762 ((Elf_Internal_Vernaux *)
3763 bfd_alloc (abfd,
3764 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
3765
3766 evernaux = ((Elf_External_Vernaux *)
3767 ((bfd_byte *) everneed + iverneed->vn_aux));
3768 ivernaux = iverneed->vn_auxptr;
3769 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
3770 {
3771 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
3772
3773 ivernaux->vna_nodename =
3774 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
3775 ivernaux->vna_name);
3776 if (ivernaux->vna_nodename == NULL)
3777 goto error_return;
3778
3779 if (j + 1 < iverneed->vn_cnt)
3780 ivernaux->vna_nextptr = ivernaux + 1;
3781 else
3782 ivernaux->vna_nextptr = NULL;
3783
3784 evernaux = ((Elf_External_Vernaux *)
3785 ((bfd_byte *) evernaux + ivernaux->vna_next));
3786 }
3787
3788 if (i + 1 < hdr->sh_info)
3789 iverneed->vn_nextref = iverneed + 1;
3790 else
3791 iverneed->vn_nextref = NULL;
3792
3793 everneed = ((Elf_External_Verneed *)
3794 ((bfd_byte *) everneed + iverneed->vn_next));
3795 }
3796
3797 free (contents);
3798 contents = NULL;
3799 }
3800
3801 return true;
3802
3803 error_return:
3804 if (contents == NULL)
3805 free (contents);
3806 return false;
3807}
3808\f
ede4eed4
KR
3809asymbol *
3810_bfd_elf_make_empty_symbol (abfd)
3811 bfd *abfd;
3812{
3813 elf_symbol_type *newsym;
3814
3815 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3816 if (!newsym)
a9713b91 3817 return NULL;
ede4eed4
KR
3818 else
3819 {
3820 newsym->symbol.the_bfd = abfd;
3821 return &newsym->symbol;
3822 }
3823}
3824
3825void
3826_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
3827 bfd *ignore_abfd;
3828 asymbol *symbol;
3829 symbol_info *ret;
3830{
3831 bfd_symbol_info (symbol, ret);
3832}
3833
a66a61a0
ILT
3834/* Return whether a symbol name implies a local symbol. In ELF, local
3835 symbols generally start with ``.L''. Most targets use this
3836 function for the is_local_label_name entry point, but some override
3837 it. */
3838
3839boolean
3840_bfd_elf_is_local_label_name (abfd, name)
3841 bfd *abfd;
3842 const char *name;
3843{
3844 return name[0] == '.' && name[1] == 'L';
3845}
3846
ede4eed4
KR
3847alent *
3848_bfd_elf_get_lineno (ignore_abfd, symbol)
3849 bfd *ignore_abfd;
3850 asymbol *symbol;
3851{
8cd2f4fe 3852 abort ();
ede4eed4
KR
3853 return NULL;
3854}
3855
3856boolean
3857_bfd_elf_set_arch_mach (abfd, arch, machine)
3858 bfd *abfd;
3859 enum bfd_architecture arch;
3860 unsigned long machine;
3861{
3862 /* If this isn't the right architecture for this backend, and this
3863 isn't the generic backend, fail. */
3864 if (arch != get_elf_backend_data (abfd)->arch
3865 && arch != bfd_arch_unknown
3866 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3867 return false;
3868
3869 return bfd_default_set_arch_mach (abfd, arch, machine);
3870}
3871
6f904fce
ILT
3872/* Find the nearest line to a particular section and offset, for error
3873 reporting. */
3874
ede4eed4
KR
3875boolean
3876_bfd_elf_find_nearest_line (abfd,
6f904fce
ILT
3877 section,
3878 symbols,
3879 offset,
3880 filename_ptr,
3881 functionname_ptr,
3882 line_ptr)
ede4eed4
KR
3883 bfd *abfd;
3884 asection *section;
3885 asymbol **symbols;
3886 bfd_vma offset;
3887 CONST char **filename_ptr;
3888 CONST char **functionname_ptr;
3889 unsigned int *line_ptr;
3890{
86aac8ea 3891 boolean found;
6f904fce
ILT
3892 const char *filename;
3893 asymbol *func;
86aac8ea 3894 bfd_vma low_func;
6f904fce
ILT
3895 asymbol **p;
3896
86aac8ea
ILT
3897 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3898 &found, filename_ptr,
3899 functionname_ptr, line_ptr,
3900 &elf_tdata (abfd)->line_info))
3901 return false;
3902 if (found)
3903 return true;
3904
6f904fce
ILT
3905 if (symbols == NULL)
3906 return false;
3907
3908 filename = NULL;
3909 func = NULL;
86aac8ea 3910 low_func = 0;
6f904fce
ILT
3911
3912 for (p = symbols; *p != NULL; p++)
3913 {
3914 elf_symbol_type *q;
3915
3916 q = (elf_symbol_type *) *p;
3917
3918 if (bfd_get_section (&q->symbol) != section)
3919 continue;
3920
3921 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3922 {
3923 default:
3924 break;
3925 case STT_FILE:
3926 filename = bfd_asymbol_name (&q->symbol);
3927 break;
3928 case STT_FUNC:
86aac8ea
ILT
3929 if (q->symbol.section == section
3930 && q->symbol.value >= low_func
3931 && q->symbol.value <= offset)
3932 {
3933 func = (asymbol *) q;
3934 low_func = q->symbol.value;
3935 }
6f904fce
ILT
3936 break;
3937 }
3938 }
3939
3940 if (func == NULL)
3941 return false;
3942
3943 *filename_ptr = filename;
3944 *functionname_ptr = bfd_asymbol_name (func);
3945 *line_ptr = 0;
3946 return true;
ede4eed4
KR
3947}
3948
3949int
3950_bfd_elf_sizeof_headers (abfd, reloc)
3951 bfd *abfd;
3952 boolean reloc;
3953{
3954 int ret;
3955
3956 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
3957 if (! reloc)
fd0198f0 3958 ret += get_program_header_size (abfd);
ede4eed4
KR
3959 return ret;
3960}
3961
3962boolean
3963_bfd_elf_set_section_contents (abfd, section, location, offset, count)
3964 bfd *abfd;
3965 sec_ptr section;
3966 PTR location;
3967 file_ptr offset;
3968 bfd_size_type count;
3969{
3970 Elf_Internal_Shdr *hdr;
3971
3972 if (! abfd->output_has_begun
3973 && ! _bfd_elf_compute_section_file_positions (abfd,
3974 (struct bfd_link_info *) NULL))
3975 return false;
3976
3977 hdr = &elf_section_data (section)->this_hdr;
3978
3979 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3980 return false;
3981 if (bfd_write (location, 1, count, abfd) != count)
3982 return false;
3983
3984 return true;
3985}
3986
3987void
3988_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
3989 bfd *abfd;
3990 arelent *cache_ptr;
3991 Elf_Internal_Rela *dst;
3992{
8cd2f4fe 3993 abort ();
ede4eed4
KR
3994}
3995
3996#if 0
3997void
3998_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3999 bfd *abfd;
4000 arelent *cache_ptr;
4001 Elf_Internal_Rel *dst;
4002{
8cd2f4fe 4003 abort ();
ede4eed4
KR
4004}
4005#endif
7fc6a16a
ILT
4006
4007/* Try to convert a non-ELF reloc into an ELF one. */
4008
4009boolean
4010_bfd_elf_validate_reloc (abfd, areloc)
4011 bfd *abfd;
4012 arelent *areloc;
4013{
4014 /* Check whether we really have an ELF howto. */
4015
4016 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4017 {
4018 bfd_reloc_code_real_type code;
4019 reloc_howto_type *howto;
4020
4021 /* Alien reloc: Try to determine its type to replace it with an
4022 equivalent ELF reloc. */
4023
4024 if (areloc->howto->pc_relative)
4025 {
4026 switch (areloc->howto->bitsize)
4027 {
4028 case 8:
4029 code = BFD_RELOC_8_PCREL;
4030 break;
4031 case 12:
4032 code = BFD_RELOC_12_PCREL;
4033 break;
4034 case 16:
4035 code = BFD_RELOC_16_PCREL;
4036 break;
4037 case 24:
4038 code = BFD_RELOC_24_PCREL;
4039 break;
4040 case 32:
4041 code = BFD_RELOC_32_PCREL;
4042 break;
4043 case 64:
4044 code = BFD_RELOC_64_PCREL;
4045 break;
4046 default:
4047 goto fail;
4048 }
4049
4050 howto = bfd_reloc_type_lookup (abfd, code);
4051
4052 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4053 {
4054 if (howto->pcrel_offset)
4055 areloc->addend += areloc->address;
4056 else
4057 areloc->addend -= areloc->address; /* addend is unsigned!! */
4058 }
4059 }
4060 else
4061 {
4062 switch (areloc->howto->bitsize)
4063 {
4064 case 8:
4065 code = BFD_RELOC_8;
4066 break;
4067 case 14:
4068 code = BFD_RELOC_14;
4069 break;
4070 case 16:
4071 code = BFD_RELOC_16;
4072 break;
4073 case 26:
4074 code = BFD_RELOC_26;
4075 break;
4076 case 32:
4077 code = BFD_RELOC_32;
4078 break;
4079 case 64:
4080 code = BFD_RELOC_64;
4081 break;
4082 default:
4083 goto fail;
4084 }
4085
4086 howto = bfd_reloc_type_lookup (abfd, code);
4087 }
4088
4089 if (howto)
4090 areloc->howto = howto;
4091 else
4092 goto fail;
4093 }
4094
4095 return true;
4096
4097 fail:
4098 (*_bfd_error_handler)
4099 ("%s: unsupported relocation type %s",
4100 bfd_get_filename (abfd), areloc->howto->name);
4101 bfd_set_error (bfd_error_bad_value);
4102 return false;
4103}
This page took 0.312588 seconds and 4 git commands to generate.