]>
Commit | Line | Data |
---|---|---|
32090b8e | 1 | /* ELF executable support for BFD. |
fd8d7c31 | 2 | Copyright 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc. |
32090b8e KR |
3 | |
4 | This file is part of BFD, the Binary File Descriptor library. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
6f904fce | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
32090b8e | 19 | |
d1b44e83 ILT |
20 | /* |
21 | ||
22 | SECTION | |
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 | |
31 | haven't bothered yet. | |
32 | */ | |
33 | ||
32090b8e KR |
34 | #include "bfd.h" |
35 | #include "sysdep.h" | |
013dec1a | 36 | #include "bfdlink.h" |
32090b8e KR |
37 | #include "libbfd.h" |
38 | #define ARCH_SIZE 0 | |
6ab826bd | 39 | #include "elf-bfd.h" |
32090b8e | 40 | |
fd0198f0 | 41 | static INLINE struct elf_segment_map *make_mapping |
edf3fe48 | 42 | PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean)); |
191d910c | 43 | static boolean map_sections_to_segments PARAMS ((bfd *)); |
fd0198f0 ILT |
44 | static int elf_sort_sections PARAMS ((const PTR, const PTR)); |
45 | static boolean assign_file_positions_for_segments PARAMS ((bfd *)); | |
46 | static boolean assign_file_positions_except_relocs PARAMS ((bfd *)); | |
ede4eed4 KR |
47 | static boolean prep_headers PARAMS ((bfd *)); |
48 | static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **)); | |
3dbf33ee | 49 | static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *)); |
ea3f0585 FF |
50 | static char *elf_read PARAMS ((bfd *, long, unsigned int)); |
51 | static void elf_fake_sections PARAMS ((bfd *, asection *, PTR)); | |
52 | static boolean assign_section_numbers PARAMS ((bfd *)); | |
53 | static INLINE int sym_is_global PARAMS ((bfd *, asymbol *)); | |
54 | static boolean elf_map_symbols PARAMS ((bfd *)); | |
55 | static bfd_size_type get_program_header_size PARAMS ((bfd *)); | |
ede4eed4 | 56 | |
a66a61a0 ILT |
57 | /* Swap version information in and out. The version information is |
58 | currently size independent. If that ever changes, this code will | |
59 | need to move into elfcode.h. */ | |
60 | ||
61 | /* Swap in a Verdef structure. */ | |
62 | ||
63 | void | |
64 | _bfd_elf_swap_verdef_in (abfd, src, dst) | |
65 | bfd *abfd; | |
66 | const Elf_External_Verdef *src; | |
67 | Elf_Internal_Verdef *dst; | |
68 | { | |
69 | dst->vd_version = bfd_h_get_16 (abfd, src->vd_version); | |
70 | dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags); | |
71 | dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx); | |
72 | dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt); | |
73 | dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash); | |
74 | dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux); | |
75 | dst->vd_next = bfd_h_get_32 (abfd, src->vd_next); | |
76 | } | |
77 | ||
78 | /* Swap out a Verdef structure. */ | |
79 | ||
80 | void | |
81 | _bfd_elf_swap_verdef_out (abfd, src, dst) | |
82 | bfd *abfd; | |
83 | const Elf_Internal_Verdef *src; | |
84 | Elf_External_Verdef *dst; | |
85 | { | |
86 | bfd_h_put_16 (abfd, src->vd_version, dst->vd_version); | |
87 | bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | bfd_h_put_32 (abfd, src->vd_next, dst->vd_next); | |
93 | } | |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
98 | _bfd_elf_swap_verdaux_in (abfd, src, dst) | |
99 | bfd *abfd; | |
100 | const Elf_External_Verdaux *src; | |
101 | Elf_Internal_Verdaux *dst; | |
102 | { | |
103 | dst->vda_name = bfd_h_get_32 (abfd, src->vda_name); | |
104 | dst->vda_next = bfd_h_get_32 (abfd, src->vda_next); | |
105 | } | |
106 | ||
107 | /* Swap out a Verdaux structure. */ | |
108 | ||
109 | void | |
110 | _bfd_elf_swap_verdaux_out (abfd, src, dst) | |
111 | bfd *abfd; | |
112 | const Elf_Internal_Verdaux *src; | |
113 | Elf_External_Verdaux *dst; | |
114 | { | |
115 | bfd_h_put_32 (abfd, src->vda_name, dst->vda_name); | |
116 | bfd_h_put_32 (abfd, src->vda_next, dst->vda_next); | |
117 | } | |
118 | ||
119 | /* Swap in a Verneed structure. */ | |
120 | ||
121 | void | |
122 | _bfd_elf_swap_verneed_in (abfd, src, dst) | |
123 | bfd *abfd; | |
124 | const Elf_External_Verneed *src; | |
125 | Elf_Internal_Verneed *dst; | |
126 | { | |
127 | dst->vn_version = bfd_h_get_16 (abfd, src->vn_version); | |
128 | dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt); | |
129 | dst->vn_file = bfd_h_get_32 (abfd, src->vn_file); | |
130 | dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux); | |
131 | dst->vn_next = bfd_h_get_32 (abfd, src->vn_next); | |
132 | } | |
133 | ||
134 | /* Swap out a Verneed structure. */ | |
135 | ||
136 | void | |
137 | _bfd_elf_swap_verneed_out (abfd, src, dst) | |
138 | bfd *abfd; | |
139 | const Elf_Internal_Verneed *src; | |
140 | Elf_External_Verneed *dst; | |
141 | { | |
142 | bfd_h_put_16 (abfd, src->vn_version, dst->vn_version); | |
143 | bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
144 | bfd_h_put_32 (abfd, src->vn_file, dst->vn_file); | |
145 | bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux); | |
146 | bfd_h_put_32 (abfd, src->vn_next, dst->vn_next); | |
147 | } | |
148 | ||
149 | /* Swap in a Vernaux structure. */ | |
150 | ||
151 | void | |
152 | _bfd_elf_swap_vernaux_in (abfd, src, dst) | |
153 | bfd *abfd; | |
154 | const Elf_External_Vernaux *src; | |
155 | Elf_Internal_Vernaux *dst; | |
156 | { | |
157 | dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash); | |
158 | dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags); | |
159 | dst->vna_other = bfd_h_get_16 (abfd, src->vna_other); | |
160 | dst->vna_name = bfd_h_get_32 (abfd, src->vna_name); | |
161 | dst->vna_next = bfd_h_get_32 (abfd, src->vna_next); | |
162 | } | |
163 | ||
164 | /* Swap out a Vernaux structure. */ | |
165 | ||
166 | void | |
167 | _bfd_elf_swap_vernaux_out (abfd, src, dst) | |
168 | bfd *abfd; | |
169 | const Elf_Internal_Vernaux *src; | |
170 | Elf_External_Vernaux *dst; | |
171 | { | |
172 | bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash); | |
173 | bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags); | |
174 | bfd_h_put_16 (abfd, src->vna_other, dst->vna_other); | |
175 | bfd_h_put_32 (abfd, src->vna_name, dst->vna_name); | |
176 | bfd_h_put_32 (abfd, src->vna_next, dst->vna_next); | |
177 | } | |
178 | ||
179 | /* Swap in a Versym structure. */ | |
180 | ||
181 | void | |
182 | _bfd_elf_swap_versym_in (abfd, src, dst) | |
183 | bfd *abfd; | |
184 | const Elf_External_Versym *src; | |
185 | Elf_Internal_Versym *dst; | |
186 | { | |
187 | dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers); | |
188 | } | |
189 | ||
190 | /* Swap out a Versym structure. */ | |
191 | ||
192 | void | |
193 | _bfd_elf_swap_versym_out (abfd, src, dst) | |
194 | bfd *abfd; | |
195 | const Elf_Internal_Versym *src; | |
196 | Elf_External_Versym *dst; | |
197 | { | |
198 | bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers); | |
199 | } | |
200 | ||
32090b8e KR |
201 | /* Standard ELF hash function. Do not change this function; you will |
202 | cause invalid hash tables to be generated. (Well, you would if this | |
203 | were being used yet.) */ | |
204 | unsigned long | |
013dec1a ILT |
205 | bfd_elf_hash (name) |
206 | CONST unsigned char *name; | |
32090b8e KR |
207 | { |
208 | unsigned long h = 0; | |
209 | unsigned long g; | |
210 | int ch; | |
211 | ||
212 | while ((ch = *name++) != '\0') | |
213 | { | |
214 | h = (h << 4) + ch; | |
215 | if ((g = (h & 0xf0000000)) != 0) | |
216 | { | |
217 | h ^= g >> 24; | |
218 | h &= ~g; | |
219 | } | |
220 | } | |
221 | return h; | |
222 | } | |
223 | ||
224 | /* Read a specified number of bytes at a specified offset in an ELF | |
225 | file, into a newly allocated buffer, and return a pointer to the | |
226 | buffer. */ | |
227 | ||
228 | static char * | |
013dec1a ILT |
229 | elf_read (abfd, offset, size) |
230 | bfd * abfd; | |
231 | long offset; | |
ae115e51 | 232 | unsigned int size; |
32090b8e KR |
233 | { |
234 | char *buf; | |
235 | ||
236 | if ((buf = bfd_alloc (abfd, size)) == NULL) | |
a9713b91 | 237 | return NULL; |
32090b8e | 238 | if (bfd_seek (abfd, offset, SEEK_SET) == -1) |
013dec1a | 239 | return NULL; |
32090b8e KR |
240 | if (bfd_read ((PTR) buf, size, 1, abfd) != size) |
241 | { | |
013dec1a ILT |
242 | if (bfd_get_error () != bfd_error_system_call) |
243 | bfd_set_error (bfd_error_file_truncated); | |
32090b8e KR |
244 | return NULL; |
245 | } | |
246 | return buf; | |
247 | } | |
248 | ||
249 | boolean | |
ff12f303 | 250 | bfd_elf_mkobject (abfd) |
013dec1a | 251 | bfd * abfd; |
32090b8e KR |
252 | { |
253 | /* this just does initialization */ | |
254 | /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */ | |
255 | elf_tdata (abfd) = (struct elf_obj_tdata *) | |
256 | bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); | |
257 | if (elf_tdata (abfd) == 0) | |
a9713b91 | 258 | return false; |
32090b8e KR |
259 | /* since everything is done at close time, do we need any |
260 | initialization? */ | |
261 | ||
262 | return true; | |
263 | } | |
264 | ||
265 | char * | |
ede4eed4 | 266 | bfd_elf_get_str_section (abfd, shindex) |
013dec1a ILT |
267 | bfd * abfd; |
268 | unsigned int shindex; | |
32090b8e KR |
269 | { |
270 | Elf_Internal_Shdr **i_shdrp; | |
271 | char *shstrtab = NULL; | |
272 | unsigned int offset; | |
273 | unsigned int shstrtabsize; | |
274 | ||
275 | i_shdrp = elf_elfsections (abfd); | |
276 | if (i_shdrp == 0 || i_shdrp[shindex] == 0) | |
277 | return 0; | |
278 | ||
b176e1e9 | 279 | shstrtab = (char *) i_shdrp[shindex]->contents; |
32090b8e KR |
280 | if (shstrtab == NULL) |
281 | { | |
282 | /* No cached one, attempt to read, and cache what we read. */ | |
283 | offset = i_shdrp[shindex]->sh_offset; | |
284 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
285 | shstrtab = elf_read (abfd, offset, shstrtabsize); | |
b176e1e9 | 286 | i_shdrp[shindex]->contents = (PTR) shstrtab; |
32090b8e KR |
287 | } |
288 | return shstrtab; | |
289 | } | |
290 | ||
291 | char * | |
ede4eed4 | 292 | bfd_elf_string_from_elf_section (abfd, shindex, strindex) |
013dec1a ILT |
293 | bfd * abfd; |
294 | unsigned int shindex; | |
295 | unsigned int strindex; | |
32090b8e KR |
296 | { |
297 | Elf_Internal_Shdr *hdr; | |
298 | ||
299 | if (strindex == 0) | |
300 | return ""; | |
301 | ||
302 | hdr = elf_elfsections (abfd)[shindex]; | |
303 | ||
b176e1e9 | 304 | if (hdr->contents == NULL |
ede4eed4 | 305 | && bfd_elf_get_str_section (abfd, shindex) == NULL) |
32090b8e KR |
306 | return NULL; |
307 | ||
b176e1e9 | 308 | return ((char *) hdr->contents) + strindex; |
32090b8e KR |
309 | } |
310 | ||
497c5434 | 311 | /* Make a BFD section from an ELF section. We store a pointer to the |
b176e1e9 | 312 | BFD section in the bfd_section field of the header. */ |
497c5434 ILT |
313 | |
314 | boolean | |
315 | _bfd_elf_make_section_from_shdr (abfd, hdr, name) | |
316 | bfd *abfd; | |
317 | Elf_Internal_Shdr *hdr; | |
318 | const char *name; | |
319 | { | |
320 | asection *newsect; | |
321 | flagword flags; | |
322 | ||
b176e1e9 | 323 | if (hdr->bfd_section != NULL) |
497c5434 | 324 | { |
b176e1e9 ILT |
325 | BFD_ASSERT (strcmp (name, |
326 | bfd_get_section_name (abfd, hdr->bfd_section)) == 0); | |
497c5434 ILT |
327 | return true; |
328 | } | |
329 | ||
330 | newsect = bfd_make_section_anyway (abfd, name); | |
331 | if (newsect == NULL) | |
332 | return false; | |
333 | ||
334 | newsect->filepos = hdr->sh_offset; | |
335 | ||
336 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
337 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
338 | || ! bfd_set_section_alignment (abfd, newsect, | |
339 | bfd_log2 (hdr->sh_addralign))) | |
340 | return false; | |
341 | ||
342 | flags = SEC_NO_FLAGS; | |
343 | if (hdr->sh_type != SHT_NOBITS) | |
344 | flags |= SEC_HAS_CONTENTS; | |
345 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
346 | { | |
347 | flags |= SEC_ALLOC; | |
348 | if (hdr->sh_type != SHT_NOBITS) | |
349 | flags |= SEC_LOAD; | |
350 | } | |
351 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
352 | flags |= SEC_READONLY; | |
353 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
354 | flags |= SEC_CODE; | |
7c6da9ca | 355 | else if ((flags & SEC_LOAD) != 0) |
497c5434 ILT |
356 | flags |= SEC_DATA; |
357 | ||
358 | /* The debugging sections appear to be recognized only by name, not | |
359 | any sort of flag. */ | |
360 | if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0 | |
361 | || strncmp (name, ".line", sizeof ".line" - 1) == 0 | |
362 | || strncmp (name, ".stab", sizeof ".stab" - 1) == 0) | |
363 | flags |= SEC_DEBUGGING; | |
364 | ||
f0c12b73 DE |
365 | /* As a GNU extension, if the name begins with .gnu.linkonce, we |
366 | only link a single copy of the section. This is used to support | |
367 | g++. g++ will emit each template expansion in its own section. | |
368 | The symbols will be defined as weak, so that multiple definitions | |
369 | are permitted. The GNU linker extension is to actually discard | |
370 | all but one of the sections. */ | |
371 | if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0) | |
372 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
373 | ||
497c5434 ILT |
374 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
375 | return false; | |
376 | ||
fd0198f0 ILT |
377 | if ((flags & SEC_ALLOC) != 0) |
378 | { | |
379 | Elf_Internal_Phdr *phdr; | |
380 | unsigned int i; | |
381 | ||
382 | /* Look through the phdrs to see if we need to adjust the lma. */ | |
383 | phdr = elf_tdata (abfd)->phdr; | |
384 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
385 | { | |
386 | if (phdr->p_type == PT_LOAD | |
6933148a | 387 | && phdr->p_paddr != 0 |
fd0198f0 ILT |
388 | && phdr->p_vaddr != phdr->p_paddr |
389 | && phdr->p_vaddr <= hdr->sh_addr | |
b944e7e8 ILT |
390 | && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size |
391 | && ((flags & SEC_LOAD) == 0 | |
392 | || (phdr->p_offset <= hdr->sh_offset | |
393 | && (phdr->p_offset + phdr->p_filesz | |
394 | >= hdr->sh_offset + hdr->sh_size)))) | |
fd0198f0 ILT |
395 | { |
396 | newsect->lma += phdr->p_paddr - phdr->p_vaddr; | |
397 | break; | |
398 | } | |
399 | } | |
400 | } | |
401 | ||
b176e1e9 | 402 | hdr->bfd_section = newsect; |
497c5434 ILT |
403 | elf_section_data (newsect)->this_hdr = *hdr; |
404 | ||
405 | return true; | |
406 | } | |
407 | ||
32090b8e KR |
408 | /* |
409 | INTERNAL_FUNCTION | |
410 | bfd_elf_find_section | |
411 | ||
412 | SYNOPSIS | |
413 | struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); | |
414 | ||
415 | DESCRIPTION | |
416 | Helper functions for GDB to locate the string tables. | |
417 | Since BFD hides string tables from callers, GDB needs to use an | |
418 | internal hook to find them. Sun's .stabstr, in particular, | |
419 | isn't even pointed to by the .stab section, so ordinary | |
420 | mechanisms wouldn't work to find it, even if we had some. | |
421 | */ | |
422 | ||
423 | struct elf_internal_shdr * | |
013dec1a ILT |
424 | bfd_elf_find_section (abfd, name) |
425 | bfd * abfd; | |
426 | char *name; | |
32090b8e KR |
427 | { |
428 | Elf_Internal_Shdr **i_shdrp; | |
429 | char *shstrtab; | |
430 | unsigned int max; | |
431 | unsigned int i; | |
432 | ||
433 | i_shdrp = elf_elfsections (abfd); | |
434 | if (i_shdrp != NULL) | |
435 | { | |
ede4eed4 | 436 | shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx); |
32090b8e KR |
437 | if (shstrtab != NULL) |
438 | { | |
439 | max = elf_elfheader (abfd)->e_shnum; | |
440 | for (i = 1; i < max; i++) | |
441 | if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) | |
442 | return i_shdrp[i]; | |
443 | } | |
444 | } | |
445 | return 0; | |
446 | } | |
447 | ||
32090b8e KR |
448 | const char *const bfd_elf_section_type_names[] = { |
449 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", | |
450 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
451 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
452 | }; | |
453 | ||
454 | /* ELF relocs are against symbols. If we are producing relocateable | |
455 | output, and the reloc is against an external symbol, and nothing | |
456 | has given us any additional addend, the resulting reloc will also | |
457 | be against the same symbol. In such a case, we don't want to | |
458 | change anything about the way the reloc is handled, since it will | |
459 | all be done at final link time. Rather than put special case code | |
460 | into bfd_perform_relocation, all the reloc types use this howto | |
461 | function. It just short circuits the reloc if producing | |
462 | relocateable output against an external symbol. */ | |
463 | ||
013dec1a | 464 | /*ARGSUSED*/ |
32090b8e KR |
465 | bfd_reloc_status_type |
466 | bfd_elf_generic_reloc (abfd, | |
467 | reloc_entry, | |
468 | symbol, | |
469 | data, | |
470 | input_section, | |
4c3721d5 ILT |
471 | output_bfd, |
472 | error_message) | |
32090b8e KR |
473 | bfd *abfd; |
474 | arelent *reloc_entry; | |
475 | asymbol *symbol; | |
476 | PTR data; | |
477 | asection *input_section; | |
478 | bfd *output_bfd; | |
4c3721d5 | 479 | char **error_message; |
32090b8e KR |
480 | { |
481 | if (output_bfd != (bfd *) NULL | |
482 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
d1b44e83 ILT |
483 | && (! reloc_entry->howto->partial_inplace |
484 | || reloc_entry->addend == 0)) | |
32090b8e KR |
485 | { |
486 | reloc_entry->address += input_section->output_offset; | |
487 | return bfd_reloc_ok; | |
488 | } | |
489 | ||
490 | return bfd_reloc_continue; | |
491 | } | |
013dec1a | 492 | \f |
27fb8f29 ILT |
493 | /* Print out the program headers. */ |
494 | ||
495 | boolean | |
496 | _bfd_elf_print_private_bfd_data (abfd, farg) | |
497 | bfd *abfd; | |
498 | PTR farg; | |
499 | { | |
500 | FILE *f = (FILE *) farg; | |
501 | Elf_Internal_Phdr *p; | |
02fcd126 ILT |
502 | asection *s; |
503 | bfd_byte *dynbuf = NULL; | |
27fb8f29 ILT |
504 | |
505 | p = elf_tdata (abfd)->phdr; | |
02fcd126 | 506 | if (p != NULL) |
27fb8f29 | 507 | { |
02fcd126 | 508 | unsigned int i, c; |
27fb8f29 | 509 | |
02fcd126 ILT |
510 | fprintf (f, "\nProgram Header:\n"); |
511 | c = elf_elfheader (abfd)->e_phnum; | |
512 | for (i = 0; i < c; i++, p++) | |
27fb8f29 | 513 | { |
02fcd126 ILT |
514 | const char *s; |
515 | char buf[20]; | |
516 | ||
517 | switch (p->p_type) | |
518 | { | |
519 | case PT_NULL: s = "NULL"; break; | |
520 | case PT_LOAD: s = "LOAD"; break; | |
521 | case PT_DYNAMIC: s = "DYNAMIC"; break; | |
522 | case PT_INTERP: s = "INTERP"; break; | |
523 | case PT_NOTE: s = "NOTE"; break; | |
524 | case PT_SHLIB: s = "SHLIB"; break; | |
525 | case PT_PHDR: s = "PHDR"; break; | |
526 | default: sprintf (buf, "0x%lx", p->p_type); s = buf; break; | |
527 | } | |
528 | fprintf (f, "%8s off 0x", s); | |
529 | fprintf_vma (f, p->p_offset); | |
530 | fprintf (f, " vaddr 0x"); | |
531 | fprintf_vma (f, p->p_vaddr); | |
532 | fprintf (f, " paddr 0x"); | |
533 | fprintf_vma (f, p->p_paddr); | |
534 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); | |
535 | fprintf (f, " filesz 0x"); | |
536 | fprintf_vma (f, p->p_filesz); | |
537 | fprintf (f, " memsz 0x"); | |
538 | fprintf_vma (f, p->p_memsz); | |
539 | fprintf (f, " flags %c%c%c", | |
540 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
541 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
542 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
543 | if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0) | |
544 | fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X)); | |
545 | fprintf (f, "\n"); | |
546 | } | |
547 | } | |
548 | ||
549 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
550 | if (s != NULL) | |
551 | { | |
552 | int elfsec; | |
553 | unsigned long link; | |
554 | bfd_byte *extdyn, *extdynend; | |
555 | size_t extdynsize; | |
556 | void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *)); | |
557 | ||
558 | fprintf (f, "\nDynamic Section:\n"); | |
559 | ||
560 | dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size); | |
561 | if (dynbuf == NULL) | |
562 | goto error_return; | |
563 | if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0, | |
564 | s->_raw_size)) | |
565 | goto error_return; | |
566 | ||
567 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
568 | if (elfsec == -1) | |
569 | goto error_return; | |
570 | link = elf_elfsections (abfd)[elfsec]->sh_link; | |
571 | ||
572 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
573 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
574 | ||
575 | extdyn = dynbuf; | |
576 | extdynend = extdyn + s->_raw_size; | |
577 | for (; extdyn < extdynend; extdyn += extdynsize) | |
578 | { | |
579 | Elf_Internal_Dyn dyn; | |
580 | const char *name; | |
581 | char ab[20]; | |
582 | boolean stringp; | |
583 | ||
584 | (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn); | |
585 | ||
586 | if (dyn.d_tag == DT_NULL) | |
587 | break; | |
588 | ||
589 | stringp = false; | |
590 | switch (dyn.d_tag) | |
591 | { | |
592 | default: | |
927d05b5 | 593 | sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); |
02fcd126 ILT |
594 | name = ab; |
595 | break; | |
596 | ||
597 | case DT_NEEDED: name = "NEEDED"; stringp = true; break; | |
598 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; | |
599 | case DT_PLTGOT: name = "PLTGOT"; break; | |
600 | case DT_HASH: name = "HASH"; break; | |
601 | case DT_STRTAB: name = "STRTAB"; break; | |
602 | case DT_SYMTAB: name = "SYMTAB"; break; | |
603 | case DT_RELA: name = "RELA"; break; | |
604 | case DT_RELASZ: name = "RELASZ"; break; | |
605 | case DT_RELAENT: name = "RELAENT"; break; | |
606 | case DT_STRSZ: name = "STRSZ"; break; | |
607 | case DT_SYMENT: name = "SYMENT"; break; | |
608 | case DT_INIT: name = "INIT"; break; | |
609 | case DT_FINI: name = "FINI"; break; | |
610 | case DT_SONAME: name = "SONAME"; stringp = true; break; | |
611 | case DT_RPATH: name = "RPATH"; stringp = true; break; | |
612 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; | |
613 | case DT_REL: name = "REL"; break; | |
614 | case DT_RELSZ: name = "RELSZ"; break; | |
615 | case DT_RELENT: name = "RELENT"; break; | |
616 | case DT_PLTREL: name = "PLTREL"; break; | |
617 | case DT_DEBUG: name = "DEBUG"; break; | |
618 | case DT_TEXTREL: name = "TEXTREL"; break; | |
619 | case DT_JMPREL: name = "JMPREL"; break; | |
148437ec ILT |
620 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break; |
621 | case DT_FILTER: name = "FILTER"; stringp = true; break; | |
a66a61a0 ILT |
622 | case DT_VERSYM: name = "VERSYM"; break; |
623 | case DT_VERDEF: name = "VERDEF"; break; | |
624 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
625 | case DT_VERNEED: name = "VERNEED"; break; | |
626 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
02fcd126 ILT |
627 | } |
628 | ||
629 | fprintf (f, " %-11s ", name); | |
630 | if (! stringp) | |
927d05b5 | 631 | fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val); |
02fcd126 ILT |
632 | else |
633 | { | |
634 | const char *string; | |
635 | ||
636 | string = bfd_elf_string_from_elf_section (abfd, link, | |
637 | dyn.d_un.d_val); | |
638 | if (string == NULL) | |
639 | goto error_return; | |
640 | fprintf (f, "%s", string); | |
641 | } | |
642 | fprintf (f, "\n"); | |
27fb8f29 | 643 | } |
02fcd126 ILT |
644 | |
645 | free (dynbuf); | |
646 | dynbuf = NULL; | |
27fb8f29 ILT |
647 | } |
648 | ||
a66a61a0 ILT |
649 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) |
650 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
651 | { | |
652 | if (! _bfd_elf_slurp_version_tables (abfd)) | |
653 | return false; | |
654 | } | |
655 | ||
656 | if (elf_dynverdef (abfd) != 0) | |
657 | { | |
658 | Elf_Internal_Verdef *t; | |
659 | ||
660 | fprintf (f, "\nVersion definitions:\n"); | |
661 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
662 | { | |
663 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
664 | t->vd_flags, t->vd_hash, t->vd_nodename); | |
665 | if (t->vd_auxptr->vda_nextptr != NULL) | |
666 | { | |
667 | Elf_Internal_Verdaux *a; | |
668 | ||
669 | fprintf (f, "\t"); | |
670 | for (a = t->vd_auxptr->vda_nextptr; | |
671 | a != NULL; | |
672 | a = a->vda_nextptr) | |
673 | fprintf (f, "%s ", a->vda_nodename); | |
674 | fprintf (f, "\n"); | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | if (elf_dynverref (abfd) != 0) | |
680 | { | |
681 | Elf_Internal_Verneed *t; | |
682 | ||
683 | fprintf (f, "\nVersion References:\n"); | |
684 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
685 | { | |
686 | Elf_Internal_Vernaux *a; | |
687 | ||
688 | fprintf (f, " required from %s:\n", t->vn_filename); | |
689 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
690 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
691 | a->vna_flags, a->vna_other, a->vna_nodename); | |
692 | } | |
693 | } | |
694 | ||
27fb8f29 | 695 | return true; |
02fcd126 ILT |
696 | |
697 | error_return: | |
698 | if (dynbuf != NULL) | |
699 | free (dynbuf); | |
700 | return false; | |
27fb8f29 ILT |
701 | } |
702 | ||
b176e1e9 | 703 | /* Display ELF-specific fields of a symbol. */ |
d6bfcdb5 | 704 | |
b176e1e9 | 705 | void |
d6bfcdb5 ILT |
706 | bfd_elf_print_symbol (abfd, filep, symbol, how) |
707 | bfd *abfd; | |
b176e1e9 ILT |
708 | PTR filep; |
709 | asymbol *symbol; | |
710 | bfd_print_symbol_type how; | |
711 | { | |
712 | FILE *file = (FILE *) filep; | |
713 | switch (how) | |
714 | { | |
715 | case bfd_print_symbol_name: | |
716 | fprintf (file, "%s", symbol->name); | |
717 | break; | |
718 | case bfd_print_symbol_more: | |
719 | fprintf (file, "elf "); | |
720 | fprintf_vma (file, symbol->value); | |
721 | fprintf (file, " %lx", (long) symbol->flags); | |
722 | break; | |
723 | case bfd_print_symbol_all: | |
724 | { | |
725 | CONST char *section_name; | |
726 | section_name = symbol->section ? symbol->section->name : "(*none*)"; | |
727 | bfd_print_symbol_vandf ((PTR) file, symbol); | |
728 | fprintf (file, " %s\t", section_name); | |
729 | /* Print the "other" value for a symbol. For common symbols, | |
730 | we've already printed the size; now print the alignment. | |
731 | For other symbols, we have no specified alignment, and | |
732 | we've printed the address; now print the size. */ | |
733 | fprintf_vma (file, | |
734 | (bfd_is_com_section (symbol->section) | |
735 | ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value | |
736 | : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size)); | |
d6bfcdb5 ILT |
737 | |
738 | /* If we have version information, print it. */ | |
739 | if (elf_tdata (abfd)->dynversym_section != 0 | |
740 | && (elf_tdata (abfd)->dynverdef_section != 0 | |
741 | || elf_tdata (abfd)->dynverref_section != 0)) | |
742 | { | |
743 | unsigned int vernum; | |
744 | const char *version_string; | |
745 | ||
746 | vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; | |
747 | ||
748 | if (vernum == 0) | |
749 | version_string = ""; | |
750 | else if (vernum == 1) | |
751 | version_string = "Base"; | |
752 | else if (vernum < elf_tdata (abfd)->cverdefs) | |
753 | version_string = | |
754 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
755 | else | |
756 | { | |
757 | Elf_Internal_Verneed *t; | |
758 | ||
759 | version_string = ""; | |
760 | for (t = elf_tdata (abfd)->verref; | |
761 | t != NULL; | |
762 | t = t->vn_nextref) | |
763 | { | |
764 | Elf_Internal_Vernaux *a; | |
765 | ||
766 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
767 | { | |
768 | if (a->vna_other == vernum) | |
769 | { | |
770 | version_string = a->vna_nodename; | |
771 | break; | |
772 | } | |
773 | } | |
774 | } | |
775 | } | |
776 | ||
777 | if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) | |
778 | fprintf (file, " %-12s", version_string); | |
779 | else | |
780 | { | |
781 | int i; | |
782 | ||
783 | fprintf (file, " (%s)", version_string); | |
784 | for (i = strlen (version_string) - 10; i > 0; --i) | |
785 | putc (' ', file); | |
786 | } | |
787 | } | |
788 | ||
69e2ff18 ILT |
789 | /* If the st_other field is not zero, print it. */ |
790 | if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0) | |
791 | fprintf (file, " 0x%02x", | |
792 | ((unsigned int) | |
793 | ((elf_symbol_type *) symbol)->internal_elf_sym.st_other)); | |
d6bfcdb5 | 794 | |
b176e1e9 ILT |
795 | fprintf (file, " %s", symbol->name); |
796 | } | |
797 | break; | |
798 | } | |
799 | } | |
800 | \f | |
013dec1a ILT |
801 | /* Create an entry in an ELF linker hash table. */ |
802 | ||
5315c428 ILT |
803 | struct bfd_hash_entry * |
804 | _bfd_elf_link_hash_newfunc (entry, table, string) | |
013dec1a ILT |
805 | struct bfd_hash_entry *entry; |
806 | struct bfd_hash_table *table; | |
807 | const char *string; | |
808 | { | |
809 | struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; | |
810 | ||
811 | /* Allocate the structure if it has not already been allocated by a | |
812 | subclass. */ | |
813 | if (ret == (struct elf_link_hash_entry *) NULL) | |
814 | ret = ((struct elf_link_hash_entry *) | |
815 | bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry))); | |
816 | if (ret == (struct elf_link_hash_entry *) NULL) | |
a9713b91 | 817 | return (struct bfd_hash_entry *) ret; |
013dec1a ILT |
818 | |
819 | /* Call the allocation method of the superclass. */ | |
820 | ret = ((struct elf_link_hash_entry *) | |
821 | _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
822 | table, string)); | |
823 | if (ret != (struct elf_link_hash_entry *) NULL) | |
824 | { | |
825 | /* Set local fields. */ | |
826 | ret->indx = -1; | |
827 | ret->size = 0; | |
013dec1a ILT |
828 | ret->dynindx = -1; |
829 | ret->dynstr_index = 0; | |
830 | ret->weakdef = NULL; | |
b176e1e9 ILT |
831 | ret->got_offset = (bfd_vma) -1; |
832 | ret->plt_offset = (bfd_vma) -1; | |
86aac8ea | 833 | ret->linker_section_pointer = (elf_linker_section_pointers_t *)0; |
a66a61a0 | 834 | ret->verinfo.verdef = NULL; |
013dec1a | 835 | ret->type = STT_NOTYPE; |
80be821d | 836 | ret->other = 0; |
869b7d80 ILT |
837 | /* Assume that we have been called by a non-ELF symbol reader. |
838 | This flag is then reset by the code which reads an ELF input | |
839 | file. This ensures that a symbol created by a non-ELF symbol | |
840 | reader will have the flag set correctly. */ | |
841 | ret->elf_link_hash_flags = ELF_LINK_NON_ELF; | |
013dec1a ILT |
842 | } |
843 | ||
844 | return (struct bfd_hash_entry *) ret; | |
845 | } | |
846 | ||
5315c428 ILT |
847 | /* Initialize an ELF linker hash table. */ |
848 | ||
849 | boolean | |
850 | _bfd_elf_link_hash_table_init (table, abfd, newfunc) | |
851 | struct elf_link_hash_table *table; | |
852 | bfd *abfd; | |
853 | struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, | |
854 | struct bfd_hash_table *, | |
855 | const char *)); | |
856 | { | |
b176e1e9 | 857 | table->dynamic_sections_created = false; |
5315c428 | 858 | table->dynobj = NULL; |
b176e1e9 ILT |
859 | /* The first dynamic symbol is a dummy. */ |
860 | table->dynsymcount = 1; | |
5315c428 ILT |
861 | table->dynstr = NULL; |
862 | table->bucketcount = 0; | |
b176e1e9 | 863 | table->needed = NULL; |
19bfbcbe | 864 | table->hgot = NULL; |
d1bf45aa | 865 | table->stab_info = NULL; |
5315c428 ILT |
866 | return _bfd_link_hash_table_init (&table->root, abfd, newfunc); |
867 | } | |
868 | ||
013dec1a ILT |
869 | /* Create an ELF linker hash table. */ |
870 | ||
871 | struct bfd_link_hash_table * | |
872 | _bfd_elf_link_hash_table_create (abfd) | |
873 | bfd *abfd; | |
874 | { | |
875 | struct elf_link_hash_table *ret; | |
876 | ||
877 | ret = ((struct elf_link_hash_table *) | |
878 | bfd_alloc (abfd, sizeof (struct elf_link_hash_table))); | |
879 | if (ret == (struct elf_link_hash_table *) NULL) | |
a9713b91 | 880 | return NULL; |
5315c428 ILT |
881 | |
882 | if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc)) | |
013dec1a ILT |
883 | { |
884 | bfd_release (abfd, ret); | |
885 | return NULL; | |
886 | } | |
887 | ||
013dec1a ILT |
888 | return &ret->root; |
889 | } | |
7c6da9ca ILT |
890 | |
891 | /* This is a hook for the ELF emulation code in the generic linker to | |
892 | tell the backend linker what file name to use for the DT_NEEDED | |
b176e1e9 ILT |
893 | entry for a dynamic object. The generic linker passes name as an |
894 | empty string to indicate that no DT_NEEDED entry should be made. */ | |
7c6da9ca ILT |
895 | |
896 | void | |
897 | bfd_elf_set_dt_needed_name (abfd, name) | |
898 | bfd *abfd; | |
899 | const char *name; | |
900 | { | |
053ae1d7 ILT |
901 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour |
902 | && bfd_get_format (abfd) == bfd_object) | |
903 | elf_dt_name (abfd) = name; | |
7c6da9ca | 904 | } |
b176e1e9 | 905 | |
053ae1d7 ILT |
906 | /* Get the list of DT_NEEDED entries for a link. This is a hook for |
907 | the ELF emulation code. */ | |
b176e1e9 | 908 | |
5fe14a9f | 909 | struct bfd_link_needed_list * |
b176e1e9 ILT |
910 | bfd_elf_get_needed_list (abfd, info) |
911 | bfd *abfd; | |
912 | struct bfd_link_info *info; | |
913 | { | |
b2193cc5 ILT |
914 | if (info->hash->creator->flavour != bfd_target_elf_flavour) |
915 | return NULL; | |
b176e1e9 ILT |
916 | return elf_hash_table (info)->needed; |
917 | } | |
053ae1d7 ILT |
918 | |
919 | /* Get the name actually used for a dynamic object for a link. This | |
920 | is the SONAME entry if there is one. Otherwise, it is the string | |
921 | passed to bfd_elf_set_dt_needed_name, or it is the filename. */ | |
922 | ||
923 | const char * | |
924 | bfd_elf_get_dt_soname (abfd) | |
925 | bfd *abfd; | |
926 | { | |
927 | if (bfd_get_flavour (abfd) == bfd_target_elf_flavour | |
928 | && bfd_get_format (abfd) == bfd_object) | |
929 | return elf_dt_name (abfd); | |
930 | return NULL; | |
931 | } | |
ede4eed4 KR |
932 | \f |
933 | /* Allocate an ELF string table--force the first byte to be zero. */ | |
934 | ||
935 | struct bfd_strtab_hash * | |
936 | _bfd_elf_stringtab_init () | |
937 | { | |
938 | struct bfd_strtab_hash *ret; | |
939 | ||
940 | ret = _bfd_stringtab_init (); | |
941 | if (ret != NULL) | |
942 | { | |
943 | bfd_size_type loc; | |
944 | ||
945 | loc = _bfd_stringtab_add (ret, "", true, false); | |
946 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); | |
947 | if (loc == (bfd_size_type) -1) | |
948 | { | |
949 | _bfd_stringtab_free (ret); | |
950 | ret = NULL; | |
951 | } | |
952 | } | |
953 | return ret; | |
954 | } | |
955 | \f | |
956 | /* ELF .o/exec file reading */ | |
957 | ||
958 | /* Create a new bfd section from an ELF section header. */ | |
959 | ||
960 | boolean | |
961 | bfd_section_from_shdr (abfd, shindex) | |
962 | bfd *abfd; | |
963 | unsigned int shindex; | |
964 | { | |
965 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; | |
966 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
967 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
968 | char *name; | |
969 | ||
970 | name = elf_string_from_elf_strtab (abfd, hdr->sh_name); | |
971 | ||
972 | switch (hdr->sh_type) | |
973 | { | |
974 | case SHT_NULL: | |
975 | /* Inactive section. Throw it away. */ | |
976 | return true; | |
977 | ||
978 | case SHT_PROGBITS: /* Normal section with contents. */ | |
979 | case SHT_DYNAMIC: /* Dynamic linking information. */ | |
980 | case SHT_NOBITS: /* .bss section. */ | |
981 | case SHT_HASH: /* .hash section. */ | |
5b3b9ff6 | 982 | case SHT_NOTE: /* .note section. */ |
ede4eed4 KR |
983 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
984 | ||
985 | case SHT_SYMTAB: /* A symbol table */ | |
986 | if (elf_onesymtab (abfd) == shindex) | |
987 | return true; | |
988 | ||
989 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
990 | BFD_ASSERT (elf_onesymtab (abfd) == 0); | |
991 | elf_onesymtab (abfd) = shindex; | |
992 | elf_tdata (abfd)->symtab_hdr = *hdr; | |
fd0198f0 | 993 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; |
ede4eed4 KR |
994 | abfd->flags |= HAS_SYMS; |
995 | ||
996 | /* Sometimes a shared object will map in the symbol table. If | |
997 | SHF_ALLOC is set, and this is a shared object, then we also | |
998 | treat this section as a BFD section. We can not base the | |
999 | decision purely on SHF_ALLOC, because that flag is sometimes | |
1000 | set in a relocateable object file, which would confuse the | |
1001 | linker. */ | |
1002 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
1003 | && (abfd->flags & DYNAMIC) != 0 | |
1004 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
1005 | return false; | |
1006 | ||
1007 | return true; | |
1008 | ||
1009 | case SHT_DYNSYM: /* A dynamic symbol table */ | |
1010 | if (elf_dynsymtab (abfd) == shindex) | |
1011 | return true; | |
1012 | ||
1013 | BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); | |
1014 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
1015 | elf_dynsymtab (abfd) = shindex; | |
1016 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
fd0198f0 | 1017 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; |
ede4eed4 KR |
1018 | abfd->flags |= HAS_SYMS; |
1019 | ||
1020 | /* Besides being a symbol table, we also treat this as a regular | |
1021 | section, so that objcopy can handle it. */ | |
1022 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1023 | ||
1024 | case SHT_STRTAB: /* A string table */ | |
1025 | if (hdr->bfd_section != NULL) | |
1026 | return true; | |
1027 | if (ehdr->e_shstrndx == shindex) | |
1028 | { | |
1029 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
1030 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
1031 | return true; | |
1032 | } | |
1033 | { | |
1034 | unsigned int i; | |
1035 | ||
1036 | for (i = 1; i < ehdr->e_shnum; i++) | |
1037 | { | |
1038 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
1039 | if (hdr2->sh_link == shindex) | |
1040 | { | |
1041 | if (! bfd_section_from_shdr (abfd, i)) | |
1042 | return false; | |
1043 | if (elf_onesymtab (abfd) == i) | |
1044 | { | |
1045 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
1046 | elf_elfsections (abfd)[shindex] = | |
1047 | &elf_tdata (abfd)->strtab_hdr; | |
1048 | return true; | |
1049 | } | |
1050 | if (elf_dynsymtab (abfd) == i) | |
1051 | { | |
1052 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
fd0198f0 | 1053 | elf_elfsections (abfd)[shindex] = hdr = |
ede4eed4 KR |
1054 | &elf_tdata (abfd)->dynstrtab_hdr; |
1055 | /* We also treat this as a regular section, so | |
1056 | that objcopy can handle it. */ | |
1057 | break; | |
1058 | } | |
1059 | #if 0 /* Not handling other string tables specially right now. */ | |
1060 | hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ | |
1061 | /* We have a strtab for some random other section. */ | |
1062 | newsect = (asection *) hdr2->bfd_section; | |
1063 | if (!newsect) | |
1064 | break; | |
1065 | hdr->bfd_section = newsect; | |
1066 | hdr2 = &elf_section_data (newsect)->str_hdr; | |
1067 | *hdr2 = *hdr; | |
1068 | elf_elfsections (abfd)[shindex] = hdr2; | |
1069 | #endif | |
1070 | } | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
1075 | ||
1076 | case SHT_REL: | |
1077 | case SHT_RELA: | |
1078 | /* *These* do a lot of work -- but build no sections! */ | |
1079 | { | |
1080 | asection *target_sect; | |
1081 | Elf_Internal_Shdr *hdr2; | |
ede4eed4 | 1082 | |
ae115e51 ILT |
1083 | /* For some incomprehensible reason Oracle distributes |
1084 | libraries for Solaris in which some of the objects have | |
1085 | bogus sh_link fields. It would be nice if we could just | |
1086 | reject them, but, unfortunately, some people need to use | |
1087 | them. We scan through the section headers; if we find only | |
1088 | one suitable symbol table, we clobber the sh_link to point | |
1089 | to it. I hope this doesn't break anything. */ | |
1090 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
1091 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) | |
1092 | { | |
1093 | int scan; | |
1094 | int found; | |
1095 | ||
1096 | found = 0; | |
1097 | for (scan = 1; scan < ehdr->e_shnum; scan++) | |
1098 | { | |
1099 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
1100 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
1101 | { | |
1102 | if (found != 0) | |
1103 | { | |
1104 | found = 0; | |
1105 | break; | |
1106 | } | |
1107 | found = scan; | |
1108 | } | |
1109 | } | |
1110 | if (found != 0) | |
1111 | hdr->sh_link = found; | |
1112 | } | |
1113 | ||
ede4eed4 | 1114 | /* Get the symbol table. */ |
ae115e51 ILT |
1115 | if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
1116 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
ede4eed4 KR |
1117 | return false; |
1118 | ||
1119 | /* If this reloc section does not use the main symbol table we | |
1120 | don't treat it as a reloc section. BFD can't adequately | |
1121 | represent such a section, so at least for now, we don't | |
1122 | try. We just present it as a normal section. */ | |
1123 | if (hdr->sh_link != elf_onesymtab (abfd)) | |
e85f2fbd | 1124 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
ede4eed4 | 1125 | |
ede4eed4 KR |
1126 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) |
1127 | return false; | |
1128 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); | |
1129 | if (target_sect == NULL) | |
1130 | return false; | |
1131 | ||
d1bf45aa ILT |
1132 | if ((target_sect->flags & SEC_RELOC) == 0 |
1133 | || target_sect->reloc_count == 0) | |
1134 | hdr2 = &elf_section_data (target_sect)->rel_hdr; | |
1135 | else | |
1136 | { | |
1137 | BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); | |
1138 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); | |
1139 | elf_section_data (target_sect)->rel_hdr2 = hdr2; | |
1140 | } | |
ede4eed4 KR |
1141 | *hdr2 = *hdr; |
1142 | elf_elfsections (abfd)[shindex] = hdr2; | |
d1bf45aa | 1143 | target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize; |
ede4eed4 KR |
1144 | target_sect->flags |= SEC_RELOC; |
1145 | target_sect->relocation = NULL; | |
1146 | target_sect->rel_filepos = hdr->sh_offset; | |
1147 | abfd->flags |= HAS_RELOC; | |
1148 | return true; | |
1149 | } | |
1150 | break; | |
1151 | ||
a66a61a0 ILT |
1152 | case SHT_GNU_verdef: |
1153 | elf_dynverdef (abfd) = shindex; | |
1154 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
d6bfcdb5 | 1155 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
a66a61a0 ILT |
1156 | break; |
1157 | ||
1158 | case SHT_GNU_versym: | |
1159 | elf_dynversym (abfd) = shindex; | |
1160 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
d6bfcdb5 | 1161 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
a66a61a0 ILT |
1162 | break; |
1163 | ||
1164 | case SHT_GNU_verneed: | |
1165 | elf_dynverref (abfd) = shindex; | |
1166 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
d6bfcdb5 | 1167 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
a66a61a0 ILT |
1168 | break; |
1169 | ||
ede4eed4 | 1170 | case SHT_SHLIB: |
ede4eed4 KR |
1171 | return true; |
1172 | ||
1173 | default: | |
1174 | /* Check for any processor-specific section types. */ | |
1175 | { | |
1176 | if (bed->elf_backend_section_from_shdr) | |
1177 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
1178 | } | |
1179 | break; | |
1180 | } | |
1181 | ||
1182 | return true; | |
1183 | } | |
1184 | ||
1185 | /* Given an ELF section number, retrieve the corresponding BFD | |
1186 | section. */ | |
1187 | ||
1188 | asection * | |
1189 | bfd_section_from_elf_index (abfd, index) | |
1190 | bfd *abfd; | |
1191 | unsigned int index; | |
1192 | { | |
1193 | BFD_ASSERT (index > 0 && index < SHN_LORESERVE); | |
1194 | if (index >= elf_elfheader (abfd)->e_shnum) | |
1195 | return NULL; | |
1196 | return elf_elfsections (abfd)[index]->bfd_section; | |
1197 | } | |
1198 | ||
1199 | boolean | |
1200 | _bfd_elf_new_section_hook (abfd, sec) | |
1201 | bfd *abfd; | |
1202 | asection *sec; | |
1203 | { | |
1204 | struct bfd_elf_section_data *sdata; | |
1205 | ||
1206 | sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata)); | |
1207 | if (!sdata) | |
a9713b91 | 1208 | return false; |
ede4eed4 KR |
1209 | sec->used_by_bfd = (PTR) sdata; |
1210 | memset (sdata, 0, sizeof (*sdata)); | |
1211 | return true; | |
1212 | } | |
1213 | ||
1214 | /* Create a new bfd section from an ELF program header. | |
1215 | ||
1216 | Since program segments have no names, we generate a synthetic name | |
1217 | of the form segment<NUM>, where NUM is generally the index in the | |
1218 | program header table. For segments that are split (see below) we | |
1219 | generate the names segment<NUM>a and segment<NUM>b. | |
1220 | ||
1221 | Note that some program segments may have a file size that is different than | |
1222 | (less than) the memory size. All this means is that at execution the | |
1223 | system must allocate the amount of memory specified by the memory size, | |
1224 | but only initialize it with the first "file size" bytes read from the | |
1225 | file. This would occur for example, with program segments consisting | |
1226 | of combined data+bss. | |
1227 | ||
1228 | To handle the above situation, this routine generates TWO bfd sections | |
1229 | for the single program segment. The first has the length specified by | |
1230 | the file size of the segment, and the second has the length specified | |
1231 | by the difference between the two sizes. In effect, the segment is split | |
1232 | into it's initialized and uninitialized parts. | |
1233 | ||
1234 | */ | |
1235 | ||
1236 | boolean | |
1237 | bfd_section_from_phdr (abfd, hdr, index) | |
1238 | bfd *abfd; | |
1239 | Elf_Internal_Phdr *hdr; | |
1240 | int index; | |
1241 | { | |
1242 | asection *newsect; | |
1243 | char *name; | |
1244 | char namebuf[64]; | |
1245 | int split; | |
1246 | ||
1247 | split = ((hdr->p_memsz > 0) && | |
1248 | (hdr->p_filesz > 0) && | |
1249 | (hdr->p_memsz > hdr->p_filesz)); | |
1250 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
1251 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
1252 | if (!name) | |
a9713b91 | 1253 | return false; |
ede4eed4 KR |
1254 | strcpy (name, namebuf); |
1255 | newsect = bfd_make_section (abfd, name); | |
1256 | if (newsect == NULL) | |
1257 | return false; | |
1258 | newsect->vma = hdr->p_vaddr; | |
ae115e51 | 1259 | newsect->lma = hdr->p_paddr; |
ede4eed4 KR |
1260 | newsect->_raw_size = hdr->p_filesz; |
1261 | newsect->filepos = hdr->p_offset; | |
1262 | newsect->flags |= SEC_HAS_CONTENTS; | |
1263 | if (hdr->p_type == PT_LOAD) | |
1264 | { | |
1265 | newsect->flags |= SEC_ALLOC; | |
1266 | newsect->flags |= SEC_LOAD; | |
1267 | if (hdr->p_flags & PF_X) | |
1268 | { | |
1269 | /* FIXME: all we known is that it has execute PERMISSION, | |
1270 | may be data. */ | |
1271 | newsect->flags |= SEC_CODE; | |
1272 | } | |
1273 | } | |
1274 | if (!(hdr->p_flags & PF_W)) | |
1275 | { | |
1276 | newsect->flags |= SEC_READONLY; | |
1277 | } | |
1278 | ||
1279 | if (split) | |
1280 | { | |
1281 | sprintf (namebuf, "segment%db", index); | |
1282 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
1283 | if (!name) | |
a9713b91 | 1284 | return false; |
ede4eed4 KR |
1285 | strcpy (name, namebuf); |
1286 | newsect = bfd_make_section (abfd, name); | |
1287 | if (newsect == NULL) | |
1288 | return false; | |
1289 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; | |
ae115e51 | 1290 | newsect->lma = hdr->p_paddr + hdr->p_filesz; |
ede4eed4 KR |
1291 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; |
1292 | if (hdr->p_type == PT_LOAD) | |
1293 | { | |
1294 | newsect->flags |= SEC_ALLOC; | |
1295 | if (hdr->p_flags & PF_X) | |
1296 | newsect->flags |= SEC_CODE; | |
1297 | } | |
1298 | if (!(hdr->p_flags & PF_W)) | |
1299 | newsect->flags |= SEC_READONLY; | |
1300 | } | |
1301 | ||
1302 | return true; | |
1303 | } | |
1304 | ||
1305 | /* Set up an ELF internal section header for a section. */ | |
1306 | ||
1307 | /*ARGSUSED*/ | |
1308 | static void | |
1309 | elf_fake_sections (abfd, asect, failedptrarg) | |
1310 | bfd *abfd; | |
1311 | asection *asect; | |
1312 | PTR failedptrarg; | |
1313 | { | |
1314 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1315 | boolean *failedptr = (boolean *) failedptrarg; | |
1316 | Elf_Internal_Shdr *this_hdr; | |
1317 | ||
1318 | if (*failedptr) | |
1319 | { | |
1320 | /* We already failed; just get out of the bfd_map_over_sections | |
1321 | loop. */ | |
1322 | return; | |
1323 | } | |
1324 | ||
1325 | this_hdr = &elf_section_data (asect)->this_hdr; | |
1326 | ||
1327 | this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd), | |
1328 | asect->name, | |
1329 | true, false); | |
1330 | if (this_hdr->sh_name == (unsigned long) -1) | |
1331 | { | |
1332 | *failedptr = true; | |
1333 | return; | |
1334 | } | |
1335 | ||
1336 | this_hdr->sh_flags = 0; | |
ae115e51 | 1337 | |
50bd50d4 MH |
1338 | if ((asect->flags & SEC_ALLOC) != 0 |
1339 | || asect->user_set_vma) | |
fd0198f0 | 1340 | this_hdr->sh_addr = asect->vma; |
ede4eed4 KR |
1341 | else |
1342 | this_hdr->sh_addr = 0; | |
ae115e51 | 1343 | |
ede4eed4 KR |
1344 | this_hdr->sh_offset = 0; |
1345 | this_hdr->sh_size = asect->_raw_size; | |
1346 | this_hdr->sh_link = 0; | |
ede4eed4 | 1347 | this_hdr->sh_addralign = 1 << asect->alignment_power; |
fd0198f0 ILT |
1348 | /* The sh_entsize and sh_info fields may have been set already by |
1349 | copy_private_section_data. */ | |
ede4eed4 KR |
1350 | |
1351 | this_hdr->bfd_section = asect; | |
1352 | this_hdr->contents = NULL; | |
1353 | ||
1354 | /* FIXME: This should not be based on section names. */ | |
1355 | if (strcmp (asect->name, ".dynstr") == 0) | |
1356 | this_hdr->sh_type = SHT_STRTAB; | |
1357 | else if (strcmp (asect->name, ".hash") == 0) | |
1358 | { | |
1359 | this_hdr->sh_type = SHT_HASH; | |
1360 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
1361 | } | |
1362 | else if (strcmp (asect->name, ".dynsym") == 0) | |
1363 | { | |
1364 | this_hdr->sh_type = SHT_DYNSYM; | |
1365 | this_hdr->sh_entsize = bed->s->sizeof_sym; | |
1366 | } | |
1367 | else if (strcmp (asect->name, ".dynamic") == 0) | |
1368 | { | |
1369 | this_hdr->sh_type = SHT_DYNAMIC; | |
1370 | this_hdr->sh_entsize = bed->s->sizeof_dyn; | |
1371 | } | |
1372 | else if (strncmp (asect->name, ".rela", 5) == 0 | |
1373 | && get_elf_backend_data (abfd)->use_rela_p) | |
1374 | { | |
1375 | this_hdr->sh_type = SHT_RELA; | |
1376 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
1377 | } | |
1378 | else if (strncmp (asect->name, ".rel", 4) == 0 | |
1379 | && ! get_elf_backend_data (abfd)->use_rela_p) | |
1380 | { | |
1381 | this_hdr->sh_type = SHT_REL; | |
1382 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
1383 | } | |
a66a61a0 | 1384 | else if (strncmp (asect->name, ".note", 5) == 0) |
ede4eed4 KR |
1385 | this_hdr->sh_type = SHT_NOTE; |
1386 | else if (strncmp (asect->name, ".stab", 5) == 0 | |
1387 | && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) | |
1388 | this_hdr->sh_type = SHT_STRTAB; | |
a66a61a0 ILT |
1389 | else if (strcmp (asect->name, ".gnu.version") == 0) |
1390 | { | |
1391 | this_hdr->sh_type = SHT_GNU_versym; | |
1392 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); | |
1393 | } | |
1394 | else if (strcmp (asect->name, ".gnu.version_d") == 0) | |
1395 | { | |
1396 | this_hdr->sh_type = SHT_GNU_verdef; | |
1397 | this_hdr->sh_entsize = 0; | |
d6bfcdb5 ILT |
1398 | /* objcopy or strip will copy over sh_info, but may not set |
1399 | cverdefs. The linker will set cverdefs, but sh_info will be | |
1400 | zero. */ | |
1401 | if (this_hdr->sh_info == 0) | |
1402 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
1403 | else | |
1404 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
1405 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
a66a61a0 ILT |
1406 | } |
1407 | else if (strcmp (asect->name, ".gnu.version_r") == 0) | |
1408 | { | |
1409 | this_hdr->sh_type = SHT_GNU_verneed; | |
1410 | this_hdr->sh_entsize = 0; | |
d6bfcdb5 ILT |
1411 | /* objcopy or strip will copy over sh_info, but may not set |
1412 | cverrefs. The linker will set cverrefs, but sh_info will be | |
1413 | zero. */ | |
1414 | if (this_hdr->sh_info == 0) | |
1415 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
1416 | else | |
1417 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
1418 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
a66a61a0 | 1419 | } |
ede4eed4 KR |
1420 | else if ((asect->flags & SEC_ALLOC) != 0 |
1421 | && (asect->flags & SEC_LOAD) != 0) | |
1422 | this_hdr->sh_type = SHT_PROGBITS; | |
1423 | else if ((asect->flags & SEC_ALLOC) != 0 | |
1424 | && ((asect->flags & SEC_LOAD) == 0)) | |
5fe14a9f | 1425 | this_hdr->sh_type = SHT_NOBITS; |
ede4eed4 KR |
1426 | else |
1427 | { | |
1428 | /* Who knows? */ | |
1429 | this_hdr->sh_type = SHT_PROGBITS; | |
1430 | } | |
1431 | ||
1432 | if ((asect->flags & SEC_ALLOC) != 0) | |
1433 | this_hdr->sh_flags |= SHF_ALLOC; | |
1434 | if ((asect->flags & SEC_READONLY) == 0) | |
1435 | this_hdr->sh_flags |= SHF_WRITE; | |
1436 | if ((asect->flags & SEC_CODE) != 0) | |
1437 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
1438 | ||
1439 | /* Check for processor-specific section types. */ | |
1440 | { | |
1441 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1442 | ||
1443 | if (bed->elf_backend_fake_sections) | |
1444 | (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect); | |
1445 | } | |
1446 | ||
1447 | /* If the section has relocs, set up a section header for the | |
1448 | SHT_REL[A] section. */ | |
1449 | if ((asect->flags & SEC_RELOC) != 0) | |
1450 | { | |
1451 | Elf_Internal_Shdr *rela_hdr; | |
1452 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
1453 | char *name; | |
1454 | ||
1455 | rela_hdr = &elf_section_data (asect)->rel_hdr; | |
1456 | name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name)); | |
1457 | if (name == NULL) | |
1458 | { | |
ede4eed4 KR |
1459 | *failedptr = true; |
1460 | return; | |
1461 | } | |
1462 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); | |
1463 | rela_hdr->sh_name = | |
1464 | (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name, | |
1465 | true, false); | |
1466 | if (rela_hdr->sh_name == (unsigned int) -1) | |
1467 | { | |
1468 | *failedptr = true; | |
1469 | return; | |
1470 | } | |
1471 | rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; | |
1472 | rela_hdr->sh_entsize = (use_rela_p | |
1473 | ? bed->s->sizeof_rela | |
1474 | : bed->s->sizeof_rel); | |
1475 | rela_hdr->sh_addralign = bed->s->file_align; | |
1476 | rela_hdr->sh_flags = 0; | |
1477 | rela_hdr->sh_addr = 0; | |
1478 | rela_hdr->sh_size = 0; | |
1479 | rela_hdr->sh_offset = 0; | |
1480 | } | |
1481 | } | |
1482 | ||
1483 | /* Assign all ELF section numbers. The dummy first section is handled here | |
1484 | too. The link/info pointers for the standard section types are filled | |
1485 | in here too, while we're at it. */ | |
1486 | ||
1487 | static boolean | |
1488 | assign_section_numbers (abfd) | |
1489 | bfd *abfd; | |
1490 | { | |
1491 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
1492 | asection *sec; | |
1493 | unsigned int section_number; | |
1494 | Elf_Internal_Shdr **i_shdrp; | |
1495 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1496 | ||
1497 | section_number = 1; | |
1498 | ||
1499 | for (sec = abfd->sections; sec; sec = sec->next) | |
1500 | { | |
1501 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1502 | ||
1503 | d->this_idx = section_number++; | |
1504 | if ((sec->flags & SEC_RELOC) == 0) | |
1505 | d->rel_idx = 0; | |
1506 | else | |
1507 | d->rel_idx = section_number++; | |
1508 | } | |
1509 | ||
1510 | t->shstrtab_section = section_number++; | |
1511 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; | |
1512 | t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
1513 | ||
1514 | if (abfd->symcount > 0) | |
1515 | { | |
1516 | t->symtab_section = section_number++; | |
1517 | t->strtab_section = section_number++; | |
1518 | } | |
1519 | ||
1520 | elf_elfheader (abfd)->e_shnum = section_number; | |
1521 | ||
1522 | /* Set up the list of section header pointers, in agreement with the | |
1523 | indices. */ | |
1524 | i_shdrp = ((Elf_Internal_Shdr **) | |
1525 | bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *))); | |
1526 | if (i_shdrp == NULL) | |
a9713b91 | 1527 | return false; |
ede4eed4 KR |
1528 | |
1529 | i_shdrp[0] = ((Elf_Internal_Shdr *) | |
1530 | bfd_alloc (abfd, sizeof (Elf_Internal_Shdr))); | |
1531 | if (i_shdrp[0] == NULL) | |
1532 | { | |
1533 | bfd_release (abfd, i_shdrp); | |
ede4eed4 KR |
1534 | return false; |
1535 | } | |
1536 | memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr)); | |
1537 | ||
1538 | elf_elfsections (abfd) = i_shdrp; | |
1539 | ||
1540 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
1541 | if (abfd->symcount > 0) | |
1542 | { | |
1543 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
1544 | i_shdrp[t->strtab_section] = &t->strtab_hdr; | |
1545 | t->symtab_hdr.sh_link = t->strtab_section; | |
1546 | } | |
1547 | for (sec = abfd->sections; sec; sec = sec->next) | |
1548 | { | |
1549 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1550 | asection *s; | |
1551 | const char *name; | |
1552 | ||
1553 | i_shdrp[d->this_idx] = &d->this_hdr; | |
1554 | if (d->rel_idx != 0) | |
1555 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
1556 | ||
1557 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
1558 | ||
1559 | /* sh_link of a reloc section is the section index of the symbol | |
1560 | table. sh_info is the section index of the section to which | |
1561 | the relocation entries apply. */ | |
1562 | if (d->rel_idx != 0) | |
1563 | { | |
1564 | d->rel_hdr.sh_link = t->symtab_section; | |
1565 | d->rel_hdr.sh_info = d->this_idx; | |
1566 | } | |
1567 | ||
1568 | switch (d->this_hdr.sh_type) | |
1569 | { | |
1570 | case SHT_REL: | |
1571 | case SHT_RELA: | |
1572 | /* A reloc section which we are treating as a normal BFD | |
1573 | section. sh_link is the section index of the symbol | |
1574 | table. sh_info is the section index of the section to | |
1575 | which the relocation entries apply. We assume that an | |
1576 | allocated reloc section uses the dynamic symbol table. | |
1577 | FIXME: How can we be sure? */ | |
1578 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1579 | if (s != NULL) | |
1580 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1581 | ||
1582 | /* We look up the section the relocs apply to by name. */ | |
1583 | name = sec->name; | |
1584 | if (d->this_hdr.sh_type == SHT_REL) | |
1585 | name += 4; | |
1586 | else | |
1587 | name += 5; | |
1588 | s = bfd_get_section_by_name (abfd, name); | |
1589 | if (s != NULL) | |
1590 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
1591 | break; | |
1592 | ||
1593 | case SHT_STRTAB: | |
1594 | /* We assume that a section named .stab*str is a stabs | |
1595 | string section. We look for a section with the same name | |
1596 | but without the trailing ``str'', and set its sh_link | |
1597 | field to point to this section. */ | |
1598 | if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 | |
1599 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) | |
1600 | { | |
1601 | size_t len; | |
1602 | char *alc; | |
1603 | ||
1604 | len = strlen (sec->name); | |
58142f10 | 1605 | alc = (char *) bfd_malloc (len - 2); |
ede4eed4 | 1606 | if (alc == NULL) |
58142f10 | 1607 | return false; |
ede4eed4 KR |
1608 | strncpy (alc, sec->name, len - 3); |
1609 | alc[len - 3] = '\0'; | |
1610 | s = bfd_get_section_by_name (abfd, alc); | |
1611 | free (alc); | |
1612 | if (s != NULL) | |
1613 | { | |
1614 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
1615 | ||
1616 | /* This is a .stab section. */ | |
1617 | elf_section_data (s)->this_hdr.sh_entsize = | |
1618 | 4 + 2 * (bed->s->arch_size / 8); | |
1619 | } | |
1620 | } | |
1621 | break; | |
1622 | ||
1623 | case SHT_DYNAMIC: | |
1624 | case SHT_DYNSYM: | |
a66a61a0 ILT |
1625 | case SHT_GNU_verneed: |
1626 | case SHT_GNU_verdef: | |
ede4eed4 | 1627 | /* sh_link is the section header index of the string table |
a66a61a0 ILT |
1628 | used for the dynamic entries, or the symbol table, or the |
1629 | version strings. */ | |
ede4eed4 KR |
1630 | s = bfd_get_section_by_name (abfd, ".dynstr"); |
1631 | if (s != NULL) | |
1632 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1633 | break; | |
1634 | ||
1635 | case SHT_HASH: | |
a66a61a0 | 1636 | case SHT_GNU_versym: |
ede4eed4 | 1637 | /* sh_link is the section header index of the symbol table |
a66a61a0 | 1638 | this hash table or version table is for. */ |
ede4eed4 KR |
1639 | s = bfd_get_section_by_name (abfd, ".dynsym"); |
1640 | if (s != NULL) | |
1641 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1642 | break; | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | return true; | |
1647 | } | |
1648 | ||
1649 | /* Map symbol from it's internal number to the external number, moving | |
1650 | all local symbols to be at the head of the list. */ | |
1651 | ||
1652 | static INLINE int | |
1653 | sym_is_global (abfd, sym) | |
1654 | bfd *abfd; | |
1655 | asymbol *sym; | |
1656 | { | |
1657 | /* If the backend has a special mapping, use it. */ | |
1658 | if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1659 | return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1660 | (abfd, sym)); | |
1661 | ||
1662 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 | |
1663 | || bfd_is_und_section (bfd_get_section (sym)) | |
1664 | || bfd_is_com_section (bfd_get_section (sym))); | |
1665 | } | |
1666 | ||
1667 | static boolean | |
1668 | elf_map_symbols (abfd) | |
1669 | bfd *abfd; | |
1670 | { | |
1671 | int symcount = bfd_get_symcount (abfd); | |
1672 | asymbol **syms = bfd_get_outsymbols (abfd); | |
1673 | asymbol **sect_syms; | |
1674 | int num_locals = 0; | |
1675 | int num_globals = 0; | |
1676 | int num_locals2 = 0; | |
1677 | int num_globals2 = 0; | |
1678 | int max_index = 0; | |
1679 | int num_sections = 0; | |
1680 | int idx; | |
1681 | asection *asect; | |
1682 | asymbol **new_syms; | |
1683 | ||
1684 | #ifdef DEBUG | |
1685 | fprintf (stderr, "elf_map_symbols\n"); | |
1686 | fflush (stderr); | |
1687 | #endif | |
1688 | ||
1689 | /* Add a section symbol for each BFD section. FIXME: Is this really | |
1690 | necessary? */ | |
1691 | for (asect = abfd->sections; asect; asect = asect->next) | |
1692 | { | |
1693 | if (max_index < asect->index) | |
1694 | max_index = asect->index; | |
1695 | } | |
1696 | ||
1697 | max_index++; | |
1698 | sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *)); | |
1699 | if (sect_syms == NULL) | |
a9713b91 | 1700 | return false; |
ede4eed4 KR |
1701 | elf_section_syms (abfd) = sect_syms; |
1702 | ||
1703 | for (idx = 0; idx < symcount; idx++) | |
1704 | { | |
1705 | if ((syms[idx]->flags & BSF_SECTION_SYM) != 0 | |
fd0198f0 | 1706 | && (syms[idx]->value + syms[idx]->section->vma) == 0) |
ede4eed4 KR |
1707 | { |
1708 | asection *sec; | |
1709 | ||
1710 | sec = syms[idx]->section; | |
1711 | if (sec->owner != NULL) | |
1712 | { | |
1713 | if (sec->owner != abfd) | |
1714 | { | |
1715 | if (sec->output_offset != 0) | |
1716 | continue; | |
1717 | sec = sec->output_section; | |
1718 | BFD_ASSERT (sec->owner == abfd); | |
1719 | } | |
1720 | sect_syms[sec->index] = syms[idx]; | |
1721 | } | |
1722 | } | |
1723 | } | |
1724 | ||
1725 | for (asect = abfd->sections; asect; asect = asect->next) | |
1726 | { | |
1727 | asymbol *sym; | |
1728 | ||
1729 | if (sect_syms[asect->index] != NULL) | |
1730 | continue; | |
1731 | ||
1732 | sym = bfd_make_empty_symbol (abfd); | |
1733 | if (sym == NULL) | |
1734 | return false; | |
1735 | sym->the_bfd = abfd; | |
1736 | sym->name = asect->name; | |
1737 | sym->value = 0; | |
1738 | /* Set the flags to 0 to indicate that this one was newly added. */ | |
1739 | sym->flags = 0; | |
1740 | sym->section = asect; | |
1741 | sect_syms[asect->index] = sym; | |
1742 | num_sections++; | |
1743 | #ifdef DEBUG | |
1744 | fprintf (stderr, | |
1745 | "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n", | |
1746 | asect->name, (long) asect->vma, asect->index, (long) asect); | |
1747 | #endif | |
1748 | } | |
1749 | ||
1750 | /* Classify all of the symbols. */ | |
1751 | for (idx = 0; idx < symcount; idx++) | |
1752 | { | |
1753 | if (!sym_is_global (abfd, syms[idx])) | |
1754 | num_locals++; | |
1755 | else | |
1756 | num_globals++; | |
1757 | } | |
1758 | for (asect = abfd->sections; asect; asect = asect->next) | |
1759 | { | |
1760 | if (sect_syms[asect->index] != NULL | |
1761 | && sect_syms[asect->index]->flags == 0) | |
1762 | { | |
1763 | sect_syms[asect->index]->flags = BSF_SECTION_SYM; | |
1764 | if (!sym_is_global (abfd, sect_syms[asect->index])) | |
1765 | num_locals++; | |
1766 | else | |
1767 | num_globals++; | |
1768 | sect_syms[asect->index]->flags = 0; | |
1769 | } | |
1770 | } | |
1771 | ||
1772 | /* Now sort the symbols so the local symbols are first. */ | |
1773 | new_syms = ((asymbol **) | |
1774 | bfd_alloc (abfd, | |
1775 | (num_locals + num_globals) * sizeof (asymbol *))); | |
1776 | if (new_syms == NULL) | |
a9713b91 | 1777 | return false; |
ede4eed4 KR |
1778 | |
1779 | for (idx = 0; idx < symcount; idx++) | |
1780 | { | |
1781 | asymbol *sym = syms[idx]; | |
1782 | int i; | |
1783 | ||
1784 | if (!sym_is_global (abfd, sym)) | |
1785 | i = num_locals2++; | |
1786 | else | |
1787 | i = num_locals + num_globals2++; | |
1788 | new_syms[i] = sym; | |
1789 | sym->udata.i = i + 1; | |
1790 | } | |
1791 | for (asect = abfd->sections; asect; asect = asect->next) | |
1792 | { | |
1793 | if (sect_syms[asect->index] != NULL | |
1794 | && sect_syms[asect->index]->flags == 0) | |
1795 | { | |
1796 | asymbol *sym = sect_syms[asect->index]; | |
1797 | int i; | |
1798 | ||
1799 | sym->flags = BSF_SECTION_SYM; | |
1800 | if (!sym_is_global (abfd, sym)) | |
1801 | i = num_locals2++; | |
1802 | else | |
1803 | i = num_locals + num_globals2++; | |
1804 | new_syms[i] = sym; | |
1805 | sym->udata.i = i + 1; | |
1806 | } | |
1807 | } | |
1808 | ||
1809 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
1810 | ||
1811 | elf_num_locals (abfd) = num_locals; | |
1812 | elf_num_globals (abfd) = num_globals; | |
1813 | return true; | |
1814 | } | |
1815 | ||
fd0198f0 ILT |
1816 | /* Align to the maximum file alignment that could be required for any |
1817 | ELF data structure. */ | |
1818 | ||
1819 | static INLINE file_ptr align_file_position PARAMS ((file_ptr, int)); | |
1820 | static INLINE file_ptr | |
1821 | align_file_position (off, align) | |
1822 | file_ptr off; | |
1823 | int align; | |
1824 | { | |
1825 | return (off + align - 1) & ~(align - 1); | |
1826 | } | |
1827 | ||
1828 | /* Assign a file position to a section, optionally aligning to the | |
1829 | required section alignment. */ | |
1830 | ||
1831 | INLINE file_ptr | |
1832 | _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align) | |
1833 | Elf_Internal_Shdr *i_shdrp; | |
1834 | file_ptr offset; | |
1835 | boolean align; | |
1836 | { | |
1837 | if (align) | |
1838 | { | |
1839 | unsigned int al; | |
1840 | ||
1841 | al = i_shdrp->sh_addralign; | |
1842 | if (al > 1) | |
1843 | offset = BFD_ALIGN (offset, al); | |
1844 | } | |
1845 | i_shdrp->sh_offset = offset; | |
1846 | if (i_shdrp->bfd_section != NULL) | |
1847 | i_shdrp->bfd_section->filepos = offset; | |
1848 | if (i_shdrp->sh_type != SHT_NOBITS) | |
1849 | offset += i_shdrp->sh_size; | |
1850 | return offset; | |
1851 | } | |
1852 | ||
ede4eed4 KR |
1853 | /* Compute the file positions we are going to put the sections at, and |
1854 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
1855 | is not NULL, this is being called by the ELF backend linker. */ | |
1856 | ||
1857 | boolean | |
1858 | _bfd_elf_compute_section_file_positions (abfd, link_info) | |
1859 | bfd *abfd; | |
1860 | struct bfd_link_info *link_info; | |
1861 | { | |
1862 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1863 | boolean failed; | |
1864 | struct bfd_strtab_hash *strtab; | |
1865 | Elf_Internal_Shdr *shstrtab_hdr; | |
1866 | ||
1867 | if (abfd->output_has_begun) | |
1868 | return true; | |
1869 | ||
1870 | /* Do any elf backend specific processing first. */ | |
1871 | if (bed->elf_backend_begin_write_processing) | |
1872 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
1873 | ||
1874 | if (! prep_headers (abfd)) | |
1875 | return false; | |
1876 | ||
1877 | failed = false; | |
1878 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); | |
1879 | if (failed) | |
1880 | return false; | |
1881 | ||
1882 | if (!assign_section_numbers (abfd)) | |
1883 | return false; | |
1884 | ||
1885 | /* The backend linker builds symbol table information itself. */ | |
fd0198f0 | 1886 | if (link_info == NULL && abfd->symcount > 0) |
ede4eed4 KR |
1887 | { |
1888 | if (! swap_out_syms (abfd, &strtab)) | |
1889 | return false; | |
1890 | } | |
1891 | ||
1892 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; | |
1893 | /* sh_name was set in prep_headers. */ | |
1894 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
1895 | shstrtab_hdr->sh_flags = 0; | |
1896 | shstrtab_hdr->sh_addr = 0; | |
1897 | shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); | |
1898 | shstrtab_hdr->sh_entsize = 0; | |
1899 | shstrtab_hdr->sh_link = 0; | |
1900 | shstrtab_hdr->sh_info = 0; | |
fd0198f0 | 1901 | /* sh_offset is set in assign_file_positions_except_relocs. */ |
ede4eed4 KR |
1902 | shstrtab_hdr->sh_addralign = 1; |
1903 | ||
fd0198f0 | 1904 | if (!assign_file_positions_except_relocs (abfd)) |
ede4eed4 KR |
1905 | return false; |
1906 | ||
fd0198f0 | 1907 | if (link_info == NULL && abfd->symcount > 0) |
ede4eed4 | 1908 | { |
fd0198f0 ILT |
1909 | file_ptr off; |
1910 | Elf_Internal_Shdr *hdr; | |
1911 | ||
1912 | off = elf_tdata (abfd)->next_file_pos; | |
1913 | ||
1914 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
1915 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
1916 | ||
1917 | hdr = &elf_tdata (abfd)->strtab_hdr; | |
1918 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
1919 | ||
1920 | elf_tdata (abfd)->next_file_pos = off; | |
1921 | ||
ede4eed4 KR |
1922 | /* Now that we know where the .strtab section goes, write it |
1923 | out. */ | |
fd0198f0 | 1924 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ede4eed4 KR |
1925 | || ! _bfd_stringtab_emit (abfd, strtab)) |
1926 | return false; | |
1927 | _bfd_stringtab_free (strtab); | |
1928 | } | |
1929 | ||
1930 | abfd->output_has_begun = true; | |
1931 | ||
1932 | return true; | |
1933 | } | |
1934 | ||
fd0198f0 | 1935 | /* Create a mapping from a set of sections to a program segment. */ |
ede4eed4 | 1936 | |
fd0198f0 | 1937 | static INLINE struct elf_segment_map * |
edf3fe48 | 1938 | make_mapping (abfd, sections, from, to, phdr) |
fd0198f0 ILT |
1939 | bfd *abfd; |
1940 | asection **sections; | |
1941 | unsigned int from; | |
1942 | unsigned int to; | |
edf3fe48 | 1943 | boolean phdr; |
ede4eed4 | 1944 | { |
fd0198f0 ILT |
1945 | struct elf_segment_map *m; |
1946 | unsigned int i; | |
1947 | asection **hdrpp; | |
1948 | ||
1949 | m = ((struct elf_segment_map *) | |
1950 | bfd_zalloc (abfd, | |
1951 | (sizeof (struct elf_segment_map) | |
1952 | + (to - from - 1) * sizeof (asection *)))); | |
1953 | if (m == NULL) | |
a9713b91 | 1954 | return NULL; |
fd0198f0 ILT |
1955 | m->next = NULL; |
1956 | m->p_type = PT_LOAD; | |
1957 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
1958 | m->sections[i - from] = *hdrpp; | |
1959 | m->count = to - from; | |
1960 | ||
edf3fe48 | 1961 | if (from == 0 && phdr) |
6933148a ILT |
1962 | { |
1963 | /* Include the headers in the first PT_LOAD segment. */ | |
1964 | m->includes_filehdr = 1; | |
1965 | m->includes_phdrs = 1; | |
1966 | } | |
1967 | ||
fd0198f0 | 1968 | return m; |
ede4eed4 KR |
1969 | } |
1970 | ||
fd0198f0 | 1971 | /* Set up a mapping from BFD sections to program segments. */ |
ede4eed4 | 1972 | |
fd0198f0 ILT |
1973 | static boolean |
1974 | map_sections_to_segments (abfd) | |
1975 | bfd *abfd; | |
ede4eed4 | 1976 | { |
fd0198f0 ILT |
1977 | asection **sections = NULL; |
1978 | asection *s; | |
1979 | unsigned int i; | |
1980 | unsigned int count; | |
1981 | struct elf_segment_map *mfirst; | |
1982 | struct elf_segment_map **pm; | |
1983 | struct elf_segment_map *m; | |
1984 | asection *last_hdr; | |
1985 | unsigned int phdr_index; | |
1986 | bfd_vma maxpagesize; | |
1987 | asection **hdrpp; | |
edf3fe48 ILT |
1988 | boolean phdr_in_section = true; |
1989 | boolean writable; | |
1990 | asection *dynsec; | |
fd0198f0 ILT |
1991 | |
1992 | if (elf_tdata (abfd)->segment_map != NULL) | |
1993 | return true; | |
1994 | ||
1995 | if (bfd_count_sections (abfd) == 0) | |
1996 | return true; | |
1997 | ||
1998 | /* Select the allocated sections, and sort them. */ | |
1999 | ||
58142f10 ILT |
2000 | sections = (asection **) bfd_malloc (bfd_count_sections (abfd) |
2001 | * sizeof (asection *)); | |
fd0198f0 | 2002 | if (sections == NULL) |
58142f10 | 2003 | goto error_return; |
ede4eed4 | 2004 | |
fd0198f0 ILT |
2005 | i = 0; |
2006 | for (s = abfd->sections; s != NULL; s = s->next) | |
2007 | { | |
2008 | if ((s->flags & SEC_ALLOC) != 0) | |
2009 | { | |
2010 | sections[i] = s; | |
2011 | ++i; | |
2012 | } | |
5fe14a9f | 2013 | } |
fd0198f0 ILT |
2014 | BFD_ASSERT (i <= bfd_count_sections (abfd)); |
2015 | count = i; | |
ede4eed4 | 2016 | |
fd0198f0 | 2017 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
ede4eed4 | 2018 | |
fd0198f0 | 2019 | /* Build the mapping. */ |
ede4eed4 | 2020 | |
fd0198f0 ILT |
2021 | mfirst = NULL; |
2022 | pm = &mfirst; | |
ede4eed4 | 2023 | |
fd0198f0 ILT |
2024 | /* If we have a .interp section, then create a PT_PHDR segment for |
2025 | the program headers and a PT_INTERP segment for the .interp | |
2026 | section. */ | |
2027 | s = bfd_get_section_by_name (abfd, ".interp"); | |
2028 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
2029 | { | |
2030 | m = ((struct elf_segment_map *) | |
2031 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
2032 | if (m == NULL) | |
a9713b91 | 2033 | goto error_return; |
fd0198f0 ILT |
2034 | m->next = NULL; |
2035 | m->p_type = PT_PHDR; | |
2036 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
2037 | m->p_flags = PF_R | PF_X; | |
2038 | m->p_flags_valid = 1; | |
6933148a | 2039 | m->includes_phdrs = 1; |
ede4eed4 | 2040 | |
fd0198f0 ILT |
2041 | *pm = m; |
2042 | pm = &m->next; | |
ede4eed4 | 2043 | |
fd0198f0 ILT |
2044 | m = ((struct elf_segment_map *) |
2045 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
2046 | if (m == NULL) | |
a9713b91 | 2047 | goto error_return; |
fd0198f0 ILT |
2048 | m->next = NULL; |
2049 | m->p_type = PT_INTERP; | |
2050 | m->count = 1; | |
2051 | m->sections[0] = s; | |
ede4eed4 | 2052 | |
fd0198f0 ILT |
2053 | *pm = m; |
2054 | pm = &m->next; | |
2055 | } | |
ede4eed4 | 2056 | |
fd0198f0 ILT |
2057 | /* Look through the sections. We put sections in the same program |
2058 | segment when the start of the second section can be placed within | |
2059 | a few bytes of the end of the first section. */ | |
2060 | last_hdr = NULL; | |
2061 | phdr_index = 0; | |
2062 | maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
edf3fe48 ILT |
2063 | writable = false; |
2064 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
2065 | if (dynsec != NULL | |
2066 | && (dynsec->flags & SEC_LOAD) == 0) | |
2067 | dynsec = NULL; | |
2068 | ||
7fc6a16a ILT |
2069 | /* Deal with -Ttext or something similar such that the first section |
2070 | is not adjacent to the program headers. This is an | |
2071 | approximation, since at this point we don't know exactly how many | |
2072 | program headers we will need. */ | |
2073 | if (count > 0) | |
2074 | { | |
2075 | bfd_size_type phdr_size; | |
2076 | ||
2077 | phdr_size = elf_tdata (abfd)->program_header_size; | |
2078 | if (phdr_size == 0) | |
2079 | phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr; | |
cdb88e87 ILT |
2080 | if ((abfd->flags & D_PAGED) == 0 |
2081 | || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) | |
7fc6a16a ILT |
2082 | phdr_in_section = false; |
2083 | } | |
edf3fe48 | 2084 | |
fd0198f0 | 2085 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
ede4eed4 | 2086 | { |
fd0198f0 | 2087 | asection *hdr; |
191d910c | 2088 | boolean new_segment; |
ede4eed4 | 2089 | |
fd0198f0 | 2090 | hdr = *hdrpp; |
ede4eed4 | 2091 | |
fd0198f0 | 2092 | /* See if this section and the last one will fit in the same |
191d910c ILT |
2093 | segment. */ |
2094 | ||
2095 | if (last_hdr == NULL) | |
2096 | { | |
2097 | /* If we don't have a segment yet, then we don't need a new | |
2098 | one (we build the last one after this loop). */ | |
2099 | new_segment = false; | |
2100 | } | |
2101 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
2102 | { | |
2103 | /* If this section has a different relation between the | |
2104 | virtual address and the load address, then we need a new | |
2105 | segment. */ | |
2106 | new_segment = true; | |
2107 | } | |
191d910c ILT |
2108 | else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) |
2109 | < hdr->lma) | |
2110 | { | |
2111 | /* If putting this section in this segment would force us to | |
2112 | skip a page in the segment, then we need a new segment. */ | |
2113 | new_segment = true; | |
2114 | } | |
f0c12b73 DE |
2115 | else if ((abfd->flags & D_PAGED) == 0) |
2116 | { | |
2117 | /* If the file is not demand paged, which means that we | |
2118 | don't require the sections to be correctly aligned in the | |
2119 | file, then there is no other reason for a new segment. */ | |
2120 | new_segment = false; | |
2121 | } | |
191d910c ILT |
2122 | else if ((last_hdr->flags & SEC_LOAD) == 0 |
2123 | && (hdr->flags & SEC_LOAD) != 0) | |
2124 | { | |
2125 | /* We don't want to put a loadable section after a | |
2126 | nonloadable section in the same segment. */ | |
2127 | new_segment = true; | |
2128 | } | |
2129 | else if (! writable | |
2130 | && (hdr->flags & SEC_READONLY) == 0 | |
2131 | && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) | |
2132 | == hdr->lma)) | |
2133 | { | |
2134 | /* We don't want to put a writable section in a read only | |
2135 | segment, unless they are on the same page in memory | |
2136 | anyhow. We already know that the last section does not | |
2137 | bring us past the current section on the page, so the | |
2138 | only case in which the new section is not on the same | |
2139 | page as the previous section is when the previous section | |
2140 | ends precisely on a page boundary. */ | |
2141 | new_segment = true; | |
2142 | } | |
2143 | else | |
2144 | { | |
2145 | /* Otherwise, we can use the same segment. */ | |
2146 | new_segment = false; | |
2147 | } | |
2148 | ||
2149 | if (! new_segment) | |
fd0198f0 | 2150 | { |
50bd50d4 MH |
2151 | if ((hdr->flags & SEC_READONLY) == 0) |
2152 | writable = true; | |
fd0198f0 ILT |
2153 | last_hdr = hdr; |
2154 | continue; | |
2155 | } | |
ede4eed4 | 2156 | |
191d910c ILT |
2157 | /* We need a new program segment. We must create a new program |
2158 | header holding all the sections from phdr_index until hdr. */ | |
ede4eed4 | 2159 | |
edf3fe48 | 2160 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section); |
fd0198f0 ILT |
2161 | if (m == NULL) |
2162 | goto error_return; | |
ede4eed4 | 2163 | |
fd0198f0 ILT |
2164 | *pm = m; |
2165 | pm = &m->next; | |
ede4eed4 | 2166 | |
edf3fe48 ILT |
2167 | if ((hdr->flags & SEC_READONLY) == 0) |
2168 | writable = true; | |
50bd50d4 MH |
2169 | else |
2170 | writable = false; | |
edf3fe48 | 2171 | |
fd0198f0 ILT |
2172 | last_hdr = hdr; |
2173 | phdr_index = i; | |
edf3fe48 | 2174 | phdr_in_section = false; |
ede4eed4 | 2175 | } |
fd0198f0 ILT |
2176 | |
2177 | /* Create a final PT_LOAD program segment. */ | |
2178 | if (last_hdr != NULL) | |
ede4eed4 | 2179 | { |
edf3fe48 | 2180 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section); |
fd0198f0 ILT |
2181 | if (m == NULL) |
2182 | goto error_return; | |
2183 | ||
2184 | *pm = m; | |
2185 | pm = &m->next; | |
ede4eed4 KR |
2186 | } |
2187 | ||
fd0198f0 | 2188 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
edf3fe48 | 2189 | if (dynsec != NULL) |
ede4eed4 | 2190 | { |
fd0198f0 ILT |
2191 | m = ((struct elf_segment_map *) |
2192 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
2193 | if (m == NULL) | |
a9713b91 | 2194 | goto error_return; |
fd0198f0 ILT |
2195 | m->next = NULL; |
2196 | m->p_type = PT_DYNAMIC; | |
2197 | m->count = 1; | |
edf3fe48 | 2198 | m->sections[0] = dynsec; |
ede4eed4 | 2199 | |
fd0198f0 ILT |
2200 | *pm = m; |
2201 | pm = &m->next; | |
ede4eed4 KR |
2202 | } |
2203 | ||
a66a61a0 ILT |
2204 | /* For each loadable .note section, add a PT_NOTE segment. We don't |
2205 | use bfd_get_section_by_name, because if we link together | |
2206 | nonloadable .note sections and loadable .note sections, we will | |
2207 | generate two .note sections in the output file. FIXME: Using | |
2208 | names for section types is bogus anyhow. */ | |
2209 | for (s = abfd->sections; s != NULL; s = s->next) | |
2210 | { | |
2211 | if ((s->flags & SEC_LOAD) != 0 | |
2212 | && strncmp (s->name, ".note", 5) == 0) | |
2213 | { | |
2214 | m = ((struct elf_segment_map *) | |
2215 | bfd_zalloc (abfd, sizeof (struct elf_segment_map))); | |
2216 | if (m == NULL) | |
2217 | goto error_return; | |
2218 | m->next = NULL; | |
2219 | m->p_type = PT_NOTE; | |
2220 | m->count = 1; | |
2221 | m->sections[0] = s; | |
2222 | ||
2223 | *pm = m; | |
2224 | pm = &m->next; | |
2225 | } | |
2226 | } | |
2227 | ||
fd0198f0 ILT |
2228 | free (sections); |
2229 | sections = NULL; | |
ae115e51 | 2230 | |
fd0198f0 ILT |
2231 | elf_tdata (abfd)->segment_map = mfirst; |
2232 | return true; | |
2233 | ||
2234 | error_return: | |
2235 | if (sections != NULL) | |
2236 | free (sections); | |
2237 | return false; | |
ede4eed4 KR |
2238 | } |
2239 | ||
fd0198f0 | 2240 | /* Sort sections by VMA. */ |
ede4eed4 | 2241 | |
fd0198f0 ILT |
2242 | static int |
2243 | elf_sort_sections (arg1, arg2) | |
2244 | const PTR arg1; | |
2245 | const PTR arg2; | |
ede4eed4 | 2246 | { |
fd0198f0 ILT |
2247 | const asection *sec1 = *(const asection **) arg1; |
2248 | const asection *sec2 = *(const asection **) arg2; | |
ede4eed4 | 2249 | |
fd0198f0 ILT |
2250 | if (sec1->vma < sec2->vma) |
2251 | return -1; | |
2252 | else if (sec1->vma > sec2->vma) | |
2253 | return 1; | |
ede4eed4 | 2254 | |
cdb88e87 ILT |
2255 | /* Sort by LMA. Normally the LMA and the VMA will be the same, and |
2256 | this will do nothing. */ | |
2257 | if (sec1->lma < sec2->lma) | |
2258 | return -1; | |
2259 | else if (sec1->lma > sec2->lma) | |
2260 | return 1; | |
2261 | ||
fd0198f0 | 2262 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ |
ede4eed4 | 2263 | |
fd0198f0 | 2264 | #define TOEND(x) (((x)->flags & SEC_LOAD) == 0) |
ede4eed4 | 2265 | |
fd0198f0 ILT |
2266 | if (TOEND (sec1)) |
2267 | if (TOEND (sec2)) | |
2268 | return sec1->target_index - sec2->target_index; | |
2269 | else | |
2270 | return 1; | |
ede4eed4 | 2271 | |
fd0198f0 ILT |
2272 | if (TOEND (sec2)) |
2273 | return -1; | |
ede4eed4 | 2274 | |
fd0198f0 | 2275 | #undef TOEND |
ede4eed4 | 2276 | |
fd0198f0 ILT |
2277 | /* Sort by size, to put zero sized sections before others at the |
2278 | same address. */ | |
ede4eed4 | 2279 | |
fd0198f0 ILT |
2280 | if (sec1->_raw_size < sec2->_raw_size) |
2281 | return -1; | |
2282 | if (sec1->_raw_size > sec2->_raw_size) | |
2283 | return 1; | |
ede4eed4 | 2284 | |
fd0198f0 ILT |
2285 | return sec1->target_index - sec2->target_index; |
2286 | } | |
ede4eed4 | 2287 | |
fd0198f0 ILT |
2288 | /* Assign file positions to the sections based on the mapping from |
2289 | sections to segments. This function also sets up some fields in | |
2290 | the file header, and writes out the program headers. */ | |
ede4eed4 | 2291 | |
fd0198f0 ILT |
2292 | static boolean |
2293 | assign_file_positions_for_segments (abfd) | |
2294 | bfd *abfd; | |
2295 | { | |
2296 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2297 | unsigned int count; | |
2298 | struct elf_segment_map *m; | |
2299 | unsigned int alloc; | |
2300 | Elf_Internal_Phdr *phdrs; | |
64f808f9 | 2301 | file_ptr off, voff; |
6933148a ILT |
2302 | bfd_vma filehdr_vaddr, filehdr_paddr; |
2303 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
fd0198f0 ILT |
2304 | Elf_Internal_Phdr *p; |
2305 | ||
2306 | if (elf_tdata (abfd)->segment_map == NULL) | |
2307 | { | |
2308 | if (! map_sections_to_segments (abfd)) | |
2309 | return false; | |
2310 | } | |
ede4eed4 | 2311 | |
5b3b9ff6 ILT |
2312 | if (bed->elf_backend_modify_segment_map) |
2313 | { | |
2314 | if (! (*bed->elf_backend_modify_segment_map) (abfd)) | |
2315 | return false; | |
2316 | } | |
2317 | ||
fd0198f0 ILT |
2318 | count = 0; |
2319 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
2320 | ++count; | |
ede4eed4 | 2321 | |
fd0198f0 ILT |
2322 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; |
2323 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
2324 | elf_elfheader (abfd)->e_phnum = count; | |
ede4eed4 | 2325 | |
fd0198f0 ILT |
2326 | if (count == 0) |
2327 | return true; | |
ede4eed4 | 2328 | |
fd0198f0 ILT |
2329 | /* If we already counted the number of program segments, make sure |
2330 | that we allocated enough space. This happens when SIZEOF_HEADERS | |
2331 | is used in a linker script. */ | |
2332 | alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr; | |
2333 | if (alloc != 0 && count > alloc) | |
2334 | { | |
2335 | ((*_bfd_error_handler) | |
2336 | ("%s: Not enough room for program headers (allocated %u, need %u)", | |
2337 | bfd_get_filename (abfd), alloc, count)); | |
2338 | bfd_set_error (bfd_error_bad_value); | |
2339 | return false; | |
ede4eed4 KR |
2340 | } |
2341 | ||
fd0198f0 ILT |
2342 | if (alloc == 0) |
2343 | alloc = count; | |
2344 | ||
2345 | phdrs = ((Elf_Internal_Phdr *) | |
2346 | bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr))); | |
2347 | if (phdrs == NULL) | |
a9713b91 | 2348 | return false; |
ede4eed4 | 2349 | |
fd0198f0 ILT |
2350 | off = bed->s->sizeof_ehdr; |
2351 | off += alloc * bed->s->sizeof_phdr; | |
ede4eed4 | 2352 | |
6933148a ILT |
2353 | filehdr_vaddr = 0; |
2354 | filehdr_paddr = 0; | |
2355 | phdrs_vaddr = 0; | |
2356 | phdrs_paddr = 0; | |
fd0198f0 ILT |
2357 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; |
2358 | m != NULL; | |
2359 | m = m->next, p++) | |
2360 | { | |
2361 | unsigned int i; | |
2362 | asection **secpp; | |
fd0198f0 | 2363 | |
3b950780 ILT |
2364 | /* If elf_segment_map is not from map_sections_to_segments, the |
2365 | sections may not be correctly ordered. */ | |
2366 | if (m->count > 0) | |
2367 | qsort (m->sections, (size_t) m->count, sizeof (asection *), | |
2368 | elf_sort_sections); | |
2369 | ||
fd0198f0 ILT |
2370 | p->p_type = m->p_type; |
2371 | ||
2372 | if (m->p_flags_valid) | |
2373 | p->p_flags = m->p_flags; | |
14899eb7 ILT |
2374 | else |
2375 | p->p_flags = 0; | |
fd0198f0 | 2376 | |
d49ddb85 ILT |
2377 | if (p->p_type == PT_LOAD |
2378 | && m->count > 0 | |
d7775b43 | 2379 | && (m->sections[0]->flags & SEC_ALLOC) != 0) |
cdb88e87 ILT |
2380 | { |
2381 | if ((abfd->flags & D_PAGED) != 0) | |
2382 | off += (m->sections[0]->vma - off) % bed->maxpagesize; | |
2383 | else | |
2384 | off += ((m->sections[0]->vma - off) | |
2385 | % (1 << bfd_get_section_alignment (abfd, m->sections[0]))); | |
2386 | } | |
44ef8897 | 2387 | |
fd0198f0 ILT |
2388 | if (m->count == 0) |
2389 | p->p_vaddr = 0; | |
2390 | else | |
2391 | p->p_vaddr = m->sections[0]->vma; | |
ede4eed4 | 2392 | |
fd0198f0 ILT |
2393 | if (m->p_paddr_valid) |
2394 | p->p_paddr = m->p_paddr; | |
2395 | else if (m->count == 0) | |
2396 | p->p_paddr = 0; | |
2397 | else | |
2398 | p->p_paddr = m->sections[0]->lma; | |
2399 | ||
cdb88e87 ILT |
2400 | if (p->p_type == PT_LOAD |
2401 | && (abfd->flags & D_PAGED) != 0) | |
fd0198f0 ILT |
2402 | p->p_align = bed->maxpagesize; |
2403 | else if (m->count == 0) | |
2404 | p->p_align = bed->s->file_align; | |
2405 | else | |
2406 | p->p_align = 0; | |
2407 | ||
6933148a | 2408 | p->p_offset = 0; |
fd0198f0 ILT |
2409 | p->p_filesz = 0; |
2410 | p->p_memsz = 0; | |
2411 | ||
6933148a | 2412 | if (m->includes_filehdr) |
ede4eed4 | 2413 | { |
14899eb7 ILT |
2414 | if (! m->p_flags_valid) |
2415 | p->p_flags |= PF_R; | |
6933148a ILT |
2416 | p->p_offset = 0; |
2417 | p->p_filesz = bed->s->sizeof_ehdr; | |
2418 | p->p_memsz = bed->s->sizeof_ehdr; | |
2419 | if (m->count > 0) | |
2420 | { | |
2421 | BFD_ASSERT (p->p_type == PT_LOAD); | |
2422 | p->p_vaddr -= off; | |
2423 | if (! m->p_paddr_valid) | |
2424 | p->p_paddr -= off; | |
2425 | } | |
2426 | if (p->p_type == PT_LOAD) | |
2427 | { | |
2428 | filehdr_vaddr = p->p_vaddr; | |
2429 | filehdr_paddr = p->p_paddr; | |
2430 | } | |
2431 | } | |
fd0198f0 | 2432 | |
6933148a ILT |
2433 | if (m->includes_phdrs) |
2434 | { | |
14899eb7 ILT |
2435 | if (! m->p_flags_valid) |
2436 | p->p_flags |= PF_R; | |
6933148a | 2437 | if (m->includes_filehdr) |
fd0198f0 | 2438 | { |
6933148a | 2439 | if (p->p_type == PT_LOAD) |
fd0198f0 | 2440 | { |
6933148a ILT |
2441 | phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr; |
2442 | phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr; | |
fd0198f0 | 2443 | } |
6933148a ILT |
2444 | } |
2445 | else | |
2446 | { | |
2447 | p->p_offset = bed->s->sizeof_ehdr; | |
2448 | if (m->count > 0) | |
2449 | { | |
2450 | BFD_ASSERT (p->p_type == PT_LOAD); | |
2451 | p->p_vaddr -= off - p->p_offset; | |
2452 | if (! m->p_paddr_valid) | |
2453 | p->p_paddr -= off - p->p_offset; | |
2454 | } | |
2455 | if (p->p_type == PT_LOAD) | |
fd0198f0 | 2456 | { |
6933148a ILT |
2457 | phdrs_vaddr = p->p_vaddr; |
2458 | phdrs_paddr = p->p_paddr; | |
fd0198f0 | 2459 | } |
6933148a ILT |
2460 | } |
2461 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
2462 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2463 | } | |
2464 | ||
2465 | if (p->p_type == PT_LOAD) | |
2466 | { | |
2467 | if (! m->includes_filehdr && ! m->includes_phdrs) | |
2468 | p->p_offset = off; | |
2469 | else | |
2470 | { | |
2471 | file_ptr adjust; | |
fd0198f0 | 2472 | |
6933148a ILT |
2473 | adjust = off - (p->p_offset + p->p_filesz); |
2474 | p->p_filesz += adjust; | |
2475 | p->p_memsz += adjust; | |
fd0198f0 | 2476 | } |
ede4eed4 KR |
2477 | } |
2478 | ||
64f808f9 | 2479 | voff = off; |
fd0198f0 | 2480 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
ede4eed4 | 2481 | { |
fd0198f0 ILT |
2482 | asection *sec; |
2483 | flagword flags; | |
2484 | bfd_size_type align; | |
2485 | ||
2486 | sec = *secpp; | |
2487 | flags = sec->flags; | |
cdb88e87 | 2488 | align = 1 << bfd_get_section_alignment (abfd, sec); |
fd0198f0 ILT |
2489 | |
2490 | if (p->p_type == PT_LOAD) | |
2491 | { | |
2492 | bfd_vma adjust; | |
2493 | ||
2494 | /* The section VMA must equal the file position modulo | |
2495 | the page size. */ | |
09609415 | 2496 | if ((flags & SEC_ALLOC) != 0) |
fd0198f0 | 2497 | { |
cdb88e87 ILT |
2498 | if ((abfd->flags & D_PAGED) != 0) |
2499 | adjust = (sec->vma - voff) % bed->maxpagesize; | |
2500 | else | |
2501 | adjust = (sec->vma - voff) % align; | |
d49ddb85 ILT |
2502 | if (adjust != 0) |
2503 | { | |
2504 | if (i == 0) | |
2505 | abort (); | |
2506 | p->p_memsz += adjust; | |
19bfbcbe | 2507 | off += adjust; |
64f808f9 | 2508 | voff += adjust; |
d49ddb85 | 2509 | if ((flags & SEC_LOAD) != 0) |
19bfbcbe | 2510 | p->p_filesz += adjust; |
d49ddb85 | 2511 | } |
fd0198f0 ILT |
2512 | } |
2513 | ||
2514 | sec->filepos = off; | |
2515 | ||
2516 | if ((flags & SEC_LOAD) != 0) | |
2517 | off += sec->_raw_size; | |
64f808f9 ILT |
2518 | if ((flags & SEC_ALLOC) != 0) |
2519 | voff += sec->_raw_size; | |
fd0198f0 ILT |
2520 | } |
2521 | ||
2522 | p->p_memsz += sec->_raw_size; | |
2523 | ||
2524 | if ((flags & SEC_LOAD) != 0) | |
2525 | p->p_filesz += sec->_raw_size; | |
2526 | ||
fd0198f0 ILT |
2527 | if (align > p->p_align) |
2528 | p->p_align = align; | |
2529 | ||
2530 | if (! m->p_flags_valid) | |
2531 | { | |
14899eb7 | 2532 | p->p_flags |= PF_R; |
fd0198f0 ILT |
2533 | if ((flags & SEC_CODE) != 0) |
2534 | p->p_flags |= PF_X; | |
2535 | if ((flags & SEC_READONLY) == 0) | |
2536 | p->p_flags |= PF_W; | |
2537 | } | |
ede4eed4 | 2538 | } |
fd0198f0 | 2539 | } |
ede4eed4 | 2540 | |
fd0198f0 ILT |
2541 | /* Now that we have set the section file positions, we can set up |
2542 | the file positions for the non PT_LOAD segments. */ | |
2543 | for (m = elf_tdata (abfd)->segment_map, p = phdrs; | |
2544 | m != NULL; | |
2545 | m = m->next, p++) | |
2546 | { | |
2547 | if (p->p_type != PT_LOAD && m->count > 0) | |
ede4eed4 | 2548 | { |
6933148a ILT |
2549 | BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs); |
2550 | p->p_offset = m->sections[0]->filepos; | |
2551 | } | |
2552 | if (m->count == 0) | |
2553 | { | |
2554 | if (m->includes_filehdr) | |
2555 | { | |
2556 | p->p_vaddr = filehdr_vaddr; | |
2557 | if (! m->p_paddr_valid) | |
2558 | p->p_paddr = filehdr_paddr; | |
2559 | } | |
2560 | else if (m->includes_phdrs) | |
2561 | { | |
2562 | p->p_vaddr = phdrs_vaddr; | |
2563 | if (! m->p_paddr_valid) | |
2564 | p->p_paddr = phdrs_paddr; | |
2565 | } | |
ede4eed4 | 2566 | } |
ede4eed4 KR |
2567 | } |
2568 | ||
fd0198f0 ILT |
2569 | /* Clear out any program headers we allocated but did not use. */ |
2570 | for (; count < alloc; count++, p++) | |
ede4eed4 | 2571 | { |
fd0198f0 ILT |
2572 | memset (p, 0, sizeof *p); |
2573 | p->p_type = PT_NULL; | |
ede4eed4 KR |
2574 | } |
2575 | ||
fd0198f0 | 2576 | elf_tdata (abfd)->phdr = phdrs; |
ede4eed4 | 2577 | |
fd0198f0 | 2578 | elf_tdata (abfd)->next_file_pos = off; |
ede4eed4 | 2579 | |
fd0198f0 ILT |
2580 | /* Write out the program headers. */ |
2581 | if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0 | |
2582 | || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0) | |
2583 | return false; | |
2584 | ||
2585 | return true; | |
2586 | } | |
2587 | ||
2588 | /* Get the size of the program header. | |
2589 | ||
2590 | If this is called by the linker before any of the section VMA's are set, it | |
2591 | can't calculate the correct value for a strange memory layout. This only | |
2592 | happens when SIZEOF_HEADERS is used in a linker script. In this case, | |
2593 | SORTED_HDRS is NULL and we assume the normal scenario of one text and one | |
2594 | data segment (exclusive of .interp and .dynamic). | |
2595 | ||
2596 | ??? User written scripts must either not use SIZEOF_HEADERS, or assume there | |
2597 | will be two segments. */ | |
2598 | ||
2599 | static bfd_size_type | |
2600 | get_program_header_size (abfd) | |
2601 | bfd *abfd; | |
2602 | { | |
2603 | size_t segs; | |
2604 | asection *s; | |
2605 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2606 | ||
2607 | /* We can't return a different result each time we're called. */ | |
2608 | if (elf_tdata (abfd)->program_header_size != 0) | |
2609 | return elf_tdata (abfd)->program_header_size; | |
ae115e51 | 2610 | |
3b950780 ILT |
2611 | if (elf_tdata (abfd)->segment_map != NULL) |
2612 | { | |
2613 | struct elf_segment_map *m; | |
2614 | ||
2615 | segs = 0; | |
2616 | for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) | |
2617 | ++segs; | |
2618 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; | |
2619 | return elf_tdata (abfd)->program_header_size; | |
2620 | } | |
2621 | ||
fd0198f0 ILT |
2622 | /* Assume we will need exactly two PT_LOAD segments: one for text |
2623 | and one for data. */ | |
2624 | segs = 2; | |
2625 | ||
2626 | s = bfd_get_section_by_name (abfd, ".interp"); | |
2627 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
ede4eed4 | 2628 | { |
fd0198f0 ILT |
2629 | /* If we have a loadable interpreter section, we need a |
2630 | PT_INTERP segment. In this case, assume we also need a | |
2631 | PT_PHDR segment, although that may not be true for all | |
2632 | targets. */ | |
2633 | segs += 2; | |
ede4eed4 KR |
2634 | } |
2635 | ||
fd0198f0 | 2636 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) |
ede4eed4 | 2637 | { |
fd0198f0 ILT |
2638 | /* We need a PT_DYNAMIC segment. */ |
2639 | ++segs; | |
ede4eed4 | 2640 | } |
ede4eed4 | 2641 | |
a66a61a0 ILT |
2642 | for (s = abfd->sections; s != NULL; s = s->next) |
2643 | { | |
2644 | if ((s->flags & SEC_LOAD) != 0 | |
2645 | && strncmp (s->name, ".note", 5) == 0) | |
2646 | { | |
2647 | /* We need a PT_NOTE segment. */ | |
2648 | ++segs; | |
2649 | } | |
2650 | } | |
2651 | ||
fd0198f0 | 2652 | /* Let the backend count up any program headers it might need. */ |
5b3b9ff6 ILT |
2653 | if (bed->elf_backend_additional_program_headers) |
2654 | { | |
2655 | int a; | |
2656 | ||
2657 | a = (*bed->elf_backend_additional_program_headers) (abfd); | |
2658 | if (a == -1) | |
2659 | abort (); | |
2660 | segs += a; | |
2661 | } | |
ede4eed4 | 2662 | |
fd0198f0 ILT |
2663 | elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; |
2664 | return elf_tdata (abfd)->program_header_size; | |
ede4eed4 KR |
2665 | } |
2666 | ||
2667 | /* Work out the file positions of all the sections. This is called by | |
2668 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
2669 | VMAs must be known before this is called. | |
2670 | ||
2671 | We do not consider reloc sections at this point, unless they form | |
2672 | part of the loadable image. Reloc sections are assigned file | |
2673 | positions in assign_file_positions_for_relocs, which is called by | |
2674 | write_object_contents and final_link. | |
2675 | ||
fd0198f0 | 2676 | We also don't set the positions of the .symtab and .strtab here. */ |
ede4eed4 KR |
2677 | |
2678 | static boolean | |
fd0198f0 | 2679 | assign_file_positions_except_relocs (abfd) |
ede4eed4 | 2680 | bfd *abfd; |
ede4eed4 KR |
2681 | { |
2682 | struct elf_obj_tdata * const tdata = elf_tdata (abfd); | |
2683 | Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); | |
2684 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
2685 | file_ptr off; | |
2686 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2687 | ||
ede4eed4 KR |
2688 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) |
2689 | { | |
2690 | Elf_Internal_Shdr **hdrpp; | |
2691 | unsigned int i; | |
2692 | ||
fd0198f0 ILT |
2693 | /* Start after the ELF header. */ |
2694 | off = i_ehdrp->e_ehsize; | |
2695 | ||
ede4eed4 KR |
2696 | /* We are not creating an executable, which means that we are |
2697 | not creating a program header, and that the actual order of | |
2698 | the sections in the file is unimportant. */ | |
2699 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
2700 | { | |
2701 | Elf_Internal_Shdr *hdr; | |
2702 | ||
2703 | hdr = *hdrpp; | |
2704 | if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
2705 | { | |
2706 | hdr->sh_offset = -1; | |
2707 | continue; | |
2708 | } | |
fd0198f0 ILT |
2709 | if (i == tdata->symtab_section |
2710 | || i == tdata->strtab_section) | |
ede4eed4 KR |
2711 | { |
2712 | hdr->sh_offset = -1; | |
2713 | continue; | |
2714 | } | |
2715 | ||
5fe14a9f | 2716 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); |
ede4eed4 KR |
2717 | } |
2718 | } | |
2719 | else | |
2720 | { | |
ede4eed4 | 2721 | unsigned int i; |
fd0198f0 | 2722 | Elf_Internal_Shdr **hdrpp; |
ede4eed4 | 2723 | |
fd0198f0 ILT |
2724 | /* Assign file positions for the loaded sections based on the |
2725 | assignment of sections to segments. */ | |
2726 | if (! assign_file_positions_for_segments (abfd)) | |
ede4eed4 KR |
2727 | return false; |
2728 | ||
fd0198f0 ILT |
2729 | /* Assign file positions for the other sections. */ |
2730 | ||
2731 | off = elf_tdata (abfd)->next_file_pos; | |
2732 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
ede4eed4 KR |
2733 | { |
2734 | Elf_Internal_Shdr *hdr; | |
2735 | ||
2736 | hdr = *hdrpp; | |
fd0198f0 ILT |
2737 | if (hdr->bfd_section != NULL |
2738 | && hdr->bfd_section->filepos != 0) | |
2739 | hdr->sh_offset = hdr->bfd_section->filepos; | |
2740 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
ede4eed4 | 2741 | { |
fd0198f0 ILT |
2742 | ((*_bfd_error_handler) |
2743 | ("%s: warning: allocated section `%s' not in segment", | |
2744 | bfd_get_filename (abfd), | |
2745 | (hdr->bfd_section == NULL | |
2746 | ? "*unknown*" | |
2747 | : hdr->bfd_section->name))); | |
cdb88e87 ILT |
2748 | if ((abfd->flags & D_PAGED) != 0) |
2749 | off += (hdr->sh_addr - off) % bed->maxpagesize; | |
2750 | else | |
2751 | off += (hdr->sh_addr - off) % hdr->sh_addralign; | |
5fe14a9f ILT |
2752 | off = _bfd_elf_assign_file_position_for_section (hdr, off, |
2753 | false); | |
ede4eed4 | 2754 | } |
fd0198f0 ILT |
2755 | else if (hdr->sh_type == SHT_REL |
2756 | || hdr->sh_type == SHT_RELA | |
2757 | || hdr == i_shdrpp[tdata->symtab_section] | |
2758 | || hdr == i_shdrpp[tdata->strtab_section]) | |
2759 | hdr->sh_offset = -1; | |
2760 | else | |
2761 | off = _bfd_elf_assign_file_position_for_section (hdr, off, true); | |
2762 | } | |
ede4eed4 KR |
2763 | } |
2764 | ||
2765 | /* Place the section headers. */ | |
2766 | off = align_file_position (off, bed->s->file_align); | |
2767 | i_ehdrp->e_shoff = off; | |
2768 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
2769 | ||
2770 | elf_tdata (abfd)->next_file_pos = off; | |
2771 | ||
2772 | return true; | |
2773 | } | |
2774 | ||
ede4eed4 KR |
2775 | static boolean |
2776 | prep_headers (abfd) | |
2777 | bfd *abfd; | |
2778 | { | |
2779 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
2780 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ | |
2781 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
2782 | int count; | |
2783 | struct bfd_strtab_hash *shstrtab; | |
2784 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2785 | ||
2786 | i_ehdrp = elf_elfheader (abfd); | |
2787 | i_shdrp = elf_elfsections (abfd); | |
2788 | ||
2789 | shstrtab = _bfd_elf_stringtab_init (); | |
2790 | if (shstrtab == NULL) | |
2791 | return false; | |
2792 | ||
2793 | elf_shstrtab (abfd) = shstrtab; | |
2794 | ||
2795 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
2796 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
2797 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
2798 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
2799 | ||
2800 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
2801 | i_ehdrp->e_ident[EI_DATA] = | |
86587dd4 | 2802 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; |
ede4eed4 KR |
2803 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; |
2804 | ||
2805 | for (count = EI_PAD; count < EI_NIDENT; count++) | |
2806 | i_ehdrp->e_ident[count] = 0; | |
2807 | ||
2808 | if ((abfd->flags & DYNAMIC) != 0) | |
2809 | i_ehdrp->e_type = ET_DYN; | |
2810 | else if ((abfd->flags & EXEC_P) != 0) | |
2811 | i_ehdrp->e_type = ET_EXEC; | |
2812 | else | |
2813 | i_ehdrp->e_type = ET_REL; | |
2814 | ||
2815 | switch (bfd_get_arch (abfd)) | |
2816 | { | |
2817 | case bfd_arch_unknown: | |
2818 | i_ehdrp->e_machine = EM_NONE; | |
2819 | break; | |
2820 | case bfd_arch_sparc: | |
2821 | if (bed->s->arch_size == 64) | |
2822 | i_ehdrp->e_machine = EM_SPARC64; | |
2823 | else | |
2824 | i_ehdrp->e_machine = EM_SPARC; | |
2825 | break; | |
2826 | case bfd_arch_i386: | |
2827 | i_ehdrp->e_machine = EM_386; | |
2828 | break; | |
2829 | case bfd_arch_m68k: | |
2830 | i_ehdrp->e_machine = EM_68K; | |
2831 | break; | |
2832 | case bfd_arch_m88k: | |
2833 | i_ehdrp->e_machine = EM_88K; | |
2834 | break; | |
2835 | case bfd_arch_i860: | |
2836 | i_ehdrp->e_machine = EM_860; | |
2837 | break; | |
2838 | case bfd_arch_mips: /* MIPS Rxxxx */ | |
2839 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
2840 | break; | |
2841 | case bfd_arch_hppa: | |
2842 | i_ehdrp->e_machine = EM_PARISC; | |
2843 | break; | |
2844 | case bfd_arch_powerpc: | |
2845 | i_ehdrp->e_machine = EM_PPC; | |
2846 | break; | |
50bd50d4 MH |
2847 | case bfd_arch_alpha: |
2848 | i_ehdrp->e_machine = EM_ALPHA; | |
2849 | break; | |
f0c12b73 DE |
2850 | case bfd_arch_sh: |
2851 | i_ehdrp->e_machine = EM_SH; | |
2852 | break; | |
50bd50d4 MH |
2853 | case bfd_arch_d10v: |
2854 | i_ehdrp->e_machine = EM_CYGNUS_D10V; | |
2855 | break; | |
fd8d7c31 MH |
2856 | /* start-sanitize-d30v */ |
2857 | case bfd_arch_d30v: | |
2858 | i_ehdrp->e_machine = EM_CYGNUS_D30V; | |
2859 | break; | |
2860 | /* end-sanitize-d30v */ | |
f0c12b73 DE |
2861 | /* start-sanitize-v850 */ |
2862 | case bfd_arch_v850: | |
2863 | i_ehdrp->e_machine = EM_CYGNUS_V850; | |
2864 | break; | |
2865 | /* end-sanitize-v850 */ | |
ede4eed4 KR |
2866 | /* start-sanitize-arc */ |
2867 | case bfd_arch_arc: | |
2868 | i_ehdrp->e_machine = EM_CYGNUS_ARC; | |
2869 | break; | |
2870 | /* end-sanitize-arc */ | |
f0c12b73 DE |
2871 | /* start-sanitize-m32r */ |
2872 | case bfd_arch_m32r: | |
2873 | i_ehdrp->e_machine = EM_CYGNUS_M32R; | |
2874 | break; | |
2875 | /* end-sanitize-m32r */ | |
80be821d ILT |
2876 | case bfd_arch_mn10200: |
2877 | i_ehdrp->e_machine = EM_CYGNUS_MN10200; | |
2878 | break; | |
2879 | case bfd_arch_mn10300: | |
2880 | i_ehdrp->e_machine = EM_CYGNUS_MN10300; | |
efc2b064 | 2881 | break; |
ede4eed4 KR |
2882 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ |
2883 | default: | |
2884 | i_ehdrp->e_machine = EM_NONE; | |
2885 | } | |
2886 | i_ehdrp->e_version = bed->s->ev_current; | |
2887 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
2888 | ||
2889 | /* no program header, for now. */ | |
2890 | i_ehdrp->e_phoff = 0; | |
2891 | i_ehdrp->e_phentsize = 0; | |
2892 | i_ehdrp->e_phnum = 0; | |
2893 | ||
2894 | /* each bfd section is section header entry */ | |
2895 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
2896 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
2897 | ||
2898 | /* if we're building an executable, we'll need a program header table */ | |
2899 | if (abfd->flags & EXEC_P) | |
2900 | { | |
2901 | /* it all happens later */ | |
2902 | #if 0 | |
2903 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
2904 | ||
2905 | /* elf_build_phdrs() returns a (NULL-terminated) array of | |
2906 | Elf_Internal_Phdrs */ | |
2907 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
2908 | i_ehdrp->e_phoff = outbase; | |
2909 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
2910 | #endif | |
2911 | } | |
2912 | else | |
2913 | { | |
2914 | i_ehdrp->e_phentsize = 0; | |
2915 | i_phdrp = 0; | |
2916 | i_ehdrp->e_phoff = 0; | |
2917 | } | |
2918 | ||
2919 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
2920 | (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false); | |
2921 | elf_tdata (abfd)->strtab_hdr.sh_name = | |
2922 | (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false); | |
2923 | elf_tdata (abfd)->shstrtab_hdr.sh_name = | |
2924 | (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false); | |
2925 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2926 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2927 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
2928 | return false; | |
2929 | ||
2930 | return true; | |
2931 | } | |
2932 | ||
2933 | /* Assign file positions for all the reloc sections which are not part | |
2934 | of the loadable file image. */ | |
2935 | ||
2936 | void | |
2937 | _bfd_elf_assign_file_positions_for_relocs (abfd) | |
2938 | bfd *abfd; | |
2939 | { | |
2940 | file_ptr off; | |
2941 | unsigned int i; | |
2942 | Elf_Internal_Shdr **shdrpp; | |
2943 | ||
2944 | off = elf_tdata (abfd)->next_file_pos; | |
2945 | ||
2946 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; | |
2947 | i < elf_elfheader (abfd)->e_shnum; | |
2948 | i++, shdrpp++) | |
2949 | { | |
2950 | Elf_Internal_Shdr *shdrp; | |
2951 | ||
2952 | shdrp = *shdrpp; | |
2953 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
2954 | && shdrp->sh_offset == -1) | |
5fe14a9f | 2955 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); |
ede4eed4 KR |
2956 | } |
2957 | ||
2958 | elf_tdata (abfd)->next_file_pos = off; | |
2959 | } | |
2960 | ||
2961 | boolean | |
2962 | _bfd_elf_write_object_contents (abfd) | |
2963 | bfd *abfd; | |
2964 | { | |
2965 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2966 | Elf_Internal_Ehdr *i_ehdrp; | |
2967 | Elf_Internal_Shdr **i_shdrp; | |
2968 | boolean failed; | |
2969 | unsigned int count; | |
2970 | ||
2971 | if (! abfd->output_has_begun | |
2972 | && ! _bfd_elf_compute_section_file_positions (abfd, | |
2973 | (struct bfd_link_info *) NULL)) | |
2974 | return false; | |
2975 | ||
2976 | i_shdrp = elf_elfsections (abfd); | |
2977 | i_ehdrp = elf_elfheader (abfd); | |
2978 | ||
2979 | failed = false; | |
2980 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); | |
2981 | if (failed) | |
2982 | return false; | |
2983 | _bfd_elf_assign_file_positions_for_relocs (abfd); | |
2984 | ||
2985 | /* After writing the headers, we need to write the sections too... */ | |
2986 | for (count = 1; count < i_ehdrp->e_shnum; count++) | |
2987 | { | |
2988 | if (bed->elf_backend_section_processing) | |
2989 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
2990 | if (i_shdrp[count]->contents) | |
2991 | { | |
2992 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 | |
2993 | || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, | |
2994 | 1, abfd) | |
2995 | != i_shdrp[count]->sh_size)) | |
2996 | return false; | |
2997 | } | |
2998 | } | |
2999 | ||
3000 | /* Write out the section header names. */ | |
3001 | if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
3002 | || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd))) | |
3003 | return false; | |
3004 | ||
3005 | if (bed->elf_backend_final_write_processing) | |
3006 | (*bed->elf_backend_final_write_processing) (abfd, | |
3007 | elf_tdata (abfd)->linker); | |
3008 | ||
3009 | return bed->s->write_shdrs_and_ehdr (abfd); | |
3010 | } | |
3011 | ||
3012 | /* given a section, search the header to find them... */ | |
3013 | int | |
3014 | _bfd_elf_section_from_bfd_section (abfd, asect) | |
3015 | bfd *abfd; | |
3016 | struct sec *asect; | |
3017 | { | |
3018 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3019 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); | |
3020 | int index; | |
3021 | Elf_Internal_Shdr *hdr; | |
3022 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
3023 | ||
3024 | for (index = 0; index < maxindex; index++) | |
3025 | { | |
3026 | hdr = i_shdrp[index]; | |
3027 | if (hdr->bfd_section == asect) | |
3028 | return index; | |
3029 | } | |
3030 | ||
3031 | if (bed->elf_backend_section_from_bfd_section) | |
3032 | { | |
3033 | for (index = 0; index < maxindex; index++) | |
3034 | { | |
3035 | int retval; | |
3036 | ||
3037 | hdr = i_shdrp[index]; | |
3038 | retval = index; | |
3039 | if ((*bed->elf_backend_section_from_bfd_section) | |
3040 | (abfd, hdr, asect, &retval)) | |
3041 | return retval; | |
3042 | } | |
3043 | } | |
3044 | ||
3045 | if (bfd_is_abs_section (asect)) | |
3046 | return SHN_ABS; | |
3047 | if (bfd_is_com_section (asect)) | |
3048 | return SHN_COMMON; | |
3049 | if (bfd_is_und_section (asect)) | |
3050 | return SHN_UNDEF; | |
3051 | ||
3052 | return -1; | |
3053 | } | |
3054 | ||
cb84f028 ILT |
3055 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 |
3056 | on error. */ | |
3057 | ||
3058 | int | |
ede4eed4 KR |
3059 | _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) |
3060 | bfd *abfd; | |
7fc6a16a | 3061 | asymbol **asym_ptr_ptr; |
ede4eed4 | 3062 | { |
7fc6a16a | 3063 | asymbol *asym_ptr = *asym_ptr_ptr; |
ede4eed4 KR |
3064 | int idx; |
3065 | flagword flags = asym_ptr->flags; | |
3066 | ||
3067 | /* When gas creates relocations against local labels, it creates its | |
3068 | own symbol for the section, but does put the symbol into the | |
3069 | symbol chain, so udata is 0. When the linker is generating | |
3070 | relocatable output, this section symbol may be for one of the | |
3071 | input sections rather than the output section. */ | |
3072 | if (asym_ptr->udata.i == 0 | |
3073 | && (flags & BSF_SECTION_SYM) | |
3074 | && asym_ptr->section) | |
3075 | { | |
3076 | int indx; | |
3077 | ||
3078 | if (asym_ptr->section->output_section != NULL) | |
3079 | indx = asym_ptr->section->output_section->index; | |
3080 | else | |
3081 | indx = asym_ptr->section->index; | |
3082 | if (elf_section_syms (abfd)[indx]) | |
3083 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; | |
3084 | } | |
3085 | ||
3086 | idx = asym_ptr->udata.i; | |
cb84f028 ILT |
3087 | |
3088 | if (idx == 0) | |
3089 | { | |
3090 | /* This case can occur when using --strip-symbol on a symbol | |
3091 | which is used in a relocation entry. */ | |
3092 | (*_bfd_error_handler) | |
3093 | ("%s: symbol `%s' required but not present", | |
3094 | bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr)); | |
3095 | bfd_set_error (bfd_error_no_symbols); | |
3096 | return -1; | |
3097 | } | |
ede4eed4 KR |
3098 | |
3099 | #if DEBUG & 4 | |
3100 | { | |
3101 | fprintf (stderr, | |
3102 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", | |
cb84f028 ILT |
3103 | (long) asym_ptr, asym_ptr->name, idx, flags, |
3104 | elf_symbol_flags (flags)); | |
ede4eed4 KR |
3105 | fflush (stderr); |
3106 | } | |
3107 | #endif | |
3108 | ||
3109 | return idx; | |
3110 | } | |
3111 | ||
3dbf33ee ILT |
3112 | /* Copy private BFD data. This copies any program header information. */ |
3113 | ||
3114 | static boolean | |
3115 | copy_private_bfd_data (ibfd, obfd) | |
3116 | bfd *ibfd; | |
3117 | bfd *obfd; | |
3118 | { | |
6933148a | 3119 | Elf_Internal_Ehdr *iehdr; |
3dbf33ee ILT |
3120 | struct elf_segment_map *mfirst; |
3121 | struct elf_segment_map **pm; | |
3122 | Elf_Internal_Phdr *p; | |
3123 | unsigned int i, c; | |
3124 | ||
3125 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
3126 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
3127 | return true; | |
3128 | ||
3129 | if (elf_tdata (ibfd)->phdr == NULL) | |
3130 | return true; | |
3131 | ||
6933148a ILT |
3132 | iehdr = elf_elfheader (ibfd); |
3133 | ||
3dbf33ee ILT |
3134 | mfirst = NULL; |
3135 | pm = &mfirst; | |
3136 | ||
3137 | c = elf_elfheader (ibfd)->e_phnum; | |
3138 | for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++) | |
3139 | { | |
3dbf33ee | 3140 | unsigned int csecs; |
6933148a ILT |
3141 | asection *s; |
3142 | struct elf_segment_map *m; | |
3143 | unsigned int isec; | |
3dbf33ee ILT |
3144 | |
3145 | csecs = 0; | |
3dbf33ee | 3146 | |
6933148a ILT |
3147 | /* The complicated case when p_vaddr is 0 is to handle the |
3148 | Solaris linker, which generates a PT_INTERP section with | |
3149 | p_vaddr and p_memsz set to 0. */ | |
3150 | for (s = ibfd->sections; s != NULL; s = s->next) | |
3151 | if (((s->vma >= p->p_vaddr | |
3152 | && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz | |
3153 | || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz)) | |
3154 | || (p->p_vaddr == 0 | |
3155 | && p->p_filesz > 0 | |
3156 | && (s->flags & SEC_HAS_CONTENTS) != 0 | |
3157 | && (bfd_vma) s->filepos >= p->p_offset | |
3158 | && ((bfd_vma) s->filepos + s->_raw_size | |
3159 | <= p->p_offset + p->p_filesz))) | |
86587dd4 | 3160 | && (s->flags & SEC_ALLOC) != 0 |
6933148a ILT |
3161 | && s->output_section != NULL) |
3162 | ++csecs; | |
3dbf33ee ILT |
3163 | |
3164 | m = ((struct elf_segment_map *) | |
3165 | bfd_alloc (obfd, | |
3166 | (sizeof (struct elf_segment_map) | |
3167 | + (csecs - 1) * sizeof (asection *)))); | |
3168 | if (m == NULL) | |
a9713b91 | 3169 | return false; |
3dbf33ee ILT |
3170 | |
3171 | m->next = NULL; | |
3172 | m->p_type = p->p_type; | |
3173 | m->p_flags = p->p_flags; | |
3174 | m->p_flags_valid = 1; | |
3175 | m->p_paddr = p->p_paddr; | |
3176 | m->p_paddr_valid = 1; | |
3177 | ||
6933148a ILT |
3178 | m->includes_filehdr = (p->p_offset == 0 |
3179 | && p->p_filesz >= iehdr->e_ehsize); | |
3180 | ||
3181 | m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff | |
3182 | && (p->p_offset + p->p_filesz | |
3183 | >= ((bfd_vma) iehdr->e_phoff | |
3184 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
3dbf33ee | 3185 | |
6933148a ILT |
3186 | isec = 0; |
3187 | for (s = ibfd->sections; s != NULL; s = s->next) | |
3188 | { | |
3189 | if (((s->vma >= p->p_vaddr | |
3190 | && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz | |
3191 | || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz)) | |
3192 | || (p->p_vaddr == 0 | |
3193 | && p->p_filesz > 0 | |
3194 | && (s->flags & SEC_HAS_CONTENTS) != 0 | |
3195 | && (bfd_vma) s->filepos >= p->p_offset | |
3196 | && ((bfd_vma) s->filepos + s->_raw_size | |
3197 | <= p->p_offset + p->p_filesz))) | |
86587dd4 | 3198 | && (s->flags & SEC_ALLOC) != 0 |
6933148a | 3199 | && s->output_section != NULL) |
3dbf33ee | 3200 | { |
6933148a ILT |
3201 | m->sections[isec] = s->output_section; |
3202 | ++isec; | |
3dbf33ee | 3203 | } |
3dbf33ee | 3204 | } |
6933148a | 3205 | BFD_ASSERT (isec == csecs); |
6933148a | 3206 | m->count = csecs; |
3dbf33ee ILT |
3207 | |
3208 | *pm = m; | |
3209 | pm = &m->next; | |
3210 | } | |
3211 | ||
3212 | elf_tdata (obfd)->segment_map = mfirst; | |
3213 | ||
3214 | return true; | |
3215 | } | |
3216 | ||
fd0198f0 ILT |
3217 | /* Copy private section information. This copies over the entsize |
3218 | field, and sometimes the info field. */ | |
3219 | ||
3220 | boolean | |
3221 | _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) | |
3222 | bfd *ibfd; | |
3223 | asection *isec; | |
3224 | bfd *obfd; | |
3225 | asection *osec; | |
3226 | { | |
3227 | Elf_Internal_Shdr *ihdr, *ohdr; | |
3228 | ||
3229 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
3230 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
3231 | return true; | |
3232 | ||
3dbf33ee ILT |
3233 | /* Copy over private BFD data if it has not already been copied. |
3234 | This must be done here, rather than in the copy_private_bfd_data | |
3235 | entry point, because the latter is called after the section | |
3236 | contents have been set, which means that the program headers have | |
3237 | already been worked out. */ | |
3238 | if (elf_tdata (obfd)->segment_map == NULL | |
3239 | && elf_tdata (ibfd)->phdr != NULL) | |
3240 | { | |
3241 | asection *s; | |
3242 | ||
3243 | /* Only set up the segments when all the sections have been set | |
3244 | up. */ | |
3245 | for (s = ibfd->sections; s != NULL; s = s->next) | |
3246 | if (s->output_section == NULL) | |
3247 | break; | |
3248 | if (s == NULL) | |
3249 | { | |
3250 | if (! copy_private_bfd_data (ibfd, obfd)) | |
3251 | return false; | |
3252 | } | |
3253 | } | |
3254 | ||
fd0198f0 ILT |
3255 | ihdr = &elf_section_data (isec)->this_hdr; |
3256 | ohdr = &elf_section_data (osec)->this_hdr; | |
3257 | ||
3258 | ohdr->sh_entsize = ihdr->sh_entsize; | |
3259 | ||
3260 | if (ihdr->sh_type == SHT_SYMTAB | |
d6bfcdb5 ILT |
3261 | || ihdr->sh_type == SHT_DYNSYM |
3262 | || ihdr->sh_type == SHT_GNU_verneed | |
3263 | || ihdr->sh_type == SHT_GNU_verdef) | |
fd0198f0 ILT |
3264 | ohdr->sh_info = ihdr->sh_info; |
3265 | ||
3266 | return true; | |
3267 | } | |
3268 | ||
3269 | /* Copy private symbol information. If this symbol is in a section | |
3270 | which we did not map into a BFD section, try to map the section | |
3271 | index correctly. We use special macro definitions for the mapped | |
3272 | section indices; these definitions are interpreted by the | |
3273 | swap_out_syms function. */ | |
3274 | ||
3275 | #define MAP_ONESYMTAB (SHN_LORESERVE - 1) | |
3276 | #define MAP_DYNSYMTAB (SHN_LORESERVE - 2) | |
3277 | #define MAP_STRTAB (SHN_LORESERVE - 3) | |
3278 | #define MAP_SHSTRTAB (SHN_LORESERVE - 4) | |
3279 | ||
3280 | boolean | |
3281 | _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg) | |
3282 | bfd *ibfd; | |
3283 | asymbol *isymarg; | |
3284 | bfd *obfd; | |
3285 | asymbol *osymarg; | |
3286 | { | |
3287 | elf_symbol_type *isym, *osym; | |
3288 | ||
efc2b064 JL |
3289 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
3290 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
3291 | return true; | |
3292 | ||
fd0198f0 ILT |
3293 | isym = elf_symbol_from (ibfd, isymarg); |
3294 | osym = elf_symbol_from (obfd, osymarg); | |
3295 | ||
3296 | if (isym != NULL | |
3297 | && osym != NULL | |
3298 | && bfd_is_abs_section (isym->symbol.section)) | |
3299 | { | |
3300 | unsigned int shndx; | |
3301 | ||
3302 | shndx = isym->internal_elf_sym.st_shndx; | |
3303 | if (shndx == elf_onesymtab (ibfd)) | |
3304 | shndx = MAP_ONESYMTAB; | |
3305 | else if (shndx == elf_dynsymtab (ibfd)) | |
3306 | shndx = MAP_DYNSYMTAB; | |
3307 | else if (shndx == elf_tdata (ibfd)->strtab_section) | |
3308 | shndx = MAP_STRTAB; | |
3309 | else if (shndx == elf_tdata (ibfd)->shstrtab_section) | |
3310 | shndx = MAP_SHSTRTAB; | |
3311 | osym->internal_elf_sym.st_shndx = shndx; | |
3312 | } | |
3313 | ||
3314 | return true; | |
3315 | } | |
3316 | ||
3317 | /* Swap out the symbols. */ | |
3318 | ||
ede4eed4 KR |
3319 | static boolean |
3320 | swap_out_syms (abfd, sttp) | |
3321 | bfd *abfd; | |
3322 | struct bfd_strtab_hash **sttp; | |
3323 | { | |
3324 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3325 | ||
3326 | if (!elf_map_symbols (abfd)) | |
3327 | return false; | |
3328 | ||
3329 | /* Dump out the symtabs. */ | |
3330 | { | |
3331 | int symcount = bfd_get_symcount (abfd); | |
3332 | asymbol **syms = bfd_get_outsymbols (abfd); | |
3333 | struct bfd_strtab_hash *stt; | |
3334 | Elf_Internal_Shdr *symtab_hdr; | |
3335 | Elf_Internal_Shdr *symstrtab_hdr; | |
3336 | char *outbound_syms; | |
3337 | int idx; | |
3338 | ||
3339 | stt = _bfd_elf_stringtab_init (); | |
3340 | if (stt == NULL) | |
3341 | return false; | |
3342 | ||
3343 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
3344 | symtab_hdr->sh_type = SHT_SYMTAB; | |
3345 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
3346 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
3347 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
3348 | symtab_hdr->sh_addralign = bed->s->file_align; | |
3349 | ||
3350 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
3351 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
3352 | ||
3353 | outbound_syms = bfd_alloc (abfd, | |
3354 | (1 + symcount) * bed->s->sizeof_sym); | |
3355 | if (outbound_syms == NULL) | |
a9713b91 | 3356 | return false; |
ede4eed4 KR |
3357 | symtab_hdr->contents = (PTR) outbound_syms; |
3358 | ||
3359 | /* now generate the data (for "contents") */ | |
3360 | { | |
3361 | /* Fill in zeroth symbol and swap it out. */ | |
3362 | Elf_Internal_Sym sym; | |
3363 | sym.st_name = 0; | |
3364 | sym.st_value = 0; | |
3365 | sym.st_size = 0; | |
3366 | sym.st_info = 0; | |
3367 | sym.st_other = 0; | |
3368 | sym.st_shndx = SHN_UNDEF; | |
cf9fb9f2 | 3369 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); |
ede4eed4 KR |
3370 | outbound_syms += bed->s->sizeof_sym; |
3371 | } | |
3372 | for (idx = 0; idx < symcount; idx++) | |
3373 | { | |
3374 | Elf_Internal_Sym sym; | |
3375 | bfd_vma value = syms[idx]->value; | |
3376 | elf_symbol_type *type_ptr; | |
3377 | flagword flags = syms[idx]->flags; | |
052b35d2 | 3378 | int type; |
ede4eed4 KR |
3379 | |
3380 | if (flags & BSF_SECTION_SYM) | |
3381 | /* Section symbols have no names. */ | |
3382 | sym.st_name = 0; | |
3383 | else | |
3384 | { | |
3385 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, | |
3386 | syms[idx]->name, | |
3387 | true, false); | |
3388 | if (sym.st_name == (unsigned long) -1) | |
3389 | return false; | |
3390 | } | |
3391 | ||
3392 | type_ptr = elf_symbol_from (abfd, syms[idx]); | |
3393 | ||
3394 | if (bfd_is_com_section (syms[idx]->section)) | |
3395 | { | |
3396 | /* ELF common symbols put the alignment into the `value' field, | |
3397 | and the size into the `size' field. This is backwards from | |
3398 | how BFD handles it, so reverse it here. */ | |
3399 | sym.st_size = value; | |
3400 | if (type_ptr == NULL | |
3401 | || type_ptr->internal_elf_sym.st_value == 0) | |
3402 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
3403 | else | |
3404 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
3405 | sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, | |
3406 | syms[idx]->section); | |
3407 | } | |
3408 | else | |
3409 | { | |
3410 | asection *sec = syms[idx]->section; | |
3411 | int shndx; | |
3412 | ||
3413 | if (sec->output_section) | |
3414 | { | |
3415 | value += sec->output_offset; | |
3416 | sec = sec->output_section; | |
3417 | } | |
3418 | value += sec->vma; | |
3419 | sym.st_value = value; | |
3420 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
fd0198f0 ILT |
3421 | |
3422 | if (bfd_is_abs_section (sec) | |
3423 | && type_ptr != NULL | |
3424 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
ede4eed4 | 3425 | { |
fd0198f0 ILT |
3426 | /* This symbol is in a real ELF section which we did |
3427 | not create as a BFD section. Undo the mapping done | |
3428 | by copy_private_symbol_data. */ | |
3429 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
3430 | switch (shndx) | |
3431 | { | |
3432 | case MAP_ONESYMTAB: | |
3433 | shndx = elf_onesymtab (abfd); | |
3434 | break; | |
3435 | case MAP_DYNSYMTAB: | |
3436 | shndx = elf_dynsymtab (abfd); | |
3437 | break; | |
3438 | case MAP_STRTAB: | |
3439 | shndx = elf_tdata (abfd)->strtab_section; | |
3440 | break; | |
3441 | case MAP_SHSTRTAB: | |
3442 | shndx = elf_tdata (abfd)->shstrtab_section; | |
3443 | break; | |
3444 | default: | |
3445 | break; | |
3446 | } | |
3447 | } | |
3448 | else | |
3449 | { | |
3450 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
3451 | ||
3452 | if (shndx == -1) | |
3453 | { | |
3454 | asection *sec2; | |
3455 | ||
3456 | /* Writing this would be a hell of a lot easier if | |
3457 | we had some decent documentation on bfd, and | |
3458 | knew what to expect of the library, and what to | |
3459 | demand of applications. For example, it | |
3460 | appears that `objcopy' might not set the | |
3461 | section of a symbol to be a section that is | |
3462 | actually in the output file. */ | |
3463 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
3464 | BFD_ASSERT (sec2 != 0); | |
3465 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
3466 | BFD_ASSERT (shndx != -1); | |
3467 | } | |
ede4eed4 | 3468 | } |
fd0198f0 ILT |
3469 | |
3470 | sym.st_shndx = shndx; | |
ede4eed4 KR |
3471 | } |
3472 | ||
052b35d2 ILT |
3473 | if ((flags & BSF_FUNCTION) != 0) |
3474 | type = STT_FUNC; | |
3475 | else if ((flags & BSF_OBJECT) != 0) | |
3476 | type = STT_OBJECT; | |
3477 | else | |
3478 | type = STT_NOTYPE; | |
3479 | ||
ede4eed4 | 3480 | if (bfd_is_com_section (syms[idx]->section)) |
052b35d2 | 3481 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); |
ede4eed4 KR |
3482 | else if (bfd_is_und_section (syms[idx]->section)) |
3483 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
3484 | ? STB_WEAK | |
3485 | : STB_GLOBAL), | |
052b35d2 | 3486 | type); |
ede4eed4 KR |
3487 | else if (flags & BSF_SECTION_SYM) |
3488 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
3489 | else if (flags & BSF_FILE) | |
3490 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
3491 | else | |
3492 | { | |
3493 | int bind = STB_LOCAL; | |
ede4eed4 KR |
3494 | |
3495 | if (flags & BSF_LOCAL) | |
3496 | bind = STB_LOCAL; | |
3497 | else if (flags & BSF_WEAK) | |
3498 | bind = STB_WEAK; | |
3499 | else if (flags & BSF_GLOBAL) | |
3500 | bind = STB_GLOBAL; | |
3501 | ||
ede4eed4 KR |
3502 | sym.st_info = ELF_ST_INFO (bind, type); |
3503 | } | |
3504 | ||
80be821d ILT |
3505 | if (type_ptr != NULL) |
3506 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
3507 | else | |
3508 | sym.st_other = 0; | |
3509 | ||
cf9fb9f2 | 3510 | bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); |
ede4eed4 KR |
3511 | outbound_syms += bed->s->sizeof_sym; |
3512 | } | |
3513 | ||
3514 | *sttp = stt; | |
3515 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
3516 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
3517 | ||
3518 | symstrtab_hdr->sh_flags = 0; | |
3519 | symstrtab_hdr->sh_addr = 0; | |
3520 | symstrtab_hdr->sh_entsize = 0; | |
3521 | symstrtab_hdr->sh_link = 0; | |
3522 | symstrtab_hdr->sh_info = 0; | |
3523 | symstrtab_hdr->sh_addralign = 1; | |
3524 | } | |
3525 | ||
3526 | return true; | |
3527 | } | |
3528 | ||
3529 | /* Return the number of bytes required to hold the symtab vector. | |
3530 | ||
3531 | Note that we base it on the count plus 1, since we will null terminate | |
3532 | the vector allocated based on this size. However, the ELF symbol table | |
3533 | always has a dummy entry as symbol #0, so it ends up even. */ | |
3534 | ||
3535 | long | |
3536 | _bfd_elf_get_symtab_upper_bound (abfd) | |
3537 | bfd *abfd; | |
3538 | { | |
3539 | long symcount; | |
3540 | long symtab_size; | |
3541 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
3542 | ||
3543 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3544 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
3545 | ||
3546 | return symtab_size; | |
3547 | } | |
3548 | ||
3549 | long | |
3550 | _bfd_elf_get_dynamic_symtab_upper_bound (abfd) | |
3551 | bfd *abfd; | |
3552 | { | |
3553 | long symcount; | |
3554 | long symtab_size; | |
3555 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
3556 | ||
3557 | if (elf_dynsymtab (abfd) == 0) | |
3558 | { | |
3559 | bfd_set_error (bfd_error_invalid_operation); | |
3560 | return -1; | |
3561 | } | |
3562 | ||
3563 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
3564 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
3565 | ||
3566 | return symtab_size; | |
3567 | } | |
3568 | ||
3569 | long | |
3570 | _bfd_elf_get_reloc_upper_bound (abfd, asect) | |
3571 | bfd *abfd; | |
3572 | sec_ptr asect; | |
3573 | { | |
3574 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
3575 | } | |
3576 | ||
3577 | /* Canonicalize the relocs. */ | |
3578 | ||
3579 | long | |
3580 | _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
3581 | bfd *abfd; | |
3582 | sec_ptr section; | |
3583 | arelent **relptr; | |
3584 | asymbol **symbols; | |
3585 | { | |
3586 | arelent *tblptr; | |
3587 | unsigned int i; | |
3588 | ||
e35765a9 ILT |
3589 | if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, |
3590 | section, | |
3591 | symbols, | |
3592 | false)) | |
ede4eed4 KR |
3593 | return -1; |
3594 | ||
3595 | tblptr = section->relocation; | |
3596 | for (i = 0; i < section->reloc_count; i++) | |
3597 | *relptr++ = tblptr++; | |
3598 | ||
3599 | *relptr = NULL; | |
3600 | ||
3601 | return section->reloc_count; | |
3602 | } | |
3603 | ||
3604 | long | |
3605 | _bfd_elf_get_symtab (abfd, alocation) | |
3606 | bfd *abfd; | |
3607 | asymbol **alocation; | |
3608 | { | |
3609 | long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false); | |
3610 | ||
3611 | if (symcount >= 0) | |
3612 | bfd_get_symcount (abfd) = symcount; | |
3613 | return symcount; | |
3614 | } | |
3615 | ||
3616 | long | |
3617 | _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation) | |
3618 | bfd *abfd; | |
3619 | asymbol **alocation; | |
3620 | { | |
3621 | return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true); | |
3622 | } | |
3623 | ||
e35765a9 ILT |
3624 | /* Return the size required for the dynamic reloc entries. Any |
3625 | section that was actually installed in the BFD, and has type | |
3626 | SHT_REL or SHT_RELA, and uses the dynamic symbol table, is | |
3627 | considered to be a dynamic reloc section. */ | |
3628 | ||
3629 | long | |
3630 | _bfd_elf_get_dynamic_reloc_upper_bound (abfd) | |
3631 | bfd *abfd; | |
3632 | { | |
3633 | long ret; | |
3634 | asection *s; | |
3635 | ||
3636 | if (elf_dynsymtab (abfd) == 0) | |
3637 | { | |
3638 | bfd_set_error (bfd_error_invalid_operation); | |
3639 | return -1; | |
3640 | } | |
3641 | ||
3642 | ret = sizeof (arelent *); | |
3643 | for (s = abfd->sections; s != NULL; s = s->next) | |
3644 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
3645 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
3646 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
3647 | ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize) | |
3648 | * sizeof (arelent *)); | |
3649 | ||
3650 | return ret; | |
3651 | } | |
3652 | ||
3653 | /* Canonicalize the dynamic relocation entries. Note that we return | |
3654 | the dynamic relocations as a single block, although they are | |
3655 | actually associated with particular sections; the interface, which | |
3656 | was designed for SunOS style shared libraries, expects that there | |
3657 | is only one set of dynamic relocs. Any section that was actually | |
3658 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses | |
3659 | the dynamic symbol table, is considered to be a dynamic reloc | |
3660 | section. */ | |
3661 | ||
3662 | long | |
3663 | _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms) | |
3664 | bfd *abfd; | |
3665 | arelent **storage; | |
3666 | asymbol **syms; | |
3667 | { | |
3668 | boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean)); | |
3669 | asection *s; | |
3670 | long ret; | |
3671 | ||
3672 | if (elf_dynsymtab (abfd) == 0) | |
3673 | { | |
3674 | bfd_set_error (bfd_error_invalid_operation); | |
3675 | return -1; | |
3676 | } | |
3677 | ||
3678 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
3679 | ret = 0; | |
3680 | for (s = abfd->sections; s != NULL; s = s->next) | |
3681 | { | |
3682 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) | |
3683 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL | |
3684 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
3685 | { | |
3686 | arelent *p; | |
3687 | long count, i; | |
3688 | ||
3689 | if (! (*slurp_relocs) (abfd, s, syms, true)) | |
3690 | return -1; | |
3691 | count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize; | |
3692 | p = s->relocation; | |
3693 | for (i = 0; i < count; i++) | |
3694 | *storage++ = p++; | |
3695 | ret += count; | |
3696 | } | |
3697 | } | |
3698 | ||
3699 | *storage = NULL; | |
3700 | ||
3701 | return ret; | |
3702 | } | |
a66a61a0 ILT |
3703 | \f |
3704 | /* Read in the version information. */ | |
3705 | ||
3706 | boolean | |
3707 | _bfd_elf_slurp_version_tables (abfd) | |
3708 | bfd *abfd; | |
3709 | { | |
3710 | bfd_byte *contents = NULL; | |
3711 | ||
3712 | if (elf_dynverdef (abfd) != 0) | |
3713 | { | |
3714 | Elf_Internal_Shdr *hdr; | |
3715 | Elf_External_Verdef *everdef; | |
3716 | Elf_Internal_Verdef *iverdef; | |
3717 | unsigned int i; | |
3718 | ||
3719 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
3720 | ||
3721 | elf_tdata (abfd)->verdef = | |
3722 | ((Elf_Internal_Verdef *) | |
3723 | bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef))); | |
3724 | if (elf_tdata (abfd)->verdef == NULL) | |
3725 | goto error_return; | |
3726 | ||
3727 | elf_tdata (abfd)->cverdefs = hdr->sh_info; | |
e35765a9 | 3728 | |
a66a61a0 ILT |
3729 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
3730 | if (contents == NULL) | |
3731 | goto error_return; | |
3732 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
3733 | || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size) | |
3734 | goto error_return; | |
3735 | ||
3736 | everdef = (Elf_External_Verdef *) contents; | |
3737 | iverdef = elf_tdata (abfd)->verdef; | |
3738 | for (i = 0; i < hdr->sh_info; i++, iverdef++) | |
3739 | { | |
3740 | Elf_External_Verdaux *everdaux; | |
3741 | Elf_Internal_Verdaux *iverdaux; | |
3742 | unsigned int j; | |
3743 | ||
3744 | _bfd_elf_swap_verdef_in (abfd, everdef, iverdef); | |
3745 | ||
3746 | iverdef->vd_bfd = abfd; | |
3747 | ||
3748 | iverdef->vd_auxptr = ((Elf_Internal_Verdaux *) | |
3749 | bfd_alloc (abfd, | |
3750 | (iverdef->vd_cnt | |
3751 | * sizeof (Elf_Internal_Verdaux)))); | |
3752 | if (iverdef->vd_auxptr == NULL) | |
3753 | goto error_return; | |
3754 | ||
3755 | everdaux = ((Elf_External_Verdaux *) | |
3756 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
3757 | iverdaux = iverdef->vd_auxptr; | |
3758 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
3759 | { | |
3760 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
3761 | ||
3762 | iverdaux->vda_nodename = | |
3763 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3764 | iverdaux->vda_name); | |
3765 | if (iverdaux->vda_nodename == NULL) | |
3766 | goto error_return; | |
3767 | ||
3768 | if (j + 1 < iverdef->vd_cnt) | |
3769 | iverdaux->vda_nextptr = iverdaux + 1; | |
3770 | else | |
3771 | iverdaux->vda_nextptr = NULL; | |
3772 | ||
3773 | everdaux = ((Elf_External_Verdaux *) | |
3774 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
3775 | } | |
3776 | ||
3777 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
3778 | ||
3779 | if (i + 1 < hdr->sh_info) | |
3780 | iverdef->vd_nextdef = iverdef + 1; | |
3781 | else | |
3782 | iverdef->vd_nextdef = NULL; | |
3783 | ||
3784 | everdef = ((Elf_External_Verdef *) | |
3785 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
3786 | } | |
3787 | ||
3788 | free (contents); | |
3789 | contents = NULL; | |
3790 | } | |
3791 | ||
3792 | if (elf_dynverref (abfd) != 0) | |
3793 | { | |
3794 | Elf_Internal_Shdr *hdr; | |
3795 | Elf_External_Verneed *everneed; | |
3796 | Elf_Internal_Verneed *iverneed; | |
3797 | unsigned int i; | |
3798 | ||
3799 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
3800 | ||
3801 | elf_tdata (abfd)->verref = | |
3802 | ((Elf_Internal_Verneed *) | |
3803 | bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed))); | |
3804 | if (elf_tdata (abfd)->verref == NULL) | |
3805 | goto error_return; | |
3806 | ||
3807 | elf_tdata (abfd)->cverrefs = hdr->sh_info; | |
3808 | ||
3809 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
3810 | if (contents == NULL) | |
3811 | goto error_return; | |
3812 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 | |
3813 | || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size) | |
3814 | goto error_return; | |
3815 | ||
3816 | everneed = (Elf_External_Verneed *) contents; | |
3817 | iverneed = elf_tdata (abfd)->verref; | |
3818 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
3819 | { | |
3820 | Elf_External_Vernaux *evernaux; | |
3821 | Elf_Internal_Vernaux *ivernaux; | |
3822 | unsigned int j; | |
3823 | ||
3824 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
3825 | ||
3826 | iverneed->vn_bfd = abfd; | |
3827 | ||
3828 | iverneed->vn_filename = | |
3829 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3830 | iverneed->vn_file); | |
3831 | if (iverneed->vn_filename == NULL) | |
3832 | goto error_return; | |
3833 | ||
3834 | iverneed->vn_auxptr = | |
3835 | ((Elf_Internal_Vernaux *) | |
3836 | bfd_alloc (abfd, | |
3837 | iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux))); | |
3838 | ||
3839 | evernaux = ((Elf_External_Vernaux *) | |
3840 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
3841 | ivernaux = iverneed->vn_auxptr; | |
3842 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
3843 | { | |
3844 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
3845 | ||
3846 | ivernaux->vna_nodename = | |
3847 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
3848 | ivernaux->vna_name); | |
3849 | if (ivernaux->vna_nodename == NULL) | |
3850 | goto error_return; | |
3851 | ||
3852 | if (j + 1 < iverneed->vn_cnt) | |
3853 | ivernaux->vna_nextptr = ivernaux + 1; | |
3854 | else | |
3855 | ivernaux->vna_nextptr = NULL; | |
3856 | ||
3857 | evernaux = ((Elf_External_Vernaux *) | |
3858 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
3859 | } | |
3860 | ||
3861 | if (i + 1 < hdr->sh_info) | |
3862 | iverneed->vn_nextref = iverneed + 1; | |
3863 | else | |
3864 | iverneed->vn_nextref = NULL; | |
3865 | ||
3866 | everneed = ((Elf_External_Verneed *) | |
3867 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
3868 | } | |
3869 | ||
3870 | free (contents); | |
3871 | contents = NULL; | |
3872 | } | |
3873 | ||
3874 | return true; | |
3875 | ||
3876 | error_return: | |
3877 | if (contents == NULL) | |
3878 | free (contents); | |
3879 | return false; | |
3880 | } | |
3881 | \f | |
ede4eed4 KR |
3882 | asymbol * |
3883 | _bfd_elf_make_empty_symbol (abfd) | |
3884 | bfd *abfd; | |
3885 | { | |
3886 | elf_symbol_type *newsym; | |
3887 | ||
3888 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
3889 | if (!newsym) | |
a9713b91 | 3890 | return NULL; |
ede4eed4 KR |
3891 | else |
3892 | { | |
3893 | newsym->symbol.the_bfd = abfd; | |
3894 | return &newsym->symbol; | |
3895 | } | |
3896 | } | |
3897 | ||
3898 | void | |
3899 | _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret) | |
3900 | bfd *ignore_abfd; | |
3901 | asymbol *symbol; | |
3902 | symbol_info *ret; | |
3903 | { | |
3904 | bfd_symbol_info (symbol, ret); | |
3905 | } | |
3906 | ||
d6bfcdb5 ILT |
3907 | /* Return whether a symbol name implies a local symbol. Most targets |
3908 | use this function for the is_local_label_name entry point, but some | |
3909 | override it. */ | |
a66a61a0 ILT |
3910 | |
3911 | boolean | |
3912 | _bfd_elf_is_local_label_name (abfd, name) | |
3913 | bfd *abfd; | |
3914 | const char *name; | |
3915 | { | |
d6bfcdb5 ILT |
3916 | /* Normal local symbols start with ``.L''. */ |
3917 | if (name[0] == '.' && name[1] == 'L') | |
3918 | return true; | |
3919 | ||
3920 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
3921 | DWARF debugging symbols starting with ``..''. */ | |
3922 | if (name[0] == '.' && name[1] == '.') | |
3923 | return true; | |
3924 | ||
3925 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
3926 | emitting DWARF debugging output. I suspect this is actually a | |
3927 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
3928 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
3929 | underscore to be emitted on some ELF targets). For ease of use, | |
3930 | we treat such symbols as local. */ | |
3931 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
3932 | return true; | |
3933 | ||
3934 | return false; | |
a66a61a0 ILT |
3935 | } |
3936 | ||
ede4eed4 KR |
3937 | alent * |
3938 | _bfd_elf_get_lineno (ignore_abfd, symbol) | |
3939 | bfd *ignore_abfd; | |
3940 | asymbol *symbol; | |
3941 | { | |
8cd2f4fe | 3942 | abort (); |
ede4eed4 KR |
3943 | return NULL; |
3944 | } | |
3945 | ||
3946 | boolean | |
3947 | _bfd_elf_set_arch_mach (abfd, arch, machine) | |
3948 | bfd *abfd; | |
3949 | enum bfd_architecture arch; | |
3950 | unsigned long machine; | |
3951 | { | |
3952 | /* If this isn't the right architecture for this backend, and this | |
3953 | isn't the generic backend, fail. */ | |
3954 | if (arch != get_elf_backend_data (abfd)->arch | |
3955 | && arch != bfd_arch_unknown | |
3956 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
3957 | return false; | |
3958 | ||
3959 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
3960 | } | |
3961 | ||
6f904fce ILT |
3962 | /* Find the nearest line to a particular section and offset, for error |
3963 | reporting. */ | |
3964 | ||
ede4eed4 KR |
3965 | boolean |
3966 | _bfd_elf_find_nearest_line (abfd, | |
6f904fce ILT |
3967 | section, |
3968 | symbols, | |
3969 | offset, | |
3970 | filename_ptr, | |
3971 | functionname_ptr, | |
3972 | line_ptr) | |
ede4eed4 KR |
3973 | bfd *abfd; |
3974 | asection *section; | |
3975 | asymbol **symbols; | |
3976 | bfd_vma offset; | |
3977 | CONST char **filename_ptr; | |
3978 | CONST char **functionname_ptr; | |
3979 | unsigned int *line_ptr; | |
3980 | { | |
86aac8ea | 3981 | boolean found; |
6f904fce ILT |
3982 | const char *filename; |
3983 | asymbol *func; | |
86aac8ea | 3984 | bfd_vma low_func; |
6f904fce ILT |
3985 | asymbol **p; |
3986 | ||
86aac8ea ILT |
3987 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, |
3988 | &found, filename_ptr, | |
3989 | functionname_ptr, line_ptr, | |
3990 | &elf_tdata (abfd)->line_info)) | |
3991 | return false; | |
3992 | if (found) | |
3993 | return true; | |
3994 | ||
6f904fce ILT |
3995 | if (symbols == NULL) |
3996 | return false; | |
3997 | ||
3998 | filename = NULL; | |
3999 | func = NULL; | |
86aac8ea | 4000 | low_func = 0; |
6f904fce ILT |
4001 | |
4002 | for (p = symbols; *p != NULL; p++) | |
4003 | { | |
4004 | elf_symbol_type *q; | |
4005 | ||
4006 | q = (elf_symbol_type *) *p; | |
4007 | ||
4008 | if (bfd_get_section (&q->symbol) != section) | |
4009 | continue; | |
4010 | ||
4011 | switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) | |
4012 | { | |
4013 | default: | |
4014 | break; | |
4015 | case STT_FILE: | |
4016 | filename = bfd_asymbol_name (&q->symbol); | |
4017 | break; | |
4018 | case STT_FUNC: | |
86aac8ea ILT |
4019 | if (q->symbol.section == section |
4020 | && q->symbol.value >= low_func | |
4021 | && q->symbol.value <= offset) | |
4022 | { | |
4023 | func = (asymbol *) q; | |
4024 | low_func = q->symbol.value; | |
4025 | } | |
6f904fce ILT |
4026 | break; |
4027 | } | |
4028 | } | |
4029 | ||
4030 | if (func == NULL) | |
4031 | return false; | |
4032 | ||
4033 | *filename_ptr = filename; | |
4034 | *functionname_ptr = bfd_asymbol_name (func); | |
4035 | *line_ptr = 0; | |
4036 | return true; | |
ede4eed4 KR |
4037 | } |
4038 | ||
4039 | int | |
4040 | _bfd_elf_sizeof_headers (abfd, reloc) | |
4041 | bfd *abfd; | |
4042 | boolean reloc; | |
4043 | { | |
4044 | int ret; | |
4045 | ||
4046 | ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; | |
4047 | if (! reloc) | |
fd0198f0 | 4048 | ret += get_program_header_size (abfd); |
ede4eed4 KR |
4049 | return ret; |
4050 | } | |
4051 | ||
4052 | boolean | |
4053 | _bfd_elf_set_section_contents (abfd, section, location, offset, count) | |
4054 | bfd *abfd; | |
4055 | sec_ptr section; | |
4056 | PTR location; | |
4057 | file_ptr offset; | |
4058 | bfd_size_type count; | |
4059 | { | |
4060 | Elf_Internal_Shdr *hdr; | |
4061 | ||
4062 | if (! abfd->output_has_begun | |
4063 | && ! _bfd_elf_compute_section_file_positions (abfd, | |
4064 | (struct bfd_link_info *) NULL)) | |
4065 | return false; | |
4066 | ||
4067 | hdr = &elf_section_data (section)->this_hdr; | |
4068 | ||
4069 | if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) | |
4070 | return false; | |
4071 | if (bfd_write (location, 1, count, abfd) != count) | |
4072 | return false; | |
4073 | ||
4074 | return true; | |
4075 | } | |
4076 | ||
4077 | void | |
4078 | _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst) | |
4079 | bfd *abfd; | |
4080 | arelent *cache_ptr; | |
4081 | Elf_Internal_Rela *dst; | |
4082 | { | |
8cd2f4fe | 4083 | abort (); |
ede4eed4 KR |
4084 | } |
4085 | ||
4086 | #if 0 | |
4087 | void | |
4088 | _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst) | |
4089 | bfd *abfd; | |
4090 | arelent *cache_ptr; | |
4091 | Elf_Internal_Rel *dst; | |
4092 | { | |
8cd2f4fe | 4093 | abort (); |
ede4eed4 KR |
4094 | } |
4095 | #endif | |
7fc6a16a ILT |
4096 | |
4097 | /* Try to convert a non-ELF reloc into an ELF one. */ | |
4098 | ||
4099 | boolean | |
4100 | _bfd_elf_validate_reloc (abfd, areloc) | |
4101 | bfd *abfd; | |
4102 | arelent *areloc; | |
4103 | { | |
4104 | /* Check whether we really have an ELF howto. */ | |
4105 | ||
4106 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
4107 | { | |
4108 | bfd_reloc_code_real_type code; | |
4109 | reloc_howto_type *howto; | |
4110 | ||
4111 | /* Alien reloc: Try to determine its type to replace it with an | |
4112 | equivalent ELF reloc. */ | |
4113 | ||
4114 | if (areloc->howto->pc_relative) | |
4115 | { | |
4116 | switch (areloc->howto->bitsize) | |
4117 | { | |
4118 | case 8: | |
4119 | code = BFD_RELOC_8_PCREL; | |
4120 | break; | |
4121 | case 12: | |
4122 | code = BFD_RELOC_12_PCREL; | |
4123 | break; | |
4124 | case 16: | |
4125 | code = BFD_RELOC_16_PCREL; | |
4126 | break; | |
4127 | case 24: | |
4128 | code = BFD_RELOC_24_PCREL; | |
4129 | break; | |
4130 | case 32: | |
4131 | code = BFD_RELOC_32_PCREL; | |
4132 | break; | |
4133 | case 64: | |
4134 | code = BFD_RELOC_64_PCREL; | |
4135 | break; | |
4136 | default: | |
4137 | goto fail; | |
4138 | } | |
4139 | ||
4140 | howto = bfd_reloc_type_lookup (abfd, code); | |
4141 | ||
4142 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
4143 | { | |
4144 | if (howto->pcrel_offset) | |
4145 | areloc->addend += areloc->address; | |
4146 | else | |
4147 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
4148 | } | |
4149 | } | |
4150 | else | |
4151 | { | |
4152 | switch (areloc->howto->bitsize) | |
4153 | { | |
4154 | case 8: | |
4155 | code = BFD_RELOC_8; | |
4156 | break; | |
4157 | case 14: | |
4158 | code = BFD_RELOC_14; | |
4159 | break; | |
4160 | case 16: | |
4161 | code = BFD_RELOC_16; | |
4162 | break; | |
4163 | case 26: | |
4164 | code = BFD_RELOC_26; | |
4165 | break; | |
4166 | case 32: | |
4167 | code = BFD_RELOC_32; | |
4168 | break; | |
4169 | case 64: | |
4170 | code = BFD_RELOC_64; | |
4171 | break; | |
4172 | default: | |
4173 | goto fail; | |
4174 | } | |
4175 | ||
4176 | howto = bfd_reloc_type_lookup (abfd, code); | |
4177 | } | |
4178 | ||
4179 | if (howto) | |
4180 | areloc->howto = howto; | |
4181 | else | |
4182 | goto fail; | |
4183 | } | |
4184 | ||
4185 | return true; | |
4186 | ||
4187 | fail: | |
4188 | (*_bfd_error_handler) | |
4189 | ("%s: unsupported relocation type %s", | |
4190 | bfd_get_filename (abfd), areloc->howto->name); | |
4191 | bfd_set_error (bfd_error_bad_value); | |
4192 | return false; | |
4193 | } |