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