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