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