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 | |
39 | #include "libelf.h" | |
40 | ||
ede4eed4 KR |
41 | static file_ptr map_program_segments PARAMS ((bfd *, file_ptr, |
42 | Elf_Internal_Shdr *, | |
43 | Elf_Internal_Shdr **, | |
44 | bfd_size_type)); | |
45 | static boolean assign_file_positions_except_relocs PARAMS ((bfd *, boolean)); | |
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 | ||
b176e1e9 | 222 | hdr->bfd_section = newsect; |
497c5434 ILT |
223 | elf_section_data (newsect)->this_hdr = *hdr; |
224 | ||
225 | return true; | |
226 | } | |
227 | ||
32090b8e KR |
228 | /* |
229 | INTERNAL_FUNCTION | |
230 | bfd_elf_find_section | |
231 | ||
232 | SYNOPSIS | |
233 | struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); | |
234 | ||
235 | DESCRIPTION | |
236 | Helper functions for GDB to locate the string tables. | |
237 | Since BFD hides string tables from callers, GDB needs to use an | |
238 | internal hook to find them. Sun's .stabstr, in particular, | |
239 | isn't even pointed to by the .stab section, so ordinary | |
240 | mechanisms wouldn't work to find it, even if we had some. | |
241 | */ | |
242 | ||
243 | struct elf_internal_shdr * | |
013dec1a ILT |
244 | bfd_elf_find_section (abfd, name) |
245 | bfd * abfd; | |
246 | char *name; | |
32090b8e KR |
247 | { |
248 | Elf_Internal_Shdr **i_shdrp; | |
249 | char *shstrtab; | |
250 | unsigned int max; | |
251 | unsigned int i; | |
252 | ||
253 | i_shdrp = elf_elfsections (abfd); | |
254 | if (i_shdrp != NULL) | |
255 | { | |
ede4eed4 | 256 | shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx); |
32090b8e KR |
257 | if (shstrtab != NULL) |
258 | { | |
259 | max = elf_elfheader (abfd)->e_shnum; | |
260 | for (i = 1; i < max; i++) | |
261 | if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) | |
262 | return i_shdrp[i]; | |
263 | } | |
264 | } | |
265 | return 0; | |
266 | } | |
267 | ||
32090b8e KR |
268 | const char *const bfd_elf_section_type_names[] = { |
269 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
270 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
271 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
272 | }; | |
273 | ||
274 | /* ELF relocs are against symbols. If we are producing relocateable | |
275 | output, and the reloc is against an external symbol, and nothing | |
276 | has given us any additional addend, the resulting reloc will also | |
277 | be against the same symbol. In such a case, we don't want to | |
278 | change anything about the way the reloc is handled, since it will | |
279 | all be done at final link time. Rather than put special case code | |
280 | into bfd_perform_relocation, all the reloc types use this howto | |
281 | function. It just short circuits the reloc if producing | |
282 | relocateable output against an external symbol. */ | |
283 | ||
013dec1a | 284 | /*ARGSUSED*/ |
32090b8e KR |
285 | bfd_reloc_status_type |
286 | bfd_elf_generic_reloc (abfd, | |
287 | reloc_entry, | |
288 | symbol, | |
289 | data, | |
290 | input_section, | |
4c3721d5 ILT |
291 | output_bfd, |
292 | error_message) | |
32090b8e KR |
293 | bfd *abfd; |
294 | arelent *reloc_entry; | |
295 | asymbol *symbol; | |
296 | PTR data; | |
297 | asection *input_section; | |
298 | bfd *output_bfd; | |
4c3721d5 | 299 | char **error_message; |
32090b8e KR |
300 | { |
301 | if (output_bfd != (bfd *) NULL | |
302 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
d1b44e83 ILT |
303 | && (! reloc_entry->howto->partial_inplace |
304 | || reloc_entry->addend == 0)) | |
32090b8e KR |
305 | { |
306 | reloc_entry->address += input_section->output_offset; | |
307 | return bfd_reloc_ok; | |
308 | } | |
309 | ||
310 | return bfd_reloc_continue; | |
311 | } | |
013dec1a | 312 | \f |
b176e1e9 ILT |
313 | /* Display ELF-specific fields of a symbol. */ |
314 | void | |
315 | bfd_elf_print_symbol (ignore_abfd, filep, symbol, how) | |
316 | bfd *ignore_abfd; | |
317 | PTR filep; | |
318 | asymbol *symbol; | |
319 | bfd_print_symbol_type how; | |
320 | { | |
321 | FILE *file = (FILE *) filep; | |
322 | switch (how) | |
323 | { | |
324 | case bfd_print_symbol_name: | |
325 | fprintf (file, "%s", symbol->name); | |
326 | break; | |
327 | case bfd_print_symbol_more: | |
328 | fprintf (file, "elf "); | |
329 | fprintf_vma (file, symbol->value); | |
330 | fprintf (file, " %lx", (long) symbol->flags); | |
331 | break; | |
332 | case bfd_print_symbol_all: | |
333 | { | |
334 | CONST char *section_name; | |
335 | section_name = symbol->section ? symbol->section->name : "(*none*)"; | |
336 | bfd_print_symbol_vandf ((PTR) file, symbol); | |
337 | fprintf (file, " %s\t", section_name); | |
338 | /* Print the "other" value for a symbol. For common symbols, | |
339 | we've already printed the size; now print the alignment. | |
340 | For other symbols, we have no specified alignment, and | |
341 | we've printed the address; now print the size. */ | |
342 | fprintf_vma (file, | |
343 | (bfd_is_com_section (symbol->section) | |
344 | ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value | |
345 | : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size)); | |
346 | fprintf (file, " %s", symbol->name); | |
347 | } | |
348 | break; | |
349 | } | |
350 | } | |
351 | \f | |
013dec1a ILT |
352 | /* Create an entry in an ELF linker hash table. */ |
353 | ||
5315c428 ILT |
354 | struct bfd_hash_entry * |
355 | _bfd_elf_link_hash_newfunc (entry, table, string) | |
013dec1a ILT |
356 | struct bfd_hash_entry *entry; |
357 | struct bfd_hash_table *table; | |
358 | const char *string; | |
359 | { | |
360 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
361 | ||
362 | /* Allocate the structure if it has not already been allocated by a | |
363 | subclass. */ | |
364 | if (ret == (struct elf_link_hash_entry *) NULL) | |
365 | ret = ((struct elf_link_hash_entry *) | |
366 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry))); | |
367 | if (ret == (struct elf_link_hash_entry *) NULL) | |
368 | { | |
369 | bfd_set_error (bfd_error_no_memory); | |
370 | return (struct bfd_hash_entry *) ret; | |
371 | } | |
372 | ||
373 | /* Call the allocation method of the superclass. */ | |
374 | ret = ((struct elf_link_hash_entry *) | |
375 | _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
376 | table, string)); | |
377 | if (ret != (struct elf_link_hash_entry *) NULL) | |
378 | { | |
379 | /* Set local fields. */ | |
380 | ret->indx = -1; | |
381 | ret->size = 0; | |
013dec1a ILT |
382 | ret->dynindx = -1; |
383 | ret->dynstr_index = 0; | |
384 | ret->weakdef = NULL; | |
b176e1e9 ILT |
385 | ret->got_offset = (bfd_vma) -1; |
386 | ret->plt_offset = (bfd_vma) -1; | |
013dec1a ILT |
387 | ret->type = STT_NOTYPE; |
388 | ret->elf_link_hash_flags = 0; | |
389 | } | |
390 | ||
391 | return (struct bfd_hash_entry *) ret; | |
392 | } | |
393 | ||
5315c428 ILT |
394 | /* Initialize an ELF linker hash table. */ |
395 | ||
396 | boolean | |
397 | _bfd_elf_link_hash_table_init (table, abfd, newfunc) | |
398 | struct elf_link_hash_table *table; | |
399 | bfd *abfd; | |
400 | struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, | |
401 | struct bfd_hash_table *, | |
402 | const char *)); | |
403 | { | |
b176e1e9 | 404 | table->dynamic_sections_created = false; |
5315c428 | 405 | table->dynobj = NULL; |
b176e1e9 ILT |
406 | /* The first dynamic symbol is a dummy. */ |
407 | table->dynsymcount = 1; | |
5315c428 ILT |
408 | table->dynstr = NULL; |
409 | table->bucketcount = 0; | |
b176e1e9 | 410 | table->needed = NULL; |
5315c428 ILT |
411 | return _bfd_link_hash_table_init (&table->root, abfd, newfunc); |
412 | } | |
413 | ||
013dec1a ILT |
414 | /* Create an ELF linker hash table. */ |
415 | ||
416 | struct bfd_link_hash_table * | |
417 | _bfd_elf_link_hash_table_create (abfd) | |
418 | bfd *abfd; | |
419 | { | |
420 | struct elf_link_hash_table *ret; | |
421 | ||
422 | ret = ((struct elf_link_hash_table *) | |
423 | bfd_alloc (abfd, sizeof (struct elf_link_hash_table))); | |
424 | if (ret == (struct elf_link_hash_table *) NULL) | |
425 | { | |
426 | bfd_set_error (bfd_error_no_memory); | |
427 | return NULL; | |
428 | } | |
5315c428 ILT |
429 | |
430 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc)) | |
013dec1a ILT |
431 | { |
432 | bfd_release (abfd, ret); | |
433 | return NULL; | |
434 | } | |
435 | ||
013dec1a ILT |
436 | return &ret->root; |
437 | } | |
7c6da9ca ILT |
438 | |
439 | /* This is a hook for the ELF emulation code in the generic linker to | |
440 | tell the backend linker what file name to use for the DT_NEEDED | |
b176e1e9 ILT |
441 | entry for a dynamic object. The generic linker passes name as an |
442 | empty string to indicate that no DT_NEEDED entry should be made. */ | |
7c6da9ca ILT |
443 | |
444 | void | |
445 | bfd_elf_set_dt_needed_name (abfd, name) | |
446 | bfd *abfd; | |
447 | const char *name; | |
448 | { | |
449 | elf_dt_needed_name (abfd) = name; | |
450 | } | |
b176e1e9 ILT |
451 | |
452 | /* Get the list of DT_NEEDED entries for a link. */ | |
453 | ||
454 | struct bfd_elf_link_needed_list * | |
455 | bfd_elf_get_needed_list (abfd, info) | |
456 | bfd *abfd; | |
457 | struct bfd_link_info *info; | |
458 | { | |
459 | return elf_hash_table (info)->needed; | |
460 | } | |
ede4eed4 KR |
461 | \f |
462 | /* Allocate an ELF string table--force the first byte to be zero. */ | |
463 | ||
464 | struct bfd_strtab_hash * | |
465 | _bfd_elf_stringtab_init () | |
466 | { | |
467 | struct bfd_strtab_hash *ret; | |
468 | ||
469 | ret = _bfd_stringtab_init (); | |
470 | if (ret != NULL) | |
471 | { | |
472 | bfd_size_type loc; | |
473 | ||
474 | loc = _bfd_stringtab_add (ret, "", true, false); | |
475 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); | |
476 | if (loc == (bfd_size_type) -1) | |
477 | { | |
478 | _bfd_stringtab_free (ret); | |
479 | ret = NULL; | |
480 | } | |
481 | } | |
482 | return ret; | |
483 | } | |
484 | \f | |
485 | /* ELF .o/exec file reading */ | |
486 | ||
487 | /* Create a new bfd section from an ELF section header. */ | |
488 | ||
489 | boolean | |
490 | bfd_section_from_shdr (abfd, shindex) | |
491 | bfd *abfd; | |
492 | unsigned int shindex; | |
493 | { | |
494 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; | |
495 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
496 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
497 | char *name; | |
498 | ||
499 | name = elf_string_from_elf_strtab (abfd, hdr->sh_name); | |
500 | ||
501 | switch (hdr->sh_type) | |
502 | { | |
503 | case SHT_NULL: | |
504 | /* Inactive section. Throw it away. */ | |
505 | return true; | |
506 | ||
507 | case SHT_PROGBITS: /* Normal section with contents. */ | |
508 | case SHT_DYNAMIC: /* Dynamic linking information. */ | |
509 | case SHT_NOBITS: /* .bss section. */ | |
510 | case SHT_HASH: /* .hash section. */ | |
511 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
512 | ||
513 | case SHT_SYMTAB: /* A symbol table */ | |
514 | if (elf_onesymtab (abfd) == shindex) | |
515 | return true; | |
516 | ||
517 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
518 | BFD_ASSERT (elf_onesymtab (abfd) == 0); | |
519 | elf_onesymtab (abfd) = shindex; | |
520 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
521 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr; | |
522 | abfd->flags |= HAS_SYMS; | |
523 | ||
524 | /* Sometimes a shared object will map in the symbol table. If | |
525 | SHF_ALLOC is set, and this is a shared object, then we also | |
526 | treat this section as a BFD section. We can not base the | |
527 | decision purely on SHF_ALLOC, because that flag is sometimes | |
528 | set in a relocateable object file, which would confuse the | |
529 | linker. */ | |
530 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
531 | && (abfd->flags & DYNAMIC) != 0 | |
532 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
533 | return false; | |
534 | ||
535 | return true; | |
536 | ||
537 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
538 | if (elf_dynsymtab (abfd) == shindex) | |
539 | return true; | |
540 | ||
541 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
542 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
543 | elf_dynsymtab (abfd) = shindex; | |
544 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
545 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynsymtab_hdr; | |
546 | abfd->flags |= HAS_SYMS; | |
547 | ||
548 | /* Besides being a symbol table, we also treat this as a regular | |
549 | section, so that objcopy can handle it. */ | |
550 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
551 | ||
552 | case SHT_STRTAB: /* A string table */ | |
553 | if (hdr->bfd_section != NULL) | |
554 | return true; | |
555 | if (ehdr->e_shstrndx == shindex) | |
556 | { | |
557 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
558 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
559 | return true; | |
560 | } | |
561 | { | |
562 | unsigned int i; | |
563 | ||
564 | for (i = 1; i < ehdr->e_shnum; i++) | |
565 | { | |
566 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
567 | if (hdr2->sh_link == shindex) | |
568 | { | |
569 | if (! bfd_section_from_shdr (abfd, i)) | |
570 | return false; | |
571 | if (elf_onesymtab (abfd) == i) | |
572 | { | |
573 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
574 | elf_elfsections (abfd)[shindex] = | |
575 | &elf_tdata (abfd)->strtab_hdr; | |
576 | return true; | |
577 | } | |
578 | if (elf_dynsymtab (abfd) == i) | |
579 | { | |
580 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
581 | elf_elfsections (abfd)[shindex] = | |
582 | &elf_tdata (abfd)->dynstrtab_hdr; | |
583 | /* We also treat this as a regular section, so | |
584 | that objcopy can handle it. */ | |
585 | break; | |
586 | } | |
587 | #if 0 /* Not handling other string tables specially right now. */ | |
588 | hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ | |
589 | /* We have a strtab for some random other section. */ | |
590 | newsect = (asection *) hdr2->bfd_section; | |
591 | if (!newsect) | |
592 | break; | |
593 | hdr->bfd_section = newsect; | |
594 | hdr2 = &elf_section_data (newsect)->str_hdr; | |
595 | *hdr2 = *hdr; | |
596 | elf_elfsections (abfd)[shindex] = hdr2; | |
597 | #endif | |
598 | } | |
599 | } | |
600 | } | |
601 | ||
602 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
603 | ||
604 | case SHT_REL: | |
605 | case SHT_RELA: | |
606 | /* *These* do a lot of work -- but build no sections! */ | |
607 | { | |
608 | asection *target_sect; | |
609 | Elf_Internal_Shdr *hdr2; | |
610 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
611 | ||
ae115e51 ILT |
612 | /* For some incomprehensible reason Oracle distributes |
613 | libraries for Solaris in which some of the objects have | |
614 | bogus sh_link fields. It would be nice if we could just | |
615 | reject them, but, unfortunately, some people need to use | |
616 | them. We scan through the section headers; if we find only | |
617 | one suitable symbol table, we clobber the sh_link to point | |
618 | to it. I hope this doesn't break anything. */ | |
619 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
620 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
621 | { | |
622 | int scan; | |
623 | int found; | |
624 | ||
625 | found = 0; | |
626 | for (scan = 1; scan < ehdr->e_shnum; scan++) | |
627 | { | |
628 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
629 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
630 | { | |
631 | if (found != 0) | |
632 | { | |
633 | found = 0; | |
634 | break; | |
635 | } | |
636 | found = scan; | |
637 | } | |
638 | } | |
639 | if (found != 0) | |
640 | hdr->sh_link = found; | |
641 | } | |
642 | ||
ede4eed4 | 643 | /* Get the symbol table. */ |
ae115e51 ILT |
644 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
645 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
ede4eed4 KR |
646 | return false; |
647 | ||
648 | /* If this reloc section does not use the main symbol table we | |
649 | don't treat it as a reloc section. BFD can't adequately | |
650 | represent such a section, so at least for now, we don't | |
651 | try. We just present it as a normal section. */ | |
652 | if (hdr->sh_link != elf_onesymtab (abfd)) | |
653 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
654 | ||
655 | /* Don't allow REL relocations on a machine that uses RELA and | |
656 | vice versa. */ | |
657 | /* @@ Actually, the generic ABI does suggest that both might be | |
658 | used in one file. But the four ABI Processor Supplements I | |
659 | have access to right now all specify that only one is used on | |
660 | each of those architectures. It's conceivable that, e.g., a | |
661 | bunch of absolute 32-bit relocs might be more compact in REL | |
662 | form even on a RELA machine... */ | |
663 | BFD_ASSERT (use_rela_p | |
664 | ? (hdr->sh_type == SHT_RELA | |
665 | && hdr->sh_entsize == bed->s->sizeof_rela) | |
666 | : (hdr->sh_type == SHT_REL | |
667 | && hdr->sh_entsize == bed->s->sizeof_rel)); | |
668 | ||
669 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
670 | return false; | |
671 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); | |
672 | if (target_sect == NULL) | |
673 | return false; | |
674 | ||
675 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
676 | *hdr2 = *hdr; | |
677 | elf_elfsections (abfd)[shindex] = hdr2; | |
678 | target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize; | |
679 | target_sect->flags |= SEC_RELOC; | |
680 | target_sect->relocation = NULL; | |
681 | target_sect->rel_filepos = hdr->sh_offset; | |
682 | abfd->flags |= HAS_RELOC; | |
683 | return true; | |
684 | } | |
685 | break; | |
686 | ||
687 | case SHT_NOTE: | |
688 | #if 0 | |
689 | fprintf (stderr, "Note Sections not yet supported.\n"); | |
690 | BFD_FAIL (); | |
691 | #endif | |
692 | break; | |
693 | ||
694 | case SHT_SHLIB: | |
695 | #if 0 | |
696 | fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n"); | |
697 | #endif | |
698 | return true; | |
699 | ||
700 | default: | |
701 | /* Check for any processor-specific section types. */ | |
702 | { | |
703 | if (bed->elf_backend_section_from_shdr) | |
704 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
705 | } | |
706 | break; | |
707 | } | |
708 | ||
709 | return true; | |
710 | } | |
711 | ||
712 | /* Given an ELF section number, retrieve the corresponding BFD | |
713 | section. */ | |
714 | ||
715 | asection * | |
716 | bfd_section_from_elf_index (abfd, index) | |
717 | bfd *abfd; | |
718 | unsigned int index; | |
719 | { | |
720 | BFD_ASSERT (index > 0 && index < SHN_LORESERVE); | |
721 | if (index >= elf_elfheader (abfd)->e_shnum) | |
722 | return NULL; | |
723 | return elf_elfsections (abfd)[index]->bfd_section; | |
724 | } | |
725 | ||
726 | boolean | |
727 | _bfd_elf_new_section_hook (abfd, sec) | |
728 | bfd *abfd; | |
729 | asection *sec; | |
730 | { | |
731 | struct bfd_elf_section_data *sdata; | |
732 | ||
733 | sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata)); | |
734 | if (!sdata) | |
735 | { | |
736 | bfd_set_error (bfd_error_no_memory); | |
737 | return false; | |
738 | } | |
739 | sec->used_by_bfd = (PTR) sdata; | |
740 | memset (sdata, 0, sizeof (*sdata)); | |
741 | return true; | |
742 | } | |
743 | ||
744 | /* Create a new bfd section from an ELF program header. | |
745 | ||
746 | Since program segments have no names, we generate a synthetic name | |
747 | of the form segment<NUM>, where NUM is generally the index in the | |
748 | program header table. For segments that are split (see below) we | |
749 | generate the names segment<NUM>a and segment<NUM>b. | |
750 | ||
751 | Note that some program segments may have a file size that is different than | |
752 | (less than) the memory size. All this means is that at execution the | |
753 | system must allocate the amount of memory specified by the memory size, | |
754 | but only initialize it with the first "file size" bytes read from the | |
755 | file. This would occur for example, with program segments consisting | |
756 | of combined data+bss. | |
757 | ||
758 | To handle the above situation, this routine generates TWO bfd sections | |
759 | for the single program segment. The first has the length specified by | |
760 | the file size of the segment, and the second has the length specified | |
761 | by the difference between the two sizes. In effect, the segment is split | |
762 | into it's initialized and uninitialized parts. | |
763 | ||
764 | */ | |
765 | ||
766 | boolean | |
767 | bfd_section_from_phdr (abfd, hdr, index) | |
768 | bfd *abfd; | |
769 | Elf_Internal_Phdr *hdr; | |
770 | int index; | |
771 | { | |
772 | asection *newsect; | |
773 | char *name; | |
774 | char namebuf[64]; | |
775 | int split; | |
776 | ||
777 | split = ((hdr->p_memsz > 0) && | |
778 | (hdr->p_filesz > 0) && | |
779 | (hdr->p_memsz > hdr->p_filesz)); | |
780 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
781 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
782 | if (!name) | |
783 | { | |
784 | bfd_set_error (bfd_error_no_memory); | |
785 | return false; | |
786 | } | |
787 | strcpy (name, namebuf); | |
788 | newsect = bfd_make_section (abfd, name); | |
789 | if (newsect == NULL) | |
790 | return false; | |
791 | newsect->vma = hdr->p_vaddr; | |
ae115e51 | 792 | newsect->lma = hdr->p_paddr; |
ede4eed4 KR |
793 | newsect->_raw_size = hdr->p_filesz; |
794 | newsect->filepos = hdr->p_offset; | |
795 | newsect->flags |= SEC_HAS_CONTENTS; | |
796 | if (hdr->p_type == PT_LOAD) | |
797 | { | |
798 | newsect->flags |= SEC_ALLOC; | |
799 | newsect->flags |= SEC_LOAD; | |
800 | if (hdr->p_flags & PF_X) | |
801 | { | |
802 | /* FIXME: all we known is that it has execute PERMISSION, | |
803 | may be data. */ | |
804 | newsect->flags |= SEC_CODE; | |
805 | } | |
806 | } | |
807 | if (!(hdr->p_flags & PF_W)) | |
808 | { | |
809 | newsect->flags |= SEC_READONLY; | |
810 | } | |
811 | ||
812 | if (split) | |
813 | { | |
814 | sprintf (namebuf, "segment%db", index); | |
815 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
816 | if (!name) | |
817 | { | |
818 | bfd_set_error (bfd_error_no_memory); | |
819 | return false; | |
820 | } | |
821 | strcpy (name, namebuf); | |
822 | newsect = bfd_make_section (abfd, name); | |
823 | if (newsect == NULL) | |
824 | return false; | |
825 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; | |
ae115e51 | 826 | newsect->lma = hdr->p_paddr + hdr->p_filesz; |
ede4eed4 KR |
827 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; |
828 | if (hdr->p_type == PT_LOAD) | |
829 | { | |
830 | newsect->flags |= SEC_ALLOC; | |
831 | if (hdr->p_flags & PF_X) | |
832 | newsect->flags |= SEC_CODE; | |
833 | } | |
834 | if (!(hdr->p_flags & PF_W)) | |
835 | newsect->flags |= SEC_READONLY; | |
836 | } | |
837 | ||
838 | return true; | |
839 | } | |
840 | ||
841 | /* Set up an ELF internal section header for a section. */ | |
842 | ||
843 | /*ARGSUSED*/ | |
844 | static void | |
845 | elf_fake_sections (abfd, asect, failedptrarg) | |
846 | bfd *abfd; | |
847 | asection *asect; | |
848 | PTR failedptrarg; | |
849 | { | |
850 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
851 | boolean *failedptr = (boolean *) failedptrarg; | |
852 | Elf_Internal_Shdr *this_hdr; | |
853 | ||
854 | if (*failedptr) | |
855 | { | |
856 | /* We already failed; just get out of the bfd_map_over_sections | |
857 | loop. */ | |
858 | return; | |
859 | } | |
860 | ||
861 | this_hdr = &elf_section_data (asect)->this_hdr; | |
862 | ||
863 | this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd), | |
864 | asect->name, | |
865 | true, false); | |
866 | if (this_hdr->sh_name == (unsigned long) -1) | |
867 | { | |
868 | *failedptr = true; | |
869 | return; | |
870 | } | |
871 | ||
872 | this_hdr->sh_flags = 0; | |
ae115e51 ILT |
873 | |
874 | /* FIXME: This should really use vma, rather than lma. However, | |
875 | that would mean that the lma information was lost, which would | |
876 | mean that the AT keyword in linker scripts would not work. | |
877 | Fortunately, native scripts do not use the AT keyword, so we can | |
878 | get away with using lma here. The right way to handle this is to | |
879 | 1) read the program headers as well as the section headers, and | |
880 | set the lma fields of the BFD sections based on the p_paddr | |
881 | fields of the program headers, and 2) set the p_paddr fields of | |
882 | the program headers based on the section lma fields when writing | |
883 | them out. */ | |
ede4eed4 | 884 | if ((asect->flags & SEC_ALLOC) != 0) |
ae115e51 | 885 | this_hdr->sh_addr = asect->lma; |
ede4eed4 KR |
886 | else |
887 | this_hdr->sh_addr = 0; | |
ae115e51 | 888 | |
ede4eed4 KR |
889 | this_hdr->sh_offset = 0; |
890 | this_hdr->sh_size = asect->_raw_size; | |
891 | this_hdr->sh_link = 0; | |
892 | this_hdr->sh_info = 0; | |
893 | this_hdr->sh_addralign = 1 << asect->alignment_power; | |
894 | this_hdr->sh_entsize = 0; | |
895 | ||
896 | this_hdr->bfd_section = asect; | |
897 | this_hdr->contents = NULL; | |
898 | ||
899 | /* FIXME: This should not be based on section names. */ | |
900 | if (strcmp (asect->name, ".dynstr") == 0) | |
901 | this_hdr->sh_type = SHT_STRTAB; | |
902 | else if (strcmp (asect->name, ".hash") == 0) | |
903 | { | |
904 | this_hdr->sh_type = SHT_HASH; | |
905 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
906 | } | |
907 | else if (strcmp (asect->name, ".dynsym") == 0) | |
908 | { | |
909 | this_hdr->sh_type = SHT_DYNSYM; | |
910 | this_hdr->sh_entsize = bed->s->sizeof_sym; | |
911 | } | |
912 | else if (strcmp (asect->name, ".dynamic") == 0) | |
913 | { | |
914 | this_hdr->sh_type = SHT_DYNAMIC; | |
915 | this_hdr->sh_entsize = bed->s->sizeof_dyn; | |
916 | } | |
917 | else if (strncmp (asect->name, ".rela", 5) == 0 | |
918 | && get_elf_backend_data (abfd)->use_rela_p) | |
919 | { | |
920 | this_hdr->sh_type = SHT_RELA; | |
921 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
922 | } | |
923 | else if (strncmp (asect->name, ".rel", 4) == 0 | |
924 | && ! get_elf_backend_data (abfd)->use_rela_p) | |
925 | { | |
926 | this_hdr->sh_type = SHT_REL; | |
927 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
928 | } | |
929 | else if (strcmp (asect->name, ".note") == 0) | |
930 | this_hdr->sh_type = SHT_NOTE; | |
931 | else if (strncmp (asect->name, ".stab", 5) == 0 | |
932 | && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) | |
933 | this_hdr->sh_type = SHT_STRTAB; | |
934 | else if ((asect->flags & SEC_ALLOC) != 0 | |
935 | && (asect->flags & SEC_LOAD) != 0) | |
936 | this_hdr->sh_type = SHT_PROGBITS; | |
937 | else if ((asect->flags & SEC_ALLOC) != 0 | |
938 | && ((asect->flags & SEC_LOAD) == 0)) | |
939 | { | |
940 | BFD_ASSERT (strcmp (asect->name, ".bss") == 0 | |
941 | || strcmp (asect->name, ".sbss") == 0 | |
942 | || strcmp (asect->name, ".scommon") == 0 | |
943 | || strcmp (asect->name, "COMMON") == 0); | |
944 | this_hdr->sh_type = SHT_NOBITS; | |
945 | } | |
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 | |
1233 | && syms[idx]->value == 0) | |
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 | ||
1346 | /* Compute the file positions we are going to put the sections at, and | |
1347 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
1348 | is not NULL, this is being called by the ELF backend linker. */ | |
1349 | ||
1350 | boolean | |
1351 | _bfd_elf_compute_section_file_positions (abfd, link_info) | |
1352 | bfd *abfd; | |
1353 | struct bfd_link_info *link_info; | |
1354 | { | |
1355 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1356 | boolean failed; | |
1357 | struct bfd_strtab_hash *strtab; | |
1358 | Elf_Internal_Shdr *shstrtab_hdr; | |
1359 | ||
1360 | if (abfd->output_has_begun) | |
1361 | return true; | |
1362 | ||
1363 | /* Do any elf backend specific processing first. */ | |
1364 | if (bed->elf_backend_begin_write_processing) | |
1365 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
1366 | ||
1367 | if (! prep_headers (abfd)) | |
1368 | return false; | |
1369 | ||
1370 | failed = false; | |
1371 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); | |
1372 | if (failed) | |
1373 | return false; | |
1374 | ||
1375 | if (!assign_section_numbers (abfd)) | |
1376 | return false; | |
1377 | ||
1378 | /* The backend linker builds symbol table information itself. */ | |
1379 | if (link_info == NULL) | |
1380 | { | |
1381 | if (! swap_out_syms (abfd, &strtab)) | |
1382 | return false; | |
1383 | } | |
1384 | ||
1385 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; | |
1386 | /* sh_name was set in prep_headers. */ | |
1387 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
1388 | shstrtab_hdr->sh_flags = 0; | |
1389 | shstrtab_hdr->sh_addr = 0; | |
1390 | shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
1391 | shstrtab_hdr->sh_entsize = 0; | |
1392 | shstrtab_hdr->sh_link = 0; | |
1393 | shstrtab_hdr->sh_info = 0; | |
1394 | /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs. */ | |
1395 | shstrtab_hdr->sh_addralign = 1; | |
1396 | ||
1397 | if (!assign_file_positions_except_relocs (abfd, | |
1398 | link_info == NULL ? true : false)) | |
1399 | return false; | |
1400 | ||
1401 | if (link_info == NULL) | |
1402 | { | |
1403 | /* Now that we know where the .strtab section goes, write it | |
1404 | out. */ | |
1405 | if ((bfd_seek (abfd, elf_tdata (abfd)->strtab_hdr.sh_offset, SEEK_SET) | |
1406 | != 0) | |
1407 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
1408 | return false; | |
1409 | _bfd_stringtab_free (strtab); | |
1410 | } | |
1411 | ||
1412 | abfd->output_has_begun = true; | |
1413 | ||
1414 | return true; | |
1415 | } | |
1416 | ||
1417 | ||
1418 | /* Align to the maximum file alignment that could be required for any | |
1419 | ELF data structure. */ | |
1420 | ||
1421 | static INLINE file_ptr align_file_position PARAMS ((file_ptr, int)); | |
1422 | static INLINE file_ptr | |
1423 | align_file_position (off, align) | |
1424 | file_ptr off; | |
1425 | int align; | |
1426 | { | |
1427 | return (off + align - 1) & ~(align - 1); | |
1428 | } | |
1429 | ||
1430 | /* Assign a file position to a section, optionally aligning to the | |
1431 | required section alignment. */ | |
1432 | ||
1433 | INLINE file_ptr | |
1434 | _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align) | |
1435 | Elf_Internal_Shdr *i_shdrp; | |
1436 | file_ptr offset; | |
1437 | boolean align; | |
1438 | { | |
1439 | if (align) | |
1440 | { | |
1441 | unsigned int al; | |
1442 | ||
1443 | al = i_shdrp->sh_addralign; | |
1444 | if (al > 1) | |
1445 | offset = BFD_ALIGN (offset, al); | |
1446 | } | |
1447 | i_shdrp->sh_offset = offset; | |
1448 | if (i_shdrp->bfd_section != NULL) | |
1449 | i_shdrp->bfd_section->filepos = offset; | |
1450 | if (i_shdrp->sh_type != SHT_NOBITS) | |
1451 | offset += i_shdrp->sh_size; | |
1452 | return offset; | |
1453 | } | |
1454 | ||
1455 | /* Get the size of the program header. | |
1456 | ||
1457 | SORTED_HDRS, if non-NULL, is an array of COUNT pointers to headers sorted | |
1458 | by VMA. Non-allocated sections (!SHF_ALLOC) must appear last. All | |
1459 | section VMAs and sizes are known so we can compute the correct value. | |
1460 | (??? This may not be perfectly true. What cases do we miss?) | |
1461 | ||
1462 | If SORTED_HDRS is NULL we assume there are two segments: text and data | |
1463 | (exclusive of .interp and .dynamic). | |
1464 | ||
1465 | If this is called by the linker before any of the section VMA's are set, it | |
1466 | can't calculate the correct value for a strange memory layout. This only | |
1467 | happens when SIZEOF_HEADERS is used in a linker script. In this case, | |
1468 | SORTED_HDRS is NULL and we assume the normal scenario of one text and one | |
1469 | data segment (exclusive of .interp and .dynamic). | |
1470 | ||
1471 | ??? User written scripts must either not use SIZEOF_HEADERS, or assume there | |
1472 | will be two segments. */ | |
1473 | ||
1474 | static bfd_size_type | |
1475 | get_program_header_size (abfd, sorted_hdrs, count, maxpagesize) | |
1476 | bfd *abfd; | |
1477 | Elf_Internal_Shdr **sorted_hdrs; | |
1478 | unsigned int count; | |
1479 | bfd_vma maxpagesize; | |
1480 | { | |
1481 | size_t segs; | |
1482 | asection *s; | |
ae115e51 | 1483 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
ede4eed4 KR |
1484 | |
1485 | /* We can't return a different result each time we're called. */ | |
1486 | if (elf_tdata (abfd)->program_header_size != 0) | |
1487 | return elf_tdata (abfd)->program_header_size; | |
1488 | ||
1489 | if (sorted_hdrs != NULL) | |
1490 | { | |
1491 | unsigned int i; | |
1492 | unsigned int last_type; | |
1493 | Elf_Internal_Shdr **hdrpp; | |
1494 | /* What we think the current segment's offset is. */ | |
1495 | bfd_vma p_offset; | |
1496 | /* What we think the current segment's address is. */ | |
1497 | bfd_vma p_vaddr; | |
1498 | /* How big we think the current segment is. */ | |
1499 | bfd_vma p_memsz; | |
1500 | /* What we think the current file offset is. */ | |
1501 | bfd_vma file_offset; | |
1502 | bfd_vma next_offset; | |
1503 | ||
1504 | /* Scan the headers and compute the number of segments required. This | |
1505 | code is intentionally similar to the code in map_program_segments. | |
1506 | ||
1507 | The `sh_offset' field isn't valid at this point, so we keep our own | |
1508 | running total in `file_offset'. | |
1509 | ||
1510 | This works because section VMAs are already known. */ | |
1511 | ||
1512 | segs = 1; | |
1513 | /* Make sure the first section goes in the first segment. */ | |
1514 | file_offset = p_offset = sorted_hdrs[0]->sh_addr % maxpagesize; | |
1515 | p_vaddr = sorted_hdrs[0]->sh_addr; | |
1516 | p_memsz = 0; | |
1517 | last_type = SHT_PROGBITS; | |
1518 | ||
1519 | for (i = 0, hdrpp = sorted_hdrs; i < count; i++, hdrpp++) | |
1520 | { | |
1521 | Elf_Internal_Shdr *hdr; | |
1522 | ||
1523 | hdr = *hdrpp; | |
1524 | ||
1525 | /* Ignore any section which will not be part of the process | |
1526 | image. */ | |
1527 | if ((hdr->sh_flags & SHF_ALLOC) == 0) | |
1528 | continue; | |
1529 | ||
1530 | /* Keep track of where this and the next sections go. | |
1531 | The section VMA must equal the file position modulo | |
1532 | the page size. */ | |
1533 | file_offset += (hdr->sh_addr - file_offset) % maxpagesize; | |
1534 | next_offset = file_offset; | |
1535 | if (hdr->sh_type != SHT_NOBITS) | |
1536 | next_offset = file_offset + hdr->sh_size; | |
1537 | ||
1538 | /* If this section fits in the segment we are constructing, add | |
1539 | it in. */ | |
1540 | if ((file_offset - (p_offset + p_memsz) | |
1541 | == hdr->sh_addr - (p_vaddr + p_memsz)) | |
1542 | && (last_type != SHT_NOBITS || hdr->sh_type == SHT_NOBITS)) | |
1543 | { | |
1544 | bfd_size_type adjust; | |
1545 | ||
1546 | adjust = hdr->sh_addr - (p_vaddr + p_memsz); | |
1547 | p_memsz += hdr->sh_size + adjust; | |
1548 | file_offset = next_offset; | |
1549 | last_type = hdr->sh_type; | |
1550 | continue; | |
1551 | } | |
1552 | ||
1553 | /* The section won't fit, start a new segment. */ | |
1554 | ++segs; | |
1555 | ||
1556 | /* Initialize the segment. */ | |
1557 | p_vaddr = hdr->sh_addr; | |
1558 | p_memsz = hdr->sh_size; | |
1559 | p_offset = file_offset; | |
1560 | file_offset = next_offset; | |
1561 | ||
1562 | last_type = hdr->sh_type; | |
1563 | } | |
1564 | } | |
1565 | else | |
1566 | { | |
1567 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
1568 | and one for data. */ | |
1569 | segs = 2; | |
1570 | } | |
1571 | ||
1572 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1573 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
1574 | { | |
1575 | /* If we have a loadable interpreter section, we need a | |
1576 | PT_INTERP segment. In this case, assume we also need a | |
1577 | PT_PHDR segment, although that may not be true for all | |
1578 | targets. */ | |
1579 | segs += 2; | |
1580 | } | |
1581 | ||
1582 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
1583 | { | |
1584 | /* We need a PT_DYNAMIC segment. */ | |
1585 | ++segs; | |
1586 | } | |
1587 | ||
ae115e51 ILT |
1588 | /* Let the backend count up any program headers it might need. */ |
1589 | if (bed->elf_backend_create_program_headers) | |
1590 | segs = ((*bed->elf_backend_create_program_headers) | |
1591 | (abfd, (Elf_Internal_Phdr *) NULL, segs)); | |
1592 | ||
1593 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; | |
ede4eed4 KR |
1594 | return elf_tdata (abfd)->program_header_size; |
1595 | } | |
1596 | ||
1597 | /* Create the program header. OFF is the file offset where the | |
1598 | program header should be written. FIRST is the first loadable ELF | |
1599 | section. SORTED_HDRS is the ELF sections sorted by section | |
1600 | address. PHDR_SIZE is the size of the program header as returned | |
1601 | by get_program_header_size. */ | |
1602 | ||
1603 | static file_ptr | |
1604 | map_program_segments (abfd, off, first, sorted_hdrs, phdr_size) | |
1605 | bfd *abfd; | |
1606 | file_ptr off; | |
1607 | Elf_Internal_Shdr *first; | |
1608 | Elf_Internal_Shdr **sorted_hdrs; | |
1609 | bfd_size_type phdr_size; | |
1610 | { | |
1611 | Elf_Internal_Phdr phdrs[10]; | |
1612 | unsigned int phdr_count; | |
1613 | Elf_Internal_Phdr *phdr; | |
1614 | int phdr_size_adjust; | |
1615 | unsigned int i; | |
1616 | Elf_Internal_Shdr **hdrpp; | |
1617 | asection *sinterp, *sdyn; | |
1618 | unsigned int last_type; | |
1619 | Elf_Internal_Ehdr *i_ehdrp; | |
1620 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1621 | ||
1622 | BFD_ASSERT ((abfd->flags & (EXEC_P | DYNAMIC)) != 0); | |
1623 | BFD_ASSERT (phdr_size / sizeof (Elf_Internal_Phdr) | |
1624 | <= sizeof phdrs / sizeof (phdrs[0])); | |
1625 | ||
1626 | phdr_count = 0; | |
1627 | phdr = phdrs; | |
1628 | ||
ae115e51 ILT |
1629 | if (bed->want_hdr_in_seg) |
1630 | phdr_size_adjust = first->sh_offset - phdr_size; | |
1631 | else | |
1632 | phdr_size_adjust = 0; | |
ede4eed4 KR |
1633 | |
1634 | /* If we have a loadable .interp section, we must create a PT_INTERP | |
1635 | segment which must precede all PT_LOAD segments. We assume that | |
1636 | we must also create a PT_PHDR segment, although that may not be | |
1637 | true for all targets. */ | |
1638 | sinterp = bfd_get_section_by_name (abfd, ".interp"); | |
1639 | if (sinterp != NULL && (sinterp->flags & SEC_LOAD) != 0) | |
1640 | { | |
1641 | BFD_ASSERT (first != NULL); | |
1642 | ||
1643 | phdr->p_type = PT_PHDR; | |
1644 | ||
1645 | phdr->p_offset = off; | |
1646 | ||
1647 | /* Account for any adjustment made because of the alignment of | |
1648 | the first loadable section. */ | |
1649 | phdr_size_adjust = (first->sh_offset - phdr_size) - off; | |
1650 | BFD_ASSERT (phdr_size_adjust >= 0 && phdr_size_adjust < 128); | |
1651 | ||
1652 | /* The program header precedes all loadable sections. This lets | |
1653 | us compute its loadable address. This depends on the linker | |
1654 | script. */ | |
1655 | phdr->p_vaddr = first->sh_addr - (phdr_size + phdr_size_adjust); | |
1656 | ||
1657 | phdr->p_paddr = 0; | |
1658 | phdr->p_filesz = phdr_size; | |
1659 | phdr->p_memsz = phdr_size; | |
1660 | ||
1661 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
1662 | phdr->p_flags = PF_R | PF_X; | |
1663 | ||
1664 | phdr->p_align = bed->s->file_align; | |
1665 | BFD_ASSERT ((phdr->p_vaddr - phdr->p_offset) % bed->s->file_align == 0); | |
1666 | ||
1667 | /* Include the ELF header in the first loadable segment. */ | |
1668 | phdr_size_adjust += off; | |
1669 | ||
1670 | ++phdr_count; | |
1671 | ++phdr; | |
1672 | ||
1673 | phdr->p_type = PT_INTERP; | |
1674 | phdr->p_offset = sinterp->filepos; | |
1675 | phdr->p_vaddr = sinterp->vma; | |
1676 | phdr->p_paddr = 0; | |
1677 | phdr->p_filesz = sinterp->_raw_size; | |
1678 | phdr->p_memsz = sinterp->_raw_size; | |
1679 | phdr->p_flags = PF_R; | |
1680 | phdr->p_align = 1 << bfd_get_section_alignment (abfd, sinterp); | |
1681 | ||
1682 | ++phdr_count; | |
1683 | ++phdr; | |
1684 | } | |
1685 | ||
1686 | /* Look through the sections to see how they will be divided into | |
1687 | program segments. The sections must be arranged in order by | |
1688 | sh_addr for this to work correctly. */ | |
1689 | phdr->p_type = PT_NULL; | |
1690 | last_type = SHT_PROGBITS; | |
1691 | for (i = 1, hdrpp = sorted_hdrs; | |
1692 | i < elf_elfheader (abfd)->e_shnum; | |
1693 | i++, hdrpp++) | |
1694 | { | |
1695 | Elf_Internal_Shdr *hdr; | |
1696 | ||
1697 | hdr = *hdrpp; | |
1698 | ||
1699 | /* Ignore any section which will not be part of the process | |
1700 | image. */ | |
1701 | if ((hdr->sh_flags & SHF_ALLOC) == 0) | |
1702 | continue; | |
1703 | ||
1704 | /* If this section fits in the segment we are constructing, add | |
1705 | it in. */ | |
1706 | if (phdr->p_type != PT_NULL | |
1707 | && (hdr->sh_offset - (phdr->p_offset + phdr->p_memsz) | |
1708 | == hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz)) | |
1709 | && (last_type != SHT_NOBITS || hdr->sh_type == SHT_NOBITS)) | |
1710 | { | |
1711 | bfd_size_type adjust; | |
1712 | ||
1713 | adjust = hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz); | |
1714 | phdr->p_memsz += hdr->sh_size + adjust; | |
1715 | if (hdr->sh_type != SHT_NOBITS) | |
1716 | phdr->p_filesz += hdr->sh_size + adjust; | |
1717 | if ((hdr->sh_flags & SHF_WRITE) != 0) | |
1718 | phdr->p_flags |= PF_W; | |
1719 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1720 | phdr->p_flags |= PF_X; | |
1721 | last_type = hdr->sh_type; | |
1722 | continue; | |
1723 | } | |
1724 | ||
1725 | /* The section won't fit, start a new segment. If we're already in one, | |
1726 | move to the next one. */ | |
1727 | if (phdr->p_type != PT_NULL) | |
1728 | { | |
1729 | ++phdr; | |
1730 | ++phdr_count; | |
1731 | } | |
1732 | ||
1733 | /* Initialize the segment. */ | |
1734 | phdr->p_type = PT_LOAD; | |
1735 | phdr->p_offset = hdr->sh_offset; | |
1736 | phdr->p_vaddr = hdr->sh_addr; | |
1737 | phdr->p_paddr = 0; | |
1738 | if (hdr->sh_type == SHT_NOBITS) | |
1739 | phdr->p_filesz = 0; | |
1740 | else | |
1741 | phdr->p_filesz = hdr->sh_size; | |
1742 | phdr->p_memsz = hdr->sh_size; | |
1743 | phdr->p_flags = PF_R; | |
1744 | if ((hdr->sh_flags & SHF_WRITE) != 0) | |
1745 | phdr->p_flags |= PF_W; | |
1746 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1747 | phdr->p_flags |= PF_X; | |
1748 | phdr->p_align = bed->maxpagesize; | |
1749 | ||
1750 | if (hdr == first | |
ae115e51 ILT |
1751 | && (bed->want_hdr_in_seg |
1752 | || (sinterp != NULL | |
1753 | && (sinterp->flags & SEC_LOAD) != 0))) | |
ede4eed4 KR |
1754 | { |
1755 | phdr->p_offset -= phdr_size + phdr_size_adjust; | |
1756 | phdr->p_vaddr -= phdr_size + phdr_size_adjust; | |
1757 | phdr->p_filesz += phdr_size + phdr_size_adjust; | |
1758 | phdr->p_memsz += phdr_size + phdr_size_adjust; | |
1759 | } | |
1760 | ||
1761 | last_type = hdr->sh_type; | |
1762 | } | |
1763 | ||
1764 | if (phdr->p_type != PT_NULL) | |
1765 | { | |
1766 | ++phdr; | |
1767 | ++phdr_count; | |
1768 | } | |
1769 | ||
1770 | /* If we have a .dynamic section, create a PT_DYNAMIC segment. */ | |
1771 | sdyn = bfd_get_section_by_name (abfd, ".dynamic"); | |
1772 | if (sdyn != NULL && (sdyn->flags & SEC_LOAD) != 0) | |
1773 | { | |
1774 | phdr->p_type = PT_DYNAMIC; | |
1775 | phdr->p_offset = sdyn->filepos; | |
1776 | phdr->p_vaddr = sdyn->vma; | |
1777 | phdr->p_paddr = 0; | |
1778 | phdr->p_filesz = sdyn->_raw_size; | |
1779 | phdr->p_memsz = sdyn->_raw_size; | |
1780 | phdr->p_flags = PF_R; | |
1781 | if ((sdyn->flags & SEC_READONLY) == 0) | |
1782 | phdr->p_flags |= PF_W; | |
1783 | if ((sdyn->flags & SEC_CODE) != 0) | |
1784 | phdr->p_flags |= PF_X; | |
1785 | phdr->p_align = 1 << bfd_get_section_alignment (abfd, sdyn); | |
1786 | ||
1787 | ++phdr; | |
1788 | ++phdr_count; | |
1789 | } | |
1790 | ||
ae115e51 ILT |
1791 | /* Let the backend create additional program headers. */ |
1792 | if (bed->elf_backend_create_program_headers) | |
1793 | phdr_count = (*bed->elf_backend_create_program_headers) (abfd, | |
1794 | phdrs, | |
1795 | phdr_count); | |
1796 | ||
ede4eed4 KR |
1797 | /* Make sure the return value from get_program_header_size matches |
1798 | what we computed here. Actually, it's OK if we allocated too | |
1799 | much space in the program header. */ | |
1800 | if (phdr_count > phdr_size / bed->s->sizeof_phdr) | |
1801 | { | |
1802 | ((*_bfd_error_handler) | |
1803 | ("%s: Not enough room for program headers (allocated %lu, need %u)", | |
1804 | bfd_get_filename (abfd), | |
1805 | (unsigned long) (phdr_size / bed->s->sizeof_phdr), | |
1806 | phdr_count)); | |
1807 | bfd_set_error (bfd_error_bad_value); | |
1808 | return (file_ptr) -1; | |
1809 | } | |
1810 | ||
1811 | /* Set up program header information. */ | |
1812 | i_ehdrp = elf_elfheader (abfd); | |
1813 | i_ehdrp->e_phentsize = bed->s->sizeof_phdr; | |
1814 | i_ehdrp->e_phoff = off; | |
1815 | i_ehdrp->e_phnum = phdr_count; | |
1816 | ||
1817 | /* Save the program headers away. I don't think anybody uses this | |
1818 | information right now. */ | |
1819 | elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *) | |
1820 | bfd_alloc (abfd, | |
1821 | (phdr_count | |
1822 | * sizeof (Elf_Internal_Phdr)))); | |
1823 | if (elf_tdata (abfd)->phdr == NULL && phdr_count != 0) | |
1824 | { | |
1825 | bfd_set_error (bfd_error_no_memory); | |
1826 | return (file_ptr) -1; | |
1827 | } | |
1828 | memcpy (elf_tdata (abfd)->phdr, phdrs, | |
1829 | phdr_count * sizeof (Elf_Internal_Phdr)); | |
1830 | ||
1831 | /* Write out the program headers. */ | |
1832 | if (bfd_seek (abfd, off, SEEK_SET) != 0) | |
1833 | return (file_ptr) -1; | |
1834 | ||
1835 | if (bed->s->write_out_phdrs (abfd, phdrs, phdr_count) != 0) | |
1836 | return (file_ptr) -1; | |
1837 | ||
1838 | return off + phdr_count * bed->s->sizeof_phdr; | |
1839 | } | |
1840 | ||
1841 | /* Work out the file positions of all the sections. This is called by | |
1842 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
1843 | VMAs must be known before this is called. | |
1844 | ||
1845 | We do not consider reloc sections at this point, unless they form | |
1846 | part of the loadable image. Reloc sections are assigned file | |
1847 | positions in assign_file_positions_for_relocs, which is called by | |
1848 | write_object_contents and final_link. | |
1849 | ||
1850 | If DOSYMS is false, we do not assign file positions for the symbol | |
1851 | table or the string table. */ | |
1852 | ||
1853 | static int elf_sort_hdrs PARAMS ((const PTR, const PTR)); | |
1854 | ||
1855 | static boolean | |
1856 | assign_file_positions_except_relocs (abfd, dosyms) | |
1857 | bfd *abfd; | |
1858 | boolean dosyms; | |
1859 | { | |
1860 | struct elf_obj_tdata * const tdata = elf_tdata (abfd); | |
1861 | Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); | |
1862 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
1863 | file_ptr off; | |
1864 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1865 | ||
1866 | /* Start after the ELF header. */ | |
1867 | off = i_ehdrp->e_ehsize; | |
1868 | ||
1869 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) | |
1870 | { | |
1871 | Elf_Internal_Shdr **hdrpp; | |
1872 | unsigned int i; | |
1873 | ||
1874 | /* We are not creating an executable, which means that we are | |
1875 | not creating a program header, and that the actual order of | |
1876 | the sections in the file is unimportant. */ | |
1877 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
1878 | { | |
1879 | Elf_Internal_Shdr *hdr; | |
1880 | ||
1881 | hdr = *hdrpp; | |
1882 | if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
1883 | { | |
1884 | hdr->sh_offset = -1; | |
1885 | continue; | |
1886 | } | |
1887 | if (! dosyms | |
1888 | && (i == tdata->symtab_section | |
1889 | || i == tdata->strtab_section)) | |
1890 | { | |
1891 | hdr->sh_offset = -1; | |
1892 | continue; | |
1893 | } | |
1894 | ||
1895 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
1896 | } | |
1897 | } | |
1898 | else | |
1899 | { | |
1900 | file_ptr phdr_off; | |
1901 | bfd_size_type phdr_size; | |
1902 | bfd_vma maxpagesize; | |
1903 | size_t hdrppsize; | |
1904 | Elf_Internal_Shdr **sorted_hdrs; | |
1905 | Elf_Internal_Shdr **hdrpp; | |
1906 | unsigned int i; | |
1907 | Elf_Internal_Shdr *first; | |
1908 | file_ptr phdr_map; | |
1909 | ||
1910 | /* We are creating an executable. */ | |
1911 | ||
1912 | maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
1913 | if (maxpagesize == 0) | |
1914 | maxpagesize = 1; | |
1915 | ||
1916 | /* We must sort the sections. The GNU linker will always create | |
1917 | the sections in an appropriate order, but the Irix 5 linker | |
1918 | will not. We don't include the dummy first section in the | |
1919 | sort. We sort sections which are not SHF_ALLOC to the end. */ | |
1920 | hdrppsize = (i_ehdrp->e_shnum - 1) * sizeof (Elf_Internal_Shdr *); | |
1921 | sorted_hdrs = (Elf_Internal_Shdr **) malloc (hdrppsize); | |
1922 | if (sorted_hdrs == NULL) | |
1923 | { | |
1924 | bfd_set_error (bfd_error_no_memory); | |
1925 | return false; | |
1926 | } | |
1927 | ||
1928 | memcpy (sorted_hdrs, i_shdrpp + 1, hdrppsize); | |
ae115e51 ILT |
1929 | qsort (sorted_hdrs, (size_t) i_ehdrp->e_shnum - 1, |
1930 | sizeof (Elf_Internal_Shdr *), elf_sort_hdrs); | |
ede4eed4 KR |
1931 | |
1932 | /* We can't actually create the program header until we have set the | |
1933 | file positions for the sections, and we can't do that until we know | |
1934 | how big the header is going to be. */ | |
1935 | off = align_file_position (off, bed->s->file_align); | |
1936 | phdr_size = get_program_header_size (abfd, | |
1937 | sorted_hdrs, i_ehdrp->e_shnum - 1, | |
1938 | maxpagesize); | |
ae115e51 | 1939 | if (phdr_size == (bfd_size_type) -1) |
ede4eed4 KR |
1940 | return false; |
1941 | ||
1942 | /* Compute the file offsets of each section. */ | |
1943 | phdr_off = off; | |
1944 | off += phdr_size; | |
1945 | first = NULL; | |
1946 | for (i = 1, hdrpp = sorted_hdrs; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
1947 | { | |
1948 | Elf_Internal_Shdr *hdr; | |
1949 | ||
1950 | hdr = *hdrpp; | |
1951 | if ((hdr->sh_flags & SHF_ALLOC) == 0) | |
1952 | { | |
1953 | if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
1954 | { | |
1955 | hdr->sh_offset = -1; | |
1956 | continue; | |
1957 | } | |
1958 | if (! dosyms | |
1959 | && (hdr == i_shdrpp[tdata->symtab_section] | |
1960 | || hdr == i_shdrpp[tdata->strtab_section])) | |
1961 | { | |
1962 | hdr->sh_offset = -1; | |
1963 | continue; | |
1964 | } | |
ae115e51 ILT |
1965 | off = _bfd_elf_assign_file_position_for_section (hdr, off, |
1966 | true); | |
ede4eed4 KR |
1967 | } |
1968 | else | |
1969 | { | |
1970 | if (first == NULL) | |
1971 | first = hdr; | |
1972 | ||
1973 | /* The section VMA must equal the file position modulo | |
1974 | the page size. This is required by the program | |
1975 | header. */ | |
1976 | off += (hdr->sh_addr - off) % maxpagesize; | |
ae115e51 ILT |
1977 | off = _bfd_elf_assign_file_position_for_section (hdr, off, |
1978 | false); | |
ede4eed4 | 1979 | } |
ede4eed4 KR |
1980 | } |
1981 | ||
1982 | /* Create the program header. */ | |
1983 | phdr_map = map_program_segments (abfd, phdr_off, first, sorted_hdrs, | |
1984 | phdr_size); | |
1985 | if (phdr_map == (file_ptr) -1) | |
1986 | return false; | |
ae115e51 ILT |
1987 | BFD_ASSERT ((bfd_size_type) phdr_map |
1988 | <= (bfd_size_type) phdr_off + phdr_size); | |
ede4eed4 KR |
1989 | |
1990 | free (sorted_hdrs); | |
1991 | } | |
1992 | ||
1993 | /* Place the section headers. */ | |
1994 | off = align_file_position (off, bed->s->file_align); | |
1995 | i_ehdrp->e_shoff = off; | |
1996 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
1997 | ||
1998 | elf_tdata (abfd)->next_file_pos = off; | |
1999 | ||
2000 | return true; | |
2001 | } | |
2002 | ||
2003 | /* Sort the ELF headers by VMA. We sort headers which are not | |
2004 | SHF_ALLOC to the end. */ | |
2005 | static int | |
2006 | elf_sort_hdrs (arg1, arg2) | |
2007 | const PTR arg1; | |
2008 | const PTR arg2; | |
2009 | { | |
2010 | int ret; | |
2011 | const Elf_Internal_Shdr *hdr1 = *(const Elf_Internal_Shdr **) arg1; | |
2012 | const Elf_Internal_Shdr *hdr2 = *(const Elf_Internal_Shdr **) arg2; | |
2013 | ||
2014 | #define TOEND(x) (((x)->sh_flags & SHF_ALLOC)==0) | |
2015 | ||
2016 | if (TOEND (hdr1)) | |
2017 | if (TOEND (hdr2)) | |
2018 | return 0; | |
2019 | else | |
2020 | return 1; | |
2021 | ||
2022 | if (TOEND (hdr2)) | |
2023 | return -1; | |
2024 | ||
2025 | if (hdr1->sh_addr < hdr2->sh_addr) | |
2026 | return -1; | |
2027 | else if (hdr1->sh_addr > hdr2->sh_addr) | |
2028 | return 1; | |
2029 | ||
2030 | /* Put !SHT_NOBITS sections before SHT_NOBITS ones. | |
2031 | The main loop in map_program_segments requires this. */ | |
2032 | ||
2033 | ret = (hdr1->sh_type == SHT_NOBITS) - (hdr2->sh_type == SHT_NOBITS); | |
2034 | ||
2035 | if (ret != 0) | |
2036 | return ret; | |
2037 | if (hdr1->sh_size < hdr2->sh_size) | |
2038 | return -1; | |
2039 | if (hdr1->sh_size > hdr2->sh_size) | |
2040 | return 1; | |
2041 | return 0; | |
2042 | } | |
2043 | ||
2044 | static boolean | |
2045 | prep_headers (abfd) | |
2046 | bfd *abfd; | |
2047 | { | |
2048 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
2049 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
2050 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
2051 | int count; | |
2052 | struct bfd_strtab_hash *shstrtab; | |
2053 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2054 | ||
2055 | i_ehdrp = elf_elfheader (abfd); | |
2056 | i_shdrp = elf_elfsections (abfd); | |
2057 | ||
2058 | shstrtab = _bfd_elf_stringtab_init (); | |
2059 | if (shstrtab == NULL) | |
2060 | return false; | |
2061 | ||
2062 | elf_shstrtab (abfd) = shstrtab; | |
2063 | ||
2064 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
2065 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
2066 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
2067 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
2068 | ||
2069 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
2070 | i_ehdrp->e_ident[EI_DATA] = | |
2071 | abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB; | |
2072 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
2073 | ||
2074 | for (count = EI_PAD; count < EI_NIDENT; count++) | |
2075 | i_ehdrp->e_ident[count] = 0; | |
2076 | ||
2077 | if ((abfd->flags & DYNAMIC) != 0) | |
2078 | i_ehdrp->e_type = ET_DYN; | |
2079 | else if ((abfd->flags & EXEC_P) != 0) | |
2080 | i_ehdrp->e_type = ET_EXEC; | |
2081 | else | |
2082 | i_ehdrp->e_type = ET_REL; | |
2083 | ||
2084 | switch (bfd_get_arch (abfd)) | |
2085 | { | |
2086 | case bfd_arch_unknown: | |
2087 | i_ehdrp->e_machine = EM_NONE; | |
2088 | break; | |
2089 | case bfd_arch_sparc: | |
2090 | if (bed->s->arch_size == 64) | |
2091 | i_ehdrp->e_machine = EM_SPARC64; | |
2092 | else | |
2093 | i_ehdrp->e_machine = EM_SPARC; | |
2094 | break; | |
2095 | case bfd_arch_i386: | |
2096 | i_ehdrp->e_machine = EM_386; | |
2097 | break; | |
2098 | case bfd_arch_m68k: | |
2099 | i_ehdrp->e_machine = EM_68K; | |
2100 | break; | |
2101 | case bfd_arch_m88k: | |
2102 | i_ehdrp->e_machine = EM_88K; | |
2103 | break; | |
2104 | case bfd_arch_i860: | |
2105 | i_ehdrp->e_machine = EM_860; | |
2106 | break; | |
2107 | case bfd_arch_mips: /* MIPS Rxxxx */ | |
2108 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
2109 | break; | |
2110 | case bfd_arch_hppa: | |
2111 | i_ehdrp->e_machine = EM_PARISC; | |
2112 | break; | |
2113 | case bfd_arch_powerpc: | |
2114 | i_ehdrp->e_machine = EM_PPC; | |
2115 | break; | |
2116 | /* start-sanitize-arc */ | |
2117 | case bfd_arch_arc: | |
2118 | i_ehdrp->e_machine = EM_CYGNUS_ARC; | |
2119 | break; | |
2120 | /* end-sanitize-arc */ | |
2121 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ | |
2122 | default: | |
2123 | i_ehdrp->e_machine = EM_NONE; | |
2124 | } | |
2125 | i_ehdrp->e_version = bed->s->ev_current; | |
2126 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
2127 | ||
2128 | /* no program header, for now. */ | |
2129 | i_ehdrp->e_phoff = 0; | |
2130 | i_ehdrp->e_phentsize = 0; | |
2131 | i_ehdrp->e_phnum = 0; | |
2132 | ||
2133 | /* each bfd section is section header entry */ | |
2134 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
2135 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
2136 | ||
2137 | /* if we're building an executable, we'll need a program header table */ | |
2138 | if (abfd->flags & EXEC_P) | |
2139 | { | |
2140 | /* it all happens later */ | |
2141 | #if 0 | |
2142 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
2143 | ||
2144 | /* elf_build_phdrs() returns a (NULL-terminated) array of | |
2145 | Elf_Internal_Phdrs */ | |
2146 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
2147 | i_ehdrp->e_phoff = outbase; | |
2148 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
2149 | #endif | |
2150 | } | |
2151 | else | |
2152 | { | |
2153 | i_ehdrp->e_phentsize = 0; | |
2154 | i_phdrp = 0; | |
2155 | i_ehdrp->e_phoff = 0; | |
2156 | } | |
2157 | ||
2158 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
2159 | (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false); | |
2160 | elf_tdata (abfd)->strtab_hdr.sh_name = | |
2161 | (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false); | |
2162 | elf_tdata (abfd)->shstrtab_hdr.sh_name = | |
2163 | (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false); | |
2164 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2165 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2166 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
2167 | return false; | |
2168 | ||
2169 | return true; | |
2170 | } | |
2171 | ||
2172 | /* Assign file positions for all the reloc sections which are not part | |
2173 | of the loadable file image. */ | |
2174 | ||
2175 | void | |
2176 | _bfd_elf_assign_file_positions_for_relocs (abfd) | |
2177 | bfd *abfd; | |
2178 | { | |
2179 | file_ptr off; | |
2180 | unsigned int i; | |
2181 | Elf_Internal_Shdr **shdrpp; | |
2182 | ||
2183 | off = elf_tdata (abfd)->next_file_pos; | |
2184 | ||
2185 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; | |
2186 | i < elf_elfheader (abfd)->e_shnum; | |
2187 | i++, shdrpp++) | |
2188 | { | |
2189 | Elf_Internal_Shdr *shdrp; | |
2190 | ||
2191 | shdrp = *shdrpp; | |
2192 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
2193 | && shdrp->sh_offset == -1) | |
2194 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); | |
2195 | } | |
2196 | ||
2197 | elf_tdata (abfd)->next_file_pos = off; | |
2198 | } | |
2199 | ||
2200 | boolean | |
2201 | _bfd_elf_write_object_contents (abfd) | |
2202 | bfd *abfd; | |
2203 | { | |
2204 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2205 | Elf_Internal_Ehdr *i_ehdrp; | |
2206 | Elf_Internal_Shdr **i_shdrp; | |
2207 | boolean failed; | |
2208 | unsigned int count; | |
2209 | ||
2210 | if (! abfd->output_has_begun | |
2211 | && ! _bfd_elf_compute_section_file_positions (abfd, | |
2212 | (struct bfd_link_info *) NULL)) | |
2213 | return false; | |
2214 | ||
2215 | i_shdrp = elf_elfsections (abfd); | |
2216 | i_ehdrp = elf_elfheader (abfd); | |
2217 | ||
2218 | failed = false; | |
2219 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); | |
2220 | if (failed) | |
2221 | return false; | |
2222 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
2223 | ||
2224 | /* After writing the headers, we need to write the sections too... */ | |
2225 | for (count = 1; count < i_ehdrp->e_shnum; count++) | |
2226 | { | |
2227 | if (bed->elf_backend_section_processing) | |
2228 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
2229 | if (i_shdrp[count]->contents) | |
2230 | { | |
2231 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 | |
2232 | || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, | |
2233 | 1, abfd) | |
2234 | != i_shdrp[count]->sh_size)) | |
2235 | return false; | |
2236 | } | |
2237 | } | |
2238 | ||
2239 | /* Write out the section header names. */ | |
2240 | if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
2241 | || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd))) | |
2242 | return false; | |
2243 | ||
2244 | if (bed->elf_backend_final_write_processing) | |
2245 | (*bed->elf_backend_final_write_processing) (abfd, | |
2246 | elf_tdata (abfd)->linker); | |
2247 | ||
2248 | return bed->s->write_shdrs_and_ehdr (abfd); | |
2249 | } | |
2250 | ||
2251 | /* given a section, search the header to find them... */ | |
2252 | int | |
2253 | _bfd_elf_section_from_bfd_section (abfd, asect) | |
2254 | bfd *abfd; | |
2255 | struct sec *asect; | |
2256 | { | |
2257 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2258 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); | |
2259 | int index; | |
2260 | Elf_Internal_Shdr *hdr; | |
2261 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
2262 | ||
2263 | for (index = 0; index < maxindex; index++) | |
2264 | { | |
2265 | hdr = i_shdrp[index]; | |
2266 | if (hdr->bfd_section == asect) | |
2267 | return index; | |
2268 | } | |
2269 | ||
2270 | if (bed->elf_backend_section_from_bfd_section) | |
2271 | { | |
2272 | for (index = 0; index < maxindex; index++) | |
2273 | { | |
2274 | int retval; | |
2275 | ||
2276 | hdr = i_shdrp[index]; | |
2277 | retval = index; | |
2278 | if ((*bed->elf_backend_section_from_bfd_section) | |
2279 | (abfd, hdr, asect, &retval)) | |
2280 | return retval; | |
2281 | } | |
2282 | } | |
2283 | ||
2284 | if (bfd_is_abs_section (asect)) | |
2285 | return SHN_ABS; | |
2286 | if (bfd_is_com_section (asect)) | |
2287 | return SHN_COMMON; | |
2288 | if (bfd_is_und_section (asect)) | |
2289 | return SHN_UNDEF; | |
2290 | ||
2291 | return -1; | |
2292 | } | |
2293 | ||
2294 | /* given a symbol, return the bfd index for that symbol. */ | |
2295 | int | |
2296 | _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) | |
2297 | bfd *abfd; | |
2298 | struct symbol_cache_entry **asym_ptr_ptr; | |
2299 | { | |
2300 | struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr; | |
2301 | int idx; | |
2302 | flagword flags = asym_ptr->flags; | |
2303 | ||
2304 | /* When gas creates relocations against local labels, it creates its | |
2305 | own symbol for the section, but does put the symbol into the | |
2306 | symbol chain, so udata is 0. When the linker is generating | |
2307 | relocatable output, this section symbol may be for one of the | |
2308 | input sections rather than the output section. */ | |
2309 | if (asym_ptr->udata.i == 0 | |
2310 | && (flags & BSF_SECTION_SYM) | |
2311 | && asym_ptr->section) | |
2312 | { | |
2313 | int indx; | |
2314 | ||
2315 | if (asym_ptr->section->output_section != NULL) | |
2316 | indx = asym_ptr->section->output_section->index; | |
2317 | else | |
2318 | indx = asym_ptr->section->index; | |
2319 | if (elf_section_syms (abfd)[indx]) | |
2320 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; | |
2321 | } | |
2322 | ||
2323 | idx = asym_ptr->udata.i; | |
2324 | BFD_ASSERT (idx != 0); | |
2325 | ||
2326 | #if DEBUG & 4 | |
2327 | { | |
2328 | fprintf (stderr, | |
2329 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", | |
2330 | (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags)); | |
2331 | fflush (stderr); | |
2332 | } | |
2333 | #endif | |
2334 | ||
2335 | return idx; | |
2336 | } | |
2337 | ||
2338 | static boolean | |
2339 | swap_out_syms (abfd, sttp) | |
2340 | bfd *abfd; | |
2341 | struct bfd_strtab_hash **sttp; | |
2342 | { | |
2343 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2344 | ||
2345 | if (!elf_map_symbols (abfd)) | |
2346 | return false; | |
2347 | ||
2348 | /* Dump out the symtabs. */ | |
2349 | { | |
2350 | int symcount = bfd_get_symcount (abfd); | |
2351 | asymbol **syms = bfd_get_outsymbols (abfd); | |
2352 | struct bfd_strtab_hash *stt; | |
2353 | Elf_Internal_Shdr *symtab_hdr; | |
2354 | Elf_Internal_Shdr *symstrtab_hdr; | |
2355 | char *outbound_syms; | |
2356 | int idx; | |
2357 | ||
2358 | stt = _bfd_elf_stringtab_init (); | |
2359 | if (stt == NULL) | |
2360 | return false; | |
2361 | ||
2362 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2363 | symtab_hdr->sh_type = SHT_SYMTAB; | |
2364 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
2365 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
2366 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
2367 | symtab_hdr->sh_addralign = bed->s->file_align; | |
2368 | ||
2369 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
2370 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
2371 | ||
2372 | outbound_syms = bfd_alloc (abfd, | |
2373 | (1 + symcount) * bed->s->sizeof_sym); | |
2374 | if (outbound_syms == NULL) | |
2375 | { | |
2376 | bfd_set_error (bfd_error_no_memory); | |
2377 | return false; | |
2378 | } | |
2379 | symtab_hdr->contents = (PTR) outbound_syms; | |
2380 | ||
2381 | /* now generate the data (for "contents") */ | |
2382 | { | |
2383 | /* Fill in zeroth symbol and swap it out. */ | |
2384 | Elf_Internal_Sym sym; | |
2385 | sym.st_name = 0; | |
2386 | sym.st_value = 0; | |
2387 | sym.st_size = 0; | |
2388 | sym.st_info = 0; | |
2389 | sym.st_other = 0; | |
2390 | sym.st_shndx = SHN_UNDEF; | |
cf9fb9f2 | 2391 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); |
ede4eed4 KR |
2392 | outbound_syms += bed->s->sizeof_sym; |
2393 | } | |
2394 | for (idx = 0; idx < symcount; idx++) | |
2395 | { | |
2396 | Elf_Internal_Sym sym; | |
2397 | bfd_vma value = syms[idx]->value; | |
2398 | elf_symbol_type *type_ptr; | |
2399 | flagword flags = syms[idx]->flags; | |
2400 | ||
2401 | if (flags & BSF_SECTION_SYM) | |
2402 | /* Section symbols have no names. */ | |
2403 | sym.st_name = 0; | |
2404 | else | |
2405 | { | |
2406 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
2407 | syms[idx]->name, | |
2408 | true, false); | |
2409 | if (sym.st_name == (unsigned long) -1) | |
2410 | return false; | |
2411 | } | |
2412 | ||
2413 | type_ptr = elf_symbol_from (abfd, syms[idx]); | |
2414 | ||
2415 | if (bfd_is_com_section (syms[idx]->section)) | |
2416 | { | |
2417 | /* ELF common symbols put the alignment into the `value' field, | |
2418 | and the size into the `size' field. This is backwards from | |
2419 | how BFD handles it, so reverse it here. */ | |
2420 | sym.st_size = value; | |
2421 | if (type_ptr == NULL | |
2422 | || type_ptr->internal_elf_sym.st_value == 0) | |
2423 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
2424 | else | |
2425 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
2426 | sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, | |
2427 | syms[idx]->section); | |
2428 | } | |
2429 | else | |
2430 | { | |
2431 | asection *sec = syms[idx]->section; | |
2432 | int shndx; | |
2433 | ||
2434 | if (sec->output_section) | |
2435 | { | |
2436 | value += sec->output_offset; | |
2437 | sec = sec->output_section; | |
2438 | } | |
2439 | value += sec->vma; | |
2440 | sym.st_value = value; | |
2441 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
2442 | sym.st_shndx = shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
2443 | if (shndx == -1) | |
2444 | { | |
2445 | asection *sec2; | |
2446 | /* Writing this would be a hell of a lot easier if we had | |
2447 | some decent documentation on bfd, and knew what to expect | |
2448 | of the library, and what to demand of applications. For | |
2449 | example, it appears that `objcopy' might not set the | |
2450 | section of a symbol to be a section that is actually in | |
2451 | the output file. */ | |
2452 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
2453 | BFD_ASSERT (sec2 != 0); | |
2454 | sym.st_shndx = shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
2455 | BFD_ASSERT (shndx != -1); | |
2456 | } | |
2457 | } | |
2458 | ||
2459 | if (bfd_is_com_section (syms[idx]->section)) | |
2460 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT); | |
2461 | else if (bfd_is_und_section (syms[idx]->section)) | |
2462 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
2463 | ? STB_WEAK | |
2464 | : STB_GLOBAL), | |
2465 | ((flags & BSF_FUNCTION) | |
2466 | ? STT_FUNC | |
2467 | : STT_NOTYPE)); | |
2468 | else if (flags & BSF_SECTION_SYM) | |
2469 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
2470 | else if (flags & BSF_FILE) | |
2471 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
2472 | else | |
2473 | { | |
2474 | int bind = STB_LOCAL; | |
2475 | int type = STT_OBJECT; | |
2476 | ||
2477 | if (flags & BSF_LOCAL) | |
2478 | bind = STB_LOCAL; | |
2479 | else if (flags & BSF_WEAK) | |
2480 | bind = STB_WEAK; | |
2481 | else if (flags & BSF_GLOBAL) | |
2482 | bind = STB_GLOBAL; | |
2483 | ||
2484 | if (flags & BSF_FUNCTION) | |
2485 | type = STT_FUNC; | |
2486 | ||
2487 | sym.st_info = ELF_ST_INFO (bind, type); | |
2488 | } | |
2489 | ||
2490 | sym.st_other = 0; | |
cf9fb9f2 | 2491 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); |
ede4eed4 KR |
2492 | outbound_syms += bed->s->sizeof_sym; |
2493 | } | |
2494 | ||
2495 | *sttp = stt; | |
2496 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
2497 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
2498 | ||
2499 | symstrtab_hdr->sh_flags = 0; | |
2500 | symstrtab_hdr->sh_addr = 0; | |
2501 | symstrtab_hdr->sh_entsize = 0; | |
2502 | symstrtab_hdr->sh_link = 0; | |
2503 | symstrtab_hdr->sh_info = 0; | |
2504 | symstrtab_hdr->sh_addralign = 1; | |
2505 | } | |
2506 | ||
2507 | return true; | |
2508 | } | |
2509 | ||
2510 | /* Return the number of bytes required to hold the symtab vector. | |
2511 | ||
2512 | Note that we base it on the count plus 1, since we will null terminate | |
2513 | the vector allocated based on this size. However, the ELF symbol table | |
2514 | always has a dummy entry as symbol #0, so it ends up even. */ | |
2515 | ||
2516 | long | |
2517 | _bfd_elf_get_symtab_upper_bound (abfd) | |
2518 | bfd *abfd; | |
2519 | { | |
2520 | long symcount; | |
2521 | long symtab_size; | |
2522 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
2523 | ||
2524 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2525 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
2526 | ||
2527 | return symtab_size; | |
2528 | } | |
2529 | ||
2530 | long | |
2531 | _bfd_elf_get_dynamic_symtab_upper_bound (abfd) | |
2532 | bfd *abfd; | |
2533 | { | |
2534 | long symcount; | |
2535 | long symtab_size; | |
2536 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2537 | ||
2538 | if (elf_dynsymtab (abfd) == 0) | |
2539 | { | |
2540 | bfd_set_error (bfd_error_invalid_operation); | |
2541 | return -1; | |
2542 | } | |
2543 | ||
2544 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
2545 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
2546 | ||
2547 | return symtab_size; | |
2548 | } | |
2549 | ||
2550 | long | |
2551 | _bfd_elf_get_reloc_upper_bound (abfd, asect) | |
2552 | bfd *abfd; | |
2553 | sec_ptr asect; | |
2554 | { | |
2555 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
2556 | } | |
2557 | ||
2558 | /* Canonicalize the relocs. */ | |
2559 | ||
2560 | long | |
2561 | _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
2562 | bfd *abfd; | |
2563 | sec_ptr section; | |
2564 | arelent **relptr; | |
2565 | asymbol **symbols; | |
2566 | { | |
2567 | arelent *tblptr; | |
2568 | unsigned int i; | |
2569 | ||
2570 | if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols)) | |
2571 | return -1; | |
2572 | ||
2573 | tblptr = section->relocation; | |
2574 | for (i = 0; i < section->reloc_count; i++) | |
2575 | *relptr++ = tblptr++; | |
2576 | ||
2577 | *relptr = NULL; | |
2578 | ||
2579 | return section->reloc_count; | |
2580 | } | |
2581 | ||
2582 | long | |
2583 | _bfd_elf_get_symtab (abfd, alocation) | |
2584 | bfd *abfd; | |
2585 | asymbol **alocation; | |
2586 | { | |
2587 | long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false); | |
2588 | ||
2589 | if (symcount >= 0) | |
2590 | bfd_get_symcount (abfd) = symcount; | |
2591 | return symcount; | |
2592 | } | |
2593 | ||
2594 | long | |
2595 | _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation) | |
2596 | bfd *abfd; | |
2597 | asymbol **alocation; | |
2598 | { | |
2599 | return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true); | |
2600 | } | |
2601 | ||
2602 | asymbol * | |
2603 | _bfd_elf_make_empty_symbol (abfd) | |
2604 | bfd *abfd; | |
2605 | { | |
2606 | elf_symbol_type *newsym; | |
2607 | ||
2608 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
2609 | if (!newsym) | |
2610 | { | |
2611 | bfd_set_error (bfd_error_no_memory); | |
2612 | return NULL; | |
2613 | } | |
2614 | else | |
2615 | { | |
2616 | newsym->symbol.the_bfd = abfd; | |
2617 | return &newsym->symbol; | |
2618 | } | |
2619 | } | |
2620 | ||
2621 | void | |
2622 | _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret) | |
2623 | bfd *ignore_abfd; | |
2624 | asymbol *symbol; | |
2625 | symbol_info *ret; | |
2626 | { | |
2627 | bfd_symbol_info (symbol, ret); | |
2628 | } | |
2629 | ||
2630 | alent * | |
2631 | _bfd_elf_get_lineno (ignore_abfd, symbol) | |
2632 | bfd *ignore_abfd; | |
2633 | asymbol *symbol; | |
2634 | { | |
2635 | fprintf (stderr, "elf_get_lineno unimplemented\n"); | |
2636 | fflush (stderr); | |
2637 | BFD_FAIL (); | |
2638 | return NULL; | |
2639 | } | |
2640 | ||
2641 | boolean | |
2642 | _bfd_elf_set_arch_mach (abfd, arch, machine) | |
2643 | bfd *abfd; | |
2644 | enum bfd_architecture arch; | |
2645 | unsigned long machine; | |
2646 | { | |
2647 | /* If this isn't the right architecture for this backend, and this | |
2648 | isn't the generic backend, fail. */ | |
2649 | if (arch != get_elf_backend_data (abfd)->arch | |
2650 | && arch != bfd_arch_unknown | |
2651 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
2652 | return false; | |
2653 | ||
2654 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
2655 | } | |
2656 | ||
6f904fce ILT |
2657 | /* Find the nearest line to a particular section and offset, for error |
2658 | reporting. */ | |
2659 | ||
ede4eed4 KR |
2660 | boolean |
2661 | _bfd_elf_find_nearest_line (abfd, | |
6f904fce ILT |
2662 | section, |
2663 | symbols, | |
2664 | offset, | |
2665 | filename_ptr, | |
2666 | functionname_ptr, | |
2667 | line_ptr) | |
ede4eed4 KR |
2668 | bfd *abfd; |
2669 | asection *section; | |
2670 | asymbol **symbols; | |
2671 | bfd_vma offset; | |
2672 | CONST char **filename_ptr; | |
2673 | CONST char **functionname_ptr; | |
2674 | unsigned int *line_ptr; | |
2675 | { | |
6f904fce ILT |
2676 | const char *filename; |
2677 | asymbol *func; | |
2678 | asymbol **p; | |
2679 | ||
2680 | if (symbols == NULL) | |
2681 | return false; | |
2682 | ||
2683 | filename = NULL; | |
2684 | func = NULL; | |
2685 | ||
2686 | for (p = symbols; *p != NULL; p++) | |
2687 | { | |
2688 | elf_symbol_type *q; | |
2689 | ||
2690 | q = (elf_symbol_type *) *p; | |
2691 | ||
2692 | if (bfd_get_section (&q->symbol) != section) | |
2693 | continue; | |
2694 | ||
2695 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) | |
2696 | { | |
2697 | default: | |
2698 | break; | |
2699 | case STT_FILE: | |
2700 | filename = bfd_asymbol_name (&q->symbol); | |
2701 | break; | |
2702 | case STT_FUNC: | |
2703 | if (func == NULL | |
2704 | || q->symbol.value <= offset) | |
2705 | func = (asymbol *) q; | |
2706 | break; | |
2707 | } | |
2708 | } | |
2709 | ||
2710 | if (func == NULL) | |
2711 | return false; | |
2712 | ||
2713 | *filename_ptr = filename; | |
2714 | *functionname_ptr = bfd_asymbol_name (func); | |
2715 | *line_ptr = 0; | |
2716 | return true; | |
ede4eed4 KR |
2717 | } |
2718 | ||
2719 | int | |
2720 | _bfd_elf_sizeof_headers (abfd, reloc) | |
2721 | bfd *abfd; | |
2722 | boolean reloc; | |
2723 | { | |
2724 | int ret; | |
2725 | ||
2726 | ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; | |
2727 | if (! reloc) | |
2728 | ret += get_program_header_size (abfd, (Elf_Internal_Shdr **) NULL, 0, | |
2729 | (bfd_vma) 0); | |
2730 | return ret; | |
2731 | } | |
2732 | ||
2733 | boolean | |
2734 | _bfd_elf_set_section_contents (abfd, section, location, offset, count) | |
2735 | bfd *abfd; | |
2736 | sec_ptr section; | |
2737 | PTR location; | |
2738 | file_ptr offset; | |
2739 | bfd_size_type count; | |
2740 | { | |
2741 | Elf_Internal_Shdr *hdr; | |
2742 | ||
2743 | if (! abfd->output_has_begun | |
2744 | && ! _bfd_elf_compute_section_file_positions (abfd, | |
2745 | (struct bfd_link_info *) NULL)) | |
2746 | return false; | |
2747 | ||
2748 | hdr = &elf_section_data (section)->this_hdr; | |
2749 | ||
2750 | if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) | |
2751 | return false; | |
2752 | if (bfd_write (location, 1, count, abfd) != count) | |
2753 | return false; | |
2754 | ||
2755 | return true; | |
2756 | } | |
2757 | ||
2758 | void | |
2759 | _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst) | |
2760 | bfd *abfd; | |
2761 | arelent *cache_ptr; | |
2762 | Elf_Internal_Rela *dst; | |
2763 | { | |
2764 | fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n"); | |
2765 | fflush (stderr); | |
2766 | BFD_FAIL (); | |
2767 | } | |
2768 | ||
2769 | #if 0 | |
2770 | void | |
2771 | _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst) | |
2772 | bfd *abfd; | |
2773 | arelent *cache_ptr; | |
2774 | Elf_Internal_Rel *dst; | |
2775 | { | |
2776 | fprintf (stderr, "elf REL relocation support for target machine unimplemented\n"); | |
2777 | fflush (stderr); | |
2778 | BFD_FAIL (); | |
2779 | } | |
2780 | #endif |