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