Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
f0abc2a1 AM |
2 | Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, |
3 | 2003 Free Software Foundation, Inc. | |
252b5132 | 4 | |
5e8d7549 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
5e8d7549 NC |
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. | |
252b5132 | 11 | |
5e8d7549 NC |
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. | |
252b5132 | 16 | |
5e8d7549 | 17 | You should have received a copy of the GNU General Public License |
b34976b6 | 18 | along with this program; if not, write to the Free Software |
5e8d7549 | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
252b5132 | 20 | |
661a3fd4 | 21 | /* SECTION |
47d9a591 | 22 | |
252b5132 RH |
23 | ELF backends |
24 | ||
25 | BFD support for ELF formats is being worked on. | |
26 | Currently, the best supported back ends are for sparc and i386 | |
27 | (running svr4 or Solaris 2). | |
28 | ||
29 | Documentation of the internals of the support code still needs | |
30 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 31 | haven't bothered yet. */ |
252b5132 | 32 | |
7ee38065 MS |
33 | /* For sparc64-cross-sparc32. */ |
34 | #define _SYSCALL32 | |
252b5132 RH |
35 | #include "bfd.h" |
36 | #include "sysdep.h" | |
37 | #include "bfdlink.h" | |
38 | #include "libbfd.h" | |
39 | #define ARCH_SIZE 0 | |
40 | #include "elf-bfd.h" | |
e0e8c97f | 41 | #include "libiberty.h" |
252b5132 RH |
42 | |
43 | static INLINE struct elf_segment_map *make_mapping | |
b34976b6 AM |
44 | PARAMS ((bfd *, asection **, unsigned int, unsigned int, bfd_boolean)); |
45 | static bfd_boolean map_sections_to_segments | |
46 | PARAMS ((bfd *)); | |
47 | static int elf_sort_sections | |
48 | PARAMS ((const PTR, const PTR)); | |
49 | static bfd_boolean assign_file_positions_for_segments | |
50 | PARAMS ((bfd *)); | |
51 | static bfd_boolean assign_file_positions_except_relocs | |
52 | PARAMS ((bfd *)); | |
53 | static bfd_boolean prep_headers | |
54 | PARAMS ((bfd *)); | |
55 | static bfd_boolean swap_out_syms | |
56 | PARAMS ((bfd *, struct bfd_strtab_hash **, int)); | |
57 | static bfd_boolean copy_private_bfd_data | |
58 | PARAMS ((bfd *, bfd *)); | |
59 | static char *elf_read | |
60 | PARAMS ((bfd *, file_ptr, bfd_size_type)); | |
61 | static const char *group_signature | |
62 | PARAMS ((bfd *, Elf_Internal_Shdr *)); | |
63 | static bfd_boolean setup_group | |
64 | PARAMS ((bfd *, Elf_Internal_Shdr *, asection *)); | |
65 | static void merge_sections_remove_hook | |
66 | PARAMS ((bfd *, asection *)); | |
67 | static void elf_fake_sections | |
68 | PARAMS ((bfd *, asection *, PTR)); | |
69 | static bfd_boolean assign_section_numbers | |
70 | PARAMS ((bfd *)); | |
71 | static INLINE int sym_is_global | |
72 | PARAMS ((bfd *, asymbol *)); | |
73 | static bfd_boolean elf_map_symbols | |
74 | PARAMS ((bfd *)); | |
75 | static bfd_size_type get_program_header_size | |
76 | PARAMS ((bfd *)); | |
77 | static bfd_boolean elfcore_read_notes | |
78 | PARAMS ((bfd *, file_ptr, bfd_size_type)); | |
79 | static bfd_boolean elf_find_function | |
80 | PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **, | |
81 | const char **)); | |
82 | static int elfcore_make_pid | |
83 | PARAMS ((bfd *)); | |
84 | static bfd_boolean elfcore_maybe_make_sect | |
85 | PARAMS ((bfd *, char *, asection *)); | |
86 | static bfd_boolean elfcore_make_note_pseudosection | |
87 | PARAMS ((bfd *, char *, Elf_Internal_Note *)); | |
88 | static bfd_boolean elfcore_grok_prfpreg | |
89 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
90 | static bfd_boolean elfcore_grok_prxfpreg | |
91 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
92 | static bfd_boolean elfcore_grok_note | |
93 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
94 | static bfd_boolean elfcore_netbsd_get_lwpid | |
95 | PARAMS ((Elf_Internal_Note *, int *)); | |
96 | static bfd_boolean elfcore_grok_netbsd_procinfo | |
97 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
98 | static bfd_boolean elfcore_grok_netbsd_note | |
99 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
07c6e936 NC |
100 | static bfd_boolean elfcore_grok_nto_gregs |
101 | PARAMS ((bfd *, Elf_Internal_Note *, pid_t)); | |
102 | static bfd_boolean elfcore_grok_nto_status | |
103 | PARAMS ((bfd *, Elf_Internal_Note *, pid_t *)); | |
104 | static bfd_boolean elfcore_grok_nto_note | |
105 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
50b2bdb7 | 106 | |
252b5132 RH |
107 | /* Swap version information in and out. The version information is |
108 | currently size independent. If that ever changes, this code will | |
109 | need to move into elfcode.h. */ | |
110 | ||
111 | /* Swap in a Verdef structure. */ | |
112 | ||
113 | void | |
114 | _bfd_elf_swap_verdef_in (abfd, src, dst) | |
115 | bfd *abfd; | |
116 | const Elf_External_Verdef *src; | |
117 | Elf_Internal_Verdef *dst; | |
118 | { | |
dc810e39 AM |
119 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
120 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
121 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
122 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
123 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
124 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
125 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
126 | } |
127 | ||
128 | /* Swap out a Verdef structure. */ | |
129 | ||
130 | void | |
131 | _bfd_elf_swap_verdef_out (abfd, src, dst) | |
132 | bfd *abfd; | |
133 | const Elf_Internal_Verdef *src; | |
134 | Elf_External_Verdef *dst; | |
135 | { | |
dc810e39 AM |
136 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
137 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
138 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
139 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
140 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
141 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
142 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
143 | } |
144 | ||
145 | /* Swap in a Verdaux structure. */ | |
146 | ||
147 | void | |
148 | _bfd_elf_swap_verdaux_in (abfd, src, dst) | |
149 | bfd *abfd; | |
150 | const Elf_External_Verdaux *src; | |
151 | Elf_Internal_Verdaux *dst; | |
152 | { | |
dc810e39 AM |
153 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
154 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
155 | } |
156 | ||
157 | /* Swap out a Verdaux structure. */ | |
158 | ||
159 | void | |
160 | _bfd_elf_swap_verdaux_out (abfd, src, dst) | |
161 | bfd *abfd; | |
162 | const Elf_Internal_Verdaux *src; | |
163 | Elf_External_Verdaux *dst; | |
164 | { | |
dc810e39 AM |
165 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
166 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
167 | } |
168 | ||
169 | /* Swap in a Verneed structure. */ | |
170 | ||
171 | void | |
172 | _bfd_elf_swap_verneed_in (abfd, src, dst) | |
173 | bfd *abfd; | |
174 | const Elf_External_Verneed *src; | |
175 | Elf_Internal_Verneed *dst; | |
176 | { | |
dc810e39 AM |
177 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
178 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
179 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
180 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
181 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
182 | } |
183 | ||
184 | /* Swap out a Verneed structure. */ | |
185 | ||
186 | void | |
187 | _bfd_elf_swap_verneed_out (abfd, src, dst) | |
188 | bfd *abfd; | |
189 | const Elf_Internal_Verneed *src; | |
190 | Elf_External_Verneed *dst; | |
191 | { | |
dc810e39 AM |
192 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
193 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
194 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
195 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
196 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
197 | } |
198 | ||
199 | /* Swap in a Vernaux structure. */ | |
200 | ||
201 | void | |
202 | _bfd_elf_swap_vernaux_in (abfd, src, dst) | |
203 | bfd *abfd; | |
204 | const Elf_External_Vernaux *src; | |
205 | Elf_Internal_Vernaux *dst; | |
206 | { | |
dc810e39 AM |
207 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
208 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
209 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
210 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
211 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
212 | } |
213 | ||
214 | /* Swap out a Vernaux structure. */ | |
215 | ||
216 | void | |
217 | _bfd_elf_swap_vernaux_out (abfd, src, dst) | |
218 | bfd *abfd; | |
219 | const Elf_Internal_Vernaux *src; | |
220 | Elf_External_Vernaux *dst; | |
221 | { | |
dc810e39 AM |
222 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
223 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
224 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
225 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
226 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
227 | } |
228 | ||
229 | /* Swap in a Versym structure. */ | |
230 | ||
231 | void | |
232 | _bfd_elf_swap_versym_in (abfd, src, dst) | |
233 | bfd *abfd; | |
234 | const Elf_External_Versym *src; | |
235 | Elf_Internal_Versym *dst; | |
236 | { | |
dc810e39 | 237 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
238 | } |
239 | ||
240 | /* Swap out a Versym structure. */ | |
241 | ||
242 | void | |
243 | _bfd_elf_swap_versym_out (abfd, src, dst) | |
244 | bfd *abfd; | |
245 | const Elf_Internal_Versym *src; | |
246 | Elf_External_Versym *dst; | |
247 | { | |
dc810e39 | 248 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
249 | } |
250 | ||
251 | /* Standard ELF hash function. Do not change this function; you will | |
252 | cause invalid hash tables to be generated. */ | |
3a99b017 | 253 | |
252b5132 | 254 | unsigned long |
3a99b017 ILT |
255 | bfd_elf_hash (namearg) |
256 | const char *namearg; | |
252b5132 | 257 | { |
3a99b017 | 258 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
259 | unsigned long h = 0; |
260 | unsigned long g; | |
261 | int ch; | |
262 | ||
263 | while ((ch = *name++) != '\0') | |
264 | { | |
265 | h = (h << 4) + ch; | |
266 | if ((g = (h & 0xf0000000)) != 0) | |
267 | { | |
268 | h ^= g >> 24; | |
269 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
270 | this case and on some machines one insn instead of two. */ | |
271 | h ^= g; | |
272 | } | |
273 | } | |
274 | return h; | |
275 | } | |
276 | ||
277 | /* Read a specified number of bytes at a specified offset in an ELF | |
278 | file, into a newly allocated buffer, and return a pointer to the | |
c044fabd | 279 | buffer. */ |
252b5132 RH |
280 | |
281 | static char * | |
282 | elf_read (abfd, offset, size) | |
c044fabd | 283 | bfd *abfd; |
dc810e39 AM |
284 | file_ptr offset; |
285 | bfd_size_type size; | |
252b5132 RH |
286 | { |
287 | char *buf; | |
288 | ||
289 | if ((buf = bfd_alloc (abfd, size)) == NULL) | |
290 | return NULL; | |
dc810e39 | 291 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) |
252b5132 | 292 | return NULL; |
dc810e39 | 293 | if (bfd_bread ((PTR) buf, size, abfd) != size) |
252b5132 RH |
294 | { |
295 | if (bfd_get_error () != bfd_error_system_call) | |
296 | bfd_set_error (bfd_error_file_truncated); | |
297 | return NULL; | |
298 | } | |
299 | return buf; | |
300 | } | |
301 | ||
b34976b6 | 302 | bfd_boolean |
252b5132 | 303 | bfd_elf_mkobject (abfd) |
c044fabd | 304 | bfd *abfd; |
252b5132 | 305 | { |
c044fabd KH |
306 | /* This just does initialization. */ |
307 | /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */ | |
dc810e39 AM |
308 | bfd_size_type amt = sizeof (struct elf_obj_tdata); |
309 | elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt); | |
252b5132 | 310 | if (elf_tdata (abfd) == 0) |
b34976b6 | 311 | return FALSE; |
c044fabd KH |
312 | /* Since everything is done at close time, do we need any |
313 | initialization? */ | |
252b5132 | 314 | |
b34976b6 | 315 | return TRUE; |
252b5132 RH |
316 | } |
317 | ||
b34976b6 | 318 | bfd_boolean |
252b5132 | 319 | bfd_elf_mkcorefile (abfd) |
c044fabd | 320 | bfd *abfd; |
252b5132 | 321 | { |
c044fabd | 322 | /* I think this can be done just like an object file. */ |
252b5132 RH |
323 | return bfd_elf_mkobject (abfd); |
324 | } | |
325 | ||
326 | char * | |
327 | bfd_elf_get_str_section (abfd, shindex) | |
c044fabd | 328 | bfd *abfd; |
252b5132 RH |
329 | unsigned int shindex; |
330 | { | |
331 | Elf_Internal_Shdr **i_shdrp; | |
332 | char *shstrtab = NULL; | |
dc810e39 AM |
333 | file_ptr offset; |
334 | bfd_size_type shstrtabsize; | |
252b5132 RH |
335 | |
336 | i_shdrp = elf_elfsections (abfd); | |
337 | if (i_shdrp == 0 || i_shdrp[shindex] == 0) | |
338 | return 0; | |
339 | ||
340 | shstrtab = (char *) i_shdrp[shindex]->contents; | |
341 | if (shstrtab == NULL) | |
342 | { | |
c044fabd | 343 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
344 | offset = i_shdrp[shindex]->sh_offset; |
345 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
346 | shstrtab = elf_read (abfd, offset, shstrtabsize); | |
347 | i_shdrp[shindex]->contents = (PTR) shstrtab; | |
348 | } | |
349 | return shstrtab; | |
350 | } | |
351 | ||
352 | char * | |
353 | bfd_elf_string_from_elf_section (abfd, shindex, strindex) | |
c044fabd | 354 | bfd *abfd; |
252b5132 RH |
355 | unsigned int shindex; |
356 | unsigned int strindex; | |
357 | { | |
358 | Elf_Internal_Shdr *hdr; | |
359 | ||
360 | if (strindex == 0) | |
361 | return ""; | |
362 | ||
363 | hdr = elf_elfsections (abfd)[shindex]; | |
364 | ||
365 | if (hdr->contents == NULL | |
366 | && bfd_elf_get_str_section (abfd, shindex) == NULL) | |
367 | return NULL; | |
368 | ||
369 | if (strindex >= hdr->sh_size) | |
370 | { | |
371 | (*_bfd_error_handler) | |
372 | (_("%s: invalid string offset %u >= %lu for section `%s'"), | |
8f615d07 | 373 | bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size, |
252b5132 RH |
374 | ((shindex == elf_elfheader(abfd)->e_shstrndx |
375 | && strindex == hdr->sh_name) | |
376 | ? ".shstrtab" | |
377 | : elf_string_from_elf_strtab (abfd, hdr->sh_name))); | |
378 | return ""; | |
379 | } | |
380 | ||
381 | return ((char *) hdr->contents) + strindex; | |
382 | } | |
383 | ||
6cdc0ccc AM |
384 | /* Read and convert symbols to internal format. |
385 | SYMCOUNT specifies the number of symbols to read, starting from | |
386 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
387 | are non-NULL, they are used to store the internal symbols, external | |
388 | symbols, and symbol section index extensions, respectively. */ | |
389 | ||
390 | Elf_Internal_Sym * | |
391 | bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset, | |
392 | intsym_buf, extsym_buf, extshndx_buf) | |
393 | bfd *ibfd; | |
394 | Elf_Internal_Shdr *symtab_hdr; | |
395 | size_t symcount; | |
396 | size_t symoffset; | |
397 | Elf_Internal_Sym *intsym_buf; | |
398 | PTR extsym_buf; | |
399 | Elf_External_Sym_Shndx *extshndx_buf; | |
400 | { | |
401 | Elf_Internal_Shdr *shndx_hdr; | |
402 | PTR alloc_ext; | |
df622259 | 403 | const bfd_byte *esym; |
6cdc0ccc AM |
404 | Elf_External_Sym_Shndx *alloc_extshndx; |
405 | Elf_External_Sym_Shndx *shndx; | |
406 | Elf_Internal_Sym *isym; | |
407 | Elf_Internal_Sym *isymend; | |
408 | struct elf_backend_data *bed; | |
409 | size_t extsym_size; | |
410 | bfd_size_type amt; | |
411 | file_ptr pos; | |
412 | ||
413 | if (symcount == 0) | |
414 | return intsym_buf; | |
415 | ||
416 | /* Normal syms might have section extension entries. */ | |
417 | shndx_hdr = NULL; | |
418 | if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) | |
419 | shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; | |
420 | ||
421 | /* Read the symbols. */ | |
422 | alloc_ext = NULL; | |
423 | alloc_extshndx = NULL; | |
424 | bed = get_elf_backend_data (ibfd); | |
425 | extsym_size = bed->s->sizeof_sym; | |
426 | amt = symcount * extsym_size; | |
427 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; | |
428 | if (extsym_buf == NULL) | |
429 | { | |
430 | alloc_ext = bfd_malloc (amt); | |
431 | extsym_buf = alloc_ext; | |
432 | } | |
433 | if (extsym_buf == NULL | |
434 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
435 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
436 | { | |
437 | intsym_buf = NULL; | |
438 | goto out; | |
439 | } | |
440 | ||
441 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
442 | extshndx_buf = NULL; | |
443 | else | |
444 | { | |
445 | amt = symcount * sizeof (Elf_External_Sym_Shndx); | |
446 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); | |
447 | if (extshndx_buf == NULL) | |
448 | { | |
449 | alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); | |
450 | extshndx_buf = alloc_extshndx; | |
451 | } | |
452 | if (extshndx_buf == NULL | |
453 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
454 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
455 | { | |
456 | intsym_buf = NULL; | |
457 | goto out; | |
458 | } | |
459 | } | |
460 | ||
461 | if (intsym_buf == NULL) | |
462 | { | |
463 | bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym); | |
464 | intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt); | |
465 | if (intsym_buf == NULL) | |
466 | goto out; | |
467 | } | |
468 | ||
469 | /* Convert the symbols to internal form. */ | |
470 | isymend = intsym_buf + symcount; | |
471 | for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf; | |
472 | isym < isymend; | |
473 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
474 | (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym); | |
475 | ||
476 | out: | |
477 | if (alloc_ext != NULL) | |
478 | free (alloc_ext); | |
479 | if (alloc_extshndx != NULL) | |
480 | free (alloc_extshndx); | |
481 | ||
482 | return intsym_buf; | |
483 | } | |
484 | ||
5cab59f6 AM |
485 | /* Look up a symbol name. */ |
486 | const char * | |
487 | bfd_elf_local_sym_name (abfd, isym) | |
488 | bfd *abfd; | |
489 | Elf_Internal_Sym *isym; | |
490 | { | |
491 | unsigned int iname = isym->st_name; | |
492 | unsigned int shindex = elf_tdata (abfd)->symtab_hdr.sh_link; | |
493 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
494 | { | |
495 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
496 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
497 | } | |
498 | ||
499 | return bfd_elf_string_from_elf_section (abfd, shindex, iname); | |
500 | } | |
501 | ||
dbb410c3 AM |
502 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
503 | sections. The first element is the flags, the rest are section | |
504 | pointers. */ | |
505 | ||
506 | typedef union elf_internal_group { | |
507 | Elf_Internal_Shdr *shdr; | |
508 | unsigned int flags; | |
509 | } Elf_Internal_Group; | |
510 | ||
b885599b AM |
511 | /* Return the name of the group signature symbol. Why isn't the |
512 | signature just a string? */ | |
513 | ||
514 | static const char * | |
515 | group_signature (abfd, ghdr) | |
516 | bfd *abfd; | |
517 | Elf_Internal_Shdr *ghdr; | |
518 | { | |
9dce4196 | 519 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
520 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
521 | Elf_External_Sym_Shndx eshndx; | |
522 | Elf_Internal_Sym isym; | |
b885599b AM |
523 | |
524 | /* First we need to ensure the symbol table is available. */ | |
525 | if (! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
526 | return NULL; | |
527 | ||
9dce4196 AM |
528 | /* Go read the symbol. */ |
529 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
530 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
531 | &isym, esym, &eshndx) == NULL) | |
b885599b | 532 | return NULL; |
9dce4196 | 533 | |
5cab59f6 | 534 | return bfd_elf_local_sym_name (abfd, &isym); |
b885599b AM |
535 | } |
536 | ||
dbb410c3 AM |
537 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
538 | ||
b34976b6 | 539 | static bfd_boolean |
dbb410c3 AM |
540 | setup_group (abfd, hdr, newsect) |
541 | bfd *abfd; | |
542 | Elf_Internal_Shdr *hdr; | |
543 | asection *newsect; | |
544 | { | |
545 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
546 | ||
547 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
548 | is set to -1 if there are no SHT_GROUP sections. */ | |
549 | if (num_group == 0) | |
550 | { | |
551 | unsigned int i, shnum; | |
552 | ||
553 | /* First count the number of groups. If we have a SHT_GROUP | |
554 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 555 | shnum = elf_numsections (abfd); |
dbb410c3 AM |
556 | num_group = 0; |
557 | for (i = 0; i < shnum; i++) | |
558 | { | |
559 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
560 | if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8) | |
561 | num_group += 1; | |
562 | } | |
563 | ||
564 | if (num_group == 0) | |
973ffd63 | 565 | num_group = (unsigned) -1; |
dbb410c3 AM |
566 | elf_tdata (abfd)->num_group = num_group; |
567 | ||
568 | if (num_group > 0) | |
569 | { | |
570 | /* We keep a list of elf section headers for group sections, | |
571 | so we can find them quickly. */ | |
572 | bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *); | |
573 | elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt); | |
574 | if (elf_tdata (abfd)->group_sect_ptr == NULL) | |
b34976b6 | 575 | return FALSE; |
dbb410c3 AM |
576 | |
577 | num_group = 0; | |
578 | for (i = 0; i < shnum; i++) | |
579 | { | |
580 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
581 | if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8) | |
582 | { | |
973ffd63 | 583 | unsigned char *src; |
dbb410c3 AM |
584 | Elf_Internal_Group *dest; |
585 | ||
586 | /* Add to list of sections. */ | |
587 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
588 | num_group += 1; | |
589 | ||
590 | /* Read the raw contents. */ | |
591 | BFD_ASSERT (sizeof (*dest) >= 4); | |
592 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
593 | shdr->contents = bfd_alloc (abfd, amt); | |
594 | if (shdr->contents == NULL | |
595 | || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
596 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) | |
597 | != shdr->sh_size)) | |
b34976b6 | 598 | return FALSE; |
dbb410c3 AM |
599 | |
600 | /* Translate raw contents, a flag word followed by an | |
601 | array of elf section indices all in target byte order, | |
602 | to the flag word followed by an array of elf section | |
603 | pointers. */ | |
604 | src = shdr->contents + shdr->sh_size; | |
605 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
606 | while (1) | |
607 | { | |
608 | unsigned int idx; | |
609 | ||
610 | src -= 4; | |
611 | --dest; | |
612 | idx = H_GET_32 (abfd, src); | |
613 | if (src == shdr->contents) | |
614 | { | |
615 | dest->flags = idx; | |
b885599b AM |
616 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
617 | shdr->bfd_section->flags | |
618 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
619 | break; |
620 | } | |
621 | if (idx >= shnum) | |
622 | { | |
623 | ((*_bfd_error_handler) | |
624 | (_("%s: invalid SHT_GROUP entry"), | |
625 | bfd_archive_filename (abfd))); | |
626 | idx = 0; | |
627 | } | |
628 | dest->shdr = elf_elfsections (abfd)[idx]; | |
629 | } | |
630 | } | |
631 | } | |
632 | } | |
633 | } | |
634 | ||
635 | if (num_group != (unsigned) -1) | |
636 | { | |
637 | unsigned int i; | |
638 | ||
639 | for (i = 0; i < num_group; i++) | |
640 | { | |
641 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
642 | Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; | |
643 | unsigned int n_elt = shdr->sh_size / 4; | |
644 | ||
645 | /* Look through this group's sections to see if current | |
646 | section is a member. */ | |
647 | while (--n_elt != 0) | |
648 | if ((++idx)->shdr == hdr) | |
649 | { | |
e0e8c97f | 650 | asection *s = NULL; |
dbb410c3 AM |
651 | |
652 | /* We are a member of this group. Go looking through | |
653 | other members to see if any others are linked via | |
654 | next_in_group. */ | |
655 | idx = (Elf_Internal_Group *) shdr->contents; | |
656 | n_elt = shdr->sh_size / 4; | |
657 | while (--n_elt != 0) | |
658 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 659 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
660 | break; |
661 | if (n_elt != 0) | |
662 | { | |
dbb410c3 AM |
663 | /* Snarf the group name from other member, and |
664 | insert current section in circular list. */ | |
945906ff AM |
665 | elf_group_name (newsect) = elf_group_name (s); |
666 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
667 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
668 | } |
669 | else | |
670 | { | |
dbb410c3 AM |
671 | const char *gname; |
672 | ||
b885599b AM |
673 | gname = group_signature (abfd, shdr); |
674 | if (gname == NULL) | |
b34976b6 | 675 | return FALSE; |
945906ff | 676 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
677 | |
678 | /* Start a circular list with one element. */ | |
945906ff | 679 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 680 | } |
b885599b | 681 | |
9dce4196 AM |
682 | /* If the group section has been created, point to the |
683 | new member. */ | |
dbb410c3 | 684 | if (shdr->bfd_section != NULL) |
945906ff | 685 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 686 | |
dbb410c3 AM |
687 | i = num_group - 1; |
688 | break; | |
689 | } | |
690 | } | |
691 | } | |
692 | ||
945906ff | 693 | if (elf_group_name (newsect) == NULL) |
dbb410c3 AM |
694 | { |
695 | (*_bfd_error_handler) (_("%s: no group info for section %s"), | |
696 | bfd_archive_filename (abfd), newsect->name); | |
697 | } | |
b34976b6 | 698 | return TRUE; |
dbb410c3 AM |
699 | } |
700 | ||
b34976b6 | 701 | bfd_boolean |
b885599b AM |
702 | bfd_elf_discard_group (abfd, group) |
703 | bfd *abfd ATTRIBUTE_UNUSED; | |
704 | asection *group; | |
705 | { | |
706 | asection *first = elf_next_in_group (group); | |
707 | asection *s = first; | |
708 | ||
709 | while (s != NULL) | |
710 | { | |
711 | s->output_section = bfd_abs_section_ptr; | |
712 | s = elf_next_in_group (s); | |
713 | /* These lists are circular. */ | |
714 | if (s == first) | |
715 | break; | |
716 | } | |
b34976b6 | 717 | return TRUE; |
b885599b AM |
718 | } |
719 | ||
252b5132 RH |
720 | /* Make a BFD section from an ELF section. We store a pointer to the |
721 | BFD section in the bfd_section field of the header. */ | |
722 | ||
b34976b6 | 723 | bfd_boolean |
252b5132 RH |
724 | _bfd_elf_make_section_from_shdr (abfd, hdr, name) |
725 | bfd *abfd; | |
726 | Elf_Internal_Shdr *hdr; | |
727 | const char *name; | |
728 | { | |
729 | asection *newsect; | |
730 | flagword flags; | |
fa152c49 | 731 | struct elf_backend_data *bed; |
252b5132 RH |
732 | |
733 | if (hdr->bfd_section != NULL) | |
734 | { | |
735 | BFD_ASSERT (strcmp (name, | |
736 | bfd_get_section_name (abfd, hdr->bfd_section)) == 0); | |
b34976b6 | 737 | return TRUE; |
252b5132 RH |
738 | } |
739 | ||
740 | newsect = bfd_make_section_anyway (abfd, name); | |
741 | if (newsect == NULL) | |
b34976b6 | 742 | return FALSE; |
252b5132 RH |
743 | |
744 | newsect->filepos = hdr->sh_offset; | |
745 | ||
746 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
747 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
748 | || ! bfd_set_section_alignment (abfd, newsect, | |
dc810e39 | 749 | bfd_log2 ((bfd_vma) hdr->sh_addralign))) |
b34976b6 | 750 | return FALSE; |
252b5132 RH |
751 | |
752 | flags = SEC_NO_FLAGS; | |
753 | if (hdr->sh_type != SHT_NOBITS) | |
754 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 AM |
755 | if (hdr->sh_type == SHT_GROUP) |
756 | flags |= SEC_GROUP | SEC_EXCLUDE; | |
252b5132 RH |
757 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
758 | { | |
759 | flags |= SEC_ALLOC; | |
760 | if (hdr->sh_type != SHT_NOBITS) | |
761 | flags |= SEC_LOAD; | |
762 | } | |
763 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
764 | flags |= SEC_READONLY; | |
765 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
766 | flags |= SEC_CODE; | |
767 | else if ((flags & SEC_LOAD) != 0) | |
768 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
769 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
770 | { | |
771 | flags |= SEC_MERGE; | |
772 | newsect->entsize = hdr->sh_entsize; | |
773 | if ((hdr->sh_flags & SHF_STRINGS) != 0) | |
774 | flags |= SEC_STRINGS; | |
775 | } | |
dbb410c3 AM |
776 | if (hdr->sh_flags & SHF_GROUP) |
777 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 778 | return FALSE; |
13ae64f3 JJ |
779 | if ((hdr->sh_flags & SHF_TLS) != 0) |
780 | flags |= SEC_THREAD_LOCAL; | |
252b5132 RH |
781 | |
782 | /* The debugging sections appear to be recognized only by name, not | |
783 | any sort of flag. */ | |
7a6cc5fb | 784 | { |
dbf48117 | 785 | static const char *debug_sec_names [] = |
7a6cc5fb NC |
786 | { |
787 | ".debug", | |
788 | ".gnu.linkonce.wi.", | |
789 | ".line", | |
790 | ".stab" | |
791 | }; | |
792 | int i; | |
793 | ||
e0e8c97f | 794 | for (i = ARRAY_SIZE (debug_sec_names); i--;) |
7a6cc5fb NC |
795 | if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0) |
796 | break; | |
797 | ||
798 | if (i >= 0) | |
799 | flags |= SEC_DEBUGGING; | |
800 | } | |
252b5132 RH |
801 | |
802 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
803 | only link a single copy of the section. This is used to support | |
804 | g++. g++ will emit each template expansion in its own section. | |
805 | The symbols will be defined as weak, so that multiple definitions | |
806 | are permitted. The GNU linker extension is to actually discard | |
807 | all but one of the sections. */ | |
b885599b AM |
808 | if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0 |
809 | && elf_next_in_group (newsect) == NULL) | |
252b5132 RH |
810 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
811 | ||
fa152c49 JW |
812 | bed = get_elf_backend_data (abfd); |
813 | if (bed->elf_backend_section_flags) | |
814 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 815 | return FALSE; |
fa152c49 | 816 | |
252b5132 | 817 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 818 | return FALSE; |
252b5132 RH |
819 | |
820 | if ((flags & SEC_ALLOC) != 0) | |
821 | { | |
822 | Elf_Internal_Phdr *phdr; | |
823 | unsigned int i; | |
824 | ||
825 | /* Look through the phdrs to see if we need to adjust the lma. | |
826 | If all the p_paddr fields are zero, we ignore them, since | |
827 | some ELF linkers produce such output. */ | |
828 | phdr = elf_tdata (abfd)->phdr; | |
829 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
830 | { | |
831 | if (phdr->p_paddr != 0) | |
832 | break; | |
833 | } | |
834 | if (i < elf_elfheader (abfd)->e_phnum) | |
835 | { | |
836 | phdr = elf_tdata (abfd)->phdr; | |
837 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
838 | { | |
e0e8c97f NC |
839 | /* This section is part of this segment if its file |
840 | offset plus size lies within the segment's memory | |
841 | span and, if the section is loaded, the extent of the | |
47d9a591 | 842 | loaded data lies within the extent of the segment. |
bf36db18 NC |
843 | |
844 | Note - we used to check the p_paddr field as well, and | |
845 | refuse to set the LMA if it was 0. This is wrong | |
dba143ef | 846 | though, as a perfectly valid initialised segment can |
bf36db18 | 847 | have a p_paddr of zero. Some architectures, eg ARM, |
dba143ef | 848 | place special significance on the address 0 and |
bf36db18 NC |
849 | executables need to be able to have a segment which |
850 | covers this address. */ | |
252b5132 | 851 | if (phdr->p_type == PT_LOAD |
e0e8c97f NC |
852 | && (bfd_vma) hdr->sh_offset >= phdr->p_offset |
853 | && (hdr->sh_offset + hdr->sh_size | |
854 | <= phdr->p_offset + phdr->p_memsz) | |
252b5132 | 855 | && ((flags & SEC_LOAD) == 0 |
d7866f04 AM |
856 | || (hdr->sh_offset + hdr->sh_size |
857 | <= phdr->p_offset + phdr->p_filesz))) | |
252b5132 | 858 | { |
dba143ef | 859 | if ((flags & SEC_LOAD) == 0) |
d7866f04 AM |
860 | newsect->lma = (phdr->p_paddr |
861 | + hdr->sh_addr - phdr->p_vaddr); | |
dba143ef AM |
862 | else |
863 | /* We used to use the same adjustment for SEC_LOAD | |
864 | sections, but that doesn't work if the segment | |
865 | is packed with code from multiple VMAs. | |
866 | Instead we calculate the section LMA based on | |
867 | the segment LMA. It is assumed that the | |
868 | segment will contain sections with contiguous | |
869 | LMAs, even if the VMAs are not. */ | |
870 | newsect->lma = (phdr->p_paddr | |
871 | + hdr->sh_offset - phdr->p_offset); | |
d7866f04 AM |
872 | |
873 | /* With contiguous segments, we can't tell from file | |
874 | offsets whether a section with zero size should | |
875 | be placed at the end of one segment or the | |
876 | beginning of the next. Decide based on vaddr. */ | |
877 | if (hdr->sh_addr >= phdr->p_vaddr | |
878 | && (hdr->sh_addr + hdr->sh_size | |
879 | <= phdr->p_vaddr + phdr->p_memsz)) | |
880 | break; | |
252b5132 RH |
881 | } |
882 | } | |
883 | } | |
884 | } | |
885 | ||
886 | hdr->bfd_section = newsect; | |
887 | elf_section_data (newsect)->this_hdr = *hdr; | |
888 | ||
b34976b6 | 889 | return TRUE; |
252b5132 RH |
890 | } |
891 | ||
892 | /* | |
893 | INTERNAL_FUNCTION | |
894 | bfd_elf_find_section | |
895 | ||
896 | SYNOPSIS | |
897 | struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); | |
898 | ||
899 | DESCRIPTION | |
900 | Helper functions for GDB to locate the string tables. | |
901 | Since BFD hides string tables from callers, GDB needs to use an | |
902 | internal hook to find them. Sun's .stabstr, in particular, | |
903 | isn't even pointed to by the .stab section, so ordinary | |
904 | mechanisms wouldn't work to find it, even if we had some. | |
905 | */ | |
906 | ||
907 | struct elf_internal_shdr * | |
908 | bfd_elf_find_section (abfd, name) | |
c044fabd | 909 | bfd *abfd; |
252b5132 RH |
910 | char *name; |
911 | { | |
912 | Elf_Internal_Shdr **i_shdrp; | |
913 | char *shstrtab; | |
914 | unsigned int max; | |
915 | unsigned int i; | |
916 | ||
917 | i_shdrp = elf_elfsections (abfd); | |
918 | if (i_shdrp != NULL) | |
919 | { | |
9ad5cbcf AM |
920 | shstrtab = bfd_elf_get_str_section (abfd, |
921 | elf_elfheader (abfd)->e_shstrndx); | |
252b5132 RH |
922 | if (shstrtab != NULL) |
923 | { | |
9ad5cbcf | 924 | max = elf_numsections (abfd); |
252b5132 RH |
925 | for (i = 1; i < max; i++) |
926 | if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) | |
927 | return i_shdrp[i]; | |
928 | } | |
929 | } | |
930 | return 0; | |
931 | } | |
932 | ||
933 | const char *const bfd_elf_section_type_names[] = { | |
934 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
935 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
936 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
937 | }; | |
938 | ||
939 | /* ELF relocs are against symbols. If we are producing relocateable | |
940 | output, and the reloc is against an external symbol, and nothing | |
941 | has given us any additional addend, the resulting reloc will also | |
942 | be against the same symbol. In such a case, we don't want to | |
943 | change anything about the way the reloc is handled, since it will | |
944 | all be done at final link time. Rather than put special case code | |
945 | into bfd_perform_relocation, all the reloc types use this howto | |
946 | function. It just short circuits the reloc if producing | |
947 | relocateable output against an external symbol. */ | |
948 | ||
252b5132 RH |
949 | bfd_reloc_status_type |
950 | bfd_elf_generic_reloc (abfd, | |
951 | reloc_entry, | |
952 | symbol, | |
953 | data, | |
954 | input_section, | |
955 | output_bfd, | |
956 | error_message) | |
7442e600 | 957 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
958 | arelent *reloc_entry; |
959 | asymbol *symbol; | |
7442e600 | 960 | PTR data ATTRIBUTE_UNUSED; |
252b5132 RH |
961 | asection *input_section; |
962 | bfd *output_bfd; | |
7442e600 | 963 | char **error_message ATTRIBUTE_UNUSED; |
252b5132 RH |
964 | { |
965 | if (output_bfd != (bfd *) NULL | |
966 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
967 | && (! reloc_entry->howto->partial_inplace | |
968 | || reloc_entry->addend == 0)) | |
969 | { | |
970 | reloc_entry->address += input_section->output_offset; | |
971 | return bfd_reloc_ok; | |
972 | } | |
973 | ||
974 | return bfd_reloc_continue; | |
975 | } | |
976 | \f | |
d3c456e9 JJ |
977 | /* Make sure sec_info_type is cleared if sec_info is cleared too. */ |
978 | ||
979 | static void | |
980 | merge_sections_remove_hook (abfd, sec) | |
981 | bfd *abfd ATTRIBUTE_UNUSED; | |
982 | asection *sec; | |
983 | { | |
68bfbfcc AM |
984 | BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE); |
985 | sec->sec_info_type = ELF_INFO_TYPE_NONE; | |
d3c456e9 JJ |
986 | } |
987 | ||
8550eb6e JJ |
988 | /* Finish SHF_MERGE section merging. */ |
989 | ||
b34976b6 | 990 | bfd_boolean |
8550eb6e JJ |
991 | _bfd_elf_merge_sections (abfd, info) |
992 | bfd *abfd; | |
993 | struct bfd_link_info *info; | |
994 | { | |
b0f35f36 | 995 | if (!is_elf_hash_table (info)) |
b34976b6 | 996 | return FALSE; |
b0f35f36 | 997 | if (elf_hash_table (info)->merge_info) |
d3c456e9 JJ |
998 | _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info, |
999 | merge_sections_remove_hook); | |
b34976b6 | 1000 | return TRUE; |
8550eb6e | 1001 | } |
2d653fc7 AM |
1002 | |
1003 | void | |
1004 | _bfd_elf_link_just_syms (sec, info) | |
1005 | asection *sec; | |
1006 | struct bfd_link_info *info; | |
1007 | { | |
1008 | sec->output_section = bfd_abs_section_ptr; | |
1009 | sec->output_offset = sec->vma; | |
1010 | if (!is_elf_hash_table (info)) | |
1011 | return; | |
1012 | ||
68bfbfcc | 1013 | sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS; |
2d653fc7 | 1014 | } |
8550eb6e | 1015 | \f |
0ac4564e L |
1016 | /* Copy the program header and other data from one object module to |
1017 | another. */ | |
252b5132 | 1018 | |
b34976b6 | 1019 | bfd_boolean |
2d502050 L |
1020 | _bfd_elf_copy_private_bfd_data (ibfd, obfd) |
1021 | bfd *ibfd; | |
1022 | bfd *obfd; | |
1023 | { | |
1024 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1025 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1026 | return TRUE; |
2d502050 L |
1027 | |
1028 | BFD_ASSERT (!elf_flags_init (obfd) | |
1029 | || (elf_elfheader (obfd)->e_flags | |
1030 | == elf_elfheader (ibfd)->e_flags)); | |
1031 | ||
0ac4564e | 1032 | elf_gp (obfd) = elf_gp (ibfd); |
2d502050 | 1033 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; |
b34976b6 AM |
1034 | elf_flags_init (obfd) = TRUE; |
1035 | return TRUE; | |
2d502050 L |
1036 | } |
1037 | ||
f0b79d91 L |
1038 | /* Print out the program headers. */ |
1039 | ||
b34976b6 | 1040 | bfd_boolean |
252b5132 RH |
1041 | _bfd_elf_print_private_bfd_data (abfd, farg) |
1042 | bfd *abfd; | |
1043 | PTR farg; | |
1044 | { | |
1045 | FILE *f = (FILE *) farg; | |
1046 | Elf_Internal_Phdr *p; | |
1047 | asection *s; | |
1048 | bfd_byte *dynbuf = NULL; | |
1049 | ||
1050 | p = elf_tdata (abfd)->phdr; | |
1051 | if (p != NULL) | |
1052 | { | |
1053 | unsigned int i, c; | |
1054 | ||
1055 | fprintf (f, _("\nProgram Header:\n")); | |
1056 | c = elf_elfheader (abfd)->e_phnum; | |
1057 | for (i = 0; i < c; i++, p++) | |
1058 | { | |
dc810e39 | 1059 | const char *pt; |
252b5132 RH |
1060 | char buf[20]; |
1061 | ||
1062 | switch (p->p_type) | |
1063 | { | |
dc810e39 AM |
1064 | case PT_NULL: pt = "NULL"; break; |
1065 | case PT_LOAD: pt = "LOAD"; break; | |
1066 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1067 | case PT_INTERP: pt = "INTERP"; break; | |
1068 | case PT_NOTE: pt = "NOTE"; break; | |
1069 | case PT_SHLIB: pt = "SHLIB"; break; | |
1070 | case PT_PHDR: pt = "PHDR"; break; | |
13ae64f3 | 1071 | case PT_TLS: pt = "TLS"; break; |
65765700 | 1072 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; |
dc810e39 | 1073 | default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break; |
252b5132 | 1074 | } |
dc810e39 | 1075 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1076 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1077 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1078 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1079 | fprintf (f, " paddr 0x"); |
60b89a18 | 1080 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1081 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1082 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1083 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1084 | fprintf (f, " memsz 0x"); |
60b89a18 | 1085 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1086 | fprintf (f, " flags %c%c%c", |
1087 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1088 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1089 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1090 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1091 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1092 | fprintf (f, "\n"); |
1093 | } | |
1094 | } | |
1095 | ||
1096 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1097 | if (s != NULL) | |
1098 | { | |
1099 | int elfsec; | |
dc810e39 | 1100 | unsigned long shlink; |
252b5132 RH |
1101 | bfd_byte *extdyn, *extdynend; |
1102 | size_t extdynsize; | |
1103 | void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *)); | |
1104 | ||
1105 | fprintf (f, _("\nDynamic Section:\n")); | |
1106 | ||
1107 | dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size); | |
1108 | if (dynbuf == NULL) | |
1109 | goto error_return; | |
1110 | if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0, | |
1111 | s->_raw_size)) | |
1112 | goto error_return; | |
1113 | ||
1114 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
1115 | if (elfsec == -1) | |
1116 | goto error_return; | |
dc810e39 | 1117 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1118 | |
1119 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1120 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1121 | ||
1122 | extdyn = dynbuf; | |
1123 | extdynend = extdyn + s->_raw_size; | |
1124 | for (; extdyn < extdynend; extdyn += extdynsize) | |
1125 | { | |
1126 | Elf_Internal_Dyn dyn; | |
1127 | const char *name; | |
1128 | char ab[20]; | |
b34976b6 | 1129 | bfd_boolean stringp; |
252b5132 RH |
1130 | |
1131 | (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn); | |
1132 | ||
1133 | if (dyn.d_tag == DT_NULL) | |
1134 | break; | |
1135 | ||
b34976b6 | 1136 | stringp = FALSE; |
252b5132 RH |
1137 | switch (dyn.d_tag) |
1138 | { | |
1139 | default: | |
1140 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); | |
1141 | name = ab; | |
1142 | break; | |
1143 | ||
b34976b6 | 1144 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1145 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1146 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1147 | case DT_HASH: name = "HASH"; break; | |
1148 | case DT_STRTAB: name = "STRTAB"; break; | |
1149 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1150 | case DT_RELA: name = "RELA"; break; | |
1151 | case DT_RELASZ: name = "RELASZ"; break; | |
1152 | case DT_RELAENT: name = "RELAENT"; break; | |
1153 | case DT_STRSZ: name = "STRSZ"; break; | |
1154 | case DT_SYMENT: name = "SYMENT"; break; | |
1155 | case DT_INIT: name = "INIT"; break; | |
1156 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1157 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1158 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1159 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1160 | case DT_REL: name = "REL"; break; | |
1161 | case DT_RELSZ: name = "RELSZ"; break; | |
1162 | case DT_RELENT: name = "RELENT"; break; | |
1163 | case DT_PLTREL: name = "PLTREL"; break; | |
1164 | case DT_DEBUG: name = "DEBUG"; break; | |
1165 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1166 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1167 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1168 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1169 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1170 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1171 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1172 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1173 | case DT_FLAGS: name = "FLAGS"; break; |
1174 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1175 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1176 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1177 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1178 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1179 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1180 | case DT_FEATURE: name = "FEATURE"; break; | |
1181 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1182 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1183 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1184 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1185 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1186 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1187 | case DT_PLTPAD: name = "PLTPAD"; break; |
1188 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1189 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1190 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1191 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1192 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1193 | case DT_VERSYM: name = "VERSYM"; break; |
1194 | case DT_VERDEF: name = "VERDEF"; break; | |
1195 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1196 | case DT_VERNEED: name = "VERNEED"; break; | |
1197 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1198 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1199 | case DT_USED: name = "USED"; break; |
b34976b6 | 1200 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
252b5132 RH |
1201 | } |
1202 | ||
1203 | fprintf (f, " %-11s ", name); | |
1204 | if (! stringp) | |
1205 | fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val); | |
1206 | else | |
1207 | { | |
1208 | const char *string; | |
dc810e39 | 1209 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1210 | |
dc810e39 | 1211 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1212 | if (string == NULL) |
1213 | goto error_return; | |
1214 | fprintf (f, "%s", string); | |
1215 | } | |
1216 | fprintf (f, "\n"); | |
1217 | } | |
1218 | ||
1219 | free (dynbuf); | |
1220 | dynbuf = NULL; | |
1221 | } | |
1222 | ||
1223 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1224 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1225 | { | |
1226 | if (! _bfd_elf_slurp_version_tables (abfd)) | |
b34976b6 | 1227 | return FALSE; |
252b5132 RH |
1228 | } |
1229 | ||
1230 | if (elf_dynverdef (abfd) != 0) | |
1231 | { | |
1232 | Elf_Internal_Verdef *t; | |
1233 | ||
1234 | fprintf (f, _("\nVersion definitions:\n")); | |
1235 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1236 | { | |
1237 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
1238 | t->vd_flags, t->vd_hash, t->vd_nodename); | |
1239 | if (t->vd_auxptr->vda_nextptr != NULL) | |
1240 | { | |
1241 | Elf_Internal_Verdaux *a; | |
1242 | ||
1243 | fprintf (f, "\t"); | |
1244 | for (a = t->vd_auxptr->vda_nextptr; | |
1245 | a != NULL; | |
1246 | a = a->vda_nextptr) | |
1247 | fprintf (f, "%s ", a->vda_nodename); | |
1248 | fprintf (f, "\n"); | |
1249 | } | |
1250 | } | |
1251 | } | |
1252 | ||
1253 | if (elf_dynverref (abfd) != 0) | |
1254 | { | |
1255 | Elf_Internal_Verneed *t; | |
1256 | ||
1257 | fprintf (f, _("\nVersion References:\n")); | |
1258 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1259 | { | |
1260 | Elf_Internal_Vernaux *a; | |
1261 | ||
1262 | fprintf (f, _(" required from %s:\n"), t->vn_filename); | |
1263 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1264 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
1265 | a->vna_flags, a->vna_other, a->vna_nodename); | |
1266 | } | |
1267 | } | |
1268 | ||
b34976b6 | 1269 | return TRUE; |
252b5132 RH |
1270 | |
1271 | error_return: | |
1272 | if (dynbuf != NULL) | |
1273 | free (dynbuf); | |
b34976b6 | 1274 | return FALSE; |
252b5132 RH |
1275 | } |
1276 | ||
1277 | /* Display ELF-specific fields of a symbol. */ | |
1278 | ||
1279 | void | |
1280 | bfd_elf_print_symbol (abfd, filep, symbol, how) | |
1281 | bfd *abfd; | |
1282 | PTR filep; | |
1283 | asymbol *symbol; | |
1284 | bfd_print_symbol_type how; | |
1285 | { | |
1286 | FILE *file = (FILE *) filep; | |
1287 | switch (how) | |
1288 | { | |
1289 | case bfd_print_symbol_name: | |
1290 | fprintf (file, "%s", symbol->name); | |
1291 | break; | |
1292 | case bfd_print_symbol_more: | |
1293 | fprintf (file, "elf "); | |
60b89a18 | 1294 | bfd_fprintf_vma (abfd, file, symbol->value); |
252b5132 RH |
1295 | fprintf (file, " %lx", (long) symbol->flags); |
1296 | break; | |
1297 | case bfd_print_symbol_all: | |
1298 | { | |
4e8a9624 AM |
1299 | const char *section_name; |
1300 | const char *name = NULL; | |
587ff49e | 1301 | struct elf_backend_data *bed; |
7a13edea | 1302 | unsigned char st_other; |
dbb410c3 | 1303 | bfd_vma val; |
c044fabd | 1304 | |
252b5132 | 1305 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1306 | |
1307 | bed = get_elf_backend_data (abfd); | |
1308 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1309 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1310 | |
1311 | if (name == NULL) | |
1312 | { | |
7ee38065 | 1313 | name = symbol->name; |
60b89a18 | 1314 | bfd_print_symbol_vandf (abfd, (PTR) file, symbol); |
587ff49e RH |
1315 | } |
1316 | ||
252b5132 RH |
1317 | fprintf (file, " %s\t", section_name); |
1318 | /* Print the "other" value for a symbol. For common symbols, | |
1319 | we've already printed the size; now print the alignment. | |
1320 | For other symbols, we have no specified alignment, and | |
1321 | we've printed the address; now print the size. */ | |
dbb410c3 AM |
1322 | if (bfd_is_com_section (symbol->section)) |
1323 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; | |
1324 | else | |
1325 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1326 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1327 | |
1328 | /* If we have version information, print it. */ | |
1329 | if (elf_tdata (abfd)->dynversym_section != 0 | |
1330 | && (elf_tdata (abfd)->dynverdef_section != 0 | |
1331 | || elf_tdata (abfd)->dynverref_section != 0)) | |
1332 | { | |
1333 | unsigned int vernum; | |
1334 | const char *version_string; | |
1335 | ||
1336 | vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; | |
1337 | ||
1338 | if (vernum == 0) | |
1339 | version_string = ""; | |
1340 | else if (vernum == 1) | |
1341 | version_string = "Base"; | |
1342 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1343 | version_string = | |
1344 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1345 | else | |
1346 | { | |
1347 | Elf_Internal_Verneed *t; | |
1348 | ||
1349 | version_string = ""; | |
1350 | for (t = elf_tdata (abfd)->verref; | |
1351 | t != NULL; | |
1352 | t = t->vn_nextref) | |
1353 | { | |
1354 | Elf_Internal_Vernaux *a; | |
1355 | ||
1356 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1357 | { | |
1358 | if (a->vna_other == vernum) | |
1359 | { | |
1360 | version_string = a->vna_nodename; | |
1361 | break; | |
1362 | } | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) | |
1368 | fprintf (file, " %-11s", version_string); | |
1369 | else | |
1370 | { | |
1371 | int i; | |
1372 | ||
1373 | fprintf (file, " (%s)", version_string); | |
1374 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1375 | putc (' ', file); | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1380 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1381 | |
7a13edea NC |
1382 | switch (st_other) |
1383 | { | |
1384 | case 0: break; | |
1385 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1386 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1387 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1388 | default: | |
1389 | /* Some other non-defined flags are also present, so print | |
1390 | everything hex. */ | |
1391 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1392 | } | |
252b5132 | 1393 | |
587ff49e | 1394 | fprintf (file, " %s", name); |
252b5132 RH |
1395 | } |
1396 | break; | |
1397 | } | |
1398 | } | |
1399 | \f | |
1400 | /* Create an entry in an ELF linker hash table. */ | |
1401 | ||
1402 | struct bfd_hash_entry * | |
1403 | _bfd_elf_link_hash_newfunc (entry, table, string) | |
1404 | struct bfd_hash_entry *entry; | |
1405 | struct bfd_hash_table *table; | |
1406 | const char *string; | |
1407 | { | |
252b5132 RH |
1408 | /* Allocate the structure if it has not already been allocated by a |
1409 | subclass. */ | |
51b64d56 AM |
1410 | if (entry == NULL) |
1411 | { | |
1412 | entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); | |
1413 | if (entry == NULL) | |
1414 | return entry; | |
1415 | } | |
252b5132 RH |
1416 | |
1417 | /* Call the allocation method of the superclass. */ | |
51b64d56 AM |
1418 | entry = _bfd_link_hash_newfunc (entry, table, string); |
1419 | if (entry != NULL) | |
252b5132 | 1420 | { |
51b64d56 AM |
1421 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; |
1422 | struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; | |
1423 | ||
252b5132 RH |
1424 | /* Set local fields. */ |
1425 | ret->indx = -1; | |
252b5132 RH |
1426 | ret->dynindx = -1; |
1427 | ret->dynstr_index = 0; | |
73722af0 | 1428 | ret->elf_hash_value = 0; |
252b5132 | 1429 | ret->weakdef = NULL; |
51b64d56 | 1430 | ret->linker_section_pointer = NULL; |
252b5132 | 1431 | ret->verinfo.verdef = NULL; |
252b5132 | 1432 | ret->vtable_entries_size = 0; |
73722af0 | 1433 | ret->vtable_entries_used = NULL; |
252b5132 | 1434 | ret->vtable_parent = NULL; |
5cab59f6 AM |
1435 | ret->got = htab->init_refcount; |
1436 | ret->plt = htab->init_refcount; | |
73722af0 | 1437 | ret->size = 0; |
252b5132 RH |
1438 | ret->type = STT_NOTYPE; |
1439 | ret->other = 0; | |
1440 | /* Assume that we have been called by a non-ELF symbol reader. | |
1441 | This flag is then reset by the code which reads an ELF input | |
1442 | file. This ensures that a symbol created by a non-ELF symbol | |
1443 | reader will have the flag set correctly. */ | |
1444 | ret->elf_link_hash_flags = ELF_LINK_NON_ELF; | |
1445 | } | |
1446 | ||
51b64d56 | 1447 | return entry; |
252b5132 RH |
1448 | } |
1449 | ||
2920b85c | 1450 | /* Copy data from an indirect symbol to its direct symbol, hiding the |
0a991dfe | 1451 | old indirect symbol. Also used for copying flags to a weakdef. */ |
2920b85c | 1452 | |
c61b8717 | 1453 | void |
b48fa14c AM |
1454 | _bfd_elf_link_hash_copy_indirect (bed, dir, ind) |
1455 | struct elf_backend_data *bed; | |
2920b85c RH |
1456 | struct elf_link_hash_entry *dir, *ind; |
1457 | { | |
3c3e9281 | 1458 | bfd_signed_vma tmp; |
b48fa14c | 1459 | bfd_signed_vma lowest_valid = bed->can_refcount; |
3c3e9281 | 1460 | |
2920b85c RH |
1461 | /* Copy down any references that we may have already seen to the |
1462 | symbol which just became indirect. */ | |
1463 | ||
1464 | dir->elf_link_hash_flags |= | |
1465 | (ind->elf_link_hash_flags | |
1466 | & (ELF_LINK_HASH_REF_DYNAMIC | |
1467 | | ELF_LINK_HASH_REF_REGULAR | |
1468 | | ELF_LINK_HASH_REF_REGULAR_NONWEAK | |
1469 | | ELF_LINK_NON_GOT_REF)); | |
1470 | ||
1e370bd2 | 1471 | if (ind->root.type != bfd_link_hash_indirect) |
0a991dfe AM |
1472 | return; |
1473 | ||
51b64d56 | 1474 | /* Copy over the global and procedure linkage table refcount entries. |
2920b85c | 1475 | These may have been already set up by a check_relocs routine. */ |
3c3e9281 | 1476 | tmp = dir->got.refcount; |
b48fa14c | 1477 | if (tmp < lowest_valid) |
2920b85c | 1478 | { |
51b64d56 | 1479 | dir->got.refcount = ind->got.refcount; |
3c3e9281 | 1480 | ind->got.refcount = tmp; |
2920b85c | 1481 | } |
3c3e9281 | 1482 | else |
b48fa14c | 1483 | BFD_ASSERT (ind->got.refcount < lowest_valid); |
2920b85c | 1484 | |
3c3e9281 | 1485 | tmp = dir->plt.refcount; |
b48fa14c | 1486 | if (tmp < lowest_valid) |
2920b85c | 1487 | { |
51b64d56 | 1488 | dir->plt.refcount = ind->plt.refcount; |
3c3e9281 | 1489 | ind->plt.refcount = tmp; |
2920b85c | 1490 | } |
3c3e9281 | 1491 | else |
b48fa14c | 1492 | BFD_ASSERT (ind->plt.refcount < lowest_valid); |
2920b85c RH |
1493 | |
1494 | if (dir->dynindx == -1) | |
1495 | { | |
1496 | dir->dynindx = ind->dynindx; | |
1497 | dir->dynstr_index = ind->dynstr_index; | |
1498 | ind->dynindx = -1; | |
1499 | ind->dynstr_index = 0; | |
1500 | } | |
3c3e9281 AM |
1501 | else |
1502 | BFD_ASSERT (ind->dynindx == -1); | |
2920b85c RH |
1503 | } |
1504 | ||
c61b8717 | 1505 | void |
e5094212 AM |
1506 | _bfd_elf_link_hash_hide_symbol (info, h, force_local) |
1507 | struct bfd_link_info *info; | |
2920b85c | 1508 | struct elf_link_hash_entry *h; |
b34976b6 | 1509 | bfd_boolean force_local; |
2920b85c | 1510 | { |
5cab59f6 | 1511 | h->plt = elf_hash_table (info)->init_offset; |
e5094212 AM |
1512 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; |
1513 | if (force_local) | |
1514 | { | |
1515 | h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; | |
1516 | if (h->dynindx != -1) | |
1517 | { | |
1518 | h->dynindx = -1; | |
1519 | _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, | |
1520 | h->dynstr_index); | |
1521 | } | |
1522 | } | |
2920b85c RH |
1523 | } |
1524 | ||
252b5132 RH |
1525 | /* Initialize an ELF linker hash table. */ |
1526 | ||
b34976b6 | 1527 | bfd_boolean |
252b5132 RH |
1528 | _bfd_elf_link_hash_table_init (table, abfd, newfunc) |
1529 | struct elf_link_hash_table *table; | |
1530 | bfd *abfd; | |
b34976b6 AM |
1531 | struct bfd_hash_entry *(*newfunc) |
1532 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, | |
1533 | const char *)); | |
252b5132 | 1534 | { |
b34976b6 | 1535 | bfd_boolean ret; |
8ea2e4bd | 1536 | |
b34976b6 | 1537 | table->dynamic_sections_created = FALSE; |
252b5132 | 1538 | table->dynobj = NULL; |
963f13ec AO |
1539 | /* Make sure can_refcount is extended to the width and signedness of |
1540 | init_refcount before we subtract one from it. */ | |
5cab59f6 AM |
1541 | table->init_refcount.refcount = get_elf_backend_data (abfd)->can_refcount; |
1542 | table->init_refcount.refcount -= 1; | |
1543 | table->init_offset.offset = -(bfd_vma) 1; | |
252b5132 RH |
1544 | /* The first dynamic symbol is a dummy. */ |
1545 | table->dynsymcount = 1; | |
1546 | table->dynstr = NULL; | |
1547 | table->bucketcount = 0; | |
1548 | table->needed = NULL; | |
1549 | table->hgot = NULL; | |
1550 | table->stab_info = NULL; | |
f5fa8ca2 | 1551 | table->merge_info = NULL; |
73722af0 | 1552 | memset (&table->eh_info, 0, sizeof (table->eh_info)); |
1ae00f9d | 1553 | table->dynlocal = NULL; |
73722af0 AM |
1554 | table->runpath = NULL; |
1555 | table->tls_segment = NULL; | |
1556 | table->loaded = NULL; | |
1557 | ||
1558 | ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc); | |
8ea2e4bd NC |
1559 | table->root.type = bfd_link_elf_hash_table; |
1560 | ||
1561 | return ret; | |
252b5132 RH |
1562 | } |
1563 | ||
1564 | /* Create an ELF linker hash table. */ | |
1565 | ||
1566 | struct bfd_link_hash_table * | |
1567 | _bfd_elf_link_hash_table_create (abfd) | |
1568 | bfd *abfd; | |
1569 | { | |
1570 | struct elf_link_hash_table *ret; | |
dc810e39 | 1571 | bfd_size_type amt = sizeof (struct elf_link_hash_table); |
252b5132 | 1572 | |
e2d34d7d | 1573 | ret = (struct elf_link_hash_table *) bfd_malloc (amt); |
252b5132 RH |
1574 | if (ret == (struct elf_link_hash_table *) NULL) |
1575 | return NULL; | |
1576 | ||
1577 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc)) | |
1578 | { | |
e2d34d7d | 1579 | free (ret); |
252b5132 RH |
1580 | return NULL; |
1581 | } | |
1582 | ||
1583 | return &ret->root; | |
1584 | } | |
1585 | ||
1586 | /* This is a hook for the ELF emulation code in the generic linker to | |
1587 | tell the backend linker what file name to use for the DT_NEEDED | |
1588 | entry for a dynamic object. The generic linker passes name as an | |
1589 | empty string to indicate that no DT_NEEDED entry should be made. */ | |
1590 | ||
1591 | void | |
1592 | bfd_elf_set_dt_needed_name (abfd, name) | |
1593 | bfd *abfd; | |
1594 | const char *name; | |
1595 | { | |
1596 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
1597 | && bfd_get_format (abfd) == bfd_object) | |
1598 | elf_dt_name (abfd) = name; | |
1599 | } | |
1600 | ||
74816898 L |
1601 | void |
1602 | bfd_elf_set_dt_needed_soname (abfd, name) | |
1603 | bfd *abfd; | |
1604 | const char *name; | |
1605 | { | |
1606 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
1607 | && bfd_get_format (abfd) == bfd_object) | |
1608 | elf_dt_soname (abfd) = name; | |
1609 | } | |
1610 | ||
252b5132 RH |
1611 | /* Get the list of DT_NEEDED entries for a link. This is a hook for |
1612 | the linker ELF emulation code. */ | |
1613 | ||
1614 | struct bfd_link_needed_list * | |
1615 | bfd_elf_get_needed_list (abfd, info) | |
7442e600 | 1616 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
1617 | struct bfd_link_info *info; |
1618 | { | |
1619 | if (info->hash->creator->flavour != bfd_target_elf_flavour) | |
1620 | return NULL; | |
1621 | return elf_hash_table (info)->needed; | |
1622 | } | |
1623 | ||
a963dc6a L |
1624 | /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a |
1625 | hook for the linker ELF emulation code. */ | |
1626 | ||
1627 | struct bfd_link_needed_list * | |
1628 | bfd_elf_get_runpath_list (abfd, info) | |
1629 | bfd *abfd ATTRIBUTE_UNUSED; | |
1630 | struct bfd_link_info *info; | |
1631 | { | |
1632 | if (info->hash->creator->flavour != bfd_target_elf_flavour) | |
1633 | return NULL; | |
1634 | return elf_hash_table (info)->runpath; | |
1635 | } | |
1636 | ||
252b5132 RH |
1637 | /* Get the name actually used for a dynamic object for a link. This |
1638 | is the SONAME entry if there is one. Otherwise, it is the string | |
1639 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
1640 | ||
1641 | const char * | |
1642 | bfd_elf_get_dt_soname (abfd) | |
1643 | bfd *abfd; | |
1644 | { | |
1645 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
1646 | && bfd_get_format (abfd) == bfd_object) | |
1647 | return elf_dt_name (abfd); | |
1648 | return NULL; | |
1649 | } | |
1650 | ||
1651 | /* Get the list of DT_NEEDED entries from a BFD. This is a hook for | |
1652 | the ELF linker emulation code. */ | |
1653 | ||
b34976b6 | 1654 | bfd_boolean |
252b5132 RH |
1655 | bfd_elf_get_bfd_needed_list (abfd, pneeded) |
1656 | bfd *abfd; | |
1657 | struct bfd_link_needed_list **pneeded; | |
1658 | { | |
1659 | asection *s; | |
1660 | bfd_byte *dynbuf = NULL; | |
1661 | int elfsec; | |
dc810e39 | 1662 | unsigned long shlink; |
252b5132 RH |
1663 | bfd_byte *extdyn, *extdynend; |
1664 | size_t extdynsize; | |
1665 | void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *)); | |
1666 | ||
1667 | *pneeded = NULL; | |
1668 | ||
1669 | if (bfd_get_flavour (abfd) != bfd_target_elf_flavour | |
1670 | || bfd_get_format (abfd) != bfd_object) | |
b34976b6 | 1671 | return TRUE; |
252b5132 RH |
1672 | |
1673 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1674 | if (s == NULL || s->_raw_size == 0) | |
b34976b6 | 1675 | return TRUE; |
252b5132 RH |
1676 | |
1677 | dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size); | |
1678 | if (dynbuf == NULL) | |
1679 | goto error_return; | |
1680 | ||
1681 | if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0, | |
1682 | s->_raw_size)) | |
1683 | goto error_return; | |
1684 | ||
1685 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
1686 | if (elfsec == -1) | |
1687 | goto error_return; | |
1688 | ||
dc810e39 | 1689 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1690 | |
1691 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1692 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1693 | ||
1694 | extdyn = dynbuf; | |
1695 | extdynend = extdyn + s->_raw_size; | |
1696 | for (; extdyn < extdynend; extdyn += extdynsize) | |
1697 | { | |
1698 | Elf_Internal_Dyn dyn; | |
1699 | ||
1700 | (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn); | |
1701 | ||
1702 | if (dyn.d_tag == DT_NULL) | |
1703 | break; | |
1704 | ||
1705 | if (dyn.d_tag == DT_NEEDED) | |
1706 | { | |
1707 | const char *string; | |
1708 | struct bfd_link_needed_list *l; | |
dc810e39 AM |
1709 | unsigned int tagv = dyn.d_un.d_val; |
1710 | bfd_size_type amt; | |
252b5132 | 1711 | |
dc810e39 | 1712 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1713 | if (string == NULL) |
1714 | goto error_return; | |
1715 | ||
dc810e39 AM |
1716 | amt = sizeof *l; |
1717 | l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); | |
252b5132 RH |
1718 | if (l == NULL) |
1719 | goto error_return; | |
1720 | ||
1721 | l->by = abfd; | |
1722 | l->name = string; | |
1723 | l->next = *pneeded; | |
1724 | *pneeded = l; | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | free (dynbuf); | |
1729 | ||
b34976b6 | 1730 | return TRUE; |
252b5132 RH |
1731 | |
1732 | error_return: | |
1733 | if (dynbuf != NULL) | |
1734 | free (dynbuf); | |
b34976b6 | 1735 | return FALSE; |
252b5132 RH |
1736 | } |
1737 | \f | |
1738 | /* Allocate an ELF string table--force the first byte to be zero. */ | |
1739 | ||
1740 | struct bfd_strtab_hash * | |
1741 | _bfd_elf_stringtab_init () | |
1742 | { | |
1743 | struct bfd_strtab_hash *ret; | |
1744 | ||
1745 | ret = _bfd_stringtab_init (); | |
1746 | if (ret != NULL) | |
1747 | { | |
1748 | bfd_size_type loc; | |
1749 | ||
b34976b6 | 1750 | loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); |
252b5132 RH |
1751 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); |
1752 | if (loc == (bfd_size_type) -1) | |
1753 | { | |
1754 | _bfd_stringtab_free (ret); | |
1755 | ret = NULL; | |
1756 | } | |
1757 | } | |
1758 | return ret; | |
1759 | } | |
1760 | \f | |
1761 | /* ELF .o/exec file reading */ | |
1762 | ||
c044fabd | 1763 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1764 | |
b34976b6 | 1765 | bfd_boolean |
252b5132 RH |
1766 | bfd_section_from_shdr (abfd, shindex) |
1767 | bfd *abfd; | |
1768 | unsigned int shindex; | |
1769 | { | |
1770 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; | |
1771 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
1772 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
90937f86 | 1773 | const char *name; |
252b5132 RH |
1774 | |
1775 | name = elf_string_from_elf_strtab (abfd, hdr->sh_name); | |
1776 | ||
1777 | switch (hdr->sh_type) | |
1778 | { | |
1779 | case SHT_NULL: | |
1780 | /* Inactive section. Throw it away. */ | |
b34976b6 | 1781 | return TRUE; |
252b5132 RH |
1782 | |
1783 | case SHT_PROGBITS: /* Normal section with contents. */ | |
252b5132 RH |
1784 | case SHT_NOBITS: /* .bss section. */ |
1785 | case SHT_HASH: /* .hash section. */ | |
1786 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
1787 | case SHT_INIT_ARRAY: /* .init_array section. */ |
1788 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
1789 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
252b5132 RH |
1790 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
1791 | ||
797fc050 AM |
1792 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
1793 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
b34976b6 | 1794 | return FALSE; |
797fc050 AM |
1795 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
1796 | { | |
1797 | Elf_Internal_Shdr *dynsymhdr; | |
1798 | ||
1799 | /* The shared libraries distributed with hpux11 have a bogus | |
1800 | sh_link field for the ".dynamic" section. Find the | |
1801 | string table for the ".dynsym" section instead. */ | |
1802 | if (elf_dynsymtab (abfd) != 0) | |
1803 | { | |
1804 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
1805 | hdr->sh_link = dynsymhdr->sh_link; | |
1806 | } | |
1807 | else | |
1808 | { | |
1809 | unsigned int i, num_sec; | |
1810 | ||
1811 | num_sec = elf_numsections (abfd); | |
1812 | for (i = 1; i < num_sec; i++) | |
1813 | { | |
1814 | dynsymhdr = elf_elfsections (abfd)[i]; | |
1815 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
1816 | { | |
1817 | hdr->sh_link = dynsymhdr->sh_link; | |
1818 | break; | |
1819 | } | |
1820 | } | |
1821 | } | |
1822 | } | |
1823 | break; | |
1824 | ||
252b5132 RH |
1825 | case SHT_SYMTAB: /* A symbol table */ |
1826 | if (elf_onesymtab (abfd) == shindex) | |
b34976b6 | 1827 | return TRUE; |
252b5132 RH |
1828 | |
1829 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
1830 | BFD_ASSERT (elf_onesymtab (abfd) == 0); | |
1831 | elf_onesymtab (abfd) = shindex; | |
1832 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
1833 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; | |
1834 | abfd->flags |= HAS_SYMS; | |
1835 | ||
1836 | /* Sometimes a shared object will map in the symbol table. If | |
1837 | SHF_ALLOC is set, and this is a shared object, then we also | |
1838 | treat this section as a BFD section. We can not base the | |
1839 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1840 | set in a relocateable object file, which would confuse the | |
1841 | linker. */ | |
1842 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
1843 | && (abfd->flags & DYNAMIC) != 0 | |
1844 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
b34976b6 | 1845 | return FALSE; |
252b5132 | 1846 | |
b34976b6 | 1847 | return TRUE; |
252b5132 RH |
1848 | |
1849 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
1850 | if (elf_dynsymtab (abfd) == shindex) | |
b34976b6 | 1851 | return TRUE; |
252b5132 RH |
1852 | |
1853 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
1854 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
1855 | elf_dynsymtab (abfd) = shindex; | |
1856 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
1857 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
1858 | abfd->flags |= HAS_SYMS; | |
1859 | ||
1860 | /* Besides being a symbol table, we also treat this as a regular | |
1861 | section, so that objcopy can handle it. */ | |
1862 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1863 | ||
9ad5cbcf AM |
1864 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ |
1865 | if (elf_symtab_shndx (abfd) == shindex) | |
b34976b6 | 1866 | return TRUE; |
9ad5cbcf AM |
1867 | |
1868 | /* Get the associated symbol table. */ | |
1869 | if (! bfd_section_from_shdr (abfd, hdr->sh_link) | |
1870 | || hdr->sh_link != elf_onesymtab (abfd)) | |
b34976b6 | 1871 | return FALSE; |
9ad5cbcf AM |
1872 | |
1873 | elf_symtab_shndx (abfd) = shindex; | |
1874 | elf_tdata (abfd)->symtab_shndx_hdr = *hdr; | |
1875 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; | |
b34976b6 | 1876 | return TRUE; |
9ad5cbcf | 1877 | |
252b5132 RH |
1878 | case SHT_STRTAB: /* A string table */ |
1879 | if (hdr->bfd_section != NULL) | |
b34976b6 | 1880 | return TRUE; |
252b5132 RH |
1881 | if (ehdr->e_shstrndx == shindex) |
1882 | { | |
1883 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1884 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
b34976b6 | 1885 | return TRUE; |
252b5132 RH |
1886 | } |
1887 | { | |
9ad5cbcf | 1888 | unsigned int i, num_sec; |
252b5132 | 1889 | |
9ad5cbcf AM |
1890 | num_sec = elf_numsections (abfd); |
1891 | for (i = 1; i < num_sec; i++) | |
252b5132 RH |
1892 | { |
1893 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1894 | if (hdr2->sh_link == shindex) | |
1895 | { | |
1896 | if (! bfd_section_from_shdr (abfd, i)) | |
b34976b6 | 1897 | return FALSE; |
252b5132 RH |
1898 | if (elf_onesymtab (abfd) == i) |
1899 | { | |
1900 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1901 | elf_elfsections (abfd)[shindex] = | |
1902 | &elf_tdata (abfd)->strtab_hdr; | |
b34976b6 | 1903 | return TRUE; |
252b5132 RH |
1904 | } |
1905 | if (elf_dynsymtab (abfd) == i) | |
1906 | { | |
1907 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
1908 | elf_elfsections (abfd)[shindex] = hdr = | |
1909 | &elf_tdata (abfd)->dynstrtab_hdr; | |
1910 | /* We also treat this as a regular section, so | |
1911 | that objcopy can handle it. */ | |
1912 | break; | |
1913 | } | |
1914 | #if 0 /* Not handling other string tables specially right now. */ | |
1915 | hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ | |
1916 | /* We have a strtab for some random other section. */ | |
1917 | newsect = (asection *) hdr2->bfd_section; | |
1918 | if (!newsect) | |
1919 | break; | |
1920 | hdr->bfd_section = newsect; | |
1921 | hdr2 = &elf_section_data (newsect)->str_hdr; | |
1922 | *hdr2 = *hdr; | |
1923 | elf_elfsections (abfd)[shindex] = hdr2; | |
1924 | #endif | |
1925 | } | |
1926 | } | |
1927 | } | |
1928 | ||
1929 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1930 | ||
1931 | case SHT_REL: | |
1932 | case SHT_RELA: | |
1933 | /* *These* do a lot of work -- but build no sections! */ | |
1934 | { | |
1935 | asection *target_sect; | |
1936 | Elf_Internal_Shdr *hdr2; | |
9ad5cbcf | 1937 | unsigned int num_sec = elf_numsections (abfd); |
252b5132 | 1938 | |
03ae5f59 | 1939 | /* Check for a bogus link to avoid crashing. */ |
9ad5cbcf AM |
1940 | if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE) |
1941 | || hdr->sh_link >= num_sec) | |
03ae5f59 ILT |
1942 | { |
1943 | ((*_bfd_error_handler) | |
1944 | (_("%s: invalid link %lu for reloc section %s (index %u)"), | |
8f615d07 | 1945 | bfd_archive_filename (abfd), hdr->sh_link, name, shindex)); |
03ae5f59 ILT |
1946 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
1947 | } | |
1948 | ||
252b5132 RH |
1949 | /* For some incomprehensible reason Oracle distributes |
1950 | libraries for Solaris in which some of the objects have | |
1951 | bogus sh_link fields. It would be nice if we could just | |
1952 | reject them, but, unfortunately, some people need to use | |
1953 | them. We scan through the section headers; if we find only | |
1954 | one suitable symbol table, we clobber the sh_link to point | |
1955 | to it. I hope this doesn't break anything. */ | |
1956 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
1957 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
1958 | { | |
9ad5cbcf | 1959 | unsigned int scan; |
252b5132 RH |
1960 | int found; |
1961 | ||
1962 | found = 0; | |
9ad5cbcf | 1963 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
1964 | { |
1965 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1966 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1967 | { | |
1968 | if (found != 0) | |
1969 | { | |
1970 | found = 0; | |
1971 | break; | |
1972 | } | |
1973 | found = scan; | |
1974 | } | |
1975 | } | |
1976 | if (found != 0) | |
1977 | hdr->sh_link = found; | |
1978 | } | |
1979 | ||
1980 | /* Get the symbol table. */ | |
1981 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB | |
1982 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
b34976b6 | 1983 | return FALSE; |
252b5132 RH |
1984 | |
1985 | /* If this reloc section does not use the main symbol table we | |
1986 | don't treat it as a reloc section. BFD can't adequately | |
1987 | represent such a section, so at least for now, we don't | |
c044fabd | 1988 | try. We just present it as a normal section. We also |
60bcf0fa | 1989 | can't use it as a reloc section if it points to the null |
c044fabd | 1990 | section. */ |
60bcf0fa | 1991 | if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF) |
252b5132 RH |
1992 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
1993 | ||
1994 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
b34976b6 | 1995 | return FALSE; |
252b5132 RH |
1996 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
1997 | if (target_sect == NULL) | |
b34976b6 | 1998 | return FALSE; |
252b5132 RH |
1999 | |
2000 | if ((target_sect->flags & SEC_RELOC) == 0 | |
2001 | || target_sect->reloc_count == 0) | |
2002 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
2003 | else | |
2004 | { | |
dc810e39 | 2005 | bfd_size_type amt; |
252b5132 | 2006 | BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); |
dc810e39 AM |
2007 | amt = sizeof (*hdr2); |
2008 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); | |
252b5132 RH |
2009 | elf_section_data (target_sect)->rel_hdr2 = hdr2; |
2010 | } | |
2011 | *hdr2 = *hdr; | |
2012 | elf_elfsections (abfd)[shindex] = hdr2; | |
d9bc7a44 | 2013 | target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); |
252b5132 RH |
2014 | target_sect->flags |= SEC_RELOC; |
2015 | target_sect->relocation = NULL; | |
2016 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2017 | /* In the section to which the relocations apply, mark whether |
2018 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2019 | if (hdr->sh_size != 0) |
68bfbfcc | 2020 | target_sect->use_rela_p = hdr->sh_type == SHT_RELA; |
252b5132 | 2021 | abfd->flags |= HAS_RELOC; |
b34976b6 | 2022 | return TRUE; |
252b5132 RH |
2023 | } |
2024 | break; | |
2025 | ||
2026 | case SHT_GNU_verdef: | |
2027 | elf_dynverdef (abfd) = shindex; | |
2028 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
2029 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
2030 | break; | |
2031 | ||
2032 | case SHT_GNU_versym: | |
2033 | elf_dynversym (abfd) = shindex; | |
2034 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
2035 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
2036 | break; | |
2037 | ||
2038 | case SHT_GNU_verneed: | |
2039 | elf_dynverref (abfd) = shindex; | |
2040 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
2041 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
2042 | break; | |
2043 | ||
2044 | case SHT_SHLIB: | |
b34976b6 | 2045 | return TRUE; |
252b5132 | 2046 | |
dbb410c3 | 2047 | case SHT_GROUP: |
b885599b AM |
2048 | /* We need a BFD section for objcopy and relocatable linking, |
2049 | and it's handy to have the signature available as the section | |
2050 | name. */ | |
2051 | name = group_signature (abfd, hdr); | |
2052 | if (name == NULL) | |
b34976b6 | 2053 | return FALSE; |
dbb410c3 | 2054 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name)) |
b34976b6 | 2055 | return FALSE; |
dbb410c3 AM |
2056 | if (hdr->contents != NULL) |
2057 | { | |
2058 | Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; | |
2059 | unsigned int n_elt = hdr->sh_size / 4; | |
2060 | asection *s; | |
2061 | ||
b885599b AM |
2062 | if (idx->flags & GRP_COMDAT) |
2063 | hdr->bfd_section->flags | |
2064 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
2065 | ||
dbb410c3 AM |
2066 | while (--n_elt != 0) |
2067 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 2068 | && elf_next_in_group (s) != NULL) |
dbb410c3 | 2069 | { |
945906ff | 2070 | elf_next_in_group (hdr->bfd_section) = s; |
dbb410c3 AM |
2071 | break; |
2072 | } | |
2073 | } | |
2074 | break; | |
2075 | ||
252b5132 RH |
2076 | default: |
2077 | /* Check for any processor-specific section types. */ | |
2078 | { | |
2079 | if (bed->elf_backend_section_from_shdr) | |
2080 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
2081 | } | |
2082 | break; | |
2083 | } | |
2084 | ||
b34976b6 | 2085 | return TRUE; |
252b5132 RH |
2086 | } |
2087 | ||
ec338859 AM |
2088 | /* Return the section for the local symbol specified by ABFD, R_SYMNDX. |
2089 | Return SEC for sections that have no elf section, and NULL on error. */ | |
2090 | ||
2091 | asection * | |
2092 | bfd_section_from_r_symndx (abfd, cache, sec, r_symndx) | |
2093 | bfd *abfd; | |
2094 | struct sym_sec_cache *cache; | |
2095 | asection *sec; | |
2096 | unsigned long r_symndx; | |
2097 | { | |
ec338859 | 2098 | Elf_Internal_Shdr *symtab_hdr; |
6cdc0ccc AM |
2099 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
2100 | Elf_External_Sym_Shndx eshndx; | |
2101 | Elf_Internal_Sym isym; | |
ec338859 AM |
2102 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2103 | ||
2104 | if (cache->abfd == abfd && cache->indx[ent] == r_symndx) | |
2105 | return cache->sec[ent]; | |
2106 | ||
2107 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
2108 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, |
2109 | &isym, esym, &eshndx) == NULL) | |
ec338859 | 2110 | return NULL; |
9ad5cbcf | 2111 | |
ec338859 AM |
2112 | if (cache->abfd != abfd) |
2113 | { | |
2114 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2115 | cache->abfd = abfd; | |
2116 | } | |
2117 | cache->indx[ent] = r_symndx; | |
2118 | cache->sec[ent] = sec; | |
6cdc0ccc | 2119 | if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE) |
ec338859 AM |
2120 | { |
2121 | asection *s; | |
6cdc0ccc | 2122 | s = bfd_section_from_elf_index (abfd, isym.st_shndx); |
ec338859 AM |
2123 | if (s != NULL) |
2124 | cache->sec[ent] = s; | |
2125 | } | |
2126 | return cache->sec[ent]; | |
2127 | } | |
2128 | ||
252b5132 RH |
2129 | /* Given an ELF section number, retrieve the corresponding BFD |
2130 | section. */ | |
2131 | ||
2132 | asection * | |
2133 | bfd_section_from_elf_index (abfd, index) | |
2134 | bfd *abfd; | |
2135 | unsigned int index; | |
2136 | { | |
9ad5cbcf | 2137 | if (index >= elf_numsections (abfd)) |
252b5132 RH |
2138 | return NULL; |
2139 | return elf_elfsections (abfd)[index]->bfd_section; | |
2140 | } | |
2141 | ||
b34976b6 | 2142 | bfd_boolean |
252b5132 RH |
2143 | _bfd_elf_new_section_hook (abfd, sec) |
2144 | bfd *abfd; | |
2145 | asection *sec; | |
2146 | { | |
2147 | struct bfd_elf_section_data *sdata; | |
2148 | ||
f0abc2a1 AM |
2149 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2150 | if (sdata == NULL) | |
2151 | { | |
2152 | bfd_size_type amt = sizeof (*sdata); | |
2153 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt); | |
2154 | if (sdata == NULL) | |
2155 | return FALSE; | |
2156 | sec->used_by_bfd = (PTR) sdata; | |
2157 | } | |
bf572ba0 MM |
2158 | |
2159 | /* Indicate whether or not this section should use RELA relocations. */ | |
68bfbfcc | 2160 | sec->use_rela_p = get_elf_backend_data (abfd)->default_use_rela_p; |
bf572ba0 | 2161 | |
b34976b6 | 2162 | return TRUE; |
252b5132 RH |
2163 | } |
2164 | ||
2165 | /* Create a new bfd section from an ELF program header. | |
2166 | ||
2167 | Since program segments have no names, we generate a synthetic name | |
2168 | of the form segment<NUM>, where NUM is generally the index in the | |
2169 | program header table. For segments that are split (see below) we | |
2170 | generate the names segment<NUM>a and segment<NUM>b. | |
2171 | ||
2172 | Note that some program segments may have a file size that is different than | |
2173 | (less than) the memory size. All this means is that at execution the | |
2174 | system must allocate the amount of memory specified by the memory size, | |
2175 | but only initialize it with the first "file size" bytes read from the | |
2176 | file. This would occur for example, with program segments consisting | |
2177 | of combined data+bss. | |
2178 | ||
2179 | To handle the above situation, this routine generates TWO bfd sections | |
2180 | for the single program segment. The first has the length specified by | |
2181 | the file size of the segment, and the second has the length specified | |
2182 | by the difference between the two sizes. In effect, the segment is split | |
2183 | into it's initialized and uninitialized parts. | |
2184 | ||
2185 | */ | |
2186 | ||
b34976b6 | 2187 | bfd_boolean |
20cfcaae | 2188 | _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename) |
252b5132 RH |
2189 | bfd *abfd; |
2190 | Elf_Internal_Phdr *hdr; | |
2191 | int index; | |
20cfcaae | 2192 | const char *typename; |
252b5132 RH |
2193 | { |
2194 | asection *newsect; | |
2195 | char *name; | |
2196 | char namebuf[64]; | |
d4c88bbb | 2197 | size_t len; |
252b5132 RH |
2198 | int split; |
2199 | ||
2200 | split = ((hdr->p_memsz > 0) | |
2201 | && (hdr->p_filesz > 0) | |
2202 | && (hdr->p_memsz > hdr->p_filesz)); | |
27ac83bf | 2203 | sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : ""); |
d4c88bbb AM |
2204 | len = strlen (namebuf) + 1; |
2205 | name = bfd_alloc (abfd, (bfd_size_type) len); | |
252b5132 | 2206 | if (!name) |
b34976b6 | 2207 | return FALSE; |
d4c88bbb | 2208 | memcpy (name, namebuf, len); |
252b5132 RH |
2209 | newsect = bfd_make_section (abfd, name); |
2210 | if (newsect == NULL) | |
b34976b6 | 2211 | return FALSE; |
252b5132 RH |
2212 | newsect->vma = hdr->p_vaddr; |
2213 | newsect->lma = hdr->p_paddr; | |
2214 | newsect->_raw_size = hdr->p_filesz; | |
2215 | newsect->filepos = hdr->p_offset; | |
2216 | newsect->flags |= SEC_HAS_CONTENTS; | |
2217 | if (hdr->p_type == PT_LOAD) | |
2218 | { | |
2219 | newsect->flags |= SEC_ALLOC; | |
2220 | newsect->flags |= SEC_LOAD; | |
2221 | if (hdr->p_flags & PF_X) | |
2222 | { | |
2223 | /* FIXME: all we known is that it has execute PERMISSION, | |
c044fabd | 2224 | may be data. */ |
252b5132 RH |
2225 | newsect->flags |= SEC_CODE; |
2226 | } | |
2227 | } | |
2228 | if (!(hdr->p_flags & PF_W)) | |
2229 | { | |
2230 | newsect->flags |= SEC_READONLY; | |
2231 | } | |
2232 | ||
2233 | if (split) | |
2234 | { | |
27ac83bf | 2235 | sprintf (namebuf, "%s%db", typename, index); |
d4c88bbb AM |
2236 | len = strlen (namebuf) + 1; |
2237 | name = bfd_alloc (abfd, (bfd_size_type) len); | |
252b5132 | 2238 | if (!name) |
b34976b6 | 2239 | return FALSE; |
d4c88bbb | 2240 | memcpy (name, namebuf, len); |
252b5132 RH |
2241 | newsect = bfd_make_section (abfd, name); |
2242 | if (newsect == NULL) | |
b34976b6 | 2243 | return FALSE; |
252b5132 RH |
2244 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2245 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
2246 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; | |
2247 | if (hdr->p_type == PT_LOAD) | |
2248 | { | |
2249 | newsect->flags |= SEC_ALLOC; | |
2250 | if (hdr->p_flags & PF_X) | |
2251 | newsect->flags |= SEC_CODE; | |
2252 | } | |
2253 | if (!(hdr->p_flags & PF_W)) | |
2254 | newsect->flags |= SEC_READONLY; | |
2255 | } | |
2256 | ||
b34976b6 | 2257 | return TRUE; |
252b5132 RH |
2258 | } |
2259 | ||
b34976b6 | 2260 | bfd_boolean |
20cfcaae NC |
2261 | bfd_section_from_phdr (abfd, hdr, index) |
2262 | bfd *abfd; | |
2263 | Elf_Internal_Phdr *hdr; | |
2264 | int index; | |
2265 | { | |
2266 | struct elf_backend_data *bed; | |
2267 | ||
2268 | switch (hdr->p_type) | |
2269 | { | |
2270 | case PT_NULL: | |
2271 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null"); | |
2272 | ||
2273 | case PT_LOAD: | |
2274 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load"); | |
2275 | ||
2276 | case PT_DYNAMIC: | |
2277 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic"); | |
2278 | ||
2279 | case PT_INTERP: | |
2280 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp"); | |
2281 | ||
2282 | case PT_NOTE: | |
2283 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note")) | |
b34976b6 | 2284 | return FALSE; |
dc810e39 | 2285 | if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2286 | return FALSE; |
2287 | return TRUE; | |
20cfcaae NC |
2288 | |
2289 | case PT_SHLIB: | |
2290 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib"); | |
2291 | ||
2292 | case PT_PHDR: | |
2293 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr"); | |
2294 | ||
2295 | default: | |
2296 | /* Check for any processor-specific program segment types. | |
c044fabd | 2297 | If no handler for them, default to making "segment" sections. */ |
20cfcaae NC |
2298 | bed = get_elf_backend_data (abfd); |
2299 | if (bed->elf_backend_section_from_phdr) | |
2300 | return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index); | |
2301 | else | |
2302 | return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment"); | |
2303 | } | |
2304 | } | |
2305 | ||
23bc299b | 2306 | /* Initialize REL_HDR, the section-header for new section, containing |
b34976b6 | 2307 | relocations against ASECT. If USE_RELA_P is TRUE, we use RELA |
23bc299b MM |
2308 | relocations; otherwise, we use REL relocations. */ |
2309 | ||
b34976b6 | 2310 | bfd_boolean |
23bc299b MM |
2311 | _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p) |
2312 | bfd *abfd; | |
2313 | Elf_Internal_Shdr *rel_hdr; | |
2314 | asection *asect; | |
b34976b6 | 2315 | bfd_boolean use_rela_p; |
23bc299b MM |
2316 | { |
2317 | char *name; | |
dc810e39 AM |
2318 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
2319 | bfd_size_type amt = sizeof ".rela" + strlen (asect->name); | |
23bc299b | 2320 | |
dc810e39 | 2321 | name = bfd_alloc (abfd, amt); |
23bc299b | 2322 | if (name == NULL) |
b34976b6 | 2323 | return FALSE; |
23bc299b MM |
2324 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); |
2325 | rel_hdr->sh_name = | |
2b0f7ef9 | 2326 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, |
b34976b6 | 2327 | FALSE); |
23bc299b | 2328 | if (rel_hdr->sh_name == (unsigned int) -1) |
b34976b6 | 2329 | return FALSE; |
23bc299b MM |
2330 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
2331 | rel_hdr->sh_entsize = (use_rela_p | |
2332 | ? bed->s->sizeof_rela | |
2333 | : bed->s->sizeof_rel); | |
2334 | rel_hdr->sh_addralign = bed->s->file_align; | |
2335 | rel_hdr->sh_flags = 0; | |
2336 | rel_hdr->sh_addr = 0; | |
2337 | rel_hdr->sh_size = 0; | |
2338 | rel_hdr->sh_offset = 0; | |
2339 | ||
b34976b6 | 2340 | return TRUE; |
23bc299b MM |
2341 | } |
2342 | ||
252b5132 RH |
2343 | /* Set up an ELF internal section header for a section. */ |
2344 | ||
252b5132 RH |
2345 | static void |
2346 | elf_fake_sections (abfd, asect, failedptrarg) | |
2347 | bfd *abfd; | |
2348 | asection *asect; | |
2349 | PTR failedptrarg; | |
2350 | { | |
2351 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b34976b6 | 2352 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
252b5132 RH |
2353 | Elf_Internal_Shdr *this_hdr; |
2354 | ||
2355 | if (*failedptr) | |
2356 | { | |
2357 | /* We already failed; just get out of the bfd_map_over_sections | |
2358 | loop. */ | |
2359 | return; | |
2360 | } | |
2361 | ||
2362 | this_hdr = &elf_section_data (asect)->this_hdr; | |
2363 | ||
2b0f7ef9 | 2364 | this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 2365 | asect->name, FALSE); |
252b5132 RH |
2366 | if (this_hdr->sh_name == (unsigned long) -1) |
2367 | { | |
b34976b6 | 2368 | *failedptr = TRUE; |
252b5132 RH |
2369 | return; |
2370 | } | |
2371 | ||
2372 | this_hdr->sh_flags = 0; | |
2373 | ||
2374 | if ((asect->flags & SEC_ALLOC) != 0 | |
2375 | || asect->user_set_vma) | |
2376 | this_hdr->sh_addr = asect->vma; | |
2377 | else | |
2378 | this_hdr->sh_addr = 0; | |
2379 | ||
2380 | this_hdr->sh_offset = 0; | |
2381 | this_hdr->sh_size = asect->_raw_size; | |
2382 | this_hdr->sh_link = 0; | |
2383 | this_hdr->sh_addralign = 1 << asect->alignment_power; | |
2384 | /* The sh_entsize and sh_info fields may have been set already by | |
2385 | copy_private_section_data. */ | |
2386 | ||
2387 | this_hdr->bfd_section = asect; | |
2388 | this_hdr->contents = NULL; | |
2389 | ||
2390 | /* FIXME: This should not be based on section names. */ | |
2391 | if (strcmp (asect->name, ".dynstr") == 0) | |
2392 | this_hdr->sh_type = SHT_STRTAB; | |
2393 | else if (strcmp (asect->name, ".hash") == 0) | |
2394 | { | |
2395 | this_hdr->sh_type = SHT_HASH; | |
c7ac6ff8 | 2396 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
252b5132 RH |
2397 | } |
2398 | else if (strcmp (asect->name, ".dynsym") == 0) | |
2399 | { | |
2400 | this_hdr->sh_type = SHT_DYNSYM; | |
2401 | this_hdr->sh_entsize = bed->s->sizeof_sym; | |
2402 | } | |
2403 | else if (strcmp (asect->name, ".dynamic") == 0) | |
2404 | { | |
2405 | this_hdr->sh_type = SHT_DYNAMIC; | |
2406 | this_hdr->sh_entsize = bed->s->sizeof_dyn; | |
2407 | } | |
a9d024b8 | 2408 | else if (strncmp (asect->name, ".rela", 5) == 0 |
bf572ba0 | 2409 | && get_elf_backend_data (abfd)->may_use_rela_p) |
252b5132 RH |
2410 | { |
2411 | this_hdr->sh_type = SHT_RELA; | |
2412 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
2413 | } | |
a9d024b8 | 2414 | else if (strncmp (asect->name, ".rel", 4) == 0 |
bf572ba0 | 2415 | && get_elf_backend_data (abfd)->may_use_rel_p) |
252b5132 RH |
2416 | { |
2417 | this_hdr->sh_type = SHT_REL; | |
2418 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
2419 | } | |
25e27870 L |
2420 | else if (strcmp (asect->name, ".init_array") == 0) |
2421 | this_hdr->sh_type = SHT_INIT_ARRAY; | |
2422 | else if (strcmp (asect->name, ".fini_array") == 0) | |
2423 | this_hdr->sh_type = SHT_FINI_ARRAY; | |
2424 | else if (strcmp (asect->name, ".preinit_array") == 0) | |
2425 | this_hdr->sh_type = SHT_PREINIT_ARRAY; | |
252b5132 RH |
2426 | else if (strncmp (asect->name, ".note", 5) == 0) |
2427 | this_hdr->sh_type = SHT_NOTE; | |
2428 | else if (strncmp (asect->name, ".stab", 5) == 0 | |
2429 | && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) | |
2430 | this_hdr->sh_type = SHT_STRTAB; | |
2431 | else if (strcmp (asect->name, ".gnu.version") == 0) | |
2432 | { | |
2433 | this_hdr->sh_type = SHT_GNU_versym; | |
2434 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); | |
2435 | } | |
2436 | else if (strcmp (asect->name, ".gnu.version_d") == 0) | |
2437 | { | |
2438 | this_hdr->sh_type = SHT_GNU_verdef; | |
2439 | this_hdr->sh_entsize = 0; | |
2440 | /* objcopy or strip will copy over sh_info, but may not set | |
2441 | cverdefs. The linker will set cverdefs, but sh_info will be | |
2442 | zero. */ | |
2443 | if (this_hdr->sh_info == 0) | |
2444 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
2445 | else | |
2446 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
2447 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2448 | } | |
2449 | else if (strcmp (asect->name, ".gnu.version_r") == 0) | |
2450 | { | |
2451 | this_hdr->sh_type = SHT_GNU_verneed; | |
2452 | this_hdr->sh_entsize = 0; | |
2453 | /* objcopy or strip will copy over sh_info, but may not set | |
2454 | cverrefs. The linker will set cverrefs, but sh_info will be | |
2455 | zero. */ | |
2456 | if (this_hdr->sh_info == 0) | |
2457 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
2458 | else | |
2459 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
2460 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2461 | } | |
dbb410c3 AM |
2462 | else if ((asect->flags & SEC_GROUP) != 0) |
2463 | { | |
2464 | this_hdr->sh_type = SHT_GROUP; | |
2465 | this_hdr->sh_entsize = 4; | |
2466 | } | |
252b5132 | 2467 | else if ((asect->flags & SEC_ALLOC) != 0 |
edd29cf9 AM |
2468 | && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
2469 | || (asect->flags & SEC_NEVER_LOAD) != 0)) | |
252b5132 RH |
2470 | this_hdr->sh_type = SHT_NOBITS; |
2471 | else | |
6c99a5c3 | 2472 | this_hdr->sh_type = SHT_PROGBITS; |
252b5132 RH |
2473 | |
2474 | if ((asect->flags & SEC_ALLOC) != 0) | |
2475 | this_hdr->sh_flags |= SHF_ALLOC; | |
2476 | if ((asect->flags & SEC_READONLY) == 0) | |
2477 | this_hdr->sh_flags |= SHF_WRITE; | |
2478 | if ((asect->flags & SEC_CODE) != 0) | |
2479 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
2480 | if ((asect->flags & SEC_MERGE) != 0) |
2481 | { | |
2482 | this_hdr->sh_flags |= SHF_MERGE; | |
2483 | this_hdr->sh_entsize = asect->entsize; | |
2484 | if ((asect->flags & SEC_STRINGS) != 0) | |
2485 | this_hdr->sh_flags |= SHF_STRINGS; | |
2486 | } | |
1126897b | 2487 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 2488 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 2489 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
2490 | { |
2491 | this_hdr->sh_flags |= SHF_TLS; | |
2492 | if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
2493 | { | |
2494 | struct bfd_link_order *o; | |
b34976b6 | 2495 | |
704afa60 JJ |
2496 | this_hdr->sh_size = 0; |
2497 | for (o = asect->link_order_head; o != NULL; o = o->next) | |
2498 | if (this_hdr->sh_size < o->offset + o->size) | |
2499 | this_hdr->sh_size = o->offset + o->size; | |
2500 | if (this_hdr->sh_size) | |
2501 | this_hdr->sh_type = SHT_NOBITS; | |
2502 | } | |
2503 | } | |
252b5132 RH |
2504 | |
2505 | /* Check for processor-specific section types. */ | |
e1fddb6b AO |
2506 | if (bed->elf_backend_fake_sections |
2507 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
b34976b6 | 2508 | *failedptr = TRUE; |
252b5132 RH |
2509 | |
2510 | /* If the section has relocs, set up a section header for the | |
23bc299b MM |
2511 | SHT_REL[A] section. If two relocation sections are required for |
2512 | this section, it is up to the processor-specific back-end to | |
c044fabd | 2513 | create the other. */ |
23bc299b | 2514 | if ((asect->flags & SEC_RELOC) != 0 |
c044fabd | 2515 | && !_bfd_elf_init_reloc_shdr (abfd, |
23bc299b | 2516 | &elf_section_data (asect)->rel_hdr, |
c044fabd | 2517 | asect, |
68bfbfcc | 2518 | asect->use_rela_p)) |
b34976b6 | 2519 | *failedptr = TRUE; |
252b5132 RH |
2520 | } |
2521 | ||
dbb410c3 AM |
2522 | /* Fill in the contents of a SHT_GROUP section. */ |
2523 | ||
1126897b AM |
2524 | void |
2525 | bfd_elf_set_group_contents (abfd, sec, failedptrarg) | |
dbb410c3 AM |
2526 | bfd *abfd; |
2527 | asection *sec; | |
1126897b | 2528 | PTR failedptrarg; |
dbb410c3 | 2529 | { |
b34976b6 | 2530 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
dbb410c3 | 2531 | unsigned long symindx; |
9dce4196 | 2532 | asection *elt, *first; |
dbb410c3 AM |
2533 | unsigned char *loc; |
2534 | struct bfd_link_order *l; | |
b34976b6 | 2535 | bfd_boolean gas; |
dbb410c3 AM |
2536 | |
2537 | if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP | |
2538 | || *failedptr) | |
2539 | return; | |
2540 | ||
1126897b AM |
2541 | symindx = 0; |
2542 | if (elf_group_id (sec) != NULL) | |
2543 | symindx = elf_group_id (sec)->udata.i; | |
2544 | ||
2545 | if (symindx == 0) | |
2546 | { | |
2547 | /* If called from the assembler, swap_out_syms will have set up | |
2548 | elf_section_syms; If called for "ld -r", use target_index. */ | |
2549 | if (elf_section_syms (abfd) != NULL) | |
2550 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
2551 | else | |
2552 | symindx = sec->target_index; | |
2553 | } | |
dbb410c3 AM |
2554 | elf_section_data (sec)->this_hdr.sh_info = symindx; |
2555 | ||
1126897b | 2556 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 2557 | gas = TRUE; |
dbb410c3 AM |
2558 | if (sec->contents == NULL) |
2559 | { | |
b34976b6 | 2560 | gas = FALSE; |
dbb410c3 | 2561 | sec->contents = bfd_alloc (abfd, sec->_raw_size); |
9dce4196 AM |
2562 | |
2563 | /* Arrange for the section to be written out. */ | |
2564 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
2565 | if (sec->contents == NULL) |
2566 | { | |
b34976b6 | 2567 | *failedptr = TRUE; |
dbb410c3 AM |
2568 | return; |
2569 | } | |
2570 | } | |
2571 | ||
2572 | loc = sec->contents + sec->_raw_size; | |
2573 | ||
9dce4196 AM |
2574 | /* Get the pointer to the first section in the group that gas |
2575 | squirreled away here. objcopy arranges for this to be set to the | |
2576 | start of the input section group. */ | |
2577 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
2578 | |
2579 | /* First element is a flag word. Rest of section is elf section | |
2580 | indices for all the sections of the group. Write them backwards | |
2581 | just to keep the group in the same order as given in .section | |
2582 | directives, not that it matters. */ | |
2583 | while (elt != NULL) | |
2584 | { | |
9dce4196 AM |
2585 | asection *s; |
2586 | unsigned int idx; | |
2587 | ||
dbb410c3 | 2588 | loc -= 4; |
9dce4196 AM |
2589 | s = elt; |
2590 | if (!gas) | |
2591 | s = s->output_section; | |
2592 | idx = 0; | |
2593 | if (s != NULL) | |
2594 | idx = elf_section_data (s)->this_idx; | |
2595 | H_PUT_32 (abfd, idx, loc); | |
945906ff | 2596 | elt = elf_next_in_group (elt); |
9dce4196 AM |
2597 | if (elt == first) |
2598 | break; | |
dbb410c3 AM |
2599 | } |
2600 | ||
2601 | /* If this is a relocatable link, then the above did nothing because | |
2602 | SEC is the output section. Look through the input sections | |
2603 | instead. */ | |
2604 | for (l = sec->link_order_head; l != NULL; l = l->next) | |
2605 | if (l->type == bfd_indirect_link_order | |
945906ff | 2606 | && (elt = elf_next_in_group (l->u.indirect.section)) != NULL) |
dbb410c3 AM |
2607 | do |
2608 | { | |
2609 | loc -= 4; | |
2610 | H_PUT_32 (abfd, | |
2611 | elf_section_data (elt->output_section)->this_idx, loc); | |
945906ff | 2612 | elt = elf_next_in_group (elt); |
dbb410c3 AM |
2613 | /* During a relocatable link, the lists are circular. */ |
2614 | } | |
945906ff | 2615 | while (elt != elf_next_in_group (l->u.indirect.section)); |
dbb410c3 | 2616 | |
9dce4196 AM |
2617 | /* With ld -r, merging SHT_GROUP sections results in wasted space |
2618 | due to allowing for the flag word on each input. We may well | |
2619 | duplicate entries too. */ | |
2620 | while ((loc -= 4) > sec->contents) | |
2621 | H_PUT_32 (abfd, 0, loc); | |
2622 | ||
2623 | if (loc != sec->contents) | |
2624 | abort (); | |
dbb410c3 | 2625 | |
9dce4196 | 2626 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
2627 | } |
2628 | ||
252b5132 RH |
2629 | /* Assign all ELF section numbers. The dummy first section is handled here |
2630 | too. The link/info pointers for the standard section types are filled | |
2631 | in here too, while we're at it. */ | |
2632 | ||
b34976b6 | 2633 | static bfd_boolean |
252b5132 RH |
2634 | assign_section_numbers (abfd) |
2635 | bfd *abfd; | |
2636 | { | |
2637 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
2638 | asection *sec; | |
2b0f7ef9 | 2639 | unsigned int section_number, secn; |
252b5132 | 2640 | Elf_Internal_Shdr **i_shdrp; |
dc810e39 | 2641 | bfd_size_type amt; |
252b5132 RH |
2642 | |
2643 | section_number = 1; | |
2644 | ||
2b0f7ef9 JJ |
2645 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
2646 | ||
252b5132 RH |
2647 | for (sec = abfd->sections; sec; sec = sec->next) |
2648 | { | |
2649 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
2650 | ||
9ad5cbcf AM |
2651 | if (section_number == SHN_LORESERVE) |
2652 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 | 2653 | d->this_idx = section_number++; |
2b0f7ef9 | 2654 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); |
252b5132 RH |
2655 | if ((sec->flags & SEC_RELOC) == 0) |
2656 | d->rel_idx = 0; | |
2657 | else | |
2b0f7ef9 | 2658 | { |
9ad5cbcf AM |
2659 | if (section_number == SHN_LORESERVE) |
2660 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
2b0f7ef9 JJ |
2661 | d->rel_idx = section_number++; |
2662 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name); | |
2663 | } | |
23bc299b MM |
2664 | |
2665 | if (d->rel_hdr2) | |
2b0f7ef9 | 2666 | { |
9ad5cbcf AM |
2667 | if (section_number == SHN_LORESERVE) |
2668 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
2b0f7ef9 JJ |
2669 | d->rel_idx2 = section_number++; |
2670 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name); | |
2671 | } | |
23bc299b MM |
2672 | else |
2673 | d->rel_idx2 = 0; | |
252b5132 RH |
2674 | } |
2675 | ||
9ad5cbcf AM |
2676 | if (section_number == SHN_LORESERVE) |
2677 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 | 2678 | t->shstrtab_section = section_number++; |
2b0f7ef9 | 2679 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); |
252b5132 | 2680 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; |
252b5132 RH |
2681 | |
2682 | if (bfd_get_symcount (abfd) > 0) | |
2683 | { | |
9ad5cbcf AM |
2684 | if (section_number == SHN_LORESERVE) |
2685 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 | 2686 | t->symtab_section = section_number++; |
2b0f7ef9 | 2687 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
9ad5cbcf AM |
2688 | if (section_number > SHN_LORESERVE - 2) |
2689 | { | |
2690 | if (section_number == SHN_LORESERVE) | |
2691 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
2692 | t->symtab_shndx_section = section_number++; | |
2693 | t->symtab_shndx_hdr.sh_name | |
2694 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
b34976b6 | 2695 | ".symtab_shndx", FALSE); |
9ad5cbcf | 2696 | if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 2697 | return FALSE; |
9ad5cbcf AM |
2698 | } |
2699 | if (section_number == SHN_LORESERVE) | |
2700 | section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 | 2701 | t->strtab_section = section_number++; |
2b0f7ef9 | 2702 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
2703 | } |
2704 | ||
2b0f7ef9 JJ |
2705 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); |
2706 | t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
9ad5cbcf AM |
2707 | |
2708 | elf_numsections (abfd) = section_number; | |
252b5132 | 2709 | elf_elfheader (abfd)->e_shnum = section_number; |
9ad5cbcf AM |
2710 | if (section_number > SHN_LORESERVE) |
2711 | elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 RH |
2712 | |
2713 | /* Set up the list of section header pointers, in agreement with the | |
2714 | indices. */ | |
dc810e39 | 2715 | amt = section_number * sizeof (Elf_Internal_Shdr *); |
c97e73dd | 2716 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); |
252b5132 | 2717 | if (i_shdrp == NULL) |
b34976b6 | 2718 | return FALSE; |
252b5132 | 2719 | |
dc810e39 | 2720 | amt = sizeof (Elf_Internal_Shdr); |
c97e73dd | 2721 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt); |
252b5132 RH |
2722 | if (i_shdrp[0] == NULL) |
2723 | { | |
2724 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 2725 | return FALSE; |
252b5132 | 2726 | } |
252b5132 RH |
2727 | |
2728 | elf_elfsections (abfd) = i_shdrp; | |
2729 | ||
2730 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
2731 | if (bfd_get_symcount (abfd) > 0) | |
2732 | { | |
2733 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
9ad5cbcf AM |
2734 | if (elf_numsections (abfd) > SHN_LORESERVE) |
2735 | { | |
2736 | i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; | |
2737 | t->symtab_shndx_hdr.sh_link = t->symtab_section; | |
2738 | } | |
252b5132 RH |
2739 | i_shdrp[t->strtab_section] = &t->strtab_hdr; |
2740 | t->symtab_hdr.sh_link = t->strtab_section; | |
2741 | } | |
2742 | for (sec = abfd->sections; sec; sec = sec->next) | |
2743 | { | |
2744 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
2745 | asection *s; | |
2746 | const char *name; | |
2747 | ||
2748 | i_shdrp[d->this_idx] = &d->this_hdr; | |
2749 | if (d->rel_idx != 0) | |
2750 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
23bc299b MM |
2751 | if (d->rel_idx2 != 0) |
2752 | i_shdrp[d->rel_idx2] = d->rel_hdr2; | |
252b5132 RH |
2753 | |
2754 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
2755 | ||
2756 | /* sh_link of a reloc section is the section index of the symbol | |
2757 | table. sh_info is the section index of the section to which | |
2758 | the relocation entries apply. */ | |
2759 | if (d->rel_idx != 0) | |
2760 | { | |
2761 | d->rel_hdr.sh_link = t->symtab_section; | |
2762 | d->rel_hdr.sh_info = d->this_idx; | |
2763 | } | |
23bc299b MM |
2764 | if (d->rel_idx2 != 0) |
2765 | { | |
2766 | d->rel_hdr2->sh_link = t->symtab_section; | |
2767 | d->rel_hdr2->sh_info = d->this_idx; | |
2768 | } | |
252b5132 RH |
2769 | |
2770 | switch (d->this_hdr.sh_type) | |
2771 | { | |
2772 | case SHT_REL: | |
2773 | case SHT_RELA: | |
2774 | /* A reloc section which we are treating as a normal BFD | |
2775 | section. sh_link is the section index of the symbol | |
2776 | table. sh_info is the section index of the section to | |
2777 | which the relocation entries apply. We assume that an | |
2778 | allocated reloc section uses the dynamic symbol table. | |
2779 | FIXME: How can we be sure? */ | |
2780 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
2781 | if (s != NULL) | |
2782 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
2783 | ||
2784 | /* We look up the section the relocs apply to by name. */ | |
2785 | name = sec->name; | |
2786 | if (d->this_hdr.sh_type == SHT_REL) | |
2787 | name += 4; | |
2788 | else | |
2789 | name += 5; | |
2790 | s = bfd_get_section_by_name (abfd, name); | |
2791 | if (s != NULL) | |
2792 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
2793 | break; | |
2794 | ||
2795 | case SHT_STRTAB: | |
2796 | /* We assume that a section named .stab*str is a stabs | |
2797 | string section. We look for a section with the same name | |
2798 | but without the trailing ``str'', and set its sh_link | |
2799 | field to point to this section. */ | |
2800 | if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 | |
2801 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) | |
2802 | { | |
2803 | size_t len; | |
2804 | char *alc; | |
2805 | ||
2806 | len = strlen (sec->name); | |
d4c88bbb | 2807 | alc = (char *) bfd_malloc ((bfd_size_type) (len - 2)); |
252b5132 | 2808 | if (alc == NULL) |
b34976b6 | 2809 | return FALSE; |
d4c88bbb | 2810 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
2811 | alc[len - 3] = '\0'; |
2812 | s = bfd_get_section_by_name (abfd, alc); | |
2813 | free (alc); | |
2814 | if (s != NULL) | |
2815 | { | |
2816 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
2817 | ||
2818 | /* This is a .stab section. */ | |
0594c12d AM |
2819 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
2820 | elf_section_data (s)->this_hdr.sh_entsize | |
2821 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
2822 | } |
2823 | } | |
2824 | break; | |
2825 | ||
2826 | case SHT_DYNAMIC: | |
2827 | case SHT_DYNSYM: | |
2828 | case SHT_GNU_verneed: | |
2829 | case SHT_GNU_verdef: | |
2830 | /* sh_link is the section header index of the string table | |
2831 | used for the dynamic entries, or the symbol table, or the | |
2832 | version strings. */ | |
2833 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
2834 | if (s != NULL) | |
2835 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
2836 | break; | |
2837 | ||
2838 | case SHT_HASH: | |
2839 | case SHT_GNU_versym: | |
2840 | /* sh_link is the section header index of the symbol table | |
2841 | this hash table or version table is for. */ | |
2842 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
2843 | if (s != NULL) | |
2844 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
2845 | break; | |
dbb410c3 AM |
2846 | |
2847 | case SHT_GROUP: | |
2848 | d->this_hdr.sh_link = t->symtab_section; | |
252b5132 RH |
2849 | } |
2850 | } | |
2851 | ||
2b0f7ef9 | 2852 | for (secn = 1; secn < section_number; ++secn) |
9ad5cbcf AM |
2853 | if (i_shdrp[secn] == NULL) |
2854 | i_shdrp[secn] = i_shdrp[0]; | |
2855 | else | |
2856 | i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
2857 | i_shdrp[secn]->sh_name); | |
b34976b6 | 2858 | return TRUE; |
252b5132 RH |
2859 | } |
2860 | ||
2861 | /* Map symbol from it's internal number to the external number, moving | |
2862 | all local symbols to be at the head of the list. */ | |
2863 | ||
2864 | static INLINE int | |
2865 | sym_is_global (abfd, sym) | |
2866 | bfd *abfd; | |
2867 | asymbol *sym; | |
2868 | { | |
2869 | /* If the backend has a special mapping, use it. */ | |
2870 | if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
2871 | return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
2872 | (abfd, sym)); | |
2873 | ||
2874 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 | |
2875 | || bfd_is_und_section (bfd_get_section (sym)) | |
2876 | || bfd_is_com_section (bfd_get_section (sym))); | |
2877 | } | |
2878 | ||
b34976b6 | 2879 | static bfd_boolean |
252b5132 RH |
2880 | elf_map_symbols (abfd) |
2881 | bfd *abfd; | |
2882 | { | |
dc810e39 | 2883 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
2884 | asymbol **syms = bfd_get_outsymbols (abfd); |
2885 | asymbol **sect_syms; | |
dc810e39 AM |
2886 | unsigned int num_locals = 0; |
2887 | unsigned int num_globals = 0; | |
2888 | unsigned int num_locals2 = 0; | |
2889 | unsigned int num_globals2 = 0; | |
252b5132 | 2890 | int max_index = 0; |
dc810e39 | 2891 | unsigned int idx; |
252b5132 RH |
2892 | asection *asect; |
2893 | asymbol **new_syms; | |
dc810e39 | 2894 | bfd_size_type amt; |
252b5132 RH |
2895 | |
2896 | #ifdef DEBUG | |
2897 | fprintf (stderr, "elf_map_symbols\n"); | |
2898 | fflush (stderr); | |
2899 | #endif | |
2900 | ||
252b5132 RH |
2901 | for (asect = abfd->sections; asect; asect = asect->next) |
2902 | { | |
2903 | if (max_index < asect->index) | |
2904 | max_index = asect->index; | |
2905 | } | |
2906 | ||
2907 | max_index++; | |
dc810e39 AM |
2908 | amt = max_index * sizeof (asymbol *); |
2909 | sect_syms = (asymbol **) bfd_zalloc (abfd, amt); | |
252b5132 | 2910 | if (sect_syms == NULL) |
b34976b6 | 2911 | return FALSE; |
252b5132 | 2912 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 2913 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 2914 | |
079e9a2f AM |
2915 | /* Init sect_syms entries for any section symbols we have already |
2916 | decided to output. */ | |
252b5132 RH |
2917 | for (idx = 0; idx < symcount; idx++) |
2918 | { | |
dc810e39 | 2919 | asymbol *sym = syms[idx]; |
c044fabd | 2920 | |
252b5132 RH |
2921 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
2922 | && sym->value == 0) | |
2923 | { | |
2924 | asection *sec; | |
2925 | ||
2926 | sec = sym->section; | |
2927 | ||
2928 | if (sec->owner != NULL) | |
2929 | { | |
2930 | if (sec->owner != abfd) | |
2931 | { | |
2932 | if (sec->output_offset != 0) | |
2933 | continue; | |
c044fabd | 2934 | |
252b5132 RH |
2935 | sec = sec->output_section; |
2936 | ||
079e9a2f AM |
2937 | /* Empty sections in the input files may have had a |
2938 | section symbol created for them. (See the comment | |
2939 | near the end of _bfd_generic_link_output_symbols in | |
2940 | linker.c). If the linker script discards such | |
2941 | sections then we will reach this point. Since we know | |
2942 | that we cannot avoid this case, we detect it and skip | |
2943 | the abort and the assignment to the sect_syms array. | |
2944 | To reproduce this particular case try running the | |
2945 | linker testsuite test ld-scripts/weak.exp for an ELF | |
2946 | port that uses the generic linker. */ | |
252b5132 RH |
2947 | if (sec->owner == NULL) |
2948 | continue; | |
2949 | ||
2950 | BFD_ASSERT (sec->owner == abfd); | |
2951 | } | |
2952 | sect_syms[sec->index] = syms[idx]; | |
2953 | } | |
2954 | } | |
2955 | } | |
2956 | ||
252b5132 RH |
2957 | /* Classify all of the symbols. */ |
2958 | for (idx = 0; idx < symcount; idx++) | |
2959 | { | |
2960 | if (!sym_is_global (abfd, syms[idx])) | |
2961 | num_locals++; | |
2962 | else | |
2963 | num_globals++; | |
2964 | } | |
079e9a2f AM |
2965 | |
2966 | /* We will be adding a section symbol for each BFD section. Most normal | |
2967 | sections will already have a section symbol in outsymbols, but | |
2968 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
2969 | at least in that case. */ | |
252b5132 RH |
2970 | for (asect = abfd->sections; asect; asect = asect->next) |
2971 | { | |
079e9a2f | 2972 | if (sect_syms[asect->index] == NULL) |
252b5132 | 2973 | { |
079e9a2f | 2974 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
2975 | num_locals++; |
2976 | else | |
2977 | num_globals++; | |
252b5132 RH |
2978 | } |
2979 | } | |
2980 | ||
2981 | /* Now sort the symbols so the local symbols are first. */ | |
dc810e39 AM |
2982 | amt = (num_locals + num_globals) * sizeof (asymbol *); |
2983 | new_syms = (asymbol **) bfd_alloc (abfd, amt); | |
2984 | ||
252b5132 | 2985 | if (new_syms == NULL) |
b34976b6 | 2986 | return FALSE; |
252b5132 RH |
2987 | |
2988 | for (idx = 0; idx < symcount; idx++) | |
2989 | { | |
2990 | asymbol *sym = syms[idx]; | |
dc810e39 | 2991 | unsigned int i; |
252b5132 RH |
2992 | |
2993 | if (!sym_is_global (abfd, sym)) | |
2994 | i = num_locals2++; | |
2995 | else | |
2996 | i = num_locals + num_globals2++; | |
2997 | new_syms[i] = sym; | |
2998 | sym->udata.i = i + 1; | |
2999 | } | |
3000 | for (asect = abfd->sections; asect; asect = asect->next) | |
3001 | { | |
079e9a2f | 3002 | if (sect_syms[asect->index] == NULL) |
252b5132 | 3003 | { |
079e9a2f | 3004 | asymbol *sym = asect->symbol; |
dc810e39 | 3005 | unsigned int i; |
252b5132 | 3006 | |
079e9a2f | 3007 | sect_syms[asect->index] = sym; |
252b5132 RH |
3008 | if (!sym_is_global (abfd, sym)) |
3009 | i = num_locals2++; | |
3010 | else | |
3011 | i = num_locals + num_globals2++; | |
3012 | new_syms[i] = sym; | |
3013 | sym->udata.i = i + 1; | |
3014 | } | |
3015 | } | |
3016 | ||
3017 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
3018 | ||
3019 | elf_num_locals (abfd) = num_locals; | |
3020 | elf_num_globals (abfd) = num_globals; | |
b34976b6 | 3021 | return TRUE; |
252b5132 RH |
3022 | } |
3023 | ||
3024 | /* Align to the maximum file alignment that could be required for any | |
3025 | ELF data structure. */ | |
3026 | ||
b34976b6 AM |
3027 | static INLINE file_ptr align_file_position |
3028 | PARAMS ((file_ptr, int)); | |
252b5132 RH |
3029 | static INLINE file_ptr |
3030 | align_file_position (off, align) | |
3031 | file_ptr off; | |
3032 | int align; | |
3033 | { | |
3034 | return (off + align - 1) & ~(align - 1); | |
3035 | } | |
3036 | ||
3037 | /* Assign a file position to a section, optionally aligning to the | |
3038 | required section alignment. */ | |
3039 | ||
3040 | INLINE file_ptr | |
3041 | _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align) | |
3042 | Elf_Internal_Shdr *i_shdrp; | |
3043 | file_ptr offset; | |
b34976b6 | 3044 | bfd_boolean align; |
252b5132 RH |
3045 | { |
3046 | if (align) | |
3047 | { | |
3048 | unsigned int al; | |
3049 | ||
3050 | al = i_shdrp->sh_addralign; | |
3051 | if (al > 1) | |
3052 | offset = BFD_ALIGN (offset, al); | |
3053 | } | |
3054 | i_shdrp->sh_offset = offset; | |
3055 | if (i_shdrp->bfd_section != NULL) | |
3056 | i_shdrp->bfd_section->filepos = offset; | |
3057 | if (i_shdrp->sh_type != SHT_NOBITS) | |
3058 | offset += i_shdrp->sh_size; | |
3059 | return offset; | |
3060 | } | |
3061 | ||
3062 | /* Compute the file positions we are going to put the sections at, and | |
3063 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
3064 | is not NULL, this is being called by the ELF backend linker. */ | |
3065 | ||
b34976b6 | 3066 | bfd_boolean |
252b5132 RH |
3067 | _bfd_elf_compute_section_file_positions (abfd, link_info) |
3068 | bfd *abfd; | |
3069 | struct bfd_link_info *link_info; | |
3070 | { | |
3071 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
b34976b6 | 3072 | bfd_boolean failed; |
252b5132 RH |
3073 | struct bfd_strtab_hash *strtab; |
3074 | Elf_Internal_Shdr *shstrtab_hdr; | |
3075 | ||
3076 | if (abfd->output_has_begun) | |
b34976b6 | 3077 | return TRUE; |
252b5132 RH |
3078 | |
3079 | /* Do any elf backend specific processing first. */ | |
3080 | if (bed->elf_backend_begin_write_processing) | |
3081 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
3082 | ||
3083 | if (! prep_headers (abfd)) | |
b34976b6 | 3084 | return FALSE; |
252b5132 | 3085 | |
e6c51ed4 NC |
3086 | /* Post process the headers if necessary. */ |
3087 | if (bed->elf_backend_post_process_headers) | |
3088 | (*bed->elf_backend_post_process_headers) (abfd, link_info); | |
3089 | ||
b34976b6 | 3090 | failed = FALSE; |
252b5132 RH |
3091 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); |
3092 | if (failed) | |
b34976b6 | 3093 | return FALSE; |
252b5132 RH |
3094 | |
3095 | if (!assign_section_numbers (abfd)) | |
b34976b6 | 3096 | return FALSE; |
252b5132 RH |
3097 | |
3098 | /* The backend linker builds symbol table information itself. */ | |
3099 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
3100 | { | |
3101 | /* Non-zero if doing a relocatable link. */ | |
3102 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
3103 | ||
3104 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 3105 | return FALSE; |
252b5132 RH |
3106 | } |
3107 | ||
1126897b | 3108 | if (link_info == NULL) |
dbb410c3 | 3109 | { |
1126897b | 3110 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 3111 | if (failed) |
b34976b6 | 3112 | return FALSE; |
dbb410c3 AM |
3113 | } |
3114 | ||
252b5132 RH |
3115 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
3116 | /* sh_name was set in prep_headers. */ | |
3117 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
3118 | shstrtab_hdr->sh_flags = 0; | |
3119 | shstrtab_hdr->sh_addr = 0; | |
2b0f7ef9 | 3120 | shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); |
252b5132 RH |
3121 | shstrtab_hdr->sh_entsize = 0; |
3122 | shstrtab_hdr->sh_link = 0; | |
3123 | shstrtab_hdr->sh_info = 0; | |
3124 | /* sh_offset is set in assign_file_positions_except_relocs. */ | |
3125 | shstrtab_hdr->sh_addralign = 1; | |
3126 | ||
3127 | if (!assign_file_positions_except_relocs (abfd)) | |
b34976b6 | 3128 | return FALSE; |
252b5132 RH |
3129 | |
3130 | if (link_info == NULL && bfd_get_symcount (abfd) > 0) | |
3131 | { | |
3132 | file_ptr off; | |
3133 | Elf_Internal_Shdr *hdr; | |
3134 | ||
3135 | off = elf_tdata (abfd)->next_file_pos; | |
3136 | ||
3137 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
b34976b6 | 3138 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 3139 | |
9ad5cbcf AM |
3140 | hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
3141 | if (hdr->sh_size != 0) | |
b34976b6 | 3142 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf | 3143 | |
252b5132 | 3144 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 3145 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 RH |
3146 | |
3147 | elf_tdata (abfd)->next_file_pos = off; | |
3148 | ||
3149 | /* Now that we know where the .strtab section goes, write it | |
3150 | out. */ | |
3151 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
3152 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
b34976b6 | 3153 | return FALSE; |
252b5132 RH |
3154 | _bfd_stringtab_free (strtab); |
3155 | } | |
3156 | ||
b34976b6 | 3157 | abfd->output_has_begun = TRUE; |
252b5132 | 3158 | |
b34976b6 | 3159 | return TRUE; |
252b5132 RH |
3160 | } |
3161 | ||
3162 | /* Create a mapping from a set of sections to a program segment. */ | |
3163 | ||
3164 | static INLINE struct elf_segment_map * | |
3165 | make_mapping (abfd, sections, from, to, phdr) | |
3166 | bfd *abfd; | |
3167 | asection **sections; | |
3168 | unsigned int from; | |
3169 | unsigned int to; | |
b34976b6 | 3170 | bfd_boolean phdr; |
252b5132 RH |
3171 | { |
3172 | struct elf_segment_map *m; | |
3173 | unsigned int i; | |
3174 | asection **hdrpp; | |
dc810e39 | 3175 | bfd_size_type amt; |
252b5132 | 3176 | |
dc810e39 AM |
3177 | amt = sizeof (struct elf_segment_map); |
3178 | amt += (to - from - 1) * sizeof (asection *); | |
3179 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
3180 | if (m == NULL) |
3181 | return NULL; | |
3182 | m->next = NULL; | |
3183 | m->p_type = PT_LOAD; | |
3184 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
3185 | m->sections[i - from] = *hdrpp; | |
3186 | m->count = to - from; | |
3187 | ||
3188 | if (from == 0 && phdr) | |
3189 | { | |
3190 | /* Include the headers in the first PT_LOAD segment. */ | |
3191 | m->includes_filehdr = 1; | |
3192 | m->includes_phdrs = 1; | |
3193 | } | |
3194 | ||
3195 | return m; | |
3196 | } | |
3197 | ||
3198 | /* Set up a mapping from BFD sections to program segments. */ | |
3199 | ||
b34976b6 | 3200 | static bfd_boolean |
252b5132 RH |
3201 | map_sections_to_segments (abfd) |
3202 | bfd *abfd; | |
3203 | { | |
3204 | asection **sections = NULL; | |
3205 | asection *s; | |
3206 | unsigned int i; | |
3207 | unsigned int count; | |
3208 | struct elf_segment_map *mfirst; | |
3209 | struct elf_segment_map **pm; | |
3210 | struct elf_segment_map *m; | |
3211 | asection *last_hdr; | |
3212 | unsigned int phdr_index; | |
3213 | bfd_vma maxpagesize; | |
3214 | asection **hdrpp; | |
b34976b6 AM |
3215 | bfd_boolean phdr_in_segment = TRUE; |
3216 | bfd_boolean writable; | |
13ae64f3 JJ |
3217 | int tls_count = 0; |
3218 | asection *first_tls = NULL; | |
65765700 | 3219 | asection *dynsec, *eh_frame_hdr; |
dc810e39 | 3220 | bfd_size_type amt; |
252b5132 RH |
3221 | |
3222 | if (elf_tdata (abfd)->segment_map != NULL) | |
b34976b6 | 3223 | return TRUE; |
252b5132 RH |
3224 | |
3225 | if (bfd_count_sections (abfd) == 0) | |
b34976b6 | 3226 | return TRUE; |
252b5132 RH |
3227 | |
3228 | /* Select the allocated sections, and sort them. */ | |
3229 | ||
dc810e39 AM |
3230 | amt = bfd_count_sections (abfd) * sizeof (asection *); |
3231 | sections = (asection **) bfd_malloc (amt); | |
252b5132 RH |
3232 | if (sections == NULL) |
3233 | goto error_return; | |
3234 | ||
3235 | i = 0; | |
3236 | for (s = abfd->sections; s != NULL; s = s->next) | |
3237 | { | |
3238 | if ((s->flags & SEC_ALLOC) != 0) | |
3239 | { | |
3240 | sections[i] = s; | |
3241 | ++i; | |
3242 | } | |
3243 | } | |
3244 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
3245 | count = i; | |
3246 | ||
3247 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); | |
3248 | ||
3249 | /* Build the mapping. */ | |
3250 | ||
3251 | mfirst = NULL; | |
3252 | pm = &mfirst; | |
3253 | ||
3254 | /* If we have a .interp section, then create a PT_PHDR segment for | |
3255 | the program headers and a PT_INTERP segment for the .interp | |
3256 | section. */ | |
3257 | s = bfd_get_section_by_name (abfd, ".interp"); | |
3258 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
3259 | { | |
dc810e39 AM |
3260 | amt = sizeof (struct elf_segment_map); |
3261 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
3262 | if (m == NULL) |
3263 | goto error_return; | |
3264 | m->next = NULL; | |
3265 | m->p_type = PT_PHDR; | |
3266 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
3267 | m->p_flags = PF_R | PF_X; | |
3268 | m->p_flags_valid = 1; | |
3269 | m->includes_phdrs = 1; | |
3270 | ||
3271 | *pm = m; | |
3272 | pm = &m->next; | |
3273 | ||
dc810e39 AM |
3274 | amt = sizeof (struct elf_segment_map); |
3275 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
3276 | if (m == NULL) |
3277 | goto error_return; | |
3278 | m->next = NULL; | |
3279 | m->p_type = PT_INTERP; | |
3280 | m->count = 1; | |
3281 | m->sections[0] = s; | |
3282 | ||
3283 | *pm = m; | |
3284 | pm = &m->next; | |
3285 | } | |
3286 | ||
3287 | /* Look through the sections. We put sections in the same program | |
3288 | segment when the start of the second section can be placed within | |
3289 | a few bytes of the end of the first section. */ | |
3290 | last_hdr = NULL; | |
3291 | phdr_index = 0; | |
3292 | maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
b34976b6 | 3293 | writable = FALSE; |
252b5132 RH |
3294 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); |
3295 | if (dynsec != NULL | |
3296 | && (dynsec->flags & SEC_LOAD) == 0) | |
3297 | dynsec = NULL; | |
3298 | ||
3299 | /* Deal with -Ttext or something similar such that the first section | |
3300 | is not adjacent to the program headers. This is an | |
3301 | approximation, since at this point we don't know exactly how many | |
3302 | program headers we will need. */ | |
3303 | if (count > 0) | |
3304 | { | |
3305 | bfd_size_type phdr_size; | |
3306 | ||
3307 | phdr_size = elf_tdata (abfd)->program_header_size; | |
3308 | if (phdr_size == 0) | |
3309 | phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr; | |
3310 | if ((abfd->flags & D_PAGED) == 0 | |
3311 | || sections[0]->lma < phdr_size | |
3312 | || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) | |
b34976b6 | 3313 | phdr_in_segment = FALSE; |
252b5132 RH |
3314 | } |
3315 | ||
3316 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) | |
3317 | { | |
3318 | asection *hdr; | |
b34976b6 | 3319 | bfd_boolean new_segment; |
252b5132 RH |
3320 | |
3321 | hdr = *hdrpp; | |
3322 | ||
3323 | /* See if this section and the last one will fit in the same | |
3324 | segment. */ | |
3325 | ||
3326 | if (last_hdr == NULL) | |
3327 | { | |
3328 | /* If we don't have a segment yet, then we don't need a new | |
3329 | one (we build the last one after this loop). */ | |
b34976b6 | 3330 | new_segment = FALSE; |
252b5132 RH |
3331 | } |
3332 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
3333 | { | |
3334 | /* If this section has a different relation between the | |
3335 | virtual address and the load address, then we need a new | |
3336 | segment. */ | |
b34976b6 | 3337 | new_segment = TRUE; |
252b5132 RH |
3338 | } |
3339 | else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) | |
3340 | < BFD_ALIGN (hdr->lma, maxpagesize)) | |
3341 | { | |
3342 | /* If putting this section in this segment would force us to | |
3343 | skip a page in the segment, then we need a new segment. */ | |
b34976b6 | 3344 | new_segment = TRUE; |
252b5132 RH |
3345 | } |
3346 | else if ((last_hdr->flags & SEC_LOAD) == 0 | |
3347 | && (hdr->flags & SEC_LOAD) != 0) | |
3348 | { | |
3349 | /* We don't want to put a loadable section after a | |
3350 | nonloadable section in the same segment. */ | |
b34976b6 | 3351 | new_segment = TRUE; |
252b5132 RH |
3352 | } |
3353 | else if ((abfd->flags & D_PAGED) == 0) | |
3354 | { | |
3355 | /* If the file is not demand paged, which means that we | |
3356 | don't require the sections to be correctly aligned in the | |
3357 | file, then there is no other reason for a new segment. */ | |
b34976b6 | 3358 | new_segment = FALSE; |
252b5132 RH |
3359 | } |
3360 | else if (! writable | |
3361 | && (hdr->flags & SEC_READONLY) == 0 | |
b89fe0ee AM |
3362 | && (((last_hdr->lma + last_hdr->_raw_size - 1) |
3363 | & ~(maxpagesize - 1)) | |
3364 | != (hdr->lma & ~(maxpagesize - 1)))) | |
252b5132 RH |
3365 | { |
3366 | /* We don't want to put a writable section in a read only | |
3367 | segment, unless they are on the same page in memory | |
3368 | anyhow. We already know that the last section does not | |
3369 | bring us past the current section on the page, so the | |
3370 | only case in which the new section is not on the same | |
3371 | page as the previous section is when the previous section | |
3372 | ends precisely on a page boundary. */ | |
b34976b6 | 3373 | new_segment = TRUE; |
252b5132 RH |
3374 | } |
3375 | else | |
3376 | { | |
3377 | /* Otherwise, we can use the same segment. */ | |
b34976b6 | 3378 | new_segment = FALSE; |
252b5132 RH |
3379 | } |
3380 | ||
3381 | if (! new_segment) | |
3382 | { | |
3383 | if ((hdr->flags & SEC_READONLY) == 0) | |
b34976b6 | 3384 | writable = TRUE; |
252b5132 RH |
3385 | last_hdr = hdr; |
3386 | continue; | |
3387 | } | |
3388 | ||
3389 | /* We need a new program segment. We must create a new program | |
3390 | header holding all the sections from phdr_index until hdr. */ | |
3391 | ||
3392 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3393 | if (m == NULL) | |
3394 | goto error_return; | |
3395 | ||
3396 | *pm = m; | |
3397 | pm = &m->next; | |
3398 | ||
3399 | if ((hdr->flags & SEC_READONLY) == 0) | |
b34976b6 | 3400 | writable = TRUE; |
252b5132 | 3401 | else |
b34976b6 | 3402 | writable = FALSE; |
252b5132 RH |
3403 | |
3404 | last_hdr = hdr; | |
3405 | phdr_index = i; | |
b34976b6 | 3406 | phdr_in_segment = FALSE; |
252b5132 RH |
3407 | } |
3408 | ||
3409 | /* Create a final PT_LOAD program segment. */ | |
3410 | if (last_hdr != NULL) | |
3411 | { | |
3412 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
3413 | if (m == NULL) | |
3414 | goto error_return; | |
3415 | ||
3416 | *pm = m; | |
3417 | pm = &m->next; | |
3418 | } | |
3419 | ||
3420 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ | |
3421 | if (dynsec != NULL) | |
3422 | { | |
dc810e39 AM |
3423 | amt = sizeof (struct elf_segment_map); |
3424 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
3425 | if (m == NULL) |
3426 | goto error_return; | |
3427 | m->next = NULL; | |
3428 | m->p_type = PT_DYNAMIC; | |
3429 | m->count = 1; | |
3430 | m->sections[0] = dynsec; | |
3431 | ||
3432 | *pm = m; | |
3433 | pm = &m->next; | |
3434 | } | |
3435 | ||
3436 | /* For each loadable .note section, add a PT_NOTE segment. We don't | |
3437 | use bfd_get_section_by_name, because if we link together | |
3438 | nonloadable .note sections and loadable .note sections, we will | |
3439 | generate two .note sections in the output file. FIXME: Using | |
3440 | names for section types is bogus anyhow. */ | |
3441 | for (s = abfd->sections; s != NULL; s = s->next) | |
3442 | { | |
3443 | if ((s->flags & SEC_LOAD) != 0 | |
3444 | && strncmp (s->name, ".note", 5) == 0) | |
3445 | { | |
dc810e39 AM |
3446 | amt = sizeof (struct elf_segment_map); |
3447 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
3448 | if (m == NULL) |
3449 | goto error_return; | |
3450 | m->next = NULL; | |
3451 | m->p_type = PT_NOTE; | |
3452 | m->count = 1; | |
3453 | m->sections[0] = s; | |
3454 | ||
3455 | *pm = m; | |
3456 | pm = &m->next; | |
3457 | } | |
13ae64f3 JJ |
3458 | if (s->flags & SEC_THREAD_LOCAL) |
3459 | { | |
3460 | if (! tls_count) | |
3461 | first_tls = s; | |
3462 | tls_count++; | |
3463 | } | |
3464 | } | |
3465 | ||
3466 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ | |
3467 | if (tls_count > 0) | |
3468 | { | |
3469 | int i; | |
3470 | ||
3471 | amt = sizeof (struct elf_segment_map); | |
3472 | amt += (tls_count - 1) * sizeof (asection *); | |
3473 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
3474 | if (m == NULL) | |
3475 | goto error_return; | |
3476 | m->next = NULL; | |
3477 | m->p_type = PT_TLS; | |
3478 | m->count = tls_count; | |
3479 | /* Mandated PF_R. */ | |
3480 | m->p_flags = PF_R; | |
3481 | m->p_flags_valid = 1; | |
3482 | for (i = 0; i < tls_count; ++i) | |
3483 | { | |
3484 | BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); | |
3485 | m->sections[i] = first_tls; | |
3486 | first_tls = first_tls->next; | |
3487 | } | |
3488 | ||
3489 | *pm = m; | |
3490 | pm = &m->next; | |
252b5132 RH |
3491 | } |
3492 | ||
65765700 JJ |
3493 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
3494 | segment. */ | |
126495ed AM |
3495 | eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr; |
3496 | if (eh_frame_hdr != NULL | |
3497 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
65765700 JJ |
3498 | { |
3499 | amt = sizeof (struct elf_segment_map); | |
3500 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); | |
3501 | if (m == NULL) | |
3502 | goto error_return; | |
3503 | m->next = NULL; | |
3504 | m->p_type = PT_GNU_EH_FRAME; | |
3505 | m->count = 1; | |
126495ed | 3506 | m->sections[0] = eh_frame_hdr->output_section; |
65765700 JJ |
3507 | |
3508 | *pm = m; | |
3509 | pm = &m->next; | |
3510 | } | |
3511 | ||
252b5132 RH |
3512 | free (sections); |
3513 | sections = NULL; | |
3514 | ||
3515 | elf_tdata (abfd)->segment_map = mfirst; | |
b34976b6 | 3516 | return TRUE; |
252b5132 RH |
3517 | |
3518 | error_return: | |
3519 | if (sections != NULL) | |
3520 | free (sections); | |
b34976b6 | 3521 | return FALSE; |
252b5132 RH |
3522 | } |
3523 | ||
3524 | /* Sort sections by address. */ | |
3525 | ||
3526 | static int | |
3527 | elf_sort_sections (arg1, arg2) | |
3528 | const PTR arg1; | |
3529 | const PTR arg2; | |
3530 | { | |
3531 | const asection *sec1 = *(const asection **) arg1; | |
3532 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 3533 | bfd_size_type size1, size2; |
252b5132 RH |
3534 | |
3535 | /* Sort by LMA first, since this is the address used to | |
3536 | place the section into a segment. */ | |
3537 | if (sec1->lma < sec2->lma) | |
3538 | return -1; | |
3539 | else if (sec1->lma > sec2->lma) | |
3540 | return 1; | |
3541 | ||
3542 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
3543 | the same, and this will do nothing. */ | |
3544 | if (sec1->vma < sec2->vma) | |
3545 | return -1; | |
3546 | else if (sec1->vma > sec2->vma) | |
3547 | return 1; | |
3548 | ||
3549 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
3550 | ||
07c6e936 | 3551 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
3552 | |
3553 | if (TOEND (sec1)) | |
3554 | { | |
3555 | if (TOEND (sec2)) | |
00a7cdc5 NC |
3556 | { |
3557 | /* If the indicies are the same, do not return 0 | |
3558 | here, but continue to try the next comparison. */ | |
3559 | if (sec1->target_index - sec2->target_index != 0) | |
3560 | return sec1->target_index - sec2->target_index; | |
3561 | } | |
252b5132 RH |
3562 | else |
3563 | return 1; | |
3564 | } | |
00a7cdc5 | 3565 | else if (TOEND (sec2)) |
252b5132 RH |
3566 | return -1; |
3567 | ||
3568 | #undef TOEND | |
3569 | ||
00a7cdc5 NC |
3570 | /* Sort by size, to put zero sized sections |
3571 | before others at the same address. */ | |
252b5132 | 3572 | |
eecdbe52 JJ |
3573 | size1 = (sec1->flags & SEC_LOAD) ? sec1->_raw_size : 0; |
3574 | size2 = (sec2->flags & SEC_LOAD) ? sec2->_raw_size : 0; | |
3575 | ||
3576 | if (size1 < size2) | |
252b5132 | 3577 | return -1; |
eecdbe52 | 3578 | if (size1 > size2) |
252b5132 RH |
3579 | return 1; |
3580 | ||
3581 | return sec1->target_index - sec2->target_index; | |
3582 | } | |
3583 | ||
3584 | /* Assign file positions to the sections based on the mapping from | |
3585 | sections to segments. This function also sets up some fields in | |
3586 | the file header, and writes out the program headers. */ | |
3587 | ||
b34976b6 | 3588 | static bfd_boolean |
252b5132 RH |
3589 | assign_file_positions_for_segments (abfd) |
3590 | bfd *abfd; | |
3591 | { | |
3592 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3593 | unsigned int count; | |
3594 | struct elf_segment_map *m; | |
3595 | unsigned int alloc; | |
3596 | Elf_Internal_Phdr *phdrs; | |
3597 | file_ptr off, voff; | |
3598 | bfd_vma filehdr_vaddr, filehdr_paddr; | |
3599 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
3600 | Elf_Internal_Phdr *p; | |
dc810e39 | 3601 | bfd_size_type amt; |
252b5132 RH |
3602 | |
3603 | if (elf_tdata (abfd)->segment_map == NULL) | |
3604 | { | |
3605 | if (! map_sections_to_segments (abfd)) | |
b34976b6 | 3606 | return FALSE; |
252b5132 | 3607 | } |
1ed89aa9 NC |
3608 | else |
3609 | { | |
3610 | /* The placement algorithm assumes that non allocated sections are | |
3611 | not in PT_LOAD segments. We ensure this here by removing such | |
3612 | sections from the segment map. */ | |
3613 | for (m = elf_tdata (abfd)->segment_map; | |
3614 | m != NULL; | |
3615 | m = m->next) | |
3616 | { | |
3617 | unsigned int new_count; | |
3618 | unsigned int i; | |
3619 | ||
3620 | if (m->p_type != PT_LOAD) | |
3621 | continue; | |
3622 | ||
3623 | new_count = 0; | |
3624 | for (i = 0; i < m->count; i ++) | |
3625 | { | |
3626 | if ((m->sections[i]->flags & SEC_ALLOC) != 0) | |
3627 | { | |
47d9a591 | 3628 | if (i != new_count) |
1ed89aa9 NC |
3629 | m->sections[new_count] = m->sections[i]; |
3630 | ||
3631 | new_count ++; | |
3632 | } | |
3633 | } | |
3634 | ||
3635 | if (new_count != m->count) | |
3636 | m->count = new_count; | |
3637 | } | |
3638 | } | |
252b5132 RH |
3639 | |
3640 | if (bed->elf_backend_modify_segment_map) | |
3641 | { | |
3642 | if (! (*bed->elf_backend_modify_segment_map) (abfd)) | |
b34976b6 | 3643 | return FALSE; |
252b5132 RH |
3644 | } |
3645 | ||
3646 | count = 0; | |
3647 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
3648 | ++count; | |
3649 | ||
3650 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
3651 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
3652 | elf_elfheader (abfd)->e_phnum = count; | |
3653 | ||
3654 | if (count == 0) | |
b34976b6 | 3655 | return TRUE; |
252b5132 RH |
3656 | |
3657 | /* If we already counted the number of program segments, make sure | |
3658 | that we allocated enough space. This happens when SIZEOF_HEADERS | |
3659 | is used in a linker script. */ | |
3660 | alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr; | |
3661 | if (alloc != 0 && count > alloc) | |
3662 | { | |
3663 | ((*_bfd_error_handler) | |
3664 | (_("%s: Not enough room for program headers (allocated %u, need %u)"), | |
3665 | bfd_get_filename (abfd), alloc, count)); | |
3666 | bfd_set_error (bfd_error_bad_value); | |
b34976b6 | 3667 | return FALSE; |
252b5132 RH |
3668 | } |
3669 | ||
3670 | if (alloc == 0) | |
3671 | alloc = count; | |
3672 | ||
dc810e39 AM |
3673 | amt = alloc * sizeof (Elf_Internal_Phdr); |
3674 | phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt); | |
252b5132 | 3675 | if (phdrs == NULL) |
b34976b6 | 3676 | return FALSE; |
252b5132 RH |
3677 | |
3678 | off = bed->s->sizeof_ehdr; | |
3679 | off += alloc * bed->s->sizeof_phdr; | |
3680 | ||
3681 | filehdr_vaddr = 0; | |
3682 | filehdr_paddr = 0; | |
3683 | phdrs_vaddr = 0; | |
3684 | phdrs_paddr = 0; | |
3685 | ||
3686 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
3687 | m != NULL; | |
3688 | m = m->next, p++) | |
3689 | { | |
3690 | unsigned int i; | |
3691 | asection **secpp; | |
3692 | ||
3693 | /* If elf_segment_map is not from map_sections_to_segments, the | |
47d9a591 | 3694 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
3695 | not be done to the PT_NOTE section of a corefile, which may |
3696 | contain several pseudo-sections artificially created by bfd. | |
3697 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
3698 | if (m->count > 1 |
3699 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 3700 | && m->p_type == PT_NOTE)) |
252b5132 RH |
3701 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
3702 | elf_sort_sections); | |
3703 | ||
3704 | p->p_type = m->p_type; | |
28a7f3e7 | 3705 | p->p_flags = m->p_flags; |
252b5132 RH |
3706 | |
3707 | if (p->p_type == PT_LOAD | |
3708 | && m->count > 0 | |
3709 | && (m->sections[0]->flags & SEC_ALLOC) != 0) | |
3710 | { | |
3711 | if ((abfd->flags & D_PAGED) != 0) | |
3712 | off += (m->sections[0]->vma - off) % bed->maxpagesize; | |
3713 | else | |
3714 | { | |
3715 | bfd_size_type align; | |
3716 | ||
3717 | align = 0; | |
3718 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
3719 | { | |
3720 | bfd_size_type secalign; | |
3721 | ||
3722 | secalign = bfd_get_section_alignment (abfd, *secpp); | |
3723 | if (secalign > align) | |
3724 | align = secalign; | |
3725 | } | |
3726 | ||
3727 | off += (m->sections[0]->vma - off) % (1 << align); | |
3728 | } | |
3729 | } | |
3730 | ||
3731 | if (m->count == 0) | |
3732 | p->p_vaddr = 0; | |
3733 | else | |
3734 | p->p_vaddr = m->sections[0]->vma; | |
3735 | ||
3736 | if (m->p_paddr_valid) | |
3737 | p->p_paddr = m->p_paddr; | |
3738 | else if (m->count == 0) | |
3739 | p->p_paddr = 0; | |
3740 | else | |
3741 | p->p_paddr = m->sections[0]->lma; | |
3742 | ||
3743 | if (p->p_type == PT_LOAD | |
3744 | && (abfd->flags & D_PAGED) != 0) | |
3745 | p->p_align = bed->maxpagesize; | |
3746 | else if (m->count == 0) | |
3747 | p->p_align = bed->s->file_align; | |
3748 | else | |
3749 | p->p_align = 0; | |
3750 | ||
3751 | p->p_offset = 0; | |
3752 | p->p_filesz = 0; | |
3753 | p->p_memsz = 0; | |
3754 | ||
3755 | if (m->includes_filehdr) | |
3756 | { | |
3757 | if (! m->p_flags_valid) | |
3758 | p->p_flags |= PF_R; | |
3759 | p->p_offset = 0; | |
3760 | p->p_filesz = bed->s->sizeof_ehdr; | |
3761 | p->p_memsz = bed->s->sizeof_ehdr; | |
3762 | if (m->count > 0) | |
3763 | { | |
3764 | BFD_ASSERT (p->p_type == PT_LOAD); | |
3765 | ||
3766 | if (p->p_vaddr < (bfd_vma) off) | |
3767 | { | |
caf47ea6 AM |
3768 | (*_bfd_error_handler) |
3769 | (_("%s: Not enough room for program headers, try linking with -N"), | |
3770 | bfd_get_filename (abfd)); | |
252b5132 | 3771 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 3772 | return FALSE; |
252b5132 RH |
3773 | } |
3774 | ||
3775 | p->p_vaddr -= off; | |
3776 | if (! m->p_paddr_valid) | |
3777 | p->p_paddr -= off; | |
3778 | } | |
3779 | if (p->p_type == PT_LOAD) | |
3780 | { | |
3781 | filehdr_vaddr = p->p_vaddr; | |
3782 | filehdr_paddr = p->p_paddr; | |
3783 | } | |
3784 | } | |
3785 | ||
3786 | if (m->includes_phdrs) | |
3787 | { | |
3788 | if (! m->p_flags_valid) | |
3789 | p->p_flags |= PF_R; | |
3790 | ||
3791 | if (m->includes_filehdr) | |
3792 | { | |
3793 | if (p->p_type == PT_LOAD) | |
3794 | { | |
3795 | phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr; | |
3796 | phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr; | |
3797 | } | |
3798 | } | |
3799 | else | |
3800 | { | |
3801 | p->p_offset = bed->s->sizeof_ehdr; | |
3802 | ||
3803 | if (m->count > 0) | |
3804 | { | |
3805 | BFD_ASSERT (p->p_type == PT_LOAD); | |
3806 | p->p_vaddr -= off - p->p_offset; | |
3807 | if (! m->p_paddr_valid) | |
3808 | p->p_paddr -= off - p->p_offset; | |
3809 | } | |
3810 | ||
3811 | if (p->p_type == PT_LOAD) | |
3812 | { | |
3813 | phdrs_vaddr = p->p_vaddr; | |
3814 | phdrs_paddr = p->p_paddr; | |
3815 | } | |
3816 | else | |
3817 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
3818 | } | |
3819 | ||
3820 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
3821 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
3822 | } | |
3823 | ||
3824 | if (p->p_type == PT_LOAD | |
3825 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
3826 | { | |
3827 | if (! m->includes_filehdr && ! m->includes_phdrs) | |
3828 | p->p_offset = off; | |
3829 | else | |
3830 | { | |
3831 | file_ptr adjust; | |
3832 | ||
3833 | adjust = off - (p->p_offset + p->p_filesz); | |
3834 | p->p_filesz += adjust; | |
3835 | p->p_memsz += adjust; | |
3836 | } | |
3837 | } | |
3838 | ||
3839 | voff = off; | |
3840 | ||
3841 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) | |
3842 | { | |
3843 | asection *sec; | |
3844 | flagword flags; | |
3845 | bfd_size_type align; | |
3846 | ||
3847 | sec = *secpp; | |
3848 | flags = sec->flags; | |
3849 | align = 1 << bfd_get_section_alignment (abfd, sec); | |
3850 | ||
3851 | /* The section may have artificial alignment forced by a | |
3852 | link script. Notice this case by the gap between the | |
f5ffc919 NC |
3853 | cumulative phdr lma and the section's lma. */ |
3854 | if (p->p_paddr + p->p_memsz < sec->lma) | |
252b5132 | 3855 | { |
f5ffc919 | 3856 | bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz); |
252b5132 RH |
3857 | |
3858 | p->p_memsz += adjust; | |
eecdbe52 JJ |
3859 | if (p->p_type == PT_LOAD |
3860 | || (p->p_type == PT_NOTE | |
3861 | && bfd_get_format (abfd) == bfd_core)) | |
3862 | { | |
3863 | off += adjust; | |
3864 | voff += adjust; | |
3865 | } | |
3866 | if ((flags & SEC_LOAD) != 0 | |
3867 | || (flags & SEC_THREAD_LOCAL) != 0) | |
252b5132 RH |
3868 | p->p_filesz += adjust; |
3869 | } | |
3870 | ||
3871 | if (p->p_type == PT_LOAD) | |
3872 | { | |
3873 | bfd_signed_vma adjust; | |
3874 | ||
3875 | if ((flags & SEC_LOAD) != 0) | |
3876 | { | |
3877 | adjust = sec->lma - (p->p_paddr + p->p_memsz); | |
3878 | if (adjust < 0) | |
3879 | adjust = 0; | |
3880 | } | |
3881 | else if ((flags & SEC_ALLOC) != 0) | |
3882 | { | |
3883 | /* The section VMA must equal the file position | |
3884 | modulo the page size. FIXME: I'm not sure if | |
3885 | this adjustment is really necessary. We used to | |
3886 | not have the SEC_LOAD case just above, and then | |
3887 | this was necessary, but now I'm not sure. */ | |
3888 | if ((abfd->flags & D_PAGED) != 0) | |
3889 | adjust = (sec->vma - voff) % bed->maxpagesize; | |
3890 | else | |
3891 | adjust = (sec->vma - voff) % align; | |
3892 | } | |
3893 | else | |
3894 | adjust = 0; | |
3895 | ||
3896 | if (adjust != 0) | |
3897 | { | |
3898 | if (i == 0) | |
3899 | { | |
cdc7c09f NC |
3900 | (* _bfd_error_handler) (_("\ |
3901 | Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"), | |
3902 | bfd_section_name (abfd, sec), | |
3903 | sec->lma, | |
3904 | p->p_paddr); | |
b34976b6 | 3905 | return FALSE; |
252b5132 RH |
3906 | } |
3907 | p->p_memsz += adjust; | |
3908 | off += adjust; | |
3909 | voff += adjust; | |
3910 | if ((flags & SEC_LOAD) != 0) | |
3911 | p->p_filesz += adjust; | |
3912 | } | |
3913 | ||
3914 | sec->filepos = off; | |
3915 | ||
3916 | /* We check SEC_HAS_CONTENTS here because if NOLOAD is | |
3917 | used in a linker script we may have a section with | |
3918 | SEC_LOAD clear but which is supposed to have | |
3919 | contents. */ | |
3920 | if ((flags & SEC_LOAD) != 0 | |
3921 | || (flags & SEC_HAS_CONTENTS) != 0) | |
3922 | off += sec->_raw_size; | |
3923 | ||
eecdbe52 JJ |
3924 | if ((flags & SEC_ALLOC) != 0 |
3925 | && ((flags & SEC_LOAD) != 0 | |
3926 | || (flags & SEC_THREAD_LOCAL) == 0)) | |
252b5132 RH |
3927 | voff += sec->_raw_size; |
3928 | } | |
3929 | ||
3930 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
3931 | { | |
4a938328 MS |
3932 | /* The actual "note" segment has i == 0. |
3933 | This is the one that actually contains everything. */ | |
3934 | if (i == 0) | |
3935 | { | |
252b5132 RH |
3936 | sec->filepos = off; |
3937 | p->p_filesz = sec->_raw_size; | |
3938 | off += sec->_raw_size; | |
3939 | voff = off; | |
3940 | } | |
4a938328 | 3941 | else |
252b5132 | 3942 | { |
4a938328 | 3943 | /* Fake sections -- don't need to be written. */ |
252b5132 RH |
3944 | sec->filepos = 0; |
3945 | sec->_raw_size = 0; | |
4a938328 | 3946 | flags = sec->flags = 0; |
252b5132 RH |
3947 | } |
3948 | p->p_memsz = 0; | |
3949 | p->p_align = 1; | |
3950 | } | |
3951 | else | |
3952 | { | |
eecdbe52 JJ |
3953 | if ((sec->flags & SEC_LOAD) != 0 |
3954 | || (sec->flags & SEC_THREAD_LOCAL) == 0 | |
3955 | || p->p_type == PT_TLS) | |
252b5132 RH |
3956 | p->p_memsz += sec->_raw_size; |
3957 | ||
3958 | if ((flags & SEC_LOAD) != 0) | |
3959 | p->p_filesz += sec->_raw_size; | |
3960 | ||
13ae64f3 JJ |
3961 | if (p->p_type == PT_TLS |
3962 | && sec->_raw_size == 0 | |
3963 | && (sec->flags & SEC_HAS_CONTENTS) == 0) | |
3964 | { | |
3965 | struct bfd_link_order *o; | |
3966 | bfd_vma tbss_size = 0; | |
3967 | ||
3968 | for (o = sec->link_order_head; o != NULL; o = o->next) | |
3969 | if (tbss_size < o->offset + o->size) | |
3970 | tbss_size = o->offset + o->size; | |
3971 | ||
3972 | p->p_memsz += tbss_size; | |
3973 | } | |
3974 | ||
252b5132 RH |
3975 | if (align > p->p_align |
3976 | && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0)) | |
3977 | p->p_align = align; | |
3978 | } | |
3979 | ||
3980 | if (! m->p_flags_valid) | |
3981 | { | |
3982 | p->p_flags |= PF_R; | |
3983 | if ((flags & SEC_CODE) != 0) | |
3984 | p->p_flags |= PF_X; | |
3985 | if ((flags & SEC_READONLY) == 0) | |
3986 | p->p_flags |= PF_W; | |
3987 | } | |
3988 | } | |
3989 | } | |
3990 | ||
3991 | /* Now that we have set the section file positions, we can set up | |
3992 | the file positions for the non PT_LOAD segments. */ | |
3993 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
3994 | m != NULL; | |
3995 | m = m->next, p++) | |
3996 | { | |
3997 | if (p->p_type != PT_LOAD && m->count > 0) | |
3998 | { | |
3999 | BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs); | |
4000 | p->p_offset = m->sections[0]->filepos; | |
4001 | } | |
4002 | if (m->count == 0) | |
4003 | { | |
4004 | if (m->includes_filehdr) | |
4005 | { | |
4006 | p->p_vaddr = filehdr_vaddr; | |
4007 | if (! m->p_paddr_valid) | |
4008 | p->p_paddr = filehdr_paddr; | |
4009 | } | |
4010 | else if (m->includes_phdrs) | |
4011 | { | |
4012 | p->p_vaddr = phdrs_vaddr; | |
4013 | if (! m->p_paddr_valid) | |
4014 | p->p_paddr = phdrs_paddr; | |
4015 | } | |
4016 | } | |
4017 | } | |
4018 | ||
4019 | /* Clear out any program headers we allocated but did not use. */ | |
4020 | for (; count < alloc; count++, p++) | |
4021 | { | |
4022 | memset (p, 0, sizeof *p); | |
4023 | p->p_type = PT_NULL; | |
4024 | } | |
4025 | ||
4026 | elf_tdata (abfd)->phdr = phdrs; | |
4027 | ||
4028 | elf_tdata (abfd)->next_file_pos = off; | |
4029 | ||
4030 | /* Write out the program headers. */ | |
dc810e39 | 4031 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
252b5132 | 4032 | || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0) |
b34976b6 | 4033 | return FALSE; |
252b5132 | 4034 | |
b34976b6 | 4035 | return TRUE; |
252b5132 RH |
4036 | } |
4037 | ||
4038 | /* Get the size of the program header. | |
4039 | ||
4040 | If this is called by the linker before any of the section VMA's are set, it | |
4041 | can't calculate the correct value for a strange memory layout. This only | |
4042 | happens when SIZEOF_HEADERS is used in a linker script. In this case, | |
4043 | SORTED_HDRS is NULL and we assume the normal scenario of one text and one | |
4044 | data segment (exclusive of .interp and .dynamic). | |
4045 | ||
4046 | ??? User written scripts must either not use SIZEOF_HEADERS, or assume there | |
4047 | will be two segments. */ | |
4048 | ||
4049 | static bfd_size_type | |
4050 | get_program_header_size (abfd) | |
4051 | bfd *abfd; | |
4052 | { | |
4053 | size_t segs; | |
4054 | asection *s; | |
4055 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4056 | ||
4057 | /* We can't return a different result each time we're called. */ | |
4058 | if (elf_tdata (abfd)->program_header_size != 0) | |
4059 | return elf_tdata (abfd)->program_header_size; | |
4060 | ||
4061 | if (elf_tdata (abfd)->segment_map != NULL) | |
4062 | { | |
4063 | struct elf_segment_map *m; | |
4064 | ||
4065 | segs = 0; | |
4066 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
4067 | ++segs; | |
4068 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; | |
4069 | return elf_tdata (abfd)->program_header_size; | |
4070 | } | |
4071 | ||
4072 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4073 | and one for data. */ | |
4074 | segs = 2; | |
4075 | ||
4076 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4077 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4078 | { | |
4079 | /* If we have a loadable interpreter section, we need a | |
4080 | PT_INTERP segment. In this case, assume we also need a | |
ab3acfbe | 4081 | PT_PHDR segment, although that may not be true for all |
252b5132 RH |
4082 | targets. */ |
4083 | segs += 2; | |
4084 | } | |
4085 | ||
4086 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4087 | { | |
4088 | /* We need a PT_DYNAMIC segment. */ | |
4089 | ++segs; | |
4090 | } | |
4091 | ||
126495ed | 4092 | if (elf_tdata (abfd)->eh_frame_hdr) |
65765700 JJ |
4093 | { |
4094 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4095 | ++segs; | |
4096 | } | |
4097 | ||
252b5132 RH |
4098 | for (s = abfd->sections; s != NULL; s = s->next) |
4099 | { | |
4100 | if ((s->flags & SEC_LOAD) != 0 | |
4101 | && strncmp (s->name, ".note", 5) == 0) | |
4102 | { | |
4103 | /* We need a PT_NOTE segment. */ | |
4104 | ++segs; | |
4105 | } | |
4106 | } | |
4107 | ||
13ae64f3 JJ |
4108 | for (s = abfd->sections; s != NULL; s = s->next) |
4109 | { | |
4110 | if (s->flags & SEC_THREAD_LOCAL) | |
4111 | { | |
4112 | /* We need a PT_TLS segment. */ | |
4113 | ++segs; | |
4114 | break; | |
4115 | } | |
4116 | } | |
4117 | ||
252b5132 RH |
4118 | /* Let the backend count up any program headers it might need. */ |
4119 | if (bed->elf_backend_additional_program_headers) | |
4120 | { | |
4121 | int a; | |
4122 | ||
4123 | a = (*bed->elf_backend_additional_program_headers) (abfd); | |
4124 | if (a == -1) | |
4125 | abort (); | |
4126 | segs += a; | |
4127 | } | |
4128 | ||
4129 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; | |
4130 | return elf_tdata (abfd)->program_header_size; | |
4131 | } | |
4132 | ||
4133 | /* Work out the file positions of all the sections. This is called by | |
4134 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
4135 | VMAs must be known before this is called. | |
4136 | ||
4137 | We do not consider reloc sections at this point, unless they form | |
4138 | part of the loadable image. Reloc sections are assigned file | |
4139 | positions in assign_file_positions_for_relocs, which is called by | |
4140 | write_object_contents and final_link. | |
4141 | ||
4142 | We also don't set the positions of the .symtab and .strtab here. */ | |
4143 | ||
b34976b6 | 4144 | static bfd_boolean |
252b5132 RH |
4145 | assign_file_positions_except_relocs (abfd) |
4146 | bfd *abfd; | |
4147 | { | |
4148 | struct elf_obj_tdata * const tdata = elf_tdata (abfd); | |
4149 | Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); | |
4150 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
9ad5cbcf | 4151 | unsigned int num_sec = elf_numsections (abfd); |
252b5132 RH |
4152 | file_ptr off; |
4153 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4154 | ||
4155 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
4156 | && bfd_get_format (abfd) != bfd_core) | |
4157 | { | |
4158 | Elf_Internal_Shdr **hdrpp; | |
4159 | unsigned int i; | |
4160 | ||
4161 | /* Start after the ELF header. */ | |
4162 | off = i_ehdrp->e_ehsize; | |
4163 | ||
4164 | /* We are not creating an executable, which means that we are | |
4165 | not creating a program header, and that the actual order of | |
4166 | the sections in the file is unimportant. */ | |
9ad5cbcf | 4167 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
4168 | { |
4169 | Elf_Internal_Shdr *hdr; | |
4170 | ||
4171 | hdr = *hdrpp; | |
9ad5cbcf AM |
4172 | if (hdr->sh_type == SHT_REL |
4173 | || hdr->sh_type == SHT_RELA | |
4174 | || i == tdata->symtab_section | |
4175 | || i == tdata->symtab_shndx_section | |
252b5132 RH |
4176 | || i == tdata->strtab_section) |
4177 | { | |
4178 | hdr->sh_offset = -1; | |
252b5132 | 4179 | } |
9ad5cbcf | 4180 | else |
b34976b6 | 4181 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4182 | |
9ad5cbcf AM |
4183 | if (i == SHN_LORESERVE - 1) |
4184 | { | |
4185 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
4186 | hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
4187 | } | |
252b5132 RH |
4188 | } |
4189 | } | |
4190 | else | |
4191 | { | |
4192 | unsigned int i; | |
4193 | Elf_Internal_Shdr **hdrpp; | |
4194 | ||
4195 | /* Assign file positions for the loaded sections based on the | |
4196 | assignment of sections to segments. */ | |
4197 | if (! assign_file_positions_for_segments (abfd)) | |
b34976b6 | 4198 | return FALSE; |
252b5132 RH |
4199 | |
4200 | /* Assign file positions for the other sections. */ | |
4201 | ||
4202 | off = elf_tdata (abfd)->next_file_pos; | |
9ad5cbcf | 4203 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
4204 | { |
4205 | Elf_Internal_Shdr *hdr; | |
4206 | ||
4207 | hdr = *hdrpp; | |
4208 | if (hdr->bfd_section != NULL | |
4209 | && hdr->bfd_section->filepos != 0) | |
4210 | hdr->sh_offset = hdr->bfd_section->filepos; | |
4211 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
4212 | { | |
4213 | ((*_bfd_error_handler) | |
4214 | (_("%s: warning: allocated section `%s' not in segment"), | |
4215 | bfd_get_filename (abfd), | |
4216 | (hdr->bfd_section == NULL | |
4217 | ? "*unknown*" | |
4218 | : hdr->bfd_section->name))); | |
4219 | if ((abfd->flags & D_PAGED) != 0) | |
4220 | off += (hdr->sh_addr - off) % bed->maxpagesize; | |
4221 | else | |
4222 | off += (hdr->sh_addr - off) % hdr->sh_addralign; | |
4223 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
b34976b6 | 4224 | FALSE); |
252b5132 RH |
4225 | } |
4226 | else if (hdr->sh_type == SHT_REL | |
4227 | || hdr->sh_type == SHT_RELA | |
4228 | || hdr == i_shdrpp[tdata->symtab_section] | |
9ad5cbcf | 4229 | || hdr == i_shdrpp[tdata->symtab_shndx_section] |
252b5132 RH |
4230 | || hdr == i_shdrpp[tdata->strtab_section]) |
4231 | hdr->sh_offset = -1; | |
4232 | else | |
b34976b6 | 4233 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
9ad5cbcf AM |
4234 | |
4235 | if (i == SHN_LORESERVE - 1) | |
4236 | { | |
4237 | i += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
4238 | hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
4239 | } | |
252b5132 RH |
4240 | } |
4241 | } | |
4242 | ||
4243 | /* Place the section headers. */ | |
4244 | off = align_file_position (off, bed->s->file_align); | |
4245 | i_ehdrp->e_shoff = off; | |
4246 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
4247 | ||
4248 | elf_tdata (abfd)->next_file_pos = off; | |
4249 | ||
b34976b6 | 4250 | return TRUE; |
252b5132 RH |
4251 | } |
4252 | ||
b34976b6 | 4253 | static bfd_boolean |
252b5132 RH |
4254 | prep_headers (abfd) |
4255 | bfd *abfd; | |
4256 | { | |
4257 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
4258 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
4259 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
2b0f7ef9 | 4260 | struct elf_strtab_hash *shstrtab; |
252b5132 RH |
4261 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
4262 | ||
4263 | i_ehdrp = elf_elfheader (abfd); | |
4264 | i_shdrp = elf_elfsections (abfd); | |
4265 | ||
2b0f7ef9 | 4266 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 4267 | if (shstrtab == NULL) |
b34976b6 | 4268 | return FALSE; |
252b5132 RH |
4269 | |
4270 | elf_shstrtab (abfd) = shstrtab; | |
4271 | ||
4272 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
4273 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
4274 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
4275 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
4276 | ||
4277 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
4278 | i_ehdrp->e_ident[EI_DATA] = | |
4279 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
4280 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
4281 | ||
252b5132 RH |
4282 | if ((abfd->flags & DYNAMIC) != 0) |
4283 | i_ehdrp->e_type = ET_DYN; | |
4284 | else if ((abfd->flags & EXEC_P) != 0) | |
4285 | i_ehdrp->e_type = ET_EXEC; | |
4286 | else if (bfd_get_format (abfd) == bfd_core) | |
4287 | i_ehdrp->e_type = ET_CORE; | |
4288 | else | |
4289 | i_ehdrp->e_type = ET_REL; | |
4290 | ||
4291 | switch (bfd_get_arch (abfd)) | |
4292 | { | |
4293 | case bfd_arch_unknown: | |
4294 | i_ehdrp->e_machine = EM_NONE; | |
4295 | break; | |
aa4f99bb AO |
4296 | |
4297 | /* There used to be a long list of cases here, each one setting | |
4298 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
4299 | in the corresponding bfd definition. To avoid duplication, | |
4300 | the switch was removed. Machines that need special handling | |
4301 | can generally do it in elf_backend_final_write_processing(), | |
4302 | unless they need the information earlier than the final write. | |
4303 | Such need can generally be supplied by replacing the tests for | |
4304 | e_machine with the conditions used to determine it. */ | |
252b5132 | 4305 | default: |
aa4f99bb AO |
4306 | if (get_elf_backend_data (abfd) != NULL) |
4307 | i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code; | |
4308 | else | |
4309 | i_ehdrp->e_machine = EM_NONE; | |
4310 | } | |
4311 | ||
252b5132 RH |
4312 | i_ehdrp->e_version = bed->s->ev_current; |
4313 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
4314 | ||
c044fabd | 4315 | /* No program header, for now. */ |
252b5132 RH |
4316 | i_ehdrp->e_phoff = 0; |
4317 | i_ehdrp->e_phentsize = 0; | |
4318 | i_ehdrp->e_phnum = 0; | |
4319 | ||
c044fabd | 4320 | /* Each bfd section is section header entry. */ |
252b5132 RH |
4321 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
4322 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
4323 | ||
c044fabd | 4324 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 RH |
4325 | if (abfd->flags & EXEC_P) |
4326 | { | |
c044fabd | 4327 | /* It all happens later. */ |
252b5132 RH |
4328 | #if 0 |
4329 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
4330 | ||
4331 | /* elf_build_phdrs() returns a (NULL-terminated) array of | |
c044fabd | 4332 | Elf_Internal_Phdrs. */ |
252b5132 RH |
4333 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); |
4334 | i_ehdrp->e_phoff = outbase; | |
4335 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
4336 | #endif | |
4337 | } | |
4338 | else | |
4339 | { | |
4340 | i_ehdrp->e_phentsize = 0; | |
4341 | i_phdrp = 0; | |
4342 | i_ehdrp->e_phoff = 0; | |
4343 | } | |
4344 | ||
4345 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 4346 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 4347 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 4348 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 4349 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 4350 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 RH |
4351 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
4352 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
4353 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
b34976b6 | 4354 | return FALSE; |
252b5132 | 4355 | |
b34976b6 | 4356 | return TRUE; |
252b5132 RH |
4357 | } |
4358 | ||
4359 | /* Assign file positions for all the reloc sections which are not part | |
4360 | of the loadable file image. */ | |
4361 | ||
4362 | void | |
4363 | _bfd_elf_assign_file_positions_for_relocs (abfd) | |
4364 | bfd *abfd; | |
4365 | { | |
4366 | file_ptr off; | |
9ad5cbcf | 4367 | unsigned int i, num_sec; |
252b5132 RH |
4368 | Elf_Internal_Shdr **shdrpp; |
4369 | ||
4370 | off = elf_tdata (abfd)->next_file_pos; | |
4371 | ||
9ad5cbcf AM |
4372 | num_sec = elf_numsections (abfd); |
4373 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) | |
252b5132 RH |
4374 | { |
4375 | Elf_Internal_Shdr *shdrp; | |
4376 | ||
4377 | shdrp = *shdrpp; | |
4378 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
4379 | && shdrp->sh_offset == -1) | |
b34976b6 | 4380 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); |
252b5132 RH |
4381 | } |
4382 | ||
4383 | elf_tdata (abfd)->next_file_pos = off; | |
4384 | } | |
4385 | ||
b34976b6 | 4386 | bfd_boolean |
252b5132 RH |
4387 | _bfd_elf_write_object_contents (abfd) |
4388 | bfd *abfd; | |
4389 | { | |
4390 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
4391 | Elf_Internal_Ehdr *i_ehdrp; | |
4392 | Elf_Internal_Shdr **i_shdrp; | |
b34976b6 | 4393 | bfd_boolean failed; |
9ad5cbcf | 4394 | unsigned int count, num_sec; |
252b5132 RH |
4395 | |
4396 | if (! abfd->output_has_begun | |
4397 | && ! _bfd_elf_compute_section_file_positions | |
4398 | (abfd, (struct bfd_link_info *) NULL)) | |
b34976b6 | 4399 | return FALSE; |
252b5132 RH |
4400 | |
4401 | i_shdrp = elf_elfsections (abfd); | |
4402 | i_ehdrp = elf_elfheader (abfd); | |
4403 | ||
b34976b6 | 4404 | failed = FALSE; |
252b5132 RH |
4405 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
4406 | if (failed) | |
b34976b6 | 4407 | return FALSE; |
252b5132 RH |
4408 | |
4409 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
4410 | ||
c044fabd | 4411 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
4412 | num_sec = elf_numsections (abfd); |
4413 | for (count = 1; count < num_sec; count++) | |
252b5132 RH |
4414 | { |
4415 | if (bed->elf_backend_section_processing) | |
4416 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
4417 | if (i_shdrp[count]->contents) | |
4418 | { | |
dc810e39 AM |
4419 | bfd_size_type amt = i_shdrp[count]->sh_size; |
4420 | ||
252b5132 | 4421 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 4422 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 4423 | return FALSE; |
252b5132 | 4424 | } |
9ad5cbcf AM |
4425 | if (count == SHN_LORESERVE - 1) |
4426 | count += SHN_HIRESERVE + 1 - SHN_LORESERVE; | |
252b5132 RH |
4427 | } |
4428 | ||
4429 | /* Write out the section header names. */ | |
4430 | if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
2b0f7ef9 | 4431 | || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))) |
b34976b6 | 4432 | return FALSE; |
252b5132 RH |
4433 | |
4434 | if (bed->elf_backend_final_write_processing) | |
4435 | (*bed->elf_backend_final_write_processing) (abfd, | |
4436 | elf_tdata (abfd)->linker); | |
4437 | ||
4438 | return bed->s->write_shdrs_and_ehdr (abfd); | |
4439 | } | |
4440 | ||
b34976b6 | 4441 | bfd_boolean |
252b5132 RH |
4442 | _bfd_elf_write_corefile_contents (abfd) |
4443 | bfd *abfd; | |
4444 | { | |
c044fabd | 4445 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
4446 | return _bfd_elf_write_object_contents (abfd); |
4447 | } | |
c044fabd KH |
4448 | |
4449 | /* Given a section, search the header to find them. */ | |
4450 | ||
252b5132 RH |
4451 | int |
4452 | _bfd_elf_section_from_bfd_section (abfd, asect) | |
4453 | bfd *abfd; | |
4454 | struct sec *asect; | |
4455 | { | |
af746e92 | 4456 | struct elf_backend_data *bed; |
252b5132 | 4457 | int index; |
252b5132 | 4458 | |
9ad5cbcf AM |
4459 | if (elf_section_data (asect) != NULL |
4460 | && elf_section_data (asect)->this_idx != 0) | |
4461 | return elf_section_data (asect)->this_idx; | |
4462 | ||
4463 | if (bfd_is_abs_section (asect)) | |
af746e92 AM |
4464 | index = SHN_ABS; |
4465 | else if (bfd_is_com_section (asect)) | |
4466 | index = SHN_COMMON; | |
4467 | else if (bfd_is_und_section (asect)) | |
4468 | index = SHN_UNDEF; | |
4469 | else | |
252b5132 | 4470 | { |
af746e92 AM |
4471 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); |
4472 | int maxindex = elf_numsections (abfd); | |
4473 | ||
4474 | for (index = 1; index < maxindex; index++) | |
4475 | { | |
4476 | Elf_Internal_Shdr *hdr = i_shdrp[index]; | |
4477 | ||
4478 | if (hdr != NULL && hdr->bfd_section == asect) | |
4479 | return index; | |
4480 | } | |
4481 | index = -1; | |
252b5132 RH |
4482 | } |
4483 | ||
af746e92 | 4484 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
4485 | if (bed->elf_backend_section_from_bfd_section) |
4486 | { | |
af746e92 | 4487 | int retval = index; |
9ad5cbcf | 4488 | |
af746e92 AM |
4489 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
4490 | return retval; | |
252b5132 RH |
4491 | } |
4492 | ||
af746e92 AM |
4493 | if (index == -1) |
4494 | bfd_set_error (bfd_error_nonrepresentable_section); | |
252b5132 | 4495 | |
af746e92 | 4496 | return index; |
252b5132 RH |
4497 | } |
4498 | ||
4499 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
4500 | on error. */ | |
4501 | ||
4502 | int | |
4503 | _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) | |
4504 | bfd *abfd; | |
4505 | asymbol **asym_ptr_ptr; | |
4506 | { | |
4507 | asymbol *asym_ptr = *asym_ptr_ptr; | |
4508 | int idx; | |
4509 | flagword flags = asym_ptr->flags; | |
4510 | ||
4511 | /* When gas creates relocations against local labels, it creates its | |
4512 | own symbol for the section, but does put the symbol into the | |
4513 | symbol chain, so udata is 0. When the linker is generating | |
4514 | relocatable output, this section symbol may be for one of the | |
4515 | input sections rather than the output section. */ | |
4516 | if (asym_ptr->udata.i == 0 | |
4517 | && (flags & BSF_SECTION_SYM) | |
4518 | && asym_ptr->section) | |
4519 | { | |
4520 | int indx; | |
4521 | ||
4522 | if (asym_ptr->section->output_section != NULL) | |
4523 | indx = asym_ptr->section->output_section->index; | |
4524 | else | |
4525 | indx = asym_ptr->section->index; | |
4e89ac30 L |
4526 | if (indx < elf_num_section_syms (abfd) |
4527 | && elf_section_syms (abfd)[indx] != NULL) | |
252b5132 RH |
4528 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
4529 | } | |
4530 | ||
4531 | idx = asym_ptr->udata.i; | |
4532 | ||
4533 | if (idx == 0) | |
4534 | { | |
4535 | /* This case can occur when using --strip-symbol on a symbol | |
4536 | which is used in a relocation entry. */ | |
4537 | (*_bfd_error_handler) | |
4538 | (_("%s: symbol `%s' required but not present"), | |
8f615d07 | 4539 | bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr)); |
252b5132 RH |
4540 | bfd_set_error (bfd_error_no_symbols); |
4541 | return -1; | |
4542 | } | |
4543 | ||
4544 | #if DEBUG & 4 | |
4545 | { | |
4546 | fprintf (stderr, | |
661a3fd4 | 4547 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", |
252b5132 RH |
4548 | (long) asym_ptr, asym_ptr->name, idx, flags, |
4549 | elf_symbol_flags (flags)); | |
4550 | fflush (stderr); | |
4551 | } | |
4552 | #endif | |
4553 | ||
4554 | return idx; | |
4555 | } | |
4556 | ||
4557 | /* Copy private BFD data. This copies any program header information. */ | |
4558 | ||
b34976b6 | 4559 | static bfd_boolean |
252b5132 RH |
4560 | copy_private_bfd_data (ibfd, obfd) |
4561 | bfd *ibfd; | |
4562 | bfd *obfd; | |
4563 | { | |
b34976b6 AM |
4564 | Elf_Internal_Ehdr *iehdr; |
4565 | struct elf_segment_map *map; | |
4566 | struct elf_segment_map *map_first; | |
4567 | struct elf_segment_map **pointer_to_map; | |
4568 | Elf_Internal_Phdr *segment; | |
4569 | asection *section; | |
4570 | unsigned int i; | |
4571 | unsigned int num_segments; | |
4572 | bfd_boolean phdr_included = FALSE; | |
4573 | bfd_vma maxpagesize; | |
4574 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
4575 | unsigned int phdr_adjust_num = 0; | |
4576 | struct elf_backend_data *bed; | |
bc67d8a6 | 4577 | |
c044fabd | 4578 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
252b5132 | 4579 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 4580 | return TRUE; |
252b5132 RH |
4581 | |
4582 | if (elf_tdata (ibfd)->phdr == NULL) | |
b34976b6 | 4583 | return TRUE; |
252b5132 | 4584 | |
caf47ea6 | 4585 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
4586 | iehdr = elf_elfheader (ibfd); |
4587 | ||
bc67d8a6 | 4588 | map_first = NULL; |
c044fabd | 4589 | pointer_to_map = &map_first; |
252b5132 RH |
4590 | |
4591 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
4592 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
4593 | ||
4594 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
4595 | #define SEGMENT_END(segment, start) \ |
4596 | (start + (segment->p_memsz > segment->p_filesz \ | |
4597 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 4598 | |
eecdbe52 JJ |
4599 | #define SECTION_SIZE(section, segment) \ |
4600 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
4601 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
4602 | ? section->_raw_size : 0) | |
4603 | ||
b34976b6 | 4604 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 4605 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
4606 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
4607 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 4608 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 4609 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 4610 | |
b34976b6 | 4611 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 4612 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
4613 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
4614 | (section->lma >= base \ | |
eecdbe52 | 4615 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 4616 | <= SEGMENT_END (segment, base))) |
252b5132 | 4617 | |
c044fabd | 4618 | /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */ |
aecc8f8a AM |
4619 | #define IS_COREFILE_NOTE(p, s) \ |
4620 | (p->p_type == PT_NOTE \ | |
4621 | && bfd_get_format (ibfd) == bfd_core \ | |
4622 | && s->vma == 0 && s->lma == 0 \ | |
4623 | && (bfd_vma) s->filepos >= p->p_offset \ | |
4624 | && ((bfd_vma) s->filepos + s->_raw_size \ | |
4625 | <= p->p_offset + p->p_filesz)) | |
252b5132 RH |
4626 | |
4627 | /* The complicated case when p_vaddr is 0 is to handle the Solaris | |
4628 | linker, which generates a PT_INTERP section with p_vaddr and | |
4629 | p_memsz set to 0. */ | |
aecc8f8a AM |
4630 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
4631 | (p->p_vaddr == 0 \ | |
4632 | && p->p_paddr == 0 \ | |
4633 | && p->p_memsz == 0 \ | |
4634 | && p->p_filesz > 0 \ | |
4635 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
4636 | && s->_raw_size > 0 \ | |
4637 | && (bfd_vma) s->filepos >= p->p_offset \ | |
4638 | && ((bfd_vma) s->filepos + s->_raw_size \ | |
4639 | <= p->p_offset + p->p_filesz)) | |
5c440b1e | 4640 | |
bc67d8a6 NC |
4641 | /* Decide if the given section should be included in the given segment. |
4642 | A section will be included if: | |
f5ffc919 NC |
4643 | 1. It is within the address space of the segment -- we use the LMA |
4644 | if that is set for the segment and the VMA otherwise, | |
bc67d8a6 NC |
4645 | 2. It is an allocated segment, |
4646 | 3. There is an output section associated with it, | |
eecdbe52 JJ |
4647 | 4. The section has not already been allocated to a previous segment. |
4648 | 5. PT_TLS segment includes only SHF_TLS sections. | |
4649 | 6. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. */ | |
caf47ea6 | 4650 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ |
aecc8f8a AM |
4651 | ((((segment->p_paddr \ |
4652 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
4653 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
f5ffc919 | 4654 | && (section->flags & SEC_ALLOC) != 0) \ |
b6821651 | 4655 | || IS_COREFILE_NOTE (segment, section)) \ |
f5ffc919 | 4656 | && section->output_section != NULL \ |
eecdbe52 JJ |
4657 | && (segment->p_type != PT_TLS \ |
4658 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
4659 | && (segment->p_type == PT_LOAD \ | |
4660 | || segment->p_type == PT_TLS \ | |
4661 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
82e51918 | 4662 | && ! section->segment_mark) |
bc67d8a6 | 4663 | |
b34976b6 | 4664 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
4665 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
4666 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
4667 | ||
4668 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
4669 | their VMA address ranges and their LMA address ranges overlap. | |
4670 | It is possible to have overlapping VMA ranges without overlapping LMA | |
4671 | ranges. RedBoot images for example can have both .data and .bss mapped | |
4672 | to the same VMA range, but with the .data section mapped to a different | |
4673 | LMA. */ | |
aecc8f8a | 4674 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea NC |
4675 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
4676 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ | |
4677 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ | |
4678 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) | |
bc67d8a6 NC |
4679 | |
4680 | /* Initialise the segment mark field. */ | |
4681 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 4682 | section->segment_mark = FALSE; |
bc67d8a6 | 4683 | |
252b5132 | 4684 | /* Scan through the segments specified in the program header |
bc67d8a6 | 4685 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 4686 | in the loadable segments. These can be created by weird |
aecc8f8a | 4687 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
4688 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
4689 | i < num_segments; | |
c044fabd | 4690 | i++, segment++) |
252b5132 | 4691 | { |
252b5132 | 4692 | unsigned int j; |
c044fabd | 4693 | Elf_Internal_Phdr *segment2; |
252b5132 | 4694 | |
aecc8f8a AM |
4695 | if (segment->p_type == PT_INTERP) |
4696 | for (section = ibfd->sections; section; section = section->next) | |
4697 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
4698 | { | |
4699 | /* Mininal change so that the normal section to segment | |
4700 | assigment code will work. */ | |
4701 | segment->p_vaddr = section->vma; | |
4702 | break; | |
4703 | } | |
4704 | ||
bc67d8a6 NC |
4705 | if (segment->p_type != PT_LOAD) |
4706 | continue; | |
c044fabd | 4707 | |
bc67d8a6 | 4708 | /* Determine if this segment overlaps any previous segments. */ |
c044fabd | 4709 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++) |
bc67d8a6 NC |
4710 | { |
4711 | bfd_signed_vma extra_length; | |
c044fabd | 4712 | |
bc67d8a6 NC |
4713 | if (segment2->p_type != PT_LOAD |
4714 | || ! SEGMENT_OVERLAPS (segment, segment2)) | |
4715 | continue; | |
c044fabd | 4716 | |
bc67d8a6 NC |
4717 | /* Merge the two segments together. */ |
4718 | if (segment2->p_vaddr < segment->p_vaddr) | |
4719 | { | |
c044fabd KH |
4720 | /* Extend SEGMENT2 to include SEGMENT and then delete |
4721 | SEGMENT. */ | |
bc67d8a6 NC |
4722 | extra_length = |
4723 | SEGMENT_END (segment, segment->p_vaddr) | |
4724 | - SEGMENT_END (segment2, segment2->p_vaddr); | |
c044fabd | 4725 | |
bc67d8a6 NC |
4726 | if (extra_length > 0) |
4727 | { | |
4728 | segment2->p_memsz += extra_length; | |
4729 | segment2->p_filesz += extra_length; | |
4730 | } | |
c044fabd | 4731 | |
bc67d8a6 | 4732 | segment->p_type = PT_NULL; |
c044fabd | 4733 | |
bc67d8a6 NC |
4734 | /* Since we have deleted P we must restart the outer loop. */ |
4735 | i = 0; | |
4736 | segment = elf_tdata (ibfd)->phdr; | |
4737 | break; | |
4738 | } | |
4739 | else | |
4740 | { | |
c044fabd KH |
4741 | /* Extend SEGMENT to include SEGMENT2 and then delete |
4742 | SEGMENT2. */ | |
bc67d8a6 NC |
4743 | extra_length = |
4744 | SEGMENT_END (segment2, segment2->p_vaddr) | |
4745 | - SEGMENT_END (segment, segment->p_vaddr); | |
c044fabd | 4746 | |
bc67d8a6 NC |
4747 | if (extra_length > 0) |
4748 | { | |
4749 | segment->p_memsz += extra_length; | |
4750 | segment->p_filesz += extra_length; | |
4751 | } | |
c044fabd | 4752 | |
bc67d8a6 NC |
4753 | segment2->p_type = PT_NULL; |
4754 | } | |
4755 | } | |
4756 | } | |
c044fabd | 4757 | |
bc67d8a6 NC |
4758 | /* The second scan attempts to assign sections to segments. */ |
4759 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
4760 | i < num_segments; | |
4761 | i ++, segment ++) | |
4762 | { | |
4763 | unsigned int section_count; | |
4764 | asection ** sections; | |
4765 | asection * output_section; | |
4766 | unsigned int isec; | |
4767 | bfd_vma matching_lma; | |
4768 | bfd_vma suggested_lma; | |
4769 | unsigned int j; | |
dc810e39 | 4770 | bfd_size_type amt; |
bc67d8a6 NC |
4771 | |
4772 | if (segment->p_type == PT_NULL) | |
4773 | continue; | |
c044fabd | 4774 | |
bc67d8a6 | 4775 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
4776 | for (section = ibfd->sections, section_count = 0; |
4777 | section != NULL; | |
4778 | section = section->next) | |
caf47ea6 | 4779 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c044fabd | 4780 | ++section_count; |
b5f852ea NC |
4781 | |
4782 | /* Allocate a segment map big enough to contain | |
4783 | all of the sections we have selected. */ | |
dc810e39 AM |
4784 | amt = sizeof (struct elf_segment_map); |
4785 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
4786 | map = (struct elf_segment_map *) bfd_alloc (obfd, amt); | |
bc67d8a6 | 4787 | if (map == NULL) |
b34976b6 | 4788 | return FALSE; |
252b5132 RH |
4789 | |
4790 | /* Initialise the fields of the segment map. Default to | |
4791 | using the physical address of the segment in the input BFD. */ | |
bc67d8a6 NC |
4792 | map->next = NULL; |
4793 | map->p_type = segment->p_type; | |
4794 | map->p_flags = segment->p_flags; | |
4795 | map->p_flags_valid = 1; | |
4796 | map->p_paddr = segment->p_paddr; | |
4797 | map->p_paddr_valid = 1; | |
252b5132 RH |
4798 | |
4799 | /* Determine if this segment contains the ELF file header | |
4800 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
4801 | map->includes_filehdr = (segment->p_offset == 0 |
4802 | && segment->p_filesz >= iehdr->e_ehsize); | |
252b5132 | 4803 | |
bc67d8a6 | 4804 | map->includes_phdrs = 0; |
252b5132 | 4805 | |
bc67d8a6 | 4806 | if (! phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 4807 | { |
bc67d8a6 NC |
4808 | map->includes_phdrs = |
4809 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
4810 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
4811 | >= ((bfd_vma) iehdr->e_phoff |
4812 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 4813 | |
bc67d8a6 | 4814 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 4815 | phdr_included = TRUE; |
252b5132 RH |
4816 | } |
4817 | ||
bc67d8a6 | 4818 | if (section_count == 0) |
252b5132 RH |
4819 | { |
4820 | /* Special segments, such as the PT_PHDR segment, may contain | |
4821 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 NC |
4822 | something. They are allowed by the ELF spec however, so only |
4823 | a warning is produced. */ | |
bc67d8a6 | 4824 | if (segment->p_type == PT_LOAD) |
caf47ea6 | 4825 | (*_bfd_error_handler) |
1ed89aa9 | 4826 | (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"), |
caf47ea6 | 4827 | bfd_archive_filename (ibfd)); |
252b5132 | 4828 | |
bc67d8a6 | 4829 | map->count = 0; |
c044fabd KH |
4830 | *pointer_to_map = map; |
4831 | pointer_to_map = &map->next; | |
252b5132 RH |
4832 | |
4833 | continue; | |
4834 | } | |
4835 | ||
4836 | /* Now scan the sections in the input BFD again and attempt | |
4837 | to add their corresponding output sections to the segment map. | |
4838 | The problem here is how to handle an output section which has | |
4839 | been moved (ie had its LMA changed). There are four possibilities: | |
4840 | ||
4841 | 1. None of the sections have been moved. | |
4842 | In this case we can continue to use the segment LMA from the | |
4843 | input BFD. | |
4844 | ||
4845 | 2. All of the sections have been moved by the same amount. | |
4846 | In this case we can change the segment's LMA to match the LMA | |
4847 | of the first section. | |
4848 | ||
4849 | 3. Some of the sections have been moved, others have not. | |
4850 | In this case those sections which have not been moved can be | |
4851 | placed in the current segment which will have to have its size, | |
4852 | and possibly its LMA changed, and a new segment or segments will | |
4853 | have to be created to contain the other sections. | |
4854 | ||
b5f852ea | 4855 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
4856 | In this case we can change the segment's LMA to match the LMA |
4857 | of the first section and we will have to create a new segment | |
4858 | or segments to contain the other sections. | |
4859 | ||
4860 | In order to save time, we allocate an array to hold the section | |
4861 | pointers that we are interested in. As these sections get assigned | |
4862 | to a segment, they are removed from this array. */ | |
4863 | ||
0b14c2aa L |
4864 | /* Gcc 2.96 miscompiles this code on mips. Don't do casting here |
4865 | to work around this long long bug. */ | |
4866 | amt = section_count * sizeof (asection *); | |
dc810e39 | 4867 | sections = (asection **) bfd_malloc (amt); |
252b5132 | 4868 | if (sections == NULL) |
b34976b6 | 4869 | return FALSE; |
252b5132 RH |
4870 | |
4871 | /* Step One: Scan for segment vs section LMA conflicts. | |
4872 | Also add the sections to the section array allocated above. | |
4873 | Also add the sections to the current segment. In the common | |
4874 | case, where the sections have not been moved, this means that | |
4875 | we have completely filled the segment, and there is nothing | |
4876 | more to do. */ | |
252b5132 | 4877 | isec = 0; |
72730e0c | 4878 | matching_lma = 0; |
252b5132 RH |
4879 | suggested_lma = 0; |
4880 | ||
bc67d8a6 NC |
4881 | for (j = 0, section = ibfd->sections; |
4882 | section != NULL; | |
4883 | section = section->next) | |
252b5132 | 4884 | { |
caf47ea6 | 4885 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 4886 | { |
bc67d8a6 NC |
4887 | output_section = section->output_section; |
4888 | ||
4889 | sections[j ++] = section; | |
252b5132 RH |
4890 | |
4891 | /* The Solaris native linker always sets p_paddr to 0. | |
4892 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
4893 | correct value. Note - some backends require that |
4894 | p_paddr be left as zero. */ | |
bc67d8a6 | 4895 | if (segment->p_paddr == 0 |
4455705d | 4896 | && segment->p_vaddr != 0 |
5e8d7549 | 4897 | && (! bed->want_p_paddr_set_to_zero) |
252b5132 | 4898 | && isec == 0 |
bc67d8a6 NC |
4899 | && output_section->lma != 0 |
4900 | && (output_section->vma == (segment->p_vaddr | |
4901 | + (map->includes_filehdr | |
4902 | ? iehdr->e_ehsize | |
4903 | : 0) | |
4904 | + (map->includes_phdrs | |
079e9a2f AM |
4905 | ? (iehdr->e_phnum |
4906 | * iehdr->e_phentsize) | |
bc67d8a6 NC |
4907 | : 0)))) |
4908 | map->p_paddr = segment->p_vaddr; | |
252b5132 RH |
4909 | |
4910 | /* Match up the physical address of the segment with the | |
4911 | LMA address of the output section. */ | |
bc67d8a6 | 4912 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 NC |
4913 | || IS_COREFILE_NOTE (segment, section) |
4914 | || (bed->want_p_paddr_set_to_zero && | |
4915 | IS_CONTAINED_BY_VMA (output_section, segment)) | |
4916 | ) | |
252b5132 RH |
4917 | { |
4918 | if (matching_lma == 0) | |
bc67d8a6 | 4919 | matching_lma = output_section->lma; |
252b5132 RH |
4920 | |
4921 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 4922 | then it does not overlap any other section within that |
252b5132 | 4923 | segment. */ |
bc67d8a6 | 4924 | map->sections[isec ++] = output_section; |
252b5132 RH |
4925 | } |
4926 | else if (suggested_lma == 0) | |
bc67d8a6 | 4927 | suggested_lma = output_section->lma; |
252b5132 RH |
4928 | } |
4929 | } | |
4930 | ||
bc67d8a6 | 4931 | BFD_ASSERT (j == section_count); |
252b5132 RH |
4932 | |
4933 | /* Step Two: Adjust the physical address of the current segment, | |
4934 | if necessary. */ | |
bc67d8a6 | 4935 | if (isec == section_count) |
252b5132 RH |
4936 | { |
4937 | /* All of the sections fitted within the segment as currently | |
4938 | specified. This is the default case. Add the segment to | |
4939 | the list of built segments and carry on to process the next | |
4940 | program header in the input BFD. */ | |
bc67d8a6 | 4941 | map->count = section_count; |
c044fabd KH |
4942 | *pointer_to_map = map; |
4943 | pointer_to_map = &map->next; | |
252b5132 RH |
4944 | |
4945 | free (sections); | |
4946 | continue; | |
4947 | } | |
252b5132 RH |
4948 | else |
4949 | { | |
72730e0c AM |
4950 | if (matching_lma != 0) |
4951 | { | |
4952 | /* At least one section fits inside the current segment. | |
4953 | Keep it, but modify its physical address to match the | |
4954 | LMA of the first section that fitted. */ | |
bc67d8a6 | 4955 | map->p_paddr = matching_lma; |
72730e0c AM |
4956 | } |
4957 | else | |
4958 | { | |
4959 | /* None of the sections fitted inside the current segment. | |
4960 | Change the current segment's physical address to match | |
4961 | the LMA of the first section. */ | |
bc67d8a6 | 4962 | map->p_paddr = suggested_lma; |
72730e0c AM |
4963 | } |
4964 | ||
bc67d8a6 NC |
4965 | /* Offset the segment physical address from the lma |
4966 | to allow for space taken up by elf headers. */ | |
4967 | if (map->includes_filehdr) | |
4968 | map->p_paddr -= iehdr->e_ehsize; | |
252b5132 | 4969 | |
bc67d8a6 NC |
4970 | if (map->includes_phdrs) |
4971 | { | |
4972 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
4973 | ||
4974 | /* iehdr->e_phnum is just an estimate of the number | |
4975 | of program headers that we will need. Make a note | |
4976 | here of the number we used and the segment we chose | |
4977 | to hold these headers, so that we can adjust the | |
4978 | offset when we know the correct value. */ | |
4979 | phdr_adjust_num = iehdr->e_phnum; | |
4980 | phdr_adjust_seg = map; | |
4981 | } | |
252b5132 RH |
4982 | } |
4983 | ||
4984 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 4985 | those that fit to the current segment and removing them from the |
252b5132 RH |
4986 | sections array; but making sure not to leave large gaps. Once all |
4987 | possible sections have been assigned to the current segment it is | |
4988 | added to the list of built segments and if sections still remain | |
4989 | to be assigned, a new segment is constructed before repeating | |
4990 | the loop. */ | |
4991 | isec = 0; | |
4992 | do | |
4993 | { | |
bc67d8a6 | 4994 | map->count = 0; |
252b5132 RH |
4995 | suggested_lma = 0; |
4996 | ||
4997 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 4998 | for (j = 0; j < section_count; j++) |
252b5132 | 4999 | { |
bc67d8a6 | 5000 | section = sections[j]; |
252b5132 | 5001 | |
bc67d8a6 | 5002 | if (section == NULL) |
252b5132 RH |
5003 | continue; |
5004 | ||
bc67d8a6 | 5005 | output_section = section->output_section; |
252b5132 | 5006 | |
bc67d8a6 | 5007 | BFD_ASSERT (output_section != NULL); |
c044fabd | 5008 | |
bc67d8a6 NC |
5009 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5010 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 5011 | { |
bc67d8a6 | 5012 | if (map->count == 0) |
252b5132 RH |
5013 | { |
5014 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
5015 | the beginning of the segment, then something is |
5016 | wrong. */ | |
5017 | if (output_section->lma != | |
5018 | (map->p_paddr | |
5019 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
5020 | + (map->includes_phdrs | |
5021 | ? iehdr->e_phnum * iehdr->e_phentsize | |
5022 | : 0))) | |
252b5132 RH |
5023 | abort (); |
5024 | } | |
5025 | else | |
5026 | { | |
5027 | asection * prev_sec; | |
252b5132 | 5028 | |
bc67d8a6 | 5029 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
5030 | |
5031 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
5032 | and the start of this section is more than |
5033 | maxpagesize then we need to start a new segment. */ | |
079e9a2f AM |
5034 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, |
5035 | maxpagesize) | |
caf47ea6 | 5036 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
079e9a2f AM |
5037 | || ((prev_sec->lma + prev_sec->_raw_size) |
5038 | > output_section->lma)) | |
252b5132 RH |
5039 | { |
5040 | if (suggested_lma == 0) | |
bc67d8a6 | 5041 | suggested_lma = output_section->lma; |
252b5132 RH |
5042 | |
5043 | continue; | |
5044 | } | |
5045 | } | |
5046 | ||
bc67d8a6 | 5047 | map->sections[map->count++] = output_section; |
252b5132 RH |
5048 | ++isec; |
5049 | sections[j] = NULL; | |
b34976b6 | 5050 | section->segment_mark = TRUE; |
252b5132 RH |
5051 | } |
5052 | else if (suggested_lma == 0) | |
bc67d8a6 | 5053 | suggested_lma = output_section->lma; |
252b5132 RH |
5054 | } |
5055 | ||
bc67d8a6 | 5056 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
5057 | |
5058 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
5059 | *pointer_to_map = map; |
5060 | pointer_to_map = &map->next; | |
252b5132 | 5061 | |
bc67d8a6 | 5062 | if (isec < section_count) |
252b5132 RH |
5063 | { |
5064 | /* We still have not allocated all of the sections to | |
5065 | segments. Create a new segment here, initialise it | |
5066 | and carry on looping. */ | |
dc810e39 AM |
5067 | amt = sizeof (struct elf_segment_map); |
5068 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5069 | map = (struct elf_segment_map *) bfd_alloc (obfd, amt); | |
bc67d8a6 | 5070 | if (map == NULL) |
5ed6aba4 NC |
5071 | { |
5072 | free (sections); | |
5073 | return FALSE; | |
5074 | } | |
252b5132 RH |
5075 | |
5076 | /* Initialise the fields of the segment map. Set the physical | |
5077 | physical address to the LMA of the first section that has | |
5078 | not yet been assigned. */ | |
bc67d8a6 NC |
5079 | map->next = NULL; |
5080 | map->p_type = segment->p_type; | |
5081 | map->p_flags = segment->p_flags; | |
5082 | map->p_flags_valid = 1; | |
5083 | map->p_paddr = suggested_lma; | |
5084 | map->p_paddr_valid = 1; | |
5085 | map->includes_filehdr = 0; | |
5086 | map->includes_phdrs = 0; | |
252b5132 RH |
5087 | } |
5088 | } | |
bc67d8a6 | 5089 | while (isec < section_count); |
252b5132 RH |
5090 | |
5091 | free (sections); | |
5092 | } | |
5093 | ||
5094 | /* The Solaris linker creates program headers in which all the | |
5095 | p_paddr fields are zero. When we try to objcopy or strip such a | |
5096 | file, we get confused. Check for this case, and if we find it | |
5097 | reset the p_paddr_valid fields. */ | |
bc67d8a6 NC |
5098 | for (map = map_first; map != NULL; map = map->next) |
5099 | if (map->p_paddr != 0) | |
252b5132 | 5100 | break; |
bc67d8a6 | 5101 | if (map == NULL) |
b5f852ea NC |
5102 | for (map = map_first; map != NULL; map = map->next) |
5103 | map->p_paddr_valid = 0; | |
252b5132 | 5104 | |
bc67d8a6 NC |
5105 | elf_tdata (obfd)->segment_map = map_first; |
5106 | ||
5107 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 5108 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
5109 | the offset if necessary. */ |
5110 | if (phdr_adjust_seg != NULL) | |
5111 | { | |
5112 | unsigned int count; | |
c044fabd | 5113 | |
bc67d8a6 | 5114 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 5115 | count++; |
252b5132 | 5116 | |
bc67d8a6 NC |
5117 | if (count > phdr_adjust_num) |
5118 | phdr_adjust_seg->p_paddr | |
5119 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
5120 | } | |
c044fabd | 5121 | |
252b5132 | 5122 | #if 0 |
c044fabd KH |
5123 | /* Final Step: Sort the segments into ascending order of physical |
5124 | address. */ | |
bc67d8a6 | 5125 | if (map_first != NULL) |
252b5132 | 5126 | { |
c044fabd | 5127 | struct elf_segment_map *prev; |
252b5132 | 5128 | |
bc67d8a6 NC |
5129 | prev = map_first; |
5130 | for (map = map_first->next; map != NULL; prev = map, map = map->next) | |
252b5132 | 5131 | { |
bc67d8a6 NC |
5132 | /* Yes I know - its a bubble sort.... */ |
5133 | if (map->next != NULL && (map->next->p_paddr < map->p_paddr)) | |
252b5132 | 5134 | { |
bc67d8a6 NC |
5135 | /* Swap map and map->next. */ |
5136 | prev->next = map->next; | |
5137 | map->next = map->next->next; | |
5138 | prev->next->next = map; | |
252b5132 | 5139 | |
bc67d8a6 NC |
5140 | /* Restart loop. */ |
5141 | map = map_first; | |
252b5132 RH |
5142 | } |
5143 | } | |
5144 | } | |
5145 | #endif | |
5146 | ||
bc67d8a6 | 5147 | #undef SEGMENT_END |
eecdbe52 | 5148 | #undef SECTION_SIZE |
bc67d8a6 NC |
5149 | #undef IS_CONTAINED_BY_VMA |
5150 | #undef IS_CONTAINED_BY_LMA | |
252b5132 | 5151 | #undef IS_COREFILE_NOTE |
bc67d8a6 NC |
5152 | #undef IS_SOLARIS_PT_INTERP |
5153 | #undef INCLUDE_SECTION_IN_SEGMENT | |
5154 | #undef SEGMENT_AFTER_SEGMENT | |
5155 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 5156 | return TRUE; |
252b5132 RH |
5157 | } |
5158 | ||
5159 | /* Copy private section information. This copies over the entsize | |
5160 | field, and sometimes the info field. */ | |
5161 | ||
b34976b6 | 5162 | bfd_boolean |
252b5132 RH |
5163 | _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) |
5164 | bfd *ibfd; | |
5165 | asection *isec; | |
5166 | bfd *obfd; | |
5167 | asection *osec; | |
5168 | { | |
5169 | Elf_Internal_Shdr *ihdr, *ohdr; | |
5170 | ||
5171 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
5172 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 5173 | return TRUE; |
252b5132 | 5174 | |
ad12c1c5 | 5175 | if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) |
caf47ea6 AM |
5176 | { |
5177 | asection *s; | |
5178 | ||
5179 | /* Only set up the segments if there are no more SEC_ALLOC | |
5180 | sections. FIXME: This won't do the right thing if objcopy is | |
5181 | used to remove the last SEC_ALLOC section, since objcopy | |
5182 | won't call this routine in that case. */ | |
5183 | for (s = isec->next; s != NULL; s = s->next) | |
5184 | if ((s->flags & SEC_ALLOC) != 0) | |
5185 | break; | |
5186 | if (s == NULL) | |
5187 | { | |
5188 | if (! copy_private_bfd_data (ibfd, obfd)) | |
b34976b6 | 5189 | return FALSE; |
caf47ea6 | 5190 | } |
252b5132 RH |
5191 | } |
5192 | ||
5193 | ihdr = &elf_section_data (isec)->this_hdr; | |
5194 | ohdr = &elf_section_data (osec)->this_hdr; | |
5195 | ||
5196 | ohdr->sh_entsize = ihdr->sh_entsize; | |
5197 | ||
5198 | if (ihdr->sh_type == SHT_SYMTAB | |
5199 | || ihdr->sh_type == SHT_DYNSYM | |
5200 | || ihdr->sh_type == SHT_GNU_verneed | |
5201 | || ihdr->sh_type == SHT_GNU_verdef) | |
5202 | ohdr->sh_info = ihdr->sh_info; | |
5203 | ||
9dce4196 AM |
5204 | /* Set things up for objcopy. The output SHT_GROUP section will |
5205 | have its elf_next_in_group pointing back to the input group | |
5206 | members. */ | |
5207 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
5208 | elf_group_name (osec) = elf_group_name (isec); | |
5209 | ||
68bfbfcc | 5210 | osec->use_rela_p = isec->use_rela_p; |
bf572ba0 | 5211 | |
b34976b6 | 5212 | return TRUE; |
252b5132 RH |
5213 | } |
5214 | ||
5215 | /* Copy private symbol information. If this symbol is in a section | |
5216 | which we did not map into a BFD section, try to map the section | |
5217 | index correctly. We use special macro definitions for the mapped | |
5218 | section indices; these definitions are interpreted by the | |
5219 | swap_out_syms function. */ | |
5220 | ||
9ad5cbcf AM |
5221 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
5222 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
5223 | #define MAP_STRTAB (SHN_HIOS + 3) | |
5224 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
5225 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 5226 | |
b34976b6 | 5227 | bfd_boolean |
252b5132 RH |
5228 | _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg) |
5229 | bfd *ibfd; | |
5230 | asymbol *isymarg; | |
5231 | bfd *obfd; | |
5232 | asymbol *osymarg; | |
5233 | { | |
5234 | elf_symbol_type *isym, *osym; | |
5235 | ||
5236 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
5237 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 5238 | return TRUE; |
252b5132 RH |
5239 | |
5240 | isym = elf_symbol_from (ibfd, isymarg); | |
5241 | osym = elf_symbol_from (obfd, osymarg); | |
5242 | ||
5243 | if (isym != NULL | |
5244 | && osym != NULL | |
5245 | && bfd_is_abs_section (isym->symbol.section)) | |
5246 | { | |
5247 | unsigned int shndx; | |
5248 | ||
5249 | shndx = isym->internal_elf_sym.st_shndx; | |
5250 | if (shndx == elf_onesymtab (ibfd)) | |
5251 | shndx = MAP_ONESYMTAB; | |
5252 | else if (shndx == elf_dynsymtab (ibfd)) | |
5253 | shndx = MAP_DYNSYMTAB; | |
5254 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
5255 | shndx = MAP_STRTAB; | |
5256 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
5257 | shndx = MAP_SHSTRTAB; | |
9ad5cbcf AM |
5258 | else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) |
5259 | shndx = MAP_SYM_SHNDX; | |
252b5132 RH |
5260 | osym->internal_elf_sym.st_shndx = shndx; |
5261 | } | |
5262 | ||
b34976b6 | 5263 | return TRUE; |
252b5132 RH |
5264 | } |
5265 | ||
5266 | /* Swap out the symbols. */ | |
5267 | ||
b34976b6 | 5268 | static bfd_boolean |
252b5132 RH |
5269 | swap_out_syms (abfd, sttp, relocatable_p) |
5270 | bfd *abfd; | |
5271 | struct bfd_strtab_hash **sttp; | |
5272 | int relocatable_p; | |
5273 | { | |
079e9a2f AM |
5274 | struct elf_backend_data *bed; |
5275 | int symcount; | |
5276 | asymbol **syms; | |
5277 | struct bfd_strtab_hash *stt; | |
5278 | Elf_Internal_Shdr *symtab_hdr; | |
9ad5cbcf | 5279 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f AM |
5280 | Elf_Internal_Shdr *symstrtab_hdr; |
5281 | char *outbound_syms; | |
9ad5cbcf | 5282 | char *outbound_shndx; |
079e9a2f AM |
5283 | int idx; |
5284 | bfd_size_type amt; | |
252b5132 RH |
5285 | |
5286 | if (!elf_map_symbols (abfd)) | |
b34976b6 | 5287 | return FALSE; |
252b5132 | 5288 | |
c044fabd | 5289 | /* Dump out the symtabs. */ |
079e9a2f AM |
5290 | stt = _bfd_elf_stringtab_init (); |
5291 | if (stt == NULL) | |
b34976b6 | 5292 | return FALSE; |
252b5132 | 5293 | |
079e9a2f AM |
5294 | bed = get_elf_backend_data (abfd); |
5295 | symcount = bfd_get_symcount (abfd); | |
5296 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
5297 | symtab_hdr->sh_type = SHT_SYMTAB; | |
5298 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
5299 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
5300 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
5301 | symtab_hdr->sh_addralign = bed->s->file_align; | |
5302 | ||
5303 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
5304 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
5305 | ||
5306 | amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym; | |
5307 | outbound_syms = bfd_alloc (abfd, amt); | |
5308 | if (outbound_syms == NULL) | |
5ed6aba4 NC |
5309 | { |
5310 | _bfd_stringtab_free (stt); | |
5311 | return FALSE; | |
5312 | } | |
079e9a2f | 5313 | symtab_hdr->contents = (PTR) outbound_syms; |
252b5132 | 5314 | |
9ad5cbcf AM |
5315 | outbound_shndx = NULL; |
5316 | symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
5317 | if (symtab_shndx_hdr->sh_name != 0) | |
5318 | { | |
5319 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
1126897b | 5320 | outbound_shndx = bfd_zalloc (abfd, amt); |
9ad5cbcf | 5321 | if (outbound_shndx == NULL) |
5ed6aba4 NC |
5322 | { |
5323 | _bfd_stringtab_free (stt); | |
5324 | return FALSE; | |
5325 | } | |
5326 | ||
9ad5cbcf AM |
5327 | symtab_shndx_hdr->contents = outbound_shndx; |
5328 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
5329 | symtab_shndx_hdr->sh_size = amt; | |
5330 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
5331 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
5332 | } | |
5333 | ||
589e6347 | 5334 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
5335 | { |
5336 | /* Fill in zeroth symbol and swap it out. */ | |
5337 | Elf_Internal_Sym sym; | |
5338 | sym.st_name = 0; | |
5339 | sym.st_value = 0; | |
5340 | sym.st_size = 0; | |
5341 | sym.st_info = 0; | |
5342 | sym.st_other = 0; | |
5343 | sym.st_shndx = SHN_UNDEF; | |
9ad5cbcf | 5344 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 5345 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
5346 | if (outbound_shndx != NULL) |
5347 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 5348 | } |
252b5132 | 5349 | |
079e9a2f AM |
5350 | syms = bfd_get_outsymbols (abfd); |
5351 | for (idx = 0; idx < symcount; idx++) | |
252b5132 | 5352 | { |
252b5132 | 5353 | Elf_Internal_Sym sym; |
079e9a2f AM |
5354 | bfd_vma value = syms[idx]->value; |
5355 | elf_symbol_type *type_ptr; | |
5356 | flagword flags = syms[idx]->flags; | |
5357 | int type; | |
252b5132 | 5358 | |
079e9a2f AM |
5359 | if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) |
5360 | { | |
5361 | /* Local section symbols have no name. */ | |
5362 | sym.st_name = 0; | |
5363 | } | |
5364 | else | |
5365 | { | |
5366 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
5367 | syms[idx]->name, | |
b34976b6 | 5368 | TRUE, FALSE); |
079e9a2f | 5369 | if (sym.st_name == (unsigned long) -1) |
5ed6aba4 NC |
5370 | { |
5371 | _bfd_stringtab_free (stt); | |
5372 | return FALSE; | |
5373 | } | |
079e9a2f | 5374 | } |
252b5132 | 5375 | |
079e9a2f | 5376 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 5377 | |
079e9a2f AM |
5378 | if ((flags & BSF_SECTION_SYM) == 0 |
5379 | && bfd_is_com_section (syms[idx]->section)) | |
5380 | { | |
5381 | /* ELF common symbols put the alignment into the `value' field, | |
5382 | and the size into the `size' field. This is backwards from | |
5383 | how BFD handles it, so reverse it here. */ | |
5384 | sym.st_size = value; | |
5385 | if (type_ptr == NULL | |
5386 | || type_ptr->internal_elf_sym.st_value == 0) | |
5387 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
5388 | else | |
5389 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
5390 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
5391 | (abfd, syms[idx]->section); | |
5392 | } | |
5393 | else | |
5394 | { | |
5395 | asection *sec = syms[idx]->section; | |
5396 | int shndx; | |
252b5132 | 5397 | |
079e9a2f AM |
5398 | if (sec->output_section) |
5399 | { | |
5400 | value += sec->output_offset; | |
5401 | sec = sec->output_section; | |
5402 | } | |
589e6347 | 5403 | |
079e9a2f AM |
5404 | /* Don't add in the section vma for relocatable output. */ |
5405 | if (! relocatable_p) | |
5406 | value += sec->vma; | |
5407 | sym.st_value = value; | |
5408 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
5409 | ||
5410 | if (bfd_is_abs_section (sec) | |
5411 | && type_ptr != NULL | |
5412 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
5413 | { | |
5414 | /* This symbol is in a real ELF section which we did | |
5415 | not create as a BFD section. Undo the mapping done | |
5416 | by copy_private_symbol_data. */ | |
5417 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
5418 | switch (shndx) | |
5419 | { | |
5420 | case MAP_ONESYMTAB: | |
5421 | shndx = elf_onesymtab (abfd); | |
5422 | break; | |
5423 | case MAP_DYNSYMTAB: | |
5424 | shndx = elf_dynsymtab (abfd); | |
5425 | break; | |
5426 | case MAP_STRTAB: | |
5427 | shndx = elf_tdata (abfd)->strtab_section; | |
5428 | break; | |
5429 | case MAP_SHSTRTAB: | |
5430 | shndx = elf_tdata (abfd)->shstrtab_section; | |
5431 | break; | |
9ad5cbcf AM |
5432 | case MAP_SYM_SHNDX: |
5433 | shndx = elf_tdata (abfd)->symtab_shndx_section; | |
5434 | break; | |
079e9a2f AM |
5435 | default: |
5436 | break; | |
5437 | } | |
5438 | } | |
5439 | else | |
5440 | { | |
5441 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 5442 | |
079e9a2f AM |
5443 | if (shndx == -1) |
5444 | { | |
5445 | asection *sec2; | |
5446 | ||
5447 | /* Writing this would be a hell of a lot easier if | |
5448 | we had some decent documentation on bfd, and | |
5449 | knew what to expect of the library, and what to | |
5450 | demand of applications. For example, it | |
5451 | appears that `objcopy' might not set the | |
5452 | section of a symbol to be a section that is | |
5453 | actually in the output file. */ | |
5454 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
589e6347 NC |
5455 | if (sec2 == NULL) |
5456 | { | |
5457 | _bfd_error_handler (_("\ | |
5458 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
5459 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
5460 | sec->name); | |
5461 | bfd_set_error (bfd_error_invalid_operation); | |
5ed6aba4 | 5462 | _bfd_stringtab_free (stt); |
589e6347 NC |
5463 | return FALSE; |
5464 | } | |
5465 | ||
079e9a2f AM |
5466 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); |
5467 | BFD_ASSERT (shndx != -1); | |
5468 | } | |
5469 | } | |
252b5132 | 5470 | |
079e9a2f AM |
5471 | sym.st_shndx = shndx; |
5472 | } | |
252b5132 | 5473 | |
13ae64f3 JJ |
5474 | if ((flags & BSF_THREAD_LOCAL) != 0) |
5475 | type = STT_TLS; | |
5476 | else if ((flags & BSF_FUNCTION) != 0) | |
079e9a2f AM |
5477 | type = STT_FUNC; |
5478 | else if ((flags & BSF_OBJECT) != 0) | |
5479 | type = STT_OBJECT; | |
5480 | else | |
5481 | type = STT_NOTYPE; | |
252b5132 | 5482 | |
13ae64f3 JJ |
5483 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
5484 | type = STT_TLS; | |
5485 | ||
589e6347 | 5486 | /* Processor-specific types. */ |
079e9a2f AM |
5487 | if (type_ptr != NULL |
5488 | && bed->elf_backend_get_symbol_type) | |
5489 | type = ((*bed->elf_backend_get_symbol_type) | |
5490 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 5491 | |
079e9a2f AM |
5492 | if (flags & BSF_SECTION_SYM) |
5493 | { | |
5494 | if (flags & BSF_GLOBAL) | |
5495 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
5496 | else | |
5497 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
5498 | } | |
5499 | else if (bfd_is_com_section (syms[idx]->section)) | |
5500 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
5501 | else if (bfd_is_und_section (syms[idx]->section)) | |
5502 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
5503 | ? STB_WEAK | |
5504 | : STB_GLOBAL), | |
5505 | type); | |
5506 | else if (flags & BSF_FILE) | |
5507 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
5508 | else | |
5509 | { | |
5510 | int bind = STB_LOCAL; | |
252b5132 | 5511 | |
079e9a2f AM |
5512 | if (flags & BSF_LOCAL) |
5513 | bind = STB_LOCAL; | |
5514 | else if (flags & BSF_WEAK) | |
5515 | bind = STB_WEAK; | |
5516 | else if (flags & BSF_GLOBAL) | |
5517 | bind = STB_GLOBAL; | |
252b5132 | 5518 | |
079e9a2f AM |
5519 | sym.st_info = ELF_ST_INFO (bind, type); |
5520 | } | |
252b5132 | 5521 | |
079e9a2f AM |
5522 | if (type_ptr != NULL) |
5523 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
5524 | else | |
5525 | sym.st_other = 0; | |
252b5132 | 5526 | |
9ad5cbcf | 5527 | bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); |
079e9a2f | 5528 | outbound_syms += bed->s->sizeof_sym; |
9ad5cbcf AM |
5529 | if (outbound_shndx != NULL) |
5530 | outbound_shndx += sizeof (Elf_External_Sym_Shndx); | |
079e9a2f | 5531 | } |
252b5132 | 5532 | |
079e9a2f AM |
5533 | *sttp = stt; |
5534 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
5535 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
252b5132 | 5536 | |
079e9a2f AM |
5537 | symstrtab_hdr->sh_flags = 0; |
5538 | symstrtab_hdr->sh_addr = 0; | |
5539 | symstrtab_hdr->sh_entsize = 0; | |
5540 | symstrtab_hdr->sh_link = 0; | |
5541 | symstrtab_hdr->sh_info = 0; | |
5542 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 5543 | |
b34976b6 | 5544 | return TRUE; |
252b5132 RH |
5545 | } |
5546 | ||
5547 | /* Return the number of bytes required to hold the symtab vector. | |
5548 | ||
5549 | Note that we base it on the count plus 1, since we will null terminate | |
5550 | the vector allocated based on this size. However, the ELF symbol table | |
5551 | always has a dummy entry as symbol #0, so it ends up even. */ | |
5552 | ||
5553 | long | |
5554 | _bfd_elf_get_symtab_upper_bound (abfd) | |
5555 | bfd *abfd; | |
5556 | { | |
5557 | long symcount; | |
5558 | long symtab_size; | |
5559 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
5560 | ||
5561 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
5562 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
5563 | if (symcount > 0) | |
5564 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
5565 | |
5566 | return symtab_size; | |
5567 | } | |
5568 | ||
5569 | long | |
5570 | _bfd_elf_get_dynamic_symtab_upper_bound (abfd) | |
5571 | bfd *abfd; | |
5572 | { | |
5573 | long symcount; | |
5574 | long symtab_size; | |
5575 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
5576 | ||
5577 | if (elf_dynsymtab (abfd) == 0) | |
5578 | { | |
5579 | bfd_set_error (bfd_error_invalid_operation); | |
5580 | return -1; | |
5581 | } | |
5582 | ||
5583 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
5584 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
5585 | if (symcount > 0) | |
5586 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
5587 | |
5588 | return symtab_size; | |
5589 | } | |
5590 | ||
5591 | long | |
5592 | _bfd_elf_get_reloc_upper_bound (abfd, asect) | |
7442e600 | 5593 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
5594 | sec_ptr asect; |
5595 | { | |
5596 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
5597 | } | |
5598 | ||
5599 | /* Canonicalize the relocs. */ | |
5600 | ||
5601 | long | |
5602 | _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
5603 | bfd *abfd; | |
5604 | sec_ptr section; | |
5605 | arelent **relptr; | |
5606 | asymbol **symbols; | |
5607 | { | |
5608 | arelent *tblptr; | |
5609 | unsigned int i; | |
dbb410c3 | 5610 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 5611 | |
b34976b6 | 5612 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
5613 | return -1; |
5614 | ||
5615 | tblptr = section->relocation; | |
5616 | for (i = 0; i < section->reloc_count; i++) | |
5617 | *relptr++ = tblptr++; | |
5618 | ||
5619 | *relptr = NULL; | |
5620 | ||
5621 | return section->reloc_count; | |
5622 | } | |
5623 | ||
5624 | long | |
5625 | _bfd_elf_get_symtab (abfd, alocation) | |
5626 | bfd *abfd; | |
5627 | asymbol **alocation; | |
5628 | { | |
dbb410c3 | 5629 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
b34976b6 | 5630 | long symcount = bed->s->slurp_symbol_table (abfd, alocation, FALSE); |
252b5132 RH |
5631 | |
5632 | if (symcount >= 0) | |
5633 | bfd_get_symcount (abfd) = symcount; | |
5634 | return symcount; | |
5635 | } | |
5636 | ||
5637 | long | |
5638 | _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation) | |
5639 | bfd *abfd; | |
5640 | asymbol **alocation; | |
5641 | { | |
dbb410c3 | 5642 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
b34976b6 | 5643 | long symcount = bed->s->slurp_symbol_table (abfd, alocation, TRUE); |
1f70368c DJ |
5644 | |
5645 | if (symcount >= 0) | |
5646 | bfd_get_dynamic_symcount (abfd) = symcount; | |
5647 | return symcount; | |
252b5132 RH |
5648 | } |
5649 | ||
5650 | /* Return the size required for the dynamic reloc entries. Any | |
5651 | section that was actually installed in the BFD, and has type | |
5652 | SHT_REL or SHT_RELA, and uses the dynamic symbol table, is | |
5653 | considered to be a dynamic reloc section. */ | |
5654 | ||
5655 | long | |
5656 | _bfd_elf_get_dynamic_reloc_upper_bound (abfd) | |
5657 | bfd *abfd; | |
5658 | { | |
5659 | long ret; | |
5660 | asection *s; | |
5661 | ||
5662 | if (elf_dynsymtab (abfd) == 0) | |
5663 | { | |
5664 | bfd_set_error (bfd_error_invalid_operation); | |
5665 | return -1; | |
5666 | } | |
5667 | ||
5668 | ret = sizeof (arelent *); | |
5669 | for (s = abfd->sections; s != NULL; s = s->next) | |
5670 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
5671 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
5672 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
5673 | ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize) | |
5674 | * sizeof (arelent *)); | |
5675 | ||
5676 | return ret; | |
5677 | } | |
5678 | ||
5679 | /* Canonicalize the dynamic relocation entries. Note that we return | |
5680 | the dynamic relocations as a single block, although they are | |
5681 | actually associated with particular sections; the interface, which | |
5682 | was designed for SunOS style shared libraries, expects that there | |
5683 | is only one set of dynamic relocs. Any section that was actually | |
5684 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses | |
5685 | the dynamic symbol table, is considered to be a dynamic reloc | |
5686 | section. */ | |
5687 | ||
5688 | long | |
5689 | _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms) | |
5690 | bfd *abfd; | |
5691 | arelent **storage; | |
5692 | asymbol **syms; | |
5693 | { | |
b34976b6 AM |
5694 | bfd_boolean (*slurp_relocs) |
5695 | PARAMS ((bfd *, asection *, asymbol **, bfd_boolean)); | |
252b5132 RH |
5696 | asection *s; |
5697 | long ret; | |
5698 | ||
5699 | if (elf_dynsymtab (abfd) == 0) | |
5700 | { | |
5701 | bfd_set_error (bfd_error_invalid_operation); | |
5702 | return -1; | |
5703 | } | |
5704 | ||
5705 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
5706 | ret = 0; | |
5707 | for (s = abfd->sections; s != NULL; s = s->next) | |
5708 | { | |
5709 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
5710 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
5711 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
5712 | { | |
5713 | arelent *p; | |
5714 | long count, i; | |
5715 | ||
b34976b6 | 5716 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 RH |
5717 | return -1; |
5718 | count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize; | |
5719 | p = s->relocation; | |
5720 | for (i = 0; i < count; i++) | |
5721 | *storage++ = p++; | |
5722 | ret += count; | |
5723 | } | |
5724 | } | |
5725 | ||
5726 | *storage = NULL; | |
5727 | ||
5728 | return ret; | |
5729 | } | |
5730 | \f | |
5731 | /* Read in the version information. */ | |
5732 | ||
b34976b6 | 5733 | bfd_boolean |
252b5132 RH |
5734 | _bfd_elf_slurp_version_tables (abfd) |
5735 | bfd *abfd; | |
5736 | { | |
5737 | bfd_byte *contents = NULL; | |
dc810e39 | 5738 | bfd_size_type amt; |
252b5132 RH |
5739 | |
5740 | if (elf_dynverdef (abfd) != 0) | |
5741 | { | |
5742 | Elf_Internal_Shdr *hdr; | |
5743 | Elf_External_Verdef *everdef; | |
5744 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
5745 | Elf_Internal_Verdef *iverdefarr; |
5746 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 5747 | unsigned int i; |
062e2358 | 5748 | unsigned int maxidx; |
252b5132 RH |
5749 | |
5750 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
5751 | ||
252b5132 RH |
5752 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
5753 | if (contents == NULL) | |
5754 | goto error_return; | |
5755 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
dc810e39 | 5756 | || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size) |
252b5132 RH |
5757 | goto error_return; |
5758 | ||
f631889e UD |
5759 | /* We know the number of entries in the section but not the maximum |
5760 | index. Therefore we have to run through all entries and find | |
5761 | the maximum. */ | |
252b5132 | 5762 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
5763 | maxidx = 0; |
5764 | for (i = 0; i < hdr->sh_info; ++i) | |
5765 | { | |
5766 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
5767 | ||
062e2358 AM |
5768 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
5769 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e UD |
5770 | |
5771 | everdef = ((Elf_External_Verdef *) | |
5772 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
5773 | } | |
5774 | ||
dc810e39 AM |
5775 | amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef); |
5776 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); | |
f631889e UD |
5777 | if (elf_tdata (abfd)->verdef == NULL) |
5778 | goto error_return; | |
5779 | ||
5780 | elf_tdata (abfd)->cverdefs = maxidx; | |
5781 | ||
5782 | everdef = (Elf_External_Verdef *) contents; | |
5783 | iverdefarr = elf_tdata (abfd)->verdef; | |
5784 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
5785 | { |
5786 | Elf_External_Verdaux *everdaux; | |
5787 | Elf_Internal_Verdaux *iverdaux; | |
5788 | unsigned int j; | |
5789 | ||
f631889e UD |
5790 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
5791 | ||
5792 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; | |
5793 | memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); | |
252b5132 RH |
5794 | |
5795 | iverdef->vd_bfd = abfd; | |
5796 | ||
dc810e39 AM |
5797 | amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux); |
5798 | iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt); | |
252b5132 RH |
5799 | if (iverdef->vd_auxptr == NULL) |
5800 | goto error_return; | |
5801 | ||
5802 | everdaux = ((Elf_External_Verdaux *) | |
5803 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
5804 | iverdaux = iverdef->vd_auxptr; | |
5805 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
5806 | { | |
5807 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
5808 | ||
5809 | iverdaux->vda_nodename = | |
5810 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
5811 | iverdaux->vda_name); | |
5812 | if (iverdaux->vda_nodename == NULL) | |
5813 | goto error_return; | |
5814 | ||
5815 | if (j + 1 < iverdef->vd_cnt) | |
5816 | iverdaux->vda_nextptr = iverdaux + 1; | |
5817 | else | |
5818 | iverdaux->vda_nextptr = NULL; | |
5819 | ||
5820 | everdaux = ((Elf_External_Verdaux *) | |
5821 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
5822 | } | |
5823 | ||
5824 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
5825 | ||
5826 | if (i + 1 < hdr->sh_info) | |
5827 | iverdef->vd_nextdef = iverdef + 1; | |
5828 | else | |
5829 | iverdef->vd_nextdef = NULL; | |
5830 | ||
5831 | everdef = ((Elf_External_Verdef *) | |
5832 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
5833 | } | |
5834 | ||
5835 | free (contents); | |
5836 | contents = NULL; | |
5837 | } | |
5838 | ||
5839 | if (elf_dynverref (abfd) != 0) | |
5840 | { | |
5841 | Elf_Internal_Shdr *hdr; | |
5842 | Elf_External_Verneed *everneed; | |
5843 | Elf_Internal_Verneed *iverneed; | |
5844 | unsigned int i; | |
5845 | ||
5846 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
5847 | ||
dc810e39 | 5848 | amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed); |
252b5132 | 5849 | elf_tdata (abfd)->verref = |
dc810e39 | 5850 | (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt); |
252b5132 RH |
5851 | if (elf_tdata (abfd)->verref == NULL) |
5852 | goto error_return; | |
5853 | ||
5854 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
5855 | ||
5856 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
5857 | if (contents == NULL) | |
5858 | goto error_return; | |
5859 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
dc810e39 | 5860 | || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size) |
252b5132 RH |
5861 | goto error_return; |
5862 | ||
5863 | everneed = (Elf_External_Verneed *) contents; | |
5864 | iverneed = elf_tdata (abfd)->verref; | |
5865 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
5866 | { | |
5867 | Elf_External_Vernaux *evernaux; | |
5868 | Elf_Internal_Vernaux *ivernaux; | |
5869 | unsigned int j; | |
5870 | ||
5871 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
5872 | ||
5873 | iverneed->vn_bfd = abfd; | |
5874 | ||
5875 | iverneed->vn_filename = | |
5876 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
5877 | iverneed->vn_file); | |
5878 | if (iverneed->vn_filename == NULL) | |
5879 | goto error_return; | |
5880 | ||
dc810e39 AM |
5881 | amt = iverneed->vn_cnt; |
5882 | amt *= sizeof (Elf_Internal_Vernaux); | |
5883 | iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt); | |
252b5132 RH |
5884 | |
5885 | evernaux = ((Elf_External_Vernaux *) | |
5886 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
5887 | ivernaux = iverneed->vn_auxptr; | |
5888 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
5889 | { | |
5890 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
5891 | ||
5892 | ivernaux->vna_nodename = | |
5893 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
5894 | ivernaux->vna_name); | |
5895 | if (ivernaux->vna_nodename == NULL) | |
5896 | goto error_return; | |
5897 | ||
5898 | if (j + 1 < iverneed->vn_cnt) | |
5899 | ivernaux->vna_nextptr = ivernaux + 1; | |
5900 | else | |
5901 | ivernaux->vna_nextptr = NULL; | |
5902 | ||
5903 | evernaux = ((Elf_External_Vernaux *) | |
5904 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
5905 | } | |
5906 | ||
5907 | if (i + 1 < hdr->sh_info) | |
5908 | iverneed->vn_nextref = iverneed + 1; | |
5909 | else | |
5910 | iverneed->vn_nextref = NULL; | |
5911 | ||
5912 | everneed = ((Elf_External_Verneed *) | |
5913 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
5914 | } | |
5915 | ||
5916 | free (contents); | |
5917 | contents = NULL; | |
5918 | } | |
5919 | ||
b34976b6 | 5920 | return TRUE; |
252b5132 RH |
5921 | |
5922 | error_return: | |
5ed6aba4 | 5923 | if (contents != NULL) |
252b5132 | 5924 | free (contents); |
b34976b6 | 5925 | return FALSE; |
252b5132 RH |
5926 | } |
5927 | \f | |
5928 | asymbol * | |
5929 | _bfd_elf_make_empty_symbol (abfd) | |
5930 | bfd *abfd; | |
5931 | { | |
5932 | elf_symbol_type *newsym; | |
dc810e39 | 5933 | bfd_size_type amt = sizeof (elf_symbol_type); |
252b5132 | 5934 | |
dc810e39 | 5935 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt); |
252b5132 RH |
5936 | if (!newsym) |
5937 | return NULL; | |
5938 | else | |
5939 | { | |
5940 | newsym->symbol.the_bfd = abfd; | |
5941 | return &newsym->symbol; | |
5942 | } | |
5943 | } | |
5944 | ||
5945 | void | |
5946 | _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret) | |
7442e600 | 5947 | bfd *ignore_abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
5948 | asymbol *symbol; |
5949 | symbol_info *ret; | |
5950 | { | |
5951 | bfd_symbol_info (symbol, ret); | |
5952 | } | |
5953 | ||
5954 | /* Return whether a symbol name implies a local symbol. Most targets | |
5955 | use this function for the is_local_label_name entry point, but some | |
5956 | override it. */ | |
5957 | ||
b34976b6 | 5958 | bfd_boolean |
252b5132 | 5959 | _bfd_elf_is_local_label_name (abfd, name) |
7442e600 | 5960 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
5961 | const char *name; |
5962 | { | |
5963 | /* Normal local symbols start with ``.L''. */ | |
5964 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 5965 | return TRUE; |
252b5132 RH |
5966 | |
5967 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
5968 | DWARF debugging symbols starting with ``..''. */ | |
5969 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 5970 | return TRUE; |
252b5132 RH |
5971 | |
5972 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
5973 | emitting DWARF debugging output. I suspect this is actually a | |
5974 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
5975 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
5976 | underscore to be emitted on some ELF targets). For ease of use, | |
5977 | we treat such symbols as local. */ | |
5978 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 5979 | return TRUE; |
252b5132 | 5980 | |
b34976b6 | 5981 | return FALSE; |
252b5132 RH |
5982 | } |
5983 | ||
5984 | alent * | |
5985 | _bfd_elf_get_lineno (ignore_abfd, symbol) | |
7442e600 ILT |
5986 | bfd *ignore_abfd ATTRIBUTE_UNUSED; |
5987 | asymbol *symbol ATTRIBUTE_UNUSED; | |
252b5132 RH |
5988 | { |
5989 | abort (); | |
5990 | return NULL; | |
5991 | } | |
5992 | ||
b34976b6 | 5993 | bfd_boolean |
252b5132 RH |
5994 | _bfd_elf_set_arch_mach (abfd, arch, machine) |
5995 | bfd *abfd; | |
5996 | enum bfd_architecture arch; | |
5997 | unsigned long machine; | |
5998 | { | |
5999 | /* If this isn't the right architecture for this backend, and this | |
6000 | isn't the generic backend, fail. */ | |
6001 | if (arch != get_elf_backend_data (abfd)->arch | |
6002 | && arch != bfd_arch_unknown | |
6003 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 6004 | return FALSE; |
252b5132 RH |
6005 | |
6006 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
6007 | } | |
6008 | ||
d1fad7c6 NC |
6009 | /* Find the function to a particular section and offset, |
6010 | for error reporting. */ | |
252b5132 | 6011 | |
b34976b6 | 6012 | static bfd_boolean |
d1fad7c6 | 6013 | elf_find_function (abfd, section, symbols, offset, |
4e8a9624 | 6014 | filename_ptr, functionname_ptr) |
d1fad7c6 | 6015 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
6016 | asection *section; |
6017 | asymbol **symbols; | |
6018 | bfd_vma offset; | |
4e8a9624 AM |
6019 | const char **filename_ptr; |
6020 | const char **functionname_ptr; | |
252b5132 | 6021 | { |
252b5132 RH |
6022 | const char *filename; |
6023 | asymbol *func; | |
6024 | bfd_vma low_func; | |
6025 | asymbol **p; | |
6026 | ||
252b5132 RH |
6027 | filename = NULL; |
6028 | func = NULL; | |
6029 | low_func = 0; | |
6030 | ||
6031 | for (p = symbols; *p != NULL; p++) | |
6032 | { | |
6033 | elf_symbol_type *q; | |
6034 | ||
6035 | q = (elf_symbol_type *) *p; | |
6036 | ||
6037 | if (bfd_get_section (&q->symbol) != section) | |
6038 | continue; | |
6039 | ||
6040 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) | |
6041 | { | |
6042 | default: | |
6043 | break; | |
6044 | case STT_FILE: | |
6045 | filename = bfd_asymbol_name (&q->symbol); | |
6046 | break; | |
6047 | case STT_NOTYPE: | |
6048 | case STT_FUNC: | |
6049 | if (q->symbol.section == section | |
6050 | && q->symbol.value >= low_func | |
6051 | && q->symbol.value <= offset) | |
6052 | { | |
6053 | func = (asymbol *) q; | |
6054 | low_func = q->symbol.value; | |
6055 | } | |
6056 | break; | |
6057 | } | |
6058 | } | |
6059 | ||
6060 | if (func == NULL) | |
b34976b6 | 6061 | return FALSE; |
252b5132 | 6062 | |
d1fad7c6 NC |
6063 | if (filename_ptr) |
6064 | *filename_ptr = filename; | |
6065 | if (functionname_ptr) | |
6066 | *functionname_ptr = bfd_asymbol_name (func); | |
6067 | ||
b34976b6 | 6068 | return TRUE; |
d1fad7c6 NC |
6069 | } |
6070 | ||
6071 | /* Find the nearest line to a particular section and offset, | |
6072 | for error reporting. */ | |
6073 | ||
b34976b6 | 6074 | bfd_boolean |
d1fad7c6 | 6075 | _bfd_elf_find_nearest_line (abfd, section, symbols, offset, |
4e8a9624 | 6076 | filename_ptr, functionname_ptr, line_ptr) |
d1fad7c6 NC |
6077 | bfd *abfd; |
6078 | asection *section; | |
6079 | asymbol **symbols; | |
6080 | bfd_vma offset; | |
4e8a9624 AM |
6081 | const char **filename_ptr; |
6082 | const char **functionname_ptr; | |
d1fad7c6 NC |
6083 | unsigned int *line_ptr; |
6084 | { | |
b34976b6 | 6085 | bfd_boolean found; |
d1fad7c6 NC |
6086 | |
6087 | if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
6088 | filename_ptr, functionname_ptr, |
6089 | line_ptr)) | |
d1fad7c6 NC |
6090 | { |
6091 | if (!*functionname_ptr) | |
4e8a9624 AM |
6092 | elf_find_function (abfd, section, symbols, offset, |
6093 | *filename_ptr ? NULL : filename_ptr, | |
6094 | functionname_ptr); | |
6095 | ||
b34976b6 | 6096 | return TRUE; |
d1fad7c6 NC |
6097 | } |
6098 | ||
6099 | if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
4e8a9624 AM |
6100 | filename_ptr, functionname_ptr, |
6101 | line_ptr, 0, | |
6102 | &elf_tdata (abfd)->dwarf2_find_line_info)) | |
d1fad7c6 NC |
6103 | { |
6104 | if (!*functionname_ptr) | |
4e8a9624 AM |
6105 | elf_find_function (abfd, section, symbols, offset, |
6106 | *filename_ptr ? NULL : filename_ptr, | |
6107 | functionname_ptr); | |
6108 | ||
b34976b6 | 6109 | return TRUE; |
d1fad7c6 NC |
6110 | } |
6111 | ||
6112 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
6113 | &found, filename_ptr, |
6114 | functionname_ptr, line_ptr, | |
6115 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 6116 | return FALSE; |
dc43ada5 | 6117 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 6118 | return TRUE; |
d1fad7c6 NC |
6119 | |
6120 | if (symbols == NULL) | |
b34976b6 | 6121 | return FALSE; |
d1fad7c6 NC |
6122 | |
6123 | if (! elf_find_function (abfd, section, symbols, offset, | |
4e8a9624 | 6124 | filename_ptr, functionname_ptr)) |
b34976b6 | 6125 | return FALSE; |
d1fad7c6 | 6126 | |
252b5132 | 6127 | *line_ptr = 0; |
b34976b6 | 6128 | return TRUE; |
252b5132 RH |
6129 | } |
6130 | ||
6131 | int | |
6132 | _bfd_elf_sizeof_headers (abfd, reloc) | |
6133 | bfd *abfd; | |
b34976b6 | 6134 | bfd_boolean reloc; |
252b5132 RH |
6135 | { |
6136 | int ret; | |
6137 | ||
6138 | ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; | |
6139 | if (! reloc) | |
6140 | ret += get_program_header_size (abfd); | |
6141 | return ret; | |
6142 | } | |
6143 | ||
b34976b6 | 6144 | bfd_boolean |
252b5132 RH |
6145 | _bfd_elf_set_section_contents (abfd, section, location, offset, count) |
6146 | bfd *abfd; | |
6147 | sec_ptr section; | |
6148 | PTR location; | |
6149 | file_ptr offset; | |
6150 | bfd_size_type count; | |
6151 | { | |
6152 | Elf_Internal_Shdr *hdr; | |
dc810e39 | 6153 | bfd_signed_vma pos; |
252b5132 RH |
6154 | |
6155 | if (! abfd->output_has_begun | |
82e51918 AM |
6156 | && ! (_bfd_elf_compute_section_file_positions |
6157 | (abfd, (struct bfd_link_info *) NULL))) | |
b34976b6 | 6158 | return FALSE; |
252b5132 RH |
6159 | |
6160 | hdr = &elf_section_data (section)->this_hdr; | |
dc810e39 AM |
6161 | pos = hdr->sh_offset + offset; |
6162 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
6163 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 6164 | return FALSE; |
252b5132 | 6165 | |
b34976b6 | 6166 | return TRUE; |
252b5132 RH |
6167 | } |
6168 | ||
6169 | void | |
6170 | _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst) | |
7442e600 ILT |
6171 | bfd *abfd ATTRIBUTE_UNUSED; |
6172 | arelent *cache_ptr ATTRIBUTE_UNUSED; | |
6173 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED; | |
252b5132 RH |
6174 | { |
6175 | abort (); | |
6176 | } | |
6177 | ||
252b5132 RH |
6178 | /* Try to convert a non-ELF reloc into an ELF one. */ |
6179 | ||
b34976b6 | 6180 | bfd_boolean |
252b5132 RH |
6181 | _bfd_elf_validate_reloc (abfd, areloc) |
6182 | bfd *abfd; | |
6183 | arelent *areloc; | |
6184 | { | |
c044fabd | 6185 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
6186 | |
6187 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
6188 | { | |
6189 | bfd_reloc_code_real_type code; | |
6190 | reloc_howto_type *howto; | |
6191 | ||
6192 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 6193 | equivalent ELF reloc. */ |
252b5132 RH |
6194 | |
6195 | if (areloc->howto->pc_relative) | |
6196 | { | |
6197 | switch (areloc->howto->bitsize) | |
6198 | { | |
6199 | case 8: | |
6200 | code = BFD_RELOC_8_PCREL; | |
6201 | break; | |
6202 | case 12: | |
6203 | code = BFD_RELOC_12_PCREL; | |
6204 | break; | |
6205 | case 16: | |
6206 | code = BFD_RELOC_16_PCREL; | |
6207 | break; | |
6208 | case 24: | |
6209 | code = BFD_RELOC_24_PCREL; | |
6210 | break; | |
6211 | case 32: | |
6212 | code = BFD_RELOC_32_PCREL; | |
6213 | break; | |
6214 | case 64: | |
6215 | code = BFD_RELOC_64_PCREL; | |
6216 | break; | |
6217 | default: | |
6218 | goto fail; | |
6219 | } | |
6220 | ||
6221 | howto = bfd_reloc_type_lookup (abfd, code); | |
6222 | ||
6223 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
6224 | { | |
6225 | if (howto->pcrel_offset) | |
6226 | areloc->addend += areloc->address; | |
6227 | else | |
6228 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
6229 | } | |
6230 | } | |
6231 | else | |
6232 | { | |
6233 | switch (areloc->howto->bitsize) | |
6234 | { | |
6235 | case 8: | |
6236 | code = BFD_RELOC_8; | |
6237 | break; | |
6238 | case 14: | |
6239 | code = BFD_RELOC_14; | |
6240 | break; | |
6241 | case 16: | |
6242 | code = BFD_RELOC_16; | |
6243 | break; | |
6244 | case 26: | |
6245 | code = BFD_RELOC_26; | |
6246 | break; | |
6247 | case 32: | |
6248 | code = BFD_RELOC_32; | |
6249 | break; | |
6250 | case 64: | |
6251 | code = BFD_RELOC_64; | |
6252 | break; | |
6253 | default: | |
6254 | goto fail; | |
6255 | } | |
6256 | ||
6257 | howto = bfd_reloc_type_lookup (abfd, code); | |
6258 | } | |
6259 | ||
6260 | if (howto) | |
6261 | areloc->howto = howto; | |
6262 | else | |
6263 | goto fail; | |
6264 | } | |
6265 | ||
b34976b6 | 6266 | return TRUE; |
252b5132 RH |
6267 | |
6268 | fail: | |
6269 | (*_bfd_error_handler) | |
6270 | (_("%s: unsupported relocation type %s"), | |
8f615d07 | 6271 | bfd_archive_filename (abfd), areloc->howto->name); |
252b5132 | 6272 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 6273 | return FALSE; |
252b5132 RH |
6274 | } |
6275 | ||
b34976b6 | 6276 | bfd_boolean |
252b5132 RH |
6277 | _bfd_elf_close_and_cleanup (abfd) |
6278 | bfd *abfd; | |
6279 | { | |
6280 | if (bfd_get_format (abfd) == bfd_object) | |
6281 | { | |
6282 | if (elf_shstrtab (abfd) != NULL) | |
2b0f7ef9 | 6283 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
252b5132 RH |
6284 | } |
6285 | ||
6286 | return _bfd_generic_close_and_cleanup (abfd); | |
6287 | } | |
6288 | ||
6289 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
6290 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
6291 | range-checking to interfere. There is nothing else to do in processing | |
6292 | this reloc. */ | |
6293 | ||
6294 | bfd_reloc_status_type | |
6295 | _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg) | |
7442e600 ILT |
6296 | bfd *abfd ATTRIBUTE_UNUSED; |
6297 | arelent *re ATTRIBUTE_UNUSED; | |
6298 | struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED; | |
6299 | PTR data ATTRIBUTE_UNUSED; | |
6300 | asection *is ATTRIBUTE_UNUSED; | |
6301 | bfd *obfd ATTRIBUTE_UNUSED; | |
6302 | char **errmsg ATTRIBUTE_UNUSED; | |
252b5132 RH |
6303 | { |
6304 | return bfd_reloc_ok; | |
6305 | } | |
252b5132 RH |
6306 | \f |
6307 | /* Elf core file support. Much of this only works on native | |
6308 | toolchains, since we rely on knowing the | |
6309 | machine-dependent procfs structure in order to pick | |
c044fabd | 6310 | out details about the corefile. */ |
252b5132 RH |
6311 | |
6312 | #ifdef HAVE_SYS_PROCFS_H | |
6313 | # include <sys/procfs.h> | |
6314 | #endif | |
6315 | ||
c044fabd | 6316 | /* FIXME: this is kinda wrong, but it's what gdb wants. */ |
252b5132 RH |
6317 | |
6318 | static int | |
6319 | elfcore_make_pid (abfd) | |
c044fabd | 6320 | bfd *abfd; |
252b5132 RH |
6321 | { |
6322 | return ((elf_tdata (abfd)->core_lwpid << 16) | |
6323 | + (elf_tdata (abfd)->core_pid)); | |
6324 | } | |
6325 | ||
252b5132 RH |
6326 | /* If there isn't a section called NAME, make one, using |
6327 | data from SECT. Note, this function will generate a | |
6328 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 6329 | overwrite it. */ |
252b5132 | 6330 | |
b34976b6 | 6331 | static bfd_boolean |
252b5132 | 6332 | elfcore_maybe_make_sect (abfd, name, sect) |
c044fabd KH |
6333 | bfd *abfd; |
6334 | char *name; | |
6335 | asection *sect; | |
252b5132 | 6336 | { |
c044fabd | 6337 | asection *sect2; |
252b5132 RH |
6338 | |
6339 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 6340 | return TRUE; |
252b5132 RH |
6341 | |
6342 | sect2 = bfd_make_section (abfd, name); | |
6343 | if (sect2 == NULL) | |
b34976b6 | 6344 | return FALSE; |
252b5132 RH |
6345 | |
6346 | sect2->_raw_size = sect->_raw_size; | |
6347 | sect2->filepos = sect->filepos; | |
6348 | sect2->flags = sect->flags; | |
6349 | sect2->alignment_power = sect->alignment_power; | |
b34976b6 | 6350 | return TRUE; |
252b5132 RH |
6351 | } |
6352 | ||
bb0082d6 AM |
6353 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
6354 | actually creates up to two pseudosections: | |
6355 | - For the single-threaded case, a section named NAME, unless | |
6356 | such a section already exists. | |
6357 | - For the multi-threaded case, a section named "NAME/PID", where | |
6358 | PID is elfcore_make_pid (abfd). | |
6359 | Both pseudosections have identical contents. */ | |
b34976b6 | 6360 | bfd_boolean |
bb0082d6 AM |
6361 | _bfd_elfcore_make_pseudosection (abfd, name, size, filepos) |
6362 | bfd *abfd; | |
6363 | char *name; | |
dc810e39 AM |
6364 | size_t size; |
6365 | ufile_ptr filepos; | |
bb0082d6 AM |
6366 | { |
6367 | char buf[100]; | |
6368 | char *threaded_name; | |
d4c88bbb | 6369 | size_t len; |
bb0082d6 AM |
6370 | asection *sect; |
6371 | ||
6372 | /* Build the section name. */ | |
6373 | ||
6374 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb AM |
6375 | len = strlen (buf) + 1; |
6376 | threaded_name = bfd_alloc (abfd, (bfd_size_type) len); | |
bb0082d6 | 6377 | if (threaded_name == NULL) |
b34976b6 | 6378 | return FALSE; |
d4c88bbb | 6379 | memcpy (threaded_name, buf, len); |
bb0082d6 AM |
6380 | |
6381 | sect = bfd_make_section (abfd, threaded_name); | |
6382 | if (sect == NULL) | |
b34976b6 | 6383 | return FALSE; |
bb0082d6 AM |
6384 | sect->_raw_size = size; |
6385 | sect->filepos = filepos; | |
6386 | sect->flags = SEC_HAS_CONTENTS; | |
6387 | sect->alignment_power = 2; | |
6388 | ||
936e320b | 6389 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
6390 | } |
6391 | ||
252b5132 | 6392 | /* prstatus_t exists on: |
4a938328 | 6393 | solaris 2.5+ |
252b5132 RH |
6394 | linux 2.[01] + glibc |
6395 | unixware 4.2 | |
6396 | */ | |
6397 | ||
6398 | #if defined (HAVE_PRSTATUS_T) | |
b34976b6 AM |
6399 | static bfd_boolean elfcore_grok_prstatus |
6400 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
a7b97311 | 6401 | |
b34976b6 | 6402 | static bfd_boolean |
252b5132 | 6403 | elfcore_grok_prstatus (abfd, note) |
c044fabd KH |
6404 | bfd *abfd; |
6405 | Elf_Internal_Note *note; | |
252b5132 | 6406 | { |
dc810e39 | 6407 | size_t raw_size; |
7ee38065 | 6408 | int offset; |
252b5132 | 6409 | |
4a938328 MS |
6410 | if (note->descsz == sizeof (prstatus_t)) |
6411 | { | |
6412 | prstatus_t prstat; | |
252b5132 | 6413 | |
e0ebfc61 | 6414 | raw_size = sizeof (prstat.pr_reg); |
7ee38065 | 6415 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 6416 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 6417 | |
fa49d224 NC |
6418 | /* Do not overwrite the core signal if it |
6419 | has already been set by another thread. */ | |
6420 | if (elf_tdata (abfd)->core_signal == 0) | |
6421 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4a938328 | 6422 | elf_tdata (abfd)->core_pid = prstat.pr_pid; |
252b5132 | 6423 | |
4a938328 MS |
6424 | /* pr_who exists on: |
6425 | solaris 2.5+ | |
6426 | unixware 4.2 | |
6427 | pr_who doesn't exist on: | |
6428 | linux 2.[01] | |
6429 | */ | |
252b5132 | 6430 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
4a938328 | 6431 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
252b5132 | 6432 | #endif |
4a938328 | 6433 | } |
7ee38065 | 6434 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
6435 | else if (note->descsz == sizeof (prstatus32_t)) |
6436 | { | |
6437 | /* 64-bit host, 32-bit corefile */ | |
6438 | prstatus32_t prstat; | |
6439 | ||
e0ebfc61 | 6440 | raw_size = sizeof (prstat.pr_reg); |
7ee38065 | 6441 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
6442 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
6443 | ||
fa49d224 NC |
6444 | /* Do not overwrite the core signal if it |
6445 | has already been set by another thread. */ | |
6446 | if (elf_tdata (abfd)->core_signal == 0) | |
6447 | elf_tdata (abfd)->core_signal = prstat.pr_cursig; | |
4a938328 MS |
6448 | elf_tdata (abfd)->core_pid = prstat.pr_pid; |
6449 | ||
6450 | /* pr_who exists on: | |
6451 | solaris 2.5+ | |
6452 | unixware 4.2 | |
6453 | pr_who doesn't exist on: | |
6454 | linux 2.[01] | |
6455 | */ | |
7ee38065 | 6456 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
4a938328 MS |
6457 | elf_tdata (abfd)->core_lwpid = prstat.pr_who; |
6458 | #endif | |
6459 | } | |
7ee38065 | 6460 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
6461 | else |
6462 | { | |
6463 | /* Fail - we don't know how to handle any other | |
6464 | note size (ie. data object type). */ | |
b34976b6 | 6465 | return TRUE; |
4a938328 | 6466 | } |
252b5132 | 6467 | |
bb0082d6 | 6468 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b AM |
6469 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
6470 | raw_size, note->descpos + offset); | |
252b5132 RH |
6471 | } |
6472 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
6473 | ||
bb0082d6 | 6474 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 6475 | static bfd_boolean |
ff08c6bb | 6476 | elfcore_make_note_pseudosection (abfd, name, note) |
c044fabd | 6477 | bfd *abfd; |
ff08c6bb | 6478 | char *name; |
c044fabd | 6479 | Elf_Internal_Note *note; |
252b5132 | 6480 | { |
936e320b AM |
6481 | return _bfd_elfcore_make_pseudosection (abfd, name, |
6482 | note->descsz, note->descpos); | |
252b5132 RH |
6483 | } |
6484 | ||
ff08c6bb JB |
6485 | /* There isn't a consistent prfpregset_t across platforms, |
6486 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
6487 | data structure apart. */ |
6488 | ||
b34976b6 | 6489 | static bfd_boolean |
ff08c6bb | 6490 | elfcore_grok_prfpreg (abfd, note) |
c044fabd KH |
6491 | bfd *abfd; |
6492 | Elf_Internal_Note *note; | |
ff08c6bb JB |
6493 | { |
6494 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
6495 | } | |
6496 | ||
ff08c6bb JB |
6497 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
6498 | type of 5 (NT_PRXFPREG). Just include the whole note's contents | |
6499 | literally. */ | |
c044fabd | 6500 | |
b34976b6 | 6501 | static bfd_boolean |
ff08c6bb | 6502 | elfcore_grok_prxfpreg (abfd, note) |
c044fabd KH |
6503 | bfd *abfd; |
6504 | Elf_Internal_Note *note; | |
ff08c6bb JB |
6505 | { |
6506 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
6507 | } | |
6508 | ||
252b5132 | 6509 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 6510 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 6511 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
6512 | typedef prpsinfo32_t elfcore_psinfo32_t; |
6513 | #endif | |
252b5132 RH |
6514 | #endif |
6515 | ||
6516 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 6517 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 6518 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
6519 | typedef psinfo32_t elfcore_psinfo32_t; |
6520 | #endif | |
252b5132 RH |
6521 | #endif |
6522 | ||
252b5132 RH |
6523 | /* return a malloc'ed copy of a string at START which is at |
6524 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 6525 | the copy will always have a terminating '\0'. */ |
252b5132 | 6526 | |
936e320b | 6527 | char * |
bb0082d6 | 6528 | _bfd_elfcore_strndup (abfd, start, max) |
c044fabd KH |
6529 | bfd *abfd; |
6530 | char *start; | |
dc810e39 | 6531 | size_t max; |
252b5132 | 6532 | { |
dc810e39 | 6533 | char *dups; |
c044fabd | 6534 | char *end = memchr (start, '\0', max); |
dc810e39 | 6535 | size_t len; |
252b5132 RH |
6536 | |
6537 | if (end == NULL) | |
6538 | len = max; | |
6539 | else | |
6540 | len = end - start; | |
6541 | ||
dc810e39 AM |
6542 | dups = bfd_alloc (abfd, (bfd_size_type) len + 1); |
6543 | if (dups == NULL) | |
252b5132 RH |
6544 | return NULL; |
6545 | ||
dc810e39 AM |
6546 | memcpy (dups, start, len); |
6547 | dups[len] = '\0'; | |
252b5132 | 6548 | |
dc810e39 | 6549 | return dups; |
252b5132 RH |
6550 | } |
6551 | ||
bb0082d6 | 6552 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 AM |
6553 | static bfd_boolean elfcore_grok_psinfo |
6554 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
bb0082d6 | 6555 | |
b34976b6 | 6556 | static bfd_boolean |
252b5132 | 6557 | elfcore_grok_psinfo (abfd, note) |
c044fabd KH |
6558 | bfd *abfd; |
6559 | Elf_Internal_Note *note; | |
252b5132 | 6560 | { |
4a938328 MS |
6561 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
6562 | { | |
6563 | elfcore_psinfo_t psinfo; | |
252b5132 | 6564 | |
7ee38065 | 6565 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 6566 | |
4a938328 | 6567 | elf_tdata (abfd)->core_program |
936e320b AM |
6568 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
6569 | sizeof (psinfo.pr_fname)); | |
252b5132 | 6570 | |
4a938328 | 6571 | elf_tdata (abfd)->core_command |
936e320b AM |
6572 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
6573 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 6574 | } |
7ee38065 | 6575 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
6576 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
6577 | { | |
6578 | /* 64-bit host, 32-bit corefile */ | |
6579 | elfcore_psinfo32_t psinfo; | |
6580 | ||
7ee38065 | 6581 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 6582 | |
4a938328 | 6583 | elf_tdata (abfd)->core_program |
936e320b AM |
6584 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
6585 | sizeof (psinfo.pr_fname)); | |
4a938328 MS |
6586 | |
6587 | elf_tdata (abfd)->core_command | |
936e320b AM |
6588 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
6589 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
6590 | } |
6591 | #endif | |
6592 | ||
6593 | else | |
6594 | { | |
6595 | /* Fail - we don't know how to handle any other | |
6596 | note size (ie. data object type). */ | |
b34976b6 | 6597 | return TRUE; |
4a938328 | 6598 | } |
252b5132 RH |
6599 | |
6600 | /* Note that for some reason, a spurious space is tacked | |
6601 | onto the end of the args in some (at least one anyway) | |
c044fabd | 6602 | implementations, so strip it off if it exists. */ |
252b5132 RH |
6603 | |
6604 | { | |
c044fabd | 6605 | char *command = elf_tdata (abfd)->core_command; |
252b5132 RH |
6606 | int n = strlen (command); |
6607 | ||
6608 | if (0 < n && command[n - 1] == ' ') | |
6609 | command[n - 1] = '\0'; | |
6610 | } | |
6611 | ||
b34976b6 | 6612 | return TRUE; |
252b5132 RH |
6613 | } |
6614 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
6615 | ||
252b5132 | 6616 | #if defined (HAVE_PSTATUS_T) |
b34976b6 AM |
6617 | static bfd_boolean elfcore_grok_pstatus |
6618 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
51316059 | 6619 | |
b34976b6 | 6620 | static bfd_boolean |
252b5132 | 6621 | elfcore_grok_pstatus (abfd, note) |
c044fabd KH |
6622 | bfd *abfd; |
6623 | Elf_Internal_Note *note; | |
252b5132 | 6624 | { |
f572a39d AM |
6625 | if (note->descsz == sizeof (pstatus_t) |
6626 | #if defined (HAVE_PXSTATUS_T) | |
6627 | || note->descsz == sizeof (pxstatus_t) | |
6628 | #endif | |
6629 | ) | |
4a938328 MS |
6630 | { |
6631 | pstatus_t pstat; | |
252b5132 | 6632 | |
4a938328 | 6633 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 6634 | |
4a938328 MS |
6635 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
6636 | } | |
7ee38065 | 6637 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
6638 | else if (note->descsz == sizeof (pstatus32_t)) |
6639 | { | |
6640 | /* 64-bit host, 32-bit corefile */ | |
6641 | pstatus32_t pstat; | |
252b5132 | 6642 | |
4a938328 | 6643 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 6644 | |
4a938328 MS |
6645 | elf_tdata (abfd)->core_pid = pstat.pr_pid; |
6646 | } | |
6647 | #endif | |
252b5132 RH |
6648 | /* Could grab some more details from the "representative" |
6649 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 6650 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 6651 | |
b34976b6 | 6652 | return TRUE; |
252b5132 RH |
6653 | } |
6654 | #endif /* defined (HAVE_PSTATUS_T) */ | |
6655 | ||
252b5132 | 6656 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 AM |
6657 | static bfd_boolean elfcore_grok_lwpstatus |
6658 | PARAMS ((bfd *, Elf_Internal_Note *)); | |
51316059 | 6659 | |
b34976b6 | 6660 | static bfd_boolean |
252b5132 | 6661 | elfcore_grok_lwpstatus (abfd, note) |
c044fabd KH |
6662 | bfd *abfd; |
6663 | Elf_Internal_Note *note; | |
252b5132 RH |
6664 | { |
6665 | lwpstatus_t lwpstat; | |
6666 | char buf[100]; | |
c044fabd | 6667 | char *name; |
d4c88bbb | 6668 | size_t len; |
c044fabd | 6669 | asection *sect; |
252b5132 | 6670 | |
f572a39d AM |
6671 | if (note->descsz != sizeof (lwpstat) |
6672 | #if defined (HAVE_LWPXSTATUS_T) | |
6673 | && note->descsz != sizeof (lwpxstatus_t) | |
6674 | #endif | |
6675 | ) | |
b34976b6 | 6676 | return TRUE; |
252b5132 RH |
6677 | |
6678 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
6679 | ||
6680 | elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; | |
6681 | elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; | |
6682 | ||
c044fabd | 6683 | /* Make a ".reg/999" section. */ |
252b5132 RH |
6684 | |
6685 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb AM |
6686 | len = strlen (buf) + 1; |
6687 | name = bfd_alloc (abfd, (bfd_size_type) len); | |
252b5132 | 6688 | if (name == NULL) |
b34976b6 | 6689 | return FALSE; |
d4c88bbb | 6690 | memcpy (name, buf, len); |
252b5132 RH |
6691 | |
6692 | sect = bfd_make_section (abfd, name); | |
6693 | if (sect == NULL) | |
b34976b6 | 6694 | return FALSE; |
252b5132 RH |
6695 | |
6696 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
6697 | sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); | |
6698 | sect->filepos = note->descpos | |
6699 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
6700 | #endif | |
6701 | ||
6702 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
6703 | sect->_raw_size = sizeof (lwpstat.pr_reg); | |
6704 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); | |
6705 | #endif | |
6706 | ||
6707 | sect->flags = SEC_HAS_CONTENTS; | |
6708 | sect->alignment_power = 2; | |
6709 | ||
6710 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 6711 | return FALSE; |
252b5132 RH |
6712 | |
6713 | /* Make a ".reg2/999" section */ | |
6714 | ||
6715 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb AM |
6716 | len = strlen (buf) + 1; |
6717 | name = bfd_alloc (abfd, (bfd_size_type) len); | |
252b5132 | 6718 | if (name == NULL) |
b34976b6 | 6719 | return FALSE; |
d4c88bbb | 6720 | memcpy (name, buf, len); |
252b5132 RH |
6721 | |
6722 | sect = bfd_make_section (abfd, name); | |
6723 | if (sect == NULL) | |
b34976b6 | 6724 | return FALSE; |
252b5132 RH |
6725 | |
6726 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
6727 | sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); | |
6728 | sect->filepos = note->descpos | |
6729 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
6730 | #endif | |
6731 | ||
6732 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
6733 | sect->_raw_size = sizeof (lwpstat.pr_fpreg); | |
6734 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); | |
6735 | #endif | |
6736 | ||
6737 | sect->flags = SEC_HAS_CONTENTS; | |
6738 | sect->alignment_power = 2; | |
6739 | ||
936e320b | 6740 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
6741 | } |
6742 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
6743 | ||
16e9c715 | 6744 | #if defined (HAVE_WIN32_PSTATUS_T) |
b34976b6 | 6745 | static bfd_boolean |
16e9c715 | 6746 | elfcore_grok_win32pstatus (abfd, note) |
c044fabd KH |
6747 | bfd *abfd; |
6748 | Elf_Internal_Note *note; | |
16e9c715 NC |
6749 | { |
6750 | char buf[30]; | |
c044fabd | 6751 | char *name; |
d4c88bbb | 6752 | size_t len; |
c044fabd | 6753 | asection *sect; |
16e9c715 NC |
6754 | win32_pstatus_t pstatus; |
6755 | ||
6756 | if (note->descsz < sizeof (pstatus)) | |
b34976b6 | 6757 | return TRUE; |
16e9c715 | 6758 | |
e8eab623 | 6759 | memcpy (&pstatus, note->descdata, sizeof (pstatus)); |
c044fabd KH |
6760 | |
6761 | switch (pstatus.data_type) | |
16e9c715 NC |
6762 | { |
6763 | case NOTE_INFO_PROCESS: | |
6764 | /* FIXME: need to add ->core_command. */ | |
6765 | elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal; | |
6766 | elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid; | |
c044fabd | 6767 | break; |
16e9c715 NC |
6768 | |
6769 | case NOTE_INFO_THREAD: | |
6770 | /* Make a ".reg/999" section. */ | |
6771 | sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid); | |
c044fabd | 6772 | |
d4c88bbb AM |
6773 | len = strlen (buf) + 1; |
6774 | name = bfd_alloc (abfd, (bfd_size_type) len); | |
16e9c715 | 6775 | if (name == NULL) |
b34976b6 | 6776 | return FALSE; |
c044fabd | 6777 | |
d4c88bbb | 6778 | memcpy (name, buf, len); |
16e9c715 NC |
6779 | |
6780 | sect = bfd_make_section (abfd, name); | |
6781 | if (sect == NULL) | |
b34976b6 | 6782 | return FALSE; |
c044fabd | 6783 | |
16e9c715 | 6784 | sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context); |
079e9a2f AM |
6785 | sect->filepos = (note->descpos |
6786 | + offsetof (struct win32_pstatus, | |
6787 | data.thread_info.thread_context)); | |
16e9c715 NC |
6788 | sect->flags = SEC_HAS_CONTENTS; |
6789 | sect->alignment_power = 2; | |
6790 | ||
6791 | if (pstatus.data.thread_info.is_active_thread) | |
6792 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 6793 | return FALSE; |
16e9c715 NC |
6794 | break; |
6795 | ||
6796 | case NOTE_INFO_MODULE: | |
6797 | /* Make a ".module/xxxxxxxx" section. */ | |
c044fabd KH |
6798 | sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address); |
6799 | ||
d4c88bbb AM |
6800 | len = strlen (buf) + 1; |
6801 | name = bfd_alloc (abfd, (bfd_size_type) len); | |
16e9c715 | 6802 | if (name == NULL) |
b34976b6 | 6803 | return FALSE; |
c044fabd | 6804 | |
d4c88bbb | 6805 | memcpy (name, buf, len); |
252b5132 | 6806 | |
16e9c715 | 6807 | sect = bfd_make_section (abfd, name); |
c044fabd | 6808 | |
16e9c715 | 6809 | if (sect == NULL) |
b34976b6 | 6810 | return FALSE; |
c044fabd | 6811 | |
16e9c715 NC |
6812 | sect->_raw_size = note->descsz; |
6813 | sect->filepos = note->descpos; | |
6814 | sect->flags = SEC_HAS_CONTENTS; | |
6815 | sect->alignment_power = 2; | |
6816 | break; | |
6817 | ||
6818 | default: | |
b34976b6 | 6819 | return TRUE; |
16e9c715 NC |
6820 | } |
6821 | ||
b34976b6 | 6822 | return TRUE; |
16e9c715 NC |
6823 | } |
6824 | #endif /* HAVE_WIN32_PSTATUS_T */ | |
252b5132 | 6825 | |
b34976b6 | 6826 | static bfd_boolean |
252b5132 | 6827 | elfcore_grok_note (abfd, note) |
c044fabd KH |
6828 | bfd *abfd; |
6829 | Elf_Internal_Note *note; | |
252b5132 | 6830 | { |
bb0082d6 AM |
6831 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6832 | ||
252b5132 RH |
6833 | switch (note->type) |
6834 | { | |
6835 | default: | |
b34976b6 | 6836 | return TRUE; |
252b5132 | 6837 | |
252b5132 | 6838 | case NT_PRSTATUS: |
bb0082d6 AM |
6839 | if (bed->elf_backend_grok_prstatus) |
6840 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 6841 | return TRUE; |
bb0082d6 | 6842 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 6843 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 6844 | #else |
b34976b6 | 6845 | return TRUE; |
252b5132 RH |
6846 | #endif |
6847 | ||
6848 | #if defined (HAVE_PSTATUS_T) | |
6849 | case NT_PSTATUS: | |
6850 | return elfcore_grok_pstatus (abfd, note); | |
6851 | #endif | |
6852 | ||
6853 | #if defined (HAVE_LWPSTATUS_T) | |
6854 | case NT_LWPSTATUS: | |
6855 | return elfcore_grok_lwpstatus (abfd, note); | |
6856 | #endif | |
6857 | ||
6858 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
6859 | return elfcore_grok_prfpreg (abfd, note); | |
6860 | ||
16e9c715 | 6861 | #if defined (HAVE_WIN32_PSTATUS_T) |
c044fabd | 6862 | case NT_WIN32PSTATUS: |
16e9c715 NC |
6863 | return elfcore_grok_win32pstatus (abfd, note); |
6864 | #endif | |
6865 | ||
c044fabd | 6866 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
6867 | if (note->namesz == 6 |
6868 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
6869 | return elfcore_grok_prxfpreg (abfd, note); |
6870 | else | |
b34976b6 | 6871 | return TRUE; |
ff08c6bb | 6872 | |
252b5132 RH |
6873 | case NT_PRPSINFO: |
6874 | case NT_PSINFO: | |
bb0082d6 AM |
6875 | if (bed->elf_backend_grok_psinfo) |
6876 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 6877 | return TRUE; |
bb0082d6 | 6878 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 6879 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 6880 | #else |
b34976b6 | 6881 | return TRUE; |
252b5132 RH |
6882 | #endif |
6883 | } | |
6884 | } | |
6885 | ||
b34976b6 | 6886 | static bfd_boolean |
50b2bdb7 AM |
6887 | elfcore_netbsd_get_lwpid (note, lwpidp) |
6888 | Elf_Internal_Note *note; | |
6889 | int *lwpidp; | |
6890 | { | |
6891 | char *cp; | |
6892 | ||
6893 | cp = strchr (note->namedata, '@'); | |
6894 | if (cp != NULL) | |
6895 | { | |
d2b64500 | 6896 | *lwpidp = atoi(cp + 1); |
b34976b6 | 6897 | return TRUE; |
50b2bdb7 | 6898 | } |
b34976b6 | 6899 | return FALSE; |
50b2bdb7 AM |
6900 | } |
6901 | ||
b34976b6 | 6902 | static bfd_boolean |
50b2bdb7 AM |
6903 | elfcore_grok_netbsd_procinfo (abfd, note) |
6904 | bfd *abfd; | |
6905 | Elf_Internal_Note *note; | |
6906 | { | |
6907 | ||
6908 | /* Signal number at offset 0x08. */ | |
6909 | elf_tdata (abfd)->core_signal | |
6910 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); | |
6911 | ||
6912 | /* Process ID at offset 0x50. */ | |
6913 | elf_tdata (abfd)->core_pid | |
6914 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); | |
6915 | ||
6916 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
6917 | elf_tdata (abfd)->core_command | |
6918 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); | |
6919 | ||
b34976b6 | 6920 | return TRUE; |
50b2bdb7 AM |
6921 | } |
6922 | ||
b34976b6 | 6923 | static bfd_boolean |
50b2bdb7 AM |
6924 | elfcore_grok_netbsd_note (abfd, note) |
6925 | bfd *abfd; | |
6926 | Elf_Internal_Note *note; | |
6927 | { | |
6928 | int lwp; | |
6929 | ||
6930 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
6931 | elf_tdata (abfd)->core_lwpid = lwp; | |
6932 | ||
b4db1224 | 6933 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
6934 | { |
6935 | /* NetBSD-specific core "procinfo". Note that we expect to | |
6936 | find this note before any of the others, which is fine, | |
6937 | since the kernel writes this note out first when it | |
6938 | creates a core file. */ | |
47d9a591 | 6939 | |
50b2bdb7 AM |
6940 | return elfcore_grok_netbsd_procinfo (abfd, note); |
6941 | } | |
6942 | ||
b4db1224 JT |
6943 | /* As of Jan 2002 there are no other machine-independent notes |
6944 | defined for NetBSD core files. If the note type is less | |
6945 | than the start of the machine-dependent note types, we don't | |
6946 | understand it. */ | |
47d9a591 | 6947 | |
b4db1224 | 6948 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 6949 | return TRUE; |
50b2bdb7 AM |
6950 | |
6951 | ||
6952 | switch (bfd_get_arch (abfd)) | |
6953 | { | |
6954 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and | |
6955 | PT_GETFPREGS == mach+2. */ | |
6956 | ||
6957 | case bfd_arch_alpha: | |
6958 | case bfd_arch_sparc: | |
6959 | switch (note->type) | |
6960 | { | |
b4db1224 | 6961 | case NT_NETBSDCORE_FIRSTMACH+0: |
50b2bdb7 AM |
6962 | return elfcore_make_note_pseudosection (abfd, ".reg", note); |
6963 | ||
b4db1224 | 6964 | case NT_NETBSDCORE_FIRSTMACH+2: |
50b2bdb7 AM |
6965 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); |
6966 | ||
6967 | default: | |
b34976b6 | 6968 | return TRUE; |
50b2bdb7 AM |
6969 | } |
6970 | ||
6971 | /* On all other arch's, PT_GETREGS == mach+1 and | |
6972 | PT_GETFPREGS == mach+3. */ | |
6973 | ||
6974 | default: | |
6975 | switch (note->type) | |
6976 | { | |
b4db1224 | 6977 | case NT_NETBSDCORE_FIRSTMACH+1: |
50b2bdb7 AM |
6978 | return elfcore_make_note_pseudosection (abfd, ".reg", note); |
6979 | ||
b4db1224 | 6980 | case NT_NETBSDCORE_FIRSTMACH+3: |
50b2bdb7 AM |
6981 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); |
6982 | ||
6983 | default: | |
b34976b6 | 6984 | return TRUE; |
50b2bdb7 AM |
6985 | } |
6986 | } | |
6987 | /* NOTREACHED */ | |
6988 | } | |
6989 | ||
07c6e936 NC |
6990 | static bfd_boolean |
6991 | elfcore_grok_nto_status (abfd, note, tid) | |
6992 | bfd *abfd; | |
6993 | Elf_Internal_Note *note; | |
6994 | pid_t *tid; | |
6995 | { | |
6996 | void *ddata = note->descdata; | |
6997 | char buf[100]; | |
6998 | char *name; | |
6999 | asection *sect; | |
7000 | ||
7001 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
7002 | elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata); | |
7003 | ||
7004 | /* nto_procfs_status 'tid' field is at offset 4. */ | |
7005 | elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
7006 | ||
7007 | /* nto_procfs_status 'what' field is at offset 14. */ | |
7008 | elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, (bfd_byte *) ddata + 14); | |
7009 | ||
7010 | /* Pass tid back. */ | |
7011 | *tid = elf_tdata (abfd)->core_lwpid; | |
7012 | ||
7013 | /* Make a ".qnx_core_status/%d" section. */ | |
7014 | sprintf (buf, ".qnx_core_status/%d", *tid); | |
7015 | ||
7016 | name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1); | |
7017 | if (name == NULL) | |
7018 | return FALSE; | |
7019 | strcpy (name, buf); | |
7020 | ||
7021 | sect = bfd_make_section (abfd, name); | |
7022 | if (sect == NULL) | |
7023 | return FALSE; | |
7024 | ||
7025 | sect->_raw_size = note->descsz; | |
7026 | sect->filepos = note->descpos; | |
7027 | sect->flags = SEC_HAS_CONTENTS; | |
7028 | sect->alignment_power = 2; | |
7029 | ||
7030 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
7031 | } | |
7032 | ||
7033 | static bfd_boolean | |
7034 | elfcore_grok_nto_gregs (abfd, note, tid) | |
7035 | bfd *abfd; | |
7036 | Elf_Internal_Note *note; | |
7037 | pid_t tid; | |
7038 | { | |
7039 | char buf[100]; | |
7040 | char *name; | |
7041 | asection *sect; | |
7042 | ||
7043 | /* Make a ".reg/%d" section. */ | |
7044 | sprintf (buf, ".reg/%d", tid); | |
7045 | ||
7046 | name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1); | |
7047 | if (name == NULL) | |
7048 | return FALSE; | |
7049 | strcpy (name, buf); | |
7050 | ||
7051 | sect = bfd_make_section (abfd, name); | |
7052 | if (sect == NULL) | |
7053 | return FALSE; | |
7054 | ||
7055 | sect->_raw_size = note->descsz; | |
7056 | sect->filepos = note->descpos; | |
7057 | sect->flags = SEC_HAS_CONTENTS; | |
7058 | sect->alignment_power = 2; | |
7059 | ||
7060 | return elfcore_maybe_make_sect (abfd, ".reg", sect); | |
7061 | } | |
7062 | ||
7063 | #define BFD_QNT_CORE_INFO 7 | |
7064 | #define BFD_QNT_CORE_STATUS 8 | |
7065 | #define BFD_QNT_CORE_GREG 9 | |
7066 | #define BFD_QNT_CORE_FPREG 10 | |
7067 | ||
7068 | static bfd_boolean | |
7069 | elfcore_grok_nto_note (abfd, note) | |
7070 | bfd *abfd; | |
7071 | Elf_Internal_Note *note; | |
7072 | { | |
7073 | /* Every GREG section has a STATUS section before it. Store the | |
7074 | tid from the previous call to pass down to the next gregs | |
7075 | function. */ | |
7076 | static pid_t tid = 1; | |
7077 | ||
7078 | switch (note->type) | |
7079 | { | |
7080 | case BFD_QNT_CORE_INFO: return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
7081 | case BFD_QNT_CORE_STATUS: return elfcore_grok_nto_status (abfd, note, &tid); | |
7082 | case BFD_QNT_CORE_GREG: return elfcore_grok_nto_gregs (abfd, note, tid); | |
7083 | case BFD_QNT_CORE_FPREG: return elfcore_grok_prfpreg (abfd, note); | |
7084 | default: return TRUE; | |
7085 | } | |
7086 | } | |
7087 | ||
7c76fa91 MS |
7088 | /* Function: elfcore_write_note |
7089 | ||
47d9a591 | 7090 | Inputs: |
7c76fa91 MS |
7091 | buffer to hold note |
7092 | name of note | |
7093 | type of note | |
7094 | data for note | |
7095 | size of data for note | |
7096 | ||
7097 | Return: | |
7098 | End of buffer containing note. */ | |
7099 | ||
7100 | char * | |
7101 | elfcore_write_note (abfd, buf, bufsiz, name, type, input, size) | |
7102 | bfd *abfd; | |
7103 | char *buf; | |
7104 | int *bufsiz; | |
d4c88bbb | 7105 | const char *name; |
7c76fa91 | 7106 | int type; |
d4c88bbb | 7107 | const PTR input; |
7c76fa91 MS |
7108 | int size; |
7109 | { | |
7110 | Elf_External_Note *xnp; | |
d4c88bbb AM |
7111 | size_t namesz; |
7112 | size_t pad; | |
7113 | size_t newspace; | |
7c76fa91 MS |
7114 | char *p, *dest; |
7115 | ||
d4c88bbb AM |
7116 | namesz = 0; |
7117 | pad = 0; | |
7118 | if (name != NULL) | |
7119 | { | |
7120 | struct elf_backend_data *bed; | |
7121 | ||
7122 | namesz = strlen (name) + 1; | |
7123 | bed = get_elf_backend_data (abfd); | |
7124 | pad = -namesz & (bed->s->file_align - 1); | |
7125 | } | |
7126 | ||
7127 | newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size; | |
7128 | ||
7c76fa91 MS |
7129 | p = realloc (buf, *bufsiz + newspace); |
7130 | dest = p + *bufsiz; | |
7131 | *bufsiz += newspace; | |
7132 | xnp = (Elf_External_Note *) dest; | |
7133 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
7134 | H_PUT_32 (abfd, size, xnp->descsz); | |
7135 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
7136 | dest = xnp->name; |
7137 | if (name != NULL) | |
7138 | { | |
7139 | memcpy (dest, name, namesz); | |
7140 | dest += namesz; | |
7141 | while (pad != 0) | |
7142 | { | |
7143 | *dest++ = '\0'; | |
7144 | --pad; | |
7145 | } | |
7146 | } | |
7147 | memcpy (dest, input, size); | |
7c76fa91 MS |
7148 | return p; |
7149 | } | |
7150 | ||
7151 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) | |
7152 | char * | |
7153 | elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs) | |
7154 | bfd *abfd; | |
7155 | char *buf; | |
7156 | int *bufsiz; | |
47d9a591 | 7157 | const char *fname; |
d4c88bbb | 7158 | const char *psargs; |
7c76fa91 MS |
7159 | { |
7160 | int note_type; | |
7161 | char *note_name = "CORE"; | |
7162 | ||
7163 | #if defined (HAVE_PSINFO_T) | |
7164 | psinfo_t data; | |
7165 | note_type = NT_PSINFO; | |
7166 | #else | |
7167 | prpsinfo_t data; | |
7168 | note_type = NT_PRPSINFO; | |
7169 | #endif | |
7170 | ||
7171 | memset (&data, 0, sizeof (data)); | |
7172 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
7173 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
47d9a591 | 7174 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
7175 | note_name, note_type, &data, sizeof (data)); |
7176 | } | |
7177 | #endif /* PSINFO_T or PRPSINFO_T */ | |
7178 | ||
7179 | #if defined (HAVE_PRSTATUS_T) | |
7180 | char * | |
7181 | elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs) | |
7182 | bfd *abfd; | |
7183 | char *buf; | |
7184 | int *bufsiz; | |
b87011e9 | 7185 | long pid; |
7c76fa91 | 7186 | int cursig; |
d4c88bbb | 7187 | const PTR gregs; |
7c76fa91 MS |
7188 | { |
7189 | prstatus_t prstat; | |
7190 | char *note_name = "CORE"; | |
7191 | ||
7192 | memset (&prstat, 0, sizeof (prstat)); | |
7193 | prstat.pr_pid = pid; | |
7194 | prstat.pr_cursig = cursig; | |
c106e334 | 7195 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); |
47d9a591 | 7196 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
7197 | note_name, NT_PRSTATUS, &prstat, sizeof (prstat)); |
7198 | } | |
7199 | #endif /* HAVE_PRSTATUS_T */ | |
7200 | ||
51316059 MS |
7201 | #if defined (HAVE_LWPSTATUS_T) |
7202 | char * | |
7203 | elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs) | |
7204 | bfd *abfd; | |
7205 | char *buf; | |
7206 | int *bufsiz; | |
7207 | long pid; | |
7208 | int cursig; | |
d4c88bbb | 7209 | const PTR gregs; |
51316059 MS |
7210 | { |
7211 | lwpstatus_t lwpstat; | |
7212 | char *note_name = "CORE"; | |
7213 | ||
7214 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
7215 | lwpstat.pr_lwpid = pid >> 16; | |
7216 | lwpstat.pr_cursig = cursig; | |
7217 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
7218 | memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); | |
7219 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
7220 | #if !defined(gregs) | |
7221 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
7222 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
7223 | #else | |
7224 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
7225 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
7226 | #endif | |
7227 | #endif | |
47d9a591 | 7228 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
7229 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
7230 | } | |
7231 | #endif /* HAVE_LWPSTATUS_T */ | |
7232 | ||
7c76fa91 MS |
7233 | #if defined (HAVE_PSTATUS_T) |
7234 | char * | |
7235 | elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs) | |
7236 | bfd *abfd; | |
7237 | char *buf; | |
7238 | int *bufsiz; | |
b87011e9 | 7239 | long pid; |
7c76fa91 | 7240 | int cursig; |
d4c88bbb | 7241 | const PTR gregs; |
7c76fa91 MS |
7242 | { |
7243 | pstatus_t pstat; | |
7244 | char *note_name = "CORE"; | |
7245 | ||
51316059 MS |
7246 | memset (&pstat, 0, sizeof (pstat)); |
7247 | pstat.pr_pid = pid & 0xffff; | |
47d9a591 | 7248 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
7249 | NT_PSTATUS, &pstat, sizeof (pstat)); |
7250 | return buf; | |
7c76fa91 MS |
7251 | } |
7252 | #endif /* HAVE_PSTATUS_T */ | |
7253 | ||
7254 | char * | |
7255 | elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size) | |
7256 | bfd *abfd; | |
7257 | char *buf; | |
7258 | int *bufsiz; | |
d4c88bbb | 7259 | const PTR fpregs; |
7c76fa91 MS |
7260 | int size; |
7261 | { | |
7262 | char *note_name = "CORE"; | |
47d9a591 | 7263 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
7264 | note_name, NT_FPREGSET, fpregs, size); |
7265 | } | |
7266 | ||
7267 | char * | |
7268 | elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size) | |
7269 | bfd *abfd; | |
7270 | char *buf; | |
7271 | int *bufsiz; | |
d4c88bbb | 7272 | const PTR xfpregs; |
7c76fa91 MS |
7273 | int size; |
7274 | { | |
7275 | char *note_name = "LINUX"; | |
47d9a591 | 7276 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
7277 | note_name, NT_PRXFPREG, xfpregs, size); |
7278 | } | |
7279 | ||
b34976b6 | 7280 | static bfd_boolean |
252b5132 | 7281 | elfcore_read_notes (abfd, offset, size) |
c044fabd | 7282 | bfd *abfd; |
dc810e39 AM |
7283 | file_ptr offset; |
7284 | bfd_size_type size; | |
252b5132 | 7285 | { |
c044fabd KH |
7286 | char *buf; |
7287 | char *p; | |
252b5132 RH |
7288 | |
7289 | if (size <= 0) | |
b34976b6 | 7290 | return TRUE; |
252b5132 | 7291 | |
dc810e39 | 7292 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) |
b34976b6 | 7293 | return FALSE; |
252b5132 | 7294 | |
dc810e39 | 7295 | buf = bfd_malloc (size); |
252b5132 | 7296 | if (buf == NULL) |
b34976b6 | 7297 | return FALSE; |
252b5132 | 7298 | |
dc810e39 | 7299 | if (bfd_bread (buf, size, abfd) != size) |
252b5132 RH |
7300 | { |
7301 | error: | |
7302 | free (buf); | |
b34976b6 | 7303 | return FALSE; |
252b5132 RH |
7304 | } |
7305 | ||
7306 | p = buf; | |
7307 | while (p < buf + size) | |
7308 | { | |
c044fabd KH |
7309 | /* FIXME: bad alignment assumption. */ |
7310 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
7311 | Elf_Internal_Note in; |
7312 | ||
dc810e39 | 7313 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 7314 | |
dc810e39 | 7315 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 RH |
7316 | in.namedata = xnp->name; |
7317 | ||
dc810e39 | 7318 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
7319 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
7320 | in.descpos = offset + (in.descdata - buf); | |
7321 | ||
50b2bdb7 AM |
7322 | if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0) |
7323 | { | |
7324 | if (! elfcore_grok_netbsd_note (abfd, &in)) | |
7325 | goto error; | |
7326 | } | |
07c6e936 NC |
7327 | else if (strncmp (in.namedata, "QNX", 3) == 0) |
7328 | { | |
7329 | if (! elfcore_grok_nto_note (abfd, &in)) | |
7330 | goto error; | |
7331 | } | |
50b2bdb7 AM |
7332 | else |
7333 | { | |
7334 | if (! elfcore_grok_note (abfd, &in)) | |
7335 | goto error; | |
7336 | } | |
252b5132 RH |
7337 | |
7338 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
7339 | } | |
7340 | ||
7341 | free (buf); | |
b34976b6 | 7342 | return TRUE; |
252b5132 | 7343 | } |
98d8431c JB |
7344 | \f |
7345 | /* Providing external access to the ELF program header table. */ | |
7346 | ||
7347 | /* Return an upper bound on the number of bytes required to store a | |
7348 | copy of ABFD's program header table entries. Return -1 if an error | |
7349 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 7350 | |
98d8431c JB |
7351 | long |
7352 | bfd_get_elf_phdr_upper_bound (abfd) | |
7353 | bfd *abfd; | |
7354 | { | |
7355 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
7356 | { | |
7357 | bfd_set_error (bfd_error_wrong_format); | |
7358 | return -1; | |
7359 | } | |
7360 | ||
936e320b | 7361 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
7362 | } |
7363 | ||
98d8431c JB |
7364 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
7365 | will be stored as an array of Elf_Internal_Phdr structures, as | |
7366 | defined in include/elf/internal.h. To find out how large the | |
7367 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
7368 | ||
7369 | Return the number of program header table entries read, or -1 if an | |
7370 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 7371 | |
98d8431c JB |
7372 | int |
7373 | bfd_get_elf_phdrs (abfd, phdrs) | |
7374 | bfd *abfd; | |
7375 | void *phdrs; | |
7376 | { | |
7377 | int num_phdrs; | |
7378 | ||
7379 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
7380 | { | |
7381 | bfd_set_error (bfd_error_wrong_format); | |
7382 | return -1; | |
7383 | } | |
7384 | ||
7385 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 7386 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
7387 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
7388 | ||
7389 | return num_phdrs; | |
7390 | } | |
ae4221d7 L |
7391 | |
7392 | void | |
4e771d61 | 7393 | _bfd_elf_sprintf_vma (abfd, buf, value) |
cc55aec9 | 7394 | bfd *abfd ATTRIBUTE_UNUSED; |
ae4221d7 L |
7395 | char *buf; |
7396 | bfd_vma value; | |
7397 | { | |
d3b05f8d | 7398 | #ifdef BFD64 |
ae4221d7 L |
7399 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ |
7400 | ||
7401 | i_ehdrp = elf_elfheader (abfd); | |
7402 | if (i_ehdrp == NULL) | |
7403 | sprintf_vma (buf, value); | |
7404 | else | |
7405 | { | |
7406 | if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64) | |
cc55aec9 | 7407 | { |
ae4221d7 | 7408 | #if BFD_HOST_64BIT_LONG |
cc55aec9 | 7409 | sprintf (buf, "%016lx", value); |
ae4221d7 | 7410 | #else |
cc55aec9 AM |
7411 | sprintf (buf, "%08lx%08lx", _bfd_int64_high (value), |
7412 | _bfd_int64_low (value)); | |
ae4221d7 | 7413 | #endif |
cc55aec9 | 7414 | } |
ae4221d7 L |
7415 | else |
7416 | sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff)); | |
7417 | } | |
d3b05f8d L |
7418 | #else |
7419 | sprintf_vma (buf, value); | |
7420 | #endif | |
ae4221d7 L |
7421 | } |
7422 | ||
7423 | void | |
4e771d61 | 7424 | _bfd_elf_fprintf_vma (abfd, stream, value) |
cc55aec9 | 7425 | bfd *abfd ATTRIBUTE_UNUSED; |
ae4221d7 L |
7426 | PTR stream; |
7427 | bfd_vma value; | |
7428 | { | |
d3b05f8d | 7429 | #ifdef BFD64 |
ae4221d7 L |
7430 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ |
7431 | ||
7432 | i_ehdrp = elf_elfheader (abfd); | |
7433 | if (i_ehdrp == NULL) | |
7434 | fprintf_vma ((FILE *) stream, value); | |
7435 | else | |
7436 | { | |
7437 | if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64) | |
cc55aec9 | 7438 | { |
ae4221d7 | 7439 | #if BFD_HOST_64BIT_LONG |
cc55aec9 | 7440 | fprintf ((FILE *) stream, "%016lx", value); |
ae4221d7 | 7441 | #else |
cc55aec9 AM |
7442 | fprintf ((FILE *) stream, "%08lx%08lx", |
7443 | _bfd_int64_high (value), _bfd_int64_low (value)); | |
ae4221d7 | 7444 | #endif |
cc55aec9 | 7445 | } |
ae4221d7 L |
7446 | else |
7447 | fprintf ((FILE *) stream, "%08lx", | |
7448 | (unsigned long) (value & 0xffffffff)); | |
7449 | } | |
d3b05f8d L |
7450 | #else |
7451 | fprintf_vma ((FILE *) stream, value); | |
7452 | #endif | |
ae4221d7 | 7453 | } |
db6751f2 JJ |
7454 | |
7455 | enum elf_reloc_type_class | |
f51e552e AM |
7456 | _bfd_elf_reloc_type_class (rela) |
7457 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED; | |
db6751f2 JJ |
7458 | { |
7459 | return reloc_class_normal; | |
7460 | } | |
f8df10f4 | 7461 | |
47d9a591 | 7462 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
7463 | relocation against a local symbol. */ |
7464 | ||
7465 | bfd_vma | |
7466 | _bfd_elf_rela_local_sym (abfd, sym, sec, rel) | |
7467 | bfd *abfd; | |
7468 | Elf_Internal_Sym *sym; | |
7469 | asection *sec; | |
7470 | Elf_Internal_Rela *rel; | |
7471 | { | |
7472 | bfd_vma relocation; | |
7473 | ||
7474 | relocation = (sec->output_section->vma | |
7475 | + sec->output_offset | |
7476 | + sym->st_value); | |
7477 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 7478 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
68bfbfcc | 7479 | && sec->sec_info_type == ELF_INFO_TYPE_MERGE) |
f8df10f4 JJ |
7480 | { |
7481 | asection *msec; | |
7482 | ||
7483 | msec = sec; | |
7484 | rel->r_addend = | |
7485 | _bfd_merged_section_offset (abfd, &msec, | |
65765700 | 7486 | elf_section_data (sec)->sec_info, |
f8df10f4 JJ |
7487 | sym->st_value + rel->r_addend, |
7488 | (bfd_vma) 0) | |
7489 | - relocation; | |
7490 | rel->r_addend += msec->output_section->vma + msec->output_offset; | |
7491 | } | |
7492 | return relocation; | |
7493 | } | |
c629eae0 JJ |
7494 | |
7495 | bfd_vma | |
7496 | _bfd_elf_rel_local_sym (abfd, sym, psec, addend) | |
7497 | bfd *abfd; | |
7498 | Elf_Internal_Sym *sym; | |
7499 | asection **psec; | |
7500 | bfd_vma addend; | |
47d9a591 | 7501 | { |
c629eae0 JJ |
7502 | asection *sec = *psec; |
7503 | ||
68bfbfcc | 7504 | if (sec->sec_info_type != ELF_INFO_TYPE_MERGE) |
c629eae0 JJ |
7505 | return sym->st_value + addend; |
7506 | ||
7507 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 7508 | elf_section_data (sec)->sec_info, |
c629eae0 JJ |
7509 | sym->st_value + addend, (bfd_vma) 0); |
7510 | } | |
7511 | ||
7512 | bfd_vma | |
7513 | _bfd_elf_section_offset (abfd, info, sec, offset) | |
7514 | bfd *abfd; | |
7515 | struct bfd_link_info *info; | |
7516 | asection *sec; | |
7517 | bfd_vma offset; | |
7518 | { | |
7519 | struct bfd_elf_section_data *sec_data; | |
7520 | ||
7521 | sec_data = elf_section_data (sec); | |
68bfbfcc | 7522 | switch (sec->sec_info_type) |
65765700 JJ |
7523 | { |
7524 | case ELF_INFO_TYPE_STABS: | |
126495ed AM |
7525 | return _bfd_stab_section_offset (abfd, |
7526 | &elf_hash_table (info)->merge_info, | |
7527 | sec, &sec_data->sec_info, offset); | |
65765700 JJ |
7528 | case ELF_INFO_TYPE_EH_FRAME: |
7529 | return _bfd_elf_eh_frame_section_offset (abfd, sec, offset); | |
7530 | default: | |
7531 | return offset; | |
7532 | } | |
c629eae0 | 7533 | } |