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