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