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