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