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