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