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