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