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