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