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