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