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