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