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