]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* ELF executable support for BFD. |
340b6d91 | 2 | |
2571583a | 3 | Copyright (C) 1993-2017 Free Software Foundation, Inc. |
252b5132 | 4 | |
5e8d7549 | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
5e8d7549 NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
5e8d7549 | 10 | (at your option) any later version. |
252b5132 | 11 | |
5e8d7549 NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
5e8d7549 | 17 | You should have received a copy of the GNU General Public License |
b34976b6 | 18 | along with this program; if not, write to the Free Software |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
21 | ||
252b5132 | 22 | |
1b74d094 BW |
23 | /* |
24 | SECTION | |
252b5132 RH |
25 | ELF backends |
26 | ||
27 | BFD support for ELF formats is being worked on. | |
28 | Currently, the best supported back ends are for sparc and i386 | |
29 | (running svr4 or Solaris 2). | |
30 | ||
31 | Documentation of the internals of the support code still needs | |
32 | to be written. The code is changing quickly enough that we | |
661a3fd4 | 33 | haven't bothered yet. */ |
252b5132 | 34 | |
7ee38065 MS |
35 | /* For sparc64-cross-sparc32. */ |
36 | #define _SYSCALL32 | |
252b5132 | 37 | #include "sysdep.h" |
3db64b00 | 38 | #include "bfd.h" |
252b5132 RH |
39 | #include "bfdlink.h" |
40 | #include "libbfd.h" | |
41 | #define ARCH_SIZE 0 | |
42 | #include "elf-bfd.h" | |
e0e8c97f | 43 | #include "libiberty.h" |
ff59fc36 | 44 | #include "safe-ctype.h" |
de64ce13 | 45 | #include "elf-linux-core.h" |
252b5132 | 46 | |
8bc7f138 L |
47 | #ifdef CORE_HEADER |
48 | #include CORE_HEADER | |
49 | #endif | |
50 | ||
217aa764 | 51 | static int elf_sort_sections (const void *, const void *); |
c84fca4d | 52 | static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); |
217aa764 | 53 | static bfd_boolean prep_headers (bfd *); |
ef10c3ac | 54 | static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; |
718175fa JK |
55 | static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ; |
56 | static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, | |
57 | file_ptr offset); | |
50b2bdb7 | 58 | |
252b5132 RH |
59 | /* Swap version information in and out. The version information is |
60 | currently size independent. If that ever changes, this code will | |
61 | need to move into elfcode.h. */ | |
62 | ||
63 | /* Swap in a Verdef structure. */ | |
64 | ||
65 | void | |
217aa764 AM |
66 | _bfd_elf_swap_verdef_in (bfd *abfd, |
67 | const Elf_External_Verdef *src, | |
68 | Elf_Internal_Verdef *dst) | |
252b5132 | 69 | { |
dc810e39 AM |
70 | dst->vd_version = H_GET_16 (abfd, src->vd_version); |
71 | dst->vd_flags = H_GET_16 (abfd, src->vd_flags); | |
72 | dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); | |
73 | dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); | |
74 | dst->vd_hash = H_GET_32 (abfd, src->vd_hash); | |
75 | dst->vd_aux = H_GET_32 (abfd, src->vd_aux); | |
76 | dst->vd_next = H_GET_32 (abfd, src->vd_next); | |
252b5132 RH |
77 | } |
78 | ||
79 | /* Swap out a Verdef structure. */ | |
80 | ||
81 | void | |
217aa764 AM |
82 | _bfd_elf_swap_verdef_out (bfd *abfd, |
83 | const Elf_Internal_Verdef *src, | |
84 | Elf_External_Verdef *dst) | |
252b5132 | 85 | { |
dc810e39 AM |
86 | H_PUT_16 (abfd, src->vd_version, dst->vd_version); |
87 | H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); | |
88 | H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); | |
89 | H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); | |
90 | H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); | |
91 | H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); | |
92 | H_PUT_32 (abfd, src->vd_next, dst->vd_next); | |
252b5132 RH |
93 | } |
94 | ||
95 | /* Swap in a Verdaux structure. */ | |
96 | ||
97 | void | |
217aa764 AM |
98 | _bfd_elf_swap_verdaux_in (bfd *abfd, |
99 | const Elf_External_Verdaux *src, | |
100 | Elf_Internal_Verdaux *dst) | |
252b5132 | 101 | { |
dc810e39 AM |
102 | dst->vda_name = H_GET_32 (abfd, src->vda_name); |
103 | dst->vda_next = H_GET_32 (abfd, src->vda_next); | |
252b5132 RH |
104 | } |
105 | ||
106 | /* Swap out a Verdaux structure. */ | |
107 | ||
108 | void | |
217aa764 AM |
109 | _bfd_elf_swap_verdaux_out (bfd *abfd, |
110 | const Elf_Internal_Verdaux *src, | |
111 | Elf_External_Verdaux *dst) | |
252b5132 | 112 | { |
dc810e39 AM |
113 | H_PUT_32 (abfd, src->vda_name, dst->vda_name); |
114 | H_PUT_32 (abfd, src->vda_next, dst->vda_next); | |
252b5132 RH |
115 | } |
116 | ||
117 | /* Swap in a Verneed structure. */ | |
118 | ||
119 | void | |
217aa764 AM |
120 | _bfd_elf_swap_verneed_in (bfd *abfd, |
121 | const Elf_External_Verneed *src, | |
122 | Elf_Internal_Verneed *dst) | |
252b5132 | 123 | { |
dc810e39 AM |
124 | dst->vn_version = H_GET_16 (abfd, src->vn_version); |
125 | dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); | |
126 | dst->vn_file = H_GET_32 (abfd, src->vn_file); | |
127 | dst->vn_aux = H_GET_32 (abfd, src->vn_aux); | |
128 | dst->vn_next = H_GET_32 (abfd, src->vn_next); | |
252b5132 RH |
129 | } |
130 | ||
131 | /* Swap out a Verneed structure. */ | |
132 | ||
133 | void | |
217aa764 AM |
134 | _bfd_elf_swap_verneed_out (bfd *abfd, |
135 | const Elf_Internal_Verneed *src, | |
136 | Elf_External_Verneed *dst) | |
252b5132 | 137 | { |
dc810e39 AM |
138 | H_PUT_16 (abfd, src->vn_version, dst->vn_version); |
139 | H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); | |
140 | H_PUT_32 (abfd, src->vn_file, dst->vn_file); | |
141 | H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); | |
142 | H_PUT_32 (abfd, src->vn_next, dst->vn_next); | |
252b5132 RH |
143 | } |
144 | ||
145 | /* Swap in a Vernaux structure. */ | |
146 | ||
147 | void | |
217aa764 AM |
148 | _bfd_elf_swap_vernaux_in (bfd *abfd, |
149 | const Elf_External_Vernaux *src, | |
150 | Elf_Internal_Vernaux *dst) | |
252b5132 | 151 | { |
dc810e39 AM |
152 | dst->vna_hash = H_GET_32 (abfd, src->vna_hash); |
153 | dst->vna_flags = H_GET_16 (abfd, src->vna_flags); | |
154 | dst->vna_other = H_GET_16 (abfd, src->vna_other); | |
155 | dst->vna_name = H_GET_32 (abfd, src->vna_name); | |
156 | dst->vna_next = H_GET_32 (abfd, src->vna_next); | |
252b5132 RH |
157 | } |
158 | ||
159 | /* Swap out a Vernaux structure. */ | |
160 | ||
161 | void | |
217aa764 AM |
162 | _bfd_elf_swap_vernaux_out (bfd *abfd, |
163 | const Elf_Internal_Vernaux *src, | |
164 | Elf_External_Vernaux *dst) | |
252b5132 | 165 | { |
dc810e39 AM |
166 | H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); |
167 | H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); | |
168 | H_PUT_16 (abfd, src->vna_other, dst->vna_other); | |
169 | H_PUT_32 (abfd, src->vna_name, dst->vna_name); | |
170 | H_PUT_32 (abfd, src->vna_next, dst->vna_next); | |
252b5132 RH |
171 | } |
172 | ||
173 | /* Swap in a Versym structure. */ | |
174 | ||
175 | void | |
217aa764 AM |
176 | _bfd_elf_swap_versym_in (bfd *abfd, |
177 | const Elf_External_Versym *src, | |
178 | Elf_Internal_Versym *dst) | |
252b5132 | 179 | { |
dc810e39 | 180 | dst->vs_vers = H_GET_16 (abfd, src->vs_vers); |
252b5132 RH |
181 | } |
182 | ||
183 | /* Swap out a Versym structure. */ | |
184 | ||
185 | void | |
217aa764 AM |
186 | _bfd_elf_swap_versym_out (bfd *abfd, |
187 | const Elf_Internal_Versym *src, | |
188 | Elf_External_Versym *dst) | |
252b5132 | 189 | { |
dc810e39 | 190 | H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); |
252b5132 RH |
191 | } |
192 | ||
193 | /* Standard ELF hash function. Do not change this function; you will | |
194 | cause invalid hash tables to be generated. */ | |
3a99b017 | 195 | |
252b5132 | 196 | unsigned long |
217aa764 | 197 | bfd_elf_hash (const char *namearg) |
252b5132 | 198 | { |
3a99b017 | 199 | const unsigned char *name = (const unsigned char *) namearg; |
252b5132 RH |
200 | unsigned long h = 0; |
201 | unsigned long g; | |
202 | int ch; | |
203 | ||
204 | while ((ch = *name++) != '\0') | |
205 | { | |
206 | h = (h << 4) + ch; | |
207 | if ((g = (h & 0xf0000000)) != 0) | |
208 | { | |
209 | h ^= g >> 24; | |
210 | /* The ELF ABI says `h &= ~g', but this is equivalent in | |
211 | this case and on some machines one insn instead of two. */ | |
212 | h ^= g; | |
213 | } | |
214 | } | |
32dfa85d | 215 | return h & 0xffffffff; |
252b5132 RH |
216 | } |
217 | ||
fdc90cb4 JJ |
218 | /* DT_GNU_HASH hash function. Do not change this function; you will |
219 | cause invalid hash tables to be generated. */ | |
220 | ||
221 | unsigned long | |
222 | bfd_elf_gnu_hash (const char *namearg) | |
223 | { | |
224 | const unsigned char *name = (const unsigned char *) namearg; | |
225 | unsigned long h = 5381; | |
226 | unsigned char ch; | |
227 | ||
228 | while ((ch = *name++) != '\0') | |
229 | h = (h << 5) + h + ch; | |
230 | return h & 0xffffffff; | |
231 | } | |
232 | ||
0c8d6e5c AM |
233 | /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with |
234 | the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ | |
b34976b6 | 235 | bfd_boolean |
0c8d6e5c | 236 | bfd_elf_allocate_object (bfd *abfd, |
0ffa91dd | 237 | size_t object_size, |
4dfe6ac6 | 238 | enum elf_target_id object_id) |
252b5132 | 239 | { |
0ffa91dd NC |
240 | BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); |
241 | abfd->tdata.any = bfd_zalloc (abfd, object_size); | |
242 | if (abfd->tdata.any == NULL) | |
243 | return FALSE; | |
252b5132 | 244 | |
0ffa91dd | 245 | elf_object_id (abfd) = object_id; |
c0355132 AM |
246 | if (abfd->direction != read_direction) |
247 | { | |
248 | struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); | |
249 | if (o == NULL) | |
250 | return FALSE; | |
251 | elf_tdata (abfd)->o = o; | |
252 | elf_program_header_size (abfd) = (bfd_size_type) -1; | |
253 | } | |
b34976b6 | 254 | return TRUE; |
252b5132 RH |
255 | } |
256 | ||
0ffa91dd NC |
257 | |
258 | bfd_boolean | |
ae95ffa6 | 259 | bfd_elf_make_object (bfd *abfd) |
0ffa91dd | 260 | { |
ae95ffa6 | 261 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
0ffa91dd | 262 | return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), |
ae95ffa6 | 263 | bed->target_id); |
0ffa91dd NC |
264 | } |
265 | ||
b34976b6 | 266 | bfd_boolean |
217aa764 | 267 | bfd_elf_mkcorefile (bfd *abfd) |
252b5132 | 268 | { |
c044fabd | 269 | /* I think this can be done just like an object file. */ |
228e534f AM |
270 | if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) |
271 | return FALSE; | |
272 | elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); | |
273 | return elf_tdata (abfd)->core != NULL; | |
252b5132 RH |
274 | } |
275 | ||
72a80a16 | 276 | static char * |
217aa764 | 277 | bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) |
252b5132 RH |
278 | { |
279 | Elf_Internal_Shdr **i_shdrp; | |
f075ee0c | 280 | bfd_byte *shstrtab = NULL; |
dc810e39 AM |
281 | file_ptr offset; |
282 | bfd_size_type shstrtabsize; | |
252b5132 RH |
283 | |
284 | i_shdrp = elf_elfsections (abfd); | |
74f2e02b AM |
285 | if (i_shdrp == 0 |
286 | || shindex >= elf_numsections (abfd) | |
287 | || i_shdrp[shindex] == 0) | |
f075ee0c | 288 | return NULL; |
252b5132 | 289 | |
f075ee0c | 290 | shstrtab = i_shdrp[shindex]->contents; |
252b5132 RH |
291 | if (shstrtab == NULL) |
292 | { | |
c044fabd | 293 | /* No cached one, attempt to read, and cache what we read. */ |
252b5132 RH |
294 | offset = i_shdrp[shindex]->sh_offset; |
295 | shstrtabsize = i_shdrp[shindex]->sh_size; | |
c6c60d09 JJ |
296 | |
297 | /* Allocate and clear an extra byte at the end, to prevent crashes | |
298 | in case the string table is not terminated. */ | |
3471d59d | 299 | if (shstrtabsize + 1 <= 1 |
06614111 NC |
300 | || bfd_seek (abfd, offset, SEEK_SET) != 0 |
301 | || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL) | |
c6c60d09 JJ |
302 | shstrtab = NULL; |
303 | else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) | |
304 | { | |
305 | if (bfd_get_error () != bfd_error_system_call) | |
306 | bfd_set_error (bfd_error_file_truncated); | |
06614111 | 307 | bfd_release (abfd, shstrtab); |
c6c60d09 | 308 | shstrtab = NULL; |
3471d59d CC |
309 | /* Once we've failed to read it, make sure we don't keep |
310 | trying. Otherwise, we'll keep allocating space for | |
311 | the string table over and over. */ | |
312 | i_shdrp[shindex]->sh_size = 0; | |
c6c60d09 JJ |
313 | } |
314 | else | |
315 | shstrtab[shstrtabsize] = '\0'; | |
217aa764 | 316 | i_shdrp[shindex]->contents = shstrtab; |
252b5132 | 317 | } |
f075ee0c | 318 | return (char *) shstrtab; |
252b5132 RH |
319 | } |
320 | ||
321 | char * | |
217aa764 AM |
322 | bfd_elf_string_from_elf_section (bfd *abfd, |
323 | unsigned int shindex, | |
324 | unsigned int strindex) | |
252b5132 RH |
325 | { |
326 | Elf_Internal_Shdr *hdr; | |
327 | ||
328 | if (strindex == 0) | |
329 | return ""; | |
330 | ||
74f2e02b AM |
331 | if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) |
332 | return NULL; | |
333 | ||
252b5132 RH |
334 | hdr = elf_elfsections (abfd)[shindex]; |
335 | ||
06614111 NC |
336 | if (hdr->contents == NULL) |
337 | { | |
338 | if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) | |
339 | { | |
340 | /* PR 17512: file: f057ec89. */ | |
695344c0 | 341 | /* xgettext:c-format */ |
63a5468a AM |
342 | _bfd_error_handler (_("%B: attempt to load strings from" |
343 | " a non-string section (number %d)"), | |
06614111 NC |
344 | abfd, shindex); |
345 | return NULL; | |
346 | } | |
b1fa9dd6 | 347 | |
06614111 NC |
348 | if (bfd_elf_get_str_section (abfd, shindex) == NULL) |
349 | return NULL; | |
350 | } | |
252b5132 RH |
351 | |
352 | if (strindex >= hdr->sh_size) | |
353 | { | |
1b3a8575 | 354 | unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; |
4eca0228 | 355 | _bfd_error_handler |
695344c0 | 356 | /* xgettext:c-format */ |
d003868e AM |
357 | (_("%B: invalid string offset %u >= %lu for section `%s'"), |
358 | abfd, strindex, (unsigned long) hdr->sh_size, | |
1b3a8575 | 359 | (shindex == shstrndx && strindex == hdr->sh_name |
252b5132 | 360 | ? ".shstrtab" |
1b3a8575 | 361 | : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); |
45b222d6 | 362 | return NULL; |
252b5132 RH |
363 | } |
364 | ||
365 | return ((char *) hdr->contents) + strindex; | |
366 | } | |
367 | ||
6cdc0ccc AM |
368 | /* Read and convert symbols to internal format. |
369 | SYMCOUNT specifies the number of symbols to read, starting from | |
370 | symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF | |
371 | are non-NULL, they are used to store the internal symbols, external | |
b7c368d0 NC |
372 | symbols, and symbol section index extensions, respectively. |
373 | Returns a pointer to the internal symbol buffer (malloced if necessary) | |
374 | or NULL if there were no symbols or some kind of problem. */ | |
6cdc0ccc AM |
375 | |
376 | Elf_Internal_Sym * | |
217aa764 AM |
377 | bfd_elf_get_elf_syms (bfd *ibfd, |
378 | Elf_Internal_Shdr *symtab_hdr, | |
379 | size_t symcount, | |
380 | size_t symoffset, | |
381 | Elf_Internal_Sym *intsym_buf, | |
382 | void *extsym_buf, | |
383 | Elf_External_Sym_Shndx *extshndx_buf) | |
6cdc0ccc AM |
384 | { |
385 | Elf_Internal_Shdr *shndx_hdr; | |
217aa764 | 386 | void *alloc_ext; |
df622259 | 387 | const bfd_byte *esym; |
6cdc0ccc AM |
388 | Elf_External_Sym_Shndx *alloc_extshndx; |
389 | Elf_External_Sym_Shndx *shndx; | |
4dd07732 | 390 | Elf_Internal_Sym *alloc_intsym; |
6cdc0ccc AM |
391 | Elf_Internal_Sym *isym; |
392 | Elf_Internal_Sym *isymend; | |
9c5bfbb7 | 393 | const struct elf_backend_data *bed; |
6cdc0ccc AM |
394 | size_t extsym_size; |
395 | bfd_size_type amt; | |
396 | file_ptr pos; | |
397 | ||
e44a2c9c AM |
398 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) |
399 | abort (); | |
400 | ||
6cdc0ccc AM |
401 | if (symcount == 0) |
402 | return intsym_buf; | |
403 | ||
404 | /* Normal syms might have section extension entries. */ | |
405 | shndx_hdr = NULL; | |
6a40cf0c NC |
406 | if (elf_symtab_shndx_list (ibfd) != NULL) |
407 | { | |
408 | elf_section_list * entry; | |
409 | Elf_Internal_Shdr **sections = elf_elfsections (ibfd); | |
410 | ||
411 | /* Find an index section that is linked to this symtab section. */ | |
412 | for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) | |
315350be NC |
413 | { |
414 | /* PR 20063. */ | |
415 | if (entry->hdr.sh_link >= elf_numsections (ibfd)) | |
416 | continue; | |
417 | ||
418 | if (sections[entry->hdr.sh_link] == symtab_hdr) | |
419 | { | |
420 | shndx_hdr = & entry->hdr; | |
421 | break; | |
422 | }; | |
423 | } | |
6a40cf0c NC |
424 | |
425 | if (shndx_hdr == NULL) | |
426 | { | |
427 | if (symtab_hdr == & elf_symtab_hdr (ibfd)) | |
428 | /* Not really accurate, but this was how the old code used to work. */ | |
429 | shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; | |
430 | /* Otherwise we do nothing. The assumption is that | |
431 | the index table will not be needed. */ | |
432 | } | |
433 | } | |
6cdc0ccc AM |
434 | |
435 | /* Read the symbols. */ | |
436 | alloc_ext = NULL; | |
437 | alloc_extshndx = NULL; | |
4dd07732 | 438 | alloc_intsym = NULL; |
6cdc0ccc AM |
439 | bed = get_elf_backend_data (ibfd); |
440 | extsym_size = bed->s->sizeof_sym; | |
ef53be89 | 441 | amt = (bfd_size_type) symcount * extsym_size; |
6cdc0ccc AM |
442 | pos = symtab_hdr->sh_offset + symoffset * extsym_size; |
443 | if (extsym_buf == NULL) | |
444 | { | |
d0fb9a8d | 445 | alloc_ext = bfd_malloc2 (symcount, extsym_size); |
6cdc0ccc AM |
446 | extsym_buf = alloc_ext; |
447 | } | |
448 | if (extsym_buf == NULL | |
449 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
450 | || bfd_bread (extsym_buf, amt, ibfd) != amt) | |
451 | { | |
452 | intsym_buf = NULL; | |
453 | goto out; | |
454 | } | |
455 | ||
456 | if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) | |
457 | extshndx_buf = NULL; | |
458 | else | |
459 | { | |
ef53be89 | 460 | amt = (bfd_size_type) symcount * sizeof (Elf_External_Sym_Shndx); |
6cdc0ccc AM |
461 | pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); |
462 | if (extshndx_buf == NULL) | |
463 | { | |
a50b1753 NC |
464 | alloc_extshndx = (Elf_External_Sym_Shndx *) |
465 | bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx)); | |
6cdc0ccc AM |
466 | extshndx_buf = alloc_extshndx; |
467 | } | |
468 | if (extshndx_buf == NULL | |
469 | || bfd_seek (ibfd, pos, SEEK_SET) != 0 | |
470 | || bfd_bread (extshndx_buf, amt, ibfd) != amt) | |
471 | { | |
472 | intsym_buf = NULL; | |
473 | goto out; | |
474 | } | |
475 | } | |
476 | ||
477 | if (intsym_buf == NULL) | |
478 | { | |
a50b1753 NC |
479 | alloc_intsym = (Elf_Internal_Sym *) |
480 | bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); | |
4dd07732 | 481 | intsym_buf = alloc_intsym; |
6cdc0ccc AM |
482 | if (intsym_buf == NULL) |
483 | goto out; | |
484 | } | |
485 | ||
486 | /* Convert the symbols to internal form. */ | |
487 | isymend = intsym_buf + symcount; | |
a50b1753 NC |
488 | for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, |
489 | shndx = extshndx_buf; | |
6cdc0ccc AM |
490 | isym < isymend; |
491 | esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) | |
8384fb8f AM |
492 | if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) |
493 | { | |
494 | symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; | |
695344c0 | 495 | /* xgettext:c-format */ |
63a5468a AM |
496 | _bfd_error_handler (_("%B symbol number %lu references" |
497 | " nonexistent SHT_SYMTAB_SHNDX section"), | |
4eca0228 | 498 | ibfd, (unsigned long) symoffset); |
4dd07732 AM |
499 | if (alloc_intsym != NULL) |
500 | free (alloc_intsym); | |
8384fb8f AM |
501 | intsym_buf = NULL; |
502 | goto out; | |
503 | } | |
6cdc0ccc AM |
504 | |
505 | out: | |
506 | if (alloc_ext != NULL) | |
507 | free (alloc_ext); | |
508 | if (alloc_extshndx != NULL) | |
509 | free (alloc_extshndx); | |
510 | ||
511 | return intsym_buf; | |
512 | } | |
513 | ||
5cab59f6 AM |
514 | /* Look up a symbol name. */ |
515 | const char * | |
be8dd2ca AM |
516 | bfd_elf_sym_name (bfd *abfd, |
517 | Elf_Internal_Shdr *symtab_hdr, | |
26c61ae5 L |
518 | Elf_Internal_Sym *isym, |
519 | asection *sym_sec) | |
5cab59f6 | 520 | { |
26c61ae5 | 521 | const char *name; |
5cab59f6 | 522 | unsigned int iname = isym->st_name; |
be8dd2ca | 523 | unsigned int shindex = symtab_hdr->sh_link; |
26c61ae5 | 524 | |
138f35cc JJ |
525 | if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION |
526 | /* Check for a bogus st_shndx to avoid crashing. */ | |
4fbb74a6 | 527 | && isym->st_shndx < elf_numsections (abfd)) |
5cab59f6 AM |
528 | { |
529 | iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; | |
530 | shindex = elf_elfheader (abfd)->e_shstrndx; | |
531 | } | |
532 | ||
26c61ae5 L |
533 | name = bfd_elf_string_from_elf_section (abfd, shindex, iname); |
534 | if (name == NULL) | |
535 | name = "(null)"; | |
536 | else if (sym_sec && *name == '\0') | |
537 | name = bfd_section_name (abfd, sym_sec); | |
538 | ||
539 | return name; | |
5cab59f6 AM |
540 | } |
541 | ||
dbb410c3 AM |
542 | /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP |
543 | sections. The first element is the flags, the rest are section | |
544 | pointers. */ | |
545 | ||
546 | typedef union elf_internal_group { | |
547 | Elf_Internal_Shdr *shdr; | |
548 | unsigned int flags; | |
549 | } Elf_Internal_Group; | |
550 | ||
b885599b AM |
551 | /* Return the name of the group signature symbol. Why isn't the |
552 | signature just a string? */ | |
553 | ||
554 | static const char * | |
217aa764 | 555 | group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) |
b885599b | 556 | { |
9dce4196 | 557 | Elf_Internal_Shdr *hdr; |
9dce4196 AM |
558 | unsigned char esym[sizeof (Elf64_External_Sym)]; |
559 | Elf_External_Sym_Shndx eshndx; | |
560 | Elf_Internal_Sym isym; | |
b885599b | 561 | |
13792e9d L |
562 | /* First we need to ensure the symbol table is available. Make sure |
563 | that it is a symbol table section. */ | |
4fbb74a6 AM |
564 | if (ghdr->sh_link >= elf_numsections (abfd)) |
565 | return NULL; | |
13792e9d L |
566 | hdr = elf_elfsections (abfd) [ghdr->sh_link]; |
567 | if (hdr->sh_type != SHT_SYMTAB | |
568 | || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) | |
b885599b AM |
569 | return NULL; |
570 | ||
9dce4196 AM |
571 | /* Go read the symbol. */ |
572 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
6cdc0ccc AM |
573 | if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, |
574 | &isym, esym, &eshndx) == NULL) | |
b885599b | 575 | return NULL; |
9dce4196 | 576 | |
26c61ae5 | 577 | return bfd_elf_sym_name (abfd, hdr, &isym, NULL); |
b885599b AM |
578 | } |
579 | ||
dbb410c3 AM |
580 | /* Set next_in_group list pointer, and group name for NEWSECT. */ |
581 | ||
b34976b6 | 582 | static bfd_boolean |
217aa764 | 583 | setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) |
dbb410c3 AM |
584 | { |
585 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
586 | ||
587 | /* If num_group is zero, read in all SHT_GROUP sections. The count | |
588 | is set to -1 if there are no SHT_GROUP sections. */ | |
589 | if (num_group == 0) | |
590 | { | |
591 | unsigned int i, shnum; | |
592 | ||
593 | /* First count the number of groups. If we have a SHT_GROUP | |
594 | section with just a flag word (ie. sh_size is 4), ignore it. */ | |
9ad5cbcf | 595 | shnum = elf_numsections (abfd); |
dbb410c3 | 596 | num_group = 0; |
08a40648 | 597 | |
44534af3 | 598 | #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ |
1783205a | 599 | ( (shdr)->sh_type == SHT_GROUP \ |
44534af3 | 600 | && (shdr)->sh_size >= minsize \ |
1783205a NC |
601 | && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ |
602 | && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) | |
08a40648 | 603 | |
dbb410c3 AM |
604 | for (i = 0; i < shnum; i++) |
605 | { | |
606 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 607 | |
44534af3 | 608 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 AM |
609 | num_group += 1; |
610 | } | |
611 | ||
612 | if (num_group == 0) | |
20dbb49d L |
613 | { |
614 | num_group = (unsigned) -1; | |
615 | elf_tdata (abfd)->num_group = num_group; | |
ce497010 | 616 | elf_tdata (abfd)->group_sect_ptr = NULL; |
20dbb49d L |
617 | } |
618 | else | |
dbb410c3 AM |
619 | { |
620 | /* We keep a list of elf section headers for group sections, | |
621 | so we can find them quickly. */ | |
20dbb49d | 622 | bfd_size_type amt; |
d0fb9a8d | 623 | |
20dbb49d | 624 | elf_tdata (abfd)->num_group = num_group; |
a50b1753 NC |
625 | elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **) |
626 | bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); | |
dbb410c3 | 627 | if (elf_tdata (abfd)->group_sect_ptr == NULL) |
b34976b6 | 628 | return FALSE; |
ce497010 | 629 | memset (elf_tdata (abfd)->group_sect_ptr, 0, num_group * sizeof (Elf_Internal_Shdr *)); |
dbb410c3 | 630 | num_group = 0; |
ce497010 | 631 | |
dbb410c3 AM |
632 | for (i = 0; i < shnum; i++) |
633 | { | |
634 | Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; | |
1783205a | 635 | |
44534af3 | 636 | if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) |
dbb410c3 | 637 | { |
973ffd63 | 638 | unsigned char *src; |
dbb410c3 AM |
639 | Elf_Internal_Group *dest; |
640 | ||
78e8a2ff JM |
641 | /* Make sure the group section has a BFD section |
642 | attached to it. */ | |
643 | if (!bfd_section_from_shdr (abfd, i)) | |
644 | return FALSE; | |
645 | ||
dbb410c3 AM |
646 | /* Add to list of sections. */ |
647 | elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; | |
648 | num_group += 1; | |
649 | ||
650 | /* Read the raw contents. */ | |
651 | BFD_ASSERT (sizeof (*dest) >= 4); | |
652 | amt = shdr->sh_size * sizeof (*dest) / 4; | |
a50b1753 NC |
653 | shdr->contents = (unsigned char *) |
654 | bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4); | |
1783205a NC |
655 | /* PR binutils/4110: Handle corrupt group headers. */ |
656 | if (shdr->contents == NULL) | |
657 | { | |
658 | _bfd_error_handler | |
695344c0 | 659 | /* xgettext:c-format */ |
63a5468a | 660 | (_("%B: corrupt size field in group section" |
76cfced5 | 661 | " header: %#Lx"), abfd, shdr->sh_size); |
1783205a | 662 | bfd_set_error (bfd_error_bad_value); |
493a3386 NC |
663 | -- num_group; |
664 | continue; | |
1783205a NC |
665 | } |
666 | ||
667 | memset (shdr->contents, 0, amt); | |
668 | ||
669 | if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 | |
dbb410c3 AM |
670 | || (bfd_bread (shdr->contents, shdr->sh_size, abfd) |
671 | != shdr->sh_size)) | |
493a3386 NC |
672 | { |
673 | _bfd_error_handler | |
695344c0 | 674 | /* xgettext:c-format */ |
63a5468a | 675 | (_("%B: invalid size field in group section" |
76cfced5 | 676 | " header: %#Lx"), abfd, shdr->sh_size); |
493a3386 NC |
677 | bfd_set_error (bfd_error_bad_value); |
678 | -- num_group; | |
63a5468a AM |
679 | /* PR 17510: If the group contents are even |
680 | partially corrupt, do not allow any of the | |
681 | contents to be used. */ | |
493a3386 NC |
682 | memset (shdr->contents, 0, amt); |
683 | continue; | |
684 | } | |
708d7d0d | 685 | |
dbb410c3 AM |
686 | /* Translate raw contents, a flag word followed by an |
687 | array of elf section indices all in target byte order, | |
688 | to the flag word followed by an array of elf section | |
689 | pointers. */ | |
690 | src = shdr->contents + shdr->sh_size; | |
691 | dest = (Elf_Internal_Group *) (shdr->contents + amt); | |
06614111 | 692 | |
dbb410c3 AM |
693 | while (1) |
694 | { | |
695 | unsigned int idx; | |
696 | ||
697 | src -= 4; | |
698 | --dest; | |
699 | idx = H_GET_32 (abfd, src); | |
700 | if (src == shdr->contents) | |
701 | { | |
702 | dest->flags = idx; | |
b885599b AM |
703 | if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) |
704 | shdr->bfd_section->flags | |
705 | |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; | |
dbb410c3 AM |
706 | break; |
707 | } | |
708 | if (idx >= shnum) | |
709 | { | |
4eca0228 AM |
710 | _bfd_error_handler |
711 | (_("%B: invalid SHT_GROUP entry"), abfd); | |
dbb410c3 AM |
712 | idx = 0; |
713 | } | |
714 | dest->shdr = elf_elfsections (abfd)[idx]; | |
715 | } | |
716 | } | |
717 | } | |
493a3386 NC |
718 | |
719 | /* PR 17510: Corrupt binaries might contain invalid groups. */ | |
720 | if (num_group != (unsigned) elf_tdata (abfd)->num_group) | |
721 | { | |
722 | elf_tdata (abfd)->num_group = num_group; | |
723 | ||
724 | /* If all groups are invalid then fail. */ | |
725 | if (num_group == 0) | |
726 | { | |
727 | elf_tdata (abfd)->group_sect_ptr = NULL; | |
728 | elf_tdata (abfd)->num_group = num_group = -1; | |
4eca0228 AM |
729 | _bfd_error_handler |
730 | (_("%B: no valid group sections found"), abfd); | |
493a3386 NC |
731 | bfd_set_error (bfd_error_bad_value); |
732 | } | |
733 | } | |
dbb410c3 AM |
734 | } |
735 | } | |
736 | ||
737 | if (num_group != (unsigned) -1) | |
738 | { | |
739 | unsigned int i; | |
740 | ||
741 | for (i = 0; i < num_group; i++) | |
742 | { | |
743 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
ce497010 NC |
744 | Elf_Internal_Group *idx; |
745 | unsigned int n_elt; | |
746 | ||
747 | if (shdr == NULL) | |
748 | continue; | |
749 | ||
750 | idx = (Elf_Internal_Group *) shdr->contents; | |
751 | n_elt = shdr->sh_size / 4; | |
dbb410c3 AM |
752 | |
753 | /* Look through this group's sections to see if current | |
754 | section is a member. */ | |
755 | while (--n_elt != 0) | |
756 | if ((++idx)->shdr == hdr) | |
757 | { | |
e0e8c97f | 758 | asection *s = NULL; |
dbb410c3 AM |
759 | |
760 | /* We are a member of this group. Go looking through | |
761 | other members to see if any others are linked via | |
762 | next_in_group. */ | |
763 | idx = (Elf_Internal_Group *) shdr->contents; | |
764 | n_elt = shdr->sh_size / 4; | |
765 | while (--n_elt != 0) | |
766 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 767 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
768 | break; |
769 | if (n_elt != 0) | |
770 | { | |
dbb410c3 AM |
771 | /* Snarf the group name from other member, and |
772 | insert current section in circular list. */ | |
945906ff AM |
773 | elf_group_name (newsect) = elf_group_name (s); |
774 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
775 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
776 | } |
777 | else | |
778 | { | |
dbb410c3 AM |
779 | const char *gname; |
780 | ||
b885599b AM |
781 | gname = group_signature (abfd, shdr); |
782 | if (gname == NULL) | |
b34976b6 | 783 | return FALSE; |
945906ff | 784 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
785 | |
786 | /* Start a circular list with one element. */ | |
945906ff | 787 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 788 | } |
b885599b | 789 | |
9dce4196 AM |
790 | /* If the group section has been created, point to the |
791 | new member. */ | |
dbb410c3 | 792 | if (shdr->bfd_section != NULL) |
945906ff | 793 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 794 | |
dbb410c3 AM |
795 | i = num_group - 1; |
796 | break; | |
797 | } | |
798 | } | |
799 | } | |
800 | ||
945906ff | 801 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 802 | { |
695344c0 | 803 | /* xgettext:c-format */ |
4eca0228 AM |
804 | _bfd_error_handler (_("%B: no group info for section %A"), |
805 | abfd, newsect); | |
493a3386 | 806 | return FALSE; |
dbb410c3 | 807 | } |
b34976b6 | 808 | return TRUE; |
dbb410c3 AM |
809 | } |
810 | ||
3d7f7666 | 811 | bfd_boolean |
dd863624 | 812 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
813 | { |
814 | unsigned int i; | |
815 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
816 | bfd_boolean result = TRUE; | |
dd863624 L |
817 | asection *s; |
818 | ||
819 | /* Process SHF_LINK_ORDER. */ | |
820 | for (s = abfd->sections; s != NULL; s = s->next) | |
821 | { | |
822 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
823 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
824 | { | |
825 | unsigned int elfsec = this_hdr->sh_link; | |
826 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
827 | not set the sh_link or sh_info fields. Hence we could | |
828 | get the situation where elfsec is 0. */ | |
829 | if (elfsec == 0) | |
830 | { | |
4fbb74a6 | 831 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
832 | if (bed->link_order_error_handler) |
833 | bed->link_order_error_handler | |
695344c0 | 834 | /* xgettext:c-format */ |
dd863624 L |
835 | (_("%B: warning: sh_link not set for section `%A'"), |
836 | abfd, s); | |
837 | } | |
838 | else | |
839 | { | |
91d6fa6a | 840 | asection *linksec = NULL; |
25bbc984 | 841 | |
4fbb74a6 AM |
842 | if (elfsec < elf_numsections (abfd)) |
843 | { | |
844 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 845 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 846 | } |
25bbc984 L |
847 | |
848 | /* PR 1991, 2008: | |
849 | Some strip/objcopy may leave an incorrect value in | |
850 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 851 | if (linksec == NULL) |
25bbc984 | 852 | { |
4eca0228 | 853 | _bfd_error_handler |
695344c0 | 854 | /* xgettext:c-format */ |
25bbc984 | 855 | (_("%B: sh_link [%d] in section `%A' is incorrect"), |
c08bb8dd | 856 | s->owner, elfsec, s); |
25bbc984 L |
857 | result = FALSE; |
858 | } | |
859 | ||
91d6fa6a | 860 | elf_linked_to_section (s) = linksec; |
dd863624 L |
861 | } |
862 | } | |
53720c49 AM |
863 | else if (this_hdr->sh_type == SHT_GROUP |
864 | && elf_next_in_group (s) == NULL) | |
865 | { | |
4eca0228 | 866 | _bfd_error_handler |
695344c0 | 867 | /* xgettext:c-format */ |
53720c49 AM |
868 | (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
869 | abfd, elf_section_data (s)->this_idx); | |
870 | result = FALSE; | |
871 | } | |
dd863624 | 872 | } |
3d7f7666 | 873 | |
dd863624 | 874 | /* Process section groups. */ |
3d7f7666 L |
875 | if (num_group == (unsigned) -1) |
876 | return result; | |
877 | ||
878 | for (i = 0; i < num_group; i++) | |
879 | { | |
880 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
881 | Elf_Internal_Group *idx; |
882 | unsigned int n_elt; | |
3d7f7666 | 883 | |
4b0e8a5f NC |
884 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
885 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
886 | { | |
4eca0228 | 887 | _bfd_error_handler |
695344c0 | 888 | /* xgettext:c-format */ |
4b0e8a5f NC |
889 | (_("%B: section group entry number %u is corrupt"), |
890 | abfd, i); | |
891 | result = FALSE; | |
892 | continue; | |
893 | } | |
894 | ||
895 | idx = (Elf_Internal_Group *) shdr->contents; | |
896 | n_elt = shdr->sh_size / 4; | |
1b786873 | 897 | |
3d7f7666 L |
898 | while (--n_elt != 0) |
899 | if ((++idx)->shdr->bfd_section) | |
900 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
901 | else if (idx->shdr->sh_type == SHT_RELA | |
902 | || idx->shdr->sh_type == SHT_REL) | |
903 | /* We won't include relocation sections in section groups in | |
904 | output object files. We adjust the group section size here | |
905 | so that relocatable link will work correctly when | |
906 | relocation sections are in section group in input object | |
907 | files. */ | |
908 | shdr->bfd_section->size -= 4; | |
909 | else | |
910 | { | |
911 | /* There are some unknown sections in the group. */ | |
4eca0228 | 912 | _bfd_error_handler |
695344c0 | 913 | /* xgettext:c-format */ |
76cfced5 | 914 | (_("%B: unknown type [%#x] section `%s' in group [%A]"), |
d003868e | 915 | abfd, |
76cfced5 | 916 | idx->shdr->sh_type, |
1b3a8575 AM |
917 | bfd_elf_string_from_elf_section (abfd, |
918 | (elf_elfheader (abfd) | |
919 | ->e_shstrndx), | |
920 | idx->shdr->sh_name), | |
dae82561 | 921 | shdr->bfd_section); |
3d7f7666 L |
922 | result = FALSE; |
923 | } | |
924 | } | |
925 | return result; | |
926 | } | |
927 | ||
72adc230 AM |
928 | bfd_boolean |
929 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
930 | { | |
931 | return elf_next_in_group (sec) != NULL; | |
932 | } | |
933 | ||
f6fe1ccd L |
934 | static char * |
935 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
936 | { | |
937 | unsigned int len = strlen (name); | |
938 | char *new_name = bfd_alloc (abfd, len + 2); | |
939 | if (new_name == NULL) | |
940 | return NULL; | |
941 | new_name[0] = '.'; | |
942 | new_name[1] = 'z'; | |
943 | memcpy (new_name + 2, name + 1, len); | |
944 | return new_name; | |
945 | } | |
946 | ||
947 | static char * | |
948 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
949 | { | |
950 | unsigned int len = strlen (name); | |
951 | char *new_name = bfd_alloc (abfd, len); | |
952 | if (new_name == NULL) | |
953 | return NULL; | |
954 | new_name[0] = '.'; | |
955 | memcpy (new_name + 1, name + 2, len - 1); | |
956 | return new_name; | |
957 | } | |
958 | ||
252b5132 RH |
959 | /* Make a BFD section from an ELF section. We store a pointer to the |
960 | BFD section in the bfd_section field of the header. */ | |
961 | ||
b34976b6 | 962 | bfd_boolean |
217aa764 AM |
963 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
964 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
965 | const char *name, |
966 | int shindex) | |
252b5132 RH |
967 | { |
968 | asection *newsect; | |
969 | flagword flags; | |
9c5bfbb7 | 970 | const struct elf_backend_data *bed; |
252b5132 RH |
971 | |
972 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 973 | return TRUE; |
252b5132 RH |
974 | |
975 | newsect = bfd_make_section_anyway (abfd, name); | |
976 | if (newsect == NULL) | |
b34976b6 | 977 | return FALSE; |
252b5132 | 978 | |
1829f4b2 AM |
979 | hdr->bfd_section = newsect; |
980 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 981 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 982 | |
2f89ff8d L |
983 | /* Always use the real type/flags. */ |
984 | elf_section_type (newsect) = hdr->sh_type; | |
985 | elf_section_flags (newsect) = hdr->sh_flags; | |
986 | ||
252b5132 RH |
987 | newsect->filepos = hdr->sh_offset; |
988 | ||
989 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
990 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
991 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 992 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 993 | return FALSE; |
252b5132 RH |
994 | |
995 | flags = SEC_NO_FLAGS; | |
996 | if (hdr->sh_type != SHT_NOBITS) | |
997 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 998 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 999 | flags |= SEC_GROUP; |
252b5132 RH |
1000 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1001 | { | |
1002 | flags |= SEC_ALLOC; | |
1003 | if (hdr->sh_type != SHT_NOBITS) | |
1004 | flags |= SEC_LOAD; | |
1005 | } | |
1006 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1007 | flags |= SEC_READONLY; | |
1008 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1009 | flags |= SEC_CODE; | |
1010 | else if ((flags & SEC_LOAD) != 0) | |
1011 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1012 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1013 | { | |
1014 | flags |= SEC_MERGE; | |
1015 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1016 | } |
84865015 NC |
1017 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1018 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1019 | if (hdr->sh_flags & SHF_GROUP) |
1020 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1021 | return FALSE; |
13ae64f3 JJ |
1022 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1023 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1024 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1025 | flags |= SEC_EXCLUDE; | |
252b5132 | 1026 | |
3d2b39cf | 1027 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1028 | { |
3d2b39cf L |
1029 | /* The debugging sections appear to be recognized only by name, |
1030 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1031 | if (name [0] == '.') |
1032 | { | |
f073ced3 AM |
1033 | const char *p; |
1034 | int n; | |
1035 | if (name[1] == 'd') | |
1036 | p = ".debug", n = 6; | |
1037 | else if (name[1] == 'g' && name[2] == 'n') | |
1038 | p = ".gnu.linkonce.wi.", n = 17; | |
1039 | else if (name[1] == 'g' && name[2] == 'd') | |
1040 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
1041 | else if (name[1] == 'l') | |
1042 | p = ".line", n = 5; | |
1043 | else if (name[1] == 's') | |
1044 | p = ".stab", n = 5; | |
1045 | else if (name[1] == 'z') | |
1046 | p = ".zdebug", n = 7; | |
1047 | else | |
1048 | p = NULL, n = 0; | |
1049 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1050 | flags |= SEC_DEBUGGING; |
1051 | } | |
1052 | } | |
252b5132 RH |
1053 | |
1054 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1055 | only link a single copy of the section. This is used to support | |
1056 | g++. g++ will emit each template expansion in its own section. | |
1057 | The symbols will be defined as weak, so that multiple definitions | |
1058 | are permitted. The GNU linker extension is to actually discard | |
1059 | all but one of the sections. */ | |
0112cd26 | 1060 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1061 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1062 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1063 | ||
fa152c49 JW |
1064 | bed = get_elf_backend_data (abfd); |
1065 | if (bed->elf_backend_section_flags) | |
1066 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1067 | return FALSE; |
fa152c49 | 1068 | |
252b5132 | 1069 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1070 | return FALSE; |
252b5132 | 1071 | |
718175fa JK |
1072 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1073 | separate debug info files which may have the segments offsets corrupted. | |
1074 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1075 | if (hdr->sh_type == SHT_NOTE) | |
1076 | { | |
baea7ef1 | 1077 | bfd_byte *contents; |
718175fa | 1078 | |
baea7ef1 | 1079 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1080 | return FALSE; |
1081 | ||
08698b7b | 1082 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, hdr->sh_offset); |
718175fa JK |
1083 | free (contents); |
1084 | } | |
1085 | ||
252b5132 RH |
1086 | if ((flags & SEC_ALLOC) != 0) |
1087 | { | |
1088 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1089 | unsigned int i, nload; |
1090 | ||
1091 | /* Some ELF linkers produce binaries with all the program header | |
1092 | p_paddr fields zero. If we have such a binary with more than | |
1093 | one PT_LOAD header, then leave the section lma equal to vma | |
1094 | so that we don't create sections with overlapping lma. */ | |
1095 | phdr = elf_tdata (abfd)->phdr; | |
1096 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1097 | if (phdr->p_paddr != 0) | |
1098 | break; | |
1099 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1100 | ++nload; | |
1101 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1102 | return TRUE; | |
252b5132 | 1103 | |
252b5132 RH |
1104 | phdr = elf_tdata (abfd)->phdr; |
1105 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1106 | { | |
86b2281f AM |
1107 | if (((phdr->p_type == PT_LOAD |
1108 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1109 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1110 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1111 | { |
88967714 AM |
1112 | if ((flags & SEC_LOAD) == 0) |
1113 | newsect->lma = (phdr->p_paddr | |
1114 | + hdr->sh_addr - phdr->p_vaddr); | |
1115 | else | |
1116 | /* We used to use the same adjustment for SEC_LOAD | |
1117 | sections, but that doesn't work if the segment | |
1118 | is packed with code from multiple VMAs. | |
1119 | Instead we calculate the section LMA based on | |
1120 | the segment LMA. It is assumed that the | |
1121 | segment will contain sections with contiguous | |
1122 | LMAs, even if the VMAs are not. */ | |
1123 | newsect->lma = (phdr->p_paddr | |
1124 | + hdr->sh_offset - phdr->p_offset); | |
1125 | ||
1126 | /* With contiguous segments, we can't tell from file | |
1127 | offsets whether a section with zero size should | |
1128 | be placed at the end of one segment or the | |
1129 | beginning of the next. Decide based on vaddr. */ | |
1130 | if (hdr->sh_addr >= phdr->p_vaddr | |
1131 | && (hdr->sh_addr + hdr->sh_size | |
1132 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1133 | break; | |
252b5132 RH |
1134 | } |
1135 | } | |
1136 | } | |
1137 | ||
4a114e3e L |
1138 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1139 | .zdebug_*, after the section flags is set. */ | |
1140 | if ((flags & SEC_DEBUGGING) | |
1141 | && ((name[1] == 'd' && name[6] == '_') | |
1142 | || (name[1] == 'z' && name[7] == '_'))) | |
1143 | { | |
1144 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1145 | int compression_header_size; |
dab394de | 1146 | bfd_size_type uncompressed_size; |
151411f8 L |
1147 | bfd_boolean compressed |
1148 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1149 | &compression_header_size, |
1150 | &uncompressed_size); | |
4a114e3e | 1151 | |
151411f8 | 1152 | if (compressed) |
4a114e3e L |
1153 | { |
1154 | /* Compressed section. Check if we should decompress. */ | |
1155 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1156 | action = decompress; | |
1157 | } | |
151411f8 L |
1158 | |
1159 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1160 | section. Check if we should compress. */ | |
1161 | if (action == nothing) | |
4a114e3e | 1162 | { |
151411f8 L |
1163 | if (newsect->size != 0 |
1164 | && (abfd->flags & BFD_COMPRESS) | |
1165 | && compression_header_size >= 0 | |
dab394de | 1166 | && uncompressed_size > 0 |
151411f8 L |
1167 | && (!compressed |
1168 | || ((compression_header_size > 0) | |
1169 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1170 | action = compress; |
151411f8 L |
1171 | else |
1172 | return TRUE; | |
4a114e3e L |
1173 | } |
1174 | ||
151411f8 | 1175 | if (action == compress) |
4a114e3e | 1176 | { |
4a114e3e L |
1177 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1178 | { | |
4eca0228 | 1179 | _bfd_error_handler |
695344c0 | 1180 | /* xgettext:c-format */ |
bc823199 | 1181 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1182 | abfd, name); |
1183 | return FALSE; | |
1184 | } | |
151411f8 L |
1185 | } |
1186 | else | |
1187 | { | |
4a114e3e L |
1188 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1189 | { | |
4eca0228 | 1190 | _bfd_error_handler |
695344c0 | 1191 | /* xgettext:c-format */ |
bc823199 | 1192 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1193 | abfd, name); |
1194 | return FALSE; | |
1195 | } | |
151411f8 L |
1196 | } |
1197 | ||
f6fe1ccd | 1198 | if (abfd->is_linker_input) |
151411f8 | 1199 | { |
f6fe1ccd L |
1200 | if (name[1] == 'z' |
1201 | && (action == decompress | |
1202 | || (action == compress | |
1203 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1204 | { |
f6fe1ccd L |
1205 | /* Convert section name from .zdebug_* to .debug_* so |
1206 | that linker will consider this section as a debug | |
1207 | section. */ | |
1208 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1209 | if (new_name == NULL) |
1210 | return FALSE; | |
f6fe1ccd | 1211 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1212 | } |
4a114e3e | 1213 | } |
f6fe1ccd L |
1214 | else |
1215 | /* For objdump, don't rename the section. For objcopy, delay | |
1216 | section rename to elf_fake_sections. */ | |
1217 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1218 | } |
1219 | ||
b34976b6 | 1220 | return TRUE; |
252b5132 RH |
1221 | } |
1222 | ||
84865015 NC |
1223 | const char *const bfd_elf_section_type_names[] = |
1224 | { | |
252b5132 RH |
1225 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1226 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1227 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1228 | }; | |
1229 | ||
1049f94e | 1230 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1231 | output, and the reloc is against an external symbol, and nothing |
1232 | has given us any additional addend, the resulting reloc will also | |
1233 | be against the same symbol. In such a case, we don't want to | |
1234 | change anything about the way the reloc is handled, since it will | |
1235 | all be done at final link time. Rather than put special case code | |
1236 | into bfd_perform_relocation, all the reloc types use this howto | |
1237 | function. It just short circuits the reloc if producing | |
1049f94e | 1238 | relocatable output against an external symbol. */ |
252b5132 | 1239 | |
252b5132 | 1240 | bfd_reloc_status_type |
217aa764 AM |
1241 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1242 | arelent *reloc_entry, | |
1243 | asymbol *symbol, | |
1244 | void *data ATTRIBUTE_UNUSED, | |
1245 | asection *input_section, | |
1246 | bfd *output_bfd, | |
1247 | char **error_message ATTRIBUTE_UNUSED) | |
1248 | { | |
1249 | if (output_bfd != NULL | |
252b5132 RH |
1250 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1251 | && (! reloc_entry->howto->partial_inplace | |
1252 | || reloc_entry->addend == 0)) | |
1253 | { | |
1254 | reloc_entry->address += input_section->output_offset; | |
1255 | return bfd_reloc_ok; | |
1256 | } | |
1257 | ||
1258 | return bfd_reloc_continue; | |
1259 | } | |
1260 | \f | |
84865015 NC |
1261 | /* Returns TRUE if section A matches section B. |
1262 | Names, addresses and links may be different, but everything else | |
1263 | should be the same. */ | |
1264 | ||
1265 | static bfd_boolean | |
5522f910 NC |
1266 | section_match (const Elf_Internal_Shdr * a, |
1267 | const Elf_Internal_Shdr * b) | |
84865015 NC |
1268 | { |
1269 | return | |
1270 | a->sh_type == b->sh_type | |
5522f910 NC |
1271 | && (a->sh_flags & ~ SHF_INFO_LINK) |
1272 | == (b->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1273 | && a->sh_addralign == b->sh_addralign |
1274 | && a->sh_size == b->sh_size | |
1275 | && a->sh_entsize == b->sh_entsize | |
1276 | /* FIXME: Check sh_addr ? */ | |
1277 | ; | |
1278 | } | |
1279 | ||
1280 | /* Find a section in OBFD that has the same characteristics | |
1281 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1282 | none can be found. Check's section HINT first, as this is likely | |
1283 | to be the correct section. */ | |
1284 | ||
1285 | static unsigned int | |
5cc4ca83 ST |
1286 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1287 | const unsigned int hint) | |
84865015 NC |
1288 | { |
1289 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1290 | unsigned int i; | |
1291 | ||
a55c9876 NC |
1292 | BFD_ASSERT (iheader != NULL); |
1293 | ||
1294 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1295 | if (hint < elf_numsections (obfd) |
1296 | && oheaders[hint] != NULL | |
a55c9876 | 1297 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1298 | return hint; |
1299 | ||
1300 | for (i = 1; i < elf_numsections (obfd); i++) | |
1301 | { | |
1302 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1303 | ||
a55c9876 NC |
1304 | if (oheader == NULL) |
1305 | continue; | |
84865015 NC |
1306 | if (section_match (oheader, iheader)) |
1307 | /* FIXME: Do we care if there is a potential for | |
1308 | multiple matches ? */ | |
1309 | return i; | |
1310 | } | |
1311 | ||
1312 | return SHN_UNDEF; | |
1313 | } | |
1314 | ||
5522f910 NC |
1315 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1316 | Processor specific section, based upon a matching input section. | |
1317 | Returns TRUE upon success, FALSE otherwise. */ | |
1318 | ||
1319 | static bfd_boolean | |
1320 | copy_special_section_fields (const bfd *ibfd, | |
1321 | bfd *obfd, | |
1322 | const Elf_Internal_Shdr *iheader, | |
1323 | Elf_Internal_Shdr *oheader, | |
1324 | const unsigned int secnum) | |
1325 | { | |
1326 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1327 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1328 | bfd_boolean changed = FALSE; | |
1329 | unsigned int sh_link; | |
1330 | ||
1331 | if (oheader->sh_type == SHT_NOBITS) | |
1332 | { | |
1333 | /* This is a feature for objcopy --only-keep-debug: | |
1334 | When a section's type is changed to NOBITS, we preserve | |
1335 | the sh_link and sh_info fields so that they can be | |
1336 | matched up with the original. | |
1337 | ||
1338 | Note: Strictly speaking these assignments are wrong. | |
1339 | The sh_link and sh_info fields should point to the | |
1340 | relevent sections in the output BFD, which may not be in | |
1341 | the same location as they were in the input BFD. But | |
1342 | the whole point of this action is to preserve the | |
1343 | original values of the sh_link and sh_info fields, so | |
1344 | that they can be matched up with the section headers in | |
1345 | the original file. So strictly speaking we may be | |
1346 | creating an invalid ELF file, but it is only for a file | |
1347 | that just contains debug info and only for sections | |
1348 | without any contents. */ | |
1349 | if (oheader->sh_link == 0) | |
1350 | oheader->sh_link = iheader->sh_link; | |
1351 | if (oheader->sh_info == 0) | |
1352 | oheader->sh_info = iheader->sh_info; | |
1353 | return TRUE; | |
1354 | } | |
1355 | ||
1356 | /* Allow the target a chance to decide how these fields should be set. */ | |
1357 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1358 | && bed->elf_backend_copy_special_section_fields | |
1359 | (ibfd, obfd, iheader, oheader)) | |
1360 | return TRUE; | |
1361 | ||
1362 | /* We have an iheader which might match oheader, and which has non-zero | |
1363 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1364 | the section in the output bfd which corresponds to the linked section | |
1365 | in the input bfd. */ | |
1366 | if (iheader->sh_link != SHN_UNDEF) | |
1367 | { | |
4f3ca05b NC |
1368 | /* See PR 20931 for a reproducer. */ |
1369 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1370 | { | |
76cfced5 | 1371 | _bfd_error_handler |
4f3ca05b NC |
1372 | /* xgettext:c-format */ |
1373 | (_("%B: Invalid sh_link field (%d) in section number %d"), | |
1374 | ibfd, iheader->sh_link, secnum); | |
1375 | return FALSE; | |
1376 | } | |
1377 | ||
5522f910 NC |
1378 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1379 | if (sh_link != SHN_UNDEF) | |
1380 | { | |
1381 | oheader->sh_link = sh_link; | |
1382 | changed = TRUE; | |
1383 | } | |
1384 | else | |
1385 | /* FIXME: Should we install iheader->sh_link | |
1386 | if we could not find a match ? */ | |
76cfced5 | 1387 | _bfd_error_handler |
695344c0 | 1388 | /* xgettext:c-format */ |
5522f910 NC |
1389 | (_("%B: Failed to find link section for section %d"), obfd, secnum); |
1390 | } | |
1391 | ||
1392 | if (iheader->sh_info) | |
1393 | { | |
1394 | /* The sh_info field can hold arbitrary information, but if the | |
1395 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1396 | section index. */ | |
1397 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1398 | { | |
1399 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1400 | iheader->sh_info); | |
1401 | if (sh_link != SHN_UNDEF) | |
1402 | oheader->sh_flags |= SHF_INFO_LINK; | |
1403 | } | |
1404 | else | |
1405 | /* No idea what it means - just copy it. */ | |
1406 | sh_link = iheader->sh_info; | |
1407 | ||
1408 | if (sh_link != SHN_UNDEF) | |
1409 | { | |
1410 | oheader->sh_info = sh_link; | |
1411 | changed = TRUE; | |
1412 | } | |
1413 | else | |
76cfced5 | 1414 | _bfd_error_handler |
695344c0 | 1415 | /* xgettext:c-format */ |
5522f910 NC |
1416 | (_("%B: Failed to find info section for section %d"), obfd, secnum); |
1417 | } | |
1418 | ||
1419 | return changed; | |
1420 | } | |
1421 | ||
0ac4564e L |
1422 | /* Copy the program header and other data from one object module to |
1423 | another. */ | |
252b5132 | 1424 | |
b34976b6 | 1425 | bfd_boolean |
217aa764 | 1426 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1427 | { |
5522f910 NC |
1428 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1429 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1430 | const struct elf_backend_data *bed; | |
84865015 NC |
1431 | unsigned int i; |
1432 | ||
2d502050 | 1433 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1434 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1435 | return TRUE; |
2d502050 | 1436 | |
57b828ef L |
1437 | if (!elf_flags_init (obfd)) |
1438 | { | |
1439 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1440 | elf_flags_init (obfd) = TRUE; | |
1441 | } | |
2d502050 | 1442 | |
0ac4564e | 1443 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1444 | |
1445 | /* Also copy the EI_OSABI field. */ | |
1446 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1447 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1448 | |
5522f910 NC |
1449 | /* If set, copy the EI_ABIVERSION field. */ |
1450 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1451 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1452 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
1453 | ||
104d59d1 JM |
1454 | /* Copy object attributes. */ |
1455 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1456 | |
84865015 NC |
1457 | if (iheaders == NULL || oheaders == NULL) |
1458 | return TRUE; | |
63b9bbb7 | 1459 | |
5522f910 NC |
1460 | bed = get_elf_backend_data (obfd); |
1461 | ||
1462 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1463 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1464 | { |
84865015 NC |
1465 | unsigned int j; |
1466 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1467 | |
5522f910 NC |
1468 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1469 | because of a special case need for generating separate debug info | |
1470 | files. See below for more details. */ | |
84865015 NC |
1471 | if (oheader == NULL |
1472 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1473 | && oheader->sh_type < SHT_LOOS)) |
1474 | continue; | |
1475 | ||
1476 | /* Ignore empty sections, and sections whose | |
1477 | fields have already been initialised. */ | |
1478 | if (oheader->sh_size == 0 | |
84865015 NC |
1479 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1480 | continue; | |
63b9bbb7 | 1481 | |
84865015 | 1482 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1483 | First we try for a direct mapping between the input and output sections. */ |
1484 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1485 | { | |
1486 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1487 | ||
1488 | if (iheader == NULL) | |
1489 | continue; | |
1490 | ||
1491 | if (oheader->bfd_section != NULL | |
1492 | && iheader->bfd_section != NULL | |
1493 | && iheader->bfd_section->output_section != NULL | |
1494 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1495 | { | |
1496 | /* We have found a connection from the input section to the | |
1497 | output section. Attempt to copy the header fields. If | |
1498 | this fails then do not try any further sections - there | |
1499 | should only be a one-to-one mapping between input and output. */ | |
1500 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1501 | j = elf_numsections (ibfd); | |
1502 | break; | |
1503 | } | |
1504 | } | |
1505 | ||
1506 | if (j < elf_numsections (ibfd)) | |
1507 | continue; | |
1508 | ||
1509 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1510 | Unfortunately we cannot compare names as the output string table |
1511 | is empty, so instead we check size, address and type. */ | |
1512 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1513 | { | |
5522f910 | 1514 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1515 | |
5522f910 NC |
1516 | if (iheader == NULL) |
1517 | continue; | |
1518 | ||
1519 | /* Try matching fields in the input section's header. | |
1520 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1521 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1522 | input type. */ | |
1523 | if ((oheader->sh_type == SHT_NOBITS | |
1524 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1525 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1526 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1527 | && iheader->sh_addralign == oheader->sh_addralign |
1528 | && iheader->sh_entsize == oheader->sh_entsize | |
1529 | && iheader->sh_size == oheader->sh_size | |
1530 | && iheader->sh_addr == oheader->sh_addr | |
1531 | && (iheader->sh_info != oheader->sh_info | |
1532 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1533 | { |
5522f910 NC |
1534 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1535 | break; | |
63b9bbb7 NC |
1536 | } |
1537 | } | |
5522f910 NC |
1538 | |
1539 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1540 | { | |
1541 | /* Final attempt. Call the backend copy function | |
1542 | with a NULL input section. */ | |
1543 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1544 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1545 | } | |
63b9bbb7 NC |
1546 | } |
1547 | ||
b34976b6 | 1548 | return TRUE; |
2d502050 L |
1549 | } |
1550 | ||
cedc298e L |
1551 | static const char * |
1552 | get_segment_type (unsigned int p_type) | |
1553 | { | |
1554 | const char *pt; | |
1555 | switch (p_type) | |
1556 | { | |
1557 | case PT_NULL: pt = "NULL"; break; | |
1558 | case PT_LOAD: pt = "LOAD"; break; | |
1559 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1560 | case PT_INTERP: pt = "INTERP"; break; | |
1561 | case PT_NOTE: pt = "NOTE"; break; | |
1562 | case PT_SHLIB: pt = "SHLIB"; break; | |
1563 | case PT_PHDR: pt = "PHDR"; break; | |
1564 | case PT_TLS: pt = "TLS"; break; | |
1565 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1566 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1567 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1568 | default: pt = NULL; break; | |
1569 | } | |
1570 | return pt; | |
1571 | } | |
1572 | ||
f0b79d91 L |
1573 | /* Print out the program headers. */ |
1574 | ||
b34976b6 | 1575 | bfd_boolean |
217aa764 | 1576 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1577 | { |
a50b1753 | 1578 | FILE *f = (FILE *) farg; |
252b5132 RH |
1579 | Elf_Internal_Phdr *p; |
1580 | asection *s; | |
1581 | bfd_byte *dynbuf = NULL; | |
1582 | ||
1583 | p = elf_tdata (abfd)->phdr; | |
1584 | if (p != NULL) | |
1585 | { | |
1586 | unsigned int i, c; | |
1587 | ||
1588 | fprintf (f, _("\nProgram Header:\n")); | |
1589 | c = elf_elfheader (abfd)->e_phnum; | |
1590 | for (i = 0; i < c; i++, p++) | |
1591 | { | |
cedc298e | 1592 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1593 | char buf[20]; |
1594 | ||
cedc298e | 1595 | if (pt == NULL) |
252b5132 | 1596 | { |
cedc298e L |
1597 | sprintf (buf, "0x%lx", p->p_type); |
1598 | pt = buf; | |
252b5132 | 1599 | } |
dc810e39 | 1600 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1601 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1602 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1603 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1604 | fprintf (f, " paddr 0x"); |
60b89a18 | 1605 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1606 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1607 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1608 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1609 | fprintf (f, " memsz 0x"); |
60b89a18 | 1610 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1611 | fprintf (f, " flags %c%c%c", |
1612 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1613 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1614 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1615 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1616 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1617 | fprintf (f, "\n"); |
1618 | } | |
1619 | } | |
1620 | ||
1621 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1622 | if (s != NULL) | |
1623 | { | |
cb33740c | 1624 | unsigned int elfsec; |
dc810e39 | 1625 | unsigned long shlink; |
252b5132 RH |
1626 | bfd_byte *extdyn, *extdynend; |
1627 | size_t extdynsize; | |
217aa764 | 1628 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1629 | |
1630 | fprintf (f, _("\nDynamic Section:\n")); | |
1631 | ||
eea6121a | 1632 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1633 | goto error_return; |
1634 | ||
1635 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1636 | if (elfsec == SHN_BAD) |
252b5132 | 1637 | goto error_return; |
dc810e39 | 1638 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1639 | |
1640 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1641 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1642 | ||
1643 | extdyn = dynbuf; | |
06614111 NC |
1644 | /* PR 17512: file: 6f427532. */ |
1645 | if (s->size < extdynsize) | |
1646 | goto error_return; | |
eea6121a | 1647 | extdynend = extdyn + s->size; |
1036838a NC |
1648 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1649 | Fix range check. */ | |
1650 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1651 | { |
1652 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1653 | const char *name = ""; |
252b5132 | 1654 | char ab[20]; |
b34976b6 | 1655 | bfd_boolean stringp; |
ad9563d6 | 1656 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1657 | |
217aa764 | 1658 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1659 | |
1660 | if (dyn.d_tag == DT_NULL) | |
1661 | break; | |
1662 | ||
b34976b6 | 1663 | stringp = FALSE; |
252b5132 RH |
1664 | switch (dyn.d_tag) |
1665 | { | |
1666 | default: | |
ad9563d6 CM |
1667 | if (bed->elf_backend_get_target_dtag) |
1668 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1669 | ||
1670 | if (!strcmp (name, "")) | |
1671 | { | |
cd9af601 | 1672 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1673 | name = ab; |
1674 | } | |
252b5132 RH |
1675 | break; |
1676 | ||
b34976b6 | 1677 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1678 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1679 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1680 | case DT_HASH: name = "HASH"; break; | |
1681 | case DT_STRTAB: name = "STRTAB"; break; | |
1682 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1683 | case DT_RELA: name = "RELA"; break; | |
1684 | case DT_RELASZ: name = "RELASZ"; break; | |
1685 | case DT_RELAENT: name = "RELAENT"; break; | |
1686 | case DT_STRSZ: name = "STRSZ"; break; | |
1687 | case DT_SYMENT: name = "SYMENT"; break; | |
1688 | case DT_INIT: name = "INIT"; break; | |
1689 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1690 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1691 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1692 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1693 | case DT_REL: name = "REL"; break; | |
1694 | case DT_RELSZ: name = "RELSZ"; break; | |
1695 | case DT_RELENT: name = "RELENT"; break; | |
1696 | case DT_PLTREL: name = "PLTREL"; break; | |
1697 | case DT_DEBUG: name = "DEBUG"; break; | |
1698 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1699 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1700 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1701 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1702 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1703 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1704 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1705 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1706 | case DT_FLAGS: name = "FLAGS"; break; |
1707 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1708 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1709 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1710 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1711 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1712 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1713 | case DT_FEATURE: name = "FEATURE"; break; | |
1714 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1715 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1716 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1717 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1718 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1719 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1720 | case DT_PLTPAD: name = "PLTPAD"; break; |
1721 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1722 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1723 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1724 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1725 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1726 | case DT_VERSYM: name = "VERSYM"; break; |
1727 | case DT_VERDEF: name = "VERDEF"; break; | |
1728 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1729 | case DT_VERNEED: name = "VERNEED"; break; | |
1730 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1731 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1732 | case DT_USED: name = "USED"; break; |
b34976b6 | 1733 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1734 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1735 | } |
1736 | ||
ad9563d6 | 1737 | fprintf (f, " %-20s ", name); |
252b5132 | 1738 | if (! stringp) |
a1f3c56e AN |
1739 | { |
1740 | fprintf (f, "0x"); | |
1741 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1742 | } | |
252b5132 RH |
1743 | else |
1744 | { | |
1745 | const char *string; | |
dc810e39 | 1746 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1747 | |
dc810e39 | 1748 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1749 | if (string == NULL) |
1750 | goto error_return; | |
1751 | fprintf (f, "%s", string); | |
1752 | } | |
1753 | fprintf (f, "\n"); | |
1754 | } | |
1755 | ||
1756 | free (dynbuf); | |
1757 | dynbuf = NULL; | |
1758 | } | |
1759 | ||
1760 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1761 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1762 | { | |
fc0e6df6 | 1763 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1764 | return FALSE; |
252b5132 RH |
1765 | } |
1766 | ||
1767 | if (elf_dynverdef (abfd) != 0) | |
1768 | { | |
1769 | Elf_Internal_Verdef *t; | |
1770 | ||
1771 | fprintf (f, _("\nVersion definitions:\n")); | |
1772 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1773 | { | |
1774 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1775 | t->vd_flags, t->vd_hash, |
1776 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1777 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1778 | { |
1779 | Elf_Internal_Verdaux *a; | |
1780 | ||
1781 | fprintf (f, "\t"); | |
1782 | for (a = t->vd_auxptr->vda_nextptr; | |
1783 | a != NULL; | |
1784 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1785 | fprintf (f, "%s ", |
1786 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1787 | fprintf (f, "\n"); |
1788 | } | |
1789 | } | |
1790 | } | |
1791 | ||
1792 | if (elf_dynverref (abfd) != 0) | |
1793 | { | |
1794 | Elf_Internal_Verneed *t; | |
1795 | ||
1796 | fprintf (f, _("\nVersion References:\n")); | |
1797 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1798 | { | |
1799 | Elf_Internal_Vernaux *a; | |
1800 | ||
d0fb9a8d JJ |
1801 | fprintf (f, _(" required from %s:\n"), |
1802 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1803 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1804 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1805 | a->vna_flags, a->vna_other, |
1806 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1807 | } |
1808 | } | |
1809 | ||
b34976b6 | 1810 | return TRUE; |
252b5132 RH |
1811 | |
1812 | error_return: | |
1813 | if (dynbuf != NULL) | |
1814 | free (dynbuf); | |
b34976b6 | 1815 | return FALSE; |
252b5132 RH |
1816 | } |
1817 | ||
bb4d2ac2 L |
1818 | /* Get version string. */ |
1819 | ||
1820 | const char * | |
60bb06bc L |
1821 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1822 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1823 | { |
1824 | const char *version_string = NULL; | |
1825 | if (elf_dynversym (abfd) != 0 | |
1826 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1827 | { | |
1828 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1829 | ||
1830 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1831 | vernum &= VERSYM_VERSION; | |
1832 | ||
1833 | if (vernum == 0) | |
1834 | version_string = ""; | |
1835 | else if (vernum == 1) | |
1836 | version_string = "Base"; | |
1837 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1838 | version_string = | |
1839 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1840 | else | |
1841 | { | |
1842 | Elf_Internal_Verneed *t; | |
1843 | ||
1844 | version_string = ""; | |
1845 | for (t = elf_tdata (abfd)->verref; | |
1846 | t != NULL; | |
1847 | t = t->vn_nextref) | |
1848 | { | |
1849 | Elf_Internal_Vernaux *a; | |
1850 | ||
1851 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1852 | { | |
1853 | if (a->vna_other == vernum) | |
1854 | { | |
1855 | version_string = a->vna_nodename; | |
1856 | break; | |
1857 | } | |
1858 | } | |
1859 | } | |
1860 | } | |
1861 | } | |
1862 | return version_string; | |
1863 | } | |
1864 | ||
252b5132 RH |
1865 | /* Display ELF-specific fields of a symbol. */ |
1866 | ||
1867 | void | |
217aa764 AM |
1868 | bfd_elf_print_symbol (bfd *abfd, |
1869 | void *filep, | |
1870 | asymbol *symbol, | |
1871 | bfd_print_symbol_type how) | |
252b5132 | 1872 | { |
a50b1753 | 1873 | FILE *file = (FILE *) filep; |
252b5132 RH |
1874 | switch (how) |
1875 | { | |
1876 | case bfd_print_symbol_name: | |
1877 | fprintf (file, "%s", symbol->name); | |
1878 | break; | |
1879 | case bfd_print_symbol_more: | |
1880 | fprintf (file, "elf "); | |
60b89a18 | 1881 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1882 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1883 | break; |
1884 | case bfd_print_symbol_all: | |
1885 | { | |
4e8a9624 AM |
1886 | const char *section_name; |
1887 | const char *name = NULL; | |
9c5bfbb7 | 1888 | const struct elf_backend_data *bed; |
7a13edea | 1889 | unsigned char st_other; |
dbb410c3 | 1890 | bfd_vma val; |
bb4d2ac2 L |
1891 | const char *version_string; |
1892 | bfd_boolean hidden; | |
c044fabd | 1893 | |
252b5132 | 1894 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1895 | |
1896 | bed = get_elf_backend_data (abfd); | |
1897 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1898 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1899 | |
1900 | if (name == NULL) | |
1901 | { | |
7ee38065 | 1902 | name = symbol->name; |
217aa764 | 1903 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1904 | } |
1905 | ||
252b5132 RH |
1906 | fprintf (file, " %s\t", section_name); |
1907 | /* Print the "other" value for a symbol. For common symbols, | |
1908 | we've already printed the size; now print the alignment. | |
1909 | For other symbols, we have no specified alignment, and | |
1910 | we've printed the address; now print the size. */ | |
dcf6c779 | 1911 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1912 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1913 | else | |
1914 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1915 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1916 | |
1917 | /* If we have version information, print it. */ | |
60bb06bc L |
1918 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1919 | symbol, | |
1920 | &hidden); | |
bb4d2ac2 | 1921 | if (version_string) |
252b5132 | 1922 | { |
bb4d2ac2 | 1923 | if (!hidden) |
252b5132 RH |
1924 | fprintf (file, " %-11s", version_string); |
1925 | else | |
1926 | { | |
1927 | int i; | |
1928 | ||
1929 | fprintf (file, " (%s)", version_string); | |
1930 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1931 | putc (' ', file); | |
1932 | } | |
1933 | } | |
1934 | ||
1935 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1936 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1937 | |
7a13edea NC |
1938 | switch (st_other) |
1939 | { | |
1940 | case 0: break; | |
1941 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1942 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1943 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1944 | default: | |
1945 | /* Some other non-defined flags are also present, so print | |
1946 | everything hex. */ | |
1947 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1948 | } | |
252b5132 | 1949 | |
587ff49e | 1950 | fprintf (file, " %s", name); |
252b5132 RH |
1951 | } |
1952 | break; | |
1953 | } | |
1954 | } | |
252b5132 RH |
1955 | \f |
1956 | /* ELF .o/exec file reading */ | |
1957 | ||
c044fabd | 1958 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1959 | |
b34976b6 | 1960 | bfd_boolean |
217aa764 | 1961 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1962 | { |
4fbb74a6 AM |
1963 | Elf_Internal_Shdr *hdr; |
1964 | Elf_Internal_Ehdr *ehdr; | |
1965 | const struct elf_backend_data *bed; | |
90937f86 | 1966 | const char *name; |
bf67003b NC |
1967 | bfd_boolean ret = TRUE; |
1968 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1969 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1970 | static unsigned int nesting = 0; |
252b5132 | 1971 | |
4fbb74a6 AM |
1972 | if (shindex >= elf_numsections (abfd)) |
1973 | return FALSE; | |
1974 | ||
bf67003b NC |
1975 | if (++ nesting > 3) |
1976 | { | |
1977 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1978 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1979 | loop. Detect this here, by refusing to load a section that we are |
1980 | already in the process of loading. We only trigger this test if | |
1981 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1982 | can expect to recurse at least once. |
1983 | ||
1984 | FIXME: It would be better if this array was attached to the bfd, | |
1985 | rather than being held in a static pointer. */ | |
1986 | ||
1987 | if (sections_being_created_abfd != abfd) | |
1988 | sections_being_created = NULL; | |
bf67003b NC |
1989 | if (sections_being_created == NULL) |
1990 | { | |
1991 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
1992 | sections_being_created = (bfd_boolean *) | |
1993 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 1994 | sections_being_created_abfd = abfd; |
bf67003b NC |
1995 | } |
1996 | if (sections_being_created [shindex]) | |
1997 | { | |
4eca0228 | 1998 | _bfd_error_handler |
bf67003b NC |
1999 | (_("%B: warning: loop in section dependencies detected"), abfd); |
2000 | return FALSE; | |
2001 | } | |
2002 | sections_being_created [shindex] = TRUE; | |
2003 | } | |
2004 | ||
4fbb74a6 AM |
2005 | hdr = elf_elfsections (abfd)[shindex]; |
2006 | ehdr = elf_elfheader (abfd); | |
2007 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2008 | hdr->sh_name); |
933d961a | 2009 | if (name == NULL) |
bf67003b | 2010 | goto fail; |
252b5132 | 2011 | |
4fbb74a6 | 2012 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2013 | switch (hdr->sh_type) |
2014 | { | |
2015 | case SHT_NULL: | |
2016 | /* Inactive section. Throw it away. */ | |
bf67003b | 2017 | goto success; |
252b5132 | 2018 | |
bf67003b NC |
2019 | case SHT_PROGBITS: /* Normal section with contents. */ |
2020 | case SHT_NOBITS: /* .bss section. */ | |
2021 | case SHT_HASH: /* .hash section. */ | |
2022 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2023 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2024 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2025 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2026 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2027 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2028 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2029 | goto success; | |
252b5132 | 2030 | |
797fc050 | 2031 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2032 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2033 | goto fail; |
2034 | ||
cfcac11d NC |
2035 | if (hdr->sh_link > elf_numsections (abfd)) |
2036 | { | |
caa83f8b | 2037 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2038 | field set to SHN_BEFORE or SHN_AFTER. */ |
2039 | switch (bfd_get_arch (abfd)) | |
2040 | { | |
caa83f8b | 2041 | case bfd_arch_i386: |
cfcac11d NC |
2042 | case bfd_arch_sparc: |
2043 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2044 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2045 | break; | |
2046 | /* Otherwise fall through. */ | |
2047 | default: | |
bf67003b | 2048 | goto fail; |
cfcac11d NC |
2049 | } |
2050 | } | |
2051 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2052 | goto fail; |
cfcac11d | 2053 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2054 | { |
2055 | Elf_Internal_Shdr *dynsymhdr; | |
2056 | ||
2057 | /* The shared libraries distributed with hpux11 have a bogus | |
2058 | sh_link field for the ".dynamic" section. Find the | |
2059 | string table for the ".dynsym" section instead. */ | |
2060 | if (elf_dynsymtab (abfd) != 0) | |
2061 | { | |
2062 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2063 | hdr->sh_link = dynsymhdr->sh_link; | |
2064 | } | |
2065 | else | |
2066 | { | |
2067 | unsigned int i, num_sec; | |
2068 | ||
2069 | num_sec = elf_numsections (abfd); | |
2070 | for (i = 1; i < num_sec; i++) | |
2071 | { | |
2072 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2073 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2074 | { | |
2075 | hdr->sh_link = dynsymhdr->sh_link; | |
2076 | break; | |
2077 | } | |
2078 | } | |
2079 | } | |
2080 | } | |
bf67003b | 2081 | goto success; |
797fc050 | 2082 | |
bf67003b | 2083 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2084 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2085 | goto success; |
252b5132 | 2086 | |
a50b2160 | 2087 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2088 | goto fail; |
2089 | ||
3337c1e5 | 2090 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2091 | { |
2092 | if (hdr->sh_size != 0) | |
bf67003b | 2093 | goto fail; |
eee3b786 AM |
2094 | /* Some assemblers erroneously set sh_info to one with a |
2095 | zero sh_size. ld sees this as a global symbol count | |
2096 | of (unsigned) -1. Fix it here. */ | |
2097 | hdr->sh_info = 0; | |
bf67003b | 2098 | goto success; |
eee3b786 | 2099 | } |
bf67003b | 2100 | |
16ad13ec NC |
2101 | /* PR 18854: A binary might contain more than one symbol table. |
2102 | Unusual, but possible. Warn, but continue. */ | |
2103 | if (elf_onesymtab (abfd) != 0) | |
2104 | { | |
4eca0228 | 2105 | _bfd_error_handler |
695344c0 | 2106 | /* xgettext:c-format */ |
63a5468a AM |
2107 | (_("%B: warning: multiple symbol tables detected" |
2108 | " - ignoring the table in section %u"), | |
16ad13ec NC |
2109 | abfd, shindex); |
2110 | goto success; | |
2111 | } | |
252b5132 | 2112 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2113 | elf_symtab_hdr (abfd) = *hdr; |
2114 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2115 | abfd->flags |= HAS_SYMS; |
2116 | ||
2117 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2118 | SHF_ALLOC is set, and this is a shared object, then we also |
2119 | treat this section as a BFD section. We can not base the | |
2120 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2121 | set in a relocatable object file, which would confuse the | |
2122 | linker. */ | |
252b5132 RH |
2123 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2124 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2125 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2126 | shindex)) | |
bf67003b | 2127 | goto fail; |
252b5132 | 2128 | |
1b3a8575 AM |
2129 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2130 | can't read symbols without that section loaded as well. It | |
2131 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2132 | { |
2133 | elf_section_list * entry; | |
2134 | unsigned int i, num_sec; | |
1b3a8575 | 2135 | |
6a40cf0c NC |
2136 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2137 | if (entry->hdr.sh_link == shindex) | |
2138 | goto success; | |
2139 | ||
2140 | num_sec = elf_numsections (abfd); | |
2141 | for (i = shindex + 1; i < num_sec; i++) | |
2142 | { | |
2143 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2144 | ||
2145 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2146 | && hdr2->sh_link == shindex) | |
2147 | break; | |
2148 | } | |
2149 | ||
2150 | if (i == num_sec) | |
2151 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2152 | { |
2153 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2154 | |
1b3a8575 AM |
2155 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2156 | && hdr2->sh_link == shindex) | |
2157 | break; | |
2158 | } | |
6a40cf0c NC |
2159 | |
2160 | if (i != shindex) | |
2161 | ret = bfd_section_from_shdr (abfd, i); | |
2162 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2163 | goto success; | |
2164 | } | |
252b5132 | 2165 | |
bf67003b | 2166 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2167 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2168 | goto success; |
252b5132 | 2169 | |
a50b2160 | 2170 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2171 | goto fail; |
2172 | ||
eee3b786 AM |
2173 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2174 | { | |
2175 | if (hdr->sh_size != 0) | |
bf67003b NC |
2176 | goto fail; |
2177 | ||
eee3b786 AM |
2178 | /* Some linkers erroneously set sh_info to one with a |
2179 | zero sh_size. ld sees this as a global symbol count | |
2180 | of (unsigned) -1. Fix it here. */ | |
2181 | hdr->sh_info = 0; | |
bf67003b | 2182 | goto success; |
eee3b786 | 2183 | } |
bf67003b | 2184 | |
16ad13ec NC |
2185 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2186 | Unusual, but possible. Warn, but continue. */ | |
2187 | if (elf_dynsymtab (abfd) != 0) | |
2188 | { | |
4eca0228 | 2189 | _bfd_error_handler |
695344c0 | 2190 | /* xgettext:c-format */ |
63a5468a AM |
2191 | (_("%B: warning: multiple dynamic symbol tables detected" |
2192 | " - ignoring the table in section %u"), | |
16ad13ec NC |
2193 | abfd, shindex); |
2194 | goto success; | |
2195 | } | |
252b5132 RH |
2196 | elf_dynsymtab (abfd) = shindex; |
2197 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2198 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2199 | abfd->flags |= HAS_SYMS; | |
2200 | ||
2201 | /* Besides being a symbol table, we also treat this as a regular | |
2202 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2203 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2204 | goto success; | |
252b5132 | 2205 | |
bf67003b | 2206 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2207 | { |
2208 | elf_section_list * entry; | |
9ad5cbcf | 2209 | |
6a40cf0c NC |
2210 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2211 | if (entry->ndx == shindex) | |
2212 | goto success; | |
2213 | ||
2214 | entry = bfd_alloc (abfd, sizeof * entry); | |
2215 | if (entry == NULL) | |
2216 | goto fail; | |
2217 | entry->ndx = shindex; | |
2218 | entry->hdr = * hdr; | |
2219 | entry->next = elf_symtab_shndx_list (abfd); | |
2220 | elf_symtab_shndx_list (abfd) = entry; | |
2221 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2222 | goto success; | |
2223 | } | |
9ad5cbcf | 2224 | |
bf67003b | 2225 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2226 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2227 | goto success; |
2228 | ||
252b5132 RH |
2229 | if (ehdr->e_shstrndx == shindex) |
2230 | { | |
2231 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2232 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2233 | goto success; |
252b5132 | 2234 | } |
bf67003b | 2235 | |
1b3a8575 AM |
2236 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2237 | { | |
2238 | symtab_strtab: | |
2239 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2240 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2241 | goto success; |
1b3a8575 | 2242 | } |
bf67003b | 2243 | |
1b3a8575 AM |
2244 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2245 | { | |
2246 | dynsymtab_strtab: | |
2247 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2248 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2249 | elf_elfsections (abfd)[shindex] = hdr; | |
2250 | /* We also treat this as a regular section, so that objcopy | |
2251 | can handle it. */ | |
bf67003b NC |
2252 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2253 | shindex); | |
2254 | goto success; | |
1b3a8575 | 2255 | } |
252b5132 | 2256 | |
1b3a8575 AM |
2257 | /* If the string table isn't one of the above, then treat it as a |
2258 | regular section. We need to scan all the headers to be sure, | |
2259 | just in case this strtab section appeared before the above. */ | |
2260 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2261 | { | |
2262 | unsigned int i, num_sec; | |
252b5132 | 2263 | |
1b3a8575 AM |
2264 | num_sec = elf_numsections (abfd); |
2265 | for (i = 1; i < num_sec; i++) | |
2266 | { | |
2267 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2268 | if (hdr2->sh_link == shindex) | |
2269 | { | |
933d961a JJ |
2270 | /* Prevent endless recursion on broken objects. */ |
2271 | if (i == shindex) | |
bf67003b | 2272 | goto fail; |
1b3a8575 | 2273 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2274 | goto fail; |
1b3a8575 AM |
2275 | if (elf_onesymtab (abfd) == i) |
2276 | goto symtab_strtab; | |
2277 | if (elf_dynsymtab (abfd) == i) | |
2278 | goto dynsymtab_strtab; | |
2279 | } | |
2280 | } | |
2281 | } | |
bf67003b NC |
2282 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2283 | goto success; | |
252b5132 RH |
2284 | |
2285 | case SHT_REL: | |
2286 | case SHT_RELA: | |
2287 | /* *These* do a lot of work -- but build no sections! */ | |
2288 | { | |
2289 | asection *target_sect; | |
d4730f92 | 2290 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2291 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2292 | struct bfd_elf_section_data *esdt; |
252b5132 | 2293 | |
aa2ca951 JJ |
2294 | if (hdr->sh_entsize |
2295 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2296 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2297 | goto fail; |
a50b2160 | 2298 | |
03ae5f59 | 2299 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2300 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2301 | { |
4eca0228 | 2302 | _bfd_error_handler |
695344c0 | 2303 | /* xgettext:c-format */ |
76cfced5 | 2304 | (_("%B: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2305 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2306 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2307 | shindex); | |
2308 | goto success; | |
03ae5f59 ILT |
2309 | } |
2310 | ||
252b5132 RH |
2311 | /* For some incomprehensible reason Oracle distributes |
2312 | libraries for Solaris in which some of the objects have | |
2313 | bogus sh_link fields. It would be nice if we could just | |
2314 | reject them, but, unfortunately, some people need to use | |
2315 | them. We scan through the section headers; if we find only | |
2316 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2317 | to it. I hope this doesn't break anything. |
2318 | ||
2319 | Don't do it on executable nor shared library. */ | |
2320 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2321 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2322 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2323 | { | |
9ad5cbcf | 2324 | unsigned int scan; |
252b5132 RH |
2325 | int found; |
2326 | ||
2327 | found = 0; | |
9ad5cbcf | 2328 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2329 | { |
2330 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2331 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2332 | { | |
2333 | if (found != 0) | |
2334 | { | |
2335 | found = 0; | |
2336 | break; | |
2337 | } | |
2338 | found = scan; | |
2339 | } | |
2340 | } | |
2341 | if (found != 0) | |
2342 | hdr->sh_link = found; | |
2343 | } | |
2344 | ||
2345 | /* Get the symbol table. */ | |
1b3a8575 AM |
2346 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2347 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2348 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2349 | goto fail; |
252b5132 RH |
2350 | |
2351 | /* If this reloc section does not use the main symbol table we | |
2352 | don't treat it as a reloc section. BFD can't adequately | |
2353 | represent such a section, so at least for now, we don't | |
c044fabd | 2354 | try. We just present it as a normal section. We also |
60bcf0fa | 2355 | can't use it as a reloc section if it points to the null |
83b89087 L |
2356 | section, an invalid section, another reloc section, or its |
2357 | sh_link points to the null section. */ | |
185ef66d | 2358 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2359 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2360 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2361 | || hdr->sh_info >= num_sec |
2362 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2363 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2364 | { |
2365 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2366 | shindex); | |
2367 | goto success; | |
2368 | } | |
252b5132 RH |
2369 | |
2370 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2371 | goto fail; |
2372 | ||
252b5132 RH |
2373 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2374 | if (target_sect == NULL) | |
bf67003b | 2375 | goto fail; |
252b5132 | 2376 | |
d4730f92 BS |
2377 | esdt = elf_section_data (target_sect); |
2378 | if (hdr->sh_type == SHT_RELA) | |
2379 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2380 | else |
d4730f92 BS |
2381 | p_hdr = &esdt->rel.hdr; |
2382 | ||
06614111 NC |
2383 | /* PR 17512: file: 0b4f81b7. */ |
2384 | if (*p_hdr != NULL) | |
2385 | goto fail; | |
ef53be89 | 2386 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2387 | if (hdr2 == NULL) |
bf67003b | 2388 | goto fail; |
252b5132 | 2389 | *hdr2 = *hdr; |
d4730f92 | 2390 | *p_hdr = hdr2; |
252b5132 | 2391 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2392 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2393 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2394 | target_sect->flags |= SEC_RELOC; |
2395 | target_sect->relocation = NULL; | |
2396 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2397 | /* In the section to which the relocations apply, mark whether |
2398 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2399 | if (hdr->sh_size != 0) |
d4730f92 BS |
2400 | { |
2401 | if (hdr->sh_type == SHT_RELA) | |
2402 | target_sect->use_rela_p = 1; | |
2403 | } | |
252b5132 | 2404 | abfd->flags |= HAS_RELOC; |
bf67003b | 2405 | goto success; |
252b5132 | 2406 | } |
252b5132 RH |
2407 | |
2408 | case SHT_GNU_verdef: | |
2409 | elf_dynverdef (abfd) = shindex; | |
2410 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2411 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2412 | goto success; | |
252b5132 RH |
2413 | |
2414 | case SHT_GNU_versym: | |
a50b2160 | 2415 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2416 | goto fail; |
2417 | ||
252b5132 RH |
2418 | elf_dynversym (abfd) = shindex; |
2419 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2420 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2421 | goto success; | |
252b5132 RH |
2422 | |
2423 | case SHT_GNU_verneed: | |
2424 | elf_dynverref (abfd) = shindex; | |
2425 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2426 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2427 | goto success; | |
252b5132 RH |
2428 | |
2429 | case SHT_SHLIB: | |
bf67003b | 2430 | goto success; |
252b5132 | 2431 | |
dbb410c3 | 2432 | case SHT_GROUP: |
44534af3 | 2433 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2434 | goto fail; |
2435 | ||
6dc132d9 | 2436 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2437 | goto fail; |
2438 | ||
bf67003b | 2439 | goto success; |
dbb410c3 | 2440 | |
252b5132 | 2441 | default: |
104d59d1 JM |
2442 | /* Possibly an attributes section. */ |
2443 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2444 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2445 | { | |
2446 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2447 | goto fail; |
104d59d1 | 2448 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2449 | goto success; |
104d59d1 JM |
2450 | } |
2451 | ||
252b5132 | 2452 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2453 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2454 | goto success; |
3eb70a79 L |
2455 | |
2456 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2457 | { | |
2458 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2459 | /* FIXME: How to properly handle allocated section reserved | |
2460 | for applications? */ | |
4eca0228 | 2461 | _bfd_error_handler |
695344c0 | 2462 | /* xgettext:c-format */ |
76cfced5 AM |
2463 | (_("%B: unknown type [%#x] section `%s'"), |
2464 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2465 | else |
bf67003b NC |
2466 | { |
2467 | /* Allow sections reserved for applications. */ | |
2468 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2469 | shindex); | |
2470 | goto success; | |
2471 | } | |
3eb70a79 L |
2472 | } |
2473 | else if (hdr->sh_type >= SHT_LOPROC | |
2474 | && hdr->sh_type <= SHT_HIPROC) | |
2475 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2476 | _bfd_error_handler |
695344c0 | 2477 | /* xgettext:c-format */ |
76cfced5 AM |
2478 | (_("%B: unknown type [%#x] section `%s'"), |
2479 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2480 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2481 | { |
2482 | /* Unrecognised OS-specific sections. */ | |
2483 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2484 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2485 | required to correctly process the section and the file should |
ff15b240 | 2486 | be rejected with an error message. */ |
4eca0228 | 2487 | _bfd_error_handler |
695344c0 | 2488 | /* xgettext:c-format */ |
76cfced5 AM |
2489 | (_("%B: unknown type [%#x] section `%s'"), |
2490 | abfd, hdr->sh_type, name); | |
ff15b240 | 2491 | else |
bf67003b NC |
2492 | { |
2493 | /* Otherwise it should be processed. */ | |
2494 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2495 | goto success; | |
2496 | } | |
ff15b240 | 2497 | } |
3eb70a79 L |
2498 | else |
2499 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2500 | _bfd_error_handler |
695344c0 | 2501 | /* xgettext:c-format */ |
76cfced5 AM |
2502 | (_("%B: unknown type [%#x] section `%s'"), |
2503 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2504 | |
bf67003b | 2505 | goto fail; |
252b5132 RH |
2506 | } |
2507 | ||
bf67003b NC |
2508 | fail: |
2509 | ret = FALSE; | |
2510 | success: | |
e5b470e2 | 2511 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2512 | sections_being_created [shindex] = FALSE; |
2513 | if (-- nesting == 0) | |
5a4b0ccc NC |
2514 | { |
2515 | sections_being_created = NULL; | |
2516 | sections_being_created_abfd = abfd; | |
2517 | } | |
bf67003b | 2518 | return ret; |
252b5132 RH |
2519 | } |
2520 | ||
87d72d41 | 2521 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2522 | |
87d72d41 AM |
2523 | Elf_Internal_Sym * |
2524 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2525 | bfd *abfd, | |
2526 | unsigned long r_symndx) | |
ec338859 | 2527 | { |
ec338859 AM |
2528 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2529 | ||
a5d1b3b5 AM |
2530 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2531 | { | |
2532 | Elf_Internal_Shdr *symtab_hdr; | |
2533 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2534 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2535 | |
a5d1b3b5 AM |
2536 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2537 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2538 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2539 | return NULL; |
9ad5cbcf | 2540 | |
a5d1b3b5 AM |
2541 | if (cache->abfd != abfd) |
2542 | { | |
2543 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2544 | cache->abfd = abfd; | |
2545 | } | |
2546 | cache->indx[ent] = r_symndx; | |
ec338859 | 2547 | } |
a5d1b3b5 | 2548 | |
87d72d41 | 2549 | return &cache->sym[ent]; |
ec338859 AM |
2550 | } |
2551 | ||
252b5132 RH |
2552 | /* Given an ELF section number, retrieve the corresponding BFD |
2553 | section. */ | |
2554 | ||
2555 | asection * | |
91d6fa6a | 2556 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2557 | { |
91d6fa6a | 2558 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2559 | return NULL; |
91d6fa6a | 2560 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2561 | } |
2562 | ||
b35d266b | 2563 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2564 | { |
0112cd26 NC |
2565 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2566 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2567 | }; |
2568 | ||
b35d266b | 2569 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2570 | { |
0112cd26 NC |
2571 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2572 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2573 | }; |
2574 | ||
b35d266b | 2575 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2576 | { |
0112cd26 NC |
2577 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2578 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2579 | /* There are more DWARF sections than these, but they needn't be added here |
2580 | unless you have to cope with broken compilers that don't emit section | |
2581 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2582 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2583 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2584 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2585 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2586 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2587 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2588 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2589 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2590 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2591 | }; |
2592 | ||
b35d266b | 2593 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2594 | { |
6f9dbcd4 AM |
2595 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2596 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2597 | { NULL, 0 , 0, 0, 0 } | |
7f4d3958 L |
2598 | }; |
2599 | ||
b35d266b | 2600 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2601 | { |
0112cd26 | 2602 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2603 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2604 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2605 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2606 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2607 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2608 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2609 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2610 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2611 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2612 | }; |
2613 | ||
b35d266b | 2614 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2615 | { |
0112cd26 NC |
2616 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2617 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2618 | }; |
2619 | ||
b35d266b | 2620 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2621 | { |
6f9dbcd4 AM |
2622 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2623 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2624 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2625 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2626 | }; |
2627 | ||
b35d266b | 2628 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2629 | { |
0112cd26 NC |
2630 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2631 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2632 | }; |
2633 | ||
b35d266b | 2634 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2635 | { |
0112cd26 NC |
2636 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2637 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2638 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2639 | }; |
2640 | ||
b35d266b | 2641 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2642 | { |
6f9dbcd4 AM |
2643 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2644 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2645 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2646 | }; |
2647 | ||
b35d266b | 2648 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2649 | { |
0112cd26 NC |
2650 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2651 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2652 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2653 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2654 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2655 | }; |
2656 | ||
b35d266b | 2657 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2658 | { |
0112cd26 NC |
2659 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2660 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2661 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2662 | /* See struct bfd_elf_special_section declaration for the semantics of |
2663 | this special case where .prefix_length != strlen (.prefix). */ | |
2664 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2665 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2666 | }; |
2667 | ||
b35d266b | 2668 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2669 | { |
0112cd26 NC |
2670 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2671 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2672 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2673 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2674 | }; |
2675 | ||
1b315056 CS |
2676 | static const struct bfd_elf_special_section special_sections_z[] = |
2677 | { | |
2678 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2679 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2680 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2681 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2682 | { NULL, 0, 0, 0, 0 } | |
2683 | }; | |
2684 | ||
e4c93b56 | 2685 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2686 | { |
7f4d3958 | 2687 | special_sections_b, /* 'b' */ |
98ece1b3 | 2688 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2689 | special_sections_d, /* 'd' */ |
2690 | NULL, /* 'e' */ | |
2691 | special_sections_f, /* 'f' */ | |
2692 | special_sections_g, /* 'g' */ | |
2693 | special_sections_h, /* 'h' */ | |
2694 | special_sections_i, /* 'i' */ | |
2695 | NULL, /* 'j' */ | |
2696 | NULL, /* 'k' */ | |
2697 | special_sections_l, /* 'l' */ | |
2698 | NULL, /* 'm' */ | |
2699 | special_sections_n, /* 'n' */ | |
2700 | NULL, /* 'o' */ | |
2701 | special_sections_p, /* 'p' */ | |
2702 | NULL, /* 'q' */ | |
2703 | special_sections_r, /* 'r' */ | |
2704 | special_sections_s, /* 's' */ | |
2705 | special_sections_t, /* 't' */ | |
1b315056 CS |
2706 | NULL, /* 'u' */ |
2707 | NULL, /* 'v' */ | |
2708 | NULL, /* 'w' */ | |
2709 | NULL, /* 'x' */ | |
2710 | NULL, /* 'y' */ | |
2711 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2712 | }; |
2713 | ||
551b43fd AM |
2714 | const struct bfd_elf_special_section * |
2715 | _bfd_elf_get_special_section (const char *name, | |
2716 | const struct bfd_elf_special_section *spec, | |
2717 | unsigned int rela) | |
2f89ff8d L |
2718 | { |
2719 | int i; | |
7f4d3958 | 2720 | int len; |
7f4d3958 | 2721 | |
551b43fd | 2722 | len = strlen (name); |
7f4d3958 | 2723 | |
551b43fd | 2724 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2725 | { |
2726 | int suffix_len; | |
551b43fd | 2727 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2728 | |
2729 | if (len < prefix_len) | |
2730 | continue; | |
551b43fd | 2731 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2732 | continue; |
2733 | ||
551b43fd | 2734 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2735 | if (suffix_len <= 0) |
2736 | { | |
2737 | if (name[prefix_len] != 0) | |
2738 | { | |
2739 | if (suffix_len == 0) | |
2740 | continue; | |
2741 | if (name[prefix_len] != '.' | |
2742 | && (suffix_len == -2 | |
551b43fd | 2743 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2744 | continue; |
2745 | } | |
2746 | } | |
2747 | else | |
2748 | { | |
2749 | if (len < prefix_len + suffix_len) | |
2750 | continue; | |
2751 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2752 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2753 | suffix_len) != 0) |
2754 | continue; | |
2755 | } | |
551b43fd | 2756 | return &spec[i]; |
7dcb9820 | 2757 | } |
2f89ff8d L |
2758 | |
2759 | return NULL; | |
2760 | } | |
2761 | ||
7dcb9820 | 2762 | const struct bfd_elf_special_section * |
29ef7005 | 2763 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2764 | { |
551b43fd AM |
2765 | int i; |
2766 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2767 | const struct elf_backend_data *bed; |
2f89ff8d L |
2768 | |
2769 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2770 | if (sec->name == NULL) |
2771 | return NULL; | |
2f89ff8d | 2772 | |
29ef7005 L |
2773 | bed = get_elf_backend_data (abfd); |
2774 | spec = bed->special_sections; | |
2775 | if (spec) | |
2776 | { | |
2777 | spec = _bfd_elf_get_special_section (sec->name, | |
2778 | bed->special_sections, | |
2779 | sec->use_rela_p); | |
2780 | if (spec != NULL) | |
2781 | return spec; | |
2782 | } | |
2783 | ||
551b43fd AM |
2784 | if (sec->name[0] != '.') |
2785 | return NULL; | |
2f89ff8d | 2786 | |
551b43fd | 2787 | i = sec->name[1] - 'b'; |
1b315056 | 2788 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2789 | return NULL; |
2790 | ||
2791 | spec = special_sections[i]; | |
2f89ff8d | 2792 | |
551b43fd AM |
2793 | if (spec == NULL) |
2794 | return NULL; | |
2795 | ||
2796 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2797 | } |
2798 | ||
b34976b6 | 2799 | bfd_boolean |
217aa764 | 2800 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2801 | { |
2802 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2803 | const struct elf_backend_data *bed; |
7dcb9820 | 2804 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2805 | |
f0abc2a1 AM |
2806 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2807 | if (sdata == NULL) | |
2808 | { | |
a50b1753 NC |
2809 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2810 | sizeof (*sdata)); | |
f0abc2a1 AM |
2811 | if (sdata == NULL) |
2812 | return FALSE; | |
217aa764 | 2813 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2814 | } |
bf572ba0 | 2815 | |
551b43fd AM |
2816 | /* Indicate whether or not this section should use RELA relocations. */ |
2817 | bed = get_elf_backend_data (abfd); | |
2818 | sec->use_rela_p = bed->default_use_rela_p; | |
2819 | ||
e843e0f8 L |
2820 | /* When we read a file, we don't need to set ELF section type and |
2821 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2822 | anyway. We will set ELF section type and flags for all linker | |
2823 | created sections. If user specifies BFD section flags, we will | |
2824 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2825 | elf_fake_sections. Special handling for .init_array/.fini_array |
2826 | output sections since they may contain .ctors/.dtors input | |
2827 | sections. We don't want _bfd_elf_init_private_section_data to | |
2828 | copy ELF section type from .ctors/.dtors input sections. */ | |
2829 | if (abfd->direction != read_direction | |
3496cb2a | 2830 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2831 | { |
551b43fd | 2832 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2833 | if (ssect != NULL |
2834 | && (!sec->flags | |
2835 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2836 | || ssect->type == SHT_INIT_ARRAY | |
2837 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2838 | { |
2839 | elf_section_type (sec) = ssect->type; | |
2840 | elf_section_flags (sec) = ssect->attr; | |
2841 | } | |
2f89ff8d L |
2842 | } |
2843 | ||
f592407e | 2844 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2845 | } |
2846 | ||
2847 | /* Create a new bfd section from an ELF program header. | |
2848 | ||
2849 | Since program segments have no names, we generate a synthetic name | |
2850 | of the form segment<NUM>, where NUM is generally the index in the | |
2851 | program header table. For segments that are split (see below) we | |
2852 | generate the names segment<NUM>a and segment<NUM>b. | |
2853 | ||
2854 | Note that some program segments may have a file size that is different than | |
2855 | (less than) the memory size. All this means is that at execution the | |
2856 | system must allocate the amount of memory specified by the memory size, | |
2857 | but only initialize it with the first "file size" bytes read from the | |
2858 | file. This would occur for example, with program segments consisting | |
2859 | of combined data+bss. | |
2860 | ||
2861 | To handle the above situation, this routine generates TWO bfd sections | |
2862 | for the single program segment. The first has the length specified by | |
2863 | the file size of the segment, and the second has the length specified | |
2864 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2865 | into its initialized and uninitialized parts. |
252b5132 RH |
2866 | |
2867 | */ | |
2868 | ||
b34976b6 | 2869 | bfd_boolean |
217aa764 AM |
2870 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2871 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2872 | int hdr_index, |
a50b1753 | 2873 | const char *type_name) |
252b5132 RH |
2874 | { |
2875 | asection *newsect; | |
2876 | char *name; | |
2877 | char namebuf[64]; | |
d4c88bbb | 2878 | size_t len; |
252b5132 RH |
2879 | int split; |
2880 | ||
2881 | split = ((hdr->p_memsz > 0) | |
2882 | && (hdr->p_filesz > 0) | |
2883 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2884 | |
2885 | if (hdr->p_filesz > 0) | |
252b5132 | 2886 | { |
91d6fa6a | 2887 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2888 | len = strlen (namebuf) + 1; |
a50b1753 | 2889 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2890 | if (!name) |
2891 | return FALSE; | |
2892 | memcpy (name, namebuf, len); | |
2893 | newsect = bfd_make_section (abfd, name); | |
2894 | if (newsect == NULL) | |
2895 | return FALSE; | |
2896 | newsect->vma = hdr->p_vaddr; | |
2897 | newsect->lma = hdr->p_paddr; | |
2898 | newsect->size = hdr->p_filesz; | |
2899 | newsect->filepos = hdr->p_offset; | |
2900 | newsect->flags |= SEC_HAS_CONTENTS; | |
2901 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2902 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2903 | { |
d5191d0c AM |
2904 | newsect->flags |= SEC_ALLOC; |
2905 | newsect->flags |= SEC_LOAD; | |
2906 | if (hdr->p_flags & PF_X) | |
2907 | { | |
2908 | /* FIXME: all we known is that it has execute PERMISSION, | |
2909 | may be data. */ | |
2910 | newsect->flags |= SEC_CODE; | |
2911 | } | |
2912 | } | |
2913 | if (!(hdr->p_flags & PF_W)) | |
2914 | { | |
2915 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2916 | } |
252b5132 RH |
2917 | } |
2918 | ||
d5191d0c | 2919 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2920 | { |
d5191d0c AM |
2921 | bfd_vma align; |
2922 | ||
91d6fa6a | 2923 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2924 | len = strlen (namebuf) + 1; |
a50b1753 | 2925 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2926 | if (!name) |
b34976b6 | 2927 | return FALSE; |
d4c88bbb | 2928 | memcpy (name, namebuf, len); |
252b5132 RH |
2929 | newsect = bfd_make_section (abfd, name); |
2930 | if (newsect == NULL) | |
b34976b6 | 2931 | return FALSE; |
252b5132 RH |
2932 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2933 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2934 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2935 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2936 | align = newsect->vma & -newsect->vma; | |
2937 | if (align == 0 || align > hdr->p_align) | |
2938 | align = hdr->p_align; | |
2939 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2940 | if (hdr->p_type == PT_LOAD) |
2941 | { | |
d5191d0c AM |
2942 | /* Hack for gdb. Segments that have not been modified do |
2943 | not have their contents written to a core file, on the | |
2944 | assumption that a debugger can find the contents in the | |
2945 | executable. We flag this case by setting the fake | |
2946 | section size to zero. Note that "real" bss sections will | |
2947 | always have their contents dumped to the core file. */ | |
2948 | if (bfd_get_format (abfd) == bfd_core) | |
2949 | newsect->size = 0; | |
252b5132 RH |
2950 | newsect->flags |= SEC_ALLOC; |
2951 | if (hdr->p_flags & PF_X) | |
2952 | newsect->flags |= SEC_CODE; | |
2953 | } | |
2954 | if (!(hdr->p_flags & PF_W)) | |
2955 | newsect->flags |= SEC_READONLY; | |
2956 | } | |
2957 | ||
b34976b6 | 2958 | return TRUE; |
252b5132 RH |
2959 | } |
2960 | ||
b34976b6 | 2961 | bfd_boolean |
91d6fa6a | 2962 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2963 | { |
9c5bfbb7 | 2964 | const struct elf_backend_data *bed; |
20cfcaae NC |
2965 | |
2966 | switch (hdr->p_type) | |
2967 | { | |
2968 | case PT_NULL: | |
91d6fa6a | 2969 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2970 | |
2971 | case PT_LOAD: | |
91d6fa6a | 2972 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2973 | |
2974 | case PT_DYNAMIC: | |
91d6fa6a | 2975 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2976 | |
2977 | case PT_INTERP: | |
91d6fa6a | 2978 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2979 | |
2980 | case PT_NOTE: | |
91d6fa6a | 2981 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2982 | return FALSE; |
718175fa | 2983 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
2984 | return FALSE; |
2985 | return TRUE; | |
20cfcaae NC |
2986 | |
2987 | case PT_SHLIB: | |
91d6fa6a | 2988 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
2989 | |
2990 | case PT_PHDR: | |
91d6fa6a | 2991 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 2992 | |
811072d8 | 2993 | case PT_GNU_EH_FRAME: |
91d6fa6a | 2994 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
2995 | "eh_frame_hdr"); |
2996 | ||
2b05f1b7 | 2997 | case PT_GNU_STACK: |
91d6fa6a | 2998 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 2999 | |
8c37241b | 3000 | case PT_GNU_RELRO: |
91d6fa6a | 3001 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3002 | |
20cfcaae | 3003 | default: |
8c1acd09 | 3004 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3005 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3006 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3007 | } |
3008 | } | |
3009 | ||
d4730f92 BS |
3010 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3011 | REL or RELA. */ | |
3012 | ||
3013 | Elf_Internal_Shdr * | |
3014 | _bfd_elf_single_rel_hdr (asection *sec) | |
3015 | { | |
3016 | if (elf_section_data (sec)->rel.hdr) | |
3017 | { | |
3018 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3019 | return elf_section_data (sec)->rel.hdr; | |
3020 | } | |
3021 | else | |
3022 | return elf_section_data (sec)->rela.hdr; | |
3023 | } | |
3024 | ||
3e19fb8f L |
3025 | static bfd_boolean |
3026 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3027 | Elf_Internal_Shdr *rel_hdr, | |
3028 | const char *sec_name, | |
3029 | bfd_boolean use_rela_p) | |
3030 | { | |
3031 | char *name = (char *) bfd_alloc (abfd, | |
3032 | sizeof ".rela" + strlen (sec_name)); | |
3033 | if (name == NULL) | |
3034 | return FALSE; | |
3035 | ||
3036 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3037 | rel_hdr->sh_name = | |
3038 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3039 | FALSE); | |
3040 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3041 | return FALSE; | |
3042 | ||
3043 | return TRUE; | |
3044 | } | |
3045 | ||
d4730f92 BS |
3046 | /* Allocate and initialize a section-header for a new reloc section, |
3047 | containing relocations against ASECT. It is stored in RELDATA. If | |
3048 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3049 | relocations. */ | |
23bc299b | 3050 | |
5d13b3b3 | 3051 | static bfd_boolean |
217aa764 | 3052 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3053 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3054 | const char *sec_name, |
3e19fb8f L |
3055 | bfd_boolean use_rela_p, |
3056 | bfd_boolean delay_st_name_p) | |
23bc299b | 3057 | { |
d4730f92 | 3058 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3059 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3060 | |
d4730f92 | 3061 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3062 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3063 | reldata->hdr = rel_hdr; |
23bc299b | 3064 | |
3e19fb8f L |
3065 | if (delay_st_name_p) |
3066 | rel_hdr->sh_name = (unsigned int) -1; | |
3067 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3068 | use_rela_p)) | |
b34976b6 | 3069 | return FALSE; |
23bc299b MM |
3070 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3071 | rel_hdr->sh_entsize = (use_rela_p | |
3072 | ? bed->s->sizeof_rela | |
3073 | : bed->s->sizeof_rel); | |
72de5009 | 3074 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
3075 | rel_hdr->sh_flags = 0; |
3076 | rel_hdr->sh_addr = 0; | |
3077 | rel_hdr->sh_size = 0; | |
3078 | rel_hdr->sh_offset = 0; | |
3079 | ||
b34976b6 | 3080 | return TRUE; |
23bc299b MM |
3081 | } |
3082 | ||
94be91de JB |
3083 | /* Return the default section type based on the passed in section flags. */ |
3084 | ||
3085 | int | |
3086 | bfd_elf_get_default_section_type (flagword flags) | |
3087 | { | |
3088 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3089 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3090 | return SHT_NOBITS; |
3091 | return SHT_PROGBITS; | |
3092 | } | |
3093 | ||
d4730f92 BS |
3094 | struct fake_section_arg |
3095 | { | |
3096 | struct bfd_link_info *link_info; | |
3097 | bfd_boolean failed; | |
3098 | }; | |
3099 | ||
252b5132 RH |
3100 | /* Set up an ELF internal section header for a section. */ |
3101 | ||
252b5132 | 3102 | static void |
d4730f92 | 3103 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3104 | { |
d4730f92 | 3105 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3106 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3107 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3108 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3109 | unsigned int sh_type; |
0ce398f1 | 3110 | const char *name = asect->name; |
3e19fb8f | 3111 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3112 | |
d4730f92 | 3113 | if (arg->failed) |
252b5132 RH |
3114 | { |
3115 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3116 | loop. */ |
252b5132 RH |
3117 | return; |
3118 | } | |
3119 | ||
d4730f92 | 3120 | this_hdr = &esd->this_hdr; |
252b5132 | 3121 | |
f6fe1ccd | 3122 | if (arg->link_info) |
0ce398f1 | 3123 | { |
f6fe1ccd L |
3124 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3125 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3126 | && (asect->flags & SEC_DEBUGGING) | |
3127 | && name[1] == 'd' | |
3128 | && name[6] == '_') | |
3129 | { | |
3130 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3131 | compressed. */ | |
3132 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3133 | |
dd905818 | 3134 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3135 | name to section name section after it is compressed in |
3136 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3137 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3138 | } |
3139 | } | |
3140 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3141 | { | |
3142 | /* objcopy: rename output DWARF debug section. */ | |
3143 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3144 | { | |
3145 | /* When we decompress or compress with SHF_COMPRESSED, | |
3146 | convert section name from .zdebug_* to .debug_* if | |
3147 | needed. */ | |
3148 | if (name[1] == 'z') | |
3149 | { | |
3150 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3151 | if (new_name == NULL) | |
3152 | { | |
3153 | arg->failed = TRUE; | |
3154 | return; | |
3155 | } | |
3156 | name = new_name; | |
3157 | } | |
3158 | } | |
3159 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3160 | { |
f6fe1ccd L |
3161 | /* PR binutils/18087: Compression does not always make a |
3162 | section smaller. So only rename the section when | |
3163 | compression has actually taken place. If input section | |
3164 | name is .zdebug_*, we should never compress it again. */ | |
3165 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3166 | if (new_name == NULL) |
3167 | { | |
3168 | arg->failed = TRUE; | |
3169 | return; | |
3170 | } | |
f6fe1ccd L |
3171 | BFD_ASSERT (name[1] != 'z'); |
3172 | name = new_name; | |
0ce398f1 L |
3173 | } |
3174 | } | |
3175 | ||
3e19fb8f L |
3176 | if (delay_st_name_p) |
3177 | this_hdr->sh_name = (unsigned int) -1; | |
3178 | else | |
252b5132 | 3179 | { |
3e19fb8f L |
3180 | this_hdr->sh_name |
3181 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3182 | name, FALSE); | |
3183 | if (this_hdr->sh_name == (unsigned int) -1) | |
3184 | { | |
3185 | arg->failed = TRUE; | |
3186 | return; | |
3187 | } | |
252b5132 RH |
3188 | } |
3189 | ||
a4d8e49b | 3190 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3191 | |
3192 | if ((asect->flags & SEC_ALLOC) != 0 | |
3193 | || asect->user_set_vma) | |
3194 | this_hdr->sh_addr = asect->vma; | |
3195 | else | |
3196 | this_hdr->sh_addr = 0; | |
3197 | ||
3198 | this_hdr->sh_offset = 0; | |
eea6121a | 3199 | this_hdr->sh_size = asect->size; |
252b5132 | 3200 | this_hdr->sh_link = 0; |
c86934ce NC |
3201 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3202 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3203 | { | |
4eca0228 | 3204 | _bfd_error_handler |
695344c0 | 3205 | /* xgettext:c-format */ |
c86934ce | 3206 | (_("%B: error: Alignment power %d of section `%A' is too big"), |
c08bb8dd | 3207 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3208 | arg->failed = TRUE; |
3209 | return; | |
3210 | } | |
72de5009 | 3211 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3212 | /* The sh_entsize and sh_info fields may have been set already by |
3213 | copy_private_section_data. */ | |
3214 | ||
3215 | this_hdr->bfd_section = asect; | |
3216 | this_hdr->contents = NULL; | |
3217 | ||
3cddba1e L |
3218 | /* If the section type is unspecified, we set it based on |
3219 | asect->flags. */ | |
98ece1b3 AM |
3220 | if ((asect->flags & SEC_GROUP) != 0) |
3221 | sh_type = SHT_GROUP; | |
98ece1b3 | 3222 | else |
94be91de | 3223 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3224 | |
3cddba1e | 3225 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3226 | this_hdr->sh_type = sh_type; |
3227 | else if (this_hdr->sh_type == SHT_NOBITS | |
3228 | && sh_type == SHT_PROGBITS | |
3229 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3230 | { |
98ece1b3 AM |
3231 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3232 | allow the link to proceed. This can happen when users link | |
3233 | non-bss input sections to bss output sections, or emit data | |
3234 | to a bss output section via a linker script. */ | |
4eca0228 | 3235 | _bfd_error_handler |
58f0869b | 3236 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3237 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3238 | } |
3239 | ||
2f89ff8d | 3240 | switch (this_hdr->sh_type) |
252b5132 | 3241 | { |
2f89ff8d | 3242 | default: |
2f89ff8d L |
3243 | break; |
3244 | ||
3245 | case SHT_STRTAB: | |
2f89ff8d L |
3246 | case SHT_NOTE: |
3247 | case SHT_NOBITS: | |
3248 | case SHT_PROGBITS: | |
3249 | break; | |
606851fb AM |
3250 | |
3251 | case SHT_INIT_ARRAY: | |
3252 | case SHT_FINI_ARRAY: | |
3253 | case SHT_PREINIT_ARRAY: | |
3254 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3255 | break; | |
2f89ff8d L |
3256 | |
3257 | case SHT_HASH: | |
c7ac6ff8 | 3258 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3259 | break; |
5de3bf90 | 3260 | |
2f89ff8d | 3261 | case SHT_DYNSYM: |
252b5132 | 3262 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3263 | break; |
3264 | ||
3265 | case SHT_DYNAMIC: | |
252b5132 | 3266 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3267 | break; |
3268 | ||
3269 | case SHT_RELA: | |
3270 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3271 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3272 | break; | |
3273 | ||
3274 | case SHT_REL: | |
3275 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3276 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3277 | break; | |
3278 | ||
3279 | case SHT_GNU_versym: | |
252b5132 | 3280 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3281 | break; |
3282 | ||
3283 | case SHT_GNU_verdef: | |
252b5132 RH |
3284 | this_hdr->sh_entsize = 0; |
3285 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3286 | cverdefs. The linker will set cverdefs, but sh_info will be |
3287 | zero. */ | |
252b5132 RH |
3288 | if (this_hdr->sh_info == 0) |
3289 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3290 | else | |
3291 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3292 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3293 | break; |
3294 | ||
3295 | case SHT_GNU_verneed: | |
252b5132 RH |
3296 | this_hdr->sh_entsize = 0; |
3297 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3298 | cverrefs. The linker will set cverrefs, but sh_info will be |
3299 | zero. */ | |
252b5132 RH |
3300 | if (this_hdr->sh_info == 0) |
3301 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3302 | else | |
3303 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3304 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3305 | break; |
3306 | ||
3307 | case SHT_GROUP: | |
1783205a | 3308 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3309 | break; |
fdc90cb4 JJ |
3310 | |
3311 | case SHT_GNU_HASH: | |
3312 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3313 | break; | |
dbb410c3 | 3314 | } |
252b5132 RH |
3315 | |
3316 | if ((asect->flags & SEC_ALLOC) != 0) | |
3317 | this_hdr->sh_flags |= SHF_ALLOC; | |
3318 | if ((asect->flags & SEC_READONLY) == 0) | |
3319 | this_hdr->sh_flags |= SHF_WRITE; | |
3320 | if ((asect->flags & SEC_CODE) != 0) | |
3321 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3322 | if ((asect->flags & SEC_MERGE) != 0) |
3323 | { | |
3324 | this_hdr->sh_flags |= SHF_MERGE; | |
3325 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3326 | } |
84865015 NC |
3327 | if ((asect->flags & SEC_STRINGS) != 0) |
3328 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3329 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3330 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3331 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3332 | { |
3333 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3334 | if (asect->size == 0 |
3335 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3336 | { |
3a800eb9 | 3337 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3338 | |
704afa60 | 3339 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3340 | if (o != NULL) |
3341 | { | |
704afa60 | 3342 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3343 | if (this_hdr->sh_size != 0) |
3344 | this_hdr->sh_type = SHT_NOBITS; | |
3345 | } | |
704afa60 JJ |
3346 | } |
3347 | } | |
18ae9cc1 L |
3348 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3349 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3350 | |
d4730f92 BS |
3351 | /* If the section has relocs, set up a section header for the |
3352 | SHT_REL[A] section. If two relocation sections are required for | |
3353 | this section, it is up to the processor-specific back-end to | |
3354 | create the other. */ | |
3355 | if ((asect->flags & SEC_RELOC) != 0) | |
3356 | { | |
3357 | /* When doing a relocatable link, create both REL and RELA sections if | |
3358 | needed. */ | |
3359 | if (arg->link_info | |
3360 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3361 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3362 | && (bfd_link_relocatable (arg->link_info) |
3363 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3364 | { |
3365 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3366 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3367 | delay_st_name_p)) | |
d4730f92 BS |
3368 | { |
3369 | arg->failed = TRUE; | |
3370 | return; | |
3371 | } | |
3372 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3373 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3374 | delay_st_name_p)) | |
d4730f92 BS |
3375 | { |
3376 | arg->failed = TRUE; | |
3377 | return; | |
3378 | } | |
3379 | } | |
3380 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3381 | (asect->use_rela_p | |
3382 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3383 | name, |
3e19fb8f L |
3384 | asect->use_rela_p, |
3385 | delay_st_name_p)) | |
d4730f92 BS |
3386 | arg->failed = TRUE; |
3387 | } | |
3388 | ||
252b5132 | 3389 | /* Check for processor-specific section types. */ |
0414f35b | 3390 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3391 | if (bed->elf_backend_fake_sections |
3392 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3393 | arg->failed = TRUE; |
252b5132 | 3394 | |
42bb2e33 | 3395 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3396 | { |
3397 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3398 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3399 | this_hdr->sh_type = sh_type; |
3400 | } | |
252b5132 RH |
3401 | } |
3402 | ||
bcacc0f5 AM |
3403 | /* Fill in the contents of a SHT_GROUP section. Called from |
3404 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3405 | when ELF targets use the generic linker, ld. Called for ld -r | |
3406 | from bfd_elf_final_link. */ | |
dbb410c3 | 3407 | |
1126897b | 3408 | void |
217aa764 | 3409 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3410 | { |
a50b1753 | 3411 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3412 | asection *elt, *first; |
dbb410c3 | 3413 | unsigned char *loc; |
b34976b6 | 3414 | bfd_boolean gas; |
dbb410c3 | 3415 | |
7e4111ad L |
3416 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3417 | elfxx-ia64.c. */ | |
3418 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3419 | || *failedptr) |
3420 | return; | |
3421 | ||
bcacc0f5 AM |
3422 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3423 | { | |
3424 | unsigned long symindx = 0; | |
3425 | ||
3426 | /* elf_group_id will have been set up by objcopy and the | |
3427 | generic linker. */ | |
3428 | if (elf_group_id (sec) != NULL) | |
3429 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3430 | |
bcacc0f5 AM |
3431 | if (symindx == 0) |
3432 | { | |
3433 | /* If called from the assembler, swap_out_syms will have set up | |
3434 | elf_section_syms. */ | |
3435 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3436 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3437 | } | |
3438 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3439 | } | |
3440 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3441 | { |
bcacc0f5 AM |
3442 | /* The ELF backend linker sets sh_info to -2 when the group |
3443 | signature symbol is global, and thus the index can't be | |
3444 | set until all local symbols are output. */ | |
53720c49 AM |
3445 | asection *igroup; |
3446 | struct bfd_elf_section_data *sec_data; | |
3447 | unsigned long symndx; | |
3448 | unsigned long extsymoff; | |
bcacc0f5 AM |
3449 | struct elf_link_hash_entry *h; |
3450 | ||
53720c49 AM |
3451 | /* The point of this little dance to the first SHF_GROUP section |
3452 | then back to the SHT_GROUP section is that this gets us to | |
3453 | the SHT_GROUP in the input object. */ | |
3454 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3455 | sec_data = elf_section_data (igroup); | |
3456 | symndx = sec_data->this_hdr.sh_info; | |
3457 | extsymoff = 0; | |
bcacc0f5 AM |
3458 | if (!elf_bad_symtab (igroup->owner)) |
3459 | { | |
3460 | Elf_Internal_Shdr *symtab_hdr; | |
3461 | ||
3462 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3463 | extsymoff = symtab_hdr->sh_info; | |
3464 | } | |
3465 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3466 | while (h->root.type == bfd_link_hash_indirect | |
3467 | || h->root.type == bfd_link_hash_warning) | |
3468 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3469 | ||
3470 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3471 | } |
dbb410c3 | 3472 | |
1126897b | 3473 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3474 | gas = TRUE; |
dbb410c3 AM |
3475 | if (sec->contents == NULL) |
3476 | { | |
b34976b6 | 3477 | gas = FALSE; |
a50b1753 | 3478 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3479 | |
3480 | /* Arrange for the section to be written out. */ | |
3481 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3482 | if (sec->contents == NULL) |
3483 | { | |
b34976b6 | 3484 | *failedptr = TRUE; |
dbb410c3 AM |
3485 | return; |
3486 | } | |
3487 | } | |
3488 | ||
eea6121a | 3489 | loc = sec->contents + sec->size; |
dbb410c3 | 3490 | |
9dce4196 AM |
3491 | /* Get the pointer to the first section in the group that gas |
3492 | squirreled away here. objcopy arranges for this to be set to the | |
3493 | start of the input section group. */ | |
3494 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3495 | |
3496 | /* First element is a flag word. Rest of section is elf section | |
3497 | indices for all the sections of the group. Write them backwards | |
3498 | just to keep the group in the same order as given in .section | |
3499 | directives, not that it matters. */ | |
3500 | while (elt != NULL) | |
3501 | { | |
9dce4196 | 3502 | asection *s; |
9dce4196 | 3503 | |
9dce4196 | 3504 | s = elt; |
415f38a6 AM |
3505 | if (!gas) |
3506 | s = s->output_section; | |
3507 | if (s != NULL | |
3508 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3509 | { |
415f38a6 AM |
3510 | unsigned int idx = elf_section_data (s)->this_idx; |
3511 | ||
01e1a5bc | 3512 | loc -= 4; |
01e1a5bc NC |
3513 | H_PUT_32 (abfd, idx, loc); |
3514 | } | |
945906ff | 3515 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3516 | if (elt == first) |
3517 | break; | |
dbb410c3 AM |
3518 | } |
3519 | ||
7bdf4127 AB |
3520 | loc -= 4; |
3521 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3522 | |
9dce4196 | 3523 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3524 | } |
3525 | ||
bce964aa AM |
3526 | /* Given NAME, the name of a relocation section stripped of its |
3527 | .rel/.rela prefix, return the section in ABFD to which the | |
3528 | relocations apply. */ | |
bd53a53a L |
3529 | |
3530 | asection * | |
bce964aa AM |
3531 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3532 | { | |
3533 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3534 | section likely apply to .got.plt or .got section. */ | |
3535 | if (get_elf_backend_data (abfd)->want_got_plt | |
3536 | && strcmp (name, ".plt") == 0) | |
3537 | { | |
3538 | asection *sec; | |
3539 | ||
3540 | name = ".got.plt"; | |
3541 | sec = bfd_get_section_by_name (abfd, name); | |
3542 | if (sec != NULL) | |
3543 | return sec; | |
3544 | name = ".got"; | |
3545 | } | |
3546 | ||
3547 | return bfd_get_section_by_name (abfd, name); | |
3548 | } | |
3549 | ||
3550 | /* Return the section to which RELOC_SEC applies. */ | |
3551 | ||
3552 | static asection * | |
3553 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3554 | { |
3555 | const char *name; | |
3556 | unsigned int type; | |
3557 | bfd *abfd; | |
bce964aa | 3558 | const struct elf_backend_data *bed; |
bd53a53a L |
3559 | |
3560 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3561 | if (type != SHT_REL && type != SHT_RELA) | |
3562 | return NULL; | |
3563 | ||
3564 | /* We look up the section the relocs apply to by name. */ | |
3565 | name = reloc_sec->name; | |
bce964aa AM |
3566 | if (strncmp (name, ".rel", 4) != 0) |
3567 | return NULL; | |
3568 | name += 4; | |
3569 | if (type == SHT_RELA && *name++ != 'a') | |
3570 | return NULL; | |
bd53a53a | 3571 | |
bd53a53a | 3572 | abfd = reloc_sec->owner; |
bce964aa AM |
3573 | bed = get_elf_backend_data (abfd); |
3574 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3575 | } |
3576 | ||
252b5132 RH |
3577 | /* Assign all ELF section numbers. The dummy first section is handled here |
3578 | too. The link/info pointers for the standard section types are filled | |
3579 | in here too, while we're at it. */ | |
3580 | ||
b34976b6 | 3581 | static bfd_boolean |
da9f89d4 | 3582 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3583 | { |
3584 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3585 | asection *sec; | |
3e19fb8f | 3586 | unsigned int section_number; |
252b5132 | 3587 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3588 | struct bfd_elf_section_data *d; |
3516e984 | 3589 | bfd_boolean need_symtab; |
252b5132 RH |
3590 | |
3591 | section_number = 1; | |
3592 | ||
2b0f7ef9 JJ |
3593 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3594 | ||
da9f89d4 | 3595 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3596 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3597 | { |
ef53be89 | 3598 | size_t reloc_count = 0; |
14f2c699 | 3599 | |
da9f89d4 | 3600 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3601 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3602 | { |
5daa8fe7 | 3603 | d = elf_section_data (sec); |
da9f89d4 L |
3604 | |
3605 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3606 | { |
5daa8fe7 | 3607 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3608 | { |
3609 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3610 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3611 | abfd->section_count--; |
da9f89d4 | 3612 | } |
08a40648 | 3613 | else |
4fbb74a6 | 3614 | d->this_idx = section_number++; |
da9f89d4 | 3615 | } |
14f2c699 L |
3616 | |
3617 | /* Count relocations. */ | |
3618 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3619 | } |
14f2c699 L |
3620 | |
3621 | /* Clear HAS_RELOC if there are no relocations. */ | |
3622 | if (reloc_count == 0) | |
3623 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3624 | } |
3625 | ||
3626 | for (sec = abfd->sections; sec; sec = sec->next) | |
3627 | { | |
3628 | d = elf_section_data (sec); | |
3629 | ||
3630 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3631 | d->this_idx = section_number++; |
3e19fb8f L |
3632 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3633 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3634 | if (d->rel.hdr) |
2b0f7ef9 | 3635 | { |
d4730f92 | 3636 | d->rel.idx = section_number++; |
3e19fb8f L |
3637 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3638 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3639 | } |
d4730f92 BS |
3640 | else |
3641 | d->rel.idx = 0; | |
23bc299b | 3642 | |
d4730f92 | 3643 | if (d->rela.hdr) |
2b0f7ef9 | 3644 | { |
d4730f92 | 3645 | d->rela.idx = section_number++; |
3e19fb8f L |
3646 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3647 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3648 | } |
23bc299b | 3649 | else |
d4730f92 | 3650 | d->rela.idx = 0; |
252b5132 RH |
3651 | } |
3652 | ||
3516e984 L |
3653 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3654 | || (link_info == NULL | |
3655 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3656 | == HAS_RELOC))); | |
3657 | if (need_symtab) | |
252b5132 | 3658 | { |
12bd6957 | 3659 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3660 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3661 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3662 | { |
6a40cf0c NC |
3663 | elf_section_list * entry; |
3664 | ||
3665 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3666 | ||
3667 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3668 | entry->ndx = section_number++; | |
3669 | elf_symtab_shndx_list (abfd) = entry; | |
3670 | entry->hdr.sh_name | |
9ad5cbcf | 3671 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3672 | ".symtab_shndx", FALSE); |
6a40cf0c | 3673 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3674 | return FALSE; |
9ad5cbcf | 3675 | } |
12bd6957 | 3676 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3677 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3678 | } |
3679 | ||
dd905818 NC |
3680 | elf_shstrtab_sec (abfd) = section_number++; |
3681 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3682 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3683 | ||
1c52a645 L |
3684 | if (section_number >= SHN_LORESERVE) |
3685 | { | |
695344c0 | 3686 | /* xgettext:c-format */ |
1c52a645 L |
3687 | _bfd_error_handler (_("%B: too many sections: %u"), |
3688 | abfd, section_number); | |
3689 | return FALSE; | |
3690 | } | |
3691 | ||
9ad5cbcf | 3692 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3693 | elf_elfheader (abfd)->e_shnum = section_number; |
3694 | ||
3695 | /* Set up the list of section header pointers, in agreement with the | |
3696 | indices. */ | |
a50b1753 NC |
3697 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3698 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3699 | if (i_shdrp == NULL) |
b34976b6 | 3700 | return FALSE; |
252b5132 | 3701 | |
a50b1753 NC |
3702 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3703 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3704 | if (i_shdrp[0] == NULL) |
3705 | { | |
3706 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3707 | return FALSE; |
252b5132 | 3708 | } |
252b5132 RH |
3709 | |
3710 | elf_elfsections (abfd) = i_shdrp; | |
3711 | ||
12bd6957 | 3712 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3713 | if (need_symtab) |
252b5132 | 3714 | { |
12bd6957 | 3715 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3716 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3717 | { |
6a40cf0c NC |
3718 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3719 | BFD_ASSERT (entry != NULL); | |
3720 | i_shdrp[entry->ndx] = & entry->hdr; | |
3721 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3722 | } |
12bd6957 AM |
3723 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3724 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3725 | } |
38ce5b11 | 3726 | |
252b5132 RH |
3727 | for (sec = abfd->sections; sec; sec = sec->next) |
3728 | { | |
252b5132 | 3729 | asection *s; |
252b5132 | 3730 | |
91d6fa6a NC |
3731 | d = elf_section_data (sec); |
3732 | ||
252b5132 | 3733 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3734 | if (d->rel.idx != 0) |
3735 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3736 | if (d->rela.idx != 0) | |
3737 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3738 | |
3739 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3740 | ||
3741 | /* sh_link of a reloc section is the section index of the symbol | |
3742 | table. sh_info is the section index of the section to which | |
3743 | the relocation entries apply. */ | |
d4730f92 | 3744 | if (d->rel.idx != 0) |
252b5132 | 3745 | { |
12bd6957 | 3746 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3747 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3748 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3749 | } |
d4730f92 | 3750 | if (d->rela.idx != 0) |
23bc299b | 3751 | { |
12bd6957 | 3752 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3753 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3754 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3755 | } |
252b5132 | 3756 | |
38ce5b11 L |
3757 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3758 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3759 | { | |
3760 | s = elf_linked_to_section (sec); | |
3761 | if (s) | |
38ce5b11 | 3762 | { |
f2876037 | 3763 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3764 | if (link_info != NULL) |
38ce5b11 | 3765 | { |
f2876037 | 3766 | /* Check discarded linkonce section. */ |
dbaa2011 | 3767 | if (discarded_section (s)) |
38ce5b11 | 3768 | { |
ccd2ec6a | 3769 | asection *kept; |
4eca0228 | 3770 | _bfd_error_handler |
695344c0 | 3771 | /* xgettext:c-format */ |
63a5468a AM |
3772 | (_("%B: sh_link of section `%A' points to" |
3773 | " discarded section `%A' of `%B'"), | |
ccd2ec6a L |
3774 | abfd, d->this_hdr.bfd_section, |
3775 | s, s->owner); | |
3776 | /* Point to the kept section if it has the same | |
3777 | size as the discarded one. */ | |
c0f00686 | 3778 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3779 | if (kept == NULL) |
185d09ad | 3780 | { |
ccd2ec6a L |
3781 | bfd_set_error (bfd_error_bad_value); |
3782 | return FALSE; | |
185d09ad | 3783 | } |
ccd2ec6a | 3784 | s = kept; |
38ce5b11 | 3785 | } |
e424ecc8 | 3786 | |
ccd2ec6a L |
3787 | s = s->output_section; |
3788 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3789 | } |
f2876037 L |
3790 | else |
3791 | { | |
3792 | /* Handle objcopy. */ | |
3793 | if (s->output_section == NULL) | |
3794 | { | |
4eca0228 | 3795 | _bfd_error_handler |
695344c0 | 3796 | /* xgettext:c-format */ |
63a5468a AM |
3797 | (_("%B: sh_link of section `%A' points to" |
3798 | " removed section `%A' of `%B'"), | |
f2876037 L |
3799 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3800 | bfd_set_error (bfd_error_bad_value); | |
3801 | return FALSE; | |
3802 | } | |
3803 | s = s->output_section; | |
3804 | } | |
ccd2ec6a L |
3805 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3806 | } | |
3807 | else | |
3808 | { | |
3809 | /* PR 290: | |
3810 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3811 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3812 | sh_info fields. Hence we could get the situation | |
08a40648 | 3813 | where s is NULL. */ |
ccd2ec6a L |
3814 | const struct elf_backend_data *bed |
3815 | = get_elf_backend_data (abfd); | |
3816 | if (bed->link_order_error_handler) | |
3817 | bed->link_order_error_handler | |
695344c0 | 3818 | /* xgettext:c-format */ |
ccd2ec6a L |
3819 | (_("%B: warning: sh_link not set for section `%A'"), |
3820 | abfd, sec); | |
38ce5b11 L |
3821 | } |
3822 | } | |
3823 | ||
252b5132 RH |
3824 | switch (d->this_hdr.sh_type) |
3825 | { | |
3826 | case SHT_REL: | |
3827 | case SHT_RELA: | |
3828 | /* A reloc section which we are treating as a normal BFD | |
3829 | section. sh_link is the section index of the symbol | |
3830 | table. sh_info is the section index of the section to | |
3831 | which the relocation entries apply. We assume that an | |
3832 | allocated reloc section uses the dynamic symbol table. | |
3833 | FIXME: How can we be sure? */ | |
3834 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3835 | if (s != NULL) | |
3836 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3837 | ||
bce964aa | 3838 | s = elf_get_reloc_section (sec); |
252b5132 | 3839 | if (s != NULL) |
9ef5d938 L |
3840 | { |
3841 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3842 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3843 | } | |
252b5132 RH |
3844 | break; |
3845 | ||
3846 | case SHT_STRTAB: | |
3847 | /* We assume that a section named .stab*str is a stabs | |
3848 | string section. We look for a section with the same name | |
3849 | but without the trailing ``str'', and set its sh_link | |
3850 | field to point to this section. */ | |
0112cd26 | 3851 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3852 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3853 | { | |
3854 | size_t len; | |
3855 | char *alc; | |
3856 | ||
3857 | len = strlen (sec->name); | |
a50b1753 | 3858 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3859 | if (alc == NULL) |
b34976b6 | 3860 | return FALSE; |
d4c88bbb | 3861 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3862 | alc[len - 3] = '\0'; |
3863 | s = bfd_get_section_by_name (abfd, alc); | |
3864 | free (alc); | |
3865 | if (s != NULL) | |
3866 | { | |
3867 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3868 | ||
3869 | /* This is a .stab section. */ | |
0594c12d AM |
3870 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3871 | elf_section_data (s)->this_hdr.sh_entsize | |
3872 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3873 | } |
3874 | } | |
3875 | break; | |
3876 | ||
3877 | case SHT_DYNAMIC: | |
3878 | case SHT_DYNSYM: | |
3879 | case SHT_GNU_verneed: | |
3880 | case SHT_GNU_verdef: | |
3881 | /* sh_link is the section header index of the string table | |
3882 | used for the dynamic entries, or the symbol table, or the | |
3883 | version strings. */ | |
3884 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3885 | if (s != NULL) | |
3886 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3887 | break; | |
3888 | ||
7f1204bb JJ |
3889 | case SHT_GNU_LIBLIST: |
3890 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3891 | list used for the dynamic entries, or the symbol table, or |
3892 | the version strings. */ | |
7f1204bb JJ |
3893 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3894 | ? ".dynstr" : ".gnu.libstr"); | |
3895 | if (s != NULL) | |
3896 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3897 | break; | |
3898 | ||
252b5132 | 3899 | case SHT_HASH: |
fdc90cb4 | 3900 | case SHT_GNU_HASH: |
252b5132 RH |
3901 | case SHT_GNU_versym: |
3902 | /* sh_link is the section header index of the symbol table | |
3903 | this hash table or version table is for. */ | |
3904 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3905 | if (s != NULL) | |
3906 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3907 | break; | |
dbb410c3 AM |
3908 | |
3909 | case SHT_GROUP: | |
12bd6957 | 3910 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3911 | } |
3912 | } | |
3913 | ||
3e19fb8f L |
3914 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3915 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3916 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3917 | ||
b34976b6 | 3918 | return TRUE; |
252b5132 RH |
3919 | } |
3920 | ||
5372391b | 3921 | static bfd_boolean |
217aa764 | 3922 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3923 | { |
3924 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3925 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3926 | if (bed->elf_backend_sym_is_global) |
3927 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3928 | |
e47bf690 | 3929 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3930 | || bfd_is_und_section (bfd_get_section (sym)) |
3931 | || bfd_is_com_section (bfd_get_section (sym))); | |
3932 | } | |
3933 | ||
76359541 TP |
3934 | /* Filter global symbols of ABFD to include in the import library. All |
3935 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
3936 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
3937 | the beginning of that array. | |
3938 | ||
3939 | Returns the number of symbols to keep. */ | |
3940 | ||
3941 | unsigned int | |
3942 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
3943 | asymbol **syms, long symcount) | |
3944 | { | |
3945 | long src_count, dst_count = 0; | |
3946 | ||
3947 | for (src_count = 0; src_count < symcount; src_count++) | |
3948 | { | |
3949 | asymbol *sym = syms[src_count]; | |
3950 | char *name = (char *) bfd_asymbol_name (sym); | |
3951 | struct bfd_link_hash_entry *h; | |
3952 | ||
3953 | if (!sym_is_global (abfd, sym)) | |
3954 | continue; | |
3955 | ||
3956 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
3957 | if (h == NULL) |
3958 | continue; | |
76359541 TP |
3959 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
3960 | continue; | |
76359541 TP |
3961 | if (h->linker_def || h->ldscript_def) |
3962 | continue; | |
3963 | ||
3964 | syms[dst_count++] = sym; | |
3965 | } | |
3966 | ||
3967 | syms[dst_count] = NULL; | |
3968 | ||
3969 | return dst_count; | |
3970 | } | |
3971 | ||
5372391b | 3972 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3973 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3974 | |
3975 | static bfd_boolean | |
3976 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3977 | { | |
c6d8cab4 L |
3978 | elf_symbol_type *type_ptr; |
3979 | ||
3980 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3981 | return FALSE; | |
3982 | ||
3983 | type_ptr = elf_symbol_from (abfd, sym); | |
3984 | return ((type_ptr != NULL | |
3985 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
3986 | && bfd_is_abs_section (sym->section)) | |
3987 | || !(sym->section->owner == abfd | |
0f0a5e58 | 3988 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
3989 | && sym->section->output_offset == 0) |
3990 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
3991 | } |
3992 | ||
2633a79c AM |
3993 | /* Map symbol from it's internal number to the external number, moving |
3994 | all local symbols to be at the head of the list. */ | |
3995 | ||
b34976b6 | 3996 | static bfd_boolean |
12bd6957 | 3997 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 3998 | { |
dc810e39 | 3999 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4000 | asymbol **syms = bfd_get_outsymbols (abfd); |
4001 | asymbol **sect_syms; | |
dc810e39 AM |
4002 | unsigned int num_locals = 0; |
4003 | unsigned int num_globals = 0; | |
4004 | unsigned int num_locals2 = 0; | |
4005 | unsigned int num_globals2 = 0; | |
7292b3ac | 4006 | unsigned int max_index = 0; |
dc810e39 | 4007 | unsigned int idx; |
252b5132 RH |
4008 | asection *asect; |
4009 | asymbol **new_syms; | |
252b5132 RH |
4010 | |
4011 | #ifdef DEBUG | |
4012 | fprintf (stderr, "elf_map_symbols\n"); | |
4013 | fflush (stderr); | |
4014 | #endif | |
4015 | ||
252b5132 RH |
4016 | for (asect = abfd->sections; asect; asect = asect->next) |
4017 | { | |
4018 | if (max_index < asect->index) | |
4019 | max_index = asect->index; | |
4020 | } | |
4021 | ||
4022 | max_index++; | |
a50b1753 | 4023 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4024 | if (sect_syms == NULL) |
b34976b6 | 4025 | return FALSE; |
252b5132 | 4026 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4027 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4028 | |
079e9a2f AM |
4029 | /* Init sect_syms entries for any section symbols we have already |
4030 | decided to output. */ | |
252b5132 RH |
4031 | for (idx = 0; idx < symcount; idx++) |
4032 | { | |
dc810e39 | 4033 | asymbol *sym = syms[idx]; |
c044fabd | 4034 | |
252b5132 | 4035 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4036 | && sym->value == 0 |
2633a79c AM |
4037 | && !ignore_section_sym (abfd, sym) |
4038 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4039 | { |
5372391b | 4040 | asection *sec = sym->section; |
252b5132 | 4041 | |
5372391b AM |
4042 | if (sec->owner != abfd) |
4043 | sec = sec->output_section; | |
252b5132 | 4044 | |
5372391b | 4045 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4046 | } |
4047 | } | |
4048 | ||
252b5132 RH |
4049 | /* Classify all of the symbols. */ |
4050 | for (idx = 0; idx < symcount; idx++) | |
4051 | { | |
2633a79c | 4052 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4053 | num_globals++; |
2633a79c AM |
4054 | else if (!ignore_section_sym (abfd, syms[idx])) |
4055 | num_locals++; | |
252b5132 | 4056 | } |
079e9a2f | 4057 | |
5372391b | 4058 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4059 | sections will already have a section symbol in outsymbols, but |
4060 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4061 | at least in that case. */ | |
252b5132 RH |
4062 | for (asect = abfd->sections; asect; asect = asect->next) |
4063 | { | |
079e9a2f | 4064 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4065 | { |
079e9a2f | 4066 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4067 | num_locals++; |
4068 | else | |
4069 | num_globals++; | |
252b5132 RH |
4070 | } |
4071 | } | |
4072 | ||
4073 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
4074 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
4075 | sizeof (asymbol *)); | |
dc810e39 | 4076 | |
252b5132 | 4077 | if (new_syms == NULL) |
b34976b6 | 4078 | return FALSE; |
252b5132 RH |
4079 | |
4080 | for (idx = 0; idx < symcount; idx++) | |
4081 | { | |
4082 | asymbol *sym = syms[idx]; | |
dc810e39 | 4083 | unsigned int i; |
252b5132 | 4084 | |
2633a79c AM |
4085 | if (sym_is_global (abfd, sym)) |
4086 | i = num_locals + num_globals2++; | |
4087 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4088 | i = num_locals2++; |
4089 | else | |
2633a79c | 4090 | continue; |
252b5132 RH |
4091 | new_syms[i] = sym; |
4092 | sym->udata.i = i + 1; | |
4093 | } | |
4094 | for (asect = abfd->sections; asect; asect = asect->next) | |
4095 | { | |
079e9a2f | 4096 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4097 | { |
079e9a2f | 4098 | asymbol *sym = asect->symbol; |
dc810e39 | 4099 | unsigned int i; |
252b5132 | 4100 | |
079e9a2f | 4101 | sect_syms[asect->index] = sym; |
252b5132 RH |
4102 | if (!sym_is_global (abfd, sym)) |
4103 | i = num_locals2++; | |
4104 | else | |
4105 | i = num_locals + num_globals2++; | |
4106 | new_syms[i] = sym; | |
4107 | sym->udata.i = i + 1; | |
4108 | } | |
4109 | } | |
4110 | ||
4111 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4112 | ||
12bd6957 | 4113 | *pnum_locals = num_locals; |
b34976b6 | 4114 | return TRUE; |
252b5132 RH |
4115 | } |
4116 | ||
4117 | /* Align to the maximum file alignment that could be required for any | |
4118 | ELF data structure. */ | |
4119 | ||
268b6b39 | 4120 | static inline file_ptr |
217aa764 | 4121 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4122 | { |
4123 | return (off + align - 1) & ~(align - 1); | |
4124 | } | |
4125 | ||
4126 | /* Assign a file position to a section, optionally aligning to the | |
4127 | required section alignment. */ | |
4128 | ||
217aa764 AM |
4129 | file_ptr |
4130 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4131 | file_ptr offset, | |
4132 | bfd_boolean align) | |
252b5132 | 4133 | { |
72de5009 AM |
4134 | if (align && i_shdrp->sh_addralign > 1) |
4135 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4136 | i_shdrp->sh_offset = offset; |
4137 | if (i_shdrp->bfd_section != NULL) | |
4138 | i_shdrp->bfd_section->filepos = offset; | |
4139 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4140 | offset += i_shdrp->sh_size; | |
4141 | return offset; | |
4142 | } | |
4143 | ||
4144 | /* Compute the file positions we are going to put the sections at, and | |
4145 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4146 | is not NULL, this is being called by the ELF backend linker. */ | |
4147 | ||
b34976b6 | 4148 | bfd_boolean |
217aa764 AM |
4149 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4150 | struct bfd_link_info *link_info) | |
252b5132 | 4151 | { |
9c5bfbb7 | 4152 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4153 | struct fake_section_arg fsargs; |
b34976b6 | 4154 | bfd_boolean failed; |
ef10c3ac | 4155 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4156 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4157 | bfd_boolean need_symtab; |
252b5132 RH |
4158 | |
4159 | if (abfd->output_has_begun) | |
b34976b6 | 4160 | return TRUE; |
252b5132 RH |
4161 | |
4162 | /* Do any elf backend specific processing first. */ | |
4163 | if (bed->elf_backend_begin_write_processing) | |
4164 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4165 | ||
4166 | if (! prep_headers (abfd)) | |
b34976b6 | 4167 | return FALSE; |
252b5132 | 4168 | |
e6c51ed4 | 4169 | /* Post process the headers if necessary. */ |
78245035 | 4170 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4171 | |
d4730f92 BS |
4172 | fsargs.failed = FALSE; |
4173 | fsargs.link_info = link_info; | |
4174 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4175 | if (fsargs.failed) | |
b34976b6 | 4176 | return FALSE; |
252b5132 | 4177 | |
da9f89d4 | 4178 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4179 | return FALSE; |
252b5132 RH |
4180 | |
4181 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4182 | need_symtab = (link_info == NULL |
4183 | && (bfd_get_symcount (abfd) > 0 | |
4184 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4185 | == HAS_RELOC))); | |
4186 | if (need_symtab) | |
252b5132 RH |
4187 | { |
4188 | /* Non-zero if doing a relocatable link. */ | |
4189 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4190 | ||
4191 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4192 | return FALSE; |
252b5132 RH |
4193 | } |
4194 | ||
d4730f92 | 4195 | failed = FALSE; |
1126897b | 4196 | if (link_info == NULL) |
dbb410c3 | 4197 | { |
1126897b | 4198 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4199 | if (failed) |
b34976b6 | 4200 | return FALSE; |
dbb410c3 AM |
4201 | } |
4202 | ||
252b5132 RH |
4203 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4204 | /* sh_name was set in prep_headers. */ | |
4205 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4206 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4207 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4208 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4209 | shstrtab_hdr->sh_entsize = 0; |
4210 | shstrtab_hdr->sh_link = 0; | |
4211 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4212 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4213 | shstrtab_hdr->sh_addralign = 1; |
4214 | ||
c84fca4d | 4215 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4216 | return FALSE; |
252b5132 | 4217 | |
3516e984 | 4218 | if (need_symtab) |
252b5132 RH |
4219 | { |
4220 | file_ptr off; | |
4221 | Elf_Internal_Shdr *hdr; | |
4222 | ||
12bd6957 | 4223 | off = elf_next_file_pos (abfd); |
252b5132 | 4224 | |
6a40cf0c | 4225 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4226 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4227 | |
6a40cf0c NC |
4228 | if (elf_symtab_shndx_list (abfd) != NULL) |
4229 | { | |
4230 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4231 | if (hdr->sh_size != 0) | |
4232 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4233 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4234 | } | |
9ad5cbcf | 4235 | |
252b5132 | 4236 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4237 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4238 | |
12bd6957 | 4239 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4240 | |
4241 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4242 | out. */ |
252b5132 | 4243 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4244 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4245 | return FALSE; |
ef10c3ac | 4246 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4247 | } |
4248 | ||
b34976b6 | 4249 | abfd->output_has_begun = TRUE; |
252b5132 | 4250 | |
b34976b6 | 4251 | return TRUE; |
252b5132 RH |
4252 | } |
4253 | ||
8ded5a0f AM |
4254 | /* Make an initial estimate of the size of the program header. If we |
4255 | get the number wrong here, we'll redo section placement. */ | |
4256 | ||
4257 | static bfd_size_type | |
4258 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4259 | { | |
4260 | size_t segs; | |
4261 | asection *s; | |
2b05f1b7 | 4262 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4263 | |
4264 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4265 | and one for data. */ | |
4266 | segs = 2; | |
4267 | ||
4268 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4269 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4270 | { | |
4271 | /* If we have a loadable interpreter section, we need a | |
4272 | PT_INTERP segment. In this case, assume we also need a | |
4273 | PT_PHDR segment, although that may not be true for all | |
4274 | targets. */ | |
e9a38e0f | 4275 | segs += 2; |
8ded5a0f AM |
4276 | } |
4277 | ||
4278 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4279 | { | |
4280 | /* We need a PT_DYNAMIC segment. */ | |
4281 | ++segs; | |
f210dcff | 4282 | } |
08a40648 | 4283 | |
ceae84aa | 4284 | if (info != NULL && info->relro) |
f210dcff L |
4285 | { |
4286 | /* We need a PT_GNU_RELRO segment. */ | |
4287 | ++segs; | |
8ded5a0f AM |
4288 | } |
4289 | ||
12bd6957 | 4290 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4291 | { |
4292 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4293 | ++segs; | |
4294 | } | |
4295 | ||
12bd6957 | 4296 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4297 | { |
2b05f1b7 L |
4298 | /* We need a PT_GNU_STACK segment. */ |
4299 | ++segs; | |
4300 | } | |
94b11780 | 4301 | |
2b05f1b7 L |
4302 | for (s = abfd->sections; s != NULL; s = s->next) |
4303 | { | |
8ded5a0f | 4304 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4305 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4306 | { |
4307 | /* We need a PT_NOTE segment. */ | |
4308 | ++segs; | |
1c5265b5 JJ |
4309 | /* Try to create just one PT_NOTE segment |
4310 | for all adjacent loadable .note* sections. | |
4311 | gABI requires that within a PT_NOTE segment | |
4312 | (and also inside of each SHT_NOTE section) | |
4313 | each note is padded to a multiple of 4 size, | |
4314 | so we check whether the sections are correctly | |
4315 | aligned. */ | |
4316 | if (s->alignment_power == 2) | |
4317 | while (s->next != NULL | |
4318 | && s->next->alignment_power == 2 | |
4319 | && (s->next->flags & SEC_LOAD) != 0 | |
4320 | && CONST_STRNEQ (s->next->name, ".note")) | |
4321 | s = s->next; | |
8ded5a0f AM |
4322 | } |
4323 | } | |
4324 | ||
4325 | for (s = abfd->sections; s != NULL; s = s->next) | |
4326 | { | |
4327 | if (s->flags & SEC_THREAD_LOCAL) | |
4328 | { | |
4329 | /* We need a PT_TLS segment. */ | |
4330 | ++segs; | |
4331 | break; | |
4332 | } | |
4333 | } | |
4334 | ||
2b05f1b7 | 4335 | bed = get_elf_backend_data (abfd); |
a91e1603 L |
4336 | |
4337 | if ((abfd->flags & D_PAGED) != 0) | |
4338 | { | |
4339 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4340 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4341 | for (s = abfd->sections; s != NULL; s = s->next) | |
4342 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4343 | { | |
4344 | if (elf_section_data (s)->this_hdr.sh_info | |
4345 | > PT_GNU_MBIND_NUM) | |
4346 | { | |
4347 | _bfd_error_handler | |
4348 | /* xgettext:c-format */ | |
4349 | (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"), | |
4350 | abfd, s, elf_section_data (s)->this_hdr.sh_info); | |
4351 | continue; | |
4352 | } | |
4353 | /* Align mbind section to page size. */ | |
4354 | if (s->alignment_power < page_align_power) | |
4355 | s->alignment_power = page_align_power; | |
4356 | segs ++; | |
4357 | } | |
4358 | } | |
4359 | ||
4360 | /* Let the backend count up any program headers it might need. */ | |
4361 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4362 | { |
4363 | int a; | |
4364 | ||
4365 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4366 | if (a == -1) | |
4367 | abort (); | |
4368 | segs += a; | |
4369 | } | |
4370 | ||
4371 | return segs * bed->s->sizeof_phdr; | |
4372 | } | |
4373 | ||
2ea37f1c NC |
4374 | /* Find the segment that contains the output_section of section. */ |
4375 | ||
4376 | Elf_Internal_Phdr * | |
4377 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4378 | { | |
4379 | struct elf_segment_map *m; | |
4380 | Elf_Internal_Phdr *p; | |
4381 | ||
12bd6957 | 4382 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4383 | m != NULL; |
4384 | m = m->next, p++) | |
4385 | { | |
4386 | int i; | |
4387 | ||
4388 | for (i = m->count - 1; i >= 0; i--) | |
4389 | if (m->sections[i] == section) | |
4390 | return p; | |
4391 | } | |
4392 | ||
4393 | return NULL; | |
4394 | } | |
4395 | ||
252b5132 RH |
4396 | /* Create a mapping from a set of sections to a program segment. */ |
4397 | ||
217aa764 AM |
4398 | static struct elf_segment_map * |
4399 | make_mapping (bfd *abfd, | |
4400 | asection **sections, | |
4401 | unsigned int from, | |
4402 | unsigned int to, | |
4403 | bfd_boolean phdr) | |
252b5132 RH |
4404 | { |
4405 | struct elf_segment_map *m; | |
4406 | unsigned int i; | |
4407 | asection **hdrpp; | |
dc810e39 | 4408 | bfd_size_type amt; |
252b5132 | 4409 | |
dc810e39 AM |
4410 | amt = sizeof (struct elf_segment_map); |
4411 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4412 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4413 | if (m == NULL) |
4414 | return NULL; | |
4415 | m->next = NULL; | |
4416 | m->p_type = PT_LOAD; | |
4417 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4418 | m->sections[i - from] = *hdrpp; | |
4419 | m->count = to - from; | |
4420 | ||
4421 | if (from == 0 && phdr) | |
4422 | { | |
4423 | /* Include the headers in the first PT_LOAD segment. */ | |
4424 | m->includes_filehdr = 1; | |
4425 | m->includes_phdrs = 1; | |
4426 | } | |
4427 | ||
4428 | return m; | |
4429 | } | |
4430 | ||
229fcec5 MM |
4431 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4432 | on failure. */ | |
4433 | ||
4434 | struct elf_segment_map * | |
4435 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4436 | { | |
4437 | struct elf_segment_map *m; | |
4438 | ||
a50b1753 NC |
4439 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4440 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4441 | if (m == NULL) |
4442 | return NULL; | |
4443 | m->next = NULL; | |
4444 | m->p_type = PT_DYNAMIC; | |
4445 | m->count = 1; | |
4446 | m->sections[0] = dynsec; | |
08a40648 | 4447 | |
229fcec5 MM |
4448 | return m; |
4449 | } | |
4450 | ||
8ded5a0f | 4451 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4452 | |
b34976b6 | 4453 | static bfd_boolean |
3dea8fca AM |
4454 | elf_modify_segment_map (bfd *abfd, |
4455 | struct bfd_link_info *info, | |
4456 | bfd_boolean remove_empty_load) | |
252b5132 | 4457 | { |
252e386e | 4458 | struct elf_segment_map **m; |
8ded5a0f | 4459 | const struct elf_backend_data *bed; |
252b5132 | 4460 | |
8ded5a0f AM |
4461 | /* The placement algorithm assumes that non allocated sections are |
4462 | not in PT_LOAD segments. We ensure this here by removing such | |
4463 | sections from the segment map. We also remove excluded | |
252e386e AM |
4464 | sections. Finally, any PT_LOAD segment without sections is |
4465 | removed. */ | |
12bd6957 | 4466 | m = &elf_seg_map (abfd); |
252e386e | 4467 | while (*m) |
8ded5a0f AM |
4468 | { |
4469 | unsigned int i, new_count; | |
252b5132 | 4470 | |
252e386e | 4471 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4472 | { |
252e386e AM |
4473 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4474 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4475 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4476 | { |
252e386e AM |
4477 | (*m)->sections[new_count] = (*m)->sections[i]; |
4478 | new_count++; | |
8ded5a0f AM |
4479 | } |
4480 | } | |
252e386e | 4481 | (*m)->count = new_count; |
252b5132 | 4482 | |
1a9ccd70 NC |
4483 | if (remove_empty_load |
4484 | && (*m)->p_type == PT_LOAD | |
4485 | && (*m)->count == 0 | |
4486 | && !(*m)->includes_phdrs) | |
252e386e AM |
4487 | *m = (*m)->next; |
4488 | else | |
4489 | m = &(*m)->next; | |
8ded5a0f | 4490 | } |
252b5132 | 4491 | |
8ded5a0f AM |
4492 | bed = get_elf_backend_data (abfd); |
4493 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4494 | { |
252e386e | 4495 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4496 | return FALSE; |
252b5132 | 4497 | } |
252b5132 | 4498 | |
8ded5a0f AM |
4499 | return TRUE; |
4500 | } | |
252b5132 | 4501 | |
8ded5a0f | 4502 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4503 | |
8ded5a0f AM |
4504 | bfd_boolean |
4505 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4506 | { | |
4507 | unsigned int count; | |
4508 | struct elf_segment_map *m; | |
4509 | asection **sections = NULL; | |
4510 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4511 | bfd_boolean no_user_phdrs; |
252b5132 | 4512 | |
12bd6957 | 4513 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4514 | |
4515 | if (info != NULL) | |
4516 | info->user_phdrs = !no_user_phdrs; | |
4517 | ||
3dea8fca | 4518 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4519 | { |
8ded5a0f AM |
4520 | asection *s; |
4521 | unsigned int i; | |
4522 | struct elf_segment_map *mfirst; | |
4523 | struct elf_segment_map **pm; | |
4524 | asection *last_hdr; | |
4525 | bfd_vma last_size; | |
4526 | unsigned int phdr_index; | |
4527 | bfd_vma maxpagesize; | |
4528 | asection **hdrpp; | |
4529 | bfd_boolean phdr_in_segment = TRUE; | |
4530 | bfd_boolean writable; | |
4531 | int tls_count = 0; | |
4532 | asection *first_tls = NULL; | |
a91e1603 | 4533 | asection *first_mbind = NULL; |
8ded5a0f AM |
4534 | asection *dynsec, *eh_frame_hdr; |
4535 | bfd_size_type amt; | |
8d06853e | 4536 | bfd_vma addr_mask, wrap_to = 0; |
1a9ccd70 | 4537 | bfd_boolean linker_created_pt_phdr_segment = FALSE; |
252b5132 | 4538 | |
8ded5a0f | 4539 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4540 | |
a50b1753 NC |
4541 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4542 | sizeof (asection *)); | |
8ded5a0f | 4543 | if (sections == NULL) |
252b5132 | 4544 | goto error_return; |
252b5132 | 4545 | |
8d06853e AM |
4546 | /* Calculate top address, avoiding undefined behaviour of shift |
4547 | left operator when shift count is equal to size of type | |
4548 | being shifted. */ | |
4549 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4550 | addr_mask = (addr_mask << 1) + 1; | |
4551 | ||
8ded5a0f AM |
4552 | i = 0; |
4553 | for (s = abfd->sections; s != NULL; s = s->next) | |
4554 | { | |
4555 | if ((s->flags & SEC_ALLOC) != 0) | |
4556 | { | |
4557 | sections[i] = s; | |
4558 | ++i; | |
8d06853e AM |
4559 | /* A wrapping section potentially clashes with header. */ |
4560 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4561 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4562 | } |
4563 | } | |
4564 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4565 | count = i; | |
252b5132 | 4566 | |
8ded5a0f | 4567 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4568 | |
8ded5a0f | 4569 | /* Build the mapping. */ |
252b5132 | 4570 | |
8ded5a0f AM |
4571 | mfirst = NULL; |
4572 | pm = &mfirst; | |
252b5132 | 4573 | |
8ded5a0f AM |
4574 | /* If we have a .interp section, then create a PT_PHDR segment for |
4575 | the program headers and a PT_INTERP segment for the .interp | |
4576 | section. */ | |
4577 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4578 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4579 | { | |
4580 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4581 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4582 | if (m == NULL) |
4583 | goto error_return; | |
4584 | m->next = NULL; | |
4585 | m->p_type = PT_PHDR; | |
4586 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4587 | m->p_flags = PF_R | PF_X; | |
4588 | m->p_flags_valid = 1; | |
4589 | m->includes_phdrs = 1; | |
1a9ccd70 | 4590 | linker_created_pt_phdr_segment = TRUE; |
8ded5a0f AM |
4591 | *pm = m; |
4592 | pm = &m->next; | |
252b5132 | 4593 | |
8ded5a0f | 4594 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4595 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4596 | if (m == NULL) |
4597 | goto error_return; | |
4598 | m->next = NULL; | |
4599 | m->p_type = PT_INTERP; | |
4600 | m->count = 1; | |
4601 | m->sections[0] = s; | |
4602 | ||
4603 | *pm = m; | |
4604 | pm = &m->next; | |
252b5132 | 4605 | } |
8ded5a0f AM |
4606 | |
4607 | /* Look through the sections. We put sections in the same program | |
4608 | segment when the start of the second section can be placed within | |
4609 | a few bytes of the end of the first section. */ | |
4610 | last_hdr = NULL; | |
4611 | last_size = 0; | |
4612 | phdr_index = 0; | |
4613 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4614 | /* PR 17512: file: c8455299. |
4615 | Avoid divide-by-zero errors later on. | |
4616 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4617 | if (maxpagesize == 0) | |
4618 | maxpagesize = 1; | |
8ded5a0f AM |
4619 | writable = FALSE; |
4620 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4621 | if (dynsec != NULL | |
4622 | && (dynsec->flags & SEC_LOAD) == 0) | |
4623 | dynsec = NULL; | |
4624 | ||
4625 | /* Deal with -Ttext or something similar such that the first section | |
4626 | is not adjacent to the program headers. This is an | |
4627 | approximation, since at this point we don't know exactly how many | |
4628 | program headers we will need. */ | |
4629 | if (count > 0) | |
252b5132 | 4630 | { |
12bd6957 | 4631 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4632 | |
62d7a5f6 | 4633 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4634 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4635 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4636 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4637 | || (sections[0]->lma & addr_mask) < phdr_size |
4638 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4639 | < phdr_size % maxpagesize) | |
4640 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
1a9ccd70 NC |
4641 | { |
4642 | /* PR 20815: The ELF standard says that a PT_PHDR segment, if | |
4643 | present, must be included as part of the memory image of the | |
4644 | program. Ie it must be part of a PT_LOAD segment as well. | |
4645 | If we have had to create our own PT_PHDR segment, but it is | |
4646 | not going to be covered by the first PT_LOAD segment, then | |
4647 | force the inclusion if we can... */ | |
4648 | if ((abfd->flags & D_PAGED) != 0 | |
4649 | && linker_created_pt_phdr_segment) | |
4650 | phdr_in_segment = TRUE; | |
4651 | else | |
4652 | phdr_in_segment = FALSE; | |
4653 | } | |
252b5132 RH |
4654 | } |
4655 | ||
8ded5a0f | 4656 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4657 | { |
8ded5a0f AM |
4658 | asection *hdr; |
4659 | bfd_boolean new_segment; | |
4660 | ||
4661 | hdr = *hdrpp; | |
4662 | ||
4663 | /* See if this section and the last one will fit in the same | |
4664 | segment. */ | |
4665 | ||
4666 | if (last_hdr == NULL) | |
4667 | { | |
4668 | /* If we don't have a segment yet, then we don't need a new | |
4669 | one (we build the last one after this loop). */ | |
4670 | new_segment = FALSE; | |
4671 | } | |
4672 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4673 | { | |
4674 | /* If this section has a different relation between the | |
4675 | virtual address and the load address, then we need a new | |
4676 | segment. */ | |
4677 | new_segment = TRUE; | |
4678 | } | |
b5599592 AM |
4679 | else if (hdr->lma < last_hdr->lma + last_size |
4680 | || last_hdr->lma + last_size < last_hdr->lma) | |
4681 | { | |
4682 | /* If this section has a load address that makes it overlap | |
4683 | the previous section, then we need a new segment. */ | |
4684 | new_segment = TRUE; | |
4685 | } | |
39948a60 NC |
4686 | /* In the next test we have to be careful when last_hdr->lma is close |
4687 | to the end of the address space. If the aligned address wraps | |
4688 | around to the start of the address space, then there are no more | |
4689 | pages left in memory and it is OK to assume that the current | |
4690 | section can be included in the current segment. */ | |
4691 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4692 | > last_hdr->lma) | |
4693 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4694 | <= hdr->lma)) |
8ded5a0f AM |
4695 | { |
4696 | /* If putting this section in this segment would force us to | |
4697 | skip a page in the segment, then we need a new segment. */ | |
4698 | new_segment = TRUE; | |
4699 | } | |
4700 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4701 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4702 | && ((abfd->flags & D_PAGED) == 0 | |
4703 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4704 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4705 | { |
e5654c0f AM |
4706 | /* We don't want to put a loaded section after a |
4707 | nonloaded (ie. bss style) section in the same segment | |
4708 | as that will force the non-loaded section to be loaded. | |
4709 | Consider .tbss sections as loaded for this purpose. | |
4710 | However, like the writable/non-writable case below, | |
4711 | if they are on the same page then they must be put | |
4712 | in the same segment. */ | |
8ded5a0f AM |
4713 | new_segment = TRUE; |
4714 | } | |
4715 | else if ((abfd->flags & D_PAGED) == 0) | |
4716 | { | |
4717 | /* If the file is not demand paged, which means that we | |
4718 | don't require the sections to be correctly aligned in the | |
4719 | file, then there is no other reason for a new segment. */ | |
4720 | new_segment = FALSE; | |
4721 | } | |
4722 | else if (! writable | |
4723 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4724 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4725 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4726 | { |
4727 | /* We don't want to put a writable section in a read only | |
4728 | segment, unless they are on the same page in memory | |
4729 | anyhow. We already know that the last section does not | |
4730 | bring us past the current section on the page, so the | |
4731 | only case in which the new section is not on the same | |
4732 | page as the previous section is when the previous section | |
4733 | ends precisely on a page boundary. */ | |
4734 | new_segment = TRUE; | |
4735 | } | |
4736 | else | |
4737 | { | |
4738 | /* Otherwise, we can use the same segment. */ | |
4739 | new_segment = FALSE; | |
4740 | } | |
4741 | ||
2889e75b | 4742 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4743 | if (last_hdr != NULL |
4744 | && info != NULL | |
4745 | && info->callbacks->override_segment_assignment != NULL) | |
4746 | new_segment | |
4747 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4748 | last_hdr, | |
4749 | new_segment); | |
2889e75b | 4750 | |
8ded5a0f AM |
4751 | if (! new_segment) |
4752 | { | |
4753 | if ((hdr->flags & SEC_READONLY) == 0) | |
4754 | writable = TRUE; | |
4755 | last_hdr = hdr; | |
4756 | /* .tbss sections effectively have zero size. */ | |
4757 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4758 | != SEC_THREAD_LOCAL) | |
4759 | last_size = hdr->size; | |
4760 | else | |
4761 | last_size = 0; | |
4762 | continue; | |
4763 | } | |
4764 | ||
4765 | /* We need a new program segment. We must create a new program | |
4766 | header holding all the sections from phdr_index until hdr. */ | |
4767 | ||
4768 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4769 | if (m == NULL) | |
4770 | goto error_return; | |
4771 | ||
4772 | *pm = m; | |
4773 | pm = &m->next; | |
4774 | ||
252b5132 | 4775 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4776 | writable = TRUE; |
8ded5a0f AM |
4777 | else |
4778 | writable = FALSE; | |
4779 | ||
baaff79e JJ |
4780 | last_hdr = hdr; |
4781 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4782 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4783 | last_size = hdr->size; |
baaff79e JJ |
4784 | else |
4785 | last_size = 0; | |
8ded5a0f AM |
4786 | phdr_index = i; |
4787 | phdr_in_segment = FALSE; | |
252b5132 RH |
4788 | } |
4789 | ||
86b2281f AM |
4790 | /* Create a final PT_LOAD program segment, but not if it's just |
4791 | for .tbss. */ | |
4792 | if (last_hdr != NULL | |
4793 | && (i - phdr_index != 1 | |
4794 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4795 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4796 | { |
4797 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4798 | if (m == NULL) | |
4799 | goto error_return; | |
252b5132 | 4800 | |
8ded5a0f AM |
4801 | *pm = m; |
4802 | pm = &m->next; | |
4803 | } | |
252b5132 | 4804 | |
8ded5a0f AM |
4805 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4806 | if (dynsec != NULL) | |
4807 | { | |
4808 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4809 | if (m == NULL) | |
4810 | goto error_return; | |
4811 | *pm = m; | |
4812 | pm = &m->next; | |
4813 | } | |
252b5132 | 4814 | |
1c5265b5 JJ |
4815 | /* For each batch of consecutive loadable .note sections, |
4816 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4817 | because if we link together nonloadable .note sections and | |
4818 | loadable .note sections, we will generate two .note sections | |
4819 | in the output file. FIXME: Using names for section types is | |
4820 | bogus anyhow. */ | |
8ded5a0f AM |
4821 | for (s = abfd->sections; s != NULL; s = s->next) |
4822 | { | |
4823 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4824 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4825 | { |
1c5265b5 | 4826 | asection *s2; |
91d6fa6a NC |
4827 | |
4828 | count = 1; | |
8ded5a0f | 4829 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4830 | if (s->alignment_power == 2) |
4831 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4832 | { |
4833 | if (s2->next->alignment_power == 2 | |
4834 | && (s2->next->flags & SEC_LOAD) != 0 | |
4835 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4836 | && align_power (s2->lma + s2->size, 2) |
4837 | == s2->next->lma) | |
55b581a6 JJ |
4838 | count++; |
4839 | else | |
4840 | break; | |
4841 | } | |
1c5265b5 | 4842 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4843 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4844 | if (m == NULL) |
4845 | goto error_return; | |
4846 | m->next = NULL; | |
4847 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4848 | m->count = count; |
4849 | while (count > 1) | |
4850 | { | |
4851 | m->sections[m->count - count--] = s; | |
4852 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4853 | s = s->next; | |
4854 | } | |
4855 | m->sections[m->count - 1] = s; | |
4856 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4857 | *pm = m; |
4858 | pm = &m->next; | |
4859 | } | |
4860 | if (s->flags & SEC_THREAD_LOCAL) | |
4861 | { | |
4862 | if (! tls_count) | |
4863 | first_tls = s; | |
4864 | tls_count++; | |
4865 | } | |
a91e1603 L |
4866 | if (first_mbind == NULL |
4867 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
4868 | first_mbind = s; | |
8ded5a0f | 4869 | } |
252b5132 | 4870 | |
8ded5a0f AM |
4871 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4872 | if (tls_count > 0) | |
4873 | { | |
8ded5a0f AM |
4874 | amt = sizeof (struct elf_segment_map); |
4875 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4876 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4877 | if (m == NULL) |
4878 | goto error_return; | |
4879 | m->next = NULL; | |
4880 | m->p_type = PT_TLS; | |
4881 | m->count = tls_count; | |
4882 | /* Mandated PF_R. */ | |
4883 | m->p_flags = PF_R; | |
4884 | m->p_flags_valid = 1; | |
d923cae0 | 4885 | s = first_tls; |
91d6fa6a | 4886 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4887 | { |
d923cae0 L |
4888 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4889 | { | |
4890 | _bfd_error_handler | |
4891 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4892 | s = first_tls; | |
4893 | i = 0; | |
4894 | while (i < (unsigned int) tls_count) | |
4895 | { | |
4896 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4897 | { | |
4898 | _bfd_error_handler (_(" TLS: %A"), s); | |
4899 | i++; | |
4900 | } | |
4901 | else | |
4902 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4903 | s = s->next; | |
4904 | } | |
4905 | bfd_set_error (bfd_error_bad_value); | |
4906 | goto error_return; | |
4907 | } | |
4908 | m->sections[i] = s; | |
4909 | s = s->next; | |
8ded5a0f | 4910 | } |
252b5132 | 4911 | |
8ded5a0f AM |
4912 | *pm = m; |
4913 | pm = &m->next; | |
4914 | } | |
252b5132 | 4915 | |
a91e1603 L |
4916 | if (first_mbind && (abfd->flags & D_PAGED) != 0) |
4917 | for (s = first_mbind; s != NULL; s = s->next) | |
4918 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
4919 | && (elf_section_data (s)->this_hdr.sh_info | |
4920 | <= PT_GNU_MBIND_NUM)) | |
4921 | { | |
4922 | /* Mandated PF_R. */ | |
4923 | unsigned long p_flags = PF_R; | |
4924 | if ((s->flags & SEC_READONLY) == 0) | |
4925 | p_flags |= PF_W; | |
4926 | if ((s->flags & SEC_CODE) != 0) | |
4927 | p_flags |= PF_X; | |
4928 | ||
4929 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
4930 | m = bfd_zalloc (abfd, amt); | |
4931 | if (m == NULL) | |
4932 | goto error_return; | |
4933 | m->next = NULL; | |
4934 | m->p_type = (PT_GNU_MBIND_LO | |
4935 | + elf_section_data (s)->this_hdr.sh_info); | |
4936 | m->count = 1; | |
4937 | m->p_flags_valid = 1; | |
4938 | m->sections[0] = s; | |
4939 | m->p_flags = p_flags; | |
4940 | ||
4941 | *pm = m; | |
4942 | pm = &m->next; | |
4943 | } | |
4944 | ||
8ded5a0f AM |
4945 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4946 | segment. */ | |
12bd6957 | 4947 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4948 | if (eh_frame_hdr != NULL |
4949 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4950 | { |
dc810e39 | 4951 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4952 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4953 | if (m == NULL) |
4954 | goto error_return; | |
4955 | m->next = NULL; | |
8ded5a0f | 4956 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4957 | m->count = 1; |
8ded5a0f | 4958 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4959 | |
4960 | *pm = m; | |
4961 | pm = &m->next; | |
4962 | } | |
13ae64f3 | 4963 | |
12bd6957 | 4964 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4965 | { |
8ded5a0f | 4966 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4967 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4968 | if (m == NULL) |
4969 | goto error_return; | |
4970 | m->next = NULL; | |
2b05f1b7 | 4971 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4972 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4973 | m->p_align = bed->stack_align; |
8ded5a0f | 4974 | m->p_flags_valid = 1; |
04c3a755 NS |
4975 | m->p_align_valid = m->p_align != 0; |
4976 | if (info->stacksize > 0) | |
4977 | { | |
4978 | m->p_size = info->stacksize; | |
4979 | m->p_size_valid = 1; | |
4980 | } | |
252b5132 | 4981 | |
8ded5a0f AM |
4982 | *pm = m; |
4983 | pm = &m->next; | |
4984 | } | |
65765700 | 4985 | |
ceae84aa | 4986 | if (info != NULL && info->relro) |
8ded5a0f | 4987 | { |
f210dcff L |
4988 | for (m = mfirst; m != NULL; m = m->next) |
4989 | { | |
3832a4d8 AM |
4990 | if (m->p_type == PT_LOAD |
4991 | && m->count != 0 | |
4992 | && m->sections[0]->vma >= info->relro_start | |
4993 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 4994 | { |
3832a4d8 AM |
4995 | i = m->count; |
4996 | while (--i != (unsigned) -1) | |
4997 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4998 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
4999 | break; | |
5000 | ||
43a8475c | 5001 | if (i != (unsigned) -1) |
f210dcff L |
5002 | break; |
5003 | } | |
be01b344 | 5004 | } |
f210dcff L |
5005 | |
5006 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5007 | if (m != NULL) | |
5008 | { | |
5009 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5010 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5011 | if (m == NULL) |
5012 | goto error_return; | |
5013 | m->next = NULL; | |
5014 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5015 | *pm = m; |
5016 | pm = &m->next; | |
5017 | } | |
8ded5a0f | 5018 | } |
9ee5e499 | 5019 | |
8ded5a0f | 5020 | free (sections); |
12bd6957 | 5021 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5022 | } |
5023 | ||
3dea8fca | 5024 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5025 | return FALSE; |
8c37241b | 5026 | |
12bd6957 | 5027 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5028 | ++count; |
12bd6957 | 5029 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5030 | |
b34976b6 | 5031 | return TRUE; |
252b5132 RH |
5032 | |
5033 | error_return: | |
5034 | if (sections != NULL) | |
5035 | free (sections); | |
b34976b6 | 5036 | return FALSE; |
252b5132 RH |
5037 | } |
5038 | ||
5039 | /* Sort sections by address. */ | |
5040 | ||
5041 | static int | |
217aa764 | 5042 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5043 | { |
5044 | const asection *sec1 = *(const asection **) arg1; | |
5045 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5046 | bfd_size_type size1, size2; |
252b5132 RH |
5047 | |
5048 | /* Sort by LMA first, since this is the address used to | |
5049 | place the section into a segment. */ | |
5050 | if (sec1->lma < sec2->lma) | |
5051 | return -1; | |
5052 | else if (sec1->lma > sec2->lma) | |
5053 | return 1; | |
5054 | ||
5055 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5056 | the same, and this will do nothing. */ | |
5057 | if (sec1->vma < sec2->vma) | |
5058 | return -1; | |
5059 | else if (sec1->vma > sec2->vma) | |
5060 | return 1; | |
5061 | ||
5062 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5063 | ||
07c6e936 | 5064 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5065 | |
5066 | if (TOEND (sec1)) | |
5067 | { | |
5068 | if (TOEND (sec2)) | |
00a7cdc5 NC |
5069 | { |
5070 | /* If the indicies are the same, do not return 0 | |
5071 | here, but continue to try the next comparison. */ | |
5072 | if (sec1->target_index - sec2->target_index != 0) | |
5073 | return sec1->target_index - sec2->target_index; | |
5074 | } | |
252b5132 RH |
5075 | else |
5076 | return 1; | |
5077 | } | |
00a7cdc5 | 5078 | else if (TOEND (sec2)) |
252b5132 RH |
5079 | return -1; |
5080 | ||
5081 | #undef TOEND | |
5082 | ||
00a7cdc5 NC |
5083 | /* Sort by size, to put zero sized sections |
5084 | before others at the same address. */ | |
252b5132 | 5085 | |
eea6121a AM |
5086 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5087 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5088 | |
5089 | if (size1 < size2) | |
252b5132 | 5090 | return -1; |
eecdbe52 | 5091 | if (size1 > size2) |
252b5132 RH |
5092 | return 1; |
5093 | ||
5094 | return sec1->target_index - sec2->target_index; | |
5095 | } | |
5096 | ||
340b6d91 AC |
5097 | /* Ian Lance Taylor writes: |
5098 | ||
5099 | We shouldn't be using % with a negative signed number. That's just | |
5100 | not good. We have to make sure either that the number is not | |
5101 | negative, or that the number has an unsigned type. When the types | |
5102 | are all the same size they wind up as unsigned. When file_ptr is a | |
5103 | larger signed type, the arithmetic winds up as signed long long, | |
5104 | which is wrong. | |
5105 | ||
5106 | What we're trying to say here is something like ``increase OFF by | |
5107 | the least amount that will cause it to be equal to the VMA modulo | |
5108 | the page size.'' */ | |
5109 | /* In other words, something like: | |
5110 | ||
5111 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5112 | off_offset = off % bed->maxpagesize; | |
5113 | if (vma_offset < off_offset) | |
5114 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5115 | else | |
5116 | adjustment = vma_offset - off_offset; | |
08a40648 | 5117 | |
340b6d91 AC |
5118 | which can can be collapsed into the expression below. */ |
5119 | ||
5120 | static file_ptr | |
5121 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5122 | { | |
dc9155b2 NC |
5123 | /* PR binutils/16199: Handle an alignment of zero. */ |
5124 | if (maxpagesize == 0) | |
5125 | maxpagesize = 1; | |
340b6d91 AC |
5126 | return ((vma - off) % maxpagesize); |
5127 | } | |
5128 | ||
6d33f217 L |
5129 | static void |
5130 | print_segment_map (const struct elf_segment_map *m) | |
5131 | { | |
5132 | unsigned int j; | |
5133 | const char *pt = get_segment_type (m->p_type); | |
5134 | char buf[32]; | |
5135 | ||
5136 | if (pt == NULL) | |
5137 | { | |
5138 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5139 | sprintf (buf, "LOPROC+%7.7x", | |
5140 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5141 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5142 | sprintf (buf, "LOOS+%7.7x", | |
5143 | (unsigned int) (m->p_type - PT_LOOS)); | |
5144 | else | |
5145 | snprintf (buf, sizeof (buf), "%8.8x", | |
5146 | (unsigned int) m->p_type); | |
5147 | pt = buf; | |
5148 | } | |
4a97a0e5 | 5149 | fflush (stdout); |
6d33f217 L |
5150 | fprintf (stderr, "%s:", pt); |
5151 | for (j = 0; j < m->count; j++) | |
5152 | fprintf (stderr, " %s", m->sections [j]->name); | |
5153 | putc ('\n',stderr); | |
4a97a0e5 | 5154 | fflush (stderr); |
6d33f217 L |
5155 | } |
5156 | ||
32812159 AM |
5157 | static bfd_boolean |
5158 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5159 | { | |
5160 | void *buf; | |
5161 | bfd_boolean ret; | |
5162 | ||
5163 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5164 | return FALSE; | |
5165 | buf = bfd_zmalloc (len); | |
5166 | if (buf == NULL) | |
5167 | return FALSE; | |
5168 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5169 | free (buf); | |
5170 | return ret; | |
5171 | } | |
5172 | ||
252b5132 RH |
5173 | /* Assign file positions to the sections based on the mapping from |
5174 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5175 | the file header. */ |
252b5132 | 5176 | |
b34976b6 | 5177 | static bfd_boolean |
f3520d2f AM |
5178 | assign_file_positions_for_load_sections (bfd *abfd, |
5179 | struct bfd_link_info *link_info) | |
252b5132 RH |
5180 | { |
5181 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5182 | struct elf_segment_map *m; |
252b5132 | 5183 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5184 | Elf_Internal_Phdr *p; |
02bf8d82 | 5185 | file_ptr off; |
3f570048 | 5186 | bfd_size_type maxpagesize; |
a8c75b76 | 5187 | unsigned int pt_load_count = 0; |
f3520d2f | 5188 | unsigned int alloc; |
0920dee7 | 5189 | unsigned int i, j; |
2b0bc088 | 5190 | bfd_vma header_pad = 0; |
252b5132 | 5191 | |
e36284ab | 5192 | if (link_info == NULL |
ceae84aa | 5193 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5194 | return FALSE; |
252b5132 | 5195 | |
8ded5a0f | 5196 | alloc = 0; |
12bd6957 | 5197 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5198 | { |
5199 | ++alloc; | |
5200 | if (m->header_size) | |
5201 | header_pad = m->header_size; | |
5202 | } | |
252b5132 | 5203 | |
82f2dbf7 NC |
5204 | if (alloc) |
5205 | { | |
5206 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5207 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5208 | } | |
5209 | else | |
5210 | { | |
5211 | /* PR binutils/12467. */ | |
5212 | elf_elfheader (abfd)->e_phoff = 0; | |
5213 | elf_elfheader (abfd)->e_phentsize = 0; | |
5214 | } | |
d324f6d6 | 5215 | |
8ded5a0f | 5216 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5217 | |
12bd6957 AM |
5218 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5219 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5220 | else |
12bd6957 | 5221 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5222 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5223 | |
5224 | if (alloc == 0) | |
f3520d2f | 5225 | { |
12bd6957 | 5226 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5227 | return TRUE; |
f3520d2f | 5228 | } |
252b5132 | 5229 | |
12bd6957 | 5230 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5231 | see assign_file_positions_except_relocs, so make sure we have |
5232 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5233 | The variable alloc contains the computed need, while |
5234 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5235 | layout. |
5236 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5237 | where the layout is forced to according to a larger size in the | |
5238 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5239 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5240 | == 0); |
a50b1753 NC |
5241 | phdrs = (Elf_Internal_Phdr *) |
5242 | bfd_zalloc2 (abfd, | |
12bd6957 | 5243 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 5244 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 5245 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5246 | if (phdrs == NULL) |
b34976b6 | 5247 | return FALSE; |
252b5132 | 5248 | |
3f570048 AM |
5249 | maxpagesize = 1; |
5250 | if ((abfd->flags & D_PAGED) != 0) | |
5251 | maxpagesize = bed->maxpagesize; | |
5252 | ||
252b5132 RH |
5253 | off = bed->s->sizeof_ehdr; |
5254 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5255 | if (header_pad < (bfd_vma) off) |
5256 | header_pad = 0; | |
5257 | else | |
5258 | header_pad -= off; | |
5259 | off += header_pad; | |
252b5132 | 5260 | |
12bd6957 | 5261 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5262 | m != NULL; |
0920dee7 | 5263 | m = m->next, p++, j++) |
252b5132 | 5264 | { |
252b5132 | 5265 | asection **secpp; |
bf988460 AM |
5266 | bfd_vma off_adjust; |
5267 | bfd_boolean no_contents; | |
252b5132 RH |
5268 | |
5269 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5270 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5271 | not be done to the PT_NOTE section of a corefile, which may |
5272 | contain several pseudo-sections artificially created by bfd. | |
5273 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5274 | if (m->count > 1 |
5275 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5276 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5277 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5278 | elf_sort_sections); | |
5279 | ||
b301b248 AM |
5280 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5281 | number of sections with contents contributing to both p_filesz | |
5282 | and p_memsz, followed by a number of sections with no contents | |
5283 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5284 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5285 | p->p_type = m->p_type; |
28a7f3e7 | 5286 | p->p_flags = m->p_flags; |
252b5132 | 5287 | |
3f570048 AM |
5288 | if (m->count == 0) |
5289 | p->p_vaddr = 0; | |
5290 | else | |
3271a814 | 5291 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5292 | |
5293 | if (m->p_paddr_valid) | |
5294 | p->p_paddr = m->p_paddr; | |
5295 | else if (m->count == 0) | |
5296 | p->p_paddr = 0; | |
5297 | else | |
08a40648 | 5298 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5299 | |
5300 | if (p->p_type == PT_LOAD | |
5301 | && (abfd->flags & D_PAGED) != 0) | |
5302 | { | |
5303 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5304 | the maximum page size. When copying an executable with | |
5305 | objcopy, we set m->p_align from the input file. Use this | |
5306 | value for maxpagesize rather than bed->maxpagesize, which | |
5307 | may be different. Note that we use maxpagesize for PT_TLS | |
5308 | segment alignment later in this function, so we are relying | |
5309 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5310 | segment. */ | |
5311 | if (m->p_align_valid) | |
5312 | maxpagesize = m->p_align; | |
5313 | ||
5314 | p->p_align = maxpagesize; | |
a8c75b76 | 5315 | pt_load_count += 1; |
3f570048 | 5316 | } |
3271a814 NS |
5317 | else if (m->p_align_valid) |
5318 | p->p_align = m->p_align; | |
e970b90a DJ |
5319 | else if (m->count == 0) |
5320 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5321 | else |
5322 | p->p_align = 0; | |
5323 | ||
bf988460 AM |
5324 | no_contents = FALSE; |
5325 | off_adjust = 0; | |
252b5132 | 5326 | if (p->p_type == PT_LOAD |
b301b248 | 5327 | && m->count > 0) |
252b5132 | 5328 | { |
b301b248 | 5329 | bfd_size_type align; |
a49e53ed | 5330 | unsigned int align_power = 0; |
b301b248 | 5331 | |
3271a814 NS |
5332 | if (m->p_align_valid) |
5333 | align = p->p_align; | |
5334 | else | |
252b5132 | 5335 | { |
3271a814 NS |
5336 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5337 | { | |
5338 | unsigned int secalign; | |
08a40648 | 5339 | |
3271a814 NS |
5340 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5341 | if (secalign > align_power) | |
5342 | align_power = secalign; | |
5343 | } | |
5344 | align = (bfd_size_type) 1 << align_power; | |
5345 | if (align < maxpagesize) | |
5346 | align = maxpagesize; | |
b301b248 | 5347 | } |
252b5132 | 5348 | |
02bf8d82 AM |
5349 | for (i = 0; i < m->count; i++) |
5350 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5351 | /* If we aren't making room for this section, then | |
5352 | it must be SHT_NOBITS regardless of what we've | |
5353 | set via struct bfd_elf_special_section. */ | |
5354 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5355 | ||
bf988460 | 5356 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5357 | sections. */ |
5358 | no_contents = TRUE; | |
5359 | for (i = 0; i < m->count; i++) | |
5360 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5361 | { | |
5362 | no_contents = FALSE; | |
5363 | break; | |
5364 | } | |
bf988460 | 5365 | |
85cfcbfb | 5366 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
a8c75b76 AM |
5367 | |
5368 | /* Broken hardware and/or kernel require that files do not | |
5369 | map the same page with different permissions on some hppa | |
5370 | processors. */ | |
5371 | if (pt_load_count > 1 | |
5372 | && bed->no_page_alias | |
5373 | && (off & (maxpagesize - 1)) != 0 | |
5374 | && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize)) | |
5375 | off_adjust += maxpagesize; | |
bf988460 AM |
5376 | off += off_adjust; |
5377 | if (no_contents) | |
5378 | { | |
5379 | /* We shouldn't need to align the segment on disk since | |
5380 | the segment doesn't need file space, but the gABI | |
5381 | arguably requires the alignment and glibc ld.so | |
5382 | checks it. So to comply with the alignment | |
5383 | requirement but not waste file space, we adjust | |
5384 | p_offset for just this segment. (OFF_ADJUST is | |
5385 | subtracted from OFF later.) This may put p_offset | |
5386 | past the end of file, but that shouldn't matter. */ | |
5387 | } | |
5388 | else | |
5389 | off_adjust = 0; | |
252b5132 | 5390 | } |
b1a6d0b1 NC |
5391 | /* Make sure the .dynamic section is the first section in the |
5392 | PT_DYNAMIC segment. */ | |
5393 | else if (p->p_type == PT_DYNAMIC | |
5394 | && m->count > 1 | |
5395 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5396 | { | |
5397 | _bfd_error_handler | |
63a5468a AM |
5398 | (_("%B: The first section in the PT_DYNAMIC segment" |
5399 | " is not the .dynamic section"), | |
b301b248 | 5400 | abfd); |
b1a6d0b1 NC |
5401 | bfd_set_error (bfd_error_bad_value); |
5402 | return FALSE; | |
5403 | } | |
3f001e84 JK |
5404 | /* Set the note section type to SHT_NOTE. */ |
5405 | else if (p->p_type == PT_NOTE) | |
5406 | for (i = 0; i < m->count; i++) | |
5407 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5408 | |
252b5132 RH |
5409 | p->p_offset = 0; |
5410 | p->p_filesz = 0; | |
5411 | p->p_memsz = 0; | |
5412 | ||
5413 | if (m->includes_filehdr) | |
5414 | { | |
bf988460 | 5415 | if (!m->p_flags_valid) |
252b5132 | 5416 | p->p_flags |= PF_R; |
252b5132 RH |
5417 | p->p_filesz = bed->s->sizeof_ehdr; |
5418 | p->p_memsz = bed->s->sizeof_ehdr; | |
5419 | if (m->count > 0) | |
5420 | { | |
9ab82472 L |
5421 | if (p->p_vaddr < (bfd_vma) off |
5422 | || (!m->p_paddr_valid | |
5423 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5424 | { |
4eca0228 | 5425 | _bfd_error_handler |
63a5468a AM |
5426 | (_("%B: Not enough room for program headers," |
5427 | " try linking with -N"), | |
b301b248 | 5428 | abfd); |
252b5132 | 5429 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5430 | return FALSE; |
252b5132 RH |
5431 | } |
5432 | ||
5433 | p->p_vaddr -= off; | |
bf988460 | 5434 | if (!m->p_paddr_valid) |
252b5132 RH |
5435 | p->p_paddr -= off; |
5436 | } | |
252b5132 RH |
5437 | } |
5438 | ||
5439 | if (m->includes_phdrs) | |
5440 | { | |
bf988460 | 5441 | if (!m->p_flags_valid) |
252b5132 RH |
5442 | p->p_flags |= PF_R; |
5443 | ||
f3520d2f | 5444 | if (!m->includes_filehdr) |
252b5132 RH |
5445 | { |
5446 | p->p_offset = bed->s->sizeof_ehdr; | |
5447 | ||
5448 | if (m->count > 0) | |
5449 | { | |
252b5132 | 5450 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5451 | if (!m->p_paddr_valid) |
252b5132 RH |
5452 | p->p_paddr -= off - p->p_offset; |
5453 | } | |
252b5132 RH |
5454 | } |
5455 | ||
5456 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5457 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5458 | if (m->count) |
5459 | { | |
5460 | p->p_filesz += header_pad; | |
5461 | p->p_memsz += header_pad; | |
5462 | } | |
252b5132 RH |
5463 | } |
5464 | ||
5465 | if (p->p_type == PT_LOAD | |
5466 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5467 | { | |
bf988460 | 5468 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5469 | p->p_offset = off; |
252b5132 RH |
5470 | else |
5471 | { | |
5472 | file_ptr adjust; | |
5473 | ||
5474 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5475 | if (!no_contents) |
5476 | p->p_filesz += adjust; | |
252b5132 RH |
5477 | p->p_memsz += adjust; |
5478 | } | |
5479 | } | |
5480 | ||
1ea63fd2 AM |
5481 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5482 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5483 | core files, for sections in PT_NOTE segments. | |
5484 | assign_file_positions_for_non_load_sections will set filepos | |
5485 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5486 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5487 | { | |
5488 | asection *sec; | |
252b5132 | 5489 | bfd_size_type align; |
627b32bc | 5490 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5491 | |
5492 | sec = *secpp; | |
02bf8d82 | 5493 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5494 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5495 | |
88967714 AM |
5496 | if ((p->p_type == PT_LOAD |
5497 | || p->p_type == PT_TLS) | |
5498 | && (this_hdr->sh_type != SHT_NOBITS | |
5499 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5500 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5501 | || p->p_type == PT_TLS)))) | |
252b5132 | 5502 | { |
b5599592 AM |
5503 | bfd_vma p_start = p->p_paddr; |
5504 | bfd_vma p_end = p_start + p->p_memsz; | |
5505 | bfd_vma s_start = sec->lma; | |
5506 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5507 | |
a2d1e028 L |
5508 | if (adjust != 0 |
5509 | && (s_start < p_end | |
5510 | || p_end < p_start)) | |
252b5132 | 5511 | { |
4eca0228 | 5512 | _bfd_error_handler |
695344c0 | 5513 | /* xgettext:c-format */ |
76cfced5 AM |
5514 | (_("%B: section %A lma %#Lx adjusted to %#Lx"), |
5515 | abfd, sec, s_start, p_end); | |
88967714 | 5516 | adjust = 0; |
b5599592 | 5517 | sec->lma = p_end; |
1cfb7d1e | 5518 | } |
3ac9b6c9 | 5519 | p->p_memsz += adjust; |
1cfb7d1e | 5520 | |
88967714 AM |
5521 | if (this_hdr->sh_type != SHT_NOBITS) |
5522 | { | |
32812159 AM |
5523 | if (p->p_filesz + adjust < p->p_memsz) |
5524 | { | |
5525 | /* We have a PROGBITS section following NOBITS ones. | |
5526 | Allocate file space for the NOBITS section(s) and | |
5527 | zero it. */ | |
5528 | adjust = p->p_memsz - p->p_filesz; | |
5529 | if (!write_zeros (abfd, off, adjust)) | |
5530 | return FALSE; | |
5531 | } | |
88967714 AM |
5532 | off += adjust; |
5533 | p->p_filesz += adjust; | |
252b5132 | 5534 | } |
252b5132 RH |
5535 | } |
5536 | ||
5537 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5538 | { | |
b301b248 AM |
5539 | /* The section at i == 0 is the one that actually contains |
5540 | everything. */ | |
4a938328 MS |
5541 | if (i == 0) |
5542 | { | |
627b32bc | 5543 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5544 | off += this_hdr->sh_size; |
5545 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5546 | p->p_memsz = 0; |
5547 | p->p_align = 1; | |
252b5132 | 5548 | } |
4a938328 | 5549 | else |
252b5132 | 5550 | { |
b301b248 | 5551 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5552 | sec->filepos = 0; |
eea6121a | 5553 | sec->size = 0; |
b301b248 AM |
5554 | sec->flags = 0; |
5555 | continue; | |
252b5132 | 5556 | } |
252b5132 RH |
5557 | } |
5558 | else | |
5559 | { | |
1e951488 | 5560 | if (p->p_type == PT_LOAD) |
b301b248 | 5561 | { |
1e951488 AM |
5562 | this_hdr->sh_offset = sec->filepos = off; |
5563 | if (this_hdr->sh_type != SHT_NOBITS) | |
5564 | off += this_hdr->sh_size; | |
5565 | } | |
5566 | else if (this_hdr->sh_type == SHT_NOBITS | |
5567 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5568 | && this_hdr->sh_offset == 0) | |
5569 | { | |
5570 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5571 | (See _bfd_elf_map_sections_to_segments "Create a | |
5572 | final PT_LOAD".) Set sh_offset to the value it | |
5573 | would have if we had created a zero p_filesz and | |
5574 | p_memsz PT_LOAD header for the section. This | |
5575 | also makes the PT_TLS header have the same | |
5576 | p_offset value. */ | |
5577 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5578 | off, align); | |
5579 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5580 | } |
252b5132 | 5581 | |
02bf8d82 | 5582 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5583 | { |
6a3cd2b4 | 5584 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5585 | /* A load section without SHF_ALLOC is something like |
5586 | a note section in a PT_NOTE segment. These take | |
5587 | file space but are not loaded into memory. */ | |
5588 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5589 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5590 | } |
6a3cd2b4 | 5591 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5592 | { |
6a3cd2b4 AM |
5593 | if (p->p_type == PT_TLS) |
5594 | p->p_memsz += this_hdr->sh_size; | |
5595 | ||
5596 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5597 | normal segments. */ | |
5598 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5599 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5600 | } |
5601 | ||
b10a8ae0 L |
5602 | if (align > p->p_align |
5603 | && !m->p_align_valid | |
5604 | && (p->p_type != PT_LOAD | |
5605 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5606 | p->p_align = align; |
5607 | } | |
5608 | ||
bf988460 | 5609 | if (!m->p_flags_valid) |
252b5132 RH |
5610 | { |
5611 | p->p_flags |= PF_R; | |
02bf8d82 | 5612 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5613 | p->p_flags |= PF_X; |
02bf8d82 | 5614 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5615 | p->p_flags |= PF_W; |
5616 | } | |
5617 | } | |
43a8475c | 5618 | |
bf988460 | 5619 | off -= off_adjust; |
0920dee7 | 5620 | |
7c928300 AM |
5621 | /* Check that all sections are in a PT_LOAD segment. |
5622 | Don't check funky gdb generated core files. */ | |
5623 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5624 | { |
5625 | bfd_boolean check_vma = TRUE; | |
5626 | ||
5627 | for (i = 1; i < m->count; i++) | |
5628 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5629 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5630 | ->this_hdr), p) != 0 | |
5631 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5632 | ->this_hdr), p) != 0) | |
0920dee7 | 5633 | { |
9a83a553 AM |
5634 | /* Looks like we have overlays packed into the segment. */ |
5635 | check_vma = FALSE; | |
5636 | break; | |
0920dee7 | 5637 | } |
9a83a553 AM |
5638 | |
5639 | for (i = 0; i < m->count; i++) | |
5640 | { | |
5641 | Elf_Internal_Shdr *this_hdr; | |
5642 | asection *sec; | |
5643 | ||
5644 | sec = m->sections[i]; | |
5645 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5646 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5647 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5648 | { |
4eca0228 | 5649 | _bfd_error_handler |
695344c0 | 5650 | /* xgettext:c-format */ |
9a83a553 AM |
5651 | (_("%B: section `%A' can't be allocated in segment %d"), |
5652 | abfd, sec, j); | |
5653 | print_segment_map (m); | |
5654 | } | |
5655 | } | |
5656 | } | |
252b5132 RH |
5657 | } |
5658 | ||
12bd6957 | 5659 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5660 | return TRUE; |
5661 | } | |
5662 | ||
5663 | /* Assign file positions for the other sections. */ | |
5664 | ||
5665 | static bfd_boolean | |
5666 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5667 | struct bfd_link_info *link_info) | |
5668 | { | |
5669 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5670 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5671 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5672 | Elf_Internal_Phdr *phdrs; |
5673 | Elf_Internal_Phdr *p; | |
5674 | struct elf_segment_map *m; | |
62655c7b | 5675 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5676 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5677 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5678 | file_ptr off; | |
f3520d2f AM |
5679 | unsigned int count; |
5680 | ||
5c182d5f | 5681 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5682 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5683 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5684 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5685 | { |
5c182d5f AM |
5686 | Elf_Internal_Shdr *hdr; |
5687 | ||
5688 | hdr = *hdrpp; | |
5689 | if (hdr->bfd_section != NULL | |
252e386e AM |
5690 | && (hdr->bfd_section->filepos != 0 |
5691 | || (hdr->sh_type == SHT_NOBITS | |
5692 | && hdr->contents == NULL))) | |
627b32bc | 5693 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5694 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5695 | { | |
e8d2ba53 | 5696 | if (hdr->sh_size != 0) |
4eca0228 | 5697 | _bfd_error_handler |
695344c0 | 5698 | /* xgettext:c-format */ |
e8d2ba53 AM |
5699 | (_("%B: warning: allocated section `%s' not in segment"), |
5700 | abfd, | |
5701 | (hdr->bfd_section == NULL | |
5702 | ? "*unknown*" | |
5703 | : hdr->bfd_section->name)); | |
3ba71138 L |
5704 | /* We don't need to page align empty sections. */ |
5705 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5706 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5707 | bed->maxpagesize); | |
5708 | else | |
5709 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5710 | hdr->sh_addralign); | |
5711 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5712 | FALSE); | |
5713 | } | |
5714 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5715 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5716 | || (hdr->bfd_section != NULL |
5717 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5718 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5719 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5720 | || (elf_symtab_shndx_list (abfd) != NULL |
5721 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5722 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5723 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5724 | hdr->sh_offset = -1; |
5725 | else | |
5726 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5727 | } |
5728 | ||
252b5132 RH |
5729 | /* Now that we have set the section file positions, we can set up |
5730 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5731 | count = 0; |
5732 | filehdr_vaddr = 0; | |
5733 | filehdr_paddr = 0; | |
5734 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5735 | phdrs_paddr = 0; | |
62655c7b | 5736 | hdrs_segment = NULL; |
f3520d2f | 5737 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5738 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5739 | { |
5740 | ++count; | |
5741 | if (p->p_type != PT_LOAD) | |
5742 | continue; | |
5743 | ||
5744 | if (m->includes_filehdr) | |
5745 | { | |
5746 | filehdr_vaddr = p->p_vaddr; | |
5747 | filehdr_paddr = p->p_paddr; | |
5748 | } | |
5749 | if (m->includes_phdrs) | |
5750 | { | |
5751 | phdrs_vaddr = p->p_vaddr; | |
5752 | phdrs_paddr = p->p_paddr; | |
5753 | if (m->includes_filehdr) | |
5754 | { | |
62655c7b | 5755 | hdrs_segment = m; |
f3520d2f AM |
5756 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5757 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5758 | } | |
5759 | } | |
5760 | } | |
5761 | ||
62655c7b RM |
5762 | if (hdrs_segment != NULL && link_info != NULL) |
5763 | { | |
5764 | /* There is a segment that contains both the file headers and the | |
5765 | program headers, so provide a symbol __ehdr_start pointing there. | |
5766 | A program can use this to examine itself robustly. */ | |
5767 | ||
5768 | struct elf_link_hash_entry *hash | |
5769 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5770 | FALSE, FALSE, TRUE); | |
5771 | /* If the symbol was referenced and not defined, define it. */ | |
5772 | if (hash != NULL | |
5773 | && (hash->root.type == bfd_link_hash_new | |
5774 | || hash->root.type == bfd_link_hash_undefined | |
5775 | || hash->root.type == bfd_link_hash_undefweak | |
5776 | || hash->root.type == bfd_link_hash_common)) | |
5777 | { | |
5778 | asection *s = NULL; | |
5779 | if (hdrs_segment->count != 0) | |
5780 | /* The segment contains sections, so use the first one. */ | |
5781 | s = hdrs_segment->sections[0]; | |
5782 | else | |
5783 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5784 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5785 | if (m->count != 0) |
5786 | { | |
5787 | s = m->sections[0]; | |
5788 | break; | |
5789 | } | |
5790 | ||
5791 | if (s != NULL) | |
5792 | { | |
5793 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5794 | hash->root.u.def.section = s; | |
5795 | } | |
5796 | else | |
5797 | { | |
5798 | hash->root.u.def.value = filehdr_vaddr; | |
5799 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5800 | } | |
5801 | ||
5802 | hash->root.type = bfd_link_hash_defined; | |
5803 | hash->def_regular = 1; | |
5804 | hash->non_elf = 0; | |
5805 | } | |
5806 | } | |
5807 | ||
12bd6957 | 5808 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5809 | { |
129af99f | 5810 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5811 | { |
b84a33b5 | 5812 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5813 | struct elf_segment_map *lm; |
1ea63fd2 | 5814 | |
129af99f | 5815 | if (link_info != NULL) |
8c37241b | 5816 | { |
129af99f AS |
5817 | /* During linking the range of the RELRO segment is passed |
5818 | in link_info. */ | |
12bd6957 | 5819 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5820 | lm != NULL; |
5821 | lm = lm->next, lp++) | |
8c37241b JJ |
5822 | { |
5823 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5824 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5825 | && lm->count != 0 |
5826 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5827 | break; |
5828 | } | |
8981c88a | 5829 | |
3146fac4 | 5830 | BFD_ASSERT (lm != NULL); |
8c37241b | 5831 | } |
129af99f AS |
5832 | else |
5833 | { | |
5834 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5835 | library, but we need to use the same linker logic. */ |
129af99f AS |
5836 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5837 | { | |
5838 | if (lp->p_type == PT_LOAD | |
5839 | && lp->p_paddr == p->p_paddr) | |
5840 | break; | |
5841 | } | |
b84a33b5 AM |
5842 | } |
5843 | ||
5844 | if (lp < phdrs + count) | |
5845 | { | |
5846 | p->p_vaddr = lp->p_vaddr; | |
5847 | p->p_paddr = lp->p_paddr; | |
5848 | p->p_offset = lp->p_offset; | |
5849 | if (link_info != NULL) | |
5850 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5851 | else if (m->p_size_valid) | |
5852 | p->p_filesz = m->p_size; | |
129af99f AS |
5853 | else |
5854 | abort (); | |
b84a33b5 | 5855 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5856 | /* Preserve the alignment and flags if they are valid. The |
5857 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5858 | It is better for objcopy/strip to honor these attributes | |
5859 | otherwise gdb will choke when using separate debug files. | |
5860 | */ | |
5861 | if (!m->p_align_valid) | |
5862 | p->p_align = 1; | |
5863 | if (!m->p_flags_valid) | |
341b8ee7 | 5864 | p->p_flags = PF_R; |
129af99f | 5865 | } |
9433b9b1 | 5866 | else |
b84a33b5 AM |
5867 | { |
5868 | memset (p, 0, sizeof *p); | |
5869 | p->p_type = PT_NULL; | |
5870 | } | |
129af99f | 5871 | } |
04c3a755 NS |
5872 | else if (p->p_type == PT_GNU_STACK) |
5873 | { | |
5874 | if (m->p_size_valid) | |
5875 | p->p_memsz = m->p_size; | |
5876 | } | |
129af99f AS |
5877 | else if (m->count != 0) |
5878 | { | |
e06efbf1 | 5879 | unsigned int i; |
1a9ccd70 | 5880 | |
129af99f AS |
5881 | if (p->p_type != PT_LOAD |
5882 | && (p->p_type != PT_NOTE | |
5883 | || bfd_get_format (abfd) != bfd_core)) | |
5884 | { | |
1a9ccd70 NC |
5885 | /* A user specified segment layout may include a PHDR |
5886 | segment that overlaps with a LOAD segment... */ | |
5887 | if (p->p_type == PT_PHDR) | |
5888 | { | |
5889 | m->count = 0; | |
5890 | continue; | |
5891 | } | |
5892 | ||
c86934ce NC |
5893 | if (m->includes_filehdr || m->includes_phdrs) |
5894 | { | |
b1fa9dd6 | 5895 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 5896 | _bfd_error_handler |
76cfced5 AM |
5897 | (_("%B: error: non-load segment %d includes file header " |
5898 | "and/or program header"), | |
5899 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
5900 | return FALSE; |
5901 | } | |
129af99f | 5902 | |
86b2281f | 5903 | p->p_filesz = 0; |
129af99f | 5904 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5905 | for (i = m->count; i-- != 0;) |
5906 | { | |
5907 | asection *sect = m->sections[i]; | |
5908 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5909 | if (hdr->sh_type != SHT_NOBITS) | |
5910 | { | |
5911 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5912 | + hdr->sh_size); | |
5913 | break; | |
5914 | } | |
5915 | } | |
129af99f AS |
5916 | } |
5917 | } | |
5918 | else if (m->includes_filehdr) | |
5919 | { | |
5920 | p->p_vaddr = filehdr_vaddr; | |
5921 | if (! m->p_paddr_valid) | |
5922 | p->p_paddr = filehdr_paddr; | |
5923 | } | |
5924 | else if (m->includes_phdrs) | |
5925 | { | |
5926 | p->p_vaddr = phdrs_vaddr; | |
5927 | if (! m->p_paddr_valid) | |
5928 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5929 | } |
5930 | } | |
5931 | ||
12bd6957 | 5932 | elf_next_file_pos (abfd) = off; |
252b5132 | 5933 | |
b34976b6 | 5934 | return TRUE; |
252b5132 RH |
5935 | } |
5936 | ||
6a40cf0c NC |
5937 | static elf_section_list * |
5938 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5939 | { | |
5940 | for (;list != NULL; list = list->next) | |
5941 | if (list->ndx == i) | |
5942 | break; | |
5943 | return list; | |
5944 | } | |
5945 | ||
252b5132 RH |
5946 | /* Work out the file positions of all the sections. This is called by |
5947 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5948 | VMAs must be known before this is called. | |
5949 | ||
e0638f70 AM |
5950 | Reloc sections come in two flavours: Those processed specially as |
5951 | "side-channel" data attached to a section to which they apply, and | |
5952 | those that bfd doesn't process as relocations. The latter sort are | |
5953 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5954 | consider the former sort here, unless they form part of the loadable | |
5955 | image. Reloc sections not assigned here will be handled later by | |
5956 | assign_file_positions_for_relocs. | |
252b5132 RH |
5957 | |
5958 | We also don't set the positions of the .symtab and .strtab here. */ | |
5959 | ||
b34976b6 | 5960 | static bfd_boolean |
c84fca4d AO |
5961 | assign_file_positions_except_relocs (bfd *abfd, |
5962 | struct bfd_link_info *link_info) | |
252b5132 | 5963 | { |
5c182d5f AM |
5964 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5965 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5966 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5967 | |
5968 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5969 | && bfd_get_format (abfd) != bfd_core) | |
5970 | { | |
5c182d5f AM |
5971 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5972 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5973 | Elf_Internal_Shdr **hdrpp; |
5974 | unsigned int i; | |
a485e98e | 5975 | file_ptr off; |
252b5132 RH |
5976 | |
5977 | /* Start after the ELF header. */ | |
5978 | off = i_ehdrp->e_ehsize; | |
5979 | ||
5980 | /* We are not creating an executable, which means that we are | |
5981 | not creating a program header, and that the actual order of | |
5982 | the sections in the file is unimportant. */ | |
9ad5cbcf | 5983 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
5984 | { |
5985 | Elf_Internal_Shdr *hdr; | |
5986 | ||
5987 | hdr = *hdrpp; | |
e0638f70 AM |
5988 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
5989 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5990 | || (hdr->bfd_section != NULL |
5991 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5992 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5993 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
5994 | || (elf_symtab_shndx_list (abfd) != NULL |
5995 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5996 | || i == elf_strtab_sec (abfd) |
5997 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
5998 | { |
5999 | hdr->sh_offset = -1; | |
252b5132 | 6000 | } |
9ad5cbcf | 6001 | else |
b34976b6 | 6002 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6003 | } |
a485e98e AM |
6004 | |
6005 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
6006 | } |
6007 | else | |
6008 | { | |
f3520d2f AM |
6009 | unsigned int alloc; |
6010 | ||
252b5132 | 6011 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6012 | assignment of sections to segments. */ |
f3520d2f AM |
6013 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6014 | return FALSE; | |
6015 | ||
6016 | /* And for non-load sections. */ | |
6017 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6018 | return FALSE; | |
6019 | ||
e36284ab AM |
6020 | if (bed->elf_backend_modify_program_headers != NULL) |
6021 | { | |
6022 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
6023 | return FALSE; | |
6024 | } | |
6025 | ||
58e7ebac | 6026 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 6027 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
6028 | { |
6029 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
6030 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
6031 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6032 | ||
6033 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6034 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6035 | for (; segment < end_segment; segment++) | |
6036 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6037 | p_vaddr = segment->p_vaddr; | |
6038 | ||
6039 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6040 | segments is non-zero. */ | |
6041 | if (p_vaddr) | |
6042 | i_ehdrp->e_type = ET_EXEC; | |
6043 | } | |
6044 | ||
f3520d2f | 6045 | /* Write out the program headers. */ |
12bd6957 | 6046 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
1a9ccd70 NC |
6047 | |
6048 | /* Sort the program headers into the ordering required by the ELF standard. */ | |
6049 | if (alloc == 0) | |
6050 | return TRUE; | |
6051 | ||
1a9ccd70 NC |
6052 | /* PR ld/20815 - Check that the program header segment, if present, will |
6053 | be loaded into memory. FIXME: The check below is not sufficient as | |
6054 | really all PT_LOAD segments should be checked before issuing an error | |
6055 | message. Plus the PHDR segment does not have to be the first segment | |
6056 | in the program header table. But this version of the check should | |
cd584857 NC |
6057 | catch all real world use cases. |
6058 | ||
6059 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6060 | ascending order, as per the ELF spec. But this breaks some programs, | |
6061 | including the Linux kernel. But really either the spec should be | |
6062 | changed or the programs updated. */ | |
1a9ccd70 | 6063 | if (alloc > 1 |
cd584857 NC |
6064 | && tdata->phdr[0].p_type == PT_PHDR |
6065 | && ! bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, alloc) | |
6066 | && tdata->phdr[1].p_type == PT_LOAD | |
6067 | && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr | |
6068 | || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz) | |
6069 | < (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz))) | |
1a9ccd70 NC |
6070 | { |
6071 | /* The fix for this error is usually to edit the linker script being | |
6072 | used and set up the program headers manually. Either that or | |
6073 | leave room for the headers at the start of the SECTIONS. */ | |
6074 | _bfd_error_handler (_("\ | |
6075 | %B: error: PHDR segment not covered by LOAD segment"), | |
6076 | abfd); | |
1a9ccd70 NC |
6077 | return FALSE; |
6078 | } | |
6079 | ||
f3520d2f | 6080 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
cd584857 NC |
6081 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6082 | return FALSE; | |
252b5132 RH |
6083 | } |
6084 | ||
b34976b6 | 6085 | return TRUE; |
252b5132 RH |
6086 | } |
6087 | ||
b34976b6 | 6088 | static bfd_boolean |
217aa764 | 6089 | prep_headers (bfd *abfd) |
252b5132 | 6090 | { |
3d540e93 | 6091 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6092 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6093 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6094 | |
6095 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6096 | |
2b0f7ef9 | 6097 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6098 | if (shstrtab == NULL) |
b34976b6 | 6099 | return FALSE; |
252b5132 RH |
6100 | |
6101 | elf_shstrtab (abfd) = shstrtab; | |
6102 | ||
6103 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6104 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6105 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6106 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6107 | ||
6108 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6109 | i_ehdrp->e_ident[EI_DATA] = | |
6110 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6111 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6112 | ||
252b5132 RH |
6113 | if ((abfd->flags & DYNAMIC) != 0) |
6114 | i_ehdrp->e_type = ET_DYN; | |
6115 | else if ((abfd->flags & EXEC_P) != 0) | |
6116 | i_ehdrp->e_type = ET_EXEC; | |
6117 | else if (bfd_get_format (abfd) == bfd_core) | |
6118 | i_ehdrp->e_type = ET_CORE; | |
6119 | else | |
6120 | i_ehdrp->e_type = ET_REL; | |
6121 | ||
6122 | switch (bfd_get_arch (abfd)) | |
6123 | { | |
6124 | case bfd_arch_unknown: | |
6125 | i_ehdrp->e_machine = EM_NONE; | |
6126 | break; | |
aa4f99bb AO |
6127 | |
6128 | /* There used to be a long list of cases here, each one setting | |
6129 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6130 | in the corresponding bfd definition. To avoid duplication, | |
6131 | the switch was removed. Machines that need special handling | |
6132 | can generally do it in elf_backend_final_write_processing(), | |
6133 | unless they need the information earlier than the final write. | |
6134 | Such need can generally be supplied by replacing the tests for | |
6135 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6136 | default: |
9c5bfbb7 AM |
6137 | i_ehdrp->e_machine = bed->elf_machine_code; |
6138 | } | |
aa4f99bb | 6139 | |
252b5132 RH |
6140 | i_ehdrp->e_version = bed->s->ev_current; |
6141 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6142 | ||
c044fabd | 6143 | /* No program header, for now. */ |
252b5132 RH |
6144 | i_ehdrp->e_phoff = 0; |
6145 | i_ehdrp->e_phentsize = 0; | |
6146 | i_ehdrp->e_phnum = 0; | |
6147 | ||
c044fabd | 6148 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6149 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6150 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6151 | ||
c044fabd | 6152 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6153 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6154 | /* It all happens later. */ |
6155 | ; | |
252b5132 RH |
6156 | else |
6157 | { | |
6158 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6159 | i_ehdrp->e_phoff = 0; |
6160 | } | |
6161 | ||
6162 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6163 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6164 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6165 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6166 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6167 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6168 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6169 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6170 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6171 | return FALSE; |
252b5132 | 6172 | |
b34976b6 | 6173 | return TRUE; |
252b5132 RH |
6174 | } |
6175 | ||
6176 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6177 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6178 | |
0ce398f1 L |
6179 | static bfd_boolean |
6180 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6181 | { |
6182 | file_ptr off; | |
e06efbf1 | 6183 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6184 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6185 | Elf_Internal_Ehdr *i_ehdrp; |
6186 | const struct elf_backend_data *bed; | |
252b5132 | 6187 | |
12bd6957 | 6188 | off = elf_next_file_pos (abfd); |
252b5132 | 6189 | |
e06efbf1 L |
6190 | shdrpp = elf_elfsections (abfd); |
6191 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6192 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6193 | { |
252b5132 | 6194 | shdrp = *shdrpp; |
0ce398f1 L |
6195 | if (shdrp->sh_offset == -1) |
6196 | { | |
3e19fb8f | 6197 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6198 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6199 | || shdrp->sh_type == SHT_RELA); | |
6200 | if (is_rel | |
3e19fb8f | 6201 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6202 | { |
6203 | if (!is_rel) | |
6204 | { | |
3e19fb8f L |
6205 | const char *name = sec->name; |
6206 | struct bfd_elf_section_data *d; | |
6207 | ||
0ce398f1 | 6208 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6209 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6210 | shdrp->contents)) |
6211 | return FALSE; | |
3e19fb8f L |
6212 | |
6213 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6214 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6215 | { | |
6216 | /* If section is compressed with zlib-gnu, convert | |
6217 | section name from .debug_* to .zdebug_*. */ | |
6218 | char *new_name | |
6219 | = convert_debug_to_zdebug (abfd, name); | |
6220 | if (new_name == NULL) | |
6221 | return FALSE; | |
6222 | name = new_name; | |
6223 | } | |
dd905818 | 6224 | /* Add section name to section name section. */ |
3e19fb8f L |
6225 | if (shdrp->sh_name != (unsigned int) -1) |
6226 | abort (); | |
6227 | shdrp->sh_name | |
6228 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6229 | name, FALSE); | |
6230 | d = elf_section_data (sec); | |
6231 | ||
dd905818 | 6232 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6233 | if (d->rel.hdr |
6234 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6235 | d->rel.hdr, | |
6236 | name, FALSE)) | |
6237 | return FALSE; | |
6238 | if (d->rela.hdr | |
6239 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6240 | d->rela.hdr, | |
91cb26da | 6241 | name, TRUE)) |
3e19fb8f L |
6242 | return FALSE; |
6243 | ||
0ce398f1 | 6244 | /* Update section size and contents. */ |
3e19fb8f L |
6245 | shdrp->sh_size = sec->size; |
6246 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6247 | shdrp->bfd_section->contents = NULL; |
6248 | } | |
6249 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6250 | off, | |
6251 | TRUE); | |
6252 | } | |
6253 | } | |
252b5132 RH |
6254 | } |
6255 | ||
3e19fb8f L |
6256 | /* Place section name section after DWARF debug sections have been |
6257 | compressed. */ | |
6258 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6259 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6260 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6261 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6262 | ||
6263 | /* Place the section headers. */ | |
a485e98e AM |
6264 | i_ehdrp = elf_elfheader (abfd); |
6265 | bed = get_elf_backend_data (abfd); | |
6266 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6267 | i_ehdrp->e_shoff = off; | |
6268 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6269 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6270 | |
6271 | return TRUE; | |
252b5132 RH |
6272 | } |
6273 | ||
b34976b6 | 6274 | bfd_boolean |
217aa764 | 6275 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6276 | { |
9c5bfbb7 | 6277 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6278 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6279 | bfd_boolean failed; |
9ad5cbcf | 6280 | unsigned int count, num_sec; |
30e8ee25 | 6281 | struct elf_obj_tdata *t; |
252b5132 RH |
6282 | |
6283 | if (! abfd->output_has_begun | |
217aa764 | 6284 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6285 | return FALSE; |
252b5132 RH |
6286 | |
6287 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6288 | |
b34976b6 | 6289 | failed = FALSE; |
252b5132 RH |
6290 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6291 | if (failed) | |
b34976b6 | 6292 | return FALSE; |
252b5132 | 6293 | |
0ce398f1 L |
6294 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6295 | return FALSE; | |
252b5132 | 6296 | |
c044fabd | 6297 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6298 | num_sec = elf_numsections (abfd); |
6299 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6300 | { |
3e19fb8f L |
6301 | i_shdrp[count]->sh_name |
6302 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6303 | i_shdrp[count]->sh_name); | |
252b5132 RH |
6304 | if (bed->elf_backend_section_processing) |
6305 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
6306 | if (i_shdrp[count]->contents) | |
6307 | { | |
dc810e39 AM |
6308 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6309 | ||
252b5132 | 6310 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6311 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6312 | return FALSE; |
252b5132 RH |
6313 | } |
6314 | } | |
6315 | ||
6316 | /* Write out the section header names. */ | |
30e8ee25 | 6317 | t = elf_tdata (abfd); |
26ae6d5e | 6318 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6319 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6320 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6321 | return FALSE; |
252b5132 RH |
6322 | |
6323 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6324 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6325 | |
ff59fc36 RM |
6326 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6327 | return FALSE; | |
6328 | ||
6329 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6330 | if (t->o->build_id.after_write_object_contents != NULL) |
6331 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6332 | |
6333 | return TRUE; | |
252b5132 RH |
6334 | } |
6335 | ||
b34976b6 | 6336 | bfd_boolean |
217aa764 | 6337 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6338 | { |
c044fabd | 6339 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6340 | return _bfd_elf_write_object_contents (abfd); |
6341 | } | |
c044fabd KH |
6342 | |
6343 | /* Given a section, search the header to find them. */ | |
6344 | ||
cb33740c | 6345 | unsigned int |
198beae2 | 6346 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6347 | { |
9c5bfbb7 | 6348 | const struct elf_backend_data *bed; |
91d6fa6a | 6349 | unsigned int sec_index; |
252b5132 | 6350 | |
9ad5cbcf AM |
6351 | if (elf_section_data (asect) != NULL |
6352 | && elf_section_data (asect)->this_idx != 0) | |
6353 | return elf_section_data (asect)->this_idx; | |
6354 | ||
6355 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6356 | sec_index = SHN_ABS; |
af746e92 | 6357 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6358 | sec_index = SHN_COMMON; |
af746e92 | 6359 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6360 | sec_index = SHN_UNDEF; |
af746e92 | 6361 | else |
91d6fa6a | 6362 | sec_index = SHN_BAD; |
252b5132 | 6363 | |
af746e92 | 6364 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6365 | if (bed->elf_backend_section_from_bfd_section) |
6366 | { | |
91d6fa6a | 6367 | int retval = sec_index; |
9ad5cbcf | 6368 | |
af746e92 AM |
6369 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6370 | return retval; | |
252b5132 RH |
6371 | } |
6372 | ||
91d6fa6a | 6373 | if (sec_index == SHN_BAD) |
af746e92 | 6374 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6375 | |
91d6fa6a | 6376 | return sec_index; |
252b5132 RH |
6377 | } |
6378 | ||
6379 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6380 | on error. */ | |
6381 | ||
6382 | int | |
217aa764 | 6383 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6384 | { |
6385 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6386 | int idx; | |
6387 | flagword flags = asym_ptr->flags; | |
6388 | ||
6389 | /* When gas creates relocations against local labels, it creates its | |
6390 | own symbol for the section, but does put the symbol into the | |
6391 | symbol chain, so udata is 0. When the linker is generating | |
6392 | relocatable output, this section symbol may be for one of the | |
6393 | input sections rather than the output section. */ | |
6394 | if (asym_ptr->udata.i == 0 | |
6395 | && (flags & BSF_SECTION_SYM) | |
6396 | && asym_ptr->section) | |
6397 | { | |
5372391b | 6398 | asection *sec; |
252b5132 RH |
6399 | int indx; |
6400 | ||
5372391b AM |
6401 | sec = asym_ptr->section; |
6402 | if (sec->owner != abfd && sec->output_section != NULL) | |
6403 | sec = sec->output_section; | |
6404 | if (sec->owner == abfd | |
6405 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6406 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6407 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6408 | } | |
6409 | ||
6410 | idx = asym_ptr->udata.i; | |
6411 | ||
6412 | if (idx == 0) | |
6413 | { | |
6414 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6415 | which is used in a relocation entry. */ |
4eca0228 | 6416 | _bfd_error_handler |
695344c0 | 6417 | /* xgettext:c-format */ |
d003868e AM |
6418 | (_("%B: symbol `%s' required but not present"), |
6419 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
6420 | bfd_set_error (bfd_error_no_symbols); |
6421 | return -1; | |
6422 | } | |
6423 | ||
6424 | #if DEBUG & 4 | |
6425 | { | |
6426 | fprintf (stderr, | |
cd9af601 AM |
6427 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6428 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6429 | fflush (stderr); |
6430 | } | |
6431 | #endif | |
6432 | ||
6433 | return idx; | |
6434 | } | |
6435 | ||
84d1d650 | 6436 | /* Rewrite program header information. */ |
252b5132 | 6437 | |
b34976b6 | 6438 | static bfd_boolean |
84d1d650 | 6439 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6440 | { |
b34976b6 AM |
6441 | Elf_Internal_Ehdr *iehdr; |
6442 | struct elf_segment_map *map; | |
6443 | struct elf_segment_map *map_first; | |
6444 | struct elf_segment_map **pointer_to_map; | |
6445 | Elf_Internal_Phdr *segment; | |
6446 | asection *section; | |
6447 | unsigned int i; | |
6448 | unsigned int num_segments; | |
6449 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6450 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6451 | bfd_vma maxpagesize; |
6452 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6453 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6454 | const struct elf_backend_data *bed; |
bc67d8a6 | 6455 | |
caf47ea6 | 6456 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6457 | iehdr = elf_elfheader (ibfd); |
6458 | ||
bc67d8a6 | 6459 | map_first = NULL; |
c044fabd | 6460 | pointer_to_map = &map_first; |
252b5132 RH |
6461 | |
6462 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6463 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6464 | ||
6465 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6466 | #define SEGMENT_END(segment, start) \ |
6467 | (start + (segment->p_memsz > segment->p_filesz \ | |
6468 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6469 | |
eecdbe52 JJ |
6470 | #define SECTION_SIZE(section, segment) \ |
6471 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6472 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6473 | ? section->size : 0) |
eecdbe52 | 6474 | |
b34976b6 | 6475 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6476 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6477 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6478 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6479 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6480 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6481 | |
b34976b6 | 6482 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6483 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6484 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6485 | (section->lma >= base \ | |
eecdbe52 | 6486 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6487 | <= SEGMENT_END (segment, base))) |
252b5132 | 6488 | |
0efc80c8 L |
6489 | /* Handle PT_NOTE segment. */ |
6490 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6491 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6492 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6493 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6494 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6495 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6496 | |
0efc80c8 L |
6497 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6498 | etc. */ | |
6499 | #define IS_COREFILE_NOTE(p, s) \ | |
6500 | (IS_NOTE (p, s) \ | |
6501 | && bfd_get_format (ibfd) == bfd_core \ | |
6502 | && s->vma == 0 \ | |
6503 | && s->lma == 0) | |
6504 | ||
252b5132 RH |
6505 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6506 | linker, which generates a PT_INTERP section with p_vaddr and | |
6507 | p_memsz set to 0. */ | |
aecc8f8a AM |
6508 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6509 | (p->p_vaddr == 0 \ | |
6510 | && p->p_paddr == 0 \ | |
6511 | && p->p_memsz == 0 \ | |
6512 | && p->p_filesz > 0 \ | |
6513 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6514 | && s->size > 0 \ |
aecc8f8a | 6515 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6516 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6517 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6518 | |
bc67d8a6 NC |
6519 | /* Decide if the given section should be included in the given segment. |
6520 | A section will be included if: | |
f5ffc919 | 6521 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6522 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6523 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6524 | segment. |
bc67d8a6 | 6525 | 3. There is an output section associated with it, |
eecdbe52 | 6526 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6527 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6528 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6529 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6530 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6531 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6532 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6533 | ((((segment->p_paddr \ |
6534 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6535 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6536 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6537 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6538 | && segment->p_type != PT_GNU_STACK \ |
6539 | && (segment->p_type != PT_TLS \ | |
6540 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6541 | && (segment->p_type == PT_LOAD \ | |
6542 | || segment->p_type == PT_TLS \ | |
6543 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6544 | && (segment->p_type != PT_DYNAMIC \ | |
6545 | || SECTION_SIZE (section, segment) > 0 \ | |
6546 | || (segment->p_paddr \ | |
6547 | ? segment->p_paddr != section->lma \ | |
6548 | : segment->p_vaddr != section->vma) \ | |
6549 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6550 | == 0)) \ | |
0067a569 | 6551 | && !section->segment_mark) |
bc67d8a6 | 6552 | |
9f17e2a6 L |
6553 | /* If the output section of a section in the input segment is NULL, |
6554 | it is removed from the corresponding output segment. */ | |
6555 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6556 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6557 | && section->output_section != NULL) | |
6558 | ||
b34976b6 | 6559 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6560 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6561 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6562 | ||
6563 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6564 | their VMA address ranges and their LMA address ranges overlap. | |
6565 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6566 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6567 | to the same VMA range, but with the .data section mapped to a different | |
6568 | LMA. */ | |
aecc8f8a | 6569 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6570 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6571 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6572 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6573 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6574 | |
6575 | /* Initialise the segment mark field. */ | |
6576 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6577 | section->segment_mark = FALSE; |
bc67d8a6 | 6578 | |
5c44b38e AM |
6579 | /* The Solaris linker creates program headers in which all the |
6580 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6581 | file, we get confused. Check for this case, and if we find it | |
6582 | don't set the p_paddr_valid fields. */ | |
6583 | p_paddr_valid = FALSE; | |
6584 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6585 | i < num_segments; | |
6586 | i++, segment++) | |
6587 | if (segment->p_paddr != 0) | |
6588 | { | |
6589 | p_paddr_valid = TRUE; | |
6590 | break; | |
6591 | } | |
6592 | ||
252b5132 | 6593 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6594 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6595 | in the loadable segments. These can be created by weird |
aecc8f8a | 6596 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6597 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6598 | i < num_segments; | |
c044fabd | 6599 | i++, segment++) |
252b5132 | 6600 | { |
252b5132 | 6601 | unsigned int j; |
c044fabd | 6602 | Elf_Internal_Phdr *segment2; |
252b5132 | 6603 | |
aecc8f8a AM |
6604 | if (segment->p_type == PT_INTERP) |
6605 | for (section = ibfd->sections; section; section = section->next) | |
6606 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6607 | { | |
6608 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6609 | assignment code will work. */ |
aecc8f8a AM |
6610 | segment->p_vaddr = section->vma; |
6611 | break; | |
6612 | } | |
6613 | ||
bc67d8a6 | 6614 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6615 | { |
6616 | /* Remove PT_GNU_RELRO segment. */ | |
6617 | if (segment->p_type == PT_GNU_RELRO) | |
6618 | segment->p_type = PT_NULL; | |
6619 | continue; | |
6620 | } | |
c044fabd | 6621 | |
bc67d8a6 | 6622 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6623 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6624 | { |
6625 | bfd_signed_vma extra_length; | |
c044fabd | 6626 | |
bc67d8a6 | 6627 | if (segment2->p_type != PT_LOAD |
0067a569 | 6628 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6629 | continue; |
c044fabd | 6630 | |
bc67d8a6 NC |
6631 | /* Merge the two segments together. */ |
6632 | if (segment2->p_vaddr < segment->p_vaddr) | |
6633 | { | |
c044fabd | 6634 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6635 | SEGMENT. */ |
0067a569 AM |
6636 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6637 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6638 | |
bc67d8a6 NC |
6639 | if (extra_length > 0) |
6640 | { | |
0067a569 | 6641 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6642 | segment2->p_filesz += extra_length; |
6643 | } | |
c044fabd | 6644 | |
bc67d8a6 | 6645 | segment->p_type = PT_NULL; |
c044fabd | 6646 | |
bc67d8a6 NC |
6647 | /* Since we have deleted P we must restart the outer loop. */ |
6648 | i = 0; | |
6649 | segment = elf_tdata (ibfd)->phdr; | |
6650 | break; | |
6651 | } | |
6652 | else | |
6653 | { | |
c044fabd | 6654 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6655 | SEGMENT2. */ |
0067a569 AM |
6656 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6657 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6658 | |
bc67d8a6 NC |
6659 | if (extra_length > 0) |
6660 | { | |
0067a569 | 6661 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6662 | segment->p_filesz += extra_length; |
6663 | } | |
c044fabd | 6664 | |
bc67d8a6 NC |
6665 | segment2->p_type = PT_NULL; |
6666 | } | |
6667 | } | |
6668 | } | |
c044fabd | 6669 | |
bc67d8a6 NC |
6670 | /* The second scan attempts to assign sections to segments. */ |
6671 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6672 | i < num_segments; | |
0067a569 | 6673 | i++, segment++) |
bc67d8a6 | 6674 | { |
0067a569 AM |
6675 | unsigned int section_count; |
6676 | asection **sections; | |
6677 | asection *output_section; | |
6678 | unsigned int isec; | |
6679 | bfd_vma matching_lma; | |
6680 | bfd_vma suggested_lma; | |
6681 | unsigned int j; | |
dc810e39 | 6682 | bfd_size_type amt; |
0067a569 AM |
6683 | asection *first_section; |
6684 | bfd_boolean first_matching_lma; | |
6685 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6686 | |
6687 | if (segment->p_type == PT_NULL) | |
6688 | continue; | |
c044fabd | 6689 | |
9f17e2a6 | 6690 | first_section = NULL; |
bc67d8a6 | 6691 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6692 | for (section = ibfd->sections, section_count = 0; |
6693 | section != NULL; | |
6694 | section = section->next) | |
9f17e2a6 L |
6695 | { |
6696 | /* Find the first section in the input segment, which may be | |
6697 | removed from the corresponding output segment. */ | |
6698 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6699 | { | |
6700 | if (first_section == NULL) | |
6701 | first_section = section; | |
6702 | if (section->output_section != NULL) | |
6703 | ++section_count; | |
6704 | } | |
6705 | } | |
811072d8 | 6706 | |
b5f852ea NC |
6707 | /* Allocate a segment map big enough to contain |
6708 | all of the sections we have selected. */ | |
dc810e39 AM |
6709 | amt = sizeof (struct elf_segment_map); |
6710 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6711 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6712 | if (map == NULL) |
b34976b6 | 6713 | return FALSE; |
252b5132 RH |
6714 | |
6715 | /* Initialise the fields of the segment map. Default to | |
6716 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6717 | map->next = NULL; |
6718 | map->p_type = segment->p_type; | |
6719 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6720 | map->p_flags_valid = 1; |
55d55ac7 | 6721 | |
9f17e2a6 L |
6722 | /* If the first section in the input segment is removed, there is |
6723 | no need to preserve segment physical address in the corresponding | |
6724 | output segment. */ | |
945c025a | 6725 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6726 | { |
6727 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6728 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6729 | } |
252b5132 RH |
6730 | |
6731 | /* Determine if this segment contains the ELF file header | |
6732 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6733 | map->includes_filehdr = (segment->p_offset == 0 |
6734 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6735 | map->includes_phdrs = 0; |
252b5132 | 6736 | |
0067a569 | 6737 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6738 | { |
bc67d8a6 NC |
6739 | map->includes_phdrs = |
6740 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6741 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6742 | >= ((bfd_vma) iehdr->e_phoff |
6743 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6744 | |
bc67d8a6 | 6745 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6746 | phdr_included = TRUE; |
252b5132 RH |
6747 | } |
6748 | ||
bc67d8a6 | 6749 | if (section_count == 0) |
252b5132 RH |
6750 | { |
6751 | /* Special segments, such as the PT_PHDR segment, may contain | |
6752 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 6753 | something. They are allowed by the ELF spec however, so only |
f98450c6 NC |
6754 | a warning is produced. |
6755 | There is however the valid use case of embedded systems which | |
6756 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
6757 | flash memory with zeros. No warning is shown for that case. */ | |
6758 | if (segment->p_type == PT_LOAD | |
6759 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
6760 | /* xgettext:c-format */ | |
76cfced5 AM |
6761 | _bfd_error_handler (_("%B: warning: Empty loadable segment detected" |
6762 | " at vaddr=%#Lx, is this intentional?"), | |
f98450c6 | 6763 | ibfd, segment->p_vaddr); |
252b5132 | 6764 | |
bc67d8a6 | 6765 | map->count = 0; |
c044fabd KH |
6766 | *pointer_to_map = map; |
6767 | pointer_to_map = &map->next; | |
252b5132 RH |
6768 | |
6769 | continue; | |
6770 | } | |
6771 | ||
6772 | /* Now scan the sections in the input BFD again and attempt | |
6773 | to add their corresponding output sections to the segment map. | |
6774 | The problem here is how to handle an output section which has | |
6775 | been moved (ie had its LMA changed). There are four possibilities: | |
6776 | ||
6777 | 1. None of the sections have been moved. | |
6778 | In this case we can continue to use the segment LMA from the | |
6779 | input BFD. | |
6780 | ||
6781 | 2. All of the sections have been moved by the same amount. | |
6782 | In this case we can change the segment's LMA to match the LMA | |
6783 | of the first section. | |
6784 | ||
6785 | 3. Some of the sections have been moved, others have not. | |
6786 | In this case those sections which have not been moved can be | |
6787 | placed in the current segment which will have to have its size, | |
6788 | and possibly its LMA changed, and a new segment or segments will | |
6789 | have to be created to contain the other sections. | |
6790 | ||
b5f852ea | 6791 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6792 | In this case we can change the segment's LMA to match the LMA |
6793 | of the first section and we will have to create a new segment | |
6794 | or segments to contain the other sections. | |
6795 | ||
6796 | In order to save time, we allocate an array to hold the section | |
6797 | pointers that we are interested in. As these sections get assigned | |
6798 | to a segment, they are removed from this array. */ | |
6799 | ||
a50b1753 | 6800 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6801 | if (sections == NULL) |
b34976b6 | 6802 | return FALSE; |
252b5132 RH |
6803 | |
6804 | /* Step One: Scan for segment vs section LMA conflicts. | |
6805 | Also add the sections to the section array allocated above. | |
6806 | Also add the sections to the current segment. In the common | |
6807 | case, where the sections have not been moved, this means that | |
6808 | we have completely filled the segment, and there is nothing | |
6809 | more to do. */ | |
252b5132 | 6810 | isec = 0; |
72730e0c | 6811 | matching_lma = 0; |
252b5132 | 6812 | suggested_lma = 0; |
0067a569 AM |
6813 | first_matching_lma = TRUE; |
6814 | first_suggested_lma = TRUE; | |
252b5132 | 6815 | |
461c4b2e | 6816 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6817 | section != NULL; |
6818 | section = section->next) | |
252b5132 | 6819 | { |
caf47ea6 | 6820 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6821 | { |
bc67d8a6 NC |
6822 | output_section = section->output_section; |
6823 | ||
0067a569 | 6824 | sections[j++] = section; |
252b5132 RH |
6825 | |
6826 | /* The Solaris native linker always sets p_paddr to 0. | |
6827 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6828 | correct value. Note - some backends require that |
6829 | p_paddr be left as zero. */ | |
5c44b38e | 6830 | if (!p_paddr_valid |
4455705d | 6831 | && segment->p_vaddr != 0 |
0067a569 | 6832 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6833 | && isec == 0 |
bc67d8a6 | 6834 | && output_section->lma != 0 |
0067a569 AM |
6835 | && output_section->vma == (segment->p_vaddr |
6836 | + (map->includes_filehdr | |
6837 | ? iehdr->e_ehsize | |
6838 | : 0) | |
6839 | + (map->includes_phdrs | |
6840 | ? (iehdr->e_phnum | |
6841 | * iehdr->e_phentsize) | |
6842 | : 0))) | |
bc67d8a6 | 6843 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6844 | |
6845 | /* Match up the physical address of the segment with the | |
6846 | LMA address of the output section. */ | |
bc67d8a6 | 6847 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6848 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6849 | || (bed->want_p_paddr_set_to_zero |
6850 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6851 | { |
0067a569 AM |
6852 | if (first_matching_lma || output_section->lma < matching_lma) |
6853 | { | |
6854 | matching_lma = output_section->lma; | |
6855 | first_matching_lma = FALSE; | |
6856 | } | |
252b5132 RH |
6857 | |
6858 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6859 | then it does not overlap any other section within that |
252b5132 | 6860 | segment. */ |
0067a569 AM |
6861 | map->sections[isec++] = output_section; |
6862 | } | |
6863 | else if (first_suggested_lma) | |
6864 | { | |
6865 | suggested_lma = output_section->lma; | |
6866 | first_suggested_lma = FALSE; | |
252b5132 | 6867 | } |
147d51c2 L |
6868 | |
6869 | if (j == section_count) | |
6870 | break; | |
252b5132 RH |
6871 | } |
6872 | } | |
6873 | ||
bc67d8a6 | 6874 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6875 | |
6876 | /* Step Two: Adjust the physical address of the current segment, | |
6877 | if necessary. */ | |
bc67d8a6 | 6878 | if (isec == section_count) |
252b5132 RH |
6879 | { |
6880 | /* All of the sections fitted within the segment as currently | |
6881 | specified. This is the default case. Add the segment to | |
6882 | the list of built segments and carry on to process the next | |
6883 | program header in the input BFD. */ | |
bc67d8a6 | 6884 | map->count = section_count; |
c044fabd KH |
6885 | *pointer_to_map = map; |
6886 | pointer_to_map = &map->next; | |
08a40648 | 6887 | |
5c44b38e AM |
6888 | if (p_paddr_valid |
6889 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6890 | && matching_lma != map->p_paddr |
5c44b38e AM |
6891 | && !map->includes_filehdr |
6892 | && !map->includes_phdrs) | |
3271a814 NS |
6893 | /* There is some padding before the first section in the |
6894 | segment. So, we must account for that in the output | |
6895 | segment's vma. */ | |
6896 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6897 | |
252b5132 RH |
6898 | free (sections); |
6899 | continue; | |
6900 | } | |
252b5132 RH |
6901 | else |
6902 | { | |
0067a569 | 6903 | if (!first_matching_lma) |
72730e0c AM |
6904 | { |
6905 | /* At least one section fits inside the current segment. | |
6906 | Keep it, but modify its physical address to match the | |
6907 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6908 | map->p_paddr = matching_lma; |
72730e0c AM |
6909 | } |
6910 | else | |
6911 | { | |
6912 | /* None of the sections fitted inside the current segment. | |
6913 | Change the current segment's physical address to match | |
6914 | the LMA of the first section. */ | |
bc67d8a6 | 6915 | map->p_paddr = suggested_lma; |
72730e0c AM |
6916 | } |
6917 | ||
bc67d8a6 NC |
6918 | /* Offset the segment physical address from the lma |
6919 | to allow for space taken up by elf headers. */ | |
6920 | if (map->includes_filehdr) | |
010c8431 AM |
6921 | { |
6922 | if (map->p_paddr >= iehdr->e_ehsize) | |
6923 | map->p_paddr -= iehdr->e_ehsize; | |
6924 | else | |
6925 | { | |
6926 | map->includes_filehdr = FALSE; | |
6927 | map->includes_phdrs = FALSE; | |
6928 | } | |
6929 | } | |
252b5132 | 6930 | |
bc67d8a6 NC |
6931 | if (map->includes_phdrs) |
6932 | { | |
010c8431 AM |
6933 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6934 | { | |
6935 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6936 | ||
6937 | /* iehdr->e_phnum is just an estimate of the number | |
6938 | of program headers that we will need. Make a note | |
6939 | here of the number we used and the segment we chose | |
6940 | to hold these headers, so that we can adjust the | |
6941 | offset when we know the correct value. */ | |
6942 | phdr_adjust_num = iehdr->e_phnum; | |
6943 | phdr_adjust_seg = map; | |
6944 | } | |
6945 | else | |
6946 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6947 | } |
252b5132 RH |
6948 | } |
6949 | ||
6950 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6951 | those that fit to the current segment and removing them from the |
252b5132 RH |
6952 | sections array; but making sure not to leave large gaps. Once all |
6953 | possible sections have been assigned to the current segment it is | |
6954 | added to the list of built segments and if sections still remain | |
6955 | to be assigned, a new segment is constructed before repeating | |
6956 | the loop. */ | |
6957 | isec = 0; | |
6958 | do | |
6959 | { | |
bc67d8a6 | 6960 | map->count = 0; |
252b5132 | 6961 | suggested_lma = 0; |
0067a569 | 6962 | first_suggested_lma = TRUE; |
252b5132 RH |
6963 | |
6964 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6965 | for (j = 0; j < section_count; j++) |
252b5132 | 6966 | { |
bc67d8a6 | 6967 | section = sections[j]; |
252b5132 | 6968 | |
bc67d8a6 | 6969 | if (section == NULL) |
252b5132 RH |
6970 | continue; |
6971 | ||
bc67d8a6 | 6972 | output_section = section->output_section; |
252b5132 | 6973 | |
bc67d8a6 | 6974 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6975 | |
bc67d8a6 NC |
6976 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6977 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6978 | { |
bc67d8a6 | 6979 | if (map->count == 0) |
252b5132 RH |
6980 | { |
6981 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6982 | the beginning of the segment, then something is |
6983 | wrong. */ | |
0067a569 AM |
6984 | if (output_section->lma |
6985 | != (map->p_paddr | |
6986 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
6987 | + (map->includes_phdrs | |
6988 | ? iehdr->e_phnum * iehdr->e_phentsize | |
6989 | : 0))) | |
252b5132 RH |
6990 | abort (); |
6991 | } | |
6992 | else | |
6993 | { | |
0067a569 | 6994 | asection *prev_sec; |
252b5132 | 6995 | |
bc67d8a6 | 6996 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
6997 | |
6998 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
6999 | and the start of this section is more than |
7000 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7001 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7002 | maxpagesize) |
caf47ea6 | 7003 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7004 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7005 | > output_section->lma)) |
252b5132 | 7006 | { |
0067a569 AM |
7007 | if (first_suggested_lma) |
7008 | { | |
7009 | suggested_lma = output_section->lma; | |
7010 | first_suggested_lma = FALSE; | |
7011 | } | |
252b5132 RH |
7012 | |
7013 | continue; | |
7014 | } | |
7015 | } | |
7016 | ||
bc67d8a6 | 7017 | map->sections[map->count++] = output_section; |
252b5132 RH |
7018 | ++isec; |
7019 | sections[j] = NULL; | |
b34976b6 | 7020 | section->segment_mark = TRUE; |
252b5132 | 7021 | } |
0067a569 AM |
7022 | else if (first_suggested_lma) |
7023 | { | |
7024 | suggested_lma = output_section->lma; | |
7025 | first_suggested_lma = FALSE; | |
7026 | } | |
252b5132 RH |
7027 | } |
7028 | ||
bc67d8a6 | 7029 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
7030 | |
7031 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7032 | *pointer_to_map = map; |
7033 | pointer_to_map = &map->next; | |
252b5132 | 7034 | |
bc67d8a6 | 7035 | if (isec < section_count) |
252b5132 RH |
7036 | { |
7037 | /* We still have not allocated all of the sections to | |
7038 | segments. Create a new segment here, initialise it | |
7039 | and carry on looping. */ | |
dc810e39 AM |
7040 | amt = sizeof (struct elf_segment_map); |
7041 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 7042 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7043 | if (map == NULL) |
5ed6aba4 NC |
7044 | { |
7045 | free (sections); | |
7046 | return FALSE; | |
7047 | } | |
252b5132 RH |
7048 | |
7049 | /* Initialise the fields of the segment map. Set the physical | |
7050 | physical address to the LMA of the first section that has | |
7051 | not yet been assigned. */ | |
0067a569 AM |
7052 | map->next = NULL; |
7053 | map->p_type = segment->p_type; | |
7054 | map->p_flags = segment->p_flags; | |
7055 | map->p_flags_valid = 1; | |
7056 | map->p_paddr = suggested_lma; | |
5c44b38e | 7057 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7058 | map->includes_filehdr = 0; |
0067a569 | 7059 | map->includes_phdrs = 0; |
252b5132 RH |
7060 | } |
7061 | } | |
bc67d8a6 | 7062 | while (isec < section_count); |
252b5132 RH |
7063 | |
7064 | free (sections); | |
7065 | } | |
7066 | ||
12bd6957 | 7067 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7068 | |
7069 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7070 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7071 | the offset if necessary. */ |
7072 | if (phdr_adjust_seg != NULL) | |
7073 | { | |
7074 | unsigned int count; | |
c044fabd | 7075 | |
bc67d8a6 | 7076 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7077 | count++; |
252b5132 | 7078 | |
bc67d8a6 NC |
7079 | if (count > phdr_adjust_num) |
7080 | phdr_adjust_seg->p_paddr | |
7081 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
7082 | } | |
c044fabd | 7083 | |
bc67d8a6 | 7084 | #undef SEGMENT_END |
eecdbe52 | 7085 | #undef SECTION_SIZE |
bc67d8a6 NC |
7086 | #undef IS_CONTAINED_BY_VMA |
7087 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7088 | #undef IS_NOTE |
252b5132 | 7089 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7090 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7091 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7092 | #undef INCLUDE_SECTION_IN_SEGMENT |
7093 | #undef SEGMENT_AFTER_SEGMENT | |
7094 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7095 | return TRUE; |
252b5132 RH |
7096 | } |
7097 | ||
84d1d650 L |
7098 | /* Copy ELF program header information. */ |
7099 | ||
7100 | static bfd_boolean | |
7101 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7102 | { | |
7103 | Elf_Internal_Ehdr *iehdr; | |
7104 | struct elf_segment_map *map; | |
7105 | struct elf_segment_map *map_first; | |
7106 | struct elf_segment_map **pointer_to_map; | |
7107 | Elf_Internal_Phdr *segment; | |
7108 | unsigned int i; | |
7109 | unsigned int num_segments; | |
7110 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7111 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7112 | |
7113 | iehdr = elf_elfheader (ibfd); | |
7114 | ||
7115 | map_first = NULL; | |
7116 | pointer_to_map = &map_first; | |
7117 | ||
88967714 AM |
7118 | /* If all the segment p_paddr fields are zero, don't set |
7119 | map->p_paddr_valid. */ | |
7120 | p_paddr_valid = FALSE; | |
84d1d650 | 7121 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7122 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7123 | i < num_segments; | |
7124 | i++, segment++) | |
7125 | if (segment->p_paddr != 0) | |
7126 | { | |
7127 | p_paddr_valid = TRUE; | |
7128 | break; | |
7129 | } | |
7130 | ||
84d1d650 L |
7131 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7132 | i < num_segments; | |
7133 | i++, segment++) | |
7134 | { | |
7135 | asection *section; | |
7136 | unsigned int section_count; | |
7137 | bfd_size_type amt; | |
7138 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7139 | asection *first_section = NULL; |
a76e6f2f | 7140 | asection *lowest_section; |
84d1d650 | 7141 | |
84d1d650 L |
7142 | /* Compute how many sections are in this segment. */ |
7143 | for (section = ibfd->sections, section_count = 0; | |
7144 | section != NULL; | |
7145 | section = section->next) | |
7146 | { | |
7147 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7148 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7149 | { |
a76e6f2f AM |
7150 | if (first_section == NULL) |
7151 | first_section = section; | |
3271a814 NS |
7152 | section_count++; |
7153 | } | |
84d1d650 L |
7154 | } |
7155 | ||
7156 | /* Allocate a segment map big enough to contain | |
7157 | all of the sections we have selected. */ | |
7158 | amt = sizeof (struct elf_segment_map); | |
7159 | if (section_count != 0) | |
7160 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 7161 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7162 | if (map == NULL) |
7163 | return FALSE; | |
7164 | ||
7165 | /* Initialize the fields of the output segment map with the | |
7166 | input segment. */ | |
7167 | map->next = NULL; | |
7168 | map->p_type = segment->p_type; | |
7169 | map->p_flags = segment->p_flags; | |
7170 | map->p_flags_valid = 1; | |
7171 | map->p_paddr = segment->p_paddr; | |
88967714 | 7172 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7173 | map->p_align = segment->p_align; |
7174 | map->p_align_valid = 1; | |
3271a814 | 7175 | map->p_vaddr_offset = 0; |
84d1d650 | 7176 | |
04c3a755 NS |
7177 | if (map->p_type == PT_GNU_RELRO |
7178 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7179 | { |
7180 | /* The PT_GNU_RELRO segment may contain the first a few | |
7181 | bytes in the .got.plt section even if the whole .got.plt | |
7182 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7183 | change the size of the PT_GNU_RELRO segment. |
7184 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7185 | systems. */ | |
9433b9b1 | 7186 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7187 | map->p_size_valid = 1; |
7188 | } | |
7189 | ||
84d1d650 L |
7190 | /* Determine if this segment contains the ELF file header |
7191 | and if it contains the program headers themselves. */ | |
7192 | map->includes_filehdr = (segment->p_offset == 0 | |
7193 | && segment->p_filesz >= iehdr->e_ehsize); | |
7194 | ||
7195 | map->includes_phdrs = 0; | |
7196 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7197 | { | |
7198 | map->includes_phdrs = | |
7199 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7200 | && (segment->p_offset + segment->p_filesz | |
7201 | >= ((bfd_vma) iehdr->e_phoff | |
7202 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7203 | ||
7204 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7205 | phdr_included = TRUE; | |
7206 | } | |
7207 | ||
bbefd0a9 | 7208 | lowest_section = NULL; |
84d1d650 L |
7209 | if (section_count != 0) |
7210 | { | |
7211 | unsigned int isec = 0; | |
7212 | ||
53020534 | 7213 | for (section = first_section; |
84d1d650 L |
7214 | section != NULL; |
7215 | section = section->next) | |
7216 | { | |
7217 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7218 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7219 | { |
7220 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7221 | if ((section->flags & SEC_ALLOC) != 0) |
7222 | { | |
7223 | bfd_vma seg_off; | |
7224 | ||
bbefd0a9 AM |
7225 | if (lowest_section == NULL |
7226 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7227 | lowest_section = section; |
7228 | ||
a76e6f2f AM |
7229 | /* Section lmas are set up from PT_LOAD header |
7230 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7231 | If this header has a p_paddr that disagrees | |
7232 | with the section lma, flag the p_paddr as | |
7233 | invalid. */ | |
7234 | if ((section->flags & SEC_LOAD) != 0) | |
7235 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7236 | else | |
7237 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7238 | if (section->lma - segment->p_paddr != seg_off) | |
7239 | map->p_paddr_valid = FALSE; | |
7240 | } | |
53020534 L |
7241 | if (isec == section_count) |
7242 | break; | |
7243 | } | |
84d1d650 L |
7244 | } |
7245 | } | |
7246 | ||
a76e6f2f AM |
7247 | if (map->includes_filehdr && lowest_section != NULL) |
7248 | /* We need to keep the space used by the headers fixed. */ | |
7249 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 7250 | |
a76e6f2f AM |
7251 | if (!map->includes_phdrs |
7252 | && !map->includes_filehdr | |
7253 | && map->p_paddr_valid) | |
7254 | /* There is some other padding before the first section. */ | |
7255 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
7256 | - segment->p_paddr); | |
7257 | ||
84d1d650 L |
7258 | map->count = section_count; |
7259 | *pointer_to_map = map; | |
7260 | pointer_to_map = &map->next; | |
7261 | } | |
7262 | ||
12bd6957 | 7263 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7264 | return TRUE; |
7265 | } | |
7266 | ||
7267 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7268 | information. */ | |
7269 | ||
7270 | static bfd_boolean | |
7271 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7272 | { | |
84d1d650 L |
7273 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7274 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7275 | return TRUE; | |
7276 | ||
7277 | if (elf_tdata (ibfd)->phdr == NULL) | |
7278 | return TRUE; | |
7279 | ||
7280 | if (ibfd->xvec == obfd->xvec) | |
7281 | { | |
cb3ff1e5 NC |
7282 | /* Check to see if any sections in the input BFD |
7283 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7284 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7285 | asection *section, *osec; |
7286 | unsigned int i, num_segments; | |
7287 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7288 | const struct elf_backend_data *bed; |
7289 | ||
7290 | bed = get_elf_backend_data (ibfd); | |
7291 | ||
7292 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7293 | if (bed->want_p_paddr_set_to_zero) | |
7294 | goto rewrite; | |
84d1d650 L |
7295 | |
7296 | /* Initialize the segment mark field. */ | |
7297 | for (section = obfd->sections; section != NULL; | |
7298 | section = section->next) | |
7299 | section->segment_mark = FALSE; | |
7300 | ||
7301 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7302 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7303 | i < num_segments; | |
7304 | i++, segment++) | |
7305 | { | |
5f6999aa NC |
7306 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7307 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7308 | which severly confuses things, so always regenerate the segment | |
7309 | map in this case. */ | |
7310 | if (segment->p_paddr == 0 | |
7311 | && segment->p_memsz == 0 | |
7312 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7313 | goto rewrite; |
5f6999aa | 7314 | |
84d1d650 L |
7315 | for (section = ibfd->sections; |
7316 | section != NULL; section = section->next) | |
7317 | { | |
7318 | /* We mark the output section so that we know it comes | |
7319 | from the input BFD. */ | |
7320 | osec = section->output_section; | |
7321 | if (osec) | |
7322 | osec->segment_mark = TRUE; | |
7323 | ||
7324 | /* Check if this section is covered by the segment. */ | |
7325 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7326 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7327 | { |
7328 | /* FIXME: Check if its output section is changed or | |
7329 | removed. What else do we need to check? */ | |
7330 | if (osec == NULL | |
7331 | || section->flags != osec->flags | |
7332 | || section->lma != osec->lma | |
7333 | || section->vma != osec->vma | |
7334 | || section->size != osec->size | |
7335 | || section->rawsize != osec->rawsize | |
7336 | || section->alignment_power != osec->alignment_power) | |
7337 | goto rewrite; | |
7338 | } | |
7339 | } | |
7340 | } | |
7341 | ||
cb3ff1e5 | 7342 | /* Check to see if any output section do not come from the |
84d1d650 L |
7343 | input BFD. */ |
7344 | for (section = obfd->sections; section != NULL; | |
7345 | section = section->next) | |
7346 | { | |
535b785f | 7347 | if (!section->segment_mark) |
84d1d650 L |
7348 | goto rewrite; |
7349 | else | |
7350 | section->segment_mark = FALSE; | |
7351 | } | |
7352 | ||
7353 | return copy_elf_program_header (ibfd, obfd); | |
7354 | } | |
7355 | ||
7356 | rewrite: | |
f1d85785 L |
7357 | if (ibfd->xvec == obfd->xvec) |
7358 | { | |
7359 | /* When rewriting program header, set the output maxpagesize to | |
7360 | the maximum alignment of input PT_LOAD segments. */ | |
7361 | Elf_Internal_Phdr *segment; | |
7362 | unsigned int i; | |
7363 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7364 | bfd_vma maxpagesize = 0; | |
7365 | ||
7366 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7367 | i < num_segments; | |
7368 | i++, segment++) | |
7369 | if (segment->p_type == PT_LOAD | |
7370 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7371 | { |
7372 | /* PR 17512: file: f17299af. */ | |
7373 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7374 | /* xgettext:c-format */ |
76cfced5 AM |
7375 | _bfd_error_handler (_("%B: warning: segment alignment of %#Lx" |
7376 | " is too large"), | |
7377 | ibfd, segment->p_align); | |
c86934ce NC |
7378 | else |
7379 | maxpagesize = segment->p_align; | |
7380 | } | |
f1d85785 L |
7381 | |
7382 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7383 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7384 | } | |
7385 | ||
84d1d650 L |
7386 | return rewrite_elf_program_header (ibfd, obfd); |
7387 | } | |
7388 | ||
ccd2ec6a L |
7389 | /* Initialize private output section information from input section. */ |
7390 | ||
7391 | bfd_boolean | |
7392 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7393 | asection *isec, | |
7394 | bfd *obfd, | |
7395 | asection *osec, | |
7396 | struct bfd_link_info *link_info) | |
7397 | ||
7398 | { | |
7399 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7400 | bfd_boolean final_link = (link_info != NULL |
7401 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7402 | |
7403 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7404 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7405 | return TRUE; | |
7406 | ||
ba85c43e NC |
7407 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7408 | ||
dfa7b0b8 AM |
7409 | /* For objcopy and relocatable link, don't copy the output ELF |
7410 | section type from input if the output BFD section flags have been | |
7411 | set to something different. For a final link allow some flags | |
7412 | that the linker clears to differ. */ | |
42bb2e33 | 7413 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7414 | && (osec->flags == isec->flags |
7415 | || (final_link | |
7416 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7417 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7418 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7419 | |
7420 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7421 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7422 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7423 | |
a91e1603 L |
7424 | /* Copy sh_info from input for mbind section. */ |
7425 | if (elf_section_flags (isec) & SHF_GNU_MBIND) | |
7426 | elf_section_data (osec)->this_hdr.sh_info | |
7427 | = elf_section_data (isec)->this_hdr.sh_info; | |
7428 | ||
ccd2ec6a L |
7429 | /* Set things up for objcopy and relocatable link. The output |
7430 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7431 | to the input group members. Ignore linker created group section. | |
7432 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7433 | if ((link_info == NULL |
7434 | || !link_info->resolve_section_groups) | |
7435 | && (elf_sec_group (isec) == NULL | |
7436 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7437 | { |
7bdf4127 AB |
7438 | if (elf_section_flags (isec) & SHF_GROUP) |
7439 | elf_section_flags (osec) |= SHF_GROUP; | |
7440 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7441 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7442 | } |
7443 | ||
7bdf4127 AB |
7444 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7445 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7446 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7447 | & SHF_COMPRESSED); | |
7448 | ||
ccd2ec6a L |
7449 | ihdr = &elf_section_data (isec)->this_hdr; |
7450 | ||
7451 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7452 | don't use the output section of the linked-to section since it | |
7453 | may be NULL at this point. */ | |
7454 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7455 | { | |
7456 | ohdr = &elf_section_data (osec)->this_hdr; | |
7457 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7458 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7459 | } | |
7460 | ||
7461 | osec->use_rela_p = isec->use_rela_p; | |
7462 | ||
7463 | return TRUE; | |
7464 | } | |
7465 | ||
252b5132 RH |
7466 | /* Copy private section information. This copies over the entsize |
7467 | field, and sometimes the info field. */ | |
7468 | ||
b34976b6 | 7469 | bfd_boolean |
217aa764 AM |
7470 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7471 | asection *isec, | |
7472 | bfd *obfd, | |
7473 | asection *osec) | |
252b5132 RH |
7474 | { |
7475 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7476 | ||
7477 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7478 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7479 | return TRUE; |
252b5132 | 7480 | |
252b5132 RH |
7481 | ihdr = &elf_section_data (isec)->this_hdr; |
7482 | ohdr = &elf_section_data (osec)->this_hdr; | |
7483 | ||
7484 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7485 | ||
7486 | if (ihdr->sh_type == SHT_SYMTAB | |
7487 | || ihdr->sh_type == SHT_DYNSYM | |
7488 | || ihdr->sh_type == SHT_GNU_verneed | |
7489 | || ihdr->sh_type == SHT_GNU_verdef) | |
7490 | ohdr->sh_info = ihdr->sh_info; | |
7491 | ||
ccd2ec6a L |
7492 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7493 | NULL); | |
252b5132 RH |
7494 | } |
7495 | ||
d0bf826b AM |
7496 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7497 | necessary if we are removing either the SHT_GROUP section or any of | |
7498 | the group member sections. DISCARDED is the value that a section's | |
7499 | output_section has if the section will be discarded, NULL when this | |
7500 | function is called from objcopy, bfd_abs_section_ptr when called | |
7501 | from the linker. */ | |
80fccad2 BW |
7502 | |
7503 | bfd_boolean | |
d0bf826b | 7504 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7505 | { |
30288845 AM |
7506 | asection *isec; |
7507 | ||
30288845 | 7508 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7509 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7510 | { |
7511 | asection *first = elf_next_in_group (isec); | |
7512 | asection *s = first; | |
d0bf826b AM |
7513 | bfd_size_type removed = 0; |
7514 | ||
30288845 AM |
7515 | while (s != NULL) |
7516 | { | |
415f38a6 AM |
7517 | /* If this member section is being output but the |
7518 | SHT_GROUP section is not, then clear the group info | |
7519 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7520 | if (s->output_section != discarded |
7521 | && isec->output_section == discarded) | |
30288845 AM |
7522 | { |
7523 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7524 | elf_group_name (s->output_section) = NULL; | |
7525 | } | |
415f38a6 AM |
7526 | /* Conversely, if the member section is not being output |
7527 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7528 | else if (s->output_section == discarded |
7529 | && isec->output_section != discarded) | |
7530 | removed += 4; | |
30288845 AM |
7531 | s = elf_next_in_group (s); |
7532 | if (s == first) | |
7533 | break; | |
7534 | } | |
d0bf826b AM |
7535 | if (removed != 0) |
7536 | { | |
7537 | if (discarded != NULL) | |
7538 | { | |
7539 | /* If we've been called for ld -r, then we need to | |
7540 | adjust the input section size. This function may | |
7541 | be called multiple times, so save the original | |
7542 | size. */ | |
7543 | if (isec->rawsize == 0) | |
7544 | isec->rawsize = isec->size; | |
7545 | isec->size = isec->rawsize - removed; | |
7546 | } | |
7547 | else | |
7548 | { | |
7549 | /* Adjust the output section size when called from | |
7550 | objcopy. */ | |
7551 | isec->output_section->size -= removed; | |
7552 | } | |
7553 | } | |
30288845 AM |
7554 | } |
7555 | ||
80fccad2 BW |
7556 | return TRUE; |
7557 | } | |
7558 | ||
d0bf826b AM |
7559 | /* Copy private header information. */ |
7560 | ||
7561 | bfd_boolean | |
7562 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7563 | { | |
7564 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7565 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7566 | return TRUE; | |
7567 | ||
7568 | /* Copy over private BFD data if it has not already been copied. | |
7569 | This must be done here, rather than in the copy_private_bfd_data | |
7570 | entry point, because the latter is called after the section | |
7571 | contents have been set, which means that the program headers have | |
7572 | already been worked out. */ | |
12bd6957 | 7573 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7574 | { |
7575 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7576 | return FALSE; | |
7577 | } | |
7578 | ||
7579 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7580 | } | |
7581 | ||
252b5132 RH |
7582 | /* Copy private symbol information. If this symbol is in a section |
7583 | which we did not map into a BFD section, try to map the section | |
7584 | index correctly. We use special macro definitions for the mapped | |
7585 | section indices; these definitions are interpreted by the | |
7586 | swap_out_syms function. */ | |
7587 | ||
9ad5cbcf AM |
7588 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7589 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7590 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7591 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7592 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7593 | |
b34976b6 | 7594 | bfd_boolean |
217aa764 AM |
7595 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7596 | asymbol *isymarg, | |
7597 | bfd *obfd, | |
7598 | asymbol *osymarg) | |
252b5132 RH |
7599 | { |
7600 | elf_symbol_type *isym, *osym; | |
7601 | ||
7602 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7603 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7604 | return TRUE; |
252b5132 RH |
7605 | |
7606 | isym = elf_symbol_from (ibfd, isymarg); | |
7607 | osym = elf_symbol_from (obfd, osymarg); | |
7608 | ||
7609 | if (isym != NULL | |
8424d8f5 | 7610 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7611 | && osym != NULL |
7612 | && bfd_is_abs_section (isym->symbol.section)) | |
7613 | { | |
7614 | unsigned int shndx; | |
7615 | ||
7616 | shndx = isym->internal_elf_sym.st_shndx; | |
7617 | if (shndx == elf_onesymtab (ibfd)) | |
7618 | shndx = MAP_ONESYMTAB; | |
7619 | else if (shndx == elf_dynsymtab (ibfd)) | |
7620 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7621 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7622 | shndx = MAP_STRTAB; |
12bd6957 | 7623 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7624 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7625 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7626 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7627 | osym->internal_elf_sym.st_shndx = shndx; |
7628 | } | |
7629 | ||
b34976b6 | 7630 | return TRUE; |
252b5132 RH |
7631 | } |
7632 | ||
7633 | /* Swap out the symbols. */ | |
7634 | ||
b34976b6 | 7635 | static bfd_boolean |
217aa764 | 7636 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7637 | struct elf_strtab_hash **sttp, |
217aa764 | 7638 | int relocatable_p) |
252b5132 | 7639 | { |
9c5bfbb7 | 7640 | const struct elf_backend_data *bed; |
079e9a2f AM |
7641 | int symcount; |
7642 | asymbol **syms; | |
ef10c3ac | 7643 | struct elf_strtab_hash *stt; |
079e9a2f | 7644 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7645 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7646 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7647 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7648 | bfd_byte *outbound_syms; |
7649 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7650 | unsigned long outbound_syms_index; |
7651 | unsigned long outbound_shndx_index; | |
079e9a2f | 7652 | int idx; |
12bd6957 | 7653 | unsigned int num_locals; |
079e9a2f | 7654 | bfd_size_type amt; |
174fd7f9 | 7655 | bfd_boolean name_local_sections; |
252b5132 | 7656 | |
12bd6957 | 7657 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7658 | return FALSE; |
252b5132 | 7659 | |
c044fabd | 7660 | /* Dump out the symtabs. */ |
ef10c3ac | 7661 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7662 | if (stt == NULL) |
b34976b6 | 7663 | return FALSE; |
252b5132 | 7664 | |
079e9a2f AM |
7665 | bed = get_elf_backend_data (abfd); |
7666 | symcount = bfd_get_symcount (abfd); | |
7667 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7668 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7669 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7670 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7671 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7672 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7673 | |
7674 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7675 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7676 | ||
ef10c3ac L |
7677 | /* Allocate buffer to swap out the .strtab section. */ |
7678 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7679 | * sizeof (*symstrtab)); | |
7680 | if (symstrtab == NULL) | |
7681 | { | |
7682 | _bfd_elf_strtab_free (stt); | |
7683 | return FALSE; | |
7684 | } | |
7685 | ||
a50b1753 NC |
7686 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7687 | bed->s->sizeof_sym); | |
079e9a2f | 7688 | if (outbound_syms == NULL) |
5ed6aba4 | 7689 | { |
ef10c3ac L |
7690 | error_return: |
7691 | _bfd_elf_strtab_free (stt); | |
7692 | free (symstrtab); | |
5ed6aba4 NC |
7693 | return FALSE; |
7694 | } | |
217aa764 | 7695 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7696 | outbound_syms_index = 0; |
252b5132 | 7697 | |
9ad5cbcf | 7698 | outbound_shndx = NULL; |
ef10c3ac | 7699 | outbound_shndx_index = 0; |
6a40cf0c NC |
7700 | |
7701 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7702 | { |
6a40cf0c NC |
7703 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7704 | if (symtab_shndx_hdr->sh_name != 0) | |
7705 | { | |
7706 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7707 | outbound_shndx = (bfd_byte *) | |
7708 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7709 | if (outbound_shndx == NULL) | |
7710 | goto error_return; | |
5ed6aba4 | 7711 | |
6a40cf0c NC |
7712 | symtab_shndx_hdr->contents = outbound_shndx; |
7713 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7714 | symtab_shndx_hdr->sh_size = amt; | |
7715 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7716 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7717 | } | |
7718 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7719 | } |
7720 | ||
589e6347 | 7721 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7722 | { |
7723 | /* Fill in zeroth symbol and swap it out. */ | |
7724 | Elf_Internal_Sym sym; | |
7725 | sym.st_name = 0; | |
7726 | sym.st_value = 0; | |
7727 | sym.st_size = 0; | |
7728 | sym.st_info = 0; | |
7729 | sym.st_other = 0; | |
7730 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7731 | sym.st_target_internal = 0; |
ef10c3ac L |
7732 | symstrtab[0].sym = sym; |
7733 | symstrtab[0].dest_index = outbound_syms_index; | |
7734 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7735 | outbound_syms_index++; | |
9ad5cbcf | 7736 | if (outbound_shndx != NULL) |
ef10c3ac | 7737 | outbound_shndx_index++; |
079e9a2f | 7738 | } |
252b5132 | 7739 | |
174fd7f9 RS |
7740 | name_local_sections |
7741 | = (bed->elf_backend_name_local_section_symbols | |
7742 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7743 | ||
079e9a2f | 7744 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7745 | for (idx = 0; idx < symcount;) |
252b5132 | 7746 | { |
252b5132 | 7747 | Elf_Internal_Sym sym; |
079e9a2f AM |
7748 | bfd_vma value = syms[idx]->value; |
7749 | elf_symbol_type *type_ptr; | |
7750 | flagword flags = syms[idx]->flags; | |
7751 | int type; | |
252b5132 | 7752 | |
174fd7f9 RS |
7753 | if (!name_local_sections |
7754 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7755 | { |
7756 | /* Local section symbols have no name. */ | |
ef10c3ac | 7757 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7758 | } |
7759 | else | |
7760 | { | |
ef10c3ac L |
7761 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7762 | to get the final offset for st_name. */ | |
7763 | sym.st_name | |
7764 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7765 | FALSE); | |
079e9a2f | 7766 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7767 | goto error_return; |
079e9a2f | 7768 | } |
252b5132 | 7769 | |
079e9a2f | 7770 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7771 | |
079e9a2f AM |
7772 | if ((flags & BSF_SECTION_SYM) == 0 |
7773 | && bfd_is_com_section (syms[idx]->section)) | |
7774 | { | |
7775 | /* ELF common symbols put the alignment into the `value' field, | |
7776 | and the size into the `size' field. This is backwards from | |
7777 | how BFD handles it, so reverse it here. */ | |
7778 | sym.st_size = value; | |
7779 | if (type_ptr == NULL | |
7780 | || type_ptr->internal_elf_sym.st_value == 0) | |
7781 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7782 | else | |
7783 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7784 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7785 | (abfd, syms[idx]->section); | |
7786 | } | |
7787 | else | |
7788 | { | |
7789 | asection *sec = syms[idx]->section; | |
cb33740c | 7790 | unsigned int shndx; |
252b5132 | 7791 | |
079e9a2f AM |
7792 | if (sec->output_section) |
7793 | { | |
7794 | value += sec->output_offset; | |
7795 | sec = sec->output_section; | |
7796 | } | |
589e6347 | 7797 | |
079e9a2f AM |
7798 | /* Don't add in the section vma for relocatable output. */ |
7799 | if (! relocatable_p) | |
7800 | value += sec->vma; | |
7801 | sym.st_value = value; | |
7802 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7803 | ||
7804 | if (bfd_is_abs_section (sec) | |
7805 | && type_ptr != NULL | |
7806 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7807 | { | |
7808 | /* This symbol is in a real ELF section which we did | |
7809 | not create as a BFD section. Undo the mapping done | |
7810 | by copy_private_symbol_data. */ | |
7811 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7812 | switch (shndx) | |
7813 | { | |
7814 | case MAP_ONESYMTAB: | |
7815 | shndx = elf_onesymtab (abfd); | |
7816 | break; | |
7817 | case MAP_DYNSYMTAB: | |
7818 | shndx = elf_dynsymtab (abfd); | |
7819 | break; | |
7820 | case MAP_STRTAB: | |
12bd6957 | 7821 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7822 | break; |
7823 | case MAP_SHSTRTAB: | |
12bd6957 | 7824 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7825 | break; |
9ad5cbcf | 7826 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7827 | if (elf_symtab_shndx_list (abfd)) |
7828 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7829 | break; |
079e9a2f | 7830 | default: |
15bc576a | 7831 | shndx = SHN_ABS; |
079e9a2f AM |
7832 | break; |
7833 | } | |
7834 | } | |
7835 | else | |
7836 | { | |
7837 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7838 | |
cb33740c | 7839 | if (shndx == SHN_BAD) |
079e9a2f AM |
7840 | { |
7841 | asection *sec2; | |
7842 | ||
7843 | /* Writing this would be a hell of a lot easier if | |
7844 | we had some decent documentation on bfd, and | |
7845 | knew what to expect of the library, and what to | |
7846 | demand of applications. For example, it | |
7847 | appears that `objcopy' might not set the | |
7848 | section of a symbol to be a section that is | |
7849 | actually in the output file. */ | |
7850 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
7851 | if (sec2 != NULL) |
7852 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
7853 | if (shndx == SHN_BAD) | |
589e6347 | 7854 | { |
695344c0 | 7855 | /* xgettext:c-format */ |
589e6347 NC |
7856 | _bfd_error_handler (_("\ |
7857 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7858 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7859 | sec->name); | |
811072d8 | 7860 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7861 | goto error_return; |
589e6347 | 7862 | } |
079e9a2f AM |
7863 | } |
7864 | } | |
252b5132 | 7865 | |
079e9a2f AM |
7866 | sym.st_shndx = shndx; |
7867 | } | |
252b5132 | 7868 | |
13ae64f3 JJ |
7869 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7870 | type = STT_TLS; | |
d8045f23 NC |
7871 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7872 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7873 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7874 | type = STT_FUNC; |
7875 | else if ((flags & BSF_OBJECT) != 0) | |
7876 | type = STT_OBJECT; | |
d9352518 DB |
7877 | else if ((flags & BSF_RELC) != 0) |
7878 | type = STT_RELC; | |
7879 | else if ((flags & BSF_SRELC) != 0) | |
7880 | type = STT_SRELC; | |
079e9a2f AM |
7881 | else |
7882 | type = STT_NOTYPE; | |
252b5132 | 7883 | |
13ae64f3 JJ |
7884 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7885 | type = STT_TLS; | |
7886 | ||
589e6347 | 7887 | /* Processor-specific types. */ |
079e9a2f AM |
7888 | if (type_ptr != NULL |
7889 | && bed->elf_backend_get_symbol_type) | |
7890 | type = ((*bed->elf_backend_get_symbol_type) | |
7891 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7892 | |
079e9a2f AM |
7893 | if (flags & BSF_SECTION_SYM) |
7894 | { | |
7895 | if (flags & BSF_GLOBAL) | |
7896 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7897 | else | |
7898 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7899 | } | |
7900 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7901 | { |
b8871f35 L |
7902 | if (type != STT_TLS) |
7903 | { | |
7904 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7905 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
7906 | ? STT_COMMON : STT_OBJECT); | |
7907 | else | |
7908 | type = ((flags & BSF_ELF_COMMON) != 0 | |
7909 | ? STT_COMMON : STT_OBJECT); | |
7910 | } | |
7911 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 7912 | } |
079e9a2f AM |
7913 | else if (bfd_is_und_section (syms[idx]->section)) |
7914 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7915 | ? STB_WEAK | |
7916 | : STB_GLOBAL), | |
7917 | type); | |
7918 | else if (flags & BSF_FILE) | |
7919 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7920 | else | |
7921 | { | |
7922 | int bind = STB_LOCAL; | |
252b5132 | 7923 | |
079e9a2f AM |
7924 | if (flags & BSF_LOCAL) |
7925 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7926 | else if (flags & BSF_GNU_UNIQUE) |
7927 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7928 | else if (flags & BSF_WEAK) |
7929 | bind = STB_WEAK; | |
7930 | else if (flags & BSF_GLOBAL) | |
7931 | bind = STB_GLOBAL; | |
252b5132 | 7932 | |
079e9a2f AM |
7933 | sym.st_info = ELF_ST_INFO (bind, type); |
7934 | } | |
252b5132 | 7935 | |
079e9a2f | 7936 | if (type_ptr != NULL) |
35fc36a8 RS |
7937 | { |
7938 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7939 | sym.st_target_internal | |
7940 | = type_ptr->internal_elf_sym.st_target_internal; | |
7941 | } | |
079e9a2f | 7942 | else |
35fc36a8 RS |
7943 | { |
7944 | sym.st_other = 0; | |
7945 | sym.st_target_internal = 0; | |
7946 | } | |
252b5132 | 7947 | |
ef10c3ac L |
7948 | idx++; |
7949 | symstrtab[idx].sym = sym; | |
7950 | symstrtab[idx].dest_index = outbound_syms_index; | |
7951 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7952 | ||
7953 | outbound_syms_index++; | |
9ad5cbcf | 7954 | if (outbound_shndx != NULL) |
ef10c3ac L |
7955 | outbound_shndx_index++; |
7956 | } | |
7957 | ||
7958 | /* Finalize the .strtab section. */ | |
7959 | _bfd_elf_strtab_finalize (stt); | |
7960 | ||
7961 | /* Swap out the .strtab section. */ | |
7962 | for (idx = 0; idx <= symcount; idx++) | |
7963 | { | |
7964 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7965 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7966 | elfsym->sym.st_name = 0; | |
7967 | else | |
7968 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7969 | elfsym->sym.st_name); | |
7970 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7971 | (outbound_syms | |
7972 | + (elfsym->dest_index | |
7973 | * bed->s->sizeof_sym)), | |
7974 | (outbound_shndx | |
7975 | + (elfsym->destshndx_index | |
7976 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7977 | } |
ef10c3ac | 7978 | free (symstrtab); |
252b5132 | 7979 | |
079e9a2f | 7980 | *sttp = stt; |
ef10c3ac | 7981 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7982 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 7983 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
7984 | symstrtab_hdr->sh_addr = 0; |
7985 | symstrtab_hdr->sh_entsize = 0; | |
7986 | symstrtab_hdr->sh_link = 0; | |
7987 | symstrtab_hdr->sh_info = 0; | |
7988 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 7989 | |
b34976b6 | 7990 | return TRUE; |
252b5132 RH |
7991 | } |
7992 | ||
7993 | /* Return the number of bytes required to hold the symtab vector. | |
7994 | ||
7995 | Note that we base it on the count plus 1, since we will null terminate | |
7996 | the vector allocated based on this size. However, the ELF symbol table | |
7997 | always has a dummy entry as symbol #0, so it ends up even. */ | |
7998 | ||
7999 | long | |
217aa764 | 8000 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8001 | { |
8002 | long symcount; | |
8003 | long symtab_size; | |
8004 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8005 | ||
8006 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8007 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8008 | if (symcount > 0) | |
8009 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8010 | |
8011 | return symtab_size; | |
8012 | } | |
8013 | ||
8014 | long | |
217aa764 | 8015 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8016 | { |
8017 | long symcount; | |
8018 | long symtab_size; | |
8019 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8020 | ||
8021 | if (elf_dynsymtab (abfd) == 0) | |
8022 | { | |
8023 | bfd_set_error (bfd_error_invalid_operation); | |
8024 | return -1; | |
8025 | } | |
8026 | ||
8027 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8028 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8029 | if (symcount > 0) | |
8030 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8031 | |
8032 | return symtab_size; | |
8033 | } | |
8034 | ||
8035 | long | |
217aa764 AM |
8036 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8037 | sec_ptr asect) | |
252b5132 RH |
8038 | { |
8039 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
8040 | } | |
8041 | ||
8042 | /* Canonicalize the relocs. */ | |
8043 | ||
8044 | long | |
217aa764 AM |
8045 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8046 | sec_ptr section, | |
8047 | arelent **relptr, | |
8048 | asymbol **symbols) | |
252b5132 RH |
8049 | { |
8050 | arelent *tblptr; | |
8051 | unsigned int i; | |
9c5bfbb7 | 8052 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8053 | |
b34976b6 | 8054 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8055 | return -1; |
8056 | ||
8057 | tblptr = section->relocation; | |
8058 | for (i = 0; i < section->reloc_count; i++) | |
8059 | *relptr++ = tblptr++; | |
8060 | ||
8061 | *relptr = NULL; | |
8062 | ||
8063 | return section->reloc_count; | |
8064 | } | |
8065 | ||
8066 | long | |
6cee3f79 | 8067 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8068 | { |
9c5bfbb7 | 8069 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8070 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8071 | |
8072 | if (symcount >= 0) | |
8073 | bfd_get_symcount (abfd) = symcount; | |
8074 | return symcount; | |
8075 | } | |
8076 | ||
8077 | long | |
217aa764 AM |
8078 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8079 | asymbol **allocation) | |
252b5132 | 8080 | { |
9c5bfbb7 | 8081 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8082 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8083 | |
8084 | if (symcount >= 0) | |
8085 | bfd_get_dynamic_symcount (abfd) = symcount; | |
8086 | return symcount; | |
252b5132 RH |
8087 | } |
8088 | ||
8615f3f2 AM |
8089 | /* Return the size required for the dynamic reloc entries. Any loadable |
8090 | section that was actually installed in the BFD, and has type SHT_REL | |
8091 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8092 | dynamic reloc section. */ | |
252b5132 RH |
8093 | |
8094 | long | |
217aa764 | 8095 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
8096 | { |
8097 | long ret; | |
8098 | asection *s; | |
8099 | ||
8100 | if (elf_dynsymtab (abfd) == 0) | |
8101 | { | |
8102 | bfd_set_error (bfd_error_invalid_operation); | |
8103 | return -1; | |
8104 | } | |
8105 | ||
8106 | ret = sizeof (arelent *); | |
8107 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 8108 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8109 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8110 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 8111 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
8112 | * sizeof (arelent *)); |
8113 | ||
8114 | return ret; | |
8115 | } | |
8116 | ||
8615f3f2 AM |
8117 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8118 | dynamic relocations as a single block, although they are actually | |
8119 | associated with particular sections; the interface, which was | |
8120 | designed for SunOS style shared libraries, expects that there is only | |
8121 | one set of dynamic relocs. Any loadable section that was actually | |
8122 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8123 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8124 | |
8125 | long | |
217aa764 AM |
8126 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8127 | arelent **storage, | |
8128 | asymbol **syms) | |
252b5132 | 8129 | { |
217aa764 | 8130 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8131 | asection *s; |
8132 | long ret; | |
8133 | ||
8134 | if (elf_dynsymtab (abfd) == 0) | |
8135 | { | |
8136 | bfd_set_error (bfd_error_invalid_operation); | |
8137 | return -1; | |
8138 | } | |
8139 | ||
8140 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8141 | ret = 0; | |
8142 | for (s = abfd->sections; s != NULL; s = s->next) | |
8143 | { | |
266b05cf | 8144 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8145 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8146 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8147 | { | |
8148 | arelent *p; | |
8149 | long count, i; | |
8150 | ||
b34976b6 | 8151 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8152 | return -1; |
eea6121a | 8153 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8154 | p = s->relocation; |
8155 | for (i = 0; i < count; i++) | |
8156 | *storage++ = p++; | |
8157 | ret += count; | |
8158 | } | |
8159 | } | |
8160 | ||
8161 | *storage = NULL; | |
8162 | ||
8163 | return ret; | |
8164 | } | |
8165 | \f | |
8166 | /* Read in the version information. */ | |
8167 | ||
b34976b6 | 8168 | bfd_boolean |
fc0e6df6 | 8169 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8170 | { |
8171 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8172 | unsigned int freeidx = 0; |
8173 | ||
8174 | if (elf_dynverref (abfd) != 0) | |
8175 | { | |
8176 | Elf_Internal_Shdr *hdr; | |
8177 | Elf_External_Verneed *everneed; | |
8178 | Elf_Internal_Verneed *iverneed; | |
8179 | unsigned int i; | |
d0fb9a8d | 8180 | bfd_byte *contents_end; |
fc0e6df6 PB |
8181 | |
8182 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8183 | ||
601a03ba | 8184 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed)) |
d0fb9a8d | 8185 | { |
601a03ba | 8186 | error_return_bad_verref: |
4eca0228 | 8187 | _bfd_error_handler |
601a03ba AM |
8188 | (_("%B: .gnu.version_r invalid entry"), abfd); |
8189 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
8190 | error_return_verref: |
8191 | elf_tdata (abfd)->verref = NULL; | |
8192 | elf_tdata (abfd)->cverrefs = 0; | |
8193 | goto error_return; | |
8194 | } | |
601a03ba AM |
8195 | |
8196 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
8197 | if (contents == NULL) | |
8198 | goto error_return_verref; | |
8199 | ||
fc0e6df6 PB |
8200 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8201 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8202 | goto error_return_verref; |
fc0e6df6 | 8203 | |
601a03ba AM |
8204 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
8205 | bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); | |
8206 | ||
8207 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8208 | goto error_return_verref; |
8209 | ||
8210 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8211 | == sizeof (Elf_External_Vernaux)); | |
8212 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8213 | everneed = (Elf_External_Verneed *) contents; |
8214 | iverneed = elf_tdata (abfd)->verref; | |
8215 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8216 | { | |
8217 | Elf_External_Vernaux *evernaux; | |
8218 | Elf_Internal_Vernaux *ivernaux; | |
8219 | unsigned int j; | |
8220 | ||
8221 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8222 | ||
8223 | iverneed->vn_bfd = abfd; | |
8224 | ||
8225 | iverneed->vn_filename = | |
8226 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8227 | iverneed->vn_file); | |
8228 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8229 | goto error_return_bad_verref; |
fc0e6df6 | 8230 | |
d0fb9a8d JJ |
8231 | if (iverneed->vn_cnt == 0) |
8232 | iverneed->vn_auxptr = NULL; | |
8233 | else | |
8234 | { | |
a50b1753 NC |
8235 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
8236 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
8237 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8238 | if (iverneed->vn_auxptr == NULL) |
8239 | goto error_return_verref; | |
8240 | } | |
8241 | ||
8242 | if (iverneed->vn_aux | |
8243 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8244 | goto error_return_bad_verref; |
fc0e6df6 PB |
8245 | |
8246 | evernaux = ((Elf_External_Vernaux *) | |
8247 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8248 | ivernaux = iverneed->vn_auxptr; | |
8249 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8250 | { | |
8251 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8252 | ||
8253 | ivernaux->vna_nodename = | |
8254 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8255 | ivernaux->vna_name); | |
8256 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8257 | goto error_return_bad_verref; |
fc0e6df6 | 8258 | |
25ff461f AM |
8259 | if (ivernaux->vna_other > freeidx) |
8260 | freeidx = ivernaux->vna_other; | |
8261 | ||
8262 | ivernaux->vna_nextptr = NULL; | |
8263 | if (ivernaux->vna_next == 0) | |
8264 | { | |
8265 | iverneed->vn_cnt = j + 1; | |
8266 | break; | |
8267 | } | |
fc0e6df6 PB |
8268 | if (j + 1 < iverneed->vn_cnt) |
8269 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8270 | |
d0fb9a8d JJ |
8271 | if (ivernaux->vna_next |
8272 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8273 | goto error_return_bad_verref; |
d0fb9a8d | 8274 | |
fc0e6df6 PB |
8275 | evernaux = ((Elf_External_Vernaux *) |
8276 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8277 | } |
8278 | ||
25ff461f AM |
8279 | iverneed->vn_nextref = NULL; |
8280 | if (iverneed->vn_next == 0) | |
8281 | break; | |
fc0e6df6 PB |
8282 | if (i + 1 < hdr->sh_info) |
8283 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8284 | |
d0fb9a8d JJ |
8285 | if (iverneed->vn_next |
8286 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8287 | goto error_return_bad_verref; |
d0fb9a8d | 8288 | |
fc0e6df6 PB |
8289 | everneed = ((Elf_External_Verneed *) |
8290 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8291 | } | |
25ff461f | 8292 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8293 | |
8294 | free (contents); | |
8295 | contents = NULL; | |
8296 | } | |
252b5132 RH |
8297 | |
8298 | if (elf_dynverdef (abfd) != 0) | |
8299 | { | |
8300 | Elf_Internal_Shdr *hdr; | |
8301 | Elf_External_Verdef *everdef; | |
8302 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8303 | Elf_Internal_Verdef *iverdefarr; |
8304 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8305 | unsigned int i; |
062e2358 | 8306 | unsigned int maxidx; |
d0fb9a8d | 8307 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8308 | |
8309 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8310 | ||
601a03ba AM |
8311 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8312 | { | |
8313 | error_return_bad_verdef: | |
4eca0228 | 8314 | _bfd_error_handler |
601a03ba AM |
8315 | (_("%B: .gnu.version_d invalid entry"), abfd); |
8316 | bfd_set_error (bfd_error_bad_value); | |
8317 | error_return_verdef: | |
8318 | elf_tdata (abfd)->verdef = NULL; | |
8319 | elf_tdata (abfd)->cverdefs = 0; | |
8320 | goto error_return; | |
8321 | } | |
8322 | ||
a50b1753 | 8323 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8324 | if (contents == NULL) |
601a03ba | 8325 | goto error_return_verdef; |
252b5132 | 8326 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8327 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8328 | goto error_return_verdef; |
d0fb9a8d JJ |
8329 | |
8330 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8331 | >= sizeof (Elf_External_Verdaux)); | |
8332 | contents_end_def = contents + hdr->sh_size | |
8333 | - sizeof (Elf_External_Verdef); | |
8334 | contents_end_aux = contents + hdr->sh_size | |
8335 | - sizeof (Elf_External_Verdaux); | |
8336 | ||
f631889e UD |
8337 | /* We know the number of entries in the section but not the maximum |
8338 | index. Therefore we have to run through all entries and find | |
8339 | the maximum. */ | |
252b5132 | 8340 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8341 | maxidx = 0; |
8342 | for (i = 0; i < hdr->sh_info; ++i) | |
8343 | { | |
8344 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8345 | ||
601a03ba AM |
8346 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8347 | goto error_return_bad_verdef; | |
062e2358 AM |
8348 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8349 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8350 | |
25ff461f AM |
8351 | if (iverdefmem.vd_next == 0) |
8352 | break; | |
8353 | ||
d0fb9a8d JJ |
8354 | if (iverdefmem.vd_next |
8355 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8356 | goto error_return_bad_verdef; |
d0fb9a8d | 8357 | |
f631889e UD |
8358 | everdef = ((Elf_External_Verdef *) |
8359 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8360 | } | |
8361 | ||
fc0e6df6 PB |
8362 | if (default_imported_symver) |
8363 | { | |
8364 | if (freeidx > maxidx) | |
8365 | maxidx = ++freeidx; | |
8366 | else | |
8367 | freeidx = ++maxidx; | |
8368 | } | |
201159ec | 8369 | |
601a03ba AM |
8370 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8371 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8372 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8373 | goto error_return_verdef; |
f631889e UD |
8374 | |
8375 | elf_tdata (abfd)->cverdefs = maxidx; | |
8376 | ||
8377 | everdef = (Elf_External_Verdef *) contents; | |
8378 | iverdefarr = elf_tdata (abfd)->verdef; | |
8379 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8380 | { |
8381 | Elf_External_Verdaux *everdaux; | |
8382 | Elf_Internal_Verdaux *iverdaux; | |
8383 | unsigned int j; | |
8384 | ||
f631889e UD |
8385 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8386 | ||
d0fb9a8d | 8387 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8388 | goto error_return_bad_verdef; |
d0fb9a8d | 8389 | |
f631889e | 8390 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8391 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8392 | |
8393 | iverdef->vd_bfd = abfd; | |
8394 | ||
d0fb9a8d JJ |
8395 | if (iverdef->vd_cnt == 0) |
8396 | iverdef->vd_auxptr = NULL; | |
8397 | else | |
8398 | { | |
a50b1753 NC |
8399 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
8400 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
8401 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8402 | if (iverdef->vd_auxptr == NULL) |
8403 | goto error_return_verdef; | |
8404 | } | |
8405 | ||
8406 | if (iverdef->vd_aux | |
8407 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8408 | goto error_return_bad_verdef; |
252b5132 RH |
8409 | |
8410 | everdaux = ((Elf_External_Verdaux *) | |
8411 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8412 | iverdaux = iverdef->vd_auxptr; | |
8413 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8414 | { | |
8415 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8416 | ||
8417 | iverdaux->vda_nodename = | |
8418 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8419 | iverdaux->vda_name); | |
8420 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8421 | goto error_return_bad_verdef; |
252b5132 | 8422 | |
25ff461f AM |
8423 | iverdaux->vda_nextptr = NULL; |
8424 | if (iverdaux->vda_next == 0) | |
8425 | { | |
8426 | iverdef->vd_cnt = j + 1; | |
8427 | break; | |
8428 | } | |
252b5132 RH |
8429 | if (j + 1 < iverdef->vd_cnt) |
8430 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8431 | |
d0fb9a8d JJ |
8432 | if (iverdaux->vda_next |
8433 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8434 | goto error_return_bad_verdef; |
d0fb9a8d | 8435 | |
252b5132 RH |
8436 | everdaux = ((Elf_External_Verdaux *) |
8437 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8438 | } | |
8439 | ||
595bce75 | 8440 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8441 | if (iverdef->vd_cnt) |
8442 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8443 | |
25ff461f AM |
8444 | iverdef->vd_nextdef = NULL; |
8445 | if (iverdef->vd_next == 0) | |
8446 | break; | |
d0fb9a8d | 8447 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8448 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8449 | |
8450 | everdef = ((Elf_External_Verdef *) | |
8451 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8452 | } | |
8453 | ||
8454 | free (contents); | |
8455 | contents = NULL; | |
8456 | } | |
fc0e6df6 | 8457 | else if (default_imported_symver) |
252b5132 | 8458 | { |
fc0e6df6 PB |
8459 | if (freeidx < 3) |
8460 | freeidx = 3; | |
8461 | else | |
8462 | freeidx++; | |
252b5132 | 8463 | |
a50b1753 NC |
8464 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8465 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8466 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8467 | goto error_return; |
8468 | ||
fc0e6df6 PB |
8469 | elf_tdata (abfd)->cverdefs = freeidx; |
8470 | } | |
252b5132 | 8471 | |
fc0e6df6 PB |
8472 | /* Create a default version based on the soname. */ |
8473 | if (default_imported_symver) | |
8474 | { | |
8475 | Elf_Internal_Verdef *iverdef; | |
8476 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8477 | |
5bb3703f | 8478 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8479 | |
fc0e6df6 PB |
8480 | iverdef->vd_version = VER_DEF_CURRENT; |
8481 | iverdef->vd_flags = 0; | |
8482 | iverdef->vd_ndx = freeidx; | |
8483 | iverdef->vd_cnt = 1; | |
252b5132 | 8484 | |
fc0e6df6 | 8485 | iverdef->vd_bfd = abfd; |
252b5132 | 8486 | |
fc0e6df6 PB |
8487 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8488 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8489 | goto error_return_verdef; |
fc0e6df6 | 8490 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8491 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8492 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8493 | if (iverdef->vd_auxptr == NULL) |
8494 | goto error_return_verdef; | |
252b5132 | 8495 | |
fc0e6df6 PB |
8496 | iverdaux = iverdef->vd_auxptr; |
8497 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8498 | } |
8499 | ||
b34976b6 | 8500 | return TRUE; |
252b5132 RH |
8501 | |
8502 | error_return: | |
5ed6aba4 | 8503 | if (contents != NULL) |
252b5132 | 8504 | free (contents); |
b34976b6 | 8505 | return FALSE; |
252b5132 RH |
8506 | } |
8507 | \f | |
8508 | asymbol * | |
217aa764 | 8509 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8510 | { |
8511 | elf_symbol_type *newsym; | |
8512 | ||
201159ec | 8513 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8514 | if (!newsym) |
8515 | return NULL; | |
201159ec NC |
8516 | newsym->symbol.the_bfd = abfd; |
8517 | return &newsym->symbol; | |
252b5132 RH |
8518 | } |
8519 | ||
8520 | void | |
217aa764 AM |
8521 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8522 | asymbol *symbol, | |
8523 | symbol_info *ret) | |
252b5132 RH |
8524 | { |
8525 | bfd_symbol_info (symbol, ret); | |
8526 | } | |
8527 | ||
8528 | /* Return whether a symbol name implies a local symbol. Most targets | |
8529 | use this function for the is_local_label_name entry point, but some | |
8530 | override it. */ | |
8531 | ||
b34976b6 | 8532 | bfd_boolean |
217aa764 AM |
8533 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8534 | const char *name) | |
252b5132 RH |
8535 | { |
8536 | /* Normal local symbols start with ``.L''. */ | |
8537 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8538 | return TRUE; |
252b5132 RH |
8539 | |
8540 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8541 | DWARF debugging symbols starting with ``..''. */ | |
8542 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8543 | return TRUE; |
252b5132 RH |
8544 | |
8545 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8546 | emitting DWARF debugging output. I suspect this is actually a | |
8547 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8548 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8549 | underscore to be emitted on some ELF targets). For ease of use, | |
8550 | we treat such symbols as local. */ | |
8551 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8552 | return TRUE; |
252b5132 | 8553 | |
b1fa9dd6 NC |
8554 | /* Treat assembler generated fake symbols, dollar local labels and |
8555 | forward-backward labels (aka local labels) as locals. | |
8556 | These labels have the form: | |
8557 | ||
8558 | L0^A.* (fake symbols) | |
8559 | ||
8560 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8561 | ||
8562 | Versions which start with .L will have already been matched above, | |
8563 | so we only need to match the rest. */ | |
8564 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8565 | { | |
8566 | bfd_boolean ret = FALSE; | |
8567 | const char * p; | |
8568 | char c; | |
8569 | ||
8570 | for (p = name + 2; (c = *p); p++) | |
8571 | { | |
8572 | if (c == 1 || c == 2) | |
8573 | { | |
8574 | if (c == 1 && p == name + 2) | |
8575 | /* A fake symbol. */ | |
8576 | return TRUE; | |
8577 | ||
8578 | /* FIXME: We are being paranoid here and treating symbols like | |
8579 | L0^Bfoo as if there were non-local, on the grounds that the | |
8580 | assembler will never generate them. But can any symbol | |
8581 | containing an ASCII value in the range 1-31 ever be anything | |
8582 | other than some kind of local ? */ | |
8583 | ret = TRUE; | |
8584 | } | |
8585 | ||
8586 | if (! ISDIGIT (c)) | |
8587 | { | |
8588 | ret = FALSE; | |
8589 | break; | |
8590 | } | |
8591 | } | |
8592 | return ret; | |
8593 | } | |
ffa54770 | 8594 | |
b34976b6 | 8595 | return FALSE; |
252b5132 RH |
8596 | } |
8597 | ||
8598 | alent * | |
217aa764 AM |
8599 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8600 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8601 | { |
8602 | abort (); | |
8603 | return NULL; | |
8604 | } | |
8605 | ||
b34976b6 | 8606 | bfd_boolean |
217aa764 AM |
8607 | _bfd_elf_set_arch_mach (bfd *abfd, |
8608 | enum bfd_architecture arch, | |
8609 | unsigned long machine) | |
252b5132 RH |
8610 | { |
8611 | /* If this isn't the right architecture for this backend, and this | |
8612 | isn't the generic backend, fail. */ | |
8613 | if (arch != get_elf_backend_data (abfd)->arch | |
8614 | && arch != bfd_arch_unknown | |
8615 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8616 | return FALSE; |
252b5132 RH |
8617 | |
8618 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8619 | } | |
8620 | ||
d1fad7c6 NC |
8621 | /* Find the nearest line to a particular section and offset, |
8622 | for error reporting. */ | |
8623 | ||
b34976b6 | 8624 | bfd_boolean |
217aa764 | 8625 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8626 | asymbol **symbols, |
fb167eb2 | 8627 | asection *section, |
217aa764 AM |
8628 | bfd_vma offset, |
8629 | const char **filename_ptr, | |
8630 | const char **functionname_ptr, | |
fb167eb2 AM |
8631 | unsigned int *line_ptr, |
8632 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8633 | { |
b34976b6 | 8634 | bfd_boolean found; |
d1fad7c6 | 8635 | |
fb167eb2 | 8636 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8637 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8638 | line_ptr, discriminator_ptr, |
8639 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8640 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8641 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8642 | filename_ptr, functionname_ptr, | |
8643 | line_ptr)) | |
d1fad7c6 NC |
8644 | { |
8645 | if (!*functionname_ptr) | |
e00e8198 AM |
8646 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8647 | *filename_ptr ? NULL : filename_ptr, | |
8648 | functionname_ptr); | |
b34976b6 | 8649 | return TRUE; |
d1fad7c6 NC |
8650 | } |
8651 | ||
8652 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8653 | &found, filename_ptr, |
8654 | functionname_ptr, line_ptr, | |
8655 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8656 | return FALSE; |
dc43ada5 | 8657 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8658 | return TRUE; |
d1fad7c6 NC |
8659 | |
8660 | if (symbols == NULL) | |
b34976b6 | 8661 | return FALSE; |
d1fad7c6 | 8662 | |
e00e8198 AM |
8663 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8664 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8665 | return FALSE; |
d1fad7c6 | 8666 | |
252b5132 | 8667 | *line_ptr = 0; |
b34976b6 | 8668 | return TRUE; |
252b5132 RH |
8669 | } |
8670 | ||
5420f73d L |
8671 | /* Find the line for a symbol. */ |
8672 | ||
8673 | bfd_boolean | |
8674 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8675 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8676 | { |
fb167eb2 AM |
8677 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8678 | filename_ptr, NULL, line_ptr, NULL, | |
8679 | dwarf_debug_sections, 0, | |
8680 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8681 | } |
8682 | ||
4ab527b0 FF |
8683 | /* After a call to bfd_find_nearest_line, successive calls to |
8684 | bfd_find_inliner_info can be used to get source information about | |
8685 | each level of function inlining that terminated at the address | |
8686 | passed to bfd_find_nearest_line. Currently this is only supported | |
8687 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8688 | ||
8689 | bfd_boolean | |
8690 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8691 | const char **filename_ptr, | |
8692 | const char **functionname_ptr, | |
8693 | unsigned int *line_ptr) | |
8694 | { | |
8695 | bfd_boolean found; | |
8696 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8697 | functionname_ptr, line_ptr, | |
8698 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8699 | return found; | |
8700 | } | |
8701 | ||
252b5132 | 8702 | int |
a6b96beb | 8703 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8704 | { |
8ded5a0f AM |
8705 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8706 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8707 | |
0e1862bb | 8708 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8709 | { |
12bd6957 | 8710 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8711 | |
62d7a5f6 AM |
8712 | if (phdr_size == (bfd_size_type) -1) |
8713 | { | |
8714 | struct elf_segment_map *m; | |
8715 | ||
8716 | phdr_size = 0; | |
12bd6957 | 8717 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8718 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8719 | |
62d7a5f6 AM |
8720 | if (phdr_size == 0) |
8721 | phdr_size = get_program_header_size (abfd, info); | |
8722 | } | |
8ded5a0f | 8723 | |
12bd6957 | 8724 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8725 | ret += phdr_size; |
8726 | } | |
8727 | ||
252b5132 RH |
8728 | return ret; |
8729 | } | |
8730 | ||
b34976b6 | 8731 | bfd_boolean |
217aa764 AM |
8732 | _bfd_elf_set_section_contents (bfd *abfd, |
8733 | sec_ptr section, | |
0f867abe | 8734 | const void *location, |
217aa764 AM |
8735 | file_ptr offset, |
8736 | bfd_size_type count) | |
252b5132 RH |
8737 | { |
8738 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8739 | file_ptr pos; |
252b5132 RH |
8740 | |
8741 | if (! abfd->output_has_begun | |
217aa764 | 8742 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8743 | return FALSE; |
252b5132 | 8744 | |
0ce398f1 L |
8745 | if (!count) |
8746 | return TRUE; | |
8747 | ||
252b5132 | 8748 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8749 | if (hdr->sh_offset == (file_ptr) -1) |
8750 | { | |
8751 | /* We must compress this section. Write output to the buffer. */ | |
8752 | unsigned char *contents = hdr->contents; | |
8753 | if ((offset + count) > hdr->sh_size | |
8754 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8755 | || contents == NULL) | |
8756 | abort (); | |
8757 | memcpy (contents + offset, location, count); | |
8758 | return TRUE; | |
8759 | } | |
dc810e39 AM |
8760 | pos = hdr->sh_offset + offset; |
8761 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8762 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8763 | return FALSE; |
252b5132 | 8764 | |
b34976b6 | 8765 | return TRUE; |
252b5132 RH |
8766 | } |
8767 | ||
8768 | void | |
217aa764 AM |
8769 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8770 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8771 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8772 | { |
8773 | abort (); | |
8774 | } | |
8775 | ||
252b5132 RH |
8776 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8777 | ||
b34976b6 | 8778 | bfd_boolean |
217aa764 | 8779 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8780 | { |
c044fabd | 8781 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8782 | |
8783 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8784 | { | |
8785 | bfd_reloc_code_real_type code; | |
8786 | reloc_howto_type *howto; | |
8787 | ||
8788 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8789 | equivalent ELF reloc. */ |
252b5132 RH |
8790 | |
8791 | if (areloc->howto->pc_relative) | |
8792 | { | |
8793 | switch (areloc->howto->bitsize) | |
8794 | { | |
8795 | case 8: | |
8796 | code = BFD_RELOC_8_PCREL; | |
8797 | break; | |
8798 | case 12: | |
8799 | code = BFD_RELOC_12_PCREL; | |
8800 | break; | |
8801 | case 16: | |
8802 | code = BFD_RELOC_16_PCREL; | |
8803 | break; | |
8804 | case 24: | |
8805 | code = BFD_RELOC_24_PCREL; | |
8806 | break; | |
8807 | case 32: | |
8808 | code = BFD_RELOC_32_PCREL; | |
8809 | break; | |
8810 | case 64: | |
8811 | code = BFD_RELOC_64_PCREL; | |
8812 | break; | |
8813 | default: | |
8814 | goto fail; | |
8815 | } | |
8816 | ||
8817 | howto = bfd_reloc_type_lookup (abfd, code); | |
8818 | ||
8819 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8820 | { | |
8821 | if (howto->pcrel_offset) | |
8822 | areloc->addend += areloc->address; | |
8823 | else | |
8824 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8825 | } | |
8826 | } | |
8827 | else | |
8828 | { | |
8829 | switch (areloc->howto->bitsize) | |
8830 | { | |
8831 | case 8: | |
8832 | code = BFD_RELOC_8; | |
8833 | break; | |
8834 | case 14: | |
8835 | code = BFD_RELOC_14; | |
8836 | break; | |
8837 | case 16: | |
8838 | code = BFD_RELOC_16; | |
8839 | break; | |
8840 | case 26: | |
8841 | code = BFD_RELOC_26; | |
8842 | break; | |
8843 | case 32: | |
8844 | code = BFD_RELOC_32; | |
8845 | break; | |
8846 | case 64: | |
8847 | code = BFD_RELOC_64; | |
8848 | break; | |
8849 | default: | |
8850 | goto fail; | |
8851 | } | |
8852 | ||
8853 | howto = bfd_reloc_type_lookup (abfd, code); | |
8854 | } | |
8855 | ||
8856 | if (howto) | |
8857 | areloc->howto = howto; | |
8858 | else | |
8859 | goto fail; | |
8860 | } | |
8861 | ||
b34976b6 | 8862 | return TRUE; |
252b5132 RH |
8863 | |
8864 | fail: | |
4eca0228 | 8865 | _bfd_error_handler |
695344c0 | 8866 | /* xgettext:c-format */ |
d003868e AM |
8867 | (_("%B: unsupported relocation type %s"), |
8868 | abfd, areloc->howto->name); | |
252b5132 | 8869 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8870 | return FALSE; |
252b5132 RH |
8871 | } |
8872 | ||
b34976b6 | 8873 | bfd_boolean |
217aa764 | 8874 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8875 | { |
d9071b0c TG |
8876 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8877 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8878 | { |
c0355132 | 8879 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8880 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8881 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8882 | } |
8883 | ||
8884 | return _bfd_generic_close_and_cleanup (abfd); | |
8885 | } | |
8886 | ||
8887 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8888 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8889 | range-checking to interfere. There is nothing else to do in processing | |
8890 | this reloc. */ | |
8891 | ||
8892 | bfd_reloc_status_type | |
217aa764 AM |
8893 | _bfd_elf_rel_vtable_reloc_fn |
8894 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8895 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8896 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8897 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8898 | { |
8899 | return bfd_reloc_ok; | |
8900 | } | |
252b5132 RH |
8901 | \f |
8902 | /* Elf core file support. Much of this only works on native | |
8903 | toolchains, since we rely on knowing the | |
8904 | machine-dependent procfs structure in order to pick | |
c044fabd | 8905 | out details about the corefile. */ |
252b5132 RH |
8906 | |
8907 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8908 | /* Needed for new procfs interface on sparc-solaris. */ |
8909 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8910 | # include <sys/procfs.h> |
8911 | #endif | |
8912 | ||
261b8d08 PA |
8913 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8914 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8915 | |
8916 | static int | |
217aa764 | 8917 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8918 | { |
261b8d08 PA |
8919 | int pid; |
8920 | ||
228e534f | 8921 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8922 | if (pid == 0) |
228e534f | 8923 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8924 | |
8925 | return pid; | |
252b5132 RH |
8926 | } |
8927 | ||
252b5132 RH |
8928 | /* If there isn't a section called NAME, make one, using |
8929 | data from SECT. Note, this function will generate a | |
8930 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8931 | overwrite it. */ |
252b5132 | 8932 | |
b34976b6 | 8933 | static bfd_boolean |
217aa764 | 8934 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8935 | { |
c044fabd | 8936 | asection *sect2; |
252b5132 RH |
8937 | |
8938 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8939 | return TRUE; |
252b5132 | 8940 | |
117ed4f8 | 8941 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8942 | if (sect2 == NULL) |
b34976b6 | 8943 | return FALSE; |
252b5132 | 8944 | |
eea6121a | 8945 | sect2->size = sect->size; |
252b5132 | 8946 | sect2->filepos = sect->filepos; |
252b5132 | 8947 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8948 | return TRUE; |
252b5132 RH |
8949 | } |
8950 | ||
bb0082d6 AM |
8951 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8952 | actually creates up to two pseudosections: | |
8953 | - For the single-threaded case, a section named NAME, unless | |
8954 | such a section already exists. | |
8955 | - For the multi-threaded case, a section named "NAME/PID", where | |
8956 | PID is elfcore_make_pid (abfd). | |
8957 | Both pseudosections have identical contents. */ | |
b34976b6 | 8958 | bfd_boolean |
217aa764 AM |
8959 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8960 | char *name, | |
8961 | size_t size, | |
8962 | ufile_ptr filepos) | |
bb0082d6 AM |
8963 | { |
8964 | char buf[100]; | |
8965 | char *threaded_name; | |
d4c88bbb | 8966 | size_t len; |
bb0082d6 AM |
8967 | asection *sect; |
8968 | ||
8969 | /* Build the section name. */ | |
8970 | ||
8971 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8972 | len = strlen (buf) + 1; |
a50b1753 | 8973 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8974 | if (threaded_name == NULL) |
b34976b6 | 8975 | return FALSE; |
d4c88bbb | 8976 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8977 | |
117ed4f8 AM |
8978 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8979 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8980 | if (sect == NULL) |
b34976b6 | 8981 | return FALSE; |
eea6121a | 8982 | sect->size = size; |
bb0082d6 | 8983 | sect->filepos = filepos; |
bb0082d6 AM |
8984 | sect->alignment_power = 2; |
8985 | ||
936e320b | 8986 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
8987 | } |
8988 | ||
252b5132 | 8989 | /* prstatus_t exists on: |
4a938328 | 8990 | solaris 2.5+ |
252b5132 RH |
8991 | linux 2.[01] + glibc |
8992 | unixware 4.2 | |
8993 | */ | |
8994 | ||
8995 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 8996 | |
b34976b6 | 8997 | static bfd_boolean |
217aa764 | 8998 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 8999 | { |
eea6121a | 9000 | size_t size; |
7ee38065 | 9001 | int offset; |
252b5132 | 9002 | |
4a938328 MS |
9003 | if (note->descsz == sizeof (prstatus_t)) |
9004 | { | |
9005 | prstatus_t prstat; | |
252b5132 | 9006 | |
eea6121a | 9007 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9008 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9009 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9010 | |
fa49d224 NC |
9011 | /* Do not overwrite the core signal if it |
9012 | has already been set by another thread. */ | |
228e534f AM |
9013 | if (elf_tdata (abfd)->core->signal == 0) |
9014 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9015 | if (elf_tdata (abfd)->core->pid == 0) | |
9016 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9017 | |
4a938328 MS |
9018 | /* pr_who exists on: |
9019 | solaris 2.5+ | |
9020 | unixware 4.2 | |
9021 | pr_who doesn't exist on: | |
9022 | linux 2.[01] | |
9023 | */ | |
252b5132 | 9024 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9025 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9026 | #else |
228e534f | 9027 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9028 | #endif |
4a938328 | 9029 | } |
7ee38065 | 9030 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9031 | else if (note->descsz == sizeof (prstatus32_t)) |
9032 | { | |
9033 | /* 64-bit host, 32-bit corefile */ | |
9034 | prstatus32_t prstat; | |
9035 | ||
eea6121a | 9036 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9037 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9038 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9039 | ||
fa49d224 NC |
9040 | /* Do not overwrite the core signal if it |
9041 | has already been set by another thread. */ | |
228e534f AM |
9042 | if (elf_tdata (abfd)->core->signal == 0) |
9043 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9044 | if (elf_tdata (abfd)->core->pid == 0) | |
9045 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9046 | |
9047 | /* pr_who exists on: | |
9048 | solaris 2.5+ | |
9049 | unixware 4.2 | |
9050 | pr_who doesn't exist on: | |
9051 | linux 2.[01] | |
9052 | */ | |
7ee38065 | 9053 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9054 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9055 | #else |
228e534f | 9056 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9057 | #endif |
9058 | } | |
7ee38065 | 9059 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9060 | else |
9061 | { | |
9062 | /* Fail - we don't know how to handle any other | |
9063 | note size (ie. data object type). */ | |
b34976b6 | 9064 | return TRUE; |
4a938328 | 9065 | } |
252b5132 | 9066 | |
bb0082d6 | 9067 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9068 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9069 | size, note->descpos + offset); |
252b5132 RH |
9070 | } |
9071 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9072 | ||
bb0082d6 | 9073 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9074 | static bfd_boolean |
217aa764 AM |
9075 | elfcore_make_note_pseudosection (bfd *abfd, |
9076 | char *name, | |
9077 | Elf_Internal_Note *note) | |
252b5132 | 9078 | { |
936e320b AM |
9079 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9080 | note->descsz, note->descpos); | |
252b5132 RH |
9081 | } |
9082 | ||
ff08c6bb JB |
9083 | /* There isn't a consistent prfpregset_t across platforms, |
9084 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9085 | data structure apart. */ |
9086 | ||
b34976b6 | 9087 | static bfd_boolean |
217aa764 | 9088 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9089 | { |
9090 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9091 | } | |
9092 | ||
ff08c6bb | 9093 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9094 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9095 | literally. */ |
c044fabd | 9096 | |
b34976b6 | 9097 | static bfd_boolean |
217aa764 | 9098 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9099 | { |
9100 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9101 | } | |
9102 | ||
4339cae0 L |
9103 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9104 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9105 | contents literally. */ | |
9106 | ||
9107 | static bfd_boolean | |
9108 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9109 | { | |
9110 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9111 | } | |
9112 | ||
97753bd5 AM |
9113 | static bfd_boolean |
9114 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9115 | { | |
9116 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9117 | } | |
9118 | ||
89eeb0bc LM |
9119 | static bfd_boolean |
9120 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9121 | { | |
9122 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9123 | } | |
97753bd5 | 9124 | |
0675e188 UW |
9125 | static bfd_boolean |
9126 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9127 | { | |
9128 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9129 | } | |
9130 | ||
d7eeb400 MS |
9131 | static bfd_boolean |
9132 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9133 | { | |
9134 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9135 | } | |
9136 | ||
9137 | static bfd_boolean | |
9138 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9139 | { | |
9140 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9141 | } | |
9142 | ||
9143 | static bfd_boolean | |
9144 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9145 | { | |
9146 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9147 | } | |
9148 | ||
9149 | static bfd_boolean | |
9150 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9151 | { | |
9152 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9153 | } | |
9154 | ||
9155 | static bfd_boolean | |
9156 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9157 | { | |
9158 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9159 | } | |
9160 | ||
355b81d9 UW |
9161 | static bfd_boolean |
9162 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9163 | { | |
9164 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9165 | } | |
9166 | ||
9167 | static bfd_boolean | |
9168 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9169 | { | |
9170 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9171 | } | |
9172 | ||
abb3f6cc NC |
9173 | static bfd_boolean |
9174 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9175 | { | |
9176 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9177 | } | |
9178 | ||
4ef9f41a AA |
9179 | static bfd_boolean |
9180 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9181 | { | |
9182 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9183 | } | |
9184 | ||
9185 | static bfd_boolean | |
9186 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9187 | { | |
9188 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9189 | } | |
9190 | ||
88ab90e8 AA |
9191 | static bfd_boolean |
9192 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9193 | { | |
9194 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9195 | } | |
9196 | ||
9197 | static bfd_boolean | |
9198 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9199 | { | |
9200 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9201 | } | |
9202 | ||
faa9a424 UW |
9203 | static bfd_boolean |
9204 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9205 | { | |
9206 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9207 | } | |
9208 | ||
652451f8 YZ |
9209 | static bfd_boolean |
9210 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9211 | { | |
9212 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9213 | } | |
9214 | ||
9215 | static bfd_boolean | |
9216 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9217 | { | |
9218 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9219 | } | |
9220 | ||
9221 | static bfd_boolean | |
9222 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9223 | { | |
9224 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9225 | } | |
9226 | ||
252b5132 | 9227 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9228 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9229 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9230 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9231 | #endif | |
252b5132 RH |
9232 | #endif |
9233 | ||
9234 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9235 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9236 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9237 | typedef psinfo32_t elfcore_psinfo32_t; |
9238 | #endif | |
252b5132 RH |
9239 | #endif |
9240 | ||
252b5132 RH |
9241 | /* return a malloc'ed copy of a string at START which is at |
9242 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9243 | the copy will always have a terminating '\0'. */ |
252b5132 | 9244 | |
936e320b | 9245 | char * |
217aa764 | 9246 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9247 | { |
dc810e39 | 9248 | char *dups; |
a50b1753 | 9249 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9250 | size_t len; |
252b5132 RH |
9251 | |
9252 | if (end == NULL) | |
9253 | len = max; | |
9254 | else | |
9255 | len = end - start; | |
9256 | ||
a50b1753 | 9257 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9258 | if (dups == NULL) |
252b5132 RH |
9259 | return NULL; |
9260 | ||
dc810e39 AM |
9261 | memcpy (dups, start, len); |
9262 | dups[len] = '\0'; | |
252b5132 | 9263 | |
dc810e39 | 9264 | return dups; |
252b5132 RH |
9265 | } |
9266 | ||
bb0082d6 | 9267 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9268 | static bfd_boolean |
217aa764 | 9269 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9270 | { |
4a938328 MS |
9271 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9272 | { | |
9273 | elfcore_psinfo_t psinfo; | |
252b5132 | 9274 | |
7ee38065 | 9275 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9276 | |
335e41d4 | 9277 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9278 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9279 | #endif |
228e534f | 9280 | elf_tdata (abfd)->core->program |
936e320b AM |
9281 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9282 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9283 | |
228e534f | 9284 | elf_tdata (abfd)->core->command |
936e320b AM |
9285 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9286 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9287 | } |
7ee38065 | 9288 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9289 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9290 | { | |
9291 | /* 64-bit host, 32-bit corefile */ | |
9292 | elfcore_psinfo32_t psinfo; | |
9293 | ||
7ee38065 | 9294 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9295 | |
335e41d4 | 9296 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9297 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9298 | #endif |
228e534f | 9299 | elf_tdata (abfd)->core->program |
936e320b AM |
9300 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9301 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9302 | |
228e534f | 9303 | elf_tdata (abfd)->core->command |
936e320b AM |
9304 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9305 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9306 | } |
9307 | #endif | |
9308 | ||
9309 | else | |
9310 | { | |
9311 | /* Fail - we don't know how to handle any other | |
9312 | note size (ie. data object type). */ | |
b34976b6 | 9313 | return TRUE; |
4a938328 | 9314 | } |
252b5132 RH |
9315 | |
9316 | /* Note that for some reason, a spurious space is tacked | |
9317 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9318 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9319 | |
9320 | { | |
228e534f | 9321 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9322 | int n = strlen (command); |
9323 | ||
9324 | if (0 < n && command[n - 1] == ' ') | |
9325 | command[n - 1] = '\0'; | |
9326 | } | |
9327 | ||
b34976b6 | 9328 | return TRUE; |
252b5132 RH |
9329 | } |
9330 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9331 | ||
252b5132 | 9332 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9333 | static bfd_boolean |
217aa764 | 9334 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9335 | { |
f572a39d AM |
9336 | if (note->descsz == sizeof (pstatus_t) |
9337 | #if defined (HAVE_PXSTATUS_T) | |
9338 | || note->descsz == sizeof (pxstatus_t) | |
9339 | #endif | |
9340 | ) | |
4a938328 MS |
9341 | { |
9342 | pstatus_t pstat; | |
252b5132 | 9343 | |
4a938328 | 9344 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9345 | |
228e534f | 9346 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9347 | } |
7ee38065 | 9348 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9349 | else if (note->descsz == sizeof (pstatus32_t)) |
9350 | { | |
9351 | /* 64-bit host, 32-bit corefile */ | |
9352 | pstatus32_t pstat; | |
252b5132 | 9353 | |
4a938328 | 9354 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9355 | |
228e534f | 9356 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9357 | } |
9358 | #endif | |
252b5132 RH |
9359 | /* Could grab some more details from the "representative" |
9360 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9361 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9362 | |
b34976b6 | 9363 | return TRUE; |
252b5132 RH |
9364 | } |
9365 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9366 | ||
252b5132 | 9367 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9368 | static bfd_boolean |
217aa764 | 9369 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9370 | { |
9371 | lwpstatus_t lwpstat; | |
9372 | char buf[100]; | |
c044fabd | 9373 | char *name; |
d4c88bbb | 9374 | size_t len; |
c044fabd | 9375 | asection *sect; |
252b5132 | 9376 | |
f572a39d AM |
9377 | if (note->descsz != sizeof (lwpstat) |
9378 | #if defined (HAVE_LWPXSTATUS_T) | |
9379 | && note->descsz != sizeof (lwpxstatus_t) | |
9380 | #endif | |
9381 | ) | |
b34976b6 | 9382 | return TRUE; |
252b5132 RH |
9383 | |
9384 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9385 | ||
228e534f | 9386 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9387 | /* Do not overwrite the core signal if it has already been set by |
9388 | another thread. */ | |
228e534f AM |
9389 | if (elf_tdata (abfd)->core->signal == 0) |
9390 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9391 | |
c044fabd | 9392 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9393 | |
9394 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9395 | len = strlen (buf) + 1; |
217aa764 | 9396 | name = bfd_alloc (abfd, len); |
252b5132 | 9397 | if (name == NULL) |
b34976b6 | 9398 | return FALSE; |
d4c88bbb | 9399 | memcpy (name, buf, len); |
252b5132 | 9400 | |
117ed4f8 | 9401 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9402 | if (sect == NULL) |
b34976b6 | 9403 | return FALSE; |
252b5132 RH |
9404 | |
9405 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9406 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9407 | sect->filepos = note->descpos |
9408 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9409 | #endif | |
9410 | ||
9411 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9412 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9413 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9414 | #endif | |
9415 | ||
252b5132 RH |
9416 | sect->alignment_power = 2; |
9417 | ||
9418 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9419 | return FALSE; |
252b5132 RH |
9420 | |
9421 | /* Make a ".reg2/999" section */ | |
9422 | ||
9423 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9424 | len = strlen (buf) + 1; |
217aa764 | 9425 | name = bfd_alloc (abfd, len); |
252b5132 | 9426 | if (name == NULL) |
b34976b6 | 9427 | return FALSE; |
d4c88bbb | 9428 | memcpy (name, buf, len); |
252b5132 | 9429 | |
117ed4f8 | 9430 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9431 | if (sect == NULL) |
b34976b6 | 9432 | return FALSE; |
252b5132 RH |
9433 | |
9434 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9435 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9436 | sect->filepos = note->descpos |
9437 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9438 | #endif | |
9439 | ||
9440 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9441 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9442 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9443 | #endif | |
9444 | ||
252b5132 RH |
9445 | sect->alignment_power = 2; |
9446 | ||
936e320b | 9447 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9448 | } |
9449 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9450 | ||
b34976b6 | 9451 | static bfd_boolean |
217aa764 | 9452 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9453 | { |
9454 | char buf[30]; | |
c044fabd | 9455 | char *name; |
d4c88bbb | 9456 | size_t len; |
c044fabd | 9457 | asection *sect; |
4a6636fb PA |
9458 | int type; |
9459 | int is_active_thread; | |
9460 | bfd_vma base_addr; | |
16e9c715 | 9461 | |
4a6636fb | 9462 | if (note->descsz < 728) |
b34976b6 | 9463 | return TRUE; |
16e9c715 | 9464 | |
4a6636fb PA |
9465 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9466 | return TRUE; | |
9467 | ||
9468 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9469 | |
4a6636fb | 9470 | switch (type) |
16e9c715 | 9471 | { |
4a6636fb | 9472 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9473 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9474 | /* process_info.pid */ |
228e534f | 9475 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9476 | /* process_info.signal */ |
228e534f | 9477 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9478 | break; |
16e9c715 | 9479 | |
4a6636fb | 9480 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9481 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9482 | /* thread_info.tid */ |
9483 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9484 | |
d4c88bbb | 9485 | len = strlen (buf) + 1; |
a50b1753 | 9486 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9487 | if (name == NULL) |
b34976b6 | 9488 | return FALSE; |
c044fabd | 9489 | |
d4c88bbb | 9490 | memcpy (name, buf, len); |
16e9c715 | 9491 | |
117ed4f8 | 9492 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9493 | if (sect == NULL) |
b34976b6 | 9494 | return FALSE; |
c044fabd | 9495 | |
4a6636fb PA |
9496 | /* sizeof (thread_info.thread_context) */ |
9497 | sect->size = 716; | |
9498 | /* offsetof (thread_info.thread_context) */ | |
9499 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9500 | sect->alignment_power = 2; |
9501 | ||
4a6636fb PA |
9502 | /* thread_info.is_active_thread */ |
9503 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9504 | ||
9505 | if (is_active_thread) | |
16e9c715 | 9506 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9507 | return FALSE; |
16e9c715 NC |
9508 | break; |
9509 | ||
4a6636fb | 9510 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9511 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9512 | /* module_info.base_address */ |
9513 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9514 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9515 | |
d4c88bbb | 9516 | len = strlen (buf) + 1; |
a50b1753 | 9517 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9518 | if (name == NULL) |
b34976b6 | 9519 | return FALSE; |
c044fabd | 9520 | |
d4c88bbb | 9521 | memcpy (name, buf, len); |
252b5132 | 9522 | |
117ed4f8 | 9523 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9524 | |
16e9c715 | 9525 | if (sect == NULL) |
b34976b6 | 9526 | return FALSE; |
c044fabd | 9527 | |
eea6121a | 9528 | sect->size = note->descsz; |
16e9c715 | 9529 | sect->filepos = note->descpos; |
16e9c715 NC |
9530 | sect->alignment_power = 2; |
9531 | break; | |
9532 | ||
9533 | default: | |
b34976b6 | 9534 | return TRUE; |
16e9c715 NC |
9535 | } |
9536 | ||
b34976b6 | 9537 | return TRUE; |
16e9c715 | 9538 | } |
252b5132 | 9539 | |
b34976b6 | 9540 | static bfd_boolean |
217aa764 | 9541 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9542 | { |
9c5bfbb7 | 9543 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9544 | |
252b5132 RH |
9545 | switch (note->type) |
9546 | { | |
9547 | default: | |
b34976b6 | 9548 | return TRUE; |
252b5132 | 9549 | |
252b5132 | 9550 | case NT_PRSTATUS: |
bb0082d6 AM |
9551 | if (bed->elf_backend_grok_prstatus) |
9552 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9553 | return TRUE; |
bb0082d6 | 9554 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9555 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9556 | #else |
b34976b6 | 9557 | return TRUE; |
252b5132 RH |
9558 | #endif |
9559 | ||
9560 | #if defined (HAVE_PSTATUS_T) | |
9561 | case NT_PSTATUS: | |
9562 | return elfcore_grok_pstatus (abfd, note); | |
9563 | #endif | |
9564 | ||
9565 | #if defined (HAVE_LWPSTATUS_T) | |
9566 | case NT_LWPSTATUS: | |
9567 | return elfcore_grok_lwpstatus (abfd, note); | |
9568 | #endif | |
9569 | ||
9570 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9571 | return elfcore_grok_prfpreg (abfd, note); | |
9572 | ||
c044fabd | 9573 | case NT_WIN32PSTATUS: |
16e9c715 | 9574 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9575 | |
c044fabd | 9576 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9577 | if (note->namesz == 6 |
9578 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9579 | return elfcore_grok_prxfpreg (abfd, note); |
9580 | else | |
b34976b6 | 9581 | return TRUE; |
ff08c6bb | 9582 | |
4339cae0 L |
9583 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9584 | if (note->namesz == 6 | |
9585 | && strcmp (note->namedata, "LINUX") == 0) | |
9586 | return elfcore_grok_xstatereg (abfd, note); | |
9587 | else | |
9588 | return TRUE; | |
9589 | ||
97753bd5 AM |
9590 | case NT_PPC_VMX: |
9591 | if (note->namesz == 6 | |
9592 | && strcmp (note->namedata, "LINUX") == 0) | |
9593 | return elfcore_grok_ppc_vmx (abfd, note); | |
9594 | else | |
9595 | return TRUE; | |
9596 | ||
89eeb0bc LM |
9597 | case NT_PPC_VSX: |
9598 | if (note->namesz == 6 | |
9599 | && strcmp (note->namedata, "LINUX") == 0) | |
9600 | return elfcore_grok_ppc_vsx (abfd, note); | |
9601 | else | |
9602 | return TRUE; | |
9603 | ||
0675e188 UW |
9604 | case NT_S390_HIGH_GPRS: |
9605 | if (note->namesz == 6 | |
9606 | && strcmp (note->namedata, "LINUX") == 0) | |
9607 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9608 | else | |
9609 | return TRUE; | |
9610 | ||
d7eeb400 MS |
9611 | case NT_S390_TIMER: |
9612 | if (note->namesz == 6 | |
9613 | && strcmp (note->namedata, "LINUX") == 0) | |
9614 | return elfcore_grok_s390_timer (abfd, note); | |
9615 | else | |
9616 | return TRUE; | |
9617 | ||
9618 | case NT_S390_TODCMP: | |
9619 | if (note->namesz == 6 | |
9620 | && strcmp (note->namedata, "LINUX") == 0) | |
9621 | return elfcore_grok_s390_todcmp (abfd, note); | |
9622 | else | |
9623 | return TRUE; | |
9624 | ||
9625 | case NT_S390_TODPREG: | |
9626 | if (note->namesz == 6 | |
9627 | && strcmp (note->namedata, "LINUX") == 0) | |
9628 | return elfcore_grok_s390_todpreg (abfd, note); | |
9629 | else | |
9630 | return TRUE; | |
9631 | ||
9632 | case NT_S390_CTRS: | |
9633 | if (note->namesz == 6 | |
9634 | && strcmp (note->namedata, "LINUX") == 0) | |
9635 | return elfcore_grok_s390_ctrs (abfd, note); | |
9636 | else | |
9637 | return TRUE; | |
9638 | ||
9639 | case NT_S390_PREFIX: | |
9640 | if (note->namesz == 6 | |
9641 | && strcmp (note->namedata, "LINUX") == 0) | |
9642 | return elfcore_grok_s390_prefix (abfd, note); | |
9643 | else | |
9644 | return TRUE; | |
9645 | ||
355b81d9 UW |
9646 | case NT_S390_LAST_BREAK: |
9647 | if (note->namesz == 6 | |
9648 | && strcmp (note->namedata, "LINUX") == 0) | |
9649 | return elfcore_grok_s390_last_break (abfd, note); | |
9650 | else | |
9651 | return TRUE; | |
9652 | ||
9653 | case NT_S390_SYSTEM_CALL: | |
9654 | if (note->namesz == 6 | |
9655 | && strcmp (note->namedata, "LINUX") == 0) | |
9656 | return elfcore_grok_s390_system_call (abfd, note); | |
9657 | else | |
9658 | return TRUE; | |
9659 | ||
abb3f6cc NC |
9660 | case NT_S390_TDB: |
9661 | if (note->namesz == 6 | |
9662 | && strcmp (note->namedata, "LINUX") == 0) | |
9663 | return elfcore_grok_s390_tdb (abfd, note); | |
9664 | else | |
9665 | return TRUE; | |
9666 | ||
4ef9f41a AA |
9667 | case NT_S390_VXRS_LOW: |
9668 | if (note->namesz == 6 | |
9669 | && strcmp (note->namedata, "LINUX") == 0) | |
9670 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9671 | else | |
9672 | return TRUE; | |
9673 | ||
9674 | case NT_S390_VXRS_HIGH: | |
9675 | if (note->namesz == 6 | |
9676 | && strcmp (note->namedata, "LINUX") == 0) | |
9677 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9678 | else | |
9679 | return TRUE; | |
9680 | ||
88ab90e8 AA |
9681 | case NT_S390_GS_CB: |
9682 | if (note->namesz == 6 | |
9683 | && strcmp (note->namedata, "LINUX") == 0) | |
9684 | return elfcore_grok_s390_gs_bc (abfd, note); | |
9685 | else | |
9686 | return TRUE; | |
9687 | ||
9688 | case NT_S390_GS_BC: | |
9689 | if (note->namesz == 6 | |
9690 | && strcmp (note->namedata, "LINUX") == 0) | |
9691 | return elfcore_grok_s390_gs_cb (abfd, note); | |
9692 | else | |
9693 | return TRUE; | |
9694 | ||
faa9a424 UW |
9695 | case NT_ARM_VFP: |
9696 | if (note->namesz == 6 | |
9697 | && strcmp (note->namedata, "LINUX") == 0) | |
9698 | return elfcore_grok_arm_vfp (abfd, note); | |
9699 | else | |
9700 | return TRUE; | |
9701 | ||
652451f8 YZ |
9702 | case NT_ARM_TLS: |
9703 | if (note->namesz == 6 | |
9704 | && strcmp (note->namedata, "LINUX") == 0) | |
9705 | return elfcore_grok_aarch_tls (abfd, note); | |
9706 | else | |
9707 | return TRUE; | |
9708 | ||
9709 | case NT_ARM_HW_BREAK: | |
9710 | if (note->namesz == 6 | |
9711 | && strcmp (note->namedata, "LINUX") == 0) | |
9712 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9713 | else | |
9714 | return TRUE; | |
9715 | ||
9716 | case NT_ARM_HW_WATCH: | |
9717 | if (note->namesz == 6 | |
9718 | && strcmp (note->namedata, "LINUX") == 0) | |
9719 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9720 | else | |
9721 | return TRUE; | |
9722 | ||
252b5132 RH |
9723 | case NT_PRPSINFO: |
9724 | case NT_PSINFO: | |
bb0082d6 AM |
9725 | if (bed->elf_backend_grok_psinfo) |
9726 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9727 | return TRUE; |
bb0082d6 | 9728 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9729 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9730 | #else |
b34976b6 | 9731 | return TRUE; |
252b5132 | 9732 | #endif |
3333a7c3 RM |
9733 | |
9734 | case NT_AUXV: | |
9735 | { | |
117ed4f8 AM |
9736 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9737 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9738 | |
9739 | if (sect == NULL) | |
9740 | return FALSE; | |
eea6121a | 9741 | sect->size = note->descsz; |
3333a7c3 | 9742 | sect->filepos = note->descpos; |
3333a7c3 RM |
9743 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9744 | ||
9745 | return TRUE; | |
9746 | } | |
9015683b | 9747 | |
451b7c33 TT |
9748 | case NT_FILE: |
9749 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9750 | note); | |
9751 | ||
9015683b TT |
9752 | case NT_SIGINFO: |
9753 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9754 | note); | |
5b2c414d | 9755 | |
252b5132 RH |
9756 | } |
9757 | } | |
9758 | ||
718175fa JK |
9759 | static bfd_boolean |
9760 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9761 | { | |
c74f7d1c | 9762 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9763 | |
9764 | if (note->descsz == 0) | |
9765 | return FALSE; | |
9766 | ||
c74f7d1c JT |
9767 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9768 | if (build_id == NULL) | |
718175fa JK |
9769 | return FALSE; |
9770 | ||
c74f7d1c JT |
9771 | build_id->size = note->descsz; |
9772 | memcpy (build_id->data, note->descdata, note->descsz); | |
9773 | abfd->build_id = build_id; | |
718175fa JK |
9774 | |
9775 | return TRUE; | |
9776 | } | |
9777 | ||
9778 | static bfd_boolean | |
9779 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9780 | { | |
9781 | switch (note->type) | |
9782 | { | |
9783 | default: | |
9784 | return TRUE; | |
9785 | ||
46bed679 L |
9786 | case NT_GNU_PROPERTY_TYPE_0: |
9787 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
9788 | ||
718175fa JK |
9789 | case NT_GNU_BUILD_ID: |
9790 | return elfobj_grok_gnu_build_id (abfd, note); | |
9791 | } | |
9792 | } | |
9793 | ||
e21e5835 NC |
9794 | static bfd_boolean |
9795 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9796 | { | |
9797 | struct sdt_note *cur = | |
9798 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9799 | + note->descsz); | |
9800 | ||
9801 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9802 | cur->size = (bfd_size_type) note->descsz; | |
9803 | memcpy (cur->data, note->descdata, note->descsz); | |
9804 | ||
9805 | elf_tdata (abfd)->sdt_note_head = cur; | |
9806 | ||
9807 | return TRUE; | |
9808 | } | |
9809 | ||
9810 | static bfd_boolean | |
9811 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9812 | { | |
9813 | switch (note->type) | |
9814 | { | |
9815 | case NT_STAPSDT: | |
9816 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9817 | ||
9818 | default: | |
9819 | return TRUE; | |
9820 | } | |
9821 | } | |
9822 | ||
aa1ed4a9 JB |
9823 | static bfd_boolean |
9824 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
9825 | { | |
9826 | size_t offset; | |
9827 | ||
b5430a3c | 9828 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 9829 | { |
b5430a3c | 9830 | case ELFCLASS32: |
0064d223 JB |
9831 | if (note->descsz < 108) |
9832 | return FALSE; | |
aa1ed4a9 JB |
9833 | break; |
9834 | ||
b5430a3c | 9835 | case ELFCLASS64: |
0064d223 JB |
9836 | if (note->descsz < 120) |
9837 | return FALSE; | |
aa1ed4a9 JB |
9838 | break; |
9839 | ||
9840 | default: | |
9841 | return FALSE; | |
9842 | } | |
9843 | ||
0064d223 JB |
9844 | /* Check for version 1 in pr_version. */ |
9845 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9846 | return FALSE; | |
9847 | offset = 4; | |
9848 | ||
9849 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 9850 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
9851 | offset += 4; |
9852 | else | |
9853 | { | |
9854 | offset += 4; /* Padding before pr_psinfosz. */ | |
9855 | offset += 8; | |
9856 | } | |
9857 | ||
aa1ed4a9 JB |
9858 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
9859 | elf_tdata (abfd)->core->program | |
9860 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
9861 | offset += 17; | |
9862 | ||
9863 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
9864 | elf_tdata (abfd)->core->command | |
9865 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
9866 | offset += 81; |
9867 | ||
9868 | /* Padding before pr_pid. */ | |
9869 | offset += 2; | |
9870 | ||
9871 | /* The pr_pid field was added in version "1a". */ | |
9872 | if (note->descsz < offset + 4) | |
9873 | return TRUE; | |
9874 | ||
9875 | elf_tdata (abfd)->core->pid | |
9876 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
9877 | |
9878 | return TRUE; | |
9879 | } | |
9880 | ||
9881 | static bfd_boolean | |
9882 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
9883 | { | |
9884 | size_t offset; | |
9885 | size_t size; | |
9886 | ||
9887 | /* Check for version 1 in pr_version. */ | |
9888 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9889 | return FALSE; | |
9890 | offset = 4; | |
9891 | ||
9892 | /* Skip over pr_statussz. */ | |
b5430a3c | 9893 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 9894 | { |
b5430a3c | 9895 | case ELFCLASS32: |
aa1ed4a9 JB |
9896 | offset += 4; |
9897 | break; | |
9898 | ||
b5430a3c | 9899 | case ELFCLASS64: |
aa1ed4a9 JB |
9900 | offset += 4; /* Padding before pr_statussz. */ |
9901 | offset += 8; | |
9902 | break; | |
9903 | ||
9904 | default: | |
9905 | return FALSE; | |
9906 | } | |
9907 | ||
9908 | /* Extract size of pr_reg from pr_gregsetsz. */ | |
b5430a3c | 9909 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
aa1ed4a9 JB |
9910 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); |
9911 | else | |
9912 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
9913 | ||
9914 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c JB |
9915 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
9916 | offset += 4 * 2; | |
9917 | else | |
9918 | offset += 8 * 2; | |
aa1ed4a9 JB |
9919 | |
9920 | /* Skip over pr_osreldate. */ | |
9921 | offset += 4; | |
9922 | ||
9923 | /* Read signal from pr_cursig. */ | |
9924 | if (elf_tdata (abfd)->core->signal == 0) | |
9925 | elf_tdata (abfd)->core->signal | |
9926 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9927 | offset += 4; | |
9928 | ||
9929 | /* Read TID from pr_pid. */ | |
9930 | elf_tdata (abfd)->core->lwpid | |
9931 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9932 | offset += 4; | |
9933 | ||
9934 | /* Padding before pr_reg. */ | |
b5430a3c | 9935 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
9936 | offset += 4; |
9937 | ||
9938 | /* Make a ".reg/999" section and a ".reg" section. */ | |
9939 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
9940 | size, note->descpos + offset); | |
9941 | } | |
9942 | ||
9943 | static bfd_boolean | |
9944 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9945 | { | |
9946 | switch (note->type) | |
9947 | { | |
9948 | case NT_PRSTATUS: | |
9949 | return elfcore_grok_freebsd_prstatus (abfd, note); | |
9950 | ||
9951 | case NT_FPREGSET: | |
9952 | return elfcore_grok_prfpreg (abfd, note); | |
9953 | ||
9954 | case NT_PRPSINFO: | |
9955 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
9956 | ||
9957 | case NT_FREEBSD_THRMISC: | |
9958 | if (note->namesz == 8) | |
9959 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
9960 | else | |
9961 | return TRUE; | |
9962 | ||
3350c5f5 JB |
9963 | case NT_FREEBSD_PROCSTAT_AUXV: |
9964 | { | |
9965 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9966 | SEC_HAS_CONTENTS); | |
9967 | ||
9968 | if (sect == NULL) | |
9969 | return FALSE; | |
9970 | sect->size = note->descsz - 4; | |
9971 | sect->filepos = note->descpos + 4; | |
9972 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
9973 | ||
9974 | return TRUE; | |
9975 | } | |
9976 | ||
aa1ed4a9 JB |
9977 | case NT_X86_XSTATE: |
9978 | if (note->namesz == 8) | |
9979 | return elfcore_grok_xstatereg (abfd, note); | |
9980 | else | |
9981 | return TRUE; | |
9982 | ||
e6f3b9c3 JB |
9983 | case NT_FREEBSD_PTLWPINFO: |
9984 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
9985 | note); | |
9986 | ||
aa1ed4a9 JB |
9987 | default: |
9988 | return TRUE; | |
9989 | } | |
9990 | } | |
9991 | ||
b34976b6 | 9992 | static bfd_boolean |
217aa764 | 9993 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
9994 | { |
9995 | char *cp; | |
9996 | ||
9997 | cp = strchr (note->namedata, '@'); | |
9998 | if (cp != NULL) | |
9999 | { | |
d2b64500 | 10000 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10001 | return TRUE; |
50b2bdb7 | 10002 | } |
b34976b6 | 10003 | return FALSE; |
50b2bdb7 AM |
10004 | } |
10005 | ||
b34976b6 | 10006 | static bfd_boolean |
217aa764 | 10007 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10008 | { |
50b2bdb7 | 10009 | /* Signal number at offset 0x08. */ |
228e534f | 10010 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10011 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10012 | ||
10013 | /* Process ID at offset 0x50. */ | |
228e534f | 10014 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10015 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10016 | ||
10017 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10018 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10019 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10020 | ||
7720ba9f MK |
10021 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10022 | note); | |
50b2bdb7 AM |
10023 | } |
10024 | ||
b34976b6 | 10025 | static bfd_boolean |
217aa764 | 10026 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10027 | { |
10028 | int lwp; | |
10029 | ||
10030 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10031 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10032 | |
b4db1224 | 10033 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
10034 | { |
10035 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
10036 | find this note before any of the others, which is fine, |
10037 | since the kernel writes this note out first when it | |
10038 | creates a core file. */ | |
47d9a591 | 10039 | |
50b2bdb7 AM |
10040 | return elfcore_grok_netbsd_procinfo (abfd, note); |
10041 | } | |
10042 | ||
b4db1224 JT |
10043 | /* As of Jan 2002 there are no other machine-independent notes |
10044 | defined for NetBSD core files. If the note type is less | |
10045 | than the start of the machine-dependent note types, we don't | |
10046 | understand it. */ | |
47d9a591 | 10047 | |
b4db1224 | 10048 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10049 | return TRUE; |
50b2bdb7 AM |
10050 | |
10051 | ||
10052 | switch (bfd_get_arch (abfd)) | |
10053 | { | |
08a40648 AM |
10054 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10055 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
10056 | |
10057 | case bfd_arch_alpha: | |
10058 | case bfd_arch_sparc: | |
10059 | switch (note->type) | |
08a40648 AM |
10060 | { |
10061 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10062 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10063 | |
08a40648 AM |
10064 | case NT_NETBSDCORE_FIRSTMACH+2: |
10065 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10066 | |
08a40648 AM |
10067 | default: |
10068 | return TRUE; | |
10069 | } | |
50b2bdb7 | 10070 | |
08a40648 AM |
10071 | /* On all other arch's, PT_GETREGS == mach+1 and |
10072 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10073 | |
10074 | default: | |
10075 | switch (note->type) | |
08a40648 AM |
10076 | { |
10077 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10078 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10079 | |
08a40648 AM |
10080 | case NT_NETBSDCORE_FIRSTMACH+3: |
10081 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10082 | |
08a40648 AM |
10083 | default: |
10084 | return TRUE; | |
10085 | } | |
50b2bdb7 AM |
10086 | } |
10087 | /* NOTREACHED */ | |
10088 | } | |
10089 | ||
67cc5033 MK |
10090 | static bfd_boolean |
10091 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10092 | { | |
10093 | /* Signal number at offset 0x08. */ | |
228e534f | 10094 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10095 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10096 | ||
10097 | /* Process ID at offset 0x20. */ | |
228e534f | 10098 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10099 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10100 | ||
10101 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10102 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10103 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10104 | ||
10105 | return TRUE; | |
10106 | } | |
10107 | ||
10108 | static bfd_boolean | |
10109 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10110 | { | |
10111 | if (note->type == NT_OPENBSD_PROCINFO) | |
10112 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10113 | ||
10114 | if (note->type == NT_OPENBSD_REGS) | |
10115 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10116 | ||
10117 | if (note->type == NT_OPENBSD_FPREGS) | |
10118 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10119 | ||
10120 | if (note->type == NT_OPENBSD_XFPREGS) | |
10121 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10122 | ||
10123 | if (note->type == NT_OPENBSD_AUXV) | |
10124 | { | |
10125 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10126 | SEC_HAS_CONTENTS); | |
10127 | ||
10128 | if (sect == NULL) | |
10129 | return FALSE; | |
10130 | sect->size = note->descsz; | |
10131 | sect->filepos = note->descpos; | |
10132 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10133 | ||
10134 | return TRUE; | |
10135 | } | |
10136 | ||
10137 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10138 | { | |
10139 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10140 | SEC_HAS_CONTENTS); | |
10141 | ||
10142 | if (sect == NULL) | |
10143 | return FALSE; | |
10144 | sect->size = note->descsz; | |
10145 | sect->filepos = note->descpos; | |
10146 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10147 | ||
10148 | return TRUE; | |
10149 | } | |
10150 | ||
10151 | return TRUE; | |
10152 | } | |
10153 | ||
07c6e936 | 10154 | static bfd_boolean |
d3fd4074 | 10155 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10156 | { |
10157 | void *ddata = note->descdata; | |
10158 | char buf[100]; | |
10159 | char *name; | |
10160 | asection *sect; | |
f8843e87 AM |
10161 | short sig; |
10162 | unsigned flags; | |
07c6e936 NC |
10163 | |
10164 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 10165 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10166 | |
f8843e87 AM |
10167 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10168 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10169 | ||
10170 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10171 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10172 | |
10173 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10174 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10175 | { | |
228e534f AM |
10176 | elf_tdata (abfd)->core->signal = sig; |
10177 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10178 | } |
07c6e936 | 10179 | |
f8843e87 AM |
10180 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10181 | do not come from signals so we make sure we set the current | |
10182 | thread just in case. */ | |
10183 | if (flags & 0x00000080) | |
228e534f | 10184 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10185 | |
10186 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10187 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10188 | |
a50b1753 | 10189 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10190 | if (name == NULL) |
10191 | return FALSE; | |
10192 | strcpy (name, buf); | |
10193 | ||
117ed4f8 | 10194 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10195 | if (sect == NULL) |
10196 | return FALSE; | |
10197 | ||
eea6121a | 10198 | sect->size = note->descsz; |
07c6e936 | 10199 | sect->filepos = note->descpos; |
07c6e936 NC |
10200 | sect->alignment_power = 2; |
10201 | ||
10202 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10203 | } | |
10204 | ||
10205 | static bfd_boolean | |
d69f560c KW |
10206 | elfcore_grok_nto_regs (bfd *abfd, |
10207 | Elf_Internal_Note *note, | |
d3fd4074 | 10208 | long tid, |
d69f560c | 10209 | char *base) |
07c6e936 NC |
10210 | { |
10211 | char buf[100]; | |
10212 | char *name; | |
10213 | asection *sect; | |
10214 | ||
d69f560c | 10215 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10216 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10217 | |
a50b1753 | 10218 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10219 | if (name == NULL) |
10220 | return FALSE; | |
10221 | strcpy (name, buf); | |
10222 | ||
117ed4f8 | 10223 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10224 | if (sect == NULL) |
10225 | return FALSE; | |
10226 | ||
eea6121a | 10227 | sect->size = note->descsz; |
07c6e936 | 10228 | sect->filepos = note->descpos; |
07c6e936 NC |
10229 | sect->alignment_power = 2; |
10230 | ||
f8843e87 | 10231 | /* This is the current thread. */ |
228e534f | 10232 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10233 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10234 | |
10235 | return TRUE; | |
07c6e936 NC |
10236 | } |
10237 | ||
10238 | #define BFD_QNT_CORE_INFO 7 | |
10239 | #define BFD_QNT_CORE_STATUS 8 | |
10240 | #define BFD_QNT_CORE_GREG 9 | |
10241 | #define BFD_QNT_CORE_FPREG 10 | |
10242 | ||
10243 | static bfd_boolean | |
217aa764 | 10244 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10245 | { |
10246 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10247 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10248 | function. */ |
d3fd4074 | 10249 | static long tid = 1; |
07c6e936 NC |
10250 | |
10251 | switch (note->type) | |
10252 | { | |
d69f560c KW |
10253 | case BFD_QNT_CORE_INFO: |
10254 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10255 | case BFD_QNT_CORE_STATUS: | |
10256 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10257 | case BFD_QNT_CORE_GREG: | |
10258 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10259 | case BFD_QNT_CORE_FPREG: | |
10260 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10261 | default: | |
10262 | return TRUE; | |
07c6e936 NC |
10263 | } |
10264 | } | |
10265 | ||
b15fa79e AM |
10266 | static bfd_boolean |
10267 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10268 | { | |
10269 | char *name; | |
10270 | asection *sect; | |
10271 | size_t len; | |
10272 | ||
10273 | /* Use note name as section name. */ | |
10274 | len = note->namesz; | |
a50b1753 | 10275 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10276 | if (name == NULL) |
10277 | return FALSE; | |
10278 | memcpy (name, note->namedata, len); | |
10279 | name[len - 1] = '\0'; | |
10280 | ||
10281 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10282 | if (sect == NULL) | |
10283 | return FALSE; | |
10284 | ||
10285 | sect->size = note->descsz; | |
10286 | sect->filepos = note->descpos; | |
10287 | sect->alignment_power = 1; | |
10288 | ||
10289 | return TRUE; | |
10290 | } | |
10291 | ||
7c76fa91 MS |
10292 | /* Function: elfcore_write_note |
10293 | ||
47d9a591 | 10294 | Inputs: |
a39f3346 | 10295 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10296 | name of note |
10297 | type of note | |
10298 | data for note | |
10299 | size of data for note | |
10300 | ||
a39f3346 AM |
10301 | Writes note to end of buffer. ELF64 notes are written exactly as |
10302 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10303 | that they ought to have 8-byte namesz and descsz field, and have | |
10304 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10305 | ||
7c76fa91 | 10306 | Return: |
a39f3346 | 10307 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10308 | |
10309 | char * | |
a39f3346 | 10310 | elfcore_write_note (bfd *abfd, |
217aa764 | 10311 | char *buf, |
a39f3346 | 10312 | int *bufsiz, |
217aa764 | 10313 | const char *name, |
a39f3346 | 10314 | int type, |
217aa764 | 10315 | const void *input, |
a39f3346 | 10316 | int size) |
7c76fa91 MS |
10317 | { |
10318 | Elf_External_Note *xnp; | |
d4c88bbb | 10319 | size_t namesz; |
d4c88bbb | 10320 | size_t newspace; |
a39f3346 | 10321 | char *dest; |
7c76fa91 | 10322 | |
d4c88bbb | 10323 | namesz = 0; |
d4c88bbb | 10324 | if (name != NULL) |
a39f3346 | 10325 | namesz = strlen (name) + 1; |
d4c88bbb | 10326 | |
a39f3346 | 10327 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10328 | |
a50b1753 | 10329 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10330 | if (buf == NULL) |
10331 | return buf; | |
a39f3346 | 10332 | dest = buf + *bufsiz; |
7c76fa91 MS |
10333 | *bufsiz += newspace; |
10334 | xnp = (Elf_External_Note *) dest; | |
10335 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10336 | H_PUT_32 (abfd, size, xnp->descsz); | |
10337 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10338 | dest = xnp->name; |
10339 | if (name != NULL) | |
10340 | { | |
10341 | memcpy (dest, name, namesz); | |
10342 | dest += namesz; | |
a39f3346 | 10343 | while (namesz & 3) |
d4c88bbb AM |
10344 | { |
10345 | *dest++ = '\0'; | |
a39f3346 | 10346 | ++namesz; |
d4c88bbb AM |
10347 | } |
10348 | } | |
10349 | memcpy (dest, input, size); | |
a39f3346 AM |
10350 | dest += size; |
10351 | while (size & 3) | |
10352 | { | |
10353 | *dest++ = '\0'; | |
10354 | ++size; | |
10355 | } | |
10356 | return buf; | |
7c76fa91 MS |
10357 | } |
10358 | ||
7c76fa91 | 10359 | char * |
217aa764 AM |
10360 | elfcore_write_prpsinfo (bfd *abfd, |
10361 | char *buf, | |
10362 | int *bufsiz, | |
10363 | const char *fname, | |
10364 | const char *psargs) | |
7c76fa91 | 10365 | { |
183e98be AM |
10366 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10367 | ||
10368 | if (bed->elf_backend_write_core_note != NULL) | |
10369 | { | |
10370 | char *ret; | |
10371 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10372 | NT_PRPSINFO, fname, psargs); | |
10373 | if (ret != NULL) | |
10374 | return ret; | |
10375 | } | |
7c76fa91 | 10376 | |
1f20dca5 | 10377 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
10378 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
10379 | if (bed->s->elfclass == ELFCLASS32) | |
10380 | { | |
10381 | #if defined (HAVE_PSINFO32_T) | |
10382 | psinfo32_t data; | |
10383 | int note_type = NT_PSINFO; | |
10384 | #else | |
10385 | prpsinfo32_t data; | |
10386 | int note_type = NT_PRPSINFO; | |
10387 | #endif | |
10388 | ||
10389 | memset (&data, 0, sizeof (data)); | |
10390 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10391 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10392 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10393 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
10394 | } |
10395 | else | |
10396 | #endif | |
10397 | { | |
7c76fa91 | 10398 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
10399 | psinfo_t data; |
10400 | int note_type = NT_PSINFO; | |
7c76fa91 | 10401 | #else |
183e98be AM |
10402 | prpsinfo_t data; |
10403 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
10404 | #endif |
10405 | ||
183e98be AM |
10406 | memset (&data, 0, sizeof (data)); |
10407 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10408 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10409 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10410 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 10411 | } |
7c76fa91 MS |
10412 | #endif /* PSINFO_T or PRPSINFO_T */ |
10413 | ||
1f20dca5 UW |
10414 | free (buf); |
10415 | return NULL; | |
10416 | } | |
10417 | ||
70a38d42 SDJ |
10418 | char * |
10419 | elfcore_write_linux_prpsinfo32 | |
10420 | (bfd *abfd, char *buf, int *bufsiz, | |
10421 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10422 | { | |
10423 | struct elf_external_linux_prpsinfo32 data; | |
10424 | ||
aeb70569 | 10425 | swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10426 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, |
10427 | &data, sizeof (data)); | |
10428 | } | |
10429 | ||
10430 | char * | |
10431 | elfcore_write_linux_prpsinfo64 | |
10432 | (bfd *abfd, char *buf, int *bufsiz, | |
10433 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10434 | { | |
10435 | struct elf_external_linux_prpsinfo64 data; | |
10436 | ||
aeb70569 | 10437 | swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10438 | return elfcore_write_note (abfd, buf, bufsiz, |
10439 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10440 | } | |
10441 | ||
7c76fa91 | 10442 | char * |
217aa764 AM |
10443 | elfcore_write_prstatus (bfd *abfd, |
10444 | char *buf, | |
10445 | int *bufsiz, | |
10446 | long pid, | |
10447 | int cursig, | |
10448 | const void *gregs) | |
7c76fa91 | 10449 | { |
183e98be | 10450 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10451 | |
183e98be AM |
10452 | if (bed->elf_backend_write_core_note != NULL) |
10453 | { | |
10454 | char *ret; | |
10455 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10456 | NT_PRSTATUS, | |
10457 | pid, cursig, gregs); | |
10458 | if (ret != NULL) | |
10459 | return ret; | |
10460 | } | |
10461 | ||
1f20dca5 | 10462 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10463 | #if defined (HAVE_PRSTATUS32_T) |
10464 | if (bed->s->elfclass == ELFCLASS32) | |
10465 | { | |
10466 | prstatus32_t prstat; | |
10467 | ||
10468 | memset (&prstat, 0, sizeof (prstat)); | |
10469 | prstat.pr_pid = pid; | |
10470 | prstat.pr_cursig = cursig; | |
10471 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10472 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10473 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10474 | } | |
10475 | else | |
10476 | #endif | |
10477 | { | |
10478 | prstatus_t prstat; | |
10479 | ||
10480 | memset (&prstat, 0, sizeof (prstat)); | |
10481 | prstat.pr_pid = pid; | |
10482 | prstat.pr_cursig = cursig; | |
10483 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10484 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10485 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10486 | } | |
7c76fa91 MS |
10487 | #endif /* HAVE_PRSTATUS_T */ |
10488 | ||
1f20dca5 UW |
10489 | free (buf); |
10490 | return NULL; | |
10491 | } | |
10492 | ||
51316059 MS |
10493 | #if defined (HAVE_LWPSTATUS_T) |
10494 | char * | |
217aa764 AM |
10495 | elfcore_write_lwpstatus (bfd *abfd, |
10496 | char *buf, | |
10497 | int *bufsiz, | |
10498 | long pid, | |
10499 | int cursig, | |
10500 | const void *gregs) | |
51316059 MS |
10501 | { |
10502 | lwpstatus_t lwpstat; | |
183e98be | 10503 | const char *note_name = "CORE"; |
51316059 MS |
10504 | |
10505 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10506 | lwpstat.pr_lwpid = pid >> 16; | |
10507 | lwpstat.pr_cursig = cursig; | |
10508 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10509 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10510 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10511 | #if !defined(gregs) | |
10512 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10513 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10514 | #else | |
10515 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10516 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10517 | #endif | |
10518 | #endif | |
47d9a591 | 10519 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10520 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10521 | } | |
10522 | #endif /* HAVE_LWPSTATUS_T */ | |
10523 | ||
7c76fa91 MS |
10524 | #if defined (HAVE_PSTATUS_T) |
10525 | char * | |
217aa764 AM |
10526 | elfcore_write_pstatus (bfd *abfd, |
10527 | char *buf, | |
10528 | int *bufsiz, | |
10529 | long pid, | |
6c10990d NC |
10530 | int cursig ATTRIBUTE_UNUSED, |
10531 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10532 | { |
183e98be AM |
10533 | const char *note_name = "CORE"; |
10534 | #if defined (HAVE_PSTATUS32_T) | |
10535 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10536 | |
183e98be AM |
10537 | if (bed->s->elfclass == ELFCLASS32) |
10538 | { | |
10539 | pstatus32_t pstat; | |
10540 | ||
10541 | memset (&pstat, 0, sizeof (pstat)); | |
10542 | pstat.pr_pid = pid & 0xffff; | |
10543 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10544 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10545 | return buf; | |
10546 | } | |
10547 | else | |
10548 | #endif | |
10549 | { | |
10550 | pstatus_t pstat; | |
10551 | ||
10552 | memset (&pstat, 0, sizeof (pstat)); | |
10553 | pstat.pr_pid = pid & 0xffff; | |
10554 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10555 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10556 | return buf; | |
10557 | } | |
7c76fa91 MS |
10558 | } |
10559 | #endif /* HAVE_PSTATUS_T */ | |
10560 | ||
10561 | char * | |
217aa764 AM |
10562 | elfcore_write_prfpreg (bfd *abfd, |
10563 | char *buf, | |
10564 | int *bufsiz, | |
10565 | const void *fpregs, | |
10566 | int size) | |
7c76fa91 | 10567 | { |
183e98be | 10568 | const char *note_name = "CORE"; |
47d9a591 | 10569 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10570 | note_name, NT_FPREGSET, fpregs, size); |
10571 | } | |
10572 | ||
10573 | char * | |
217aa764 AM |
10574 | elfcore_write_prxfpreg (bfd *abfd, |
10575 | char *buf, | |
10576 | int *bufsiz, | |
10577 | const void *xfpregs, | |
10578 | int size) | |
7c76fa91 MS |
10579 | { |
10580 | char *note_name = "LINUX"; | |
47d9a591 | 10581 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10582 | note_name, NT_PRXFPREG, xfpregs, size); |
10583 | } | |
10584 | ||
4339cae0 L |
10585 | char * |
10586 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10587 | const void *xfpregs, int size) | |
10588 | { | |
97de3545 JB |
10589 | char *note_name; |
10590 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10591 | note_name = "FreeBSD"; | |
10592 | else | |
10593 | note_name = "LINUX"; | |
4339cae0 L |
10594 | return elfcore_write_note (abfd, buf, bufsiz, |
10595 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10596 | } | |
10597 | ||
97753bd5 AM |
10598 | char * |
10599 | elfcore_write_ppc_vmx (bfd *abfd, | |
10600 | char *buf, | |
10601 | int *bufsiz, | |
10602 | const void *ppc_vmx, | |
10603 | int size) | |
10604 | { | |
10605 | char *note_name = "LINUX"; | |
10606 | return elfcore_write_note (abfd, buf, bufsiz, | |
10607 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10608 | } | |
10609 | ||
89eeb0bc LM |
10610 | char * |
10611 | elfcore_write_ppc_vsx (bfd *abfd, | |
10612 | char *buf, | |
10613 | int *bufsiz, | |
10614 | const void *ppc_vsx, | |
10615 | int size) | |
10616 | { | |
10617 | char *note_name = "LINUX"; | |
10618 | return elfcore_write_note (abfd, buf, bufsiz, | |
10619 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
10620 | } | |
10621 | ||
0675e188 UW |
10622 | static char * |
10623 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10624 | char *buf, | |
10625 | int *bufsiz, | |
10626 | const void *s390_high_gprs, | |
10627 | int size) | |
10628 | { | |
10629 | char *note_name = "LINUX"; | |
10630 | return elfcore_write_note (abfd, buf, bufsiz, | |
10631 | note_name, NT_S390_HIGH_GPRS, | |
10632 | s390_high_gprs, size); | |
10633 | } | |
10634 | ||
d7eeb400 MS |
10635 | char * |
10636 | elfcore_write_s390_timer (bfd *abfd, | |
10637 | char *buf, | |
10638 | int *bufsiz, | |
10639 | const void *s390_timer, | |
10640 | int size) | |
10641 | { | |
10642 | char *note_name = "LINUX"; | |
10643 | return elfcore_write_note (abfd, buf, bufsiz, | |
10644 | note_name, NT_S390_TIMER, s390_timer, size); | |
10645 | } | |
10646 | ||
10647 | char * | |
10648 | elfcore_write_s390_todcmp (bfd *abfd, | |
10649 | char *buf, | |
10650 | int *bufsiz, | |
10651 | const void *s390_todcmp, | |
10652 | int size) | |
10653 | { | |
10654 | char *note_name = "LINUX"; | |
10655 | return elfcore_write_note (abfd, buf, bufsiz, | |
10656 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10657 | } | |
10658 | ||
10659 | char * | |
10660 | elfcore_write_s390_todpreg (bfd *abfd, | |
10661 | char *buf, | |
10662 | int *bufsiz, | |
10663 | const void *s390_todpreg, | |
10664 | int size) | |
10665 | { | |
10666 | char *note_name = "LINUX"; | |
10667 | return elfcore_write_note (abfd, buf, bufsiz, | |
10668 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10669 | } | |
10670 | ||
10671 | char * | |
10672 | elfcore_write_s390_ctrs (bfd *abfd, | |
10673 | char *buf, | |
10674 | int *bufsiz, | |
10675 | const void *s390_ctrs, | |
10676 | int size) | |
10677 | { | |
10678 | char *note_name = "LINUX"; | |
10679 | return elfcore_write_note (abfd, buf, bufsiz, | |
10680 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10681 | } | |
10682 | ||
10683 | char * | |
10684 | elfcore_write_s390_prefix (bfd *abfd, | |
10685 | char *buf, | |
10686 | int *bufsiz, | |
10687 | const void *s390_prefix, | |
10688 | int size) | |
10689 | { | |
10690 | char *note_name = "LINUX"; | |
10691 | return elfcore_write_note (abfd, buf, bufsiz, | |
10692 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10693 | } | |
10694 | ||
355b81d9 UW |
10695 | char * |
10696 | elfcore_write_s390_last_break (bfd *abfd, | |
10697 | char *buf, | |
10698 | int *bufsiz, | |
10699 | const void *s390_last_break, | |
10700 | int size) | |
10701 | { | |
10702 | char *note_name = "LINUX"; | |
10703 | return elfcore_write_note (abfd, buf, bufsiz, | |
10704 | note_name, NT_S390_LAST_BREAK, | |
10705 | s390_last_break, size); | |
10706 | } | |
10707 | ||
10708 | char * | |
10709 | elfcore_write_s390_system_call (bfd *abfd, | |
10710 | char *buf, | |
10711 | int *bufsiz, | |
10712 | const void *s390_system_call, | |
10713 | int size) | |
10714 | { | |
10715 | char *note_name = "LINUX"; | |
10716 | return elfcore_write_note (abfd, buf, bufsiz, | |
10717 | note_name, NT_S390_SYSTEM_CALL, | |
10718 | s390_system_call, size); | |
10719 | } | |
10720 | ||
abb3f6cc NC |
10721 | char * |
10722 | elfcore_write_s390_tdb (bfd *abfd, | |
10723 | char *buf, | |
10724 | int *bufsiz, | |
10725 | const void *s390_tdb, | |
10726 | int size) | |
10727 | { | |
10728 | char *note_name = "LINUX"; | |
10729 | return elfcore_write_note (abfd, buf, bufsiz, | |
10730 | note_name, NT_S390_TDB, s390_tdb, size); | |
10731 | } | |
10732 | ||
4ef9f41a AA |
10733 | char * |
10734 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10735 | char *buf, | |
10736 | int *bufsiz, | |
10737 | const void *s390_vxrs_low, | |
10738 | int size) | |
10739 | { | |
10740 | char *note_name = "LINUX"; | |
10741 | return elfcore_write_note (abfd, buf, bufsiz, | |
10742 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10743 | } | |
10744 | ||
10745 | char * | |
10746 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10747 | char *buf, | |
10748 | int *bufsiz, | |
10749 | const void *s390_vxrs_high, | |
10750 | int size) | |
10751 | { | |
10752 | char *note_name = "LINUX"; | |
10753 | return elfcore_write_note (abfd, buf, bufsiz, | |
10754 | note_name, NT_S390_VXRS_HIGH, | |
10755 | s390_vxrs_high, size); | |
10756 | } | |
10757 | ||
88ab90e8 AA |
10758 | char * |
10759 | elfcore_write_s390_gs_cb (bfd *abfd, | |
10760 | char *buf, | |
10761 | int *bufsiz, | |
10762 | const void *s390_gs_cb, | |
10763 | int size) | |
10764 | { | |
10765 | char *note_name = "LINUX"; | |
10766 | return elfcore_write_note (abfd, buf, bufsiz, | |
10767 | note_name, NT_S390_GS_CB, | |
10768 | s390_gs_cb, size); | |
10769 | } | |
10770 | ||
10771 | char * | |
10772 | elfcore_write_s390_gs_bc (bfd *abfd, | |
10773 | char *buf, | |
10774 | int *bufsiz, | |
10775 | const void *s390_gs_bc, | |
10776 | int size) | |
10777 | { | |
10778 | char *note_name = "LINUX"; | |
10779 | return elfcore_write_note (abfd, buf, bufsiz, | |
10780 | note_name, NT_S390_GS_BC, | |
10781 | s390_gs_bc, size); | |
10782 | } | |
10783 | ||
faa9a424 UW |
10784 | char * |
10785 | elfcore_write_arm_vfp (bfd *abfd, | |
10786 | char *buf, | |
10787 | int *bufsiz, | |
10788 | const void *arm_vfp, | |
10789 | int size) | |
10790 | { | |
10791 | char *note_name = "LINUX"; | |
10792 | return elfcore_write_note (abfd, buf, bufsiz, | |
10793 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10794 | } | |
10795 | ||
652451f8 YZ |
10796 | char * |
10797 | elfcore_write_aarch_tls (bfd *abfd, | |
10798 | char *buf, | |
10799 | int *bufsiz, | |
10800 | const void *aarch_tls, | |
10801 | int size) | |
10802 | { | |
10803 | char *note_name = "LINUX"; | |
10804 | return elfcore_write_note (abfd, buf, bufsiz, | |
10805 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10806 | } | |
10807 | ||
10808 | char * | |
10809 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10810 | char *buf, | |
10811 | int *bufsiz, | |
10812 | const void *aarch_hw_break, | |
10813 | int size) | |
10814 | { | |
10815 | char *note_name = "LINUX"; | |
10816 | return elfcore_write_note (abfd, buf, bufsiz, | |
10817 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10818 | } | |
10819 | ||
10820 | char * | |
10821 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10822 | char *buf, | |
10823 | int *bufsiz, | |
10824 | const void *aarch_hw_watch, | |
10825 | int size) | |
10826 | { | |
10827 | char *note_name = "LINUX"; | |
10828 | return elfcore_write_note (abfd, buf, bufsiz, | |
10829 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10830 | } | |
10831 | ||
bb864ac1 CES |
10832 | char * |
10833 | elfcore_write_register_note (bfd *abfd, | |
10834 | char *buf, | |
10835 | int *bufsiz, | |
10836 | const char *section, | |
10837 | const void *data, | |
10838 | int size) | |
10839 | { | |
10840 | if (strcmp (section, ".reg2") == 0) | |
10841 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10842 | if (strcmp (section, ".reg-xfp") == 0) | |
10843 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10844 | if (strcmp (section, ".reg-xstate") == 0) |
10845 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10846 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10847 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10848 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10849 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10850 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10851 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10852 | if (strcmp (section, ".reg-s390-timer") == 0) |
10853 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10854 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10855 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10856 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10857 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10858 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10859 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10860 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10861 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10862 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10863 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10864 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10865 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10866 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10867 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10868 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10869 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10870 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10871 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
10872 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
10873 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
10874 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
10875 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10876 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10877 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10878 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10879 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10880 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10881 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10882 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10883 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10884 | return NULL; |
10885 | } | |
10886 | ||
b34976b6 | 10887 | static bfd_boolean |
718175fa | 10888 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10889 | { |
c044fabd | 10890 | char *p; |
252b5132 | 10891 | |
252b5132 RH |
10892 | p = buf; |
10893 | while (p < buf + size) | |
10894 | { | |
c044fabd KH |
10895 | /* FIXME: bad alignment assumption. */ |
10896 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10897 | Elf_Internal_Note in; |
10898 | ||
baea7ef1 AM |
10899 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10900 | return FALSE; | |
10901 | ||
dc810e39 | 10902 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10903 | |
dc810e39 | 10904 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10905 | in.namedata = xnp->name; |
baea7ef1 AM |
10906 | if (in.namesz > buf - in.namedata + size) |
10907 | return FALSE; | |
252b5132 | 10908 | |
dc810e39 | 10909 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10910 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10911 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10912 | if (in.descsz != 0 |
10913 | && (in.descdata >= buf + size | |
10914 | || in.descsz > buf - in.descdata + size)) | |
10915 | return FALSE; | |
252b5132 | 10916 | |
718175fa JK |
10917 | switch (bfd_get_format (abfd)) |
10918 | { | |
10919 | default: | |
10920 | return TRUE; | |
10921 | ||
10922 | case bfd_core: | |
f64e188b | 10923 | { |
8acbedd6 | 10924 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10925 | struct |
718175fa | 10926 | { |
f64e188b | 10927 | const char * string; |
8acbedd6 | 10928 | size_t len; |
f64e188b | 10929 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10930 | } |
f64e188b | 10931 | grokers[] = |
b15fa79e | 10932 | { |
8acbedd6 | 10933 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 10934 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
10935 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
10936 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10937 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10938 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10939 | }; |
8acbedd6 | 10940 | #undef GROKER_ELEMENT |
f64e188b NC |
10941 | int i; |
10942 | ||
10943 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10944 | { |
10945 | if (in.namesz >= grokers[i].len | |
10946 | && strncmp (in.namedata, grokers[i].string, | |
10947 | grokers[i].len) == 0) | |
10948 | { | |
10949 | if (! grokers[i].func (abfd, & in)) | |
10950 | return FALSE; | |
10951 | break; | |
10952 | } | |
10953 | } | |
f64e188b NC |
10954 | break; |
10955 | } | |
718175fa JK |
10956 | |
10957 | case bfd_object: | |
10958 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10959 | { | |
10960 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10961 | return FALSE; | |
10962 | } | |
e21e5835 NC |
10963 | else if (in.namesz == sizeof "stapsdt" |
10964 | && strcmp (in.namedata, "stapsdt") == 0) | |
10965 | { | |
10966 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10967 | return FALSE; | |
10968 | } | |
718175fa | 10969 | break; |
08a40648 | 10970 | } |
252b5132 RH |
10971 | |
10972 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
10973 | } | |
10974 | ||
718175fa JK |
10975 | return TRUE; |
10976 | } | |
10977 | ||
10978 | static bfd_boolean | |
10979 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
10980 | { | |
10981 | char *buf; | |
10982 | ||
10983 | if (size <= 0) | |
10984 | return TRUE; | |
10985 | ||
10986 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
10987 | return FALSE; | |
10988 | ||
f64e188b | 10989 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
10990 | if (buf == NULL) |
10991 | return FALSE; | |
10992 | ||
f64e188b NC |
10993 | /* PR 17512: file: ec08f814 |
10994 | 0-termintate the buffer so that string searches will not overflow. */ | |
10995 | buf[size] = 0; | |
10996 | ||
718175fa JK |
10997 | if (bfd_bread (buf, size, abfd) != size |
10998 | || !elf_parse_notes (abfd, buf, size, offset)) | |
10999 | { | |
11000 | free (buf); | |
11001 | return FALSE; | |
11002 | } | |
11003 | ||
252b5132 | 11004 | free (buf); |
b34976b6 | 11005 | return TRUE; |
252b5132 | 11006 | } |
98d8431c JB |
11007 | \f |
11008 | /* Providing external access to the ELF program header table. */ | |
11009 | ||
11010 | /* Return an upper bound on the number of bytes required to store a | |
11011 | copy of ABFD's program header table entries. Return -1 if an error | |
11012 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11013 | |
98d8431c | 11014 | long |
217aa764 | 11015 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
11016 | { |
11017 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11018 | { | |
11019 | bfd_set_error (bfd_error_wrong_format); | |
11020 | return -1; | |
11021 | } | |
11022 | ||
936e320b | 11023 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
11024 | } |
11025 | ||
98d8431c JB |
11026 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
11027 | will be stored as an array of Elf_Internal_Phdr structures, as | |
11028 | defined in include/elf/internal.h. To find out how large the | |
11029 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
11030 | ||
11031 | Return the number of program header table entries read, or -1 if an | |
11032 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11033 | |
98d8431c | 11034 | int |
217aa764 | 11035 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
11036 | { |
11037 | int num_phdrs; | |
11038 | ||
11039 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11040 | { | |
11041 | bfd_set_error (bfd_error_wrong_format); | |
11042 | return -1; | |
11043 | } | |
11044 | ||
11045 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 11046 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
11047 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
11048 | ||
11049 | return num_phdrs; | |
11050 | } | |
ae4221d7 | 11051 | |
db6751f2 | 11052 | enum elf_reloc_type_class |
7e612e98 AM |
11053 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
11054 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
11055 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
11056 | { |
11057 | return reloc_class_normal; | |
11058 | } | |
f8df10f4 | 11059 | |
47d9a591 | 11060 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
11061 | relocation against a local symbol. */ |
11062 | ||
11063 | bfd_vma | |
217aa764 AM |
11064 | _bfd_elf_rela_local_sym (bfd *abfd, |
11065 | Elf_Internal_Sym *sym, | |
8517fae7 | 11066 | asection **psec, |
217aa764 | 11067 | Elf_Internal_Rela *rel) |
f8df10f4 | 11068 | { |
8517fae7 | 11069 | asection *sec = *psec; |
f8df10f4 JJ |
11070 | bfd_vma relocation; |
11071 | ||
11072 | relocation = (sec->output_section->vma | |
11073 | + sec->output_offset | |
11074 | + sym->st_value); | |
11075 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 11076 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 11077 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 11078 | { |
f8df10f4 | 11079 | rel->r_addend = |
8517fae7 | 11080 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 11081 | elf_section_data (sec)->sec_info, |
753731ee AM |
11082 | sym->st_value + rel->r_addend); |
11083 | if (sec != *psec) | |
11084 | { | |
11085 | /* If we have changed the section, and our original section is | |
11086 | marked with SEC_EXCLUDE, it means that the original | |
11087 | SEC_MERGE section has been completely subsumed in some | |
11088 | other SEC_MERGE section. In this case, we need to leave | |
11089 | some info around for --emit-relocs. */ | |
11090 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
11091 | sec->kept_section = *psec; | |
11092 | sec = *psec; | |
11093 | } | |
8517fae7 AM |
11094 | rel->r_addend -= relocation; |
11095 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
11096 | } |
11097 | return relocation; | |
11098 | } | |
c629eae0 JJ |
11099 | |
11100 | bfd_vma | |
217aa764 AM |
11101 | _bfd_elf_rel_local_sym (bfd *abfd, |
11102 | Elf_Internal_Sym *sym, | |
11103 | asection **psec, | |
11104 | bfd_vma addend) | |
47d9a591 | 11105 | { |
c629eae0 JJ |
11106 | asection *sec = *psec; |
11107 | ||
dbaa2011 | 11108 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
11109 | return sym->st_value + addend; |
11110 | ||
11111 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 11112 | elf_section_data (sec)->sec_info, |
753731ee | 11113 | sym->st_value + addend); |
c629eae0 JJ |
11114 | } |
11115 | ||
37b01f6a DG |
11116 | /* Adjust an address within a section. Given OFFSET within SEC, return |
11117 | the new offset within the section, based upon changes made to the | |
11118 | section. Returns -1 if the offset is now invalid. | |
11119 | The offset (in abnd out) is in target sized bytes, however big a | |
11120 | byte may be. */ | |
11121 | ||
c629eae0 | 11122 | bfd_vma |
217aa764 | 11123 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 11124 | struct bfd_link_info *info, |
217aa764 AM |
11125 | asection *sec, |
11126 | bfd_vma offset) | |
c629eae0 | 11127 | { |
68bfbfcc | 11128 | switch (sec->sec_info_type) |
65765700 | 11129 | { |
dbaa2011 | 11130 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
11131 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
11132 | offset); | |
dbaa2011 | 11133 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 11134 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 11135 | |
65765700 | 11136 | default: |
310fd250 L |
11137 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
11138 | { | |
37b01f6a | 11139 | /* Reverse the offset. */ |
310fd250 L |
11140 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11141 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
11142 | |
11143 | /* address_size and sec->size are in octets. Convert | |
11144 | to bytes before subtracting the original offset. */ | |
11145 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 11146 | } |
65765700 JJ |
11147 | return offset; |
11148 | } | |
c629eae0 | 11149 | } |
3333a7c3 RM |
11150 | \f |
11151 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
11152 | reconstruct an ELF file by reading the segments out of remote memory | |
11153 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
11154 | points to. If not null, *LOADBASEP is filled in with the difference | |
11155 | between the VMAs from which the segments were read, and the VMAs the | |
11156 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
11157 | ||
11158 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
11159 | remote memory at target address VMA into the local buffer at MYADDR; it | |
11160 | should return zero on success or an `errno' code on failure. TEMPL must | |
11161 | be a BFD for an ELF target with the word size and byte order found in | |
11162 | the remote memory. */ | |
11163 | ||
11164 | bfd * | |
217aa764 AM |
11165 | bfd_elf_bfd_from_remote_memory |
11166 | (bfd *templ, | |
11167 | bfd_vma ehdr_vma, | |
f0a5d95a | 11168 | bfd_size_type size, |
217aa764 | 11169 | bfd_vma *loadbasep, |
fe78531d | 11170 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
11171 | { |
11172 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 11173 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 11174 | } |
4c45e5c9 JJ |
11175 | \f |
11176 | long | |
c9727e01 AM |
11177 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
11178 | long symcount ATTRIBUTE_UNUSED, | |
11179 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 11180 | long dynsymcount, |
c9727e01 AM |
11181 | asymbol **dynsyms, |
11182 | asymbol **ret) | |
4c45e5c9 JJ |
11183 | { |
11184 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11185 | asection *relplt; | |
11186 | asymbol *s; | |
11187 | const char *relplt_name; | |
11188 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
11189 | arelent *p; | |
11190 | long count, i, n; | |
11191 | size_t size; | |
11192 | Elf_Internal_Shdr *hdr; | |
11193 | char *names; | |
11194 | asection *plt; | |
11195 | ||
8615f3f2 AM |
11196 | *ret = NULL; |
11197 | ||
90e3cdf2 JJ |
11198 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
11199 | return 0; | |
11200 | ||
8615f3f2 AM |
11201 | if (dynsymcount <= 0) |
11202 | return 0; | |
11203 | ||
4c45e5c9 JJ |
11204 | if (!bed->plt_sym_val) |
11205 | return 0; | |
11206 | ||
11207 | relplt_name = bed->relplt_name; | |
11208 | if (relplt_name == NULL) | |
d35fd659 | 11209 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
11210 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
11211 | if (relplt == NULL) | |
11212 | return 0; | |
11213 | ||
11214 | hdr = &elf_section_data (relplt)->this_hdr; | |
11215 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
11216 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
11217 | return 0; | |
11218 | ||
11219 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
11220 | if (plt == NULL) | |
11221 | return 0; | |
11222 | ||
11223 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 11224 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
11225 | return -1; |
11226 | ||
eea6121a | 11227 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
11228 | size = count * sizeof (asymbol); |
11229 | p = relplt->relocation; | |
cb53bf42 | 11230 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
11231 | { |
11232 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
11233 | if (p->addend != 0) | |
11234 | { | |
11235 | #ifdef BFD64 | |
11236 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
11237 | #else | |
11238 | size += sizeof ("+0x") - 1 + 8; | |
11239 | #endif | |
11240 | } | |
11241 | } | |
4c45e5c9 | 11242 | |
a50b1753 | 11243 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
11244 | if (s == NULL) |
11245 | return -1; | |
11246 | ||
11247 | names = (char *) (s + count); | |
11248 | p = relplt->relocation; | |
11249 | n = 0; | |
cb53bf42 | 11250 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
11251 | { |
11252 | size_t len; | |
11253 | bfd_vma addr; | |
11254 | ||
11255 | addr = bed->plt_sym_val (i, plt, p); | |
11256 | if (addr == (bfd_vma) -1) | |
11257 | continue; | |
11258 | ||
11259 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
11260 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
11261 | we are defining a symbol, ensure one of them is set. */ | |
11262 | if ((s->flags & BSF_LOCAL) == 0) | |
11263 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 11264 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
11265 | s->section = plt; |
11266 | s->value = addr - plt->vma; | |
11267 | s->name = names; | |
8f39ba8e | 11268 | s->udata.p = NULL; |
4c45e5c9 JJ |
11269 | len = strlen ((*p->sym_ptr_ptr)->name); |
11270 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
11271 | names += len; | |
041de40d AM |
11272 | if (p->addend != 0) |
11273 | { | |
1d770845 | 11274 | char buf[30], *a; |
d324f6d6 | 11275 | |
041de40d AM |
11276 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
11277 | names += sizeof ("+0x") - 1; | |
1d770845 L |
11278 | bfd_sprintf_vma (abfd, buf, p->addend); |
11279 | for (a = buf; *a == '0'; ++a) | |
11280 | ; | |
11281 | len = strlen (a); | |
11282 | memcpy (names, a, len); | |
11283 | names += len; | |
041de40d | 11284 | } |
4c45e5c9 JJ |
11285 | memcpy (names, "@plt", sizeof ("@plt")); |
11286 | names += sizeof ("@plt"); | |
8f39ba8e | 11287 | ++s, ++n; |
4c45e5c9 JJ |
11288 | } |
11289 | ||
11290 | return n; | |
11291 | } | |
3d7f7666 | 11292 | |
821e6ff6 AM |
11293 | /* It is only used by x86-64 so far. |
11294 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
11295 | but current usage would allow all of _bfd_std_section to be zero. */ |
11296 | static const asymbol lcomm_sym | |
11297 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 11298 | asection _bfd_elf_large_com_section |
7eacd66b | 11299 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 11300 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 11301 | |
d1036acb | 11302 | void |
78245035 L |
11303 | _bfd_elf_post_process_headers (bfd * abfd, |
11304 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
11305 | { |
11306 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
11307 | ||
11308 | i_ehdrp = elf_elfheader (abfd); | |
11309 | ||
11310 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
11311 | |
11312 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 11313 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 11314 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 11315 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 11316 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 11317 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 11318 | } |
fcb93ecf PB |
11319 | |
11320 | ||
11321 | /* Return TRUE for ELF symbol types that represent functions. | |
11322 | This is the default version of this function, which is sufficient for | |
d8045f23 | 11323 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
11324 | |
11325 | bfd_boolean | |
11326 | _bfd_elf_is_function_type (unsigned int type) | |
11327 | { | |
d8045f23 NC |
11328 | return (type == STT_FUNC |
11329 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 11330 | } |
9f296da3 | 11331 | |
aef36ac1 AM |
11332 | /* If the ELF symbol SYM might be a function in SEC, return the |
11333 | function size and set *CODE_OFF to the function's entry point, | |
11334 | otherwise return zero. */ | |
9f296da3 | 11335 | |
aef36ac1 AM |
11336 | bfd_size_type |
11337 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
11338 | bfd_vma *code_off) | |
9f296da3 | 11339 | { |
aef36ac1 AM |
11340 | bfd_size_type size; |
11341 | ||
ff9e0f5b | 11342 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
11343 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
11344 | || sym->section != sec) | |
11345 | return 0; | |
ff9e0f5b | 11346 | |
ff9e0f5b | 11347 | *code_off = sym->value; |
aef36ac1 AM |
11348 | size = 0; |
11349 | if (!(sym->flags & BSF_SYNTHETIC)) | |
11350 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
11351 | if (size == 0) | |
11352 | size = 1; | |
11353 | return size; | |
9f296da3 | 11354 | } |