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