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