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