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