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