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