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