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