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