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