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