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