* elf.c (bfd_section_from_shdr): Don't check for reloc sections
[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 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
868
869 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
870 return false;
871 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
872 if (target_sect == NULL)
873 return false;
874
875 if ((target_sect->flags & SEC_RELOC) == 0
876 || target_sect->reloc_count == 0)
877 hdr2 = &elf_section_data (target_sect)->rel_hdr;
878 else
879 {
880 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
881 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
882 elf_section_data (target_sect)->rel_hdr2 = hdr2;
883 }
884 *hdr2 = *hdr;
885 elf_elfsections (abfd)[shindex] = hdr2;
886 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
887 target_sect->flags |= SEC_RELOC;
888 target_sect->relocation = NULL;
889 target_sect->rel_filepos = hdr->sh_offset;
890 abfd->flags |= HAS_RELOC;
891 return true;
892 }
893 break;
894
895 case SHT_SHLIB:
896 return true;
897
898 default:
899 /* Check for any processor-specific section types. */
900 {
901 if (bed->elf_backend_section_from_shdr)
902 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
903 }
904 break;
905 }
906
907 return true;
908 }
909
910 /* Given an ELF section number, retrieve the corresponding BFD
911 section. */
912
913 asection *
914 bfd_section_from_elf_index (abfd, index)
915 bfd *abfd;
916 unsigned int index;
917 {
918 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
919 if (index >= elf_elfheader (abfd)->e_shnum)
920 return NULL;
921 return elf_elfsections (abfd)[index]->bfd_section;
922 }
923
924 boolean
925 _bfd_elf_new_section_hook (abfd, sec)
926 bfd *abfd;
927 asection *sec;
928 {
929 struct bfd_elf_section_data *sdata;
930
931 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
932 if (!sdata)
933 return false;
934 sec->used_by_bfd = (PTR) sdata;
935 memset (sdata, 0, sizeof (*sdata));
936 return true;
937 }
938
939 /* Create a new bfd section from an ELF program header.
940
941 Since program segments have no names, we generate a synthetic name
942 of the form segment<NUM>, where NUM is generally the index in the
943 program header table. For segments that are split (see below) we
944 generate the names segment<NUM>a and segment<NUM>b.
945
946 Note that some program segments may have a file size that is different than
947 (less than) the memory size. All this means is that at execution the
948 system must allocate the amount of memory specified by the memory size,
949 but only initialize it with the first "file size" bytes read from the
950 file. This would occur for example, with program segments consisting
951 of combined data+bss.
952
953 To handle the above situation, this routine generates TWO bfd sections
954 for the single program segment. The first has the length specified by
955 the file size of the segment, and the second has the length specified
956 by the difference between the two sizes. In effect, the segment is split
957 into it's initialized and uninitialized parts.
958
959 */
960
961 boolean
962 bfd_section_from_phdr (abfd, hdr, index)
963 bfd *abfd;
964 Elf_Internal_Phdr *hdr;
965 int index;
966 {
967 asection *newsect;
968 char *name;
969 char namebuf[64];
970 int split;
971
972 split = ((hdr->p_memsz > 0) &&
973 (hdr->p_filesz > 0) &&
974 (hdr->p_memsz > hdr->p_filesz));
975 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
976 name = bfd_alloc (abfd, strlen (namebuf) + 1);
977 if (!name)
978 return false;
979 strcpy (name, namebuf);
980 newsect = bfd_make_section (abfd, name);
981 if (newsect == NULL)
982 return false;
983 newsect->vma = hdr->p_vaddr;
984 newsect->lma = hdr->p_paddr;
985 newsect->_raw_size = hdr->p_filesz;
986 newsect->filepos = hdr->p_offset;
987 newsect->flags |= SEC_HAS_CONTENTS;
988 if (hdr->p_type == PT_LOAD)
989 {
990 newsect->flags |= SEC_ALLOC;
991 newsect->flags |= SEC_LOAD;
992 if (hdr->p_flags & PF_X)
993 {
994 /* FIXME: all we known is that it has execute PERMISSION,
995 may be data. */
996 newsect->flags |= SEC_CODE;
997 }
998 }
999 if (!(hdr->p_flags & PF_W))
1000 {
1001 newsect->flags |= SEC_READONLY;
1002 }
1003
1004 if (split)
1005 {
1006 sprintf (namebuf, "segment%db", index);
1007 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1008 if (!name)
1009 return false;
1010 strcpy (name, namebuf);
1011 newsect = bfd_make_section (abfd, name);
1012 if (newsect == NULL)
1013 return false;
1014 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1015 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1016 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1017 if (hdr->p_type == PT_LOAD)
1018 {
1019 newsect->flags |= SEC_ALLOC;
1020 if (hdr->p_flags & PF_X)
1021 newsect->flags |= SEC_CODE;
1022 }
1023 if (!(hdr->p_flags & PF_W))
1024 newsect->flags |= SEC_READONLY;
1025 }
1026
1027 return true;
1028 }
1029
1030 /* Set up an ELF internal section header for a section. */
1031
1032 /*ARGSUSED*/
1033 static void
1034 elf_fake_sections (abfd, asect, failedptrarg)
1035 bfd *abfd;
1036 asection *asect;
1037 PTR failedptrarg;
1038 {
1039 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1040 boolean *failedptr = (boolean *) failedptrarg;
1041 Elf_Internal_Shdr *this_hdr;
1042
1043 if (*failedptr)
1044 {
1045 /* We already failed; just get out of the bfd_map_over_sections
1046 loop. */
1047 return;
1048 }
1049
1050 this_hdr = &elf_section_data (asect)->this_hdr;
1051
1052 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1053 asect->name,
1054 true, false);
1055 if (this_hdr->sh_name == (unsigned long) -1)
1056 {
1057 *failedptr = true;
1058 return;
1059 }
1060
1061 this_hdr->sh_flags = 0;
1062
1063 if ((asect->flags & SEC_ALLOC) != 0
1064 || asect->user_set_vma)
1065 this_hdr->sh_addr = asect->vma;
1066 else
1067 this_hdr->sh_addr = 0;
1068
1069 this_hdr->sh_offset = 0;
1070 this_hdr->sh_size = asect->_raw_size;
1071 this_hdr->sh_link = 0;
1072 this_hdr->sh_addralign = 1 << asect->alignment_power;
1073 /* The sh_entsize and sh_info fields may have been set already by
1074 copy_private_section_data. */
1075
1076 this_hdr->bfd_section = asect;
1077 this_hdr->contents = NULL;
1078
1079 /* FIXME: This should not be based on section names. */
1080 if (strcmp (asect->name, ".dynstr") == 0)
1081 this_hdr->sh_type = SHT_STRTAB;
1082 else if (strcmp (asect->name, ".hash") == 0)
1083 {
1084 this_hdr->sh_type = SHT_HASH;
1085 this_hdr->sh_entsize = bed->s->arch_size / 8;
1086 }
1087 else if (strcmp (asect->name, ".dynsym") == 0)
1088 {
1089 this_hdr->sh_type = SHT_DYNSYM;
1090 this_hdr->sh_entsize = bed->s->sizeof_sym;
1091 }
1092 else if (strcmp (asect->name, ".dynamic") == 0)
1093 {
1094 this_hdr->sh_type = SHT_DYNAMIC;
1095 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1096 }
1097 else if (strncmp (asect->name, ".rela", 5) == 0
1098 && get_elf_backend_data (abfd)->use_rela_p)
1099 {
1100 this_hdr->sh_type = SHT_RELA;
1101 this_hdr->sh_entsize = bed->s->sizeof_rela;
1102 }
1103 else if (strncmp (asect->name, ".rel", 4) == 0
1104 && ! get_elf_backend_data (abfd)->use_rela_p)
1105 {
1106 this_hdr->sh_type = SHT_REL;
1107 this_hdr->sh_entsize = bed->s->sizeof_rel;
1108 }
1109 else if (strcmp (asect->name, ".note") == 0)
1110 this_hdr->sh_type = SHT_NOTE;
1111 else if (strncmp (asect->name, ".stab", 5) == 0
1112 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1113 this_hdr->sh_type = SHT_STRTAB;
1114 else if ((asect->flags & SEC_ALLOC) != 0
1115 && (asect->flags & SEC_LOAD) != 0)
1116 this_hdr->sh_type = SHT_PROGBITS;
1117 else if ((asect->flags & SEC_ALLOC) != 0
1118 && ((asect->flags & SEC_LOAD) == 0))
1119 this_hdr->sh_type = SHT_NOBITS;
1120 else
1121 {
1122 /* Who knows? */
1123 this_hdr->sh_type = SHT_PROGBITS;
1124 }
1125
1126 if ((asect->flags & SEC_ALLOC) != 0)
1127 this_hdr->sh_flags |= SHF_ALLOC;
1128 if ((asect->flags & SEC_READONLY) == 0)
1129 this_hdr->sh_flags |= SHF_WRITE;
1130 if ((asect->flags & SEC_CODE) != 0)
1131 this_hdr->sh_flags |= SHF_EXECINSTR;
1132
1133 /* Check for processor-specific section types. */
1134 {
1135 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1136
1137 if (bed->elf_backend_fake_sections)
1138 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1139 }
1140
1141 /* If the section has relocs, set up a section header for the
1142 SHT_REL[A] section. */
1143 if ((asect->flags & SEC_RELOC) != 0)
1144 {
1145 Elf_Internal_Shdr *rela_hdr;
1146 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1147 char *name;
1148
1149 rela_hdr = &elf_section_data (asect)->rel_hdr;
1150 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1151 if (name == NULL)
1152 {
1153 *failedptr = true;
1154 return;
1155 }
1156 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1157 rela_hdr->sh_name =
1158 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1159 true, false);
1160 if (rela_hdr->sh_name == (unsigned int) -1)
1161 {
1162 *failedptr = true;
1163 return;
1164 }
1165 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1166 rela_hdr->sh_entsize = (use_rela_p
1167 ? bed->s->sizeof_rela
1168 : bed->s->sizeof_rel);
1169 rela_hdr->sh_addralign = bed->s->file_align;
1170 rela_hdr->sh_flags = 0;
1171 rela_hdr->sh_addr = 0;
1172 rela_hdr->sh_size = 0;
1173 rela_hdr->sh_offset = 0;
1174 }
1175 }
1176
1177 /* Assign all ELF section numbers. The dummy first section is handled here
1178 too. The link/info pointers for the standard section types are filled
1179 in here too, while we're at it. */
1180
1181 static boolean
1182 assign_section_numbers (abfd)
1183 bfd *abfd;
1184 {
1185 struct elf_obj_tdata *t = elf_tdata (abfd);
1186 asection *sec;
1187 unsigned int section_number;
1188 Elf_Internal_Shdr **i_shdrp;
1189 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1190
1191 section_number = 1;
1192
1193 for (sec = abfd->sections; sec; sec = sec->next)
1194 {
1195 struct bfd_elf_section_data *d = elf_section_data (sec);
1196
1197 d->this_idx = section_number++;
1198 if ((sec->flags & SEC_RELOC) == 0)
1199 d->rel_idx = 0;
1200 else
1201 d->rel_idx = section_number++;
1202 }
1203
1204 t->shstrtab_section = section_number++;
1205 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1206 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1207
1208 if (abfd->symcount > 0)
1209 {
1210 t->symtab_section = section_number++;
1211 t->strtab_section = section_number++;
1212 }
1213
1214 elf_elfheader (abfd)->e_shnum = section_number;
1215
1216 /* Set up the list of section header pointers, in agreement with the
1217 indices. */
1218 i_shdrp = ((Elf_Internal_Shdr **)
1219 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1220 if (i_shdrp == NULL)
1221 return false;
1222
1223 i_shdrp[0] = ((Elf_Internal_Shdr *)
1224 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1225 if (i_shdrp[0] == NULL)
1226 {
1227 bfd_release (abfd, i_shdrp);
1228 return false;
1229 }
1230 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1231
1232 elf_elfsections (abfd) = i_shdrp;
1233
1234 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1235 if (abfd->symcount > 0)
1236 {
1237 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1238 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1239 t->symtab_hdr.sh_link = t->strtab_section;
1240 }
1241 for (sec = abfd->sections; sec; sec = sec->next)
1242 {
1243 struct bfd_elf_section_data *d = elf_section_data (sec);
1244 asection *s;
1245 const char *name;
1246
1247 i_shdrp[d->this_idx] = &d->this_hdr;
1248 if (d->rel_idx != 0)
1249 i_shdrp[d->rel_idx] = &d->rel_hdr;
1250
1251 /* Fill in the sh_link and sh_info fields while we're at it. */
1252
1253 /* sh_link of a reloc section is the section index of the symbol
1254 table. sh_info is the section index of the section to which
1255 the relocation entries apply. */
1256 if (d->rel_idx != 0)
1257 {
1258 d->rel_hdr.sh_link = t->symtab_section;
1259 d->rel_hdr.sh_info = d->this_idx;
1260 }
1261
1262 switch (d->this_hdr.sh_type)
1263 {
1264 case SHT_REL:
1265 case SHT_RELA:
1266 /* A reloc section which we are treating as a normal BFD
1267 section. sh_link is the section index of the symbol
1268 table. sh_info is the section index of the section to
1269 which the relocation entries apply. We assume that an
1270 allocated reloc section uses the dynamic symbol table.
1271 FIXME: How can we be sure? */
1272 s = bfd_get_section_by_name (abfd, ".dynsym");
1273 if (s != NULL)
1274 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1275
1276 /* We look up the section the relocs apply to by name. */
1277 name = sec->name;
1278 if (d->this_hdr.sh_type == SHT_REL)
1279 name += 4;
1280 else
1281 name += 5;
1282 s = bfd_get_section_by_name (abfd, name);
1283 if (s != NULL)
1284 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1285 break;
1286
1287 case SHT_STRTAB:
1288 /* We assume that a section named .stab*str is a stabs
1289 string section. We look for a section with the same name
1290 but without the trailing ``str'', and set its sh_link
1291 field to point to this section. */
1292 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1293 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1294 {
1295 size_t len;
1296 char *alc;
1297
1298 len = strlen (sec->name);
1299 alc = (char *) bfd_malloc (len - 2);
1300 if (alc == NULL)
1301 return false;
1302 strncpy (alc, sec->name, len - 3);
1303 alc[len - 3] = '\0';
1304 s = bfd_get_section_by_name (abfd, alc);
1305 free (alc);
1306 if (s != NULL)
1307 {
1308 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1309
1310 /* This is a .stab section. */
1311 elf_section_data (s)->this_hdr.sh_entsize =
1312 4 + 2 * (bed->s->arch_size / 8);
1313 }
1314 }
1315 break;
1316
1317 case SHT_DYNAMIC:
1318 case SHT_DYNSYM:
1319 /* sh_link is the section header index of the string table
1320 used for the dynamic entries or symbol table. */
1321 s = bfd_get_section_by_name (abfd, ".dynstr");
1322 if (s != NULL)
1323 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1324 break;
1325
1326 case SHT_HASH:
1327 /* sh_link is the section header index of the symbol table
1328 this hash table is for. */
1329 s = bfd_get_section_by_name (abfd, ".dynsym");
1330 if (s != NULL)
1331 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1332 break;
1333 }
1334 }
1335
1336 return true;
1337 }
1338
1339 /* Map symbol from it's internal number to the external number, moving
1340 all local symbols to be at the head of the list. */
1341
1342 static INLINE int
1343 sym_is_global (abfd, sym)
1344 bfd *abfd;
1345 asymbol *sym;
1346 {
1347 /* If the backend has a special mapping, use it. */
1348 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1349 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1350 (abfd, sym));
1351
1352 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1353 || bfd_is_und_section (bfd_get_section (sym))
1354 || bfd_is_com_section (bfd_get_section (sym)));
1355 }
1356
1357 static boolean
1358 elf_map_symbols (abfd)
1359 bfd *abfd;
1360 {
1361 int symcount = bfd_get_symcount (abfd);
1362 asymbol **syms = bfd_get_outsymbols (abfd);
1363 asymbol **sect_syms;
1364 int num_locals = 0;
1365 int num_globals = 0;
1366 int num_locals2 = 0;
1367 int num_globals2 = 0;
1368 int max_index = 0;
1369 int num_sections = 0;
1370 int idx;
1371 asection *asect;
1372 asymbol **new_syms;
1373
1374 #ifdef DEBUG
1375 fprintf (stderr, "elf_map_symbols\n");
1376 fflush (stderr);
1377 #endif
1378
1379 /* Add a section symbol for each BFD section. FIXME: Is this really
1380 necessary? */
1381 for (asect = abfd->sections; asect; asect = asect->next)
1382 {
1383 if (max_index < asect->index)
1384 max_index = asect->index;
1385 }
1386
1387 max_index++;
1388 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1389 if (sect_syms == NULL)
1390 return false;
1391 elf_section_syms (abfd) = sect_syms;
1392
1393 for (idx = 0; idx < symcount; idx++)
1394 {
1395 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1396 && (syms[idx]->value + syms[idx]->section->vma) == 0)
1397 {
1398 asection *sec;
1399
1400 sec = syms[idx]->section;
1401 if (sec->owner != NULL)
1402 {
1403 if (sec->owner != abfd)
1404 {
1405 if (sec->output_offset != 0)
1406 continue;
1407 sec = sec->output_section;
1408 BFD_ASSERT (sec->owner == abfd);
1409 }
1410 sect_syms[sec->index] = syms[idx];
1411 }
1412 }
1413 }
1414
1415 for (asect = abfd->sections; asect; asect = asect->next)
1416 {
1417 asymbol *sym;
1418
1419 if (sect_syms[asect->index] != NULL)
1420 continue;
1421
1422 sym = bfd_make_empty_symbol (abfd);
1423 if (sym == NULL)
1424 return false;
1425 sym->the_bfd = abfd;
1426 sym->name = asect->name;
1427 sym->value = 0;
1428 /* Set the flags to 0 to indicate that this one was newly added. */
1429 sym->flags = 0;
1430 sym->section = asect;
1431 sect_syms[asect->index] = sym;
1432 num_sections++;
1433 #ifdef DEBUG
1434 fprintf (stderr,
1435 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1436 asect->name, (long) asect->vma, asect->index, (long) asect);
1437 #endif
1438 }
1439
1440 /* Classify all of the symbols. */
1441 for (idx = 0; idx < symcount; idx++)
1442 {
1443 if (!sym_is_global (abfd, syms[idx]))
1444 num_locals++;
1445 else
1446 num_globals++;
1447 }
1448 for (asect = abfd->sections; asect; asect = asect->next)
1449 {
1450 if (sect_syms[asect->index] != NULL
1451 && sect_syms[asect->index]->flags == 0)
1452 {
1453 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1454 if (!sym_is_global (abfd, sect_syms[asect->index]))
1455 num_locals++;
1456 else
1457 num_globals++;
1458 sect_syms[asect->index]->flags = 0;
1459 }
1460 }
1461
1462 /* Now sort the symbols so the local symbols are first. */
1463 new_syms = ((asymbol **)
1464 bfd_alloc (abfd,
1465 (num_locals + num_globals) * sizeof (asymbol *)));
1466 if (new_syms == NULL)
1467 return false;
1468
1469 for (idx = 0; idx < symcount; idx++)
1470 {
1471 asymbol *sym = syms[idx];
1472 int i;
1473
1474 if (!sym_is_global (abfd, sym))
1475 i = num_locals2++;
1476 else
1477 i = num_locals + num_globals2++;
1478 new_syms[i] = sym;
1479 sym->udata.i = i + 1;
1480 }
1481 for (asect = abfd->sections; asect; asect = asect->next)
1482 {
1483 if (sect_syms[asect->index] != NULL
1484 && sect_syms[asect->index]->flags == 0)
1485 {
1486 asymbol *sym = sect_syms[asect->index];
1487 int i;
1488
1489 sym->flags = BSF_SECTION_SYM;
1490 if (!sym_is_global (abfd, sym))
1491 i = num_locals2++;
1492 else
1493 i = num_locals + num_globals2++;
1494 new_syms[i] = sym;
1495 sym->udata.i = i + 1;
1496 }
1497 }
1498
1499 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1500
1501 elf_num_locals (abfd) = num_locals;
1502 elf_num_globals (abfd) = num_globals;
1503 return true;
1504 }
1505
1506 /* Align to the maximum file alignment that could be required for any
1507 ELF data structure. */
1508
1509 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1510 static INLINE file_ptr
1511 align_file_position (off, align)
1512 file_ptr off;
1513 int align;
1514 {
1515 return (off + align - 1) & ~(align - 1);
1516 }
1517
1518 /* Assign a file position to a section, optionally aligning to the
1519 required section alignment. */
1520
1521 INLINE file_ptr
1522 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1523 Elf_Internal_Shdr *i_shdrp;
1524 file_ptr offset;
1525 boolean align;
1526 {
1527 if (align)
1528 {
1529 unsigned int al;
1530
1531 al = i_shdrp->sh_addralign;
1532 if (al > 1)
1533 offset = BFD_ALIGN (offset, al);
1534 }
1535 i_shdrp->sh_offset = offset;
1536 if (i_shdrp->bfd_section != NULL)
1537 i_shdrp->bfd_section->filepos = offset;
1538 if (i_shdrp->sh_type != SHT_NOBITS)
1539 offset += i_shdrp->sh_size;
1540 return offset;
1541 }
1542
1543 /* Compute the file positions we are going to put the sections at, and
1544 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1545 is not NULL, this is being called by the ELF backend linker. */
1546
1547 boolean
1548 _bfd_elf_compute_section_file_positions (abfd, link_info)
1549 bfd *abfd;
1550 struct bfd_link_info *link_info;
1551 {
1552 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1553 boolean failed;
1554 struct bfd_strtab_hash *strtab;
1555 Elf_Internal_Shdr *shstrtab_hdr;
1556
1557 if (abfd->output_has_begun)
1558 return true;
1559
1560 /* Do any elf backend specific processing first. */
1561 if (bed->elf_backend_begin_write_processing)
1562 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1563
1564 if (! prep_headers (abfd))
1565 return false;
1566
1567 failed = false;
1568 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1569 if (failed)
1570 return false;
1571
1572 if (!assign_section_numbers (abfd))
1573 return false;
1574
1575 /* The backend linker builds symbol table information itself. */
1576 if (link_info == NULL && abfd->symcount > 0)
1577 {
1578 if (! swap_out_syms (abfd, &strtab))
1579 return false;
1580 }
1581
1582 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1583 /* sh_name was set in prep_headers. */
1584 shstrtab_hdr->sh_type = SHT_STRTAB;
1585 shstrtab_hdr->sh_flags = 0;
1586 shstrtab_hdr->sh_addr = 0;
1587 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1588 shstrtab_hdr->sh_entsize = 0;
1589 shstrtab_hdr->sh_link = 0;
1590 shstrtab_hdr->sh_info = 0;
1591 /* sh_offset is set in assign_file_positions_except_relocs. */
1592 shstrtab_hdr->sh_addralign = 1;
1593
1594 if (!assign_file_positions_except_relocs (abfd))
1595 return false;
1596
1597 if (link_info == NULL && abfd->symcount > 0)
1598 {
1599 file_ptr off;
1600 Elf_Internal_Shdr *hdr;
1601
1602 off = elf_tdata (abfd)->next_file_pos;
1603
1604 hdr = &elf_tdata (abfd)->symtab_hdr;
1605 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1606
1607 hdr = &elf_tdata (abfd)->strtab_hdr;
1608 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1609
1610 elf_tdata (abfd)->next_file_pos = off;
1611
1612 /* Now that we know where the .strtab section goes, write it
1613 out. */
1614 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
1615 || ! _bfd_stringtab_emit (abfd, strtab))
1616 return false;
1617 _bfd_stringtab_free (strtab);
1618 }
1619
1620 abfd->output_has_begun = true;
1621
1622 return true;
1623 }
1624
1625 /* Create a mapping from a set of sections to a program segment. */
1626
1627 static INLINE struct elf_segment_map *
1628 make_mapping (abfd, sections, from, to, phdr)
1629 bfd *abfd;
1630 asection **sections;
1631 unsigned int from;
1632 unsigned int to;
1633 boolean phdr;
1634 {
1635 struct elf_segment_map *m;
1636 unsigned int i;
1637 asection **hdrpp;
1638
1639 m = ((struct elf_segment_map *)
1640 bfd_zalloc (abfd,
1641 (sizeof (struct elf_segment_map)
1642 + (to - from - 1) * sizeof (asection *))));
1643 if (m == NULL)
1644 return NULL;
1645 m->next = NULL;
1646 m->p_type = PT_LOAD;
1647 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1648 m->sections[i - from] = *hdrpp;
1649 m->count = to - from;
1650
1651 if (from == 0 && phdr)
1652 {
1653 /* Include the headers in the first PT_LOAD segment. */
1654 m->includes_filehdr = 1;
1655 m->includes_phdrs = 1;
1656 }
1657
1658 return m;
1659 }
1660
1661 /* Set up a mapping from BFD sections to program segments. */
1662
1663 static boolean
1664 map_sections_to_segments (abfd)
1665 bfd *abfd;
1666 {
1667 asection **sections = NULL;
1668 asection *s;
1669 unsigned int i;
1670 unsigned int count;
1671 struct elf_segment_map *mfirst;
1672 struct elf_segment_map **pm;
1673 struct elf_segment_map *m;
1674 asection *last_hdr;
1675 unsigned int phdr_index;
1676 bfd_vma maxpagesize;
1677 asection **hdrpp;
1678 boolean phdr_in_section = true;
1679 boolean writable;
1680 asection *dynsec;
1681
1682 if (elf_tdata (abfd)->segment_map != NULL)
1683 return true;
1684
1685 if (bfd_count_sections (abfd) == 0)
1686 return true;
1687
1688 /* Select the allocated sections, and sort them. */
1689
1690 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
1691 * sizeof (asection *));
1692 if (sections == NULL)
1693 goto error_return;
1694
1695 i = 0;
1696 for (s = abfd->sections; s != NULL; s = s->next)
1697 {
1698 if ((s->flags & SEC_ALLOC) != 0)
1699 {
1700 sections[i] = s;
1701 ++i;
1702 }
1703 }
1704 BFD_ASSERT (i <= bfd_count_sections (abfd));
1705 count = i;
1706
1707 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
1708
1709 /* Build the mapping. */
1710
1711 mfirst = NULL;
1712 pm = &mfirst;
1713
1714 /* If we have a .interp section, then create a PT_PHDR segment for
1715 the program headers and a PT_INTERP segment for the .interp
1716 section. */
1717 s = bfd_get_section_by_name (abfd, ".interp");
1718 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1719 {
1720 m = ((struct elf_segment_map *)
1721 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1722 if (m == NULL)
1723 goto error_return;
1724 m->next = NULL;
1725 m->p_type = PT_PHDR;
1726 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1727 m->p_flags = PF_R | PF_X;
1728 m->p_flags_valid = 1;
1729 m->includes_phdrs = 1;
1730
1731 *pm = m;
1732 pm = &m->next;
1733
1734 m = ((struct elf_segment_map *)
1735 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1736 if (m == NULL)
1737 goto error_return;
1738 m->next = NULL;
1739 m->p_type = PT_INTERP;
1740 m->count = 1;
1741 m->sections[0] = s;
1742
1743 *pm = m;
1744 pm = &m->next;
1745 }
1746
1747 /* Look through the sections. We put sections in the same program
1748 segment when the start of the second section can be placed within
1749 a few bytes of the end of the first section. */
1750 last_hdr = NULL;
1751 phdr_index = 0;
1752 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1753 writable = false;
1754 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
1755 if (dynsec != NULL
1756 && (dynsec->flags & SEC_LOAD) == 0)
1757 dynsec = NULL;
1758
1759 /* Deal with -Ttext or something similar such that the first section
1760 is not adjacent to the program headers. This is an
1761 approximation, since at this point we don't know exactly how many
1762 program headers we will need. */
1763 if (count > 0)
1764 {
1765 bfd_size_type phdr_size;
1766
1767 phdr_size = elf_tdata (abfd)->program_header_size;
1768 if (phdr_size == 0)
1769 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
1770 if ((abfd->flags & D_PAGED) == 0
1771 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
1772 phdr_in_section = false;
1773 }
1774
1775 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
1776 {
1777 asection *hdr;
1778 boolean new_segment;
1779
1780 hdr = *hdrpp;
1781
1782 /* See if this section and the last one will fit in the same
1783 segment. */
1784
1785 if (last_hdr == NULL)
1786 {
1787 /* If we don't have a segment yet, then we don't need a new
1788 one (we build the last one after this loop). */
1789 new_segment = false;
1790 }
1791 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
1792 {
1793 /* If this section has a different relation between the
1794 virtual address and the load address, then we need a new
1795 segment. */
1796 new_segment = true;
1797 }
1798 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1799 < hdr->lma)
1800 {
1801 /* If putting this section in this segment would force us to
1802 skip a page in the segment, then we need a new segment. */
1803 new_segment = true;
1804 }
1805 else if ((abfd->flags & D_PAGED) == 0)
1806 {
1807 /* If the file is not demand paged, which means that we
1808 don't require the sections to be correctly aligned in the
1809 file, then there is no other reason for a new segment. */
1810 new_segment = false;
1811 }
1812 else if ((last_hdr->flags & SEC_LOAD) == 0
1813 && (hdr->flags & SEC_LOAD) != 0)
1814 {
1815 /* We don't want to put a loadable section after a
1816 nonloadable section in the same segment. */
1817 new_segment = true;
1818 }
1819 else if (! writable
1820 && (hdr->flags & SEC_READONLY) == 0
1821 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1822 == hdr->lma))
1823 {
1824 /* We don't want to put a writable section in a read only
1825 segment, unless they are on the same page in memory
1826 anyhow. We already know that the last section does not
1827 bring us past the current section on the page, so the
1828 only case in which the new section is not on the same
1829 page as the previous section is when the previous section
1830 ends precisely on a page boundary. */
1831 new_segment = true;
1832 }
1833 else
1834 {
1835 /* Otherwise, we can use the same segment. */
1836 new_segment = false;
1837 }
1838
1839 if (! new_segment)
1840 {
1841 if ((hdr->flags & SEC_READONLY) == 0)
1842 writable = true;
1843 last_hdr = hdr;
1844 continue;
1845 }
1846
1847 /* We need a new program segment. We must create a new program
1848 header holding all the sections from phdr_index until hdr. */
1849
1850 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
1851 if (m == NULL)
1852 goto error_return;
1853
1854 *pm = m;
1855 pm = &m->next;
1856
1857 if ((hdr->flags & SEC_READONLY) == 0)
1858 writable = true;
1859 else
1860 writable = false;
1861
1862 last_hdr = hdr;
1863 phdr_index = i;
1864 phdr_in_section = false;
1865 }
1866
1867 /* Create a final PT_LOAD program segment. */
1868 if (last_hdr != NULL)
1869 {
1870 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
1871 if (m == NULL)
1872 goto error_return;
1873
1874 *pm = m;
1875 pm = &m->next;
1876 }
1877
1878 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
1879 if (dynsec != NULL)
1880 {
1881 m = ((struct elf_segment_map *)
1882 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1883 if (m == NULL)
1884 goto error_return;
1885 m->next = NULL;
1886 m->p_type = PT_DYNAMIC;
1887 m->count = 1;
1888 m->sections[0] = dynsec;
1889
1890 *pm = m;
1891 pm = &m->next;
1892 }
1893
1894 free (sections);
1895 sections = NULL;
1896
1897 elf_tdata (abfd)->segment_map = mfirst;
1898 return true;
1899
1900 error_return:
1901 if (sections != NULL)
1902 free (sections);
1903 return false;
1904 }
1905
1906 /* Sort sections by VMA. */
1907
1908 static int
1909 elf_sort_sections (arg1, arg2)
1910 const PTR arg1;
1911 const PTR arg2;
1912 {
1913 const asection *sec1 = *(const asection **) arg1;
1914 const asection *sec2 = *(const asection **) arg2;
1915
1916 if (sec1->vma < sec2->vma)
1917 return -1;
1918 else if (sec1->vma > sec2->vma)
1919 return 1;
1920
1921 /* Sort by LMA. Normally the LMA and the VMA will be the same, and
1922 this will do nothing. */
1923 if (sec1->lma < sec2->lma)
1924 return -1;
1925 else if (sec1->lma > sec2->lma)
1926 return 1;
1927
1928 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
1929
1930 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1931
1932 if (TOEND (sec1))
1933 if (TOEND (sec2))
1934 return sec1->target_index - sec2->target_index;
1935 else
1936 return 1;
1937
1938 if (TOEND (sec2))
1939 return -1;
1940
1941 #undef TOEND
1942
1943 /* Sort by size, to put zero sized sections before others at the
1944 same address. */
1945
1946 if (sec1->_raw_size < sec2->_raw_size)
1947 return -1;
1948 if (sec1->_raw_size > sec2->_raw_size)
1949 return 1;
1950
1951 return sec1->target_index - sec2->target_index;
1952 }
1953
1954 /* Assign file positions to the sections based on the mapping from
1955 sections to segments. This function also sets up some fields in
1956 the file header, and writes out the program headers. */
1957
1958 static boolean
1959 assign_file_positions_for_segments (abfd)
1960 bfd *abfd;
1961 {
1962 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1963 unsigned int count;
1964 struct elf_segment_map *m;
1965 unsigned int alloc;
1966 Elf_Internal_Phdr *phdrs;
1967 file_ptr off, voff;
1968 bfd_vma filehdr_vaddr, filehdr_paddr;
1969 bfd_vma phdrs_vaddr, phdrs_paddr;
1970 Elf_Internal_Phdr *p;
1971
1972 if (elf_tdata (abfd)->segment_map == NULL)
1973 {
1974 if (! map_sections_to_segments (abfd))
1975 return false;
1976 }
1977
1978 if (bed->elf_backend_modify_segment_map)
1979 {
1980 if (! (*bed->elf_backend_modify_segment_map) (abfd))
1981 return false;
1982 }
1983
1984 count = 0;
1985 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1986 ++count;
1987
1988 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
1989 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
1990 elf_elfheader (abfd)->e_phnum = count;
1991
1992 if (count == 0)
1993 return true;
1994
1995 /* If we already counted the number of program segments, make sure
1996 that we allocated enough space. This happens when SIZEOF_HEADERS
1997 is used in a linker script. */
1998 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
1999 if (alloc != 0 && count > alloc)
2000 {
2001 ((*_bfd_error_handler)
2002 ("%s: Not enough room for program headers (allocated %u, need %u)",
2003 bfd_get_filename (abfd), alloc, count));
2004 bfd_set_error (bfd_error_bad_value);
2005 return false;
2006 }
2007
2008 if (alloc == 0)
2009 alloc = count;
2010
2011 phdrs = ((Elf_Internal_Phdr *)
2012 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2013 if (phdrs == NULL)
2014 return false;
2015
2016 off = bed->s->sizeof_ehdr;
2017 off += alloc * bed->s->sizeof_phdr;
2018
2019 filehdr_vaddr = 0;
2020 filehdr_paddr = 0;
2021 phdrs_vaddr = 0;
2022 phdrs_paddr = 0;
2023 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2024 m != NULL;
2025 m = m->next, p++)
2026 {
2027 unsigned int i;
2028 asection **secpp;
2029
2030 /* If elf_segment_map is not from map_sections_to_segments, the
2031 sections may not be correctly ordered. */
2032 if (m->count > 0)
2033 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2034 elf_sort_sections);
2035
2036 p->p_type = m->p_type;
2037
2038 if (m->p_flags_valid)
2039 p->p_flags = m->p_flags;
2040 else
2041 p->p_flags = 0;
2042
2043 if (p->p_type == PT_LOAD
2044 && m->count > 0
2045 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2046 {
2047 if ((abfd->flags & D_PAGED) != 0)
2048 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2049 else
2050 off += ((m->sections[0]->vma - off)
2051 % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2052 }
2053
2054 if (m->count == 0)
2055 p->p_vaddr = 0;
2056 else
2057 p->p_vaddr = m->sections[0]->vma;
2058
2059 if (m->p_paddr_valid)
2060 p->p_paddr = m->p_paddr;
2061 else if (m->count == 0)
2062 p->p_paddr = 0;
2063 else
2064 p->p_paddr = m->sections[0]->lma;
2065
2066 if (p->p_type == PT_LOAD
2067 && (abfd->flags & D_PAGED) != 0)
2068 p->p_align = bed->maxpagesize;
2069 else if (m->count == 0)
2070 p->p_align = bed->s->file_align;
2071 else
2072 p->p_align = 0;
2073
2074 p->p_offset = 0;
2075 p->p_filesz = 0;
2076 p->p_memsz = 0;
2077
2078 if (m->includes_filehdr)
2079 {
2080 if (! m->p_flags_valid)
2081 p->p_flags |= PF_R;
2082 p->p_offset = 0;
2083 p->p_filesz = bed->s->sizeof_ehdr;
2084 p->p_memsz = bed->s->sizeof_ehdr;
2085 if (m->count > 0)
2086 {
2087 BFD_ASSERT (p->p_type == PT_LOAD);
2088 p->p_vaddr -= off;
2089 if (! m->p_paddr_valid)
2090 p->p_paddr -= off;
2091 }
2092 if (p->p_type == PT_LOAD)
2093 {
2094 filehdr_vaddr = p->p_vaddr;
2095 filehdr_paddr = p->p_paddr;
2096 }
2097 }
2098
2099 if (m->includes_phdrs)
2100 {
2101 if (! m->p_flags_valid)
2102 p->p_flags |= PF_R;
2103 if (m->includes_filehdr)
2104 {
2105 if (p->p_type == PT_LOAD)
2106 {
2107 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2108 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2109 }
2110 }
2111 else
2112 {
2113 p->p_offset = bed->s->sizeof_ehdr;
2114 if (m->count > 0)
2115 {
2116 BFD_ASSERT (p->p_type == PT_LOAD);
2117 p->p_vaddr -= off - p->p_offset;
2118 if (! m->p_paddr_valid)
2119 p->p_paddr -= off - p->p_offset;
2120 }
2121 if (p->p_type == PT_LOAD)
2122 {
2123 phdrs_vaddr = p->p_vaddr;
2124 phdrs_paddr = p->p_paddr;
2125 }
2126 }
2127 p->p_filesz += alloc * bed->s->sizeof_phdr;
2128 p->p_memsz += alloc * bed->s->sizeof_phdr;
2129 }
2130
2131 if (p->p_type == PT_LOAD)
2132 {
2133 if (! m->includes_filehdr && ! m->includes_phdrs)
2134 p->p_offset = off;
2135 else
2136 {
2137 file_ptr adjust;
2138
2139 adjust = off - (p->p_offset + p->p_filesz);
2140 p->p_filesz += adjust;
2141 p->p_memsz += adjust;
2142 }
2143 }
2144
2145 voff = off;
2146 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2147 {
2148 asection *sec;
2149 flagword flags;
2150 bfd_size_type align;
2151
2152 sec = *secpp;
2153 flags = sec->flags;
2154 align = 1 << bfd_get_section_alignment (abfd, sec);
2155
2156 if (p->p_type == PT_LOAD)
2157 {
2158 bfd_vma adjust;
2159
2160 /* The section VMA must equal the file position modulo
2161 the page size. */
2162 if ((flags & SEC_ALLOC) != 0)
2163 {
2164 if ((abfd->flags & D_PAGED) != 0)
2165 adjust = (sec->vma - voff) % bed->maxpagesize;
2166 else
2167 adjust = (sec->vma - voff) % align;
2168 if (adjust != 0)
2169 {
2170 if (i == 0)
2171 abort ();
2172 p->p_memsz += adjust;
2173 off += adjust;
2174 voff += adjust;
2175 if ((flags & SEC_LOAD) != 0)
2176 p->p_filesz += adjust;
2177 }
2178 }
2179
2180 sec->filepos = off;
2181
2182 if ((flags & SEC_LOAD) != 0)
2183 off += sec->_raw_size;
2184 if ((flags & SEC_ALLOC) != 0)
2185 voff += sec->_raw_size;
2186 }
2187
2188 p->p_memsz += sec->_raw_size;
2189
2190 if ((flags & SEC_LOAD) != 0)
2191 p->p_filesz += sec->_raw_size;
2192
2193 if (align > p->p_align)
2194 p->p_align = align;
2195
2196 if (! m->p_flags_valid)
2197 {
2198 p->p_flags |= PF_R;
2199 if ((flags & SEC_CODE) != 0)
2200 p->p_flags |= PF_X;
2201 if ((flags & SEC_READONLY) == 0)
2202 p->p_flags |= PF_W;
2203 }
2204 }
2205 }
2206
2207 /* Now that we have set the section file positions, we can set up
2208 the file positions for the non PT_LOAD segments. */
2209 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2210 m != NULL;
2211 m = m->next, p++)
2212 {
2213 if (p->p_type != PT_LOAD && m->count > 0)
2214 {
2215 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2216 p->p_offset = m->sections[0]->filepos;
2217 }
2218 if (m->count == 0)
2219 {
2220 if (m->includes_filehdr)
2221 {
2222 p->p_vaddr = filehdr_vaddr;
2223 if (! m->p_paddr_valid)
2224 p->p_paddr = filehdr_paddr;
2225 }
2226 else if (m->includes_phdrs)
2227 {
2228 p->p_vaddr = phdrs_vaddr;
2229 if (! m->p_paddr_valid)
2230 p->p_paddr = phdrs_paddr;
2231 }
2232 }
2233 }
2234
2235 /* Clear out any program headers we allocated but did not use. */
2236 for (; count < alloc; count++, p++)
2237 {
2238 memset (p, 0, sizeof *p);
2239 p->p_type = PT_NULL;
2240 }
2241
2242 elf_tdata (abfd)->phdr = phdrs;
2243
2244 elf_tdata (abfd)->next_file_pos = off;
2245
2246 /* Write out the program headers. */
2247 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2248 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2249 return false;
2250
2251 return true;
2252 }
2253
2254 /* Get the size of the program header.
2255
2256 If this is called by the linker before any of the section VMA's are set, it
2257 can't calculate the correct value for a strange memory layout. This only
2258 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2259 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2260 data segment (exclusive of .interp and .dynamic).
2261
2262 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2263 will be two segments. */
2264
2265 static bfd_size_type
2266 get_program_header_size (abfd)
2267 bfd *abfd;
2268 {
2269 size_t segs;
2270 asection *s;
2271 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2272
2273 /* We can't return a different result each time we're called. */
2274 if (elf_tdata (abfd)->program_header_size != 0)
2275 return elf_tdata (abfd)->program_header_size;
2276
2277 if (elf_tdata (abfd)->segment_map != NULL)
2278 {
2279 struct elf_segment_map *m;
2280
2281 segs = 0;
2282 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2283 ++segs;
2284 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2285 return elf_tdata (abfd)->program_header_size;
2286 }
2287
2288 /* Assume we will need exactly two PT_LOAD segments: one for text
2289 and one for data. */
2290 segs = 2;
2291
2292 s = bfd_get_section_by_name (abfd, ".interp");
2293 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2294 {
2295 /* If we have a loadable interpreter section, we need a
2296 PT_INTERP segment. In this case, assume we also need a
2297 PT_PHDR segment, although that may not be true for all
2298 targets. */
2299 segs += 2;
2300 }
2301
2302 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2303 {
2304 /* We need a PT_DYNAMIC segment. */
2305 ++segs;
2306 }
2307
2308 /* Let the backend count up any program headers it might need. */
2309 if (bed->elf_backend_additional_program_headers)
2310 {
2311 int a;
2312
2313 a = (*bed->elf_backend_additional_program_headers) (abfd);
2314 if (a == -1)
2315 abort ();
2316 segs += a;
2317 }
2318
2319 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2320 return elf_tdata (abfd)->program_header_size;
2321 }
2322
2323 /* Work out the file positions of all the sections. This is called by
2324 _bfd_elf_compute_section_file_positions. All the section sizes and
2325 VMAs must be known before this is called.
2326
2327 We do not consider reloc sections at this point, unless they form
2328 part of the loadable image. Reloc sections are assigned file
2329 positions in assign_file_positions_for_relocs, which is called by
2330 write_object_contents and final_link.
2331
2332 We also don't set the positions of the .symtab and .strtab here. */
2333
2334 static boolean
2335 assign_file_positions_except_relocs (abfd)
2336 bfd *abfd;
2337 {
2338 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2339 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2340 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2341 file_ptr off;
2342 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2343
2344 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2345 {
2346 Elf_Internal_Shdr **hdrpp;
2347 unsigned int i;
2348
2349 /* Start after the ELF header. */
2350 off = i_ehdrp->e_ehsize;
2351
2352 /* We are not creating an executable, which means that we are
2353 not creating a program header, and that the actual order of
2354 the sections in the file is unimportant. */
2355 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2356 {
2357 Elf_Internal_Shdr *hdr;
2358
2359 hdr = *hdrpp;
2360 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2361 {
2362 hdr->sh_offset = -1;
2363 continue;
2364 }
2365 if (i == tdata->symtab_section
2366 || i == tdata->strtab_section)
2367 {
2368 hdr->sh_offset = -1;
2369 continue;
2370 }
2371
2372 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2373 }
2374 }
2375 else
2376 {
2377 unsigned int i;
2378 Elf_Internal_Shdr **hdrpp;
2379
2380 /* Assign file positions for the loaded sections based on the
2381 assignment of sections to segments. */
2382 if (! assign_file_positions_for_segments (abfd))
2383 return false;
2384
2385 /* Assign file positions for the other sections. */
2386
2387 off = elf_tdata (abfd)->next_file_pos;
2388 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2389 {
2390 Elf_Internal_Shdr *hdr;
2391
2392 hdr = *hdrpp;
2393 if (hdr->bfd_section != NULL
2394 && hdr->bfd_section->filepos != 0)
2395 hdr->sh_offset = hdr->bfd_section->filepos;
2396 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2397 {
2398 ((*_bfd_error_handler)
2399 ("%s: warning: allocated section `%s' not in segment",
2400 bfd_get_filename (abfd),
2401 (hdr->bfd_section == NULL
2402 ? "*unknown*"
2403 : hdr->bfd_section->name)));
2404 if ((abfd->flags & D_PAGED) != 0)
2405 off += (hdr->sh_addr - off) % bed->maxpagesize;
2406 else
2407 off += (hdr->sh_addr - off) % hdr->sh_addralign;
2408 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2409 false);
2410 }
2411 else if (hdr->sh_type == SHT_REL
2412 || hdr->sh_type == SHT_RELA
2413 || hdr == i_shdrpp[tdata->symtab_section]
2414 || hdr == i_shdrpp[tdata->strtab_section])
2415 hdr->sh_offset = -1;
2416 else
2417 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2418 }
2419 }
2420
2421 /* Place the section headers. */
2422 off = align_file_position (off, bed->s->file_align);
2423 i_ehdrp->e_shoff = off;
2424 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2425
2426 elf_tdata (abfd)->next_file_pos = off;
2427
2428 return true;
2429 }
2430
2431 static boolean
2432 prep_headers (abfd)
2433 bfd *abfd;
2434 {
2435 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2436 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2437 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2438 int count;
2439 struct bfd_strtab_hash *shstrtab;
2440 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2441
2442 i_ehdrp = elf_elfheader (abfd);
2443 i_shdrp = elf_elfsections (abfd);
2444
2445 shstrtab = _bfd_elf_stringtab_init ();
2446 if (shstrtab == NULL)
2447 return false;
2448
2449 elf_shstrtab (abfd) = shstrtab;
2450
2451 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2452 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2453 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2454 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2455
2456 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2457 i_ehdrp->e_ident[EI_DATA] =
2458 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
2459 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2460
2461 for (count = EI_PAD; count < EI_NIDENT; count++)
2462 i_ehdrp->e_ident[count] = 0;
2463
2464 if ((abfd->flags & DYNAMIC) != 0)
2465 i_ehdrp->e_type = ET_DYN;
2466 else if ((abfd->flags & EXEC_P) != 0)
2467 i_ehdrp->e_type = ET_EXEC;
2468 else
2469 i_ehdrp->e_type = ET_REL;
2470
2471 switch (bfd_get_arch (abfd))
2472 {
2473 case bfd_arch_unknown:
2474 i_ehdrp->e_machine = EM_NONE;
2475 break;
2476 case bfd_arch_sparc:
2477 if (bed->s->arch_size == 64)
2478 i_ehdrp->e_machine = EM_SPARC64;
2479 else
2480 i_ehdrp->e_machine = EM_SPARC;
2481 break;
2482 case bfd_arch_i386:
2483 i_ehdrp->e_machine = EM_386;
2484 break;
2485 case bfd_arch_m68k:
2486 i_ehdrp->e_machine = EM_68K;
2487 break;
2488 case bfd_arch_m88k:
2489 i_ehdrp->e_machine = EM_88K;
2490 break;
2491 case bfd_arch_i860:
2492 i_ehdrp->e_machine = EM_860;
2493 break;
2494 case bfd_arch_mips: /* MIPS Rxxxx */
2495 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2496 break;
2497 case bfd_arch_hppa:
2498 i_ehdrp->e_machine = EM_PARISC;
2499 break;
2500 case bfd_arch_powerpc:
2501 i_ehdrp->e_machine = EM_PPC;
2502 break;
2503 case bfd_arch_alpha:
2504 i_ehdrp->e_machine = EM_ALPHA;
2505 break;
2506 case bfd_arch_sh:
2507 i_ehdrp->e_machine = EM_SH;
2508 break;
2509 /* start-sanitize-d10v */
2510 case bfd_arch_d10v:
2511 i_ehdrp->e_machine = EM_CYGNUS_D10V;
2512 break;
2513 /* end-sanitize-d10v */
2514 /* start-sanitize-v850 */
2515 case bfd_arch_v850:
2516 i_ehdrp->e_machine = EM_CYGNUS_V850;
2517 break;
2518 /* end-sanitize-v850 */
2519 /* start-sanitize-arc */
2520 case bfd_arch_arc:
2521 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2522 break;
2523 /* end-sanitize-arc */
2524 /* start-sanitize-m32r */
2525 case bfd_arch_m32r:
2526 i_ehdrp->e_machine = EM_CYGNUS_M32R;
2527 break;
2528 /* end-sanitize-m32r */
2529 case bfd_arch_mn10200:
2530 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
2531 break;
2532 case bfd_arch_mn10300:
2533 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
2534 break;
2535 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2536 default:
2537 i_ehdrp->e_machine = EM_NONE;
2538 }
2539 i_ehdrp->e_version = bed->s->ev_current;
2540 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2541
2542 /* no program header, for now. */
2543 i_ehdrp->e_phoff = 0;
2544 i_ehdrp->e_phentsize = 0;
2545 i_ehdrp->e_phnum = 0;
2546
2547 /* each bfd section is section header entry */
2548 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2549 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2550
2551 /* if we're building an executable, we'll need a program header table */
2552 if (abfd->flags & EXEC_P)
2553 {
2554 /* it all happens later */
2555 #if 0
2556 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2557
2558 /* elf_build_phdrs() returns a (NULL-terminated) array of
2559 Elf_Internal_Phdrs */
2560 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2561 i_ehdrp->e_phoff = outbase;
2562 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2563 #endif
2564 }
2565 else
2566 {
2567 i_ehdrp->e_phentsize = 0;
2568 i_phdrp = 0;
2569 i_ehdrp->e_phoff = 0;
2570 }
2571
2572 elf_tdata (abfd)->symtab_hdr.sh_name =
2573 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2574 elf_tdata (abfd)->strtab_hdr.sh_name =
2575 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2576 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2577 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2578 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2579 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2580 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2581 return false;
2582
2583 return true;
2584 }
2585
2586 /* Assign file positions for all the reloc sections which are not part
2587 of the loadable file image. */
2588
2589 void
2590 _bfd_elf_assign_file_positions_for_relocs (abfd)
2591 bfd *abfd;
2592 {
2593 file_ptr off;
2594 unsigned int i;
2595 Elf_Internal_Shdr **shdrpp;
2596
2597 off = elf_tdata (abfd)->next_file_pos;
2598
2599 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2600 i < elf_elfheader (abfd)->e_shnum;
2601 i++, shdrpp++)
2602 {
2603 Elf_Internal_Shdr *shdrp;
2604
2605 shdrp = *shdrpp;
2606 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2607 && shdrp->sh_offset == -1)
2608 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
2609 }
2610
2611 elf_tdata (abfd)->next_file_pos = off;
2612 }
2613
2614 boolean
2615 _bfd_elf_write_object_contents (abfd)
2616 bfd *abfd;
2617 {
2618 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2619 Elf_Internal_Ehdr *i_ehdrp;
2620 Elf_Internal_Shdr **i_shdrp;
2621 boolean failed;
2622 unsigned int count;
2623
2624 if (! abfd->output_has_begun
2625 && ! _bfd_elf_compute_section_file_positions (abfd,
2626 (struct bfd_link_info *) NULL))
2627 return false;
2628
2629 i_shdrp = elf_elfsections (abfd);
2630 i_ehdrp = elf_elfheader (abfd);
2631
2632 failed = false;
2633 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2634 if (failed)
2635 return false;
2636 _bfd_elf_assign_file_positions_for_relocs (abfd);
2637
2638 /* After writing the headers, we need to write the sections too... */
2639 for (count = 1; count < i_ehdrp->e_shnum; count++)
2640 {
2641 if (bed->elf_backend_section_processing)
2642 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2643 if (i_shdrp[count]->contents)
2644 {
2645 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2646 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2647 1, abfd)
2648 != i_shdrp[count]->sh_size))
2649 return false;
2650 }
2651 }
2652
2653 /* Write out the section header names. */
2654 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2655 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2656 return false;
2657
2658 if (bed->elf_backend_final_write_processing)
2659 (*bed->elf_backend_final_write_processing) (abfd,
2660 elf_tdata (abfd)->linker);
2661
2662 return bed->s->write_shdrs_and_ehdr (abfd);
2663 }
2664
2665 /* given a section, search the header to find them... */
2666 int
2667 _bfd_elf_section_from_bfd_section (abfd, asect)
2668 bfd *abfd;
2669 struct sec *asect;
2670 {
2671 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2672 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2673 int index;
2674 Elf_Internal_Shdr *hdr;
2675 int maxindex = elf_elfheader (abfd)->e_shnum;
2676
2677 for (index = 0; index < maxindex; index++)
2678 {
2679 hdr = i_shdrp[index];
2680 if (hdr->bfd_section == asect)
2681 return index;
2682 }
2683
2684 if (bed->elf_backend_section_from_bfd_section)
2685 {
2686 for (index = 0; index < maxindex; index++)
2687 {
2688 int retval;
2689
2690 hdr = i_shdrp[index];
2691 retval = index;
2692 if ((*bed->elf_backend_section_from_bfd_section)
2693 (abfd, hdr, asect, &retval))
2694 return retval;
2695 }
2696 }
2697
2698 if (bfd_is_abs_section (asect))
2699 return SHN_ABS;
2700 if (bfd_is_com_section (asect))
2701 return SHN_COMMON;
2702 if (bfd_is_und_section (asect))
2703 return SHN_UNDEF;
2704
2705 return -1;
2706 }
2707
2708 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
2709 on error. */
2710
2711 int
2712 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2713 bfd *abfd;
2714 asymbol **asym_ptr_ptr;
2715 {
2716 asymbol *asym_ptr = *asym_ptr_ptr;
2717 int idx;
2718 flagword flags = asym_ptr->flags;
2719
2720 /* When gas creates relocations against local labels, it creates its
2721 own symbol for the section, but does put the symbol into the
2722 symbol chain, so udata is 0. When the linker is generating
2723 relocatable output, this section symbol may be for one of the
2724 input sections rather than the output section. */
2725 if (asym_ptr->udata.i == 0
2726 && (flags & BSF_SECTION_SYM)
2727 && asym_ptr->section)
2728 {
2729 int indx;
2730
2731 if (asym_ptr->section->output_section != NULL)
2732 indx = asym_ptr->section->output_section->index;
2733 else
2734 indx = asym_ptr->section->index;
2735 if (elf_section_syms (abfd)[indx])
2736 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
2737 }
2738
2739 idx = asym_ptr->udata.i;
2740
2741 if (idx == 0)
2742 {
2743 /* This case can occur when using --strip-symbol on a symbol
2744 which is used in a relocation entry. */
2745 (*_bfd_error_handler)
2746 ("%s: symbol `%s' required but not present",
2747 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
2748 bfd_set_error (bfd_error_no_symbols);
2749 return -1;
2750 }
2751
2752 #if DEBUG & 4
2753 {
2754 fprintf (stderr,
2755 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2756 (long) asym_ptr, asym_ptr->name, idx, flags,
2757 elf_symbol_flags (flags));
2758 fflush (stderr);
2759 }
2760 #endif
2761
2762 return idx;
2763 }
2764
2765 /* Copy private BFD data. This copies any program header information. */
2766
2767 static boolean
2768 copy_private_bfd_data (ibfd, obfd)
2769 bfd *ibfd;
2770 bfd *obfd;
2771 {
2772 Elf_Internal_Ehdr *iehdr;
2773 struct elf_segment_map *mfirst;
2774 struct elf_segment_map **pm;
2775 Elf_Internal_Phdr *p;
2776 unsigned int i, c;
2777
2778 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2779 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2780 return true;
2781
2782 if (elf_tdata (ibfd)->phdr == NULL)
2783 return true;
2784
2785 iehdr = elf_elfheader (ibfd);
2786
2787 mfirst = NULL;
2788 pm = &mfirst;
2789
2790 c = elf_elfheader (ibfd)->e_phnum;
2791 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
2792 {
2793 unsigned int csecs;
2794 asection *s;
2795 struct elf_segment_map *m;
2796 unsigned int isec;
2797
2798 csecs = 0;
2799
2800 /* The complicated case when p_vaddr is 0 is to handle the
2801 Solaris linker, which generates a PT_INTERP section with
2802 p_vaddr and p_memsz set to 0. */
2803 for (s = ibfd->sections; s != NULL; s = s->next)
2804 if (((s->vma >= p->p_vaddr
2805 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2806 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2807 || (p->p_vaddr == 0
2808 && p->p_filesz > 0
2809 && (s->flags & SEC_HAS_CONTENTS) != 0
2810 && (bfd_vma) s->filepos >= p->p_offset
2811 && ((bfd_vma) s->filepos + s->_raw_size
2812 <= p->p_offset + p->p_filesz)))
2813 && (s->flags & SEC_ALLOC) != 0
2814 && s->output_section != NULL)
2815 ++csecs;
2816
2817 m = ((struct elf_segment_map *)
2818 bfd_alloc (obfd,
2819 (sizeof (struct elf_segment_map)
2820 + (csecs - 1) * sizeof (asection *))));
2821 if (m == NULL)
2822 return false;
2823
2824 m->next = NULL;
2825 m->p_type = p->p_type;
2826 m->p_flags = p->p_flags;
2827 m->p_flags_valid = 1;
2828 m->p_paddr = p->p_paddr;
2829 m->p_paddr_valid = 1;
2830
2831 m->includes_filehdr = (p->p_offset == 0
2832 && p->p_filesz >= iehdr->e_ehsize);
2833
2834 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
2835 && (p->p_offset + p->p_filesz
2836 >= ((bfd_vma) iehdr->e_phoff
2837 + iehdr->e_phnum * iehdr->e_phentsize)));
2838
2839 isec = 0;
2840 for (s = ibfd->sections; s != NULL; s = s->next)
2841 {
2842 if (((s->vma >= p->p_vaddr
2843 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2844 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2845 || (p->p_vaddr == 0
2846 && p->p_filesz > 0
2847 && (s->flags & SEC_HAS_CONTENTS) != 0
2848 && (bfd_vma) s->filepos >= p->p_offset
2849 && ((bfd_vma) s->filepos + s->_raw_size
2850 <= p->p_offset + p->p_filesz)))
2851 && (s->flags & SEC_ALLOC) != 0
2852 && s->output_section != NULL)
2853 {
2854 m->sections[isec] = s->output_section;
2855 ++isec;
2856 }
2857 }
2858 BFD_ASSERT (isec == csecs);
2859 m->count = csecs;
2860
2861 *pm = m;
2862 pm = &m->next;
2863 }
2864
2865 elf_tdata (obfd)->segment_map = mfirst;
2866
2867 return true;
2868 }
2869
2870 /* Copy private section information. This copies over the entsize
2871 field, and sometimes the info field. */
2872
2873 boolean
2874 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
2875 bfd *ibfd;
2876 asection *isec;
2877 bfd *obfd;
2878 asection *osec;
2879 {
2880 Elf_Internal_Shdr *ihdr, *ohdr;
2881
2882 if (ibfd->xvec->flavour != bfd_target_elf_flavour
2883 || obfd->xvec->flavour != bfd_target_elf_flavour)
2884 return true;
2885
2886 /* Copy over private BFD data if it has not already been copied.
2887 This must be done here, rather than in the copy_private_bfd_data
2888 entry point, because the latter is called after the section
2889 contents have been set, which means that the program headers have
2890 already been worked out. */
2891 if (elf_tdata (obfd)->segment_map == NULL
2892 && elf_tdata (ibfd)->phdr != NULL)
2893 {
2894 asection *s;
2895
2896 /* Only set up the segments when all the sections have been set
2897 up. */
2898 for (s = ibfd->sections; s != NULL; s = s->next)
2899 if (s->output_section == NULL)
2900 break;
2901 if (s == NULL)
2902 {
2903 if (! copy_private_bfd_data (ibfd, obfd))
2904 return false;
2905 }
2906 }
2907
2908 ihdr = &elf_section_data (isec)->this_hdr;
2909 ohdr = &elf_section_data (osec)->this_hdr;
2910
2911 ohdr->sh_entsize = ihdr->sh_entsize;
2912
2913 if (ihdr->sh_type == SHT_SYMTAB
2914 || ihdr->sh_type == SHT_DYNSYM)
2915 ohdr->sh_info = ihdr->sh_info;
2916
2917 return true;
2918 }
2919
2920 /* Copy private symbol information. If this symbol is in a section
2921 which we did not map into a BFD section, try to map the section
2922 index correctly. We use special macro definitions for the mapped
2923 section indices; these definitions are interpreted by the
2924 swap_out_syms function. */
2925
2926 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2927 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2928 #define MAP_STRTAB (SHN_LORESERVE - 3)
2929 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2930
2931 boolean
2932 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
2933 bfd *ibfd;
2934 asymbol *isymarg;
2935 bfd *obfd;
2936 asymbol *osymarg;
2937 {
2938 elf_symbol_type *isym, *osym;
2939
2940 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2941 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2942 return true;
2943
2944 isym = elf_symbol_from (ibfd, isymarg);
2945 osym = elf_symbol_from (obfd, osymarg);
2946
2947 if (isym != NULL
2948 && osym != NULL
2949 && bfd_is_abs_section (isym->symbol.section))
2950 {
2951 unsigned int shndx;
2952
2953 shndx = isym->internal_elf_sym.st_shndx;
2954 if (shndx == elf_onesymtab (ibfd))
2955 shndx = MAP_ONESYMTAB;
2956 else if (shndx == elf_dynsymtab (ibfd))
2957 shndx = MAP_DYNSYMTAB;
2958 else if (shndx == elf_tdata (ibfd)->strtab_section)
2959 shndx = MAP_STRTAB;
2960 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
2961 shndx = MAP_SHSTRTAB;
2962 osym->internal_elf_sym.st_shndx = shndx;
2963 }
2964
2965 return true;
2966 }
2967
2968 /* Swap out the symbols. */
2969
2970 static boolean
2971 swap_out_syms (abfd, sttp)
2972 bfd *abfd;
2973 struct bfd_strtab_hash **sttp;
2974 {
2975 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2976
2977 if (!elf_map_symbols (abfd))
2978 return false;
2979
2980 /* Dump out the symtabs. */
2981 {
2982 int symcount = bfd_get_symcount (abfd);
2983 asymbol **syms = bfd_get_outsymbols (abfd);
2984 struct bfd_strtab_hash *stt;
2985 Elf_Internal_Shdr *symtab_hdr;
2986 Elf_Internal_Shdr *symstrtab_hdr;
2987 char *outbound_syms;
2988 int idx;
2989
2990 stt = _bfd_elf_stringtab_init ();
2991 if (stt == NULL)
2992 return false;
2993
2994 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2995 symtab_hdr->sh_type = SHT_SYMTAB;
2996 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
2997 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2998 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2999 symtab_hdr->sh_addralign = bed->s->file_align;
3000
3001 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3002 symstrtab_hdr->sh_type = SHT_STRTAB;
3003
3004 outbound_syms = bfd_alloc (abfd,
3005 (1 + symcount) * bed->s->sizeof_sym);
3006 if (outbound_syms == NULL)
3007 return false;
3008 symtab_hdr->contents = (PTR) outbound_syms;
3009
3010 /* now generate the data (for "contents") */
3011 {
3012 /* Fill in zeroth symbol and swap it out. */
3013 Elf_Internal_Sym sym;
3014 sym.st_name = 0;
3015 sym.st_value = 0;
3016 sym.st_size = 0;
3017 sym.st_info = 0;
3018 sym.st_other = 0;
3019 sym.st_shndx = SHN_UNDEF;
3020 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3021 outbound_syms += bed->s->sizeof_sym;
3022 }
3023 for (idx = 0; idx < symcount; idx++)
3024 {
3025 Elf_Internal_Sym sym;
3026 bfd_vma value = syms[idx]->value;
3027 elf_symbol_type *type_ptr;
3028 flagword flags = syms[idx]->flags;
3029 int type;
3030
3031 if (flags & BSF_SECTION_SYM)
3032 /* Section symbols have no names. */
3033 sym.st_name = 0;
3034 else
3035 {
3036 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
3037 syms[idx]->name,
3038 true, false);
3039 if (sym.st_name == (unsigned long) -1)
3040 return false;
3041 }
3042
3043 type_ptr = elf_symbol_from (abfd, syms[idx]);
3044
3045 if (bfd_is_com_section (syms[idx]->section))
3046 {
3047 /* ELF common symbols put the alignment into the `value' field,
3048 and the size into the `size' field. This is backwards from
3049 how BFD handles it, so reverse it here. */
3050 sym.st_size = value;
3051 if (type_ptr == NULL
3052 || type_ptr->internal_elf_sym.st_value == 0)
3053 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3054 else
3055 sym.st_value = type_ptr->internal_elf_sym.st_value;
3056 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
3057 syms[idx]->section);
3058 }
3059 else
3060 {
3061 asection *sec = syms[idx]->section;
3062 int shndx;
3063
3064 if (sec->output_section)
3065 {
3066 value += sec->output_offset;
3067 sec = sec->output_section;
3068 }
3069 value += sec->vma;
3070 sym.st_value = value;
3071 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
3072
3073 if (bfd_is_abs_section (sec)
3074 && type_ptr != NULL
3075 && type_ptr->internal_elf_sym.st_shndx != 0)
3076 {
3077 /* This symbol is in a real ELF section which we did
3078 not create as a BFD section. Undo the mapping done
3079 by copy_private_symbol_data. */
3080 shndx = type_ptr->internal_elf_sym.st_shndx;
3081 switch (shndx)
3082 {
3083 case MAP_ONESYMTAB:
3084 shndx = elf_onesymtab (abfd);
3085 break;
3086 case MAP_DYNSYMTAB:
3087 shndx = elf_dynsymtab (abfd);
3088 break;
3089 case MAP_STRTAB:
3090 shndx = elf_tdata (abfd)->strtab_section;
3091 break;
3092 case MAP_SHSTRTAB:
3093 shndx = elf_tdata (abfd)->shstrtab_section;
3094 break;
3095 default:
3096 break;
3097 }
3098 }
3099 else
3100 {
3101 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3102
3103 if (shndx == -1)
3104 {
3105 asection *sec2;
3106
3107 /* Writing this would be a hell of a lot easier if
3108 we had some decent documentation on bfd, and
3109 knew what to expect of the library, and what to
3110 demand of applications. For example, it
3111 appears that `objcopy' might not set the
3112 section of a symbol to be a section that is
3113 actually in the output file. */
3114 sec2 = bfd_get_section_by_name (abfd, sec->name);
3115 BFD_ASSERT (sec2 != 0);
3116 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3117 BFD_ASSERT (shndx != -1);
3118 }
3119 }
3120
3121 sym.st_shndx = shndx;
3122 }
3123
3124 if ((flags & BSF_FUNCTION) != 0)
3125 type = STT_FUNC;
3126 else if ((flags & BSF_OBJECT) != 0)
3127 type = STT_OBJECT;
3128 else
3129 type = STT_NOTYPE;
3130
3131 if (bfd_is_com_section (syms[idx]->section))
3132 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
3133 else if (bfd_is_und_section (syms[idx]->section))
3134 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
3135 ? STB_WEAK
3136 : STB_GLOBAL),
3137 type);
3138 else if (flags & BSF_SECTION_SYM)
3139 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3140 else if (flags & BSF_FILE)
3141 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3142 else
3143 {
3144 int bind = STB_LOCAL;
3145
3146 if (flags & BSF_LOCAL)
3147 bind = STB_LOCAL;
3148 else if (flags & BSF_WEAK)
3149 bind = STB_WEAK;
3150 else if (flags & BSF_GLOBAL)
3151 bind = STB_GLOBAL;
3152
3153 sym.st_info = ELF_ST_INFO (bind, type);
3154 }
3155
3156 if (type_ptr != NULL)
3157 sym.st_other = type_ptr->internal_elf_sym.st_other;
3158 else
3159 sym.st_other = 0;
3160
3161 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3162 outbound_syms += bed->s->sizeof_sym;
3163 }
3164
3165 *sttp = stt;
3166 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
3167 symstrtab_hdr->sh_type = SHT_STRTAB;
3168
3169 symstrtab_hdr->sh_flags = 0;
3170 symstrtab_hdr->sh_addr = 0;
3171 symstrtab_hdr->sh_entsize = 0;
3172 symstrtab_hdr->sh_link = 0;
3173 symstrtab_hdr->sh_info = 0;
3174 symstrtab_hdr->sh_addralign = 1;
3175 }
3176
3177 return true;
3178 }
3179
3180 /* Return the number of bytes required to hold the symtab vector.
3181
3182 Note that we base it on the count plus 1, since we will null terminate
3183 the vector allocated based on this size. However, the ELF symbol table
3184 always has a dummy entry as symbol #0, so it ends up even. */
3185
3186 long
3187 _bfd_elf_get_symtab_upper_bound (abfd)
3188 bfd *abfd;
3189 {
3190 long symcount;
3191 long symtab_size;
3192 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
3193
3194 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3195 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3196
3197 return symtab_size;
3198 }
3199
3200 long
3201 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
3202 bfd *abfd;
3203 {
3204 long symcount;
3205 long symtab_size;
3206 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3207
3208 if (elf_dynsymtab (abfd) == 0)
3209 {
3210 bfd_set_error (bfd_error_invalid_operation);
3211 return -1;
3212 }
3213
3214 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3215 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3216
3217 return symtab_size;
3218 }
3219
3220 long
3221 _bfd_elf_get_reloc_upper_bound (abfd, asect)
3222 bfd *abfd;
3223 sec_ptr asect;
3224 {
3225 return (asect->reloc_count + 1) * sizeof (arelent *);
3226 }
3227
3228 /* Canonicalize the relocs. */
3229
3230 long
3231 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
3232 bfd *abfd;
3233 sec_ptr section;
3234 arelent **relptr;
3235 asymbol **symbols;
3236 {
3237 arelent *tblptr;
3238 unsigned int i;
3239
3240 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
3241 section,
3242 symbols,
3243 false))
3244 return -1;
3245
3246 tblptr = section->relocation;
3247 for (i = 0; i < section->reloc_count; i++)
3248 *relptr++ = tblptr++;
3249
3250 *relptr = NULL;
3251
3252 return section->reloc_count;
3253 }
3254
3255 long
3256 _bfd_elf_get_symtab (abfd, alocation)
3257 bfd *abfd;
3258 asymbol **alocation;
3259 {
3260 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
3261
3262 if (symcount >= 0)
3263 bfd_get_symcount (abfd) = symcount;
3264 return symcount;
3265 }
3266
3267 long
3268 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
3269 bfd *abfd;
3270 asymbol **alocation;
3271 {
3272 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
3273 }
3274
3275 /* Return the size required for the dynamic reloc entries. Any
3276 section that was actually installed in the BFD, and has type
3277 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
3278 considered to be a dynamic reloc section. */
3279
3280 long
3281 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
3282 bfd *abfd;
3283 {
3284 long ret;
3285 asection *s;
3286
3287 if (elf_dynsymtab (abfd) == 0)
3288 {
3289 bfd_set_error (bfd_error_invalid_operation);
3290 return -1;
3291 }
3292
3293 ret = sizeof (arelent *);
3294 for (s = abfd->sections; s != NULL; s = s->next)
3295 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
3296 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
3297 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3298 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
3299 * sizeof (arelent *));
3300
3301 return ret;
3302 }
3303
3304 /* Canonicalize the dynamic relocation entries. Note that we return
3305 the dynamic relocations as a single block, although they are
3306 actually associated with particular sections; the interface, which
3307 was designed for SunOS style shared libraries, expects that there
3308 is only one set of dynamic relocs. Any section that was actually
3309 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
3310 the dynamic symbol table, is considered to be a dynamic reloc
3311 section. */
3312
3313 long
3314 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
3315 bfd *abfd;
3316 arelent **storage;
3317 asymbol **syms;
3318 {
3319 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
3320 asection *s;
3321 long ret;
3322
3323 if (elf_dynsymtab (abfd) == 0)
3324 {
3325 bfd_set_error (bfd_error_invalid_operation);
3326 return -1;
3327 }
3328
3329 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3330 ret = 0;
3331 for (s = abfd->sections; s != NULL; s = s->next)
3332 {
3333 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
3334 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
3335 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
3336 {
3337 arelent *p;
3338 long count, i;
3339
3340 if (! (*slurp_relocs) (abfd, s, syms, true))
3341 return -1;
3342 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
3343 p = s->relocation;
3344 for (i = 0; i < count; i++)
3345 *storage++ = p++;
3346 ret += count;
3347 }
3348 }
3349
3350 *storage = NULL;
3351
3352 return ret;
3353 }
3354
3355 asymbol *
3356 _bfd_elf_make_empty_symbol (abfd)
3357 bfd *abfd;
3358 {
3359 elf_symbol_type *newsym;
3360
3361 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3362 if (!newsym)
3363 return NULL;
3364 else
3365 {
3366 newsym->symbol.the_bfd = abfd;
3367 return &newsym->symbol;
3368 }
3369 }
3370
3371 void
3372 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
3373 bfd *ignore_abfd;
3374 asymbol *symbol;
3375 symbol_info *ret;
3376 {
3377 bfd_symbol_info (symbol, ret);
3378 }
3379
3380 alent *
3381 _bfd_elf_get_lineno (ignore_abfd, symbol)
3382 bfd *ignore_abfd;
3383 asymbol *symbol;
3384 {
3385 abort ();
3386 return NULL;
3387 }
3388
3389 boolean
3390 _bfd_elf_set_arch_mach (abfd, arch, machine)
3391 bfd *abfd;
3392 enum bfd_architecture arch;
3393 unsigned long machine;
3394 {
3395 /* If this isn't the right architecture for this backend, and this
3396 isn't the generic backend, fail. */
3397 if (arch != get_elf_backend_data (abfd)->arch
3398 && arch != bfd_arch_unknown
3399 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3400 return false;
3401
3402 return bfd_default_set_arch_mach (abfd, arch, machine);
3403 }
3404
3405 /* Find the nearest line to a particular section and offset, for error
3406 reporting. */
3407
3408 boolean
3409 _bfd_elf_find_nearest_line (abfd,
3410 section,
3411 symbols,
3412 offset,
3413 filename_ptr,
3414 functionname_ptr,
3415 line_ptr)
3416 bfd *abfd;
3417 asection *section;
3418 asymbol **symbols;
3419 bfd_vma offset;
3420 CONST char **filename_ptr;
3421 CONST char **functionname_ptr;
3422 unsigned int *line_ptr;
3423 {
3424 boolean found;
3425 const char *filename;
3426 asymbol *func;
3427 bfd_vma low_func;
3428 asymbol **p;
3429
3430 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3431 &found, filename_ptr,
3432 functionname_ptr, line_ptr,
3433 &elf_tdata (abfd)->line_info))
3434 return false;
3435 if (found)
3436 return true;
3437
3438 if (symbols == NULL)
3439 return false;
3440
3441 filename = NULL;
3442 func = NULL;
3443 low_func = 0;
3444
3445 for (p = symbols; *p != NULL; p++)
3446 {
3447 elf_symbol_type *q;
3448
3449 q = (elf_symbol_type *) *p;
3450
3451 if (bfd_get_section (&q->symbol) != section)
3452 continue;
3453
3454 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3455 {
3456 default:
3457 break;
3458 case STT_FILE:
3459 filename = bfd_asymbol_name (&q->symbol);
3460 break;
3461 case STT_FUNC:
3462 if (q->symbol.section == section
3463 && q->symbol.value >= low_func
3464 && q->symbol.value <= offset)
3465 {
3466 func = (asymbol *) q;
3467 low_func = q->symbol.value;
3468 }
3469 break;
3470 }
3471 }
3472
3473 if (func == NULL)
3474 return false;
3475
3476 *filename_ptr = filename;
3477 *functionname_ptr = bfd_asymbol_name (func);
3478 *line_ptr = 0;
3479 return true;
3480 }
3481
3482 int
3483 _bfd_elf_sizeof_headers (abfd, reloc)
3484 bfd *abfd;
3485 boolean reloc;
3486 {
3487 int ret;
3488
3489 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
3490 if (! reloc)
3491 ret += get_program_header_size (abfd);
3492 return ret;
3493 }
3494
3495 boolean
3496 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
3497 bfd *abfd;
3498 sec_ptr section;
3499 PTR location;
3500 file_ptr offset;
3501 bfd_size_type count;
3502 {
3503 Elf_Internal_Shdr *hdr;
3504
3505 if (! abfd->output_has_begun
3506 && ! _bfd_elf_compute_section_file_positions (abfd,
3507 (struct bfd_link_info *) NULL))
3508 return false;
3509
3510 hdr = &elf_section_data (section)->this_hdr;
3511
3512 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3513 return false;
3514 if (bfd_write (location, 1, count, abfd) != count)
3515 return false;
3516
3517 return true;
3518 }
3519
3520 void
3521 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
3522 bfd *abfd;
3523 arelent *cache_ptr;
3524 Elf_Internal_Rela *dst;
3525 {
3526 abort ();
3527 }
3528
3529 #if 0
3530 void
3531 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3532 bfd *abfd;
3533 arelent *cache_ptr;
3534 Elf_Internal_Rel *dst;
3535 {
3536 abort ();
3537 }
3538 #endif
3539
3540 /* Try to convert a non-ELF reloc into an ELF one. */
3541
3542 boolean
3543 _bfd_elf_validate_reloc (abfd, areloc)
3544 bfd *abfd;
3545 arelent *areloc;
3546 {
3547 /* Check whether we really have an ELF howto. */
3548
3549 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
3550 {
3551 bfd_reloc_code_real_type code;
3552 reloc_howto_type *howto;
3553
3554 /* Alien reloc: Try to determine its type to replace it with an
3555 equivalent ELF reloc. */
3556
3557 if (areloc->howto->pc_relative)
3558 {
3559 switch (areloc->howto->bitsize)
3560 {
3561 case 8:
3562 code = BFD_RELOC_8_PCREL;
3563 break;
3564 case 12:
3565 code = BFD_RELOC_12_PCREL;
3566 break;
3567 case 16:
3568 code = BFD_RELOC_16_PCREL;
3569 break;
3570 case 24:
3571 code = BFD_RELOC_24_PCREL;
3572 break;
3573 case 32:
3574 code = BFD_RELOC_32_PCREL;
3575 break;
3576 case 64:
3577 code = BFD_RELOC_64_PCREL;
3578 break;
3579 default:
3580 goto fail;
3581 }
3582
3583 howto = bfd_reloc_type_lookup (abfd, code);
3584
3585 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
3586 {
3587 if (howto->pcrel_offset)
3588 areloc->addend += areloc->address;
3589 else
3590 areloc->addend -= areloc->address; /* addend is unsigned!! */
3591 }
3592 }
3593 else
3594 {
3595 switch (areloc->howto->bitsize)
3596 {
3597 case 8:
3598 code = BFD_RELOC_8;
3599 break;
3600 case 14:
3601 code = BFD_RELOC_14;
3602 break;
3603 case 16:
3604 code = BFD_RELOC_16;
3605 break;
3606 case 26:
3607 code = BFD_RELOC_26;
3608 break;
3609 case 32:
3610 code = BFD_RELOC_32;
3611 break;
3612 case 64:
3613 code = BFD_RELOC_64;
3614 break;
3615 default:
3616 goto fail;
3617 }
3618
3619 howto = bfd_reloc_type_lookup (abfd, code);
3620 }
3621
3622 if (howto)
3623 areloc->howto = howto;
3624 else
3625 goto fail;
3626 }
3627
3628 return true;
3629
3630 fail:
3631 (*_bfd_error_handler)
3632 ("%s: unsupported relocation type %s",
3633 bfd_get_filename (abfd), areloc->howto->name);
3634 bfd_set_error (bfd_error_bad_value);
3635 return false;
3636 }