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