* config/tc-h8300.c (do_a_fix_imm): Rename last argument to
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
32090b8e 1/* ELF executable support for BFD.
6014cea7 2 Copyright 1993, 1994, 1995, 1996 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));
fd0198f0
ILT
43static int elf_sort_sections PARAMS ((const PTR, const PTR));
44static boolean assign_file_positions_for_segments PARAMS ((bfd *));
45static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
ede4eed4
KR
46static boolean prep_headers PARAMS ((bfd *));
47static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **));
3dbf33ee 48static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
ede4eed4 49
32090b8e
KR
50/* Standard ELF hash function. Do not change this function; you will
51 cause invalid hash tables to be generated. (Well, you would if this
52 were being used yet.) */
53unsigned long
013dec1a
ILT
54bfd_elf_hash (name)
55 CONST unsigned char *name;
32090b8e
KR
56{
57 unsigned long h = 0;
58 unsigned long g;
59 int ch;
60
61 while ((ch = *name++) != '\0')
62 {
63 h = (h << 4) + ch;
64 if ((g = (h & 0xf0000000)) != 0)
65 {
66 h ^= g >> 24;
67 h &= ~g;
68 }
69 }
70 return h;
71}
72
73/* Read a specified number of bytes at a specified offset in an ELF
74 file, into a newly allocated buffer, and return a pointer to the
75 buffer. */
76
77static char *
013dec1a
ILT
78elf_read (abfd, offset, size)
79 bfd * abfd;
80 long offset;
ae115e51 81 unsigned int size;
32090b8e
KR
82{
83 char *buf;
84
85 if ((buf = bfd_alloc (abfd, size)) == NULL)
a9713b91 86 return NULL;
32090b8e 87 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
013dec1a 88 return NULL;
32090b8e
KR
89 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
90 {
013dec1a
ILT
91 if (bfd_get_error () != bfd_error_system_call)
92 bfd_set_error (bfd_error_file_truncated);
32090b8e
KR
93 return NULL;
94 }
95 return buf;
96}
97
98boolean
013dec1a
ILT
99elf_mkobject (abfd)
100 bfd * abfd;
32090b8e
KR
101{
102 /* this just does initialization */
103 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
104 elf_tdata (abfd) = (struct elf_obj_tdata *)
105 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
106 if (elf_tdata (abfd) == 0)
a9713b91 107 return false;
32090b8e
KR
108 /* since everything is done at close time, do we need any
109 initialization? */
110
111 return true;
112}
113
114char *
ede4eed4 115bfd_elf_get_str_section (abfd, shindex)
013dec1a
ILT
116 bfd * abfd;
117 unsigned int shindex;
32090b8e
KR
118{
119 Elf_Internal_Shdr **i_shdrp;
120 char *shstrtab = NULL;
121 unsigned int offset;
122 unsigned int shstrtabsize;
123
124 i_shdrp = elf_elfsections (abfd);
125 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
126 return 0;
127
b176e1e9 128 shstrtab = (char *) i_shdrp[shindex]->contents;
32090b8e
KR
129 if (shstrtab == NULL)
130 {
131 /* No cached one, attempt to read, and cache what we read. */
132 offset = i_shdrp[shindex]->sh_offset;
133 shstrtabsize = i_shdrp[shindex]->sh_size;
134 shstrtab = elf_read (abfd, offset, shstrtabsize);
b176e1e9 135 i_shdrp[shindex]->contents = (PTR) shstrtab;
32090b8e
KR
136 }
137 return shstrtab;
138}
139
140char *
ede4eed4 141bfd_elf_string_from_elf_section (abfd, shindex, strindex)
013dec1a
ILT
142 bfd * abfd;
143 unsigned int shindex;
144 unsigned int strindex;
32090b8e
KR
145{
146 Elf_Internal_Shdr *hdr;
147
148 if (strindex == 0)
149 return "";
150
151 hdr = elf_elfsections (abfd)[shindex];
152
b176e1e9 153 if (hdr->contents == NULL
ede4eed4 154 && bfd_elf_get_str_section (abfd, shindex) == NULL)
32090b8e
KR
155 return NULL;
156
b176e1e9 157 return ((char *) hdr->contents) + strindex;
32090b8e
KR
158}
159
497c5434 160/* Make a BFD section from an ELF section. We store a pointer to the
b176e1e9 161 BFD section in the bfd_section field of the header. */
497c5434
ILT
162
163boolean
164_bfd_elf_make_section_from_shdr (abfd, hdr, name)
165 bfd *abfd;
166 Elf_Internal_Shdr *hdr;
167 const char *name;
168{
169 asection *newsect;
170 flagword flags;
171
b176e1e9 172 if (hdr->bfd_section != NULL)
497c5434 173 {
b176e1e9
ILT
174 BFD_ASSERT (strcmp (name,
175 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
497c5434
ILT
176 return true;
177 }
178
179 newsect = bfd_make_section_anyway (abfd, name);
180 if (newsect == NULL)
181 return false;
182
183 newsect->filepos = hdr->sh_offset;
184
185 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
186 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
187 || ! bfd_set_section_alignment (abfd, newsect,
188 bfd_log2 (hdr->sh_addralign)))
189 return false;
190
191 flags = SEC_NO_FLAGS;
192 if (hdr->sh_type != SHT_NOBITS)
193 flags |= SEC_HAS_CONTENTS;
194 if ((hdr->sh_flags & SHF_ALLOC) != 0)
195 {
196 flags |= SEC_ALLOC;
197 if (hdr->sh_type != SHT_NOBITS)
198 flags |= SEC_LOAD;
199 }
200 if ((hdr->sh_flags & SHF_WRITE) == 0)
201 flags |= SEC_READONLY;
202 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
203 flags |= SEC_CODE;
7c6da9ca 204 else if ((flags & SEC_LOAD) != 0)
497c5434
ILT
205 flags |= SEC_DATA;
206
207 /* The debugging sections appear to be recognized only by name, not
208 any sort of flag. */
209 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
210 || strncmp (name, ".line", sizeof ".line" - 1) == 0
211 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
212 flags |= SEC_DEBUGGING;
213
214 if (! bfd_set_section_flags (abfd, newsect, flags))
215 return false;
216
fd0198f0
ILT
217 if ((flags & SEC_ALLOC) != 0)
218 {
219 Elf_Internal_Phdr *phdr;
220 unsigned int i;
221
222 /* Look through the phdrs to see if we need to adjust the lma. */
223 phdr = elf_tdata (abfd)->phdr;
224 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
225 {
226 if (phdr->p_type == PT_LOAD
6933148a 227 && phdr->p_paddr != 0
fd0198f0
ILT
228 && phdr->p_vaddr != phdr->p_paddr
229 && phdr->p_vaddr <= hdr->sh_addr
230 && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size)
231 {
232 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
233 break;
234 }
235 }
236 }
237
b176e1e9 238 hdr->bfd_section = newsect;
497c5434
ILT
239 elf_section_data (newsect)->this_hdr = *hdr;
240
241 return true;
242}
243
32090b8e
KR
244/*
245INTERNAL_FUNCTION
246 bfd_elf_find_section
247
248SYNOPSIS
249 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
250
251DESCRIPTION
252 Helper functions for GDB to locate the string tables.
253 Since BFD hides string tables from callers, GDB needs to use an
254 internal hook to find them. Sun's .stabstr, in particular,
255 isn't even pointed to by the .stab section, so ordinary
256 mechanisms wouldn't work to find it, even if we had some.
257*/
258
259struct elf_internal_shdr *
013dec1a
ILT
260bfd_elf_find_section (abfd, name)
261 bfd * abfd;
262 char *name;
32090b8e
KR
263{
264 Elf_Internal_Shdr **i_shdrp;
265 char *shstrtab;
266 unsigned int max;
267 unsigned int i;
268
269 i_shdrp = elf_elfsections (abfd);
270 if (i_shdrp != NULL)
271 {
ede4eed4 272 shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
32090b8e
KR
273 if (shstrtab != NULL)
274 {
275 max = elf_elfheader (abfd)->e_shnum;
276 for (i = 1; i < max; i++)
277 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
278 return i_shdrp[i];
279 }
280 }
281 return 0;
282}
283
32090b8e
KR
284const char *const bfd_elf_section_type_names[] = {
285 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
286 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
287 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
288};
289
290/* ELF relocs are against symbols. If we are producing relocateable
291 output, and the reloc is against an external symbol, and nothing
292 has given us any additional addend, the resulting reloc will also
293 be against the same symbol. In such a case, we don't want to
294 change anything about the way the reloc is handled, since it will
295 all be done at final link time. Rather than put special case code
296 into bfd_perform_relocation, all the reloc types use this howto
297 function. It just short circuits the reloc if producing
298 relocateable output against an external symbol. */
299
013dec1a 300/*ARGSUSED*/
32090b8e
KR
301bfd_reloc_status_type
302bfd_elf_generic_reloc (abfd,
303 reloc_entry,
304 symbol,
305 data,
306 input_section,
4c3721d5
ILT
307 output_bfd,
308 error_message)
32090b8e
KR
309 bfd *abfd;
310 arelent *reloc_entry;
311 asymbol *symbol;
312 PTR data;
313 asection *input_section;
314 bfd *output_bfd;
4c3721d5 315 char **error_message;
32090b8e
KR
316{
317 if (output_bfd != (bfd *) NULL
318 && (symbol->flags & BSF_SECTION_SYM) == 0
d1b44e83
ILT
319 && (! reloc_entry->howto->partial_inplace
320 || reloc_entry->addend == 0))
32090b8e
KR
321 {
322 reloc_entry->address += input_section->output_offset;
323 return bfd_reloc_ok;
324 }
325
326 return bfd_reloc_continue;
327}
013dec1a 328\f
27fb8f29
ILT
329/* Print out the program headers. */
330
331boolean
332_bfd_elf_print_private_bfd_data (abfd, farg)
333 bfd *abfd;
334 PTR farg;
335{
336 FILE *f = (FILE *) farg;
337 Elf_Internal_Phdr *p;
02fcd126
ILT
338 asection *s;
339 bfd_byte *dynbuf = NULL;
27fb8f29
ILT
340
341 p = elf_tdata (abfd)->phdr;
02fcd126 342 if (p != NULL)
27fb8f29 343 {
02fcd126 344 unsigned int i, c;
27fb8f29 345
02fcd126
ILT
346 fprintf (f, "\nProgram Header:\n");
347 c = elf_elfheader (abfd)->e_phnum;
348 for (i = 0; i < c; i++, p++)
27fb8f29 349 {
02fcd126
ILT
350 const char *s;
351 char buf[20];
352
353 switch (p->p_type)
354 {
355 case PT_NULL: s = "NULL"; break;
356 case PT_LOAD: s = "LOAD"; break;
357 case PT_DYNAMIC: s = "DYNAMIC"; break;
358 case PT_INTERP: s = "INTERP"; break;
359 case PT_NOTE: s = "NOTE"; break;
360 case PT_SHLIB: s = "SHLIB"; break;
361 case PT_PHDR: s = "PHDR"; break;
362 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
363 }
364 fprintf (f, "%8s off 0x", s);
365 fprintf_vma (f, p->p_offset);
366 fprintf (f, " vaddr 0x");
367 fprintf_vma (f, p->p_vaddr);
368 fprintf (f, " paddr 0x");
369 fprintf_vma (f, p->p_paddr);
370 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
371 fprintf (f, " filesz 0x");
372 fprintf_vma (f, p->p_filesz);
373 fprintf (f, " memsz 0x");
374 fprintf_vma (f, p->p_memsz);
375 fprintf (f, " flags %c%c%c",
376 (p->p_flags & PF_R) != 0 ? 'r' : '-',
377 (p->p_flags & PF_W) != 0 ? 'w' : '-',
378 (p->p_flags & PF_X) != 0 ? 'x' : '-');
379 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
380 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
381 fprintf (f, "\n");
382 }
383 }
384
385 s = bfd_get_section_by_name (abfd, ".dynamic");
386 if (s != NULL)
387 {
388 int elfsec;
389 unsigned long link;
390 bfd_byte *extdyn, *extdynend;
391 size_t extdynsize;
392 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
393
394 fprintf (f, "\nDynamic Section:\n");
395
396 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
397 if (dynbuf == NULL)
398 goto error_return;
399 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
400 s->_raw_size))
401 goto error_return;
402
403 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
404 if (elfsec == -1)
405 goto error_return;
406 link = elf_elfsections (abfd)[elfsec]->sh_link;
407
408 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
409 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
410
411 extdyn = dynbuf;
412 extdynend = extdyn + s->_raw_size;
413 for (; extdyn < extdynend; extdyn += extdynsize)
414 {
415 Elf_Internal_Dyn dyn;
416 const char *name;
417 char ab[20];
418 boolean stringp;
419
420 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
421
422 if (dyn.d_tag == DT_NULL)
423 break;
424
425 stringp = false;
426 switch (dyn.d_tag)
427 {
428 default:
927d05b5 429 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
02fcd126
ILT
430 name = ab;
431 break;
432
433 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
434 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
435 case DT_PLTGOT: name = "PLTGOT"; break;
436 case DT_HASH: name = "HASH"; break;
437 case DT_STRTAB: name = "STRTAB"; break;
438 case DT_SYMTAB: name = "SYMTAB"; break;
439 case DT_RELA: name = "RELA"; break;
440 case DT_RELASZ: name = "RELASZ"; break;
441 case DT_RELAENT: name = "RELAENT"; break;
442 case DT_STRSZ: name = "STRSZ"; break;
443 case DT_SYMENT: name = "SYMENT"; break;
444 case DT_INIT: name = "INIT"; break;
445 case DT_FINI: name = "FINI"; break;
446 case DT_SONAME: name = "SONAME"; stringp = true; break;
447 case DT_RPATH: name = "RPATH"; stringp = true; break;
448 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
449 case DT_REL: name = "REL"; break;
450 case DT_RELSZ: name = "RELSZ"; break;
451 case DT_RELENT: name = "RELENT"; break;
452 case DT_PLTREL: name = "PLTREL"; break;
453 case DT_DEBUG: name = "DEBUG"; break;
454 case DT_TEXTREL: name = "TEXTREL"; break;
455 case DT_JMPREL: name = "JMPREL"; break;
456 }
457
458 fprintf (f, " %-11s ", name);
459 if (! stringp)
927d05b5 460 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
02fcd126
ILT
461 else
462 {
463 const char *string;
464
465 string = bfd_elf_string_from_elf_section (abfd, link,
466 dyn.d_un.d_val);
467 if (string == NULL)
468 goto error_return;
469 fprintf (f, "%s", string);
470 }
471 fprintf (f, "\n");
27fb8f29 472 }
02fcd126
ILT
473
474 free (dynbuf);
475 dynbuf = NULL;
27fb8f29
ILT
476 }
477
478 return true;
02fcd126
ILT
479
480 error_return:
481 if (dynbuf != NULL)
482 free (dynbuf);
483 return false;
27fb8f29
ILT
484}
485
b176e1e9
ILT
486/* Display ELF-specific fields of a symbol. */
487void
488bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
489 bfd *ignore_abfd;
490 PTR filep;
491 asymbol *symbol;
492 bfd_print_symbol_type how;
493{
494 FILE *file = (FILE *) filep;
495 switch (how)
496 {
497 case bfd_print_symbol_name:
498 fprintf (file, "%s", symbol->name);
499 break;
500 case bfd_print_symbol_more:
501 fprintf (file, "elf ");
502 fprintf_vma (file, symbol->value);
503 fprintf (file, " %lx", (long) symbol->flags);
504 break;
505 case bfd_print_symbol_all:
506 {
507 CONST char *section_name;
508 section_name = symbol->section ? symbol->section->name : "(*none*)";
509 bfd_print_symbol_vandf ((PTR) file, symbol);
510 fprintf (file, " %s\t", section_name);
511 /* Print the "other" value for a symbol. For common symbols,
512 we've already printed the size; now print the alignment.
513 For other symbols, we have no specified alignment, and
514 we've printed the address; now print the size. */
515 fprintf_vma (file,
516 (bfd_is_com_section (symbol->section)
517 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
518 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
519 fprintf (file, " %s", symbol->name);
520 }
521 break;
522 }
523}
524\f
013dec1a
ILT
525/* Create an entry in an ELF linker hash table. */
526
5315c428
ILT
527struct bfd_hash_entry *
528_bfd_elf_link_hash_newfunc (entry, table, string)
013dec1a
ILT
529 struct bfd_hash_entry *entry;
530 struct bfd_hash_table *table;
531 const char *string;
532{
533 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
534
535 /* Allocate the structure if it has not already been allocated by a
536 subclass. */
537 if (ret == (struct elf_link_hash_entry *) NULL)
538 ret = ((struct elf_link_hash_entry *)
539 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
540 if (ret == (struct elf_link_hash_entry *) NULL)
a9713b91 541 return (struct bfd_hash_entry *) ret;
013dec1a
ILT
542
543 /* Call the allocation method of the superclass. */
544 ret = ((struct elf_link_hash_entry *)
545 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
546 table, string));
547 if (ret != (struct elf_link_hash_entry *) NULL)
548 {
549 /* Set local fields. */
550 ret->indx = -1;
551 ret->size = 0;
013dec1a
ILT
552 ret->dynindx = -1;
553 ret->dynstr_index = 0;
554 ret->weakdef = NULL;
b176e1e9
ILT
555 ret->got_offset = (bfd_vma) -1;
556 ret->plt_offset = (bfd_vma) -1;
86aac8ea 557 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
013dec1a 558 ret->type = STT_NOTYPE;
869b7d80
ILT
559 /* Assume that we have been called by a non-ELF symbol reader.
560 This flag is then reset by the code which reads an ELF input
561 file. This ensures that a symbol created by a non-ELF symbol
562 reader will have the flag set correctly. */
563 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
013dec1a
ILT
564 }
565
566 return (struct bfd_hash_entry *) ret;
567}
568
5315c428
ILT
569/* Initialize an ELF linker hash table. */
570
571boolean
572_bfd_elf_link_hash_table_init (table, abfd, newfunc)
573 struct elf_link_hash_table *table;
574 bfd *abfd;
575 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
576 struct bfd_hash_table *,
577 const char *));
578{
b176e1e9 579 table->dynamic_sections_created = false;
5315c428 580 table->dynobj = NULL;
b176e1e9
ILT
581 /* The first dynamic symbol is a dummy. */
582 table->dynsymcount = 1;
5315c428
ILT
583 table->dynstr = NULL;
584 table->bucketcount = 0;
b176e1e9 585 table->needed = NULL;
19bfbcbe 586 table->hgot = NULL;
5315c428
ILT
587 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
588}
589
013dec1a
ILT
590/* Create an ELF linker hash table. */
591
592struct bfd_link_hash_table *
593_bfd_elf_link_hash_table_create (abfd)
594 bfd *abfd;
595{
596 struct elf_link_hash_table *ret;
597
598 ret = ((struct elf_link_hash_table *)
599 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
600 if (ret == (struct elf_link_hash_table *) NULL)
a9713b91 601 return NULL;
5315c428
ILT
602
603 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
013dec1a
ILT
604 {
605 bfd_release (abfd, ret);
606 return NULL;
607 }
608
013dec1a
ILT
609 return &ret->root;
610}
7c6da9ca
ILT
611
612/* This is a hook for the ELF emulation code in the generic linker to
613 tell the backend linker what file name to use for the DT_NEEDED
b176e1e9
ILT
614 entry for a dynamic object. The generic linker passes name as an
615 empty string to indicate that no DT_NEEDED entry should be made. */
7c6da9ca
ILT
616
617void
618bfd_elf_set_dt_needed_name (abfd, name)
619 bfd *abfd;
620 const char *name;
621{
053ae1d7
ILT
622 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
623 && bfd_get_format (abfd) == bfd_object)
624 elf_dt_name (abfd) = name;
7c6da9ca 625}
b176e1e9 626
053ae1d7
ILT
627/* Get the list of DT_NEEDED entries for a link. This is a hook for
628 the ELF emulation code. */
b176e1e9 629
5fe14a9f 630struct bfd_link_needed_list *
b176e1e9
ILT
631bfd_elf_get_needed_list (abfd, info)
632 bfd *abfd;
633 struct bfd_link_info *info;
634{
b2193cc5
ILT
635 if (info->hash->creator->flavour != bfd_target_elf_flavour)
636 return NULL;
b176e1e9
ILT
637 return elf_hash_table (info)->needed;
638}
053ae1d7
ILT
639
640/* Get the name actually used for a dynamic object for a link. This
641 is the SONAME entry if there is one. Otherwise, it is the string
642 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
643
644const char *
645bfd_elf_get_dt_soname (abfd)
646 bfd *abfd;
647{
648 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
649 && bfd_get_format (abfd) == bfd_object)
650 return elf_dt_name (abfd);
651 return NULL;
652}
ede4eed4
KR
653\f
654/* Allocate an ELF string table--force the first byte to be zero. */
655
656struct bfd_strtab_hash *
657_bfd_elf_stringtab_init ()
658{
659 struct bfd_strtab_hash *ret;
660
661 ret = _bfd_stringtab_init ();
662 if (ret != NULL)
663 {
664 bfd_size_type loc;
665
666 loc = _bfd_stringtab_add (ret, "", true, false);
667 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
668 if (loc == (bfd_size_type) -1)
669 {
670 _bfd_stringtab_free (ret);
671 ret = NULL;
672 }
673 }
674 return ret;
675}
676\f
677/* ELF .o/exec file reading */
678
679/* Create a new bfd section from an ELF section header. */
680
681boolean
682bfd_section_from_shdr (abfd, shindex)
683 bfd *abfd;
684 unsigned int shindex;
685{
686 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
687 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
688 struct elf_backend_data *bed = get_elf_backend_data (abfd);
689 char *name;
690
691 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
692
693 switch (hdr->sh_type)
694 {
695 case SHT_NULL:
696 /* Inactive section. Throw it away. */
697 return true;
698
699 case SHT_PROGBITS: /* Normal section with contents. */
700 case SHT_DYNAMIC: /* Dynamic linking information. */
701 case SHT_NOBITS: /* .bss section. */
702 case SHT_HASH: /* .hash section. */
5b3b9ff6 703 case SHT_NOTE: /* .note section. */
ede4eed4
KR
704 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
705
706 case SHT_SYMTAB: /* A symbol table */
707 if (elf_onesymtab (abfd) == shindex)
708 return true;
709
710 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
711 BFD_ASSERT (elf_onesymtab (abfd) == 0);
712 elf_onesymtab (abfd) = shindex;
713 elf_tdata (abfd)->symtab_hdr = *hdr;
fd0198f0 714 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
ede4eed4
KR
715 abfd->flags |= HAS_SYMS;
716
717 /* Sometimes a shared object will map in the symbol table. If
718 SHF_ALLOC is set, and this is a shared object, then we also
719 treat this section as a BFD section. We can not base the
720 decision purely on SHF_ALLOC, because that flag is sometimes
721 set in a relocateable object file, which would confuse the
722 linker. */
723 if ((hdr->sh_flags & SHF_ALLOC) != 0
724 && (abfd->flags & DYNAMIC) != 0
725 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
726 return false;
727
728 return true;
729
730 case SHT_DYNSYM: /* A dynamic symbol table */
731 if (elf_dynsymtab (abfd) == shindex)
732 return true;
733
734 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
735 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
736 elf_dynsymtab (abfd) = shindex;
737 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
fd0198f0 738 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
ede4eed4
KR
739 abfd->flags |= HAS_SYMS;
740
741 /* Besides being a symbol table, we also treat this as a regular
742 section, so that objcopy can handle it. */
743 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
744
745 case SHT_STRTAB: /* A string table */
746 if (hdr->bfd_section != NULL)
747 return true;
748 if (ehdr->e_shstrndx == shindex)
749 {
750 elf_tdata (abfd)->shstrtab_hdr = *hdr;
751 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
752 return true;
753 }
754 {
755 unsigned int i;
756
757 for (i = 1; i < ehdr->e_shnum; i++)
758 {
759 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
760 if (hdr2->sh_link == shindex)
761 {
762 if (! bfd_section_from_shdr (abfd, i))
763 return false;
764 if (elf_onesymtab (abfd) == i)
765 {
766 elf_tdata (abfd)->strtab_hdr = *hdr;
767 elf_elfsections (abfd)[shindex] =
768 &elf_tdata (abfd)->strtab_hdr;
769 return true;
770 }
771 if (elf_dynsymtab (abfd) == i)
772 {
773 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
fd0198f0 774 elf_elfsections (abfd)[shindex] = hdr =
ede4eed4
KR
775 &elf_tdata (abfd)->dynstrtab_hdr;
776 /* We also treat this as a regular section, so
777 that objcopy can handle it. */
778 break;
779 }
780#if 0 /* Not handling other string tables specially right now. */
781 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
782 /* We have a strtab for some random other section. */
783 newsect = (asection *) hdr2->bfd_section;
784 if (!newsect)
785 break;
786 hdr->bfd_section = newsect;
787 hdr2 = &elf_section_data (newsect)->str_hdr;
788 *hdr2 = *hdr;
789 elf_elfsections (abfd)[shindex] = hdr2;
790#endif
791 }
792 }
793 }
794
795 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
796
797 case SHT_REL:
798 case SHT_RELA:
799 /* *These* do a lot of work -- but build no sections! */
800 {
801 asection *target_sect;
802 Elf_Internal_Shdr *hdr2;
803 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
804
ae115e51
ILT
805 /* For some incomprehensible reason Oracle distributes
806 libraries for Solaris in which some of the objects have
807 bogus sh_link fields. It would be nice if we could just
808 reject them, but, unfortunately, some people need to use
809 them. We scan through the section headers; if we find only
810 one suitable symbol table, we clobber the sh_link to point
811 to it. I hope this doesn't break anything. */
812 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
813 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
814 {
815 int scan;
816 int found;
817
818 found = 0;
819 for (scan = 1; scan < ehdr->e_shnum; scan++)
820 {
821 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
822 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
823 {
824 if (found != 0)
825 {
826 found = 0;
827 break;
828 }
829 found = scan;
830 }
831 }
832 if (found != 0)
833 hdr->sh_link = found;
834 }
835
ede4eed4 836 /* Get the symbol table. */
ae115e51
ILT
837 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
838 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
ede4eed4
KR
839 return false;
840
841 /* If this reloc section does not use the main symbol table we
842 don't treat it as a reloc section. BFD can't adequately
843 represent such a section, so at least for now, we don't
844 try. We just present it as a normal section. */
845 if (hdr->sh_link != elf_onesymtab (abfd))
846 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
847
848 /* Don't allow REL relocations on a machine that uses RELA and
849 vice versa. */
850 /* @@ Actually, the generic ABI does suggest that both might be
851 used in one file. But the four ABI Processor Supplements I
852 have access to right now all specify that only one is used on
853 each of those architectures. It's conceivable that, e.g., a
854 bunch of absolute 32-bit relocs might be more compact in REL
855 form even on a RELA machine... */
856 BFD_ASSERT (use_rela_p
857 ? (hdr->sh_type == SHT_RELA
858 && hdr->sh_entsize == bed->s->sizeof_rela)
859 : (hdr->sh_type == SHT_REL
860 && hdr->sh_entsize == bed->s->sizeof_rel));
861
862 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
863 return false;
864 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
865 if (target_sect == NULL)
866 return false;
867
868 hdr2 = &elf_section_data (target_sect)->rel_hdr;
869 *hdr2 = *hdr;
870 elf_elfsections (abfd)[shindex] = hdr2;
871 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
872 target_sect->flags |= SEC_RELOC;
873 target_sect->relocation = NULL;
874 target_sect->rel_filepos = hdr->sh_offset;
875 abfd->flags |= HAS_RELOC;
876 return true;
877 }
878 break;
879
ede4eed4 880 case SHT_SHLIB:
ede4eed4
KR
881 return true;
882
883 default:
884 /* Check for any processor-specific section types. */
885 {
886 if (bed->elf_backend_section_from_shdr)
887 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
888 }
889 break;
890 }
891
892 return true;
893}
894
895/* Given an ELF section number, retrieve the corresponding BFD
896 section. */
897
898asection *
899bfd_section_from_elf_index (abfd, index)
900 bfd *abfd;
901 unsigned int index;
902{
903 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
904 if (index >= elf_elfheader (abfd)->e_shnum)
905 return NULL;
906 return elf_elfsections (abfd)[index]->bfd_section;
907}
908
909boolean
910_bfd_elf_new_section_hook (abfd, sec)
911 bfd *abfd;
912 asection *sec;
913{
914 struct bfd_elf_section_data *sdata;
915
916 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
917 if (!sdata)
a9713b91 918 return false;
ede4eed4
KR
919 sec->used_by_bfd = (PTR) sdata;
920 memset (sdata, 0, sizeof (*sdata));
921 return true;
922}
923
924/* Create a new bfd section from an ELF program header.
925
926 Since program segments have no names, we generate a synthetic name
927 of the form segment<NUM>, where NUM is generally the index in the
928 program header table. For segments that are split (see below) we
929 generate the names segment<NUM>a and segment<NUM>b.
930
931 Note that some program segments may have a file size that is different than
932 (less than) the memory size. All this means is that at execution the
933 system must allocate the amount of memory specified by the memory size,
934 but only initialize it with the first "file size" bytes read from the
935 file. This would occur for example, with program segments consisting
936 of combined data+bss.
937
938 To handle the above situation, this routine generates TWO bfd sections
939 for the single program segment. The first has the length specified by
940 the file size of the segment, and the second has the length specified
941 by the difference between the two sizes. In effect, the segment is split
942 into it's initialized and uninitialized parts.
943
944 */
945
946boolean
947bfd_section_from_phdr (abfd, hdr, index)
948 bfd *abfd;
949 Elf_Internal_Phdr *hdr;
950 int index;
951{
952 asection *newsect;
953 char *name;
954 char namebuf[64];
955 int split;
956
957 split = ((hdr->p_memsz > 0) &&
958 (hdr->p_filesz > 0) &&
959 (hdr->p_memsz > hdr->p_filesz));
960 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
961 name = bfd_alloc (abfd, strlen (namebuf) + 1);
962 if (!name)
a9713b91 963 return false;
ede4eed4
KR
964 strcpy (name, namebuf);
965 newsect = bfd_make_section (abfd, name);
966 if (newsect == NULL)
967 return false;
968 newsect->vma = hdr->p_vaddr;
ae115e51 969 newsect->lma = hdr->p_paddr;
ede4eed4
KR
970 newsect->_raw_size = hdr->p_filesz;
971 newsect->filepos = hdr->p_offset;
972 newsect->flags |= SEC_HAS_CONTENTS;
973 if (hdr->p_type == PT_LOAD)
974 {
975 newsect->flags |= SEC_ALLOC;
976 newsect->flags |= SEC_LOAD;
977 if (hdr->p_flags & PF_X)
978 {
979 /* FIXME: all we known is that it has execute PERMISSION,
980 may be data. */
981 newsect->flags |= SEC_CODE;
982 }
983 }
984 if (!(hdr->p_flags & PF_W))
985 {
986 newsect->flags |= SEC_READONLY;
987 }
988
989 if (split)
990 {
991 sprintf (namebuf, "segment%db", index);
992 name = bfd_alloc (abfd, strlen (namebuf) + 1);
993 if (!name)
a9713b91 994 return false;
ede4eed4
KR
995 strcpy (name, namebuf);
996 newsect = bfd_make_section (abfd, name);
997 if (newsect == NULL)
998 return false;
999 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
ae115e51 1000 newsect->lma = hdr->p_paddr + hdr->p_filesz;
ede4eed4
KR
1001 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1002 if (hdr->p_type == PT_LOAD)
1003 {
1004 newsect->flags |= SEC_ALLOC;
1005 if (hdr->p_flags & PF_X)
1006 newsect->flags |= SEC_CODE;
1007 }
1008 if (!(hdr->p_flags & PF_W))
1009 newsect->flags |= SEC_READONLY;
1010 }
1011
1012 return true;
1013}
1014
1015/* Set up an ELF internal section header for a section. */
1016
1017/*ARGSUSED*/
1018static void
1019elf_fake_sections (abfd, asect, failedptrarg)
1020 bfd *abfd;
1021 asection *asect;
1022 PTR failedptrarg;
1023{
1024 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1025 boolean *failedptr = (boolean *) failedptrarg;
1026 Elf_Internal_Shdr *this_hdr;
1027
1028 if (*failedptr)
1029 {
1030 /* We already failed; just get out of the bfd_map_over_sections
1031 loop. */
1032 return;
1033 }
1034
1035 this_hdr = &elf_section_data (asect)->this_hdr;
1036
1037 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1038 asect->name,
1039 true, false);
1040 if (this_hdr->sh_name == (unsigned long) -1)
1041 {
1042 *failedptr = true;
1043 return;
1044 }
1045
1046 this_hdr->sh_flags = 0;
ae115e51 1047
ede4eed4 1048 if ((asect->flags & SEC_ALLOC) != 0)
fd0198f0 1049 this_hdr->sh_addr = asect->vma;
ede4eed4
KR
1050 else
1051 this_hdr->sh_addr = 0;
ae115e51 1052
ede4eed4
KR
1053 this_hdr->sh_offset = 0;
1054 this_hdr->sh_size = asect->_raw_size;
1055 this_hdr->sh_link = 0;
ede4eed4 1056 this_hdr->sh_addralign = 1 << asect->alignment_power;
fd0198f0
ILT
1057 /* The sh_entsize and sh_info fields may have been set already by
1058 copy_private_section_data. */
ede4eed4
KR
1059
1060 this_hdr->bfd_section = asect;
1061 this_hdr->contents = NULL;
1062
1063 /* FIXME: This should not be based on section names. */
1064 if (strcmp (asect->name, ".dynstr") == 0)
1065 this_hdr->sh_type = SHT_STRTAB;
1066 else if (strcmp (asect->name, ".hash") == 0)
1067 {
1068 this_hdr->sh_type = SHT_HASH;
1069 this_hdr->sh_entsize = bed->s->arch_size / 8;
1070 }
1071 else if (strcmp (asect->name, ".dynsym") == 0)
1072 {
1073 this_hdr->sh_type = SHT_DYNSYM;
1074 this_hdr->sh_entsize = bed->s->sizeof_sym;
1075 }
1076 else if (strcmp (asect->name, ".dynamic") == 0)
1077 {
1078 this_hdr->sh_type = SHT_DYNAMIC;
1079 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1080 }
1081 else if (strncmp (asect->name, ".rela", 5) == 0
1082 && get_elf_backend_data (abfd)->use_rela_p)
1083 {
1084 this_hdr->sh_type = SHT_RELA;
1085 this_hdr->sh_entsize = bed->s->sizeof_rela;
1086 }
1087 else if (strncmp (asect->name, ".rel", 4) == 0
1088 && ! get_elf_backend_data (abfd)->use_rela_p)
1089 {
1090 this_hdr->sh_type = SHT_REL;
1091 this_hdr->sh_entsize = bed->s->sizeof_rel;
1092 }
1093 else if (strcmp (asect->name, ".note") == 0)
1094 this_hdr->sh_type = SHT_NOTE;
1095 else if (strncmp (asect->name, ".stab", 5) == 0
1096 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1097 this_hdr->sh_type = SHT_STRTAB;
1098 else if ((asect->flags & SEC_ALLOC) != 0
1099 && (asect->flags & SEC_LOAD) != 0)
1100 this_hdr->sh_type = SHT_PROGBITS;
1101 else if ((asect->flags & SEC_ALLOC) != 0
1102 && ((asect->flags & SEC_LOAD) == 0))
5fe14a9f 1103 this_hdr->sh_type = SHT_NOBITS;
ede4eed4
KR
1104 else
1105 {
1106 /* Who knows? */
1107 this_hdr->sh_type = SHT_PROGBITS;
1108 }
1109
1110 if ((asect->flags & SEC_ALLOC) != 0)
1111 this_hdr->sh_flags |= SHF_ALLOC;
1112 if ((asect->flags & SEC_READONLY) == 0)
1113 this_hdr->sh_flags |= SHF_WRITE;
1114 if ((asect->flags & SEC_CODE) != 0)
1115 this_hdr->sh_flags |= SHF_EXECINSTR;
1116
1117 /* Check for processor-specific section types. */
1118 {
1119 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1120
1121 if (bed->elf_backend_fake_sections)
1122 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1123 }
1124
1125 /* If the section has relocs, set up a section header for the
1126 SHT_REL[A] section. */
1127 if ((asect->flags & SEC_RELOC) != 0)
1128 {
1129 Elf_Internal_Shdr *rela_hdr;
1130 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1131 char *name;
1132
1133 rela_hdr = &elf_section_data (asect)->rel_hdr;
1134 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1135 if (name == NULL)
1136 {
ede4eed4
KR
1137 *failedptr = true;
1138 return;
1139 }
1140 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1141 rela_hdr->sh_name =
1142 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1143 true, false);
1144 if (rela_hdr->sh_name == (unsigned int) -1)
1145 {
1146 *failedptr = true;
1147 return;
1148 }
1149 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1150 rela_hdr->sh_entsize = (use_rela_p
1151 ? bed->s->sizeof_rela
1152 : bed->s->sizeof_rel);
1153 rela_hdr->sh_addralign = bed->s->file_align;
1154 rela_hdr->sh_flags = 0;
1155 rela_hdr->sh_addr = 0;
1156 rela_hdr->sh_size = 0;
1157 rela_hdr->sh_offset = 0;
1158 }
1159}
1160
1161/* Assign all ELF section numbers. The dummy first section is handled here
1162 too. The link/info pointers for the standard section types are filled
1163 in here too, while we're at it. */
1164
1165static boolean
1166assign_section_numbers (abfd)
1167 bfd *abfd;
1168{
1169 struct elf_obj_tdata *t = elf_tdata (abfd);
1170 asection *sec;
1171 unsigned int section_number;
1172 Elf_Internal_Shdr **i_shdrp;
1173 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1174
1175 section_number = 1;
1176
1177 for (sec = abfd->sections; sec; sec = sec->next)
1178 {
1179 struct bfd_elf_section_data *d = elf_section_data (sec);
1180
1181 d->this_idx = section_number++;
1182 if ((sec->flags & SEC_RELOC) == 0)
1183 d->rel_idx = 0;
1184 else
1185 d->rel_idx = section_number++;
1186 }
1187
1188 t->shstrtab_section = section_number++;
1189 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1190 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1191
1192 if (abfd->symcount > 0)
1193 {
1194 t->symtab_section = section_number++;
1195 t->strtab_section = section_number++;
1196 }
1197
1198 elf_elfheader (abfd)->e_shnum = section_number;
1199
1200 /* Set up the list of section header pointers, in agreement with the
1201 indices. */
1202 i_shdrp = ((Elf_Internal_Shdr **)
1203 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1204 if (i_shdrp == NULL)
a9713b91 1205 return false;
ede4eed4
KR
1206
1207 i_shdrp[0] = ((Elf_Internal_Shdr *)
1208 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1209 if (i_shdrp[0] == NULL)
1210 {
1211 bfd_release (abfd, i_shdrp);
ede4eed4
KR
1212 return false;
1213 }
1214 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1215
1216 elf_elfsections (abfd) = i_shdrp;
1217
1218 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1219 if (abfd->symcount > 0)
1220 {
1221 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1222 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1223 t->symtab_hdr.sh_link = t->strtab_section;
1224 }
1225 for (sec = abfd->sections; sec; sec = sec->next)
1226 {
1227 struct bfd_elf_section_data *d = elf_section_data (sec);
1228 asection *s;
1229 const char *name;
1230
1231 i_shdrp[d->this_idx] = &d->this_hdr;
1232 if (d->rel_idx != 0)
1233 i_shdrp[d->rel_idx] = &d->rel_hdr;
1234
1235 /* Fill in the sh_link and sh_info fields while we're at it. */
1236
1237 /* sh_link of a reloc section is the section index of the symbol
1238 table. sh_info is the section index of the section to which
1239 the relocation entries apply. */
1240 if (d->rel_idx != 0)
1241 {
1242 d->rel_hdr.sh_link = t->symtab_section;
1243 d->rel_hdr.sh_info = d->this_idx;
1244 }
1245
1246 switch (d->this_hdr.sh_type)
1247 {
1248 case SHT_REL:
1249 case SHT_RELA:
1250 /* A reloc section which we are treating as a normal BFD
1251 section. sh_link is the section index of the symbol
1252 table. sh_info is the section index of the section to
1253 which the relocation entries apply. We assume that an
1254 allocated reloc section uses the dynamic symbol table.
1255 FIXME: How can we be sure? */
1256 s = bfd_get_section_by_name (abfd, ".dynsym");
1257 if (s != NULL)
1258 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1259
1260 /* We look up the section the relocs apply to by name. */
1261 name = sec->name;
1262 if (d->this_hdr.sh_type == SHT_REL)
1263 name += 4;
1264 else
1265 name += 5;
1266 s = bfd_get_section_by_name (abfd, name);
1267 if (s != NULL)
1268 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1269 break;
1270
1271 case SHT_STRTAB:
1272 /* We assume that a section named .stab*str is a stabs
1273 string section. We look for a section with the same name
1274 but without the trailing ``str'', and set its sh_link
1275 field to point to this section. */
1276 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1277 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1278 {
1279 size_t len;
1280 char *alc;
1281
1282 len = strlen (sec->name);
58142f10 1283 alc = (char *) bfd_malloc (len - 2);
ede4eed4 1284 if (alc == NULL)
58142f10 1285 return false;
ede4eed4
KR
1286 strncpy (alc, sec->name, len - 3);
1287 alc[len - 3] = '\0';
1288 s = bfd_get_section_by_name (abfd, alc);
1289 free (alc);
1290 if (s != NULL)
1291 {
1292 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1293
1294 /* This is a .stab section. */
1295 elf_section_data (s)->this_hdr.sh_entsize =
1296 4 + 2 * (bed->s->arch_size / 8);
1297 }
1298 }
1299 break;
1300
1301 case SHT_DYNAMIC:
1302 case SHT_DYNSYM:
1303 /* sh_link is the section header index of the string table
1304 used for the dynamic entries or symbol table. */
1305 s = bfd_get_section_by_name (abfd, ".dynstr");
1306 if (s != NULL)
1307 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1308 break;
1309
1310 case SHT_HASH:
1311 /* sh_link is the section header index of the symbol table
1312 this hash table is for. */
1313 s = bfd_get_section_by_name (abfd, ".dynsym");
1314 if (s != NULL)
1315 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1316 break;
1317 }
1318 }
1319
1320 return true;
1321}
1322
1323/* Map symbol from it's internal number to the external number, moving
1324 all local symbols to be at the head of the list. */
1325
1326static INLINE int
1327sym_is_global (abfd, sym)
1328 bfd *abfd;
1329 asymbol *sym;
1330{
1331 /* If the backend has a special mapping, use it. */
1332 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1333 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1334 (abfd, sym));
1335
1336 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1337 || bfd_is_und_section (bfd_get_section (sym))
1338 || bfd_is_com_section (bfd_get_section (sym)));
1339}
1340
1341static boolean
1342elf_map_symbols (abfd)
1343 bfd *abfd;
1344{
1345 int symcount = bfd_get_symcount (abfd);
1346 asymbol **syms = bfd_get_outsymbols (abfd);
1347 asymbol **sect_syms;
1348 int num_locals = 0;
1349 int num_globals = 0;
1350 int num_locals2 = 0;
1351 int num_globals2 = 0;
1352 int max_index = 0;
1353 int num_sections = 0;
1354 int idx;
1355 asection *asect;
1356 asymbol **new_syms;
1357
1358#ifdef DEBUG
1359 fprintf (stderr, "elf_map_symbols\n");
1360 fflush (stderr);
1361#endif
1362
1363 /* Add a section symbol for each BFD section. FIXME: Is this really
1364 necessary? */
1365 for (asect = abfd->sections; asect; asect = asect->next)
1366 {
1367 if (max_index < asect->index)
1368 max_index = asect->index;
1369 }
1370
1371 max_index++;
1372 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1373 if (sect_syms == NULL)
a9713b91 1374 return false;
ede4eed4
KR
1375 elf_section_syms (abfd) = sect_syms;
1376
1377 for (idx = 0; idx < symcount; idx++)
1378 {
1379 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
fd0198f0 1380 && (syms[idx]->value + syms[idx]->section->vma) == 0)
ede4eed4
KR
1381 {
1382 asection *sec;
1383
1384 sec = syms[idx]->section;
1385 if (sec->owner != NULL)
1386 {
1387 if (sec->owner != abfd)
1388 {
1389 if (sec->output_offset != 0)
1390 continue;
1391 sec = sec->output_section;
1392 BFD_ASSERT (sec->owner == abfd);
1393 }
1394 sect_syms[sec->index] = syms[idx];
1395 }
1396 }
1397 }
1398
1399 for (asect = abfd->sections; asect; asect = asect->next)
1400 {
1401 asymbol *sym;
1402
1403 if (sect_syms[asect->index] != NULL)
1404 continue;
1405
1406 sym = bfd_make_empty_symbol (abfd);
1407 if (sym == NULL)
1408 return false;
1409 sym->the_bfd = abfd;
1410 sym->name = asect->name;
1411 sym->value = 0;
1412 /* Set the flags to 0 to indicate that this one was newly added. */
1413 sym->flags = 0;
1414 sym->section = asect;
1415 sect_syms[asect->index] = sym;
1416 num_sections++;
1417#ifdef DEBUG
1418 fprintf (stderr,
1419 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1420 asect->name, (long) asect->vma, asect->index, (long) asect);
1421#endif
1422 }
1423
1424 /* Classify all of the symbols. */
1425 for (idx = 0; idx < symcount; idx++)
1426 {
1427 if (!sym_is_global (abfd, syms[idx]))
1428 num_locals++;
1429 else
1430 num_globals++;
1431 }
1432 for (asect = abfd->sections; asect; asect = asect->next)
1433 {
1434 if (sect_syms[asect->index] != NULL
1435 && sect_syms[asect->index]->flags == 0)
1436 {
1437 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1438 if (!sym_is_global (abfd, sect_syms[asect->index]))
1439 num_locals++;
1440 else
1441 num_globals++;
1442 sect_syms[asect->index]->flags = 0;
1443 }
1444 }
1445
1446 /* Now sort the symbols so the local symbols are first. */
1447 new_syms = ((asymbol **)
1448 bfd_alloc (abfd,
1449 (num_locals + num_globals) * sizeof (asymbol *)));
1450 if (new_syms == NULL)
a9713b91 1451 return false;
ede4eed4
KR
1452
1453 for (idx = 0; idx < symcount; idx++)
1454 {
1455 asymbol *sym = syms[idx];
1456 int i;
1457
1458 if (!sym_is_global (abfd, sym))
1459 i = num_locals2++;
1460 else
1461 i = num_locals + num_globals2++;
1462 new_syms[i] = sym;
1463 sym->udata.i = i + 1;
1464 }
1465 for (asect = abfd->sections; asect; asect = asect->next)
1466 {
1467 if (sect_syms[asect->index] != NULL
1468 && sect_syms[asect->index]->flags == 0)
1469 {
1470 asymbol *sym = sect_syms[asect->index];
1471 int i;
1472
1473 sym->flags = BSF_SECTION_SYM;
1474 if (!sym_is_global (abfd, sym))
1475 i = num_locals2++;
1476 else
1477 i = num_locals + num_globals2++;
1478 new_syms[i] = sym;
1479 sym->udata.i = i + 1;
1480 }
1481 }
1482
1483 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1484
1485 elf_num_locals (abfd) = num_locals;
1486 elf_num_globals (abfd) = num_globals;
1487 return true;
1488}
1489
fd0198f0
ILT
1490/* Align to the maximum file alignment that could be required for any
1491 ELF data structure. */
1492
1493static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1494static INLINE file_ptr
1495align_file_position (off, align)
1496 file_ptr off;
1497 int align;
1498{
1499 return (off + align - 1) & ~(align - 1);
1500}
1501
1502/* Assign a file position to a section, optionally aligning to the
1503 required section alignment. */
1504
1505INLINE file_ptr
1506_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1507 Elf_Internal_Shdr *i_shdrp;
1508 file_ptr offset;
1509 boolean align;
1510{
1511 if (align)
1512 {
1513 unsigned int al;
1514
1515 al = i_shdrp->sh_addralign;
1516 if (al > 1)
1517 offset = BFD_ALIGN (offset, al);
1518 }
1519 i_shdrp->sh_offset = offset;
1520 if (i_shdrp->bfd_section != NULL)
1521 i_shdrp->bfd_section->filepos = offset;
1522 if (i_shdrp->sh_type != SHT_NOBITS)
1523 offset += i_shdrp->sh_size;
1524 return offset;
1525}
1526
ede4eed4
KR
1527/* Compute the file positions we are going to put the sections at, and
1528 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1529 is not NULL, this is being called by the ELF backend linker. */
1530
1531boolean
1532_bfd_elf_compute_section_file_positions (abfd, link_info)
1533 bfd *abfd;
1534 struct bfd_link_info *link_info;
1535{
1536 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1537 boolean failed;
1538 struct bfd_strtab_hash *strtab;
1539 Elf_Internal_Shdr *shstrtab_hdr;
1540
1541 if (abfd->output_has_begun)
1542 return true;
1543
1544 /* Do any elf backend specific processing first. */
1545 if (bed->elf_backend_begin_write_processing)
1546 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1547
1548 if (! prep_headers (abfd))
1549 return false;
1550
1551 failed = false;
1552 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1553 if (failed)
1554 return false;
1555
1556 if (!assign_section_numbers (abfd))
1557 return false;
1558
1559 /* The backend linker builds symbol table information itself. */
fd0198f0 1560 if (link_info == NULL && abfd->symcount > 0)
ede4eed4
KR
1561 {
1562 if (! swap_out_syms (abfd, &strtab))
1563 return false;
1564 }
1565
1566 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1567 /* sh_name was set in prep_headers. */
1568 shstrtab_hdr->sh_type = SHT_STRTAB;
1569 shstrtab_hdr->sh_flags = 0;
1570 shstrtab_hdr->sh_addr = 0;
1571 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1572 shstrtab_hdr->sh_entsize = 0;
1573 shstrtab_hdr->sh_link = 0;
1574 shstrtab_hdr->sh_info = 0;
fd0198f0 1575 /* sh_offset is set in assign_file_positions_except_relocs. */
ede4eed4
KR
1576 shstrtab_hdr->sh_addralign = 1;
1577
fd0198f0 1578 if (!assign_file_positions_except_relocs (abfd))
ede4eed4
KR
1579 return false;
1580
fd0198f0 1581 if (link_info == NULL && abfd->symcount > 0)
ede4eed4 1582 {
fd0198f0
ILT
1583 file_ptr off;
1584 Elf_Internal_Shdr *hdr;
1585
1586 off = elf_tdata (abfd)->next_file_pos;
1587
1588 hdr = &elf_tdata (abfd)->symtab_hdr;
1589 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1590
1591 hdr = &elf_tdata (abfd)->strtab_hdr;
1592 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1593
1594 elf_tdata (abfd)->next_file_pos = off;
1595
ede4eed4
KR
1596 /* Now that we know where the .strtab section goes, write it
1597 out. */
fd0198f0 1598 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
ede4eed4
KR
1599 || ! _bfd_stringtab_emit (abfd, strtab))
1600 return false;
1601 _bfd_stringtab_free (strtab);
1602 }
1603
1604 abfd->output_has_begun = true;
1605
1606 return true;
1607}
1608
fd0198f0 1609/* Create a mapping from a set of sections to a program segment. */
ede4eed4 1610
fd0198f0 1611static INLINE struct elf_segment_map *
edf3fe48 1612make_mapping (abfd, sections, from, to, phdr)
fd0198f0
ILT
1613 bfd *abfd;
1614 asection **sections;
1615 unsigned int from;
1616 unsigned int to;
edf3fe48 1617 boolean phdr;
ede4eed4 1618{
fd0198f0
ILT
1619 struct elf_segment_map *m;
1620 unsigned int i;
1621 asection **hdrpp;
1622
1623 m = ((struct elf_segment_map *)
1624 bfd_zalloc (abfd,
1625 (sizeof (struct elf_segment_map)
1626 + (to - from - 1) * sizeof (asection *))));
1627 if (m == NULL)
a9713b91 1628 return NULL;
fd0198f0
ILT
1629 m->next = NULL;
1630 m->p_type = PT_LOAD;
1631 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1632 m->sections[i - from] = *hdrpp;
1633 m->count = to - from;
1634
edf3fe48 1635 if (from == 0 && phdr)
6933148a
ILT
1636 {
1637 /* Include the headers in the first PT_LOAD segment. */
1638 m->includes_filehdr = 1;
1639 m->includes_phdrs = 1;
1640 }
1641
fd0198f0 1642 return m;
ede4eed4
KR
1643}
1644
fd0198f0 1645/* Set up a mapping from BFD sections to program segments. */
ede4eed4 1646
fd0198f0
ILT
1647static boolean
1648map_sections_to_segments (abfd)
1649 bfd *abfd;
ede4eed4 1650{
fd0198f0
ILT
1651 asection **sections = NULL;
1652 asection *s;
1653 unsigned int i;
1654 unsigned int count;
1655 struct elf_segment_map *mfirst;
1656 struct elf_segment_map **pm;
1657 struct elf_segment_map *m;
1658 asection *last_hdr;
1659 unsigned int phdr_index;
1660 bfd_vma maxpagesize;
1661 asection **hdrpp;
edf3fe48
ILT
1662 boolean phdr_in_section = true;
1663 boolean writable;
1664 asection *dynsec;
fd0198f0
ILT
1665
1666 if (elf_tdata (abfd)->segment_map != NULL)
1667 return true;
1668
1669 if (bfd_count_sections (abfd) == 0)
1670 return true;
1671
1672 /* Select the allocated sections, and sort them. */
1673
58142f10
ILT
1674 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
1675 * sizeof (asection *));
fd0198f0 1676 if (sections == NULL)
58142f10 1677 goto error_return;
ede4eed4 1678
fd0198f0
ILT
1679 i = 0;
1680 for (s = abfd->sections; s != NULL; s = s->next)
1681 {
1682 if ((s->flags & SEC_ALLOC) != 0)
1683 {
1684 sections[i] = s;
1685 ++i;
1686 }
5fe14a9f 1687 }
fd0198f0
ILT
1688 BFD_ASSERT (i <= bfd_count_sections (abfd));
1689 count = i;
ede4eed4 1690
fd0198f0 1691 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
ede4eed4 1692
fd0198f0 1693 /* Build the mapping. */
ede4eed4 1694
fd0198f0
ILT
1695 mfirst = NULL;
1696 pm = &mfirst;
ede4eed4 1697
fd0198f0
ILT
1698 /* If we have a .interp section, then create a PT_PHDR segment for
1699 the program headers and a PT_INTERP segment for the .interp
1700 section. */
1701 s = bfd_get_section_by_name (abfd, ".interp");
1702 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1703 {
1704 m = ((struct elf_segment_map *)
1705 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1706 if (m == NULL)
a9713b91 1707 goto error_return;
fd0198f0
ILT
1708 m->next = NULL;
1709 m->p_type = PT_PHDR;
1710 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1711 m->p_flags = PF_R | PF_X;
1712 m->p_flags_valid = 1;
6933148a 1713 m->includes_phdrs = 1;
ede4eed4 1714
fd0198f0
ILT
1715 *pm = m;
1716 pm = &m->next;
ede4eed4 1717
fd0198f0
ILT
1718 m = ((struct elf_segment_map *)
1719 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1720 if (m == NULL)
a9713b91 1721 goto error_return;
fd0198f0
ILT
1722 m->next = NULL;
1723 m->p_type = PT_INTERP;
1724 m->count = 1;
1725 m->sections[0] = s;
ede4eed4 1726
fd0198f0
ILT
1727 *pm = m;
1728 pm = &m->next;
1729 }
ede4eed4 1730
fd0198f0
ILT
1731 /* Look through the sections. We put sections in the same program
1732 segment when the start of the second section can be placed within
1733 a few bytes of the end of the first section. */
1734 last_hdr = NULL;
1735 phdr_index = 0;
1736 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
edf3fe48
ILT
1737 writable = false;
1738 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
1739 if (dynsec != NULL
1740 && (dynsec->flags & SEC_LOAD) == 0)
1741 dynsec = NULL;
1742
1743 /* Deal with -Ttext or something similar such that the
1744 first section is not adjacent to the program headers. */
1745 if (count
1746 && ((sections[0]->lma % maxpagesize) <
1747 (elf_tdata (abfd)->program_header_size % maxpagesize)))
1748 phdr_in_section = false;
1749
fd0198f0 1750 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
ede4eed4 1751 {
fd0198f0 1752 asection *hdr;
ede4eed4 1753
fd0198f0 1754 hdr = *hdrpp;
ede4eed4 1755
fd0198f0 1756 /* See if this section and the last one will fit in the same
edf3fe48
ILT
1757 segment. Don't put a loadable section after a non-loadable
1758 section. If we are building a dynamic executable, don't put
1759 a writable section in a read only segment (we don't do this
1760 for a non-dynamic executable because some people prefer to
1761 have only one program segment; anybody can use PHDRS in their
1762 linker script to control what happens anyhow). */
fd0198f0
ILT
1763 if (last_hdr == NULL
1764 || ((BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1765 >= hdr->lma)
1766 && ((last_hdr->flags & SEC_LOAD) != 0
edf3fe48
ILT
1767 || (hdr->flags & SEC_LOAD) == 0)
1768 && (dynsec == NULL
1769 || writable
1770 || (hdr->flags & SEC_READONLY) != 0)))
fd0198f0
ILT
1771 {
1772 last_hdr = hdr;
1773 continue;
1774 }
ede4eed4 1775
fd0198f0
ILT
1776 /* This section won't fit in the program segment. We must
1777 create a new program header holding all the sections from
1778 phdr_index until hdr. */
ede4eed4 1779
edf3fe48 1780 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
fd0198f0
ILT
1781 if (m == NULL)
1782 goto error_return;
ede4eed4 1783
fd0198f0
ILT
1784 *pm = m;
1785 pm = &m->next;
ede4eed4 1786
edf3fe48
ILT
1787 if ((hdr->flags & SEC_READONLY) == 0)
1788 writable = true;
1789
fd0198f0
ILT
1790 last_hdr = hdr;
1791 phdr_index = i;
edf3fe48 1792 phdr_in_section = false;
ede4eed4 1793 }
fd0198f0
ILT
1794
1795 /* Create a final PT_LOAD program segment. */
1796 if (last_hdr != NULL)
ede4eed4 1797 {
edf3fe48 1798 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
fd0198f0
ILT
1799 if (m == NULL)
1800 goto error_return;
1801
1802 *pm = m;
1803 pm = &m->next;
ede4eed4
KR
1804 }
1805
fd0198f0 1806 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
edf3fe48 1807 if (dynsec != NULL)
ede4eed4 1808 {
fd0198f0
ILT
1809 m = ((struct elf_segment_map *)
1810 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1811 if (m == NULL)
a9713b91 1812 goto error_return;
fd0198f0
ILT
1813 m->next = NULL;
1814 m->p_type = PT_DYNAMIC;
1815 m->count = 1;
edf3fe48 1816 m->sections[0] = dynsec;
ede4eed4 1817
fd0198f0
ILT
1818 *pm = m;
1819 pm = &m->next;
ede4eed4
KR
1820 }
1821
fd0198f0
ILT
1822 free (sections);
1823 sections = NULL;
ae115e51 1824
fd0198f0
ILT
1825 elf_tdata (abfd)->segment_map = mfirst;
1826 return true;
1827
1828 error_return:
1829 if (sections != NULL)
1830 free (sections);
1831 return false;
ede4eed4
KR
1832}
1833
fd0198f0 1834/* Sort sections by VMA. */
ede4eed4 1835
fd0198f0
ILT
1836static int
1837elf_sort_sections (arg1, arg2)
1838 const PTR arg1;
1839 const PTR arg2;
ede4eed4 1840{
fd0198f0
ILT
1841 const asection *sec1 = *(const asection **) arg1;
1842 const asection *sec2 = *(const asection **) arg2;
ede4eed4 1843
fd0198f0
ILT
1844 if (sec1->vma < sec2->vma)
1845 return -1;
1846 else if (sec1->vma > sec2->vma)
1847 return 1;
ede4eed4 1848
fd0198f0 1849 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
ede4eed4 1850
fd0198f0 1851#define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
ede4eed4 1852
fd0198f0
ILT
1853 if (TOEND (sec1))
1854 if (TOEND (sec2))
1855 return sec1->target_index - sec2->target_index;
1856 else
1857 return 1;
ede4eed4 1858
fd0198f0
ILT
1859 if (TOEND (sec2))
1860 return -1;
ede4eed4 1861
fd0198f0 1862#undef TOEND
ede4eed4 1863
fd0198f0
ILT
1864 /* Sort by size, to put zero sized sections before others at the
1865 same address. */
ede4eed4 1866
fd0198f0
ILT
1867 if (sec1->_raw_size < sec2->_raw_size)
1868 return -1;
1869 if (sec1->_raw_size > sec2->_raw_size)
1870 return 1;
ede4eed4 1871
fd0198f0
ILT
1872 return sec1->target_index - sec2->target_index;
1873}
ede4eed4 1874
fd0198f0
ILT
1875/* Assign file positions to the sections based on the mapping from
1876 sections to segments. This function also sets up some fields in
1877 the file header, and writes out the program headers. */
ede4eed4 1878
fd0198f0
ILT
1879static boolean
1880assign_file_positions_for_segments (abfd)
1881 bfd *abfd;
1882{
1883 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1884 unsigned int count;
1885 struct elf_segment_map *m;
1886 unsigned int alloc;
1887 Elf_Internal_Phdr *phdrs;
1888 file_ptr off;
6933148a
ILT
1889 bfd_vma filehdr_vaddr, filehdr_paddr;
1890 bfd_vma phdrs_vaddr, phdrs_paddr;
fd0198f0
ILT
1891 Elf_Internal_Phdr *p;
1892
1893 if (elf_tdata (abfd)->segment_map == NULL)
1894 {
1895 if (! map_sections_to_segments (abfd))
1896 return false;
1897 }
ede4eed4 1898
5b3b9ff6
ILT
1899 if (bed->elf_backend_modify_segment_map)
1900 {
1901 if (! (*bed->elf_backend_modify_segment_map) (abfd))
1902 return false;
1903 }
1904
fd0198f0
ILT
1905 count = 0;
1906 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1907 ++count;
ede4eed4 1908
fd0198f0
ILT
1909 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
1910 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
1911 elf_elfheader (abfd)->e_phnum = count;
ede4eed4 1912
fd0198f0
ILT
1913 if (count == 0)
1914 return true;
ede4eed4 1915
fd0198f0
ILT
1916 /* If we already counted the number of program segments, make sure
1917 that we allocated enough space. This happens when SIZEOF_HEADERS
1918 is used in a linker script. */
1919 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
1920 if (alloc != 0 && count > alloc)
1921 {
1922 ((*_bfd_error_handler)
1923 ("%s: Not enough room for program headers (allocated %u, need %u)",
1924 bfd_get_filename (abfd), alloc, count));
1925 bfd_set_error (bfd_error_bad_value);
1926 return false;
ede4eed4
KR
1927 }
1928
fd0198f0
ILT
1929 if (alloc == 0)
1930 alloc = count;
1931
1932 phdrs = ((Elf_Internal_Phdr *)
1933 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
1934 if (phdrs == NULL)
a9713b91 1935 return false;
ede4eed4 1936
fd0198f0
ILT
1937 off = bed->s->sizeof_ehdr;
1938 off += alloc * bed->s->sizeof_phdr;
ede4eed4 1939
6933148a
ILT
1940 filehdr_vaddr = 0;
1941 filehdr_paddr = 0;
1942 phdrs_vaddr = 0;
1943 phdrs_paddr = 0;
fd0198f0
ILT
1944 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
1945 m != NULL;
1946 m = m->next, p++)
1947 {
1948 unsigned int i;
1949 asection **secpp;
fd0198f0 1950
3b950780
ILT
1951 /* If elf_segment_map is not from map_sections_to_segments, the
1952 sections may not be correctly ordered. */
1953 if (m->count > 0)
1954 qsort (m->sections, (size_t) m->count, sizeof (asection *),
1955 elf_sort_sections);
1956
fd0198f0
ILT
1957 p->p_type = m->p_type;
1958
1959 if (m->p_flags_valid)
1960 p->p_flags = m->p_flags;
14899eb7
ILT
1961 else
1962 p->p_flags = 0;
fd0198f0 1963
d49ddb85
ILT
1964 if (p->p_type == PT_LOAD
1965 && m->count > 0
1966 && (m->sections[0]->flags & SEC_LOAD) != 0)
44ef8897
ILT
1967 off += (m->sections[0]->vma - off) % bed->maxpagesize;
1968
fd0198f0
ILT
1969 if (m->count == 0)
1970 p->p_vaddr = 0;
1971 else
1972 p->p_vaddr = m->sections[0]->vma;
ede4eed4 1973
fd0198f0
ILT
1974 if (m->p_paddr_valid)
1975 p->p_paddr = m->p_paddr;
1976 else if (m->count == 0)
1977 p->p_paddr = 0;
1978 else
1979 p->p_paddr = m->sections[0]->lma;
1980
1981 if (p->p_type == PT_LOAD)
1982 p->p_align = bed->maxpagesize;
1983 else if (m->count == 0)
1984 p->p_align = bed->s->file_align;
1985 else
1986 p->p_align = 0;
1987
6933148a 1988 p->p_offset = 0;
fd0198f0
ILT
1989 p->p_filesz = 0;
1990 p->p_memsz = 0;
1991
6933148a 1992 if (m->includes_filehdr)
ede4eed4 1993 {
14899eb7
ILT
1994 if (! m->p_flags_valid)
1995 p->p_flags |= PF_R;
6933148a
ILT
1996 p->p_offset = 0;
1997 p->p_filesz = bed->s->sizeof_ehdr;
1998 p->p_memsz = bed->s->sizeof_ehdr;
1999 if (m->count > 0)
2000 {
2001 BFD_ASSERT (p->p_type == PT_LOAD);
2002 p->p_vaddr -= off;
2003 if (! m->p_paddr_valid)
2004 p->p_paddr -= off;
2005 }
2006 if (p->p_type == PT_LOAD)
2007 {
2008 filehdr_vaddr = p->p_vaddr;
2009 filehdr_paddr = p->p_paddr;
2010 }
2011 }
fd0198f0 2012
6933148a
ILT
2013 if (m->includes_phdrs)
2014 {
14899eb7
ILT
2015 if (! m->p_flags_valid)
2016 p->p_flags |= PF_R;
6933148a 2017 if (m->includes_filehdr)
fd0198f0 2018 {
6933148a 2019 if (p->p_type == PT_LOAD)
fd0198f0 2020 {
6933148a
ILT
2021 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2022 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
fd0198f0 2023 }
6933148a
ILT
2024 }
2025 else
2026 {
2027 p->p_offset = bed->s->sizeof_ehdr;
2028 if (m->count > 0)
2029 {
2030 BFD_ASSERT (p->p_type == PT_LOAD);
2031 p->p_vaddr -= off - p->p_offset;
2032 if (! m->p_paddr_valid)
2033 p->p_paddr -= off - p->p_offset;
2034 }
2035 if (p->p_type == PT_LOAD)
fd0198f0 2036 {
6933148a
ILT
2037 phdrs_vaddr = p->p_vaddr;
2038 phdrs_paddr = p->p_paddr;
fd0198f0 2039 }
6933148a
ILT
2040 }
2041 p->p_filesz += alloc * bed->s->sizeof_phdr;
2042 p->p_memsz += alloc * bed->s->sizeof_phdr;
2043 }
2044
2045 if (p->p_type == PT_LOAD)
2046 {
2047 if (! m->includes_filehdr && ! m->includes_phdrs)
2048 p->p_offset = off;
2049 else
2050 {
2051 file_ptr adjust;
fd0198f0 2052
6933148a
ILT
2053 adjust = off - (p->p_offset + p->p_filesz);
2054 p->p_filesz += adjust;
2055 p->p_memsz += adjust;
fd0198f0 2056 }
ede4eed4
KR
2057 }
2058
fd0198f0 2059 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
ede4eed4 2060 {
fd0198f0
ILT
2061 asection *sec;
2062 flagword flags;
2063 bfd_size_type align;
2064
2065 sec = *secpp;
2066 flags = sec->flags;
2067
2068 if (p->p_type == PT_LOAD)
2069 {
2070 bfd_vma adjust;
2071
2072 /* The section VMA must equal the file position modulo
2073 the page size. */
09609415 2074 if ((flags & SEC_ALLOC) != 0)
fd0198f0 2075 {
d49ddb85
ILT
2076 adjust = (sec->vma - off) % bed->maxpagesize;
2077 if (adjust != 0)
2078 {
2079 if (i == 0)
2080 abort ();
2081 p->p_memsz += adjust;
19bfbcbe 2082 off += adjust;
d49ddb85 2083 if ((flags & SEC_LOAD) != 0)
19bfbcbe 2084 p->p_filesz += adjust;
d49ddb85 2085 }
fd0198f0
ILT
2086 }
2087
2088 sec->filepos = off;
2089
2090 if ((flags & SEC_LOAD) != 0)
2091 off += sec->_raw_size;
2092 }
2093
2094 p->p_memsz += sec->_raw_size;
2095
2096 if ((flags & SEC_LOAD) != 0)
2097 p->p_filesz += sec->_raw_size;
2098
2099 align = 1 << bfd_get_section_alignment (abfd, sec);
2100 if (align > p->p_align)
2101 p->p_align = align;
2102
2103 if (! m->p_flags_valid)
2104 {
14899eb7 2105 p->p_flags |= PF_R;
fd0198f0
ILT
2106 if ((flags & SEC_CODE) != 0)
2107 p->p_flags |= PF_X;
2108 if ((flags & SEC_READONLY) == 0)
2109 p->p_flags |= PF_W;
2110 }
ede4eed4 2111 }
fd0198f0 2112 }
ede4eed4 2113
fd0198f0
ILT
2114 /* Now that we have set the section file positions, we can set up
2115 the file positions for the non PT_LOAD segments. */
2116 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2117 m != NULL;
2118 m = m->next, p++)
2119 {
2120 if (p->p_type != PT_LOAD && m->count > 0)
ede4eed4 2121 {
6933148a
ILT
2122 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2123 p->p_offset = m->sections[0]->filepos;
2124 }
2125 if (m->count == 0)
2126 {
2127 if (m->includes_filehdr)
2128 {
2129 p->p_vaddr = filehdr_vaddr;
2130 if (! m->p_paddr_valid)
2131 p->p_paddr = filehdr_paddr;
2132 }
2133 else if (m->includes_phdrs)
2134 {
2135 p->p_vaddr = phdrs_vaddr;
2136 if (! m->p_paddr_valid)
2137 p->p_paddr = phdrs_paddr;
2138 }
ede4eed4 2139 }
ede4eed4
KR
2140 }
2141
fd0198f0
ILT
2142 /* Clear out any program headers we allocated but did not use. */
2143 for (; count < alloc; count++, p++)
ede4eed4 2144 {
fd0198f0
ILT
2145 memset (p, 0, sizeof *p);
2146 p->p_type = PT_NULL;
ede4eed4
KR
2147 }
2148
fd0198f0 2149 elf_tdata (abfd)->phdr = phdrs;
ede4eed4 2150
fd0198f0 2151 elf_tdata (abfd)->next_file_pos = off;
ede4eed4 2152
fd0198f0
ILT
2153 /* Write out the program headers. */
2154 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2155 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2156 return false;
2157
2158 return true;
2159}
2160
2161/* Get the size of the program header.
2162
2163 If this is called by the linker before any of the section VMA's are set, it
2164 can't calculate the correct value for a strange memory layout. This only
2165 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2166 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2167 data segment (exclusive of .interp and .dynamic).
2168
2169 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2170 will be two segments. */
2171
2172static bfd_size_type
2173get_program_header_size (abfd)
2174 bfd *abfd;
2175{
2176 size_t segs;
2177 asection *s;
2178 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2179
2180 /* We can't return a different result each time we're called. */
2181 if (elf_tdata (abfd)->program_header_size != 0)
2182 return elf_tdata (abfd)->program_header_size;
ae115e51 2183
3b950780
ILT
2184 if (elf_tdata (abfd)->segment_map != NULL)
2185 {
2186 struct elf_segment_map *m;
2187
2188 segs = 0;
2189 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2190 ++segs;
2191 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2192 return elf_tdata (abfd)->program_header_size;
2193 }
2194
fd0198f0
ILT
2195 /* Assume we will need exactly two PT_LOAD segments: one for text
2196 and one for data. */
2197 segs = 2;
2198
2199 s = bfd_get_section_by_name (abfd, ".interp");
2200 if (s != NULL && (s->flags & SEC_LOAD) != 0)
ede4eed4 2201 {
fd0198f0
ILT
2202 /* If we have a loadable interpreter section, we need a
2203 PT_INTERP segment. In this case, assume we also need a
2204 PT_PHDR segment, although that may not be true for all
2205 targets. */
2206 segs += 2;
ede4eed4
KR
2207 }
2208
fd0198f0 2209 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
ede4eed4 2210 {
fd0198f0
ILT
2211 /* We need a PT_DYNAMIC segment. */
2212 ++segs;
ede4eed4 2213 }
ede4eed4 2214
fd0198f0 2215 /* Let the backend count up any program headers it might need. */
5b3b9ff6
ILT
2216 if (bed->elf_backend_additional_program_headers)
2217 {
2218 int a;
2219
2220 a = (*bed->elf_backend_additional_program_headers) (abfd);
2221 if (a == -1)
2222 abort ();
2223 segs += a;
2224 }
ede4eed4 2225
fd0198f0
ILT
2226 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2227 return elf_tdata (abfd)->program_header_size;
ede4eed4
KR
2228}
2229
2230/* Work out the file positions of all the sections. This is called by
2231 _bfd_elf_compute_section_file_positions. All the section sizes and
2232 VMAs must be known before this is called.
2233
2234 We do not consider reloc sections at this point, unless they form
2235 part of the loadable image. Reloc sections are assigned file
2236 positions in assign_file_positions_for_relocs, which is called by
2237 write_object_contents and final_link.
2238
fd0198f0 2239 We also don't set the positions of the .symtab and .strtab here. */
ede4eed4
KR
2240
2241static boolean
fd0198f0 2242assign_file_positions_except_relocs (abfd)
ede4eed4 2243 bfd *abfd;
ede4eed4
KR
2244{
2245 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2246 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2247 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2248 file_ptr off;
2249 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2250
ede4eed4
KR
2251 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2252 {
2253 Elf_Internal_Shdr **hdrpp;
2254 unsigned int i;
2255
fd0198f0
ILT
2256 /* Start after the ELF header. */
2257 off = i_ehdrp->e_ehsize;
2258
ede4eed4
KR
2259 /* We are not creating an executable, which means that we are
2260 not creating a program header, and that the actual order of
2261 the sections in the file is unimportant. */
2262 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2263 {
2264 Elf_Internal_Shdr *hdr;
2265
2266 hdr = *hdrpp;
2267 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2268 {
2269 hdr->sh_offset = -1;
2270 continue;
2271 }
fd0198f0
ILT
2272 if (i == tdata->symtab_section
2273 || i == tdata->strtab_section)
ede4eed4
KR
2274 {
2275 hdr->sh_offset = -1;
2276 continue;
2277 }
2278
5fe14a9f 2279 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
ede4eed4
KR
2280 }
2281 }
2282 else
2283 {
ede4eed4 2284 unsigned int i;
fd0198f0 2285 Elf_Internal_Shdr **hdrpp;
ede4eed4 2286
fd0198f0
ILT
2287 /* Assign file positions for the loaded sections based on the
2288 assignment of sections to segments. */
2289 if (! assign_file_positions_for_segments (abfd))
ede4eed4
KR
2290 return false;
2291
fd0198f0
ILT
2292 /* Assign file positions for the other sections. */
2293
2294 off = elf_tdata (abfd)->next_file_pos;
2295 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
ede4eed4
KR
2296 {
2297 Elf_Internal_Shdr *hdr;
2298
2299 hdr = *hdrpp;
fd0198f0
ILT
2300 if (hdr->bfd_section != NULL
2301 && hdr->bfd_section->filepos != 0)
2302 hdr->sh_offset = hdr->bfd_section->filepos;
2303 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
ede4eed4 2304 {
fd0198f0
ILT
2305 ((*_bfd_error_handler)
2306 ("%s: warning: allocated section `%s' not in segment",
2307 bfd_get_filename (abfd),
2308 (hdr->bfd_section == NULL
2309 ? "*unknown*"
2310 : hdr->bfd_section->name)));
2311 off += (hdr->sh_addr - off) % bed->maxpagesize;
5fe14a9f
ILT
2312 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2313 false);
ede4eed4 2314 }
fd0198f0
ILT
2315 else if (hdr->sh_type == SHT_REL
2316 || hdr->sh_type == SHT_RELA
2317 || hdr == i_shdrpp[tdata->symtab_section]
2318 || hdr == i_shdrpp[tdata->strtab_section])
2319 hdr->sh_offset = -1;
2320 else
2321 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2322 }
ede4eed4
KR
2323 }
2324
2325 /* Place the section headers. */
2326 off = align_file_position (off, bed->s->file_align);
2327 i_ehdrp->e_shoff = off;
2328 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2329
2330 elf_tdata (abfd)->next_file_pos = off;
2331
2332 return true;
2333}
2334
ede4eed4
KR
2335static boolean
2336prep_headers (abfd)
2337 bfd *abfd;
2338{
2339 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2340 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2341 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2342 int count;
2343 struct bfd_strtab_hash *shstrtab;
2344 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2345
2346 i_ehdrp = elf_elfheader (abfd);
2347 i_shdrp = elf_elfsections (abfd);
2348
2349 shstrtab = _bfd_elf_stringtab_init ();
2350 if (shstrtab == NULL)
2351 return false;
2352
2353 elf_shstrtab (abfd) = shstrtab;
2354
2355 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2356 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2357 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2358 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2359
2360 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2361 i_ehdrp->e_ident[EI_DATA] =
86587dd4 2362 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
ede4eed4
KR
2363 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2364
2365 for (count = EI_PAD; count < EI_NIDENT; count++)
2366 i_ehdrp->e_ident[count] = 0;
2367
2368 if ((abfd->flags & DYNAMIC) != 0)
2369 i_ehdrp->e_type = ET_DYN;
2370 else if ((abfd->flags & EXEC_P) != 0)
2371 i_ehdrp->e_type = ET_EXEC;
2372 else
2373 i_ehdrp->e_type = ET_REL;
2374
2375 switch (bfd_get_arch (abfd))
2376 {
2377 case bfd_arch_unknown:
2378 i_ehdrp->e_machine = EM_NONE;
2379 break;
2380 case bfd_arch_sparc:
2381 if (bed->s->arch_size == 64)
2382 i_ehdrp->e_machine = EM_SPARC64;
2383 else
2384 i_ehdrp->e_machine = EM_SPARC;
2385 break;
2386 case bfd_arch_i386:
2387 i_ehdrp->e_machine = EM_386;
2388 break;
2389 case bfd_arch_m68k:
2390 i_ehdrp->e_machine = EM_68K;
2391 break;
2392 case bfd_arch_m88k:
2393 i_ehdrp->e_machine = EM_88K;
2394 break;
2395 case bfd_arch_i860:
2396 i_ehdrp->e_machine = EM_860;
2397 break;
2398 case bfd_arch_mips: /* MIPS Rxxxx */
2399 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2400 break;
2401 case bfd_arch_hppa:
2402 i_ehdrp->e_machine = EM_PARISC;
2403 break;
2404 case bfd_arch_powerpc:
2405 i_ehdrp->e_machine = EM_PPC;
2406 break;
2407/* start-sanitize-arc */
2408 case bfd_arch_arc:
2409 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2410 break;
2411/* end-sanitize-arc */
2412 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2413 default:
2414 i_ehdrp->e_machine = EM_NONE;
2415 }
2416 i_ehdrp->e_version = bed->s->ev_current;
2417 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2418
2419 /* no program header, for now. */
2420 i_ehdrp->e_phoff = 0;
2421 i_ehdrp->e_phentsize = 0;
2422 i_ehdrp->e_phnum = 0;
2423
2424 /* each bfd section is section header entry */
2425 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2426 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2427
2428 /* if we're building an executable, we'll need a program header table */
2429 if (abfd->flags & EXEC_P)
2430 {
2431 /* it all happens later */
2432#if 0
2433 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2434
2435 /* elf_build_phdrs() returns a (NULL-terminated) array of
2436 Elf_Internal_Phdrs */
2437 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2438 i_ehdrp->e_phoff = outbase;
2439 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2440#endif
2441 }
2442 else
2443 {
2444 i_ehdrp->e_phentsize = 0;
2445 i_phdrp = 0;
2446 i_ehdrp->e_phoff = 0;
2447 }
2448
2449 elf_tdata (abfd)->symtab_hdr.sh_name =
2450 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2451 elf_tdata (abfd)->strtab_hdr.sh_name =
2452 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2453 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2454 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2455 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2456 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2457 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2458 return false;
2459
2460 return true;
2461}
2462
2463/* Assign file positions for all the reloc sections which are not part
2464 of the loadable file image. */
2465
2466void
2467_bfd_elf_assign_file_positions_for_relocs (abfd)
2468 bfd *abfd;
2469{
2470 file_ptr off;
2471 unsigned int i;
2472 Elf_Internal_Shdr **shdrpp;
2473
2474 off = elf_tdata (abfd)->next_file_pos;
2475
2476 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2477 i < elf_elfheader (abfd)->e_shnum;
2478 i++, shdrpp++)
2479 {
2480 Elf_Internal_Shdr *shdrp;
2481
2482 shdrp = *shdrpp;
2483 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2484 && shdrp->sh_offset == -1)
5fe14a9f 2485 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
ede4eed4
KR
2486 }
2487
2488 elf_tdata (abfd)->next_file_pos = off;
2489}
2490
2491boolean
2492_bfd_elf_write_object_contents (abfd)
2493 bfd *abfd;
2494{
2495 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2496 Elf_Internal_Ehdr *i_ehdrp;
2497 Elf_Internal_Shdr **i_shdrp;
2498 boolean failed;
2499 unsigned int count;
2500
2501 if (! abfd->output_has_begun
2502 && ! _bfd_elf_compute_section_file_positions (abfd,
2503 (struct bfd_link_info *) NULL))
2504 return false;
2505
2506 i_shdrp = elf_elfsections (abfd);
2507 i_ehdrp = elf_elfheader (abfd);
2508
2509 failed = false;
2510 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2511 if (failed)
2512 return false;
2513 _bfd_elf_assign_file_positions_for_relocs (abfd);
2514
2515 /* After writing the headers, we need to write the sections too... */
2516 for (count = 1; count < i_ehdrp->e_shnum; count++)
2517 {
2518 if (bed->elf_backend_section_processing)
2519 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2520 if (i_shdrp[count]->contents)
2521 {
2522 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2523 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2524 1, abfd)
2525 != i_shdrp[count]->sh_size))
2526 return false;
2527 }
2528 }
2529
2530 /* Write out the section header names. */
2531 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2532 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2533 return false;
2534
2535 if (bed->elf_backend_final_write_processing)
2536 (*bed->elf_backend_final_write_processing) (abfd,
2537 elf_tdata (abfd)->linker);
2538
2539 return bed->s->write_shdrs_and_ehdr (abfd);
2540}
2541
2542/* given a section, search the header to find them... */
2543int
2544_bfd_elf_section_from_bfd_section (abfd, asect)
2545 bfd *abfd;
2546 struct sec *asect;
2547{
2548 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2549 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2550 int index;
2551 Elf_Internal_Shdr *hdr;
2552 int maxindex = elf_elfheader (abfd)->e_shnum;
2553
2554 for (index = 0; index < maxindex; index++)
2555 {
2556 hdr = i_shdrp[index];
2557 if (hdr->bfd_section == asect)
2558 return index;
2559 }
2560
2561 if (bed->elf_backend_section_from_bfd_section)
2562 {
2563 for (index = 0; index < maxindex; index++)
2564 {
2565 int retval;
2566
2567 hdr = i_shdrp[index];
2568 retval = index;
2569 if ((*bed->elf_backend_section_from_bfd_section)
2570 (abfd, hdr, asect, &retval))
2571 return retval;
2572 }
2573 }
2574
2575 if (bfd_is_abs_section (asect))
2576 return SHN_ABS;
2577 if (bfd_is_com_section (asect))
2578 return SHN_COMMON;
2579 if (bfd_is_und_section (asect))
2580 return SHN_UNDEF;
2581
2582 return -1;
2583}
2584
cb84f028
ILT
2585/* Given a BFD symbol, return the index in the ELF symbol table, or -1
2586 on error. */
2587
2588int
ede4eed4
KR
2589_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2590 bfd *abfd;
2591 struct symbol_cache_entry **asym_ptr_ptr;
2592{
2593 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2594 int idx;
2595 flagword flags = asym_ptr->flags;
2596
2597 /* When gas creates relocations against local labels, it creates its
2598 own symbol for the section, but does put the symbol into the
2599 symbol chain, so udata is 0. When the linker is generating
2600 relocatable output, this section symbol may be for one of the
2601 input sections rather than the output section. */
2602 if (asym_ptr->udata.i == 0
2603 && (flags & BSF_SECTION_SYM)
2604 && asym_ptr->section)
2605 {
2606 int indx;
2607
2608 if (asym_ptr->section->output_section != NULL)
2609 indx = asym_ptr->section->output_section->index;
2610 else
2611 indx = asym_ptr->section->index;
2612 if (elf_section_syms (abfd)[indx])
2613 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
2614 }
2615
2616 idx = asym_ptr->udata.i;
cb84f028
ILT
2617
2618 if (idx == 0)
2619 {
2620 /* This case can occur when using --strip-symbol on a symbol
2621 which is used in a relocation entry. */
2622 (*_bfd_error_handler)
2623 ("%s: symbol `%s' required but not present",
2624 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
2625 bfd_set_error (bfd_error_no_symbols);
2626 return -1;
2627 }
ede4eed4
KR
2628
2629#if DEBUG & 4
2630 {
2631 fprintf (stderr,
2632 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
cb84f028
ILT
2633 (long) asym_ptr, asym_ptr->name, idx, flags,
2634 elf_symbol_flags (flags));
ede4eed4
KR
2635 fflush (stderr);
2636 }
2637#endif
2638
2639 return idx;
2640}
2641
3dbf33ee
ILT
2642/* Copy private BFD data. This copies any program header information. */
2643
2644static boolean
2645copy_private_bfd_data (ibfd, obfd)
2646 bfd *ibfd;
2647 bfd *obfd;
2648{
6933148a 2649 Elf_Internal_Ehdr *iehdr;
3dbf33ee
ILT
2650 struct elf_segment_map *mfirst;
2651 struct elf_segment_map **pm;
2652 Elf_Internal_Phdr *p;
2653 unsigned int i, c;
2654
2655 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2656 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2657 return true;
2658
2659 if (elf_tdata (ibfd)->phdr == NULL)
2660 return true;
2661
6933148a
ILT
2662 iehdr = elf_elfheader (ibfd);
2663
3dbf33ee
ILT
2664 mfirst = NULL;
2665 pm = &mfirst;
2666
2667 c = elf_elfheader (ibfd)->e_phnum;
2668 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
2669 {
3dbf33ee 2670 unsigned int csecs;
6933148a
ILT
2671 asection *s;
2672 struct elf_segment_map *m;
2673 unsigned int isec;
3dbf33ee
ILT
2674
2675 csecs = 0;
3dbf33ee 2676
6933148a
ILT
2677 /* The complicated case when p_vaddr is 0 is to handle the
2678 Solaris linker, which generates a PT_INTERP section with
2679 p_vaddr and p_memsz set to 0. */
2680 for (s = ibfd->sections; s != NULL; s = s->next)
2681 if (((s->vma >= p->p_vaddr
2682 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2683 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2684 || (p->p_vaddr == 0
2685 && p->p_filesz > 0
2686 && (s->flags & SEC_HAS_CONTENTS) != 0
2687 && (bfd_vma) s->filepos >= p->p_offset
2688 && ((bfd_vma) s->filepos + s->_raw_size
2689 <= p->p_offset + p->p_filesz)))
86587dd4 2690 && (s->flags & SEC_ALLOC) != 0
6933148a
ILT
2691 && s->output_section != NULL)
2692 ++csecs;
3dbf33ee
ILT
2693
2694 m = ((struct elf_segment_map *)
2695 bfd_alloc (obfd,
2696 (sizeof (struct elf_segment_map)
2697 + (csecs - 1) * sizeof (asection *))));
2698 if (m == NULL)
a9713b91 2699 return false;
3dbf33ee
ILT
2700
2701 m->next = NULL;
2702 m->p_type = p->p_type;
2703 m->p_flags = p->p_flags;
2704 m->p_flags_valid = 1;
2705 m->p_paddr = p->p_paddr;
2706 m->p_paddr_valid = 1;
2707
6933148a
ILT
2708 m->includes_filehdr = (p->p_offset == 0
2709 && p->p_filesz >= iehdr->e_ehsize);
2710
2711 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
2712 && (p->p_offset + p->p_filesz
2713 >= ((bfd_vma) iehdr->e_phoff
2714 + iehdr->e_phnum * iehdr->e_phentsize)));
3dbf33ee 2715
6933148a
ILT
2716 isec = 0;
2717 for (s = ibfd->sections; s != NULL; s = s->next)
2718 {
2719 if (((s->vma >= p->p_vaddr
2720 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2721 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2722 || (p->p_vaddr == 0
2723 && p->p_filesz > 0
2724 && (s->flags & SEC_HAS_CONTENTS) != 0
2725 && (bfd_vma) s->filepos >= p->p_offset
2726 && ((bfd_vma) s->filepos + s->_raw_size
2727 <= p->p_offset + p->p_filesz)))
86587dd4 2728 && (s->flags & SEC_ALLOC) != 0
6933148a 2729 && s->output_section != NULL)
3dbf33ee 2730 {
6933148a
ILT
2731 m->sections[isec] = s->output_section;
2732 ++isec;
3dbf33ee 2733 }
3dbf33ee 2734 }
6933148a 2735 BFD_ASSERT (isec == csecs);
6933148a 2736 m->count = csecs;
3dbf33ee
ILT
2737
2738 *pm = m;
2739 pm = &m->next;
2740 }
2741
2742 elf_tdata (obfd)->segment_map = mfirst;
2743
2744 return true;
2745}
2746
fd0198f0
ILT
2747/* Copy private section information. This copies over the entsize
2748 field, and sometimes the info field. */
2749
2750boolean
2751_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
2752 bfd *ibfd;
2753 asection *isec;
2754 bfd *obfd;
2755 asection *osec;
2756{
2757 Elf_Internal_Shdr *ihdr, *ohdr;
2758
2759 if (ibfd->xvec->flavour != bfd_target_elf_flavour
2760 || obfd->xvec->flavour != bfd_target_elf_flavour)
2761 return true;
2762
3dbf33ee
ILT
2763 /* Copy over private BFD data if it has not already been copied.
2764 This must be done here, rather than in the copy_private_bfd_data
2765 entry point, because the latter is called after the section
2766 contents have been set, which means that the program headers have
2767 already been worked out. */
2768 if (elf_tdata (obfd)->segment_map == NULL
2769 && elf_tdata (ibfd)->phdr != NULL)
2770 {
2771 asection *s;
2772
2773 /* Only set up the segments when all the sections have been set
2774 up. */
2775 for (s = ibfd->sections; s != NULL; s = s->next)
2776 if (s->output_section == NULL)
2777 break;
2778 if (s == NULL)
2779 {
2780 if (! copy_private_bfd_data (ibfd, obfd))
2781 return false;
2782 }
2783 }
2784
fd0198f0
ILT
2785 ihdr = &elf_section_data (isec)->this_hdr;
2786 ohdr = &elf_section_data (osec)->this_hdr;
2787
2788 ohdr->sh_entsize = ihdr->sh_entsize;
2789
2790 if (ihdr->sh_type == SHT_SYMTAB
2791 || ihdr->sh_type == SHT_DYNSYM)
2792 ohdr->sh_info = ihdr->sh_info;
2793
2794 return true;
2795}
2796
2797/* Copy private symbol information. If this symbol is in a section
2798 which we did not map into a BFD section, try to map the section
2799 index correctly. We use special macro definitions for the mapped
2800 section indices; these definitions are interpreted by the
2801 swap_out_syms function. */
2802
2803#define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2804#define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2805#define MAP_STRTAB (SHN_LORESERVE - 3)
2806#define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2807
2808boolean
2809_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
2810 bfd *ibfd;
2811 asymbol *isymarg;
2812 bfd *obfd;
2813 asymbol *osymarg;
2814{
2815 elf_symbol_type *isym, *osym;
2816
2817 isym = elf_symbol_from (ibfd, isymarg);
2818 osym = elf_symbol_from (obfd, osymarg);
2819
2820 if (isym != NULL
2821 && osym != NULL
2822 && bfd_is_abs_section (isym->symbol.section))
2823 {
2824 unsigned int shndx;
2825
2826 shndx = isym->internal_elf_sym.st_shndx;
2827 if (shndx == elf_onesymtab (ibfd))
2828 shndx = MAP_ONESYMTAB;
2829 else if (shndx == elf_dynsymtab (ibfd))
2830 shndx = MAP_DYNSYMTAB;
2831 else if (shndx == elf_tdata (ibfd)->strtab_section)
2832 shndx = MAP_STRTAB;
2833 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
2834 shndx = MAP_SHSTRTAB;
2835 osym->internal_elf_sym.st_shndx = shndx;
2836 }
2837
2838 return true;
2839}
2840
2841/* Swap out the symbols. */
2842
ede4eed4
KR
2843static boolean
2844swap_out_syms (abfd, sttp)
2845 bfd *abfd;
2846 struct bfd_strtab_hash **sttp;
2847{
2848 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2849
2850 if (!elf_map_symbols (abfd))
2851 return false;
2852
2853 /* Dump out the symtabs. */
2854 {
2855 int symcount = bfd_get_symcount (abfd);
2856 asymbol **syms = bfd_get_outsymbols (abfd);
2857 struct bfd_strtab_hash *stt;
2858 Elf_Internal_Shdr *symtab_hdr;
2859 Elf_Internal_Shdr *symstrtab_hdr;
2860 char *outbound_syms;
2861 int idx;
2862
2863 stt = _bfd_elf_stringtab_init ();
2864 if (stt == NULL)
2865 return false;
2866
2867 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2868 symtab_hdr->sh_type = SHT_SYMTAB;
2869 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
2870 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2871 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2872 symtab_hdr->sh_addralign = bed->s->file_align;
2873
2874 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2875 symstrtab_hdr->sh_type = SHT_STRTAB;
2876
2877 outbound_syms = bfd_alloc (abfd,
2878 (1 + symcount) * bed->s->sizeof_sym);
2879 if (outbound_syms == NULL)
a9713b91 2880 return false;
ede4eed4
KR
2881 symtab_hdr->contents = (PTR) outbound_syms;
2882
2883 /* now generate the data (for "contents") */
2884 {
2885 /* Fill in zeroth symbol and swap it out. */
2886 Elf_Internal_Sym sym;
2887 sym.st_name = 0;
2888 sym.st_value = 0;
2889 sym.st_size = 0;
2890 sym.st_info = 0;
2891 sym.st_other = 0;
2892 sym.st_shndx = SHN_UNDEF;
cf9fb9f2 2893 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
ede4eed4
KR
2894 outbound_syms += bed->s->sizeof_sym;
2895 }
2896 for (idx = 0; idx < symcount; idx++)
2897 {
2898 Elf_Internal_Sym sym;
2899 bfd_vma value = syms[idx]->value;
2900 elf_symbol_type *type_ptr;
2901 flagword flags = syms[idx]->flags;
052b35d2 2902 int type;
ede4eed4
KR
2903
2904 if (flags & BSF_SECTION_SYM)
2905 /* Section symbols have no names. */
2906 sym.st_name = 0;
2907 else
2908 {
2909 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
2910 syms[idx]->name,
2911 true, false);
2912 if (sym.st_name == (unsigned long) -1)
2913 return false;
2914 }
2915
2916 type_ptr = elf_symbol_from (abfd, syms[idx]);
2917
2918 if (bfd_is_com_section (syms[idx]->section))
2919 {
2920 /* ELF common symbols put the alignment into the `value' field,
2921 and the size into the `size' field. This is backwards from
2922 how BFD handles it, so reverse it here. */
2923 sym.st_size = value;
2924 if (type_ptr == NULL
2925 || type_ptr->internal_elf_sym.st_value == 0)
2926 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
2927 else
2928 sym.st_value = type_ptr->internal_elf_sym.st_value;
2929 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
2930 syms[idx]->section);
2931 }
2932 else
2933 {
2934 asection *sec = syms[idx]->section;
2935 int shndx;
2936
2937 if (sec->output_section)
2938 {
2939 value += sec->output_offset;
2940 sec = sec->output_section;
2941 }
2942 value += sec->vma;
2943 sym.st_value = value;
2944 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
fd0198f0
ILT
2945
2946 if (bfd_is_abs_section (sec)
2947 && type_ptr != NULL
2948 && type_ptr->internal_elf_sym.st_shndx != 0)
ede4eed4 2949 {
fd0198f0
ILT
2950 /* This symbol is in a real ELF section which we did
2951 not create as a BFD section. Undo the mapping done
2952 by copy_private_symbol_data. */
2953 shndx = type_ptr->internal_elf_sym.st_shndx;
2954 switch (shndx)
2955 {
2956 case MAP_ONESYMTAB:
2957 shndx = elf_onesymtab (abfd);
2958 break;
2959 case MAP_DYNSYMTAB:
2960 shndx = elf_dynsymtab (abfd);
2961 break;
2962 case MAP_STRTAB:
2963 shndx = elf_tdata (abfd)->strtab_section;
2964 break;
2965 case MAP_SHSTRTAB:
2966 shndx = elf_tdata (abfd)->shstrtab_section;
2967 break;
2968 default:
2969 break;
2970 }
2971 }
2972 else
2973 {
2974 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2975
2976 if (shndx == -1)
2977 {
2978 asection *sec2;
2979
2980 /* Writing this would be a hell of a lot easier if
2981 we had some decent documentation on bfd, and
2982 knew what to expect of the library, and what to
2983 demand of applications. For example, it
2984 appears that `objcopy' might not set the
2985 section of a symbol to be a section that is
2986 actually in the output file. */
2987 sec2 = bfd_get_section_by_name (abfd, sec->name);
2988 BFD_ASSERT (sec2 != 0);
2989 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
2990 BFD_ASSERT (shndx != -1);
2991 }
ede4eed4 2992 }
fd0198f0
ILT
2993
2994 sym.st_shndx = shndx;
ede4eed4
KR
2995 }
2996
052b35d2
ILT
2997 if ((flags & BSF_FUNCTION) != 0)
2998 type = STT_FUNC;
2999 else if ((flags & BSF_OBJECT) != 0)
3000 type = STT_OBJECT;
3001 else
3002 type = STT_NOTYPE;
3003
ede4eed4 3004 if (bfd_is_com_section (syms[idx]->section))
052b35d2 3005 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
ede4eed4
KR
3006 else if (bfd_is_und_section (syms[idx]->section))
3007 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
3008 ? STB_WEAK
3009 : STB_GLOBAL),
052b35d2 3010 type);
ede4eed4
KR
3011 else if (flags & BSF_SECTION_SYM)
3012 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3013 else if (flags & BSF_FILE)
3014 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3015 else
3016 {
3017 int bind = STB_LOCAL;
ede4eed4
KR
3018
3019 if (flags & BSF_LOCAL)
3020 bind = STB_LOCAL;
3021 else if (flags & BSF_WEAK)
3022 bind = STB_WEAK;
3023 else if (flags & BSF_GLOBAL)
3024 bind = STB_GLOBAL;
3025
ede4eed4
KR
3026 sym.st_info = ELF_ST_INFO (bind, type);
3027 }
3028
3029 sym.st_other = 0;
cf9fb9f2 3030 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
ede4eed4
KR
3031 outbound_syms += bed->s->sizeof_sym;
3032 }
3033
3034 *sttp = stt;
3035 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
3036 symstrtab_hdr->sh_type = SHT_STRTAB;
3037
3038 symstrtab_hdr->sh_flags = 0;
3039 symstrtab_hdr->sh_addr = 0;
3040 symstrtab_hdr->sh_entsize = 0;
3041 symstrtab_hdr->sh_link = 0;
3042 symstrtab_hdr->sh_info = 0;
3043 symstrtab_hdr->sh_addralign = 1;
3044 }
3045
3046 return true;
3047}
3048
3049/* Return the number of bytes required to hold the symtab vector.
3050
3051 Note that we base it on the count plus 1, since we will null terminate
3052 the vector allocated based on this size. However, the ELF symbol table
3053 always has a dummy entry as symbol #0, so it ends up even. */
3054
3055long
3056_bfd_elf_get_symtab_upper_bound (abfd)
3057 bfd *abfd;
3058{
3059 long symcount;
3060 long symtab_size;
3061 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
3062
3063 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3064 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3065
3066 return symtab_size;
3067}
3068
3069long
3070_bfd_elf_get_dynamic_symtab_upper_bound (abfd)
3071 bfd *abfd;
3072{
3073 long symcount;
3074 long symtab_size;
3075 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3076
3077 if (elf_dynsymtab (abfd) == 0)
3078 {
3079 bfd_set_error (bfd_error_invalid_operation);
3080 return -1;
3081 }
3082
3083 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3084 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3085
3086 return symtab_size;
3087}
3088
3089long
3090_bfd_elf_get_reloc_upper_bound (abfd, asect)
3091 bfd *abfd;
3092 sec_ptr asect;
3093{
3094 return (asect->reloc_count + 1) * sizeof (arelent *);
3095}
3096
3097/* Canonicalize the relocs. */
3098
3099long
3100_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
3101 bfd *abfd;
3102 sec_ptr section;
3103 arelent **relptr;
3104 asymbol **symbols;
3105{
3106 arelent *tblptr;
3107 unsigned int i;
3108
3109 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols))
3110 return -1;
3111
3112 tblptr = section->relocation;
3113 for (i = 0; i < section->reloc_count; i++)
3114 *relptr++ = tblptr++;
3115
3116 *relptr = NULL;
3117
3118 return section->reloc_count;
3119}
3120
3121long
3122_bfd_elf_get_symtab (abfd, alocation)
3123 bfd *abfd;
3124 asymbol **alocation;
3125{
3126 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
3127
3128 if (symcount >= 0)
3129 bfd_get_symcount (abfd) = symcount;
3130 return symcount;
3131}
3132
3133long
3134_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
3135 bfd *abfd;
3136 asymbol **alocation;
3137{
3138 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
3139}
3140
3141asymbol *
3142_bfd_elf_make_empty_symbol (abfd)
3143 bfd *abfd;
3144{
3145 elf_symbol_type *newsym;
3146
3147 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3148 if (!newsym)
a9713b91 3149 return NULL;
ede4eed4
KR
3150 else
3151 {
3152 newsym->symbol.the_bfd = abfd;
3153 return &newsym->symbol;
3154 }
3155}
3156
3157void
3158_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
3159 bfd *ignore_abfd;
3160 asymbol *symbol;
3161 symbol_info *ret;
3162{
3163 bfd_symbol_info (symbol, ret);
3164}
3165
3166alent *
3167_bfd_elf_get_lineno (ignore_abfd, symbol)
3168 bfd *ignore_abfd;
3169 asymbol *symbol;
3170{
8cd2f4fe 3171 abort ();
ede4eed4
KR
3172 return NULL;
3173}
3174
3175boolean
3176_bfd_elf_set_arch_mach (abfd, arch, machine)
3177 bfd *abfd;
3178 enum bfd_architecture arch;
3179 unsigned long machine;
3180{
3181 /* If this isn't the right architecture for this backend, and this
3182 isn't the generic backend, fail. */
3183 if (arch != get_elf_backend_data (abfd)->arch
3184 && arch != bfd_arch_unknown
3185 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3186 return false;
3187
3188 return bfd_default_set_arch_mach (abfd, arch, machine);
3189}
3190
6f904fce
ILT
3191/* Find the nearest line to a particular section and offset, for error
3192 reporting. */
3193
ede4eed4
KR
3194boolean
3195_bfd_elf_find_nearest_line (abfd,
6f904fce
ILT
3196 section,
3197 symbols,
3198 offset,
3199 filename_ptr,
3200 functionname_ptr,
3201 line_ptr)
ede4eed4
KR
3202 bfd *abfd;
3203 asection *section;
3204 asymbol **symbols;
3205 bfd_vma offset;
3206 CONST char **filename_ptr;
3207 CONST char **functionname_ptr;
3208 unsigned int *line_ptr;
3209{
86aac8ea 3210 boolean found;
6f904fce
ILT
3211 const char *filename;
3212 asymbol *func;
86aac8ea 3213 bfd_vma low_func;
6f904fce
ILT
3214 asymbol **p;
3215
86aac8ea
ILT
3216 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3217 &found, filename_ptr,
3218 functionname_ptr, line_ptr,
3219 &elf_tdata (abfd)->line_info))
3220 return false;
3221 if (found)
3222 return true;
3223
6f904fce
ILT
3224 if (symbols == NULL)
3225 return false;
3226
3227 filename = NULL;
3228 func = NULL;
86aac8ea 3229 low_func = 0;
6f904fce
ILT
3230
3231 for (p = symbols; *p != NULL; p++)
3232 {
3233 elf_symbol_type *q;
3234
3235 q = (elf_symbol_type *) *p;
3236
3237 if (bfd_get_section (&q->symbol) != section)
3238 continue;
3239
3240 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3241 {
3242 default:
3243 break;
3244 case STT_FILE:
3245 filename = bfd_asymbol_name (&q->symbol);
3246 break;
3247 case STT_FUNC:
86aac8ea
ILT
3248 if (q->symbol.section == section
3249 && q->symbol.value >= low_func
3250 && q->symbol.value <= offset)
3251 {
3252 func = (asymbol *) q;
3253 low_func = q->symbol.value;
3254 }
6f904fce
ILT
3255 break;
3256 }
3257 }
3258
3259 if (func == NULL)
3260 return false;
3261
3262 *filename_ptr = filename;
3263 *functionname_ptr = bfd_asymbol_name (func);
3264 *line_ptr = 0;
3265 return true;
ede4eed4
KR
3266}
3267
3268int
3269_bfd_elf_sizeof_headers (abfd, reloc)
3270 bfd *abfd;
3271 boolean reloc;
3272{
3273 int ret;
3274
3275 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
3276 if (! reloc)
fd0198f0 3277 ret += get_program_header_size (abfd);
ede4eed4
KR
3278 return ret;
3279}
3280
3281boolean
3282_bfd_elf_set_section_contents (abfd, section, location, offset, count)
3283 bfd *abfd;
3284 sec_ptr section;
3285 PTR location;
3286 file_ptr offset;
3287 bfd_size_type count;
3288{
3289 Elf_Internal_Shdr *hdr;
3290
3291 if (! abfd->output_has_begun
3292 && ! _bfd_elf_compute_section_file_positions (abfd,
3293 (struct bfd_link_info *) NULL))
3294 return false;
3295
3296 hdr = &elf_section_data (section)->this_hdr;
3297
3298 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3299 return false;
3300 if (bfd_write (location, 1, count, abfd) != count)
3301 return false;
3302
3303 return true;
3304}
3305
3306void
3307_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
3308 bfd *abfd;
3309 arelent *cache_ptr;
3310 Elf_Internal_Rela *dst;
3311{
8cd2f4fe 3312 abort ();
ede4eed4
KR
3313}
3314
3315#if 0
3316void
3317_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3318 bfd *abfd;
3319 arelent *cache_ptr;
3320 Elf_Internal_Rel *dst;
3321{
8cd2f4fe 3322 abort ();
ede4eed4
KR
3323}
3324#endif
This page took 0.27422 seconds and 4 git commands to generate.