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