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