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