]>
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 */ |
d42c267e AM |
357 | (_("%B: invalid string offset %u >= %Lu for section `%s'"), |
358 | abfd, strindex, 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 | 744 | Elf_Internal_Group *idx; |
0c54f692 | 745 | bfd_size_type n_elt; |
ce497010 NC |
746 | |
747 | if (shdr == NULL) | |
748 | continue; | |
749 | ||
750 | idx = (Elf_Internal_Group *) shdr->contents; | |
0c54f692 NC |
751 | if (idx == NULL || shdr->sh_size < 4) |
752 | { | |
753 | /* See PR 21957 for a reproducer. */ | |
754 | /* xgettext:c-format */ | |
755 | _bfd_error_handler (_("%B: group section '%A' has no contents"), | |
756 | abfd, shdr->bfd_section); | |
757 | elf_tdata (abfd)->group_sect_ptr[i] = NULL; | |
758 | bfd_set_error (bfd_error_bad_value); | |
759 | return FALSE; | |
760 | } | |
ce497010 | 761 | n_elt = shdr->sh_size / 4; |
dbb410c3 AM |
762 | |
763 | /* Look through this group's sections to see if current | |
764 | section is a member. */ | |
765 | while (--n_elt != 0) | |
766 | if ((++idx)->shdr == hdr) | |
767 | { | |
e0e8c97f | 768 | asection *s = NULL; |
dbb410c3 AM |
769 | |
770 | /* We are a member of this group. Go looking through | |
771 | other members to see if any others are linked via | |
772 | next_in_group. */ | |
773 | idx = (Elf_Internal_Group *) shdr->contents; | |
774 | n_elt = shdr->sh_size / 4; | |
775 | while (--n_elt != 0) | |
776 | if ((s = (++idx)->shdr->bfd_section) != NULL | |
945906ff | 777 | && elf_next_in_group (s) != NULL) |
dbb410c3 AM |
778 | break; |
779 | if (n_elt != 0) | |
780 | { | |
dbb410c3 AM |
781 | /* Snarf the group name from other member, and |
782 | insert current section in circular list. */ | |
945906ff AM |
783 | elf_group_name (newsect) = elf_group_name (s); |
784 | elf_next_in_group (newsect) = elf_next_in_group (s); | |
785 | elf_next_in_group (s) = newsect; | |
dbb410c3 AM |
786 | } |
787 | else | |
788 | { | |
dbb410c3 AM |
789 | const char *gname; |
790 | ||
b885599b AM |
791 | gname = group_signature (abfd, shdr); |
792 | if (gname == NULL) | |
b34976b6 | 793 | return FALSE; |
945906ff | 794 | elf_group_name (newsect) = gname; |
dbb410c3 AM |
795 | |
796 | /* Start a circular list with one element. */ | |
945906ff | 797 | elf_next_in_group (newsect) = newsect; |
dbb410c3 | 798 | } |
b885599b | 799 | |
9dce4196 AM |
800 | /* If the group section has been created, point to the |
801 | new member. */ | |
dbb410c3 | 802 | if (shdr->bfd_section != NULL) |
945906ff | 803 | elf_next_in_group (shdr->bfd_section) = newsect; |
b885599b | 804 | |
dbb410c3 AM |
805 | i = num_group - 1; |
806 | break; | |
807 | } | |
808 | } | |
809 | } | |
810 | ||
945906ff | 811 | if (elf_group_name (newsect) == NULL) |
dbb410c3 | 812 | { |
695344c0 | 813 | /* xgettext:c-format */ |
0c54f692 | 814 | _bfd_error_handler (_("%B: no group info for section '%A'"), |
4eca0228 | 815 | abfd, newsect); |
493a3386 | 816 | return FALSE; |
dbb410c3 | 817 | } |
b34976b6 | 818 | return TRUE; |
dbb410c3 AM |
819 | } |
820 | ||
3d7f7666 | 821 | bfd_boolean |
dd863624 | 822 | _bfd_elf_setup_sections (bfd *abfd) |
3d7f7666 L |
823 | { |
824 | unsigned int i; | |
825 | unsigned int num_group = elf_tdata (abfd)->num_group; | |
826 | bfd_boolean result = TRUE; | |
dd863624 L |
827 | asection *s; |
828 | ||
829 | /* Process SHF_LINK_ORDER. */ | |
830 | for (s = abfd->sections; s != NULL; s = s->next) | |
831 | { | |
832 | Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; | |
833 | if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) | |
834 | { | |
835 | unsigned int elfsec = this_hdr->sh_link; | |
836 | /* FIXME: The old Intel compiler and old strip/objcopy may | |
837 | not set the sh_link or sh_info fields. Hence we could | |
838 | get the situation where elfsec is 0. */ | |
839 | if (elfsec == 0) | |
840 | { | |
4fbb74a6 | 841 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
dd863624 L |
842 | if (bed->link_order_error_handler) |
843 | bed->link_order_error_handler | |
695344c0 | 844 | /* xgettext:c-format */ |
dd863624 L |
845 | (_("%B: warning: sh_link not set for section `%A'"), |
846 | abfd, s); | |
847 | } | |
848 | else | |
849 | { | |
91d6fa6a | 850 | asection *linksec = NULL; |
25bbc984 | 851 | |
4fbb74a6 AM |
852 | if (elfsec < elf_numsections (abfd)) |
853 | { | |
854 | this_hdr = elf_elfsections (abfd)[elfsec]; | |
91d6fa6a | 855 | linksec = this_hdr->bfd_section; |
4fbb74a6 | 856 | } |
25bbc984 L |
857 | |
858 | /* PR 1991, 2008: | |
859 | Some strip/objcopy may leave an incorrect value in | |
860 | sh_link. We don't want to proceed. */ | |
91d6fa6a | 861 | if (linksec == NULL) |
25bbc984 | 862 | { |
4eca0228 | 863 | _bfd_error_handler |
695344c0 | 864 | /* xgettext:c-format */ |
25bbc984 | 865 | (_("%B: sh_link [%d] in section `%A' is incorrect"), |
c08bb8dd | 866 | s->owner, elfsec, s); |
25bbc984 L |
867 | result = FALSE; |
868 | } | |
869 | ||
91d6fa6a | 870 | elf_linked_to_section (s) = linksec; |
dd863624 L |
871 | } |
872 | } | |
53720c49 AM |
873 | else if (this_hdr->sh_type == SHT_GROUP |
874 | && elf_next_in_group (s) == NULL) | |
875 | { | |
4eca0228 | 876 | _bfd_error_handler |
695344c0 | 877 | /* xgettext:c-format */ |
53720c49 AM |
878 | (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"), |
879 | abfd, elf_section_data (s)->this_idx); | |
880 | result = FALSE; | |
881 | } | |
dd863624 | 882 | } |
3d7f7666 | 883 | |
dd863624 | 884 | /* Process section groups. */ |
3d7f7666 L |
885 | if (num_group == (unsigned) -1) |
886 | return result; | |
887 | ||
888 | for (i = 0; i < num_group; i++) | |
889 | { | |
890 | Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; | |
4b0e8a5f NC |
891 | Elf_Internal_Group *idx; |
892 | unsigned int n_elt; | |
3d7f7666 | 893 | |
4b0e8a5f NC |
894 | /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ |
895 | if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) | |
896 | { | |
4eca0228 | 897 | _bfd_error_handler |
695344c0 | 898 | /* xgettext:c-format */ |
4b0e8a5f NC |
899 | (_("%B: section group entry number %u is corrupt"), |
900 | abfd, i); | |
901 | result = FALSE; | |
902 | continue; | |
903 | } | |
904 | ||
905 | idx = (Elf_Internal_Group *) shdr->contents; | |
906 | n_elt = shdr->sh_size / 4; | |
1b786873 | 907 | |
3d7f7666 | 908 | while (--n_elt != 0) |
24d3e51b NC |
909 | { |
910 | ++ idx; | |
911 | ||
912 | if (idx->shdr == NULL) | |
913 | continue; | |
914 | else if (idx->shdr->bfd_section) | |
915 | elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; | |
916 | else if (idx->shdr->sh_type == SHT_RELA | |
917 | || idx->shdr->sh_type == SHT_REL) | |
918 | /* We won't include relocation sections in section groups in | |
919 | output object files. We adjust the group section size here | |
920 | so that relocatable link will work correctly when | |
921 | relocation sections are in section group in input object | |
922 | files. */ | |
923 | shdr->bfd_section->size -= 4; | |
924 | else | |
925 | { | |
926 | /* There are some unknown sections in the group. */ | |
927 | _bfd_error_handler | |
928 | /* xgettext:c-format */ | |
929 | (_("%B: unknown type [%#x] section `%s' in group [%A]"), | |
930 | abfd, | |
931 | idx->shdr->sh_type, | |
932 | bfd_elf_string_from_elf_section (abfd, | |
933 | (elf_elfheader (abfd) | |
934 | ->e_shstrndx), | |
935 | idx->shdr->sh_name), | |
936 | shdr->bfd_section); | |
937 | result = FALSE; | |
938 | } | |
939 | } | |
3d7f7666 | 940 | } |
24d3e51b | 941 | |
3d7f7666 L |
942 | return result; |
943 | } | |
944 | ||
72adc230 AM |
945 | bfd_boolean |
946 | bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) | |
947 | { | |
948 | return elf_next_in_group (sec) != NULL; | |
949 | } | |
950 | ||
f6fe1ccd L |
951 | static char * |
952 | convert_debug_to_zdebug (bfd *abfd, const char *name) | |
953 | { | |
954 | unsigned int len = strlen (name); | |
955 | char *new_name = bfd_alloc (abfd, len + 2); | |
956 | if (new_name == NULL) | |
957 | return NULL; | |
958 | new_name[0] = '.'; | |
959 | new_name[1] = 'z'; | |
960 | memcpy (new_name + 2, name + 1, len); | |
961 | return new_name; | |
962 | } | |
963 | ||
964 | static char * | |
965 | convert_zdebug_to_debug (bfd *abfd, const char *name) | |
966 | { | |
967 | unsigned int len = strlen (name); | |
968 | char *new_name = bfd_alloc (abfd, len); | |
969 | if (new_name == NULL) | |
970 | return NULL; | |
971 | new_name[0] = '.'; | |
972 | memcpy (new_name + 1, name + 2, len - 1); | |
973 | return new_name; | |
974 | } | |
975 | ||
252b5132 RH |
976 | /* Make a BFD section from an ELF section. We store a pointer to the |
977 | BFD section in the bfd_section field of the header. */ | |
978 | ||
b34976b6 | 979 | bfd_boolean |
217aa764 AM |
980 | _bfd_elf_make_section_from_shdr (bfd *abfd, |
981 | Elf_Internal_Shdr *hdr, | |
6dc132d9 L |
982 | const char *name, |
983 | int shindex) | |
252b5132 RH |
984 | { |
985 | asection *newsect; | |
986 | flagword flags; | |
9c5bfbb7 | 987 | const struct elf_backend_data *bed; |
252b5132 RH |
988 | |
989 | if (hdr->bfd_section != NULL) | |
4e011fb5 | 990 | return TRUE; |
252b5132 RH |
991 | |
992 | newsect = bfd_make_section_anyway (abfd, name); | |
993 | if (newsect == NULL) | |
b34976b6 | 994 | return FALSE; |
252b5132 | 995 | |
1829f4b2 AM |
996 | hdr->bfd_section = newsect; |
997 | elf_section_data (newsect)->this_hdr = *hdr; | |
6dc132d9 | 998 | elf_section_data (newsect)->this_idx = shindex; |
1829f4b2 | 999 | |
2f89ff8d L |
1000 | /* Always use the real type/flags. */ |
1001 | elf_section_type (newsect) = hdr->sh_type; | |
1002 | elf_section_flags (newsect) = hdr->sh_flags; | |
1003 | ||
252b5132 RH |
1004 | newsect->filepos = hdr->sh_offset; |
1005 | ||
1006 | if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) | |
1007 | || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) | |
1008 | || ! bfd_set_section_alignment (abfd, newsect, | |
72de5009 | 1009 | bfd_log2 (hdr->sh_addralign))) |
b34976b6 | 1010 | return FALSE; |
252b5132 RH |
1011 | |
1012 | flags = SEC_NO_FLAGS; | |
1013 | if (hdr->sh_type != SHT_NOBITS) | |
1014 | flags |= SEC_HAS_CONTENTS; | |
dbb410c3 | 1015 | if (hdr->sh_type == SHT_GROUP) |
7bdf4127 | 1016 | flags |= SEC_GROUP; |
252b5132 RH |
1017 | if ((hdr->sh_flags & SHF_ALLOC) != 0) |
1018 | { | |
1019 | flags |= SEC_ALLOC; | |
1020 | if (hdr->sh_type != SHT_NOBITS) | |
1021 | flags |= SEC_LOAD; | |
1022 | } | |
1023 | if ((hdr->sh_flags & SHF_WRITE) == 0) | |
1024 | flags |= SEC_READONLY; | |
1025 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1026 | flags |= SEC_CODE; | |
1027 | else if ((flags & SEC_LOAD) != 0) | |
1028 | flags |= SEC_DATA; | |
f5fa8ca2 JJ |
1029 | if ((hdr->sh_flags & SHF_MERGE) != 0) |
1030 | { | |
1031 | flags |= SEC_MERGE; | |
1032 | newsect->entsize = hdr->sh_entsize; | |
f5fa8ca2 | 1033 | } |
84865015 NC |
1034 | if ((hdr->sh_flags & SHF_STRINGS) != 0) |
1035 | flags |= SEC_STRINGS; | |
dbb410c3 AM |
1036 | if (hdr->sh_flags & SHF_GROUP) |
1037 | if (!setup_group (abfd, hdr, newsect)) | |
b34976b6 | 1038 | return FALSE; |
13ae64f3 JJ |
1039 | if ((hdr->sh_flags & SHF_TLS) != 0) |
1040 | flags |= SEC_THREAD_LOCAL; | |
18ae9cc1 L |
1041 | if ((hdr->sh_flags & SHF_EXCLUDE) != 0) |
1042 | flags |= SEC_EXCLUDE; | |
252b5132 | 1043 | |
3d2b39cf | 1044 | if ((flags & SEC_ALLOC) == 0) |
7a6cc5fb | 1045 | { |
3d2b39cf L |
1046 | /* The debugging sections appear to be recognized only by name, |
1047 | not any sort of flag. Their SEC_ALLOC bits are cleared. */ | |
3d2b39cf L |
1048 | if (name [0] == '.') |
1049 | { | |
f073ced3 AM |
1050 | const char *p; |
1051 | int n; | |
1052 | if (name[1] == 'd') | |
1053 | p = ".debug", n = 6; | |
1054 | else if (name[1] == 'g' && name[2] == 'n') | |
1055 | p = ".gnu.linkonce.wi.", n = 17; | |
1056 | else if (name[1] == 'g' && name[2] == 'd') | |
1057 | p = ".gdb_index", n = 11; /* yes we really do mean 11. */ | |
1058 | else if (name[1] == 'l') | |
1059 | p = ".line", n = 5; | |
1060 | else if (name[1] == 's') | |
1061 | p = ".stab", n = 5; | |
1062 | else if (name[1] == 'z') | |
1063 | p = ".zdebug", n = 7; | |
1064 | else | |
1065 | p = NULL, n = 0; | |
1066 | if (p != NULL && strncmp (name, p, n) == 0) | |
3d2b39cf L |
1067 | flags |= SEC_DEBUGGING; |
1068 | } | |
1069 | } | |
252b5132 RH |
1070 | |
1071 | /* As a GNU extension, if the name begins with .gnu.linkonce, we | |
1072 | only link a single copy of the section. This is used to support | |
1073 | g++. g++ will emit each template expansion in its own section. | |
1074 | The symbols will be defined as weak, so that multiple definitions | |
1075 | are permitted. The GNU linker extension is to actually discard | |
1076 | all but one of the sections. */ | |
0112cd26 | 1077 | if (CONST_STRNEQ (name, ".gnu.linkonce") |
b885599b | 1078 | && elf_next_in_group (newsect) == NULL) |
252b5132 RH |
1079 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1080 | ||
fa152c49 JW |
1081 | bed = get_elf_backend_data (abfd); |
1082 | if (bed->elf_backend_section_flags) | |
1083 | if (! bed->elf_backend_section_flags (&flags, hdr)) | |
b34976b6 | 1084 | return FALSE; |
fa152c49 | 1085 | |
252b5132 | 1086 | if (! bfd_set_section_flags (abfd, newsect, flags)) |
b34976b6 | 1087 | return FALSE; |
252b5132 | 1088 | |
718175fa JK |
1089 | /* We do not parse the PT_NOTE segments as we are interested even in the |
1090 | separate debug info files which may have the segments offsets corrupted. | |
1091 | PT_NOTEs from the core files are currently not parsed using BFD. */ | |
1092 | if (hdr->sh_type == SHT_NOTE) | |
1093 | { | |
baea7ef1 | 1094 | bfd_byte *contents; |
718175fa | 1095 | |
baea7ef1 | 1096 | if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) |
718175fa JK |
1097 | return FALSE; |
1098 | ||
08698b7b | 1099 | elf_parse_notes (abfd, (char *) contents, hdr->sh_size, hdr->sh_offset); |
718175fa JK |
1100 | free (contents); |
1101 | } | |
1102 | ||
252b5132 RH |
1103 | if ((flags & SEC_ALLOC) != 0) |
1104 | { | |
1105 | Elf_Internal_Phdr *phdr; | |
6ffd7900 AM |
1106 | unsigned int i, nload; |
1107 | ||
1108 | /* Some ELF linkers produce binaries with all the program header | |
1109 | p_paddr fields zero. If we have such a binary with more than | |
1110 | one PT_LOAD header, then leave the section lma equal to vma | |
1111 | so that we don't create sections with overlapping lma. */ | |
1112 | phdr = elf_tdata (abfd)->phdr; | |
1113 | for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1114 | if (phdr->p_paddr != 0) | |
1115 | break; | |
1116 | else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) | |
1117 | ++nload; | |
1118 | if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) | |
1119 | return TRUE; | |
252b5132 | 1120 | |
252b5132 RH |
1121 | phdr = elf_tdata (abfd)->phdr; |
1122 | for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) | |
1123 | { | |
86b2281f AM |
1124 | if (((phdr->p_type == PT_LOAD |
1125 | && (hdr->sh_flags & SHF_TLS) == 0) | |
1126 | || phdr->p_type == PT_TLS) | |
9a83a553 | 1127 | && ELF_SECTION_IN_SEGMENT (hdr, phdr)) |
252b5132 | 1128 | { |
88967714 AM |
1129 | if ((flags & SEC_LOAD) == 0) |
1130 | newsect->lma = (phdr->p_paddr | |
1131 | + hdr->sh_addr - phdr->p_vaddr); | |
1132 | else | |
1133 | /* We used to use the same adjustment for SEC_LOAD | |
1134 | sections, but that doesn't work if the segment | |
1135 | is packed with code from multiple VMAs. | |
1136 | Instead we calculate the section LMA based on | |
1137 | the segment LMA. It is assumed that the | |
1138 | segment will contain sections with contiguous | |
1139 | LMAs, even if the VMAs are not. */ | |
1140 | newsect->lma = (phdr->p_paddr | |
1141 | + hdr->sh_offset - phdr->p_offset); | |
1142 | ||
1143 | /* With contiguous segments, we can't tell from file | |
1144 | offsets whether a section with zero size should | |
1145 | be placed at the end of one segment or the | |
1146 | beginning of the next. Decide based on vaddr. */ | |
1147 | if (hdr->sh_addr >= phdr->p_vaddr | |
1148 | && (hdr->sh_addr + hdr->sh_size | |
1149 | <= phdr->p_vaddr + phdr->p_memsz)) | |
1150 | break; | |
252b5132 RH |
1151 | } |
1152 | } | |
1153 | } | |
1154 | ||
4a114e3e L |
1155 | /* Compress/decompress DWARF debug sections with names: .debug_* and |
1156 | .zdebug_*, after the section flags is set. */ | |
1157 | if ((flags & SEC_DEBUGGING) | |
1158 | && ((name[1] == 'd' && name[6] == '_') | |
1159 | || (name[1] == 'z' && name[7] == '_'))) | |
1160 | { | |
1161 | enum { nothing, compress, decompress } action = nothing; | |
151411f8 | 1162 | int compression_header_size; |
dab394de | 1163 | bfd_size_type uncompressed_size; |
151411f8 L |
1164 | bfd_boolean compressed |
1165 | = bfd_is_section_compressed_with_header (abfd, newsect, | |
dab394de L |
1166 | &compression_header_size, |
1167 | &uncompressed_size); | |
4a114e3e | 1168 | |
151411f8 | 1169 | if (compressed) |
4a114e3e L |
1170 | { |
1171 | /* Compressed section. Check if we should decompress. */ | |
1172 | if ((abfd->flags & BFD_DECOMPRESS)) | |
1173 | action = decompress; | |
1174 | } | |
151411f8 L |
1175 | |
1176 | /* Compress the uncompressed section or convert from/to .zdebug* | |
1177 | section. Check if we should compress. */ | |
1178 | if (action == nothing) | |
4a114e3e | 1179 | { |
151411f8 L |
1180 | if (newsect->size != 0 |
1181 | && (abfd->flags & BFD_COMPRESS) | |
1182 | && compression_header_size >= 0 | |
dab394de | 1183 | && uncompressed_size > 0 |
151411f8 L |
1184 | && (!compressed |
1185 | || ((compression_header_size > 0) | |
1186 | != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) | |
4a114e3e | 1187 | action = compress; |
151411f8 L |
1188 | else |
1189 | return TRUE; | |
4a114e3e L |
1190 | } |
1191 | ||
151411f8 | 1192 | if (action == compress) |
4a114e3e | 1193 | { |
4a114e3e L |
1194 | if (!bfd_init_section_compress_status (abfd, newsect)) |
1195 | { | |
4eca0228 | 1196 | _bfd_error_handler |
695344c0 | 1197 | /* xgettext:c-format */ |
bc823199 | 1198 | (_("%B: unable to initialize compress status for section %s"), |
4a114e3e L |
1199 | abfd, name); |
1200 | return FALSE; | |
1201 | } | |
151411f8 L |
1202 | } |
1203 | else | |
1204 | { | |
4a114e3e L |
1205 | if (!bfd_init_section_decompress_status (abfd, newsect)) |
1206 | { | |
4eca0228 | 1207 | _bfd_error_handler |
695344c0 | 1208 | /* xgettext:c-format */ |
bc823199 | 1209 | (_("%B: unable to initialize decompress status for section %s"), |
4a114e3e L |
1210 | abfd, name); |
1211 | return FALSE; | |
1212 | } | |
151411f8 L |
1213 | } |
1214 | ||
f6fe1ccd | 1215 | if (abfd->is_linker_input) |
151411f8 | 1216 | { |
f6fe1ccd L |
1217 | if (name[1] == 'z' |
1218 | && (action == decompress | |
1219 | || (action == compress | |
1220 | && (abfd->flags & BFD_COMPRESS_GABI) != 0))) | |
4e011fb5 | 1221 | { |
f6fe1ccd L |
1222 | /* Convert section name from .zdebug_* to .debug_* so |
1223 | that linker will consider this section as a debug | |
1224 | section. */ | |
1225 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
151411f8 L |
1226 | if (new_name == NULL) |
1227 | return FALSE; | |
f6fe1ccd | 1228 | bfd_rename_section (abfd, newsect, new_name); |
151411f8 | 1229 | } |
4a114e3e | 1230 | } |
f6fe1ccd L |
1231 | else |
1232 | /* For objdump, don't rename the section. For objcopy, delay | |
1233 | section rename to elf_fake_sections. */ | |
1234 | newsect->flags |= SEC_ELF_RENAME; | |
4a114e3e L |
1235 | } |
1236 | ||
b34976b6 | 1237 | return TRUE; |
252b5132 RH |
1238 | } |
1239 | ||
84865015 NC |
1240 | const char *const bfd_elf_section_type_names[] = |
1241 | { | |
252b5132 RH |
1242 | "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", |
1243 | "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", | |
1244 | "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", | |
1245 | }; | |
1246 | ||
1049f94e | 1247 | /* ELF relocs are against symbols. If we are producing relocatable |
252b5132 RH |
1248 | output, and the reloc is against an external symbol, and nothing |
1249 | has given us any additional addend, the resulting reloc will also | |
1250 | be against the same symbol. In such a case, we don't want to | |
1251 | change anything about the way the reloc is handled, since it will | |
1252 | all be done at final link time. Rather than put special case code | |
1253 | into bfd_perform_relocation, all the reloc types use this howto | |
1254 | function. It just short circuits the reloc if producing | |
1049f94e | 1255 | relocatable output against an external symbol. */ |
252b5132 | 1256 | |
252b5132 | 1257 | bfd_reloc_status_type |
217aa764 AM |
1258 | bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
1259 | arelent *reloc_entry, | |
1260 | asymbol *symbol, | |
1261 | void *data ATTRIBUTE_UNUSED, | |
1262 | asection *input_section, | |
1263 | bfd *output_bfd, | |
1264 | char **error_message ATTRIBUTE_UNUSED) | |
1265 | { | |
1266 | if (output_bfd != NULL | |
252b5132 RH |
1267 | && (symbol->flags & BSF_SECTION_SYM) == 0 |
1268 | && (! reloc_entry->howto->partial_inplace | |
1269 | || reloc_entry->addend == 0)) | |
1270 | { | |
1271 | reloc_entry->address += input_section->output_offset; | |
1272 | return bfd_reloc_ok; | |
1273 | } | |
1274 | ||
1275 | return bfd_reloc_continue; | |
1276 | } | |
1277 | \f | |
84865015 NC |
1278 | /* Returns TRUE if section A matches section B. |
1279 | Names, addresses and links may be different, but everything else | |
1280 | should be the same. */ | |
1281 | ||
1282 | static bfd_boolean | |
5522f910 NC |
1283 | section_match (const Elf_Internal_Shdr * a, |
1284 | const Elf_Internal_Shdr * b) | |
84865015 NC |
1285 | { |
1286 | return | |
1287 | a->sh_type == b->sh_type | |
5522f910 NC |
1288 | && (a->sh_flags & ~ SHF_INFO_LINK) |
1289 | == (b->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1290 | && a->sh_addralign == b->sh_addralign |
1291 | && a->sh_size == b->sh_size | |
1292 | && a->sh_entsize == b->sh_entsize | |
1293 | /* FIXME: Check sh_addr ? */ | |
1294 | ; | |
1295 | } | |
1296 | ||
1297 | /* Find a section in OBFD that has the same characteristics | |
1298 | as IHEADER. Return the index of this section or SHN_UNDEF if | |
1299 | none can be found. Check's section HINT first, as this is likely | |
1300 | to be the correct section. */ | |
1301 | ||
1302 | static unsigned int | |
5cc4ca83 ST |
1303 | find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, |
1304 | const unsigned int hint) | |
84865015 NC |
1305 | { |
1306 | Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); | |
1307 | unsigned int i; | |
1308 | ||
a55c9876 NC |
1309 | BFD_ASSERT (iheader != NULL); |
1310 | ||
1311 | /* See PR 20922 for a reproducer of the NULL test. */ | |
5cc4ca83 ST |
1312 | if (hint < elf_numsections (obfd) |
1313 | && oheaders[hint] != NULL | |
a55c9876 | 1314 | && section_match (oheaders[hint], iheader)) |
84865015 NC |
1315 | return hint; |
1316 | ||
1317 | for (i = 1; i < elf_numsections (obfd); i++) | |
1318 | { | |
1319 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
1320 | ||
a55c9876 NC |
1321 | if (oheader == NULL) |
1322 | continue; | |
84865015 NC |
1323 | if (section_match (oheader, iheader)) |
1324 | /* FIXME: Do we care if there is a potential for | |
1325 | multiple matches ? */ | |
1326 | return i; | |
1327 | } | |
1328 | ||
1329 | return SHN_UNDEF; | |
1330 | } | |
1331 | ||
5522f910 NC |
1332 | /* PR 19938: Attempt to set the ELF section header fields of an OS or |
1333 | Processor specific section, based upon a matching input section. | |
1334 | Returns TRUE upon success, FALSE otherwise. */ | |
1335 | ||
1336 | static bfd_boolean | |
1337 | copy_special_section_fields (const bfd *ibfd, | |
1338 | bfd *obfd, | |
1339 | const Elf_Internal_Shdr *iheader, | |
1340 | Elf_Internal_Shdr *oheader, | |
1341 | const unsigned int secnum) | |
1342 | { | |
1343 | const struct elf_backend_data *bed = get_elf_backend_data (obfd); | |
1344 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); | |
1345 | bfd_boolean changed = FALSE; | |
1346 | unsigned int sh_link; | |
1347 | ||
1348 | if (oheader->sh_type == SHT_NOBITS) | |
1349 | { | |
1350 | /* This is a feature for objcopy --only-keep-debug: | |
1351 | When a section's type is changed to NOBITS, we preserve | |
1352 | the sh_link and sh_info fields so that they can be | |
1353 | matched up with the original. | |
1354 | ||
1355 | Note: Strictly speaking these assignments are wrong. | |
1356 | The sh_link and sh_info fields should point to the | |
1357 | relevent sections in the output BFD, which may not be in | |
1358 | the same location as they were in the input BFD. But | |
1359 | the whole point of this action is to preserve the | |
1360 | original values of the sh_link and sh_info fields, so | |
1361 | that they can be matched up with the section headers in | |
1362 | the original file. So strictly speaking we may be | |
1363 | creating an invalid ELF file, but it is only for a file | |
1364 | that just contains debug info and only for sections | |
1365 | without any contents. */ | |
1366 | if (oheader->sh_link == 0) | |
1367 | oheader->sh_link = iheader->sh_link; | |
1368 | if (oheader->sh_info == 0) | |
1369 | oheader->sh_info = iheader->sh_info; | |
1370 | return TRUE; | |
1371 | } | |
1372 | ||
1373 | /* Allow the target a chance to decide how these fields should be set. */ | |
1374 | if (bed->elf_backend_copy_special_section_fields != NULL | |
1375 | && bed->elf_backend_copy_special_section_fields | |
1376 | (ibfd, obfd, iheader, oheader)) | |
1377 | return TRUE; | |
1378 | ||
1379 | /* We have an iheader which might match oheader, and which has non-zero | |
1380 | sh_info and/or sh_link fields. Attempt to follow those links and find | |
1381 | the section in the output bfd which corresponds to the linked section | |
1382 | in the input bfd. */ | |
1383 | if (iheader->sh_link != SHN_UNDEF) | |
1384 | { | |
4f3ca05b NC |
1385 | /* See PR 20931 for a reproducer. */ |
1386 | if (iheader->sh_link >= elf_numsections (ibfd)) | |
1387 | { | |
76cfced5 | 1388 | _bfd_error_handler |
4f3ca05b NC |
1389 | /* xgettext:c-format */ |
1390 | (_("%B: Invalid sh_link field (%d) in section number %d"), | |
1391 | ibfd, iheader->sh_link, secnum); | |
1392 | return FALSE; | |
1393 | } | |
1394 | ||
5522f910 NC |
1395 | sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); |
1396 | if (sh_link != SHN_UNDEF) | |
1397 | { | |
1398 | oheader->sh_link = sh_link; | |
1399 | changed = TRUE; | |
1400 | } | |
1401 | else | |
1402 | /* FIXME: Should we install iheader->sh_link | |
1403 | if we could not find a match ? */ | |
76cfced5 | 1404 | _bfd_error_handler |
695344c0 | 1405 | /* xgettext:c-format */ |
5522f910 NC |
1406 | (_("%B: Failed to find link section for section %d"), obfd, secnum); |
1407 | } | |
1408 | ||
1409 | if (iheader->sh_info) | |
1410 | { | |
1411 | /* The sh_info field can hold arbitrary information, but if the | |
1412 | SHF_LINK_INFO flag is set then it should be interpreted as a | |
1413 | section index. */ | |
1414 | if (iheader->sh_flags & SHF_INFO_LINK) | |
1415 | { | |
1416 | sh_link = find_link (obfd, iheaders[iheader->sh_info], | |
1417 | iheader->sh_info); | |
1418 | if (sh_link != SHN_UNDEF) | |
1419 | oheader->sh_flags |= SHF_INFO_LINK; | |
1420 | } | |
1421 | else | |
1422 | /* No idea what it means - just copy it. */ | |
1423 | sh_link = iheader->sh_info; | |
1424 | ||
1425 | if (sh_link != SHN_UNDEF) | |
1426 | { | |
1427 | oheader->sh_info = sh_link; | |
1428 | changed = TRUE; | |
1429 | } | |
1430 | else | |
76cfced5 | 1431 | _bfd_error_handler |
695344c0 | 1432 | /* xgettext:c-format */ |
5522f910 NC |
1433 | (_("%B: Failed to find info section for section %d"), obfd, secnum); |
1434 | } | |
1435 | ||
1436 | return changed; | |
1437 | } | |
1438 | ||
0ac4564e L |
1439 | /* Copy the program header and other data from one object module to |
1440 | another. */ | |
252b5132 | 1441 | |
b34976b6 | 1442 | bfd_boolean |
217aa764 | 1443 | _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
2d502050 | 1444 | { |
5522f910 NC |
1445 | const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); |
1446 | Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); | |
1447 | const struct elf_backend_data *bed; | |
84865015 NC |
1448 | unsigned int i; |
1449 | ||
2d502050 | 1450 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
84865015 | 1451 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
b34976b6 | 1452 | return TRUE; |
2d502050 | 1453 | |
57b828ef L |
1454 | if (!elf_flags_init (obfd)) |
1455 | { | |
1456 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
1457 | elf_flags_init (obfd) = TRUE; | |
1458 | } | |
2d502050 | 1459 | |
0ac4564e | 1460 | elf_gp (obfd) = elf_gp (ibfd); |
57b828ef L |
1461 | |
1462 | /* Also copy the EI_OSABI field. */ | |
1463 | elf_elfheader (obfd)->e_ident[EI_OSABI] = | |
1464 | elf_elfheader (ibfd)->e_ident[EI_OSABI]; | |
104d59d1 | 1465 | |
5522f910 NC |
1466 | /* If set, copy the EI_ABIVERSION field. */ |
1467 | if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) | |
1468 | elf_elfheader (obfd)->e_ident[EI_ABIVERSION] | |
1469 | = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; | |
1470 | ||
104d59d1 JM |
1471 | /* Copy object attributes. */ |
1472 | _bfd_elf_copy_obj_attributes (ibfd, obfd); | |
63b9bbb7 | 1473 | |
84865015 NC |
1474 | if (iheaders == NULL || oheaders == NULL) |
1475 | return TRUE; | |
63b9bbb7 | 1476 | |
5522f910 NC |
1477 | bed = get_elf_backend_data (obfd); |
1478 | ||
1479 | /* Possibly copy other fields in the section header. */ | |
84865015 | 1480 | for (i = 1; i < elf_numsections (obfd); i++) |
63b9bbb7 | 1481 | { |
84865015 NC |
1482 | unsigned int j; |
1483 | Elf_Internal_Shdr * oheader = oheaders[i]; | |
63b9bbb7 | 1484 | |
5522f910 NC |
1485 | /* Ignore ordinary sections. SHT_NOBITS sections are considered however |
1486 | because of a special case need for generating separate debug info | |
1487 | files. See below for more details. */ | |
84865015 NC |
1488 | if (oheader == NULL |
1489 | || (oheader->sh_type != SHT_NOBITS | |
5522f910 NC |
1490 | && oheader->sh_type < SHT_LOOS)) |
1491 | continue; | |
1492 | ||
1493 | /* Ignore empty sections, and sections whose | |
1494 | fields have already been initialised. */ | |
1495 | if (oheader->sh_size == 0 | |
84865015 NC |
1496 | || (oheader->sh_info != 0 && oheader->sh_link != 0)) |
1497 | continue; | |
63b9bbb7 | 1498 | |
84865015 | 1499 | /* Scan for the matching section in the input bfd. |
5522f910 NC |
1500 | First we try for a direct mapping between the input and output sections. */ |
1501 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1502 | { | |
1503 | const Elf_Internal_Shdr * iheader = iheaders[j]; | |
1504 | ||
1505 | if (iheader == NULL) | |
1506 | continue; | |
1507 | ||
1508 | if (oheader->bfd_section != NULL | |
1509 | && iheader->bfd_section != NULL | |
1510 | && iheader->bfd_section->output_section != NULL | |
1511 | && iheader->bfd_section->output_section == oheader->bfd_section) | |
1512 | { | |
1513 | /* We have found a connection from the input section to the | |
1514 | output section. Attempt to copy the header fields. If | |
1515 | this fails then do not try any further sections - there | |
1516 | should only be a one-to-one mapping between input and output. */ | |
1517 | if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) | |
1518 | j = elf_numsections (ibfd); | |
1519 | break; | |
1520 | } | |
1521 | } | |
1522 | ||
1523 | if (j < elf_numsections (ibfd)) | |
1524 | continue; | |
1525 | ||
1526 | /* That failed. So try to deduce the corresponding input section. | |
84865015 NC |
1527 | Unfortunately we cannot compare names as the output string table |
1528 | is empty, so instead we check size, address and type. */ | |
1529 | for (j = 1; j < elf_numsections (ibfd); j++) | |
1530 | { | |
5522f910 | 1531 | const Elf_Internal_Shdr * iheader = iheaders[j]; |
84865015 | 1532 | |
5522f910 NC |
1533 | if (iheader == NULL) |
1534 | continue; | |
1535 | ||
1536 | /* Try matching fields in the input section's header. | |
1537 | Since --only-keep-debug turns all non-debug sections into | |
84865015 NC |
1538 | SHT_NOBITS sections, the output SHT_NOBITS type matches any |
1539 | input type. */ | |
1540 | if ((oheader->sh_type == SHT_NOBITS | |
1541 | || iheader->sh_type == oheader->sh_type) | |
5522f910 NC |
1542 | && (iheader->sh_flags & ~ SHF_INFO_LINK) |
1543 | == (oheader->sh_flags & ~ SHF_INFO_LINK) | |
84865015 NC |
1544 | && iheader->sh_addralign == oheader->sh_addralign |
1545 | && iheader->sh_entsize == oheader->sh_entsize | |
1546 | && iheader->sh_size == oheader->sh_size | |
1547 | && iheader->sh_addr == oheader->sh_addr | |
1548 | && (iheader->sh_info != oheader->sh_info | |
1549 | || iheader->sh_link != oheader->sh_link)) | |
63b9bbb7 | 1550 | { |
5522f910 NC |
1551 | if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) |
1552 | break; | |
63b9bbb7 NC |
1553 | } |
1554 | } | |
5522f910 NC |
1555 | |
1556 | if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) | |
1557 | { | |
1558 | /* Final attempt. Call the backend copy function | |
1559 | with a NULL input section. */ | |
1560 | if (bed->elf_backend_copy_special_section_fields != NULL) | |
1561 | bed->elf_backend_copy_special_section_fields (ibfd, obfd, NULL, oheader); | |
1562 | } | |
63b9bbb7 NC |
1563 | } |
1564 | ||
b34976b6 | 1565 | return TRUE; |
2d502050 L |
1566 | } |
1567 | ||
cedc298e L |
1568 | static const char * |
1569 | get_segment_type (unsigned int p_type) | |
1570 | { | |
1571 | const char *pt; | |
1572 | switch (p_type) | |
1573 | { | |
1574 | case PT_NULL: pt = "NULL"; break; | |
1575 | case PT_LOAD: pt = "LOAD"; break; | |
1576 | case PT_DYNAMIC: pt = "DYNAMIC"; break; | |
1577 | case PT_INTERP: pt = "INTERP"; break; | |
1578 | case PT_NOTE: pt = "NOTE"; break; | |
1579 | case PT_SHLIB: pt = "SHLIB"; break; | |
1580 | case PT_PHDR: pt = "PHDR"; break; | |
1581 | case PT_TLS: pt = "TLS"; break; | |
1582 | case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; | |
2b05f1b7 | 1583 | case PT_GNU_STACK: pt = "STACK"; break; |
cedc298e L |
1584 | case PT_GNU_RELRO: pt = "RELRO"; break; |
1585 | default: pt = NULL; break; | |
1586 | } | |
1587 | return pt; | |
1588 | } | |
1589 | ||
f0b79d91 L |
1590 | /* Print out the program headers. */ |
1591 | ||
b34976b6 | 1592 | bfd_boolean |
217aa764 | 1593 | _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) |
252b5132 | 1594 | { |
a50b1753 | 1595 | FILE *f = (FILE *) farg; |
252b5132 RH |
1596 | Elf_Internal_Phdr *p; |
1597 | asection *s; | |
1598 | bfd_byte *dynbuf = NULL; | |
1599 | ||
1600 | p = elf_tdata (abfd)->phdr; | |
1601 | if (p != NULL) | |
1602 | { | |
1603 | unsigned int i, c; | |
1604 | ||
1605 | fprintf (f, _("\nProgram Header:\n")); | |
1606 | c = elf_elfheader (abfd)->e_phnum; | |
1607 | for (i = 0; i < c; i++, p++) | |
1608 | { | |
cedc298e | 1609 | const char *pt = get_segment_type (p->p_type); |
252b5132 RH |
1610 | char buf[20]; |
1611 | ||
cedc298e | 1612 | if (pt == NULL) |
252b5132 | 1613 | { |
cedc298e L |
1614 | sprintf (buf, "0x%lx", p->p_type); |
1615 | pt = buf; | |
252b5132 | 1616 | } |
dc810e39 | 1617 | fprintf (f, "%8s off 0x", pt); |
60b89a18 | 1618 | bfd_fprintf_vma (abfd, f, p->p_offset); |
252b5132 | 1619 | fprintf (f, " vaddr 0x"); |
60b89a18 | 1620 | bfd_fprintf_vma (abfd, f, p->p_vaddr); |
252b5132 | 1621 | fprintf (f, " paddr 0x"); |
60b89a18 | 1622 | bfd_fprintf_vma (abfd, f, p->p_paddr); |
252b5132 RH |
1623 | fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); |
1624 | fprintf (f, " filesz 0x"); | |
60b89a18 | 1625 | bfd_fprintf_vma (abfd, f, p->p_filesz); |
252b5132 | 1626 | fprintf (f, " memsz 0x"); |
60b89a18 | 1627 | bfd_fprintf_vma (abfd, f, p->p_memsz); |
252b5132 RH |
1628 | fprintf (f, " flags %c%c%c", |
1629 | (p->p_flags & PF_R) != 0 ? 'r' : '-', | |
1630 | (p->p_flags & PF_W) != 0 ? 'w' : '-', | |
1631 | (p->p_flags & PF_X) != 0 ? 'x' : '-'); | |
dc810e39 AM |
1632 | if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) |
1633 | fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); | |
252b5132 RH |
1634 | fprintf (f, "\n"); |
1635 | } | |
1636 | } | |
1637 | ||
1638 | s = bfd_get_section_by_name (abfd, ".dynamic"); | |
1639 | if (s != NULL) | |
1640 | { | |
cb33740c | 1641 | unsigned int elfsec; |
dc810e39 | 1642 | unsigned long shlink; |
252b5132 RH |
1643 | bfd_byte *extdyn, *extdynend; |
1644 | size_t extdynsize; | |
217aa764 | 1645 | void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); |
252b5132 RH |
1646 | |
1647 | fprintf (f, _("\nDynamic Section:\n")); | |
1648 | ||
eea6121a | 1649 | if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) |
252b5132 RH |
1650 | goto error_return; |
1651 | ||
1652 | elfsec = _bfd_elf_section_from_bfd_section (abfd, s); | |
cb33740c | 1653 | if (elfsec == SHN_BAD) |
252b5132 | 1654 | goto error_return; |
dc810e39 | 1655 | shlink = elf_elfsections (abfd)[elfsec]->sh_link; |
252b5132 RH |
1656 | |
1657 | extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; | |
1658 | swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; | |
1659 | ||
1660 | extdyn = dynbuf; | |
06614111 NC |
1661 | /* PR 17512: file: 6f427532. */ |
1662 | if (s->size < extdynsize) | |
1663 | goto error_return; | |
eea6121a | 1664 | extdynend = extdyn + s->size; |
1036838a NC |
1665 | /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. |
1666 | Fix range check. */ | |
1667 | for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) | |
252b5132 RH |
1668 | { |
1669 | Elf_Internal_Dyn dyn; | |
ad9563d6 | 1670 | const char *name = ""; |
252b5132 | 1671 | char ab[20]; |
b34976b6 | 1672 | bfd_boolean stringp; |
ad9563d6 | 1673 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 1674 | |
217aa764 | 1675 | (*swap_dyn_in) (abfd, extdyn, &dyn); |
252b5132 RH |
1676 | |
1677 | if (dyn.d_tag == DT_NULL) | |
1678 | break; | |
1679 | ||
b34976b6 | 1680 | stringp = FALSE; |
252b5132 RH |
1681 | switch (dyn.d_tag) |
1682 | { | |
1683 | default: | |
ad9563d6 CM |
1684 | if (bed->elf_backend_get_target_dtag) |
1685 | name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); | |
1686 | ||
1687 | if (!strcmp (name, "")) | |
1688 | { | |
cd9af601 | 1689 | sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); |
ad9563d6 CM |
1690 | name = ab; |
1691 | } | |
252b5132 RH |
1692 | break; |
1693 | ||
b34976b6 | 1694 | case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; |
252b5132 RH |
1695 | case DT_PLTRELSZ: name = "PLTRELSZ"; break; |
1696 | case DT_PLTGOT: name = "PLTGOT"; break; | |
1697 | case DT_HASH: name = "HASH"; break; | |
1698 | case DT_STRTAB: name = "STRTAB"; break; | |
1699 | case DT_SYMTAB: name = "SYMTAB"; break; | |
1700 | case DT_RELA: name = "RELA"; break; | |
1701 | case DT_RELASZ: name = "RELASZ"; break; | |
1702 | case DT_RELAENT: name = "RELAENT"; break; | |
1703 | case DT_STRSZ: name = "STRSZ"; break; | |
1704 | case DT_SYMENT: name = "SYMENT"; break; | |
1705 | case DT_INIT: name = "INIT"; break; | |
1706 | case DT_FINI: name = "FINI"; break; | |
b34976b6 AM |
1707 | case DT_SONAME: name = "SONAME"; stringp = TRUE; break; |
1708 | case DT_RPATH: name = "RPATH"; stringp = TRUE; break; | |
252b5132 RH |
1709 | case DT_SYMBOLIC: name = "SYMBOLIC"; break; |
1710 | case DT_REL: name = "REL"; break; | |
1711 | case DT_RELSZ: name = "RELSZ"; break; | |
1712 | case DT_RELENT: name = "RELENT"; break; | |
1713 | case DT_PLTREL: name = "PLTREL"; break; | |
1714 | case DT_DEBUG: name = "DEBUG"; break; | |
1715 | case DT_TEXTREL: name = "TEXTREL"; break; | |
1716 | case DT_JMPREL: name = "JMPREL"; break; | |
94558834 L |
1717 | case DT_BIND_NOW: name = "BIND_NOW"; break; |
1718 | case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; | |
1719 | case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; | |
1720 | case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; | |
1721 | case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; | |
b34976b6 | 1722 | case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; |
94558834 L |
1723 | case DT_FLAGS: name = "FLAGS"; break; |
1724 | case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; | |
1725 | case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; | |
d48188b9 | 1726 | case DT_CHECKSUM: name = "CHECKSUM"; break; |
94558834 L |
1727 | case DT_PLTPADSZ: name = "PLTPADSZ"; break; |
1728 | case DT_MOVEENT: name = "MOVEENT"; break; | |
1729 | case DT_MOVESZ: name = "MOVESZ"; break; | |
1730 | case DT_FEATURE: name = "FEATURE"; break; | |
1731 | case DT_POSFLAG_1: name = "POSFLAG_1"; break; | |
1732 | case DT_SYMINSZ: name = "SYMINSZ"; break; | |
1733 | case DT_SYMINENT: name = "SYMINENT"; break; | |
b34976b6 AM |
1734 | case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; |
1735 | case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; | |
1736 | case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; | |
94558834 L |
1737 | case DT_PLTPAD: name = "PLTPAD"; break; |
1738 | case DT_MOVETAB: name = "MOVETAB"; break; | |
1739 | case DT_SYMINFO: name = "SYMINFO"; break; | |
1740 | case DT_RELACOUNT: name = "RELACOUNT"; break; | |
1741 | case DT_RELCOUNT: name = "RELCOUNT"; break; | |
1742 | case DT_FLAGS_1: name = "FLAGS_1"; break; | |
252b5132 RH |
1743 | case DT_VERSYM: name = "VERSYM"; break; |
1744 | case DT_VERDEF: name = "VERDEF"; break; | |
1745 | case DT_VERDEFNUM: name = "VERDEFNUM"; break; | |
1746 | case DT_VERNEED: name = "VERNEED"; break; | |
1747 | case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; | |
b34976b6 | 1748 | case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; |
94558834 | 1749 | case DT_USED: name = "USED"; break; |
b34976b6 | 1750 | case DT_FILTER: name = "FILTER"; stringp = TRUE; break; |
fdc90cb4 | 1751 | case DT_GNU_HASH: name = "GNU_HASH"; break; |
252b5132 RH |
1752 | } |
1753 | ||
ad9563d6 | 1754 | fprintf (f, " %-20s ", name); |
252b5132 | 1755 | if (! stringp) |
a1f3c56e AN |
1756 | { |
1757 | fprintf (f, "0x"); | |
1758 | bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); | |
1759 | } | |
252b5132 RH |
1760 | else |
1761 | { | |
1762 | const char *string; | |
dc810e39 | 1763 | unsigned int tagv = dyn.d_un.d_val; |
252b5132 | 1764 | |
dc810e39 | 1765 | string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); |
252b5132 RH |
1766 | if (string == NULL) |
1767 | goto error_return; | |
1768 | fprintf (f, "%s", string); | |
1769 | } | |
1770 | fprintf (f, "\n"); | |
1771 | } | |
1772 | ||
1773 | free (dynbuf); | |
1774 | dynbuf = NULL; | |
1775 | } | |
1776 | ||
1777 | if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) | |
1778 | || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) | |
1779 | { | |
fc0e6df6 | 1780 | if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) |
b34976b6 | 1781 | return FALSE; |
252b5132 RH |
1782 | } |
1783 | ||
1784 | if (elf_dynverdef (abfd) != 0) | |
1785 | { | |
1786 | Elf_Internal_Verdef *t; | |
1787 | ||
1788 | fprintf (f, _("\nVersion definitions:\n")); | |
1789 | for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) | |
1790 | { | |
1791 | fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, | |
d0fb9a8d JJ |
1792 | t->vd_flags, t->vd_hash, |
1793 | t->vd_nodename ? t->vd_nodename : "<corrupt>"); | |
1794 | if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) | |
252b5132 RH |
1795 | { |
1796 | Elf_Internal_Verdaux *a; | |
1797 | ||
1798 | fprintf (f, "\t"); | |
1799 | for (a = t->vd_auxptr->vda_nextptr; | |
1800 | a != NULL; | |
1801 | a = a->vda_nextptr) | |
d0fb9a8d JJ |
1802 | fprintf (f, "%s ", |
1803 | a->vda_nodename ? a->vda_nodename : "<corrupt>"); | |
252b5132 RH |
1804 | fprintf (f, "\n"); |
1805 | } | |
1806 | } | |
1807 | } | |
1808 | ||
1809 | if (elf_dynverref (abfd) != 0) | |
1810 | { | |
1811 | Elf_Internal_Verneed *t; | |
1812 | ||
1813 | fprintf (f, _("\nVersion References:\n")); | |
1814 | for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) | |
1815 | { | |
1816 | Elf_Internal_Vernaux *a; | |
1817 | ||
d0fb9a8d JJ |
1818 | fprintf (f, _(" required from %s:\n"), |
1819 | t->vn_filename ? t->vn_filename : "<corrupt>"); | |
252b5132 RH |
1820 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) |
1821 | fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, | |
d0fb9a8d JJ |
1822 | a->vna_flags, a->vna_other, |
1823 | a->vna_nodename ? a->vna_nodename : "<corrupt>"); | |
252b5132 RH |
1824 | } |
1825 | } | |
1826 | ||
b34976b6 | 1827 | return TRUE; |
252b5132 RH |
1828 | |
1829 | error_return: | |
1830 | if (dynbuf != NULL) | |
1831 | free (dynbuf); | |
b34976b6 | 1832 | return FALSE; |
252b5132 RH |
1833 | } |
1834 | ||
bb4d2ac2 L |
1835 | /* Get version string. */ |
1836 | ||
1837 | const char * | |
60bb06bc L |
1838 | _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, |
1839 | bfd_boolean *hidden) | |
bb4d2ac2 L |
1840 | { |
1841 | const char *version_string = NULL; | |
1842 | if (elf_dynversym (abfd) != 0 | |
1843 | && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) | |
1844 | { | |
1845 | unsigned int vernum = ((elf_symbol_type *) symbol)->version; | |
1846 | ||
1847 | *hidden = (vernum & VERSYM_HIDDEN) != 0; | |
1848 | vernum &= VERSYM_VERSION; | |
1849 | ||
1850 | if (vernum == 0) | |
1851 | version_string = ""; | |
1852 | else if (vernum == 1) | |
1853 | version_string = "Base"; | |
1854 | else if (vernum <= elf_tdata (abfd)->cverdefs) | |
1855 | version_string = | |
1856 | elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; | |
1857 | else | |
1858 | { | |
1859 | Elf_Internal_Verneed *t; | |
1860 | ||
1861 | version_string = ""; | |
1862 | for (t = elf_tdata (abfd)->verref; | |
1863 | t != NULL; | |
1864 | t = t->vn_nextref) | |
1865 | { | |
1866 | Elf_Internal_Vernaux *a; | |
1867 | ||
1868 | for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) | |
1869 | { | |
1870 | if (a->vna_other == vernum) | |
1871 | { | |
1872 | version_string = a->vna_nodename; | |
1873 | break; | |
1874 | } | |
1875 | } | |
1876 | } | |
1877 | } | |
1878 | } | |
1879 | return version_string; | |
1880 | } | |
1881 | ||
252b5132 RH |
1882 | /* Display ELF-specific fields of a symbol. */ |
1883 | ||
1884 | void | |
217aa764 AM |
1885 | bfd_elf_print_symbol (bfd *abfd, |
1886 | void *filep, | |
1887 | asymbol *symbol, | |
1888 | bfd_print_symbol_type how) | |
252b5132 | 1889 | { |
a50b1753 | 1890 | FILE *file = (FILE *) filep; |
252b5132 RH |
1891 | switch (how) |
1892 | { | |
1893 | case bfd_print_symbol_name: | |
1894 | fprintf (file, "%s", symbol->name); | |
1895 | break; | |
1896 | case bfd_print_symbol_more: | |
1897 | fprintf (file, "elf "); | |
60b89a18 | 1898 | bfd_fprintf_vma (abfd, file, symbol->value); |
cd9af601 | 1899 | fprintf (file, " %x", symbol->flags); |
252b5132 RH |
1900 | break; |
1901 | case bfd_print_symbol_all: | |
1902 | { | |
4e8a9624 AM |
1903 | const char *section_name; |
1904 | const char *name = NULL; | |
9c5bfbb7 | 1905 | const struct elf_backend_data *bed; |
7a13edea | 1906 | unsigned char st_other; |
dbb410c3 | 1907 | bfd_vma val; |
bb4d2ac2 L |
1908 | const char *version_string; |
1909 | bfd_boolean hidden; | |
c044fabd | 1910 | |
252b5132 | 1911 | section_name = symbol->section ? symbol->section->name : "(*none*)"; |
587ff49e RH |
1912 | |
1913 | bed = get_elf_backend_data (abfd); | |
1914 | if (bed->elf_backend_print_symbol_all) | |
c044fabd | 1915 | name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); |
587ff49e RH |
1916 | |
1917 | if (name == NULL) | |
1918 | { | |
7ee38065 | 1919 | name = symbol->name; |
217aa764 | 1920 | bfd_print_symbol_vandf (abfd, file, symbol); |
587ff49e RH |
1921 | } |
1922 | ||
252b5132 RH |
1923 | fprintf (file, " %s\t", section_name); |
1924 | /* Print the "other" value for a symbol. For common symbols, | |
1925 | we've already printed the size; now print the alignment. | |
1926 | For other symbols, we have no specified alignment, and | |
1927 | we've printed the address; now print the size. */ | |
dcf6c779 | 1928 | if (symbol->section && bfd_is_com_section (symbol->section)) |
dbb410c3 AM |
1929 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; |
1930 | else | |
1931 | val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; | |
1932 | bfd_fprintf_vma (abfd, file, val); | |
252b5132 RH |
1933 | |
1934 | /* If we have version information, print it. */ | |
60bb06bc L |
1935 | version_string = _bfd_elf_get_symbol_version_string (abfd, |
1936 | symbol, | |
1937 | &hidden); | |
bb4d2ac2 | 1938 | if (version_string) |
252b5132 | 1939 | { |
bb4d2ac2 | 1940 | if (!hidden) |
252b5132 RH |
1941 | fprintf (file, " %-11s", version_string); |
1942 | else | |
1943 | { | |
1944 | int i; | |
1945 | ||
1946 | fprintf (file, " (%s)", version_string); | |
1947 | for (i = 10 - strlen (version_string); i > 0; --i) | |
1948 | putc (' ', file); | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | /* If the st_other field is not zero, print it. */ | |
7a13edea | 1953 | st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; |
c044fabd | 1954 | |
7a13edea NC |
1955 | switch (st_other) |
1956 | { | |
1957 | case 0: break; | |
1958 | case STV_INTERNAL: fprintf (file, " .internal"); break; | |
1959 | case STV_HIDDEN: fprintf (file, " .hidden"); break; | |
1960 | case STV_PROTECTED: fprintf (file, " .protected"); break; | |
1961 | default: | |
1962 | /* Some other non-defined flags are also present, so print | |
1963 | everything hex. */ | |
1964 | fprintf (file, " 0x%02x", (unsigned int) st_other); | |
1965 | } | |
252b5132 | 1966 | |
587ff49e | 1967 | fprintf (file, " %s", name); |
252b5132 RH |
1968 | } |
1969 | break; | |
1970 | } | |
1971 | } | |
252b5132 RH |
1972 | \f |
1973 | /* ELF .o/exec file reading */ | |
1974 | ||
c044fabd | 1975 | /* Create a new bfd section from an ELF section header. */ |
252b5132 | 1976 | |
b34976b6 | 1977 | bfd_boolean |
217aa764 | 1978 | bfd_section_from_shdr (bfd *abfd, unsigned int shindex) |
252b5132 | 1979 | { |
4fbb74a6 AM |
1980 | Elf_Internal_Shdr *hdr; |
1981 | Elf_Internal_Ehdr *ehdr; | |
1982 | const struct elf_backend_data *bed; | |
90937f86 | 1983 | const char *name; |
bf67003b NC |
1984 | bfd_boolean ret = TRUE; |
1985 | static bfd_boolean * sections_being_created = NULL; | |
5a4b0ccc | 1986 | static bfd * sections_being_created_abfd = NULL; |
bf67003b | 1987 | static unsigned int nesting = 0; |
252b5132 | 1988 | |
4fbb74a6 AM |
1989 | if (shindex >= elf_numsections (abfd)) |
1990 | return FALSE; | |
1991 | ||
bf67003b NC |
1992 | if (++ nesting > 3) |
1993 | { | |
1994 | /* PR17512: A corrupt ELF binary might contain a recursive group of | |
06614111 | 1995 | sections, with each the string indicies pointing to the next in the |
bf67003b NC |
1996 | loop. Detect this here, by refusing to load a section that we are |
1997 | already in the process of loading. We only trigger this test if | |
1998 | we have nested at least three sections deep as normal ELF binaries | |
5a4b0ccc NC |
1999 | can expect to recurse at least once. |
2000 | ||
2001 | FIXME: It would be better if this array was attached to the bfd, | |
2002 | rather than being held in a static pointer. */ | |
2003 | ||
2004 | if (sections_being_created_abfd != abfd) | |
2005 | sections_being_created = NULL; | |
bf67003b NC |
2006 | if (sections_being_created == NULL) |
2007 | { | |
2008 | /* FIXME: It would be more efficient to attach this array to the bfd somehow. */ | |
2009 | sections_being_created = (bfd_boolean *) | |
2010 | bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean)); | |
5a4b0ccc | 2011 | sections_being_created_abfd = abfd; |
bf67003b NC |
2012 | } |
2013 | if (sections_being_created [shindex]) | |
2014 | { | |
4eca0228 | 2015 | _bfd_error_handler |
bf67003b NC |
2016 | (_("%B: warning: loop in section dependencies detected"), abfd); |
2017 | return FALSE; | |
2018 | } | |
2019 | sections_being_created [shindex] = TRUE; | |
2020 | } | |
2021 | ||
4fbb74a6 AM |
2022 | hdr = elf_elfsections (abfd)[shindex]; |
2023 | ehdr = elf_elfheader (abfd); | |
2024 | name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, | |
1b3a8575 | 2025 | hdr->sh_name); |
933d961a | 2026 | if (name == NULL) |
bf67003b | 2027 | goto fail; |
252b5132 | 2028 | |
4fbb74a6 | 2029 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
2030 | switch (hdr->sh_type) |
2031 | { | |
2032 | case SHT_NULL: | |
2033 | /* Inactive section. Throw it away. */ | |
bf67003b | 2034 | goto success; |
252b5132 | 2035 | |
bf67003b NC |
2036 | case SHT_PROGBITS: /* Normal section with contents. */ |
2037 | case SHT_NOBITS: /* .bss section. */ | |
2038 | case SHT_HASH: /* .hash section. */ | |
2039 | case SHT_NOTE: /* .note section. */ | |
25e27870 L |
2040 | case SHT_INIT_ARRAY: /* .init_array section. */ |
2041 | case SHT_FINI_ARRAY: /* .fini_array section. */ | |
2042 | case SHT_PREINIT_ARRAY: /* .preinit_array section. */ | |
7f1204bb | 2043 | case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ |
fdc90cb4 | 2044 | case SHT_GNU_HASH: /* .gnu.hash section. */ |
bf67003b NC |
2045 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2046 | goto success; | |
252b5132 | 2047 | |
797fc050 | 2048 | case SHT_DYNAMIC: /* Dynamic linking information. */ |
6dc132d9 | 2049 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2050 | goto fail; |
2051 | ||
cfcac11d NC |
2052 | if (hdr->sh_link > elf_numsections (abfd)) |
2053 | { | |
caa83f8b | 2054 | /* PR 10478: Accept Solaris binaries with a sh_link |
cfcac11d NC |
2055 | field set to SHN_BEFORE or SHN_AFTER. */ |
2056 | switch (bfd_get_arch (abfd)) | |
2057 | { | |
caa83f8b | 2058 | case bfd_arch_i386: |
cfcac11d NC |
2059 | case bfd_arch_sparc: |
2060 | if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ | |
2061 | || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) | |
2062 | break; | |
2063 | /* Otherwise fall through. */ | |
2064 | default: | |
bf67003b | 2065 | goto fail; |
cfcac11d NC |
2066 | } |
2067 | } | |
2068 | else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) | |
bf67003b | 2069 | goto fail; |
cfcac11d | 2070 | else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) |
797fc050 AM |
2071 | { |
2072 | Elf_Internal_Shdr *dynsymhdr; | |
2073 | ||
2074 | /* The shared libraries distributed with hpux11 have a bogus | |
2075 | sh_link field for the ".dynamic" section. Find the | |
2076 | string table for the ".dynsym" section instead. */ | |
2077 | if (elf_dynsymtab (abfd) != 0) | |
2078 | { | |
2079 | dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; | |
2080 | hdr->sh_link = dynsymhdr->sh_link; | |
2081 | } | |
2082 | else | |
2083 | { | |
2084 | unsigned int i, num_sec; | |
2085 | ||
2086 | num_sec = elf_numsections (abfd); | |
2087 | for (i = 1; i < num_sec; i++) | |
2088 | { | |
2089 | dynsymhdr = elf_elfsections (abfd)[i]; | |
2090 | if (dynsymhdr->sh_type == SHT_DYNSYM) | |
2091 | { | |
2092 | hdr->sh_link = dynsymhdr->sh_link; | |
2093 | break; | |
2094 | } | |
2095 | } | |
2096 | } | |
2097 | } | |
bf67003b | 2098 | goto success; |
797fc050 | 2099 | |
bf67003b | 2100 | case SHT_SYMTAB: /* A symbol table. */ |
252b5132 | 2101 | if (elf_onesymtab (abfd) == shindex) |
bf67003b | 2102 | goto success; |
252b5132 | 2103 | |
a50b2160 | 2104 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2105 | goto fail; |
2106 | ||
3337c1e5 | 2107 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
eee3b786 AM |
2108 | { |
2109 | if (hdr->sh_size != 0) | |
bf67003b | 2110 | goto fail; |
eee3b786 AM |
2111 | /* Some assemblers erroneously set sh_info to one with a |
2112 | zero sh_size. ld sees this as a global symbol count | |
2113 | of (unsigned) -1. Fix it here. */ | |
2114 | hdr->sh_info = 0; | |
bf67003b | 2115 | goto success; |
eee3b786 | 2116 | } |
bf67003b | 2117 | |
16ad13ec NC |
2118 | /* PR 18854: A binary might contain more than one symbol table. |
2119 | Unusual, but possible. Warn, but continue. */ | |
2120 | if (elf_onesymtab (abfd) != 0) | |
2121 | { | |
4eca0228 | 2122 | _bfd_error_handler |
695344c0 | 2123 | /* xgettext:c-format */ |
63a5468a AM |
2124 | (_("%B: warning: multiple symbol tables detected" |
2125 | " - ignoring the table in section %u"), | |
16ad13ec NC |
2126 | abfd, shindex); |
2127 | goto success; | |
2128 | } | |
252b5132 | 2129 | elf_onesymtab (abfd) = shindex; |
6a40cf0c NC |
2130 | elf_symtab_hdr (abfd) = *hdr; |
2131 | elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); | |
252b5132 RH |
2132 | abfd->flags |= HAS_SYMS; |
2133 | ||
2134 | /* Sometimes a shared object will map in the symbol table. If | |
08a40648 AM |
2135 | SHF_ALLOC is set, and this is a shared object, then we also |
2136 | treat this section as a BFD section. We can not base the | |
2137 | decision purely on SHF_ALLOC, because that flag is sometimes | |
2138 | set in a relocatable object file, which would confuse the | |
2139 | linker. */ | |
252b5132 RH |
2140 | if ((hdr->sh_flags & SHF_ALLOC) != 0 |
2141 | && (abfd->flags & DYNAMIC) != 0 | |
6dc132d9 L |
2142 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2143 | shindex)) | |
bf67003b | 2144 | goto fail; |
252b5132 | 2145 | |
1b3a8575 AM |
2146 | /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we |
2147 | can't read symbols without that section loaded as well. It | |
2148 | is most likely specified by the next section header. */ | |
6a40cf0c NC |
2149 | { |
2150 | elf_section_list * entry; | |
2151 | unsigned int i, num_sec; | |
1b3a8575 | 2152 | |
6a40cf0c NC |
2153 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2154 | if (entry->hdr.sh_link == shindex) | |
2155 | goto success; | |
2156 | ||
2157 | num_sec = elf_numsections (abfd); | |
2158 | for (i = shindex + 1; i < num_sec; i++) | |
2159 | { | |
2160 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2161 | ||
2162 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX | |
2163 | && hdr2->sh_link == shindex) | |
2164 | break; | |
2165 | } | |
2166 | ||
2167 | if (i == num_sec) | |
2168 | for (i = 1; i < shindex; i++) | |
1b3a8575 AM |
2169 | { |
2170 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
6a40cf0c | 2171 | |
1b3a8575 AM |
2172 | if (hdr2->sh_type == SHT_SYMTAB_SHNDX |
2173 | && hdr2->sh_link == shindex) | |
2174 | break; | |
2175 | } | |
6a40cf0c NC |
2176 | |
2177 | if (i != shindex) | |
2178 | ret = bfd_section_from_shdr (abfd, i); | |
2179 | /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ | |
2180 | goto success; | |
2181 | } | |
252b5132 | 2182 | |
bf67003b | 2183 | case SHT_DYNSYM: /* A dynamic symbol table. */ |
252b5132 | 2184 | if (elf_dynsymtab (abfd) == shindex) |
bf67003b | 2185 | goto success; |
252b5132 | 2186 | |
a50b2160 | 2187 | if (hdr->sh_entsize != bed->s->sizeof_sym) |
bf67003b NC |
2188 | goto fail; |
2189 | ||
eee3b786 AM |
2190 | if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) |
2191 | { | |
2192 | if (hdr->sh_size != 0) | |
bf67003b NC |
2193 | goto fail; |
2194 | ||
eee3b786 AM |
2195 | /* Some linkers erroneously set sh_info to one with a |
2196 | zero sh_size. ld sees this as a global symbol count | |
2197 | of (unsigned) -1. Fix it here. */ | |
2198 | hdr->sh_info = 0; | |
bf67003b | 2199 | goto success; |
eee3b786 | 2200 | } |
bf67003b | 2201 | |
16ad13ec NC |
2202 | /* PR 18854: A binary might contain more than one dynamic symbol table. |
2203 | Unusual, but possible. Warn, but continue. */ | |
2204 | if (elf_dynsymtab (abfd) != 0) | |
2205 | { | |
4eca0228 | 2206 | _bfd_error_handler |
695344c0 | 2207 | /* xgettext:c-format */ |
63a5468a AM |
2208 | (_("%B: warning: multiple dynamic symbol tables detected" |
2209 | " - ignoring the table in section %u"), | |
16ad13ec NC |
2210 | abfd, shindex); |
2211 | goto success; | |
2212 | } | |
252b5132 RH |
2213 | elf_dynsymtab (abfd) = shindex; |
2214 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
2215 | elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2216 | abfd->flags |= HAS_SYMS; | |
2217 | ||
2218 | /* Besides being a symbol table, we also treat this as a regular | |
2219 | section, so that objcopy can handle it. */ | |
bf67003b NC |
2220 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2221 | goto success; | |
252b5132 | 2222 | |
bf67003b | 2223 | case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ |
6a40cf0c NC |
2224 | { |
2225 | elf_section_list * entry; | |
9ad5cbcf | 2226 | |
6a40cf0c NC |
2227 | for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) |
2228 | if (entry->ndx == shindex) | |
2229 | goto success; | |
2230 | ||
2231 | entry = bfd_alloc (abfd, sizeof * entry); | |
2232 | if (entry == NULL) | |
2233 | goto fail; | |
2234 | entry->ndx = shindex; | |
2235 | entry->hdr = * hdr; | |
2236 | entry->next = elf_symtab_shndx_list (abfd); | |
2237 | elf_symtab_shndx_list (abfd) = entry; | |
2238 | elf_elfsections (abfd)[shindex] = & entry->hdr; | |
2239 | goto success; | |
2240 | } | |
9ad5cbcf | 2241 | |
bf67003b | 2242 | case SHT_STRTAB: /* A string table. */ |
252b5132 | 2243 | if (hdr->bfd_section != NULL) |
bf67003b NC |
2244 | goto success; |
2245 | ||
252b5132 RH |
2246 | if (ehdr->e_shstrndx == shindex) |
2247 | { | |
2248 | elf_tdata (abfd)->shstrtab_hdr = *hdr; | |
2249 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
bf67003b | 2250 | goto success; |
252b5132 | 2251 | } |
bf67003b | 2252 | |
1b3a8575 AM |
2253 | if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) |
2254 | { | |
2255 | symtab_strtab: | |
2256 | elf_tdata (abfd)->strtab_hdr = *hdr; | |
2257 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
bf67003b | 2258 | goto success; |
1b3a8575 | 2259 | } |
bf67003b | 2260 | |
1b3a8575 AM |
2261 | if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) |
2262 | { | |
2263 | dynsymtab_strtab: | |
2264 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
2265 | hdr = &elf_tdata (abfd)->dynstrtab_hdr; | |
2266 | elf_elfsections (abfd)[shindex] = hdr; | |
2267 | /* We also treat this as a regular section, so that objcopy | |
2268 | can handle it. */ | |
bf67003b NC |
2269 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2270 | shindex); | |
2271 | goto success; | |
1b3a8575 | 2272 | } |
252b5132 | 2273 | |
1b3a8575 AM |
2274 | /* If the string table isn't one of the above, then treat it as a |
2275 | regular section. We need to scan all the headers to be sure, | |
2276 | just in case this strtab section appeared before the above. */ | |
2277 | if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) | |
2278 | { | |
2279 | unsigned int i, num_sec; | |
252b5132 | 2280 | |
1b3a8575 AM |
2281 | num_sec = elf_numsections (abfd); |
2282 | for (i = 1; i < num_sec; i++) | |
2283 | { | |
2284 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; | |
2285 | if (hdr2->sh_link == shindex) | |
2286 | { | |
933d961a JJ |
2287 | /* Prevent endless recursion on broken objects. */ |
2288 | if (i == shindex) | |
bf67003b | 2289 | goto fail; |
1b3a8575 | 2290 | if (! bfd_section_from_shdr (abfd, i)) |
bf67003b | 2291 | goto fail; |
1b3a8575 AM |
2292 | if (elf_onesymtab (abfd) == i) |
2293 | goto symtab_strtab; | |
2294 | if (elf_dynsymtab (abfd) == i) | |
2295 | goto dynsymtab_strtab; | |
2296 | } | |
2297 | } | |
2298 | } | |
bf67003b NC |
2299 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2300 | goto success; | |
252b5132 RH |
2301 | |
2302 | case SHT_REL: | |
2303 | case SHT_RELA: | |
2304 | /* *These* do a lot of work -- but build no sections! */ | |
2305 | { | |
2306 | asection *target_sect; | |
d4730f92 | 2307 | Elf_Internal_Shdr *hdr2, **p_hdr; |
9ad5cbcf | 2308 | unsigned int num_sec = elf_numsections (abfd); |
d4730f92 | 2309 | struct bfd_elf_section_data *esdt; |
252b5132 | 2310 | |
aa2ca951 JJ |
2311 | if (hdr->sh_entsize |
2312 | != (bfd_size_type) (hdr->sh_type == SHT_REL | |
a50b2160 | 2313 | ? bed->s->sizeof_rel : bed->s->sizeof_rela)) |
bf67003b | 2314 | goto fail; |
a50b2160 | 2315 | |
03ae5f59 | 2316 | /* Check for a bogus link to avoid crashing. */ |
4fbb74a6 | 2317 | if (hdr->sh_link >= num_sec) |
03ae5f59 | 2318 | { |
4eca0228 | 2319 | _bfd_error_handler |
695344c0 | 2320 | /* xgettext:c-format */ |
76cfced5 | 2321 | (_("%B: invalid link %u for reloc section %s (index %u)"), |
4eca0228 | 2322 | abfd, hdr->sh_link, name, shindex); |
bf67003b NC |
2323 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, |
2324 | shindex); | |
2325 | goto success; | |
03ae5f59 ILT |
2326 | } |
2327 | ||
252b5132 RH |
2328 | /* For some incomprehensible reason Oracle distributes |
2329 | libraries for Solaris in which some of the objects have | |
2330 | bogus sh_link fields. It would be nice if we could just | |
2331 | reject them, but, unfortunately, some people need to use | |
2332 | them. We scan through the section headers; if we find only | |
2333 | one suitable symbol table, we clobber the sh_link to point | |
83b89087 L |
2334 | to it. I hope this doesn't break anything. |
2335 | ||
2336 | Don't do it on executable nor shared library. */ | |
2337 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 | |
2338 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB | |
252b5132 RH |
2339 | && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) |
2340 | { | |
9ad5cbcf | 2341 | unsigned int scan; |
252b5132 RH |
2342 | int found; |
2343 | ||
2344 | found = 0; | |
9ad5cbcf | 2345 | for (scan = 1; scan < num_sec; scan++) |
252b5132 RH |
2346 | { |
2347 | if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB | |
2348 | || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) | |
2349 | { | |
2350 | if (found != 0) | |
2351 | { | |
2352 | found = 0; | |
2353 | break; | |
2354 | } | |
2355 | found = scan; | |
2356 | } | |
2357 | } | |
2358 | if (found != 0) | |
2359 | hdr->sh_link = found; | |
2360 | } | |
2361 | ||
2362 | /* Get the symbol table. */ | |
1b3a8575 AM |
2363 | if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB |
2364 | || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) | |
252b5132 | 2365 | && ! bfd_section_from_shdr (abfd, hdr->sh_link)) |
bf67003b | 2366 | goto fail; |
252b5132 RH |
2367 | |
2368 | /* If this reloc section does not use the main symbol table we | |
2369 | don't treat it as a reloc section. BFD can't adequately | |
2370 | represent such a section, so at least for now, we don't | |
c044fabd | 2371 | try. We just present it as a normal section. We also |
60bcf0fa | 2372 | can't use it as a reloc section if it points to the null |
83b89087 L |
2373 | section, an invalid section, another reloc section, or its |
2374 | sh_link points to the null section. */ | |
185ef66d | 2375 | if (hdr->sh_link != elf_onesymtab (abfd) |
83b89087 | 2376 | || hdr->sh_link == SHN_UNDEF |
185ef66d | 2377 | || hdr->sh_info == SHN_UNDEF |
185ef66d AM |
2378 | || hdr->sh_info >= num_sec |
2379 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL | |
2380 | || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) | |
bf67003b NC |
2381 | { |
2382 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2383 | shindex); | |
2384 | goto success; | |
2385 | } | |
252b5132 RH |
2386 | |
2387 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
bf67003b NC |
2388 | goto fail; |
2389 | ||
252b5132 RH |
2390 | target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); |
2391 | if (target_sect == NULL) | |
bf67003b | 2392 | goto fail; |
252b5132 | 2393 | |
d4730f92 BS |
2394 | esdt = elf_section_data (target_sect); |
2395 | if (hdr->sh_type == SHT_RELA) | |
2396 | p_hdr = &esdt->rela.hdr; | |
252b5132 | 2397 | else |
d4730f92 BS |
2398 | p_hdr = &esdt->rel.hdr; |
2399 | ||
06614111 NC |
2400 | /* PR 17512: file: 0b4f81b7. */ |
2401 | if (*p_hdr != NULL) | |
2402 | goto fail; | |
ef53be89 | 2403 | hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); |
d4730f92 | 2404 | if (hdr2 == NULL) |
bf67003b | 2405 | goto fail; |
252b5132 | 2406 | *hdr2 = *hdr; |
d4730f92 | 2407 | *p_hdr = hdr2; |
252b5132 | 2408 | elf_elfsections (abfd)[shindex] = hdr2; |
056bafd4 MR |
2409 | target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) |
2410 | * bed->s->int_rels_per_ext_rel); | |
252b5132 RH |
2411 | target_sect->flags |= SEC_RELOC; |
2412 | target_sect->relocation = NULL; | |
2413 | target_sect->rel_filepos = hdr->sh_offset; | |
bf572ba0 MM |
2414 | /* In the section to which the relocations apply, mark whether |
2415 | its relocations are of the REL or RELA variety. */ | |
72730e0c | 2416 | if (hdr->sh_size != 0) |
d4730f92 BS |
2417 | { |
2418 | if (hdr->sh_type == SHT_RELA) | |
2419 | target_sect->use_rela_p = 1; | |
2420 | } | |
252b5132 | 2421 | abfd->flags |= HAS_RELOC; |
bf67003b | 2422 | goto success; |
252b5132 | 2423 | } |
252b5132 RH |
2424 | |
2425 | case SHT_GNU_verdef: | |
2426 | elf_dynverdef (abfd) = shindex; | |
2427 | elf_tdata (abfd)->dynverdef_hdr = *hdr; | |
bf67003b NC |
2428 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2429 | goto success; | |
252b5132 RH |
2430 | |
2431 | case SHT_GNU_versym: | |
a50b2160 | 2432 | if (hdr->sh_entsize != sizeof (Elf_External_Versym)) |
bf67003b NC |
2433 | goto fail; |
2434 | ||
252b5132 RH |
2435 | elf_dynversym (abfd) = shindex; |
2436 | elf_tdata (abfd)->dynversym_hdr = *hdr; | |
bf67003b NC |
2437 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2438 | goto success; | |
252b5132 RH |
2439 | |
2440 | case SHT_GNU_verneed: | |
2441 | elf_dynverref (abfd) = shindex; | |
2442 | elf_tdata (abfd)->dynverref_hdr = *hdr; | |
bf67003b NC |
2443 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); |
2444 | goto success; | |
252b5132 RH |
2445 | |
2446 | case SHT_SHLIB: | |
bf67003b | 2447 | goto success; |
252b5132 | 2448 | |
dbb410c3 | 2449 | case SHT_GROUP: |
44534af3 | 2450 | if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) |
bf67003b NC |
2451 | goto fail; |
2452 | ||
6dc132d9 | 2453 | if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b NC |
2454 | goto fail; |
2455 | ||
bf67003b | 2456 | goto success; |
dbb410c3 | 2457 | |
252b5132 | 2458 | default: |
104d59d1 JM |
2459 | /* Possibly an attributes section. */ |
2460 | if (hdr->sh_type == SHT_GNU_ATTRIBUTES | |
2461 | || hdr->sh_type == bed->obj_attrs_section_type) | |
2462 | { | |
2463 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) | |
bf67003b | 2464 | goto fail; |
104d59d1 | 2465 | _bfd_elf_parse_attributes (abfd, hdr); |
bf67003b | 2466 | goto success; |
104d59d1 JM |
2467 | } |
2468 | ||
252b5132 | 2469 | /* Check for any processor-specific section types. */ |
3eb70a79 | 2470 | if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) |
bf67003b | 2471 | goto success; |
3eb70a79 L |
2472 | |
2473 | if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) | |
2474 | { | |
2475 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2476 | /* FIXME: How to properly handle allocated section reserved | |
2477 | for applications? */ | |
4eca0228 | 2478 | _bfd_error_handler |
695344c0 | 2479 | /* xgettext:c-format */ |
76cfced5 AM |
2480 | (_("%B: unknown type [%#x] section `%s'"), |
2481 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2482 | else |
bf67003b NC |
2483 | { |
2484 | /* Allow sections reserved for applications. */ | |
2485 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, | |
2486 | shindex); | |
2487 | goto success; | |
2488 | } | |
3eb70a79 L |
2489 | } |
2490 | else if (hdr->sh_type >= SHT_LOPROC | |
2491 | && hdr->sh_type <= SHT_HIPROC) | |
2492 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2493 | _bfd_error_handler |
695344c0 | 2494 | /* xgettext:c-format */ |
76cfced5 AM |
2495 | (_("%B: unknown type [%#x] section `%s'"), |
2496 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2497 | else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) |
ff15b240 NC |
2498 | { |
2499 | /* Unrecognised OS-specific sections. */ | |
2500 | if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) | |
2501 | /* SHF_OS_NONCONFORMING indicates that special knowledge is | |
08a40648 | 2502 | required to correctly process the section and the file should |
ff15b240 | 2503 | be rejected with an error message. */ |
4eca0228 | 2504 | _bfd_error_handler |
695344c0 | 2505 | /* xgettext:c-format */ |
76cfced5 AM |
2506 | (_("%B: unknown type [%#x] section `%s'"), |
2507 | abfd, hdr->sh_type, name); | |
ff15b240 | 2508 | else |
bf67003b NC |
2509 | { |
2510 | /* Otherwise it should be processed. */ | |
2511 | ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); | |
2512 | goto success; | |
2513 | } | |
ff15b240 | 2514 | } |
3eb70a79 L |
2515 | else |
2516 | /* FIXME: We should handle this section. */ | |
4eca0228 | 2517 | _bfd_error_handler |
695344c0 | 2518 | /* xgettext:c-format */ |
76cfced5 AM |
2519 | (_("%B: unknown type [%#x] section `%s'"), |
2520 | abfd, hdr->sh_type, name); | |
3eb70a79 | 2521 | |
bf67003b | 2522 | goto fail; |
252b5132 RH |
2523 | } |
2524 | ||
bf67003b NC |
2525 | fail: |
2526 | ret = FALSE; | |
2527 | success: | |
e5b470e2 | 2528 | if (sections_being_created && sections_being_created_abfd == abfd) |
bf67003b NC |
2529 | sections_being_created [shindex] = FALSE; |
2530 | if (-- nesting == 0) | |
5a4b0ccc NC |
2531 | { |
2532 | sections_being_created = NULL; | |
2533 | sections_being_created_abfd = abfd; | |
2534 | } | |
bf67003b | 2535 | return ret; |
252b5132 RH |
2536 | } |
2537 | ||
87d72d41 | 2538 | /* Return the local symbol specified by ABFD, R_SYMNDX. */ |
ec338859 | 2539 | |
87d72d41 AM |
2540 | Elf_Internal_Sym * |
2541 | bfd_sym_from_r_symndx (struct sym_cache *cache, | |
2542 | bfd *abfd, | |
2543 | unsigned long r_symndx) | |
ec338859 | 2544 | { |
ec338859 AM |
2545 | unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; |
2546 | ||
a5d1b3b5 AM |
2547 | if (cache->abfd != abfd || cache->indx[ent] != r_symndx) |
2548 | { | |
2549 | Elf_Internal_Shdr *symtab_hdr; | |
2550 | unsigned char esym[sizeof (Elf64_External_Sym)]; | |
2551 | Elf_External_Sym_Shndx eshndx; | |
ec338859 | 2552 | |
a5d1b3b5 AM |
2553 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2554 | if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, | |
87d72d41 | 2555 | &cache->sym[ent], esym, &eshndx) == NULL) |
a5d1b3b5 | 2556 | return NULL; |
9ad5cbcf | 2557 | |
a5d1b3b5 AM |
2558 | if (cache->abfd != abfd) |
2559 | { | |
2560 | memset (cache->indx, -1, sizeof (cache->indx)); | |
2561 | cache->abfd = abfd; | |
2562 | } | |
2563 | cache->indx[ent] = r_symndx; | |
ec338859 | 2564 | } |
a5d1b3b5 | 2565 | |
87d72d41 | 2566 | return &cache->sym[ent]; |
ec338859 AM |
2567 | } |
2568 | ||
252b5132 RH |
2569 | /* Given an ELF section number, retrieve the corresponding BFD |
2570 | section. */ | |
2571 | ||
2572 | asection * | |
91d6fa6a | 2573 | bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) |
252b5132 | 2574 | { |
91d6fa6a | 2575 | if (sec_index >= elf_numsections (abfd)) |
252b5132 | 2576 | return NULL; |
91d6fa6a | 2577 | return elf_elfsections (abfd)[sec_index]->bfd_section; |
252b5132 RH |
2578 | } |
2579 | ||
b35d266b | 2580 | static const struct bfd_elf_special_section special_sections_b[] = |
2f89ff8d | 2581 | { |
0112cd26 NC |
2582 | { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
2583 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2584 | }; |
2585 | ||
b35d266b | 2586 | static const struct bfd_elf_special_section special_sections_c[] = |
7f4d3958 | 2587 | { |
0112cd26 NC |
2588 | { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, |
2589 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2590 | }; |
2591 | ||
b35d266b | 2592 | static const struct bfd_elf_special_section special_sections_d[] = |
7f4d3958 | 2593 | { |
0112cd26 NC |
2594 | { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2595 | { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, | |
a9a72a65 DE |
2596 | /* There are more DWARF sections than these, but they needn't be added here |
2597 | unless you have to cope with broken compilers that don't emit section | |
2598 | attributes or you want to help the user writing assembler. */ | |
0112cd26 NC |
2599 | { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, |
2600 | { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, | |
2601 | { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, | |
2602 | { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2603 | { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2604 | { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, | |
2605 | { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, | |
2606 | { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, | |
2607 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2608 | }; |
2609 | ||
b35d266b | 2610 | static const struct bfd_elf_special_section special_sections_f[] = |
7f4d3958 | 2611 | { |
6f9dbcd4 AM |
2612 | { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2613 | { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2614 | { NULL, 0 , 0, 0, 0 } | |
7f4d3958 L |
2615 | }; |
2616 | ||
b35d266b | 2617 | static const struct bfd_elf_special_section special_sections_g[] = |
7f4d3958 | 2618 | { |
0112cd26 | 2619 | { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, |
68efed41 | 2620 | { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, |
0112cd26 NC |
2621 | { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, |
2622 | { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, | |
2623 | { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, | |
2624 | { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, | |
2625 | { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, | |
2626 | { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, | |
2627 | { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, | |
2628 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2629 | }; |
2630 | ||
b35d266b | 2631 | static const struct bfd_elf_special_section special_sections_h[] = |
7f4d3958 | 2632 | { |
0112cd26 NC |
2633 | { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, |
2634 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2635 | }; |
2636 | ||
b35d266b | 2637 | static const struct bfd_elf_special_section special_sections_i[] = |
7f4d3958 | 2638 | { |
6f9dbcd4 AM |
2639 | { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2640 | { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, | |
2641 | { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, | |
2642 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2643 | }; |
2644 | ||
b35d266b | 2645 | static const struct bfd_elf_special_section special_sections_l[] = |
7f4d3958 | 2646 | { |
0112cd26 NC |
2647 | { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, |
2648 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2649 | }; |
2650 | ||
b35d266b | 2651 | static const struct bfd_elf_special_section special_sections_n[] = |
7f4d3958 | 2652 | { |
0112cd26 NC |
2653 | { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, |
2654 | { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, | |
2655 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2656 | }; |
2657 | ||
b35d266b | 2658 | static const struct bfd_elf_special_section special_sections_p[] = |
7f4d3958 | 2659 | { |
6f9dbcd4 AM |
2660 | { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, |
2661 | { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, | |
2662 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2663 | }; |
2664 | ||
b35d266b | 2665 | static const struct bfd_elf_special_section special_sections_r[] = |
7f4d3958 | 2666 | { |
0112cd26 NC |
2667 | { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, |
2668 | { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, | |
2669 | { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, | |
2670 | { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, | |
2671 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2672 | }; |
2673 | ||
b35d266b | 2674 | static const struct bfd_elf_special_section special_sections_s[] = |
7f4d3958 | 2675 | { |
0112cd26 NC |
2676 | { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, |
2677 | { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, | |
2678 | { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, | |
60ff4dc4 HPN |
2679 | /* See struct bfd_elf_special_section declaration for the semantics of |
2680 | this special case where .prefix_length != strlen (.prefix). */ | |
2681 | { ".stabstr", 5, 3, SHT_STRTAB, 0 }, | |
0112cd26 | 2682 | { NULL, 0, 0, 0, 0 } |
2f89ff8d L |
2683 | }; |
2684 | ||
b35d266b | 2685 | static const struct bfd_elf_special_section special_sections_t[] = |
7f4d3958 | 2686 | { |
0112cd26 NC |
2687 | { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, |
2688 | { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2689 | { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, | |
2690 | { NULL, 0, 0, 0, 0 } | |
7f4d3958 L |
2691 | }; |
2692 | ||
1b315056 CS |
2693 | static const struct bfd_elf_special_section special_sections_z[] = |
2694 | { | |
2695 | { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, | |
2696 | { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, | |
2697 | { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, | |
2698 | { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, | |
2699 | { NULL, 0, 0, 0, 0 } | |
2700 | }; | |
2701 | ||
e4c93b56 | 2702 | static const struct bfd_elf_special_section * const special_sections[] = |
7f4d3958 | 2703 | { |
7f4d3958 | 2704 | special_sections_b, /* 'b' */ |
98ece1b3 | 2705 | special_sections_c, /* 'c' */ |
7f4d3958 L |
2706 | special_sections_d, /* 'd' */ |
2707 | NULL, /* 'e' */ | |
2708 | special_sections_f, /* 'f' */ | |
2709 | special_sections_g, /* 'g' */ | |
2710 | special_sections_h, /* 'h' */ | |
2711 | special_sections_i, /* 'i' */ | |
2712 | NULL, /* 'j' */ | |
2713 | NULL, /* 'k' */ | |
2714 | special_sections_l, /* 'l' */ | |
2715 | NULL, /* 'm' */ | |
2716 | special_sections_n, /* 'n' */ | |
2717 | NULL, /* 'o' */ | |
2718 | special_sections_p, /* 'p' */ | |
2719 | NULL, /* 'q' */ | |
2720 | special_sections_r, /* 'r' */ | |
2721 | special_sections_s, /* 's' */ | |
2722 | special_sections_t, /* 't' */ | |
1b315056 CS |
2723 | NULL, /* 'u' */ |
2724 | NULL, /* 'v' */ | |
2725 | NULL, /* 'w' */ | |
2726 | NULL, /* 'x' */ | |
2727 | NULL, /* 'y' */ | |
2728 | special_sections_z /* 'z' */ | |
7f4d3958 L |
2729 | }; |
2730 | ||
551b43fd AM |
2731 | const struct bfd_elf_special_section * |
2732 | _bfd_elf_get_special_section (const char *name, | |
2733 | const struct bfd_elf_special_section *spec, | |
2734 | unsigned int rela) | |
2f89ff8d L |
2735 | { |
2736 | int i; | |
7f4d3958 | 2737 | int len; |
7f4d3958 | 2738 | |
551b43fd | 2739 | len = strlen (name); |
7f4d3958 | 2740 | |
551b43fd | 2741 | for (i = 0; spec[i].prefix != NULL; i++) |
7dcb9820 AM |
2742 | { |
2743 | int suffix_len; | |
551b43fd | 2744 | int prefix_len = spec[i].prefix_length; |
7dcb9820 AM |
2745 | |
2746 | if (len < prefix_len) | |
2747 | continue; | |
551b43fd | 2748 | if (memcmp (name, spec[i].prefix, prefix_len) != 0) |
7dcb9820 AM |
2749 | continue; |
2750 | ||
551b43fd | 2751 | suffix_len = spec[i].suffix_length; |
7dcb9820 AM |
2752 | if (suffix_len <= 0) |
2753 | { | |
2754 | if (name[prefix_len] != 0) | |
2755 | { | |
2756 | if (suffix_len == 0) | |
2757 | continue; | |
2758 | if (name[prefix_len] != '.' | |
2759 | && (suffix_len == -2 | |
551b43fd | 2760 | || (rela && spec[i].type == SHT_REL))) |
7dcb9820 AM |
2761 | continue; |
2762 | } | |
2763 | } | |
2764 | else | |
2765 | { | |
2766 | if (len < prefix_len + suffix_len) | |
2767 | continue; | |
2768 | if (memcmp (name + len - suffix_len, | |
551b43fd | 2769 | spec[i].prefix + prefix_len, |
7dcb9820 AM |
2770 | suffix_len) != 0) |
2771 | continue; | |
2772 | } | |
551b43fd | 2773 | return &spec[i]; |
7dcb9820 | 2774 | } |
2f89ff8d L |
2775 | |
2776 | return NULL; | |
2777 | } | |
2778 | ||
7dcb9820 | 2779 | const struct bfd_elf_special_section * |
29ef7005 | 2780 | _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) |
2f89ff8d | 2781 | { |
551b43fd AM |
2782 | int i; |
2783 | const struct bfd_elf_special_section *spec; | |
29ef7005 | 2784 | const struct elf_backend_data *bed; |
2f89ff8d L |
2785 | |
2786 | /* See if this is one of the special sections. */ | |
551b43fd AM |
2787 | if (sec->name == NULL) |
2788 | return NULL; | |
2f89ff8d | 2789 | |
29ef7005 L |
2790 | bed = get_elf_backend_data (abfd); |
2791 | spec = bed->special_sections; | |
2792 | if (spec) | |
2793 | { | |
2794 | spec = _bfd_elf_get_special_section (sec->name, | |
2795 | bed->special_sections, | |
2796 | sec->use_rela_p); | |
2797 | if (spec != NULL) | |
2798 | return spec; | |
2799 | } | |
2800 | ||
551b43fd AM |
2801 | if (sec->name[0] != '.') |
2802 | return NULL; | |
2f89ff8d | 2803 | |
551b43fd | 2804 | i = sec->name[1] - 'b'; |
1b315056 | 2805 | if (i < 0 || i > 'z' - 'b') |
551b43fd AM |
2806 | return NULL; |
2807 | ||
2808 | spec = special_sections[i]; | |
2f89ff8d | 2809 | |
551b43fd AM |
2810 | if (spec == NULL) |
2811 | return NULL; | |
2812 | ||
2813 | return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); | |
2f89ff8d L |
2814 | } |
2815 | ||
b34976b6 | 2816 | bfd_boolean |
217aa764 | 2817 | _bfd_elf_new_section_hook (bfd *abfd, asection *sec) |
252b5132 RH |
2818 | { |
2819 | struct bfd_elf_section_data *sdata; | |
551b43fd | 2820 | const struct elf_backend_data *bed; |
7dcb9820 | 2821 | const struct bfd_elf_special_section *ssect; |
252b5132 | 2822 | |
f0abc2a1 AM |
2823 | sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; |
2824 | if (sdata == NULL) | |
2825 | { | |
a50b1753 NC |
2826 | sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, |
2827 | sizeof (*sdata)); | |
f0abc2a1 AM |
2828 | if (sdata == NULL) |
2829 | return FALSE; | |
217aa764 | 2830 | sec->used_by_bfd = sdata; |
f0abc2a1 | 2831 | } |
bf572ba0 | 2832 | |
551b43fd AM |
2833 | /* Indicate whether or not this section should use RELA relocations. */ |
2834 | bed = get_elf_backend_data (abfd); | |
2835 | sec->use_rela_p = bed->default_use_rela_p; | |
2836 | ||
e843e0f8 L |
2837 | /* When we read a file, we don't need to set ELF section type and |
2838 | flags. They will be overridden in _bfd_elf_make_section_from_shdr | |
2839 | anyway. We will set ELF section type and flags for all linker | |
2840 | created sections. If user specifies BFD section flags, we will | |
2841 | set ELF section type and flags based on BFD section flags in | |
02ecc8e9 L |
2842 | elf_fake_sections. Special handling for .init_array/.fini_array |
2843 | output sections since they may contain .ctors/.dtors input | |
2844 | sections. We don't want _bfd_elf_init_private_section_data to | |
2845 | copy ELF section type from .ctors/.dtors input sections. */ | |
2846 | if (abfd->direction != read_direction | |
3496cb2a | 2847 | || (sec->flags & SEC_LINKER_CREATED) != 0) |
2f89ff8d | 2848 | { |
551b43fd | 2849 | ssect = (*bed->get_sec_type_attr) (abfd, sec); |
02ecc8e9 L |
2850 | if (ssect != NULL |
2851 | && (!sec->flags | |
2852 | || (sec->flags & SEC_LINKER_CREATED) != 0 | |
2853 | || ssect->type == SHT_INIT_ARRAY | |
2854 | || ssect->type == SHT_FINI_ARRAY)) | |
a31501e9 L |
2855 | { |
2856 | elf_section_type (sec) = ssect->type; | |
2857 | elf_section_flags (sec) = ssect->attr; | |
2858 | } | |
2f89ff8d L |
2859 | } |
2860 | ||
f592407e | 2861 | return _bfd_generic_new_section_hook (abfd, sec); |
252b5132 RH |
2862 | } |
2863 | ||
2864 | /* Create a new bfd section from an ELF program header. | |
2865 | ||
2866 | Since program segments have no names, we generate a synthetic name | |
2867 | of the form segment<NUM>, where NUM is generally the index in the | |
2868 | program header table. For segments that are split (see below) we | |
2869 | generate the names segment<NUM>a and segment<NUM>b. | |
2870 | ||
2871 | Note that some program segments may have a file size that is different than | |
2872 | (less than) the memory size. All this means is that at execution the | |
2873 | system must allocate the amount of memory specified by the memory size, | |
2874 | but only initialize it with the first "file size" bytes read from the | |
2875 | file. This would occur for example, with program segments consisting | |
2876 | of combined data+bss. | |
2877 | ||
2878 | To handle the above situation, this routine generates TWO bfd sections | |
2879 | for the single program segment. The first has the length specified by | |
2880 | the file size of the segment, and the second has the length specified | |
2881 | by the difference between the two sizes. In effect, the segment is split | |
d5191d0c | 2882 | into its initialized and uninitialized parts. |
252b5132 RH |
2883 | |
2884 | */ | |
2885 | ||
b34976b6 | 2886 | bfd_boolean |
217aa764 AM |
2887 | _bfd_elf_make_section_from_phdr (bfd *abfd, |
2888 | Elf_Internal_Phdr *hdr, | |
91d6fa6a | 2889 | int hdr_index, |
a50b1753 | 2890 | const char *type_name) |
252b5132 RH |
2891 | { |
2892 | asection *newsect; | |
2893 | char *name; | |
2894 | char namebuf[64]; | |
d4c88bbb | 2895 | size_t len; |
252b5132 RH |
2896 | int split; |
2897 | ||
2898 | split = ((hdr->p_memsz > 0) | |
2899 | && (hdr->p_filesz > 0) | |
2900 | && (hdr->p_memsz > hdr->p_filesz)); | |
d5191d0c AM |
2901 | |
2902 | if (hdr->p_filesz > 0) | |
252b5132 | 2903 | { |
91d6fa6a | 2904 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); |
d5191d0c | 2905 | len = strlen (namebuf) + 1; |
a50b1753 | 2906 | name = (char *) bfd_alloc (abfd, len); |
d5191d0c AM |
2907 | if (!name) |
2908 | return FALSE; | |
2909 | memcpy (name, namebuf, len); | |
2910 | newsect = bfd_make_section (abfd, name); | |
2911 | if (newsect == NULL) | |
2912 | return FALSE; | |
2913 | newsect->vma = hdr->p_vaddr; | |
2914 | newsect->lma = hdr->p_paddr; | |
2915 | newsect->size = hdr->p_filesz; | |
2916 | newsect->filepos = hdr->p_offset; | |
2917 | newsect->flags |= SEC_HAS_CONTENTS; | |
2918 | newsect->alignment_power = bfd_log2 (hdr->p_align); | |
2919 | if (hdr->p_type == PT_LOAD) | |
252b5132 | 2920 | { |
d5191d0c AM |
2921 | newsect->flags |= SEC_ALLOC; |
2922 | newsect->flags |= SEC_LOAD; | |
2923 | if (hdr->p_flags & PF_X) | |
2924 | { | |
2925 | /* FIXME: all we known is that it has execute PERMISSION, | |
2926 | may be data. */ | |
2927 | newsect->flags |= SEC_CODE; | |
2928 | } | |
2929 | } | |
2930 | if (!(hdr->p_flags & PF_W)) | |
2931 | { | |
2932 | newsect->flags |= SEC_READONLY; | |
252b5132 | 2933 | } |
252b5132 RH |
2934 | } |
2935 | ||
d5191d0c | 2936 | if (hdr->p_memsz > hdr->p_filesz) |
252b5132 | 2937 | { |
d5191d0c AM |
2938 | bfd_vma align; |
2939 | ||
91d6fa6a | 2940 | sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); |
d4c88bbb | 2941 | len = strlen (namebuf) + 1; |
a50b1753 | 2942 | name = (char *) bfd_alloc (abfd, len); |
252b5132 | 2943 | if (!name) |
b34976b6 | 2944 | return FALSE; |
d4c88bbb | 2945 | memcpy (name, namebuf, len); |
252b5132 RH |
2946 | newsect = bfd_make_section (abfd, name); |
2947 | if (newsect == NULL) | |
b34976b6 | 2948 | return FALSE; |
252b5132 RH |
2949 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
2950 | newsect->lma = hdr->p_paddr + hdr->p_filesz; | |
eea6121a | 2951 | newsect->size = hdr->p_memsz - hdr->p_filesz; |
d5191d0c AM |
2952 | newsect->filepos = hdr->p_offset + hdr->p_filesz; |
2953 | align = newsect->vma & -newsect->vma; | |
2954 | if (align == 0 || align > hdr->p_align) | |
2955 | align = hdr->p_align; | |
2956 | newsect->alignment_power = bfd_log2 (align); | |
252b5132 RH |
2957 | if (hdr->p_type == PT_LOAD) |
2958 | { | |
d5191d0c AM |
2959 | /* Hack for gdb. Segments that have not been modified do |
2960 | not have their contents written to a core file, on the | |
2961 | assumption that a debugger can find the contents in the | |
2962 | executable. We flag this case by setting the fake | |
2963 | section size to zero. Note that "real" bss sections will | |
2964 | always have their contents dumped to the core file. */ | |
2965 | if (bfd_get_format (abfd) == bfd_core) | |
2966 | newsect->size = 0; | |
252b5132 RH |
2967 | newsect->flags |= SEC_ALLOC; |
2968 | if (hdr->p_flags & PF_X) | |
2969 | newsect->flags |= SEC_CODE; | |
2970 | } | |
2971 | if (!(hdr->p_flags & PF_W)) | |
2972 | newsect->flags |= SEC_READONLY; | |
2973 | } | |
2974 | ||
b34976b6 | 2975 | return TRUE; |
252b5132 RH |
2976 | } |
2977 | ||
b34976b6 | 2978 | bfd_boolean |
91d6fa6a | 2979 | bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) |
20cfcaae | 2980 | { |
9c5bfbb7 | 2981 | const struct elf_backend_data *bed; |
20cfcaae NC |
2982 | |
2983 | switch (hdr->p_type) | |
2984 | { | |
2985 | case PT_NULL: | |
91d6fa6a | 2986 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); |
20cfcaae NC |
2987 | |
2988 | case PT_LOAD: | |
91d6fa6a | 2989 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"); |
20cfcaae NC |
2990 | |
2991 | case PT_DYNAMIC: | |
91d6fa6a | 2992 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); |
20cfcaae NC |
2993 | |
2994 | case PT_INTERP: | |
91d6fa6a | 2995 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); |
20cfcaae NC |
2996 | |
2997 | case PT_NOTE: | |
91d6fa6a | 2998 | if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) |
b34976b6 | 2999 | return FALSE; |
718175fa | 3000 | if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) |
b34976b6 AM |
3001 | return FALSE; |
3002 | return TRUE; | |
20cfcaae NC |
3003 | |
3004 | case PT_SHLIB: | |
91d6fa6a | 3005 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); |
20cfcaae NC |
3006 | |
3007 | case PT_PHDR: | |
91d6fa6a | 3008 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); |
20cfcaae | 3009 | |
811072d8 | 3010 | case PT_GNU_EH_FRAME: |
91d6fa6a | 3011 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, |
811072d8 RM |
3012 | "eh_frame_hdr"); |
3013 | ||
2b05f1b7 | 3014 | case PT_GNU_STACK: |
91d6fa6a | 3015 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); |
9ee5e499 | 3016 | |
8c37241b | 3017 | case PT_GNU_RELRO: |
91d6fa6a | 3018 | return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); |
8c37241b | 3019 | |
20cfcaae | 3020 | default: |
8c1acd09 | 3021 | /* Check for any processor-specific program segment types. */ |
20cfcaae | 3022 | bed = get_elf_backend_data (abfd); |
91d6fa6a | 3023 | return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); |
20cfcaae NC |
3024 | } |
3025 | } | |
3026 | ||
d4730f92 BS |
3027 | /* Return the REL_HDR for SEC, assuming there is only a single one, either |
3028 | REL or RELA. */ | |
3029 | ||
3030 | Elf_Internal_Shdr * | |
3031 | _bfd_elf_single_rel_hdr (asection *sec) | |
3032 | { | |
3033 | if (elf_section_data (sec)->rel.hdr) | |
3034 | { | |
3035 | BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); | |
3036 | return elf_section_data (sec)->rel.hdr; | |
3037 | } | |
3038 | else | |
3039 | return elf_section_data (sec)->rela.hdr; | |
3040 | } | |
3041 | ||
3e19fb8f L |
3042 | static bfd_boolean |
3043 | _bfd_elf_set_reloc_sh_name (bfd *abfd, | |
3044 | Elf_Internal_Shdr *rel_hdr, | |
3045 | const char *sec_name, | |
3046 | bfd_boolean use_rela_p) | |
3047 | { | |
3048 | char *name = (char *) bfd_alloc (abfd, | |
3049 | sizeof ".rela" + strlen (sec_name)); | |
3050 | if (name == NULL) | |
3051 | return FALSE; | |
3052 | ||
3053 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); | |
3054 | rel_hdr->sh_name = | |
3055 | (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, | |
3056 | FALSE); | |
3057 | if (rel_hdr->sh_name == (unsigned int) -1) | |
3058 | return FALSE; | |
3059 | ||
3060 | return TRUE; | |
3061 | } | |
3062 | ||
d4730f92 BS |
3063 | /* Allocate and initialize a section-header for a new reloc section, |
3064 | containing relocations against ASECT. It is stored in RELDATA. If | |
3065 | USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL | |
3066 | relocations. */ | |
23bc299b | 3067 | |
5d13b3b3 | 3068 | static bfd_boolean |
217aa764 | 3069 | _bfd_elf_init_reloc_shdr (bfd *abfd, |
d4730f92 | 3070 | struct bfd_elf_section_reloc_data *reldata, |
f6fe1ccd | 3071 | const char *sec_name, |
3e19fb8f L |
3072 | bfd_boolean use_rela_p, |
3073 | bfd_boolean delay_st_name_p) | |
23bc299b | 3074 | { |
d4730f92 | 3075 | Elf_Internal_Shdr *rel_hdr; |
9c5bfbb7 | 3076 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3077 | |
d4730f92 | 3078 | BFD_ASSERT (reldata->hdr == NULL); |
ef53be89 | 3079 | rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); |
d4730f92 | 3080 | reldata->hdr = rel_hdr; |
23bc299b | 3081 | |
3e19fb8f L |
3082 | if (delay_st_name_p) |
3083 | rel_hdr->sh_name = (unsigned int) -1; | |
3084 | else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, | |
3085 | use_rela_p)) | |
b34976b6 | 3086 | return FALSE; |
23bc299b MM |
3087 | rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
3088 | rel_hdr->sh_entsize = (use_rela_p | |
3089 | ? bed->s->sizeof_rela | |
3090 | : bed->s->sizeof_rel); | |
72de5009 | 3091 | rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
23bc299b MM |
3092 | rel_hdr->sh_flags = 0; |
3093 | rel_hdr->sh_addr = 0; | |
3094 | rel_hdr->sh_size = 0; | |
3095 | rel_hdr->sh_offset = 0; | |
3096 | ||
b34976b6 | 3097 | return TRUE; |
23bc299b MM |
3098 | } |
3099 | ||
94be91de JB |
3100 | /* Return the default section type based on the passed in section flags. */ |
3101 | ||
3102 | int | |
3103 | bfd_elf_get_default_section_type (flagword flags) | |
3104 | { | |
3105 | if ((flags & SEC_ALLOC) != 0 | |
2e76e85a | 3106 | && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) |
94be91de JB |
3107 | return SHT_NOBITS; |
3108 | return SHT_PROGBITS; | |
3109 | } | |
3110 | ||
d4730f92 BS |
3111 | struct fake_section_arg |
3112 | { | |
3113 | struct bfd_link_info *link_info; | |
3114 | bfd_boolean failed; | |
3115 | }; | |
3116 | ||
252b5132 RH |
3117 | /* Set up an ELF internal section header for a section. */ |
3118 | ||
252b5132 | 3119 | static void |
d4730f92 | 3120 | elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) |
252b5132 | 3121 | { |
d4730f92 | 3122 | struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; |
9c5bfbb7 | 3123 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 3124 | struct bfd_elf_section_data *esd = elf_section_data (asect); |
252b5132 | 3125 | Elf_Internal_Shdr *this_hdr; |
0414f35b | 3126 | unsigned int sh_type; |
0ce398f1 | 3127 | const char *name = asect->name; |
3e19fb8f | 3128 | bfd_boolean delay_st_name_p = FALSE; |
252b5132 | 3129 | |
d4730f92 | 3130 | if (arg->failed) |
252b5132 RH |
3131 | { |
3132 | /* We already failed; just get out of the bfd_map_over_sections | |
08a40648 | 3133 | loop. */ |
252b5132 RH |
3134 | return; |
3135 | } | |
3136 | ||
d4730f92 | 3137 | this_hdr = &esd->this_hdr; |
252b5132 | 3138 | |
f6fe1ccd | 3139 | if (arg->link_info) |
0ce398f1 | 3140 | { |
f6fe1ccd L |
3141 | /* ld: compress DWARF debug sections with names: .debug_*. */ |
3142 | if ((arg->link_info->compress_debug & COMPRESS_DEBUG) | |
3143 | && (asect->flags & SEC_DEBUGGING) | |
3144 | && name[1] == 'd' | |
3145 | && name[6] == '_') | |
3146 | { | |
3147 | /* Set SEC_ELF_COMPRESS to indicate this section should be | |
3148 | compressed. */ | |
3149 | asect->flags |= SEC_ELF_COMPRESS; | |
0ce398f1 | 3150 | |
dd905818 | 3151 | /* If this section will be compressed, delay adding section |
3e19fb8f L |
3152 | name to section name section after it is compressed in |
3153 | _bfd_elf_assign_file_positions_for_non_load. */ | |
3154 | delay_st_name_p = TRUE; | |
f6fe1ccd L |
3155 | } |
3156 | } | |
3157 | else if ((asect->flags & SEC_ELF_RENAME)) | |
3158 | { | |
3159 | /* objcopy: rename output DWARF debug section. */ | |
3160 | if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) | |
3161 | { | |
3162 | /* When we decompress or compress with SHF_COMPRESSED, | |
3163 | convert section name from .zdebug_* to .debug_* if | |
3164 | needed. */ | |
3165 | if (name[1] == 'z') | |
3166 | { | |
3167 | char *new_name = convert_zdebug_to_debug (abfd, name); | |
3168 | if (new_name == NULL) | |
3169 | { | |
3170 | arg->failed = TRUE; | |
3171 | return; | |
3172 | } | |
3173 | name = new_name; | |
3174 | } | |
3175 | } | |
3176 | else if (asect->compress_status == COMPRESS_SECTION_DONE) | |
0ce398f1 | 3177 | { |
f6fe1ccd L |
3178 | /* PR binutils/18087: Compression does not always make a |
3179 | section smaller. So only rename the section when | |
3180 | compression has actually taken place. If input section | |
3181 | name is .zdebug_*, we should never compress it again. */ | |
3182 | char *new_name = convert_debug_to_zdebug (abfd, name); | |
0ce398f1 L |
3183 | if (new_name == NULL) |
3184 | { | |
3185 | arg->failed = TRUE; | |
3186 | return; | |
3187 | } | |
f6fe1ccd L |
3188 | BFD_ASSERT (name[1] != 'z'); |
3189 | name = new_name; | |
0ce398f1 L |
3190 | } |
3191 | } | |
3192 | ||
3e19fb8f L |
3193 | if (delay_st_name_p) |
3194 | this_hdr->sh_name = (unsigned int) -1; | |
3195 | else | |
252b5132 | 3196 | { |
3e19fb8f L |
3197 | this_hdr->sh_name |
3198 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
3199 | name, FALSE); | |
3200 | if (this_hdr->sh_name == (unsigned int) -1) | |
3201 | { | |
3202 | arg->failed = TRUE; | |
3203 | return; | |
3204 | } | |
252b5132 RH |
3205 | } |
3206 | ||
a4d8e49b | 3207 | /* Don't clear sh_flags. Assembler may set additional bits. */ |
252b5132 RH |
3208 | |
3209 | if ((asect->flags & SEC_ALLOC) != 0 | |
3210 | || asect->user_set_vma) | |
3211 | this_hdr->sh_addr = asect->vma; | |
3212 | else | |
3213 | this_hdr->sh_addr = 0; | |
3214 | ||
3215 | this_hdr->sh_offset = 0; | |
eea6121a | 3216 | this_hdr->sh_size = asect->size; |
252b5132 | 3217 | this_hdr->sh_link = 0; |
c86934ce NC |
3218 | /* PR 17512: file: 0eb809fe, 8b0535ee. */ |
3219 | if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) | |
3220 | { | |
4eca0228 | 3221 | _bfd_error_handler |
695344c0 | 3222 | /* xgettext:c-format */ |
c86934ce | 3223 | (_("%B: error: Alignment power %d of section `%A' is too big"), |
c08bb8dd | 3224 | abfd, asect->alignment_power, asect); |
c86934ce NC |
3225 | arg->failed = TRUE; |
3226 | return; | |
3227 | } | |
72de5009 | 3228 | this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power; |
252b5132 RH |
3229 | /* The sh_entsize and sh_info fields may have been set already by |
3230 | copy_private_section_data. */ | |
3231 | ||
3232 | this_hdr->bfd_section = asect; | |
3233 | this_hdr->contents = NULL; | |
3234 | ||
3cddba1e L |
3235 | /* If the section type is unspecified, we set it based on |
3236 | asect->flags. */ | |
98ece1b3 AM |
3237 | if ((asect->flags & SEC_GROUP) != 0) |
3238 | sh_type = SHT_GROUP; | |
98ece1b3 | 3239 | else |
94be91de | 3240 | sh_type = bfd_elf_get_default_section_type (asect->flags); |
98ece1b3 | 3241 | |
3cddba1e | 3242 | if (this_hdr->sh_type == SHT_NULL) |
98ece1b3 AM |
3243 | this_hdr->sh_type = sh_type; |
3244 | else if (this_hdr->sh_type == SHT_NOBITS | |
3245 | && sh_type == SHT_PROGBITS | |
3246 | && (asect->flags & SEC_ALLOC) != 0) | |
3cddba1e | 3247 | { |
98ece1b3 AM |
3248 | /* Warn if we are changing a NOBITS section to PROGBITS, but |
3249 | allow the link to proceed. This can happen when users link | |
3250 | non-bss input sections to bss output sections, or emit data | |
3251 | to a bss output section via a linker script. */ | |
4eca0228 | 3252 | _bfd_error_handler |
58f0869b | 3253 | (_("warning: section `%A' type changed to PROGBITS"), asect); |
98ece1b3 | 3254 | this_hdr->sh_type = sh_type; |
3cddba1e L |
3255 | } |
3256 | ||
2f89ff8d | 3257 | switch (this_hdr->sh_type) |
252b5132 | 3258 | { |
2f89ff8d | 3259 | default: |
2f89ff8d L |
3260 | break; |
3261 | ||
3262 | case SHT_STRTAB: | |
2f89ff8d L |
3263 | case SHT_NOTE: |
3264 | case SHT_NOBITS: | |
3265 | case SHT_PROGBITS: | |
3266 | break; | |
606851fb AM |
3267 | |
3268 | case SHT_INIT_ARRAY: | |
3269 | case SHT_FINI_ARRAY: | |
3270 | case SHT_PREINIT_ARRAY: | |
3271 | this_hdr->sh_entsize = bed->s->arch_size / 8; | |
3272 | break; | |
2f89ff8d L |
3273 | |
3274 | case SHT_HASH: | |
c7ac6ff8 | 3275 | this_hdr->sh_entsize = bed->s->sizeof_hash_entry; |
2f89ff8d | 3276 | break; |
5de3bf90 | 3277 | |
2f89ff8d | 3278 | case SHT_DYNSYM: |
252b5132 | 3279 | this_hdr->sh_entsize = bed->s->sizeof_sym; |
2f89ff8d L |
3280 | break; |
3281 | ||
3282 | case SHT_DYNAMIC: | |
252b5132 | 3283 | this_hdr->sh_entsize = bed->s->sizeof_dyn; |
2f89ff8d L |
3284 | break; |
3285 | ||
3286 | case SHT_RELA: | |
3287 | if (get_elf_backend_data (abfd)->may_use_rela_p) | |
3288 | this_hdr->sh_entsize = bed->s->sizeof_rela; | |
3289 | break; | |
3290 | ||
3291 | case SHT_REL: | |
3292 | if (get_elf_backend_data (abfd)->may_use_rel_p) | |
3293 | this_hdr->sh_entsize = bed->s->sizeof_rel; | |
3294 | break; | |
3295 | ||
3296 | case SHT_GNU_versym: | |
252b5132 | 3297 | this_hdr->sh_entsize = sizeof (Elf_External_Versym); |
2f89ff8d L |
3298 | break; |
3299 | ||
3300 | case SHT_GNU_verdef: | |
252b5132 RH |
3301 | this_hdr->sh_entsize = 0; |
3302 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3303 | cverdefs. The linker will set cverdefs, but sh_info will be |
3304 | zero. */ | |
252b5132 RH |
3305 | if (this_hdr->sh_info == 0) |
3306 | this_hdr->sh_info = elf_tdata (abfd)->cverdefs; | |
3307 | else | |
3308 | BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 | |
3309 | || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); | |
2f89ff8d L |
3310 | break; |
3311 | ||
3312 | case SHT_GNU_verneed: | |
252b5132 RH |
3313 | this_hdr->sh_entsize = 0; |
3314 | /* objcopy or strip will copy over sh_info, but may not set | |
08a40648 AM |
3315 | cverrefs. The linker will set cverrefs, but sh_info will be |
3316 | zero. */ | |
252b5132 RH |
3317 | if (this_hdr->sh_info == 0) |
3318 | this_hdr->sh_info = elf_tdata (abfd)->cverrefs; | |
3319 | else | |
3320 | BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 | |
3321 | || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); | |
2f89ff8d L |
3322 | break; |
3323 | ||
3324 | case SHT_GROUP: | |
1783205a | 3325 | this_hdr->sh_entsize = GRP_ENTRY_SIZE; |
2f89ff8d | 3326 | break; |
fdc90cb4 JJ |
3327 | |
3328 | case SHT_GNU_HASH: | |
3329 | this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; | |
3330 | break; | |
dbb410c3 | 3331 | } |
252b5132 RH |
3332 | |
3333 | if ((asect->flags & SEC_ALLOC) != 0) | |
3334 | this_hdr->sh_flags |= SHF_ALLOC; | |
3335 | if ((asect->flags & SEC_READONLY) == 0) | |
3336 | this_hdr->sh_flags |= SHF_WRITE; | |
3337 | if ((asect->flags & SEC_CODE) != 0) | |
3338 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
f5fa8ca2 JJ |
3339 | if ((asect->flags & SEC_MERGE) != 0) |
3340 | { | |
3341 | this_hdr->sh_flags |= SHF_MERGE; | |
3342 | this_hdr->sh_entsize = asect->entsize; | |
f5fa8ca2 | 3343 | } |
84865015 NC |
3344 | if ((asect->flags & SEC_STRINGS) != 0) |
3345 | this_hdr->sh_flags |= SHF_STRINGS; | |
1126897b | 3346 | if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) |
dbb410c3 | 3347 | this_hdr->sh_flags |= SHF_GROUP; |
13ae64f3 | 3348 | if ((asect->flags & SEC_THREAD_LOCAL) != 0) |
704afa60 JJ |
3349 | { |
3350 | this_hdr->sh_flags |= SHF_TLS; | |
3a800eb9 AM |
3351 | if (asect->size == 0 |
3352 | && (asect->flags & SEC_HAS_CONTENTS) == 0) | |
704afa60 | 3353 | { |
3a800eb9 | 3354 | struct bfd_link_order *o = asect->map_tail.link_order; |
b34976b6 | 3355 | |
704afa60 | 3356 | this_hdr->sh_size = 0; |
3a800eb9 AM |
3357 | if (o != NULL) |
3358 | { | |
704afa60 | 3359 | this_hdr->sh_size = o->offset + o->size; |
3a800eb9 AM |
3360 | if (this_hdr->sh_size != 0) |
3361 | this_hdr->sh_type = SHT_NOBITS; | |
3362 | } | |
704afa60 JJ |
3363 | } |
3364 | } | |
18ae9cc1 L |
3365 | if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) |
3366 | this_hdr->sh_flags |= SHF_EXCLUDE; | |
252b5132 | 3367 | |
d4730f92 BS |
3368 | /* If the section has relocs, set up a section header for the |
3369 | SHT_REL[A] section. If two relocation sections are required for | |
3370 | this section, it is up to the processor-specific back-end to | |
3371 | create the other. */ | |
3372 | if ((asect->flags & SEC_RELOC) != 0) | |
3373 | { | |
3374 | /* When doing a relocatable link, create both REL and RELA sections if | |
3375 | needed. */ | |
3376 | if (arg->link_info | |
3377 | /* Do the normal setup if we wouldn't create any sections here. */ | |
3378 | && esd->rel.count + esd->rela.count > 0 | |
0e1862bb L |
3379 | && (bfd_link_relocatable (arg->link_info) |
3380 | || arg->link_info->emitrelocations)) | |
d4730f92 BS |
3381 | { |
3382 | if (esd->rel.count && esd->rel.hdr == NULL | |
3e19fb8f L |
3383 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE, |
3384 | delay_st_name_p)) | |
d4730f92 BS |
3385 | { |
3386 | arg->failed = TRUE; | |
3387 | return; | |
3388 | } | |
3389 | if (esd->rela.count && esd->rela.hdr == NULL | |
3e19fb8f L |
3390 | && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE, |
3391 | delay_st_name_p)) | |
d4730f92 BS |
3392 | { |
3393 | arg->failed = TRUE; | |
3394 | return; | |
3395 | } | |
3396 | } | |
3397 | else if (!_bfd_elf_init_reloc_shdr (abfd, | |
3398 | (asect->use_rela_p | |
3399 | ? &esd->rela : &esd->rel), | |
f6fe1ccd | 3400 | name, |
3e19fb8f L |
3401 | asect->use_rela_p, |
3402 | delay_st_name_p)) | |
d4730f92 BS |
3403 | arg->failed = TRUE; |
3404 | } | |
3405 | ||
252b5132 | 3406 | /* Check for processor-specific section types. */ |
0414f35b | 3407 | sh_type = this_hdr->sh_type; |
e1fddb6b AO |
3408 | if (bed->elf_backend_fake_sections |
3409 | && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) | |
d4730f92 | 3410 | arg->failed = TRUE; |
252b5132 | 3411 | |
42bb2e33 | 3412 | if (sh_type == SHT_NOBITS && asect->size != 0) |
0414f35b AM |
3413 | { |
3414 | /* Don't change the header type from NOBITS if we are being | |
42bb2e33 | 3415 | called for objcopy --only-keep-debug. */ |
0414f35b AM |
3416 | this_hdr->sh_type = sh_type; |
3417 | } | |
252b5132 RH |
3418 | } |
3419 | ||
bcacc0f5 AM |
3420 | /* Fill in the contents of a SHT_GROUP section. Called from |
3421 | _bfd_elf_compute_section_file_positions for gas, objcopy, and | |
3422 | when ELF targets use the generic linker, ld. Called for ld -r | |
3423 | from bfd_elf_final_link. */ | |
dbb410c3 | 3424 | |
1126897b | 3425 | void |
217aa764 | 3426 | bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) |
dbb410c3 | 3427 | { |
a50b1753 | 3428 | bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; |
9dce4196 | 3429 | asection *elt, *first; |
dbb410c3 | 3430 | unsigned char *loc; |
b34976b6 | 3431 | bfd_boolean gas; |
dbb410c3 | 3432 | |
7e4111ad L |
3433 | /* Ignore linker created group section. See elfNN_ia64_object_p in |
3434 | elfxx-ia64.c. */ | |
3435 | if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) | |
dbb410c3 AM |
3436 | || *failedptr) |
3437 | return; | |
3438 | ||
bcacc0f5 AM |
3439 | if (elf_section_data (sec)->this_hdr.sh_info == 0) |
3440 | { | |
3441 | unsigned long symindx = 0; | |
3442 | ||
3443 | /* elf_group_id will have been set up by objcopy and the | |
3444 | generic linker. */ | |
3445 | if (elf_group_id (sec) != NULL) | |
3446 | symindx = elf_group_id (sec)->udata.i; | |
1126897b | 3447 | |
bcacc0f5 AM |
3448 | if (symindx == 0) |
3449 | { | |
3450 | /* If called from the assembler, swap_out_syms will have set up | |
3451 | elf_section_syms. */ | |
3452 | BFD_ASSERT (elf_section_syms (abfd) != NULL); | |
3453 | symindx = elf_section_syms (abfd)[sec->index]->udata.i; | |
3454 | } | |
3455 | elf_section_data (sec)->this_hdr.sh_info = symindx; | |
3456 | } | |
3457 | else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) | |
1126897b | 3458 | { |
bcacc0f5 AM |
3459 | /* The ELF backend linker sets sh_info to -2 when the group |
3460 | signature symbol is global, and thus the index can't be | |
3461 | set until all local symbols are output. */ | |
53720c49 AM |
3462 | asection *igroup; |
3463 | struct bfd_elf_section_data *sec_data; | |
3464 | unsigned long symndx; | |
3465 | unsigned long extsymoff; | |
bcacc0f5 AM |
3466 | struct elf_link_hash_entry *h; |
3467 | ||
53720c49 AM |
3468 | /* The point of this little dance to the first SHF_GROUP section |
3469 | then back to the SHT_GROUP section is that this gets us to | |
3470 | the SHT_GROUP in the input object. */ | |
3471 | igroup = elf_sec_group (elf_next_in_group (sec)); | |
3472 | sec_data = elf_section_data (igroup); | |
3473 | symndx = sec_data->this_hdr.sh_info; | |
3474 | extsymoff = 0; | |
bcacc0f5 AM |
3475 | if (!elf_bad_symtab (igroup->owner)) |
3476 | { | |
3477 | Elf_Internal_Shdr *symtab_hdr; | |
3478 | ||
3479 | symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; | |
3480 | extsymoff = symtab_hdr->sh_info; | |
3481 | } | |
3482 | h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; | |
3483 | while (h->root.type == bfd_link_hash_indirect | |
3484 | || h->root.type == bfd_link_hash_warning) | |
3485 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
3486 | ||
3487 | elf_section_data (sec)->this_hdr.sh_info = h->indx; | |
1126897b | 3488 | } |
dbb410c3 | 3489 | |
1126897b | 3490 | /* The contents won't be allocated for "ld -r" or objcopy. */ |
b34976b6 | 3491 | gas = TRUE; |
dbb410c3 AM |
3492 | if (sec->contents == NULL) |
3493 | { | |
b34976b6 | 3494 | gas = FALSE; |
a50b1753 | 3495 | sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); |
9dce4196 AM |
3496 | |
3497 | /* Arrange for the section to be written out. */ | |
3498 | elf_section_data (sec)->this_hdr.contents = sec->contents; | |
dbb410c3 AM |
3499 | if (sec->contents == NULL) |
3500 | { | |
b34976b6 | 3501 | *failedptr = TRUE; |
dbb410c3 AM |
3502 | return; |
3503 | } | |
3504 | } | |
3505 | ||
eea6121a | 3506 | loc = sec->contents + sec->size; |
dbb410c3 | 3507 | |
9dce4196 AM |
3508 | /* Get the pointer to the first section in the group that gas |
3509 | squirreled away here. objcopy arranges for this to be set to the | |
3510 | start of the input section group. */ | |
3511 | first = elt = elf_next_in_group (sec); | |
dbb410c3 AM |
3512 | |
3513 | /* First element is a flag word. Rest of section is elf section | |
3514 | indices for all the sections of the group. Write them backwards | |
3515 | just to keep the group in the same order as given in .section | |
3516 | directives, not that it matters. */ | |
3517 | while (elt != NULL) | |
3518 | { | |
9dce4196 | 3519 | asection *s; |
9dce4196 | 3520 | |
9dce4196 | 3521 | s = elt; |
415f38a6 AM |
3522 | if (!gas) |
3523 | s = s->output_section; | |
3524 | if (s != NULL | |
3525 | && !bfd_is_abs_section (s)) | |
01e1a5bc | 3526 | { |
415f38a6 AM |
3527 | unsigned int idx = elf_section_data (s)->this_idx; |
3528 | ||
01e1a5bc | 3529 | loc -= 4; |
01e1a5bc NC |
3530 | H_PUT_32 (abfd, idx, loc); |
3531 | } | |
945906ff | 3532 | elt = elf_next_in_group (elt); |
9dce4196 AM |
3533 | if (elt == first) |
3534 | break; | |
dbb410c3 AM |
3535 | } |
3536 | ||
7bdf4127 AB |
3537 | loc -= 4; |
3538 | BFD_ASSERT (loc == sec->contents); | |
dbb410c3 | 3539 | |
9dce4196 | 3540 | H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); |
dbb410c3 AM |
3541 | } |
3542 | ||
bce964aa AM |
3543 | /* Given NAME, the name of a relocation section stripped of its |
3544 | .rel/.rela prefix, return the section in ABFD to which the | |
3545 | relocations apply. */ | |
bd53a53a L |
3546 | |
3547 | asection * | |
bce964aa AM |
3548 | _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) |
3549 | { | |
3550 | /* If a target needs .got.plt section, relocations in rela.plt/rel.plt | |
3551 | section likely apply to .got.plt or .got section. */ | |
3552 | if (get_elf_backend_data (abfd)->want_got_plt | |
3553 | && strcmp (name, ".plt") == 0) | |
3554 | { | |
3555 | asection *sec; | |
3556 | ||
3557 | name = ".got.plt"; | |
3558 | sec = bfd_get_section_by_name (abfd, name); | |
3559 | if (sec != NULL) | |
3560 | return sec; | |
3561 | name = ".got"; | |
3562 | } | |
3563 | ||
3564 | return bfd_get_section_by_name (abfd, name); | |
3565 | } | |
3566 | ||
3567 | /* Return the section to which RELOC_SEC applies. */ | |
3568 | ||
3569 | static asection * | |
3570 | elf_get_reloc_section (asection *reloc_sec) | |
bd53a53a L |
3571 | { |
3572 | const char *name; | |
3573 | unsigned int type; | |
3574 | bfd *abfd; | |
bce964aa | 3575 | const struct elf_backend_data *bed; |
bd53a53a L |
3576 | |
3577 | type = elf_section_data (reloc_sec)->this_hdr.sh_type; | |
3578 | if (type != SHT_REL && type != SHT_RELA) | |
3579 | return NULL; | |
3580 | ||
3581 | /* We look up the section the relocs apply to by name. */ | |
3582 | name = reloc_sec->name; | |
bce964aa AM |
3583 | if (strncmp (name, ".rel", 4) != 0) |
3584 | return NULL; | |
3585 | name += 4; | |
3586 | if (type == SHT_RELA && *name++ != 'a') | |
3587 | return NULL; | |
bd53a53a | 3588 | |
bd53a53a | 3589 | abfd = reloc_sec->owner; |
bce964aa AM |
3590 | bed = get_elf_backend_data (abfd); |
3591 | return bed->get_reloc_section (abfd, name); | |
bd53a53a L |
3592 | } |
3593 | ||
252b5132 RH |
3594 | /* Assign all ELF section numbers. The dummy first section is handled here |
3595 | too. The link/info pointers for the standard section types are filled | |
3596 | in here too, while we're at it. */ | |
3597 | ||
b34976b6 | 3598 | static bfd_boolean |
da9f89d4 | 3599 | assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) |
252b5132 RH |
3600 | { |
3601 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
3602 | asection *sec; | |
3e19fb8f | 3603 | unsigned int section_number; |
252b5132 | 3604 | Elf_Internal_Shdr **i_shdrp; |
47cc2cf5 | 3605 | struct bfd_elf_section_data *d; |
3516e984 | 3606 | bfd_boolean need_symtab; |
252b5132 RH |
3607 | |
3608 | section_number = 1; | |
3609 | ||
2b0f7ef9 JJ |
3610 | _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); |
3611 | ||
da9f89d4 | 3612 | /* SHT_GROUP sections are in relocatable files only. */ |
7bdf4127 | 3613 | if (link_info == NULL || !link_info->resolve_section_groups) |
252b5132 | 3614 | { |
ef53be89 | 3615 | size_t reloc_count = 0; |
14f2c699 | 3616 | |
da9f89d4 | 3617 | /* Put SHT_GROUP sections first. */ |
04dd1667 | 3618 | for (sec = abfd->sections; sec != NULL; sec = sec->next) |
47cc2cf5 | 3619 | { |
5daa8fe7 | 3620 | d = elf_section_data (sec); |
da9f89d4 L |
3621 | |
3622 | if (d->this_hdr.sh_type == SHT_GROUP) | |
08a40648 | 3623 | { |
5daa8fe7 | 3624 | if (sec->flags & SEC_LINKER_CREATED) |
da9f89d4 L |
3625 | { |
3626 | /* Remove the linker created SHT_GROUP sections. */ | |
5daa8fe7 | 3627 | bfd_section_list_remove (abfd, sec); |
da9f89d4 | 3628 | abfd->section_count--; |
da9f89d4 | 3629 | } |
08a40648 | 3630 | else |
4fbb74a6 | 3631 | d->this_idx = section_number++; |
da9f89d4 | 3632 | } |
14f2c699 L |
3633 | |
3634 | /* Count relocations. */ | |
3635 | reloc_count += sec->reloc_count; | |
47cc2cf5 | 3636 | } |
14f2c699 L |
3637 | |
3638 | /* Clear HAS_RELOC if there are no relocations. */ | |
3639 | if (reloc_count == 0) | |
3640 | abfd->flags &= ~HAS_RELOC; | |
47cc2cf5 PB |
3641 | } |
3642 | ||
3643 | for (sec = abfd->sections; sec; sec = sec->next) | |
3644 | { | |
3645 | d = elf_section_data (sec); | |
3646 | ||
3647 | if (d->this_hdr.sh_type != SHT_GROUP) | |
4fbb74a6 | 3648 | d->this_idx = section_number++; |
3e19fb8f L |
3649 | if (d->this_hdr.sh_name != (unsigned int) -1) |
3650 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); | |
d4730f92 | 3651 | if (d->rel.hdr) |
2b0f7ef9 | 3652 | { |
d4730f92 | 3653 | d->rel.idx = section_number++; |
3e19fb8f L |
3654 | if (d->rel.hdr->sh_name != (unsigned int) -1) |
3655 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); | |
2b0f7ef9 | 3656 | } |
d4730f92 BS |
3657 | else |
3658 | d->rel.idx = 0; | |
23bc299b | 3659 | |
d4730f92 | 3660 | if (d->rela.hdr) |
2b0f7ef9 | 3661 | { |
d4730f92 | 3662 | d->rela.idx = section_number++; |
3e19fb8f L |
3663 | if (d->rela.hdr->sh_name != (unsigned int) -1) |
3664 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); | |
2b0f7ef9 | 3665 | } |
23bc299b | 3666 | else |
d4730f92 | 3667 | d->rela.idx = 0; |
252b5132 RH |
3668 | } |
3669 | ||
3516e984 L |
3670 | need_symtab = (bfd_get_symcount (abfd) > 0 |
3671 | || (link_info == NULL | |
3672 | && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
3673 | == HAS_RELOC))); | |
3674 | if (need_symtab) | |
252b5132 | 3675 | { |
12bd6957 | 3676 | elf_onesymtab (abfd) = section_number++; |
2b0f7ef9 | 3677 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); |
4fbb74a6 | 3678 | if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) |
9ad5cbcf | 3679 | { |
6a40cf0c NC |
3680 | elf_section_list * entry; |
3681 | ||
3682 | BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); | |
3683 | ||
3684 | entry = bfd_zalloc (abfd, sizeof * entry); | |
3685 | entry->ndx = section_number++; | |
3686 | elf_symtab_shndx_list (abfd) = entry; | |
3687 | entry->hdr.sh_name | |
9ad5cbcf | 3688 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), |
b34976b6 | 3689 | ".symtab_shndx", FALSE); |
6a40cf0c | 3690 | if (entry->hdr.sh_name == (unsigned int) -1) |
b34976b6 | 3691 | return FALSE; |
9ad5cbcf | 3692 | } |
12bd6957 | 3693 | elf_strtab_sec (abfd) = section_number++; |
2b0f7ef9 | 3694 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); |
252b5132 RH |
3695 | } |
3696 | ||
dd905818 NC |
3697 | elf_shstrtab_sec (abfd) = section_number++; |
3698 | _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); | |
3699 | elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); | |
3700 | ||
1c52a645 L |
3701 | if (section_number >= SHN_LORESERVE) |
3702 | { | |
695344c0 | 3703 | /* xgettext:c-format */ |
1c52a645 L |
3704 | _bfd_error_handler (_("%B: too many sections: %u"), |
3705 | abfd, section_number); | |
3706 | return FALSE; | |
3707 | } | |
3708 | ||
9ad5cbcf | 3709 | elf_numsections (abfd) = section_number; |
252b5132 RH |
3710 | elf_elfheader (abfd)->e_shnum = section_number; |
3711 | ||
3712 | /* Set up the list of section header pointers, in agreement with the | |
3713 | indices. */ | |
a50b1753 NC |
3714 | i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number, |
3715 | sizeof (Elf_Internal_Shdr *)); | |
252b5132 | 3716 | if (i_shdrp == NULL) |
b34976b6 | 3717 | return FALSE; |
252b5132 | 3718 | |
a50b1753 NC |
3719 | i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, |
3720 | sizeof (Elf_Internal_Shdr)); | |
252b5132 RH |
3721 | if (i_shdrp[0] == NULL) |
3722 | { | |
3723 | bfd_release (abfd, i_shdrp); | |
b34976b6 | 3724 | return FALSE; |
252b5132 | 3725 | } |
252b5132 RH |
3726 | |
3727 | elf_elfsections (abfd) = i_shdrp; | |
3728 | ||
12bd6957 | 3729 | i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; |
3516e984 | 3730 | if (need_symtab) |
252b5132 | 3731 | { |
12bd6957 | 3732 | i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; |
4fbb74a6 | 3733 | if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) |
9ad5cbcf | 3734 | { |
6a40cf0c NC |
3735 | elf_section_list * entry = elf_symtab_shndx_list (abfd); |
3736 | BFD_ASSERT (entry != NULL); | |
3737 | i_shdrp[entry->ndx] = & entry->hdr; | |
3738 | entry->hdr.sh_link = elf_onesymtab (abfd); | |
9ad5cbcf | 3739 | } |
12bd6957 AM |
3740 | i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; |
3741 | t->symtab_hdr.sh_link = elf_strtab_sec (abfd); | |
252b5132 | 3742 | } |
38ce5b11 | 3743 | |
252b5132 RH |
3744 | for (sec = abfd->sections; sec; sec = sec->next) |
3745 | { | |
252b5132 | 3746 | asection *s; |
252b5132 | 3747 | |
91d6fa6a NC |
3748 | d = elf_section_data (sec); |
3749 | ||
252b5132 | 3750 | i_shdrp[d->this_idx] = &d->this_hdr; |
d4730f92 BS |
3751 | if (d->rel.idx != 0) |
3752 | i_shdrp[d->rel.idx] = d->rel.hdr; | |
3753 | if (d->rela.idx != 0) | |
3754 | i_shdrp[d->rela.idx] = d->rela.hdr; | |
252b5132 RH |
3755 | |
3756 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
3757 | ||
3758 | /* sh_link of a reloc section is the section index of the symbol | |
3759 | table. sh_info is the section index of the section to which | |
3760 | the relocation entries apply. */ | |
d4730f92 | 3761 | if (d->rel.idx != 0) |
252b5132 | 3762 | { |
12bd6957 | 3763 | d->rel.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3764 | d->rel.hdr->sh_info = d->this_idx; |
9ef5d938 | 3765 | d->rel.hdr->sh_flags |= SHF_INFO_LINK; |
252b5132 | 3766 | } |
d4730f92 | 3767 | if (d->rela.idx != 0) |
23bc299b | 3768 | { |
12bd6957 | 3769 | d->rela.hdr->sh_link = elf_onesymtab (abfd); |
d4730f92 | 3770 | d->rela.hdr->sh_info = d->this_idx; |
9ef5d938 | 3771 | d->rela.hdr->sh_flags |= SHF_INFO_LINK; |
23bc299b | 3772 | } |
252b5132 | 3773 | |
38ce5b11 L |
3774 | /* We need to set up sh_link for SHF_LINK_ORDER. */ |
3775 | if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) | |
3776 | { | |
3777 | s = elf_linked_to_section (sec); | |
3778 | if (s) | |
38ce5b11 | 3779 | { |
f2876037 | 3780 | /* elf_linked_to_section points to the input section. */ |
ccd2ec6a | 3781 | if (link_info != NULL) |
38ce5b11 | 3782 | { |
f2876037 | 3783 | /* Check discarded linkonce section. */ |
dbaa2011 | 3784 | if (discarded_section (s)) |
38ce5b11 | 3785 | { |
ccd2ec6a | 3786 | asection *kept; |
4eca0228 | 3787 | _bfd_error_handler |
695344c0 | 3788 | /* xgettext:c-format */ |
63a5468a AM |
3789 | (_("%B: sh_link of section `%A' points to" |
3790 | " discarded section `%A' of `%B'"), | |
ccd2ec6a L |
3791 | abfd, d->this_hdr.bfd_section, |
3792 | s, s->owner); | |
3793 | /* Point to the kept section if it has the same | |
3794 | size as the discarded one. */ | |
c0f00686 | 3795 | kept = _bfd_elf_check_kept_section (s, link_info); |
ccd2ec6a | 3796 | if (kept == NULL) |
185d09ad | 3797 | { |
ccd2ec6a L |
3798 | bfd_set_error (bfd_error_bad_value); |
3799 | return FALSE; | |
185d09ad | 3800 | } |
ccd2ec6a | 3801 | s = kept; |
38ce5b11 | 3802 | } |
e424ecc8 | 3803 | |
ccd2ec6a L |
3804 | s = s->output_section; |
3805 | BFD_ASSERT (s != NULL); | |
38ce5b11 | 3806 | } |
f2876037 L |
3807 | else |
3808 | { | |
3809 | /* Handle objcopy. */ | |
3810 | if (s->output_section == NULL) | |
3811 | { | |
4eca0228 | 3812 | _bfd_error_handler |
695344c0 | 3813 | /* xgettext:c-format */ |
63a5468a AM |
3814 | (_("%B: sh_link of section `%A' points to" |
3815 | " removed section `%A' of `%B'"), | |
f2876037 L |
3816 | abfd, d->this_hdr.bfd_section, s, s->owner); |
3817 | bfd_set_error (bfd_error_bad_value); | |
3818 | return FALSE; | |
3819 | } | |
3820 | s = s->output_section; | |
3821 | } | |
ccd2ec6a L |
3822 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; |
3823 | } | |
3824 | else | |
3825 | { | |
3826 | /* PR 290: | |
3827 | The Intel C compiler generates SHT_IA_64_UNWIND with | |
3828 | SHF_LINK_ORDER. But it doesn't set the sh_link or | |
3829 | sh_info fields. Hence we could get the situation | |
08a40648 | 3830 | where s is NULL. */ |
ccd2ec6a L |
3831 | const struct elf_backend_data *bed |
3832 | = get_elf_backend_data (abfd); | |
3833 | if (bed->link_order_error_handler) | |
3834 | bed->link_order_error_handler | |
695344c0 | 3835 | /* xgettext:c-format */ |
ccd2ec6a L |
3836 | (_("%B: warning: sh_link not set for section `%A'"), |
3837 | abfd, sec); | |
38ce5b11 L |
3838 | } |
3839 | } | |
3840 | ||
252b5132 RH |
3841 | switch (d->this_hdr.sh_type) |
3842 | { | |
3843 | case SHT_REL: | |
3844 | case SHT_RELA: | |
3845 | /* A reloc section which we are treating as a normal BFD | |
3846 | section. sh_link is the section index of the symbol | |
3847 | table. sh_info is the section index of the section to | |
3848 | which the relocation entries apply. We assume that an | |
3849 | allocated reloc section uses the dynamic symbol table. | |
3850 | FIXME: How can we be sure? */ | |
3851 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3852 | if (s != NULL) | |
3853 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3854 | ||
bce964aa | 3855 | s = elf_get_reloc_section (sec); |
252b5132 | 3856 | if (s != NULL) |
9ef5d938 L |
3857 | { |
3858 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
3859 | d->this_hdr.sh_flags |= SHF_INFO_LINK; | |
3860 | } | |
252b5132 RH |
3861 | break; |
3862 | ||
3863 | case SHT_STRTAB: | |
3864 | /* We assume that a section named .stab*str is a stabs | |
3865 | string section. We look for a section with the same name | |
3866 | but without the trailing ``str'', and set its sh_link | |
3867 | field to point to this section. */ | |
0112cd26 | 3868 | if (CONST_STRNEQ (sec->name, ".stab") |
252b5132 RH |
3869 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) |
3870 | { | |
3871 | size_t len; | |
3872 | char *alc; | |
3873 | ||
3874 | len = strlen (sec->name); | |
a50b1753 | 3875 | alc = (char *) bfd_malloc (len - 2); |
252b5132 | 3876 | if (alc == NULL) |
b34976b6 | 3877 | return FALSE; |
d4c88bbb | 3878 | memcpy (alc, sec->name, len - 3); |
252b5132 RH |
3879 | alc[len - 3] = '\0'; |
3880 | s = bfd_get_section_by_name (abfd, alc); | |
3881 | free (alc); | |
3882 | if (s != NULL) | |
3883 | { | |
3884 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
3885 | ||
3886 | /* This is a .stab section. */ | |
0594c12d AM |
3887 | if (elf_section_data (s)->this_hdr.sh_entsize == 0) |
3888 | elf_section_data (s)->this_hdr.sh_entsize | |
3889 | = 4 + 2 * bfd_get_arch_size (abfd) / 8; | |
252b5132 RH |
3890 | } |
3891 | } | |
3892 | break; | |
3893 | ||
3894 | case SHT_DYNAMIC: | |
3895 | case SHT_DYNSYM: | |
3896 | case SHT_GNU_verneed: | |
3897 | case SHT_GNU_verdef: | |
3898 | /* sh_link is the section header index of the string table | |
3899 | used for the dynamic entries, or the symbol table, or the | |
3900 | version strings. */ | |
3901 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
3902 | if (s != NULL) | |
3903 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3904 | break; | |
3905 | ||
7f1204bb JJ |
3906 | case SHT_GNU_LIBLIST: |
3907 | /* sh_link is the section header index of the prelink library | |
08a40648 AM |
3908 | list used for the dynamic entries, or the symbol table, or |
3909 | the version strings. */ | |
7f1204bb JJ |
3910 | s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) |
3911 | ? ".dynstr" : ".gnu.libstr"); | |
3912 | if (s != NULL) | |
3913 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3914 | break; | |
3915 | ||
252b5132 | 3916 | case SHT_HASH: |
fdc90cb4 | 3917 | case SHT_GNU_HASH: |
252b5132 RH |
3918 | case SHT_GNU_versym: |
3919 | /* sh_link is the section header index of the symbol table | |
3920 | this hash table or version table is for. */ | |
3921 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
3922 | if (s != NULL) | |
3923 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
3924 | break; | |
dbb410c3 AM |
3925 | |
3926 | case SHT_GROUP: | |
12bd6957 | 3927 | d->this_hdr.sh_link = elf_onesymtab (abfd); |
252b5132 RH |
3928 | } |
3929 | } | |
3930 | ||
3e19fb8f L |
3931 | /* Delay setting sh_name to _bfd_elf_write_object_contents so that |
3932 | _bfd_elf_assign_file_positions_for_non_load can convert DWARF | |
3933 | debug section name from .debug_* to .zdebug_* if needed. */ | |
3934 | ||
b34976b6 | 3935 | return TRUE; |
252b5132 RH |
3936 | } |
3937 | ||
5372391b | 3938 | static bfd_boolean |
217aa764 | 3939 | sym_is_global (bfd *abfd, asymbol *sym) |
252b5132 RH |
3940 | { |
3941 | /* If the backend has a special mapping, use it. */ | |
9c5bfbb7 | 3942 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 AM |
3943 | if (bed->elf_backend_sym_is_global) |
3944 | return (*bed->elf_backend_sym_is_global) (abfd, sym); | |
252b5132 | 3945 | |
e47bf690 | 3946 | return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 |
252b5132 RH |
3947 | || bfd_is_und_section (bfd_get_section (sym)) |
3948 | || bfd_is_com_section (bfd_get_section (sym))); | |
3949 | } | |
3950 | ||
76359541 TP |
3951 | /* Filter global symbols of ABFD to include in the import library. All |
3952 | SYMCOUNT symbols of ABFD can be examined from their pointers in | |
3953 | SYMS. Pointers of symbols to keep should be stored contiguously at | |
3954 | the beginning of that array. | |
3955 | ||
3956 | Returns the number of symbols to keep. */ | |
3957 | ||
3958 | unsigned int | |
3959 | _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, | |
3960 | asymbol **syms, long symcount) | |
3961 | { | |
3962 | long src_count, dst_count = 0; | |
3963 | ||
3964 | for (src_count = 0; src_count < symcount; src_count++) | |
3965 | { | |
3966 | asymbol *sym = syms[src_count]; | |
3967 | char *name = (char *) bfd_asymbol_name (sym); | |
3968 | struct bfd_link_hash_entry *h; | |
3969 | ||
3970 | if (!sym_is_global (abfd, sym)) | |
3971 | continue; | |
3972 | ||
3973 | h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); | |
5df1bc57 AM |
3974 | if (h == NULL) |
3975 | continue; | |
76359541 TP |
3976 | if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) |
3977 | continue; | |
76359541 TP |
3978 | if (h->linker_def || h->ldscript_def) |
3979 | continue; | |
3980 | ||
3981 | syms[dst_count++] = sym; | |
3982 | } | |
3983 | ||
3984 | syms[dst_count] = NULL; | |
3985 | ||
3986 | return dst_count; | |
3987 | } | |
3988 | ||
5372391b | 3989 | /* Don't output section symbols for sections that are not going to be |
c6d8cab4 | 3990 | output, that are duplicates or there is no BFD section. */ |
5372391b AM |
3991 | |
3992 | static bfd_boolean | |
3993 | ignore_section_sym (bfd *abfd, asymbol *sym) | |
3994 | { | |
c6d8cab4 L |
3995 | elf_symbol_type *type_ptr; |
3996 | ||
3997 | if ((sym->flags & BSF_SECTION_SYM) == 0) | |
3998 | return FALSE; | |
3999 | ||
4000 | type_ptr = elf_symbol_from (abfd, sym); | |
4001 | return ((type_ptr != NULL | |
4002 | && type_ptr->internal_elf_sym.st_shndx != 0 | |
4003 | && bfd_is_abs_section (sym->section)) | |
4004 | || !(sym->section->owner == abfd | |
0f0a5e58 | 4005 | || (sym->section->output_section->owner == abfd |
2633a79c AM |
4006 | && sym->section->output_offset == 0) |
4007 | || bfd_is_abs_section (sym->section))); | |
5372391b AM |
4008 | } |
4009 | ||
2633a79c AM |
4010 | /* Map symbol from it's internal number to the external number, moving |
4011 | all local symbols to be at the head of the list. */ | |
4012 | ||
b34976b6 | 4013 | static bfd_boolean |
12bd6957 | 4014 | elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) |
252b5132 | 4015 | { |
dc810e39 | 4016 | unsigned int symcount = bfd_get_symcount (abfd); |
252b5132 RH |
4017 | asymbol **syms = bfd_get_outsymbols (abfd); |
4018 | asymbol **sect_syms; | |
dc810e39 AM |
4019 | unsigned int num_locals = 0; |
4020 | unsigned int num_globals = 0; | |
4021 | unsigned int num_locals2 = 0; | |
4022 | unsigned int num_globals2 = 0; | |
7292b3ac | 4023 | unsigned int max_index = 0; |
dc810e39 | 4024 | unsigned int idx; |
252b5132 RH |
4025 | asection *asect; |
4026 | asymbol **new_syms; | |
252b5132 RH |
4027 | |
4028 | #ifdef DEBUG | |
4029 | fprintf (stderr, "elf_map_symbols\n"); | |
4030 | fflush (stderr); | |
4031 | #endif | |
4032 | ||
252b5132 RH |
4033 | for (asect = abfd->sections; asect; asect = asect->next) |
4034 | { | |
4035 | if (max_index < asect->index) | |
4036 | max_index = asect->index; | |
4037 | } | |
4038 | ||
4039 | max_index++; | |
a50b1753 | 4040 | sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); |
252b5132 | 4041 | if (sect_syms == NULL) |
b34976b6 | 4042 | return FALSE; |
252b5132 | 4043 | elf_section_syms (abfd) = sect_syms; |
4e89ac30 | 4044 | elf_num_section_syms (abfd) = max_index; |
252b5132 | 4045 | |
079e9a2f AM |
4046 | /* Init sect_syms entries for any section symbols we have already |
4047 | decided to output. */ | |
252b5132 RH |
4048 | for (idx = 0; idx < symcount; idx++) |
4049 | { | |
dc810e39 | 4050 | asymbol *sym = syms[idx]; |
c044fabd | 4051 | |
252b5132 | 4052 | if ((sym->flags & BSF_SECTION_SYM) != 0 |
0f0a5e58 | 4053 | && sym->value == 0 |
2633a79c AM |
4054 | && !ignore_section_sym (abfd, sym) |
4055 | && !bfd_is_abs_section (sym->section)) | |
252b5132 | 4056 | { |
5372391b | 4057 | asection *sec = sym->section; |
252b5132 | 4058 | |
5372391b AM |
4059 | if (sec->owner != abfd) |
4060 | sec = sec->output_section; | |
252b5132 | 4061 | |
5372391b | 4062 | sect_syms[sec->index] = syms[idx]; |
252b5132 RH |
4063 | } |
4064 | } | |
4065 | ||
252b5132 RH |
4066 | /* Classify all of the symbols. */ |
4067 | for (idx = 0; idx < symcount; idx++) | |
4068 | { | |
2633a79c | 4069 | if (sym_is_global (abfd, syms[idx])) |
252b5132 | 4070 | num_globals++; |
2633a79c AM |
4071 | else if (!ignore_section_sym (abfd, syms[idx])) |
4072 | num_locals++; | |
252b5132 | 4073 | } |
079e9a2f | 4074 | |
5372391b | 4075 | /* We will be adding a section symbol for each normal BFD section. Most |
079e9a2f AM |
4076 | sections will already have a section symbol in outsymbols, but |
4077 | eg. SHT_GROUP sections will not, and we need the section symbol mapped | |
4078 | at least in that case. */ | |
252b5132 RH |
4079 | for (asect = abfd->sections; asect; asect = asect->next) |
4080 | { | |
079e9a2f | 4081 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4082 | { |
079e9a2f | 4083 | if (!sym_is_global (abfd, asect->symbol)) |
252b5132 RH |
4084 | num_locals++; |
4085 | else | |
4086 | num_globals++; | |
252b5132 RH |
4087 | } |
4088 | } | |
4089 | ||
4090 | /* Now sort the symbols so the local symbols are first. */ | |
a50b1753 NC |
4091 | new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals, |
4092 | sizeof (asymbol *)); | |
dc810e39 | 4093 | |
252b5132 | 4094 | if (new_syms == NULL) |
b34976b6 | 4095 | return FALSE; |
252b5132 RH |
4096 | |
4097 | for (idx = 0; idx < symcount; idx++) | |
4098 | { | |
4099 | asymbol *sym = syms[idx]; | |
dc810e39 | 4100 | unsigned int i; |
252b5132 | 4101 | |
2633a79c AM |
4102 | if (sym_is_global (abfd, sym)) |
4103 | i = num_locals + num_globals2++; | |
4104 | else if (!ignore_section_sym (abfd, sym)) | |
252b5132 RH |
4105 | i = num_locals2++; |
4106 | else | |
2633a79c | 4107 | continue; |
252b5132 RH |
4108 | new_syms[i] = sym; |
4109 | sym->udata.i = i + 1; | |
4110 | } | |
4111 | for (asect = abfd->sections; asect; asect = asect->next) | |
4112 | { | |
079e9a2f | 4113 | if (sect_syms[asect->index] == NULL) |
252b5132 | 4114 | { |
079e9a2f | 4115 | asymbol *sym = asect->symbol; |
dc810e39 | 4116 | unsigned int i; |
252b5132 | 4117 | |
079e9a2f | 4118 | sect_syms[asect->index] = sym; |
252b5132 RH |
4119 | if (!sym_is_global (abfd, sym)) |
4120 | i = num_locals2++; | |
4121 | else | |
4122 | i = num_locals + num_globals2++; | |
4123 | new_syms[i] = sym; | |
4124 | sym->udata.i = i + 1; | |
4125 | } | |
4126 | } | |
4127 | ||
4128 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); | |
4129 | ||
12bd6957 | 4130 | *pnum_locals = num_locals; |
b34976b6 | 4131 | return TRUE; |
252b5132 RH |
4132 | } |
4133 | ||
4134 | /* Align to the maximum file alignment that could be required for any | |
4135 | ELF data structure. */ | |
4136 | ||
268b6b39 | 4137 | static inline file_ptr |
217aa764 | 4138 | align_file_position (file_ptr off, int align) |
252b5132 RH |
4139 | { |
4140 | return (off + align - 1) & ~(align - 1); | |
4141 | } | |
4142 | ||
4143 | /* Assign a file position to a section, optionally aligning to the | |
4144 | required section alignment. */ | |
4145 | ||
217aa764 AM |
4146 | file_ptr |
4147 | _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, | |
4148 | file_ptr offset, | |
4149 | bfd_boolean align) | |
252b5132 | 4150 | { |
72de5009 AM |
4151 | if (align && i_shdrp->sh_addralign > 1) |
4152 | offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); | |
252b5132 RH |
4153 | i_shdrp->sh_offset = offset; |
4154 | if (i_shdrp->bfd_section != NULL) | |
4155 | i_shdrp->bfd_section->filepos = offset; | |
4156 | if (i_shdrp->sh_type != SHT_NOBITS) | |
4157 | offset += i_shdrp->sh_size; | |
4158 | return offset; | |
4159 | } | |
4160 | ||
4161 | /* Compute the file positions we are going to put the sections at, and | |
4162 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
4163 | is not NULL, this is being called by the ELF backend linker. */ | |
4164 | ||
b34976b6 | 4165 | bfd_boolean |
217aa764 AM |
4166 | _bfd_elf_compute_section_file_positions (bfd *abfd, |
4167 | struct bfd_link_info *link_info) | |
252b5132 | 4168 | { |
9c5bfbb7 | 4169 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
d4730f92 | 4170 | struct fake_section_arg fsargs; |
b34976b6 | 4171 | bfd_boolean failed; |
ef10c3ac | 4172 | struct elf_strtab_hash *strtab = NULL; |
252b5132 | 4173 | Elf_Internal_Shdr *shstrtab_hdr; |
3516e984 | 4174 | bfd_boolean need_symtab; |
252b5132 RH |
4175 | |
4176 | if (abfd->output_has_begun) | |
b34976b6 | 4177 | return TRUE; |
252b5132 RH |
4178 | |
4179 | /* Do any elf backend specific processing first. */ | |
4180 | if (bed->elf_backend_begin_write_processing) | |
4181 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); | |
4182 | ||
4183 | if (! prep_headers (abfd)) | |
b34976b6 | 4184 | return FALSE; |
252b5132 | 4185 | |
e6c51ed4 | 4186 | /* Post process the headers if necessary. */ |
78245035 | 4187 | (*bed->elf_backend_post_process_headers) (abfd, link_info); |
e6c51ed4 | 4188 | |
d4730f92 BS |
4189 | fsargs.failed = FALSE; |
4190 | fsargs.link_info = link_info; | |
4191 | bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); | |
4192 | if (fsargs.failed) | |
b34976b6 | 4193 | return FALSE; |
252b5132 | 4194 | |
da9f89d4 | 4195 | if (!assign_section_numbers (abfd, link_info)) |
b34976b6 | 4196 | return FALSE; |
252b5132 RH |
4197 | |
4198 | /* The backend linker builds symbol table information itself. */ | |
3516e984 L |
4199 | need_symtab = (link_info == NULL |
4200 | && (bfd_get_symcount (abfd) > 0 | |
4201 | || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) | |
4202 | == HAS_RELOC))); | |
4203 | if (need_symtab) | |
252b5132 RH |
4204 | { |
4205 | /* Non-zero if doing a relocatable link. */ | |
4206 | int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); | |
4207 | ||
4208 | if (! swap_out_syms (abfd, &strtab, relocatable_p)) | |
b34976b6 | 4209 | return FALSE; |
252b5132 RH |
4210 | } |
4211 | ||
d4730f92 | 4212 | failed = FALSE; |
1126897b | 4213 | if (link_info == NULL) |
dbb410c3 | 4214 | { |
1126897b | 4215 | bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); |
dbb410c3 | 4216 | if (failed) |
b34976b6 | 4217 | return FALSE; |
dbb410c3 AM |
4218 | } |
4219 | ||
252b5132 RH |
4220 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
4221 | /* sh_name was set in prep_headers. */ | |
4222 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
84865015 | 4223 | shstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
252b5132 | 4224 | shstrtab_hdr->sh_addr = 0; |
946748d5 | 4225 | /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4226 | shstrtab_hdr->sh_entsize = 0; |
4227 | shstrtab_hdr->sh_link = 0; | |
4228 | shstrtab_hdr->sh_info = 0; | |
3e19fb8f | 4229 | /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ |
252b5132 RH |
4230 | shstrtab_hdr->sh_addralign = 1; |
4231 | ||
c84fca4d | 4232 | if (!assign_file_positions_except_relocs (abfd, link_info)) |
b34976b6 | 4233 | return FALSE; |
252b5132 | 4234 | |
3516e984 | 4235 | if (need_symtab) |
252b5132 RH |
4236 | { |
4237 | file_ptr off; | |
4238 | Elf_Internal_Shdr *hdr; | |
4239 | ||
12bd6957 | 4240 | off = elf_next_file_pos (abfd); |
252b5132 | 4241 | |
6a40cf0c | 4242 | hdr = & elf_symtab_hdr (abfd); |
b34976b6 | 4243 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4244 | |
6a40cf0c NC |
4245 | if (elf_symtab_shndx_list (abfd) != NULL) |
4246 | { | |
4247 | hdr = & elf_symtab_shndx_list (abfd)->hdr; | |
4248 | if (hdr->sh_size != 0) | |
4249 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
4250 | /* FIXME: What about other symtab_shndx sections in the list ? */ | |
4251 | } | |
9ad5cbcf | 4252 | |
252b5132 | 4253 | hdr = &elf_tdata (abfd)->strtab_hdr; |
b34976b6 | 4254 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 4255 | |
12bd6957 | 4256 | elf_next_file_pos (abfd) = off; |
252b5132 RH |
4257 | |
4258 | /* Now that we know where the .strtab section goes, write it | |
08a40648 | 4259 | out. */ |
252b5132 | 4260 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
ef10c3ac | 4261 | || ! _bfd_elf_strtab_emit (abfd, strtab)) |
b34976b6 | 4262 | return FALSE; |
ef10c3ac | 4263 | _bfd_elf_strtab_free (strtab); |
252b5132 RH |
4264 | } |
4265 | ||
b34976b6 | 4266 | abfd->output_has_begun = TRUE; |
252b5132 | 4267 | |
b34976b6 | 4268 | return TRUE; |
252b5132 RH |
4269 | } |
4270 | ||
8ded5a0f AM |
4271 | /* Make an initial estimate of the size of the program header. If we |
4272 | get the number wrong here, we'll redo section placement. */ | |
4273 | ||
4274 | static bfd_size_type | |
4275 | get_program_header_size (bfd *abfd, struct bfd_link_info *info) | |
4276 | { | |
4277 | size_t segs; | |
4278 | asection *s; | |
2b05f1b7 | 4279 | const struct elf_backend_data *bed; |
8ded5a0f AM |
4280 | |
4281 | /* Assume we will need exactly two PT_LOAD segments: one for text | |
4282 | and one for data. */ | |
4283 | segs = 2; | |
4284 | ||
4285 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4286 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4287 | { | |
4288 | /* If we have a loadable interpreter section, we need a | |
4289 | PT_INTERP segment. In this case, assume we also need a | |
4290 | PT_PHDR segment, although that may not be true for all | |
4291 | targets. */ | |
e9a38e0f | 4292 | segs += 2; |
8ded5a0f AM |
4293 | } |
4294 | ||
4295 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
4296 | { | |
4297 | /* We need a PT_DYNAMIC segment. */ | |
4298 | ++segs; | |
f210dcff | 4299 | } |
08a40648 | 4300 | |
ceae84aa | 4301 | if (info != NULL && info->relro) |
f210dcff L |
4302 | { |
4303 | /* We need a PT_GNU_RELRO segment. */ | |
4304 | ++segs; | |
8ded5a0f AM |
4305 | } |
4306 | ||
12bd6957 | 4307 | if (elf_eh_frame_hdr (abfd)) |
8ded5a0f AM |
4308 | { |
4309 | /* We need a PT_GNU_EH_FRAME segment. */ | |
4310 | ++segs; | |
4311 | } | |
4312 | ||
12bd6957 | 4313 | if (elf_stack_flags (abfd)) |
8ded5a0f | 4314 | { |
2b05f1b7 L |
4315 | /* We need a PT_GNU_STACK segment. */ |
4316 | ++segs; | |
4317 | } | |
94b11780 | 4318 | |
2b05f1b7 L |
4319 | for (s = abfd->sections; s != NULL; s = s->next) |
4320 | { | |
8ded5a0f | 4321 | if ((s->flags & SEC_LOAD) != 0 |
0112cd26 | 4322 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f AM |
4323 | { |
4324 | /* We need a PT_NOTE segment. */ | |
4325 | ++segs; | |
1c5265b5 JJ |
4326 | /* Try to create just one PT_NOTE segment |
4327 | for all adjacent loadable .note* sections. | |
4328 | gABI requires that within a PT_NOTE segment | |
4329 | (and also inside of each SHT_NOTE section) | |
4330 | each note is padded to a multiple of 4 size, | |
4331 | so we check whether the sections are correctly | |
4332 | aligned. */ | |
4333 | if (s->alignment_power == 2) | |
4334 | while (s->next != NULL | |
4335 | && s->next->alignment_power == 2 | |
4336 | && (s->next->flags & SEC_LOAD) != 0 | |
4337 | && CONST_STRNEQ (s->next->name, ".note")) | |
4338 | s = s->next; | |
8ded5a0f AM |
4339 | } |
4340 | } | |
4341 | ||
4342 | for (s = abfd->sections; s != NULL; s = s->next) | |
4343 | { | |
4344 | if (s->flags & SEC_THREAD_LOCAL) | |
4345 | { | |
4346 | /* We need a PT_TLS segment. */ | |
4347 | ++segs; | |
4348 | break; | |
4349 | } | |
4350 | } | |
4351 | ||
2b05f1b7 | 4352 | bed = get_elf_backend_data (abfd); |
a91e1603 L |
4353 | |
4354 | if ((abfd->flags & D_PAGED) != 0) | |
4355 | { | |
4356 | /* Add a PT_GNU_MBIND segment for each mbind section. */ | |
4357 | unsigned int page_align_power = bfd_log2 (bed->commonpagesize); | |
4358 | for (s = abfd->sections; s != NULL; s = s->next) | |
4359 | if (elf_section_flags (s) & SHF_GNU_MBIND) | |
4360 | { | |
4361 | if (elf_section_data (s)->this_hdr.sh_info | |
4362 | > PT_GNU_MBIND_NUM) | |
4363 | { | |
4364 | _bfd_error_handler | |
4365 | /* xgettext:c-format */ | |
4366 | (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"), | |
4367 | abfd, s, elf_section_data (s)->this_hdr.sh_info); | |
4368 | continue; | |
4369 | } | |
4370 | /* Align mbind section to page size. */ | |
4371 | if (s->alignment_power < page_align_power) | |
4372 | s->alignment_power = page_align_power; | |
4373 | segs ++; | |
4374 | } | |
4375 | } | |
4376 | ||
4377 | /* Let the backend count up any program headers it might need. */ | |
4378 | if (bed->elf_backend_additional_program_headers) | |
8ded5a0f AM |
4379 | { |
4380 | int a; | |
4381 | ||
4382 | a = (*bed->elf_backend_additional_program_headers) (abfd, info); | |
4383 | if (a == -1) | |
4384 | abort (); | |
4385 | segs += a; | |
4386 | } | |
4387 | ||
4388 | return segs * bed->s->sizeof_phdr; | |
4389 | } | |
4390 | ||
2ea37f1c NC |
4391 | /* Find the segment that contains the output_section of section. */ |
4392 | ||
4393 | Elf_Internal_Phdr * | |
4394 | _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) | |
4395 | { | |
4396 | struct elf_segment_map *m; | |
4397 | Elf_Internal_Phdr *p; | |
4398 | ||
12bd6957 | 4399 | for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; |
2ea37f1c NC |
4400 | m != NULL; |
4401 | m = m->next, p++) | |
4402 | { | |
4403 | int i; | |
4404 | ||
4405 | for (i = m->count - 1; i >= 0; i--) | |
4406 | if (m->sections[i] == section) | |
4407 | return p; | |
4408 | } | |
4409 | ||
4410 | return NULL; | |
4411 | } | |
4412 | ||
252b5132 RH |
4413 | /* Create a mapping from a set of sections to a program segment. */ |
4414 | ||
217aa764 AM |
4415 | static struct elf_segment_map * |
4416 | make_mapping (bfd *abfd, | |
4417 | asection **sections, | |
4418 | unsigned int from, | |
4419 | unsigned int to, | |
4420 | bfd_boolean phdr) | |
252b5132 RH |
4421 | { |
4422 | struct elf_segment_map *m; | |
4423 | unsigned int i; | |
4424 | asection **hdrpp; | |
dc810e39 | 4425 | bfd_size_type amt; |
252b5132 | 4426 | |
dc810e39 AM |
4427 | amt = sizeof (struct elf_segment_map); |
4428 | amt += (to - from - 1) * sizeof (asection *); | |
a50b1753 | 4429 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4430 | if (m == NULL) |
4431 | return NULL; | |
4432 | m->next = NULL; | |
4433 | m->p_type = PT_LOAD; | |
4434 | for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) | |
4435 | m->sections[i - from] = *hdrpp; | |
4436 | m->count = to - from; | |
4437 | ||
4438 | if (from == 0 && phdr) | |
4439 | { | |
4440 | /* Include the headers in the first PT_LOAD segment. */ | |
4441 | m->includes_filehdr = 1; | |
4442 | m->includes_phdrs = 1; | |
4443 | } | |
4444 | ||
4445 | return m; | |
4446 | } | |
4447 | ||
229fcec5 MM |
4448 | /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL |
4449 | on failure. */ | |
4450 | ||
4451 | struct elf_segment_map * | |
4452 | _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) | |
4453 | { | |
4454 | struct elf_segment_map *m; | |
4455 | ||
a50b1753 NC |
4456 | m = (struct elf_segment_map *) bfd_zalloc (abfd, |
4457 | sizeof (struct elf_segment_map)); | |
229fcec5 MM |
4458 | if (m == NULL) |
4459 | return NULL; | |
4460 | m->next = NULL; | |
4461 | m->p_type = PT_DYNAMIC; | |
4462 | m->count = 1; | |
4463 | m->sections[0] = dynsec; | |
08a40648 | 4464 | |
229fcec5 MM |
4465 | return m; |
4466 | } | |
4467 | ||
8ded5a0f | 4468 | /* Possibly add or remove segments from the segment map. */ |
252b5132 | 4469 | |
b34976b6 | 4470 | static bfd_boolean |
3dea8fca AM |
4471 | elf_modify_segment_map (bfd *abfd, |
4472 | struct bfd_link_info *info, | |
4473 | bfd_boolean remove_empty_load) | |
252b5132 | 4474 | { |
252e386e | 4475 | struct elf_segment_map **m; |
8ded5a0f | 4476 | const struct elf_backend_data *bed; |
252b5132 | 4477 | |
8ded5a0f AM |
4478 | /* The placement algorithm assumes that non allocated sections are |
4479 | not in PT_LOAD segments. We ensure this here by removing such | |
4480 | sections from the segment map. We also remove excluded | |
252e386e AM |
4481 | sections. Finally, any PT_LOAD segment without sections is |
4482 | removed. */ | |
12bd6957 | 4483 | m = &elf_seg_map (abfd); |
252e386e | 4484 | while (*m) |
8ded5a0f AM |
4485 | { |
4486 | unsigned int i, new_count; | |
252b5132 | 4487 | |
252e386e | 4488 | for (new_count = 0, i = 0; i < (*m)->count; i++) |
8ded5a0f | 4489 | { |
252e386e AM |
4490 | if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 |
4491 | && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 | |
4492 | || (*m)->p_type != PT_LOAD)) | |
8ded5a0f | 4493 | { |
252e386e AM |
4494 | (*m)->sections[new_count] = (*m)->sections[i]; |
4495 | new_count++; | |
8ded5a0f AM |
4496 | } |
4497 | } | |
252e386e | 4498 | (*m)->count = new_count; |
252b5132 | 4499 | |
1a9ccd70 NC |
4500 | if (remove_empty_load |
4501 | && (*m)->p_type == PT_LOAD | |
4502 | && (*m)->count == 0 | |
4503 | && !(*m)->includes_phdrs) | |
252e386e AM |
4504 | *m = (*m)->next; |
4505 | else | |
4506 | m = &(*m)->next; | |
8ded5a0f | 4507 | } |
252b5132 | 4508 | |
8ded5a0f AM |
4509 | bed = get_elf_backend_data (abfd); |
4510 | if (bed->elf_backend_modify_segment_map != NULL) | |
252b5132 | 4511 | { |
252e386e | 4512 | if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) |
8ded5a0f | 4513 | return FALSE; |
252b5132 | 4514 | } |
252b5132 | 4515 | |
8ded5a0f AM |
4516 | return TRUE; |
4517 | } | |
252b5132 | 4518 | |
8ded5a0f | 4519 | /* Set up a mapping from BFD sections to program segments. */ |
252b5132 | 4520 | |
8ded5a0f AM |
4521 | bfd_boolean |
4522 | _bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) | |
4523 | { | |
4524 | unsigned int count; | |
4525 | struct elf_segment_map *m; | |
4526 | asection **sections = NULL; | |
4527 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
3dea8fca | 4528 | bfd_boolean no_user_phdrs; |
252b5132 | 4529 | |
12bd6957 | 4530 | no_user_phdrs = elf_seg_map (abfd) == NULL; |
d324f6d6 RM |
4531 | |
4532 | if (info != NULL) | |
4533 | info->user_phdrs = !no_user_phdrs; | |
4534 | ||
3dea8fca | 4535 | if (no_user_phdrs && bfd_count_sections (abfd) != 0) |
252b5132 | 4536 | { |
8ded5a0f AM |
4537 | asection *s; |
4538 | unsigned int i; | |
4539 | struct elf_segment_map *mfirst; | |
4540 | struct elf_segment_map **pm; | |
4541 | asection *last_hdr; | |
4542 | bfd_vma last_size; | |
4543 | unsigned int phdr_index; | |
4544 | bfd_vma maxpagesize; | |
4545 | asection **hdrpp; | |
4546 | bfd_boolean phdr_in_segment = TRUE; | |
4547 | bfd_boolean writable; | |
4548 | int tls_count = 0; | |
4549 | asection *first_tls = NULL; | |
a91e1603 | 4550 | asection *first_mbind = NULL; |
8ded5a0f AM |
4551 | asection *dynsec, *eh_frame_hdr; |
4552 | bfd_size_type amt; | |
8d06853e | 4553 | bfd_vma addr_mask, wrap_to = 0; |
1a9ccd70 | 4554 | bfd_boolean linker_created_pt_phdr_segment = FALSE; |
252b5132 | 4555 | |
8ded5a0f | 4556 | /* Select the allocated sections, and sort them. */ |
252b5132 | 4557 | |
a50b1753 NC |
4558 | sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd), |
4559 | sizeof (asection *)); | |
8ded5a0f | 4560 | if (sections == NULL) |
252b5132 | 4561 | goto error_return; |
252b5132 | 4562 | |
8d06853e AM |
4563 | /* Calculate top address, avoiding undefined behaviour of shift |
4564 | left operator when shift count is equal to size of type | |
4565 | being shifted. */ | |
4566 | addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; | |
4567 | addr_mask = (addr_mask << 1) + 1; | |
4568 | ||
8ded5a0f AM |
4569 | i = 0; |
4570 | for (s = abfd->sections; s != NULL; s = s->next) | |
4571 | { | |
4572 | if ((s->flags & SEC_ALLOC) != 0) | |
4573 | { | |
4574 | sections[i] = s; | |
4575 | ++i; | |
8d06853e AM |
4576 | /* A wrapping section potentially clashes with header. */ |
4577 | if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask)) | |
4578 | wrap_to = (s->lma + s->size) & addr_mask; | |
8ded5a0f AM |
4579 | } |
4580 | } | |
4581 | BFD_ASSERT (i <= bfd_count_sections (abfd)); | |
4582 | count = i; | |
252b5132 | 4583 | |
8ded5a0f | 4584 | qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); |
252b5132 | 4585 | |
8ded5a0f | 4586 | /* Build the mapping. */ |
252b5132 | 4587 | |
8ded5a0f AM |
4588 | mfirst = NULL; |
4589 | pm = &mfirst; | |
252b5132 | 4590 | |
8ded5a0f AM |
4591 | /* If we have a .interp section, then create a PT_PHDR segment for |
4592 | the program headers and a PT_INTERP segment for the .interp | |
4593 | section. */ | |
4594 | s = bfd_get_section_by_name (abfd, ".interp"); | |
4595 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
4596 | { | |
4597 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 4598 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4599 | if (m == NULL) |
4600 | goto error_return; | |
4601 | m->next = NULL; | |
4602 | m->p_type = PT_PHDR; | |
4603 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
4604 | m->p_flags = PF_R | PF_X; | |
4605 | m->p_flags_valid = 1; | |
4606 | m->includes_phdrs = 1; | |
1a9ccd70 | 4607 | linker_created_pt_phdr_segment = TRUE; |
8ded5a0f AM |
4608 | *pm = m; |
4609 | pm = &m->next; | |
252b5132 | 4610 | |
8ded5a0f | 4611 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4612 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4613 | if (m == NULL) |
4614 | goto error_return; | |
4615 | m->next = NULL; | |
4616 | m->p_type = PT_INTERP; | |
4617 | m->count = 1; | |
4618 | m->sections[0] = s; | |
4619 | ||
4620 | *pm = m; | |
4621 | pm = &m->next; | |
252b5132 | 4622 | } |
8ded5a0f AM |
4623 | |
4624 | /* Look through the sections. We put sections in the same program | |
4625 | segment when the start of the second section can be placed within | |
4626 | a few bytes of the end of the first section. */ | |
4627 | last_hdr = NULL; | |
4628 | last_size = 0; | |
4629 | phdr_index = 0; | |
4630 | maxpagesize = bed->maxpagesize; | |
063bb025 NC |
4631 | /* PR 17512: file: c8455299. |
4632 | Avoid divide-by-zero errors later on. | |
4633 | FIXME: Should we abort if the maxpagesize is zero ? */ | |
4634 | if (maxpagesize == 0) | |
4635 | maxpagesize = 1; | |
8ded5a0f AM |
4636 | writable = FALSE; |
4637 | dynsec = bfd_get_section_by_name (abfd, ".dynamic"); | |
4638 | if (dynsec != NULL | |
4639 | && (dynsec->flags & SEC_LOAD) == 0) | |
4640 | dynsec = NULL; | |
4641 | ||
4642 | /* Deal with -Ttext or something similar such that the first section | |
4643 | is not adjacent to the program headers. This is an | |
4644 | approximation, since at this point we don't know exactly how many | |
4645 | program headers we will need. */ | |
4646 | if (count > 0) | |
252b5132 | 4647 | { |
12bd6957 | 4648 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 4649 | |
62d7a5f6 | 4650 | if (phdr_size == (bfd_size_type) -1) |
8ded5a0f | 4651 | phdr_size = get_program_header_size (abfd, info); |
d2bcb0d1 | 4652 | phdr_size += bed->s->sizeof_ehdr; |
8ded5a0f | 4653 | if ((abfd->flags & D_PAGED) == 0 |
8d06853e AM |
4654 | || (sections[0]->lma & addr_mask) < phdr_size |
4655 | || ((sections[0]->lma & addr_mask) % maxpagesize | |
4656 | < phdr_size % maxpagesize) | |
4657 | || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to) | |
1a9ccd70 NC |
4658 | { |
4659 | /* PR 20815: The ELF standard says that a PT_PHDR segment, if | |
4660 | present, must be included as part of the memory image of the | |
4661 | program. Ie it must be part of a PT_LOAD segment as well. | |
4662 | If we have had to create our own PT_PHDR segment, but it is | |
4663 | not going to be covered by the first PT_LOAD segment, then | |
4664 | force the inclusion if we can... */ | |
4665 | if ((abfd->flags & D_PAGED) != 0 | |
4666 | && linker_created_pt_phdr_segment) | |
4667 | phdr_in_segment = TRUE; | |
4668 | else | |
4669 | phdr_in_segment = FALSE; | |
4670 | } | |
252b5132 RH |
4671 | } |
4672 | ||
8ded5a0f | 4673 | for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) |
252b5132 | 4674 | { |
8ded5a0f AM |
4675 | asection *hdr; |
4676 | bfd_boolean new_segment; | |
4677 | ||
4678 | hdr = *hdrpp; | |
4679 | ||
4680 | /* See if this section and the last one will fit in the same | |
4681 | segment. */ | |
4682 | ||
4683 | if (last_hdr == NULL) | |
4684 | { | |
4685 | /* If we don't have a segment yet, then we don't need a new | |
4686 | one (we build the last one after this loop). */ | |
4687 | new_segment = FALSE; | |
4688 | } | |
4689 | else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) | |
4690 | { | |
4691 | /* If this section has a different relation between the | |
4692 | virtual address and the load address, then we need a new | |
4693 | segment. */ | |
4694 | new_segment = TRUE; | |
4695 | } | |
b5599592 AM |
4696 | else if (hdr->lma < last_hdr->lma + last_size |
4697 | || last_hdr->lma + last_size < last_hdr->lma) | |
4698 | { | |
4699 | /* If this section has a load address that makes it overlap | |
4700 | the previous section, then we need a new segment. */ | |
4701 | new_segment = TRUE; | |
4702 | } | |
39948a60 NC |
4703 | /* In the next test we have to be careful when last_hdr->lma is close |
4704 | to the end of the address space. If the aligned address wraps | |
4705 | around to the start of the address space, then there are no more | |
4706 | pages left in memory and it is OK to assume that the current | |
4707 | section can be included in the current segment. */ | |
4708 | else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4709 | > last_hdr->lma) | |
4710 | && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize | |
4ff73856 | 4711 | <= hdr->lma)) |
8ded5a0f AM |
4712 | { |
4713 | /* If putting this section in this segment would force us to | |
4714 | skip a page in the segment, then we need a new segment. */ | |
4715 | new_segment = TRUE; | |
4716 | } | |
4717 | else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 | |
e5654c0f AM |
4718 | && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0 |
4719 | && ((abfd->flags & D_PAGED) == 0 | |
4720 | || (((last_hdr->lma + last_size - 1) & -maxpagesize) | |
4721 | != (hdr->lma & -maxpagesize)))) | |
8ded5a0f | 4722 | { |
e5654c0f AM |
4723 | /* We don't want to put a loaded section after a |
4724 | nonloaded (ie. bss style) section in the same segment | |
4725 | as that will force the non-loaded section to be loaded. | |
4726 | Consider .tbss sections as loaded for this purpose. | |
4727 | However, like the writable/non-writable case below, | |
4728 | if they are on the same page then they must be put | |
4729 | in the same segment. */ | |
8ded5a0f AM |
4730 | new_segment = TRUE; |
4731 | } | |
4732 | else if ((abfd->flags & D_PAGED) == 0) | |
4733 | { | |
4734 | /* If the file is not demand paged, which means that we | |
4735 | don't require the sections to be correctly aligned in the | |
4736 | file, then there is no other reason for a new segment. */ | |
4737 | new_segment = FALSE; | |
4738 | } | |
4739 | else if (! writable | |
4740 | && (hdr->flags & SEC_READONLY) == 0 | |
8d06853e AM |
4741 | && (((last_hdr->lma + last_size - 1) & -maxpagesize) |
4742 | != (hdr->lma & -maxpagesize))) | |
8ded5a0f AM |
4743 | { |
4744 | /* We don't want to put a writable section in a read only | |
4745 | segment, unless they are on the same page in memory | |
4746 | anyhow. We already know that the last section does not | |
4747 | bring us past the current section on the page, so the | |
4748 | only case in which the new section is not on the same | |
4749 | page as the previous section is when the previous section | |
4750 | ends precisely on a page boundary. */ | |
4751 | new_segment = TRUE; | |
4752 | } | |
4753 | else | |
4754 | { | |
4755 | /* Otherwise, we can use the same segment. */ | |
4756 | new_segment = FALSE; | |
4757 | } | |
4758 | ||
2889e75b | 4759 | /* Allow interested parties a chance to override our decision. */ |
ceae84aa AM |
4760 | if (last_hdr != NULL |
4761 | && info != NULL | |
4762 | && info->callbacks->override_segment_assignment != NULL) | |
4763 | new_segment | |
4764 | = info->callbacks->override_segment_assignment (info, abfd, hdr, | |
4765 | last_hdr, | |
4766 | new_segment); | |
2889e75b | 4767 | |
8ded5a0f AM |
4768 | if (! new_segment) |
4769 | { | |
4770 | if ((hdr->flags & SEC_READONLY) == 0) | |
4771 | writable = TRUE; | |
4772 | last_hdr = hdr; | |
4773 | /* .tbss sections effectively have zero size. */ | |
4774 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4775 | != SEC_THREAD_LOCAL) | |
4776 | last_size = hdr->size; | |
4777 | else | |
4778 | last_size = 0; | |
4779 | continue; | |
4780 | } | |
4781 | ||
4782 | /* We need a new program segment. We must create a new program | |
4783 | header holding all the sections from phdr_index until hdr. */ | |
4784 | ||
4785 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4786 | if (m == NULL) | |
4787 | goto error_return; | |
4788 | ||
4789 | *pm = m; | |
4790 | pm = &m->next; | |
4791 | ||
252b5132 | 4792 | if ((hdr->flags & SEC_READONLY) == 0) |
b34976b6 | 4793 | writable = TRUE; |
8ded5a0f AM |
4794 | else |
4795 | writable = FALSE; | |
4796 | ||
baaff79e JJ |
4797 | last_hdr = hdr; |
4798 | /* .tbss sections effectively have zero size. */ | |
e5caec89 | 4799 | if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) |
eea6121a | 4800 | last_size = hdr->size; |
baaff79e JJ |
4801 | else |
4802 | last_size = 0; | |
8ded5a0f AM |
4803 | phdr_index = i; |
4804 | phdr_in_segment = FALSE; | |
252b5132 RH |
4805 | } |
4806 | ||
86b2281f AM |
4807 | /* Create a final PT_LOAD program segment, but not if it's just |
4808 | for .tbss. */ | |
4809 | if (last_hdr != NULL | |
4810 | && (i - phdr_index != 1 | |
4811 | || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) | |
4812 | != SEC_THREAD_LOCAL))) | |
8ded5a0f AM |
4813 | { |
4814 | m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); | |
4815 | if (m == NULL) | |
4816 | goto error_return; | |
252b5132 | 4817 | |
8ded5a0f AM |
4818 | *pm = m; |
4819 | pm = &m->next; | |
4820 | } | |
252b5132 | 4821 | |
8ded5a0f AM |
4822 | /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ |
4823 | if (dynsec != NULL) | |
4824 | { | |
4825 | m = _bfd_elf_make_dynamic_segment (abfd, dynsec); | |
4826 | if (m == NULL) | |
4827 | goto error_return; | |
4828 | *pm = m; | |
4829 | pm = &m->next; | |
4830 | } | |
252b5132 | 4831 | |
1c5265b5 JJ |
4832 | /* For each batch of consecutive loadable .note sections, |
4833 | add a PT_NOTE segment. We don't use bfd_get_section_by_name, | |
4834 | because if we link together nonloadable .note sections and | |
4835 | loadable .note sections, we will generate two .note sections | |
4836 | in the output file. FIXME: Using names for section types is | |
4837 | bogus anyhow. */ | |
8ded5a0f AM |
4838 | for (s = abfd->sections; s != NULL; s = s->next) |
4839 | { | |
4840 | if ((s->flags & SEC_LOAD) != 0 | |
0112cd26 | 4841 | && CONST_STRNEQ (s->name, ".note")) |
8ded5a0f | 4842 | { |
1c5265b5 | 4843 | asection *s2; |
91d6fa6a NC |
4844 | |
4845 | count = 1; | |
8ded5a0f | 4846 | amt = sizeof (struct elf_segment_map); |
1c5265b5 JJ |
4847 | if (s->alignment_power == 2) |
4848 | for (s2 = s; s2->next != NULL; s2 = s2->next) | |
55b581a6 JJ |
4849 | { |
4850 | if (s2->next->alignment_power == 2 | |
4851 | && (s2->next->flags & SEC_LOAD) != 0 | |
4852 | && CONST_STRNEQ (s2->next->name, ".note") | |
8d06853e AM |
4853 | && align_power (s2->lma + s2->size, 2) |
4854 | == s2->next->lma) | |
55b581a6 JJ |
4855 | count++; |
4856 | else | |
4857 | break; | |
4858 | } | |
1c5265b5 | 4859 | amt += (count - 1) * sizeof (asection *); |
a50b1753 | 4860 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4861 | if (m == NULL) |
4862 | goto error_return; | |
4863 | m->next = NULL; | |
4864 | m->p_type = PT_NOTE; | |
1c5265b5 JJ |
4865 | m->count = count; |
4866 | while (count > 1) | |
4867 | { | |
4868 | m->sections[m->count - count--] = s; | |
4869 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
4870 | s = s->next; | |
4871 | } | |
4872 | m->sections[m->count - 1] = s; | |
4873 | BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); | |
8ded5a0f AM |
4874 | *pm = m; |
4875 | pm = &m->next; | |
4876 | } | |
4877 | if (s->flags & SEC_THREAD_LOCAL) | |
4878 | { | |
4879 | if (! tls_count) | |
4880 | first_tls = s; | |
4881 | tls_count++; | |
4882 | } | |
a91e1603 L |
4883 | if (first_mbind == NULL |
4884 | && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) | |
4885 | first_mbind = s; | |
8ded5a0f | 4886 | } |
252b5132 | 4887 | |
8ded5a0f AM |
4888 | /* If there are any SHF_TLS output sections, add PT_TLS segment. */ |
4889 | if (tls_count > 0) | |
4890 | { | |
8ded5a0f AM |
4891 | amt = sizeof (struct elf_segment_map); |
4892 | amt += (tls_count - 1) * sizeof (asection *); | |
a50b1753 | 4893 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4894 | if (m == NULL) |
4895 | goto error_return; | |
4896 | m->next = NULL; | |
4897 | m->p_type = PT_TLS; | |
4898 | m->count = tls_count; | |
4899 | /* Mandated PF_R. */ | |
4900 | m->p_flags = PF_R; | |
4901 | m->p_flags_valid = 1; | |
d923cae0 | 4902 | s = first_tls; |
91d6fa6a | 4903 | for (i = 0; i < (unsigned int) tls_count; ++i) |
8ded5a0f | 4904 | { |
d923cae0 L |
4905 | if ((s->flags & SEC_THREAD_LOCAL) == 0) |
4906 | { | |
4907 | _bfd_error_handler | |
4908 | (_("%B: TLS sections are not adjacent:"), abfd); | |
4909 | s = first_tls; | |
4910 | i = 0; | |
4911 | while (i < (unsigned int) tls_count) | |
4912 | { | |
4913 | if ((s->flags & SEC_THREAD_LOCAL) != 0) | |
4914 | { | |
4915 | _bfd_error_handler (_(" TLS: %A"), s); | |
4916 | i++; | |
4917 | } | |
4918 | else | |
4919 | _bfd_error_handler (_(" non-TLS: %A"), s); | |
4920 | s = s->next; | |
4921 | } | |
4922 | bfd_set_error (bfd_error_bad_value); | |
4923 | goto error_return; | |
4924 | } | |
4925 | m->sections[i] = s; | |
4926 | s = s->next; | |
8ded5a0f | 4927 | } |
252b5132 | 4928 | |
8ded5a0f AM |
4929 | *pm = m; |
4930 | pm = &m->next; | |
4931 | } | |
252b5132 | 4932 | |
a91e1603 L |
4933 | if (first_mbind && (abfd->flags & D_PAGED) != 0) |
4934 | for (s = first_mbind; s != NULL; s = s->next) | |
4935 | if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 | |
4936 | && (elf_section_data (s)->this_hdr.sh_info | |
4937 | <= PT_GNU_MBIND_NUM)) | |
4938 | { | |
4939 | /* Mandated PF_R. */ | |
4940 | unsigned long p_flags = PF_R; | |
4941 | if ((s->flags & SEC_READONLY) == 0) | |
4942 | p_flags |= PF_W; | |
4943 | if ((s->flags & SEC_CODE) != 0) | |
4944 | p_flags |= PF_X; | |
4945 | ||
4946 | amt = sizeof (struct elf_segment_map) + sizeof (asection *); | |
4947 | m = bfd_zalloc (abfd, amt); | |
4948 | if (m == NULL) | |
4949 | goto error_return; | |
4950 | m->next = NULL; | |
4951 | m->p_type = (PT_GNU_MBIND_LO | |
4952 | + elf_section_data (s)->this_hdr.sh_info); | |
4953 | m->count = 1; | |
4954 | m->p_flags_valid = 1; | |
4955 | m->sections[0] = s; | |
4956 | m->p_flags = p_flags; | |
4957 | ||
4958 | *pm = m; | |
4959 | pm = &m->next; | |
4960 | } | |
4961 | ||
8ded5a0f AM |
4962 | /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME |
4963 | segment. */ | |
12bd6957 | 4964 | eh_frame_hdr = elf_eh_frame_hdr (abfd); |
8ded5a0f AM |
4965 | if (eh_frame_hdr != NULL |
4966 | && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) | |
252b5132 | 4967 | { |
dc810e39 | 4968 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4969 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
252b5132 RH |
4970 | if (m == NULL) |
4971 | goto error_return; | |
4972 | m->next = NULL; | |
8ded5a0f | 4973 | m->p_type = PT_GNU_EH_FRAME; |
252b5132 | 4974 | m->count = 1; |
8ded5a0f | 4975 | m->sections[0] = eh_frame_hdr->output_section; |
252b5132 RH |
4976 | |
4977 | *pm = m; | |
4978 | pm = &m->next; | |
4979 | } | |
13ae64f3 | 4980 | |
12bd6957 | 4981 | if (elf_stack_flags (abfd)) |
13ae64f3 | 4982 | { |
8ded5a0f | 4983 | amt = sizeof (struct elf_segment_map); |
a50b1753 | 4984 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
8ded5a0f AM |
4985 | if (m == NULL) |
4986 | goto error_return; | |
4987 | m->next = NULL; | |
2b05f1b7 | 4988 | m->p_type = PT_GNU_STACK; |
12bd6957 | 4989 | m->p_flags = elf_stack_flags (abfd); |
04c3a755 | 4990 | m->p_align = bed->stack_align; |
8ded5a0f | 4991 | m->p_flags_valid = 1; |
04c3a755 NS |
4992 | m->p_align_valid = m->p_align != 0; |
4993 | if (info->stacksize > 0) | |
4994 | { | |
4995 | m->p_size = info->stacksize; | |
4996 | m->p_size_valid = 1; | |
4997 | } | |
252b5132 | 4998 | |
8ded5a0f AM |
4999 | *pm = m; |
5000 | pm = &m->next; | |
5001 | } | |
65765700 | 5002 | |
ceae84aa | 5003 | if (info != NULL && info->relro) |
8ded5a0f | 5004 | { |
f210dcff L |
5005 | for (m = mfirst; m != NULL; m = m->next) |
5006 | { | |
3832a4d8 AM |
5007 | if (m->p_type == PT_LOAD |
5008 | && m->count != 0 | |
5009 | && m->sections[0]->vma >= info->relro_start | |
5010 | && m->sections[0]->vma < info->relro_end) | |
f210dcff | 5011 | { |
3832a4d8 AM |
5012 | i = m->count; |
5013 | while (--i != (unsigned) -1) | |
5014 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5015 | == (SEC_LOAD | SEC_HAS_CONTENTS)) | |
5016 | break; | |
5017 | ||
43a8475c | 5018 | if (i != (unsigned) -1) |
f210dcff L |
5019 | break; |
5020 | } | |
be01b344 | 5021 | } |
f210dcff L |
5022 | |
5023 | /* Make a PT_GNU_RELRO segment only when it isn't empty. */ | |
5024 | if (m != NULL) | |
5025 | { | |
5026 | amt = sizeof (struct elf_segment_map); | |
a50b1753 | 5027 | m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); |
f210dcff L |
5028 | if (m == NULL) |
5029 | goto error_return; | |
5030 | m->next = NULL; | |
5031 | m->p_type = PT_GNU_RELRO; | |
f210dcff L |
5032 | *pm = m; |
5033 | pm = &m->next; | |
5034 | } | |
8ded5a0f | 5035 | } |
9ee5e499 | 5036 | |
8ded5a0f | 5037 | free (sections); |
12bd6957 | 5038 | elf_seg_map (abfd) = mfirst; |
9ee5e499 JJ |
5039 | } |
5040 | ||
3dea8fca | 5041 | if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) |
8ded5a0f | 5042 | return FALSE; |
8c37241b | 5043 | |
12bd6957 | 5044 | for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) |
8ded5a0f | 5045 | ++count; |
12bd6957 | 5046 | elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; |
252b5132 | 5047 | |
b34976b6 | 5048 | return TRUE; |
252b5132 RH |
5049 | |
5050 | error_return: | |
5051 | if (sections != NULL) | |
5052 | free (sections); | |
b34976b6 | 5053 | return FALSE; |
252b5132 RH |
5054 | } |
5055 | ||
5056 | /* Sort sections by address. */ | |
5057 | ||
5058 | static int | |
217aa764 | 5059 | elf_sort_sections (const void *arg1, const void *arg2) |
252b5132 RH |
5060 | { |
5061 | const asection *sec1 = *(const asection **) arg1; | |
5062 | const asection *sec2 = *(const asection **) arg2; | |
eecdbe52 | 5063 | bfd_size_type size1, size2; |
252b5132 RH |
5064 | |
5065 | /* Sort by LMA first, since this is the address used to | |
5066 | place the section into a segment. */ | |
5067 | if (sec1->lma < sec2->lma) | |
5068 | return -1; | |
5069 | else if (sec1->lma > sec2->lma) | |
5070 | return 1; | |
5071 | ||
5072 | /* Then sort by VMA. Normally the LMA and the VMA will be | |
5073 | the same, and this will do nothing. */ | |
5074 | if (sec1->vma < sec2->vma) | |
5075 | return -1; | |
5076 | else if (sec1->vma > sec2->vma) | |
5077 | return 1; | |
5078 | ||
5079 | /* Put !SEC_LOAD sections after SEC_LOAD ones. */ | |
5080 | ||
07c6e936 | 5081 | #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) |
252b5132 RH |
5082 | |
5083 | if (TOEND (sec1)) | |
5084 | { | |
5085 | if (TOEND (sec2)) | |
00a7cdc5 NC |
5086 | { |
5087 | /* If the indicies are the same, do not return 0 | |
5088 | here, but continue to try the next comparison. */ | |
5089 | if (sec1->target_index - sec2->target_index != 0) | |
5090 | return sec1->target_index - sec2->target_index; | |
5091 | } | |
252b5132 RH |
5092 | else |
5093 | return 1; | |
5094 | } | |
00a7cdc5 | 5095 | else if (TOEND (sec2)) |
252b5132 RH |
5096 | return -1; |
5097 | ||
5098 | #undef TOEND | |
5099 | ||
00a7cdc5 NC |
5100 | /* Sort by size, to put zero sized sections |
5101 | before others at the same address. */ | |
252b5132 | 5102 | |
eea6121a AM |
5103 | size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; |
5104 | size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; | |
eecdbe52 JJ |
5105 | |
5106 | if (size1 < size2) | |
252b5132 | 5107 | return -1; |
eecdbe52 | 5108 | if (size1 > size2) |
252b5132 RH |
5109 | return 1; |
5110 | ||
5111 | return sec1->target_index - sec2->target_index; | |
5112 | } | |
5113 | ||
340b6d91 AC |
5114 | /* Ian Lance Taylor writes: |
5115 | ||
5116 | We shouldn't be using % with a negative signed number. That's just | |
5117 | not good. We have to make sure either that the number is not | |
5118 | negative, or that the number has an unsigned type. When the types | |
5119 | are all the same size they wind up as unsigned. When file_ptr is a | |
5120 | larger signed type, the arithmetic winds up as signed long long, | |
5121 | which is wrong. | |
5122 | ||
5123 | What we're trying to say here is something like ``increase OFF by | |
5124 | the least amount that will cause it to be equal to the VMA modulo | |
5125 | the page size.'' */ | |
5126 | /* In other words, something like: | |
5127 | ||
5128 | vma_offset = m->sections[0]->vma % bed->maxpagesize; | |
5129 | off_offset = off % bed->maxpagesize; | |
5130 | if (vma_offset < off_offset) | |
5131 | adjustment = vma_offset + bed->maxpagesize - off_offset; | |
5132 | else | |
5133 | adjustment = vma_offset - off_offset; | |
08a40648 | 5134 | |
de194d85 | 5135 | which can be collapsed into the expression below. */ |
340b6d91 AC |
5136 | |
5137 | static file_ptr | |
5138 | vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) | |
5139 | { | |
dc9155b2 NC |
5140 | /* PR binutils/16199: Handle an alignment of zero. */ |
5141 | if (maxpagesize == 0) | |
5142 | maxpagesize = 1; | |
340b6d91 AC |
5143 | return ((vma - off) % maxpagesize); |
5144 | } | |
5145 | ||
6d33f217 L |
5146 | static void |
5147 | print_segment_map (const struct elf_segment_map *m) | |
5148 | { | |
5149 | unsigned int j; | |
5150 | const char *pt = get_segment_type (m->p_type); | |
5151 | char buf[32]; | |
5152 | ||
5153 | if (pt == NULL) | |
5154 | { | |
5155 | if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) | |
5156 | sprintf (buf, "LOPROC+%7.7x", | |
5157 | (unsigned int) (m->p_type - PT_LOPROC)); | |
5158 | else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) | |
5159 | sprintf (buf, "LOOS+%7.7x", | |
5160 | (unsigned int) (m->p_type - PT_LOOS)); | |
5161 | else | |
5162 | snprintf (buf, sizeof (buf), "%8.8x", | |
5163 | (unsigned int) m->p_type); | |
5164 | pt = buf; | |
5165 | } | |
4a97a0e5 | 5166 | fflush (stdout); |
6d33f217 L |
5167 | fprintf (stderr, "%s:", pt); |
5168 | for (j = 0; j < m->count; j++) | |
5169 | fprintf (stderr, " %s", m->sections [j]->name); | |
5170 | putc ('\n',stderr); | |
4a97a0e5 | 5171 | fflush (stderr); |
6d33f217 L |
5172 | } |
5173 | ||
32812159 AM |
5174 | static bfd_boolean |
5175 | write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) | |
5176 | { | |
5177 | void *buf; | |
5178 | bfd_boolean ret; | |
5179 | ||
5180 | if (bfd_seek (abfd, pos, SEEK_SET) != 0) | |
5181 | return FALSE; | |
5182 | buf = bfd_zmalloc (len); | |
5183 | if (buf == NULL) | |
5184 | return FALSE; | |
5185 | ret = bfd_bwrite (buf, len, abfd) == len; | |
5186 | free (buf); | |
5187 | return ret; | |
5188 | } | |
5189 | ||
252b5132 RH |
5190 | /* Assign file positions to the sections based on the mapping from |
5191 | sections to segments. This function also sets up some fields in | |
f3520d2f | 5192 | the file header. */ |
252b5132 | 5193 | |
b34976b6 | 5194 | static bfd_boolean |
f3520d2f AM |
5195 | assign_file_positions_for_load_sections (bfd *abfd, |
5196 | struct bfd_link_info *link_info) | |
252b5132 RH |
5197 | { |
5198 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
252b5132 | 5199 | struct elf_segment_map *m; |
252b5132 | 5200 | Elf_Internal_Phdr *phdrs; |
252b5132 | 5201 | Elf_Internal_Phdr *p; |
02bf8d82 | 5202 | file_ptr off; |
3f570048 | 5203 | bfd_size_type maxpagesize; |
a8c75b76 | 5204 | unsigned int pt_load_count = 0; |
f3520d2f | 5205 | unsigned int alloc; |
0920dee7 | 5206 | unsigned int i, j; |
2b0bc088 | 5207 | bfd_vma header_pad = 0; |
252b5132 | 5208 | |
e36284ab | 5209 | if (link_info == NULL |
ceae84aa | 5210 | && !_bfd_elf_map_sections_to_segments (abfd, link_info)) |
8ded5a0f | 5211 | return FALSE; |
252b5132 | 5212 | |
8ded5a0f | 5213 | alloc = 0; |
12bd6957 | 5214 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
2b0bc088 NC |
5215 | { |
5216 | ++alloc; | |
5217 | if (m->header_size) | |
5218 | header_pad = m->header_size; | |
5219 | } | |
252b5132 | 5220 | |
82f2dbf7 NC |
5221 | if (alloc) |
5222 | { | |
5223 | elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; | |
5224 | elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; | |
5225 | } | |
5226 | else | |
5227 | { | |
5228 | /* PR binutils/12467. */ | |
5229 | elf_elfheader (abfd)->e_phoff = 0; | |
5230 | elf_elfheader (abfd)->e_phentsize = 0; | |
5231 | } | |
d324f6d6 | 5232 | |
8ded5a0f | 5233 | elf_elfheader (abfd)->e_phnum = alloc; |
252b5132 | 5234 | |
12bd6957 AM |
5235 | if (elf_program_header_size (abfd) == (bfd_size_type) -1) |
5236 | elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; | |
8ded5a0f | 5237 | else |
12bd6957 | 5238 | BFD_ASSERT (elf_program_header_size (abfd) |
59e0647f | 5239 | >= alloc * bed->s->sizeof_phdr); |
252b5132 RH |
5240 | |
5241 | if (alloc == 0) | |
f3520d2f | 5242 | { |
12bd6957 | 5243 | elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; |
8ded5a0f | 5244 | return TRUE; |
f3520d2f | 5245 | } |
252b5132 | 5246 | |
12bd6957 | 5247 | /* We're writing the size in elf_program_header_size (abfd), |
57268894 HPN |
5248 | see assign_file_positions_except_relocs, so make sure we have |
5249 | that amount allocated, with trailing space cleared. | |
12bd6957 AM |
5250 | The variable alloc contains the computed need, while |
5251 | elf_program_header_size (abfd) contains the size used for the | |
57268894 HPN |
5252 | layout. |
5253 | See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments | |
5254 | where the layout is forced to according to a larger size in the | |
5255 | last iterations for the testcase ld-elf/header. */ | |
12bd6957 | 5256 | BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr |
57268894 | 5257 | == 0); |
a50b1753 NC |
5258 | phdrs = (Elf_Internal_Phdr *) |
5259 | bfd_zalloc2 (abfd, | |
12bd6957 | 5260 | (elf_program_header_size (abfd) / bed->s->sizeof_phdr), |
a50b1753 | 5261 | sizeof (Elf_Internal_Phdr)); |
f3520d2f | 5262 | elf_tdata (abfd)->phdr = phdrs; |
252b5132 | 5263 | if (phdrs == NULL) |
b34976b6 | 5264 | return FALSE; |
252b5132 | 5265 | |
3f570048 AM |
5266 | maxpagesize = 1; |
5267 | if ((abfd->flags & D_PAGED) != 0) | |
5268 | maxpagesize = bed->maxpagesize; | |
5269 | ||
252b5132 RH |
5270 | off = bed->s->sizeof_ehdr; |
5271 | off += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5272 | if (header_pad < (bfd_vma) off) |
5273 | header_pad = 0; | |
5274 | else | |
5275 | header_pad -= off; | |
5276 | off += header_pad; | |
252b5132 | 5277 | |
12bd6957 | 5278 | for (m = elf_seg_map (abfd), p = phdrs, j = 0; |
252b5132 | 5279 | m != NULL; |
0920dee7 | 5280 | m = m->next, p++, j++) |
252b5132 | 5281 | { |
252b5132 | 5282 | asection **secpp; |
bf988460 AM |
5283 | bfd_vma off_adjust; |
5284 | bfd_boolean no_contents; | |
252b5132 RH |
5285 | |
5286 | /* If elf_segment_map is not from map_sections_to_segments, the | |
08a40648 | 5287 | sections may not be correctly ordered. NOTE: sorting should |
52e9b619 MS |
5288 | not be done to the PT_NOTE section of a corefile, which may |
5289 | contain several pseudo-sections artificially created by bfd. | |
5290 | Sorting these pseudo-sections breaks things badly. */ | |
47d9a591 AM |
5291 | if (m->count > 1 |
5292 | && !(elf_elfheader (abfd)->e_type == ET_CORE | |
52e9b619 | 5293 | && m->p_type == PT_NOTE)) |
252b5132 RH |
5294 | qsort (m->sections, (size_t) m->count, sizeof (asection *), |
5295 | elf_sort_sections); | |
5296 | ||
b301b248 AM |
5297 | /* An ELF segment (described by Elf_Internal_Phdr) may contain a |
5298 | number of sections with contents contributing to both p_filesz | |
5299 | and p_memsz, followed by a number of sections with no contents | |
5300 | that just contribute to p_memsz. In this loop, OFF tracks next | |
02bf8d82 | 5301 | available file offset for PT_LOAD and PT_NOTE segments. */ |
252b5132 | 5302 | p->p_type = m->p_type; |
28a7f3e7 | 5303 | p->p_flags = m->p_flags; |
252b5132 | 5304 | |
3f570048 AM |
5305 | if (m->count == 0) |
5306 | p->p_vaddr = 0; | |
5307 | else | |
3271a814 | 5308 | p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset; |
3f570048 AM |
5309 | |
5310 | if (m->p_paddr_valid) | |
5311 | p->p_paddr = m->p_paddr; | |
5312 | else if (m->count == 0) | |
5313 | p->p_paddr = 0; | |
5314 | else | |
08a40648 | 5315 | p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset; |
3f570048 AM |
5316 | |
5317 | if (p->p_type == PT_LOAD | |
5318 | && (abfd->flags & D_PAGED) != 0) | |
5319 | { | |
5320 | /* p_align in demand paged PT_LOAD segments effectively stores | |
5321 | the maximum page size. When copying an executable with | |
5322 | objcopy, we set m->p_align from the input file. Use this | |
5323 | value for maxpagesize rather than bed->maxpagesize, which | |
5324 | may be different. Note that we use maxpagesize for PT_TLS | |
5325 | segment alignment later in this function, so we are relying | |
5326 | on at least one PT_LOAD segment appearing before a PT_TLS | |
5327 | segment. */ | |
5328 | if (m->p_align_valid) | |
5329 | maxpagesize = m->p_align; | |
5330 | ||
5331 | p->p_align = maxpagesize; | |
a8c75b76 | 5332 | pt_load_count += 1; |
3f570048 | 5333 | } |
3271a814 NS |
5334 | else if (m->p_align_valid) |
5335 | p->p_align = m->p_align; | |
e970b90a DJ |
5336 | else if (m->count == 0) |
5337 | p->p_align = 1 << bed->s->log_file_align; | |
3f570048 AM |
5338 | else |
5339 | p->p_align = 0; | |
5340 | ||
bf988460 AM |
5341 | no_contents = FALSE; |
5342 | off_adjust = 0; | |
252b5132 | 5343 | if (p->p_type == PT_LOAD |
b301b248 | 5344 | && m->count > 0) |
252b5132 | 5345 | { |
b301b248 | 5346 | bfd_size_type align; |
a49e53ed | 5347 | unsigned int align_power = 0; |
b301b248 | 5348 | |
3271a814 NS |
5349 | if (m->p_align_valid) |
5350 | align = p->p_align; | |
5351 | else | |
252b5132 | 5352 | { |
3271a814 NS |
5353 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5354 | { | |
5355 | unsigned int secalign; | |
08a40648 | 5356 | |
3271a814 NS |
5357 | secalign = bfd_get_section_alignment (abfd, *secpp); |
5358 | if (secalign > align_power) | |
5359 | align_power = secalign; | |
5360 | } | |
5361 | align = (bfd_size_type) 1 << align_power; | |
5362 | if (align < maxpagesize) | |
5363 | align = maxpagesize; | |
b301b248 | 5364 | } |
252b5132 | 5365 | |
02bf8d82 AM |
5366 | for (i = 0; i < m->count; i++) |
5367 | if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) | |
5368 | /* If we aren't making room for this section, then | |
5369 | it must be SHT_NOBITS regardless of what we've | |
5370 | set via struct bfd_elf_special_section. */ | |
5371 | elf_section_type (m->sections[i]) = SHT_NOBITS; | |
5372 | ||
bf988460 | 5373 | /* Find out whether this segment contains any loadable |
aea274d3 AM |
5374 | sections. */ |
5375 | no_contents = TRUE; | |
5376 | for (i = 0; i < m->count; i++) | |
5377 | if (elf_section_type (m->sections[i]) != SHT_NOBITS) | |
5378 | { | |
5379 | no_contents = FALSE; | |
5380 | break; | |
5381 | } | |
bf988460 | 5382 | |
85cfcbfb | 5383 | off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align); |
a8c75b76 AM |
5384 | |
5385 | /* Broken hardware and/or kernel require that files do not | |
5386 | map the same page with different permissions on some hppa | |
5387 | processors. */ | |
5388 | if (pt_load_count > 1 | |
5389 | && bed->no_page_alias | |
5390 | && (off & (maxpagesize - 1)) != 0 | |
5391 | && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize)) | |
5392 | off_adjust += maxpagesize; | |
bf988460 AM |
5393 | off += off_adjust; |
5394 | if (no_contents) | |
5395 | { | |
5396 | /* We shouldn't need to align the segment on disk since | |
5397 | the segment doesn't need file space, but the gABI | |
5398 | arguably requires the alignment and glibc ld.so | |
5399 | checks it. So to comply with the alignment | |
5400 | requirement but not waste file space, we adjust | |
5401 | p_offset for just this segment. (OFF_ADJUST is | |
5402 | subtracted from OFF later.) This may put p_offset | |
5403 | past the end of file, but that shouldn't matter. */ | |
5404 | } | |
5405 | else | |
5406 | off_adjust = 0; | |
252b5132 | 5407 | } |
b1a6d0b1 NC |
5408 | /* Make sure the .dynamic section is the first section in the |
5409 | PT_DYNAMIC segment. */ | |
5410 | else if (p->p_type == PT_DYNAMIC | |
5411 | && m->count > 1 | |
5412 | && strcmp (m->sections[0]->name, ".dynamic") != 0) | |
5413 | { | |
5414 | _bfd_error_handler | |
63a5468a AM |
5415 | (_("%B: The first section in the PT_DYNAMIC segment" |
5416 | " is not the .dynamic section"), | |
b301b248 | 5417 | abfd); |
b1a6d0b1 NC |
5418 | bfd_set_error (bfd_error_bad_value); |
5419 | return FALSE; | |
5420 | } | |
3f001e84 JK |
5421 | /* Set the note section type to SHT_NOTE. */ |
5422 | else if (p->p_type == PT_NOTE) | |
5423 | for (i = 0; i < m->count; i++) | |
5424 | elf_section_type (m->sections[i]) = SHT_NOTE; | |
252b5132 | 5425 | |
252b5132 RH |
5426 | p->p_offset = 0; |
5427 | p->p_filesz = 0; | |
5428 | p->p_memsz = 0; | |
5429 | ||
5430 | if (m->includes_filehdr) | |
5431 | { | |
bf988460 | 5432 | if (!m->p_flags_valid) |
252b5132 | 5433 | p->p_flags |= PF_R; |
252b5132 RH |
5434 | p->p_filesz = bed->s->sizeof_ehdr; |
5435 | p->p_memsz = bed->s->sizeof_ehdr; | |
5436 | if (m->count > 0) | |
5437 | { | |
9ab82472 L |
5438 | if (p->p_vaddr < (bfd_vma) off |
5439 | || (!m->p_paddr_valid | |
5440 | && p->p_paddr < (bfd_vma) off)) | |
252b5132 | 5441 | { |
4eca0228 | 5442 | _bfd_error_handler |
63a5468a AM |
5443 | (_("%B: Not enough room for program headers," |
5444 | " try linking with -N"), | |
b301b248 | 5445 | abfd); |
252b5132 | 5446 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 5447 | return FALSE; |
252b5132 RH |
5448 | } |
5449 | ||
5450 | p->p_vaddr -= off; | |
bf988460 | 5451 | if (!m->p_paddr_valid) |
252b5132 RH |
5452 | p->p_paddr -= off; |
5453 | } | |
252b5132 RH |
5454 | } |
5455 | ||
5456 | if (m->includes_phdrs) | |
5457 | { | |
bf988460 | 5458 | if (!m->p_flags_valid) |
252b5132 RH |
5459 | p->p_flags |= PF_R; |
5460 | ||
f3520d2f | 5461 | if (!m->includes_filehdr) |
252b5132 RH |
5462 | { |
5463 | p->p_offset = bed->s->sizeof_ehdr; | |
5464 | ||
5465 | if (m->count > 0) | |
5466 | { | |
252b5132 | 5467 | p->p_vaddr -= off - p->p_offset; |
bf988460 | 5468 | if (!m->p_paddr_valid) |
252b5132 RH |
5469 | p->p_paddr -= off - p->p_offset; |
5470 | } | |
252b5132 RH |
5471 | } |
5472 | ||
5473 | p->p_filesz += alloc * bed->s->sizeof_phdr; | |
5474 | p->p_memsz += alloc * bed->s->sizeof_phdr; | |
2b0bc088 NC |
5475 | if (m->count) |
5476 | { | |
5477 | p->p_filesz += header_pad; | |
5478 | p->p_memsz += header_pad; | |
5479 | } | |
252b5132 RH |
5480 | } |
5481 | ||
5482 | if (p->p_type == PT_LOAD | |
5483 | || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) | |
5484 | { | |
bf988460 | 5485 | if (!m->includes_filehdr && !m->includes_phdrs) |
02bf8d82 | 5486 | p->p_offset = off; |
252b5132 RH |
5487 | else |
5488 | { | |
5489 | file_ptr adjust; | |
5490 | ||
5491 | adjust = off - (p->p_offset + p->p_filesz); | |
bf988460 AM |
5492 | if (!no_contents) |
5493 | p->p_filesz += adjust; | |
252b5132 RH |
5494 | p->p_memsz += adjust; |
5495 | } | |
5496 | } | |
5497 | ||
1ea63fd2 AM |
5498 | /* Set up p_filesz, p_memsz, p_align and p_flags from the section |
5499 | maps. Set filepos for sections in PT_LOAD segments, and in | |
5500 | core files, for sections in PT_NOTE segments. | |
5501 | assign_file_positions_for_non_load_sections will set filepos | |
5502 | for other sections and update p_filesz for other segments. */ | |
252b5132 RH |
5503 | for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) |
5504 | { | |
5505 | asection *sec; | |
252b5132 | 5506 | bfd_size_type align; |
627b32bc | 5507 | Elf_Internal_Shdr *this_hdr; |
252b5132 RH |
5508 | |
5509 | sec = *secpp; | |
02bf8d82 | 5510 | this_hdr = &elf_section_data (sec)->this_hdr; |
3f570048 | 5511 | align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec); |
252b5132 | 5512 | |
88967714 AM |
5513 | if ((p->p_type == PT_LOAD |
5514 | || p->p_type == PT_TLS) | |
5515 | && (this_hdr->sh_type != SHT_NOBITS | |
5516 | || ((this_hdr->sh_flags & SHF_ALLOC) != 0 | |
5517 | && ((this_hdr->sh_flags & SHF_TLS) == 0 | |
5518 | || p->p_type == PT_TLS)))) | |
252b5132 | 5519 | { |
b5599592 AM |
5520 | bfd_vma p_start = p->p_paddr; |
5521 | bfd_vma p_end = p_start + p->p_memsz; | |
5522 | bfd_vma s_start = sec->lma; | |
5523 | bfd_vma adjust = s_start - p_end; | |
252b5132 | 5524 | |
a2d1e028 L |
5525 | if (adjust != 0 |
5526 | && (s_start < p_end | |
5527 | || p_end < p_start)) | |
252b5132 | 5528 | { |
4eca0228 | 5529 | _bfd_error_handler |
695344c0 | 5530 | /* xgettext:c-format */ |
76cfced5 AM |
5531 | (_("%B: section %A lma %#Lx adjusted to %#Lx"), |
5532 | abfd, sec, s_start, p_end); | |
88967714 | 5533 | adjust = 0; |
b5599592 | 5534 | sec->lma = p_end; |
1cfb7d1e | 5535 | } |
3ac9b6c9 | 5536 | p->p_memsz += adjust; |
1cfb7d1e | 5537 | |
88967714 AM |
5538 | if (this_hdr->sh_type != SHT_NOBITS) |
5539 | { | |
32812159 AM |
5540 | if (p->p_filesz + adjust < p->p_memsz) |
5541 | { | |
5542 | /* We have a PROGBITS section following NOBITS ones. | |
5543 | Allocate file space for the NOBITS section(s) and | |
5544 | zero it. */ | |
5545 | adjust = p->p_memsz - p->p_filesz; | |
5546 | if (!write_zeros (abfd, off, adjust)) | |
5547 | return FALSE; | |
5548 | } | |
88967714 AM |
5549 | off += adjust; |
5550 | p->p_filesz += adjust; | |
252b5132 | 5551 | } |
252b5132 RH |
5552 | } |
5553 | ||
5554 | if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) | |
5555 | { | |
b301b248 AM |
5556 | /* The section at i == 0 is the one that actually contains |
5557 | everything. */ | |
4a938328 MS |
5558 | if (i == 0) |
5559 | { | |
627b32bc | 5560 | this_hdr->sh_offset = sec->filepos = off; |
6a3cd2b4 AM |
5561 | off += this_hdr->sh_size; |
5562 | p->p_filesz = this_hdr->sh_size; | |
b301b248 AM |
5563 | p->p_memsz = 0; |
5564 | p->p_align = 1; | |
252b5132 | 5565 | } |
4a938328 | 5566 | else |
252b5132 | 5567 | { |
b301b248 | 5568 | /* The rest are fake sections that shouldn't be written. */ |
252b5132 | 5569 | sec->filepos = 0; |
eea6121a | 5570 | sec->size = 0; |
b301b248 AM |
5571 | sec->flags = 0; |
5572 | continue; | |
252b5132 | 5573 | } |
252b5132 RH |
5574 | } |
5575 | else | |
5576 | { | |
1e951488 | 5577 | if (p->p_type == PT_LOAD) |
b301b248 | 5578 | { |
1e951488 AM |
5579 | this_hdr->sh_offset = sec->filepos = off; |
5580 | if (this_hdr->sh_type != SHT_NOBITS) | |
5581 | off += this_hdr->sh_size; | |
5582 | } | |
5583 | else if (this_hdr->sh_type == SHT_NOBITS | |
5584 | && (this_hdr->sh_flags & SHF_TLS) != 0 | |
5585 | && this_hdr->sh_offset == 0) | |
5586 | { | |
5587 | /* This is a .tbss section that didn't get a PT_LOAD. | |
5588 | (See _bfd_elf_map_sections_to_segments "Create a | |
5589 | final PT_LOAD".) Set sh_offset to the value it | |
5590 | would have if we had created a zero p_filesz and | |
5591 | p_memsz PT_LOAD header for the section. This | |
5592 | also makes the PT_TLS header have the same | |
5593 | p_offset value. */ | |
5594 | bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, | |
5595 | off, align); | |
5596 | this_hdr->sh_offset = sec->filepos = off + adjust; | |
b301b248 | 5597 | } |
252b5132 | 5598 | |
02bf8d82 | 5599 | if (this_hdr->sh_type != SHT_NOBITS) |
b301b248 | 5600 | { |
6a3cd2b4 | 5601 | p->p_filesz += this_hdr->sh_size; |
02bf8d82 AM |
5602 | /* A load section without SHF_ALLOC is something like |
5603 | a note section in a PT_NOTE segment. These take | |
5604 | file space but are not loaded into memory. */ | |
5605 | if ((this_hdr->sh_flags & SHF_ALLOC) != 0) | |
6a3cd2b4 | 5606 | p->p_memsz += this_hdr->sh_size; |
b301b248 | 5607 | } |
6a3cd2b4 | 5608 | else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) |
13ae64f3 | 5609 | { |
6a3cd2b4 AM |
5610 | if (p->p_type == PT_TLS) |
5611 | p->p_memsz += this_hdr->sh_size; | |
5612 | ||
5613 | /* .tbss is special. It doesn't contribute to p_memsz of | |
5614 | normal segments. */ | |
5615 | else if ((this_hdr->sh_flags & SHF_TLS) == 0) | |
5616 | p->p_memsz += this_hdr->sh_size; | |
13ae64f3 JJ |
5617 | } |
5618 | ||
b10a8ae0 L |
5619 | if (align > p->p_align |
5620 | && !m->p_align_valid | |
5621 | && (p->p_type != PT_LOAD | |
5622 | || (abfd->flags & D_PAGED) == 0)) | |
252b5132 RH |
5623 | p->p_align = align; |
5624 | } | |
5625 | ||
bf988460 | 5626 | if (!m->p_flags_valid) |
252b5132 RH |
5627 | { |
5628 | p->p_flags |= PF_R; | |
02bf8d82 | 5629 | if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) |
252b5132 | 5630 | p->p_flags |= PF_X; |
02bf8d82 | 5631 | if ((this_hdr->sh_flags & SHF_WRITE) != 0) |
252b5132 RH |
5632 | p->p_flags |= PF_W; |
5633 | } | |
5634 | } | |
43a8475c | 5635 | |
bf988460 | 5636 | off -= off_adjust; |
0920dee7 | 5637 | |
7c928300 AM |
5638 | /* Check that all sections are in a PT_LOAD segment. |
5639 | Don't check funky gdb generated core files. */ | |
5640 | if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) | |
9a83a553 AM |
5641 | { |
5642 | bfd_boolean check_vma = TRUE; | |
5643 | ||
5644 | for (i = 1; i < m->count; i++) | |
5645 | if (m->sections[i]->vma == m->sections[i - 1]->vma | |
5646 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) | |
5647 | ->this_hdr), p) != 0 | |
5648 | && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) | |
5649 | ->this_hdr), p) != 0) | |
0920dee7 | 5650 | { |
9a83a553 AM |
5651 | /* Looks like we have overlays packed into the segment. */ |
5652 | check_vma = FALSE; | |
5653 | break; | |
0920dee7 | 5654 | } |
9a83a553 AM |
5655 | |
5656 | for (i = 0; i < m->count; i++) | |
5657 | { | |
5658 | Elf_Internal_Shdr *this_hdr; | |
5659 | asection *sec; | |
5660 | ||
5661 | sec = m->sections[i]; | |
5662 | this_hdr = &(elf_section_data(sec)->this_hdr); | |
86b2281f AM |
5663 | if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) |
5664 | && !ELF_TBSS_SPECIAL (this_hdr, p)) | |
9a83a553 | 5665 | { |
4eca0228 | 5666 | _bfd_error_handler |
695344c0 | 5667 | /* xgettext:c-format */ |
9a83a553 AM |
5668 | (_("%B: section `%A' can't be allocated in segment %d"), |
5669 | abfd, sec, j); | |
5670 | print_segment_map (m); | |
5671 | } | |
5672 | } | |
5673 | } | |
252b5132 RH |
5674 | } |
5675 | ||
12bd6957 | 5676 | elf_next_file_pos (abfd) = off; |
f3520d2f AM |
5677 | return TRUE; |
5678 | } | |
5679 | ||
5680 | /* Assign file positions for the other sections. */ | |
5681 | ||
5682 | static bfd_boolean | |
5683 | assign_file_positions_for_non_load_sections (bfd *abfd, | |
5684 | struct bfd_link_info *link_info) | |
5685 | { | |
5686 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
5687 | Elf_Internal_Shdr **i_shdrpp; | |
e06efbf1 | 5688 | Elf_Internal_Shdr **hdrpp, **end_hdrpp; |
f3520d2f AM |
5689 | Elf_Internal_Phdr *phdrs; |
5690 | Elf_Internal_Phdr *p; | |
5691 | struct elf_segment_map *m; | |
62655c7b | 5692 | struct elf_segment_map *hdrs_segment; |
f3520d2f AM |
5693 | bfd_vma filehdr_vaddr, filehdr_paddr; |
5694 | bfd_vma phdrs_vaddr, phdrs_paddr; | |
5695 | file_ptr off; | |
f3520d2f AM |
5696 | unsigned int count; |
5697 | ||
5c182d5f | 5698 | i_shdrpp = elf_elfsections (abfd); |
e06efbf1 | 5699 | end_hdrpp = i_shdrpp + elf_numsections (abfd); |
12bd6957 | 5700 | off = elf_next_file_pos (abfd); |
e06efbf1 | 5701 | for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) |
5c182d5f | 5702 | { |
5c182d5f AM |
5703 | Elf_Internal_Shdr *hdr; |
5704 | ||
5705 | hdr = *hdrpp; | |
5706 | if (hdr->bfd_section != NULL | |
252e386e AM |
5707 | && (hdr->bfd_section->filepos != 0 |
5708 | || (hdr->sh_type == SHT_NOBITS | |
5709 | && hdr->contents == NULL))) | |
627b32bc | 5710 | BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); |
5c182d5f AM |
5711 | else if ((hdr->sh_flags & SHF_ALLOC) != 0) |
5712 | { | |
e8d2ba53 | 5713 | if (hdr->sh_size != 0) |
4eca0228 | 5714 | _bfd_error_handler |
695344c0 | 5715 | /* xgettext:c-format */ |
e8d2ba53 AM |
5716 | (_("%B: warning: allocated section `%s' not in segment"), |
5717 | abfd, | |
5718 | (hdr->bfd_section == NULL | |
5719 | ? "*unknown*" | |
5720 | : hdr->bfd_section->name)); | |
3ba71138 L |
5721 | /* We don't need to page align empty sections. */ |
5722 | if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) | |
5c182d5f AM |
5723 | off += vma_page_aligned_bias (hdr->sh_addr, off, |
5724 | bed->maxpagesize); | |
5725 | else | |
5726 | off += vma_page_aligned_bias (hdr->sh_addr, off, | |
5727 | hdr->sh_addralign); | |
5728 | off = _bfd_elf_assign_file_position_for_section (hdr, off, | |
5729 | FALSE); | |
5730 | } | |
5731 | else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
5732 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
5733 | || (hdr->bfd_section != NULL |
5734 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
5735 | /* Compress DWARF debug sections. */ | |
12bd6957 | 5736 | || hdr == i_shdrpp[elf_onesymtab (abfd)] |
6a40cf0c NC |
5737 | || (elf_symtab_shndx_list (abfd) != NULL |
5738 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
5739 | || hdr == i_shdrpp[elf_strtab_sec (abfd)] |
5740 | || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) | |
5c182d5f AM |
5741 | hdr->sh_offset = -1; |
5742 | else | |
5743 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); | |
5c182d5f AM |
5744 | } |
5745 | ||
252b5132 RH |
5746 | /* Now that we have set the section file positions, we can set up |
5747 | the file positions for the non PT_LOAD segments. */ | |
f3520d2f AM |
5748 | count = 0; |
5749 | filehdr_vaddr = 0; | |
5750 | filehdr_paddr = 0; | |
5751 | phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; | |
5752 | phdrs_paddr = 0; | |
62655c7b | 5753 | hdrs_segment = NULL; |
f3520d2f | 5754 | phdrs = elf_tdata (abfd)->phdr; |
12bd6957 | 5755 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
f3520d2f AM |
5756 | { |
5757 | ++count; | |
5758 | if (p->p_type != PT_LOAD) | |
5759 | continue; | |
5760 | ||
5761 | if (m->includes_filehdr) | |
5762 | { | |
5763 | filehdr_vaddr = p->p_vaddr; | |
5764 | filehdr_paddr = p->p_paddr; | |
5765 | } | |
5766 | if (m->includes_phdrs) | |
5767 | { | |
5768 | phdrs_vaddr = p->p_vaddr; | |
5769 | phdrs_paddr = p->p_paddr; | |
5770 | if (m->includes_filehdr) | |
5771 | { | |
62655c7b | 5772 | hdrs_segment = m; |
f3520d2f AM |
5773 | phdrs_vaddr += bed->s->sizeof_ehdr; |
5774 | phdrs_paddr += bed->s->sizeof_ehdr; | |
5775 | } | |
5776 | } | |
5777 | } | |
5778 | ||
62655c7b RM |
5779 | if (hdrs_segment != NULL && link_info != NULL) |
5780 | { | |
5781 | /* There is a segment that contains both the file headers and the | |
5782 | program headers, so provide a symbol __ehdr_start pointing there. | |
5783 | A program can use this to examine itself robustly. */ | |
5784 | ||
5785 | struct elf_link_hash_entry *hash | |
5786 | = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", | |
5787 | FALSE, FALSE, TRUE); | |
5788 | /* If the symbol was referenced and not defined, define it. */ | |
5789 | if (hash != NULL | |
5790 | && (hash->root.type == bfd_link_hash_new | |
5791 | || hash->root.type == bfd_link_hash_undefined | |
5792 | || hash->root.type == bfd_link_hash_undefweak | |
5793 | || hash->root.type == bfd_link_hash_common)) | |
5794 | { | |
5795 | asection *s = NULL; | |
5796 | if (hdrs_segment->count != 0) | |
5797 | /* The segment contains sections, so use the first one. */ | |
5798 | s = hdrs_segment->sections[0]; | |
5799 | else | |
5800 | /* Use the first (i.e. lowest-addressed) section in any segment. */ | |
12bd6957 | 5801 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62655c7b RM |
5802 | if (m->count != 0) |
5803 | { | |
5804 | s = m->sections[0]; | |
5805 | break; | |
5806 | } | |
5807 | ||
5808 | if (s != NULL) | |
5809 | { | |
5810 | hash->root.u.def.value = filehdr_vaddr - s->vma; | |
5811 | hash->root.u.def.section = s; | |
5812 | } | |
5813 | else | |
5814 | { | |
5815 | hash->root.u.def.value = filehdr_vaddr; | |
5816 | hash->root.u.def.section = bfd_abs_section_ptr; | |
5817 | } | |
5818 | ||
5819 | hash->root.type = bfd_link_hash_defined; | |
5820 | hash->def_regular = 1; | |
5821 | hash->non_elf = 0; | |
5822 | } | |
5823 | } | |
5824 | ||
12bd6957 | 5825 | for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) |
252b5132 | 5826 | { |
129af99f | 5827 | if (p->p_type == PT_GNU_RELRO) |
252b5132 | 5828 | { |
b84a33b5 | 5829 | const Elf_Internal_Phdr *lp; |
3146fac4 | 5830 | struct elf_segment_map *lm; |
1ea63fd2 | 5831 | |
129af99f | 5832 | if (link_info != NULL) |
8c37241b | 5833 | { |
129af99f AS |
5834 | /* During linking the range of the RELRO segment is passed |
5835 | in link_info. */ | |
12bd6957 | 5836 | for (lm = elf_seg_map (abfd), lp = phdrs; |
3146fac4 AM |
5837 | lm != NULL; |
5838 | lm = lm->next, lp++) | |
8c37241b JJ |
5839 | { |
5840 | if (lp->p_type == PT_LOAD | |
b84a33b5 | 5841 | && lp->p_vaddr < link_info->relro_end |
3146fac4 AM |
5842 | && lm->count != 0 |
5843 | && lm->sections[0]->vma >= link_info->relro_start) | |
8c37241b JJ |
5844 | break; |
5845 | } | |
8981c88a | 5846 | |
3146fac4 | 5847 | BFD_ASSERT (lm != NULL); |
8c37241b | 5848 | } |
129af99f AS |
5849 | else |
5850 | { | |
5851 | /* Otherwise we are copying an executable or shared | |
b84a33b5 | 5852 | library, but we need to use the same linker logic. */ |
129af99f AS |
5853 | for (lp = phdrs; lp < phdrs + count; ++lp) |
5854 | { | |
5855 | if (lp->p_type == PT_LOAD | |
5856 | && lp->p_paddr == p->p_paddr) | |
5857 | break; | |
5858 | } | |
b84a33b5 AM |
5859 | } |
5860 | ||
5861 | if (lp < phdrs + count) | |
5862 | { | |
5863 | p->p_vaddr = lp->p_vaddr; | |
5864 | p->p_paddr = lp->p_paddr; | |
5865 | p->p_offset = lp->p_offset; | |
5866 | if (link_info != NULL) | |
5867 | p->p_filesz = link_info->relro_end - lp->p_vaddr; | |
5868 | else if (m->p_size_valid) | |
5869 | p->p_filesz = m->p_size; | |
129af99f AS |
5870 | else |
5871 | abort (); | |
b84a33b5 | 5872 | p->p_memsz = p->p_filesz; |
f3944f72 L |
5873 | /* Preserve the alignment and flags if they are valid. The |
5874 | gold linker generates RW/4 for the PT_GNU_RELRO section. | |
5875 | It is better for objcopy/strip to honor these attributes | |
5876 | otherwise gdb will choke when using separate debug files. | |
5877 | */ | |
5878 | if (!m->p_align_valid) | |
5879 | p->p_align = 1; | |
5880 | if (!m->p_flags_valid) | |
341b8ee7 | 5881 | p->p_flags = PF_R; |
129af99f | 5882 | } |
9433b9b1 | 5883 | else |
b84a33b5 AM |
5884 | { |
5885 | memset (p, 0, sizeof *p); | |
5886 | p->p_type = PT_NULL; | |
5887 | } | |
129af99f | 5888 | } |
04c3a755 NS |
5889 | else if (p->p_type == PT_GNU_STACK) |
5890 | { | |
5891 | if (m->p_size_valid) | |
5892 | p->p_memsz = m->p_size; | |
5893 | } | |
129af99f AS |
5894 | else if (m->count != 0) |
5895 | { | |
e06efbf1 | 5896 | unsigned int i; |
1a9ccd70 | 5897 | |
129af99f AS |
5898 | if (p->p_type != PT_LOAD |
5899 | && (p->p_type != PT_NOTE | |
5900 | || bfd_get_format (abfd) != bfd_core)) | |
5901 | { | |
1a9ccd70 NC |
5902 | /* A user specified segment layout may include a PHDR |
5903 | segment that overlaps with a LOAD segment... */ | |
5904 | if (p->p_type == PT_PHDR) | |
5905 | { | |
5906 | m->count = 0; | |
5907 | continue; | |
5908 | } | |
5909 | ||
c86934ce NC |
5910 | if (m->includes_filehdr || m->includes_phdrs) |
5911 | { | |
b1fa9dd6 | 5912 | /* PR 17512: file: 2195325e. */ |
4eca0228 | 5913 | _bfd_error_handler |
76cfced5 AM |
5914 | (_("%B: error: non-load segment %d includes file header " |
5915 | "and/or program header"), | |
5916 | abfd, (int) (p - phdrs)); | |
c86934ce NC |
5917 | return FALSE; |
5918 | } | |
129af99f | 5919 | |
86b2281f | 5920 | p->p_filesz = 0; |
129af99f | 5921 | p->p_offset = m->sections[0]->filepos; |
86b2281f AM |
5922 | for (i = m->count; i-- != 0;) |
5923 | { | |
5924 | asection *sect = m->sections[i]; | |
5925 | Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; | |
5926 | if (hdr->sh_type != SHT_NOBITS) | |
5927 | { | |
5928 | p->p_filesz = (sect->filepos - m->sections[0]->filepos | |
5929 | + hdr->sh_size); | |
5930 | break; | |
5931 | } | |
5932 | } | |
129af99f AS |
5933 | } |
5934 | } | |
5935 | else if (m->includes_filehdr) | |
5936 | { | |
5937 | p->p_vaddr = filehdr_vaddr; | |
5938 | if (! m->p_paddr_valid) | |
5939 | p->p_paddr = filehdr_paddr; | |
5940 | } | |
5941 | else if (m->includes_phdrs) | |
5942 | { | |
5943 | p->p_vaddr = phdrs_vaddr; | |
5944 | if (! m->p_paddr_valid) | |
5945 | p->p_paddr = phdrs_paddr; | |
252b5132 RH |
5946 | } |
5947 | } | |
5948 | ||
12bd6957 | 5949 | elf_next_file_pos (abfd) = off; |
252b5132 | 5950 | |
b34976b6 | 5951 | return TRUE; |
252b5132 RH |
5952 | } |
5953 | ||
6a40cf0c NC |
5954 | static elf_section_list * |
5955 | find_section_in_list (unsigned int i, elf_section_list * list) | |
5956 | { | |
5957 | for (;list != NULL; list = list->next) | |
5958 | if (list->ndx == i) | |
5959 | break; | |
5960 | return list; | |
5961 | } | |
5962 | ||
252b5132 RH |
5963 | /* Work out the file positions of all the sections. This is called by |
5964 | _bfd_elf_compute_section_file_positions. All the section sizes and | |
5965 | VMAs must be known before this is called. | |
5966 | ||
e0638f70 AM |
5967 | Reloc sections come in two flavours: Those processed specially as |
5968 | "side-channel" data attached to a section to which they apply, and | |
5969 | those that bfd doesn't process as relocations. The latter sort are | |
5970 | stored in a normal bfd section by bfd_section_from_shdr. We don't | |
5971 | consider the former sort here, unless they form part of the loadable | |
5972 | image. Reloc sections not assigned here will be handled later by | |
5973 | assign_file_positions_for_relocs. | |
252b5132 RH |
5974 | |
5975 | We also don't set the positions of the .symtab and .strtab here. */ | |
5976 | ||
b34976b6 | 5977 | static bfd_boolean |
c84fca4d AO |
5978 | assign_file_positions_except_relocs (bfd *abfd, |
5979 | struct bfd_link_info *link_info) | |
252b5132 | 5980 | { |
5c182d5f AM |
5981 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
5982 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
9c5bfbb7 | 5983 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
5984 | |
5985 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 | |
5986 | && bfd_get_format (abfd) != bfd_core) | |
5987 | { | |
5c182d5f AM |
5988 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); |
5989 | unsigned int num_sec = elf_numsections (abfd); | |
252b5132 RH |
5990 | Elf_Internal_Shdr **hdrpp; |
5991 | unsigned int i; | |
a485e98e | 5992 | file_ptr off; |
252b5132 RH |
5993 | |
5994 | /* Start after the ELF header. */ | |
5995 | off = i_ehdrp->e_ehsize; | |
5996 | ||
5997 | /* We are not creating an executable, which means that we are | |
5998 | not creating a program header, and that the actual order of | |
5999 | the sections in the file is unimportant. */ | |
9ad5cbcf | 6000 | for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) |
252b5132 RH |
6001 | { |
6002 | Elf_Internal_Shdr *hdr; | |
6003 | ||
6004 | hdr = *hdrpp; | |
e0638f70 AM |
6005 | if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) |
6006 | && hdr->bfd_section == NULL) | |
0ce398f1 L |
6007 | || (hdr->bfd_section != NULL |
6008 | && (hdr->bfd_section->flags & SEC_ELF_COMPRESS)) | |
6009 | /* Compress DWARF debug sections. */ | |
12bd6957 | 6010 | || i == elf_onesymtab (abfd) |
6a40cf0c NC |
6011 | || (elf_symtab_shndx_list (abfd) != NULL |
6012 | && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) | |
3e19fb8f L |
6013 | || i == elf_strtab_sec (abfd) |
6014 | || i == elf_shstrtab_sec (abfd)) | |
252b5132 RH |
6015 | { |
6016 | hdr->sh_offset = -1; | |
252b5132 | 6017 | } |
9ad5cbcf | 6018 | else |
b34976b6 | 6019 | off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); |
252b5132 | 6020 | } |
a485e98e AM |
6021 | |
6022 | elf_next_file_pos (abfd) = off; | |
252b5132 RH |
6023 | } |
6024 | else | |
6025 | { | |
f3520d2f AM |
6026 | unsigned int alloc; |
6027 | ||
252b5132 | 6028 | /* Assign file positions for the loaded sections based on the |
08a40648 | 6029 | assignment of sections to segments. */ |
f3520d2f AM |
6030 | if (!assign_file_positions_for_load_sections (abfd, link_info)) |
6031 | return FALSE; | |
6032 | ||
6033 | /* And for non-load sections. */ | |
6034 | if (!assign_file_positions_for_non_load_sections (abfd, link_info)) | |
6035 | return FALSE; | |
6036 | ||
e36284ab AM |
6037 | if (bed->elf_backend_modify_program_headers != NULL) |
6038 | { | |
6039 | if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info)) | |
6040 | return FALSE; | |
6041 | } | |
6042 | ||
58e7ebac | 6043 | /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */ |
0e1862bb | 6044 | if (link_info != NULL && bfd_link_pie (link_info)) |
58e7ebac L |
6045 | { |
6046 | unsigned int num_segments = elf_elfheader (abfd)->e_phnum; | |
6047 | Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr; | |
6048 | Elf_Internal_Phdr *end_segment = &segment[num_segments]; | |
6049 | ||
6050 | /* Find the lowest p_vaddr in PT_LOAD segments. */ | |
6051 | bfd_vma p_vaddr = (bfd_vma) -1; | |
6052 | for (; segment < end_segment; segment++) | |
6053 | if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) | |
6054 | p_vaddr = segment->p_vaddr; | |
6055 | ||
6056 | /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD | |
6057 | segments is non-zero. */ | |
6058 | if (p_vaddr) | |
6059 | i_ehdrp->e_type = ET_EXEC; | |
6060 | } | |
6061 | ||
f3520d2f | 6062 | /* Write out the program headers. */ |
12bd6957 | 6063 | alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr; |
1a9ccd70 NC |
6064 | |
6065 | /* Sort the program headers into the ordering required by the ELF standard. */ | |
6066 | if (alloc == 0) | |
6067 | return TRUE; | |
6068 | ||
1a9ccd70 NC |
6069 | /* PR ld/20815 - Check that the program header segment, if present, will |
6070 | be loaded into memory. FIXME: The check below is not sufficient as | |
6071 | really all PT_LOAD segments should be checked before issuing an error | |
6072 | message. Plus the PHDR segment does not have to be the first segment | |
6073 | in the program header table. But this version of the check should | |
cd584857 NC |
6074 | catch all real world use cases. |
6075 | ||
6076 | FIXME: We used to have code here to sort the PT_LOAD segments into | |
6077 | ascending order, as per the ELF spec. But this breaks some programs, | |
6078 | including the Linux kernel. But really either the spec should be | |
6079 | changed or the programs updated. */ | |
1a9ccd70 | 6080 | if (alloc > 1 |
cd584857 NC |
6081 | && tdata->phdr[0].p_type == PT_PHDR |
6082 | && ! bed->elf_backend_allow_non_load_phdr (abfd, tdata->phdr, alloc) | |
6083 | && tdata->phdr[1].p_type == PT_LOAD | |
6084 | && (tdata->phdr[1].p_vaddr > tdata->phdr[0].p_vaddr | |
6085 | || (tdata->phdr[1].p_vaddr + tdata->phdr[1].p_memsz) | |
6086 | < (tdata->phdr[0].p_vaddr + tdata->phdr[0].p_memsz))) | |
1a9ccd70 NC |
6087 | { |
6088 | /* The fix for this error is usually to edit the linker script being | |
6089 | used and set up the program headers manually. Either that or | |
6090 | leave room for the headers at the start of the SECTIONS. */ | |
6091 | _bfd_error_handler (_("\ | |
6092 | %B: error: PHDR segment not covered by LOAD segment"), | |
6093 | abfd); | |
1a9ccd70 NC |
6094 | return FALSE; |
6095 | } | |
6096 | ||
f3520d2f | 6097 | if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 |
cd584857 NC |
6098 | || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) |
6099 | return FALSE; | |
252b5132 RH |
6100 | } |
6101 | ||
b34976b6 | 6102 | return TRUE; |
252b5132 RH |
6103 | } |
6104 | ||
b34976b6 | 6105 | static bfd_boolean |
217aa764 | 6106 | prep_headers (bfd *abfd) |
252b5132 | 6107 | { |
3d540e93 | 6108 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ |
2b0f7ef9 | 6109 | struct elf_strtab_hash *shstrtab; |
9c5bfbb7 | 6110 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 RH |
6111 | |
6112 | i_ehdrp = elf_elfheader (abfd); | |
252b5132 | 6113 | |
2b0f7ef9 | 6114 | shstrtab = _bfd_elf_strtab_init (); |
252b5132 | 6115 | if (shstrtab == NULL) |
b34976b6 | 6116 | return FALSE; |
252b5132 RH |
6117 | |
6118 | elf_shstrtab (abfd) = shstrtab; | |
6119 | ||
6120 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
6121 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
6122 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
6123 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
6124 | ||
6125 | i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; | |
6126 | i_ehdrp->e_ident[EI_DATA] = | |
6127 | bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; | |
6128 | i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; | |
6129 | ||
252b5132 RH |
6130 | if ((abfd->flags & DYNAMIC) != 0) |
6131 | i_ehdrp->e_type = ET_DYN; | |
6132 | else if ((abfd->flags & EXEC_P) != 0) | |
6133 | i_ehdrp->e_type = ET_EXEC; | |
6134 | else if (bfd_get_format (abfd) == bfd_core) | |
6135 | i_ehdrp->e_type = ET_CORE; | |
6136 | else | |
6137 | i_ehdrp->e_type = ET_REL; | |
6138 | ||
6139 | switch (bfd_get_arch (abfd)) | |
6140 | { | |
6141 | case bfd_arch_unknown: | |
6142 | i_ehdrp->e_machine = EM_NONE; | |
6143 | break; | |
aa4f99bb AO |
6144 | |
6145 | /* There used to be a long list of cases here, each one setting | |
6146 | e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE | |
6147 | in the corresponding bfd definition. To avoid duplication, | |
6148 | the switch was removed. Machines that need special handling | |
6149 | can generally do it in elf_backend_final_write_processing(), | |
6150 | unless they need the information earlier than the final write. | |
6151 | Such need can generally be supplied by replacing the tests for | |
6152 | e_machine with the conditions used to determine it. */ | |
252b5132 | 6153 | default: |
9c5bfbb7 AM |
6154 | i_ehdrp->e_machine = bed->elf_machine_code; |
6155 | } | |
aa4f99bb | 6156 | |
252b5132 RH |
6157 | i_ehdrp->e_version = bed->s->ev_current; |
6158 | i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; | |
6159 | ||
c044fabd | 6160 | /* No program header, for now. */ |
252b5132 RH |
6161 | i_ehdrp->e_phoff = 0; |
6162 | i_ehdrp->e_phentsize = 0; | |
6163 | i_ehdrp->e_phnum = 0; | |
6164 | ||
c044fabd | 6165 | /* Each bfd section is section header entry. */ |
252b5132 RH |
6166 | i_ehdrp->e_entry = bfd_get_start_address (abfd); |
6167 | i_ehdrp->e_shentsize = bed->s->sizeof_shdr; | |
6168 | ||
c044fabd | 6169 | /* If we're building an executable, we'll need a program header table. */ |
252b5132 | 6170 | if (abfd->flags & EXEC_P) |
0e71e495 BE |
6171 | /* It all happens later. */ |
6172 | ; | |
252b5132 RH |
6173 | else |
6174 | { | |
6175 | i_ehdrp->e_phentsize = 0; | |
252b5132 RH |
6176 | i_ehdrp->e_phoff = 0; |
6177 | } | |
6178 | ||
6179 | elf_tdata (abfd)->symtab_hdr.sh_name = | |
b34976b6 | 6180 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); |
252b5132 | 6181 | elf_tdata (abfd)->strtab_hdr.sh_name = |
b34976b6 | 6182 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); |
252b5132 | 6183 | elf_tdata (abfd)->shstrtab_hdr.sh_name = |
b34976b6 | 6184 | (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); |
252b5132 | 6185 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
17ca87fc | 6186 | || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 |
252b5132 | 6187 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) |
b34976b6 | 6188 | return FALSE; |
252b5132 | 6189 | |
b34976b6 | 6190 | return TRUE; |
252b5132 RH |
6191 | } |
6192 | ||
6193 | /* Assign file positions for all the reloc sections which are not part | |
a485e98e | 6194 | of the loadable file image, and the file position of section headers. */ |
252b5132 | 6195 | |
0ce398f1 L |
6196 | static bfd_boolean |
6197 | _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) | |
252b5132 RH |
6198 | { |
6199 | file_ptr off; | |
e06efbf1 | 6200 | Elf_Internal_Shdr **shdrpp, **end_shdrpp; |
3e19fb8f | 6201 | Elf_Internal_Shdr *shdrp; |
a485e98e AM |
6202 | Elf_Internal_Ehdr *i_ehdrp; |
6203 | const struct elf_backend_data *bed; | |
252b5132 | 6204 | |
12bd6957 | 6205 | off = elf_next_file_pos (abfd); |
252b5132 | 6206 | |
e06efbf1 L |
6207 | shdrpp = elf_elfsections (abfd); |
6208 | end_shdrpp = shdrpp + elf_numsections (abfd); | |
6209 | for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) | |
252b5132 | 6210 | { |
252b5132 | 6211 | shdrp = *shdrpp; |
0ce398f1 L |
6212 | if (shdrp->sh_offset == -1) |
6213 | { | |
3e19fb8f | 6214 | asection *sec = shdrp->bfd_section; |
0ce398f1 L |
6215 | bfd_boolean is_rel = (shdrp->sh_type == SHT_REL |
6216 | || shdrp->sh_type == SHT_RELA); | |
6217 | if (is_rel | |
3e19fb8f | 6218 | || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) |
0ce398f1 L |
6219 | { |
6220 | if (!is_rel) | |
6221 | { | |
3e19fb8f L |
6222 | const char *name = sec->name; |
6223 | struct bfd_elf_section_data *d; | |
6224 | ||
0ce398f1 | 6225 | /* Compress DWARF debug sections. */ |
3e19fb8f | 6226 | if (!bfd_compress_section (abfd, sec, |
0ce398f1 L |
6227 | shdrp->contents)) |
6228 | return FALSE; | |
3e19fb8f L |
6229 | |
6230 | if (sec->compress_status == COMPRESS_SECTION_DONE | |
6231 | && (abfd->flags & BFD_COMPRESS_GABI) == 0) | |
6232 | { | |
6233 | /* If section is compressed with zlib-gnu, convert | |
6234 | section name from .debug_* to .zdebug_*. */ | |
6235 | char *new_name | |
6236 | = convert_debug_to_zdebug (abfd, name); | |
6237 | if (new_name == NULL) | |
6238 | return FALSE; | |
6239 | name = new_name; | |
6240 | } | |
dd905818 | 6241 | /* Add section name to section name section. */ |
3e19fb8f L |
6242 | if (shdrp->sh_name != (unsigned int) -1) |
6243 | abort (); | |
6244 | shdrp->sh_name | |
6245 | = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), | |
6246 | name, FALSE); | |
6247 | d = elf_section_data (sec); | |
6248 | ||
dd905818 | 6249 | /* Add reloc section name to section name section. */ |
3e19fb8f L |
6250 | if (d->rel.hdr |
6251 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6252 | d->rel.hdr, | |
6253 | name, FALSE)) | |
6254 | return FALSE; | |
6255 | if (d->rela.hdr | |
6256 | && !_bfd_elf_set_reloc_sh_name (abfd, | |
6257 | d->rela.hdr, | |
91cb26da | 6258 | name, TRUE)) |
3e19fb8f L |
6259 | return FALSE; |
6260 | ||
0ce398f1 | 6261 | /* Update section size and contents. */ |
3e19fb8f L |
6262 | shdrp->sh_size = sec->size; |
6263 | shdrp->contents = sec->contents; | |
0ce398f1 L |
6264 | shdrp->bfd_section->contents = NULL; |
6265 | } | |
6266 | off = _bfd_elf_assign_file_position_for_section (shdrp, | |
6267 | off, | |
6268 | TRUE); | |
6269 | } | |
6270 | } | |
252b5132 RH |
6271 | } |
6272 | ||
3e19fb8f L |
6273 | /* Place section name section after DWARF debug sections have been |
6274 | compressed. */ | |
6275 | _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); | |
6276 | shdrp = &elf_tdata (abfd)->shstrtab_hdr; | |
6277 | shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); | |
6278 | off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); | |
6279 | ||
6280 | /* Place the section headers. */ | |
a485e98e AM |
6281 | i_ehdrp = elf_elfheader (abfd); |
6282 | bed = get_elf_backend_data (abfd); | |
6283 | off = align_file_position (off, 1 << bed->s->log_file_align); | |
6284 | i_ehdrp->e_shoff = off; | |
6285 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
12bd6957 | 6286 | elf_next_file_pos (abfd) = off; |
0ce398f1 L |
6287 | |
6288 | return TRUE; | |
252b5132 RH |
6289 | } |
6290 | ||
b34976b6 | 6291 | bfd_boolean |
217aa764 | 6292 | _bfd_elf_write_object_contents (bfd *abfd) |
252b5132 | 6293 | { |
9c5bfbb7 | 6294 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 6295 | Elf_Internal_Shdr **i_shdrp; |
b34976b6 | 6296 | bfd_boolean failed; |
9ad5cbcf | 6297 | unsigned int count, num_sec; |
30e8ee25 | 6298 | struct elf_obj_tdata *t; |
252b5132 RH |
6299 | |
6300 | if (! abfd->output_has_begun | |
217aa764 | 6301 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 6302 | return FALSE; |
252b5132 RH |
6303 | |
6304 | i_shdrp = elf_elfsections (abfd); | |
252b5132 | 6305 | |
b34976b6 | 6306 | failed = FALSE; |
252b5132 RH |
6307 | bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); |
6308 | if (failed) | |
b34976b6 | 6309 | return FALSE; |
252b5132 | 6310 | |
0ce398f1 L |
6311 | if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) |
6312 | return FALSE; | |
252b5132 | 6313 | |
c044fabd | 6314 | /* After writing the headers, we need to write the sections too... */ |
9ad5cbcf AM |
6315 | num_sec = elf_numsections (abfd); |
6316 | for (count = 1; count < num_sec; count++) | |
252b5132 | 6317 | { |
3e19fb8f L |
6318 | i_shdrp[count]->sh_name |
6319 | = _bfd_elf_strtab_offset (elf_shstrtab (abfd), | |
6320 | i_shdrp[count]->sh_name); | |
252b5132 RH |
6321 | if (bed->elf_backend_section_processing) |
6322 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
6323 | if (i_shdrp[count]->contents) | |
6324 | { | |
dc810e39 AM |
6325 | bfd_size_type amt = i_shdrp[count]->sh_size; |
6326 | ||
252b5132 | 6327 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
dc810e39 | 6328 | || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) |
b34976b6 | 6329 | return FALSE; |
252b5132 RH |
6330 | } |
6331 | } | |
6332 | ||
6333 | /* Write out the section header names. */ | |
30e8ee25 | 6334 | t = elf_tdata (abfd); |
26ae6d5e | 6335 | if (elf_shstrtab (abfd) != NULL |
30e8ee25 | 6336 | && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 |
08a40648 | 6337 | || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) |
b34976b6 | 6338 | return FALSE; |
252b5132 RH |
6339 | |
6340 | if (bed->elf_backend_final_write_processing) | |
12bd6957 | 6341 | (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd)); |
252b5132 | 6342 | |
ff59fc36 RM |
6343 | if (!bed->s->write_shdrs_and_ehdr (abfd)) |
6344 | return FALSE; | |
6345 | ||
6346 | /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ | |
c0355132 AM |
6347 | if (t->o->build_id.after_write_object_contents != NULL) |
6348 | return (*t->o->build_id.after_write_object_contents) (abfd); | |
ff59fc36 RM |
6349 | |
6350 | return TRUE; | |
252b5132 RH |
6351 | } |
6352 | ||
b34976b6 | 6353 | bfd_boolean |
217aa764 | 6354 | _bfd_elf_write_corefile_contents (bfd *abfd) |
252b5132 | 6355 | { |
c044fabd | 6356 | /* Hopefully this can be done just like an object file. */ |
252b5132 RH |
6357 | return _bfd_elf_write_object_contents (abfd); |
6358 | } | |
c044fabd KH |
6359 | |
6360 | /* Given a section, search the header to find them. */ | |
6361 | ||
cb33740c | 6362 | unsigned int |
198beae2 | 6363 | _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) |
252b5132 | 6364 | { |
9c5bfbb7 | 6365 | const struct elf_backend_data *bed; |
91d6fa6a | 6366 | unsigned int sec_index; |
252b5132 | 6367 | |
9ad5cbcf AM |
6368 | if (elf_section_data (asect) != NULL |
6369 | && elf_section_data (asect)->this_idx != 0) | |
6370 | return elf_section_data (asect)->this_idx; | |
6371 | ||
6372 | if (bfd_is_abs_section (asect)) | |
91d6fa6a | 6373 | sec_index = SHN_ABS; |
af746e92 | 6374 | else if (bfd_is_com_section (asect)) |
91d6fa6a | 6375 | sec_index = SHN_COMMON; |
af746e92 | 6376 | else if (bfd_is_und_section (asect)) |
91d6fa6a | 6377 | sec_index = SHN_UNDEF; |
af746e92 | 6378 | else |
91d6fa6a | 6379 | sec_index = SHN_BAD; |
252b5132 | 6380 | |
af746e92 | 6381 | bed = get_elf_backend_data (abfd); |
252b5132 RH |
6382 | if (bed->elf_backend_section_from_bfd_section) |
6383 | { | |
91d6fa6a | 6384 | int retval = sec_index; |
9ad5cbcf | 6385 | |
af746e92 AM |
6386 | if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) |
6387 | return retval; | |
252b5132 RH |
6388 | } |
6389 | ||
91d6fa6a | 6390 | if (sec_index == SHN_BAD) |
af746e92 | 6391 | bfd_set_error (bfd_error_nonrepresentable_section); |
252b5132 | 6392 | |
91d6fa6a | 6393 | return sec_index; |
252b5132 RH |
6394 | } |
6395 | ||
6396 | /* Given a BFD symbol, return the index in the ELF symbol table, or -1 | |
6397 | on error. */ | |
6398 | ||
6399 | int | |
217aa764 | 6400 | _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) |
252b5132 RH |
6401 | { |
6402 | asymbol *asym_ptr = *asym_ptr_ptr; | |
6403 | int idx; | |
6404 | flagword flags = asym_ptr->flags; | |
6405 | ||
6406 | /* When gas creates relocations against local labels, it creates its | |
6407 | own symbol for the section, but does put the symbol into the | |
6408 | symbol chain, so udata is 0. When the linker is generating | |
6409 | relocatable output, this section symbol may be for one of the | |
6410 | input sections rather than the output section. */ | |
6411 | if (asym_ptr->udata.i == 0 | |
6412 | && (flags & BSF_SECTION_SYM) | |
6413 | && asym_ptr->section) | |
6414 | { | |
5372391b | 6415 | asection *sec; |
252b5132 RH |
6416 | int indx; |
6417 | ||
5372391b AM |
6418 | sec = asym_ptr->section; |
6419 | if (sec->owner != abfd && sec->output_section != NULL) | |
6420 | sec = sec->output_section; | |
6421 | if (sec->owner == abfd | |
6422 | && (indx = sec->index) < elf_num_section_syms (abfd) | |
4e89ac30 | 6423 | && elf_section_syms (abfd)[indx] != NULL) |
252b5132 RH |
6424 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
6425 | } | |
6426 | ||
6427 | idx = asym_ptr->udata.i; | |
6428 | ||
6429 | if (idx == 0) | |
6430 | { | |
6431 | /* This case can occur when using --strip-symbol on a symbol | |
08a40648 | 6432 | which is used in a relocation entry. */ |
4eca0228 | 6433 | _bfd_error_handler |
695344c0 | 6434 | /* xgettext:c-format */ |
d003868e AM |
6435 | (_("%B: symbol `%s' required but not present"), |
6436 | abfd, bfd_asymbol_name (asym_ptr)); | |
252b5132 RH |
6437 | bfd_set_error (bfd_error_no_symbols); |
6438 | return -1; | |
6439 | } | |
6440 | ||
6441 | #if DEBUG & 4 | |
6442 | { | |
6443 | fprintf (stderr, | |
cd9af601 AM |
6444 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", |
6445 | (long) asym_ptr, asym_ptr->name, idx, flags); | |
252b5132 RH |
6446 | fflush (stderr); |
6447 | } | |
6448 | #endif | |
6449 | ||
6450 | return idx; | |
6451 | } | |
6452 | ||
84d1d650 | 6453 | /* Rewrite program header information. */ |
252b5132 | 6454 | |
b34976b6 | 6455 | static bfd_boolean |
84d1d650 | 6456 | rewrite_elf_program_header (bfd *ibfd, bfd *obfd) |
252b5132 | 6457 | { |
b34976b6 AM |
6458 | Elf_Internal_Ehdr *iehdr; |
6459 | struct elf_segment_map *map; | |
6460 | struct elf_segment_map *map_first; | |
6461 | struct elf_segment_map **pointer_to_map; | |
6462 | Elf_Internal_Phdr *segment; | |
6463 | asection *section; | |
6464 | unsigned int i; | |
6465 | unsigned int num_segments; | |
6466 | bfd_boolean phdr_included = FALSE; | |
5c44b38e | 6467 | bfd_boolean p_paddr_valid; |
b34976b6 AM |
6468 | bfd_vma maxpagesize; |
6469 | struct elf_segment_map *phdr_adjust_seg = NULL; | |
6470 | unsigned int phdr_adjust_num = 0; | |
9c5bfbb7 | 6471 | const struct elf_backend_data *bed; |
bc67d8a6 | 6472 | |
caf47ea6 | 6473 | bed = get_elf_backend_data (ibfd); |
252b5132 RH |
6474 | iehdr = elf_elfheader (ibfd); |
6475 | ||
bc67d8a6 | 6476 | map_first = NULL; |
c044fabd | 6477 | pointer_to_map = &map_first; |
252b5132 RH |
6478 | |
6479 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
bc67d8a6 NC |
6480 | maxpagesize = get_elf_backend_data (obfd)->maxpagesize; |
6481 | ||
6482 | /* Returns the end address of the segment + 1. */ | |
aecc8f8a AM |
6483 | #define SEGMENT_END(segment, start) \ |
6484 | (start + (segment->p_memsz > segment->p_filesz \ | |
6485 | ? segment->p_memsz : segment->p_filesz)) | |
bc67d8a6 | 6486 | |
eecdbe52 JJ |
6487 | #define SECTION_SIZE(section, segment) \ |
6488 | (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ | |
6489 | != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ | |
eea6121a | 6490 | ? section->size : 0) |
eecdbe52 | 6491 | |
b34976b6 | 6492 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6493 | the given segment. VMA addresses are compared. */ |
aecc8f8a AM |
6494 | #define IS_CONTAINED_BY_VMA(section, segment) \ |
6495 | (section->vma >= segment->p_vaddr \ | |
eecdbe52 | 6496 | && (section->vma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6497 | <= (SEGMENT_END (segment, segment->p_vaddr)))) |
c044fabd | 6498 | |
b34976b6 | 6499 | /* Returns TRUE if the given section is contained within |
bc67d8a6 | 6500 | the given segment. LMA addresses are compared. */ |
aecc8f8a AM |
6501 | #define IS_CONTAINED_BY_LMA(section, segment, base) \ |
6502 | (section->lma >= base \ | |
eecdbe52 | 6503 | && (section->lma + SECTION_SIZE (section, segment) \ |
aecc8f8a | 6504 | <= SEGMENT_END (segment, base))) |
252b5132 | 6505 | |
0efc80c8 L |
6506 | /* Handle PT_NOTE segment. */ |
6507 | #define IS_NOTE(p, s) \ | |
aecc8f8a | 6508 | (p->p_type == PT_NOTE \ |
0efc80c8 | 6509 | && elf_section_type (s) == SHT_NOTE \ |
aecc8f8a | 6510 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6511 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6512 | <= p->p_offset + p->p_filesz)) |
252b5132 | 6513 | |
0efc80c8 L |
6514 | /* Special case: corefile "NOTE" section containing regs, prpsinfo |
6515 | etc. */ | |
6516 | #define IS_COREFILE_NOTE(p, s) \ | |
6517 | (IS_NOTE (p, s) \ | |
6518 | && bfd_get_format (ibfd) == bfd_core \ | |
6519 | && s->vma == 0 \ | |
6520 | && s->lma == 0) | |
6521 | ||
252b5132 RH |
6522 | /* The complicated case when p_vaddr is 0 is to handle the Solaris |
6523 | linker, which generates a PT_INTERP section with p_vaddr and | |
6524 | p_memsz set to 0. */ | |
aecc8f8a AM |
6525 | #define IS_SOLARIS_PT_INTERP(p, s) \ |
6526 | (p->p_vaddr == 0 \ | |
6527 | && p->p_paddr == 0 \ | |
6528 | && p->p_memsz == 0 \ | |
6529 | && p->p_filesz > 0 \ | |
6530 | && (s->flags & SEC_HAS_CONTENTS) != 0 \ | |
eea6121a | 6531 | && s->size > 0 \ |
aecc8f8a | 6532 | && (bfd_vma) s->filepos >= p->p_offset \ |
cb3ff1e5 | 6533 | && ((bfd_vma) s->filepos + s->size \ |
aecc8f8a | 6534 | <= p->p_offset + p->p_filesz)) |
5c440b1e | 6535 | |
bc67d8a6 NC |
6536 | /* Decide if the given section should be included in the given segment. |
6537 | A section will be included if: | |
f5ffc919 | 6538 | 1. It is within the address space of the segment -- we use the LMA |
08a40648 | 6539 | if that is set for the segment and the VMA otherwise, |
0efc80c8 | 6540 | 2. It is an allocated section or a NOTE section in a PT_NOTE |
d324f6d6 | 6541 | segment. |
bc67d8a6 | 6542 | 3. There is an output section associated with it, |
eecdbe52 | 6543 | 4. The section has not already been allocated to a previous segment. |
2b05f1b7 | 6544 | 5. PT_GNU_STACK segments do not include any sections. |
03394ac9 | 6545 | 6. PT_TLS segment includes only SHF_TLS sections. |
6f79b219 JJ |
6546 | 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. |
6547 | 8. PT_DYNAMIC should not contain empty sections at the beginning | |
08a40648 | 6548 | (with the possible exception of .dynamic). */ |
9f17e2a6 | 6549 | #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \ |
2b05f1b7 L |
6550 | ((((segment->p_paddr \ |
6551 | ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ | |
6552 | : IS_CONTAINED_BY_VMA (section, segment)) \ | |
6553 | && (section->flags & SEC_ALLOC) != 0) \ | |
0efc80c8 | 6554 | || IS_NOTE (segment, section)) \ |
2b05f1b7 L |
6555 | && segment->p_type != PT_GNU_STACK \ |
6556 | && (segment->p_type != PT_TLS \ | |
6557 | || (section->flags & SEC_THREAD_LOCAL)) \ | |
6558 | && (segment->p_type == PT_LOAD \ | |
6559 | || segment->p_type == PT_TLS \ | |
6560 | || (section->flags & SEC_THREAD_LOCAL) == 0) \ | |
6561 | && (segment->p_type != PT_DYNAMIC \ | |
6562 | || SECTION_SIZE (section, segment) > 0 \ | |
6563 | || (segment->p_paddr \ | |
6564 | ? segment->p_paddr != section->lma \ | |
6565 | : segment->p_vaddr != section->vma) \ | |
6566 | || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ | |
6567 | == 0)) \ | |
0067a569 | 6568 | && !section->segment_mark) |
bc67d8a6 | 6569 | |
9f17e2a6 L |
6570 | /* If the output section of a section in the input segment is NULL, |
6571 | it is removed from the corresponding output segment. */ | |
6572 | #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ | |
6573 | (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \ | |
6574 | && section->output_section != NULL) | |
6575 | ||
b34976b6 | 6576 | /* Returns TRUE iff seg1 starts after the end of seg2. */ |
b5f852ea NC |
6577 | #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ |
6578 | (seg1->field >= SEGMENT_END (seg2, seg2->field)) | |
6579 | ||
6580 | /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both | |
6581 | their VMA address ranges and their LMA address ranges overlap. | |
6582 | It is possible to have overlapping VMA ranges without overlapping LMA | |
6583 | ranges. RedBoot images for example can have both .data and .bss mapped | |
6584 | to the same VMA range, but with the .data section mapped to a different | |
6585 | LMA. */ | |
aecc8f8a | 6586 | #define SEGMENT_OVERLAPS(seg1, seg2) \ |
b5f852ea | 6587 | ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ |
08a40648 | 6588 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ |
b5f852ea | 6589 | && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ |
08a40648 | 6590 | || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) |
bc67d8a6 NC |
6591 | |
6592 | /* Initialise the segment mark field. */ | |
6593 | for (section = ibfd->sections; section != NULL; section = section->next) | |
b34976b6 | 6594 | section->segment_mark = FALSE; |
bc67d8a6 | 6595 | |
5c44b38e AM |
6596 | /* The Solaris linker creates program headers in which all the |
6597 | p_paddr fields are zero. When we try to objcopy or strip such a | |
6598 | file, we get confused. Check for this case, and if we find it | |
6599 | don't set the p_paddr_valid fields. */ | |
6600 | p_paddr_valid = FALSE; | |
6601 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6602 | i < num_segments; | |
6603 | i++, segment++) | |
6604 | if (segment->p_paddr != 0) | |
6605 | { | |
6606 | p_paddr_valid = TRUE; | |
6607 | break; | |
6608 | } | |
6609 | ||
252b5132 | 6610 | /* Scan through the segments specified in the program header |
bc67d8a6 | 6611 | of the input BFD. For this first scan we look for overlaps |
9ad5cbcf | 6612 | in the loadable segments. These can be created by weird |
aecc8f8a | 6613 | parameters to objcopy. Also, fix some solaris weirdness. */ |
bc67d8a6 NC |
6614 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
6615 | i < num_segments; | |
c044fabd | 6616 | i++, segment++) |
252b5132 | 6617 | { |
252b5132 | 6618 | unsigned int j; |
c044fabd | 6619 | Elf_Internal_Phdr *segment2; |
252b5132 | 6620 | |
aecc8f8a AM |
6621 | if (segment->p_type == PT_INTERP) |
6622 | for (section = ibfd->sections; section; section = section->next) | |
6623 | if (IS_SOLARIS_PT_INTERP (segment, section)) | |
6624 | { | |
6625 | /* Mininal change so that the normal section to segment | |
4cc11e76 | 6626 | assignment code will work. */ |
aecc8f8a AM |
6627 | segment->p_vaddr = section->vma; |
6628 | break; | |
6629 | } | |
6630 | ||
bc67d8a6 | 6631 | if (segment->p_type != PT_LOAD) |
b10a8ae0 L |
6632 | { |
6633 | /* Remove PT_GNU_RELRO segment. */ | |
6634 | if (segment->p_type == PT_GNU_RELRO) | |
6635 | segment->p_type = PT_NULL; | |
6636 | continue; | |
6637 | } | |
c044fabd | 6638 | |
bc67d8a6 | 6639 | /* Determine if this segment overlaps any previous segments. */ |
0067a569 | 6640 | for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) |
bc67d8a6 NC |
6641 | { |
6642 | bfd_signed_vma extra_length; | |
c044fabd | 6643 | |
bc67d8a6 | 6644 | if (segment2->p_type != PT_LOAD |
0067a569 | 6645 | || !SEGMENT_OVERLAPS (segment, segment2)) |
bc67d8a6 | 6646 | continue; |
c044fabd | 6647 | |
bc67d8a6 NC |
6648 | /* Merge the two segments together. */ |
6649 | if (segment2->p_vaddr < segment->p_vaddr) | |
6650 | { | |
c044fabd | 6651 | /* Extend SEGMENT2 to include SEGMENT and then delete |
08a40648 | 6652 | SEGMENT. */ |
0067a569 AM |
6653 | extra_length = (SEGMENT_END (segment, segment->p_vaddr) |
6654 | - SEGMENT_END (segment2, segment2->p_vaddr)); | |
c044fabd | 6655 | |
bc67d8a6 NC |
6656 | if (extra_length > 0) |
6657 | { | |
0067a569 | 6658 | segment2->p_memsz += extra_length; |
bc67d8a6 NC |
6659 | segment2->p_filesz += extra_length; |
6660 | } | |
c044fabd | 6661 | |
bc67d8a6 | 6662 | segment->p_type = PT_NULL; |
c044fabd | 6663 | |
bc67d8a6 NC |
6664 | /* Since we have deleted P we must restart the outer loop. */ |
6665 | i = 0; | |
6666 | segment = elf_tdata (ibfd)->phdr; | |
6667 | break; | |
6668 | } | |
6669 | else | |
6670 | { | |
c044fabd | 6671 | /* Extend SEGMENT to include SEGMENT2 and then delete |
08a40648 | 6672 | SEGMENT2. */ |
0067a569 AM |
6673 | extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) |
6674 | - SEGMENT_END (segment, segment->p_vaddr)); | |
c044fabd | 6675 | |
bc67d8a6 NC |
6676 | if (extra_length > 0) |
6677 | { | |
0067a569 | 6678 | segment->p_memsz += extra_length; |
bc67d8a6 NC |
6679 | segment->p_filesz += extra_length; |
6680 | } | |
c044fabd | 6681 | |
bc67d8a6 NC |
6682 | segment2->p_type = PT_NULL; |
6683 | } | |
6684 | } | |
6685 | } | |
c044fabd | 6686 | |
bc67d8a6 NC |
6687 | /* The second scan attempts to assign sections to segments. */ |
6688 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
6689 | i < num_segments; | |
0067a569 | 6690 | i++, segment++) |
bc67d8a6 | 6691 | { |
0067a569 AM |
6692 | unsigned int section_count; |
6693 | asection **sections; | |
6694 | asection *output_section; | |
6695 | unsigned int isec; | |
6696 | bfd_vma matching_lma; | |
6697 | bfd_vma suggested_lma; | |
6698 | unsigned int j; | |
dc810e39 | 6699 | bfd_size_type amt; |
0067a569 AM |
6700 | asection *first_section; |
6701 | bfd_boolean first_matching_lma; | |
6702 | bfd_boolean first_suggested_lma; | |
bc67d8a6 NC |
6703 | |
6704 | if (segment->p_type == PT_NULL) | |
6705 | continue; | |
c044fabd | 6706 | |
9f17e2a6 | 6707 | first_section = NULL; |
bc67d8a6 | 6708 | /* Compute how many sections might be placed into this segment. */ |
b5f852ea NC |
6709 | for (section = ibfd->sections, section_count = 0; |
6710 | section != NULL; | |
6711 | section = section->next) | |
9f17e2a6 L |
6712 | { |
6713 | /* Find the first section in the input segment, which may be | |
6714 | removed from the corresponding output segment. */ | |
6715 | if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)) | |
6716 | { | |
6717 | if (first_section == NULL) | |
6718 | first_section = section; | |
6719 | if (section->output_section != NULL) | |
6720 | ++section_count; | |
6721 | } | |
6722 | } | |
811072d8 | 6723 | |
b5f852ea NC |
6724 | /* Allocate a segment map big enough to contain |
6725 | all of the sections we have selected. */ | |
dc810e39 AM |
6726 | amt = sizeof (struct elf_segment_map); |
6727 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 6728 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 6729 | if (map == NULL) |
b34976b6 | 6730 | return FALSE; |
252b5132 RH |
6731 | |
6732 | /* Initialise the fields of the segment map. Default to | |
6733 | using the physical address of the segment in the input BFD. */ | |
0067a569 AM |
6734 | map->next = NULL; |
6735 | map->p_type = segment->p_type; | |
6736 | map->p_flags = segment->p_flags; | |
bc67d8a6 | 6737 | map->p_flags_valid = 1; |
55d55ac7 | 6738 | |
9f17e2a6 L |
6739 | /* If the first section in the input segment is removed, there is |
6740 | no need to preserve segment physical address in the corresponding | |
6741 | output segment. */ | |
945c025a | 6742 | if (!first_section || first_section->output_section != NULL) |
9f17e2a6 L |
6743 | { |
6744 | map->p_paddr = segment->p_paddr; | |
5c44b38e | 6745 | map->p_paddr_valid = p_paddr_valid; |
9f17e2a6 | 6746 | } |
252b5132 RH |
6747 | |
6748 | /* Determine if this segment contains the ELF file header | |
6749 | and if it contains the program headers themselves. */ | |
bc67d8a6 NC |
6750 | map->includes_filehdr = (segment->p_offset == 0 |
6751 | && segment->p_filesz >= iehdr->e_ehsize); | |
bc67d8a6 | 6752 | map->includes_phdrs = 0; |
252b5132 | 6753 | |
0067a569 | 6754 | if (!phdr_included || segment->p_type != PT_LOAD) |
252b5132 | 6755 | { |
bc67d8a6 NC |
6756 | map->includes_phdrs = |
6757 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
6758 | && (segment->p_offset + segment->p_filesz | |
252b5132 RH |
6759 | >= ((bfd_vma) iehdr->e_phoff |
6760 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
c044fabd | 6761 | |
bc67d8a6 | 6762 | if (segment->p_type == PT_LOAD && map->includes_phdrs) |
b34976b6 | 6763 | phdr_included = TRUE; |
252b5132 RH |
6764 | } |
6765 | ||
bc67d8a6 | 6766 | if (section_count == 0) |
252b5132 RH |
6767 | { |
6768 | /* Special segments, such as the PT_PHDR segment, may contain | |
6769 | no sections, but ordinary, loadable segments should contain | |
1ed89aa9 | 6770 | something. They are allowed by the ELF spec however, so only |
f98450c6 NC |
6771 | a warning is produced. |
6772 | There is however the valid use case of embedded systems which | |
6773 | have segments with p_filesz of 0 and a p_memsz > 0 to initialize | |
6774 | flash memory with zeros. No warning is shown for that case. */ | |
6775 | if (segment->p_type == PT_LOAD | |
6776 | && (segment->p_filesz > 0 || segment->p_memsz == 0)) | |
6777 | /* xgettext:c-format */ | |
76cfced5 AM |
6778 | _bfd_error_handler (_("%B: warning: Empty loadable segment detected" |
6779 | " at vaddr=%#Lx, is this intentional?"), | |
f98450c6 | 6780 | ibfd, segment->p_vaddr); |
252b5132 | 6781 | |
bc67d8a6 | 6782 | map->count = 0; |
c044fabd KH |
6783 | *pointer_to_map = map; |
6784 | pointer_to_map = &map->next; | |
252b5132 RH |
6785 | |
6786 | continue; | |
6787 | } | |
6788 | ||
6789 | /* Now scan the sections in the input BFD again and attempt | |
6790 | to add their corresponding output sections to the segment map. | |
6791 | The problem here is how to handle an output section which has | |
6792 | been moved (ie had its LMA changed). There are four possibilities: | |
6793 | ||
6794 | 1. None of the sections have been moved. | |
6795 | In this case we can continue to use the segment LMA from the | |
6796 | input BFD. | |
6797 | ||
6798 | 2. All of the sections have been moved by the same amount. | |
6799 | In this case we can change the segment's LMA to match the LMA | |
6800 | of the first section. | |
6801 | ||
6802 | 3. Some of the sections have been moved, others have not. | |
6803 | In this case those sections which have not been moved can be | |
6804 | placed in the current segment which will have to have its size, | |
6805 | and possibly its LMA changed, and a new segment or segments will | |
6806 | have to be created to contain the other sections. | |
6807 | ||
b5f852ea | 6808 | 4. The sections have been moved, but not by the same amount. |
252b5132 RH |
6809 | In this case we can change the segment's LMA to match the LMA |
6810 | of the first section and we will have to create a new segment | |
6811 | or segments to contain the other sections. | |
6812 | ||
6813 | In order to save time, we allocate an array to hold the section | |
6814 | pointers that we are interested in. As these sections get assigned | |
6815 | to a segment, they are removed from this array. */ | |
6816 | ||
a50b1753 | 6817 | sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *)); |
252b5132 | 6818 | if (sections == NULL) |
b34976b6 | 6819 | return FALSE; |
252b5132 RH |
6820 | |
6821 | /* Step One: Scan for segment vs section LMA conflicts. | |
6822 | Also add the sections to the section array allocated above. | |
6823 | Also add the sections to the current segment. In the common | |
6824 | case, where the sections have not been moved, this means that | |
6825 | we have completely filled the segment, and there is nothing | |
6826 | more to do. */ | |
252b5132 | 6827 | isec = 0; |
72730e0c | 6828 | matching_lma = 0; |
252b5132 | 6829 | suggested_lma = 0; |
0067a569 AM |
6830 | first_matching_lma = TRUE; |
6831 | first_suggested_lma = TRUE; | |
252b5132 | 6832 | |
461c4b2e | 6833 | for (section = first_section, j = 0; |
bc67d8a6 NC |
6834 | section != NULL; |
6835 | section = section->next) | |
252b5132 | 6836 | { |
caf47ea6 | 6837 | if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) |
c0f7859b | 6838 | { |
bc67d8a6 NC |
6839 | output_section = section->output_section; |
6840 | ||
0067a569 | 6841 | sections[j++] = section; |
252b5132 RH |
6842 | |
6843 | /* The Solaris native linker always sets p_paddr to 0. | |
6844 | We try to catch that case here, and set it to the | |
5e8d7549 NC |
6845 | correct value. Note - some backends require that |
6846 | p_paddr be left as zero. */ | |
5c44b38e | 6847 | if (!p_paddr_valid |
4455705d | 6848 | && segment->p_vaddr != 0 |
0067a569 | 6849 | && !bed->want_p_paddr_set_to_zero |
252b5132 | 6850 | && isec == 0 |
bc67d8a6 | 6851 | && output_section->lma != 0 |
0067a569 AM |
6852 | && output_section->vma == (segment->p_vaddr |
6853 | + (map->includes_filehdr | |
6854 | ? iehdr->e_ehsize | |
6855 | : 0) | |
6856 | + (map->includes_phdrs | |
6857 | ? (iehdr->e_phnum | |
6858 | * iehdr->e_phentsize) | |
6859 | : 0))) | |
bc67d8a6 | 6860 | map->p_paddr = segment->p_vaddr; |
252b5132 RH |
6861 | |
6862 | /* Match up the physical address of the segment with the | |
6863 | LMA address of the output section. */ | |
bc67d8a6 | 6864 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
5e8d7549 | 6865 | || IS_COREFILE_NOTE (segment, section) |
0067a569 AM |
6866 | || (bed->want_p_paddr_set_to_zero |
6867 | && IS_CONTAINED_BY_VMA (output_section, segment))) | |
252b5132 | 6868 | { |
0067a569 AM |
6869 | if (first_matching_lma || output_section->lma < matching_lma) |
6870 | { | |
6871 | matching_lma = output_section->lma; | |
6872 | first_matching_lma = FALSE; | |
6873 | } | |
252b5132 RH |
6874 | |
6875 | /* We assume that if the section fits within the segment | |
bc67d8a6 | 6876 | then it does not overlap any other section within that |
252b5132 | 6877 | segment. */ |
0067a569 AM |
6878 | map->sections[isec++] = output_section; |
6879 | } | |
6880 | else if (first_suggested_lma) | |
6881 | { | |
6882 | suggested_lma = output_section->lma; | |
6883 | first_suggested_lma = FALSE; | |
252b5132 | 6884 | } |
147d51c2 L |
6885 | |
6886 | if (j == section_count) | |
6887 | break; | |
252b5132 RH |
6888 | } |
6889 | } | |
6890 | ||
bc67d8a6 | 6891 | BFD_ASSERT (j == section_count); |
252b5132 RH |
6892 | |
6893 | /* Step Two: Adjust the physical address of the current segment, | |
6894 | if necessary. */ | |
bc67d8a6 | 6895 | if (isec == section_count) |
252b5132 RH |
6896 | { |
6897 | /* All of the sections fitted within the segment as currently | |
6898 | specified. This is the default case. Add the segment to | |
6899 | the list of built segments and carry on to process the next | |
6900 | program header in the input BFD. */ | |
bc67d8a6 | 6901 | map->count = section_count; |
c044fabd KH |
6902 | *pointer_to_map = map; |
6903 | pointer_to_map = &map->next; | |
08a40648 | 6904 | |
5c44b38e AM |
6905 | if (p_paddr_valid |
6906 | && !bed->want_p_paddr_set_to_zero | |
147d51c2 | 6907 | && matching_lma != map->p_paddr |
5c44b38e AM |
6908 | && !map->includes_filehdr |
6909 | && !map->includes_phdrs) | |
3271a814 NS |
6910 | /* There is some padding before the first section in the |
6911 | segment. So, we must account for that in the output | |
6912 | segment's vma. */ | |
6913 | map->p_vaddr_offset = matching_lma - map->p_paddr; | |
08a40648 | 6914 | |
252b5132 RH |
6915 | free (sections); |
6916 | continue; | |
6917 | } | |
252b5132 RH |
6918 | else |
6919 | { | |
0067a569 | 6920 | if (!first_matching_lma) |
72730e0c AM |
6921 | { |
6922 | /* At least one section fits inside the current segment. | |
6923 | Keep it, but modify its physical address to match the | |
6924 | LMA of the first section that fitted. */ | |
bc67d8a6 | 6925 | map->p_paddr = matching_lma; |
72730e0c AM |
6926 | } |
6927 | else | |
6928 | { | |
6929 | /* None of the sections fitted inside the current segment. | |
6930 | Change the current segment's physical address to match | |
6931 | the LMA of the first section. */ | |
bc67d8a6 | 6932 | map->p_paddr = suggested_lma; |
72730e0c AM |
6933 | } |
6934 | ||
bc67d8a6 NC |
6935 | /* Offset the segment physical address from the lma |
6936 | to allow for space taken up by elf headers. */ | |
6937 | if (map->includes_filehdr) | |
010c8431 AM |
6938 | { |
6939 | if (map->p_paddr >= iehdr->e_ehsize) | |
6940 | map->p_paddr -= iehdr->e_ehsize; | |
6941 | else | |
6942 | { | |
6943 | map->includes_filehdr = FALSE; | |
6944 | map->includes_phdrs = FALSE; | |
6945 | } | |
6946 | } | |
252b5132 | 6947 | |
bc67d8a6 NC |
6948 | if (map->includes_phdrs) |
6949 | { | |
010c8431 AM |
6950 | if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize) |
6951 | { | |
6952 | map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; | |
6953 | ||
6954 | /* iehdr->e_phnum is just an estimate of the number | |
6955 | of program headers that we will need. Make a note | |
6956 | here of the number we used and the segment we chose | |
6957 | to hold these headers, so that we can adjust the | |
6958 | offset when we know the correct value. */ | |
6959 | phdr_adjust_num = iehdr->e_phnum; | |
6960 | phdr_adjust_seg = map; | |
6961 | } | |
6962 | else | |
6963 | map->includes_phdrs = FALSE; | |
bc67d8a6 | 6964 | } |
252b5132 RH |
6965 | } |
6966 | ||
6967 | /* Step Three: Loop over the sections again, this time assigning | |
caf47ea6 | 6968 | those that fit to the current segment and removing them from the |
252b5132 RH |
6969 | sections array; but making sure not to leave large gaps. Once all |
6970 | possible sections have been assigned to the current segment it is | |
6971 | added to the list of built segments and if sections still remain | |
6972 | to be assigned, a new segment is constructed before repeating | |
6973 | the loop. */ | |
6974 | isec = 0; | |
6975 | do | |
6976 | { | |
bc67d8a6 | 6977 | map->count = 0; |
252b5132 | 6978 | suggested_lma = 0; |
0067a569 | 6979 | first_suggested_lma = TRUE; |
252b5132 RH |
6980 | |
6981 | /* Fill the current segment with sections that fit. */ | |
bc67d8a6 | 6982 | for (j = 0; j < section_count; j++) |
252b5132 | 6983 | { |
bc67d8a6 | 6984 | section = sections[j]; |
252b5132 | 6985 | |
bc67d8a6 | 6986 | if (section == NULL) |
252b5132 RH |
6987 | continue; |
6988 | ||
bc67d8a6 | 6989 | output_section = section->output_section; |
252b5132 | 6990 | |
bc67d8a6 | 6991 | BFD_ASSERT (output_section != NULL); |
c044fabd | 6992 | |
bc67d8a6 NC |
6993 | if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) |
6994 | || IS_COREFILE_NOTE (segment, section)) | |
252b5132 | 6995 | { |
bc67d8a6 | 6996 | if (map->count == 0) |
252b5132 RH |
6997 | { |
6998 | /* If the first section in a segment does not start at | |
bc67d8a6 NC |
6999 | the beginning of the segment, then something is |
7000 | wrong. */ | |
0067a569 AM |
7001 | if (output_section->lma |
7002 | != (map->p_paddr | |
7003 | + (map->includes_filehdr ? iehdr->e_ehsize : 0) | |
7004 | + (map->includes_phdrs | |
7005 | ? iehdr->e_phnum * iehdr->e_phentsize | |
7006 | : 0))) | |
252b5132 RH |
7007 | abort (); |
7008 | } | |
7009 | else | |
7010 | { | |
0067a569 | 7011 | asection *prev_sec; |
252b5132 | 7012 | |
bc67d8a6 | 7013 | prev_sec = map->sections[map->count - 1]; |
252b5132 RH |
7014 | |
7015 | /* If the gap between the end of the previous section | |
bc67d8a6 NC |
7016 | and the start of this section is more than |
7017 | maxpagesize then we need to start a new segment. */ | |
eea6121a | 7018 | if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, |
079e9a2f | 7019 | maxpagesize) |
caf47ea6 | 7020 | < BFD_ALIGN (output_section->lma, maxpagesize)) |
0067a569 | 7021 | || (prev_sec->lma + prev_sec->size |
079e9a2f | 7022 | > output_section->lma)) |
252b5132 | 7023 | { |
0067a569 AM |
7024 | if (first_suggested_lma) |
7025 | { | |
7026 | suggested_lma = output_section->lma; | |
7027 | first_suggested_lma = FALSE; | |
7028 | } | |
252b5132 RH |
7029 | |
7030 | continue; | |
7031 | } | |
7032 | } | |
7033 | ||
bc67d8a6 | 7034 | map->sections[map->count++] = output_section; |
252b5132 RH |
7035 | ++isec; |
7036 | sections[j] = NULL; | |
b34976b6 | 7037 | section->segment_mark = TRUE; |
252b5132 | 7038 | } |
0067a569 AM |
7039 | else if (first_suggested_lma) |
7040 | { | |
7041 | suggested_lma = output_section->lma; | |
7042 | first_suggested_lma = FALSE; | |
7043 | } | |
252b5132 RH |
7044 | } |
7045 | ||
bc67d8a6 | 7046 | BFD_ASSERT (map->count > 0); |
252b5132 RH |
7047 | |
7048 | /* Add the current segment to the list of built segments. */ | |
c044fabd KH |
7049 | *pointer_to_map = map; |
7050 | pointer_to_map = &map->next; | |
252b5132 | 7051 | |
bc67d8a6 | 7052 | if (isec < section_count) |
252b5132 RH |
7053 | { |
7054 | /* We still have not allocated all of the sections to | |
7055 | segments. Create a new segment here, initialise it | |
7056 | and carry on looping. */ | |
dc810e39 AM |
7057 | amt = sizeof (struct elf_segment_map); |
7058 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
5964fc3a | 7059 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
bc67d8a6 | 7060 | if (map == NULL) |
5ed6aba4 NC |
7061 | { |
7062 | free (sections); | |
7063 | return FALSE; | |
7064 | } | |
252b5132 RH |
7065 | |
7066 | /* Initialise the fields of the segment map. Set the physical | |
7067 | physical address to the LMA of the first section that has | |
7068 | not yet been assigned. */ | |
0067a569 AM |
7069 | map->next = NULL; |
7070 | map->p_type = segment->p_type; | |
7071 | map->p_flags = segment->p_flags; | |
7072 | map->p_flags_valid = 1; | |
7073 | map->p_paddr = suggested_lma; | |
5c44b38e | 7074 | map->p_paddr_valid = p_paddr_valid; |
bc67d8a6 | 7075 | map->includes_filehdr = 0; |
0067a569 | 7076 | map->includes_phdrs = 0; |
252b5132 RH |
7077 | } |
7078 | } | |
bc67d8a6 | 7079 | while (isec < section_count); |
252b5132 RH |
7080 | |
7081 | free (sections); | |
7082 | } | |
7083 | ||
12bd6957 | 7084 | elf_seg_map (obfd) = map_first; |
bc67d8a6 NC |
7085 | |
7086 | /* If we had to estimate the number of program headers that were | |
9ad5cbcf | 7087 | going to be needed, then check our estimate now and adjust |
bc67d8a6 NC |
7088 | the offset if necessary. */ |
7089 | if (phdr_adjust_seg != NULL) | |
7090 | { | |
7091 | unsigned int count; | |
c044fabd | 7092 | |
bc67d8a6 | 7093 | for (count = 0, map = map_first; map != NULL; map = map->next) |
c044fabd | 7094 | count++; |
252b5132 | 7095 | |
bc67d8a6 NC |
7096 | if (count > phdr_adjust_num) |
7097 | phdr_adjust_seg->p_paddr | |
7098 | -= (count - phdr_adjust_num) * iehdr->e_phentsize; | |
7099 | } | |
c044fabd | 7100 | |
bc67d8a6 | 7101 | #undef SEGMENT_END |
eecdbe52 | 7102 | #undef SECTION_SIZE |
bc67d8a6 NC |
7103 | #undef IS_CONTAINED_BY_VMA |
7104 | #undef IS_CONTAINED_BY_LMA | |
0efc80c8 | 7105 | #undef IS_NOTE |
252b5132 | 7106 | #undef IS_COREFILE_NOTE |
bc67d8a6 | 7107 | #undef IS_SOLARIS_PT_INTERP |
9f17e2a6 | 7108 | #undef IS_SECTION_IN_INPUT_SEGMENT |
bc67d8a6 NC |
7109 | #undef INCLUDE_SECTION_IN_SEGMENT |
7110 | #undef SEGMENT_AFTER_SEGMENT | |
7111 | #undef SEGMENT_OVERLAPS | |
b34976b6 | 7112 | return TRUE; |
252b5132 RH |
7113 | } |
7114 | ||
84d1d650 L |
7115 | /* Copy ELF program header information. */ |
7116 | ||
7117 | static bfd_boolean | |
7118 | copy_elf_program_header (bfd *ibfd, bfd *obfd) | |
7119 | { | |
7120 | Elf_Internal_Ehdr *iehdr; | |
7121 | struct elf_segment_map *map; | |
7122 | struct elf_segment_map *map_first; | |
7123 | struct elf_segment_map **pointer_to_map; | |
7124 | Elf_Internal_Phdr *segment; | |
7125 | unsigned int i; | |
7126 | unsigned int num_segments; | |
7127 | bfd_boolean phdr_included = FALSE; | |
88967714 | 7128 | bfd_boolean p_paddr_valid; |
84d1d650 L |
7129 | |
7130 | iehdr = elf_elfheader (ibfd); | |
7131 | ||
7132 | map_first = NULL; | |
7133 | pointer_to_map = &map_first; | |
7134 | ||
88967714 AM |
7135 | /* If all the segment p_paddr fields are zero, don't set |
7136 | map->p_paddr_valid. */ | |
7137 | p_paddr_valid = FALSE; | |
84d1d650 | 7138 | num_segments = elf_elfheader (ibfd)->e_phnum; |
88967714 AM |
7139 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7140 | i < num_segments; | |
7141 | i++, segment++) | |
7142 | if (segment->p_paddr != 0) | |
7143 | { | |
7144 | p_paddr_valid = TRUE; | |
7145 | break; | |
7146 | } | |
7147 | ||
84d1d650 L |
7148 | for (i = 0, segment = elf_tdata (ibfd)->phdr; |
7149 | i < num_segments; | |
7150 | i++, segment++) | |
7151 | { | |
7152 | asection *section; | |
7153 | unsigned int section_count; | |
7154 | bfd_size_type amt; | |
7155 | Elf_Internal_Shdr *this_hdr; | |
53020534 | 7156 | asection *first_section = NULL; |
a76e6f2f | 7157 | asection *lowest_section; |
84d1d650 | 7158 | |
84d1d650 L |
7159 | /* Compute how many sections are in this segment. */ |
7160 | for (section = ibfd->sections, section_count = 0; | |
7161 | section != NULL; | |
7162 | section = section->next) | |
7163 | { | |
7164 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7165 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
3271a814 | 7166 | { |
a76e6f2f AM |
7167 | if (first_section == NULL) |
7168 | first_section = section; | |
3271a814 NS |
7169 | section_count++; |
7170 | } | |
84d1d650 L |
7171 | } |
7172 | ||
7173 | /* Allocate a segment map big enough to contain | |
7174 | all of the sections we have selected. */ | |
7175 | amt = sizeof (struct elf_segment_map); | |
7176 | if (section_count != 0) | |
7177 | amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); | |
a50b1753 | 7178 | map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); |
84d1d650 L |
7179 | if (map == NULL) |
7180 | return FALSE; | |
7181 | ||
7182 | /* Initialize the fields of the output segment map with the | |
7183 | input segment. */ | |
7184 | map->next = NULL; | |
7185 | map->p_type = segment->p_type; | |
7186 | map->p_flags = segment->p_flags; | |
7187 | map->p_flags_valid = 1; | |
7188 | map->p_paddr = segment->p_paddr; | |
88967714 | 7189 | map->p_paddr_valid = p_paddr_valid; |
3f570048 AM |
7190 | map->p_align = segment->p_align; |
7191 | map->p_align_valid = 1; | |
3271a814 | 7192 | map->p_vaddr_offset = 0; |
84d1d650 | 7193 | |
04c3a755 NS |
7194 | if (map->p_type == PT_GNU_RELRO |
7195 | || map->p_type == PT_GNU_STACK) | |
b10a8ae0 L |
7196 | { |
7197 | /* The PT_GNU_RELRO segment may contain the first a few | |
7198 | bytes in the .got.plt section even if the whole .got.plt | |
7199 | section isn't in the PT_GNU_RELRO segment. We won't | |
04c3a755 NS |
7200 | change the size of the PT_GNU_RELRO segment. |
7201 | Similarly, PT_GNU_STACK size is significant on uclinux | |
7202 | systems. */ | |
9433b9b1 | 7203 | map->p_size = segment->p_memsz; |
b10a8ae0 L |
7204 | map->p_size_valid = 1; |
7205 | } | |
7206 | ||
84d1d650 L |
7207 | /* Determine if this segment contains the ELF file header |
7208 | and if it contains the program headers themselves. */ | |
7209 | map->includes_filehdr = (segment->p_offset == 0 | |
7210 | && segment->p_filesz >= iehdr->e_ehsize); | |
7211 | ||
7212 | map->includes_phdrs = 0; | |
7213 | if (! phdr_included || segment->p_type != PT_LOAD) | |
7214 | { | |
7215 | map->includes_phdrs = | |
7216 | (segment->p_offset <= (bfd_vma) iehdr->e_phoff | |
7217 | && (segment->p_offset + segment->p_filesz | |
7218 | >= ((bfd_vma) iehdr->e_phoff | |
7219 | + iehdr->e_phnum * iehdr->e_phentsize))); | |
7220 | ||
7221 | if (segment->p_type == PT_LOAD && map->includes_phdrs) | |
7222 | phdr_included = TRUE; | |
7223 | } | |
7224 | ||
bbefd0a9 | 7225 | lowest_section = NULL; |
84d1d650 L |
7226 | if (section_count != 0) |
7227 | { | |
7228 | unsigned int isec = 0; | |
7229 | ||
53020534 | 7230 | for (section = first_section; |
84d1d650 L |
7231 | section != NULL; |
7232 | section = section->next) | |
7233 | { | |
7234 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7235 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
53020534 L |
7236 | { |
7237 | map->sections[isec++] = section->output_section; | |
a76e6f2f AM |
7238 | if ((section->flags & SEC_ALLOC) != 0) |
7239 | { | |
7240 | bfd_vma seg_off; | |
7241 | ||
bbefd0a9 AM |
7242 | if (lowest_section == NULL |
7243 | || section->lma < lowest_section->lma) | |
fb8a5684 AM |
7244 | lowest_section = section; |
7245 | ||
a76e6f2f AM |
7246 | /* Section lmas are set up from PT_LOAD header |
7247 | p_paddr in _bfd_elf_make_section_from_shdr. | |
7248 | If this header has a p_paddr that disagrees | |
7249 | with the section lma, flag the p_paddr as | |
7250 | invalid. */ | |
7251 | if ((section->flags & SEC_LOAD) != 0) | |
7252 | seg_off = this_hdr->sh_offset - segment->p_offset; | |
7253 | else | |
7254 | seg_off = this_hdr->sh_addr - segment->p_vaddr; | |
7255 | if (section->lma - segment->p_paddr != seg_off) | |
7256 | map->p_paddr_valid = FALSE; | |
7257 | } | |
53020534 L |
7258 | if (isec == section_count) |
7259 | break; | |
7260 | } | |
84d1d650 L |
7261 | } |
7262 | } | |
7263 | ||
a76e6f2f AM |
7264 | if (map->includes_filehdr && lowest_section != NULL) |
7265 | /* We need to keep the space used by the headers fixed. */ | |
7266 | map->header_size = lowest_section->vma - segment->p_vaddr; | |
d324f6d6 | 7267 | |
a76e6f2f AM |
7268 | if (!map->includes_phdrs |
7269 | && !map->includes_filehdr | |
7270 | && map->p_paddr_valid) | |
7271 | /* There is some other padding before the first section. */ | |
7272 | map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0) | |
7273 | - segment->p_paddr); | |
7274 | ||
84d1d650 L |
7275 | map->count = section_count; |
7276 | *pointer_to_map = map; | |
7277 | pointer_to_map = &map->next; | |
7278 | } | |
7279 | ||
12bd6957 | 7280 | elf_seg_map (obfd) = map_first; |
84d1d650 L |
7281 | return TRUE; |
7282 | } | |
7283 | ||
7284 | /* Copy private BFD data. This copies or rewrites ELF program header | |
7285 | information. */ | |
7286 | ||
7287 | static bfd_boolean | |
7288 | copy_private_bfd_data (bfd *ibfd, bfd *obfd) | |
7289 | { | |
84d1d650 L |
7290 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
7291 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7292 | return TRUE; | |
7293 | ||
7294 | if (elf_tdata (ibfd)->phdr == NULL) | |
7295 | return TRUE; | |
7296 | ||
7297 | if (ibfd->xvec == obfd->xvec) | |
7298 | { | |
cb3ff1e5 NC |
7299 | /* Check to see if any sections in the input BFD |
7300 | covered by ELF program header have changed. */ | |
d55ce4e2 | 7301 | Elf_Internal_Phdr *segment; |
84d1d650 L |
7302 | asection *section, *osec; |
7303 | unsigned int i, num_segments; | |
7304 | Elf_Internal_Shdr *this_hdr; | |
147d51c2 L |
7305 | const struct elf_backend_data *bed; |
7306 | ||
7307 | bed = get_elf_backend_data (ibfd); | |
7308 | ||
7309 | /* Regenerate the segment map if p_paddr is set to 0. */ | |
7310 | if (bed->want_p_paddr_set_to_zero) | |
7311 | goto rewrite; | |
84d1d650 L |
7312 | |
7313 | /* Initialize the segment mark field. */ | |
7314 | for (section = obfd->sections; section != NULL; | |
7315 | section = section->next) | |
7316 | section->segment_mark = FALSE; | |
7317 | ||
7318 | num_segments = elf_elfheader (ibfd)->e_phnum; | |
7319 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7320 | i < num_segments; | |
7321 | i++, segment++) | |
7322 | { | |
5f6999aa NC |
7323 | /* PR binutils/3535. The Solaris linker always sets the p_paddr |
7324 | and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 | |
7325 | which severly confuses things, so always regenerate the segment | |
7326 | map in this case. */ | |
7327 | if (segment->p_paddr == 0 | |
7328 | && segment->p_memsz == 0 | |
7329 | && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) | |
cb3ff1e5 | 7330 | goto rewrite; |
5f6999aa | 7331 | |
84d1d650 L |
7332 | for (section = ibfd->sections; |
7333 | section != NULL; section = section->next) | |
7334 | { | |
7335 | /* We mark the output section so that we know it comes | |
7336 | from the input BFD. */ | |
7337 | osec = section->output_section; | |
7338 | if (osec) | |
7339 | osec->segment_mark = TRUE; | |
7340 | ||
7341 | /* Check if this section is covered by the segment. */ | |
7342 | this_hdr = &(elf_section_data(section)->this_hdr); | |
f4638467 | 7343 | if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) |
84d1d650 L |
7344 | { |
7345 | /* FIXME: Check if its output section is changed or | |
7346 | removed. What else do we need to check? */ | |
7347 | if (osec == NULL | |
7348 | || section->flags != osec->flags | |
7349 | || section->lma != osec->lma | |
7350 | || section->vma != osec->vma | |
7351 | || section->size != osec->size | |
7352 | || section->rawsize != osec->rawsize | |
7353 | || section->alignment_power != osec->alignment_power) | |
7354 | goto rewrite; | |
7355 | } | |
7356 | } | |
7357 | } | |
7358 | ||
cb3ff1e5 | 7359 | /* Check to see if any output section do not come from the |
84d1d650 L |
7360 | input BFD. */ |
7361 | for (section = obfd->sections; section != NULL; | |
7362 | section = section->next) | |
7363 | { | |
535b785f | 7364 | if (!section->segment_mark) |
84d1d650 L |
7365 | goto rewrite; |
7366 | else | |
7367 | section->segment_mark = FALSE; | |
7368 | } | |
7369 | ||
7370 | return copy_elf_program_header (ibfd, obfd); | |
7371 | } | |
7372 | ||
7373 | rewrite: | |
f1d85785 L |
7374 | if (ibfd->xvec == obfd->xvec) |
7375 | { | |
7376 | /* When rewriting program header, set the output maxpagesize to | |
7377 | the maximum alignment of input PT_LOAD segments. */ | |
7378 | Elf_Internal_Phdr *segment; | |
7379 | unsigned int i; | |
7380 | unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; | |
7381 | bfd_vma maxpagesize = 0; | |
7382 | ||
7383 | for (i = 0, segment = elf_tdata (ibfd)->phdr; | |
7384 | i < num_segments; | |
7385 | i++, segment++) | |
7386 | if (segment->p_type == PT_LOAD | |
7387 | && maxpagesize < segment->p_align) | |
c86934ce NC |
7388 | { |
7389 | /* PR 17512: file: f17299af. */ | |
7390 | if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) | |
695344c0 | 7391 | /* xgettext:c-format */ |
76cfced5 AM |
7392 | _bfd_error_handler (_("%B: warning: segment alignment of %#Lx" |
7393 | " is too large"), | |
7394 | ibfd, segment->p_align); | |
c86934ce NC |
7395 | else |
7396 | maxpagesize = segment->p_align; | |
7397 | } | |
f1d85785 L |
7398 | |
7399 | if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) | |
7400 | bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); | |
7401 | } | |
7402 | ||
84d1d650 L |
7403 | return rewrite_elf_program_header (ibfd, obfd); |
7404 | } | |
7405 | ||
ccd2ec6a L |
7406 | /* Initialize private output section information from input section. */ |
7407 | ||
7408 | bfd_boolean | |
7409 | _bfd_elf_init_private_section_data (bfd *ibfd, | |
7410 | asection *isec, | |
7411 | bfd *obfd, | |
7412 | asection *osec, | |
7413 | struct bfd_link_info *link_info) | |
7414 | ||
7415 | { | |
7416 | Elf_Internal_Shdr *ihdr, *ohdr; | |
0e1862bb L |
7417 | bfd_boolean final_link = (link_info != NULL |
7418 | && !bfd_link_relocatable (link_info)); | |
ccd2ec6a L |
7419 | |
7420 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7421 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
7422 | return TRUE; | |
7423 | ||
ba85c43e NC |
7424 | BFD_ASSERT (elf_section_data (osec) != NULL); |
7425 | ||
dfa7b0b8 AM |
7426 | /* For objcopy and relocatable link, don't copy the output ELF |
7427 | section type from input if the output BFD section flags have been | |
7428 | set to something different. For a final link allow some flags | |
7429 | that the linker clears to differ. */ | |
42bb2e33 | 7430 | if (elf_section_type (osec) == SHT_NULL |
dfa7b0b8 AM |
7431 | && (osec->flags == isec->flags |
7432 | || (final_link | |
7433 | && ((osec->flags ^ isec->flags) | |
0814be7d | 7434 | & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) |
42bb2e33 | 7435 | elf_section_type (osec) = elf_section_type (isec); |
d270463e L |
7436 | |
7437 | /* FIXME: Is this correct for all OS/PROC specific flags? */ | |
7438 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7439 | & (SHF_MASKOS | SHF_MASKPROC)); | |
ccd2ec6a | 7440 | |
a91e1603 L |
7441 | /* Copy sh_info from input for mbind section. */ |
7442 | if (elf_section_flags (isec) & SHF_GNU_MBIND) | |
7443 | elf_section_data (osec)->this_hdr.sh_info | |
7444 | = elf_section_data (isec)->this_hdr.sh_info; | |
7445 | ||
ccd2ec6a L |
7446 | /* Set things up for objcopy and relocatable link. The output |
7447 | SHT_GROUP section will have its elf_next_in_group pointing back | |
7448 | to the input group members. Ignore linker created group section. | |
7449 | See elfNN_ia64_object_p in elfxx-ia64.c. */ | |
7bdf4127 AB |
7450 | if ((link_info == NULL |
7451 | || !link_info->resolve_section_groups) | |
7452 | && (elf_sec_group (isec) == NULL | |
7453 | || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) | |
ccd2ec6a | 7454 | { |
7bdf4127 AB |
7455 | if (elf_section_flags (isec) & SHF_GROUP) |
7456 | elf_section_flags (osec) |= SHF_GROUP; | |
7457 | elf_next_in_group (osec) = elf_next_in_group (isec); | |
7458 | elf_section_data (osec)->group = elf_section_data (isec)->group; | |
ccd2ec6a L |
7459 | } |
7460 | ||
7bdf4127 AB |
7461 | /* If not decompress, preserve SHF_COMPRESSED. */ |
7462 | if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) | |
7463 | elf_section_flags (osec) |= (elf_section_flags (isec) | |
7464 | & SHF_COMPRESSED); | |
7465 | ||
ccd2ec6a L |
7466 | ihdr = &elf_section_data (isec)->this_hdr; |
7467 | ||
7468 | /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We | |
7469 | don't use the output section of the linked-to section since it | |
7470 | may be NULL at this point. */ | |
7471 | if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) | |
7472 | { | |
7473 | ohdr = &elf_section_data (osec)->this_hdr; | |
7474 | ohdr->sh_flags |= SHF_LINK_ORDER; | |
7475 | elf_linked_to_section (osec) = elf_linked_to_section (isec); | |
7476 | } | |
7477 | ||
7478 | osec->use_rela_p = isec->use_rela_p; | |
7479 | ||
7480 | return TRUE; | |
7481 | } | |
7482 | ||
252b5132 RH |
7483 | /* Copy private section information. This copies over the entsize |
7484 | field, and sometimes the info field. */ | |
7485 | ||
b34976b6 | 7486 | bfd_boolean |
217aa764 AM |
7487 | _bfd_elf_copy_private_section_data (bfd *ibfd, |
7488 | asection *isec, | |
7489 | bfd *obfd, | |
7490 | asection *osec) | |
252b5132 RH |
7491 | { |
7492 | Elf_Internal_Shdr *ihdr, *ohdr; | |
7493 | ||
7494 | if (ibfd->xvec->flavour != bfd_target_elf_flavour | |
7495 | || obfd->xvec->flavour != bfd_target_elf_flavour) | |
b34976b6 | 7496 | return TRUE; |
252b5132 | 7497 | |
252b5132 RH |
7498 | ihdr = &elf_section_data (isec)->this_hdr; |
7499 | ohdr = &elf_section_data (osec)->this_hdr; | |
7500 | ||
7501 | ohdr->sh_entsize = ihdr->sh_entsize; | |
7502 | ||
7503 | if (ihdr->sh_type == SHT_SYMTAB | |
7504 | || ihdr->sh_type == SHT_DYNSYM | |
7505 | || ihdr->sh_type == SHT_GNU_verneed | |
7506 | || ihdr->sh_type == SHT_GNU_verdef) | |
7507 | ohdr->sh_info = ihdr->sh_info; | |
7508 | ||
ccd2ec6a L |
7509 | return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, |
7510 | NULL); | |
252b5132 RH |
7511 | } |
7512 | ||
d0bf826b AM |
7513 | /* Look at all the SHT_GROUP sections in IBFD, making any adjustments |
7514 | necessary if we are removing either the SHT_GROUP section or any of | |
7515 | the group member sections. DISCARDED is the value that a section's | |
7516 | output_section has if the section will be discarded, NULL when this | |
7517 | function is called from objcopy, bfd_abs_section_ptr when called | |
7518 | from the linker. */ | |
80fccad2 BW |
7519 | |
7520 | bfd_boolean | |
d0bf826b | 7521 | _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) |
80fccad2 | 7522 | { |
30288845 AM |
7523 | asection *isec; |
7524 | ||
30288845 | 7525 | for (isec = ibfd->sections; isec != NULL; isec = isec->next) |
415f38a6 | 7526 | if (elf_section_type (isec) == SHT_GROUP) |
30288845 AM |
7527 | { |
7528 | asection *first = elf_next_in_group (isec); | |
7529 | asection *s = first; | |
d0bf826b AM |
7530 | bfd_size_type removed = 0; |
7531 | ||
30288845 AM |
7532 | while (s != NULL) |
7533 | { | |
415f38a6 AM |
7534 | /* If this member section is being output but the |
7535 | SHT_GROUP section is not, then clear the group info | |
7536 | set up by _bfd_elf_copy_private_section_data. */ | |
d0bf826b AM |
7537 | if (s->output_section != discarded |
7538 | && isec->output_section == discarded) | |
30288845 AM |
7539 | { |
7540 | elf_section_flags (s->output_section) &= ~SHF_GROUP; | |
7541 | elf_group_name (s->output_section) = NULL; | |
7542 | } | |
415f38a6 AM |
7543 | /* Conversely, if the member section is not being output |
7544 | but the SHT_GROUP section is, then adjust its size. */ | |
d0bf826b AM |
7545 | else if (s->output_section == discarded |
7546 | && isec->output_section != discarded) | |
7547 | removed += 4; | |
30288845 AM |
7548 | s = elf_next_in_group (s); |
7549 | if (s == first) | |
7550 | break; | |
7551 | } | |
d0bf826b AM |
7552 | if (removed != 0) |
7553 | { | |
7554 | if (discarded != NULL) | |
7555 | { | |
7556 | /* If we've been called for ld -r, then we need to | |
7557 | adjust the input section size. This function may | |
7558 | be called multiple times, so save the original | |
7559 | size. */ | |
7560 | if (isec->rawsize == 0) | |
7561 | isec->rawsize = isec->size; | |
7562 | isec->size = isec->rawsize - removed; | |
7563 | } | |
7564 | else | |
7565 | { | |
7566 | /* Adjust the output section size when called from | |
7567 | objcopy. */ | |
7568 | isec->output_section->size -= removed; | |
7569 | } | |
7570 | } | |
30288845 AM |
7571 | } |
7572 | ||
80fccad2 BW |
7573 | return TRUE; |
7574 | } | |
7575 | ||
d0bf826b AM |
7576 | /* Copy private header information. */ |
7577 | ||
7578 | bfd_boolean | |
7579 | _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) | |
7580 | { | |
7581 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7582 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
7583 | return TRUE; | |
7584 | ||
7585 | /* Copy over private BFD data if it has not already been copied. | |
7586 | This must be done here, rather than in the copy_private_bfd_data | |
7587 | entry point, because the latter is called after the section | |
7588 | contents have been set, which means that the program headers have | |
7589 | already been worked out. */ | |
12bd6957 | 7590 | if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) |
d0bf826b AM |
7591 | { |
7592 | if (! copy_private_bfd_data (ibfd, obfd)) | |
7593 | return FALSE; | |
7594 | } | |
7595 | ||
7596 | return _bfd_elf_fixup_group_sections (ibfd, NULL); | |
7597 | } | |
7598 | ||
252b5132 RH |
7599 | /* Copy private symbol information. If this symbol is in a section |
7600 | which we did not map into a BFD section, try to map the section | |
7601 | index correctly. We use special macro definitions for the mapped | |
7602 | section indices; these definitions are interpreted by the | |
7603 | swap_out_syms function. */ | |
7604 | ||
9ad5cbcf AM |
7605 | #define MAP_ONESYMTAB (SHN_HIOS + 1) |
7606 | #define MAP_DYNSYMTAB (SHN_HIOS + 2) | |
7607 | #define MAP_STRTAB (SHN_HIOS + 3) | |
7608 | #define MAP_SHSTRTAB (SHN_HIOS + 4) | |
7609 | #define MAP_SYM_SHNDX (SHN_HIOS + 5) | |
252b5132 | 7610 | |
b34976b6 | 7611 | bfd_boolean |
217aa764 AM |
7612 | _bfd_elf_copy_private_symbol_data (bfd *ibfd, |
7613 | asymbol *isymarg, | |
7614 | bfd *obfd, | |
7615 | asymbol *osymarg) | |
252b5132 RH |
7616 | { |
7617 | elf_symbol_type *isym, *osym; | |
7618 | ||
7619 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
7620 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 7621 | return TRUE; |
252b5132 RH |
7622 | |
7623 | isym = elf_symbol_from (ibfd, isymarg); | |
7624 | osym = elf_symbol_from (obfd, osymarg); | |
7625 | ||
7626 | if (isym != NULL | |
8424d8f5 | 7627 | && isym->internal_elf_sym.st_shndx != 0 |
252b5132 RH |
7628 | && osym != NULL |
7629 | && bfd_is_abs_section (isym->symbol.section)) | |
7630 | { | |
7631 | unsigned int shndx; | |
7632 | ||
7633 | shndx = isym->internal_elf_sym.st_shndx; | |
7634 | if (shndx == elf_onesymtab (ibfd)) | |
7635 | shndx = MAP_ONESYMTAB; | |
7636 | else if (shndx == elf_dynsymtab (ibfd)) | |
7637 | shndx = MAP_DYNSYMTAB; | |
12bd6957 | 7638 | else if (shndx == elf_strtab_sec (ibfd)) |
252b5132 | 7639 | shndx = MAP_STRTAB; |
12bd6957 | 7640 | else if (shndx == elf_shstrtab_sec (ibfd)) |
252b5132 | 7641 | shndx = MAP_SHSTRTAB; |
6a40cf0c | 7642 | else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) |
9ad5cbcf | 7643 | shndx = MAP_SYM_SHNDX; |
252b5132 RH |
7644 | osym->internal_elf_sym.st_shndx = shndx; |
7645 | } | |
7646 | ||
b34976b6 | 7647 | return TRUE; |
252b5132 RH |
7648 | } |
7649 | ||
7650 | /* Swap out the symbols. */ | |
7651 | ||
b34976b6 | 7652 | static bfd_boolean |
217aa764 | 7653 | swap_out_syms (bfd *abfd, |
ef10c3ac | 7654 | struct elf_strtab_hash **sttp, |
217aa764 | 7655 | int relocatable_p) |
252b5132 | 7656 | { |
9c5bfbb7 | 7657 | const struct elf_backend_data *bed; |
079e9a2f AM |
7658 | int symcount; |
7659 | asymbol **syms; | |
ef10c3ac | 7660 | struct elf_strtab_hash *stt; |
079e9a2f | 7661 | Elf_Internal_Shdr *symtab_hdr; |
9ad5cbcf | 7662 | Elf_Internal_Shdr *symtab_shndx_hdr; |
079e9a2f | 7663 | Elf_Internal_Shdr *symstrtab_hdr; |
ef10c3ac | 7664 | struct elf_sym_strtab *symstrtab; |
f075ee0c AM |
7665 | bfd_byte *outbound_syms; |
7666 | bfd_byte *outbound_shndx; | |
ef10c3ac L |
7667 | unsigned long outbound_syms_index; |
7668 | unsigned long outbound_shndx_index; | |
079e9a2f | 7669 | int idx; |
12bd6957 | 7670 | unsigned int num_locals; |
079e9a2f | 7671 | bfd_size_type amt; |
174fd7f9 | 7672 | bfd_boolean name_local_sections; |
252b5132 | 7673 | |
12bd6957 | 7674 | if (!elf_map_symbols (abfd, &num_locals)) |
b34976b6 | 7675 | return FALSE; |
252b5132 | 7676 | |
c044fabd | 7677 | /* Dump out the symtabs. */ |
ef10c3ac | 7678 | stt = _bfd_elf_strtab_init (); |
079e9a2f | 7679 | if (stt == NULL) |
b34976b6 | 7680 | return FALSE; |
252b5132 | 7681 | |
079e9a2f AM |
7682 | bed = get_elf_backend_data (abfd); |
7683 | symcount = bfd_get_symcount (abfd); | |
7684 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
7685 | symtab_hdr->sh_type = SHT_SYMTAB; | |
7686 | symtab_hdr->sh_entsize = bed->s->sizeof_sym; | |
7687 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
12bd6957 | 7688 | symtab_hdr->sh_info = num_locals + 1; |
72de5009 | 7689 | symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; |
079e9a2f AM |
7690 | |
7691 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
7692 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
7693 | ||
ef10c3ac L |
7694 | /* Allocate buffer to swap out the .strtab section. */ |
7695 | symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1) | |
7696 | * sizeof (*symstrtab)); | |
7697 | if (symstrtab == NULL) | |
7698 | { | |
7699 | _bfd_elf_strtab_free (stt); | |
7700 | return FALSE; | |
7701 | } | |
7702 | ||
a50b1753 NC |
7703 | outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount, |
7704 | bed->s->sizeof_sym); | |
079e9a2f | 7705 | if (outbound_syms == NULL) |
5ed6aba4 | 7706 | { |
ef10c3ac L |
7707 | error_return: |
7708 | _bfd_elf_strtab_free (stt); | |
7709 | free (symstrtab); | |
5ed6aba4 NC |
7710 | return FALSE; |
7711 | } | |
217aa764 | 7712 | symtab_hdr->contents = outbound_syms; |
ef10c3ac | 7713 | outbound_syms_index = 0; |
252b5132 | 7714 | |
9ad5cbcf | 7715 | outbound_shndx = NULL; |
ef10c3ac | 7716 | outbound_shndx_index = 0; |
6a40cf0c NC |
7717 | |
7718 | if (elf_symtab_shndx_list (abfd)) | |
9ad5cbcf | 7719 | { |
6a40cf0c NC |
7720 | symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; |
7721 | if (symtab_shndx_hdr->sh_name != 0) | |
7722 | { | |
7723 | amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); | |
7724 | outbound_shndx = (bfd_byte *) | |
7725 | bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx)); | |
7726 | if (outbound_shndx == NULL) | |
7727 | goto error_return; | |
5ed6aba4 | 7728 | |
6a40cf0c NC |
7729 | symtab_shndx_hdr->contents = outbound_shndx; |
7730 | symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; | |
7731 | symtab_shndx_hdr->sh_size = amt; | |
7732 | symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); | |
7733 | symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); | |
7734 | } | |
7735 | /* FIXME: What about any other headers in the list ? */ | |
9ad5cbcf AM |
7736 | } |
7737 | ||
589e6347 | 7738 | /* Now generate the data (for "contents"). */ |
079e9a2f AM |
7739 | { |
7740 | /* Fill in zeroth symbol and swap it out. */ | |
7741 | Elf_Internal_Sym sym; | |
7742 | sym.st_name = 0; | |
7743 | sym.st_value = 0; | |
7744 | sym.st_size = 0; | |
7745 | sym.st_info = 0; | |
7746 | sym.st_other = 0; | |
7747 | sym.st_shndx = SHN_UNDEF; | |
35fc36a8 | 7748 | sym.st_target_internal = 0; |
ef10c3ac L |
7749 | symstrtab[0].sym = sym; |
7750 | symstrtab[0].dest_index = outbound_syms_index; | |
7751 | symstrtab[0].destshndx_index = outbound_shndx_index; | |
7752 | outbound_syms_index++; | |
9ad5cbcf | 7753 | if (outbound_shndx != NULL) |
ef10c3ac | 7754 | outbound_shndx_index++; |
079e9a2f | 7755 | } |
252b5132 | 7756 | |
174fd7f9 RS |
7757 | name_local_sections |
7758 | = (bed->elf_backend_name_local_section_symbols | |
7759 | && bed->elf_backend_name_local_section_symbols (abfd)); | |
7760 | ||
079e9a2f | 7761 | syms = bfd_get_outsymbols (abfd); |
ef10c3ac | 7762 | for (idx = 0; idx < symcount;) |
252b5132 | 7763 | { |
252b5132 | 7764 | Elf_Internal_Sym sym; |
079e9a2f AM |
7765 | bfd_vma value = syms[idx]->value; |
7766 | elf_symbol_type *type_ptr; | |
7767 | flagword flags = syms[idx]->flags; | |
7768 | int type; | |
252b5132 | 7769 | |
174fd7f9 RS |
7770 | if (!name_local_sections |
7771 | && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) | |
079e9a2f AM |
7772 | { |
7773 | /* Local section symbols have no name. */ | |
ef10c3ac | 7774 | sym.st_name = (unsigned long) -1; |
079e9a2f AM |
7775 | } |
7776 | else | |
7777 | { | |
ef10c3ac L |
7778 | /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize |
7779 | to get the final offset for st_name. */ | |
7780 | sym.st_name | |
7781 | = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, | |
7782 | FALSE); | |
079e9a2f | 7783 | if (sym.st_name == (unsigned long) -1) |
ef10c3ac | 7784 | goto error_return; |
079e9a2f | 7785 | } |
252b5132 | 7786 | |
079e9a2f | 7787 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
252b5132 | 7788 | |
079e9a2f AM |
7789 | if ((flags & BSF_SECTION_SYM) == 0 |
7790 | && bfd_is_com_section (syms[idx]->section)) | |
7791 | { | |
7792 | /* ELF common symbols put the alignment into the `value' field, | |
7793 | and the size into the `size' field. This is backwards from | |
7794 | how BFD handles it, so reverse it here. */ | |
7795 | sym.st_size = value; | |
7796 | if (type_ptr == NULL | |
7797 | || type_ptr->internal_elf_sym.st_value == 0) | |
7798 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
7799 | else | |
7800 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
7801 | sym.st_shndx = _bfd_elf_section_from_bfd_section | |
7802 | (abfd, syms[idx]->section); | |
7803 | } | |
7804 | else | |
7805 | { | |
7806 | asection *sec = syms[idx]->section; | |
cb33740c | 7807 | unsigned int shndx; |
252b5132 | 7808 | |
079e9a2f AM |
7809 | if (sec->output_section) |
7810 | { | |
7811 | value += sec->output_offset; | |
7812 | sec = sec->output_section; | |
7813 | } | |
589e6347 | 7814 | |
079e9a2f AM |
7815 | /* Don't add in the section vma for relocatable output. */ |
7816 | if (! relocatable_p) | |
7817 | value += sec->vma; | |
7818 | sym.st_value = value; | |
7819 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
7820 | ||
7821 | if (bfd_is_abs_section (sec) | |
7822 | && type_ptr != NULL | |
7823 | && type_ptr->internal_elf_sym.st_shndx != 0) | |
7824 | { | |
7825 | /* This symbol is in a real ELF section which we did | |
7826 | not create as a BFD section. Undo the mapping done | |
7827 | by copy_private_symbol_data. */ | |
7828 | shndx = type_ptr->internal_elf_sym.st_shndx; | |
7829 | switch (shndx) | |
7830 | { | |
7831 | case MAP_ONESYMTAB: | |
7832 | shndx = elf_onesymtab (abfd); | |
7833 | break; | |
7834 | case MAP_DYNSYMTAB: | |
7835 | shndx = elf_dynsymtab (abfd); | |
7836 | break; | |
7837 | case MAP_STRTAB: | |
12bd6957 | 7838 | shndx = elf_strtab_sec (abfd); |
079e9a2f AM |
7839 | break; |
7840 | case MAP_SHSTRTAB: | |
12bd6957 | 7841 | shndx = elf_shstrtab_sec (abfd); |
079e9a2f | 7842 | break; |
9ad5cbcf | 7843 | case MAP_SYM_SHNDX: |
6a40cf0c NC |
7844 | if (elf_symtab_shndx_list (abfd)) |
7845 | shndx = elf_symtab_shndx_list (abfd)->ndx; | |
9ad5cbcf | 7846 | break; |
079e9a2f | 7847 | default: |
15bc576a | 7848 | shndx = SHN_ABS; |
079e9a2f AM |
7849 | break; |
7850 | } | |
7851 | } | |
7852 | else | |
7853 | { | |
7854 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
252b5132 | 7855 | |
cb33740c | 7856 | if (shndx == SHN_BAD) |
079e9a2f AM |
7857 | { |
7858 | asection *sec2; | |
7859 | ||
7860 | /* Writing this would be a hell of a lot easier if | |
7861 | we had some decent documentation on bfd, and | |
7862 | knew what to expect of the library, and what to | |
7863 | demand of applications. For example, it | |
7864 | appears that `objcopy' might not set the | |
7865 | section of a symbol to be a section that is | |
7866 | actually in the output file. */ | |
7867 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
5df1bc57 AM |
7868 | if (sec2 != NULL) |
7869 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); | |
7870 | if (shndx == SHN_BAD) | |
589e6347 | 7871 | { |
695344c0 | 7872 | /* xgettext:c-format */ |
589e6347 NC |
7873 | _bfd_error_handler (_("\ |
7874 | Unable to find equivalent output section for symbol '%s' from section '%s'"), | |
7875 | syms[idx]->name ? syms[idx]->name : "<Local sym>", | |
7876 | sec->name); | |
811072d8 | 7877 | bfd_set_error (bfd_error_invalid_operation); |
ef10c3ac | 7878 | goto error_return; |
589e6347 | 7879 | } |
079e9a2f AM |
7880 | } |
7881 | } | |
252b5132 | 7882 | |
079e9a2f AM |
7883 | sym.st_shndx = shndx; |
7884 | } | |
252b5132 | 7885 | |
13ae64f3 JJ |
7886 | if ((flags & BSF_THREAD_LOCAL) != 0) |
7887 | type = STT_TLS; | |
d8045f23 NC |
7888 | else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) |
7889 | type = STT_GNU_IFUNC; | |
13ae64f3 | 7890 | else if ((flags & BSF_FUNCTION) != 0) |
079e9a2f AM |
7891 | type = STT_FUNC; |
7892 | else if ((flags & BSF_OBJECT) != 0) | |
7893 | type = STT_OBJECT; | |
d9352518 DB |
7894 | else if ((flags & BSF_RELC) != 0) |
7895 | type = STT_RELC; | |
7896 | else if ((flags & BSF_SRELC) != 0) | |
7897 | type = STT_SRELC; | |
079e9a2f AM |
7898 | else |
7899 | type = STT_NOTYPE; | |
252b5132 | 7900 | |
13ae64f3 JJ |
7901 | if (syms[idx]->section->flags & SEC_THREAD_LOCAL) |
7902 | type = STT_TLS; | |
7903 | ||
589e6347 | 7904 | /* Processor-specific types. */ |
079e9a2f AM |
7905 | if (type_ptr != NULL |
7906 | && bed->elf_backend_get_symbol_type) | |
7907 | type = ((*bed->elf_backend_get_symbol_type) | |
7908 | (&type_ptr->internal_elf_sym, type)); | |
252b5132 | 7909 | |
079e9a2f AM |
7910 | if (flags & BSF_SECTION_SYM) |
7911 | { | |
7912 | if (flags & BSF_GLOBAL) | |
7913 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); | |
7914 | else | |
7915 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
7916 | } | |
7917 | else if (bfd_is_com_section (syms[idx]->section)) | |
0a40daed | 7918 | { |
b8871f35 L |
7919 | if (type != STT_TLS) |
7920 | { | |
7921 | if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) | |
7922 | type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) | |
7923 | ? STT_COMMON : STT_OBJECT); | |
7924 | else | |
7925 | type = ((flags & BSF_ELF_COMMON) != 0 | |
7926 | ? STT_COMMON : STT_OBJECT); | |
7927 | } | |
7928 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); | |
0a40daed | 7929 | } |
079e9a2f AM |
7930 | else if (bfd_is_und_section (syms[idx]->section)) |
7931 | sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) | |
7932 | ? STB_WEAK | |
7933 | : STB_GLOBAL), | |
7934 | type); | |
7935 | else if (flags & BSF_FILE) | |
7936 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
7937 | else | |
7938 | { | |
7939 | int bind = STB_LOCAL; | |
252b5132 | 7940 | |
079e9a2f AM |
7941 | if (flags & BSF_LOCAL) |
7942 | bind = STB_LOCAL; | |
3e7a7d11 NC |
7943 | else if (flags & BSF_GNU_UNIQUE) |
7944 | bind = STB_GNU_UNIQUE; | |
079e9a2f AM |
7945 | else if (flags & BSF_WEAK) |
7946 | bind = STB_WEAK; | |
7947 | else if (flags & BSF_GLOBAL) | |
7948 | bind = STB_GLOBAL; | |
252b5132 | 7949 | |
079e9a2f AM |
7950 | sym.st_info = ELF_ST_INFO (bind, type); |
7951 | } | |
252b5132 | 7952 | |
079e9a2f | 7953 | if (type_ptr != NULL) |
35fc36a8 RS |
7954 | { |
7955 | sym.st_other = type_ptr->internal_elf_sym.st_other; | |
7956 | sym.st_target_internal | |
7957 | = type_ptr->internal_elf_sym.st_target_internal; | |
7958 | } | |
079e9a2f | 7959 | else |
35fc36a8 RS |
7960 | { |
7961 | sym.st_other = 0; | |
7962 | sym.st_target_internal = 0; | |
7963 | } | |
252b5132 | 7964 | |
ef10c3ac L |
7965 | idx++; |
7966 | symstrtab[idx].sym = sym; | |
7967 | symstrtab[idx].dest_index = outbound_syms_index; | |
7968 | symstrtab[idx].destshndx_index = outbound_shndx_index; | |
7969 | ||
7970 | outbound_syms_index++; | |
9ad5cbcf | 7971 | if (outbound_shndx != NULL) |
ef10c3ac L |
7972 | outbound_shndx_index++; |
7973 | } | |
7974 | ||
7975 | /* Finalize the .strtab section. */ | |
7976 | _bfd_elf_strtab_finalize (stt); | |
7977 | ||
7978 | /* Swap out the .strtab section. */ | |
7979 | for (idx = 0; idx <= symcount; idx++) | |
7980 | { | |
7981 | struct elf_sym_strtab *elfsym = &symstrtab[idx]; | |
7982 | if (elfsym->sym.st_name == (unsigned long) -1) | |
7983 | elfsym->sym.st_name = 0; | |
7984 | else | |
7985 | elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, | |
7986 | elfsym->sym.st_name); | |
7987 | bed->s->swap_symbol_out (abfd, &elfsym->sym, | |
7988 | (outbound_syms | |
7989 | + (elfsym->dest_index | |
7990 | * bed->s->sizeof_sym)), | |
7991 | (outbound_shndx | |
7992 | + (elfsym->destshndx_index | |
7993 | * sizeof (Elf_External_Sym_Shndx)))); | |
079e9a2f | 7994 | } |
ef10c3ac | 7995 | free (symstrtab); |
252b5132 | 7996 | |
079e9a2f | 7997 | *sttp = stt; |
ef10c3ac | 7998 | symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); |
079e9a2f | 7999 | symstrtab_hdr->sh_type = SHT_STRTAB; |
84865015 | 8000 | symstrtab_hdr->sh_flags = bed->elf_strtab_flags; |
079e9a2f AM |
8001 | symstrtab_hdr->sh_addr = 0; |
8002 | symstrtab_hdr->sh_entsize = 0; | |
8003 | symstrtab_hdr->sh_link = 0; | |
8004 | symstrtab_hdr->sh_info = 0; | |
8005 | symstrtab_hdr->sh_addralign = 1; | |
252b5132 | 8006 | |
b34976b6 | 8007 | return TRUE; |
252b5132 RH |
8008 | } |
8009 | ||
8010 | /* Return the number of bytes required to hold the symtab vector. | |
8011 | ||
8012 | Note that we base it on the count plus 1, since we will null terminate | |
8013 | the vector allocated based on this size. However, the ELF symbol table | |
8014 | always has a dummy entry as symbol #0, so it ends up even. */ | |
8015 | ||
8016 | long | |
217aa764 | 8017 | _bfd_elf_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8018 | { |
8019 | long symcount; | |
8020 | long symtab_size; | |
8021 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; | |
8022 | ||
8023 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8024 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8025 | if (symcount > 0) | |
8026 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8027 | |
8028 | return symtab_size; | |
8029 | } | |
8030 | ||
8031 | long | |
217aa764 | 8032 | _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
8033 | { |
8034 | long symcount; | |
8035 | long symtab_size; | |
8036 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
8037 | ||
8038 | if (elf_dynsymtab (abfd) == 0) | |
8039 | { | |
8040 | bfd_set_error (bfd_error_invalid_operation); | |
8041 | return -1; | |
8042 | } | |
8043 | ||
8044 | symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; | |
b99d1833 AM |
8045 | symtab_size = (symcount + 1) * (sizeof (asymbol *)); |
8046 | if (symcount > 0) | |
8047 | symtab_size -= sizeof (asymbol *); | |
252b5132 RH |
8048 | |
8049 | return symtab_size; | |
8050 | } | |
8051 | ||
8052 | long | |
217aa764 AM |
8053 | _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
8054 | sec_ptr asect) | |
252b5132 RH |
8055 | { |
8056 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
8057 | } | |
8058 | ||
8059 | /* Canonicalize the relocs. */ | |
8060 | ||
8061 | long | |
217aa764 AM |
8062 | _bfd_elf_canonicalize_reloc (bfd *abfd, |
8063 | sec_ptr section, | |
8064 | arelent **relptr, | |
8065 | asymbol **symbols) | |
252b5132 RH |
8066 | { |
8067 | arelent *tblptr; | |
8068 | unsigned int i; | |
9c5bfbb7 | 8069 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
252b5132 | 8070 | |
b34976b6 | 8071 | if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) |
252b5132 RH |
8072 | return -1; |
8073 | ||
8074 | tblptr = section->relocation; | |
8075 | for (i = 0; i < section->reloc_count; i++) | |
8076 | *relptr++ = tblptr++; | |
8077 | ||
8078 | *relptr = NULL; | |
8079 | ||
8080 | return section->reloc_count; | |
8081 | } | |
8082 | ||
8083 | long | |
6cee3f79 | 8084 | _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) |
252b5132 | 8085 | { |
9c5bfbb7 | 8086 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8087 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); |
252b5132 RH |
8088 | |
8089 | if (symcount >= 0) | |
8090 | bfd_get_symcount (abfd) = symcount; | |
8091 | return symcount; | |
8092 | } | |
8093 | ||
8094 | long | |
217aa764 AM |
8095 | _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, |
8096 | asymbol **allocation) | |
252b5132 | 8097 | { |
9c5bfbb7 | 8098 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
217aa764 | 8099 | long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); |
1f70368c DJ |
8100 | |
8101 | if (symcount >= 0) | |
8102 | bfd_get_dynamic_symcount (abfd) = symcount; | |
8103 | return symcount; | |
252b5132 RH |
8104 | } |
8105 | ||
8615f3f2 AM |
8106 | /* Return the size required for the dynamic reloc entries. Any loadable |
8107 | section that was actually installed in the BFD, and has type SHT_REL | |
8108 | or SHT_RELA, and uses the dynamic symbol table, is considered to be a | |
8109 | dynamic reloc section. */ | |
252b5132 RH |
8110 | |
8111 | long | |
217aa764 | 8112 | _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) |
252b5132 RH |
8113 | { |
8114 | long ret; | |
8115 | asection *s; | |
8116 | ||
8117 | if (elf_dynsymtab (abfd) == 0) | |
8118 | { | |
8119 | bfd_set_error (bfd_error_invalid_operation); | |
8120 | return -1; | |
8121 | } | |
8122 | ||
8123 | ret = sizeof (arelent *); | |
8124 | for (s = abfd->sections; s != NULL; s = s->next) | |
266b05cf | 8125 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8126 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8127 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
eea6121a | 8128 | ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) |
252b5132 RH |
8129 | * sizeof (arelent *)); |
8130 | ||
8131 | return ret; | |
8132 | } | |
8133 | ||
8615f3f2 AM |
8134 | /* Canonicalize the dynamic relocation entries. Note that we return the |
8135 | dynamic relocations as a single block, although they are actually | |
8136 | associated with particular sections; the interface, which was | |
8137 | designed for SunOS style shared libraries, expects that there is only | |
8138 | one set of dynamic relocs. Any loadable section that was actually | |
8139 | installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the | |
8140 | dynamic symbol table, is considered to be a dynamic reloc section. */ | |
252b5132 RH |
8141 | |
8142 | long | |
217aa764 AM |
8143 | _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, |
8144 | arelent **storage, | |
8145 | asymbol **syms) | |
252b5132 | 8146 | { |
217aa764 | 8147 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); |
252b5132 RH |
8148 | asection *s; |
8149 | long ret; | |
8150 | ||
8151 | if (elf_dynsymtab (abfd) == 0) | |
8152 | { | |
8153 | bfd_set_error (bfd_error_invalid_operation); | |
8154 | return -1; | |
8155 | } | |
8156 | ||
8157 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
8158 | ret = 0; | |
8159 | for (s = abfd->sections; s != NULL; s = s->next) | |
8160 | { | |
266b05cf | 8161 | if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) |
252b5132 RH |
8162 | && (elf_section_data (s)->this_hdr.sh_type == SHT_REL |
8163 | || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) | |
8164 | { | |
8165 | arelent *p; | |
8166 | long count, i; | |
8167 | ||
b34976b6 | 8168 | if (! (*slurp_relocs) (abfd, s, syms, TRUE)) |
252b5132 | 8169 | return -1; |
eea6121a | 8170 | count = s->size / elf_section_data (s)->this_hdr.sh_entsize; |
252b5132 RH |
8171 | p = s->relocation; |
8172 | for (i = 0; i < count; i++) | |
8173 | *storage++ = p++; | |
8174 | ret += count; | |
8175 | } | |
8176 | } | |
8177 | ||
8178 | *storage = NULL; | |
8179 | ||
8180 | return ret; | |
8181 | } | |
8182 | \f | |
8183 | /* Read in the version information. */ | |
8184 | ||
b34976b6 | 8185 | bfd_boolean |
fc0e6df6 | 8186 | _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) |
252b5132 RH |
8187 | { |
8188 | bfd_byte *contents = NULL; | |
fc0e6df6 PB |
8189 | unsigned int freeidx = 0; |
8190 | ||
8191 | if (elf_dynverref (abfd) != 0) | |
8192 | { | |
8193 | Elf_Internal_Shdr *hdr; | |
8194 | Elf_External_Verneed *everneed; | |
8195 | Elf_Internal_Verneed *iverneed; | |
8196 | unsigned int i; | |
d0fb9a8d | 8197 | bfd_byte *contents_end; |
fc0e6df6 PB |
8198 | |
8199 | hdr = &elf_tdata (abfd)->dynverref_hdr; | |
8200 | ||
bd61e135 AM |
8201 | if (hdr->sh_info == 0 |
8202 | || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) | |
d0fb9a8d | 8203 | { |
601a03ba | 8204 | error_return_bad_verref: |
4eca0228 | 8205 | _bfd_error_handler |
601a03ba AM |
8206 | (_("%B: .gnu.version_r invalid entry"), abfd); |
8207 | bfd_set_error (bfd_error_bad_value); | |
d0fb9a8d JJ |
8208 | error_return_verref: |
8209 | elf_tdata (abfd)->verref = NULL; | |
8210 | elf_tdata (abfd)->cverrefs = 0; | |
8211 | goto error_return; | |
8212 | } | |
601a03ba AM |
8213 | |
8214 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); | |
8215 | if (contents == NULL) | |
8216 | goto error_return_verref; | |
8217 | ||
fc0e6df6 PB |
8218 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
8219 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) | |
d0fb9a8d | 8220 | goto error_return_verref; |
fc0e6df6 | 8221 | |
601a03ba | 8222 | elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) |
bd61e135 | 8223 | bfd_alloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed)); |
601a03ba AM |
8224 | |
8225 | if (elf_tdata (abfd)->verref == NULL) | |
d0fb9a8d JJ |
8226 | goto error_return_verref; |
8227 | ||
8228 | BFD_ASSERT (sizeof (Elf_External_Verneed) | |
8229 | == sizeof (Elf_External_Vernaux)); | |
8230 | contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); | |
fc0e6df6 PB |
8231 | everneed = (Elf_External_Verneed *) contents; |
8232 | iverneed = elf_tdata (abfd)->verref; | |
8233 | for (i = 0; i < hdr->sh_info; i++, iverneed++) | |
8234 | { | |
8235 | Elf_External_Vernaux *evernaux; | |
8236 | Elf_Internal_Vernaux *ivernaux; | |
8237 | unsigned int j; | |
8238 | ||
8239 | _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); | |
8240 | ||
8241 | iverneed->vn_bfd = abfd; | |
8242 | ||
8243 | iverneed->vn_filename = | |
8244 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8245 | iverneed->vn_file); | |
8246 | if (iverneed->vn_filename == NULL) | |
601a03ba | 8247 | goto error_return_bad_verref; |
fc0e6df6 | 8248 | |
d0fb9a8d JJ |
8249 | if (iverneed->vn_cnt == 0) |
8250 | iverneed->vn_auxptr = NULL; | |
8251 | else | |
8252 | { | |
a50b1753 NC |
8253 | iverneed->vn_auxptr = (struct elf_internal_vernaux *) |
8254 | bfd_alloc2 (abfd, iverneed->vn_cnt, | |
8255 | sizeof (Elf_Internal_Vernaux)); | |
d0fb9a8d JJ |
8256 | if (iverneed->vn_auxptr == NULL) |
8257 | goto error_return_verref; | |
8258 | } | |
8259 | ||
8260 | if (iverneed->vn_aux | |
8261 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8262 | goto error_return_bad_verref; |
fc0e6df6 PB |
8263 | |
8264 | evernaux = ((Elf_External_Vernaux *) | |
8265 | ((bfd_byte *) everneed + iverneed->vn_aux)); | |
8266 | ivernaux = iverneed->vn_auxptr; | |
8267 | for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) | |
8268 | { | |
8269 | _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); | |
8270 | ||
8271 | ivernaux->vna_nodename = | |
8272 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8273 | ivernaux->vna_name); | |
8274 | if (ivernaux->vna_nodename == NULL) | |
601a03ba | 8275 | goto error_return_bad_verref; |
fc0e6df6 | 8276 | |
25ff461f AM |
8277 | if (ivernaux->vna_other > freeidx) |
8278 | freeidx = ivernaux->vna_other; | |
8279 | ||
8280 | ivernaux->vna_nextptr = NULL; | |
8281 | if (ivernaux->vna_next == 0) | |
8282 | { | |
8283 | iverneed->vn_cnt = j + 1; | |
8284 | break; | |
8285 | } | |
fc0e6df6 PB |
8286 | if (j + 1 < iverneed->vn_cnt) |
8287 | ivernaux->vna_nextptr = ivernaux + 1; | |
fc0e6df6 | 8288 | |
d0fb9a8d JJ |
8289 | if (ivernaux->vna_next |
8290 | > (size_t) (contents_end - (bfd_byte *) evernaux)) | |
601a03ba | 8291 | goto error_return_bad_verref; |
d0fb9a8d | 8292 | |
fc0e6df6 PB |
8293 | evernaux = ((Elf_External_Vernaux *) |
8294 | ((bfd_byte *) evernaux + ivernaux->vna_next)); | |
fc0e6df6 PB |
8295 | } |
8296 | ||
25ff461f AM |
8297 | iverneed->vn_nextref = NULL; |
8298 | if (iverneed->vn_next == 0) | |
8299 | break; | |
fc0e6df6 PB |
8300 | if (i + 1 < hdr->sh_info) |
8301 | iverneed->vn_nextref = iverneed + 1; | |
fc0e6df6 | 8302 | |
d0fb9a8d JJ |
8303 | if (iverneed->vn_next |
8304 | > (size_t) (contents_end - (bfd_byte *) everneed)) | |
601a03ba | 8305 | goto error_return_bad_verref; |
d0fb9a8d | 8306 | |
fc0e6df6 PB |
8307 | everneed = ((Elf_External_Verneed *) |
8308 | ((bfd_byte *) everneed + iverneed->vn_next)); | |
8309 | } | |
25ff461f | 8310 | elf_tdata (abfd)->cverrefs = i; |
fc0e6df6 PB |
8311 | |
8312 | free (contents); | |
8313 | contents = NULL; | |
8314 | } | |
252b5132 RH |
8315 | |
8316 | if (elf_dynverdef (abfd) != 0) | |
8317 | { | |
8318 | Elf_Internal_Shdr *hdr; | |
8319 | Elf_External_Verdef *everdef; | |
8320 | Elf_Internal_Verdef *iverdef; | |
f631889e UD |
8321 | Elf_Internal_Verdef *iverdefarr; |
8322 | Elf_Internal_Verdef iverdefmem; | |
252b5132 | 8323 | unsigned int i; |
062e2358 | 8324 | unsigned int maxidx; |
d0fb9a8d | 8325 | bfd_byte *contents_end_def, *contents_end_aux; |
252b5132 RH |
8326 | |
8327 | hdr = &elf_tdata (abfd)->dynverdef_hdr; | |
8328 | ||
601a03ba AM |
8329 | if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) |
8330 | { | |
8331 | error_return_bad_verdef: | |
4eca0228 | 8332 | _bfd_error_handler |
601a03ba AM |
8333 | (_("%B: .gnu.version_d invalid entry"), abfd); |
8334 | bfd_set_error (bfd_error_bad_value); | |
8335 | error_return_verdef: | |
8336 | elf_tdata (abfd)->verdef = NULL; | |
8337 | elf_tdata (abfd)->cverdefs = 0; | |
8338 | goto error_return; | |
8339 | } | |
8340 | ||
a50b1753 | 8341 | contents = (bfd_byte *) bfd_malloc (hdr->sh_size); |
252b5132 | 8342 | if (contents == NULL) |
601a03ba | 8343 | goto error_return_verdef; |
252b5132 | 8344 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 |
217aa764 | 8345 | || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) |
601a03ba | 8346 | goto error_return_verdef; |
d0fb9a8d JJ |
8347 | |
8348 | BFD_ASSERT (sizeof (Elf_External_Verdef) | |
8349 | >= sizeof (Elf_External_Verdaux)); | |
8350 | contents_end_def = contents + hdr->sh_size | |
8351 | - sizeof (Elf_External_Verdef); | |
8352 | contents_end_aux = contents + hdr->sh_size | |
8353 | - sizeof (Elf_External_Verdaux); | |
8354 | ||
f631889e UD |
8355 | /* We know the number of entries in the section but not the maximum |
8356 | index. Therefore we have to run through all entries and find | |
8357 | the maximum. */ | |
252b5132 | 8358 | everdef = (Elf_External_Verdef *) contents; |
f631889e UD |
8359 | maxidx = 0; |
8360 | for (i = 0; i < hdr->sh_info; ++i) | |
8361 | { | |
8362 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); | |
8363 | ||
601a03ba AM |
8364 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) |
8365 | goto error_return_bad_verdef; | |
062e2358 AM |
8366 | if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) |
8367 | maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); | |
f631889e | 8368 | |
25ff461f AM |
8369 | if (iverdefmem.vd_next == 0) |
8370 | break; | |
8371 | ||
d0fb9a8d JJ |
8372 | if (iverdefmem.vd_next |
8373 | > (size_t) (contents_end_def - (bfd_byte *) everdef)) | |
601a03ba | 8374 | goto error_return_bad_verdef; |
d0fb9a8d | 8375 | |
f631889e UD |
8376 | everdef = ((Elf_External_Verdef *) |
8377 | ((bfd_byte *) everdef + iverdefmem.vd_next)); | |
8378 | } | |
8379 | ||
fc0e6df6 PB |
8380 | if (default_imported_symver) |
8381 | { | |
8382 | if (freeidx > maxidx) | |
8383 | maxidx = ++freeidx; | |
8384 | else | |
8385 | freeidx = ++maxidx; | |
8386 | } | |
201159ec | 8387 | |
601a03ba AM |
8388 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8389 | bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef)); | |
f631889e | 8390 | if (elf_tdata (abfd)->verdef == NULL) |
601a03ba | 8391 | goto error_return_verdef; |
f631889e UD |
8392 | |
8393 | elf_tdata (abfd)->cverdefs = maxidx; | |
8394 | ||
8395 | everdef = (Elf_External_Verdef *) contents; | |
8396 | iverdefarr = elf_tdata (abfd)->verdef; | |
8397 | for (i = 0; i < hdr->sh_info; i++) | |
252b5132 RH |
8398 | { |
8399 | Elf_External_Verdaux *everdaux; | |
8400 | Elf_Internal_Verdaux *iverdaux; | |
8401 | unsigned int j; | |
8402 | ||
f631889e UD |
8403 | _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); |
8404 | ||
d0fb9a8d | 8405 | if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) |
601a03ba | 8406 | goto error_return_bad_verdef; |
d0fb9a8d | 8407 | |
f631889e | 8408 | iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; |
595bce75 | 8409 | memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); |
252b5132 RH |
8410 | |
8411 | iverdef->vd_bfd = abfd; | |
8412 | ||
d0fb9a8d JJ |
8413 | if (iverdef->vd_cnt == 0) |
8414 | iverdef->vd_auxptr = NULL; | |
8415 | else | |
8416 | { | |
a50b1753 NC |
8417 | iverdef->vd_auxptr = (struct elf_internal_verdaux *) |
8418 | bfd_alloc2 (abfd, iverdef->vd_cnt, | |
8419 | sizeof (Elf_Internal_Verdaux)); | |
d0fb9a8d JJ |
8420 | if (iverdef->vd_auxptr == NULL) |
8421 | goto error_return_verdef; | |
8422 | } | |
8423 | ||
8424 | if (iverdef->vd_aux | |
8425 | > (size_t) (contents_end_aux - (bfd_byte *) everdef)) | |
601a03ba | 8426 | goto error_return_bad_verdef; |
252b5132 RH |
8427 | |
8428 | everdaux = ((Elf_External_Verdaux *) | |
8429 | ((bfd_byte *) everdef + iverdef->vd_aux)); | |
8430 | iverdaux = iverdef->vd_auxptr; | |
8431 | for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) | |
8432 | { | |
8433 | _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); | |
8434 | ||
8435 | iverdaux->vda_nodename = | |
8436 | bfd_elf_string_from_elf_section (abfd, hdr->sh_link, | |
8437 | iverdaux->vda_name); | |
8438 | if (iverdaux->vda_nodename == NULL) | |
601a03ba | 8439 | goto error_return_bad_verdef; |
252b5132 | 8440 | |
25ff461f AM |
8441 | iverdaux->vda_nextptr = NULL; |
8442 | if (iverdaux->vda_next == 0) | |
8443 | { | |
8444 | iverdef->vd_cnt = j + 1; | |
8445 | break; | |
8446 | } | |
252b5132 RH |
8447 | if (j + 1 < iverdef->vd_cnt) |
8448 | iverdaux->vda_nextptr = iverdaux + 1; | |
252b5132 | 8449 | |
d0fb9a8d JJ |
8450 | if (iverdaux->vda_next |
8451 | > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) | |
601a03ba | 8452 | goto error_return_bad_verdef; |
d0fb9a8d | 8453 | |
252b5132 RH |
8454 | everdaux = ((Elf_External_Verdaux *) |
8455 | ((bfd_byte *) everdaux + iverdaux->vda_next)); | |
8456 | } | |
8457 | ||
595bce75 | 8458 | iverdef->vd_nodename = NULL; |
d0fb9a8d JJ |
8459 | if (iverdef->vd_cnt) |
8460 | iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; | |
252b5132 | 8461 | |
25ff461f AM |
8462 | iverdef->vd_nextdef = NULL; |
8463 | if (iverdef->vd_next == 0) | |
8464 | break; | |
d0fb9a8d | 8465 | if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) |
252b5132 | 8466 | iverdef->vd_nextdef = iverdef + 1; |
252b5132 RH |
8467 | |
8468 | everdef = ((Elf_External_Verdef *) | |
8469 | ((bfd_byte *) everdef + iverdef->vd_next)); | |
8470 | } | |
8471 | ||
8472 | free (contents); | |
8473 | contents = NULL; | |
8474 | } | |
fc0e6df6 | 8475 | else if (default_imported_symver) |
252b5132 | 8476 | { |
fc0e6df6 PB |
8477 | if (freeidx < 3) |
8478 | freeidx = 3; | |
8479 | else | |
8480 | freeidx++; | |
252b5132 | 8481 | |
a50b1753 NC |
8482 | elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) |
8483 | bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef)); | |
fc0e6df6 | 8484 | if (elf_tdata (abfd)->verdef == NULL) |
252b5132 RH |
8485 | goto error_return; |
8486 | ||
fc0e6df6 PB |
8487 | elf_tdata (abfd)->cverdefs = freeidx; |
8488 | } | |
252b5132 | 8489 | |
fc0e6df6 PB |
8490 | /* Create a default version based on the soname. */ |
8491 | if (default_imported_symver) | |
8492 | { | |
8493 | Elf_Internal_Verdef *iverdef; | |
8494 | Elf_Internal_Verdaux *iverdaux; | |
252b5132 | 8495 | |
5bb3703f | 8496 | iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; |
252b5132 | 8497 | |
fc0e6df6 PB |
8498 | iverdef->vd_version = VER_DEF_CURRENT; |
8499 | iverdef->vd_flags = 0; | |
8500 | iverdef->vd_ndx = freeidx; | |
8501 | iverdef->vd_cnt = 1; | |
252b5132 | 8502 | |
fc0e6df6 | 8503 | iverdef->vd_bfd = abfd; |
252b5132 | 8504 | |
fc0e6df6 PB |
8505 | iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); |
8506 | if (iverdef->vd_nodename == NULL) | |
d0fb9a8d | 8507 | goto error_return_verdef; |
fc0e6df6 | 8508 | iverdef->vd_nextdef = NULL; |
601a03ba AM |
8509 | iverdef->vd_auxptr = ((struct elf_internal_verdaux *) |
8510 | bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); | |
d0fb9a8d JJ |
8511 | if (iverdef->vd_auxptr == NULL) |
8512 | goto error_return_verdef; | |
252b5132 | 8513 | |
fc0e6df6 PB |
8514 | iverdaux = iverdef->vd_auxptr; |
8515 | iverdaux->vda_nodename = iverdef->vd_nodename; | |
252b5132 RH |
8516 | } |
8517 | ||
b34976b6 | 8518 | return TRUE; |
252b5132 RH |
8519 | |
8520 | error_return: | |
5ed6aba4 | 8521 | if (contents != NULL) |
252b5132 | 8522 | free (contents); |
b34976b6 | 8523 | return FALSE; |
252b5132 RH |
8524 | } |
8525 | \f | |
8526 | asymbol * | |
217aa764 | 8527 | _bfd_elf_make_empty_symbol (bfd *abfd) |
252b5132 RH |
8528 | { |
8529 | elf_symbol_type *newsym; | |
8530 | ||
201159ec | 8531 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym); |
252b5132 RH |
8532 | if (!newsym) |
8533 | return NULL; | |
201159ec NC |
8534 | newsym->symbol.the_bfd = abfd; |
8535 | return &newsym->symbol; | |
252b5132 RH |
8536 | } |
8537 | ||
8538 | void | |
217aa764 AM |
8539 | _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
8540 | asymbol *symbol, | |
8541 | symbol_info *ret) | |
252b5132 RH |
8542 | { |
8543 | bfd_symbol_info (symbol, ret); | |
8544 | } | |
8545 | ||
8546 | /* Return whether a symbol name implies a local symbol. Most targets | |
8547 | use this function for the is_local_label_name entry point, but some | |
8548 | override it. */ | |
8549 | ||
b34976b6 | 8550 | bfd_boolean |
217aa764 AM |
8551 | _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, |
8552 | const char *name) | |
252b5132 RH |
8553 | { |
8554 | /* Normal local symbols start with ``.L''. */ | |
8555 | if (name[0] == '.' && name[1] == 'L') | |
b34976b6 | 8556 | return TRUE; |
252b5132 RH |
8557 | |
8558 | /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate | |
8559 | DWARF debugging symbols starting with ``..''. */ | |
8560 | if (name[0] == '.' && name[1] == '.') | |
b34976b6 | 8561 | return TRUE; |
252b5132 RH |
8562 | |
8563 | /* gcc will sometimes generate symbols beginning with ``_.L_'' when | |
8564 | emitting DWARF debugging output. I suspect this is actually a | |
8565 | small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call | |
8566 | ASM_GENERATE_INTERNAL_LABEL, and this causes the leading | |
8567 | underscore to be emitted on some ELF targets). For ease of use, | |
8568 | we treat such symbols as local. */ | |
8569 | if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') | |
b34976b6 | 8570 | return TRUE; |
252b5132 | 8571 | |
b1fa9dd6 NC |
8572 | /* Treat assembler generated fake symbols, dollar local labels and |
8573 | forward-backward labels (aka local labels) as locals. | |
8574 | These labels have the form: | |
8575 | ||
8576 | L0^A.* (fake symbols) | |
8577 | ||
8578 | [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) | |
8579 | ||
8580 | Versions which start with .L will have already been matched above, | |
8581 | so we only need to match the rest. */ | |
8582 | if (name[0] == 'L' && ISDIGIT (name[1])) | |
8583 | { | |
8584 | bfd_boolean ret = FALSE; | |
8585 | const char * p; | |
8586 | char c; | |
8587 | ||
8588 | for (p = name + 2; (c = *p); p++) | |
8589 | { | |
8590 | if (c == 1 || c == 2) | |
8591 | { | |
8592 | if (c == 1 && p == name + 2) | |
8593 | /* A fake symbol. */ | |
8594 | return TRUE; | |
8595 | ||
8596 | /* FIXME: We are being paranoid here and treating symbols like | |
8597 | L0^Bfoo as if there were non-local, on the grounds that the | |
8598 | assembler will never generate them. But can any symbol | |
8599 | containing an ASCII value in the range 1-31 ever be anything | |
8600 | other than some kind of local ? */ | |
8601 | ret = TRUE; | |
8602 | } | |
8603 | ||
8604 | if (! ISDIGIT (c)) | |
8605 | { | |
8606 | ret = FALSE; | |
8607 | break; | |
8608 | } | |
8609 | } | |
8610 | return ret; | |
8611 | } | |
ffa54770 | 8612 | |
b34976b6 | 8613 | return FALSE; |
252b5132 RH |
8614 | } |
8615 | ||
8616 | alent * | |
217aa764 AM |
8617 | _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, |
8618 | asymbol *symbol ATTRIBUTE_UNUSED) | |
252b5132 RH |
8619 | { |
8620 | abort (); | |
8621 | return NULL; | |
8622 | } | |
8623 | ||
b34976b6 | 8624 | bfd_boolean |
217aa764 AM |
8625 | _bfd_elf_set_arch_mach (bfd *abfd, |
8626 | enum bfd_architecture arch, | |
8627 | unsigned long machine) | |
252b5132 RH |
8628 | { |
8629 | /* If this isn't the right architecture for this backend, and this | |
8630 | isn't the generic backend, fail. */ | |
8631 | if (arch != get_elf_backend_data (abfd)->arch | |
8632 | && arch != bfd_arch_unknown | |
8633 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
b34976b6 | 8634 | return FALSE; |
252b5132 RH |
8635 | |
8636 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
8637 | } | |
8638 | ||
d1fad7c6 NC |
8639 | /* Find the nearest line to a particular section and offset, |
8640 | for error reporting. */ | |
8641 | ||
b34976b6 | 8642 | bfd_boolean |
217aa764 | 8643 | _bfd_elf_find_nearest_line (bfd *abfd, |
217aa764 | 8644 | asymbol **symbols, |
fb167eb2 | 8645 | asection *section, |
217aa764 AM |
8646 | bfd_vma offset, |
8647 | const char **filename_ptr, | |
8648 | const char **functionname_ptr, | |
fb167eb2 AM |
8649 | unsigned int *line_ptr, |
8650 | unsigned int *discriminator_ptr) | |
d1fad7c6 | 8651 | { |
b34976b6 | 8652 | bfd_boolean found; |
d1fad7c6 | 8653 | |
fb167eb2 | 8654 | if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, |
4e8a9624 | 8655 | filename_ptr, functionname_ptr, |
fb167eb2 AM |
8656 | line_ptr, discriminator_ptr, |
8657 | dwarf_debug_sections, 0, | |
e00e8198 AM |
8658 | &elf_tdata (abfd)->dwarf2_find_line_info) |
8659 | || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, | |
8660 | filename_ptr, functionname_ptr, | |
8661 | line_ptr)) | |
d1fad7c6 NC |
8662 | { |
8663 | if (!*functionname_ptr) | |
e00e8198 AM |
8664 | _bfd_elf_find_function (abfd, symbols, section, offset, |
8665 | *filename_ptr ? NULL : filename_ptr, | |
8666 | functionname_ptr); | |
b34976b6 | 8667 | return TRUE; |
d1fad7c6 NC |
8668 | } |
8669 | ||
8670 | if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, | |
4e8a9624 AM |
8671 | &found, filename_ptr, |
8672 | functionname_ptr, line_ptr, | |
8673 | &elf_tdata (abfd)->line_info)) | |
b34976b6 | 8674 | return FALSE; |
dc43ada5 | 8675 | if (found && (*functionname_ptr || *line_ptr)) |
b34976b6 | 8676 | return TRUE; |
d1fad7c6 NC |
8677 | |
8678 | if (symbols == NULL) | |
b34976b6 | 8679 | return FALSE; |
d1fad7c6 | 8680 | |
e00e8198 AM |
8681 | if (! _bfd_elf_find_function (abfd, symbols, section, offset, |
8682 | filename_ptr, functionname_ptr)) | |
b34976b6 | 8683 | return FALSE; |
d1fad7c6 | 8684 | |
252b5132 | 8685 | *line_ptr = 0; |
b34976b6 | 8686 | return TRUE; |
252b5132 RH |
8687 | } |
8688 | ||
5420f73d L |
8689 | /* Find the line for a symbol. */ |
8690 | ||
8691 | bfd_boolean | |
8692 | _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, | |
8693 | const char **filename_ptr, unsigned int *line_ptr) | |
9b8d1a36 | 8694 | { |
fb167eb2 AM |
8695 | return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, |
8696 | filename_ptr, NULL, line_ptr, NULL, | |
8697 | dwarf_debug_sections, 0, | |
8698 | &elf_tdata (abfd)->dwarf2_find_line_info); | |
5420f73d L |
8699 | } |
8700 | ||
4ab527b0 FF |
8701 | /* After a call to bfd_find_nearest_line, successive calls to |
8702 | bfd_find_inliner_info can be used to get source information about | |
8703 | each level of function inlining that terminated at the address | |
8704 | passed to bfd_find_nearest_line. Currently this is only supported | |
8705 | for DWARF2 with appropriate DWARF3 extensions. */ | |
8706 | ||
8707 | bfd_boolean | |
8708 | _bfd_elf_find_inliner_info (bfd *abfd, | |
8709 | const char **filename_ptr, | |
8710 | const char **functionname_ptr, | |
8711 | unsigned int *line_ptr) | |
8712 | { | |
8713 | bfd_boolean found; | |
8714 | found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, | |
8715 | functionname_ptr, line_ptr, | |
8716 | & elf_tdata (abfd)->dwarf2_find_line_info); | |
8717 | return found; | |
8718 | } | |
8719 | ||
252b5132 | 8720 | int |
a6b96beb | 8721 | _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) |
252b5132 | 8722 | { |
8ded5a0f AM |
8723 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
8724 | int ret = bed->s->sizeof_ehdr; | |
252b5132 | 8725 | |
0e1862bb | 8726 | if (!bfd_link_relocatable (info)) |
8ded5a0f | 8727 | { |
12bd6957 | 8728 | bfd_size_type phdr_size = elf_program_header_size (abfd); |
8ded5a0f | 8729 | |
62d7a5f6 AM |
8730 | if (phdr_size == (bfd_size_type) -1) |
8731 | { | |
8732 | struct elf_segment_map *m; | |
8733 | ||
8734 | phdr_size = 0; | |
12bd6957 | 8735 | for (m = elf_seg_map (abfd); m != NULL; m = m->next) |
62d7a5f6 | 8736 | phdr_size += bed->s->sizeof_phdr; |
8ded5a0f | 8737 | |
62d7a5f6 AM |
8738 | if (phdr_size == 0) |
8739 | phdr_size = get_program_header_size (abfd, info); | |
8740 | } | |
8ded5a0f | 8741 | |
12bd6957 | 8742 | elf_program_header_size (abfd) = phdr_size; |
8ded5a0f AM |
8743 | ret += phdr_size; |
8744 | } | |
8745 | ||
252b5132 RH |
8746 | return ret; |
8747 | } | |
8748 | ||
b34976b6 | 8749 | bfd_boolean |
217aa764 AM |
8750 | _bfd_elf_set_section_contents (bfd *abfd, |
8751 | sec_ptr section, | |
0f867abe | 8752 | const void *location, |
217aa764 AM |
8753 | file_ptr offset, |
8754 | bfd_size_type count) | |
252b5132 RH |
8755 | { |
8756 | Elf_Internal_Shdr *hdr; | |
1b6aeedb | 8757 | file_ptr pos; |
252b5132 RH |
8758 | |
8759 | if (! abfd->output_has_begun | |
217aa764 | 8760 | && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) |
b34976b6 | 8761 | return FALSE; |
252b5132 | 8762 | |
0ce398f1 L |
8763 | if (!count) |
8764 | return TRUE; | |
8765 | ||
252b5132 | 8766 | hdr = &elf_section_data (section)->this_hdr; |
0ce398f1 L |
8767 | if (hdr->sh_offset == (file_ptr) -1) |
8768 | { | |
8769 | /* We must compress this section. Write output to the buffer. */ | |
8770 | unsigned char *contents = hdr->contents; | |
8771 | if ((offset + count) > hdr->sh_size | |
8772 | || (section->flags & SEC_ELF_COMPRESS) == 0 | |
8773 | || contents == NULL) | |
8774 | abort (); | |
8775 | memcpy (contents + offset, location, count); | |
8776 | return TRUE; | |
8777 | } | |
dc810e39 AM |
8778 | pos = hdr->sh_offset + offset; |
8779 | if (bfd_seek (abfd, pos, SEEK_SET) != 0 | |
8780 | || bfd_bwrite (location, count, abfd) != count) | |
b34976b6 | 8781 | return FALSE; |
252b5132 | 8782 | |
b34976b6 | 8783 | return TRUE; |
252b5132 RH |
8784 | } |
8785 | ||
8786 | void | |
217aa764 AM |
8787 | _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
8788 | arelent *cache_ptr ATTRIBUTE_UNUSED, | |
8789 | Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) | |
252b5132 RH |
8790 | { |
8791 | abort (); | |
8792 | } | |
8793 | ||
252b5132 RH |
8794 | /* Try to convert a non-ELF reloc into an ELF one. */ |
8795 | ||
b34976b6 | 8796 | bfd_boolean |
217aa764 | 8797 | _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) |
252b5132 | 8798 | { |
c044fabd | 8799 | /* Check whether we really have an ELF howto. */ |
252b5132 RH |
8800 | |
8801 | if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) | |
8802 | { | |
8803 | bfd_reloc_code_real_type code; | |
8804 | reloc_howto_type *howto; | |
8805 | ||
8806 | /* Alien reloc: Try to determine its type to replace it with an | |
c044fabd | 8807 | equivalent ELF reloc. */ |
252b5132 RH |
8808 | |
8809 | if (areloc->howto->pc_relative) | |
8810 | { | |
8811 | switch (areloc->howto->bitsize) | |
8812 | { | |
8813 | case 8: | |
8814 | code = BFD_RELOC_8_PCREL; | |
8815 | break; | |
8816 | case 12: | |
8817 | code = BFD_RELOC_12_PCREL; | |
8818 | break; | |
8819 | case 16: | |
8820 | code = BFD_RELOC_16_PCREL; | |
8821 | break; | |
8822 | case 24: | |
8823 | code = BFD_RELOC_24_PCREL; | |
8824 | break; | |
8825 | case 32: | |
8826 | code = BFD_RELOC_32_PCREL; | |
8827 | break; | |
8828 | case 64: | |
8829 | code = BFD_RELOC_64_PCREL; | |
8830 | break; | |
8831 | default: | |
8832 | goto fail; | |
8833 | } | |
8834 | ||
8835 | howto = bfd_reloc_type_lookup (abfd, code); | |
8836 | ||
8837 | if (areloc->howto->pcrel_offset != howto->pcrel_offset) | |
8838 | { | |
8839 | if (howto->pcrel_offset) | |
8840 | areloc->addend += areloc->address; | |
8841 | else | |
8842 | areloc->addend -= areloc->address; /* addend is unsigned!! */ | |
8843 | } | |
8844 | } | |
8845 | else | |
8846 | { | |
8847 | switch (areloc->howto->bitsize) | |
8848 | { | |
8849 | case 8: | |
8850 | code = BFD_RELOC_8; | |
8851 | break; | |
8852 | case 14: | |
8853 | code = BFD_RELOC_14; | |
8854 | break; | |
8855 | case 16: | |
8856 | code = BFD_RELOC_16; | |
8857 | break; | |
8858 | case 26: | |
8859 | code = BFD_RELOC_26; | |
8860 | break; | |
8861 | case 32: | |
8862 | code = BFD_RELOC_32; | |
8863 | break; | |
8864 | case 64: | |
8865 | code = BFD_RELOC_64; | |
8866 | break; | |
8867 | default: | |
8868 | goto fail; | |
8869 | } | |
8870 | ||
8871 | howto = bfd_reloc_type_lookup (abfd, code); | |
8872 | } | |
8873 | ||
8874 | if (howto) | |
8875 | areloc->howto = howto; | |
8876 | else | |
8877 | goto fail; | |
8878 | } | |
8879 | ||
b34976b6 | 8880 | return TRUE; |
252b5132 RH |
8881 | |
8882 | fail: | |
4eca0228 | 8883 | _bfd_error_handler |
695344c0 | 8884 | /* xgettext:c-format */ |
d003868e AM |
8885 | (_("%B: unsupported relocation type %s"), |
8886 | abfd, areloc->howto->name); | |
252b5132 | 8887 | bfd_set_error (bfd_error_bad_value); |
b34976b6 | 8888 | return FALSE; |
252b5132 RH |
8889 | } |
8890 | ||
b34976b6 | 8891 | bfd_boolean |
217aa764 | 8892 | _bfd_elf_close_and_cleanup (bfd *abfd) |
252b5132 | 8893 | { |
d9071b0c TG |
8894 | struct elf_obj_tdata *tdata = elf_tdata (abfd); |
8895 | if (bfd_get_format (abfd) == bfd_object && tdata != NULL) | |
252b5132 | 8896 | { |
c0355132 | 8897 | if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) |
2b0f7ef9 | 8898 | _bfd_elf_strtab_free (elf_shstrtab (abfd)); |
d9071b0c | 8899 | _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); |
252b5132 RH |
8900 | } |
8901 | ||
8902 | return _bfd_generic_close_and_cleanup (abfd); | |
8903 | } | |
8904 | ||
8905 | /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY | |
8906 | in the relocation's offset. Thus we cannot allow any sort of sanity | |
8907 | range-checking to interfere. There is nothing else to do in processing | |
8908 | this reloc. */ | |
8909 | ||
8910 | bfd_reloc_status_type | |
217aa764 AM |
8911 | _bfd_elf_rel_vtable_reloc_fn |
8912 | (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, | |
fc0a2244 | 8913 | struct bfd_symbol *symbol ATTRIBUTE_UNUSED, |
217aa764 AM |
8914 | void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, |
8915 | bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) | |
252b5132 RH |
8916 | { |
8917 | return bfd_reloc_ok; | |
8918 | } | |
252b5132 RH |
8919 | \f |
8920 | /* Elf core file support. Much of this only works on native | |
8921 | toolchains, since we rely on knowing the | |
8922 | machine-dependent procfs structure in order to pick | |
c044fabd | 8923 | out details about the corefile. */ |
252b5132 RH |
8924 | |
8925 | #ifdef HAVE_SYS_PROCFS_H | |
16231b7b DG |
8926 | /* Needed for new procfs interface on sparc-solaris. */ |
8927 | # define _STRUCTURED_PROC 1 | |
252b5132 RH |
8928 | # include <sys/procfs.h> |
8929 | #endif | |
8930 | ||
261b8d08 PA |
8931 | /* Return a PID that identifies a "thread" for threaded cores, or the |
8932 | PID of the main process for non-threaded cores. */ | |
252b5132 RH |
8933 | |
8934 | static int | |
217aa764 | 8935 | elfcore_make_pid (bfd *abfd) |
252b5132 | 8936 | { |
261b8d08 PA |
8937 | int pid; |
8938 | ||
228e534f | 8939 | pid = elf_tdata (abfd)->core->lwpid; |
261b8d08 | 8940 | if (pid == 0) |
228e534f | 8941 | pid = elf_tdata (abfd)->core->pid; |
261b8d08 PA |
8942 | |
8943 | return pid; | |
252b5132 RH |
8944 | } |
8945 | ||
252b5132 RH |
8946 | /* If there isn't a section called NAME, make one, using |
8947 | data from SECT. Note, this function will generate a | |
8948 | reference to NAME, so you shouldn't deallocate or | |
c044fabd | 8949 | overwrite it. */ |
252b5132 | 8950 | |
b34976b6 | 8951 | static bfd_boolean |
217aa764 | 8952 | elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) |
252b5132 | 8953 | { |
c044fabd | 8954 | asection *sect2; |
252b5132 RH |
8955 | |
8956 | if (bfd_get_section_by_name (abfd, name) != NULL) | |
b34976b6 | 8957 | return TRUE; |
252b5132 | 8958 | |
117ed4f8 | 8959 | sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); |
252b5132 | 8960 | if (sect2 == NULL) |
b34976b6 | 8961 | return FALSE; |
252b5132 | 8962 | |
eea6121a | 8963 | sect2->size = sect->size; |
252b5132 | 8964 | sect2->filepos = sect->filepos; |
252b5132 | 8965 | sect2->alignment_power = sect->alignment_power; |
b34976b6 | 8966 | return TRUE; |
252b5132 RH |
8967 | } |
8968 | ||
bb0082d6 AM |
8969 | /* Create a pseudosection containing SIZE bytes at FILEPOS. This |
8970 | actually creates up to two pseudosections: | |
8971 | - For the single-threaded case, a section named NAME, unless | |
8972 | such a section already exists. | |
8973 | - For the multi-threaded case, a section named "NAME/PID", where | |
8974 | PID is elfcore_make_pid (abfd). | |
24d3e51b | 8975 | Both pseudosections have identical contents. */ |
b34976b6 | 8976 | bfd_boolean |
217aa764 AM |
8977 | _bfd_elfcore_make_pseudosection (bfd *abfd, |
8978 | char *name, | |
8979 | size_t size, | |
8980 | ufile_ptr filepos) | |
bb0082d6 AM |
8981 | { |
8982 | char buf[100]; | |
8983 | char *threaded_name; | |
d4c88bbb | 8984 | size_t len; |
bb0082d6 AM |
8985 | asection *sect; |
8986 | ||
8987 | /* Build the section name. */ | |
8988 | ||
8989 | sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); | |
d4c88bbb | 8990 | len = strlen (buf) + 1; |
a50b1753 | 8991 | threaded_name = (char *) bfd_alloc (abfd, len); |
bb0082d6 | 8992 | if (threaded_name == NULL) |
b34976b6 | 8993 | return FALSE; |
d4c88bbb | 8994 | memcpy (threaded_name, buf, len); |
bb0082d6 | 8995 | |
117ed4f8 AM |
8996 | sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, |
8997 | SEC_HAS_CONTENTS); | |
bb0082d6 | 8998 | if (sect == NULL) |
b34976b6 | 8999 | return FALSE; |
eea6121a | 9000 | sect->size = size; |
bb0082d6 | 9001 | sect->filepos = filepos; |
bb0082d6 AM |
9002 | sect->alignment_power = 2; |
9003 | ||
936e320b | 9004 | return elfcore_maybe_make_sect (abfd, name, sect); |
bb0082d6 AM |
9005 | } |
9006 | ||
252b5132 | 9007 | /* prstatus_t exists on: |
4a938328 | 9008 | solaris 2.5+ |
252b5132 RH |
9009 | linux 2.[01] + glibc |
9010 | unixware 4.2 | |
9011 | */ | |
9012 | ||
9013 | #if defined (HAVE_PRSTATUS_T) | |
a7b97311 | 9014 | |
b34976b6 | 9015 | static bfd_boolean |
217aa764 | 9016 | elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9017 | { |
eea6121a | 9018 | size_t size; |
7ee38065 | 9019 | int offset; |
252b5132 | 9020 | |
4a938328 MS |
9021 | if (note->descsz == sizeof (prstatus_t)) |
9022 | { | |
9023 | prstatus_t prstat; | |
252b5132 | 9024 | |
eea6121a | 9025 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9026 | offset = offsetof (prstatus_t, pr_reg); |
4a938328 | 9027 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
252b5132 | 9028 | |
fa49d224 NC |
9029 | /* Do not overwrite the core signal if it |
9030 | has already been set by another thread. */ | |
228e534f AM |
9031 | if (elf_tdata (abfd)->core->signal == 0) |
9032 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9033 | if (elf_tdata (abfd)->core->pid == 0) | |
9034 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
252b5132 | 9035 | |
4a938328 MS |
9036 | /* pr_who exists on: |
9037 | solaris 2.5+ | |
9038 | unixware 4.2 | |
9039 | pr_who doesn't exist on: | |
9040 | linux 2.[01] | |
9041 | */ | |
252b5132 | 9042 | #if defined (HAVE_PRSTATUS_T_PR_WHO) |
228e534f | 9043 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9044 | #else |
228e534f | 9045 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
252b5132 | 9046 | #endif |
4a938328 | 9047 | } |
7ee38065 | 9048 | #if defined (HAVE_PRSTATUS32_T) |
4a938328 MS |
9049 | else if (note->descsz == sizeof (prstatus32_t)) |
9050 | { | |
9051 | /* 64-bit host, 32-bit corefile */ | |
9052 | prstatus32_t prstat; | |
9053 | ||
eea6121a | 9054 | size = sizeof (prstat.pr_reg); |
7ee38065 | 9055 | offset = offsetof (prstatus32_t, pr_reg); |
4a938328 MS |
9056 | memcpy (&prstat, note->descdata, sizeof (prstat)); |
9057 | ||
fa49d224 NC |
9058 | /* Do not overwrite the core signal if it |
9059 | has already been set by another thread. */ | |
228e534f AM |
9060 | if (elf_tdata (abfd)->core->signal == 0) |
9061 | elf_tdata (abfd)->core->signal = prstat.pr_cursig; | |
9062 | if (elf_tdata (abfd)->core->pid == 0) | |
9063 | elf_tdata (abfd)->core->pid = prstat.pr_pid; | |
4a938328 MS |
9064 | |
9065 | /* pr_who exists on: | |
9066 | solaris 2.5+ | |
9067 | unixware 4.2 | |
9068 | pr_who doesn't exist on: | |
9069 | linux 2.[01] | |
9070 | */ | |
7ee38065 | 9071 | #if defined (HAVE_PRSTATUS32_T_PR_WHO) |
228e534f | 9072 | elf_tdata (abfd)->core->lwpid = prstat.pr_who; |
261b8d08 | 9073 | #else |
228e534f | 9074 | elf_tdata (abfd)->core->lwpid = prstat.pr_pid; |
4a938328 MS |
9075 | #endif |
9076 | } | |
7ee38065 | 9077 | #endif /* HAVE_PRSTATUS32_T */ |
4a938328 MS |
9078 | else |
9079 | { | |
9080 | /* Fail - we don't know how to handle any other | |
9081 | note size (ie. data object type). */ | |
b34976b6 | 9082 | return TRUE; |
4a938328 | 9083 | } |
252b5132 | 9084 | |
bb0082d6 | 9085 | /* Make a ".reg/999" section and a ".reg" section. */ |
936e320b | 9086 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", |
eea6121a | 9087 | size, note->descpos + offset); |
252b5132 RH |
9088 | } |
9089 | #endif /* defined (HAVE_PRSTATUS_T) */ | |
9090 | ||
bb0082d6 | 9091 | /* Create a pseudosection containing the exact contents of NOTE. */ |
b34976b6 | 9092 | static bfd_boolean |
217aa764 AM |
9093 | elfcore_make_note_pseudosection (bfd *abfd, |
9094 | char *name, | |
9095 | Elf_Internal_Note *note) | |
252b5132 | 9096 | { |
936e320b AM |
9097 | return _bfd_elfcore_make_pseudosection (abfd, name, |
9098 | note->descsz, note->descpos); | |
252b5132 RH |
9099 | } |
9100 | ||
ff08c6bb JB |
9101 | /* There isn't a consistent prfpregset_t across platforms, |
9102 | but it doesn't matter, because we don't have to pick this | |
c044fabd KH |
9103 | data structure apart. */ |
9104 | ||
b34976b6 | 9105 | static bfd_boolean |
217aa764 | 9106 | elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9107 | { |
9108 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
9109 | } | |
9110 | ||
ff08c6bb | 9111 | /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note |
971d4640 | 9112 | type of NT_PRXFPREG. Just include the whole note's contents |
ff08c6bb | 9113 | literally. */ |
c044fabd | 9114 | |
b34976b6 | 9115 | static bfd_boolean |
217aa764 | 9116 | elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) |
ff08c6bb JB |
9117 | { |
9118 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
9119 | } | |
9120 | ||
4339cae0 L |
9121 | /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" |
9122 | with a note type of NT_X86_XSTATE. Just include the whole note's | |
9123 | contents literally. */ | |
9124 | ||
9125 | static bfd_boolean | |
9126 | elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) | |
9127 | { | |
9128 | return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); | |
9129 | } | |
9130 | ||
97753bd5 AM |
9131 | static bfd_boolean |
9132 | elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) | |
9133 | { | |
9134 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); | |
9135 | } | |
9136 | ||
89eeb0bc LM |
9137 | static bfd_boolean |
9138 | elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) | |
9139 | { | |
9140 | return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); | |
9141 | } | |
97753bd5 | 9142 | |
0675e188 UW |
9143 | static bfd_boolean |
9144 | elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) | |
9145 | { | |
9146 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); | |
9147 | } | |
9148 | ||
d7eeb400 MS |
9149 | static bfd_boolean |
9150 | elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) | |
9151 | { | |
9152 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); | |
9153 | } | |
9154 | ||
9155 | static bfd_boolean | |
9156 | elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) | |
9157 | { | |
9158 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); | |
9159 | } | |
9160 | ||
9161 | static bfd_boolean | |
9162 | elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) | |
9163 | { | |
9164 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); | |
9165 | } | |
9166 | ||
9167 | static bfd_boolean | |
9168 | elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) | |
9169 | { | |
9170 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); | |
9171 | } | |
9172 | ||
9173 | static bfd_boolean | |
9174 | elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) | |
9175 | { | |
9176 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); | |
9177 | } | |
9178 | ||
355b81d9 UW |
9179 | static bfd_boolean |
9180 | elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) | |
9181 | { | |
9182 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); | |
9183 | } | |
9184 | ||
9185 | static bfd_boolean | |
9186 | elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) | |
9187 | { | |
9188 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); | |
9189 | } | |
9190 | ||
abb3f6cc NC |
9191 | static bfd_boolean |
9192 | elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) | |
9193 | { | |
9194 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); | |
9195 | } | |
9196 | ||
4ef9f41a AA |
9197 | static bfd_boolean |
9198 | elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) | |
9199 | { | |
9200 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); | |
9201 | } | |
9202 | ||
9203 | static bfd_boolean | |
9204 | elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) | |
9205 | { | |
9206 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); | |
9207 | } | |
9208 | ||
88ab90e8 AA |
9209 | static bfd_boolean |
9210 | elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) | |
9211 | { | |
9212 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); | |
9213 | } | |
9214 | ||
9215 | static bfd_boolean | |
9216 | elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) | |
9217 | { | |
9218 | return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); | |
9219 | } | |
9220 | ||
faa9a424 UW |
9221 | static bfd_boolean |
9222 | elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) | |
9223 | { | |
9224 | return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); | |
9225 | } | |
9226 | ||
652451f8 YZ |
9227 | static bfd_boolean |
9228 | elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) | |
9229 | { | |
9230 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); | |
9231 | } | |
9232 | ||
9233 | static bfd_boolean | |
9234 | elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) | |
9235 | { | |
9236 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); | |
9237 | } | |
9238 | ||
9239 | static bfd_boolean | |
9240 | elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) | |
9241 | { | |
9242 | return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); | |
9243 | } | |
9244 | ||
252b5132 | 9245 | #if defined (HAVE_PRPSINFO_T) |
4a938328 | 9246 | typedef prpsinfo_t elfcore_psinfo_t; |
7ee38065 | 9247 | #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9248 | typedef prpsinfo32_t elfcore_psinfo32_t; |
9249 | #endif | |
252b5132 RH |
9250 | #endif |
9251 | ||
9252 | #if defined (HAVE_PSINFO_T) | |
4a938328 | 9253 | typedef psinfo_t elfcore_psinfo_t; |
7ee38065 | 9254 | #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ |
4a938328 MS |
9255 | typedef psinfo32_t elfcore_psinfo32_t; |
9256 | #endif | |
252b5132 RH |
9257 | #endif |
9258 | ||
252b5132 RH |
9259 | /* return a malloc'ed copy of a string at START which is at |
9260 | most MAX bytes long, possibly without a terminating '\0'. | |
c044fabd | 9261 | the copy will always have a terminating '\0'. */ |
252b5132 | 9262 | |
936e320b | 9263 | char * |
217aa764 | 9264 | _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) |
252b5132 | 9265 | { |
dc810e39 | 9266 | char *dups; |
a50b1753 | 9267 | char *end = (char *) memchr (start, '\0', max); |
dc810e39 | 9268 | size_t len; |
252b5132 RH |
9269 | |
9270 | if (end == NULL) | |
9271 | len = max; | |
9272 | else | |
9273 | len = end - start; | |
9274 | ||
a50b1753 | 9275 | dups = (char *) bfd_alloc (abfd, len + 1); |
dc810e39 | 9276 | if (dups == NULL) |
252b5132 RH |
9277 | return NULL; |
9278 | ||
dc810e39 AM |
9279 | memcpy (dups, start, len); |
9280 | dups[len] = '\0'; | |
252b5132 | 9281 | |
dc810e39 | 9282 | return dups; |
252b5132 RH |
9283 | } |
9284 | ||
bb0082d6 | 9285 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
b34976b6 | 9286 | static bfd_boolean |
217aa764 | 9287 | elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9288 | { |
4a938328 MS |
9289 | if (note->descsz == sizeof (elfcore_psinfo_t)) |
9290 | { | |
9291 | elfcore_psinfo_t psinfo; | |
252b5132 | 9292 | |
7ee38065 | 9293 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9294 | |
335e41d4 | 9295 | #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) |
228e534f | 9296 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9297 | #endif |
228e534f | 9298 | elf_tdata (abfd)->core->program |
936e320b AM |
9299 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9300 | sizeof (psinfo.pr_fname)); | |
252b5132 | 9301 | |
228e534f | 9302 | elf_tdata (abfd)->core->command |
936e320b AM |
9303 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9304 | sizeof (psinfo.pr_psargs)); | |
4a938328 | 9305 | } |
7ee38065 | 9306 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
4a938328 MS |
9307 | else if (note->descsz == sizeof (elfcore_psinfo32_t)) |
9308 | { | |
9309 | /* 64-bit host, 32-bit corefile */ | |
9310 | elfcore_psinfo32_t psinfo; | |
9311 | ||
7ee38065 | 9312 | memcpy (&psinfo, note->descdata, sizeof (psinfo)); |
252b5132 | 9313 | |
335e41d4 | 9314 | #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) |
228e534f | 9315 | elf_tdata (abfd)->core->pid = psinfo.pr_pid; |
335e41d4 | 9316 | #endif |
228e534f | 9317 | elf_tdata (abfd)->core->program |
936e320b AM |
9318 | = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, |
9319 | sizeof (psinfo.pr_fname)); | |
4a938328 | 9320 | |
228e534f | 9321 | elf_tdata (abfd)->core->command |
936e320b AM |
9322 | = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, |
9323 | sizeof (psinfo.pr_psargs)); | |
4a938328 MS |
9324 | } |
9325 | #endif | |
9326 | ||
9327 | else | |
9328 | { | |
9329 | /* Fail - we don't know how to handle any other | |
9330 | note size (ie. data object type). */ | |
b34976b6 | 9331 | return TRUE; |
4a938328 | 9332 | } |
252b5132 RH |
9333 | |
9334 | /* Note that for some reason, a spurious space is tacked | |
9335 | onto the end of the args in some (at least one anyway) | |
c044fabd | 9336 | implementations, so strip it off if it exists. */ |
252b5132 RH |
9337 | |
9338 | { | |
228e534f | 9339 | char *command = elf_tdata (abfd)->core->command; |
252b5132 RH |
9340 | int n = strlen (command); |
9341 | ||
9342 | if (0 < n && command[n - 1] == ' ') | |
9343 | command[n - 1] = '\0'; | |
9344 | } | |
9345 | ||
b34976b6 | 9346 | return TRUE; |
252b5132 RH |
9347 | } |
9348 | #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ | |
9349 | ||
252b5132 | 9350 | #if defined (HAVE_PSTATUS_T) |
b34976b6 | 9351 | static bfd_boolean |
217aa764 | 9352 | elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9353 | { |
f572a39d AM |
9354 | if (note->descsz == sizeof (pstatus_t) |
9355 | #if defined (HAVE_PXSTATUS_T) | |
9356 | || note->descsz == sizeof (pxstatus_t) | |
9357 | #endif | |
9358 | ) | |
4a938328 MS |
9359 | { |
9360 | pstatus_t pstat; | |
252b5132 | 9361 | |
4a938328 | 9362 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9363 | |
228e534f | 9364 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 | 9365 | } |
7ee38065 | 9366 | #if defined (HAVE_PSTATUS32_T) |
4a938328 MS |
9367 | else if (note->descsz == sizeof (pstatus32_t)) |
9368 | { | |
9369 | /* 64-bit host, 32-bit corefile */ | |
9370 | pstatus32_t pstat; | |
252b5132 | 9371 | |
4a938328 | 9372 | memcpy (&pstat, note->descdata, sizeof (pstat)); |
252b5132 | 9373 | |
228e534f | 9374 | elf_tdata (abfd)->core->pid = pstat.pr_pid; |
4a938328 MS |
9375 | } |
9376 | #endif | |
252b5132 RH |
9377 | /* Could grab some more details from the "representative" |
9378 | lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an | |
c044fabd | 9379 | NT_LWPSTATUS note, presumably. */ |
252b5132 | 9380 | |
b34976b6 | 9381 | return TRUE; |
252b5132 RH |
9382 | } |
9383 | #endif /* defined (HAVE_PSTATUS_T) */ | |
9384 | ||
252b5132 | 9385 | #if defined (HAVE_LWPSTATUS_T) |
b34976b6 | 9386 | static bfd_boolean |
217aa764 | 9387 | elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) |
252b5132 RH |
9388 | { |
9389 | lwpstatus_t lwpstat; | |
9390 | char buf[100]; | |
c044fabd | 9391 | char *name; |
d4c88bbb | 9392 | size_t len; |
c044fabd | 9393 | asection *sect; |
252b5132 | 9394 | |
f572a39d AM |
9395 | if (note->descsz != sizeof (lwpstat) |
9396 | #if defined (HAVE_LWPXSTATUS_T) | |
9397 | && note->descsz != sizeof (lwpxstatus_t) | |
9398 | #endif | |
9399 | ) | |
b34976b6 | 9400 | return TRUE; |
252b5132 RH |
9401 | |
9402 | memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); | |
9403 | ||
228e534f | 9404 | elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; |
a1504221 JB |
9405 | /* Do not overwrite the core signal if it has already been set by |
9406 | another thread. */ | |
228e534f AM |
9407 | if (elf_tdata (abfd)->core->signal == 0) |
9408 | elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; | |
252b5132 | 9409 | |
c044fabd | 9410 | /* Make a ".reg/999" section. */ |
252b5132 RH |
9411 | |
9412 | sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9413 | len = strlen (buf) + 1; |
217aa764 | 9414 | name = bfd_alloc (abfd, len); |
252b5132 | 9415 | if (name == NULL) |
b34976b6 | 9416 | return FALSE; |
d4c88bbb | 9417 | memcpy (name, buf, len); |
252b5132 | 9418 | |
117ed4f8 | 9419 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9420 | if (sect == NULL) |
b34976b6 | 9421 | return FALSE; |
252b5132 RH |
9422 | |
9423 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9424 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); |
252b5132 RH |
9425 | sect->filepos = note->descpos |
9426 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); | |
9427 | #endif | |
9428 | ||
9429 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
eea6121a | 9430 | sect->size = sizeof (lwpstat.pr_reg); |
252b5132 RH |
9431 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); |
9432 | #endif | |
9433 | ||
252b5132 RH |
9434 | sect->alignment_power = 2; |
9435 | ||
9436 | if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) | |
b34976b6 | 9437 | return FALSE; |
252b5132 RH |
9438 | |
9439 | /* Make a ".reg2/999" section */ | |
9440 | ||
9441 | sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); | |
d4c88bbb | 9442 | len = strlen (buf) + 1; |
217aa764 | 9443 | name = bfd_alloc (abfd, len); |
252b5132 | 9444 | if (name == NULL) |
b34976b6 | 9445 | return FALSE; |
d4c88bbb | 9446 | memcpy (name, buf, len); |
252b5132 | 9447 | |
117ed4f8 | 9448 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
252b5132 | 9449 | if (sect == NULL) |
b34976b6 | 9450 | return FALSE; |
252b5132 RH |
9451 | |
9452 | #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) | |
eea6121a | 9453 | sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); |
252b5132 RH |
9454 | sect->filepos = note->descpos |
9455 | + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); | |
9456 | #endif | |
9457 | ||
9458 | #if defined (HAVE_LWPSTATUS_T_PR_FPREG) | |
eea6121a | 9459 | sect->size = sizeof (lwpstat.pr_fpreg); |
252b5132 RH |
9460 | sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); |
9461 | #endif | |
9462 | ||
252b5132 RH |
9463 | sect->alignment_power = 2; |
9464 | ||
936e320b | 9465 | return elfcore_maybe_make_sect (abfd, ".reg2", sect); |
252b5132 RH |
9466 | } |
9467 | #endif /* defined (HAVE_LWPSTATUS_T) */ | |
9468 | ||
b34976b6 | 9469 | static bfd_boolean |
217aa764 | 9470 | elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) |
16e9c715 NC |
9471 | { |
9472 | char buf[30]; | |
c044fabd | 9473 | char *name; |
d4c88bbb | 9474 | size_t len; |
c044fabd | 9475 | asection *sect; |
4a6636fb PA |
9476 | int type; |
9477 | int is_active_thread; | |
9478 | bfd_vma base_addr; | |
16e9c715 | 9479 | |
4a6636fb | 9480 | if (note->descsz < 728) |
b34976b6 | 9481 | return TRUE; |
16e9c715 | 9482 | |
4a6636fb PA |
9483 | if (! CONST_STRNEQ (note->namedata, "win32")) |
9484 | return TRUE; | |
9485 | ||
9486 | type = bfd_get_32 (abfd, note->descdata); | |
c044fabd | 9487 | |
4a6636fb | 9488 | switch (type) |
16e9c715 | 9489 | { |
4a6636fb | 9490 | case 1 /* NOTE_INFO_PROCESS */: |
228e534f | 9491 | /* FIXME: need to add ->core->command. */ |
4a6636fb | 9492 | /* process_info.pid */ |
228e534f | 9493 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8); |
4a6636fb | 9494 | /* process_info.signal */ |
228e534f | 9495 | elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12); |
c044fabd | 9496 | break; |
16e9c715 | 9497 | |
4a6636fb | 9498 | case 2 /* NOTE_INFO_THREAD */: |
16e9c715 | 9499 | /* Make a ".reg/999" section. */ |
4a6636fb PA |
9500 | /* thread_info.tid */ |
9501 | sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8)); | |
c044fabd | 9502 | |
d4c88bbb | 9503 | len = strlen (buf) + 1; |
a50b1753 | 9504 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9505 | if (name == NULL) |
b34976b6 | 9506 | return FALSE; |
c044fabd | 9507 | |
d4c88bbb | 9508 | memcpy (name, buf, len); |
16e9c715 | 9509 | |
117ed4f8 | 9510 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
16e9c715 | 9511 | if (sect == NULL) |
b34976b6 | 9512 | return FALSE; |
c044fabd | 9513 | |
4a6636fb PA |
9514 | /* sizeof (thread_info.thread_context) */ |
9515 | sect->size = 716; | |
9516 | /* offsetof (thread_info.thread_context) */ | |
9517 | sect->filepos = note->descpos + 12; | |
16e9c715 NC |
9518 | sect->alignment_power = 2; |
9519 | ||
4a6636fb PA |
9520 | /* thread_info.is_active_thread */ |
9521 | is_active_thread = bfd_get_32 (abfd, note->descdata + 8); | |
9522 | ||
9523 | if (is_active_thread) | |
16e9c715 | 9524 | if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) |
b34976b6 | 9525 | return FALSE; |
16e9c715 NC |
9526 | break; |
9527 | ||
4a6636fb | 9528 | case 3 /* NOTE_INFO_MODULE */: |
16e9c715 | 9529 | /* Make a ".module/xxxxxxxx" section. */ |
4a6636fb PA |
9530 | /* module_info.base_address */ |
9531 | base_addr = bfd_get_32 (abfd, note->descdata + 4); | |
0af1713e | 9532 | sprintf (buf, ".module/%08lx", (unsigned long) base_addr); |
c044fabd | 9533 | |
d4c88bbb | 9534 | len = strlen (buf) + 1; |
a50b1753 | 9535 | name = (char *) bfd_alloc (abfd, len); |
16e9c715 | 9536 | if (name == NULL) |
b34976b6 | 9537 | return FALSE; |
c044fabd | 9538 | |
d4c88bbb | 9539 | memcpy (name, buf, len); |
252b5132 | 9540 | |
117ed4f8 | 9541 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
c044fabd | 9542 | |
16e9c715 | 9543 | if (sect == NULL) |
b34976b6 | 9544 | return FALSE; |
c044fabd | 9545 | |
eea6121a | 9546 | sect->size = note->descsz; |
16e9c715 | 9547 | sect->filepos = note->descpos; |
16e9c715 NC |
9548 | sect->alignment_power = 2; |
9549 | break; | |
9550 | ||
9551 | default: | |
b34976b6 | 9552 | return TRUE; |
16e9c715 NC |
9553 | } |
9554 | ||
b34976b6 | 9555 | return TRUE; |
16e9c715 | 9556 | } |
252b5132 | 9557 | |
b34976b6 | 9558 | static bfd_boolean |
217aa764 | 9559 | elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) |
252b5132 | 9560 | { |
9c5bfbb7 | 9561 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
bb0082d6 | 9562 | |
252b5132 RH |
9563 | switch (note->type) |
9564 | { | |
9565 | default: | |
b34976b6 | 9566 | return TRUE; |
252b5132 | 9567 | |
252b5132 | 9568 | case NT_PRSTATUS: |
bb0082d6 AM |
9569 | if (bed->elf_backend_grok_prstatus) |
9570 | if ((*bed->elf_backend_grok_prstatus) (abfd, note)) | |
b34976b6 | 9571 | return TRUE; |
bb0082d6 | 9572 | #if defined (HAVE_PRSTATUS_T) |
252b5132 | 9573 | return elfcore_grok_prstatus (abfd, note); |
bb0082d6 | 9574 | #else |
b34976b6 | 9575 | return TRUE; |
252b5132 RH |
9576 | #endif |
9577 | ||
9578 | #if defined (HAVE_PSTATUS_T) | |
9579 | case NT_PSTATUS: | |
9580 | return elfcore_grok_pstatus (abfd, note); | |
9581 | #endif | |
9582 | ||
9583 | #if defined (HAVE_LWPSTATUS_T) | |
9584 | case NT_LWPSTATUS: | |
9585 | return elfcore_grok_lwpstatus (abfd, note); | |
9586 | #endif | |
9587 | ||
9588 | case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ | |
9589 | return elfcore_grok_prfpreg (abfd, note); | |
9590 | ||
c044fabd | 9591 | case NT_WIN32PSTATUS: |
16e9c715 | 9592 | return elfcore_grok_win32pstatus (abfd, note); |
16e9c715 | 9593 | |
c044fabd | 9594 | case NT_PRXFPREG: /* Linux SSE extension */ |
e377ab71 MK |
9595 | if (note->namesz == 6 |
9596 | && strcmp (note->namedata, "LINUX") == 0) | |
ff08c6bb JB |
9597 | return elfcore_grok_prxfpreg (abfd, note); |
9598 | else | |
b34976b6 | 9599 | return TRUE; |
ff08c6bb | 9600 | |
4339cae0 L |
9601 | case NT_X86_XSTATE: /* Linux XSAVE extension */ |
9602 | if (note->namesz == 6 | |
9603 | && strcmp (note->namedata, "LINUX") == 0) | |
9604 | return elfcore_grok_xstatereg (abfd, note); | |
9605 | else | |
9606 | return TRUE; | |
9607 | ||
97753bd5 AM |
9608 | case NT_PPC_VMX: |
9609 | if (note->namesz == 6 | |
9610 | && strcmp (note->namedata, "LINUX") == 0) | |
9611 | return elfcore_grok_ppc_vmx (abfd, note); | |
9612 | else | |
9613 | return TRUE; | |
9614 | ||
89eeb0bc LM |
9615 | case NT_PPC_VSX: |
9616 | if (note->namesz == 6 | |
9617 | && strcmp (note->namedata, "LINUX") == 0) | |
9618 | return elfcore_grok_ppc_vsx (abfd, note); | |
9619 | else | |
9620 | return TRUE; | |
9621 | ||
0675e188 UW |
9622 | case NT_S390_HIGH_GPRS: |
9623 | if (note->namesz == 6 | |
9624 | && strcmp (note->namedata, "LINUX") == 0) | |
9625 | return elfcore_grok_s390_high_gprs (abfd, note); | |
9626 | else | |
9627 | return TRUE; | |
9628 | ||
d7eeb400 MS |
9629 | case NT_S390_TIMER: |
9630 | if (note->namesz == 6 | |
9631 | && strcmp (note->namedata, "LINUX") == 0) | |
9632 | return elfcore_grok_s390_timer (abfd, note); | |
9633 | else | |
9634 | return TRUE; | |
9635 | ||
9636 | case NT_S390_TODCMP: | |
9637 | if (note->namesz == 6 | |
9638 | && strcmp (note->namedata, "LINUX") == 0) | |
9639 | return elfcore_grok_s390_todcmp (abfd, note); | |
9640 | else | |
9641 | return TRUE; | |
9642 | ||
9643 | case NT_S390_TODPREG: | |
9644 | if (note->namesz == 6 | |
9645 | && strcmp (note->namedata, "LINUX") == 0) | |
9646 | return elfcore_grok_s390_todpreg (abfd, note); | |
9647 | else | |
9648 | return TRUE; | |
9649 | ||
9650 | case NT_S390_CTRS: | |
9651 | if (note->namesz == 6 | |
9652 | && strcmp (note->namedata, "LINUX") == 0) | |
9653 | return elfcore_grok_s390_ctrs (abfd, note); | |
9654 | else | |
9655 | return TRUE; | |
9656 | ||
9657 | case NT_S390_PREFIX: | |
9658 | if (note->namesz == 6 | |
9659 | && strcmp (note->namedata, "LINUX") == 0) | |
9660 | return elfcore_grok_s390_prefix (abfd, note); | |
9661 | else | |
9662 | return TRUE; | |
9663 | ||
355b81d9 UW |
9664 | case NT_S390_LAST_BREAK: |
9665 | if (note->namesz == 6 | |
9666 | && strcmp (note->namedata, "LINUX") == 0) | |
9667 | return elfcore_grok_s390_last_break (abfd, note); | |
9668 | else | |
9669 | return TRUE; | |
9670 | ||
9671 | case NT_S390_SYSTEM_CALL: | |
9672 | if (note->namesz == 6 | |
9673 | && strcmp (note->namedata, "LINUX") == 0) | |
9674 | return elfcore_grok_s390_system_call (abfd, note); | |
9675 | else | |
9676 | return TRUE; | |
9677 | ||
abb3f6cc NC |
9678 | case NT_S390_TDB: |
9679 | if (note->namesz == 6 | |
9680 | && strcmp (note->namedata, "LINUX") == 0) | |
9681 | return elfcore_grok_s390_tdb (abfd, note); | |
9682 | else | |
9683 | return TRUE; | |
9684 | ||
4ef9f41a AA |
9685 | case NT_S390_VXRS_LOW: |
9686 | if (note->namesz == 6 | |
9687 | && strcmp (note->namedata, "LINUX") == 0) | |
9688 | return elfcore_grok_s390_vxrs_low (abfd, note); | |
9689 | else | |
9690 | return TRUE; | |
9691 | ||
9692 | case NT_S390_VXRS_HIGH: | |
9693 | if (note->namesz == 6 | |
9694 | && strcmp (note->namedata, "LINUX") == 0) | |
9695 | return elfcore_grok_s390_vxrs_high (abfd, note); | |
9696 | else | |
9697 | return TRUE; | |
9698 | ||
88ab90e8 AA |
9699 | case NT_S390_GS_CB: |
9700 | if (note->namesz == 6 | |
9701 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 9702 | return elfcore_grok_s390_gs_cb (abfd, note); |
88ab90e8 AA |
9703 | else |
9704 | return TRUE; | |
9705 | ||
9706 | case NT_S390_GS_BC: | |
9707 | if (note->namesz == 6 | |
9708 | && strcmp (note->namedata, "LINUX") == 0) | |
8fe09d74 | 9709 | return elfcore_grok_s390_gs_bc (abfd, note); |
88ab90e8 AA |
9710 | else |
9711 | return TRUE; | |
9712 | ||
faa9a424 UW |
9713 | case NT_ARM_VFP: |
9714 | if (note->namesz == 6 | |
9715 | && strcmp (note->namedata, "LINUX") == 0) | |
9716 | return elfcore_grok_arm_vfp (abfd, note); | |
9717 | else | |
9718 | return TRUE; | |
9719 | ||
652451f8 YZ |
9720 | case NT_ARM_TLS: |
9721 | if (note->namesz == 6 | |
9722 | && strcmp (note->namedata, "LINUX") == 0) | |
9723 | return elfcore_grok_aarch_tls (abfd, note); | |
9724 | else | |
9725 | return TRUE; | |
9726 | ||
9727 | case NT_ARM_HW_BREAK: | |
9728 | if (note->namesz == 6 | |
9729 | && strcmp (note->namedata, "LINUX") == 0) | |
9730 | return elfcore_grok_aarch_hw_break (abfd, note); | |
9731 | else | |
9732 | return TRUE; | |
9733 | ||
9734 | case NT_ARM_HW_WATCH: | |
9735 | if (note->namesz == 6 | |
9736 | && strcmp (note->namedata, "LINUX") == 0) | |
9737 | return elfcore_grok_aarch_hw_watch (abfd, note); | |
9738 | else | |
9739 | return TRUE; | |
9740 | ||
252b5132 RH |
9741 | case NT_PRPSINFO: |
9742 | case NT_PSINFO: | |
bb0082d6 AM |
9743 | if (bed->elf_backend_grok_psinfo) |
9744 | if ((*bed->elf_backend_grok_psinfo) (abfd, note)) | |
b34976b6 | 9745 | return TRUE; |
bb0082d6 | 9746 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
252b5132 | 9747 | return elfcore_grok_psinfo (abfd, note); |
bb0082d6 | 9748 | #else |
b34976b6 | 9749 | return TRUE; |
252b5132 | 9750 | #endif |
3333a7c3 RM |
9751 | |
9752 | case NT_AUXV: | |
9753 | { | |
117ed4f8 AM |
9754 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", |
9755 | SEC_HAS_CONTENTS); | |
3333a7c3 RM |
9756 | |
9757 | if (sect == NULL) | |
9758 | return FALSE; | |
eea6121a | 9759 | sect->size = note->descsz; |
3333a7c3 | 9760 | sect->filepos = note->descpos; |
3333a7c3 RM |
9761 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; |
9762 | ||
9763 | return TRUE; | |
9764 | } | |
9015683b | 9765 | |
451b7c33 TT |
9766 | case NT_FILE: |
9767 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", | |
9768 | note); | |
9769 | ||
9015683b TT |
9770 | case NT_SIGINFO: |
9771 | return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", | |
9772 | note); | |
5b2c414d | 9773 | |
252b5132 RH |
9774 | } |
9775 | } | |
9776 | ||
718175fa JK |
9777 | static bfd_boolean |
9778 | elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) | |
9779 | { | |
c74f7d1c | 9780 | struct bfd_build_id* build_id; |
30e8ee25 AM |
9781 | |
9782 | if (note->descsz == 0) | |
9783 | return FALSE; | |
9784 | ||
c74f7d1c JT |
9785 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); |
9786 | if (build_id == NULL) | |
718175fa JK |
9787 | return FALSE; |
9788 | ||
c74f7d1c JT |
9789 | build_id->size = note->descsz; |
9790 | memcpy (build_id->data, note->descdata, note->descsz); | |
9791 | abfd->build_id = build_id; | |
718175fa JK |
9792 | |
9793 | return TRUE; | |
9794 | } | |
9795 | ||
9796 | static bfd_boolean | |
9797 | elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) | |
9798 | { | |
9799 | switch (note->type) | |
9800 | { | |
9801 | default: | |
9802 | return TRUE; | |
9803 | ||
46bed679 L |
9804 | case NT_GNU_PROPERTY_TYPE_0: |
9805 | return _bfd_elf_parse_gnu_properties (abfd, note); | |
9806 | ||
718175fa JK |
9807 | case NT_GNU_BUILD_ID: |
9808 | return elfobj_grok_gnu_build_id (abfd, note); | |
9809 | } | |
9810 | } | |
9811 | ||
e21e5835 NC |
9812 | static bfd_boolean |
9813 | elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) | |
9814 | { | |
9815 | struct sdt_note *cur = | |
9816 | (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note) | |
9817 | + note->descsz); | |
9818 | ||
9819 | cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; | |
9820 | cur->size = (bfd_size_type) note->descsz; | |
9821 | memcpy (cur->data, note->descdata, note->descsz); | |
9822 | ||
9823 | elf_tdata (abfd)->sdt_note_head = cur; | |
9824 | ||
9825 | return TRUE; | |
9826 | } | |
9827 | ||
9828 | static bfd_boolean | |
9829 | elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) | |
9830 | { | |
9831 | switch (note->type) | |
9832 | { | |
9833 | case NT_STAPSDT: | |
9834 | return elfobj_grok_stapsdt_note_1 (abfd, note); | |
9835 | ||
9836 | default: | |
9837 | return TRUE; | |
9838 | } | |
9839 | } | |
9840 | ||
aa1ed4a9 JB |
9841 | static bfd_boolean |
9842 | elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
9843 | { | |
9844 | size_t offset; | |
9845 | ||
b5430a3c | 9846 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 9847 | { |
b5430a3c | 9848 | case ELFCLASS32: |
0064d223 JB |
9849 | if (note->descsz < 108) |
9850 | return FALSE; | |
aa1ed4a9 JB |
9851 | break; |
9852 | ||
b5430a3c | 9853 | case ELFCLASS64: |
0064d223 JB |
9854 | if (note->descsz < 120) |
9855 | return FALSE; | |
aa1ed4a9 JB |
9856 | break; |
9857 | ||
9858 | default: | |
9859 | return FALSE; | |
9860 | } | |
9861 | ||
0064d223 JB |
9862 | /* Check for version 1 in pr_version. */ |
9863 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9864 | return FALSE; | |
9865 | offset = 4; | |
9866 | ||
9867 | /* Skip over pr_psinfosz. */ | |
b5430a3c | 9868 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
0064d223 JB |
9869 | offset += 4; |
9870 | else | |
9871 | { | |
9872 | offset += 4; /* Padding before pr_psinfosz. */ | |
9873 | offset += 8; | |
9874 | } | |
9875 | ||
aa1ed4a9 JB |
9876 | /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ |
9877 | elf_tdata (abfd)->core->program | |
9878 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); | |
9879 | offset += 17; | |
9880 | ||
9881 | /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ | |
9882 | elf_tdata (abfd)->core->command | |
9883 | = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); | |
0064d223 JB |
9884 | offset += 81; |
9885 | ||
9886 | /* Padding before pr_pid. */ | |
9887 | offset += 2; | |
9888 | ||
9889 | /* The pr_pid field was added in version "1a". */ | |
9890 | if (note->descsz < offset + 4) | |
9891 | return TRUE; | |
9892 | ||
9893 | elf_tdata (abfd)->core->pid | |
9894 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
aa1ed4a9 JB |
9895 | |
9896 | return TRUE; | |
9897 | } | |
9898 | ||
9899 | static bfd_boolean | |
9900 | elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
9901 | { | |
9902 | size_t offset; | |
9903 | size_t size; | |
24d3e51b | 9904 | size_t min_size; |
aa1ed4a9 | 9905 | |
24d3e51b NC |
9906 | /* Compute offset of pr_getregsz, skipping over pr_statussz. |
9907 | Also compute minimum size of this note. */ | |
b5430a3c | 9908 | switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) |
aa1ed4a9 | 9909 | { |
b5430a3c | 9910 | case ELFCLASS32: |
24d3e51b NC |
9911 | offset = 4 + 4; |
9912 | min_size = offset + (4 * 2) + 4 + 4 + 4; | |
aa1ed4a9 JB |
9913 | break; |
9914 | ||
b5430a3c | 9915 | case ELFCLASS64: |
24d3e51b NC |
9916 | offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ |
9917 | min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; | |
aa1ed4a9 JB |
9918 | break; |
9919 | ||
9920 | default: | |
9921 | return FALSE; | |
9922 | } | |
9923 | ||
24d3e51b NC |
9924 | if (note->descsz < min_size) |
9925 | return FALSE; | |
9926 | ||
9927 | /* Check for version 1 in pr_version. */ | |
9928 | if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) | |
9929 | return FALSE; | |
aa1ed4a9 | 9930 | |
24d3e51b NC |
9931 | /* Extract size of pr_reg from pr_gregsetsz. */ |
9932 | /* Skip over pr_gregsetsz and pr_fpregsetsz. */ | |
b5430a3c | 9933 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) |
24d3e51b NC |
9934 | { |
9935 | size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9936 | offset += 4 * 2; | |
9937 | } | |
b5430a3c | 9938 | else |
24d3e51b NC |
9939 | { |
9940 | size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); | |
9941 | offset += 8 * 2; | |
9942 | } | |
aa1ed4a9 | 9943 | |
24d3e51b | 9944 | /* Skip over pr_osreldate. */ |
aa1ed4a9 JB |
9945 | offset += 4; |
9946 | ||
24d3e51b | 9947 | /* Read signal from pr_cursig. */ |
aa1ed4a9 JB |
9948 | if (elf_tdata (abfd)->core->signal == 0) |
9949 | elf_tdata (abfd)->core->signal | |
9950 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9951 | offset += 4; | |
9952 | ||
24d3e51b | 9953 | /* Read TID from pr_pid. */ |
aa1ed4a9 JB |
9954 | elf_tdata (abfd)->core->lwpid |
9955 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); | |
9956 | offset += 4; | |
9957 | ||
24d3e51b | 9958 | /* Padding before pr_reg. */ |
b5430a3c | 9959 | if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) |
aa1ed4a9 JB |
9960 | offset += 4; |
9961 | ||
24d3e51b NC |
9962 | /* Make sure that there is enough data remaining in the note. */ |
9963 | if ((note->descsz - offset) < size) | |
9964 | return FALSE; | |
9965 | ||
aa1ed4a9 JB |
9966 | /* Make a ".reg/999" section and a ".reg" section. */ |
9967 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
9968 | size, note->descpos + offset); | |
9969 | } | |
9970 | ||
9971 | static bfd_boolean | |
9972 | elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) | |
9973 | { | |
9974 | switch (note->type) | |
9975 | { | |
9976 | case NT_PRSTATUS: | |
9977 | return elfcore_grok_freebsd_prstatus (abfd, note); | |
9978 | ||
9979 | case NT_FPREGSET: | |
9980 | return elfcore_grok_prfpreg (abfd, note); | |
9981 | ||
9982 | case NT_PRPSINFO: | |
9983 | return elfcore_grok_freebsd_psinfo (abfd, note); | |
9984 | ||
9985 | case NT_FREEBSD_THRMISC: | |
9986 | if (note->namesz == 8) | |
9987 | return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); | |
9988 | else | |
9989 | return TRUE; | |
9990 | ||
3350c5f5 JB |
9991 | case NT_FREEBSD_PROCSTAT_AUXV: |
9992 | { | |
9993 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
9994 | SEC_HAS_CONTENTS); | |
9995 | ||
9996 | if (sect == NULL) | |
9997 | return FALSE; | |
9998 | sect->size = note->descsz - 4; | |
9999 | sect->filepos = note->descpos + 4; | |
10000 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10001 | ||
10002 | return TRUE; | |
10003 | } | |
10004 | ||
aa1ed4a9 JB |
10005 | case NT_X86_XSTATE: |
10006 | if (note->namesz == 8) | |
10007 | return elfcore_grok_xstatereg (abfd, note); | |
10008 | else | |
10009 | return TRUE; | |
10010 | ||
e6f3b9c3 JB |
10011 | case NT_FREEBSD_PTLWPINFO: |
10012 | return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", | |
10013 | note); | |
10014 | ||
aa1ed4a9 JB |
10015 | default: |
10016 | return TRUE; | |
10017 | } | |
10018 | } | |
10019 | ||
b34976b6 | 10020 | static bfd_boolean |
217aa764 | 10021 | elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) |
50b2bdb7 AM |
10022 | { |
10023 | char *cp; | |
10024 | ||
10025 | cp = strchr (note->namedata, '@'); | |
10026 | if (cp != NULL) | |
10027 | { | |
d2b64500 | 10028 | *lwpidp = atoi(cp + 1); |
b34976b6 | 10029 | return TRUE; |
50b2bdb7 | 10030 | } |
b34976b6 | 10031 | return FALSE; |
50b2bdb7 AM |
10032 | } |
10033 | ||
b34976b6 | 10034 | static bfd_boolean |
217aa764 | 10035 | elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 | 10036 | { |
50b2bdb7 | 10037 | /* Signal number at offset 0x08. */ |
228e534f | 10038 | elf_tdata (abfd)->core->signal |
50b2bdb7 AM |
10039 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10040 | ||
10041 | /* Process ID at offset 0x50. */ | |
228e534f | 10042 | elf_tdata (abfd)->core->pid |
50b2bdb7 AM |
10043 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); |
10044 | ||
10045 | /* Command name at 0x7c (max 32 bytes, including nul). */ | |
228e534f | 10046 | elf_tdata (abfd)->core->command |
50b2bdb7 AM |
10047 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); |
10048 | ||
7720ba9f MK |
10049 | return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", |
10050 | note); | |
50b2bdb7 AM |
10051 | } |
10052 | ||
b34976b6 | 10053 | static bfd_boolean |
217aa764 | 10054 | elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) |
50b2bdb7 AM |
10055 | { |
10056 | int lwp; | |
10057 | ||
10058 | if (elfcore_netbsd_get_lwpid (note, &lwp)) | |
228e534f | 10059 | elf_tdata (abfd)->core->lwpid = lwp; |
50b2bdb7 | 10060 | |
b4db1224 | 10061 | if (note->type == NT_NETBSDCORE_PROCINFO) |
50b2bdb7 AM |
10062 | { |
10063 | /* NetBSD-specific core "procinfo". Note that we expect to | |
08a40648 AM |
10064 | find this note before any of the others, which is fine, |
10065 | since the kernel writes this note out first when it | |
10066 | creates a core file. */ | |
47d9a591 | 10067 | |
50b2bdb7 AM |
10068 | return elfcore_grok_netbsd_procinfo (abfd, note); |
10069 | } | |
10070 | ||
b4db1224 JT |
10071 | /* As of Jan 2002 there are no other machine-independent notes |
10072 | defined for NetBSD core files. If the note type is less | |
10073 | than the start of the machine-dependent note types, we don't | |
10074 | understand it. */ | |
47d9a591 | 10075 | |
b4db1224 | 10076 | if (note->type < NT_NETBSDCORE_FIRSTMACH) |
b34976b6 | 10077 | return TRUE; |
50b2bdb7 AM |
10078 | |
10079 | ||
10080 | switch (bfd_get_arch (abfd)) | |
10081 | { | |
08a40648 AM |
10082 | /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and |
10083 | PT_GETFPREGS == mach+2. */ | |
50b2bdb7 AM |
10084 | |
10085 | case bfd_arch_alpha: | |
10086 | case bfd_arch_sparc: | |
10087 | switch (note->type) | |
08a40648 AM |
10088 | { |
10089 | case NT_NETBSDCORE_FIRSTMACH+0: | |
10090 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10091 | |
08a40648 AM |
10092 | case NT_NETBSDCORE_FIRSTMACH+2: |
10093 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10094 | |
08a40648 AM |
10095 | default: |
10096 | return TRUE; | |
10097 | } | |
50b2bdb7 | 10098 | |
08a40648 AM |
10099 | /* On all other arch's, PT_GETREGS == mach+1 and |
10100 | PT_GETFPREGS == mach+3. */ | |
50b2bdb7 AM |
10101 | |
10102 | default: | |
10103 | switch (note->type) | |
08a40648 AM |
10104 | { |
10105 | case NT_NETBSDCORE_FIRSTMACH+1: | |
10106 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
50b2bdb7 | 10107 | |
08a40648 AM |
10108 | case NT_NETBSDCORE_FIRSTMACH+3: |
10109 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
50b2bdb7 | 10110 | |
08a40648 AM |
10111 | default: |
10112 | return TRUE; | |
10113 | } | |
50b2bdb7 AM |
10114 | } |
10115 | /* NOTREACHED */ | |
10116 | } | |
10117 | ||
67cc5033 MK |
10118 | static bfd_boolean |
10119 | elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) | |
10120 | { | |
10121 | /* Signal number at offset 0x08. */ | |
228e534f | 10122 | elf_tdata (abfd)->core->signal |
67cc5033 MK |
10123 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); |
10124 | ||
10125 | /* Process ID at offset 0x20. */ | |
228e534f | 10126 | elf_tdata (abfd)->core->pid |
67cc5033 MK |
10127 | = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); |
10128 | ||
10129 | /* Command name at 0x48 (max 32 bytes, including nul). */ | |
228e534f | 10130 | elf_tdata (abfd)->core->command |
67cc5033 MK |
10131 | = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); |
10132 | ||
10133 | return TRUE; | |
10134 | } | |
10135 | ||
10136 | static bfd_boolean | |
10137 | elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) | |
10138 | { | |
10139 | if (note->type == NT_OPENBSD_PROCINFO) | |
10140 | return elfcore_grok_openbsd_procinfo (abfd, note); | |
10141 | ||
10142 | if (note->type == NT_OPENBSD_REGS) | |
10143 | return elfcore_make_note_pseudosection (abfd, ".reg", note); | |
10144 | ||
10145 | if (note->type == NT_OPENBSD_FPREGS) | |
10146 | return elfcore_make_note_pseudosection (abfd, ".reg2", note); | |
10147 | ||
10148 | if (note->type == NT_OPENBSD_XFPREGS) | |
10149 | return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); | |
10150 | ||
10151 | if (note->type == NT_OPENBSD_AUXV) | |
10152 | { | |
10153 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", | |
10154 | SEC_HAS_CONTENTS); | |
10155 | ||
10156 | if (sect == NULL) | |
10157 | return FALSE; | |
10158 | sect->size = note->descsz; | |
10159 | sect->filepos = note->descpos; | |
10160 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10161 | ||
10162 | return TRUE; | |
10163 | } | |
10164 | ||
10165 | if (note->type == NT_OPENBSD_WCOOKIE) | |
10166 | { | |
10167 | asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", | |
10168 | SEC_HAS_CONTENTS); | |
10169 | ||
10170 | if (sect == NULL) | |
10171 | return FALSE; | |
10172 | sect->size = note->descsz; | |
10173 | sect->filepos = note->descpos; | |
10174 | sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; | |
10175 | ||
10176 | return TRUE; | |
10177 | } | |
10178 | ||
10179 | return TRUE; | |
10180 | } | |
10181 | ||
07c6e936 | 10182 | static bfd_boolean |
d3fd4074 | 10183 | elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) |
07c6e936 NC |
10184 | { |
10185 | void *ddata = note->descdata; | |
10186 | char buf[100]; | |
10187 | char *name; | |
10188 | asection *sect; | |
f8843e87 AM |
10189 | short sig; |
10190 | unsigned flags; | |
07c6e936 NC |
10191 | |
10192 | /* nto_procfs_status 'pid' field is at offset 0. */ | |
228e534f | 10193 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); |
07c6e936 | 10194 | |
f8843e87 AM |
10195 | /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ |
10196 | *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); | |
10197 | ||
10198 | /* nto_procfs_status 'flags' field is at offset 8. */ | |
10199 | flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); | |
07c6e936 NC |
10200 | |
10201 | /* nto_procfs_status 'what' field is at offset 14. */ | |
f8843e87 AM |
10202 | if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) |
10203 | { | |
228e534f AM |
10204 | elf_tdata (abfd)->core->signal = sig; |
10205 | elf_tdata (abfd)->core->lwpid = *tid; | |
f8843e87 | 10206 | } |
07c6e936 | 10207 | |
f8843e87 AM |
10208 | /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores |
10209 | do not come from signals so we make sure we set the current | |
10210 | thread just in case. */ | |
10211 | if (flags & 0x00000080) | |
228e534f | 10212 | elf_tdata (abfd)->core->lwpid = *tid; |
07c6e936 NC |
10213 | |
10214 | /* Make a ".qnx_core_status/%d" section. */ | |
d3fd4074 | 10215 | sprintf (buf, ".qnx_core_status/%ld", *tid); |
07c6e936 | 10216 | |
a50b1753 | 10217 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10218 | if (name == NULL) |
10219 | return FALSE; | |
10220 | strcpy (name, buf); | |
10221 | ||
117ed4f8 | 10222 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10223 | if (sect == NULL) |
10224 | return FALSE; | |
10225 | ||
eea6121a | 10226 | sect->size = note->descsz; |
07c6e936 | 10227 | sect->filepos = note->descpos; |
07c6e936 NC |
10228 | sect->alignment_power = 2; |
10229 | ||
10230 | return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); | |
10231 | } | |
10232 | ||
10233 | static bfd_boolean | |
d69f560c KW |
10234 | elfcore_grok_nto_regs (bfd *abfd, |
10235 | Elf_Internal_Note *note, | |
d3fd4074 | 10236 | long tid, |
d69f560c | 10237 | char *base) |
07c6e936 NC |
10238 | { |
10239 | char buf[100]; | |
10240 | char *name; | |
10241 | asection *sect; | |
10242 | ||
d69f560c | 10243 | /* Make a "(base)/%d" section. */ |
d3fd4074 | 10244 | sprintf (buf, "%s/%ld", base, tid); |
07c6e936 | 10245 | |
a50b1753 | 10246 | name = (char *) bfd_alloc (abfd, strlen (buf) + 1); |
07c6e936 NC |
10247 | if (name == NULL) |
10248 | return FALSE; | |
10249 | strcpy (name, buf); | |
10250 | ||
117ed4f8 | 10251 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); |
07c6e936 NC |
10252 | if (sect == NULL) |
10253 | return FALSE; | |
10254 | ||
eea6121a | 10255 | sect->size = note->descsz; |
07c6e936 | 10256 | sect->filepos = note->descpos; |
07c6e936 NC |
10257 | sect->alignment_power = 2; |
10258 | ||
f8843e87 | 10259 | /* This is the current thread. */ |
228e534f | 10260 | if (elf_tdata (abfd)->core->lwpid == tid) |
d69f560c | 10261 | return elfcore_maybe_make_sect (abfd, base, sect); |
f8843e87 AM |
10262 | |
10263 | return TRUE; | |
07c6e936 NC |
10264 | } |
10265 | ||
10266 | #define BFD_QNT_CORE_INFO 7 | |
10267 | #define BFD_QNT_CORE_STATUS 8 | |
10268 | #define BFD_QNT_CORE_GREG 9 | |
10269 | #define BFD_QNT_CORE_FPREG 10 | |
10270 | ||
10271 | static bfd_boolean | |
217aa764 | 10272 | elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) |
07c6e936 NC |
10273 | { |
10274 | /* Every GREG section has a STATUS section before it. Store the | |
811072d8 | 10275 | tid from the previous call to pass down to the next gregs |
07c6e936 | 10276 | function. */ |
d3fd4074 | 10277 | static long tid = 1; |
07c6e936 NC |
10278 | |
10279 | switch (note->type) | |
10280 | { | |
d69f560c KW |
10281 | case BFD_QNT_CORE_INFO: |
10282 | return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); | |
10283 | case BFD_QNT_CORE_STATUS: | |
10284 | return elfcore_grok_nto_status (abfd, note, &tid); | |
10285 | case BFD_QNT_CORE_GREG: | |
10286 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); | |
10287 | case BFD_QNT_CORE_FPREG: | |
10288 | return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); | |
10289 | default: | |
10290 | return TRUE; | |
07c6e936 NC |
10291 | } |
10292 | } | |
10293 | ||
b15fa79e AM |
10294 | static bfd_boolean |
10295 | elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) | |
10296 | { | |
10297 | char *name; | |
10298 | asection *sect; | |
10299 | size_t len; | |
10300 | ||
10301 | /* Use note name as section name. */ | |
10302 | len = note->namesz; | |
a50b1753 | 10303 | name = (char *) bfd_alloc (abfd, len); |
b15fa79e AM |
10304 | if (name == NULL) |
10305 | return FALSE; | |
10306 | memcpy (name, note->namedata, len); | |
10307 | name[len - 1] = '\0'; | |
10308 | ||
10309 | sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); | |
10310 | if (sect == NULL) | |
10311 | return FALSE; | |
10312 | ||
10313 | sect->size = note->descsz; | |
10314 | sect->filepos = note->descpos; | |
10315 | sect->alignment_power = 1; | |
10316 | ||
10317 | return TRUE; | |
10318 | } | |
10319 | ||
7c76fa91 MS |
10320 | /* Function: elfcore_write_note |
10321 | ||
47d9a591 | 10322 | Inputs: |
a39f3346 | 10323 | buffer to hold note, and current size of buffer |
7c76fa91 MS |
10324 | name of note |
10325 | type of note | |
10326 | data for note | |
10327 | size of data for note | |
10328 | ||
a39f3346 AM |
10329 | Writes note to end of buffer. ELF64 notes are written exactly as |
10330 | for ELF32, despite the current (as of 2006) ELF gabi specifying | |
10331 | that they ought to have 8-byte namesz and descsz field, and have | |
10332 | 8-byte alignment. Other writers, eg. Linux kernel, do the same. | |
10333 | ||
7c76fa91 | 10334 | Return: |
a39f3346 | 10335 | Pointer to realloc'd buffer, *BUFSIZ updated. */ |
7c76fa91 MS |
10336 | |
10337 | char * | |
a39f3346 | 10338 | elfcore_write_note (bfd *abfd, |
217aa764 | 10339 | char *buf, |
a39f3346 | 10340 | int *bufsiz, |
217aa764 | 10341 | const char *name, |
a39f3346 | 10342 | int type, |
217aa764 | 10343 | const void *input, |
a39f3346 | 10344 | int size) |
7c76fa91 MS |
10345 | { |
10346 | Elf_External_Note *xnp; | |
d4c88bbb | 10347 | size_t namesz; |
d4c88bbb | 10348 | size_t newspace; |
a39f3346 | 10349 | char *dest; |
7c76fa91 | 10350 | |
d4c88bbb | 10351 | namesz = 0; |
d4c88bbb | 10352 | if (name != NULL) |
a39f3346 | 10353 | namesz = strlen (name) + 1; |
d4c88bbb | 10354 | |
a39f3346 | 10355 | newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); |
d4c88bbb | 10356 | |
a50b1753 | 10357 | buf = (char *) realloc (buf, *bufsiz + newspace); |
14b1c01e AM |
10358 | if (buf == NULL) |
10359 | return buf; | |
a39f3346 | 10360 | dest = buf + *bufsiz; |
7c76fa91 MS |
10361 | *bufsiz += newspace; |
10362 | xnp = (Elf_External_Note *) dest; | |
10363 | H_PUT_32 (abfd, namesz, xnp->namesz); | |
10364 | H_PUT_32 (abfd, size, xnp->descsz); | |
10365 | H_PUT_32 (abfd, type, xnp->type); | |
d4c88bbb AM |
10366 | dest = xnp->name; |
10367 | if (name != NULL) | |
10368 | { | |
10369 | memcpy (dest, name, namesz); | |
10370 | dest += namesz; | |
a39f3346 | 10371 | while (namesz & 3) |
d4c88bbb AM |
10372 | { |
10373 | *dest++ = '\0'; | |
a39f3346 | 10374 | ++namesz; |
d4c88bbb AM |
10375 | } |
10376 | } | |
10377 | memcpy (dest, input, size); | |
a39f3346 AM |
10378 | dest += size; |
10379 | while (size & 3) | |
10380 | { | |
10381 | *dest++ = '\0'; | |
10382 | ++size; | |
10383 | } | |
10384 | return buf; | |
7c76fa91 MS |
10385 | } |
10386 | ||
7c76fa91 | 10387 | char * |
217aa764 AM |
10388 | elfcore_write_prpsinfo (bfd *abfd, |
10389 | char *buf, | |
10390 | int *bufsiz, | |
10391 | const char *fname, | |
10392 | const char *psargs) | |
7c76fa91 | 10393 | { |
183e98be AM |
10394 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
10395 | ||
10396 | if (bed->elf_backend_write_core_note != NULL) | |
10397 | { | |
10398 | char *ret; | |
10399 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10400 | NT_PRPSINFO, fname, psargs); | |
10401 | if (ret != NULL) | |
10402 | return ret; | |
10403 | } | |
7c76fa91 | 10404 | |
1f20dca5 | 10405 | #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) |
183e98be AM |
10406 | #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) |
10407 | if (bed->s->elfclass == ELFCLASS32) | |
10408 | { | |
10409 | #if defined (HAVE_PSINFO32_T) | |
10410 | psinfo32_t data; | |
10411 | int note_type = NT_PSINFO; | |
10412 | #else | |
10413 | prpsinfo32_t data; | |
10414 | int note_type = NT_PRPSINFO; | |
10415 | #endif | |
10416 | ||
10417 | memset (&data, 0, sizeof (data)); | |
10418 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10419 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10420 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10421 | "CORE", note_type, &data, sizeof (data)); |
183e98be AM |
10422 | } |
10423 | else | |
10424 | #endif | |
10425 | { | |
7c76fa91 | 10426 | #if defined (HAVE_PSINFO_T) |
183e98be AM |
10427 | psinfo_t data; |
10428 | int note_type = NT_PSINFO; | |
7c76fa91 | 10429 | #else |
183e98be AM |
10430 | prpsinfo_t data; |
10431 | int note_type = NT_PRPSINFO; | |
7c76fa91 MS |
10432 | #endif |
10433 | ||
183e98be AM |
10434 | memset (&data, 0, sizeof (data)); |
10435 | strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); | |
10436 | strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); | |
10437 | return elfcore_write_note (abfd, buf, bufsiz, | |
1f20dca5 | 10438 | "CORE", note_type, &data, sizeof (data)); |
183e98be | 10439 | } |
7c76fa91 MS |
10440 | #endif /* PSINFO_T or PRPSINFO_T */ |
10441 | ||
1f20dca5 UW |
10442 | free (buf); |
10443 | return NULL; | |
10444 | } | |
10445 | ||
70a38d42 SDJ |
10446 | char * |
10447 | elfcore_write_linux_prpsinfo32 | |
10448 | (bfd *abfd, char *buf, int *bufsiz, | |
10449 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10450 | { | |
10451 | struct elf_external_linux_prpsinfo32 data; | |
10452 | ||
aeb70569 | 10453 | swap_linux_prpsinfo32_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10454 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, |
10455 | &data, sizeof (data)); | |
10456 | } | |
10457 | ||
10458 | char * | |
10459 | elfcore_write_linux_prpsinfo64 | |
10460 | (bfd *abfd, char *buf, int *bufsiz, | |
10461 | const struct elf_internal_linux_prpsinfo *prpsinfo) | |
10462 | { | |
10463 | struct elf_external_linux_prpsinfo64 data; | |
10464 | ||
aeb70569 | 10465 | swap_linux_prpsinfo64_out (abfd, prpsinfo, &data); |
70a38d42 SDJ |
10466 | return elfcore_write_note (abfd, buf, bufsiz, |
10467 | "CORE", NT_PRPSINFO, &data, sizeof (data)); | |
10468 | } | |
10469 | ||
7c76fa91 | 10470 | char * |
217aa764 AM |
10471 | elfcore_write_prstatus (bfd *abfd, |
10472 | char *buf, | |
10473 | int *bufsiz, | |
10474 | long pid, | |
10475 | int cursig, | |
10476 | const void *gregs) | |
7c76fa91 | 10477 | { |
183e98be | 10478 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
7c76fa91 | 10479 | |
183e98be AM |
10480 | if (bed->elf_backend_write_core_note != NULL) |
10481 | { | |
10482 | char *ret; | |
10483 | ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, | |
10484 | NT_PRSTATUS, | |
10485 | pid, cursig, gregs); | |
10486 | if (ret != NULL) | |
10487 | return ret; | |
10488 | } | |
10489 | ||
1f20dca5 | 10490 | #if defined (HAVE_PRSTATUS_T) |
183e98be AM |
10491 | #if defined (HAVE_PRSTATUS32_T) |
10492 | if (bed->s->elfclass == ELFCLASS32) | |
10493 | { | |
10494 | prstatus32_t prstat; | |
10495 | ||
10496 | memset (&prstat, 0, sizeof (prstat)); | |
10497 | prstat.pr_pid = pid; | |
10498 | prstat.pr_cursig = cursig; | |
10499 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10500 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10501 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10502 | } | |
10503 | else | |
10504 | #endif | |
10505 | { | |
10506 | prstatus_t prstat; | |
10507 | ||
10508 | memset (&prstat, 0, sizeof (prstat)); | |
10509 | prstat.pr_pid = pid; | |
10510 | prstat.pr_cursig = cursig; | |
10511 | memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); | |
1f20dca5 | 10512 | return elfcore_write_note (abfd, buf, bufsiz, "CORE", |
183e98be AM |
10513 | NT_PRSTATUS, &prstat, sizeof (prstat)); |
10514 | } | |
7c76fa91 MS |
10515 | #endif /* HAVE_PRSTATUS_T */ |
10516 | ||
1f20dca5 UW |
10517 | free (buf); |
10518 | return NULL; | |
10519 | } | |
10520 | ||
51316059 MS |
10521 | #if defined (HAVE_LWPSTATUS_T) |
10522 | char * | |
217aa764 AM |
10523 | elfcore_write_lwpstatus (bfd *abfd, |
10524 | char *buf, | |
10525 | int *bufsiz, | |
10526 | long pid, | |
10527 | int cursig, | |
10528 | const void *gregs) | |
51316059 MS |
10529 | { |
10530 | lwpstatus_t lwpstat; | |
183e98be | 10531 | const char *note_name = "CORE"; |
51316059 MS |
10532 | |
10533 | memset (&lwpstat, 0, sizeof (lwpstat)); | |
10534 | lwpstat.pr_lwpid = pid >> 16; | |
10535 | lwpstat.pr_cursig = cursig; | |
10536 | #if defined (HAVE_LWPSTATUS_T_PR_REG) | |
d1e8523e | 10537 | memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); |
51316059 MS |
10538 | #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) |
10539 | #if !defined(gregs) | |
10540 | memcpy (lwpstat.pr_context.uc_mcontext.gregs, | |
10541 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); | |
10542 | #else | |
10543 | memcpy (lwpstat.pr_context.uc_mcontext.__gregs, | |
10544 | gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); | |
10545 | #endif | |
10546 | #endif | |
47d9a591 | 10547 | return elfcore_write_note (abfd, buf, bufsiz, note_name, |
51316059 MS |
10548 | NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); |
10549 | } | |
10550 | #endif /* HAVE_LWPSTATUS_T */ | |
10551 | ||
7c76fa91 MS |
10552 | #if defined (HAVE_PSTATUS_T) |
10553 | char * | |
217aa764 AM |
10554 | elfcore_write_pstatus (bfd *abfd, |
10555 | char *buf, | |
10556 | int *bufsiz, | |
10557 | long pid, | |
6c10990d NC |
10558 | int cursig ATTRIBUTE_UNUSED, |
10559 | const void *gregs ATTRIBUTE_UNUSED) | |
7c76fa91 | 10560 | { |
183e98be AM |
10561 | const char *note_name = "CORE"; |
10562 | #if defined (HAVE_PSTATUS32_T) | |
10563 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
7c76fa91 | 10564 | |
183e98be AM |
10565 | if (bed->s->elfclass == ELFCLASS32) |
10566 | { | |
10567 | pstatus32_t pstat; | |
10568 | ||
10569 | memset (&pstat, 0, sizeof (pstat)); | |
10570 | pstat.pr_pid = pid & 0xffff; | |
10571 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10572 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10573 | return buf; | |
10574 | } | |
10575 | else | |
10576 | #endif | |
10577 | { | |
10578 | pstatus_t pstat; | |
10579 | ||
10580 | memset (&pstat, 0, sizeof (pstat)); | |
10581 | pstat.pr_pid = pid & 0xffff; | |
10582 | buf = elfcore_write_note (abfd, buf, bufsiz, note_name, | |
10583 | NT_PSTATUS, &pstat, sizeof (pstat)); | |
10584 | return buf; | |
10585 | } | |
7c76fa91 MS |
10586 | } |
10587 | #endif /* HAVE_PSTATUS_T */ | |
10588 | ||
10589 | char * | |
217aa764 AM |
10590 | elfcore_write_prfpreg (bfd *abfd, |
10591 | char *buf, | |
10592 | int *bufsiz, | |
10593 | const void *fpregs, | |
10594 | int size) | |
7c76fa91 | 10595 | { |
183e98be | 10596 | const char *note_name = "CORE"; |
47d9a591 | 10597 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10598 | note_name, NT_FPREGSET, fpregs, size); |
10599 | } | |
10600 | ||
10601 | char * | |
217aa764 AM |
10602 | elfcore_write_prxfpreg (bfd *abfd, |
10603 | char *buf, | |
10604 | int *bufsiz, | |
10605 | const void *xfpregs, | |
10606 | int size) | |
7c76fa91 MS |
10607 | { |
10608 | char *note_name = "LINUX"; | |
47d9a591 | 10609 | return elfcore_write_note (abfd, buf, bufsiz, |
7c76fa91 MS |
10610 | note_name, NT_PRXFPREG, xfpregs, size); |
10611 | } | |
10612 | ||
4339cae0 L |
10613 | char * |
10614 | elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, | |
10615 | const void *xfpregs, int size) | |
10616 | { | |
97de3545 JB |
10617 | char *note_name; |
10618 | if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) | |
10619 | note_name = "FreeBSD"; | |
10620 | else | |
10621 | note_name = "LINUX"; | |
4339cae0 L |
10622 | return elfcore_write_note (abfd, buf, bufsiz, |
10623 | note_name, NT_X86_XSTATE, xfpregs, size); | |
10624 | } | |
10625 | ||
97753bd5 AM |
10626 | char * |
10627 | elfcore_write_ppc_vmx (bfd *abfd, | |
10628 | char *buf, | |
10629 | int *bufsiz, | |
10630 | const void *ppc_vmx, | |
10631 | int size) | |
10632 | { | |
10633 | char *note_name = "LINUX"; | |
10634 | return elfcore_write_note (abfd, buf, bufsiz, | |
10635 | note_name, NT_PPC_VMX, ppc_vmx, size); | |
10636 | } | |
10637 | ||
89eeb0bc LM |
10638 | char * |
10639 | elfcore_write_ppc_vsx (bfd *abfd, | |
10640 | char *buf, | |
10641 | int *bufsiz, | |
10642 | const void *ppc_vsx, | |
10643 | int size) | |
10644 | { | |
10645 | char *note_name = "LINUX"; | |
10646 | return elfcore_write_note (abfd, buf, bufsiz, | |
10647 | note_name, NT_PPC_VSX, ppc_vsx, size); | |
10648 | } | |
10649 | ||
0675e188 UW |
10650 | static char * |
10651 | elfcore_write_s390_high_gprs (bfd *abfd, | |
10652 | char *buf, | |
10653 | int *bufsiz, | |
10654 | const void *s390_high_gprs, | |
10655 | int size) | |
10656 | { | |
10657 | char *note_name = "LINUX"; | |
10658 | return elfcore_write_note (abfd, buf, bufsiz, | |
10659 | note_name, NT_S390_HIGH_GPRS, | |
10660 | s390_high_gprs, size); | |
10661 | } | |
10662 | ||
d7eeb400 MS |
10663 | char * |
10664 | elfcore_write_s390_timer (bfd *abfd, | |
10665 | char *buf, | |
10666 | int *bufsiz, | |
10667 | const void *s390_timer, | |
10668 | int size) | |
10669 | { | |
10670 | char *note_name = "LINUX"; | |
10671 | return elfcore_write_note (abfd, buf, bufsiz, | |
10672 | note_name, NT_S390_TIMER, s390_timer, size); | |
10673 | } | |
10674 | ||
10675 | char * | |
10676 | elfcore_write_s390_todcmp (bfd *abfd, | |
10677 | char *buf, | |
10678 | int *bufsiz, | |
10679 | const void *s390_todcmp, | |
10680 | int size) | |
10681 | { | |
10682 | char *note_name = "LINUX"; | |
10683 | return elfcore_write_note (abfd, buf, bufsiz, | |
10684 | note_name, NT_S390_TODCMP, s390_todcmp, size); | |
10685 | } | |
10686 | ||
10687 | char * | |
10688 | elfcore_write_s390_todpreg (bfd *abfd, | |
10689 | char *buf, | |
10690 | int *bufsiz, | |
10691 | const void *s390_todpreg, | |
10692 | int size) | |
10693 | { | |
10694 | char *note_name = "LINUX"; | |
10695 | return elfcore_write_note (abfd, buf, bufsiz, | |
10696 | note_name, NT_S390_TODPREG, s390_todpreg, size); | |
10697 | } | |
10698 | ||
10699 | char * | |
10700 | elfcore_write_s390_ctrs (bfd *abfd, | |
10701 | char *buf, | |
10702 | int *bufsiz, | |
10703 | const void *s390_ctrs, | |
10704 | int size) | |
10705 | { | |
10706 | char *note_name = "LINUX"; | |
10707 | return elfcore_write_note (abfd, buf, bufsiz, | |
10708 | note_name, NT_S390_CTRS, s390_ctrs, size); | |
10709 | } | |
10710 | ||
10711 | char * | |
10712 | elfcore_write_s390_prefix (bfd *abfd, | |
10713 | char *buf, | |
10714 | int *bufsiz, | |
10715 | const void *s390_prefix, | |
10716 | int size) | |
10717 | { | |
10718 | char *note_name = "LINUX"; | |
10719 | return elfcore_write_note (abfd, buf, bufsiz, | |
10720 | note_name, NT_S390_PREFIX, s390_prefix, size); | |
10721 | } | |
10722 | ||
355b81d9 UW |
10723 | char * |
10724 | elfcore_write_s390_last_break (bfd *abfd, | |
10725 | char *buf, | |
10726 | int *bufsiz, | |
10727 | const void *s390_last_break, | |
10728 | int size) | |
10729 | { | |
10730 | char *note_name = "LINUX"; | |
10731 | return elfcore_write_note (abfd, buf, bufsiz, | |
10732 | note_name, NT_S390_LAST_BREAK, | |
10733 | s390_last_break, size); | |
10734 | } | |
10735 | ||
10736 | char * | |
10737 | elfcore_write_s390_system_call (bfd *abfd, | |
10738 | char *buf, | |
10739 | int *bufsiz, | |
10740 | const void *s390_system_call, | |
10741 | int size) | |
10742 | { | |
10743 | char *note_name = "LINUX"; | |
10744 | return elfcore_write_note (abfd, buf, bufsiz, | |
10745 | note_name, NT_S390_SYSTEM_CALL, | |
10746 | s390_system_call, size); | |
10747 | } | |
10748 | ||
abb3f6cc NC |
10749 | char * |
10750 | elfcore_write_s390_tdb (bfd *abfd, | |
10751 | char *buf, | |
10752 | int *bufsiz, | |
10753 | const void *s390_tdb, | |
10754 | int size) | |
10755 | { | |
10756 | char *note_name = "LINUX"; | |
10757 | return elfcore_write_note (abfd, buf, bufsiz, | |
10758 | note_name, NT_S390_TDB, s390_tdb, size); | |
10759 | } | |
10760 | ||
4ef9f41a AA |
10761 | char * |
10762 | elfcore_write_s390_vxrs_low (bfd *abfd, | |
10763 | char *buf, | |
10764 | int *bufsiz, | |
10765 | const void *s390_vxrs_low, | |
10766 | int size) | |
10767 | { | |
10768 | char *note_name = "LINUX"; | |
10769 | return elfcore_write_note (abfd, buf, bufsiz, | |
10770 | note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); | |
10771 | } | |
10772 | ||
10773 | char * | |
10774 | elfcore_write_s390_vxrs_high (bfd *abfd, | |
10775 | char *buf, | |
10776 | int *bufsiz, | |
10777 | const void *s390_vxrs_high, | |
10778 | int size) | |
10779 | { | |
10780 | char *note_name = "LINUX"; | |
10781 | return elfcore_write_note (abfd, buf, bufsiz, | |
10782 | note_name, NT_S390_VXRS_HIGH, | |
10783 | s390_vxrs_high, size); | |
10784 | } | |
10785 | ||
88ab90e8 AA |
10786 | char * |
10787 | elfcore_write_s390_gs_cb (bfd *abfd, | |
10788 | char *buf, | |
10789 | int *bufsiz, | |
10790 | const void *s390_gs_cb, | |
10791 | int size) | |
10792 | { | |
10793 | char *note_name = "LINUX"; | |
10794 | return elfcore_write_note (abfd, buf, bufsiz, | |
10795 | note_name, NT_S390_GS_CB, | |
10796 | s390_gs_cb, size); | |
10797 | } | |
10798 | ||
10799 | char * | |
10800 | elfcore_write_s390_gs_bc (bfd *abfd, | |
10801 | char *buf, | |
10802 | int *bufsiz, | |
10803 | const void *s390_gs_bc, | |
10804 | int size) | |
10805 | { | |
10806 | char *note_name = "LINUX"; | |
10807 | return elfcore_write_note (abfd, buf, bufsiz, | |
10808 | note_name, NT_S390_GS_BC, | |
10809 | s390_gs_bc, size); | |
10810 | } | |
10811 | ||
faa9a424 UW |
10812 | char * |
10813 | elfcore_write_arm_vfp (bfd *abfd, | |
10814 | char *buf, | |
10815 | int *bufsiz, | |
10816 | const void *arm_vfp, | |
10817 | int size) | |
10818 | { | |
10819 | char *note_name = "LINUX"; | |
10820 | return elfcore_write_note (abfd, buf, bufsiz, | |
10821 | note_name, NT_ARM_VFP, arm_vfp, size); | |
10822 | } | |
10823 | ||
652451f8 YZ |
10824 | char * |
10825 | elfcore_write_aarch_tls (bfd *abfd, | |
10826 | char *buf, | |
10827 | int *bufsiz, | |
10828 | const void *aarch_tls, | |
10829 | int size) | |
10830 | { | |
10831 | char *note_name = "LINUX"; | |
10832 | return elfcore_write_note (abfd, buf, bufsiz, | |
10833 | note_name, NT_ARM_TLS, aarch_tls, size); | |
10834 | } | |
10835 | ||
10836 | char * | |
10837 | elfcore_write_aarch_hw_break (bfd *abfd, | |
10838 | char *buf, | |
10839 | int *bufsiz, | |
10840 | const void *aarch_hw_break, | |
10841 | int size) | |
10842 | { | |
10843 | char *note_name = "LINUX"; | |
10844 | return elfcore_write_note (abfd, buf, bufsiz, | |
10845 | note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); | |
10846 | } | |
10847 | ||
10848 | char * | |
10849 | elfcore_write_aarch_hw_watch (bfd *abfd, | |
10850 | char *buf, | |
10851 | int *bufsiz, | |
10852 | const void *aarch_hw_watch, | |
10853 | int size) | |
10854 | { | |
10855 | char *note_name = "LINUX"; | |
10856 | return elfcore_write_note (abfd, buf, bufsiz, | |
10857 | note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); | |
10858 | } | |
10859 | ||
bb864ac1 CES |
10860 | char * |
10861 | elfcore_write_register_note (bfd *abfd, | |
10862 | char *buf, | |
10863 | int *bufsiz, | |
10864 | const char *section, | |
10865 | const void *data, | |
10866 | int size) | |
10867 | { | |
10868 | if (strcmp (section, ".reg2") == 0) | |
10869 | return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); | |
10870 | if (strcmp (section, ".reg-xfp") == 0) | |
10871 | return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); | |
4339cae0 L |
10872 | if (strcmp (section, ".reg-xstate") == 0) |
10873 | return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10874 | if (strcmp (section, ".reg-ppc-vmx") == 0) |
10875 | return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); | |
89eeb0bc LM |
10876 | if (strcmp (section, ".reg-ppc-vsx") == 0) |
10877 | return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); | |
0675e188 UW |
10878 | if (strcmp (section, ".reg-s390-high-gprs") == 0) |
10879 | return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); | |
d7eeb400 MS |
10880 | if (strcmp (section, ".reg-s390-timer") == 0) |
10881 | return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); | |
10882 | if (strcmp (section, ".reg-s390-todcmp") == 0) | |
10883 | return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); | |
10884 | if (strcmp (section, ".reg-s390-todpreg") == 0) | |
10885 | return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); | |
10886 | if (strcmp (section, ".reg-s390-ctrs") == 0) | |
10887 | return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); | |
10888 | if (strcmp (section, ".reg-s390-prefix") == 0) | |
10889 | return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); | |
355b81d9 UW |
10890 | if (strcmp (section, ".reg-s390-last-break") == 0) |
10891 | return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); | |
10892 | if (strcmp (section, ".reg-s390-system-call") == 0) | |
10893 | return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); | |
abb3f6cc NC |
10894 | if (strcmp (section, ".reg-s390-tdb") == 0) |
10895 | return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); | |
4ef9f41a AA |
10896 | if (strcmp (section, ".reg-s390-vxrs-low") == 0) |
10897 | return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); | |
10898 | if (strcmp (section, ".reg-s390-vxrs-high") == 0) | |
10899 | return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); | |
88ab90e8 AA |
10900 | if (strcmp (section, ".reg-s390-gs-cb") == 0) |
10901 | return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); | |
10902 | if (strcmp (section, ".reg-s390-gs-bc") == 0) | |
10903 | return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); | |
faa9a424 UW |
10904 | if (strcmp (section, ".reg-arm-vfp") == 0) |
10905 | return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); | |
652451f8 YZ |
10906 | if (strcmp (section, ".reg-aarch-tls") == 0) |
10907 | return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); | |
10908 | if (strcmp (section, ".reg-aarch-hw-break") == 0) | |
10909 | return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); | |
10910 | if (strcmp (section, ".reg-aarch-hw-watch") == 0) | |
10911 | return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); | |
bb864ac1 CES |
10912 | return NULL; |
10913 | } | |
10914 | ||
b34976b6 | 10915 | static bfd_boolean |
718175fa | 10916 | elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset) |
252b5132 | 10917 | { |
c044fabd | 10918 | char *p; |
252b5132 | 10919 | |
252b5132 RH |
10920 | p = buf; |
10921 | while (p < buf + size) | |
10922 | { | |
c044fabd KH |
10923 | /* FIXME: bad alignment assumption. */ |
10924 | Elf_External_Note *xnp = (Elf_External_Note *) p; | |
252b5132 RH |
10925 | Elf_Internal_Note in; |
10926 | ||
baea7ef1 AM |
10927 | if (offsetof (Elf_External_Note, name) > buf - p + size) |
10928 | return FALSE; | |
10929 | ||
dc810e39 | 10930 | in.type = H_GET_32 (abfd, xnp->type); |
252b5132 | 10931 | |
dc810e39 | 10932 | in.namesz = H_GET_32 (abfd, xnp->namesz); |
252b5132 | 10933 | in.namedata = xnp->name; |
baea7ef1 AM |
10934 | if (in.namesz > buf - in.namedata + size) |
10935 | return FALSE; | |
252b5132 | 10936 | |
dc810e39 | 10937 | in.descsz = H_GET_32 (abfd, xnp->descsz); |
252b5132 RH |
10938 | in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); |
10939 | in.descpos = offset + (in.descdata - buf); | |
baea7ef1 AM |
10940 | if (in.descsz != 0 |
10941 | && (in.descdata >= buf + size | |
10942 | || in.descsz > buf - in.descdata + size)) | |
10943 | return FALSE; | |
252b5132 | 10944 | |
718175fa JK |
10945 | switch (bfd_get_format (abfd)) |
10946 | { | |
10947 | default: | |
10948 | return TRUE; | |
10949 | ||
10950 | case bfd_core: | |
f64e188b | 10951 | { |
8acbedd6 | 10952 | #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} |
f64e188b | 10953 | struct |
718175fa | 10954 | { |
f64e188b | 10955 | const char * string; |
8acbedd6 | 10956 | size_t len; |
f64e188b | 10957 | bfd_boolean (* func)(bfd *, Elf_Internal_Note *); |
718175fa | 10958 | } |
f64e188b | 10959 | grokers[] = |
b15fa79e | 10960 | { |
8acbedd6 | 10961 | GROKER_ELEMENT ("", elfcore_grok_note), |
aa1ed4a9 | 10962 | GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), |
8acbedd6 KS |
10963 | GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), |
10964 | GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), | |
10965 | GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), | |
10966 | GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note) | |
f64e188b | 10967 | }; |
8acbedd6 | 10968 | #undef GROKER_ELEMENT |
f64e188b NC |
10969 | int i; |
10970 | ||
10971 | for (i = ARRAY_SIZE (grokers); i--;) | |
8acbedd6 KS |
10972 | { |
10973 | if (in.namesz >= grokers[i].len | |
10974 | && strncmp (in.namedata, grokers[i].string, | |
10975 | grokers[i].len) == 0) | |
10976 | { | |
10977 | if (! grokers[i].func (abfd, & in)) | |
10978 | return FALSE; | |
10979 | break; | |
10980 | } | |
10981 | } | |
f64e188b NC |
10982 | break; |
10983 | } | |
718175fa JK |
10984 | |
10985 | case bfd_object: | |
10986 | if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) | |
10987 | { | |
10988 | if (! elfobj_grok_gnu_note (abfd, &in)) | |
10989 | return FALSE; | |
10990 | } | |
e21e5835 NC |
10991 | else if (in.namesz == sizeof "stapsdt" |
10992 | && strcmp (in.namedata, "stapsdt") == 0) | |
10993 | { | |
10994 | if (! elfobj_grok_stapsdt_note (abfd, &in)) | |
10995 | return FALSE; | |
10996 | } | |
718175fa | 10997 | break; |
08a40648 | 10998 | } |
252b5132 RH |
10999 | |
11000 | p = in.descdata + BFD_ALIGN (in.descsz, 4); | |
11001 | } | |
11002 | ||
718175fa JK |
11003 | return TRUE; |
11004 | } | |
11005 | ||
11006 | static bfd_boolean | |
11007 | elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) | |
11008 | { | |
11009 | char *buf; | |
11010 | ||
957e1fc1 | 11011 | if (size == 0 || (size + 1) == 0) |
718175fa JK |
11012 | return TRUE; |
11013 | ||
11014 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) | |
11015 | return FALSE; | |
11016 | ||
f64e188b | 11017 | buf = (char *) bfd_malloc (size + 1); |
718175fa JK |
11018 | if (buf == NULL) |
11019 | return FALSE; | |
11020 | ||
f64e188b NC |
11021 | /* PR 17512: file: ec08f814 |
11022 | 0-termintate the buffer so that string searches will not overflow. */ | |
11023 | buf[size] = 0; | |
11024 | ||
718175fa JK |
11025 | if (bfd_bread (buf, size, abfd) != size |
11026 | || !elf_parse_notes (abfd, buf, size, offset)) | |
11027 | { | |
11028 | free (buf); | |
11029 | return FALSE; | |
11030 | } | |
11031 | ||
252b5132 | 11032 | free (buf); |
b34976b6 | 11033 | return TRUE; |
252b5132 | 11034 | } |
98d8431c JB |
11035 | \f |
11036 | /* Providing external access to the ELF program header table. */ | |
11037 | ||
11038 | /* Return an upper bound on the number of bytes required to store a | |
11039 | copy of ABFD's program header table entries. Return -1 if an error | |
11040 | occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11041 | |
98d8431c | 11042 | long |
217aa764 | 11043 | bfd_get_elf_phdr_upper_bound (bfd *abfd) |
98d8431c JB |
11044 | { |
11045 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11046 | { | |
11047 | bfd_set_error (bfd_error_wrong_format); | |
11048 | return -1; | |
11049 | } | |
11050 | ||
936e320b | 11051 | return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); |
98d8431c JB |
11052 | } |
11053 | ||
98d8431c JB |
11054 | /* Copy ABFD's program header table entries to *PHDRS. The entries |
11055 | will be stored as an array of Elf_Internal_Phdr structures, as | |
11056 | defined in include/elf/internal.h. To find out how large the | |
11057 | buffer needs to be, call bfd_get_elf_phdr_upper_bound. | |
11058 | ||
11059 | Return the number of program header table entries read, or -1 if an | |
11060 | error occurs; bfd_get_error will return an appropriate code. */ | |
c044fabd | 11061 | |
98d8431c | 11062 | int |
217aa764 | 11063 | bfd_get_elf_phdrs (bfd *abfd, void *phdrs) |
98d8431c JB |
11064 | { |
11065 | int num_phdrs; | |
11066 | ||
11067 | if (abfd->xvec->flavour != bfd_target_elf_flavour) | |
11068 | { | |
11069 | bfd_set_error (bfd_error_wrong_format); | |
11070 | return -1; | |
11071 | } | |
11072 | ||
11073 | num_phdrs = elf_elfheader (abfd)->e_phnum; | |
c044fabd | 11074 | memcpy (phdrs, elf_tdata (abfd)->phdr, |
98d8431c JB |
11075 | num_phdrs * sizeof (Elf_Internal_Phdr)); |
11076 | ||
11077 | return num_phdrs; | |
11078 | } | |
ae4221d7 | 11079 | |
db6751f2 | 11080 | enum elf_reloc_type_class |
7e612e98 AM |
11081 | _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
11082 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
11083 | const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) | |
db6751f2 JJ |
11084 | { |
11085 | return reloc_class_normal; | |
11086 | } | |
f8df10f4 | 11087 | |
47d9a591 | 11088 | /* For RELA architectures, return the relocation value for a |
f8df10f4 JJ |
11089 | relocation against a local symbol. */ |
11090 | ||
11091 | bfd_vma | |
217aa764 AM |
11092 | _bfd_elf_rela_local_sym (bfd *abfd, |
11093 | Elf_Internal_Sym *sym, | |
8517fae7 | 11094 | asection **psec, |
217aa764 | 11095 | Elf_Internal_Rela *rel) |
f8df10f4 | 11096 | { |
8517fae7 | 11097 | asection *sec = *psec; |
f8df10f4 JJ |
11098 | bfd_vma relocation; |
11099 | ||
11100 | relocation = (sec->output_section->vma | |
11101 | + sec->output_offset | |
11102 | + sym->st_value); | |
11103 | if ((sec->flags & SEC_MERGE) | |
c629eae0 | 11104 | && ELF_ST_TYPE (sym->st_info) == STT_SECTION |
dbaa2011 | 11105 | && sec->sec_info_type == SEC_INFO_TYPE_MERGE) |
f8df10f4 | 11106 | { |
f8df10f4 | 11107 | rel->r_addend = |
8517fae7 | 11108 | _bfd_merged_section_offset (abfd, psec, |
65765700 | 11109 | elf_section_data (sec)->sec_info, |
753731ee AM |
11110 | sym->st_value + rel->r_addend); |
11111 | if (sec != *psec) | |
11112 | { | |
11113 | /* If we have changed the section, and our original section is | |
11114 | marked with SEC_EXCLUDE, it means that the original | |
11115 | SEC_MERGE section has been completely subsumed in some | |
11116 | other SEC_MERGE section. In this case, we need to leave | |
11117 | some info around for --emit-relocs. */ | |
11118 | if ((sec->flags & SEC_EXCLUDE) != 0) | |
11119 | sec->kept_section = *psec; | |
11120 | sec = *psec; | |
11121 | } | |
8517fae7 AM |
11122 | rel->r_addend -= relocation; |
11123 | rel->r_addend += sec->output_section->vma + sec->output_offset; | |
f8df10f4 JJ |
11124 | } |
11125 | return relocation; | |
11126 | } | |
c629eae0 JJ |
11127 | |
11128 | bfd_vma | |
217aa764 AM |
11129 | _bfd_elf_rel_local_sym (bfd *abfd, |
11130 | Elf_Internal_Sym *sym, | |
11131 | asection **psec, | |
11132 | bfd_vma addend) | |
47d9a591 | 11133 | { |
c629eae0 JJ |
11134 | asection *sec = *psec; |
11135 | ||
dbaa2011 | 11136 | if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) |
c629eae0 JJ |
11137 | return sym->st_value + addend; |
11138 | ||
11139 | return _bfd_merged_section_offset (abfd, psec, | |
65765700 | 11140 | elf_section_data (sec)->sec_info, |
753731ee | 11141 | sym->st_value + addend); |
c629eae0 JJ |
11142 | } |
11143 | ||
37b01f6a DG |
11144 | /* Adjust an address within a section. Given OFFSET within SEC, return |
11145 | the new offset within the section, based upon changes made to the | |
11146 | section. Returns -1 if the offset is now invalid. | |
11147 | The offset (in abnd out) is in target sized bytes, however big a | |
11148 | byte may be. */ | |
11149 | ||
c629eae0 | 11150 | bfd_vma |
217aa764 | 11151 | _bfd_elf_section_offset (bfd *abfd, |
92e4ec35 | 11152 | struct bfd_link_info *info, |
217aa764 AM |
11153 | asection *sec, |
11154 | bfd_vma offset) | |
c629eae0 | 11155 | { |
68bfbfcc | 11156 | switch (sec->sec_info_type) |
65765700 | 11157 | { |
dbaa2011 | 11158 | case SEC_INFO_TYPE_STABS: |
eea6121a AM |
11159 | return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, |
11160 | offset); | |
dbaa2011 | 11161 | case SEC_INFO_TYPE_EH_FRAME: |
92e4ec35 | 11162 | return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); |
37b01f6a | 11163 | |
65765700 | 11164 | default: |
310fd250 L |
11165 | if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) |
11166 | { | |
37b01f6a | 11167 | /* Reverse the offset. */ |
310fd250 L |
11168 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); |
11169 | bfd_size_type address_size = bed->s->arch_size / 8; | |
37b01f6a DG |
11170 | |
11171 | /* address_size and sec->size are in octets. Convert | |
11172 | to bytes before subtracting the original offset. */ | |
11173 | offset = (sec->size - address_size) / bfd_octets_per_byte (abfd) - offset; | |
310fd250 | 11174 | } |
65765700 JJ |
11175 | return offset; |
11176 | } | |
c629eae0 | 11177 | } |
3333a7c3 RM |
11178 | \f |
11179 | /* Create a new BFD as if by bfd_openr. Rather than opening a file, | |
11180 | reconstruct an ELF file by reading the segments out of remote memory | |
11181 | based on the ELF file header at EHDR_VMA and the ELF program headers it | |
11182 | points to. If not null, *LOADBASEP is filled in with the difference | |
11183 | between the VMAs from which the segments were read, and the VMAs the | |
11184 | file headers (and hence BFD's idea of each section's VMA) put them at. | |
11185 | ||
11186 | The function TARGET_READ_MEMORY is called to copy LEN bytes from the | |
11187 | remote memory at target address VMA into the local buffer at MYADDR; it | |
11188 | should return zero on success or an `errno' code on failure. TEMPL must | |
11189 | be a BFD for an ELF target with the word size and byte order found in | |
11190 | the remote memory. */ | |
11191 | ||
11192 | bfd * | |
217aa764 AM |
11193 | bfd_elf_bfd_from_remote_memory |
11194 | (bfd *templ, | |
11195 | bfd_vma ehdr_vma, | |
f0a5d95a | 11196 | bfd_size_type size, |
217aa764 | 11197 | bfd_vma *loadbasep, |
fe78531d | 11198 | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
3333a7c3 RM |
11199 | { |
11200 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) | |
5979d6b6 | 11201 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
3333a7c3 | 11202 | } |
4c45e5c9 JJ |
11203 | \f |
11204 | long | |
c9727e01 AM |
11205 | _bfd_elf_get_synthetic_symtab (bfd *abfd, |
11206 | long symcount ATTRIBUTE_UNUSED, | |
11207 | asymbol **syms ATTRIBUTE_UNUSED, | |
8615f3f2 | 11208 | long dynsymcount, |
c9727e01 AM |
11209 | asymbol **dynsyms, |
11210 | asymbol **ret) | |
4c45e5c9 JJ |
11211 | { |
11212 | const struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
11213 | asection *relplt; | |
11214 | asymbol *s; | |
11215 | const char *relplt_name; | |
11216 | bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); | |
11217 | arelent *p; | |
11218 | long count, i, n; | |
11219 | size_t size; | |
11220 | Elf_Internal_Shdr *hdr; | |
11221 | char *names; | |
11222 | asection *plt; | |
11223 | ||
8615f3f2 AM |
11224 | *ret = NULL; |
11225 | ||
90e3cdf2 JJ |
11226 | if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) |
11227 | return 0; | |
11228 | ||
8615f3f2 AM |
11229 | if (dynsymcount <= 0) |
11230 | return 0; | |
11231 | ||
4c45e5c9 JJ |
11232 | if (!bed->plt_sym_val) |
11233 | return 0; | |
11234 | ||
11235 | relplt_name = bed->relplt_name; | |
11236 | if (relplt_name == NULL) | |
d35fd659 | 11237 | relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; |
4c45e5c9 JJ |
11238 | relplt = bfd_get_section_by_name (abfd, relplt_name); |
11239 | if (relplt == NULL) | |
11240 | return 0; | |
11241 | ||
11242 | hdr = &elf_section_data (relplt)->this_hdr; | |
11243 | if (hdr->sh_link != elf_dynsymtab (abfd) | |
11244 | || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) | |
11245 | return 0; | |
11246 | ||
11247 | plt = bfd_get_section_by_name (abfd, ".plt"); | |
11248 | if (plt == NULL) | |
11249 | return 0; | |
11250 | ||
11251 | slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; | |
c9727e01 | 11252 | if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) |
4c45e5c9 JJ |
11253 | return -1; |
11254 | ||
eea6121a | 11255 | count = relplt->size / hdr->sh_entsize; |
4c45e5c9 JJ |
11256 | size = count * sizeof (asymbol); |
11257 | p = relplt->relocation; | |
cb53bf42 | 11258 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
041de40d AM |
11259 | { |
11260 | size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); | |
11261 | if (p->addend != 0) | |
11262 | { | |
11263 | #ifdef BFD64 | |
11264 | size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); | |
11265 | #else | |
11266 | size += sizeof ("+0x") - 1 + 8; | |
11267 | #endif | |
11268 | } | |
11269 | } | |
4c45e5c9 | 11270 | |
a50b1753 | 11271 | s = *ret = (asymbol *) bfd_malloc (size); |
4c45e5c9 JJ |
11272 | if (s == NULL) |
11273 | return -1; | |
11274 | ||
11275 | names = (char *) (s + count); | |
11276 | p = relplt->relocation; | |
11277 | n = 0; | |
cb53bf42 | 11278 | for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) |
4c45e5c9 JJ |
11279 | { |
11280 | size_t len; | |
11281 | bfd_vma addr; | |
11282 | ||
11283 | addr = bed->plt_sym_val (i, plt, p); | |
11284 | if (addr == (bfd_vma) -1) | |
11285 | continue; | |
11286 | ||
11287 | *s = **p->sym_ptr_ptr; | |
65a7a66f AM |
11288 | /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since |
11289 | we are defining a symbol, ensure one of them is set. */ | |
11290 | if ((s->flags & BSF_LOCAL) == 0) | |
11291 | s->flags |= BSF_GLOBAL; | |
6ba2a415 | 11292 | s->flags |= BSF_SYNTHETIC; |
4c45e5c9 JJ |
11293 | s->section = plt; |
11294 | s->value = addr - plt->vma; | |
11295 | s->name = names; | |
8f39ba8e | 11296 | s->udata.p = NULL; |
4c45e5c9 JJ |
11297 | len = strlen ((*p->sym_ptr_ptr)->name); |
11298 | memcpy (names, (*p->sym_ptr_ptr)->name, len); | |
11299 | names += len; | |
041de40d AM |
11300 | if (p->addend != 0) |
11301 | { | |
1d770845 | 11302 | char buf[30], *a; |
d324f6d6 | 11303 | |
041de40d AM |
11304 | memcpy (names, "+0x", sizeof ("+0x") - 1); |
11305 | names += sizeof ("+0x") - 1; | |
1d770845 L |
11306 | bfd_sprintf_vma (abfd, buf, p->addend); |
11307 | for (a = buf; *a == '0'; ++a) | |
11308 | ; | |
11309 | len = strlen (a); | |
11310 | memcpy (names, a, len); | |
11311 | names += len; | |
041de40d | 11312 | } |
4c45e5c9 JJ |
11313 | memcpy (names, "@plt", sizeof ("@plt")); |
11314 | names += sizeof ("@plt"); | |
8f39ba8e | 11315 | ++s, ++n; |
4c45e5c9 JJ |
11316 | } |
11317 | ||
11318 | return n; | |
11319 | } | |
3d7f7666 | 11320 | |
821e6ff6 AM |
11321 | /* It is only used by x86-64 so far. |
11322 | ??? This repeats *COM* id of zero. sec->id is supposed to be unique, | |
7eacd66b AM |
11323 | but current usage would allow all of _bfd_std_section to be zero. */ |
11324 | static const asymbol lcomm_sym | |
11325 | = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); | |
3b22753a | 11326 | asection _bfd_elf_large_com_section |
7eacd66b | 11327 | = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, |
821e6ff6 | 11328 | "LARGE_COMMON", 0, SEC_IS_COMMON); |
ecca9871 | 11329 | |
d1036acb | 11330 | void |
78245035 L |
11331 | _bfd_elf_post_process_headers (bfd * abfd, |
11332 | struct bfd_link_info * link_info ATTRIBUTE_UNUSED) | |
d1036acb L |
11333 | { |
11334 | Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ | |
11335 | ||
11336 | i_ehdrp = elf_elfheader (abfd); | |
11337 | ||
11338 | i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; | |
d8045f23 NC |
11339 | |
11340 | /* To make things simpler for the loader on Linux systems we set the | |
9c55345c | 11341 | osabi field to ELFOSABI_GNU if the binary contains symbols of |
f64b2e8d | 11342 | the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */ |
d8045f23 | 11343 | if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE |
f64b2e8d | 11344 | && elf_tdata (abfd)->has_gnu_symbols) |
9c55345c | 11345 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; |
d1036acb | 11346 | } |
fcb93ecf PB |
11347 | |
11348 | ||
11349 | /* Return TRUE for ELF symbol types that represent functions. | |
11350 | This is the default version of this function, which is sufficient for | |
d8045f23 | 11351 | most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ |
fcb93ecf PB |
11352 | |
11353 | bfd_boolean | |
11354 | _bfd_elf_is_function_type (unsigned int type) | |
11355 | { | |
d8045f23 NC |
11356 | return (type == STT_FUNC |
11357 | || type == STT_GNU_IFUNC); | |
fcb93ecf | 11358 | } |
9f296da3 | 11359 | |
aef36ac1 AM |
11360 | /* If the ELF symbol SYM might be a function in SEC, return the |
11361 | function size and set *CODE_OFF to the function's entry point, | |
11362 | otherwise return zero. */ | |
9f296da3 | 11363 | |
aef36ac1 AM |
11364 | bfd_size_type |
11365 | _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, | |
11366 | bfd_vma *code_off) | |
9f296da3 | 11367 | { |
aef36ac1 AM |
11368 | bfd_size_type size; |
11369 | ||
ff9e0f5b | 11370 | if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT |
aef36ac1 AM |
11371 | | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 |
11372 | || sym->section != sec) | |
11373 | return 0; | |
ff9e0f5b | 11374 | |
ff9e0f5b | 11375 | *code_off = sym->value; |
aef36ac1 AM |
11376 | size = 0; |
11377 | if (!(sym->flags & BSF_SYNTHETIC)) | |
11378 | size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; | |
11379 | if (size == 0) | |
11380 | size = 1; | |
11381 | return size; | |
9f296da3 | 11382 | } |