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