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 | ||
03ae5f59 ILT |
1204 | /* Check for a bogus link to avoid crashing. */ |
1205 | if (hdr->sh_link >= ehdr->e_shnum) | |
1206 | { | |
1207 | ((*_bfd_error_handler) | |
1208 | (_("%s: invalid link %lu for reloc section %s (index %u)"), | |
1209 | bfd_get_filename (abfd), hdr->sh_link, name, shindex)); | |
1210 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1211 | } | |
1212 | ||
252b5132 RH |
1213 | /* For some incomprehensible reason Oracle distributes |
1214 | libraries for Solaris in which some of the objects have | |
1215 | bogus sh_link fields. It would be nice if we could just | |
1216 | reject them, but, unfortunately, some people need to use | |
1217 | them. We scan through the section headers; if we find only | |
1218 | one suitable symbol table, we clobber the sh_link to point | |
1219 | to it. I hope this doesn't break anything. */ | |
1220 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
1221 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
1222 | { | |
1223 | int scan; | |
1224 | int found; | |
1225 | ||
1226 | found = 0; | |
1227 | for (scan = 1; scan < ehdr->e_shnum; scan++) | |
1228 | { | |
1229 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1230 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1231 | { | |
1232 | if (found != 0) | |
1233 | { | |
1234 | found = 0; | |
1235 | break; | |
1236 | } | |
1237 | found = scan; | |
1238 | } | |
1239 | } | |
1240 | if (found != 0) | |
1241 | hdr->sh_link = found; | |
1242 | } | |
1243 | ||
1244 | /* Get the symbol table. */ | |
1245 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB | |
1246 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
1247 | return false; | |
1248 | ||
1249 | /* If this reloc section does not use the main symbol table we | |
1250 | don't treat it as a reloc section. BFD can't adequately | |
1251 | represent such a section, so at least for now, we don't | |
1252 | try. We just present it as a normal section. */ | |
1253 | if (hdr->sh_link != elf_onesymtab (abfd)) | |
1254 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1255 | ||
1256 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
1257 | return false; | |
1258 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); | |
1259 | if (target_sect == NULL) | |
1260 | return false; | |
1261 | ||
1262 | if ((target_sect->flags & SEC_RELOC) == 0 | |
1263 | || target_sect->reloc_count == 0) | |
1264 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
1265 | else | |
1266 | { | |
1267 | BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); | |
1268 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); | |
1269 | elf_section_data (target_sect)->rel_hdr2 = hdr2; | |
1270 | } | |
1271 | *hdr2 = *hdr; | |
1272 | elf_elfsections (abfd)[shindex] = hdr2; | |
1273 | target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize; | |
1274 | target_sect->flags |= SEC_RELOC; | |
1275 | target_sect->relocation = NULL; | |
1276 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
1277 | /* In the section to which the relocations apply, mark whether |
1278 | its relocations are of the REL or RELA variety. */ | |
1279 | elf_section_data (target_sect)->use_rela_p | |
1280 | = (hdr->sh_type == SHT_RELA); | |
252b5132 RH |
1281 | abfd->flags |= HAS_RELOC; |
1282 | return true; | |
1283 | } | |
1284 | break; | |
1285 | ||
1286 | case SHT_GNU_verdef: | |
1287 | elf_dynverdef (abfd) = shindex; | |
1288 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
1289 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1290 | break; | |
1291 | ||
1292 | case SHT_GNU_versym: | |
1293 | elf_dynversym (abfd) = shindex; | |
1294 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
1295 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1296 | break; | |
1297 | ||
1298 | case SHT_GNU_verneed: | |
1299 | elf_dynverref (abfd) = shindex; | |
1300 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
1301 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1302 | break; | |
1303 | ||
1304 | case SHT_SHLIB: | |
1305 | return true; | |
1306 | ||
1307 | default: | |
1308 | /* Check for any processor-specific section types. */ | |
1309 | { | |
1310 | if (bed->elf_backend_section_from_shdr) | |
1311 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
1312 | } | |
1313 | break; | |
1314 | } | |
1315 | ||
1316 | return true; | |
1317 | } | |
1318 | ||
1319 | /* Given an ELF section number, retrieve the corresponding BFD | |
1320 | section. */ | |
1321 | ||
1322 | asection * | |
1323 | bfd_section_from_elf_index (abfd, index) | |
1324 | bfd *abfd; | |
1325 | unsigned int index; | |
1326 | { | |
1327 | BFD_ASSERT (index > 0 && index < SHN_LORESERVE); | |
1328 | if (index >= elf_elfheader (abfd)->e_shnum) | |
1329 | return NULL; | |
1330 | return elf_elfsections (abfd)[index]->bfd_section; | |
1331 | } | |
1332 | ||
1333 | boolean | |
1334 | _bfd_elf_new_section_hook (abfd, sec) | |
1335 | bfd *abfd; | |
1336 | asection *sec; | |
1337 | { | |
1338 | struct bfd_elf_section_data *sdata; | |
1339 | ||
23bc299b | 1340 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata)); |
252b5132 RH |
1341 | if (!sdata) |
1342 | return false; | |
1343 | sec->used_by_bfd = (PTR) sdata; | |
bf572ba0 MM |
1344 | |
1345 | /* Indicate whether or not this section should use RELA relocations. */ | |
1346 | sdata->use_rela_p | |
1347 | = get_elf_backend_data (abfd)->default_use_rela_p; | |
1348 | ||
252b5132 RH |
1349 | return true; |
1350 | } | |
1351 | ||
1352 | /* Create a new bfd section from an ELF program header. | |
1353 | ||
1354 | Since program segments have no names, we generate a synthetic name | |
1355 | of the form segment<NUM>, where NUM is generally the index in the | |
1356 | program header table. For segments that are split (see below) we | |
1357 | generate the names segment<NUM>a and segment<NUM>b. | |
1358 | ||
1359 | Note that some program segments may have a file size that is different than | |
1360 | (less than) the memory size. All this means is that at execution the | |
1361 | system must allocate the amount of memory specified by the memory size, | |
1362 | but only initialize it with the first "file size" bytes read from the | |
1363 | file. This would occur for example, with program segments consisting | |
1364 | of combined data+bss. | |
1365 | ||
1366 | To handle the above situation, this routine generates TWO bfd sections | |
1367 | for the single program segment. The first has the length specified by | |
1368 | the file size of the segment, and the second has the length specified | |
1369 | by the difference between the two sizes. In effect, the segment is split | |
1370 | into it's initialized and uninitialized parts. | |
1371 | ||
1372 | */ | |
1373 | ||
1374 | boolean | |
1375 | bfd_section_from_phdr (abfd, hdr, index) | |
1376 | bfd *abfd; | |
1377 | Elf_Internal_Phdr *hdr; | |
1378 | int index; | |
1379 | { | |
1380 | asection *newsect; | |
1381 | char *name; | |
1382 | char namebuf[64]; | |
1383 | int split; | |
1384 | ||
1385 | split = ((hdr->p_memsz > 0) | |
1386 | && (hdr->p_filesz > 0) | |
1387 | && (hdr->p_memsz > hdr->p_filesz)); | |
1388 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
1389 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
1390 | if (!name) | |
1391 | return false; | |
1392 | strcpy (name, namebuf); | |
1393 | newsect = bfd_make_section (abfd, name); | |
1394 | if (newsect == NULL) | |
1395 | return false; | |
1396 | newsect->vma = hdr->p_vaddr; | |
1397 | newsect->lma = hdr->p_paddr; | |
1398 | newsect->_raw_size = hdr->p_filesz; | |
1399 | newsect->filepos = hdr->p_offset; | |
1400 | newsect->flags |= SEC_HAS_CONTENTS; | |
1401 | if (hdr->p_type == PT_LOAD) | |
1402 | { | |
1403 | newsect->flags |= SEC_ALLOC; | |
1404 | newsect->flags |= SEC_LOAD; | |
1405 | if (hdr->p_flags & PF_X) | |
1406 | { | |
1407 | /* FIXME: all we known is that it has execute PERMISSION, | |
1408 | may be data. */ | |
1409 | newsect->flags |= SEC_CODE; | |
1410 | } | |
1411 | } | |
1412 | if (!(hdr->p_flags & PF_W)) | |
1413 | { | |
1414 | newsect->flags |= SEC_READONLY; | |
1415 | } | |
1416 | ||
1417 | if (split) | |
1418 | { | |
1419 | sprintf (namebuf, "segment%db", index); | |
1420 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
1421 | if (!name) | |
1422 | return false; | |
1423 | strcpy (name, namebuf); | |
1424 | newsect = bfd_make_section (abfd, name); | |
1425 | if (newsect == NULL) | |
1426 | return false; | |
1427 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; | |
1428 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
1429 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; | |
1430 | if (hdr->p_type == PT_LOAD) | |
1431 | { | |
1432 | newsect->flags |= SEC_ALLOC; | |
1433 | if (hdr->p_flags & PF_X) | |
1434 | newsect->flags |= SEC_CODE; | |
1435 | } | |
1436 | if (!(hdr->p_flags & PF_W)) | |
1437 | newsect->flags |= SEC_READONLY; | |
1438 | } | |
1439 | ||
1440 | return true; | |
1441 | } | |
1442 | ||
23bc299b MM |
1443 | /* Initialize REL_HDR, the section-header for new section, containing |
1444 | relocations against ASECT. If USE_RELA_P is true, we use RELA | |
1445 | relocations; otherwise, we use REL relocations. */ | |
1446 | ||
1447 | boolean | |
1448 | _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p) | |
1449 | bfd *abfd; | |
1450 | Elf_Internal_Shdr *rel_hdr; | |
1451 | asection *asect; | |
1452 | boolean use_rela_p; | |
1453 | { | |
1454 | char *name; | |
1455 | struct elf_backend_data *bed; | |
1456 | ||
1457 | bed = get_elf_backend_data (abfd); | |
1458 | name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name)); | |
1459 | if (name == NULL) | |
1460 | return false; | |
1461 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); | |
1462 | rel_hdr->sh_name = | |
1463 | (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name, | |
1464 | true, false); | |
1465 | if (rel_hdr->sh_name == (unsigned int) -1) | |
1466 | return false; | |
1467 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; | |
1468 | rel_hdr->sh_entsize = (use_rela_p | |
1469 | ? bed->s->sizeof_rela | |
1470 | : bed->s->sizeof_rel); | |
1471 | rel_hdr->sh_addralign = bed->s->file_align; | |
1472 | rel_hdr->sh_flags = 0; | |
1473 | rel_hdr->sh_addr = 0; | |
1474 | rel_hdr->sh_size = 0; | |
1475 | rel_hdr->sh_offset = 0; | |
1476 | ||
1477 | return true; | |
1478 | } | |
1479 | ||
252b5132 RH |
1480 | /* Set up an ELF internal section header for a section. */ |
1481 | ||
1482 | /*ARGSUSED*/ | |
1483 | static void | |
1484 | elf_fake_sections (abfd, asect, failedptrarg) | |
1485 | bfd *abfd; | |
1486 | asection *asect; | |
1487 | PTR failedptrarg; | |
1488 | { | |
1489 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1490 | boolean *failedptr = (boolean *) failedptrarg; | |
1491 | Elf_Internal_Shdr *this_hdr; | |
1492 | ||
1493 | if (*failedptr) | |
1494 | { | |
1495 | /* We already failed; just get out of the bfd_map_over_sections | |
1496 | loop. */ | |
1497 | return; | |
1498 | } | |
1499 | ||
1500 | this_hdr = &elf_section_data (asect)->this_hdr; | |
1501 | ||
1502 | this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd), | |
1503 | asect->name, | |
1504 | true, false); | |
1505 | if (this_hdr->sh_name == (unsigned long) -1) | |
1506 | { | |
1507 | *failedptr = true; | |
1508 | return; | |
1509 | } | |
1510 | ||
1511 | this_hdr->sh_flags = 0; | |
1512 | ||
1513 | if ((asect->flags & SEC_ALLOC) != 0 | |
1514 | || asect->user_set_vma) | |
1515 | this_hdr->sh_addr = asect->vma; | |
1516 | else | |
1517 | this_hdr->sh_addr = 0; | |
1518 | ||
1519 | this_hdr->sh_offset = 0; | |
1520 | this_hdr->sh_size = asect->_raw_size; | |
1521 | this_hdr->sh_link = 0; | |
1522 | this_hdr->sh_addralign = 1 << asect->alignment_power; | |
1523 | /* The sh_entsize and sh_info fields may have been set already by | |
1524 | copy_private_section_data. */ | |
1525 | ||
1526 | this_hdr->bfd_section = asect; | |
1527 | this_hdr->contents = NULL; | |
1528 | ||
1529 | /* FIXME: This should not be based on section names. */ | |
1530 | if (strcmp (asect->name, ".dynstr") == 0) | |
1531 | this_hdr->sh_type = SHT_STRTAB; | |
1532 | else if (strcmp (asect->name, ".hash") == 0) | |
1533 | { | |
1534 | this_hdr->sh_type = SHT_HASH; | |
1535 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
1536 | } | |
1537 | else if (strcmp (asect->name, ".dynsym") == 0) | |
1538 | { | |
1539 | this_hdr->sh_type = SHT_DYNSYM; | |
1540 | this_hdr->sh_entsize = bed->s->sizeof_sym; | |
1541 | } | |
1542 | else if (strcmp (asect->name, ".dynamic") == 0) | |
1543 | { | |
1544 | this_hdr->sh_type = SHT_DYNAMIC; | |
1545 | this_hdr->sh_entsize = bed->s->sizeof_dyn; | |
1546 | } | |
a9d024b8 | 1547 | else if (strncmp (asect->name, ".rela", 5) == 0 |
bf572ba0 | 1548 | && get_elf_backend_data (abfd)->may_use_rela_p) |
252b5132 RH |
1549 | { |
1550 | this_hdr->sh_type = SHT_RELA; | |
1551 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
1552 | } | |
a9d024b8 | 1553 | else if (strncmp (asect->name, ".rel", 4) == 0 |
bf572ba0 | 1554 | && get_elf_backend_data (abfd)->may_use_rel_p) |
252b5132 RH |
1555 | { |
1556 | this_hdr->sh_type = SHT_REL; | |
1557 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
1558 | } | |
1559 | else if (strncmp (asect->name, ".note", 5) == 0) | |
1560 | this_hdr->sh_type = SHT_NOTE; | |
1561 | else if (strncmp (asect->name, ".stab", 5) == 0 | |
1562 | && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) | |
1563 | this_hdr->sh_type = SHT_STRTAB; | |
1564 | else if (strcmp (asect->name, ".gnu.version") == 0) | |
1565 | { | |
1566 | this_hdr->sh_type = SHT_GNU_versym; | |
1567 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); | |
1568 | } | |
1569 | else if (strcmp (asect->name, ".gnu.version_d") == 0) | |
1570 | { | |
1571 | this_hdr->sh_type = SHT_GNU_verdef; | |
1572 | this_hdr->sh_entsize = 0; | |
1573 | /* objcopy or strip will copy over sh_info, but may not set | |
1574 | cverdefs. The linker will set cverdefs, but sh_info will be | |
1575 | zero. */ | |
1576 | if (this_hdr->sh_info == 0) | |
1577 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
1578 | else | |
1579 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
1580 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
1581 | } | |
1582 | else if (strcmp (asect->name, ".gnu.version_r") == 0) | |
1583 | { | |
1584 | this_hdr->sh_type = SHT_GNU_verneed; | |
1585 | this_hdr->sh_entsize = 0; | |
1586 | /* objcopy or strip will copy over sh_info, but may not set | |
1587 | cverrefs. The linker will set cverrefs, but sh_info will be | |
1588 | zero. */ | |
1589 | if (this_hdr->sh_info == 0) | |
1590 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
1591 | else | |
1592 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
1593 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
1594 | } | |
1595 | else if ((asect->flags & SEC_ALLOC) != 0 | |
1596 | && (asect->flags & SEC_LOAD) != 0) | |
1597 | this_hdr->sh_type = SHT_PROGBITS; | |
1598 | else if ((asect->flags & SEC_ALLOC) != 0 | |
1599 | && ((asect->flags & SEC_LOAD) == 0)) | |
1600 | this_hdr->sh_type = SHT_NOBITS; | |
1601 | else | |
1602 | { | |
1603 | /* Who knows? */ | |
1604 | this_hdr->sh_type = SHT_PROGBITS; | |
1605 | } | |
1606 | ||
1607 | if ((asect->flags & SEC_ALLOC) != 0) | |
1608 | this_hdr->sh_flags |= SHF_ALLOC; | |
1609 | if ((asect->flags & SEC_READONLY) == 0) | |
1610 | this_hdr->sh_flags |= SHF_WRITE; | |
1611 | if ((asect->flags & SEC_CODE) != 0) | |
1612 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
1613 | ||
1614 | /* Check for processor-specific section types. */ | |
bf572ba0 MM |
1615 | if (bed->elf_backend_fake_sections) |
1616 | (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect); | |
252b5132 RH |
1617 | |
1618 | /* If the section has relocs, set up a section header for the | |
23bc299b MM |
1619 | SHT_REL[A] section. If two relocation sections are required for |
1620 | this section, it is up to the processor-specific back-end to | |
1621 | create the other. */ | |
1622 | if ((asect->flags & SEC_RELOC) != 0 | |
1623 | && !_bfd_elf_init_reloc_shdr (abfd, | |
1624 | &elf_section_data (asect)->rel_hdr, | |
1625 | asect, | |
1626 | elf_section_data (asect)->use_rela_p)) | |
1627 | *failedptr = true; | |
252b5132 RH |
1628 | } |
1629 | ||
1630 | /* Assign all ELF section numbers. The dummy first section is handled here | |
1631 | too. The link/info pointers for the standard section types are filled | |
1632 | in here too, while we're at it. */ | |
1633 | ||
1634 | static boolean | |
1635 | assign_section_numbers (abfd) | |
1636 | bfd *abfd; | |
1637 | { | |
1638 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
1639 | asection *sec; | |
1640 | unsigned int section_number; | |
1641 | Elf_Internal_Shdr **i_shdrp; | |
1642 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1643 | ||
1644 | section_number = 1; | |
1645 | ||
1646 | for (sec = abfd->sections; sec; sec = sec->next) | |
1647 | { | |
1648 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1649 | ||
1650 | d->this_idx = section_number++; | |
1651 | if ((sec->flags & SEC_RELOC) == 0) | |
1652 | d->rel_idx = 0; | |
1653 | else | |
1654 | d->rel_idx = section_number++; | |
23bc299b MM |
1655 | |
1656 | if (d->rel_hdr2) | |
1657 | d->rel_idx2 = section_number++; | |
1658 | else | |
1659 | d->rel_idx2 = 0; | |
252b5132 RH |
1660 | } |
1661 | ||
1662 | t->shstrtab_section = section_number++; | |
1663 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; | |
1664 | t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
1665 | ||
1666 | if (bfd_get_symcount (abfd) > 0) | |
1667 | { | |
1668 | t->symtab_section = section_number++; | |
1669 | t->strtab_section = section_number++; | |
1670 | } | |
1671 | ||
1672 | elf_elfheader (abfd)->e_shnum = section_number; | |
1673 | ||
1674 | /* Set up the list of section header pointers, in agreement with the | |
1675 | indices. */ | |
1676 | i_shdrp = ((Elf_Internal_Shdr **) | |
1677 | bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *))); | |
1678 | if (i_shdrp == NULL) | |
1679 | return false; | |
1680 | ||
1681 | i_shdrp[0] = ((Elf_Internal_Shdr *) | |
1682 | bfd_alloc (abfd, sizeof (Elf_Internal_Shdr))); | |
1683 | if (i_shdrp[0] == NULL) | |
1684 | { | |
1685 | bfd_release (abfd, i_shdrp); | |
1686 | return false; | |
1687 | } | |
1688 | memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr)); | |
1689 | ||
1690 | elf_elfsections (abfd) = i_shdrp; | |
1691 | ||
1692 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
1693 | if (bfd_get_symcount (abfd) > 0) | |
1694 | { | |
1695 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
1696 | i_shdrp[t->strtab_section] = &t->strtab_hdr; | |
1697 | t->symtab_hdr.sh_link = t->strtab_section; | |
1698 | } | |
1699 | for (sec = abfd->sections; sec; sec = sec->next) | |
1700 | { | |
1701 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1702 | asection *s; | |
1703 | const char *name; | |
1704 | ||
1705 | i_shdrp[d->this_idx] = &d->this_hdr; | |
1706 | if (d->rel_idx != 0) | |
1707 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
23bc299b MM |
1708 | if (d->rel_idx2 != 0) |
1709 | i_shdrp[d->rel_idx2] = d->rel_hdr2; | |
252b5132 RH |
1710 | |
1711 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
1712 | ||
1713 | /* sh_link of a reloc section is the section index of the symbol | |
1714 | table. sh_info is the section index of the section to which | |
1715 | the relocation entries apply. */ | |
1716 | if (d->rel_idx != 0) | |
1717 | { | |
1718 | d->rel_hdr.sh_link = t->symtab_section; | |
1719 | d->rel_hdr.sh_info = d->this_idx; | |
1720 | } | |
23bc299b MM |
1721 | if (d->rel_idx2 != 0) |
1722 | { | |
1723 | d->rel_hdr2->sh_link = t->symtab_section; | |
1724 | d->rel_hdr2->sh_info = d->this_idx; | |
1725 | } | |
252b5132 RH |
1726 | |
1727 | switch (d->this_hdr.sh_type) | |
1728 | { | |
1729 | case SHT_REL: | |
1730 | case SHT_RELA: | |
1731 | /* A reloc section which we are treating as a normal BFD | |
1732 | section. sh_link is the section index of the symbol | |
1733 | table. sh_info is the section index of the section to | |
1734 | which the relocation entries apply. We assume that an | |
1735 | allocated reloc section uses the dynamic symbol table. | |
1736 | FIXME: How can we be sure? */ | |
1737 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1738 | if (s != NULL) | |
1739 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1740 | ||
1741 | /* We look up the section the relocs apply to by name. */ | |
1742 | name = sec->name; | |
1743 | if (d->this_hdr.sh_type == SHT_REL) | |
1744 | name += 4; | |
1745 | else | |
1746 | name += 5; | |
1747 | s = bfd_get_section_by_name (abfd, name); | |
1748 | if (s != NULL) | |
1749 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
1750 | break; | |
1751 | ||
1752 | case SHT_STRTAB: | |
1753 | /* We assume that a section named .stab*str is a stabs | |
1754 | string section. We look for a section with the same name | |
1755 | but without the trailing ``str'', and set its sh_link | |
1756 | field to point to this section. */ | |
1757 | if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 | |
1758 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) | |
1759 | { | |
1760 | size_t len; | |
1761 | char *alc; | |
1762 | ||
1763 | len = strlen (sec->name); | |
1764 | alc = (char *) bfd_malloc (len - 2); | |
1765 | if (alc == NULL) | |
1766 | return false; | |
1767 | strncpy (alc, sec->name, len - 3); | |
1768 | alc[len - 3] = '\0'; | |
1769 | s = bfd_get_section_by_name (abfd, alc); | |
1770 | free (alc); | |
1771 | if (s != NULL) | |
1772 | { | |
1773 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
1774 | ||
1775 | /* This is a .stab section. */ | |
1776 | elf_section_data (s)->this_hdr.sh_entsize = | |
1777 | 4 + 2 * (bed->s->arch_size / 8); | |
1778 | } | |
1779 | } | |
1780 | break; | |
1781 | ||
1782 | case SHT_DYNAMIC: | |
1783 | case SHT_DYNSYM: | |
1784 | case SHT_GNU_verneed: | |
1785 | case SHT_GNU_verdef: | |
1786 | /* sh_link is the section header index of the string table | |
1787 | used for the dynamic entries, or the symbol table, or the | |
1788 | version strings. */ | |
1789 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
1790 | if (s != NULL) | |
1791 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1792 | break; | |
1793 | ||
1794 | case SHT_HASH: | |
1795 | case SHT_GNU_versym: | |
1796 | /* sh_link is the section header index of the symbol table | |
1797 | this hash table or version table is for. */ | |
1798 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1799 | if (s != NULL) | |
1800 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1801 | break; | |
1802 | } | |
1803 | } | |
1804 | ||
1805 | return true; | |
1806 | } | |
1807 | ||
1808 | /* Map symbol from it's internal number to the external number, moving | |
1809 | all local symbols to be at the head of the list. */ | |
1810 | ||
1811 | static INLINE int | |
1812 | sym_is_global (abfd, sym) | |
1813 | bfd *abfd; | |
1814 | asymbol *sym; | |
1815 | { | |
1816 | /* If the backend has a special mapping, use it. */ | |
1817 | if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1818 | return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1819 | (abfd, sym)); | |
1820 | ||
1821 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 | |
1822 | || bfd_is_und_section (bfd_get_section (sym)) | |
1823 | || bfd_is_com_section (bfd_get_section (sym))); | |
1824 | } | |
1825 | ||
1826 | static boolean | |
1827 | elf_map_symbols (abfd) | |
1828 | bfd *abfd; | |
1829 | { | |
1830 | int symcount = bfd_get_symcount (abfd); | |
1831 | asymbol **syms = bfd_get_outsymbols (abfd); | |
1832 | asymbol **sect_syms; | |
1833 | int num_locals = 0; | |
1834 | int num_globals = 0; | |
1835 | int num_locals2 = 0; | |
1836 | int num_globals2 = 0; | |
1837 | int max_index = 0; | |
1838 | int num_sections = 0; | |
1839 | int idx; | |
1840 | asection *asect; | |
1841 | asymbol **new_syms; | |
1842 | asymbol *sym; | |
1843 | ||
1844 | #ifdef DEBUG | |
1845 | fprintf (stderr, "elf_map_symbols\n"); | |
1846 | fflush (stderr); | |
1847 | #endif | |
1848 | ||
1849 | /* Add a section symbol for each BFD section. FIXME: Is this really | |
1850 | necessary? */ | |
1851 | for (asect = abfd->sections; asect; asect = asect->next) | |
1852 | { | |
1853 | if (max_index < asect->index) | |
1854 | max_index = asect->index; | |
1855 | } | |
1856 | ||
1857 | max_index++; | |
1858 | sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *)); | |
1859 | if (sect_syms == NULL) | |
1860 | return false; | |
1861 | elf_section_syms (abfd) = sect_syms; | |
1862 | ||
1863 | for (idx = 0; idx < symcount; idx++) | |
1864 | { | |
1865 | sym = syms[idx]; | |
1866 | ||
1867 | if ((sym->flags & BSF_SECTION_SYM) != 0 | |
1868 | && sym->value == 0) | |
1869 | { | |
1870 | asection *sec; | |
1871 | ||
1872 | sec = sym->section; | |
1873 | ||
1874 | if (sec->owner != NULL) | |
1875 | { | |
1876 | if (sec->owner != abfd) | |
1877 | { | |
1878 | if (sec->output_offset != 0) | |
1879 | continue; | |
1880 | ||
1881 | sec = sec->output_section; | |
1882 | ||
1883 | /* Empty sections in the input files may have had a section | |
1884 | symbol created for them. (See the comment near the end of | |
1885 | _bfd_generic_link_output_symbols in linker.c). If the linker | |
1886 | script discards such sections then we will reach this point. | |
1887 | Since we know that we cannot avoid this case, we detect it | |
1888 | and skip the abort and the assignment to the sect_syms array. | |
1889 | To reproduce this particular case try running the linker | |
1890 | testsuite test ld-scripts/weak.exp for an ELF port that uses | |
1891 | the generic linker. */ | |
1892 | if (sec->owner == NULL) | |
1893 | continue; | |
1894 | ||
1895 | BFD_ASSERT (sec->owner == abfd); | |
1896 | } | |
1897 | sect_syms[sec->index] = syms[idx]; | |
1898 | } | |
1899 | } | |
1900 | } | |
1901 | ||
1902 | for (asect = abfd->sections; asect; asect = asect->next) | |
1903 | { | |
1904 | if (sect_syms[asect->index] != NULL) | |
1905 | continue; | |
1906 | ||
1907 | sym = bfd_make_empty_symbol (abfd); | |
1908 | if (sym == NULL) | |
1909 | return false; | |
1910 | sym->the_bfd = abfd; | |
1911 | sym->name = asect->name; | |
1912 | sym->value = 0; | |
1913 | /* Set the flags to 0 to indicate that this one was newly added. */ | |
1914 | sym->flags = 0; | |
1915 | sym->section = asect; | |
1916 | sect_syms[asect->index] = sym; | |
1917 | num_sections++; | |
1918 | #ifdef DEBUG | |
1919 | fprintf (stderr, | |
1920 | _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"), | |
1921 | asect->name, (long) asect->vma, asect->index, (long) asect); | |
1922 | #endif | |
1923 | } | |
1924 | ||
1925 | /* Classify all of the symbols. */ | |
1926 | for (idx = 0; idx < symcount; idx++) | |
1927 | { | |
1928 | if (!sym_is_global (abfd, syms[idx])) | |
1929 | num_locals++; | |
1930 | else | |
1931 | num_globals++; | |
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 | sect_syms[asect->index]->flags = BSF_SECTION_SYM; | |
1939 | if (!sym_is_global (abfd, sect_syms[asect->index])) | |
1940 | num_locals++; | |
1941 | else | |
1942 | num_globals++; | |
1943 | sect_syms[asect->index]->flags = 0; | |
1944 | } | |
1945 | } | |
1946 | ||
1947 | /* Now sort the symbols so the local symbols are first. */ | |
1948 | new_syms = ((asymbol **) | |
1949 | bfd_alloc (abfd, | |
1950 | (num_locals + num_globals) * sizeof (asymbol *))); | |
1951 | if (new_syms == NULL) | |
1952 | return false; | |
1953 | ||
1954 | for (idx = 0; idx < symcount; idx++) | |
1955 | { | |
1956 | asymbol *sym = syms[idx]; | |
1957 | int i; | |
1958 | ||
1959 | if (!sym_is_global (abfd, sym)) | |
1960 | i = num_locals2++; | |
1961 | else | |
1962 | i = num_locals + num_globals2++; | |
1963 | new_syms[i] = sym; | |
1964 | sym->udata.i = i + 1; | |
1965 | } | |
1966 | for (asect = abfd->sections; asect; asect = asect->next) | |
1967 | { | |
1968 | if (sect_syms[asect->index] != NULL | |
1969 | && sect_syms[asect->index]->flags == 0) | |
1970 | { | |
1971 | asymbol *sym = sect_syms[asect->index]; | |
1972 | int i; | |
1973 | ||
1974 | sym->flags = BSF_SECTION_SYM; | |
1975 | if (!sym_is_global (abfd, sym)) | |
1976 | i = num_locals2++; | |
1977 | else | |
1978 | i = num_locals + num_globals2++; | |
1979 | new_syms[i] = sym; | |
1980 | sym->udata.i = i + 1; | |
1981 | } | |
1982 | } | |
1983 | ||
1984 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
1985 | ||
1986 | elf_num_locals (abfd) = num_locals; | |
1987 | elf_num_globals (abfd) = num_globals; | |
1988 | return true; | |
1989 | } | |
1990 | ||
1991 | /* Align to the maximum file alignment that could be required for any | |
1992 | ELF data structure. */ | |
1993 | ||
1994 | static INLINE file_ptr align_file_position PARAMS ((file_ptr, int)); | |
1995 | static INLINE file_ptr | |
1996 | align_file_position (off, align) | |
1997 | file_ptr off; | |
1998 | int align; | |
1999 | { | |
2000 | return (off + align - 1) & ~(align - 1); | |
2001 | } | |
2002 | ||
2003 | /* Assign a file position to a section, optionally aligning to the | |
2004 | required section alignment. */ | |
2005 | ||
2006 | INLINE file_ptr | |
2007 | _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align) | |
2008 | Elf_Internal_Shdr *i_shdrp; | |
2009 | file_ptr offset; | |
2010 | boolean align; | |
2011 | { | |
2012 | if (align) | |
2013 | { | |
2014 | unsigned int al; | |
2015 | ||
2016 | al = i_shdrp->sh_addralign; | |
2017 | if (al > 1) | |
2018 | offset = BFD_ALIGN (offset, al); | |
2019 | } | |
2020 | i_shdrp->sh_offset = offset; | |
2021 | if (i_shdrp->bfd_section != NULL) | |
2022 | i_shdrp->bfd_section->filepos = offset; | |
2023 | if (i_shdrp->sh_type != SHT_NOBITS) | |
2024 | offset += i_shdrp->sh_size; | |
2025 | return offset; | |
2026 | } | |
2027 | ||
2028 | /* Compute the file positions we are going to put the sections at, and | |
2029 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
2030 | is not NULL, this is being called by the ELF backend linker. */ | |
2031 | ||
2032 | boolean | |
2033 | _bfd_elf_compute_section_file_positions (abfd, link_info) | |
2034 | bfd *abfd; | |
2035 | struct bfd_link_info *link_info; | |
2036 | { | |
2037 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2038 | boolean failed; | |
2039 | struct bfd_strtab_hash *strtab; | |
2040 | Elf_Internal_Shdr *shstrtab_hdr; | |
2041 | ||
2042 | if (abfd->output_has_begun) | |
2043 | return true; | |
2044 | ||
2045 | /* Do any elf backend specific processing first. */ | |
2046 | if (bed->elf_backend_begin_write_processing) | |
2047 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
2048 | ||
2049 | if (! prep_headers (abfd)) | |
2050 | return false; | |
2051 | ||
e6c51ed4 NC |
2052 | /* Post process the headers if necessary. */ |
2053 | if (bed->elf_backend_post_process_headers) | |
2054 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
2055 | ||
252b5132 RH |
2056 | failed = false; |
2057 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); | |
2058 | if (failed) | |
2059 | return false; | |
2060 | ||
2061 | if (!assign_section_numbers (abfd)) | |
2062 | return false; | |
2063 | ||
2064 | /* The backend linker builds symbol table information itself. */ | |
2065 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
2066 | { | |
2067 | /* Non-zero if doing a relocatable link. */ | |
2068 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
2069 | ||
2070 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
2071 | return false; | |
2072 | } | |
2073 | ||
2074 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; | |
2075 | /* sh_name was set in prep_headers. */ | |
2076 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
2077 | shstrtab_hdr->sh_flags = 0; | |
2078 | shstrtab_hdr->sh_addr = 0; | |
2079 | shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
2080 | shstrtab_hdr->sh_entsize = 0; | |
2081 | shstrtab_hdr->sh_link = 0; | |
2082 | shstrtab_hdr->sh_info = 0; | |
2083 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
2084 | shstrtab_hdr->sh_addralign = 1; | |
2085 | ||
2086 | if (!assign_file_positions_except_relocs (abfd)) | |
2087 | return false; | |
2088 | ||
2089 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
2090 | { | |
2091 | file_ptr off; | |
2092 | Elf_Internal_Shdr *hdr; | |
2093 | ||
2094 | off = elf_tdata (abfd)->next_file_pos; | |
2095 | ||
2096 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
2097 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
2098 | ||
2099 | hdr = &elf_tdata (abfd)->strtab_hdr; | |
2100 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
2101 | ||
2102 | elf_tdata (abfd)->next_file_pos = off; | |
2103 | ||
2104 | /* Now that we know where the .strtab section goes, write it | |
2105 | out. */ | |
2106 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
2107 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
2108 | return false; | |
2109 | _bfd_stringtab_free (strtab); | |
2110 | } | |
2111 | ||
2112 | abfd->output_has_begun = true; | |
2113 | ||
2114 | return true; | |
2115 | } | |
2116 | ||
2117 | /* Create a mapping from a set of sections to a program segment. */ | |
2118 | ||
2119 | static INLINE struct elf_segment_map * | |
2120 | make_mapping (abfd, sections, from, to, phdr) | |
2121 | bfd *abfd; | |
2122 | asection **sections; | |
2123 | unsigned int from; | |
2124 | unsigned int to; | |
2125 | boolean phdr; | |
2126 | { | |
2127 | struct elf_segment_map *m; | |
2128 | unsigned int i; | |
2129 | asection **hdrpp; | |
2130 | ||
2131 | m = ((struct elf_segment_map *) | |
2132 | bfd_zalloc (abfd, | |
2133 | (sizeof (struct elf_segment_map) | |
2134 | + (to - from - 1) * sizeof (asection *)))); | |
2135 | if (m == NULL) | |
2136 | return NULL; | |
2137 | m->next = NULL; | |
2138 | m->p_type = PT_LOAD; | |
2139 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
2140 | m->sections[i - from] = *hdrpp; | |
2141 | m->count = to - from; | |
2142 | ||
2143 | if (from == 0 && phdr) | |
2144 | { | |
2145 | /* Include the headers in the first PT_LOAD segment. */ | |
2146 | m->includes_filehdr = 1; | |
2147 | m->includes_phdrs = 1; | |
2148 | } | |
2149 | ||
2150 | return m; | |
2151 | } | |
2152 | ||
2153 | /* Set up a mapping from BFD sections to program segments. */ | |
2154 | ||
2155 | static boolean | |
2156 | map_sections_to_segments (abfd) | |
2157 | bfd *abfd; | |
2158 | { | |
2159 | asection **sections = NULL; | |
2160 | asection *s; | |
2161 | unsigned int i; | |
2162 | unsigned int count; | |
2163 | struct elf_segment_map *mfirst; | |
2164 | struct elf_segment_map **pm; | |
2165 | struct elf_segment_map *m; | |
2166 | asection *last_hdr; | |
2167 | unsigned int phdr_index; | |
2168 | bfd_vma maxpagesize; | |
2169 | asection **hdrpp; | |
2170 | boolean phdr_in_segment = true; | |
2171 | boolean writable; | |
2172 | asection *dynsec; | |
2173 | ||
2174 | if (elf_tdata (abfd)->segment_map != NULL) | |
2175 | return true; | |
2176 | ||
2177 | if (bfd_count_sections (abfd) == 0) | |
2178 | return true; | |
2179 | ||
2180 | /* Select the allocated sections, and sort them. */ | |
2181 | ||
2182 | sections = (asection **) bfd_malloc (bfd_count_sections (abfd) | |
2183 | * sizeof (asection *)); | |
2184 | if (sections == NULL) | |
2185 | goto error_return; | |
2186 | ||
2187 | i = 0; | |
2188 | for (s = abfd->sections; s != NULL; s = s->next) | |
2189 | { | |
2190 | if ((s->flags & SEC_ALLOC) != 0) | |
2191 | { | |
2192 | sections[i] = s; | |
2193 | ++i; | |
2194 | } | |
2195 | } | |
2196 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
2197 | count = i; | |
2198 | ||
2199 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); | |
2200 | ||
2201 | /* Build the mapping. */ | |
2202 | ||
2203 | mfirst = NULL; | |
2204 | pm = &mfirst; | |
2205 | ||
2206 | /* If we have a .interp section, then create a PT_PHDR segment for | |
2207 | the program headers and a PT_INTERP segment for the .interp | |
2208 | section. */ | |
2209 | s = bfd_get_section_by_name (abfd, ".interp"); | |
2210 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
2211 | { | |
2212 | m = ((struct elf_segment_map *) | |
2213 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
2214 | if (m == NULL) | |
2215 | goto error_return; | |
2216 | m->next = NULL; | |
2217 | m->p_type = PT_PHDR; | |
2218 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
2219 | m->p_flags = PF_R | PF_X; | |
2220 | m->p_flags_valid = 1; | |
2221 | m->includes_phdrs = 1; | |
2222 | ||
2223 | *pm = m; | |
2224 | pm = &m->next; | |
2225 | ||
2226 | m = ((struct elf_segment_map *) | |
2227 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
2228 | if (m == NULL) | |
2229 | goto error_return; | |
2230 | m->next = NULL; | |
2231 | m->p_type = PT_INTERP; | |
2232 | m->count = 1; | |
2233 | m->sections[0] = s; | |
2234 | ||
2235 | *pm = m; | |
2236 | pm = &m->next; | |
2237 | } | |
2238 | ||
2239 | /* Look through the sections. We put sections in the same program | |
2240 | segment when the start of the second section can be placed within | |
2241 | a few bytes of the end of the first section. */ | |
2242 | last_hdr = NULL; | |
2243 | phdr_index = 0; | |
2244 | maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
2245 | writable = false; | |
2246 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
2247 | if (dynsec != NULL | |
2248 | && (dynsec->flags & SEC_LOAD) == 0) | |
2249 | dynsec = NULL; | |
2250 | ||
2251 | /* Deal with -Ttext or something similar such that the first section | |
2252 | is not adjacent to the program headers. This is an | |
2253 | approximation, since at this point we don't know exactly how many | |
2254 | program headers we will need. */ | |
2255 | if (count > 0) | |
2256 | { | |
2257 | bfd_size_type phdr_size; | |
2258 | ||
2259 | phdr_size = elf_tdata (abfd)->program_header_size; | |
2260 | if (phdr_size == 0) | |
2261 | phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr; | |
2262 | if ((abfd->flags & D_PAGED) == 0 | |
2263 | || sections[0]->lma < phdr_size | |
2264 | || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) | |
2265 | phdr_in_segment = false; | |
2266 | } | |
2267 | ||
2268 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) | |
2269 | { | |
2270 | asection *hdr; | |
2271 | boolean new_segment; | |
2272 | ||
2273 | hdr = *hdrpp; | |
2274 | ||
2275 | /* See if this section and the last one will fit in the same | |
2276 | segment. */ | |
2277 | ||
2278 | if (last_hdr == NULL) | |
2279 | { | |
2280 | /* If we don't have a segment yet, then we don't need a new | |
2281 | one (we build the last one after this loop). */ | |
2282 | new_segment = false; | |
2283 | } | |
2284 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
2285 | { | |
2286 | /* If this section has a different relation between the | |
2287 | virtual address and the load address, then we need a new | |
2288 | segment. */ | |
2289 | new_segment = true; | |
2290 | } | |
2291 | else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) | |
2292 | < BFD_ALIGN (hdr->lma, maxpagesize)) | |
2293 | { | |
2294 | /* If putting this section in this segment would force us to | |
2295 | skip a page in the segment, then we need a new segment. */ | |
2296 | new_segment = true; | |
2297 | } | |
2298 | else if ((last_hdr->flags & SEC_LOAD) == 0 | |
2299 | && (hdr->flags & SEC_LOAD) != 0) | |
2300 | { | |
2301 | /* We don't want to put a loadable section after a | |
2302 | nonloadable section in the same segment. */ | |
2303 | new_segment = true; | |
2304 | } | |
2305 | else if ((abfd->flags & D_PAGED) == 0) | |
2306 | { | |
2307 | /* If the file is not demand paged, which means that we | |
2308 | don't require the sections to be correctly aligned in the | |
2309 | file, then there is no other reason for a new segment. */ | |
2310 | new_segment = false; | |
2311 | } | |
2312 | else if (! writable | |
2313 | && (hdr->flags & SEC_READONLY) == 0 | |
2314 | && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) | |
2315 | == hdr->lma)) | |
2316 | { | |
2317 | /* We don't want to put a writable section in a read only | |
2318 | segment, unless they are on the same page in memory | |
2319 | anyhow. We already know that the last section does not | |
2320 | bring us past the current section on the page, so the | |
2321 | only case in which the new section is not on the same | |
2322 | page as the previous section is when the previous section | |
2323 | ends precisely on a page boundary. */ | |
2324 | new_segment = true; | |
2325 | } | |
2326 | else | |
2327 | { | |
2328 | /* Otherwise, we can use the same segment. */ | |
2329 | new_segment = false; | |
2330 | } | |
2331 | ||
2332 | if (! new_segment) | |
2333 | { | |
2334 | if ((hdr->flags & SEC_READONLY) == 0) | |
2335 | writable = true; | |
2336 | last_hdr = hdr; | |
2337 | continue; | |
2338 | } | |
2339 | ||
2340 | /* We need a new program segment. We must create a new program | |
2341 | header holding all the sections from phdr_index until hdr. */ | |
2342 | ||
2343 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
2344 | if (m == NULL) | |
2345 | goto error_return; | |
2346 | ||
2347 | *pm = m; | |
2348 | pm = &m->next; | |
2349 | ||
2350 | if ((hdr->flags & SEC_READONLY) == 0) | |
2351 | writable = true; | |
2352 | else | |
2353 | writable = false; | |
2354 | ||
2355 | last_hdr = hdr; | |
2356 | phdr_index = i; | |
2357 | phdr_in_segment = false; | |
2358 | } | |
2359 | ||
2360 | /* Create a final PT_LOAD program segment. */ | |
2361 | if (last_hdr != NULL) | |
2362 | { | |
2363 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
2364 | if (m == NULL) | |
2365 | goto error_return; | |
2366 | ||
2367 | *pm = m; | |
2368 | pm = &m->next; | |
2369 | } | |
2370 | ||
2371 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ | |
2372 | if (dynsec != NULL) | |
2373 | { | |
2374 | m = ((struct elf_segment_map *) | |
2375 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
2376 | if (m == NULL) | |
2377 | goto error_return; | |
2378 | m->next = NULL; | |
2379 | m->p_type = PT_DYNAMIC; | |
2380 | m->count = 1; | |
2381 | m->sections[0] = dynsec; | |
2382 | ||
2383 | *pm = m; | |
2384 | pm = &m->next; | |
2385 | } | |
2386 | ||
2387 | /* For each loadable .note section, add a PT_NOTE segment. We don't | |
2388 | use bfd_get_section_by_name, because if we link together | |
2389 | nonloadable .note sections and loadable .note sections, we will | |
2390 | generate two .note sections in the output file. FIXME: Using | |
2391 | names for section types is bogus anyhow. */ | |
2392 | for (s = abfd->sections; s != NULL; s = s->next) | |
2393 | { | |
2394 | if ((s->flags & SEC_LOAD) != 0 | |
2395 | && strncmp (s->name, ".note", 5) == 0) | |
2396 | { | |
2397 | m = ((struct elf_segment_map *) | |
2398 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
2399 | if (m == NULL) | |
2400 | goto error_return; | |
2401 | m->next = NULL; | |
2402 | m->p_type = PT_NOTE; | |
2403 | m->count = 1; | |
2404 | m->sections[0] = s; | |
2405 | ||
2406 | *pm = m; | |
2407 | pm = &m->next; | |
2408 | } | |
2409 | } | |
2410 | ||
2411 | free (sections); | |
2412 | sections = NULL; | |
2413 | ||
2414 | elf_tdata (abfd)->segment_map = mfirst; | |
2415 | return true; | |
2416 | ||
2417 | error_return: | |
2418 | if (sections != NULL) | |
2419 | free (sections); | |
2420 | return false; | |
2421 | } | |
2422 | ||
2423 | /* Sort sections by address. */ | |
2424 | ||
2425 | static int | |
2426 | elf_sort_sections (arg1, arg2) | |
2427 | const PTR arg1; | |
2428 | const PTR arg2; | |
2429 | { | |
2430 | const asection *sec1 = *(const asection **) arg1; | |
2431 | const asection *sec2 = *(const asection **) arg2; | |
2432 | ||
2433 | /* Sort by LMA first, since this is the address used to | |
2434 | place the section into a segment. */ | |
2435 | if (sec1->lma < sec2->lma) | |
2436 | return -1; | |
2437 | else if (sec1->lma > sec2->lma) | |
2438 | return 1; | |
2439 | ||
2440 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
2441 | the same, and this will do nothing. */ | |
2442 | if (sec1->vma < sec2->vma) | |
2443 | return -1; | |
2444 | else if (sec1->vma > sec2->vma) | |
2445 | return 1; | |
2446 | ||
2447 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
2448 | ||
2449 | #define TOEND(x) (((x)->flags & SEC_LOAD) == 0) | |
2450 | ||
2451 | if (TOEND (sec1)) | |
2452 | { | |
2453 | if (TOEND (sec2)) | |
2454 | return sec1->target_index - sec2->target_index; | |
2455 | else | |
2456 | return 1; | |
2457 | } | |
2458 | ||
2459 | if (TOEND (sec2)) | |
2460 | return -1; | |
2461 | ||
2462 | #undef TOEND | |
2463 | ||
2464 | /* Sort by size, to put zero sized sections before others at the | |
2465 | same address. */ | |
2466 | ||
2467 | if (sec1->_raw_size < sec2->_raw_size) | |
2468 | return -1; | |
2469 | if (sec1->_raw_size > sec2->_raw_size) | |
2470 | return 1; | |
2471 | ||
2472 | return sec1->target_index - sec2->target_index; | |
2473 | } | |
2474 | ||
2475 | /* Assign file positions to the sections based on the mapping from | |
2476 | sections to segments. This function also sets up some fields in | |
2477 | the file header, and writes out the program headers. */ | |
2478 | ||
2479 | static boolean | |
2480 | assign_file_positions_for_segments (abfd) | |
2481 | bfd *abfd; | |
2482 | { | |
2483 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2484 | unsigned int count; | |
2485 | struct elf_segment_map *m; | |
2486 | unsigned int alloc; | |
2487 | Elf_Internal_Phdr *phdrs; | |
2488 | file_ptr off, voff; | |
2489 | bfd_vma filehdr_vaddr, filehdr_paddr; | |
2490 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
2491 | Elf_Internal_Phdr *p; | |
2492 | ||
2493 | if (elf_tdata (abfd)->segment_map == NULL) | |
2494 | { | |
2495 | if (! map_sections_to_segments (abfd)) | |
2496 | return false; | |
2497 | } | |
2498 | ||
2499 | if (bed->elf_backend_modify_segment_map) | |
2500 | { | |
2501 | if (! (*bed->elf_backend_modify_segment_map) (abfd)) | |
2502 | return false; | |
2503 | } | |
2504 | ||
2505 | count = 0; | |
2506 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
2507 | ++count; | |
2508 | ||
2509 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
2510 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
2511 | elf_elfheader (abfd)->e_phnum = count; | |
2512 | ||
2513 | if (count == 0) | |
2514 | return true; | |
2515 | ||
2516 | /* If we already counted the number of program segments, make sure | |
2517 | that we allocated enough space. This happens when SIZEOF_HEADERS | |
2518 | is used in a linker script. */ | |
2519 | alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr; | |
2520 | if (alloc != 0 && count > alloc) | |
2521 | { | |
2522 | ((*_bfd_error_handler) | |
2523 | (_("%s: Not enough room for program headers (allocated %u, need %u)"), | |
2524 | bfd_get_filename (abfd), alloc, count)); | |
2525 | bfd_set_error (bfd_error_bad_value); | |
2526 | return false; | |
2527 | } | |
2528 | ||
2529 | if (alloc == 0) | |
2530 | alloc = count; | |
2531 | ||
2532 | phdrs = ((Elf_Internal_Phdr *) | |
2533 | bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr))); | |
2534 | if (phdrs == NULL) | |
2535 | return false; | |
2536 | ||
2537 | off = bed->s->sizeof_ehdr; | |
2538 | off += alloc * bed->s->sizeof_phdr; | |
2539 | ||
2540 | filehdr_vaddr = 0; | |
2541 | filehdr_paddr = 0; | |
2542 | phdrs_vaddr = 0; | |
2543 | phdrs_paddr = 0; | |
2544 | ||
2545 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
2546 | m != NULL; | |
2547 | m = m->next, p++) | |
2548 | { | |
2549 | unsigned int i; | |
2550 | asection **secpp; | |
2551 | ||
2552 | /* If elf_segment_map is not from map_sections_to_segments, the | |
2553 | sections may not be correctly ordered. */ | |
2554 | if (m->count > 0) | |
2555 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
2556 | elf_sort_sections); | |
2557 | ||
2558 | p->p_type = m->p_type; | |
2559 | ||
2560 | if (m->p_flags_valid) | |
2561 | p->p_flags = m->p_flags; | |
2562 | else | |
2563 | p->p_flags = 0; | |
2564 | ||
2565 | if (p->p_type == PT_LOAD | |
2566 | && m->count > 0 | |
2567 | && (m->sections[0]->flags & SEC_ALLOC) != 0) | |
2568 | { | |
2569 | if ((abfd->flags & D_PAGED) != 0) | |
2570 | off += (m->sections[0]->vma - off) % bed->maxpagesize; | |
2571 | else | |
2572 | { | |
2573 | bfd_size_type align; | |
2574 | ||
2575 | align = 0; | |
2576 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
2577 | { | |
2578 | bfd_size_type secalign; | |
2579 | ||
2580 | secalign = bfd_get_section_alignment (abfd, *secpp); | |
2581 | if (secalign > align) | |
2582 | align = secalign; | |
2583 | } | |
2584 | ||
2585 | off += (m->sections[0]->vma - off) % (1 << align); | |
2586 | } | |
2587 | } | |
2588 | ||
2589 | if (m->count == 0) | |
2590 | p->p_vaddr = 0; | |
2591 | else | |
2592 | p->p_vaddr = m->sections[0]->vma; | |
2593 | ||
2594 | if (m->p_paddr_valid) | |
2595 | p->p_paddr = m->p_paddr; | |
2596 | else if (m->count == 0) | |
2597 | p->p_paddr = 0; | |
2598 | else | |
2599 | p->p_paddr = m->sections[0]->lma; | |
2600 | ||
2601 | if (p->p_type == PT_LOAD | |
2602 | && (abfd->flags & D_PAGED) != 0) | |
2603 | p->p_align = bed->maxpagesize; | |
2604 | else if (m->count == 0) | |
2605 | p->p_align = bed->s->file_align; | |
2606 | else | |
2607 | p->p_align = 0; | |
2608 | ||
2609 | p->p_offset = 0; | |
2610 | p->p_filesz = 0; | |
2611 | p->p_memsz = 0; | |
2612 | ||
2613 | if (m->includes_filehdr) | |
2614 | { | |
2615 | if (! m->p_flags_valid) | |
2616 | p->p_flags |= PF_R; | |
2617 | p->p_offset = 0; | |
2618 | p->p_filesz = bed->s->sizeof_ehdr; | |
2619 | p->p_memsz = bed->s->sizeof_ehdr; | |
2620 | if (m->count > 0) | |
2621 | { | |
2622 | BFD_ASSERT (p->p_type == PT_LOAD); | |
2623 | ||
2624 | if (p->p_vaddr < (bfd_vma) off) | |
2625 | { | |
2626 | _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"), | |
2627 | bfd_get_filename (abfd)); | |
2628 | bfd_set_error (bfd_error_bad_value); | |
2629 | return false; | |
2630 | } | |
2631 | ||
2632 | p->p_vaddr -= off; | |
2633 | if (! m->p_paddr_valid) | |
2634 | p->p_paddr -= off; | |
2635 | } | |
2636 | if (p->p_type == PT_LOAD) | |
2637 | { | |
2638 | filehdr_vaddr = p->p_vaddr; | |
2639 | filehdr_paddr = p->p_paddr; | |
2640 | } | |
2641 | } | |
2642 | ||
2643 | if (m->includes_phdrs) | |
2644 | { | |
2645 | if (! m->p_flags_valid) | |
2646 | p->p_flags |= PF_R; | |
2647 | ||
2648 | if (m->includes_filehdr) | |
2649 | { | |
2650 | if (p->p_type == PT_LOAD) | |
2651 | { | |
2652 | phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr; | |
2653 | phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr; | |
2654 | } | |
2655 | } | |
2656 | else | |
2657 | { | |
2658 | p->p_offset = bed->s->sizeof_ehdr; | |
2659 | ||
2660 | if (m->count > 0) | |
2661 | { | |
2662 | BFD_ASSERT (p->p_type == PT_LOAD); | |
2663 | p->p_vaddr -= off - p->p_offset; | |
2664 | if (! m->p_paddr_valid) | |
2665 | p->p_paddr -= off - p->p_offset; | |
2666 | } | |
2667 | ||
2668 | if (p->p_type == PT_LOAD) | |
2669 | { | |
2670 | phdrs_vaddr = p->p_vaddr; | |
2671 | phdrs_paddr = p->p_paddr; | |
2672 | } | |
2673 | else | |
2674 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
2675 | } | |
2676 | ||
2677 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
2678 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2679 | } | |
2680 | ||
2681 | if (p->p_type == PT_LOAD | |
2682 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
2683 | { | |
2684 | if (! m->includes_filehdr && ! m->includes_phdrs) | |
2685 | p->p_offset = off; | |
2686 | else | |
2687 | { | |
2688 | file_ptr adjust; | |
2689 | ||
2690 | adjust = off - (p->p_offset + p->p_filesz); | |
2691 | p->p_filesz += adjust; | |
2692 | p->p_memsz += adjust; | |
2693 | } | |
2694 | } | |
2695 | ||
2696 | voff = off; | |
2697 | ||
2698 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
2699 | { | |
2700 | asection *sec; | |
2701 | flagword flags; | |
2702 | bfd_size_type align; | |
2703 | ||
2704 | sec = *secpp; | |
2705 | flags = sec->flags; | |
2706 | align = 1 << bfd_get_section_alignment (abfd, sec); | |
2707 | ||
2708 | /* The section may have artificial alignment forced by a | |
2709 | link script. Notice this case by the gap between the | |
2710 | cumulative phdr vma and the section's vma. */ | |
2711 | if (p->p_vaddr + p->p_memsz < sec->vma) | |
2712 | { | |
2713 | bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz); | |
2714 | ||
2715 | p->p_memsz += adjust; | |
2716 | off += adjust; | |
2717 | voff += adjust; | |
2718 | if ((flags & SEC_LOAD) != 0) | |
2719 | p->p_filesz += adjust; | |
2720 | } | |
2721 | ||
2722 | if (p->p_type == PT_LOAD) | |
2723 | { | |
2724 | bfd_signed_vma adjust; | |
2725 | ||
2726 | if ((flags & SEC_LOAD) != 0) | |
2727 | { | |
2728 | adjust = sec->lma - (p->p_paddr + p->p_memsz); | |
2729 | if (adjust < 0) | |
2730 | adjust = 0; | |
2731 | } | |
2732 | else if ((flags & SEC_ALLOC) != 0) | |
2733 | { | |
2734 | /* The section VMA must equal the file position | |
2735 | modulo the page size. FIXME: I'm not sure if | |
2736 | this adjustment is really necessary. We used to | |
2737 | not have the SEC_LOAD case just above, and then | |
2738 | this was necessary, but now I'm not sure. */ | |
2739 | if ((abfd->flags & D_PAGED) != 0) | |
2740 | adjust = (sec->vma - voff) % bed->maxpagesize; | |
2741 | else | |
2742 | adjust = (sec->vma - voff) % align; | |
2743 | } | |
2744 | else | |
2745 | adjust = 0; | |
2746 | ||
2747 | if (adjust != 0) | |
2748 | { | |
2749 | if (i == 0) | |
2750 | { | |
2751 | (* _bfd_error_handler) | |
2752 | (_("Error: First section in segment (%s) starts at 0x%x"), | |
2753 | bfd_section_name (abfd, sec), sec->lma); | |
2754 | (* _bfd_error_handler) | |
2755 | (_(" whereas segment starts at 0x%x"), | |
2756 | p->p_paddr); | |
2757 | ||
2758 | return false; | |
2759 | } | |
2760 | p->p_memsz += adjust; | |
2761 | off += adjust; | |
2762 | voff += adjust; | |
2763 | if ((flags & SEC_LOAD) != 0) | |
2764 | p->p_filesz += adjust; | |
2765 | } | |
2766 | ||
2767 | sec->filepos = off; | |
2768 | ||
2769 | /* We check SEC_HAS_CONTENTS here because if NOLOAD is | |
2770 | used in a linker script we may have a section with | |
2771 | SEC_LOAD clear but which is supposed to have | |
2772 | contents. */ | |
2773 | if ((flags & SEC_LOAD) != 0 | |
2774 | || (flags & SEC_HAS_CONTENTS) != 0) | |
2775 | off += sec->_raw_size; | |
2776 | ||
2777 | if ((flags & SEC_ALLOC) != 0) | |
2778 | voff += sec->_raw_size; | |
2779 | } | |
2780 | ||
2781 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
2782 | { | |
2783 | if (i == 0) /* the actual "note" segment */ | |
2784 | { /* this one actually contains everything. */ | |
2785 | sec->filepos = off; | |
2786 | p->p_filesz = sec->_raw_size; | |
2787 | off += sec->_raw_size; | |
2788 | voff = off; | |
2789 | } | |
2790 | else /* fake sections -- don't need to be written */ | |
2791 | { | |
2792 | sec->filepos = 0; | |
2793 | sec->_raw_size = 0; | |
2794 | flags = sec->flags = 0; /* no contents */ | |
2795 | } | |
2796 | p->p_memsz = 0; | |
2797 | p->p_align = 1; | |
2798 | } | |
2799 | else | |
2800 | { | |
2801 | p->p_memsz += sec->_raw_size; | |
2802 | ||
2803 | if ((flags & SEC_LOAD) != 0) | |
2804 | p->p_filesz += sec->_raw_size; | |
2805 | ||
2806 | if (align > p->p_align | |
2807 | && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0)) | |
2808 | p->p_align = align; | |
2809 | } | |
2810 | ||
2811 | if (! m->p_flags_valid) | |
2812 | { | |
2813 | p->p_flags |= PF_R; | |
2814 | if ((flags & SEC_CODE) != 0) | |
2815 | p->p_flags |= PF_X; | |
2816 | if ((flags & SEC_READONLY) == 0) | |
2817 | p->p_flags |= PF_W; | |
2818 | } | |
2819 | } | |
2820 | } | |
2821 | ||
2822 | /* Now that we have set the section file positions, we can set up | |
2823 | the file positions for the non PT_LOAD segments. */ | |
2824 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
2825 | m != NULL; | |
2826 | m = m->next, p++) | |
2827 | { | |
2828 | if (p->p_type != PT_LOAD && m->count > 0) | |
2829 | { | |
2830 | BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs); | |
2831 | p->p_offset = m->sections[0]->filepos; | |
2832 | } | |
2833 | if (m->count == 0) | |
2834 | { | |
2835 | if (m->includes_filehdr) | |
2836 | { | |
2837 | p->p_vaddr = filehdr_vaddr; | |
2838 | if (! m->p_paddr_valid) | |
2839 | p->p_paddr = filehdr_paddr; | |
2840 | } | |
2841 | else if (m->includes_phdrs) | |
2842 | { | |
2843 | p->p_vaddr = phdrs_vaddr; | |
2844 | if (! m->p_paddr_valid) | |
2845 | p->p_paddr = phdrs_paddr; | |
2846 | } | |
2847 | } | |
2848 | } | |
2849 | ||
2850 | /* Clear out any program headers we allocated but did not use. */ | |
2851 | for (; count < alloc; count++, p++) | |
2852 | { | |
2853 | memset (p, 0, sizeof *p); | |
2854 | p->p_type = PT_NULL; | |
2855 | } | |
2856 | ||
2857 | elf_tdata (abfd)->phdr = phdrs; | |
2858 | ||
2859 | elf_tdata (abfd)->next_file_pos = off; | |
2860 | ||
2861 | /* Write out the program headers. */ | |
2862 | if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
2863 | || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0) | |
2864 | return false; | |
2865 | ||
2866 | return true; | |
2867 | } | |
2868 | ||
2869 | /* Get the size of the program header. | |
2870 | ||
2871 | If this is called by the linker before any of the section VMA's are set, it | |
2872 | can't calculate the correct value for a strange memory layout. This only | |
2873 | happens when SIZEOF_HEADERS is used in a linker script. In this case, | |
2874 | SORTED_HDRS is NULL and we assume the normal scenario of one text and one | |
2875 | data segment (exclusive of .interp and .dynamic). | |
2876 | ||
2877 | ??? User written scripts must either not use SIZEOF_HEADERS, or assume there | |
2878 | will be two segments. */ | |
2879 | ||
2880 | static bfd_size_type | |
2881 | get_program_header_size (abfd) | |
2882 | bfd *abfd; | |
2883 | { | |
2884 | size_t segs; | |
2885 | asection *s; | |
2886 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2887 | ||
2888 | /* We can't return a different result each time we're called. */ | |
2889 | if (elf_tdata (abfd)->program_header_size != 0) | |
2890 | return elf_tdata (abfd)->program_header_size; | |
2891 | ||
2892 | if (elf_tdata (abfd)->segment_map != NULL) | |
2893 | { | |
2894 | struct elf_segment_map *m; | |
2895 | ||
2896 | segs = 0; | |
2897 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
2898 | ++segs; | |
2899 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; | |
2900 | return elf_tdata (abfd)->program_header_size; | |
2901 | } | |
2902 | ||
2903 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
2904 | and one for data. */ | |
2905 | segs = 2; | |
2906 | ||
2907 | s = bfd_get_section_by_name (abfd, ".interp"); | |
2908 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
2909 | { | |
2910 | /* If we have a loadable interpreter section, we need a | |
2911 | PT_INTERP segment. In this case, assume we also need a | |
2912 | PT_PHDR segment, although that may not be true for all | |
2913 | targets. */ | |
2914 | segs += 2; | |
2915 | } | |
2916 | ||
2917 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
2918 | { | |
2919 | /* We need a PT_DYNAMIC segment. */ | |
2920 | ++segs; | |
2921 | } | |
2922 | ||
2923 | for (s = abfd->sections; s != NULL; s = s->next) | |
2924 | { | |
2925 | if ((s->flags & SEC_LOAD) != 0 | |
2926 | && strncmp (s->name, ".note", 5) == 0) | |
2927 | { | |
2928 | /* We need a PT_NOTE segment. */ | |
2929 | ++segs; | |
2930 | } | |
2931 | } | |
2932 | ||
2933 | /* Let the backend count up any program headers it might need. */ | |
2934 | if (bed->elf_backend_additional_program_headers) | |
2935 | { | |
2936 | int a; | |
2937 | ||
2938 | a = (*bed->elf_backend_additional_program_headers) (abfd); | |
2939 | if (a == -1) | |
2940 | abort (); | |
2941 | segs += a; | |
2942 | } | |
2943 | ||
2944 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; | |
2945 | return elf_tdata (abfd)->program_header_size; | |
2946 | } | |
2947 | ||
2948 | /* Work out the file positions of all the sections. This is called by | |
2949 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
2950 | VMAs must be known before this is called. | |
2951 | ||
2952 | We do not consider reloc sections at this point, unless they form | |
2953 | part of the loadable image. Reloc sections are assigned file | |
2954 | positions in assign_file_positions_for_relocs, which is called by | |
2955 | write_object_contents and final_link. | |
2956 | ||
2957 | We also don't set the positions of the .symtab and .strtab here. */ | |
2958 | ||
2959 | static boolean | |
2960 | assign_file_positions_except_relocs (abfd) | |
2961 | bfd *abfd; | |
2962 | { | |
2963 | struct elf_obj_tdata * const tdata = elf_tdata (abfd); | |
2964 | Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); | |
2965 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
2966 | file_ptr off; | |
2967 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2968 | ||
2969 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
2970 | && bfd_get_format (abfd) != bfd_core) | |
2971 | { | |
2972 | Elf_Internal_Shdr **hdrpp; | |
2973 | unsigned int i; | |
2974 | ||
2975 | /* Start after the ELF header. */ | |
2976 | off = i_ehdrp->e_ehsize; | |
2977 | ||
2978 | /* We are not creating an executable, which means that we are | |
2979 | not creating a program header, and that the actual order of | |
2980 | the sections in the file is unimportant. */ | |
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->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
2987 | { | |
2988 | hdr->sh_offset = -1; | |
2989 | continue; | |
2990 | } | |
2991 | if (i == tdata->symtab_section | |
2992 | || i == tdata->strtab_section) | |
2993 | { | |
2994 | hdr->sh_offset = -1; | |
2995 | continue; | |
2996 | } | |
2997 | ||
2998 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
2999 | } | |
3000 | } | |
3001 | else | |
3002 | { | |
3003 | unsigned int i; | |
3004 | Elf_Internal_Shdr **hdrpp; | |
3005 | ||
3006 | /* Assign file positions for the loaded sections based on the | |
3007 | assignment of sections to segments. */ | |
3008 | if (! assign_file_positions_for_segments (abfd)) | |
3009 | return false; | |
3010 | ||
3011 | /* Assign file positions for the other sections. */ | |
3012 | ||
3013 | off = elf_tdata (abfd)->next_file_pos; | |
3014 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
3015 | { | |
3016 | Elf_Internal_Shdr *hdr; | |
3017 | ||
3018 | hdr = *hdrpp; | |
3019 | if (hdr->bfd_section != NULL | |
3020 | && hdr->bfd_section->filepos != 0) | |
3021 | hdr->sh_offset = hdr->bfd_section->filepos; | |
3022 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
3023 | { | |
3024 | ((*_bfd_error_handler) | |
3025 | (_("%s: warning: allocated section `%s' not in segment"), | |
3026 | bfd_get_filename (abfd), | |
3027 | (hdr->bfd_section == NULL | |
3028 | ? "*unknown*" | |
3029 | : hdr->bfd_section->name))); | |
3030 | if ((abfd->flags & D_PAGED) != 0) | |
3031 | off += (hdr->sh_addr - off) % bed->maxpagesize; | |
3032 | else | |
3033 | off += (hdr->sh_addr - off) % hdr->sh_addralign; | |
3034 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
3035 | false); | |
3036 | } | |
3037 | else if (hdr->sh_type == SHT_REL | |
3038 | || hdr->sh_type == SHT_RELA | |
3039 | || hdr == i_shdrpp[tdata->symtab_section] | |
3040 | || hdr == i_shdrpp[tdata->strtab_section]) | |
3041 | hdr->sh_offset = -1; | |
3042 | else | |
3043 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
3044 | } | |
3045 | } | |
3046 | ||
3047 | /* Place the section headers. */ | |
3048 | off = align_file_position (off, bed->s->file_align); | |
3049 | i_ehdrp->e_shoff = off; | |
3050 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
3051 | ||
3052 | elf_tdata (abfd)->next_file_pos = off; | |
3053 | ||
3054 | return true; | |
3055 | } | |
3056 | ||
3057 | static boolean | |
3058 | prep_headers (abfd) | |
3059 | bfd *abfd; | |
3060 | { | |
3061 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
3062 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
3063 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
3064 | int count; | |
3065 | struct bfd_strtab_hash *shstrtab; | |
3066 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3067 | ||
3068 | i_ehdrp = elf_elfheader (abfd); | |
3069 | i_shdrp = elf_elfsections (abfd); | |
3070 | ||
3071 | shstrtab = _bfd_elf_stringtab_init (); | |
3072 | if (shstrtab == NULL) | |
3073 | return false; | |
3074 | ||
3075 | elf_shstrtab (abfd) = shstrtab; | |
3076 | ||
3077 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
3078 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
3079 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
3080 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
3081 | ||
3082 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
3083 | i_ehdrp->e_ident[EI_DATA] = | |
3084 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
3085 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
3086 | ||
e6c51ed4 NC |
3087 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_SYSV; |
3088 | i_ehdrp->e_ident[EI_ABIVERSION] = 0; | |
3089 | ||
252b5132 RH |
3090 | for (count = EI_PAD; count < EI_NIDENT; count++) |
3091 | i_ehdrp->e_ident[count] = 0; | |
3092 | ||
3093 | if ((abfd->flags & DYNAMIC) != 0) | |
3094 | i_ehdrp->e_type = ET_DYN; | |
3095 | else if ((abfd->flags & EXEC_P) != 0) | |
3096 | i_ehdrp->e_type = ET_EXEC; | |
3097 | else if (bfd_get_format (abfd) == bfd_core) | |
3098 | i_ehdrp->e_type = ET_CORE; | |
3099 | else | |
3100 | i_ehdrp->e_type = ET_REL; | |
3101 | ||
3102 | switch (bfd_get_arch (abfd)) | |
3103 | { | |
3104 | case bfd_arch_unknown: | |
3105 | i_ehdrp->e_machine = EM_NONE; | |
3106 | break; | |
3107 | case bfd_arch_sparc: | |
3108 | if (bed->s->arch_size == 64) | |
3109 | i_ehdrp->e_machine = EM_SPARCV9; | |
3110 | else | |
3111 | i_ehdrp->e_machine = EM_SPARC; | |
3112 | break; | |
3113 | case bfd_arch_i386: | |
3114 | i_ehdrp->e_machine = EM_386; | |
3115 | break; | |
3116 | case bfd_arch_m68k: | |
3117 | i_ehdrp->e_machine = EM_68K; | |
3118 | break; | |
3119 | case bfd_arch_m88k: | |
3120 | i_ehdrp->e_machine = EM_88K; | |
3121 | break; | |
3122 | case bfd_arch_i860: | |
3123 | i_ehdrp->e_machine = EM_860; | |
3124 | break; | |
b2ef150d ILT |
3125 | case bfd_arch_i960: |
3126 | i_ehdrp->e_machine = EM_960; | |
3127 | break; | |
252b5132 RH |
3128 | case bfd_arch_mips: /* MIPS Rxxxx */ |
3129 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
3130 | break; | |
3131 | case bfd_arch_hppa: | |
3132 | i_ehdrp->e_machine = EM_PARISC; | |
3133 | break; | |
3134 | case bfd_arch_powerpc: | |
3135 | i_ehdrp->e_machine = EM_PPC; | |
3136 | break; | |
3137 | case bfd_arch_alpha: | |
3138 | i_ehdrp->e_machine = EM_ALPHA; | |
3139 | break; | |
3140 | case bfd_arch_sh: | |
3141 | i_ehdrp->e_machine = EM_SH; | |
3142 | break; | |
3143 | case bfd_arch_d10v: | |
3144 | i_ehdrp->e_machine = EM_CYGNUS_D10V; | |
3145 | break; | |
3146 | case bfd_arch_d30v: | |
3147 | i_ehdrp->e_machine = EM_CYGNUS_D30V; | |
3148 | break; | |
3149 | case bfd_arch_fr30: | |
3150 | i_ehdrp->e_machine = EM_CYGNUS_FR30; | |
3151 | break; | |
3152 | case bfd_arch_mcore: | |
3153 | i_ehdrp->e_machine = EM_MCORE; | |
3154 | break; | |
3155 | case bfd_arch_v850: | |
3156 | switch (bfd_get_mach (abfd)) | |
3157 | { | |
3158 | default: | |
3159 | case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break; | |
3160 | } | |
3161 | break; | |
3162 | case bfd_arch_arc: | |
3163 | i_ehdrp->e_machine = EM_CYGNUS_ARC; | |
3164 | break; | |
3165 | case bfd_arch_arm: | |
3166 | i_ehdrp->e_machine = EM_ARM; | |
3167 | break; | |
3168 | case bfd_arch_m32r: | |
3169 | i_ehdrp->e_machine = EM_CYGNUS_M32R; | |
3170 | break; | |
3171 | case bfd_arch_mn10200: | |
3172 | i_ehdrp->e_machine = EM_CYGNUS_MN10200; | |
3173 | break; | |
3174 | case bfd_arch_mn10300: | |
3175 | i_ehdrp->e_machine = EM_CYGNUS_MN10300; | |
3176 | break; | |
3177 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ | |
3178 | default: | |
3179 | i_ehdrp->e_machine = EM_NONE; | |
3180 | } | |
3181 | i_ehdrp->e_version = bed->s->ev_current; | |
3182 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
3183 | ||
3184 | /* no program header, for now. */ | |
3185 | i_ehdrp->e_phoff = 0; | |
3186 | i_ehdrp->e_phentsize = 0; | |
3187 | i_ehdrp->e_phnum = 0; | |
3188 | ||
3189 | /* each bfd section is section header entry */ | |
3190 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
3191 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
3192 | ||
3193 | /* if we're building an executable, we'll need a program header table */ | |
3194 | if (abfd->flags & EXEC_P) | |
3195 | { | |
3196 | /* it all happens later */ | |
3197 | #if 0 | |
3198 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
3199 | ||
3200 | /* elf_build_phdrs() returns a (NULL-terminated) array of | |
3201 | Elf_Internal_Phdrs */ | |
3202 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
3203 | i_ehdrp->e_phoff = outbase; | |
3204 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
3205 | #endif | |
3206 | } | |
3207 | else | |
3208 | { | |
3209 | i_ehdrp->e_phentsize = 0; | |
3210 | i_phdrp = 0; | |
3211 | i_ehdrp->e_phoff = 0; | |
3212 | } | |
3213 | ||
3214 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
3215 | (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false); | |
3216 | elf_tdata (abfd)->strtab_hdr.sh_name = | |
3217 | (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false); | |
3218 | elf_tdata (abfd)->shstrtab_hdr.sh_name = | |
3219 | (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false); | |
3220 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
3221 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
3222 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
3223 | return false; | |
3224 | ||
3225 | return true; | |
3226 | } | |
3227 | ||
3228 | /* Assign file positions for all the reloc sections which are not part | |
3229 | of the loadable file image. */ | |
3230 | ||
3231 | void | |
3232 | _bfd_elf_assign_file_positions_for_relocs (abfd) | |
3233 | bfd *abfd; | |
3234 | { | |
3235 | file_ptr off; | |
3236 | unsigned int i; | |
3237 | Elf_Internal_Shdr **shdrpp; | |
3238 | ||
3239 | off = elf_tdata (abfd)->next_file_pos; | |
3240 | ||
3241 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; | |
3242 | i < elf_elfheader (abfd)->e_shnum; | |
3243 | i++, shdrpp++) | |
3244 | { | |
3245 | Elf_Internal_Shdr *shdrp; | |
3246 | ||
3247 | shdrp = *shdrpp; | |
3248 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
3249 | && shdrp->sh_offset == -1) | |
3250 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); | |
3251 | } | |
3252 | ||
3253 | elf_tdata (abfd)->next_file_pos = off; | |
3254 | } | |
3255 | ||
3256 | boolean | |
3257 | _bfd_elf_write_object_contents (abfd) | |
3258 | bfd *abfd; | |
3259 | { | |
3260 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3261 | Elf_Internal_Ehdr *i_ehdrp; | |
3262 | Elf_Internal_Shdr **i_shdrp; | |
3263 | boolean failed; | |
3264 | unsigned int count; | |
3265 | ||
3266 | if (! abfd->output_has_begun | |
3267 | && ! _bfd_elf_compute_section_file_positions | |
3268 | (abfd, (struct bfd_link_info *) NULL)) | |
3269 | return false; | |
3270 | ||
3271 | i_shdrp = elf_elfsections (abfd); | |
3272 | i_ehdrp = elf_elfheader (abfd); | |
3273 | ||
3274 | failed = false; | |
3275 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); | |
3276 | if (failed) | |
3277 | return false; | |
3278 | ||
3279 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
3280 | ||
3281 | /* After writing the headers, we need to write the sections too... */ | |
3282 | for (count = 1; count < i_ehdrp->e_shnum; count++) | |
3283 | { | |
3284 | if (bed->elf_backend_section_processing) | |
3285 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
3286 | if (i_shdrp[count]->contents) | |
3287 | { | |
3288 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 | |
3289 | || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, | |
3290 | 1, abfd) | |
3291 | != i_shdrp[count]->sh_size)) | |
3292 | return false; | |
3293 | } | |
3294 | } | |
3295 | ||
3296 | /* Write out the section header names. */ | |
3297 | if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
3298 | || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd))) | |
3299 | return false; | |
3300 | ||
3301 | if (bed->elf_backend_final_write_processing) | |
3302 | (*bed->elf_backend_final_write_processing) (abfd, | |
3303 | elf_tdata (abfd)->linker); | |
3304 | ||
3305 | return bed->s->write_shdrs_and_ehdr (abfd); | |
3306 | } | |
3307 | ||
3308 | boolean | |
3309 | _bfd_elf_write_corefile_contents (abfd) | |
3310 | bfd *abfd; | |
3311 | { | |
3312 | /* Hopefully this can be done just like an object file. */ | |
3313 | return _bfd_elf_write_object_contents (abfd); | |
3314 | } | |
3315 | /* given a section, search the header to find them... */ | |
3316 | int | |
3317 | _bfd_elf_section_from_bfd_section (abfd, asect) | |
3318 | bfd *abfd; | |
3319 | struct sec *asect; | |
3320 | { | |
3321 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3322 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); | |
3323 | int index; | |
3324 | Elf_Internal_Shdr *hdr; | |
3325 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
3326 | ||
3327 | for (index = 0; index < maxindex; index++) | |
3328 | { | |
3329 | hdr = i_shdrp[index]; | |
3330 | if (hdr->bfd_section == asect) | |
3331 | return index; | |
3332 | } | |
3333 | ||
3334 | if (bed->elf_backend_section_from_bfd_section) | |
3335 | { | |
3336 | for (index = 0; index < maxindex; index++) | |
3337 | { | |
3338 | int retval; | |
3339 | ||
3340 | hdr = i_shdrp[index]; | |
3341 | retval = index; | |
3342 | if ((*bed->elf_backend_section_from_bfd_section) | |
3343 | (abfd, hdr, asect, &retval)) | |
3344 | return retval; | |
3345 | } | |
3346 | } | |
3347 | ||
3348 | if (bfd_is_abs_section (asect)) | |
3349 | return SHN_ABS; | |
3350 | if (bfd_is_com_section (asect)) | |
3351 | return SHN_COMMON; | |
3352 | if (bfd_is_und_section (asect)) | |
3353 | return SHN_UNDEF; | |
3354 | ||
3355 | bfd_set_error (bfd_error_nonrepresentable_section); | |
3356 | ||
3357 | return -1; | |
3358 | } | |
3359 | ||
3360 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
3361 | on error. */ | |
3362 | ||
3363 | int | |
3364 | _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) | |
3365 | bfd *abfd; | |
3366 | asymbol **asym_ptr_ptr; | |
3367 | { | |
3368 | asymbol *asym_ptr = *asym_ptr_ptr; | |
3369 | int idx; | |
3370 | flagword flags = asym_ptr->flags; | |
3371 | ||
3372 | /* When gas creates relocations against local labels, it creates its | |
3373 | own symbol for the section, but does put the symbol into the | |
3374 | symbol chain, so udata is 0. When the linker is generating | |
3375 | relocatable output, this section symbol may be for one of the | |
3376 | input sections rather than the output section. */ | |
3377 | if (asym_ptr->udata.i == 0 | |
3378 | && (flags & BSF_SECTION_SYM) | |
3379 | && asym_ptr->section) | |
3380 | { | |
3381 | int indx; | |
3382 | ||
3383 | if (asym_ptr->section->output_section != NULL) | |
3384 | indx = asym_ptr->section->output_section->index; | |
3385 | else | |
3386 | indx = asym_ptr->section->index; | |
3387 | if (elf_section_syms (abfd)[indx]) | |
3388 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; | |
3389 | } | |
3390 | ||
3391 | idx = asym_ptr->udata.i; | |
3392 | ||
3393 | if (idx == 0) | |
3394 | { | |
3395 | /* This case can occur when using --strip-symbol on a symbol | |
3396 | which is used in a relocation entry. */ | |
3397 | (*_bfd_error_handler) | |
3398 | (_("%s: symbol `%s' required but not present"), | |
3399 | bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr)); | |
3400 | bfd_set_error (bfd_error_no_symbols); | |
3401 | return -1; | |
3402 | } | |
3403 | ||
3404 | #if DEBUG & 4 | |
3405 | { | |
3406 | fprintf (stderr, | |
3407 | _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"), | |
3408 | (long) asym_ptr, asym_ptr->name, idx, flags, | |
3409 | elf_symbol_flags (flags)); | |
3410 | fflush (stderr); | |
3411 | } | |
3412 | #endif | |
3413 | ||
3414 | return idx; | |
3415 | } | |
3416 | ||
3417 | /* Copy private BFD data. This copies any program header information. */ | |
3418 | ||
3419 | static boolean | |
3420 | copy_private_bfd_data (ibfd, obfd) | |
3421 | bfd *ibfd; | |
3422 | bfd *obfd; | |
3423 | { | |
3424 | Elf_Internal_Ehdr *iehdr; | |
3425 | struct elf_segment_map *mfirst; | |
3426 | struct elf_segment_map **pm; | |
3427 | struct elf_segment_map *m; | |
3428 | Elf_Internal_Phdr *p; | |
3429 | unsigned int i; | |
3430 | unsigned int num_segments; | |
3431 | boolean phdr_included = false; | |
3432 | ||
3433 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
3434 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
3435 | return true; | |
3436 | ||
3437 | if (elf_tdata (ibfd)->phdr == NULL) | |
3438 | return true; | |
3439 | ||
3440 | iehdr = elf_elfheader (ibfd); | |
3441 | ||
3442 | mfirst = NULL; | |
3443 | pm = &mfirst; | |
3444 | ||
3445 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
3446 | ||
3447 | #define IS_CONTAINED_BY(addr, len, bottom, phdr) \ | |
3448 | ((addr) >= (bottom) \ | |
3449 | && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \ | |
3450 | || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz))) | |
3451 | ||
3452 | /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */ | |
3453 | ||
3454 | #define IS_COREFILE_NOTE(p, s) \ | |
3455 | (p->p_type == PT_NOTE \ | |
3456 | && bfd_get_format (ibfd) == bfd_core \ | |
3457 | && s->vma == 0 && s->lma == 0 \ | |
3458 | && (bfd_vma) s->filepos >= p->p_offset \ | |
3459 | && (bfd_vma) s->filepos + s->_raw_size \ | |
3460 | <= p->p_offset + p->p_filesz) | |
3461 | ||
3462 | /* The complicated case when p_vaddr is 0 is to handle the Solaris | |
3463 | linker, which generates a PT_INTERP section with p_vaddr and | |
3464 | p_memsz set to 0. */ | |
3465 | ||
3466 | #define IS_SOLARIS_PT_INTERP(p, s) \ | |
3467 | (p->p_vaddr == 0 \ | |
3468 | && p->p_filesz > 0 \ | |
3469 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
3470 | && s->_raw_size > 0 \ | |
3471 | && (bfd_vma) s->filepos >= p->p_offset \ | |
3472 | && ((bfd_vma) s->filepos + s->_raw_size \ | |
3473 | <= p->p_offset + p->p_filesz)) | |
3474 | ||
3475 | /* Scan through the segments specified in the program header | |
3476 | of the input BFD. */ | |
3477 | for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++) | |
3478 | { | |
3479 | unsigned int csecs; | |
3480 | asection *s; | |
3481 | asection **sections; | |
3482 | asection *os; | |
3483 | unsigned int isec; | |
3484 | bfd_vma matching_lma; | |
3485 | bfd_vma suggested_lma; | |
3486 | unsigned int j; | |
3487 | ||
3488 | /* For each section in the input BFD, decide if it should be | |
3489 | included in the current segment. A section will be included | |
3490 | if it is within the address space of the segment, and it is | |
3491 | an allocated segment, and there is an output section | |
3492 | associated with it. */ | |
3493 | csecs = 0; | |
3494 | for (s = ibfd->sections; s != NULL; s = s->next) | |
3495 | if (s->output_section != NULL) | |
3496 | { | |
3497 | if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p) | |
3498 | || IS_SOLARIS_PT_INTERP (p, s)) | |
3499 | && (s->flags & SEC_ALLOC) != 0) | |
3500 | ++csecs; | |
3501 | else if (IS_COREFILE_NOTE (p, s)) | |
3502 | ++csecs; | |
3503 | } | |
3504 | ||
3505 | /* Allocate a segment map big enough to contain all of the | |
3506 | sections we have selected. */ | |
3507 | m = ((struct elf_segment_map *) | |
3508 | bfd_alloc (obfd, | |
3509 | (sizeof (struct elf_segment_map) | |
3510 | + ((size_t) csecs - 1) * sizeof (asection *)))); | |
3511 | if (m == NULL) | |
3512 | return false; | |
3513 | ||
3514 | /* Initialise the fields of the segment map. Default to | |
3515 | using the physical address of the segment in the input BFD. */ | |
3516 | m->next = NULL; | |
3517 | m->p_type = p->p_type; | |
3518 | m->p_flags = p->p_flags; | |
3519 | m->p_flags_valid = 1; | |
3520 | m->p_paddr = p->p_paddr; | |
3521 | m->p_paddr_valid = 1; | |
3522 | ||
3523 | /* Determine if this segment contains the ELF file header | |
3524 | and if it contains the program headers themselves. */ | |
3525 | m->includes_filehdr = (p->p_offset == 0 | |
3526 | && p->p_filesz >= iehdr->e_ehsize); | |
3527 | ||
3528 | m->includes_phdrs = 0; | |
3529 | ||
3530 | if (! phdr_included || p->p_type != PT_LOAD) | |
3531 | { | |
3532 | m->includes_phdrs = | |
3533 | (p->p_offset <= (bfd_vma) iehdr->e_phoff | |
3534 | && (p->p_offset + p->p_filesz | |
3535 | >= ((bfd_vma) iehdr->e_phoff | |
3536 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
3537 | if (p->p_type == PT_LOAD && m->includes_phdrs) | |
3538 | phdr_included = true; | |
3539 | } | |
3540 | ||
3541 | if (csecs == 0) | |
3542 | { | |
3543 | /* Special segments, such as the PT_PHDR segment, may contain | |
3544 | no sections, but ordinary, loadable segments should contain | |
3545 | something. */ | |
3546 | ||
3547 | if (p->p_type == PT_LOAD) | |
3548 | _bfd_error_handler | |
3549 | (_("%s: warning: Empty loadable segment detected\n"), | |
3550 | bfd_get_filename (ibfd)); | |
3551 | ||
3552 | m->count = 0; | |
3553 | *pm = m; | |
3554 | pm = &m->next; | |
3555 | ||
3556 | continue; | |
3557 | } | |
3558 | ||
3559 | /* Now scan the sections in the input BFD again and attempt | |
3560 | to add their corresponding output sections to the segment map. | |
3561 | The problem here is how to handle an output section which has | |
3562 | been moved (ie had its LMA changed). There are four possibilities: | |
3563 | ||
3564 | 1. None of the sections have been moved. | |
3565 | In this case we can continue to use the segment LMA from the | |
3566 | input BFD. | |
3567 | ||
3568 | 2. All of the sections have been moved by the same amount. | |
3569 | In this case we can change the segment's LMA to match the LMA | |
3570 | of the first section. | |
3571 | ||
3572 | 3. Some of the sections have been moved, others have not. | |
3573 | In this case those sections which have not been moved can be | |
3574 | placed in the current segment which will have to have its size, | |
3575 | and possibly its LMA changed, and a new segment or segments will | |
3576 | have to be created to contain the other sections. | |
3577 | ||
3578 | 4. The sections have been moved, but not be the same amount. | |
3579 | In this case we can change the segment's LMA to match the LMA | |
3580 | of the first section and we will have to create a new segment | |
3581 | or segments to contain the other sections. | |
3582 | ||
3583 | In order to save time, we allocate an array to hold the section | |
3584 | pointers that we are interested in. As these sections get assigned | |
3585 | to a segment, they are removed from this array. */ | |
3586 | ||
3587 | sections = (asection **) bfd_malloc (sizeof (asection *) * csecs); | |
3588 | if (sections == NULL) | |
3589 | return false; | |
3590 | ||
3591 | /* Step One: Scan for segment vs section LMA conflicts. | |
3592 | Also add the sections to the section array allocated above. | |
3593 | Also add the sections to the current segment. In the common | |
3594 | case, where the sections have not been moved, this means that | |
3595 | we have completely filled the segment, and there is nothing | |
3596 | more to do. */ | |
3597 | ||
3598 | isec = 0; | |
3599 | matching_lma = false; | |
3600 | suggested_lma = 0; | |
3601 | ||
3602 | for (j = 0, s = ibfd->sections; s != NULL; s = s->next) | |
3603 | { | |
3604 | os = s->output_section; | |
3605 | ||
3606 | if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p) | |
3607 | || IS_SOLARIS_PT_INTERP (p, s)) | |
3608 | && (s->flags & SEC_ALLOC) != 0) | |
3609 | || IS_COREFILE_NOTE (p, s)) | |
3610 | && os != NULL) | |
3611 | { | |
3612 | sections[j++] = s; | |
3613 | ||
3614 | /* The Solaris native linker always sets p_paddr to 0. | |
3615 | We try to catch that case here, and set it to the | |
3616 | correct value. */ | |
3617 | if (p->p_paddr == 0 | |
3618 | && p->p_vaddr != 0 | |
3619 | && isec == 0 | |
3620 | && os->lma != 0 | |
3621 | && (os->vma == (p->p_vaddr | |
3622 | + (m->includes_filehdr | |
3623 | ? iehdr->e_ehsize | |
3624 | : 0) | |
3625 | + (m->includes_phdrs | |
3626 | ? iehdr->e_phnum * iehdr->e_phentsize | |
3627 | : 0)))) | |
3628 | m->p_paddr = p->p_vaddr; | |
3629 | ||
3630 | /* Match up the physical address of the segment with the | |
3631 | LMA address of the output section. */ | |
3632 | if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p) | |
3633 | || IS_COREFILE_NOTE (p, s)) | |
3634 | { | |
3635 | if (matching_lma == 0) | |
3636 | matching_lma = os->lma; | |
3637 | ||
3638 | /* We assume that if the section fits within the segment | |
3639 | that it does not overlap any other section within that | |
3640 | segment. */ | |
3641 | m->sections[isec++] = os; | |
3642 | } | |
3643 | else if (suggested_lma == 0) | |
3644 | suggested_lma = os->lma; | |
3645 | } | |
3646 | } | |
3647 | ||
3648 | BFD_ASSERT (j == csecs); | |
3649 | ||
3650 | /* Step Two: Adjust the physical address of the current segment, | |
3651 | if necessary. */ | |
3652 | if (isec == csecs) | |
3653 | { | |
3654 | /* All of the sections fitted within the segment as currently | |
3655 | specified. This is the default case. Add the segment to | |
3656 | the list of built segments and carry on to process the next | |
3657 | program header in the input BFD. */ | |
3658 | m->count = csecs; | |
3659 | *pm = m; | |
3660 | pm = &m->next; | |
3661 | ||
3662 | free (sections); | |
3663 | continue; | |
3664 | } | |
3665 | else if (matching_lma != 0) | |
3666 | { | |
3667 | /* At least one section fits inside the current segment. | |
3668 | Keep it, but modify its physical address to match the | |
3669 | LMA of the first section that fitted. */ | |
3670 | ||
3671 | m->p_paddr = matching_lma; | |
3672 | } | |
3673 | else | |
3674 | { | |
3675 | /* None of the sections fitted inside the current segment. | |
3676 | Change the current segment's physical address to match | |
3677 | the LMA of the first section. */ | |
3678 | ||
3679 | m->p_paddr = suggested_lma; | |
3680 | } | |
3681 | ||
3682 | /* Step Three: Loop over the sections again, this time assigning | |
3683 | those that fit to the current segment and remvoing them from the | |
3684 | sections array; but making sure not to leave large gaps. Once all | |
3685 | possible sections have been assigned to the current segment it is | |
3686 | added to the list of built segments and if sections still remain | |
3687 | to be assigned, a new segment is constructed before repeating | |
3688 | the loop. */ | |
3689 | isec = 0; | |
3690 | do | |
3691 | { | |
3692 | m->count = 0; | |
3693 | suggested_lma = 0; | |
3694 | ||
3695 | /* Fill the current segment with sections that fit. */ | |
3696 | for (j = 0; j < csecs; j++) | |
3697 | { | |
3698 | s = sections[j]; | |
3699 | ||
3700 | if (s == NULL) | |
3701 | continue; | |
3702 | ||
3703 | os = s->output_section; | |
3704 | ||
3705 | if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p) | |
3706 | || IS_COREFILE_NOTE (p, s)) | |
3707 | { | |
3708 | if (m->count == 0) | |
3709 | { | |
3710 | /* If the first section in a segment does not start at | |
3711 | the beginning of the segment, then something is wrong. */ | |
3712 | if (os->lma != m->p_paddr) | |
3713 | abort (); | |
3714 | } | |
3715 | else | |
3716 | { | |
3717 | asection * prev_sec; | |
3718 | bfd_vma maxpagesize; | |
3719 | ||
3720 | prev_sec = m->sections[m->count - 1]; | |
3721 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; | |
3722 | ||
3723 | /* If the gap between the end of the previous section | |
3724 | and the start of this section is more than maxpagesize | |
3725 | then we need to start a new segment. */ | |
3726 | if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize) | |
3727 | < BFD_ALIGN (os->lma, maxpagesize)) | |
3728 | { | |
3729 | if (suggested_lma == 0) | |
3730 | suggested_lma = os->lma; | |
3731 | ||
3732 | continue; | |
3733 | } | |
3734 | } | |
3735 | ||
3736 | m->sections[m->count++] = os; | |
3737 | ++isec; | |
3738 | sections[j] = NULL; | |
3739 | } | |
3740 | else if (suggested_lma == 0) | |
3741 | suggested_lma = os->lma; | |
3742 | } | |
3743 | ||
3744 | BFD_ASSERT (m->count > 0); | |
3745 | ||
3746 | /* Add the current segment to the list of built segments. */ | |
3747 | *pm = m; | |
3748 | pm = &m->next; | |
3749 | ||
3750 | if (isec < csecs) | |
3751 | { | |
3752 | /* We still have not allocated all of the sections to | |
3753 | segments. Create a new segment here, initialise it | |
3754 | and carry on looping. */ | |
3755 | ||
3756 | m = ((struct elf_segment_map *) | |
3757 | bfd_alloc (obfd, | |
3758 | (sizeof (struct elf_segment_map) | |
3759 | + ((size_t) csecs - 1) * sizeof (asection *)))); | |
3760 | if (m == NULL) | |
3761 | return false; | |
3762 | ||
3763 | /* Initialise the fields of the segment map. Set the physical | |
3764 | physical address to the LMA of the first section that has | |
3765 | not yet been assigned. */ | |
3766 | ||
3767 | m->next = NULL; | |
3768 | m->p_type = p->p_type; | |
3769 | m->p_flags = p->p_flags; | |
3770 | m->p_flags_valid = 1; | |
3771 | m->p_paddr = suggested_lma; | |
3772 | m->p_paddr_valid = 1; | |
3773 | m->includes_filehdr = 0; | |
3774 | m->includes_phdrs = 0; | |
3775 | } | |
3776 | } | |
3777 | while (isec < csecs); | |
3778 | ||
3779 | free (sections); | |
3780 | } | |
3781 | ||
3782 | /* The Solaris linker creates program headers in which all the | |
3783 | p_paddr fields are zero. When we try to objcopy or strip such a | |
3784 | file, we get confused. Check for this case, and if we find it | |
3785 | reset the p_paddr_valid fields. */ | |
3786 | for (m = mfirst; m != NULL; m = m->next) | |
3787 | if (m->p_paddr != 0) | |
3788 | break; | |
3789 | if (m == NULL) | |
3790 | { | |
3791 | for (m = mfirst; m != NULL; m = m->next) | |
3792 | m->p_paddr_valid = 0; | |
3793 | } | |
3794 | ||
3795 | elf_tdata (obfd)->segment_map = mfirst; | |
3796 | ||
3797 | #if 0 | |
3798 | /* Final Step: Sort the segments into ascending order of physical address. */ | |
3799 | if (mfirst != NULL) | |
3800 | { | |
3801 | struct elf_segment_map* prev; | |
3802 | ||
3803 | prev = mfirst; | |
3804 | for (m = mfirst->next; m != NULL; prev = m, m = m->next) | |
3805 | { | |
3806 | /* Yes I know - its a bubble sort....*/ | |
3807 | if (m->next != NULL && (m->next->p_paddr < m->p_paddr)) | |
3808 | { | |
3809 | /* swap m and m->next */ | |
3810 | prev->next = m->next; | |
3811 | m->next = m->next->next; | |
3812 | prev->next->next = m; | |
3813 | ||
3814 | /* restart loop. */ | |
3815 | m = mfirst; | |
3816 | } | |
3817 | } | |
3818 | } | |
3819 | #endif | |
3820 | ||
3821 | #undef IS_CONTAINED_BY | |
3822 | #undef IS_SOLARIS_PT_INTERP | |
3823 | #undef IS_COREFILE_NOTE | |
3824 | return true; | |
3825 | } | |
3826 | ||
3827 | /* Copy private section information. This copies over the entsize | |
3828 | field, and sometimes the info field. */ | |
3829 | ||
3830 | boolean | |
3831 | _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) | |
3832 | bfd *ibfd; | |
3833 | asection *isec; | |
3834 | bfd *obfd; | |
3835 | asection *osec; | |
3836 | { | |
3837 | Elf_Internal_Shdr *ihdr, *ohdr; | |
3838 | ||
3839 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
3840 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
3841 | return true; | |
3842 | ||
3843 | /* Copy over private BFD data if it has not already been copied. | |
3844 | This must be done here, rather than in the copy_private_bfd_data | |
3845 | entry point, because the latter is called after the section | |
3846 | contents have been set, which means that the program headers have | |
3847 | already been worked out. */ | |
3848 | if (elf_tdata (obfd)->segment_map == NULL | |
3849 | && elf_tdata (ibfd)->phdr != NULL) | |
3850 | { | |
3851 | asection *s; | |
3852 | ||
3853 | /* Only set up the segments if there are no more SEC_ALLOC | |
3854 | sections. FIXME: This won't do the right thing if objcopy is | |
3855 | used to remove the last SEC_ALLOC section, since objcopy | |
3856 | won't call this routine in that case. */ | |
3857 | for (s = isec->next; s != NULL; s = s->next) | |
3858 | if ((s->flags & SEC_ALLOC) != 0) | |
3859 | break; | |
3860 | if (s == NULL) | |
3861 | { | |
3862 | if (! copy_private_bfd_data (ibfd, obfd)) | |
3863 | return false; | |
3864 | } | |
3865 | } | |
3866 | ||
3867 | ihdr = &elf_section_data (isec)->this_hdr; | |
3868 | ohdr = &elf_section_data (osec)->this_hdr; | |
3869 | ||
3870 | ohdr->sh_entsize = ihdr->sh_entsize; | |
3871 | ||
3872 | if (ihdr->sh_type == SHT_SYMTAB | |
3873 | || ihdr->sh_type == SHT_DYNSYM | |
3874 | || ihdr->sh_type == SHT_GNU_verneed | |
3875 | || ihdr->sh_type == SHT_GNU_verdef) | |
3876 | ohdr->sh_info = ihdr->sh_info; | |
3877 | ||
bf572ba0 MM |
3878 | elf_section_data (osec)->use_rela_p |
3879 | = elf_section_data (isec)->use_rela_p; | |
3880 | ||
252b5132 RH |
3881 | return true; |
3882 | } | |
3883 | ||
3884 | /* Copy private symbol information. If this symbol is in a section | |
3885 | which we did not map into a BFD section, try to map the section | |
3886 | index correctly. We use special macro definitions for the mapped | |
3887 | section indices; these definitions are interpreted by the | |
3888 | swap_out_syms function. */ | |
3889 | ||
3890 | #define MAP_ONESYMTAB (SHN_LORESERVE - 1) | |
3891 | #define MAP_DYNSYMTAB (SHN_LORESERVE - 2) | |
3892 | #define MAP_STRTAB (SHN_LORESERVE - 3) | |
3893 | #define MAP_SHSTRTAB (SHN_LORESERVE - 4) | |
3894 | ||
3895 | boolean | |
3896 | _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg) | |
3897 | bfd *ibfd; | |
3898 | asymbol *isymarg; | |
3899 | bfd *obfd; | |
3900 | asymbol *osymarg; | |
3901 | { | |
3902 | elf_symbol_type *isym, *osym; | |
3903 | ||
3904 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
3905 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
3906 | return true; | |
3907 | ||
3908 | isym = elf_symbol_from (ibfd, isymarg); | |
3909 | osym = elf_symbol_from (obfd, osymarg); | |
3910 | ||
3911 | if (isym != NULL | |
3912 | && osym != NULL | |
3913 | && bfd_is_abs_section (isym->symbol.section)) | |
3914 | { | |
3915 | unsigned int shndx; | |
3916 | ||
3917 | shndx = isym->internal_elf_sym.st_shndx; | |
3918 | if (shndx == elf_onesymtab (ibfd)) | |
3919 | shndx = MAP_ONESYMTAB; | |
3920 | else if (shndx == elf_dynsymtab (ibfd)) | |
3921 | shndx = MAP_DYNSYMTAB; | |
3922 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
3923 | shndx = MAP_STRTAB; | |
3924 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
3925 | shndx = MAP_SHSTRTAB; | |
3926 | osym->internal_elf_sym.st_shndx = shndx; | |
3927 | } | |
3928 | ||
3929 | return true; | |
3930 | } | |
3931 | ||
3932 | /* Swap out the symbols. */ | |
3933 | ||
3934 | static boolean | |
3935 | swap_out_syms (abfd, sttp, relocatable_p) | |
3936 | bfd *abfd; | |
3937 | struct bfd_strtab_hash **sttp; | |
3938 | int relocatable_p; | |
3939 | { | |
3940 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3941 | ||
3942 | if (!elf_map_symbols (abfd)) | |
3943 | return false; | |
3944 | ||
3945 | /* Dump out the symtabs. */ | |
3946 | { | |
3947 | int symcount = bfd_get_symcount (abfd); | |
3948 | asymbol **syms = bfd_get_outsymbols (abfd); | |
3949 | struct bfd_strtab_hash *stt; | |
3950 | Elf_Internal_Shdr *symtab_hdr; | |
3951 | Elf_Internal_Shdr *symstrtab_hdr; | |
3952 | char *outbound_syms; | |
3953 | int idx; | |
3954 | ||
3955 | stt = _bfd_elf_stringtab_init (); | |
3956 | if (stt == NULL) | |
3957 | return false; | |
3958 | ||
3959 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
3960 | symtab_hdr->sh_type = SHT_SYMTAB; | |
3961 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
3962 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
3963 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
3964 | symtab_hdr->sh_addralign = bed->s->file_align; | |
3965 | ||
3966 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
3967 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
3968 | ||
3969 | outbound_syms = bfd_alloc (abfd, | |
3970 | (1 + symcount) * bed->s->sizeof_sym); | |
3971 | if (outbound_syms == NULL) | |
3972 | return false; | |
3973 | symtab_hdr->contents = (PTR) outbound_syms; | |
3974 | ||
3975 | /* now generate the data (for "contents") */ | |
3976 | { | |
3977 | /* Fill in zeroth symbol and swap it out. */ | |
3978 | Elf_Internal_Sym sym; | |
3979 | sym.st_name = 0; | |
3980 | sym.st_value = 0; | |
3981 | sym.st_size = 0; | |
3982 | sym.st_info = 0; | |
3983 | sym.st_other = 0; | |
3984 | sym.st_shndx = SHN_UNDEF; | |
3985 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); | |
3986 | outbound_syms += bed->s->sizeof_sym; | |
3987 | } | |
3988 | for (idx = 0; idx < symcount; idx++) | |
3989 | { | |
3990 | Elf_Internal_Sym sym; | |
3991 | bfd_vma value = syms[idx]->value; | |
3992 | elf_symbol_type *type_ptr; | |
3993 | flagword flags = syms[idx]->flags; | |
3994 | int type; | |
3995 | ||
3996 | if (flags & BSF_SECTION_SYM) | |
3997 | /* Section symbols have no names. */ | |
3998 | sym.st_name = 0; | |
3999 | else | |
4000 | { | |
4001 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
4002 | syms[idx]->name, | |
4003 | true, false); | |
4004 | if (sym.st_name == (unsigned long) -1) | |
4005 | return false; | |
4006 | } | |
4007 | ||
4008 | type_ptr = elf_symbol_from (abfd, syms[idx]); | |
4009 | ||
4010 | if ((flags & BSF_SECTION_SYM) == 0 | |
4011 | && bfd_is_com_section (syms[idx]->section)) | |
4012 | { | |
4013 | /* ELF common symbols put the alignment into the `value' field, | |
4014 | and the size into the `size' field. This is backwards from | |
4015 | how BFD handles it, so reverse it here. */ | |
4016 | sym.st_size = value; | |
4017 | if (type_ptr == NULL | |
4018 | || type_ptr->internal_elf_sym.st_value == 0) | |
4019 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
4020 | else | |
4021 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
4022 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
4023 | (abfd, syms[idx]->section); | |
4024 | } | |
4025 | else | |
4026 | { | |
4027 | asection *sec = syms[idx]->section; | |
4028 | int shndx; | |
4029 | ||
4030 | if (sec->output_section) | |
4031 | { | |
4032 | value += sec->output_offset; | |
4033 | sec = sec->output_section; | |
4034 | } | |
4035 | /* Don't add in the section vma for relocatable output. */ | |
4036 | if (! relocatable_p) | |
4037 | value += sec->vma; | |
4038 | sym.st_value = value; | |
4039 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
4040 | ||
4041 | if (bfd_is_abs_section (sec) | |
4042 | && type_ptr != NULL | |
4043 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
4044 | { | |
4045 | /* This symbol is in a real ELF section which we did | |
4046 | not create as a BFD section. Undo the mapping done | |
4047 | by copy_private_symbol_data. */ | |
4048 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
4049 | switch (shndx) | |
4050 | { | |
4051 | case MAP_ONESYMTAB: | |
4052 | shndx = elf_onesymtab (abfd); | |
4053 | break; | |
4054 | case MAP_DYNSYMTAB: | |
4055 | shndx = elf_dynsymtab (abfd); | |
4056 | break; | |
4057 | case MAP_STRTAB: | |
4058 | shndx = elf_tdata (abfd)->strtab_section; | |
4059 | break; | |
4060 | case MAP_SHSTRTAB: | |
4061 | shndx = elf_tdata (abfd)->shstrtab_section; | |
4062 | break; | |
4063 | default: | |
4064 | break; | |
4065 | } | |
4066 | } | |
4067 | else | |
4068 | { | |
4069 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
4070 | ||
4071 | if (shndx == -1) | |
4072 | { | |
4073 | asection *sec2; | |
4074 | ||
4075 | /* Writing this would be a hell of a lot easier if | |
4076 | we had some decent documentation on bfd, and | |
4077 | knew what to expect of the library, and what to | |
4078 | demand of applications. For example, it | |
4079 | appears that `objcopy' might not set the | |
4080 | section of a symbol to be a section that is | |
4081 | actually in the output file. */ | |
4082 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
4083 | BFD_ASSERT (sec2 != 0); | |
4084 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
4085 | BFD_ASSERT (shndx != -1); | |
4086 | } | |
4087 | } | |
4088 | ||
4089 | sym.st_shndx = shndx; | |
4090 | } | |
4091 | ||
4092 | if ((flags & BSF_FUNCTION) != 0) | |
4093 | type = STT_FUNC; | |
4094 | else if ((flags & BSF_OBJECT) != 0) | |
4095 | type = STT_OBJECT; | |
4096 | else | |
4097 | type = STT_NOTYPE; | |
4098 | ||
4099 | /* Processor-specific types */ | |
4100 | if (bed->elf_backend_get_symbol_type) | |
4101 | type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type); | |
4102 | ||
4103 | if (flags & BSF_SECTION_SYM) | |
4104 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
4105 | else if (bfd_is_com_section (syms[idx]->section)) | |
4106 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
4107 | else if (bfd_is_und_section (syms[idx]->section)) | |
4108 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
4109 | ? STB_WEAK | |
4110 | : STB_GLOBAL), | |
4111 | type); | |
4112 | else if (flags & BSF_FILE) | |
4113 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
4114 | else | |
4115 | { | |
4116 | int bind = STB_LOCAL; | |
4117 | ||
4118 | if (flags & BSF_LOCAL) | |
4119 | bind = STB_LOCAL; | |
4120 | else if (flags & BSF_WEAK) | |
4121 | bind = STB_WEAK; | |
4122 | else if (flags & BSF_GLOBAL) | |
4123 | bind = STB_GLOBAL; | |
4124 | ||
4125 | sym.st_info = ELF_ST_INFO (bind, type); | |
4126 | } | |
4127 | ||
4128 | if (type_ptr != NULL) | |
4129 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
4130 | else | |
4131 | sym.st_other = 0; | |
4132 | ||
4133 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); | |
4134 | outbound_syms += bed->s->sizeof_sym; | |
4135 | } | |
4136 | ||
4137 | *sttp = stt; | |
4138 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
4139 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
4140 | ||
4141 | symstrtab_hdr->sh_flags = 0; | |
4142 | symstrtab_hdr->sh_addr = 0; | |
4143 | symstrtab_hdr->sh_entsize = 0; | |
4144 | symstrtab_hdr->sh_link = 0; | |
4145 | symstrtab_hdr->sh_info = 0; | |
4146 | symstrtab_hdr->sh_addralign = 1; | |
4147 | } | |
4148 | ||
4149 | return true; | |
4150 | } | |
4151 | ||
4152 | /* Return the number of bytes required to hold the symtab vector. | |
4153 | ||
4154 | Note that we base it on the count plus 1, since we will null terminate | |
4155 | the vector allocated based on this size. However, the ELF symbol table | |
4156 | always has a dummy entry as symbol #0, so it ends up even. */ | |
4157 | ||
4158 | long | |
4159 | _bfd_elf_get_symtab_upper_bound (abfd) | |
4160 | bfd *abfd; | |
4161 | { | |
4162 | long symcount; | |
4163 | long symtab_size; | |
4164 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
4165 | ||
4166 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
4167 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
4168 | ||
4169 | return symtab_size; | |
4170 | } | |
4171 | ||
4172 | long | |
4173 | _bfd_elf_get_dynamic_symtab_upper_bound (abfd) | |
4174 | bfd *abfd; | |
4175 | { | |
4176 | long symcount; | |
4177 | long symtab_size; | |
4178 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
4179 | ||
4180 | if (elf_dynsymtab (abfd) == 0) | |
4181 | { | |
4182 | bfd_set_error (bfd_error_invalid_operation); | |
4183 | return -1; | |
4184 | } | |
4185 | ||
4186 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
4187 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
4188 | ||
4189 | return symtab_size; | |
4190 | } | |
4191 | ||
4192 | long | |
4193 | _bfd_elf_get_reloc_upper_bound (abfd, asect) | |
4194 | bfd *abfd; | |
4195 | sec_ptr asect; | |
4196 | { | |
4197 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
4198 | } | |
4199 | ||
4200 | /* Canonicalize the relocs. */ | |
4201 | ||
4202 | long | |
4203 | _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
4204 | bfd *abfd; | |
4205 | sec_ptr section; | |
4206 | arelent **relptr; | |
4207 | asymbol **symbols; | |
4208 | { | |
4209 | arelent *tblptr; | |
4210 | unsigned int i; | |
4211 | ||
4212 | if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, | |
4213 | section, | |
4214 | symbols, | |
4215 | false)) | |
4216 | return -1; | |
4217 | ||
4218 | tblptr = section->relocation; | |
4219 | for (i = 0; i < section->reloc_count; i++) | |
4220 | *relptr++ = tblptr++; | |
4221 | ||
4222 | *relptr = NULL; | |
4223 | ||
4224 | return section->reloc_count; | |
4225 | } | |
4226 | ||
4227 | long | |
4228 | _bfd_elf_get_symtab (abfd, alocation) | |
4229 | bfd *abfd; | |
4230 | asymbol **alocation; | |
4231 | { | |
4232 | long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table | |
4233 | (abfd, alocation, false); | |
4234 | ||
4235 | if (symcount >= 0) | |
4236 | bfd_get_symcount (abfd) = symcount; | |
4237 | return symcount; | |
4238 | } | |
4239 | ||
4240 | long | |
4241 | _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation) | |
4242 | bfd *abfd; | |
4243 | asymbol **alocation; | |
4244 | { | |
4245 | return get_elf_backend_data (abfd)->s->slurp_symbol_table | |
4246 | (abfd, alocation, true); | |
4247 | } | |
4248 | ||
4249 | /* Return the size required for the dynamic reloc entries. Any | |
4250 | section that was actually installed in the BFD, and has type | |
4251 | SHT_REL or SHT_RELA, and uses the dynamic symbol table, is | |
4252 | considered to be a dynamic reloc section. */ | |
4253 | ||
4254 | long | |
4255 | _bfd_elf_get_dynamic_reloc_upper_bound (abfd) | |
4256 | bfd *abfd; | |
4257 | { | |
4258 | long ret; | |
4259 | asection *s; | |
4260 | ||
4261 | if (elf_dynsymtab (abfd) == 0) | |
4262 | { | |
4263 | bfd_set_error (bfd_error_invalid_operation); | |
4264 | return -1; | |
4265 | } | |
4266 | ||
4267 | ret = sizeof (arelent *); | |
4268 | for (s = abfd->sections; s != NULL; s = s->next) | |
4269 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
4270 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
4271 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
4272 | ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize) | |
4273 | * sizeof (arelent *)); | |
4274 | ||
4275 | return ret; | |
4276 | } | |
4277 | ||
4278 | /* Canonicalize the dynamic relocation entries. Note that we return | |
4279 | the dynamic relocations as a single block, although they are | |
4280 | actually associated with particular sections; the interface, which | |
4281 | was designed for SunOS style shared libraries, expects that there | |
4282 | is only one set of dynamic relocs. Any section that was actually | |
4283 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses | |
4284 | the dynamic symbol table, is considered to be a dynamic reloc | |
4285 | section. */ | |
4286 | ||
4287 | long | |
4288 | _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms) | |
4289 | bfd *abfd; | |
4290 | arelent **storage; | |
4291 | asymbol **syms; | |
4292 | { | |
4293 | boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean)); | |
4294 | asection *s; | |
4295 | long ret; | |
4296 | ||
4297 | if (elf_dynsymtab (abfd) == 0) | |
4298 | { | |
4299 | bfd_set_error (bfd_error_invalid_operation); | |
4300 | return -1; | |
4301 | } | |
4302 | ||
4303 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
4304 | ret = 0; | |
4305 | for (s = abfd->sections; s != NULL; s = s->next) | |
4306 | { | |
4307 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
4308 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
4309 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
4310 | { | |
4311 | arelent *p; | |
4312 | long count, i; | |
4313 | ||
4314 | if (! (*slurp_relocs) (abfd, s, syms, true)) | |
4315 | return -1; | |
4316 | count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize; | |
4317 | p = s->relocation; | |
4318 | for (i = 0; i < count; i++) | |
4319 | *storage++ = p++; | |
4320 | ret += count; | |
4321 | } | |
4322 | } | |
4323 | ||
4324 | *storage = NULL; | |
4325 | ||
4326 | return ret; | |
4327 | } | |
4328 | \f | |
4329 | /* Read in the version information. */ | |
4330 | ||
4331 | boolean | |
4332 | _bfd_elf_slurp_version_tables (abfd) | |
4333 | bfd *abfd; | |
4334 | { | |
4335 | bfd_byte *contents = NULL; | |
4336 | ||
4337 | if (elf_dynverdef (abfd) != 0) | |
4338 | { | |
4339 | Elf_Internal_Shdr *hdr; | |
4340 | Elf_External_Verdef *everdef; | |
4341 | Elf_Internal_Verdef *iverdef; | |
4342 | unsigned int i; | |
4343 | ||
4344 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
4345 | ||
4346 | elf_tdata (abfd)->verdef = | |
4347 | ((Elf_Internal_Verdef *) | |
4348 | bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef))); | |
4349 | if (elf_tdata (abfd)->verdef == NULL) | |
4350 | goto error_return; | |
4351 | ||
4352 | elf_tdata (abfd)->cverdefs = hdr->sh_info; | |
4353 | ||
4354 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
4355 | if (contents == NULL) | |
4356 | goto error_return; | |
4357 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
4358 | || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size) | |
4359 | goto error_return; | |
4360 | ||
4361 | everdef = (Elf_External_Verdef *) contents; | |
4362 | iverdef = elf_tdata (abfd)->verdef; | |
4363 | for (i = 0; i < hdr->sh_info; i++, iverdef++) | |
4364 | { | |
4365 | Elf_External_Verdaux *everdaux; | |
4366 | Elf_Internal_Verdaux *iverdaux; | |
4367 | unsigned int j; | |
4368 | ||
4369 | _bfd_elf_swap_verdef_in (abfd, everdef, iverdef); | |
4370 | ||
4371 | iverdef->vd_bfd = abfd; | |
4372 | ||
4373 | iverdef->vd_auxptr = ((Elf_Internal_Verdaux *) | |
4374 | bfd_alloc (abfd, | |
4375 | (iverdef->vd_cnt | |
4376 | * sizeof (Elf_Internal_Verdaux)))); | |
4377 | if (iverdef->vd_auxptr == NULL) | |
4378 | goto error_return; | |
4379 | ||
4380 | everdaux = ((Elf_External_Verdaux *) | |
4381 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
4382 | iverdaux = iverdef->vd_auxptr; | |
4383 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
4384 | { | |
4385 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
4386 | ||
4387 | iverdaux->vda_nodename = | |
4388 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4389 | iverdaux->vda_name); | |
4390 | if (iverdaux->vda_nodename == NULL) | |
4391 | goto error_return; | |
4392 | ||
4393 | if (j + 1 < iverdef->vd_cnt) | |
4394 | iverdaux->vda_nextptr = iverdaux + 1; | |
4395 | else | |
4396 | iverdaux->vda_nextptr = NULL; | |
4397 | ||
4398 | everdaux = ((Elf_External_Verdaux *) | |
4399 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
4400 | } | |
4401 | ||
4402 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
4403 | ||
4404 | if (i + 1 < hdr->sh_info) | |
4405 | iverdef->vd_nextdef = iverdef + 1; | |
4406 | else | |
4407 | iverdef->vd_nextdef = NULL; | |
4408 | ||
4409 | everdef = ((Elf_External_Verdef *) | |
4410 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
4411 | } | |
4412 | ||
4413 | free (contents); | |
4414 | contents = NULL; | |
4415 | } | |
4416 | ||
4417 | if (elf_dynverref (abfd) != 0) | |
4418 | { | |
4419 | Elf_Internal_Shdr *hdr; | |
4420 | Elf_External_Verneed *everneed; | |
4421 | Elf_Internal_Verneed *iverneed; | |
4422 | unsigned int i; | |
4423 | ||
4424 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
4425 | ||
4426 | elf_tdata (abfd)->verref = | |
4427 | ((Elf_Internal_Verneed *) | |
4428 | bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed))); | |
4429 | if (elf_tdata (abfd)->verref == NULL) | |
4430 | goto error_return; | |
4431 | ||
4432 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
4433 | ||
4434 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
4435 | if (contents == NULL) | |
4436 | goto error_return; | |
4437 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
4438 | || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size) | |
4439 | goto error_return; | |
4440 | ||
4441 | everneed = (Elf_External_Verneed *) contents; | |
4442 | iverneed = elf_tdata (abfd)->verref; | |
4443 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
4444 | { | |
4445 | Elf_External_Vernaux *evernaux; | |
4446 | Elf_Internal_Vernaux *ivernaux; | |
4447 | unsigned int j; | |
4448 | ||
4449 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
4450 | ||
4451 | iverneed->vn_bfd = abfd; | |
4452 | ||
4453 | iverneed->vn_filename = | |
4454 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4455 | iverneed->vn_file); | |
4456 | if (iverneed->vn_filename == NULL) | |
4457 | goto error_return; | |
4458 | ||
4459 | iverneed->vn_auxptr = | |
4460 | ((Elf_Internal_Vernaux *) | |
4461 | bfd_alloc (abfd, | |
4462 | iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux))); | |
4463 | ||
4464 | evernaux = ((Elf_External_Vernaux *) | |
4465 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
4466 | ivernaux = iverneed->vn_auxptr; | |
4467 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
4468 | { | |
4469 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
4470 | ||
4471 | ivernaux->vna_nodename = | |
4472 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
4473 | ivernaux->vna_name); | |
4474 | if (ivernaux->vna_nodename == NULL) | |
4475 | goto error_return; | |
4476 | ||
4477 | if (j + 1 < iverneed->vn_cnt) | |
4478 | ivernaux->vna_nextptr = ivernaux + 1; | |
4479 | else | |
4480 | ivernaux->vna_nextptr = NULL; | |
4481 | ||
4482 | evernaux = ((Elf_External_Vernaux *) | |
4483 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
4484 | } | |
4485 | ||
4486 | if (i + 1 < hdr->sh_info) | |
4487 | iverneed->vn_nextref = iverneed + 1; | |
4488 | else | |
4489 | iverneed->vn_nextref = NULL; | |
4490 | ||
4491 | everneed = ((Elf_External_Verneed *) | |
4492 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
4493 | } | |
4494 | ||
4495 | free (contents); | |
4496 | contents = NULL; | |
4497 | } | |
4498 | ||
4499 | return true; | |
4500 | ||
4501 | error_return: | |
4502 | if (contents == NULL) | |
4503 | free (contents); | |
4504 | return false; | |
4505 | } | |
4506 | \f | |
4507 | asymbol * | |
4508 | _bfd_elf_make_empty_symbol (abfd) | |
4509 | bfd *abfd; | |
4510 | { | |
4511 | elf_symbol_type *newsym; | |
4512 | ||
4513 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
4514 | if (!newsym) | |
4515 | return NULL; | |
4516 | else | |
4517 | { | |
4518 | newsym->symbol.the_bfd = abfd; | |
4519 | return &newsym->symbol; | |
4520 | } | |
4521 | } | |
4522 | ||
4523 | void | |
4524 | _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret) | |
4525 | bfd *ignore_abfd; | |
4526 | asymbol *symbol; | |
4527 | symbol_info *ret; | |
4528 | { | |
4529 | bfd_symbol_info (symbol, ret); | |
4530 | } | |
4531 | ||
4532 | /* Return whether a symbol name implies a local symbol. Most targets | |
4533 | use this function for the is_local_label_name entry point, but some | |
4534 | override it. */ | |
4535 | ||
4536 | boolean | |
4537 | _bfd_elf_is_local_label_name (abfd, name) | |
4538 | bfd *abfd; | |
4539 | const char *name; | |
4540 | { | |
4541 | /* Normal local symbols start with ``.L''. */ | |
4542 | if (name[0] == '.' && name[1] == 'L') | |
4543 | return true; | |
4544 | ||
4545 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
4546 | DWARF debugging symbols starting with ``..''. */ | |
4547 | if (name[0] == '.' && name[1] == '.') | |
4548 | return true; | |
4549 | ||
4550 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
4551 | emitting DWARF debugging output. I suspect this is actually a | |
4552 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
4553 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
4554 | underscore to be emitted on some ELF targets). For ease of use, | |
4555 | we treat such symbols as local. */ | |
4556 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
4557 | return true; | |
4558 | ||
4559 | return false; | |
4560 | } | |
4561 | ||
4562 | alent * | |
4563 | _bfd_elf_get_lineno (ignore_abfd, symbol) | |
4564 | bfd *ignore_abfd; | |
4565 | asymbol *symbol; | |
4566 | { | |
4567 | abort (); | |
4568 | return NULL; | |
4569 | } | |
4570 | ||
4571 | boolean | |
4572 | _bfd_elf_set_arch_mach (abfd, arch, machine) | |
4573 | bfd *abfd; | |
4574 | enum bfd_architecture arch; | |
4575 | unsigned long machine; | |
4576 | { | |
4577 | /* If this isn't the right architecture for this backend, and this | |
4578 | isn't the generic backend, fail. */ | |
4579 | if (arch != get_elf_backend_data (abfd)->arch | |
4580 | && arch != bfd_arch_unknown | |
4581 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
4582 | return false; | |
4583 | ||
4584 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
4585 | } | |
4586 | ||
4587 | /* Find the nearest line to a particular section and offset, for error | |
4588 | reporting. */ | |
4589 | ||
4590 | boolean | |
4591 | _bfd_elf_find_nearest_line (abfd, | |
4592 | section, | |
4593 | symbols, | |
4594 | offset, | |
4595 | filename_ptr, | |
4596 | functionname_ptr, | |
4597 | line_ptr) | |
4598 | bfd *abfd; | |
4599 | asection *section; | |
4600 | asymbol **symbols; | |
4601 | bfd_vma offset; | |
4602 | CONST char **filename_ptr; | |
4603 | CONST char **functionname_ptr; | |
4604 | unsigned int *line_ptr; | |
4605 | { | |
4606 | boolean found; | |
4607 | const char *filename; | |
4608 | asymbol *func; | |
4609 | bfd_vma low_func; | |
4610 | asymbol **p; | |
4611 | ||
4612 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
4613 | filename_ptr, functionname_ptr, | |
4614 | line_ptr)) | |
4615 | return true; | |
4616 | ||
4617 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
4618 | filename_ptr, functionname_ptr, | |
4619 | line_ptr)) | |
4620 | return true; | |
4621 | ||
4622 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4623 | &found, filename_ptr, | |
4624 | functionname_ptr, line_ptr, | |
4625 | &elf_tdata (abfd)->line_info)) | |
4626 | return false; | |
4627 | if (found) | |
4628 | return true; | |
4629 | ||
4630 | if (symbols == NULL) | |
4631 | return false; | |
4632 | ||
4633 | filename = NULL; | |
4634 | func = NULL; | |
4635 | low_func = 0; | |
4636 | ||
4637 | for (p = symbols; *p != NULL; p++) | |
4638 | { | |
4639 | elf_symbol_type *q; | |
4640 | ||
4641 | q = (elf_symbol_type *) *p; | |
4642 | ||
4643 | if (bfd_get_section (&q->symbol) != section) | |
4644 | continue; | |
4645 | ||
4646 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) | |
4647 | { | |
4648 | default: | |
4649 | break; | |
4650 | case STT_FILE: | |
4651 | filename = bfd_asymbol_name (&q->symbol); | |
4652 | break; | |
4653 | case STT_NOTYPE: | |
4654 | case STT_FUNC: | |
4655 | if (q->symbol.section == section | |
4656 | && q->symbol.value >= low_func | |
4657 | && q->symbol.value <= offset) | |
4658 | { | |
4659 | func = (asymbol *) q; | |
4660 | low_func = q->symbol.value; | |
4661 | } | |
4662 | break; | |
4663 | } | |
4664 | } | |
4665 | ||
4666 | if (func == NULL) | |
4667 | return false; | |
4668 | ||
4669 | *filename_ptr = filename; | |
4670 | *functionname_ptr = bfd_asymbol_name (func); | |
4671 | *line_ptr = 0; | |
4672 | return true; | |
4673 | } | |
4674 | ||
4675 | int | |
4676 | _bfd_elf_sizeof_headers (abfd, reloc) | |
4677 | bfd *abfd; | |
4678 | boolean reloc; | |
4679 | { | |
4680 | int ret; | |
4681 | ||
4682 | ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; | |
4683 | if (! reloc) | |
4684 | ret += get_program_header_size (abfd); | |
4685 | return ret; | |
4686 | } | |
4687 | ||
4688 | boolean | |
4689 | _bfd_elf_set_section_contents (abfd, section, location, offset, count) | |
4690 | bfd *abfd; | |
4691 | sec_ptr section; | |
4692 | PTR location; | |
4693 | file_ptr offset; | |
4694 | bfd_size_type count; | |
4695 | { | |
4696 | Elf_Internal_Shdr *hdr; | |
4697 | ||
4698 | if (! abfd->output_has_begun | |
4699 | && ! _bfd_elf_compute_section_file_positions | |
4700 | (abfd, (struct bfd_link_info *) NULL)) | |
4701 | return false; | |
4702 | ||
4703 | hdr = &elf_section_data (section)->this_hdr; | |
4704 | ||
4705 | if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) | |
4706 | return false; | |
4707 | if (bfd_write (location, 1, count, abfd) != count) | |
4708 | return false; | |
4709 | ||
4710 | return true; | |
4711 | } | |
4712 | ||
4713 | void | |
4714 | _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst) | |
4715 | bfd *abfd; | |
4716 | arelent *cache_ptr; | |
4717 | Elf_Internal_Rela *dst; | |
4718 | { | |
4719 | abort (); | |
4720 | } | |
4721 | ||
4722 | #if 0 | |
4723 | void | |
4724 | _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst) | |
4725 | bfd *abfd; | |
4726 | arelent *cache_ptr; | |
4727 | Elf_Internal_Rel *dst; | |
4728 | { | |
4729 | abort (); | |
4730 | } | |
4731 | #endif | |
4732 | ||
4733 | /* Try to convert a non-ELF reloc into an ELF one. */ | |
4734 | ||
4735 | boolean | |
4736 | _bfd_elf_validate_reloc (abfd, areloc) | |
4737 | bfd *abfd; | |
4738 | arelent *areloc; | |
4739 | { | |
4740 | /* Check whether we really have an ELF howto. */ | |
4741 | ||
4742 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
4743 | { | |
4744 | bfd_reloc_code_real_type code; | |
4745 | reloc_howto_type *howto; | |
4746 | ||
4747 | /* Alien reloc: Try to determine its type to replace it with an | |
4748 | equivalent ELF reloc. */ | |
4749 | ||
4750 | if (areloc->howto->pc_relative) | |
4751 | { | |
4752 | switch (areloc->howto->bitsize) | |
4753 | { | |
4754 | case 8: | |
4755 | code = BFD_RELOC_8_PCREL; | |
4756 | break; | |
4757 | case 12: | |
4758 | code = BFD_RELOC_12_PCREL; | |
4759 | break; | |
4760 | case 16: | |
4761 | code = BFD_RELOC_16_PCREL; | |
4762 | break; | |
4763 | case 24: | |
4764 | code = BFD_RELOC_24_PCREL; | |
4765 | break; | |
4766 | case 32: | |
4767 | code = BFD_RELOC_32_PCREL; | |
4768 | break; | |
4769 | case 64: | |
4770 | code = BFD_RELOC_64_PCREL; | |
4771 | break; | |
4772 | default: | |
4773 | goto fail; | |
4774 | } | |
4775 | ||
4776 | howto = bfd_reloc_type_lookup (abfd, code); | |
4777 | ||
4778 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
4779 | { | |
4780 | if (howto->pcrel_offset) | |
4781 | areloc->addend += areloc->address; | |
4782 | else | |
4783 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
4784 | } | |
4785 | } | |
4786 | else | |
4787 | { | |
4788 | switch (areloc->howto->bitsize) | |
4789 | { | |
4790 | case 8: | |
4791 | code = BFD_RELOC_8; | |
4792 | break; | |
4793 | case 14: | |
4794 | code = BFD_RELOC_14; | |
4795 | break; | |
4796 | case 16: | |
4797 | code = BFD_RELOC_16; | |
4798 | break; | |
4799 | case 26: | |
4800 | code = BFD_RELOC_26; | |
4801 | break; | |
4802 | case 32: | |
4803 | code = BFD_RELOC_32; | |
4804 | break; | |
4805 | case 64: | |
4806 | code = BFD_RELOC_64; | |
4807 | break; | |
4808 | default: | |
4809 | goto fail; | |
4810 | } | |
4811 | ||
4812 | howto = bfd_reloc_type_lookup (abfd, code); | |
4813 | } | |
4814 | ||
4815 | if (howto) | |
4816 | areloc->howto = howto; | |
4817 | else | |
4818 | goto fail; | |
4819 | } | |
4820 | ||
4821 | return true; | |
4822 | ||
4823 | fail: | |
4824 | (*_bfd_error_handler) | |
4825 | (_("%s: unsupported relocation type %s"), | |
4826 | bfd_get_filename (abfd), areloc->howto->name); | |
4827 | bfd_set_error (bfd_error_bad_value); | |
4828 | return false; | |
4829 | } | |
4830 | ||
4831 | boolean | |
4832 | _bfd_elf_close_and_cleanup (abfd) | |
4833 | bfd *abfd; | |
4834 | { | |
4835 | if (bfd_get_format (abfd) == bfd_object) | |
4836 | { | |
4837 | if (elf_shstrtab (abfd) != NULL) | |
4838 | _bfd_stringtab_free (elf_shstrtab (abfd)); | |
4839 | } | |
4840 | ||
4841 | return _bfd_generic_close_and_cleanup (abfd); | |
4842 | } | |
4843 | ||
4844 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
4845 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
4846 | range-checking to interfere. There is nothing else to do in processing | |
4847 | this reloc. */ | |
4848 | ||
4849 | bfd_reloc_status_type | |
4850 | _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg) | |
4851 | bfd *abfd; | |
4852 | arelent *re; | |
4853 | struct symbol_cache_entry *symbol; | |
4854 | PTR data; | |
4855 | asection *is; | |
4856 | bfd *obfd; | |
4857 | char **errmsg; | |
4858 | { | |
4859 | return bfd_reloc_ok; | |
4860 | } | |
4861 | ||
4862 | \f | |
4863 | /* Elf core file support. Much of this only works on native | |
4864 | toolchains, since we rely on knowing the | |
4865 | machine-dependent procfs structure in order to pick | |
4866 | out details about the corefile. */ | |
4867 | ||
4868 | #ifdef HAVE_SYS_PROCFS_H | |
4869 | # include <sys/procfs.h> | |
4870 | #endif | |
4871 | ||
4872 | ||
4873 | /* Define offsetof for those systems which lack it. */ | |
4874 | ||
4875 | #ifndef offsetof | |
4876 | # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
4877 | #endif | |
4878 | ||
4879 | ||
4880 | /* FIXME: this is kinda wrong, but it's what gdb wants. */ | |
4881 | ||
4882 | static int | |
4883 | elfcore_make_pid (abfd) | |
4884 | bfd* abfd; | |
4885 | { | |
4886 | return ((elf_tdata (abfd)->core_lwpid << 16) | |
4887 | + (elf_tdata (abfd)->core_pid)); | |
4888 | } | |
4889 | ||
4890 | ||
4891 | /* If there isn't a section called NAME, make one, using | |
4892 | data from SECT. Note, this function will generate a | |
4893 | reference to NAME, so you shouldn't deallocate or | |
4894 | overwrite it. */ | |
4895 | ||
4896 | static boolean | |
4897 | elfcore_maybe_make_sect (abfd, name, sect) | |
4898 | bfd* abfd; | |
4899 | char* name; | |
4900 | asection* sect; | |
4901 | { | |
4902 | asection* sect2; | |
4903 | ||
4904 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
4905 | return true; | |
4906 | ||
4907 | sect2 = bfd_make_section (abfd, name); | |
4908 | if (sect2 == NULL) | |
4909 | return false; | |
4910 | ||
4911 | sect2->_raw_size = sect->_raw_size; | |
4912 | sect2->filepos = sect->filepos; | |
4913 | sect2->flags = sect->flags; | |
4914 | sect2->alignment_power = sect->alignment_power; | |
4915 | return true; | |
4916 | } | |
4917 | ||
4918 | ||
4919 | /* prstatus_t exists on: | |
4920 | solaris 2.[567] | |
4921 | linux 2.[01] + glibc | |
4922 | unixware 4.2 | |
4923 | */ | |
4924 | ||
4925 | #if defined (HAVE_PRSTATUS_T) | |
4926 | static boolean | |
4927 | elfcore_grok_prstatus (abfd, note) | |
4928 | bfd* abfd; | |
4929 | Elf_Internal_Note* note; | |
4930 | { | |
4931 | prstatus_t prstat; | |
4932 | char buf[100]; | |
4933 | char* name; | |
4934 | asection* sect; | |
4935 | ||
4936 | if (note->descsz != sizeof (prstat)) | |
4937 | return true; | |
4938 | ||
4939 | memcpy (&prstat, note->descdata, sizeof (prstat)); | |
4940 | ||
4941 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4942 | elf_tdata (abfd)->core_pid = prstat.pr_pid; | |
4943 | ||
4944 | /* pr_who exists on: | |
4945 | solaris 2.[567] | |
4946 | unixware 4.2 | |
4947 | pr_who doesn't exist on: | |
4948 | linux 2.[01] | |
4949 | */ | |
4950 | #if defined (HAVE_PRSTATUS_T_PR_WHO) | |
4951 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; | |
4952 | #endif | |
4953 | ||
4954 | /* Make a ".reg/999" section. */ | |
4955 | ||
4956 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
4957 | name = bfd_alloc (abfd, strlen (buf) + 1); | |
4958 | if (name == NULL) | |
4959 | return false; | |
4960 | strcpy (name, buf); | |
4961 | ||
4962 | sect = bfd_make_section (abfd, name); | |
4963 | if (sect == NULL) | |
4964 | return false; | |
4965 | sect->_raw_size = sizeof (prstat.pr_reg); | |
4966 | sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg); | |
4967 | sect->flags = SEC_HAS_CONTENTS; | |
4968 | sect->alignment_power = 2; | |
4969 | ||
4970 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
4971 | return false; | |
4972 | ||
4973 | return true; | |
4974 | } | |
4975 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
4976 | ||
4977 | ||
4978 | /* There isn't a consistent prfpregset_t across platforms, | |
4979 | but it doesn't matter, because we don't have to pick this | |
4980 | data structure apart. */ | |
4981 | ||
4982 | static boolean | |
4983 | elfcore_grok_prfpreg (abfd, note) | |
4984 | bfd* abfd; | |
4985 | Elf_Internal_Note* note; | |
4986 | { | |
4987 | char buf[100]; | |
4988 | char* name; | |
4989 | asection* sect; | |
4990 | ||
4991 | /* Make a ".reg2/999" section. */ | |
4992 | ||
4993 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
4994 | name = bfd_alloc (abfd, strlen (buf) + 1); | |
4995 | if (name == NULL) | |
4996 | return false; | |
4997 | strcpy (name, buf); | |
4998 | ||
4999 | sect = bfd_make_section (abfd, name); | |
5000 | if (sect == NULL) | |
5001 | return false; | |
5002 | sect->_raw_size = note->descsz; | |
5003 | sect->filepos = note->descpos; | |
5004 | sect->flags = SEC_HAS_CONTENTS; | |
5005 | sect->alignment_power = 2; | |
5006 | ||
5007 | if (! elfcore_maybe_make_sect (abfd, ".reg2", sect)) | |
5008 | return false; | |
5009 | ||
5010 | return true; | |
5011 | } | |
5012 | ||
5013 | #if defined (HAVE_PRPSINFO_T) | |
5014 | # define elfcore_psinfo_t prpsinfo_t | |
5015 | #endif | |
5016 | ||
5017 | #if defined (HAVE_PSINFO_T) | |
5018 | # define elfcore_psinfo_t psinfo_t | |
5019 | #endif | |
5020 | ||
5021 | ||
5022 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
5023 | ||
5024 | /* return a malloc'ed copy of a string at START which is at | |
5025 | most MAX bytes long, possibly without a terminating '\0'. | |
5026 | the copy will always have a terminating '\0'. */ | |
5027 | ||
5028 | static char* | |
5029 | elfcore_strndup (abfd, start, max) | |
5030 | bfd* abfd; | |
5031 | char* start; | |
5032 | int max; | |
5033 | { | |
5034 | char* dup; | |
5035 | char* end = memchr (start, '\0', max); | |
5036 | int len; | |
5037 | ||
5038 | if (end == NULL) | |
5039 | len = max; | |
5040 | else | |
5041 | len = end - start; | |
5042 | ||
5043 | dup = bfd_alloc (abfd, len + 1); | |
5044 | if (dup == NULL) | |
5045 | return NULL; | |
5046 | ||
5047 | memcpy (dup, start, len); | |
5048 | dup[len] = '\0'; | |
5049 | ||
5050 | return dup; | |
5051 | } | |
5052 | ||
5053 | static boolean | |
5054 | elfcore_grok_psinfo (abfd, note) | |
5055 | bfd* abfd; | |
5056 | Elf_Internal_Note* note; | |
5057 | { | |
5058 | elfcore_psinfo_t psinfo; | |
5059 | ||
5060 | if (note->descsz != sizeof (elfcore_psinfo_t)) | |
5061 | return true; | |
5062 | ||
5063 | memcpy (&psinfo, note->descdata, note->descsz); | |
5064 | ||
5065 | elf_tdata (abfd)->core_program | |
5066 | = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname)); | |
5067 | ||
5068 | elf_tdata (abfd)->core_command | |
5069 | = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs)); | |
5070 | ||
5071 | /* Note that for some reason, a spurious space is tacked | |
5072 | onto the end of the args in some (at least one anyway) | |
5073 | implementations, so strip it off if it exists. */ | |
5074 | ||
5075 | { | |
5076 | char* command = elf_tdata (abfd)->core_command; | |
5077 | int n = strlen (command); | |
5078 | ||
5079 | if (0 < n && command[n - 1] == ' ') | |
5080 | command[n - 1] = '\0'; | |
5081 | } | |
5082 | ||
5083 | return true; | |
5084 | } | |
5085 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
5086 | ||
5087 | ||
5088 | #if defined (HAVE_PSTATUS_T) | |
5089 | static boolean | |
5090 | elfcore_grok_pstatus (abfd, note) | |
5091 | bfd* abfd; | |
5092 | Elf_Internal_Note* note; | |
5093 | { | |
5094 | pstatus_t pstat; | |
5095 | ||
5096 | if (note->descsz != sizeof (pstat)) | |
5097 | return true; | |
5098 | ||
5099 | memcpy (&pstat, note->descdata, sizeof (pstat)); | |
5100 | ||
5101 | elf_tdata (abfd)->core_pid = pstat.pr_pid; | |
5102 | ||
5103 | /* Could grab some more details from the "representative" | |
5104 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
5105 | NT_LWPSTATUS note, presumably. */ | |
5106 | ||
5107 | return true; | |
5108 | } | |
5109 | #endif /* defined (HAVE_PSTATUS_T) */ | |
5110 | ||
5111 | ||
5112 | #if defined (HAVE_LWPSTATUS_T) | |
5113 | static boolean | |
5114 | elfcore_grok_lwpstatus (abfd, note) | |
5115 | bfd* abfd; | |
5116 | Elf_Internal_Note* note; | |
5117 | { | |
5118 | lwpstatus_t lwpstat; | |
5119 | char buf[100]; | |
5120 | char* name; | |
5121 | asection* sect; | |
5122 | ||
5123 | if (note->descsz != sizeof (lwpstat)) | |
5124 | return true; | |
5125 | ||
5126 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
5127 | ||
5128 | elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; | |
5129 | elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; | |
5130 | ||
5131 | /* Make a ".reg/999" section. */ | |
5132 | ||
5133 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
5134 | name = bfd_alloc (abfd, strlen (buf) + 1); | |
5135 | if (name == NULL) | |
5136 | return false; | |
5137 | strcpy (name, buf); | |
5138 | ||
5139 | sect = bfd_make_section (abfd, name); | |
5140 | if (sect == NULL) | |
5141 | return false; | |
5142 | ||
5143 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
5144 | sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); | |
5145 | sect->filepos = note->descpos | |
5146 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
5147 | #endif | |
5148 | ||
5149 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
5150 | sect->_raw_size = sizeof (lwpstat.pr_reg); | |
5151 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); | |
5152 | #endif | |
5153 | ||
5154 | sect->flags = SEC_HAS_CONTENTS; | |
5155 | sect->alignment_power = 2; | |
5156 | ||
5157 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
5158 | return false; | |
5159 | ||
5160 | /* Make a ".reg2/999" section */ | |
5161 | ||
5162 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
5163 | name = bfd_alloc (abfd, strlen (buf) + 1); | |
5164 | if (name == NULL) | |
5165 | return false; | |
5166 | strcpy (name, buf); | |
5167 | ||
5168 | sect = bfd_make_section (abfd, name); | |
5169 | if (sect == NULL) | |
5170 | return false; | |
5171 | ||
5172 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
5173 | sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); | |
5174 | sect->filepos = note->descpos | |
5175 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
5176 | #endif | |
5177 | ||
5178 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
5179 | sect->_raw_size = sizeof (lwpstat.pr_fpreg); | |
5180 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); | |
5181 | #endif | |
5182 | ||
5183 | sect->flags = SEC_HAS_CONTENTS; | |
5184 | sect->alignment_power = 2; | |
5185 | ||
5186 | if (!elfcore_maybe_make_sect (abfd, ".reg2", sect)) | |
5187 | return false; | |
5188 | ||
5189 | return true; | |
5190 | } | |
5191 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
5192 | ||
5193 | ||
5194 | ||
5195 | static boolean | |
5196 | elfcore_grok_note (abfd, note) | |
5197 | bfd* abfd; | |
5198 | Elf_Internal_Note* note; | |
5199 | { | |
5200 | switch (note->type) | |
5201 | { | |
5202 | default: | |
5203 | return true; | |
5204 | ||
5205 | #if defined (HAVE_PRSTATUS_T) | |
5206 | case NT_PRSTATUS: | |
5207 | return elfcore_grok_prstatus (abfd, note); | |
5208 | #endif | |
5209 | ||
5210 | #if defined (HAVE_PSTATUS_T) | |
5211 | case NT_PSTATUS: | |
5212 | return elfcore_grok_pstatus (abfd, note); | |
5213 | #endif | |
5214 | ||
5215 | #if defined (HAVE_LWPSTATUS_T) | |
5216 | case NT_LWPSTATUS: | |
5217 | return elfcore_grok_lwpstatus (abfd, note); | |
5218 | #endif | |
5219 | ||
5220 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
5221 | return elfcore_grok_prfpreg (abfd, note); | |
5222 | ||
5223 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
5224 | case NT_PRPSINFO: | |
5225 | case NT_PSINFO: | |
5226 | return elfcore_grok_psinfo (abfd, note); | |
5227 | #endif | |
5228 | } | |
5229 | } | |
5230 | ||
5231 | ||
5232 | static boolean | |
5233 | elfcore_read_notes (abfd, offset, size) | |
5234 | bfd* abfd; | |
5235 | bfd_vma offset; | |
5236 | bfd_vma size; | |
5237 | { | |
5238 | char* buf; | |
5239 | char* p; | |
5240 | ||
5241 | if (size <= 0) | |
5242 | return true; | |
5243 | ||
5244 | if (bfd_seek (abfd, offset, SEEK_SET) == -1) | |
5245 | return false; | |
5246 | ||
5247 | buf = bfd_malloc ((size_t) size); | |
5248 | if (buf == NULL) | |
5249 | return false; | |
5250 | ||
5251 | if (bfd_read (buf, size, 1, abfd) != size) | |
5252 | { | |
5253 | error: | |
5254 | free (buf); | |
5255 | return false; | |
5256 | } | |
5257 | ||
5258 | p = buf; | |
5259 | while (p < buf + size) | |
5260 | { | |
5261 | /* FIXME: bad alignment assumption. */ | |
5262 | Elf_External_Note* xnp = (Elf_External_Note*) p; | |
5263 | Elf_Internal_Note in; | |
5264 | ||
5265 | in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type); | |
5266 | ||
5267 | in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz); | |
5268 | in.namedata = xnp->name; | |
5269 | ||
5270 | in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz); | |
5271 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); | |
5272 | in.descpos = offset + (in.descdata - buf); | |
5273 | ||
5274 | if (! elfcore_grok_note (abfd, &in)) | |
5275 | goto error; | |
5276 | ||
5277 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
5278 | } | |
5279 | ||
5280 | free (buf); | |
5281 | return true; | |
5282 | } | |
5283 | ||
5284 | ||
5285 | ||
5286 | boolean | |
5287 | _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num) | |
5288 | bfd* abfd; | |
5289 | Elf_Internal_Phdr* phdr; | |
5290 | int sec_num; | |
5291 | { | |
5292 | if (! bfd_section_from_phdr (abfd, phdr, sec_num)) | |
5293 | return false; | |
5294 | ||
5295 | if (phdr->p_type == PT_NOTE | |
5296 | && ! elfcore_read_notes (abfd, phdr->p_offset, phdr->p_filesz)) | |
5297 | return false; | |
5298 | ||
5299 | return true; | |
5300 | } | |
5301 |