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