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