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