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