* elf.c (assign_file_positions_for_segments): Thinko last change.
[deliverable/binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /*
21
22 SECTION
23 ELF backends
24
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
28
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet.
32 */
33
34 #include "bfd.h"
35 #include "sysdep.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #define ARCH_SIZE 0
39 #include "elf-bfd.h"
40
41 static INLINE struct elf_segment_map *make_mapping
42 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
43 static boolean map_sections_to_segments PARAMS ((bfd *));
44 static int elf_sort_sections PARAMS ((const PTR, const PTR));
45 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
46 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
47 static boolean prep_headers PARAMS ((bfd *));
48 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
49 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
50 static char *elf_read PARAMS ((bfd *, long, unsigned int));
51 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
52 static boolean assign_section_numbers PARAMS ((bfd *));
53 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
54 static boolean elf_map_symbols PARAMS ((bfd *));
55 static bfd_size_type get_program_header_size PARAMS ((bfd *));
56
57 /* Swap version information in and out. The version information is
58 currently size independent. If that ever changes, this code will
59 need to move into elfcode.h. */
60
61 /* Swap in a Verdef structure. */
62
63 void
64 _bfd_elf_swap_verdef_in (abfd, src, dst)
65 bfd *abfd;
66 const Elf_External_Verdef *src;
67 Elf_Internal_Verdef *dst;
68 {
69 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
70 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
71 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
72 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
73 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
74 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
75 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
76 }
77
78 /* Swap out a Verdef structure. */
79
80 void
81 _bfd_elf_swap_verdef_out (abfd, src, dst)
82 bfd *abfd;
83 const Elf_Internal_Verdef *src;
84 Elf_External_Verdef *dst;
85 {
86 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
87 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
88 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
89 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
90 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
91 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
92 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
93 }
94
95 /* Swap in a Verdaux structure. */
96
97 void
98 _bfd_elf_swap_verdaux_in (abfd, src, dst)
99 bfd *abfd;
100 const Elf_External_Verdaux *src;
101 Elf_Internal_Verdaux *dst;
102 {
103 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
104 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
105 }
106
107 /* Swap out a Verdaux structure. */
108
109 void
110 _bfd_elf_swap_verdaux_out (abfd, src, dst)
111 bfd *abfd;
112 const Elf_Internal_Verdaux *src;
113 Elf_External_Verdaux *dst;
114 {
115 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
116 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
117 }
118
119 /* Swap in a Verneed structure. */
120
121 void
122 _bfd_elf_swap_verneed_in (abfd, src, dst)
123 bfd *abfd;
124 const Elf_External_Verneed *src;
125 Elf_Internal_Verneed *dst;
126 {
127 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
128 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
129 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
130 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
131 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
132 }
133
134 /* Swap out a Verneed structure. */
135
136 void
137 _bfd_elf_swap_verneed_out (abfd, src, dst)
138 bfd *abfd;
139 const Elf_Internal_Verneed *src;
140 Elf_External_Verneed *dst;
141 {
142 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
143 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
144 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
145 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
146 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
147 }
148
149 /* Swap in a Vernaux structure. */
150
151 void
152 _bfd_elf_swap_vernaux_in (abfd, src, dst)
153 bfd *abfd;
154 const Elf_External_Vernaux *src;
155 Elf_Internal_Vernaux *dst;
156 {
157 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
158 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
159 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
160 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
161 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
162 }
163
164 /* Swap out a Vernaux structure. */
165
166 void
167 _bfd_elf_swap_vernaux_out (abfd, src, dst)
168 bfd *abfd;
169 const Elf_Internal_Vernaux *src;
170 Elf_External_Vernaux *dst;
171 {
172 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
173 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
174 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
175 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
176 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
177 }
178
179 /* Swap in a Versym structure. */
180
181 void
182 _bfd_elf_swap_versym_in (abfd, src, dst)
183 bfd *abfd;
184 const Elf_External_Versym *src;
185 Elf_Internal_Versym *dst;
186 {
187 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
188 }
189
190 /* Swap out a Versym structure. */
191
192 void
193 _bfd_elf_swap_versym_out (abfd, src, dst)
194 bfd *abfd;
195 const Elf_Internal_Versym *src;
196 Elf_External_Versym *dst;
197 {
198 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
199 }
200
201 /* Standard ELF hash function. Do not change this function; you will
202 cause invalid hash tables to be generated. */
203
204 unsigned long
205 bfd_elf_hash (namearg)
206 const char *namearg;
207 {
208 const unsigned char *name = (const unsigned char *) namearg;
209 unsigned long h = 0;
210 unsigned long g;
211 int ch;
212
213 while ((ch = *name++) != '\0')
214 {
215 h = (h << 4) + ch;
216 if ((g = (h & 0xf0000000)) != 0)
217 {
218 h ^= g >> 24;
219 /* The ELF ABI says `h &= ~g', but this is equivalent in
220 this case and on some machines one insn instead of two. */
221 h ^= g;
222 }
223 }
224 return h;
225 }
226
227 /* Read a specified number of bytes at a specified offset in an ELF
228 file, into a newly allocated buffer, and return a pointer to the
229 buffer. */
230
231 static char *
232 elf_read (abfd, offset, size)
233 bfd * abfd;
234 long offset;
235 unsigned int size;
236 {
237 char *buf;
238
239 if ((buf = bfd_alloc (abfd, size)) == NULL)
240 return NULL;
241 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
242 return NULL;
243 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
244 {
245 if (bfd_get_error () != bfd_error_system_call)
246 bfd_set_error (bfd_error_file_truncated);
247 return NULL;
248 }
249 return buf;
250 }
251
252 boolean
253 bfd_elf_mkobject (abfd)
254 bfd * abfd;
255 {
256 /* this just does initialization */
257 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
258 elf_tdata (abfd) = (struct elf_obj_tdata *)
259 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
260 if (elf_tdata (abfd) == 0)
261 return false;
262 /* since everything is done at close time, do we need any
263 initialization? */
264
265 return true;
266 }
267
268 boolean
269 bfd_elf_mkcorefile (abfd)
270 bfd * abfd;
271 {
272 /* I think this can be done just like an object file. */
273 return bfd_elf_mkobject (abfd);
274 }
275
276 char *
277 bfd_elf_get_str_section (abfd, shindex)
278 bfd * abfd;
279 unsigned int shindex;
280 {
281 Elf_Internal_Shdr **i_shdrp;
282 char *shstrtab = NULL;
283 unsigned int offset;
284 unsigned int shstrtabsize;
285
286 i_shdrp = elf_elfsections (abfd);
287 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
288 return 0;
289
290 shstrtab = (char *) i_shdrp[shindex]->contents;
291 if (shstrtab == NULL)
292 {
293 /* No cached one, attempt to read, and cache what we read. */
294 offset = i_shdrp[shindex]->sh_offset;
295 shstrtabsize = i_shdrp[shindex]->sh_size;
296 shstrtab = elf_read (abfd, offset, shstrtabsize);
297 i_shdrp[shindex]->contents = (PTR) shstrtab;
298 }
299 return shstrtab;
300 }
301
302 char *
303 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
304 bfd * abfd;
305 unsigned int shindex;
306 unsigned int strindex;
307 {
308 Elf_Internal_Shdr *hdr;
309
310 if (strindex == 0)
311 return "";
312
313 hdr = elf_elfsections (abfd)[shindex];
314
315 if (hdr->contents == NULL
316 && bfd_elf_get_str_section (abfd, shindex) == NULL)
317 return NULL;
318
319 if (strindex >= hdr->sh_size)
320 {
321 (*_bfd_error_handler)
322 (_("%s: invalid string offset %u >= %lu for section `%s'"),
323 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
324 ((shindex == elf_elfheader(abfd)->e_shstrndx
325 && strindex == hdr->sh_name)
326 ? ".shstrtab"
327 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
328 return "";
329 }
330
331 return ((char *) hdr->contents) + strindex;
332 }
333
334 /* Make a BFD section from an ELF section. We store a pointer to the
335 BFD section in the bfd_section field of the header. */
336
337 boolean
338 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
339 bfd *abfd;
340 Elf_Internal_Shdr *hdr;
341 const char *name;
342 {
343 asection *newsect;
344 flagword flags;
345
346 if (hdr->bfd_section != NULL)
347 {
348 BFD_ASSERT (strcmp (name,
349 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
350 return true;
351 }
352
353 newsect = bfd_make_section_anyway (abfd, name);
354 if (newsect == NULL)
355 return false;
356
357 newsect->filepos = hdr->sh_offset;
358
359 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
360 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
361 || ! bfd_set_section_alignment (abfd, newsect,
362 bfd_log2 (hdr->sh_addralign)))
363 return false;
364
365 flags = SEC_NO_FLAGS;
366 if (hdr->sh_type != SHT_NOBITS)
367 flags |= SEC_HAS_CONTENTS;
368 if ((hdr->sh_flags & SHF_ALLOC) != 0)
369 {
370 flags |= SEC_ALLOC;
371 if (hdr->sh_type != SHT_NOBITS)
372 flags |= SEC_LOAD;
373 }
374 if ((hdr->sh_flags & SHF_WRITE) == 0)
375 flags |= SEC_READONLY;
376 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
377 flags |= SEC_CODE;
378 else if ((flags & SEC_LOAD) != 0)
379 flags |= SEC_DATA;
380
381 /* The debugging sections appear to be recognized only by name, not
382 any sort of flag. */
383 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
384 || strncmp (name, ".line", sizeof ".line" - 1) == 0
385 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
386 flags |= SEC_DEBUGGING;
387
388 /* As a GNU extension, if the name begins with .gnu.linkonce, we
389 only link a single copy of the section. This is used to support
390 g++. g++ will emit each template expansion in its own section.
391 The symbols will be defined as weak, so that multiple definitions
392 are permitted. The GNU linker extension is to actually discard
393 all but one of the sections. */
394 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
395 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
396
397 if (! bfd_set_section_flags (abfd, newsect, flags))
398 return false;
399
400 if ((flags & SEC_ALLOC) != 0)
401 {
402 Elf_Internal_Phdr *phdr;
403 unsigned int i;
404
405 /* Look through the phdrs to see if we need to adjust the lma.
406 If all the p_paddr fields are zero, we ignore them, since
407 some ELF linkers produce such output. */
408 phdr = elf_tdata (abfd)->phdr;
409 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
410 {
411 if (phdr->p_paddr != 0)
412 break;
413 }
414 if (i < elf_elfheader (abfd)->e_phnum)
415 {
416 phdr = elf_tdata (abfd)->phdr;
417 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
418 {
419 if (phdr->p_type == PT_LOAD
420 && phdr->p_vaddr != phdr->p_paddr
421 && phdr->p_vaddr <= hdr->sh_addr
422 && (phdr->p_vaddr + phdr->p_memsz
423 >= hdr->sh_addr + hdr->sh_size)
424 && ((flags & SEC_LOAD) == 0
425 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
426 && (phdr->p_offset + phdr->p_filesz
427 >= hdr->sh_offset + hdr->sh_size))))
428 {
429 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
430 break;
431 }
432 }
433 }
434 }
435
436 hdr->bfd_section = newsect;
437 elf_section_data (newsect)->this_hdr = *hdr;
438
439 return true;
440 }
441
442 /*
443 INTERNAL_FUNCTION
444 bfd_elf_find_section
445
446 SYNOPSIS
447 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
448
449 DESCRIPTION
450 Helper functions for GDB to locate the string tables.
451 Since BFD hides string tables from callers, GDB needs to use an
452 internal hook to find them. Sun's .stabstr, in particular,
453 isn't even pointed to by the .stab section, so ordinary
454 mechanisms wouldn't work to find it, even if we had some.
455 */
456
457 struct elf_internal_shdr *
458 bfd_elf_find_section (abfd, name)
459 bfd * abfd;
460 char *name;
461 {
462 Elf_Internal_Shdr **i_shdrp;
463 char *shstrtab;
464 unsigned int max;
465 unsigned int i;
466
467 i_shdrp = elf_elfsections (abfd);
468 if (i_shdrp != NULL)
469 {
470 shstrtab = bfd_elf_get_str_section
471 (abfd, elf_elfheader (abfd)->e_shstrndx);
472 if (shstrtab != NULL)
473 {
474 max = elf_elfheader (abfd)->e_shnum;
475 for (i = 1; i < max; i++)
476 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
477 return i_shdrp[i];
478 }
479 }
480 return 0;
481 }
482
483 const char *const bfd_elf_section_type_names[] = {
484 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
485 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
486 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
487 };
488
489 /* ELF relocs are against symbols. If we are producing relocateable
490 output, and the reloc is against an external symbol, and nothing
491 has given us any additional addend, the resulting reloc will also
492 be against the same symbol. In such a case, we don't want to
493 change anything about the way the reloc is handled, since it will
494 all be done at final link time. Rather than put special case code
495 into bfd_perform_relocation, all the reloc types use this howto
496 function. It just short circuits the reloc if producing
497 relocateable output against an external symbol. */
498
499 /*ARGSUSED*/
500 bfd_reloc_status_type
501 bfd_elf_generic_reloc (abfd,
502 reloc_entry,
503 symbol,
504 data,
505 input_section,
506 output_bfd,
507 error_message)
508 bfd *abfd ATTRIBUTE_UNUSED;
509 arelent *reloc_entry;
510 asymbol *symbol;
511 PTR data ATTRIBUTE_UNUSED;
512 asection *input_section;
513 bfd *output_bfd;
514 char **error_message ATTRIBUTE_UNUSED;
515 {
516 if (output_bfd != (bfd *) NULL
517 && (symbol->flags & BSF_SECTION_SYM) == 0
518 && (! reloc_entry->howto->partial_inplace
519 || reloc_entry->addend == 0))
520 {
521 reloc_entry->address += input_section->output_offset;
522 return bfd_reloc_ok;
523 }
524
525 return bfd_reloc_continue;
526 }
527 \f
528 /* Print out the program headers. */
529
530 boolean
531 _bfd_elf_print_private_bfd_data (abfd, farg)
532 bfd *abfd;
533 PTR farg;
534 {
535 FILE *f = (FILE *) farg;
536 Elf_Internal_Phdr *p;
537 asection *s;
538 bfd_byte *dynbuf = NULL;
539
540 p = elf_tdata (abfd)->phdr;
541 if (p != NULL)
542 {
543 unsigned int i, c;
544
545 fprintf (f, _("\nProgram Header:\n"));
546 c = elf_elfheader (abfd)->e_phnum;
547 for (i = 0; i < c; i++, p++)
548 {
549 const char *s;
550 char buf[20];
551
552 switch (p->p_type)
553 {
554 case PT_NULL: s = "NULL"; break;
555 case PT_LOAD: s = "LOAD"; break;
556 case PT_DYNAMIC: s = "DYNAMIC"; break;
557 case PT_INTERP: s = "INTERP"; break;
558 case PT_NOTE: s = "NOTE"; break;
559 case PT_SHLIB: s = "SHLIB"; break;
560 case PT_PHDR: s = "PHDR"; break;
561 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
562 }
563 fprintf (f, "%8s off 0x", s);
564 fprintf_vma (f, p->p_offset);
565 fprintf (f, " vaddr 0x");
566 fprintf_vma (f, p->p_vaddr);
567 fprintf (f, " paddr 0x");
568 fprintf_vma (f, p->p_paddr);
569 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
570 fprintf (f, " filesz 0x");
571 fprintf_vma (f, p->p_filesz);
572 fprintf (f, " memsz 0x");
573 fprintf_vma (f, p->p_memsz);
574 fprintf (f, " flags %c%c%c",
575 (p->p_flags & PF_R) != 0 ? 'r' : '-',
576 (p->p_flags & PF_W) != 0 ? 'w' : '-',
577 (p->p_flags & PF_X) != 0 ? 'x' : '-');
578 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
579 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
580 fprintf (f, "\n");
581 }
582 }
583
584 s = bfd_get_section_by_name (abfd, ".dynamic");
585 if (s != NULL)
586 {
587 int elfsec;
588 unsigned long link;
589 bfd_byte *extdyn, *extdynend;
590 size_t extdynsize;
591 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
592
593 fprintf (f, _("\nDynamic Section:\n"));
594
595 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
596 if (dynbuf == NULL)
597 goto error_return;
598 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
599 s->_raw_size))
600 goto error_return;
601
602 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
603 if (elfsec == -1)
604 goto error_return;
605 link = elf_elfsections (abfd)[elfsec]->sh_link;
606
607 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
608 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
609
610 extdyn = dynbuf;
611 extdynend = extdyn + s->_raw_size;
612 for (; extdyn < extdynend; extdyn += extdynsize)
613 {
614 Elf_Internal_Dyn dyn;
615 const char *name;
616 char ab[20];
617 boolean stringp;
618
619 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
620
621 if (dyn.d_tag == DT_NULL)
622 break;
623
624 stringp = false;
625 switch (dyn.d_tag)
626 {
627 default:
628 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
629 name = ab;
630 break;
631
632 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
633 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
634 case DT_PLTGOT: name = "PLTGOT"; break;
635 case DT_HASH: name = "HASH"; break;
636 case DT_STRTAB: name = "STRTAB"; break;
637 case DT_SYMTAB: name = "SYMTAB"; break;
638 case DT_RELA: name = "RELA"; break;
639 case DT_RELASZ: name = "RELASZ"; break;
640 case DT_RELAENT: name = "RELAENT"; break;
641 case DT_STRSZ: name = "STRSZ"; break;
642 case DT_SYMENT: name = "SYMENT"; break;
643 case DT_INIT: name = "INIT"; break;
644 case DT_FINI: name = "FINI"; break;
645 case DT_SONAME: name = "SONAME"; stringp = true; break;
646 case DT_RPATH: name = "RPATH"; stringp = true; break;
647 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
648 case DT_REL: name = "REL"; break;
649 case DT_RELSZ: name = "RELSZ"; break;
650 case DT_RELENT: name = "RELENT"; break;
651 case DT_PLTREL: name = "PLTREL"; break;
652 case DT_DEBUG: name = "DEBUG"; break;
653 case DT_TEXTREL: name = "TEXTREL"; break;
654 case DT_JMPREL: name = "JMPREL"; break;
655 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
656 case DT_FILTER: name = "FILTER"; stringp = true; break;
657 case DT_VERSYM: name = "VERSYM"; break;
658 case DT_VERDEF: name = "VERDEF"; break;
659 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
660 case DT_VERNEED: name = "VERNEED"; break;
661 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
662 }
663
664 fprintf (f, " %-11s ", name);
665 if (! stringp)
666 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
667 else
668 {
669 const char *string;
670
671 string = bfd_elf_string_from_elf_section (abfd, link,
672 dyn.d_un.d_val);
673 if (string == NULL)
674 goto error_return;
675 fprintf (f, "%s", string);
676 }
677 fprintf (f, "\n");
678 }
679
680 free (dynbuf);
681 dynbuf = NULL;
682 }
683
684 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
685 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
686 {
687 if (! _bfd_elf_slurp_version_tables (abfd))
688 return false;
689 }
690
691 if (elf_dynverdef (abfd) != 0)
692 {
693 Elf_Internal_Verdef *t;
694
695 fprintf (f, _("\nVersion definitions:\n"));
696 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
697 {
698 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
699 t->vd_flags, t->vd_hash, t->vd_nodename);
700 if (t->vd_auxptr->vda_nextptr != NULL)
701 {
702 Elf_Internal_Verdaux *a;
703
704 fprintf (f, "\t");
705 for (a = t->vd_auxptr->vda_nextptr;
706 a != NULL;
707 a = a->vda_nextptr)
708 fprintf (f, "%s ", a->vda_nodename);
709 fprintf (f, "\n");
710 }
711 }
712 }
713
714 if (elf_dynverref (abfd) != 0)
715 {
716 Elf_Internal_Verneed *t;
717
718 fprintf (f, _("\nVersion References:\n"));
719 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
720 {
721 Elf_Internal_Vernaux *a;
722
723 fprintf (f, _(" required from %s:\n"), t->vn_filename);
724 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
725 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
726 a->vna_flags, a->vna_other, a->vna_nodename);
727 }
728 }
729
730 return true;
731
732 error_return:
733 if (dynbuf != NULL)
734 free (dynbuf);
735 return false;
736 }
737
738 /* Display ELF-specific fields of a symbol. */
739
740 void
741 bfd_elf_print_symbol (abfd, filep, symbol, how)
742 bfd *abfd;
743 PTR filep;
744 asymbol *symbol;
745 bfd_print_symbol_type how;
746 {
747 FILE *file = (FILE *) filep;
748 switch (how)
749 {
750 case bfd_print_symbol_name:
751 fprintf (file, "%s", symbol->name);
752 break;
753 case bfd_print_symbol_more:
754 fprintf (file, "elf ");
755 fprintf_vma (file, symbol->value);
756 fprintf (file, " %lx", (long) symbol->flags);
757 break;
758 case bfd_print_symbol_all:
759 {
760 CONST char *section_name;
761 section_name = symbol->section ? symbol->section->name : "(*none*)";
762 bfd_print_symbol_vandf ((PTR) file, symbol);
763 fprintf (file, " %s\t", section_name);
764 /* Print the "other" value for a symbol. For common symbols,
765 we've already printed the size; now print the alignment.
766 For other symbols, we have no specified alignment, and
767 we've printed the address; now print the size. */
768 fprintf_vma (file,
769 (bfd_is_com_section (symbol->section)
770 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
771 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
772
773 /* If we have version information, print it. */
774 if (elf_tdata (abfd)->dynversym_section != 0
775 && (elf_tdata (abfd)->dynverdef_section != 0
776 || elf_tdata (abfd)->dynverref_section != 0))
777 {
778 unsigned int vernum;
779 const char *version_string;
780
781 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
782
783 if (vernum == 0)
784 version_string = "";
785 else if (vernum == 1)
786 version_string = "Base";
787 else if (vernum <= elf_tdata (abfd)->cverdefs)
788 version_string =
789 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
790 else
791 {
792 Elf_Internal_Verneed *t;
793
794 version_string = "";
795 for (t = elf_tdata (abfd)->verref;
796 t != NULL;
797 t = t->vn_nextref)
798 {
799 Elf_Internal_Vernaux *a;
800
801 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
802 {
803 if (a->vna_other == vernum)
804 {
805 version_string = a->vna_nodename;
806 break;
807 }
808 }
809 }
810 }
811
812 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
813 fprintf (file, " %-11s", version_string);
814 else
815 {
816 int i;
817
818 fprintf (file, " (%s)", version_string);
819 for (i = 10 - strlen (version_string); i > 0; --i)
820 putc (' ', file);
821 }
822 }
823
824 /* If the st_other field is not zero, print it. */
825 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0)
826 fprintf (file, " 0x%02x",
827 ((unsigned int)
828 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other));
829
830 fprintf (file, " %s", symbol->name);
831 }
832 break;
833 }
834 }
835 \f
836 /* Create an entry in an ELF linker hash table. */
837
838 struct bfd_hash_entry *
839 _bfd_elf_link_hash_newfunc (entry, table, string)
840 struct bfd_hash_entry *entry;
841 struct bfd_hash_table *table;
842 const char *string;
843 {
844 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
845
846 /* Allocate the structure if it has not already been allocated by a
847 subclass. */
848 if (ret == (struct elf_link_hash_entry *) NULL)
849 ret = ((struct elf_link_hash_entry *)
850 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
851 if (ret == (struct elf_link_hash_entry *) NULL)
852 return (struct bfd_hash_entry *) ret;
853
854 /* Call the allocation method of the superclass. */
855 ret = ((struct elf_link_hash_entry *)
856 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
857 table, string));
858 if (ret != (struct elf_link_hash_entry *) NULL)
859 {
860 /* Set local fields. */
861 ret->indx = -1;
862 ret->size = 0;
863 ret->dynindx = -1;
864 ret->dynstr_index = 0;
865 ret->weakdef = NULL;
866 ret->got.offset = (bfd_vma) -1;
867 ret->plt.offset = (bfd_vma) -1;
868 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
869 ret->verinfo.verdef = NULL;
870 ret->vtable_entries_used = NULL;
871 ret->vtable_entries_size = 0;
872 ret->vtable_parent = NULL;
873 ret->type = STT_NOTYPE;
874 ret->other = 0;
875 /* Assume that we have been called by a non-ELF symbol reader.
876 This flag is then reset by the code which reads an ELF input
877 file. This ensures that a symbol created by a non-ELF symbol
878 reader will have the flag set correctly. */
879 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
880 }
881
882 return (struct bfd_hash_entry *) ret;
883 }
884
885 /* Initialize an ELF linker hash table. */
886
887 boolean
888 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
889 struct elf_link_hash_table *table;
890 bfd *abfd;
891 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
892 struct bfd_hash_table *,
893 const char *));
894 {
895 table->dynamic_sections_created = false;
896 table->dynobj = NULL;
897 /* The first dynamic symbol is a dummy. */
898 table->dynsymcount = 1;
899 table->dynstr = NULL;
900 table->bucketcount = 0;
901 table->needed = NULL;
902 table->hgot = NULL;
903 table->stab_info = NULL;
904 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
905 }
906
907 /* Create an ELF linker hash table. */
908
909 struct bfd_link_hash_table *
910 _bfd_elf_link_hash_table_create (abfd)
911 bfd *abfd;
912 {
913 struct elf_link_hash_table *ret;
914
915 ret = ((struct elf_link_hash_table *)
916 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
917 if (ret == (struct elf_link_hash_table *) NULL)
918 return NULL;
919
920 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
921 {
922 bfd_release (abfd, ret);
923 return NULL;
924 }
925
926 return &ret->root;
927 }
928
929 /* This is a hook for the ELF emulation code in the generic linker to
930 tell the backend linker what file name to use for the DT_NEEDED
931 entry for a dynamic object. The generic linker passes name as an
932 empty string to indicate that no DT_NEEDED entry should be made. */
933
934 void
935 bfd_elf_set_dt_needed_name (abfd, name)
936 bfd *abfd;
937 const char *name;
938 {
939 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
940 && bfd_get_format (abfd) == bfd_object)
941 elf_dt_name (abfd) = name;
942 }
943
944 /* Get the list of DT_NEEDED entries for a link. This is a hook for
945 the linker ELF emulation code. */
946
947 struct bfd_link_needed_list *
948 bfd_elf_get_needed_list (abfd, info)
949 bfd *abfd ATTRIBUTE_UNUSED;
950 struct bfd_link_info *info;
951 {
952 if (info->hash->creator->flavour != bfd_target_elf_flavour)
953 return NULL;
954 return elf_hash_table (info)->needed;
955 }
956
957 /* Get the name actually used for a dynamic object for a link. This
958 is the SONAME entry if there is one. Otherwise, it is the string
959 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
960
961 const char *
962 bfd_elf_get_dt_soname (abfd)
963 bfd *abfd;
964 {
965 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
966 && bfd_get_format (abfd) == bfd_object)
967 return elf_dt_name (abfd);
968 return NULL;
969 }
970
971 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
972 the ELF linker emulation code. */
973
974 boolean
975 bfd_elf_get_bfd_needed_list (abfd, pneeded)
976 bfd *abfd;
977 struct bfd_link_needed_list **pneeded;
978 {
979 asection *s;
980 bfd_byte *dynbuf = NULL;
981 int elfsec;
982 unsigned long link;
983 bfd_byte *extdyn, *extdynend;
984 size_t extdynsize;
985 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
986
987 *pneeded = NULL;
988
989 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
990 || bfd_get_format (abfd) != bfd_object)
991 return true;
992
993 s = bfd_get_section_by_name (abfd, ".dynamic");
994 if (s == NULL || s->_raw_size == 0)
995 return true;
996
997 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
998 if (dynbuf == NULL)
999 goto error_return;
1000
1001 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1002 s->_raw_size))
1003 goto error_return;
1004
1005 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1006 if (elfsec == -1)
1007 goto error_return;
1008
1009 link = elf_elfsections (abfd)[elfsec]->sh_link;
1010
1011 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1012 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1013
1014 extdyn = dynbuf;
1015 extdynend = extdyn + s->_raw_size;
1016 for (; extdyn < extdynend; extdyn += extdynsize)
1017 {
1018 Elf_Internal_Dyn dyn;
1019
1020 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1021
1022 if (dyn.d_tag == DT_NULL)
1023 break;
1024
1025 if (dyn.d_tag == DT_NEEDED)
1026 {
1027 const char *string;
1028 struct bfd_link_needed_list *l;
1029
1030 string = bfd_elf_string_from_elf_section (abfd, link,
1031 dyn.d_un.d_val);
1032 if (string == NULL)
1033 goto error_return;
1034
1035 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1036 if (l == NULL)
1037 goto error_return;
1038
1039 l->by = abfd;
1040 l->name = string;
1041 l->next = *pneeded;
1042 *pneeded = l;
1043 }
1044 }
1045
1046 free (dynbuf);
1047
1048 return true;
1049
1050 error_return:
1051 if (dynbuf != NULL)
1052 free (dynbuf);
1053 return false;
1054 }
1055 \f
1056 /* Allocate an ELF string table--force the first byte to be zero. */
1057
1058 struct bfd_strtab_hash *
1059 _bfd_elf_stringtab_init ()
1060 {
1061 struct bfd_strtab_hash *ret;
1062
1063 ret = _bfd_stringtab_init ();
1064 if (ret != NULL)
1065 {
1066 bfd_size_type loc;
1067
1068 loc = _bfd_stringtab_add (ret, "", true, false);
1069 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1070 if (loc == (bfd_size_type) -1)
1071 {
1072 _bfd_stringtab_free (ret);
1073 ret = NULL;
1074 }
1075 }
1076 return ret;
1077 }
1078 \f
1079 /* ELF .o/exec file reading */
1080
1081 /* Create a new bfd section from an ELF section header. */
1082
1083 boolean
1084 bfd_section_from_shdr (abfd, shindex)
1085 bfd *abfd;
1086 unsigned int shindex;
1087 {
1088 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1089 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1090 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1091 char *name;
1092
1093 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1094
1095 switch (hdr->sh_type)
1096 {
1097 case SHT_NULL:
1098 /* Inactive section. Throw it away. */
1099 return true;
1100
1101 case SHT_PROGBITS: /* Normal section with contents. */
1102 case SHT_DYNAMIC: /* Dynamic linking information. */
1103 case SHT_NOBITS: /* .bss section. */
1104 case SHT_HASH: /* .hash section. */
1105 case SHT_NOTE: /* .note section. */
1106 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1107
1108 case SHT_SYMTAB: /* A symbol table */
1109 if (elf_onesymtab (abfd) == shindex)
1110 return true;
1111
1112 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1113 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1114 elf_onesymtab (abfd) = shindex;
1115 elf_tdata (abfd)->symtab_hdr = *hdr;
1116 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1117 abfd->flags |= HAS_SYMS;
1118
1119 /* Sometimes a shared object will map in the symbol table. If
1120 SHF_ALLOC is set, and this is a shared object, then we also
1121 treat this section as a BFD section. We can not base the
1122 decision purely on SHF_ALLOC, because that flag is sometimes
1123 set in a relocateable object file, which would confuse the
1124 linker. */
1125 if ((hdr->sh_flags & SHF_ALLOC) != 0
1126 && (abfd->flags & DYNAMIC) != 0
1127 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1128 return false;
1129
1130 return true;
1131
1132 case SHT_DYNSYM: /* A dynamic symbol table */
1133 if (elf_dynsymtab (abfd) == shindex)
1134 return true;
1135
1136 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1137 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1138 elf_dynsymtab (abfd) = shindex;
1139 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1140 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1141 abfd->flags |= HAS_SYMS;
1142
1143 /* Besides being a symbol table, we also treat this as a regular
1144 section, so that objcopy can handle it. */
1145 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1146
1147 case SHT_STRTAB: /* A string table */
1148 if (hdr->bfd_section != NULL)
1149 return true;
1150 if (ehdr->e_shstrndx == shindex)
1151 {
1152 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1153 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1154 return true;
1155 }
1156 {
1157 unsigned int i;
1158
1159 for (i = 1; i < ehdr->e_shnum; i++)
1160 {
1161 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1162 if (hdr2->sh_link == shindex)
1163 {
1164 if (! bfd_section_from_shdr (abfd, i))
1165 return false;
1166 if (elf_onesymtab (abfd) == i)
1167 {
1168 elf_tdata (abfd)->strtab_hdr = *hdr;
1169 elf_elfsections (abfd)[shindex] =
1170 &elf_tdata (abfd)->strtab_hdr;
1171 return true;
1172 }
1173 if (elf_dynsymtab (abfd) == i)
1174 {
1175 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1176 elf_elfsections (abfd)[shindex] = hdr =
1177 &elf_tdata (abfd)->dynstrtab_hdr;
1178 /* We also treat this as a regular section, so
1179 that objcopy can handle it. */
1180 break;
1181 }
1182 #if 0 /* Not handling other string tables specially right now. */
1183 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1184 /* We have a strtab for some random other section. */
1185 newsect = (asection *) hdr2->bfd_section;
1186 if (!newsect)
1187 break;
1188 hdr->bfd_section = newsect;
1189 hdr2 = &elf_section_data (newsect)->str_hdr;
1190 *hdr2 = *hdr;
1191 elf_elfsections (abfd)[shindex] = hdr2;
1192 #endif
1193 }
1194 }
1195 }
1196
1197 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1198
1199 case SHT_REL:
1200 case SHT_RELA:
1201 /* *These* do a lot of work -- but build no sections! */
1202 {
1203 asection *target_sect;
1204 Elf_Internal_Shdr *hdr2;
1205
1206 /* Check for a bogus link to avoid crashing. */
1207 if (hdr->sh_link >= ehdr->e_shnum)
1208 {
1209 ((*_bfd_error_handler)
1210 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1211 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1212 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1213 }
1214
1215 /* For some incomprehensible reason Oracle distributes
1216 libraries for Solaris in which some of the objects have
1217 bogus sh_link fields. It would be nice if we could just
1218 reject them, but, unfortunately, some people need to use
1219 them. We scan through the section headers; if we find only
1220 one suitable symbol table, we clobber the sh_link to point
1221 to it. I hope this doesn't break anything. */
1222 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1223 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1224 {
1225 int scan;
1226 int found;
1227
1228 found = 0;
1229 for (scan = 1; scan < ehdr->e_shnum; scan++)
1230 {
1231 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1232 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1233 {
1234 if (found != 0)
1235 {
1236 found = 0;
1237 break;
1238 }
1239 found = scan;
1240 }
1241 }
1242 if (found != 0)
1243 hdr->sh_link = found;
1244 }
1245
1246 /* Get the symbol table. */
1247 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1248 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1249 return false;
1250
1251 /* If this reloc section does not use the main symbol table we
1252 don't treat it as a reloc section. BFD can't adequately
1253 represent such a section, so at least for now, we don't
1254 try. We just present it as a normal section. */
1255 if (hdr->sh_link != elf_onesymtab (abfd))
1256 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1257
1258 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1259 return false;
1260 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1261 if (target_sect == NULL)
1262 return false;
1263
1264 if ((target_sect->flags & SEC_RELOC) == 0
1265 || target_sect->reloc_count == 0)
1266 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1267 else
1268 {
1269 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1270 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1271 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1272 }
1273 *hdr2 = *hdr;
1274 elf_elfsections (abfd)[shindex] = hdr2;
1275 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1276 target_sect->flags |= SEC_RELOC;
1277 target_sect->relocation = NULL;
1278 target_sect->rel_filepos = hdr->sh_offset;
1279 /* In the section to which the relocations apply, mark whether
1280 its relocations are of the REL or RELA variety. */
1281 elf_section_data (target_sect)->use_rela_p
1282 = (hdr->sh_type == SHT_RELA);
1283 abfd->flags |= HAS_RELOC;
1284 return true;
1285 }
1286 break;
1287
1288 case SHT_GNU_verdef:
1289 elf_dynverdef (abfd) = shindex;
1290 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1291 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1292 break;
1293
1294 case SHT_GNU_versym:
1295 elf_dynversym (abfd) = shindex;
1296 elf_tdata (abfd)->dynversym_hdr = *hdr;
1297 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1298 break;
1299
1300 case SHT_GNU_verneed:
1301 elf_dynverref (abfd) = shindex;
1302 elf_tdata (abfd)->dynverref_hdr = *hdr;
1303 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1304 break;
1305
1306 case SHT_SHLIB:
1307 return true;
1308
1309 default:
1310 /* Check for any processor-specific section types. */
1311 {
1312 if (bed->elf_backend_section_from_shdr)
1313 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1314 }
1315 break;
1316 }
1317
1318 return true;
1319 }
1320
1321 /* Given an ELF section number, retrieve the corresponding BFD
1322 section. */
1323
1324 asection *
1325 bfd_section_from_elf_index (abfd, index)
1326 bfd *abfd;
1327 unsigned int index;
1328 {
1329 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1330 if (index >= elf_elfheader (abfd)->e_shnum)
1331 return NULL;
1332 return elf_elfsections (abfd)[index]->bfd_section;
1333 }
1334
1335 boolean
1336 _bfd_elf_new_section_hook (abfd, sec)
1337 bfd *abfd;
1338 asection *sec;
1339 {
1340 struct bfd_elf_section_data *sdata;
1341
1342 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1343 if (!sdata)
1344 return false;
1345 sec->used_by_bfd = (PTR) sdata;
1346
1347 /* Indicate whether or not this section should use RELA relocations. */
1348 sdata->use_rela_p
1349 = get_elf_backend_data (abfd)->default_use_rela_p;
1350
1351 return true;
1352 }
1353
1354 /* Create a new bfd section from an ELF program header.
1355
1356 Since program segments have no names, we generate a synthetic name
1357 of the form segment<NUM>, where NUM is generally the index in the
1358 program header table. For segments that are split (see below) we
1359 generate the names segment<NUM>a and segment<NUM>b.
1360
1361 Note that some program segments may have a file size that is different than
1362 (less than) the memory size. All this means is that at execution the
1363 system must allocate the amount of memory specified by the memory size,
1364 but only initialize it with the first "file size" bytes read from the
1365 file. This would occur for example, with program segments consisting
1366 of combined data+bss.
1367
1368 To handle the above situation, this routine generates TWO bfd sections
1369 for the single program segment. The first has the length specified by
1370 the file size of the segment, and the second has the length specified
1371 by the difference between the two sizes. In effect, the segment is split
1372 into it's initialized and uninitialized parts.
1373
1374 */
1375
1376 boolean
1377 bfd_section_from_phdr (abfd, hdr, index)
1378 bfd *abfd;
1379 Elf_Internal_Phdr *hdr;
1380 int index;
1381 {
1382 asection *newsect;
1383 char *name;
1384 char namebuf[64];
1385 int split;
1386
1387 split = ((hdr->p_memsz > 0)
1388 && (hdr->p_filesz > 0)
1389 && (hdr->p_memsz > hdr->p_filesz));
1390 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
1391 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1392 if (!name)
1393 return false;
1394 strcpy (name, namebuf);
1395 newsect = bfd_make_section (abfd, name);
1396 if (newsect == NULL)
1397 return false;
1398 newsect->vma = hdr->p_vaddr;
1399 newsect->lma = hdr->p_paddr;
1400 newsect->_raw_size = hdr->p_filesz;
1401 newsect->filepos = hdr->p_offset;
1402 newsect->flags |= SEC_HAS_CONTENTS;
1403 if (hdr->p_type == PT_LOAD)
1404 {
1405 newsect->flags |= SEC_ALLOC;
1406 newsect->flags |= SEC_LOAD;
1407 if (hdr->p_flags & PF_X)
1408 {
1409 /* FIXME: all we known is that it has execute PERMISSION,
1410 may be data. */
1411 newsect->flags |= SEC_CODE;
1412 }
1413 }
1414 if (!(hdr->p_flags & PF_W))
1415 {
1416 newsect->flags |= SEC_READONLY;
1417 }
1418
1419 if (split)
1420 {
1421 sprintf (namebuf, "segment%db", index);
1422 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1423 if (!name)
1424 return false;
1425 strcpy (name, namebuf);
1426 newsect = bfd_make_section (abfd, name);
1427 if (newsect == NULL)
1428 return false;
1429 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1430 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1431 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1432 if (hdr->p_type == PT_LOAD)
1433 {
1434 newsect->flags |= SEC_ALLOC;
1435 if (hdr->p_flags & PF_X)
1436 newsect->flags |= SEC_CODE;
1437 }
1438 if (!(hdr->p_flags & PF_W))
1439 newsect->flags |= SEC_READONLY;
1440 }
1441
1442 return true;
1443 }
1444
1445 /* Initialize REL_HDR, the section-header for new section, containing
1446 relocations against ASECT. If USE_RELA_P is true, we use RELA
1447 relocations; otherwise, we use REL relocations. */
1448
1449 boolean
1450 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1451 bfd *abfd;
1452 Elf_Internal_Shdr *rel_hdr;
1453 asection *asect;
1454 boolean use_rela_p;
1455 {
1456 char *name;
1457 struct elf_backend_data *bed;
1458
1459 bed = get_elf_backend_data (abfd);
1460 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1461 if (name == NULL)
1462 return false;
1463 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1464 rel_hdr->sh_name =
1465 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1466 true, false);
1467 if (rel_hdr->sh_name == (unsigned int) -1)
1468 return false;
1469 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1470 rel_hdr->sh_entsize = (use_rela_p
1471 ? bed->s->sizeof_rela
1472 : bed->s->sizeof_rel);
1473 rel_hdr->sh_addralign = bed->s->file_align;
1474 rel_hdr->sh_flags = 0;
1475 rel_hdr->sh_addr = 0;
1476 rel_hdr->sh_size = 0;
1477 rel_hdr->sh_offset = 0;
1478
1479 return true;
1480 }
1481
1482 /* Set up an ELF internal section header for a section. */
1483
1484 /*ARGSUSED*/
1485 static void
1486 elf_fake_sections (abfd, asect, failedptrarg)
1487 bfd *abfd;
1488 asection *asect;
1489 PTR failedptrarg;
1490 {
1491 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1492 boolean *failedptr = (boolean *) failedptrarg;
1493 Elf_Internal_Shdr *this_hdr;
1494
1495 if (*failedptr)
1496 {
1497 /* We already failed; just get out of the bfd_map_over_sections
1498 loop. */
1499 return;
1500 }
1501
1502 this_hdr = &elf_section_data (asect)->this_hdr;
1503
1504 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1505 asect->name,
1506 true, false);
1507 if (this_hdr->sh_name == (unsigned long) -1)
1508 {
1509 *failedptr = true;
1510 return;
1511 }
1512
1513 this_hdr->sh_flags = 0;
1514
1515 if ((asect->flags & SEC_ALLOC) != 0
1516 || asect->user_set_vma)
1517 this_hdr->sh_addr = asect->vma;
1518 else
1519 this_hdr->sh_addr = 0;
1520
1521 this_hdr->sh_offset = 0;
1522 this_hdr->sh_size = asect->_raw_size;
1523 this_hdr->sh_link = 0;
1524 this_hdr->sh_addralign = 1 << asect->alignment_power;
1525 /* The sh_entsize and sh_info fields may have been set already by
1526 copy_private_section_data. */
1527
1528 this_hdr->bfd_section = asect;
1529 this_hdr->contents = NULL;
1530
1531 /* FIXME: This should not be based on section names. */
1532 if (strcmp (asect->name, ".dynstr") == 0)
1533 this_hdr->sh_type = SHT_STRTAB;
1534 else if (strcmp (asect->name, ".hash") == 0)
1535 {
1536 this_hdr->sh_type = SHT_HASH;
1537 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1538 }
1539 else if (strcmp (asect->name, ".dynsym") == 0)
1540 {
1541 this_hdr->sh_type = SHT_DYNSYM;
1542 this_hdr->sh_entsize = bed->s->sizeof_sym;
1543 }
1544 else if (strcmp (asect->name, ".dynamic") == 0)
1545 {
1546 this_hdr->sh_type = SHT_DYNAMIC;
1547 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1548 }
1549 else if (strncmp (asect->name, ".rela", 5) == 0
1550 && get_elf_backend_data (abfd)->may_use_rela_p)
1551 {
1552 this_hdr->sh_type = SHT_RELA;
1553 this_hdr->sh_entsize = bed->s->sizeof_rela;
1554 }
1555 else if (strncmp (asect->name, ".rel", 4) == 0
1556 && get_elf_backend_data (abfd)->may_use_rel_p)
1557 {
1558 this_hdr->sh_type = SHT_REL;
1559 this_hdr->sh_entsize = bed->s->sizeof_rel;
1560 }
1561 else if (strncmp (asect->name, ".note", 5) == 0)
1562 this_hdr->sh_type = SHT_NOTE;
1563 else if (strncmp (asect->name, ".stab", 5) == 0
1564 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1565 this_hdr->sh_type = SHT_STRTAB;
1566 else if (strcmp (asect->name, ".gnu.version") == 0)
1567 {
1568 this_hdr->sh_type = SHT_GNU_versym;
1569 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1570 }
1571 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1572 {
1573 this_hdr->sh_type = SHT_GNU_verdef;
1574 this_hdr->sh_entsize = 0;
1575 /* objcopy or strip will copy over sh_info, but may not set
1576 cverdefs. The linker will set cverdefs, but sh_info will be
1577 zero. */
1578 if (this_hdr->sh_info == 0)
1579 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1580 else
1581 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1582 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1583 }
1584 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1585 {
1586 this_hdr->sh_type = SHT_GNU_verneed;
1587 this_hdr->sh_entsize = 0;
1588 /* objcopy or strip will copy over sh_info, but may not set
1589 cverrefs. The linker will set cverrefs, but sh_info will be
1590 zero. */
1591 if (this_hdr->sh_info == 0)
1592 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1593 else
1594 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1595 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1596 }
1597 else if ((asect->flags & SEC_ALLOC) != 0
1598 && (asect->flags & SEC_LOAD) != 0)
1599 this_hdr->sh_type = SHT_PROGBITS;
1600 else if ((asect->flags & SEC_ALLOC) != 0
1601 && ((asect->flags & SEC_LOAD) == 0))
1602 this_hdr->sh_type = SHT_NOBITS;
1603 else
1604 {
1605 /* Who knows? */
1606 this_hdr->sh_type = SHT_PROGBITS;
1607 }
1608
1609 if ((asect->flags & SEC_ALLOC) != 0)
1610 this_hdr->sh_flags |= SHF_ALLOC;
1611 if ((asect->flags & SEC_READONLY) == 0)
1612 this_hdr->sh_flags |= SHF_WRITE;
1613 if ((asect->flags & SEC_CODE) != 0)
1614 this_hdr->sh_flags |= SHF_EXECINSTR;
1615
1616 /* Check for processor-specific section types. */
1617 if (bed->elf_backend_fake_sections)
1618 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1619
1620 /* If the section has relocs, set up a section header for the
1621 SHT_REL[A] section. If two relocation sections are required for
1622 this section, it is up to the processor-specific back-end to
1623 create the other. */
1624 if ((asect->flags & SEC_RELOC) != 0
1625 && !_bfd_elf_init_reloc_shdr (abfd,
1626 &elf_section_data (asect)->rel_hdr,
1627 asect,
1628 elf_section_data (asect)->use_rela_p))
1629 *failedptr = true;
1630 }
1631
1632 /* Assign all ELF section numbers. The dummy first section is handled here
1633 too. The link/info pointers for the standard section types are filled
1634 in here too, while we're at it. */
1635
1636 static boolean
1637 assign_section_numbers (abfd)
1638 bfd *abfd;
1639 {
1640 struct elf_obj_tdata *t = elf_tdata (abfd);
1641 asection *sec;
1642 unsigned int section_number;
1643 Elf_Internal_Shdr **i_shdrp;
1644 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1645
1646 section_number = 1;
1647
1648 for (sec = abfd->sections; sec; sec = sec->next)
1649 {
1650 struct bfd_elf_section_data *d = elf_section_data (sec);
1651
1652 d->this_idx = section_number++;
1653 if ((sec->flags & SEC_RELOC) == 0)
1654 d->rel_idx = 0;
1655 else
1656 d->rel_idx = section_number++;
1657
1658 if (d->rel_hdr2)
1659 d->rel_idx2 = section_number++;
1660 else
1661 d->rel_idx2 = 0;
1662 }
1663
1664 t->shstrtab_section = section_number++;
1665 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1666 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1667
1668 if (bfd_get_symcount (abfd) > 0)
1669 {
1670 t->symtab_section = section_number++;
1671 t->strtab_section = section_number++;
1672 }
1673
1674 elf_elfheader (abfd)->e_shnum = section_number;
1675
1676 /* Set up the list of section header pointers, in agreement with the
1677 indices. */
1678 i_shdrp = ((Elf_Internal_Shdr **)
1679 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1680 if (i_shdrp == NULL)
1681 return false;
1682
1683 i_shdrp[0] = ((Elf_Internal_Shdr *)
1684 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1685 if (i_shdrp[0] == NULL)
1686 {
1687 bfd_release (abfd, i_shdrp);
1688 return false;
1689 }
1690 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1691
1692 elf_elfsections (abfd) = i_shdrp;
1693
1694 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1695 if (bfd_get_symcount (abfd) > 0)
1696 {
1697 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1698 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1699 t->symtab_hdr.sh_link = t->strtab_section;
1700 }
1701 for (sec = abfd->sections; sec; sec = sec->next)
1702 {
1703 struct bfd_elf_section_data *d = elf_section_data (sec);
1704 asection *s;
1705 const char *name;
1706
1707 i_shdrp[d->this_idx] = &d->this_hdr;
1708 if (d->rel_idx != 0)
1709 i_shdrp[d->rel_idx] = &d->rel_hdr;
1710 if (d->rel_idx2 != 0)
1711 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1712
1713 /* Fill in the sh_link and sh_info fields while we're at it. */
1714
1715 /* sh_link of a reloc section is the section index of the symbol
1716 table. sh_info is the section index of the section to which
1717 the relocation entries apply. */
1718 if (d->rel_idx != 0)
1719 {
1720 d->rel_hdr.sh_link = t->symtab_section;
1721 d->rel_hdr.sh_info = d->this_idx;
1722 }
1723 if (d->rel_idx2 != 0)
1724 {
1725 d->rel_hdr2->sh_link = t->symtab_section;
1726 d->rel_hdr2->sh_info = d->this_idx;
1727 }
1728
1729 switch (d->this_hdr.sh_type)
1730 {
1731 case SHT_REL:
1732 case SHT_RELA:
1733 /* A reloc section which we are treating as a normal BFD
1734 section. sh_link is the section index of the symbol
1735 table. sh_info is the section index of the section to
1736 which the relocation entries apply. We assume that an
1737 allocated reloc section uses the dynamic symbol table.
1738 FIXME: How can we be sure? */
1739 s = bfd_get_section_by_name (abfd, ".dynsym");
1740 if (s != NULL)
1741 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1742
1743 /* We look up the section the relocs apply to by name. */
1744 name = sec->name;
1745 if (d->this_hdr.sh_type == SHT_REL)
1746 name += 4;
1747 else
1748 name += 5;
1749 s = bfd_get_section_by_name (abfd, name);
1750 if (s != NULL)
1751 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1752 break;
1753
1754 case SHT_STRTAB:
1755 /* We assume that a section named .stab*str is a stabs
1756 string section. We look for a section with the same name
1757 but without the trailing ``str'', and set its sh_link
1758 field to point to this section. */
1759 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1760 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1761 {
1762 size_t len;
1763 char *alc;
1764
1765 len = strlen (sec->name);
1766 alc = (char *) bfd_malloc (len - 2);
1767 if (alc == NULL)
1768 return false;
1769 strncpy (alc, sec->name, len - 3);
1770 alc[len - 3] = '\0';
1771 s = bfd_get_section_by_name (abfd, alc);
1772 free (alc);
1773 if (s != NULL)
1774 {
1775 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1776
1777 /* This is a .stab section. */
1778 elf_section_data (s)->this_hdr.sh_entsize =
1779 4 + 2 * (bed->s->arch_size / 8);
1780 }
1781 }
1782 break;
1783
1784 case SHT_DYNAMIC:
1785 case SHT_DYNSYM:
1786 case SHT_GNU_verneed:
1787 case SHT_GNU_verdef:
1788 /* sh_link is the section header index of the string table
1789 used for the dynamic entries, or the symbol table, or the
1790 version strings. */
1791 s = bfd_get_section_by_name (abfd, ".dynstr");
1792 if (s != NULL)
1793 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1794 break;
1795
1796 case SHT_HASH:
1797 case SHT_GNU_versym:
1798 /* sh_link is the section header index of the symbol table
1799 this hash table or version table is for. */
1800 s = bfd_get_section_by_name (abfd, ".dynsym");
1801 if (s != NULL)
1802 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1803 break;
1804 }
1805 }
1806
1807 return true;
1808 }
1809
1810 /* Map symbol from it's internal number to the external number, moving
1811 all local symbols to be at the head of the list. */
1812
1813 static INLINE int
1814 sym_is_global (abfd, sym)
1815 bfd *abfd;
1816 asymbol *sym;
1817 {
1818 /* If the backend has a special mapping, use it. */
1819 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1820 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1821 (abfd, sym));
1822
1823 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1824 || bfd_is_und_section (bfd_get_section (sym))
1825 || bfd_is_com_section (bfd_get_section (sym)));
1826 }
1827
1828 static boolean
1829 elf_map_symbols (abfd)
1830 bfd *abfd;
1831 {
1832 int symcount = bfd_get_symcount (abfd);
1833 asymbol **syms = bfd_get_outsymbols (abfd);
1834 asymbol **sect_syms;
1835 int num_locals = 0;
1836 int num_globals = 0;
1837 int num_locals2 = 0;
1838 int num_globals2 = 0;
1839 int max_index = 0;
1840 int num_sections = 0;
1841 int idx;
1842 asection *asect;
1843 asymbol **new_syms;
1844 asymbol *sym;
1845
1846 #ifdef DEBUG
1847 fprintf (stderr, "elf_map_symbols\n");
1848 fflush (stderr);
1849 #endif
1850
1851 /* Add a section symbol for each BFD section. FIXME: Is this really
1852 necessary? */
1853 for (asect = abfd->sections; asect; asect = asect->next)
1854 {
1855 if (max_index < asect->index)
1856 max_index = asect->index;
1857 }
1858
1859 max_index++;
1860 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1861 if (sect_syms == NULL)
1862 return false;
1863 elf_section_syms (abfd) = sect_syms;
1864
1865 for (idx = 0; idx < symcount; idx++)
1866 {
1867 sym = syms[idx];
1868
1869 if ((sym->flags & BSF_SECTION_SYM) != 0
1870 && sym->value == 0)
1871 {
1872 asection *sec;
1873
1874 sec = sym->section;
1875
1876 if (sec->owner != NULL)
1877 {
1878 if (sec->owner != abfd)
1879 {
1880 if (sec->output_offset != 0)
1881 continue;
1882
1883 sec = sec->output_section;
1884
1885 /* Empty sections in the input files may have had a section
1886 symbol created for them. (See the comment near the end of
1887 _bfd_generic_link_output_symbols in linker.c). If the linker
1888 script discards such sections then we will reach this point.
1889 Since we know that we cannot avoid this case, we detect it
1890 and skip the abort and the assignment to the sect_syms array.
1891 To reproduce this particular case try running the linker
1892 testsuite test ld-scripts/weak.exp for an ELF port that uses
1893 the generic linker. */
1894 if (sec->owner == NULL)
1895 continue;
1896
1897 BFD_ASSERT (sec->owner == abfd);
1898 }
1899 sect_syms[sec->index] = syms[idx];
1900 }
1901 }
1902 }
1903
1904 for (asect = abfd->sections; asect; asect = asect->next)
1905 {
1906 if (sect_syms[asect->index] != NULL)
1907 continue;
1908
1909 sym = bfd_make_empty_symbol (abfd);
1910 if (sym == NULL)
1911 return false;
1912 sym->the_bfd = abfd;
1913 sym->name = asect->name;
1914 sym->value = 0;
1915 /* Set the flags to 0 to indicate that this one was newly added. */
1916 sym->flags = 0;
1917 sym->section = asect;
1918 sect_syms[asect->index] = sym;
1919 num_sections++;
1920 #ifdef DEBUG
1921 fprintf (stderr,
1922 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
1923 asect->name, (long) asect->vma, asect->index, (long) asect);
1924 #endif
1925 }
1926
1927 /* Classify all of the symbols. */
1928 for (idx = 0; idx < symcount; idx++)
1929 {
1930 if (!sym_is_global (abfd, syms[idx]))
1931 num_locals++;
1932 else
1933 num_globals++;
1934 }
1935 for (asect = abfd->sections; asect; asect = asect->next)
1936 {
1937 if (sect_syms[asect->index] != NULL
1938 && sect_syms[asect->index]->flags == 0)
1939 {
1940 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1941 if (!sym_is_global (abfd, sect_syms[asect->index]))
1942 num_locals++;
1943 else
1944 num_globals++;
1945 sect_syms[asect->index]->flags = 0;
1946 }
1947 }
1948
1949 /* Now sort the symbols so the local symbols are first. */
1950 new_syms = ((asymbol **)
1951 bfd_alloc (abfd,
1952 (num_locals + num_globals) * sizeof (asymbol *)));
1953 if (new_syms == NULL)
1954 return false;
1955
1956 for (idx = 0; idx < symcount; idx++)
1957 {
1958 asymbol *sym = syms[idx];
1959 int i;
1960
1961 if (!sym_is_global (abfd, sym))
1962 i = num_locals2++;
1963 else
1964 i = num_locals + num_globals2++;
1965 new_syms[i] = sym;
1966 sym->udata.i = i + 1;
1967 }
1968 for (asect = abfd->sections; asect; asect = asect->next)
1969 {
1970 if (sect_syms[asect->index] != NULL
1971 && sect_syms[asect->index]->flags == 0)
1972 {
1973 asymbol *sym = sect_syms[asect->index];
1974 int i;
1975
1976 sym->flags = BSF_SECTION_SYM;
1977 if (!sym_is_global (abfd, sym))
1978 i = num_locals2++;
1979 else
1980 i = num_locals + num_globals2++;
1981 new_syms[i] = sym;
1982 sym->udata.i = i + 1;
1983 }
1984 }
1985
1986 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1987
1988 elf_num_locals (abfd) = num_locals;
1989 elf_num_globals (abfd) = num_globals;
1990 return true;
1991 }
1992
1993 /* Align to the maximum file alignment that could be required for any
1994 ELF data structure. */
1995
1996 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1997 static INLINE file_ptr
1998 align_file_position (off, align)
1999 file_ptr off;
2000 int align;
2001 {
2002 return (off + align - 1) & ~(align - 1);
2003 }
2004
2005 /* Assign a file position to a section, optionally aligning to the
2006 required section alignment. */
2007
2008 INLINE file_ptr
2009 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2010 Elf_Internal_Shdr *i_shdrp;
2011 file_ptr offset;
2012 boolean align;
2013 {
2014 if (align)
2015 {
2016 unsigned int al;
2017
2018 al = i_shdrp->sh_addralign;
2019 if (al > 1)
2020 offset = BFD_ALIGN (offset, al);
2021 }
2022 i_shdrp->sh_offset = offset;
2023 if (i_shdrp->bfd_section != NULL)
2024 i_shdrp->bfd_section->filepos = offset;
2025 if (i_shdrp->sh_type != SHT_NOBITS)
2026 offset += i_shdrp->sh_size;
2027 return offset;
2028 }
2029
2030 /* Compute the file positions we are going to put the sections at, and
2031 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2032 is not NULL, this is being called by the ELF backend linker. */
2033
2034 boolean
2035 _bfd_elf_compute_section_file_positions (abfd, link_info)
2036 bfd *abfd;
2037 struct bfd_link_info *link_info;
2038 {
2039 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2040 boolean failed;
2041 struct bfd_strtab_hash *strtab;
2042 Elf_Internal_Shdr *shstrtab_hdr;
2043
2044 if (abfd->output_has_begun)
2045 return true;
2046
2047 /* Do any elf backend specific processing first. */
2048 if (bed->elf_backend_begin_write_processing)
2049 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2050
2051 if (! prep_headers (abfd))
2052 return false;
2053
2054 /* Post process the headers if necessary. */
2055 if (bed->elf_backend_post_process_headers)
2056 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2057
2058 failed = false;
2059 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2060 if (failed)
2061 return false;
2062
2063 if (!assign_section_numbers (abfd))
2064 return false;
2065
2066 /* The backend linker builds symbol table information itself. */
2067 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2068 {
2069 /* Non-zero if doing a relocatable link. */
2070 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2071
2072 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2073 return false;
2074 }
2075
2076 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2077 /* sh_name was set in prep_headers. */
2078 shstrtab_hdr->sh_type = SHT_STRTAB;
2079 shstrtab_hdr->sh_flags = 0;
2080 shstrtab_hdr->sh_addr = 0;
2081 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2082 shstrtab_hdr->sh_entsize = 0;
2083 shstrtab_hdr->sh_link = 0;
2084 shstrtab_hdr->sh_info = 0;
2085 /* sh_offset is set in assign_file_positions_except_relocs. */
2086 shstrtab_hdr->sh_addralign = 1;
2087
2088 if (!assign_file_positions_except_relocs (abfd))
2089 return false;
2090
2091 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2092 {
2093 file_ptr off;
2094 Elf_Internal_Shdr *hdr;
2095
2096 off = elf_tdata (abfd)->next_file_pos;
2097
2098 hdr = &elf_tdata (abfd)->symtab_hdr;
2099 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2100
2101 hdr = &elf_tdata (abfd)->strtab_hdr;
2102 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2103
2104 elf_tdata (abfd)->next_file_pos = off;
2105
2106 /* Now that we know where the .strtab section goes, write it
2107 out. */
2108 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2109 || ! _bfd_stringtab_emit (abfd, strtab))
2110 return false;
2111 _bfd_stringtab_free (strtab);
2112 }
2113
2114 abfd->output_has_begun = true;
2115
2116 return true;
2117 }
2118
2119 /* Create a mapping from a set of sections to a program segment. */
2120
2121 static INLINE struct elf_segment_map *
2122 make_mapping (abfd, sections, from, to, phdr)
2123 bfd *abfd;
2124 asection **sections;
2125 unsigned int from;
2126 unsigned int to;
2127 boolean phdr;
2128 {
2129 struct elf_segment_map *m;
2130 unsigned int i;
2131 asection **hdrpp;
2132
2133 m = ((struct elf_segment_map *)
2134 bfd_zalloc (abfd,
2135 (sizeof (struct elf_segment_map)
2136 + (to - from - 1) * sizeof (asection *))));
2137 if (m == NULL)
2138 return NULL;
2139 m->next = NULL;
2140 m->p_type = PT_LOAD;
2141 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2142 m->sections[i - from] = *hdrpp;
2143 m->count = to - from;
2144
2145 if (from == 0 && phdr)
2146 {
2147 /* Include the headers in the first PT_LOAD segment. */
2148 m->includes_filehdr = 1;
2149 m->includes_phdrs = 1;
2150 }
2151
2152 return m;
2153 }
2154
2155 /* Set up a mapping from BFD sections to program segments. */
2156
2157 static boolean
2158 map_sections_to_segments (abfd)
2159 bfd *abfd;
2160 {
2161 asection **sections = NULL;
2162 asection *s;
2163 unsigned int i;
2164 unsigned int count;
2165 struct elf_segment_map *mfirst;
2166 struct elf_segment_map **pm;
2167 struct elf_segment_map *m;
2168 asection *last_hdr;
2169 unsigned int phdr_index;
2170 bfd_vma maxpagesize;
2171 asection **hdrpp;
2172 boolean phdr_in_segment = true;
2173 boolean writable;
2174 asection *dynsec;
2175
2176 if (elf_tdata (abfd)->segment_map != NULL)
2177 return true;
2178
2179 if (bfd_count_sections (abfd) == 0)
2180 return true;
2181
2182 /* Select the allocated sections, and sort them. */
2183
2184 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2185 * sizeof (asection *));
2186 if (sections == NULL)
2187 goto error_return;
2188
2189 i = 0;
2190 for (s = abfd->sections; s != NULL; s = s->next)
2191 {
2192 if ((s->flags & SEC_ALLOC) != 0)
2193 {
2194 sections[i] = s;
2195 ++i;
2196 }
2197 }
2198 BFD_ASSERT (i <= bfd_count_sections (abfd));
2199 count = i;
2200
2201 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2202
2203 /* Build the mapping. */
2204
2205 mfirst = NULL;
2206 pm = &mfirst;
2207
2208 /* If we have a .interp section, then create a PT_PHDR segment for
2209 the program headers and a PT_INTERP segment for the .interp
2210 section. */
2211 s = bfd_get_section_by_name (abfd, ".interp");
2212 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2213 {
2214 m = ((struct elf_segment_map *)
2215 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2216 if (m == NULL)
2217 goto error_return;
2218 m->next = NULL;
2219 m->p_type = PT_PHDR;
2220 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2221 m->p_flags = PF_R | PF_X;
2222 m->p_flags_valid = 1;
2223 m->includes_phdrs = 1;
2224
2225 *pm = m;
2226 pm = &m->next;
2227
2228 m = ((struct elf_segment_map *)
2229 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2230 if (m == NULL)
2231 goto error_return;
2232 m->next = NULL;
2233 m->p_type = PT_INTERP;
2234 m->count = 1;
2235 m->sections[0] = s;
2236
2237 *pm = m;
2238 pm = &m->next;
2239 }
2240
2241 /* Look through the sections. We put sections in the same program
2242 segment when the start of the second section can be placed within
2243 a few bytes of the end of the first section. */
2244 last_hdr = NULL;
2245 phdr_index = 0;
2246 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2247 writable = false;
2248 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2249 if (dynsec != NULL
2250 && (dynsec->flags & SEC_LOAD) == 0)
2251 dynsec = NULL;
2252
2253 /* Deal with -Ttext or something similar such that the first section
2254 is not adjacent to the program headers. This is an
2255 approximation, since at this point we don't know exactly how many
2256 program headers we will need. */
2257 if (count > 0)
2258 {
2259 bfd_size_type phdr_size;
2260
2261 phdr_size = elf_tdata (abfd)->program_header_size;
2262 if (phdr_size == 0)
2263 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2264 if ((abfd->flags & D_PAGED) == 0
2265 || sections[0]->lma < phdr_size
2266 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2267 phdr_in_segment = false;
2268 }
2269
2270 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2271 {
2272 asection *hdr;
2273 boolean new_segment;
2274
2275 hdr = *hdrpp;
2276
2277 /* See if this section and the last one will fit in the same
2278 segment. */
2279
2280 if (last_hdr == NULL)
2281 {
2282 /* If we don't have a segment yet, then we don't need a new
2283 one (we build the last one after this loop). */
2284 new_segment = false;
2285 }
2286 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2287 {
2288 /* If this section has a different relation between the
2289 virtual address and the load address, then we need a new
2290 segment. */
2291 new_segment = true;
2292 }
2293 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2294 < BFD_ALIGN (hdr->lma, maxpagesize))
2295 {
2296 /* If putting this section in this segment would force us to
2297 skip a page in the segment, then we need a new segment. */
2298 new_segment = true;
2299 }
2300 else if ((last_hdr->flags & SEC_LOAD) == 0
2301 && (hdr->flags & SEC_LOAD) != 0)
2302 {
2303 /* We don't want to put a loadable section after a
2304 nonloadable section in the same segment. */
2305 new_segment = true;
2306 }
2307 else if ((abfd->flags & D_PAGED) == 0)
2308 {
2309 /* If the file is not demand paged, which means that we
2310 don't require the sections to be correctly aligned in the
2311 file, then there is no other reason for a new segment. */
2312 new_segment = false;
2313 }
2314 else if (! writable
2315 && (hdr->flags & SEC_READONLY) == 0
2316 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2317 == hdr->lma))
2318 {
2319 /* We don't want to put a writable section in a read only
2320 segment, unless they are on the same page in memory
2321 anyhow. We already know that the last section does not
2322 bring us past the current section on the page, so the
2323 only case in which the new section is not on the same
2324 page as the previous section is when the previous section
2325 ends precisely on a page boundary. */
2326 new_segment = true;
2327 }
2328 else
2329 {
2330 /* Otherwise, we can use the same segment. */
2331 new_segment = false;
2332 }
2333
2334 if (! new_segment)
2335 {
2336 if ((hdr->flags & SEC_READONLY) == 0)
2337 writable = true;
2338 last_hdr = hdr;
2339 continue;
2340 }
2341
2342 /* We need a new program segment. We must create a new program
2343 header holding all the sections from phdr_index until hdr. */
2344
2345 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2346 if (m == NULL)
2347 goto error_return;
2348
2349 *pm = m;
2350 pm = &m->next;
2351
2352 if ((hdr->flags & SEC_READONLY) == 0)
2353 writable = true;
2354 else
2355 writable = false;
2356
2357 last_hdr = hdr;
2358 phdr_index = i;
2359 phdr_in_segment = false;
2360 }
2361
2362 /* Create a final PT_LOAD program segment. */
2363 if (last_hdr != NULL)
2364 {
2365 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2366 if (m == NULL)
2367 goto error_return;
2368
2369 *pm = m;
2370 pm = &m->next;
2371 }
2372
2373 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2374 if (dynsec != NULL)
2375 {
2376 m = ((struct elf_segment_map *)
2377 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2378 if (m == NULL)
2379 goto error_return;
2380 m->next = NULL;
2381 m->p_type = PT_DYNAMIC;
2382 m->count = 1;
2383 m->sections[0] = dynsec;
2384
2385 *pm = m;
2386 pm = &m->next;
2387 }
2388
2389 /* For each loadable .note section, add a PT_NOTE segment. We don't
2390 use bfd_get_section_by_name, because if we link together
2391 nonloadable .note sections and loadable .note sections, we will
2392 generate two .note sections in the output file. FIXME: Using
2393 names for section types is bogus anyhow. */
2394 for (s = abfd->sections; s != NULL; s = s->next)
2395 {
2396 if ((s->flags & SEC_LOAD) != 0
2397 && strncmp (s->name, ".note", 5) == 0)
2398 {
2399 m = ((struct elf_segment_map *)
2400 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2401 if (m == NULL)
2402 goto error_return;
2403 m->next = NULL;
2404 m->p_type = PT_NOTE;
2405 m->count = 1;
2406 m->sections[0] = s;
2407
2408 *pm = m;
2409 pm = &m->next;
2410 }
2411 }
2412
2413 free (sections);
2414 sections = NULL;
2415
2416 elf_tdata (abfd)->segment_map = mfirst;
2417 return true;
2418
2419 error_return:
2420 if (sections != NULL)
2421 free (sections);
2422 return false;
2423 }
2424
2425 /* Sort sections by address. */
2426
2427 static int
2428 elf_sort_sections (arg1, arg2)
2429 const PTR arg1;
2430 const PTR arg2;
2431 {
2432 const asection *sec1 = *(const asection **) arg1;
2433 const asection *sec2 = *(const asection **) arg2;
2434
2435 /* Sort by LMA first, since this is the address used to
2436 place the section into a segment. */
2437 if (sec1->lma < sec2->lma)
2438 return -1;
2439 else if (sec1->lma > sec2->lma)
2440 return 1;
2441
2442 /* Then sort by VMA. Normally the LMA and the VMA will be
2443 the same, and this will do nothing. */
2444 if (sec1->vma < sec2->vma)
2445 return -1;
2446 else if (sec1->vma > sec2->vma)
2447 return 1;
2448
2449 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2450
2451 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2452
2453 if (TOEND (sec1))
2454 {
2455 if (TOEND (sec2))
2456 return sec1->target_index - sec2->target_index;
2457 else
2458 return 1;
2459 }
2460
2461 if (TOEND (sec2))
2462 return -1;
2463
2464 #undef TOEND
2465
2466 /* Sort by size, to put zero sized sections before others at the
2467 same address. */
2468
2469 if (sec1->_raw_size < sec2->_raw_size)
2470 return -1;
2471 if (sec1->_raw_size > sec2->_raw_size)
2472 return 1;
2473
2474 return sec1->target_index - sec2->target_index;
2475 }
2476
2477 /* Assign file positions to the sections based on the mapping from
2478 sections to segments. This function also sets up some fields in
2479 the file header, and writes out the program headers. */
2480
2481 static boolean
2482 assign_file_positions_for_segments (abfd)
2483 bfd *abfd;
2484 {
2485 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2486 unsigned int count;
2487 struct elf_segment_map *m;
2488 unsigned int alloc;
2489 Elf_Internal_Phdr *phdrs;
2490 file_ptr off, voff;
2491 bfd_vma filehdr_vaddr, filehdr_paddr;
2492 bfd_vma phdrs_vaddr, phdrs_paddr;
2493 Elf_Internal_Phdr *p;
2494
2495 if (elf_tdata (abfd)->segment_map == NULL)
2496 {
2497 if (! map_sections_to_segments (abfd))
2498 return false;
2499 }
2500
2501 if (bed->elf_backend_modify_segment_map)
2502 {
2503 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2504 return false;
2505 }
2506
2507 count = 0;
2508 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2509 ++count;
2510
2511 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2512 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2513 elf_elfheader (abfd)->e_phnum = count;
2514
2515 if (count == 0)
2516 return true;
2517
2518 /* If we already counted the number of program segments, make sure
2519 that we allocated enough space. This happens when SIZEOF_HEADERS
2520 is used in a linker script. */
2521 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2522 if (alloc != 0 && count > alloc)
2523 {
2524 ((*_bfd_error_handler)
2525 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2526 bfd_get_filename (abfd), alloc, count));
2527 bfd_set_error (bfd_error_bad_value);
2528 return false;
2529 }
2530
2531 if (alloc == 0)
2532 alloc = count;
2533
2534 phdrs = ((Elf_Internal_Phdr *)
2535 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2536 if (phdrs == NULL)
2537 return false;
2538
2539 off = bed->s->sizeof_ehdr;
2540 off += alloc * bed->s->sizeof_phdr;
2541
2542 filehdr_vaddr = 0;
2543 filehdr_paddr = 0;
2544 phdrs_vaddr = 0;
2545 phdrs_paddr = 0;
2546
2547 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2548 m != NULL;
2549 m = m->next, p++)
2550 {
2551 unsigned int i;
2552 asection **secpp;
2553
2554 /* If elf_segment_map is not from map_sections_to_segments, the
2555 sections may not be correctly ordered. */
2556 if (m->count > 0)
2557 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2558 elf_sort_sections);
2559
2560 p->p_type = m->p_type;
2561 p->p_flags = m->p_flags;
2562
2563 if (p->p_type == PT_LOAD
2564 && m->count > 0
2565 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2566 {
2567 if ((abfd->flags & D_PAGED) != 0)
2568 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2569 else
2570 {
2571 bfd_size_type align;
2572
2573 align = 0;
2574 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2575 {
2576 bfd_size_type secalign;
2577
2578 secalign = bfd_get_section_alignment (abfd, *secpp);
2579 if (secalign > align)
2580 align = secalign;
2581 }
2582
2583 off += (m->sections[0]->vma - off) % (1 << align);
2584 }
2585 }
2586
2587 if (m->count == 0)
2588 p->p_vaddr = 0;
2589 else
2590 p->p_vaddr = m->sections[0]->vma;
2591
2592 if (m->p_paddr_valid)
2593 p->p_paddr = m->p_paddr;
2594 else if (m->count == 0)
2595 p->p_paddr = 0;
2596 else
2597 p->p_paddr = m->sections[0]->lma;
2598
2599 if (p->p_type == PT_LOAD
2600 && (abfd->flags & D_PAGED) != 0)
2601 p->p_align = bed->maxpagesize;
2602 else if (m->count == 0)
2603 p->p_align = bed->s->file_align;
2604 else
2605 p->p_align = 0;
2606
2607 p->p_offset = 0;
2608 p->p_filesz = 0;
2609 p->p_memsz = 0;
2610
2611 if (m->includes_filehdr)
2612 {
2613 if (! m->p_flags_valid)
2614 p->p_flags |= PF_R;
2615 p->p_offset = 0;
2616 p->p_filesz = bed->s->sizeof_ehdr;
2617 p->p_memsz = bed->s->sizeof_ehdr;
2618 if (m->count > 0)
2619 {
2620 BFD_ASSERT (p->p_type == PT_LOAD);
2621
2622 if (p->p_vaddr < (bfd_vma) off)
2623 {
2624 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2625 bfd_get_filename (abfd));
2626 bfd_set_error (bfd_error_bad_value);
2627 return false;
2628 }
2629
2630 p->p_vaddr -= off;
2631 if (! m->p_paddr_valid)
2632 p->p_paddr -= off;
2633 }
2634 if (p->p_type == PT_LOAD)
2635 {
2636 filehdr_vaddr = p->p_vaddr;
2637 filehdr_paddr = p->p_paddr;
2638 }
2639 }
2640
2641 if (m->includes_phdrs)
2642 {
2643 if (! m->p_flags_valid)
2644 p->p_flags |= PF_R;
2645
2646 if (m->includes_filehdr)
2647 {
2648 if (p->p_type == PT_LOAD)
2649 {
2650 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2651 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2652 }
2653 }
2654 else
2655 {
2656 p->p_offset = bed->s->sizeof_ehdr;
2657
2658 if (m->count > 0)
2659 {
2660 BFD_ASSERT (p->p_type == PT_LOAD);
2661 p->p_vaddr -= off - p->p_offset;
2662 if (! m->p_paddr_valid)
2663 p->p_paddr -= off - p->p_offset;
2664 }
2665
2666 if (p->p_type == PT_LOAD)
2667 {
2668 phdrs_vaddr = p->p_vaddr;
2669 phdrs_paddr = p->p_paddr;
2670 }
2671 else
2672 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2673 }
2674
2675 p->p_filesz += alloc * bed->s->sizeof_phdr;
2676 p->p_memsz += alloc * bed->s->sizeof_phdr;
2677 }
2678
2679 if (p->p_type == PT_LOAD
2680 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2681 {
2682 if (! m->includes_filehdr && ! m->includes_phdrs)
2683 p->p_offset = off;
2684 else
2685 {
2686 file_ptr adjust;
2687
2688 adjust = off - (p->p_offset + p->p_filesz);
2689 p->p_filesz += adjust;
2690 p->p_memsz += adjust;
2691 }
2692 }
2693
2694 voff = off;
2695
2696 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2697 {
2698 asection *sec;
2699 flagword flags;
2700 bfd_size_type align;
2701
2702 sec = *secpp;
2703 flags = sec->flags;
2704 align = 1 << bfd_get_section_alignment (abfd, sec);
2705
2706 /* The section may have artificial alignment forced by a
2707 link script. Notice this case by the gap between the
2708 cumulative phdr vma and the section's vma. */
2709 if (p->p_vaddr + p->p_memsz < sec->vma)
2710 {
2711 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2712
2713 p->p_memsz += adjust;
2714 off += adjust;
2715 voff += adjust;
2716 if ((flags & SEC_LOAD) != 0)
2717 p->p_filesz += adjust;
2718 }
2719
2720 if (p->p_type == PT_LOAD)
2721 {
2722 bfd_signed_vma adjust;
2723
2724 if ((flags & SEC_LOAD) != 0)
2725 {
2726 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2727 if (adjust < 0)
2728 adjust = 0;
2729 }
2730 else if ((flags & SEC_ALLOC) != 0)
2731 {
2732 /* The section VMA must equal the file position
2733 modulo the page size. FIXME: I'm not sure if
2734 this adjustment is really necessary. We used to
2735 not have the SEC_LOAD case just above, and then
2736 this was necessary, but now I'm not sure. */
2737 if ((abfd->flags & D_PAGED) != 0)
2738 adjust = (sec->vma - voff) % bed->maxpagesize;
2739 else
2740 adjust = (sec->vma - voff) % align;
2741 }
2742 else
2743 adjust = 0;
2744
2745 if (adjust != 0)
2746 {
2747 if (i == 0)
2748 {
2749 (* _bfd_error_handler)
2750 (_("Error: First section in segment (%s) starts at 0x%x"),
2751 bfd_section_name (abfd, sec), sec->lma);
2752 (* _bfd_error_handler)
2753 (_(" whereas segment starts at 0x%x"),
2754 p->p_paddr);
2755
2756 return false;
2757 }
2758 p->p_memsz += adjust;
2759 off += adjust;
2760 voff += adjust;
2761 if ((flags & SEC_LOAD) != 0)
2762 p->p_filesz += adjust;
2763 }
2764
2765 sec->filepos = off;
2766
2767 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2768 used in a linker script we may have a section with
2769 SEC_LOAD clear but which is supposed to have
2770 contents. */
2771 if ((flags & SEC_LOAD) != 0
2772 || (flags & SEC_HAS_CONTENTS) != 0)
2773 off += sec->_raw_size;
2774
2775 if ((flags & SEC_ALLOC) != 0)
2776 voff += sec->_raw_size;
2777 }
2778
2779 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2780 {
2781 if (i == 0) /* the actual "note" segment */
2782 { /* this one actually contains everything. */
2783 sec->filepos = off;
2784 p->p_filesz = sec->_raw_size;
2785 off += sec->_raw_size;
2786 voff = off;
2787 }
2788 else /* fake sections -- don't need to be written */
2789 {
2790 sec->filepos = 0;
2791 sec->_raw_size = 0;
2792 flags = sec->flags = 0; /* no contents */
2793 }
2794 p->p_memsz = 0;
2795 p->p_align = 1;
2796 }
2797 else
2798 {
2799 p->p_memsz += sec->_raw_size;
2800
2801 if ((flags & SEC_LOAD) != 0)
2802 p->p_filesz += sec->_raw_size;
2803
2804 if (align > p->p_align
2805 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2806 p->p_align = align;
2807 }
2808
2809 if (! m->p_flags_valid)
2810 {
2811 p->p_flags |= PF_R;
2812 if ((flags & SEC_CODE) != 0)
2813 p->p_flags |= PF_X;
2814 if ((flags & SEC_READONLY) == 0)
2815 p->p_flags |= PF_W;
2816 }
2817 }
2818 }
2819
2820 /* Now that we have set the section file positions, we can set up
2821 the file positions for the non PT_LOAD segments. */
2822 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2823 m != NULL;
2824 m = m->next, p++)
2825 {
2826 if (p->p_type != PT_LOAD && m->count > 0)
2827 {
2828 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2829 p->p_offset = m->sections[0]->filepos;
2830 }
2831 if (m->count == 0)
2832 {
2833 if (m->includes_filehdr)
2834 {
2835 p->p_vaddr = filehdr_vaddr;
2836 if (! m->p_paddr_valid)
2837 p->p_paddr = filehdr_paddr;
2838 }
2839 else if (m->includes_phdrs)
2840 {
2841 p->p_vaddr = phdrs_vaddr;
2842 if (! m->p_paddr_valid)
2843 p->p_paddr = phdrs_paddr;
2844 }
2845 }
2846 }
2847
2848 /* Clear out any program headers we allocated but did not use. */
2849 for (; count < alloc; count++, p++)
2850 {
2851 memset (p, 0, sizeof *p);
2852 p->p_type = PT_NULL;
2853 }
2854
2855 elf_tdata (abfd)->phdr = phdrs;
2856
2857 elf_tdata (abfd)->next_file_pos = off;
2858
2859 /* Write out the program headers. */
2860 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2861 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2862 return false;
2863
2864 return true;
2865 }
2866
2867 /* Get the size of the program header.
2868
2869 If this is called by the linker before any of the section VMA's are set, it
2870 can't calculate the correct value for a strange memory layout. This only
2871 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2872 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2873 data segment (exclusive of .interp and .dynamic).
2874
2875 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2876 will be two segments. */
2877
2878 static bfd_size_type
2879 get_program_header_size (abfd)
2880 bfd *abfd;
2881 {
2882 size_t segs;
2883 asection *s;
2884 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2885
2886 /* We can't return a different result each time we're called. */
2887 if (elf_tdata (abfd)->program_header_size != 0)
2888 return elf_tdata (abfd)->program_header_size;
2889
2890 if (elf_tdata (abfd)->segment_map != NULL)
2891 {
2892 struct elf_segment_map *m;
2893
2894 segs = 0;
2895 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2896 ++segs;
2897 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2898 return elf_tdata (abfd)->program_header_size;
2899 }
2900
2901 /* Assume we will need exactly two PT_LOAD segments: one for text
2902 and one for data. */
2903 segs = 2;
2904
2905 s = bfd_get_section_by_name (abfd, ".interp");
2906 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2907 {
2908 /* If we have a loadable interpreter section, we need a
2909 PT_INTERP segment. In this case, assume we also need a
2910 PT_PHDR segment, although that may not be true for all
2911 targets. */
2912 segs += 2;
2913 }
2914
2915 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2916 {
2917 /* We need a PT_DYNAMIC segment. */
2918 ++segs;
2919 }
2920
2921 for (s = abfd->sections; s != NULL; s = s->next)
2922 {
2923 if ((s->flags & SEC_LOAD) != 0
2924 && strncmp (s->name, ".note", 5) == 0)
2925 {
2926 /* We need a PT_NOTE segment. */
2927 ++segs;
2928 }
2929 }
2930
2931 /* Let the backend count up any program headers it might need. */
2932 if (bed->elf_backend_additional_program_headers)
2933 {
2934 int a;
2935
2936 a = (*bed->elf_backend_additional_program_headers) (abfd);
2937 if (a == -1)
2938 abort ();
2939 segs += a;
2940 }
2941
2942 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2943 return elf_tdata (abfd)->program_header_size;
2944 }
2945
2946 /* Work out the file positions of all the sections. This is called by
2947 _bfd_elf_compute_section_file_positions. All the section sizes and
2948 VMAs must be known before this is called.
2949
2950 We do not consider reloc sections at this point, unless they form
2951 part of the loadable image. Reloc sections are assigned file
2952 positions in assign_file_positions_for_relocs, which is called by
2953 write_object_contents and final_link.
2954
2955 We also don't set the positions of the .symtab and .strtab here. */
2956
2957 static boolean
2958 assign_file_positions_except_relocs (abfd)
2959 bfd *abfd;
2960 {
2961 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2962 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2963 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2964 file_ptr off;
2965 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2966
2967 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
2968 && bfd_get_format (abfd) != bfd_core)
2969 {
2970 Elf_Internal_Shdr **hdrpp;
2971 unsigned int i;
2972
2973 /* Start after the ELF header. */
2974 off = i_ehdrp->e_ehsize;
2975
2976 /* We are not creating an executable, which means that we are
2977 not creating a program header, and that the actual order of
2978 the sections in the file is unimportant. */
2979 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2980 {
2981 Elf_Internal_Shdr *hdr;
2982
2983 hdr = *hdrpp;
2984 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2985 {
2986 hdr->sh_offset = -1;
2987 continue;
2988 }
2989 if (i == tdata->symtab_section
2990 || i == tdata->strtab_section)
2991 {
2992 hdr->sh_offset = -1;
2993 continue;
2994 }
2995
2996 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2997 }
2998 }
2999 else
3000 {
3001 unsigned int i;
3002 Elf_Internal_Shdr **hdrpp;
3003
3004 /* Assign file positions for the loaded sections based on the
3005 assignment of sections to segments. */
3006 if (! assign_file_positions_for_segments (abfd))
3007 return false;
3008
3009 /* Assign file positions for the other sections. */
3010
3011 off = elf_tdata (abfd)->next_file_pos;
3012 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3013 {
3014 Elf_Internal_Shdr *hdr;
3015
3016 hdr = *hdrpp;
3017 if (hdr->bfd_section != NULL
3018 && hdr->bfd_section->filepos != 0)
3019 hdr->sh_offset = hdr->bfd_section->filepos;
3020 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3021 {
3022 ((*_bfd_error_handler)
3023 (_("%s: warning: allocated section `%s' not in segment"),
3024 bfd_get_filename (abfd),
3025 (hdr->bfd_section == NULL
3026 ? "*unknown*"
3027 : hdr->bfd_section->name)));
3028 if ((abfd->flags & D_PAGED) != 0)
3029 off += (hdr->sh_addr - off) % bed->maxpagesize;
3030 else
3031 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3032 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3033 false);
3034 }
3035 else if (hdr->sh_type == SHT_REL
3036 || hdr->sh_type == SHT_RELA
3037 || hdr == i_shdrpp[tdata->symtab_section]
3038 || hdr == i_shdrpp[tdata->strtab_section])
3039 hdr->sh_offset = -1;
3040 else
3041 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3042 }
3043 }
3044
3045 /* Place the section headers. */
3046 off = align_file_position (off, bed->s->file_align);
3047 i_ehdrp->e_shoff = off;
3048 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3049
3050 elf_tdata (abfd)->next_file_pos = off;
3051
3052 return true;
3053 }
3054
3055 static boolean
3056 prep_headers (abfd)
3057 bfd *abfd;
3058 {
3059 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3060 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3061 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3062 int count;
3063 struct bfd_strtab_hash *shstrtab;
3064 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3065
3066 i_ehdrp = elf_elfheader (abfd);
3067 i_shdrp = elf_elfsections (abfd);
3068
3069 shstrtab = _bfd_elf_stringtab_init ();
3070 if (shstrtab == NULL)
3071 return false;
3072
3073 elf_shstrtab (abfd) = shstrtab;
3074
3075 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3076 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3077 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3078 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3079
3080 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3081 i_ehdrp->e_ident[EI_DATA] =
3082 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3083 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3084
3085 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_SYSV;
3086 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3087
3088 for (count = EI_PAD; count < EI_NIDENT; count++)
3089 i_ehdrp->e_ident[count] = 0;
3090
3091 if ((abfd->flags & DYNAMIC) != 0)
3092 i_ehdrp->e_type = ET_DYN;
3093 else if ((abfd->flags & EXEC_P) != 0)
3094 i_ehdrp->e_type = ET_EXEC;
3095 else if (bfd_get_format (abfd) == bfd_core)
3096 i_ehdrp->e_type = ET_CORE;
3097 else
3098 i_ehdrp->e_type = ET_REL;
3099
3100 switch (bfd_get_arch (abfd))
3101 {
3102 case bfd_arch_unknown:
3103 i_ehdrp->e_machine = EM_NONE;
3104 break;
3105 case bfd_arch_sparc:
3106 if (bed->s->arch_size == 64)
3107 i_ehdrp->e_machine = EM_SPARCV9;
3108 else
3109 i_ehdrp->e_machine = EM_SPARC;
3110 break;
3111 case bfd_arch_i386:
3112 i_ehdrp->e_machine = EM_386;
3113 break;
3114 case bfd_arch_m68k:
3115 i_ehdrp->e_machine = EM_68K;
3116 break;
3117 case bfd_arch_m88k:
3118 i_ehdrp->e_machine = EM_88K;
3119 break;
3120 case bfd_arch_i860:
3121 i_ehdrp->e_machine = EM_860;
3122 break;
3123 case bfd_arch_i960:
3124 i_ehdrp->e_machine = EM_960;
3125 break;
3126 case bfd_arch_mips: /* MIPS Rxxxx */
3127 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3128 break;
3129 case bfd_arch_hppa:
3130 i_ehdrp->e_machine = EM_PARISC;
3131 break;
3132 case bfd_arch_powerpc:
3133 i_ehdrp->e_machine = EM_PPC;
3134 break;
3135 case bfd_arch_alpha:
3136 i_ehdrp->e_machine = EM_ALPHA;
3137 break;
3138 case bfd_arch_sh:
3139 i_ehdrp->e_machine = EM_SH;
3140 break;
3141 case bfd_arch_d10v:
3142 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3143 break;
3144 case bfd_arch_d30v:
3145 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3146 break;
3147 case bfd_arch_fr30:
3148 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3149 break;
3150 case bfd_arch_mcore:
3151 i_ehdrp->e_machine = EM_MCORE;
3152 break;
3153 case bfd_arch_v850:
3154 switch (bfd_get_mach (abfd))
3155 {
3156 default:
3157 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3158 }
3159 break;
3160 case bfd_arch_arc:
3161 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3162 break;
3163 case bfd_arch_arm:
3164 i_ehdrp->e_machine = EM_ARM;
3165 break;
3166 case bfd_arch_m32r:
3167 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3168 break;
3169 case bfd_arch_mn10200:
3170 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3171 break;
3172 case bfd_arch_mn10300:
3173 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3174 break;
3175 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3176 default:
3177 i_ehdrp->e_machine = EM_NONE;
3178 }
3179 i_ehdrp->e_version = bed->s->ev_current;
3180 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3181
3182 /* no program header, for now. */
3183 i_ehdrp->e_phoff = 0;
3184 i_ehdrp->e_phentsize = 0;
3185 i_ehdrp->e_phnum = 0;
3186
3187 /* each bfd section is section header entry */
3188 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3189 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3190
3191 /* if we're building an executable, we'll need a program header table */
3192 if (abfd->flags & EXEC_P)
3193 {
3194 /* it all happens later */
3195 #if 0
3196 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3197
3198 /* elf_build_phdrs() returns a (NULL-terminated) array of
3199 Elf_Internal_Phdrs */
3200 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3201 i_ehdrp->e_phoff = outbase;
3202 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3203 #endif
3204 }
3205 else
3206 {
3207 i_ehdrp->e_phentsize = 0;
3208 i_phdrp = 0;
3209 i_ehdrp->e_phoff = 0;
3210 }
3211
3212 elf_tdata (abfd)->symtab_hdr.sh_name =
3213 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3214 elf_tdata (abfd)->strtab_hdr.sh_name =
3215 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3216 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3217 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3218 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3219 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3220 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3221 return false;
3222
3223 return true;
3224 }
3225
3226 /* Assign file positions for all the reloc sections which are not part
3227 of the loadable file image. */
3228
3229 void
3230 _bfd_elf_assign_file_positions_for_relocs (abfd)
3231 bfd *abfd;
3232 {
3233 file_ptr off;
3234 unsigned int i;
3235 Elf_Internal_Shdr **shdrpp;
3236
3237 off = elf_tdata (abfd)->next_file_pos;
3238
3239 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3240 i < elf_elfheader (abfd)->e_shnum;
3241 i++, shdrpp++)
3242 {
3243 Elf_Internal_Shdr *shdrp;
3244
3245 shdrp = *shdrpp;
3246 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3247 && shdrp->sh_offset == -1)
3248 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3249 }
3250
3251 elf_tdata (abfd)->next_file_pos = off;
3252 }
3253
3254 boolean
3255 _bfd_elf_write_object_contents (abfd)
3256 bfd *abfd;
3257 {
3258 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3259 Elf_Internal_Ehdr *i_ehdrp;
3260 Elf_Internal_Shdr **i_shdrp;
3261 boolean failed;
3262 unsigned int count;
3263
3264 if (! abfd->output_has_begun
3265 && ! _bfd_elf_compute_section_file_positions
3266 (abfd, (struct bfd_link_info *) NULL))
3267 return false;
3268
3269 i_shdrp = elf_elfsections (abfd);
3270 i_ehdrp = elf_elfheader (abfd);
3271
3272 failed = false;
3273 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3274 if (failed)
3275 return false;
3276
3277 _bfd_elf_assign_file_positions_for_relocs (abfd);
3278
3279 /* After writing the headers, we need to write the sections too... */
3280 for (count = 1; count < i_ehdrp->e_shnum; count++)
3281 {
3282 if (bed->elf_backend_section_processing)
3283 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3284 if (i_shdrp[count]->contents)
3285 {
3286 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3287 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3288 1, abfd)
3289 != i_shdrp[count]->sh_size))
3290 return false;
3291 }
3292 }
3293
3294 /* Write out the section header names. */
3295 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3296 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3297 return false;
3298
3299 if (bed->elf_backend_final_write_processing)
3300 (*bed->elf_backend_final_write_processing) (abfd,
3301 elf_tdata (abfd)->linker);
3302
3303 return bed->s->write_shdrs_and_ehdr (abfd);
3304 }
3305
3306 boolean
3307 _bfd_elf_write_corefile_contents (abfd)
3308 bfd *abfd;
3309 {
3310 /* Hopefully this can be done just like an object file. */
3311 return _bfd_elf_write_object_contents (abfd);
3312 }
3313 /* given a section, search the header to find them... */
3314 int
3315 _bfd_elf_section_from_bfd_section (abfd, asect)
3316 bfd *abfd;
3317 struct sec *asect;
3318 {
3319 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3320 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3321 int index;
3322 Elf_Internal_Shdr *hdr;
3323 int maxindex = elf_elfheader (abfd)->e_shnum;
3324
3325 for (index = 0; index < maxindex; index++)
3326 {
3327 hdr = i_shdrp[index];
3328 if (hdr->bfd_section == asect)
3329 return index;
3330 }
3331
3332 if (bed->elf_backend_section_from_bfd_section)
3333 {
3334 for (index = 0; index < maxindex; index++)
3335 {
3336 int retval;
3337
3338 hdr = i_shdrp[index];
3339 retval = index;
3340 if ((*bed->elf_backend_section_from_bfd_section)
3341 (abfd, hdr, asect, &retval))
3342 return retval;
3343 }
3344 }
3345
3346 if (bfd_is_abs_section (asect))
3347 return SHN_ABS;
3348 if (bfd_is_com_section (asect))
3349 return SHN_COMMON;
3350 if (bfd_is_und_section (asect))
3351 return SHN_UNDEF;
3352
3353 bfd_set_error (bfd_error_nonrepresentable_section);
3354
3355 return -1;
3356 }
3357
3358 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3359 on error. */
3360
3361 int
3362 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3363 bfd *abfd;
3364 asymbol **asym_ptr_ptr;
3365 {
3366 asymbol *asym_ptr = *asym_ptr_ptr;
3367 int idx;
3368 flagword flags = asym_ptr->flags;
3369
3370 /* When gas creates relocations against local labels, it creates its
3371 own symbol for the section, but does put the symbol into the
3372 symbol chain, so udata is 0. When the linker is generating
3373 relocatable output, this section symbol may be for one of the
3374 input sections rather than the output section. */
3375 if (asym_ptr->udata.i == 0
3376 && (flags & BSF_SECTION_SYM)
3377 && asym_ptr->section)
3378 {
3379 int indx;
3380
3381 if (asym_ptr->section->output_section != NULL)
3382 indx = asym_ptr->section->output_section->index;
3383 else
3384 indx = asym_ptr->section->index;
3385 if (elf_section_syms (abfd)[indx])
3386 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3387 }
3388
3389 idx = asym_ptr->udata.i;
3390
3391 if (idx == 0)
3392 {
3393 /* This case can occur when using --strip-symbol on a symbol
3394 which is used in a relocation entry. */
3395 (*_bfd_error_handler)
3396 (_("%s: symbol `%s' required but not present"),
3397 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3398 bfd_set_error (bfd_error_no_symbols);
3399 return -1;
3400 }
3401
3402 #if DEBUG & 4
3403 {
3404 fprintf (stderr,
3405 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3406 (long) asym_ptr, asym_ptr->name, idx, flags,
3407 elf_symbol_flags (flags));
3408 fflush (stderr);
3409 }
3410 #endif
3411
3412 return idx;
3413 }
3414
3415 /* Copy private BFD data. This copies any program header information. */
3416
3417 static boolean
3418 copy_private_bfd_data (ibfd, obfd)
3419 bfd *ibfd;
3420 bfd *obfd;
3421 {
3422 Elf_Internal_Ehdr *iehdr;
3423 struct elf_segment_map *mfirst;
3424 struct elf_segment_map **pm;
3425 struct elf_segment_map *m;
3426 Elf_Internal_Phdr *p;
3427 unsigned int i;
3428 unsigned int num_segments;
3429 boolean phdr_included = false;
3430
3431 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3432 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3433 return true;
3434
3435 if (elf_tdata (ibfd)->phdr == NULL)
3436 return true;
3437
3438 iehdr = elf_elfheader (ibfd);
3439
3440 mfirst = NULL;
3441 pm = &mfirst;
3442
3443 num_segments = elf_elfheader (ibfd)->e_phnum;
3444
3445 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3446 ((addr) >= (bottom) \
3447 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3448 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3449
3450 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3451
3452 #define IS_COREFILE_NOTE(p, s) \
3453 (p->p_type == PT_NOTE \
3454 && bfd_get_format (ibfd) == bfd_core \
3455 && s->vma == 0 && s->lma == 0 \
3456 && (bfd_vma) s->filepos >= p->p_offset \
3457 && (bfd_vma) s->filepos + s->_raw_size \
3458 <= p->p_offset + p->p_filesz)
3459
3460 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3461 linker, which generates a PT_INTERP section with p_vaddr and
3462 p_memsz set to 0. */
3463
3464 #define IS_SOLARIS_PT_INTERP(p, s) \
3465 (p->p_vaddr == 0 \
3466 && p->p_filesz > 0 \
3467 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3468 && s->_raw_size > 0 \
3469 && (bfd_vma) s->filepos >= p->p_offset \
3470 && ((bfd_vma) s->filepos + s->_raw_size \
3471 <= p->p_offset + p->p_filesz))
3472
3473 /* Scan through the segments specified in the program header
3474 of the input BFD. */
3475 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3476 {
3477 unsigned int csecs;
3478 asection *s;
3479 asection **sections;
3480 asection *os;
3481 unsigned int isec;
3482 bfd_vma matching_lma;
3483 bfd_vma suggested_lma;
3484 unsigned int j;
3485
3486 /* For each section in the input BFD, decide if it should be
3487 included in the current segment. A section will be included
3488 if it is within the address space of the segment, and it is
3489 an allocated segment, and there is an output section
3490 associated with it. */
3491 csecs = 0;
3492 for (s = ibfd->sections; s != NULL; s = s->next)
3493 if (s->output_section != NULL)
3494 {
3495 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3496 || IS_SOLARIS_PT_INTERP (p, s))
3497 && (s->flags & SEC_ALLOC) != 0)
3498 ++csecs;
3499 else if (IS_COREFILE_NOTE (p, s))
3500 ++csecs;
3501 }
3502
3503 /* Allocate a segment map big enough to contain all of the
3504 sections we have selected. */
3505 m = ((struct elf_segment_map *)
3506 bfd_alloc (obfd,
3507 (sizeof (struct elf_segment_map)
3508 + ((size_t) csecs - 1) * sizeof (asection *))));
3509 if (m == NULL)
3510 return false;
3511
3512 /* Initialise the fields of the segment map. Default to
3513 using the physical address of the segment in the input BFD. */
3514 m->next = NULL;
3515 m->p_type = p->p_type;
3516 m->p_flags = p->p_flags;
3517 m->p_flags_valid = 1;
3518 m->p_paddr = p->p_paddr;
3519 m->p_paddr_valid = 1;
3520
3521 /* Determine if this segment contains the ELF file header
3522 and if it contains the program headers themselves. */
3523 m->includes_filehdr = (p->p_offset == 0
3524 && p->p_filesz >= iehdr->e_ehsize);
3525
3526 m->includes_phdrs = 0;
3527
3528 if (! phdr_included || p->p_type != PT_LOAD)
3529 {
3530 m->includes_phdrs =
3531 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3532 && (p->p_offset + p->p_filesz
3533 >= ((bfd_vma) iehdr->e_phoff
3534 + iehdr->e_phnum * iehdr->e_phentsize)));
3535 if (p->p_type == PT_LOAD && m->includes_phdrs)
3536 phdr_included = true;
3537 }
3538
3539 if (csecs == 0)
3540 {
3541 /* Special segments, such as the PT_PHDR segment, may contain
3542 no sections, but ordinary, loadable segments should contain
3543 something. */
3544
3545 if (p->p_type == PT_LOAD)
3546 _bfd_error_handler
3547 (_("%s: warning: Empty loadable segment detected\n"),
3548 bfd_get_filename (ibfd));
3549
3550 m->count = 0;
3551 *pm = m;
3552 pm = &m->next;
3553
3554 continue;
3555 }
3556
3557 /* Now scan the sections in the input BFD again and attempt
3558 to add their corresponding output sections to the segment map.
3559 The problem here is how to handle an output section which has
3560 been moved (ie had its LMA changed). There are four possibilities:
3561
3562 1. None of the sections have been moved.
3563 In this case we can continue to use the segment LMA from the
3564 input BFD.
3565
3566 2. All of the sections have been moved by the same amount.
3567 In this case we can change the segment's LMA to match the LMA
3568 of the first section.
3569
3570 3. Some of the sections have been moved, others have not.
3571 In this case those sections which have not been moved can be
3572 placed in the current segment which will have to have its size,
3573 and possibly its LMA changed, and a new segment or segments will
3574 have to be created to contain the other sections.
3575
3576 4. The sections have been moved, but not be the same amount.
3577 In this case we can change the segment's LMA to match the LMA
3578 of the first section and we will have to create a new segment
3579 or segments to contain the other sections.
3580
3581 In order to save time, we allocate an array to hold the section
3582 pointers that we are interested in. As these sections get assigned
3583 to a segment, they are removed from this array. */
3584
3585 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3586 if (sections == NULL)
3587 return false;
3588
3589 /* Step One: Scan for segment vs section LMA conflicts.
3590 Also add the sections to the section array allocated above.
3591 Also add the sections to the current segment. In the common
3592 case, where the sections have not been moved, this means that
3593 we have completely filled the segment, and there is nothing
3594 more to do. */
3595
3596 isec = 0;
3597 matching_lma = false;
3598 suggested_lma = 0;
3599
3600 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3601 {
3602 os = s->output_section;
3603
3604 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3605 || IS_SOLARIS_PT_INTERP (p, s))
3606 && (s->flags & SEC_ALLOC) != 0)
3607 || IS_COREFILE_NOTE (p, s))
3608 && os != NULL)
3609 {
3610 sections[j++] = s;
3611
3612 /* The Solaris native linker always sets p_paddr to 0.
3613 We try to catch that case here, and set it to the
3614 correct value. */
3615 if (p->p_paddr == 0
3616 && p->p_vaddr != 0
3617 && isec == 0
3618 && os->lma != 0
3619 && (os->vma == (p->p_vaddr
3620 + (m->includes_filehdr
3621 ? iehdr->e_ehsize
3622 : 0)
3623 + (m->includes_phdrs
3624 ? iehdr->e_phnum * iehdr->e_phentsize
3625 : 0))))
3626 m->p_paddr = p->p_vaddr;
3627
3628 /* Match up the physical address of the segment with the
3629 LMA address of the output section. */
3630 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3631 || IS_COREFILE_NOTE (p, s))
3632 {
3633 if (matching_lma == 0)
3634 matching_lma = os->lma;
3635
3636 /* We assume that if the section fits within the segment
3637 that it does not overlap any other section within that
3638 segment. */
3639 m->sections[isec++] = os;
3640 }
3641 else if (suggested_lma == 0)
3642 suggested_lma = os->lma;
3643 }
3644 }
3645
3646 BFD_ASSERT (j == csecs);
3647
3648 /* Step Two: Adjust the physical address of the current segment,
3649 if necessary. */
3650 if (isec == csecs)
3651 {
3652 /* All of the sections fitted within the segment as currently
3653 specified. This is the default case. Add the segment to
3654 the list of built segments and carry on to process the next
3655 program header in the input BFD. */
3656 m->count = csecs;
3657 *pm = m;
3658 pm = &m->next;
3659
3660 free (sections);
3661 continue;
3662 }
3663 else if (matching_lma != 0)
3664 {
3665 /* At least one section fits inside the current segment.
3666 Keep it, but modify its physical address to match the
3667 LMA of the first section that fitted. */
3668
3669 m->p_paddr = matching_lma;
3670 }
3671 else
3672 {
3673 /* None of the sections fitted inside the current segment.
3674 Change the current segment's physical address to match
3675 the LMA of the first section. */
3676
3677 m->p_paddr = suggested_lma;
3678 }
3679
3680 /* Step Three: Loop over the sections again, this time assigning
3681 those that fit to the current segment and remvoing them from the
3682 sections array; but making sure not to leave large gaps. Once all
3683 possible sections have been assigned to the current segment it is
3684 added to the list of built segments and if sections still remain
3685 to be assigned, a new segment is constructed before repeating
3686 the loop. */
3687 isec = 0;
3688 do
3689 {
3690 m->count = 0;
3691 suggested_lma = 0;
3692
3693 /* Fill the current segment with sections that fit. */
3694 for (j = 0; j < csecs; j++)
3695 {
3696 s = sections[j];
3697
3698 if (s == NULL)
3699 continue;
3700
3701 os = s->output_section;
3702
3703 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3704 || IS_COREFILE_NOTE (p, s))
3705 {
3706 if (m->count == 0)
3707 {
3708 /* If the first section in a segment does not start at
3709 the beginning of the segment, then something is wrong. */
3710 if (os->lma != m->p_paddr)
3711 abort ();
3712 }
3713 else
3714 {
3715 asection * prev_sec;
3716 bfd_vma maxpagesize;
3717
3718 prev_sec = m->sections[m->count - 1];
3719 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3720
3721 /* If the gap between the end of the previous section
3722 and the start of this section is more than maxpagesize
3723 then we need to start a new segment. */
3724 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3725 < BFD_ALIGN (os->lma, maxpagesize))
3726 {
3727 if (suggested_lma == 0)
3728 suggested_lma = os->lma;
3729
3730 continue;
3731 }
3732 }
3733
3734 m->sections[m->count++] = os;
3735 ++isec;
3736 sections[j] = NULL;
3737 }
3738 else if (suggested_lma == 0)
3739 suggested_lma = os->lma;
3740 }
3741
3742 BFD_ASSERT (m->count > 0);
3743
3744 /* Add the current segment to the list of built segments. */
3745 *pm = m;
3746 pm = &m->next;
3747
3748 if (isec < csecs)
3749 {
3750 /* We still have not allocated all of the sections to
3751 segments. Create a new segment here, initialise it
3752 and carry on looping. */
3753
3754 m = ((struct elf_segment_map *)
3755 bfd_alloc (obfd,
3756 (sizeof (struct elf_segment_map)
3757 + ((size_t) csecs - 1) * sizeof (asection *))));
3758 if (m == NULL)
3759 return false;
3760
3761 /* Initialise the fields of the segment map. Set the physical
3762 physical address to the LMA of the first section that has
3763 not yet been assigned. */
3764
3765 m->next = NULL;
3766 m->p_type = p->p_type;
3767 m->p_flags = p->p_flags;
3768 m->p_flags_valid = 1;
3769 m->p_paddr = suggested_lma;
3770 m->p_paddr_valid = 1;
3771 m->includes_filehdr = 0;
3772 m->includes_phdrs = 0;
3773 }
3774 }
3775 while (isec < csecs);
3776
3777 free (sections);
3778 }
3779
3780 /* The Solaris linker creates program headers in which all the
3781 p_paddr fields are zero. When we try to objcopy or strip such a
3782 file, we get confused. Check for this case, and if we find it
3783 reset the p_paddr_valid fields. */
3784 for (m = mfirst; m != NULL; m = m->next)
3785 if (m->p_paddr != 0)
3786 break;
3787 if (m == NULL)
3788 {
3789 for (m = mfirst; m != NULL; m = m->next)
3790 m->p_paddr_valid = 0;
3791 }
3792
3793 elf_tdata (obfd)->segment_map = mfirst;
3794
3795 #if 0
3796 /* Final Step: Sort the segments into ascending order of physical address. */
3797 if (mfirst != NULL)
3798 {
3799 struct elf_segment_map* prev;
3800
3801 prev = mfirst;
3802 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3803 {
3804 /* Yes I know - its a bubble sort....*/
3805 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3806 {
3807 /* swap m and m->next */
3808 prev->next = m->next;
3809 m->next = m->next->next;
3810 prev->next->next = m;
3811
3812 /* restart loop. */
3813 m = mfirst;
3814 }
3815 }
3816 }
3817 #endif
3818
3819 #undef IS_CONTAINED_BY
3820 #undef IS_SOLARIS_PT_INTERP
3821 #undef IS_COREFILE_NOTE
3822 return true;
3823 }
3824
3825 /* Copy private section information. This copies over the entsize
3826 field, and sometimes the info field. */
3827
3828 boolean
3829 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3830 bfd *ibfd;
3831 asection *isec;
3832 bfd *obfd;
3833 asection *osec;
3834 {
3835 Elf_Internal_Shdr *ihdr, *ohdr;
3836
3837 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3838 || obfd->xvec->flavour != bfd_target_elf_flavour)
3839 return true;
3840
3841 /* Copy over private BFD data if it has not already been copied.
3842 This must be done here, rather than in the copy_private_bfd_data
3843 entry point, because the latter is called after the section
3844 contents have been set, which means that the program headers have
3845 already been worked out. */
3846 if (elf_tdata (obfd)->segment_map == NULL
3847 && elf_tdata (ibfd)->phdr != NULL)
3848 {
3849 asection *s;
3850
3851 /* Only set up the segments if there are no more SEC_ALLOC
3852 sections. FIXME: This won't do the right thing if objcopy is
3853 used to remove the last SEC_ALLOC section, since objcopy
3854 won't call this routine in that case. */
3855 for (s = isec->next; s != NULL; s = s->next)
3856 if ((s->flags & SEC_ALLOC) != 0)
3857 break;
3858 if (s == NULL)
3859 {
3860 if (! copy_private_bfd_data (ibfd, obfd))
3861 return false;
3862 }
3863 }
3864
3865 ihdr = &elf_section_data (isec)->this_hdr;
3866 ohdr = &elf_section_data (osec)->this_hdr;
3867
3868 ohdr->sh_entsize = ihdr->sh_entsize;
3869
3870 if (ihdr->sh_type == SHT_SYMTAB
3871 || ihdr->sh_type == SHT_DYNSYM
3872 || ihdr->sh_type == SHT_GNU_verneed
3873 || ihdr->sh_type == SHT_GNU_verdef)
3874 ohdr->sh_info = ihdr->sh_info;
3875
3876 elf_section_data (osec)->use_rela_p
3877 = elf_section_data (isec)->use_rela_p;
3878
3879 return true;
3880 }
3881
3882 /* Copy private symbol information. If this symbol is in a section
3883 which we did not map into a BFD section, try to map the section
3884 index correctly. We use special macro definitions for the mapped
3885 section indices; these definitions are interpreted by the
3886 swap_out_syms function. */
3887
3888 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
3889 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
3890 #define MAP_STRTAB (SHN_LORESERVE - 3)
3891 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
3892
3893 boolean
3894 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
3895 bfd *ibfd;
3896 asymbol *isymarg;
3897 bfd *obfd;
3898 asymbol *osymarg;
3899 {
3900 elf_symbol_type *isym, *osym;
3901
3902 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3903 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3904 return true;
3905
3906 isym = elf_symbol_from (ibfd, isymarg);
3907 osym = elf_symbol_from (obfd, osymarg);
3908
3909 if (isym != NULL
3910 && osym != NULL
3911 && bfd_is_abs_section (isym->symbol.section))
3912 {
3913 unsigned int shndx;
3914
3915 shndx = isym->internal_elf_sym.st_shndx;
3916 if (shndx == elf_onesymtab (ibfd))
3917 shndx = MAP_ONESYMTAB;
3918 else if (shndx == elf_dynsymtab (ibfd))
3919 shndx = MAP_DYNSYMTAB;
3920 else if (shndx == elf_tdata (ibfd)->strtab_section)
3921 shndx = MAP_STRTAB;
3922 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
3923 shndx = MAP_SHSTRTAB;
3924 osym->internal_elf_sym.st_shndx = shndx;
3925 }
3926
3927 return true;
3928 }
3929
3930 /* Swap out the symbols. */
3931
3932 static boolean
3933 swap_out_syms (abfd, sttp, relocatable_p)
3934 bfd *abfd;
3935 struct bfd_strtab_hash **sttp;
3936 int relocatable_p;
3937 {
3938 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3939
3940 if (!elf_map_symbols (abfd))
3941 return false;
3942
3943 /* Dump out the symtabs. */
3944 {
3945 int symcount = bfd_get_symcount (abfd);
3946 asymbol **syms = bfd_get_outsymbols (abfd);
3947 struct bfd_strtab_hash *stt;
3948 Elf_Internal_Shdr *symtab_hdr;
3949 Elf_Internal_Shdr *symstrtab_hdr;
3950 char *outbound_syms;
3951 int idx;
3952
3953 stt = _bfd_elf_stringtab_init ();
3954 if (stt == NULL)
3955 return false;
3956
3957 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3958 symtab_hdr->sh_type = SHT_SYMTAB;
3959 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
3960 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
3961 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
3962 symtab_hdr->sh_addralign = bed->s->file_align;
3963
3964 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
3965 symstrtab_hdr->sh_type = SHT_STRTAB;
3966
3967 outbound_syms = bfd_alloc (abfd,
3968 (1 + symcount) * bed->s->sizeof_sym);
3969 if (outbound_syms == NULL)
3970 return false;
3971 symtab_hdr->contents = (PTR) outbound_syms;
3972
3973 /* now generate the data (for "contents") */
3974 {
3975 /* Fill in zeroth symbol and swap it out. */
3976 Elf_Internal_Sym sym;
3977 sym.st_name = 0;
3978 sym.st_value = 0;
3979 sym.st_size = 0;
3980 sym.st_info = 0;
3981 sym.st_other = 0;
3982 sym.st_shndx = SHN_UNDEF;
3983 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3984 outbound_syms += bed->s->sizeof_sym;
3985 }
3986 for (idx = 0; idx < symcount; idx++)
3987 {
3988 Elf_Internal_Sym sym;
3989 bfd_vma value = syms[idx]->value;
3990 elf_symbol_type *type_ptr;
3991 flagword flags = syms[idx]->flags;
3992 int type;
3993
3994 if (flags & BSF_SECTION_SYM)
3995 /* Section symbols have no names. */
3996 sym.st_name = 0;
3997 else
3998 {
3999 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4000 syms[idx]->name,
4001 true, false);
4002 if (sym.st_name == (unsigned long) -1)
4003 return false;
4004 }
4005
4006 type_ptr = elf_symbol_from (abfd, syms[idx]);
4007
4008 if ((flags & BSF_SECTION_SYM) == 0
4009 && bfd_is_com_section (syms[idx]->section))
4010 {
4011 /* ELF common symbols put the alignment into the `value' field,
4012 and the size into the `size' field. This is backwards from
4013 how BFD handles it, so reverse it here. */
4014 sym.st_size = value;
4015 if (type_ptr == NULL
4016 || type_ptr->internal_elf_sym.st_value == 0)
4017 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4018 else
4019 sym.st_value = type_ptr->internal_elf_sym.st_value;
4020 sym.st_shndx = _bfd_elf_section_from_bfd_section
4021 (abfd, syms[idx]->section);
4022 }
4023 else
4024 {
4025 asection *sec = syms[idx]->section;
4026 int shndx;
4027
4028 if (sec->output_section)
4029 {
4030 value += sec->output_offset;
4031 sec = sec->output_section;
4032 }
4033 /* Don't add in the section vma for relocatable output. */
4034 if (! relocatable_p)
4035 value += sec->vma;
4036 sym.st_value = value;
4037 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4038
4039 if (bfd_is_abs_section (sec)
4040 && type_ptr != NULL
4041 && type_ptr->internal_elf_sym.st_shndx != 0)
4042 {
4043 /* This symbol is in a real ELF section which we did
4044 not create as a BFD section. Undo the mapping done
4045 by copy_private_symbol_data. */
4046 shndx = type_ptr->internal_elf_sym.st_shndx;
4047 switch (shndx)
4048 {
4049 case MAP_ONESYMTAB:
4050 shndx = elf_onesymtab (abfd);
4051 break;
4052 case MAP_DYNSYMTAB:
4053 shndx = elf_dynsymtab (abfd);
4054 break;
4055 case MAP_STRTAB:
4056 shndx = elf_tdata (abfd)->strtab_section;
4057 break;
4058 case MAP_SHSTRTAB:
4059 shndx = elf_tdata (abfd)->shstrtab_section;
4060 break;
4061 default:
4062 break;
4063 }
4064 }
4065 else
4066 {
4067 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4068
4069 if (shndx == -1)
4070 {
4071 asection *sec2;
4072
4073 /* Writing this would be a hell of a lot easier if
4074 we had some decent documentation on bfd, and
4075 knew what to expect of the library, and what to
4076 demand of applications. For example, it
4077 appears that `objcopy' might not set the
4078 section of a symbol to be a section that is
4079 actually in the output file. */
4080 sec2 = bfd_get_section_by_name (abfd, sec->name);
4081 BFD_ASSERT (sec2 != 0);
4082 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4083 BFD_ASSERT (shndx != -1);
4084 }
4085 }
4086
4087 sym.st_shndx = shndx;
4088 }
4089
4090 if ((flags & BSF_FUNCTION) != 0)
4091 type = STT_FUNC;
4092 else if ((flags & BSF_OBJECT) != 0)
4093 type = STT_OBJECT;
4094 else
4095 type = STT_NOTYPE;
4096
4097 /* Processor-specific types */
4098 if (bed->elf_backend_get_symbol_type)
4099 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4100
4101 if (flags & BSF_SECTION_SYM)
4102 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4103 else if (bfd_is_com_section (syms[idx]->section))
4104 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4105 else if (bfd_is_und_section (syms[idx]->section))
4106 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4107 ? STB_WEAK
4108 : STB_GLOBAL),
4109 type);
4110 else if (flags & BSF_FILE)
4111 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4112 else
4113 {
4114 int bind = STB_LOCAL;
4115
4116 if (flags & BSF_LOCAL)
4117 bind = STB_LOCAL;
4118 else if (flags & BSF_WEAK)
4119 bind = STB_WEAK;
4120 else if (flags & BSF_GLOBAL)
4121 bind = STB_GLOBAL;
4122
4123 sym.st_info = ELF_ST_INFO (bind, type);
4124 }
4125
4126 if (type_ptr != NULL)
4127 sym.st_other = type_ptr->internal_elf_sym.st_other;
4128 else
4129 sym.st_other = 0;
4130
4131 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4132 outbound_syms += bed->s->sizeof_sym;
4133 }
4134
4135 *sttp = stt;
4136 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4137 symstrtab_hdr->sh_type = SHT_STRTAB;
4138
4139 symstrtab_hdr->sh_flags = 0;
4140 symstrtab_hdr->sh_addr = 0;
4141 symstrtab_hdr->sh_entsize = 0;
4142 symstrtab_hdr->sh_link = 0;
4143 symstrtab_hdr->sh_info = 0;
4144 symstrtab_hdr->sh_addralign = 1;
4145 }
4146
4147 return true;
4148 }
4149
4150 /* Return the number of bytes required to hold the symtab vector.
4151
4152 Note that we base it on the count plus 1, since we will null terminate
4153 the vector allocated based on this size. However, the ELF symbol table
4154 always has a dummy entry as symbol #0, so it ends up even. */
4155
4156 long
4157 _bfd_elf_get_symtab_upper_bound (abfd)
4158 bfd *abfd;
4159 {
4160 long symcount;
4161 long symtab_size;
4162 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4163
4164 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4165 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4166
4167 return symtab_size;
4168 }
4169
4170 long
4171 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4172 bfd *abfd;
4173 {
4174 long symcount;
4175 long symtab_size;
4176 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4177
4178 if (elf_dynsymtab (abfd) == 0)
4179 {
4180 bfd_set_error (bfd_error_invalid_operation);
4181 return -1;
4182 }
4183
4184 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4185 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4186
4187 return symtab_size;
4188 }
4189
4190 long
4191 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4192 bfd *abfd ATTRIBUTE_UNUSED;
4193 sec_ptr asect;
4194 {
4195 return (asect->reloc_count + 1) * sizeof (arelent *);
4196 }
4197
4198 /* Canonicalize the relocs. */
4199
4200 long
4201 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4202 bfd *abfd;
4203 sec_ptr section;
4204 arelent **relptr;
4205 asymbol **symbols;
4206 {
4207 arelent *tblptr;
4208 unsigned int i;
4209
4210 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4211 section,
4212 symbols,
4213 false))
4214 return -1;
4215
4216 tblptr = section->relocation;
4217 for (i = 0; i < section->reloc_count; i++)
4218 *relptr++ = tblptr++;
4219
4220 *relptr = NULL;
4221
4222 return section->reloc_count;
4223 }
4224
4225 long
4226 _bfd_elf_get_symtab (abfd, alocation)
4227 bfd *abfd;
4228 asymbol **alocation;
4229 {
4230 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4231 (abfd, alocation, false);
4232
4233 if (symcount >= 0)
4234 bfd_get_symcount (abfd) = symcount;
4235 return symcount;
4236 }
4237
4238 long
4239 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4240 bfd *abfd;
4241 asymbol **alocation;
4242 {
4243 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4244 (abfd, alocation, true);
4245 }
4246
4247 /* Return the size required for the dynamic reloc entries. Any
4248 section that was actually installed in the BFD, and has type
4249 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4250 considered to be a dynamic reloc section. */
4251
4252 long
4253 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4254 bfd *abfd;
4255 {
4256 long ret;
4257 asection *s;
4258
4259 if (elf_dynsymtab (abfd) == 0)
4260 {
4261 bfd_set_error (bfd_error_invalid_operation);
4262 return -1;
4263 }
4264
4265 ret = sizeof (arelent *);
4266 for (s = abfd->sections; s != NULL; s = s->next)
4267 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4268 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4269 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4270 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4271 * sizeof (arelent *));
4272
4273 return ret;
4274 }
4275
4276 /* Canonicalize the dynamic relocation entries. Note that we return
4277 the dynamic relocations as a single block, although they are
4278 actually associated with particular sections; the interface, which
4279 was designed for SunOS style shared libraries, expects that there
4280 is only one set of dynamic relocs. Any section that was actually
4281 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4282 the dynamic symbol table, is considered to be a dynamic reloc
4283 section. */
4284
4285 long
4286 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4287 bfd *abfd;
4288 arelent **storage;
4289 asymbol **syms;
4290 {
4291 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4292 asection *s;
4293 long ret;
4294
4295 if (elf_dynsymtab (abfd) == 0)
4296 {
4297 bfd_set_error (bfd_error_invalid_operation);
4298 return -1;
4299 }
4300
4301 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4302 ret = 0;
4303 for (s = abfd->sections; s != NULL; s = s->next)
4304 {
4305 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4306 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4307 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4308 {
4309 arelent *p;
4310 long count, i;
4311
4312 if (! (*slurp_relocs) (abfd, s, syms, true))
4313 return -1;
4314 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4315 p = s->relocation;
4316 for (i = 0; i < count; i++)
4317 *storage++ = p++;
4318 ret += count;
4319 }
4320 }
4321
4322 *storage = NULL;
4323
4324 return ret;
4325 }
4326 \f
4327 /* Read in the version information. */
4328
4329 boolean
4330 _bfd_elf_slurp_version_tables (abfd)
4331 bfd *abfd;
4332 {
4333 bfd_byte *contents = NULL;
4334
4335 if (elf_dynverdef (abfd) != 0)
4336 {
4337 Elf_Internal_Shdr *hdr;
4338 Elf_External_Verdef *everdef;
4339 Elf_Internal_Verdef *iverdef;
4340 unsigned int i;
4341
4342 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4343
4344 elf_tdata (abfd)->verdef =
4345 ((Elf_Internal_Verdef *)
4346 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4347 if (elf_tdata (abfd)->verdef == NULL)
4348 goto error_return;
4349
4350 elf_tdata (abfd)->cverdefs = hdr->sh_info;
4351
4352 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4353 if (contents == NULL)
4354 goto error_return;
4355 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4356 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4357 goto error_return;
4358
4359 everdef = (Elf_External_Verdef *) contents;
4360 iverdef = elf_tdata (abfd)->verdef;
4361 for (i = 0; i < hdr->sh_info; i++, iverdef++)
4362 {
4363 Elf_External_Verdaux *everdaux;
4364 Elf_Internal_Verdaux *iverdaux;
4365 unsigned int j;
4366
4367 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4368
4369 iverdef->vd_bfd = abfd;
4370
4371 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4372 bfd_alloc (abfd,
4373 (iverdef->vd_cnt
4374 * sizeof (Elf_Internal_Verdaux))));
4375 if (iverdef->vd_auxptr == NULL)
4376 goto error_return;
4377
4378 everdaux = ((Elf_External_Verdaux *)
4379 ((bfd_byte *) everdef + iverdef->vd_aux));
4380 iverdaux = iverdef->vd_auxptr;
4381 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4382 {
4383 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4384
4385 iverdaux->vda_nodename =
4386 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4387 iverdaux->vda_name);
4388 if (iverdaux->vda_nodename == NULL)
4389 goto error_return;
4390
4391 if (j + 1 < iverdef->vd_cnt)
4392 iverdaux->vda_nextptr = iverdaux + 1;
4393 else
4394 iverdaux->vda_nextptr = NULL;
4395
4396 everdaux = ((Elf_External_Verdaux *)
4397 ((bfd_byte *) everdaux + iverdaux->vda_next));
4398 }
4399
4400 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4401
4402 if (i + 1 < hdr->sh_info)
4403 iverdef->vd_nextdef = iverdef + 1;
4404 else
4405 iverdef->vd_nextdef = NULL;
4406
4407 everdef = ((Elf_External_Verdef *)
4408 ((bfd_byte *) everdef + iverdef->vd_next));
4409 }
4410
4411 free (contents);
4412 contents = NULL;
4413 }
4414
4415 if (elf_dynverref (abfd) != 0)
4416 {
4417 Elf_Internal_Shdr *hdr;
4418 Elf_External_Verneed *everneed;
4419 Elf_Internal_Verneed *iverneed;
4420 unsigned int i;
4421
4422 hdr = &elf_tdata (abfd)->dynverref_hdr;
4423
4424 elf_tdata (abfd)->verref =
4425 ((Elf_Internal_Verneed *)
4426 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4427 if (elf_tdata (abfd)->verref == NULL)
4428 goto error_return;
4429
4430 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4431
4432 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4433 if (contents == NULL)
4434 goto error_return;
4435 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4436 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4437 goto error_return;
4438
4439 everneed = (Elf_External_Verneed *) contents;
4440 iverneed = elf_tdata (abfd)->verref;
4441 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4442 {
4443 Elf_External_Vernaux *evernaux;
4444 Elf_Internal_Vernaux *ivernaux;
4445 unsigned int j;
4446
4447 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4448
4449 iverneed->vn_bfd = abfd;
4450
4451 iverneed->vn_filename =
4452 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4453 iverneed->vn_file);
4454 if (iverneed->vn_filename == NULL)
4455 goto error_return;
4456
4457 iverneed->vn_auxptr =
4458 ((Elf_Internal_Vernaux *)
4459 bfd_alloc (abfd,
4460 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4461
4462 evernaux = ((Elf_External_Vernaux *)
4463 ((bfd_byte *) everneed + iverneed->vn_aux));
4464 ivernaux = iverneed->vn_auxptr;
4465 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4466 {
4467 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4468
4469 ivernaux->vna_nodename =
4470 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4471 ivernaux->vna_name);
4472 if (ivernaux->vna_nodename == NULL)
4473 goto error_return;
4474
4475 if (j + 1 < iverneed->vn_cnt)
4476 ivernaux->vna_nextptr = ivernaux + 1;
4477 else
4478 ivernaux->vna_nextptr = NULL;
4479
4480 evernaux = ((Elf_External_Vernaux *)
4481 ((bfd_byte *) evernaux + ivernaux->vna_next));
4482 }
4483
4484 if (i + 1 < hdr->sh_info)
4485 iverneed->vn_nextref = iverneed + 1;
4486 else
4487 iverneed->vn_nextref = NULL;
4488
4489 everneed = ((Elf_External_Verneed *)
4490 ((bfd_byte *) everneed + iverneed->vn_next));
4491 }
4492
4493 free (contents);
4494 contents = NULL;
4495 }
4496
4497 return true;
4498
4499 error_return:
4500 if (contents == NULL)
4501 free (contents);
4502 return false;
4503 }
4504 \f
4505 asymbol *
4506 _bfd_elf_make_empty_symbol (abfd)
4507 bfd *abfd;
4508 {
4509 elf_symbol_type *newsym;
4510
4511 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4512 if (!newsym)
4513 return NULL;
4514 else
4515 {
4516 newsym->symbol.the_bfd = abfd;
4517 return &newsym->symbol;
4518 }
4519 }
4520
4521 void
4522 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4523 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4524 asymbol *symbol;
4525 symbol_info *ret;
4526 {
4527 bfd_symbol_info (symbol, ret);
4528 }
4529
4530 /* Return whether a symbol name implies a local symbol. Most targets
4531 use this function for the is_local_label_name entry point, but some
4532 override it. */
4533
4534 boolean
4535 _bfd_elf_is_local_label_name (abfd, name)
4536 bfd *abfd ATTRIBUTE_UNUSED;
4537 const char *name;
4538 {
4539 /* Normal local symbols start with ``.L''. */
4540 if (name[0] == '.' && name[1] == 'L')
4541 return true;
4542
4543 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4544 DWARF debugging symbols starting with ``..''. */
4545 if (name[0] == '.' && name[1] == '.')
4546 return true;
4547
4548 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4549 emitting DWARF debugging output. I suspect this is actually a
4550 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4551 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4552 underscore to be emitted on some ELF targets). For ease of use,
4553 we treat such symbols as local. */
4554 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4555 return true;
4556
4557 return false;
4558 }
4559
4560 alent *
4561 _bfd_elf_get_lineno (ignore_abfd, symbol)
4562 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4563 asymbol *symbol ATTRIBUTE_UNUSED;
4564 {
4565 abort ();
4566 return NULL;
4567 }
4568
4569 boolean
4570 _bfd_elf_set_arch_mach (abfd, arch, machine)
4571 bfd *abfd;
4572 enum bfd_architecture arch;
4573 unsigned long machine;
4574 {
4575 /* If this isn't the right architecture for this backend, and this
4576 isn't the generic backend, fail. */
4577 if (arch != get_elf_backend_data (abfd)->arch
4578 && arch != bfd_arch_unknown
4579 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4580 return false;
4581
4582 return bfd_default_set_arch_mach (abfd, arch, machine);
4583 }
4584
4585 /* Find the nearest line to a particular section and offset, for error
4586 reporting. */
4587
4588 boolean
4589 _bfd_elf_find_nearest_line (abfd,
4590 section,
4591 symbols,
4592 offset,
4593 filename_ptr,
4594 functionname_ptr,
4595 line_ptr)
4596 bfd *abfd;
4597 asection *section;
4598 asymbol **symbols;
4599 bfd_vma offset;
4600 CONST char **filename_ptr;
4601 CONST char **functionname_ptr;
4602 unsigned int *line_ptr;
4603 {
4604 boolean found;
4605 const char *filename;
4606 asymbol *func;
4607 bfd_vma low_func;
4608 asymbol **p;
4609
4610 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4611 filename_ptr, functionname_ptr,
4612 line_ptr))
4613 return true;
4614
4615 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4616 filename_ptr, functionname_ptr,
4617 line_ptr, 0))
4618 return true;
4619
4620 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4621 &found, filename_ptr,
4622 functionname_ptr, line_ptr,
4623 &elf_tdata (abfd)->line_info))
4624 return false;
4625 if (found)
4626 return true;
4627
4628 if (symbols == NULL)
4629 return false;
4630
4631 filename = NULL;
4632 func = NULL;
4633 low_func = 0;
4634
4635 for (p = symbols; *p != NULL; p++)
4636 {
4637 elf_symbol_type *q;
4638
4639 q = (elf_symbol_type *) *p;
4640
4641 if (bfd_get_section (&q->symbol) != section)
4642 continue;
4643
4644 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4645 {
4646 default:
4647 break;
4648 case STT_FILE:
4649 filename = bfd_asymbol_name (&q->symbol);
4650 break;
4651 case STT_NOTYPE:
4652 case STT_FUNC:
4653 if (q->symbol.section == section
4654 && q->symbol.value >= low_func
4655 && q->symbol.value <= offset)
4656 {
4657 func = (asymbol *) q;
4658 low_func = q->symbol.value;
4659 }
4660 break;
4661 }
4662 }
4663
4664 if (func == NULL)
4665 return false;
4666
4667 *filename_ptr = filename;
4668 *functionname_ptr = bfd_asymbol_name (func);
4669 *line_ptr = 0;
4670 return true;
4671 }
4672
4673 int
4674 _bfd_elf_sizeof_headers (abfd, reloc)
4675 bfd *abfd;
4676 boolean reloc;
4677 {
4678 int ret;
4679
4680 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4681 if (! reloc)
4682 ret += get_program_header_size (abfd);
4683 return ret;
4684 }
4685
4686 boolean
4687 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4688 bfd *abfd;
4689 sec_ptr section;
4690 PTR location;
4691 file_ptr offset;
4692 bfd_size_type count;
4693 {
4694 Elf_Internal_Shdr *hdr;
4695
4696 if (! abfd->output_has_begun
4697 && ! _bfd_elf_compute_section_file_positions
4698 (abfd, (struct bfd_link_info *) NULL))
4699 return false;
4700
4701 hdr = &elf_section_data (section)->this_hdr;
4702
4703 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4704 return false;
4705 if (bfd_write (location, 1, count, abfd) != count)
4706 return false;
4707
4708 return true;
4709 }
4710
4711 void
4712 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4713 bfd *abfd ATTRIBUTE_UNUSED;
4714 arelent *cache_ptr ATTRIBUTE_UNUSED;
4715 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
4716 {
4717 abort ();
4718 }
4719
4720 #if 0
4721 void
4722 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4723 bfd *abfd;
4724 arelent *cache_ptr;
4725 Elf_Internal_Rel *dst;
4726 {
4727 abort ();
4728 }
4729 #endif
4730
4731 /* Try to convert a non-ELF reloc into an ELF one. */
4732
4733 boolean
4734 _bfd_elf_validate_reloc (abfd, areloc)
4735 bfd *abfd;
4736 arelent *areloc;
4737 {
4738 /* Check whether we really have an ELF howto. */
4739
4740 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4741 {
4742 bfd_reloc_code_real_type code;
4743 reloc_howto_type *howto;
4744
4745 /* Alien reloc: Try to determine its type to replace it with an
4746 equivalent ELF reloc. */
4747
4748 if (areloc->howto->pc_relative)
4749 {
4750 switch (areloc->howto->bitsize)
4751 {
4752 case 8:
4753 code = BFD_RELOC_8_PCREL;
4754 break;
4755 case 12:
4756 code = BFD_RELOC_12_PCREL;
4757 break;
4758 case 16:
4759 code = BFD_RELOC_16_PCREL;
4760 break;
4761 case 24:
4762 code = BFD_RELOC_24_PCREL;
4763 break;
4764 case 32:
4765 code = BFD_RELOC_32_PCREL;
4766 break;
4767 case 64:
4768 code = BFD_RELOC_64_PCREL;
4769 break;
4770 default:
4771 goto fail;
4772 }
4773
4774 howto = bfd_reloc_type_lookup (abfd, code);
4775
4776 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4777 {
4778 if (howto->pcrel_offset)
4779 areloc->addend += areloc->address;
4780 else
4781 areloc->addend -= areloc->address; /* addend is unsigned!! */
4782 }
4783 }
4784 else
4785 {
4786 switch (areloc->howto->bitsize)
4787 {
4788 case 8:
4789 code = BFD_RELOC_8;
4790 break;
4791 case 14:
4792 code = BFD_RELOC_14;
4793 break;
4794 case 16:
4795 code = BFD_RELOC_16;
4796 break;
4797 case 26:
4798 code = BFD_RELOC_26;
4799 break;
4800 case 32:
4801 code = BFD_RELOC_32;
4802 break;
4803 case 64:
4804 code = BFD_RELOC_64;
4805 break;
4806 default:
4807 goto fail;
4808 }
4809
4810 howto = bfd_reloc_type_lookup (abfd, code);
4811 }
4812
4813 if (howto)
4814 areloc->howto = howto;
4815 else
4816 goto fail;
4817 }
4818
4819 return true;
4820
4821 fail:
4822 (*_bfd_error_handler)
4823 (_("%s: unsupported relocation type %s"),
4824 bfd_get_filename (abfd), areloc->howto->name);
4825 bfd_set_error (bfd_error_bad_value);
4826 return false;
4827 }
4828
4829 boolean
4830 _bfd_elf_close_and_cleanup (abfd)
4831 bfd *abfd;
4832 {
4833 if (bfd_get_format (abfd) == bfd_object)
4834 {
4835 if (elf_shstrtab (abfd) != NULL)
4836 _bfd_stringtab_free (elf_shstrtab (abfd));
4837 }
4838
4839 return _bfd_generic_close_and_cleanup (abfd);
4840 }
4841
4842 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4843 in the relocation's offset. Thus we cannot allow any sort of sanity
4844 range-checking to interfere. There is nothing else to do in processing
4845 this reloc. */
4846
4847 bfd_reloc_status_type
4848 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
4849 bfd *abfd ATTRIBUTE_UNUSED;
4850 arelent *re ATTRIBUTE_UNUSED;
4851 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
4852 PTR data ATTRIBUTE_UNUSED;
4853 asection *is ATTRIBUTE_UNUSED;
4854 bfd *obfd ATTRIBUTE_UNUSED;
4855 char **errmsg ATTRIBUTE_UNUSED;
4856 {
4857 return bfd_reloc_ok;
4858 }
4859
4860 \f
4861 /* Elf core file support. Much of this only works on native
4862 toolchains, since we rely on knowing the
4863 machine-dependent procfs structure in order to pick
4864 out details about the corefile. */
4865
4866 #ifdef HAVE_SYS_PROCFS_H
4867 # include <sys/procfs.h>
4868 #endif
4869
4870
4871 /* Define offsetof for those systems which lack it. */
4872
4873 #ifndef offsetof
4874 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
4875 #endif
4876
4877
4878 /* FIXME: this is kinda wrong, but it's what gdb wants. */
4879
4880 static int
4881 elfcore_make_pid (abfd)
4882 bfd* abfd;
4883 {
4884 return ((elf_tdata (abfd)->core_lwpid << 16)
4885 + (elf_tdata (abfd)->core_pid));
4886 }
4887
4888
4889 /* If there isn't a section called NAME, make one, using
4890 data from SECT. Note, this function will generate a
4891 reference to NAME, so you shouldn't deallocate or
4892 overwrite it. */
4893
4894 static boolean
4895 elfcore_maybe_make_sect (abfd, name, sect)
4896 bfd* abfd;
4897 char* name;
4898 asection* sect;
4899 {
4900 asection* sect2;
4901
4902 if (bfd_get_section_by_name (abfd, name) != NULL)
4903 return true;
4904
4905 sect2 = bfd_make_section (abfd, name);
4906 if (sect2 == NULL)
4907 return false;
4908
4909 sect2->_raw_size = sect->_raw_size;
4910 sect2->filepos = sect->filepos;
4911 sect2->flags = sect->flags;
4912 sect2->alignment_power = sect->alignment_power;
4913 return true;
4914 }
4915
4916
4917 /* prstatus_t exists on:
4918 solaris 2.[567]
4919 linux 2.[01] + glibc
4920 unixware 4.2
4921 */
4922
4923 #if defined (HAVE_PRSTATUS_T)
4924 static boolean
4925 elfcore_grok_prstatus (abfd, note)
4926 bfd* abfd;
4927 Elf_Internal_Note* note;
4928 {
4929 prstatus_t prstat;
4930 char buf[100];
4931 char* name;
4932 asection* sect;
4933
4934 if (note->descsz != sizeof (prstat))
4935 return true;
4936
4937 memcpy (&prstat, note->descdata, sizeof (prstat));
4938
4939 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
4940 elf_tdata (abfd)->core_pid = prstat.pr_pid;
4941
4942 /* pr_who exists on:
4943 solaris 2.[567]
4944 unixware 4.2
4945 pr_who doesn't exist on:
4946 linux 2.[01]
4947 */
4948 #if defined (HAVE_PRSTATUS_T_PR_WHO)
4949 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
4950 #endif
4951
4952 /* Make a ".reg/999" section. */
4953
4954 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
4955 name = bfd_alloc (abfd, strlen (buf) + 1);
4956 if (name == NULL)
4957 return false;
4958 strcpy (name, buf);
4959
4960 sect = bfd_make_section (abfd, name);
4961 if (sect == NULL)
4962 return false;
4963 sect->_raw_size = sizeof (prstat.pr_reg);
4964 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
4965 sect->flags = SEC_HAS_CONTENTS;
4966 sect->alignment_power = 2;
4967
4968 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
4969 return false;
4970
4971 return true;
4972 }
4973 #endif /* defined (HAVE_PRSTATUS_T) */
4974
4975
4976 /* There isn't a consistent prfpregset_t across platforms,
4977 but it doesn't matter, because we don't have to pick this
4978 data structure apart. */
4979
4980 static boolean
4981 elfcore_grok_prfpreg (abfd, note)
4982 bfd* abfd;
4983 Elf_Internal_Note* note;
4984 {
4985 char buf[100];
4986 char* name;
4987 asection* sect;
4988
4989 /* Make a ".reg2/999" section. */
4990
4991 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
4992 name = bfd_alloc (abfd, strlen (buf) + 1);
4993 if (name == NULL)
4994 return false;
4995 strcpy (name, buf);
4996
4997 sect = bfd_make_section (abfd, name);
4998 if (sect == NULL)
4999 return false;
5000 sect->_raw_size = note->descsz;
5001 sect->filepos = note->descpos;
5002 sect->flags = SEC_HAS_CONTENTS;
5003 sect->alignment_power = 2;
5004
5005 if (! elfcore_maybe_make_sect (abfd, ".reg2", sect))
5006 return false;
5007
5008 return true;
5009 }
5010
5011 #if defined (HAVE_PRPSINFO_T)
5012 # define elfcore_psinfo_t prpsinfo_t
5013 #endif
5014
5015 #if defined (HAVE_PSINFO_T)
5016 # define elfcore_psinfo_t psinfo_t
5017 #endif
5018
5019
5020 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5021
5022 /* return a malloc'ed copy of a string at START which is at
5023 most MAX bytes long, possibly without a terminating '\0'.
5024 the copy will always have a terminating '\0'. */
5025
5026 static char*
5027 elfcore_strndup (abfd, start, max)
5028 bfd* abfd;
5029 char* start;
5030 int max;
5031 {
5032 char* dup;
5033 char* end = memchr (start, '\0', max);
5034 int len;
5035
5036 if (end == NULL)
5037 len = max;
5038 else
5039 len = end - start;
5040
5041 dup = bfd_alloc (abfd, len + 1);
5042 if (dup == NULL)
5043 return NULL;
5044
5045 memcpy (dup, start, len);
5046 dup[len] = '\0';
5047
5048 return dup;
5049 }
5050
5051 static boolean
5052 elfcore_grok_psinfo (abfd, note)
5053 bfd* abfd;
5054 Elf_Internal_Note* note;
5055 {
5056 elfcore_psinfo_t psinfo;
5057
5058 if (note->descsz != sizeof (elfcore_psinfo_t))
5059 return true;
5060
5061 memcpy (&psinfo, note->descdata, note->descsz);
5062
5063 elf_tdata (abfd)->core_program
5064 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5065
5066 elf_tdata (abfd)->core_command
5067 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5068
5069 /* Note that for some reason, a spurious space is tacked
5070 onto the end of the args in some (at least one anyway)
5071 implementations, so strip it off if it exists. */
5072
5073 {
5074 char* command = elf_tdata (abfd)->core_command;
5075 int n = strlen (command);
5076
5077 if (0 < n && command[n - 1] == ' ')
5078 command[n - 1] = '\0';
5079 }
5080
5081 return true;
5082 }
5083 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5084
5085
5086 #if defined (HAVE_PSTATUS_T)
5087 static boolean
5088 elfcore_grok_pstatus (abfd, note)
5089 bfd* abfd;
5090 Elf_Internal_Note* note;
5091 {
5092 pstatus_t pstat;
5093
5094 if (note->descsz != sizeof (pstat))
5095 return true;
5096
5097 memcpy (&pstat, note->descdata, sizeof (pstat));
5098
5099 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5100
5101 /* Could grab some more details from the "representative"
5102 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5103 NT_LWPSTATUS note, presumably. */
5104
5105 return true;
5106 }
5107 #endif /* defined (HAVE_PSTATUS_T) */
5108
5109
5110 #if defined (HAVE_LWPSTATUS_T)
5111 static boolean
5112 elfcore_grok_lwpstatus (abfd, note)
5113 bfd* abfd;
5114 Elf_Internal_Note* note;
5115 {
5116 lwpstatus_t lwpstat;
5117 char buf[100];
5118 char* name;
5119 asection* sect;
5120
5121 if (note->descsz != sizeof (lwpstat))
5122 return true;
5123
5124 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5125
5126 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5127 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5128
5129 /* Make a ".reg/999" section. */
5130
5131 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5132 name = bfd_alloc (abfd, strlen (buf) + 1);
5133 if (name == NULL)
5134 return false;
5135 strcpy (name, buf);
5136
5137 sect = bfd_make_section (abfd, name);
5138 if (sect == NULL)
5139 return false;
5140
5141 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5142 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5143 sect->filepos = note->descpos
5144 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5145 #endif
5146
5147 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5148 sect->_raw_size = sizeof (lwpstat.pr_reg);
5149 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5150 #endif
5151
5152 sect->flags = SEC_HAS_CONTENTS;
5153 sect->alignment_power = 2;
5154
5155 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5156 return false;
5157
5158 /* Make a ".reg2/999" section */
5159
5160 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5161 name = bfd_alloc (abfd, strlen (buf) + 1);
5162 if (name == NULL)
5163 return false;
5164 strcpy (name, buf);
5165
5166 sect = bfd_make_section (abfd, name);
5167 if (sect == NULL)
5168 return false;
5169
5170 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5171 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5172 sect->filepos = note->descpos
5173 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5174 #endif
5175
5176 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5177 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5178 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5179 #endif
5180
5181 sect->flags = SEC_HAS_CONTENTS;
5182 sect->alignment_power = 2;
5183
5184 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5185 return false;
5186
5187 return true;
5188 }
5189 #endif /* defined (HAVE_LWPSTATUS_T) */
5190
5191
5192
5193 static boolean
5194 elfcore_grok_note (abfd, note)
5195 bfd* abfd;
5196 Elf_Internal_Note* note;
5197 {
5198 switch (note->type)
5199 {
5200 default:
5201 return true;
5202
5203 #if defined (HAVE_PRSTATUS_T)
5204 case NT_PRSTATUS:
5205 return elfcore_grok_prstatus (abfd, note);
5206 #endif
5207
5208 #if defined (HAVE_PSTATUS_T)
5209 case NT_PSTATUS:
5210 return elfcore_grok_pstatus (abfd, note);
5211 #endif
5212
5213 #if defined (HAVE_LWPSTATUS_T)
5214 case NT_LWPSTATUS:
5215 return elfcore_grok_lwpstatus (abfd, note);
5216 #endif
5217
5218 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5219 return elfcore_grok_prfpreg (abfd, note);
5220
5221 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5222 case NT_PRPSINFO:
5223 case NT_PSINFO:
5224 return elfcore_grok_psinfo (abfd, note);
5225 #endif
5226 }
5227 }
5228
5229
5230 static boolean
5231 elfcore_read_notes (abfd, offset, size)
5232 bfd* abfd;
5233 bfd_vma offset;
5234 bfd_vma size;
5235 {
5236 char* buf;
5237 char* p;
5238
5239 if (size <= 0)
5240 return true;
5241
5242 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5243 return false;
5244
5245 buf = bfd_malloc ((size_t) size);
5246 if (buf == NULL)
5247 return false;
5248
5249 if (bfd_read (buf, size, 1, abfd) != size)
5250 {
5251 error:
5252 free (buf);
5253 return false;
5254 }
5255
5256 p = buf;
5257 while (p < buf + size)
5258 {
5259 /* FIXME: bad alignment assumption. */
5260 Elf_External_Note* xnp = (Elf_External_Note*) p;
5261 Elf_Internal_Note in;
5262
5263 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5264
5265 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5266 in.namedata = xnp->name;
5267
5268 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5269 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5270 in.descpos = offset + (in.descdata - buf);
5271
5272 if (! elfcore_grok_note (abfd, &in))
5273 goto error;
5274
5275 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5276 }
5277
5278 free (buf);
5279 return true;
5280 }
5281
5282
5283
5284 boolean
5285 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5286 bfd* abfd;
5287 Elf_Internal_Phdr* phdr;
5288 int sec_num;
5289 {
5290 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5291 return false;
5292
5293 if (phdr->p_type == PT_NOTE
5294 && ! elfcore_read_notes (abfd, phdr->p_offset, phdr->p_filesz))
5295 return false;
5296
5297 return true;
5298 }
5299
This page took 0.137989 seconds and 5 git commands to generate.