Commit | Line | Data |
---|---|---|
36591ba1 | 1 | /* 32-bit ELF support for Nios II. |
4b95cf5c | 2 | Copyright (C) 2012-2014 Free Software Foundation, Inc. |
36591ba1 SL |
3 | Contributed by Nigel Gray (ngray@altera.com). |
4 | Contributed by Mentor Graphics, Inc. | |
5 | ||
6 | This file is part of BFD, the Binary File Descriptor library. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | MA 02110-1301, USA. */ | |
22 | ||
23 | /* This file handles Altera Nios II ELF targets. */ | |
24 | ||
25 | #include "sysdep.h" | |
26 | #include "bfd.h" | |
27 | #include "libbfd.h" | |
28 | #include "bfdlink.h" | |
29 | #include "genlink.h" | |
30 | #include "elf-bfd.h" | |
31 | #include "elf/nios2.h" | |
32 | #include "opcode/nios2.h" | |
78058a5e | 33 | #include "elf32-nios2.h" |
36591ba1 SL |
34 | |
35 | /* Use RELA relocations. */ | |
36 | #ifndef USE_RELA | |
37 | #define USE_RELA | |
38 | #endif | |
39 | ||
40 | #ifdef USE_REL | |
41 | #undef USE_REL | |
42 | #endif | |
43 | ||
44 | /* Forward declarations. */ | |
45 | static bfd_reloc_status_type nios2_elf32_ignore_reloc | |
46 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
47 | static bfd_reloc_status_type nios2_elf32_hi16_relocate | |
48 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
49 | static bfd_reloc_status_type nios2_elf32_lo16_relocate | |
50 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
51 | static bfd_reloc_status_type nios2_elf32_hiadj16_relocate | |
52 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
53 | static bfd_reloc_status_type nios2_elf32_pcrel_lo16_relocate | |
54 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
55 | static bfd_reloc_status_type nios2_elf32_pcrel_hiadj16_relocate | |
56 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
57 | static bfd_reloc_status_type nios2_elf32_pcrel16_relocate | |
58 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
59 | static bfd_reloc_status_type nios2_elf32_call26_relocate | |
60 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
61 | static bfd_reloc_status_type nios2_elf32_gprel_relocate | |
62 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
63 | static bfd_reloc_status_type nios2_elf32_ujmp_relocate | |
64 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
65 | static bfd_reloc_status_type nios2_elf32_cjmp_relocate | |
66 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
67 | static bfd_reloc_status_type nios2_elf32_callr_relocate | |
68 | (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); | |
69 | ||
70 | /* Target vector. */ | |
6d00b590 AM |
71 | extern const bfd_target nios2_elf32_le_vec; |
72 | extern const bfd_target nios2_elf32_be_vec; | |
36591ba1 SL |
73 | |
74 | /* Offset of tp and dtp pointers from start of TLS block. */ | |
75 | #define TP_OFFSET 0x7000 | |
76 | #define DTP_OFFSET 0x8000 | |
77 | ||
78 | /* The relocation table used for SHT_REL sections. */ | |
79 | static reloc_howto_type elf_nios2_howto_table_rel[] = { | |
80 | /* No relocation. */ | |
81 | HOWTO (R_NIOS2_NONE, /* type */ | |
82 | 0, /* rightshift */ | |
83 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
84 | 0, /* bitsize */ | |
85 | FALSE, /* pc_relative */ | |
86 | 0, /* bitpos */ | |
87 | complain_overflow_dont, /* complain_on_overflow */ | |
88 | bfd_elf_generic_reloc, /* special_function */ | |
89 | "R_NIOS2_NONE", /* name */ | |
90 | FALSE, /* partial_inplace */ | |
91 | 0, /* src_mask */ | |
92 | 0, /* dst_mask */ | |
93 | FALSE), /* pcrel_offset */ | |
94 | ||
95 | /* 16-bit signed immediate relocation. */ | |
96 | HOWTO (R_NIOS2_S16, /* type */ | |
97 | 0, /* rightshift */ | |
98 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
99 | 16, /* bitsize */ | |
100 | FALSE, /* pc_relative */ | |
101 | 6, /* bitpos */ | |
102 | complain_overflow_signed, /* complain on overflow */ | |
103 | bfd_elf_generic_reloc, /* special function */ | |
104 | "R_NIOS2_S16", /* name */ | |
105 | FALSE, /* partial_inplace */ | |
106 | 0x003fffc0, /* src_mask */ | |
107 | 0x003fffc0, /* dest_mask */ | |
108 | FALSE), /* pcrel_offset */ | |
109 | ||
110 | /* 16-bit unsigned immediate relocation. */ | |
111 | HOWTO (R_NIOS2_U16, /* type */ | |
112 | 0, /* rightshift */ | |
113 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
114 | 16, /* bitsize */ | |
115 | FALSE, /* pc_relative */ | |
116 | 6, /* bitpos */ | |
117 | complain_overflow_unsigned, /* complain on overflow */ | |
118 | bfd_elf_generic_reloc, /* special function */ | |
119 | "R_NIOS2_U16", /* name */ | |
120 | FALSE, /* partial_inplace */ | |
121 | 0x003fffc0, /* src_mask */ | |
122 | 0x003fffc0, /* dest_mask */ | |
123 | FALSE), /* pcrel_offset */ | |
124 | ||
125 | HOWTO (R_NIOS2_PCREL16, /* type */ | |
126 | 0, /* rightshift */ | |
127 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
128 | 16, /* bitsize */ | |
129 | TRUE, /* pc_relative */ | |
130 | 6, /* bitpos */ | |
131 | complain_overflow_signed, /* complain on overflow */ | |
132 | nios2_elf32_pcrel16_relocate, /* special function */ | |
133 | "R_NIOS2_PCREL16", /* name */ | |
134 | FALSE, /* partial_inplace */ | |
135 | 0x003fffc0, /* src_mask */ | |
136 | 0x003fffc0, /* dest_mask */ | |
137 | TRUE), /* pcrel_offset */ | |
138 | ||
139 | HOWTO (R_NIOS2_CALL26, /* type */ | |
140 | 2, /* rightshift */ | |
141 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
142 | 26, /* bitsize */ | |
143 | FALSE, /* pc_relative */ | |
144 | 6, /* bitpos */ | |
145 | complain_overflow_dont, /* complain on overflow */ | |
146 | nios2_elf32_call26_relocate, /* special function */ | |
147 | "R_NIOS2_CALL26", /* name */ | |
148 | FALSE, /* partial_inplace */ | |
149 | 0xffffffc0, /* src_mask */ | |
150 | 0xffffffc0, /* dst_mask */ | |
151 | FALSE), /* pcrel_offset */ | |
152 | ||
153 | HOWTO (R_NIOS2_IMM5, | |
154 | 0, | |
155 | 2, | |
156 | 5, | |
157 | FALSE, | |
158 | 6, | |
159 | complain_overflow_bitfield, | |
160 | bfd_elf_generic_reloc, | |
161 | "R_NIOS2_IMM5", | |
162 | FALSE, | |
163 | 0x000007c0, | |
164 | 0x000007c0, | |
165 | FALSE), | |
166 | ||
167 | HOWTO (R_NIOS2_CACHE_OPX, | |
168 | 0, | |
169 | 2, | |
170 | 5, | |
171 | FALSE, | |
172 | 22, | |
173 | complain_overflow_bitfield, | |
174 | bfd_elf_generic_reloc, | |
175 | "R_NIOS2_CACHE_OPX", | |
176 | FALSE, | |
177 | 0x07c00000, | |
178 | 0x07c00000, | |
179 | FALSE), | |
180 | ||
181 | HOWTO (R_NIOS2_IMM6, | |
182 | 0, | |
183 | 2, | |
184 | 6, | |
185 | FALSE, | |
186 | 6, | |
187 | complain_overflow_bitfield, | |
188 | bfd_elf_generic_reloc, | |
189 | "R_NIOS2_IMM6", | |
190 | FALSE, | |
191 | 0x00000fc0, | |
192 | 0x00000fc0, | |
193 | FALSE), | |
194 | ||
195 | HOWTO (R_NIOS2_IMM8, | |
196 | 0, | |
197 | 2, | |
198 | 8, | |
199 | FALSE, | |
200 | 6, | |
201 | complain_overflow_bitfield, | |
202 | bfd_elf_generic_reloc, | |
203 | "R_NIOS2_IMM8", | |
204 | FALSE, | |
205 | 0x00003fc0, | |
206 | 0x00003fc0, | |
207 | FALSE), | |
208 | ||
209 | HOWTO (R_NIOS2_HI16, | |
210 | 0, | |
211 | 2, | |
212 | 32, | |
213 | FALSE, | |
214 | 6, | |
215 | complain_overflow_dont, | |
216 | nios2_elf32_hi16_relocate, | |
217 | "R_NIOS2_HI16", | |
218 | FALSE, | |
219 | 0x003fffc0, | |
220 | 0x003fffc0, | |
221 | FALSE), | |
222 | ||
223 | HOWTO (R_NIOS2_LO16, | |
224 | 0, | |
225 | 2, | |
226 | 32, | |
227 | FALSE, | |
228 | 6, | |
229 | complain_overflow_dont, | |
230 | nios2_elf32_lo16_relocate, | |
231 | "R_NIOS2_LO16", | |
232 | FALSE, | |
233 | 0x003fffc0, | |
234 | 0x003fffc0, | |
235 | FALSE), | |
236 | ||
237 | HOWTO (R_NIOS2_HIADJ16, | |
238 | 0, | |
239 | 2, | |
240 | 32, | |
241 | FALSE, | |
242 | 6, | |
243 | complain_overflow_dont, | |
244 | nios2_elf32_hiadj16_relocate, | |
245 | "R_NIOS2_HIADJ16", | |
246 | FALSE, | |
247 | 0x003fffc0, | |
248 | 0x003fffc0, | |
249 | FALSE), | |
250 | ||
251 | HOWTO (R_NIOS2_BFD_RELOC_32, | |
252 | 0, | |
253 | 2, /* long */ | |
254 | 32, | |
255 | FALSE, | |
256 | 0, | |
257 | complain_overflow_dont, | |
258 | bfd_elf_generic_reloc, | |
259 | "R_NIOS2_BFD_RELOC32", | |
260 | FALSE, | |
261 | 0xffffffff, | |
262 | 0xffffffff, | |
263 | FALSE), | |
264 | ||
265 | HOWTO (R_NIOS2_BFD_RELOC_16, | |
266 | 0, | |
267 | 1, /* short */ | |
268 | 16, | |
269 | FALSE, | |
270 | 0, | |
271 | complain_overflow_bitfield, | |
272 | bfd_elf_generic_reloc, | |
273 | "R_NIOS2_BFD_RELOC16", | |
274 | FALSE, | |
275 | 0x0000ffff, | |
276 | 0x0000ffff, | |
277 | FALSE), | |
278 | ||
279 | HOWTO (R_NIOS2_BFD_RELOC_8, | |
280 | 0, | |
281 | 0, /* byte */ | |
282 | 8, | |
283 | FALSE, | |
284 | 0, | |
285 | complain_overflow_bitfield, | |
286 | bfd_elf_generic_reloc, | |
287 | "R_NIOS2_BFD_RELOC8", | |
288 | FALSE, | |
289 | 0x000000ff, | |
290 | 0x000000ff, | |
291 | FALSE), | |
292 | ||
293 | HOWTO (R_NIOS2_GPREL, | |
294 | 0, | |
295 | 2, | |
296 | 32, | |
297 | FALSE, | |
298 | 6, | |
299 | complain_overflow_dont, | |
300 | nios2_elf32_gprel_relocate, | |
301 | "R_NIOS2_GPREL", | |
302 | FALSE, | |
303 | 0x003fffc0, | |
304 | 0x003fffc0, | |
305 | FALSE), | |
306 | ||
307 | HOWTO (R_NIOS2_GNU_VTINHERIT, | |
308 | 0, | |
309 | 2, /* short */ | |
310 | 0, | |
311 | FALSE, | |
312 | 0, | |
313 | complain_overflow_dont, | |
314 | NULL, | |
315 | "R_NIOS2_GNU_VTINHERIT", | |
316 | FALSE, | |
317 | 0, | |
318 | 0, | |
319 | FALSE), | |
320 | ||
321 | HOWTO (R_NIOS2_GNU_VTENTRY, | |
322 | 0, | |
323 | 2, /* byte */ | |
324 | 0, | |
325 | FALSE, | |
326 | 0, | |
327 | complain_overflow_dont, | |
328 | _bfd_elf_rel_vtable_reloc_fn, | |
329 | "R_NIOS2_GNU_VTENTRY", | |
330 | FALSE, | |
331 | 0, | |
332 | 0, | |
333 | FALSE), | |
334 | ||
335 | HOWTO (R_NIOS2_UJMP, | |
336 | 0, | |
337 | 2, | |
338 | 32, | |
339 | FALSE, | |
340 | 6, | |
341 | complain_overflow_dont, | |
342 | nios2_elf32_ujmp_relocate, | |
343 | "R_NIOS2_UJMP", | |
344 | FALSE, | |
345 | 0x003fffc0, | |
346 | 0x003fffc0, | |
347 | FALSE), | |
348 | ||
349 | HOWTO (R_NIOS2_CJMP, | |
350 | 0, | |
351 | 2, | |
352 | 32, | |
353 | FALSE, | |
354 | 6, | |
355 | complain_overflow_dont, | |
356 | nios2_elf32_cjmp_relocate, | |
357 | "R_NIOS2_CJMP", | |
358 | FALSE, | |
359 | 0x003fffc0, | |
360 | 0x003fffc0, | |
361 | FALSE), | |
362 | ||
363 | HOWTO (R_NIOS2_CALLR, | |
364 | 0, | |
365 | 2, | |
366 | 32, | |
367 | FALSE, | |
368 | 6, | |
369 | complain_overflow_dont, | |
370 | nios2_elf32_callr_relocate, | |
371 | "R_NIOS2_CALLR", | |
372 | FALSE, | |
373 | 0x003fffc0, | |
374 | 0x003fffc0, | |
375 | FALSE), | |
376 | ||
377 | HOWTO (R_NIOS2_ALIGN, | |
378 | 0, | |
379 | 2, | |
380 | 0, | |
381 | FALSE, | |
382 | 0, | |
383 | complain_overflow_dont, | |
384 | nios2_elf32_ignore_reloc, | |
385 | "R_NIOS2_ALIGN", | |
386 | FALSE, | |
387 | 0, | |
388 | 0, | |
389 | TRUE), | |
390 | ||
391 | ||
392 | HOWTO (R_NIOS2_GOT16, | |
393 | 0, | |
394 | 2, | |
395 | 16, | |
396 | FALSE, | |
397 | 6, | |
398 | complain_overflow_bitfield, | |
399 | bfd_elf_generic_reloc, | |
400 | "R_NIOS2_GOT16", | |
401 | FALSE, | |
402 | 0x003fffc0, | |
403 | 0x003fffc0, | |
404 | FALSE), | |
405 | ||
406 | HOWTO (R_NIOS2_CALL16, | |
407 | 0, | |
408 | 2, | |
409 | 16, | |
410 | FALSE, | |
411 | 6, | |
412 | complain_overflow_bitfield, | |
413 | bfd_elf_generic_reloc, | |
414 | "R_NIOS2_CALL16", | |
415 | FALSE, | |
416 | 0x003fffc0, | |
417 | 0x003fffc0, | |
418 | FALSE), | |
419 | ||
420 | HOWTO (R_NIOS2_GOTOFF_LO, | |
421 | 0, | |
422 | 2, | |
423 | 16, | |
424 | FALSE, | |
425 | 6, | |
426 | complain_overflow_dont, | |
427 | bfd_elf_generic_reloc, | |
428 | "R_NIOS2_GOTOFF_LO", | |
429 | FALSE, | |
430 | 0x003fffc0, | |
431 | 0x003fffc0, | |
432 | FALSE), | |
433 | ||
434 | HOWTO (R_NIOS2_GOTOFF_HA, | |
435 | 0, | |
436 | 2, | |
437 | 16, | |
438 | FALSE, | |
439 | 6, | |
440 | complain_overflow_dont, | |
441 | bfd_elf_generic_reloc, | |
442 | "R_NIOS2_GOTOFF_HA", | |
443 | FALSE, | |
444 | 0x003fffc0, | |
445 | 0x003fffc0, | |
446 | FALSE), | |
447 | ||
448 | HOWTO (R_NIOS2_PCREL_LO, | |
449 | 0, | |
450 | 2, | |
451 | 16, | |
452 | TRUE, | |
453 | 6, | |
454 | complain_overflow_dont, | |
455 | nios2_elf32_pcrel_lo16_relocate, | |
456 | "R_NIOS2_PCREL_LO", | |
457 | FALSE, | |
458 | 0x003fffc0, | |
459 | 0x003fffc0, | |
460 | TRUE), | |
461 | ||
462 | HOWTO (R_NIOS2_PCREL_HA, | |
463 | 0, | |
464 | 2, | |
465 | 16, | |
466 | FALSE, /* This is a PC-relative relocation, but we need to subtract | |
467 | PC ourselves before the HIADJ. */ | |
468 | 6, | |
469 | complain_overflow_dont, | |
470 | nios2_elf32_pcrel_hiadj16_relocate, | |
471 | "R_NIOS2_PCREL_HA", | |
472 | FALSE, | |
473 | 0x003fffc0, | |
474 | 0x003fffc0, | |
475 | TRUE), | |
476 | ||
477 | HOWTO (R_NIOS2_TLS_GD16, | |
478 | 0, | |
479 | 2, | |
480 | 16, | |
481 | FALSE, | |
482 | 6, | |
483 | complain_overflow_bitfield, | |
484 | bfd_elf_generic_reloc, | |
485 | "R_NIOS2_TLS_GD16", | |
486 | FALSE, | |
487 | 0x003fffc0, | |
488 | 0x003fffc0, | |
489 | FALSE), | |
490 | ||
491 | HOWTO (R_NIOS2_TLS_LDM16, | |
492 | 0, | |
493 | 2, | |
494 | 16, | |
495 | FALSE, | |
496 | 6, | |
497 | complain_overflow_bitfield, | |
498 | bfd_elf_generic_reloc, | |
499 | "R_NIOS2_TLS_LDM16", | |
500 | FALSE, | |
501 | 0x003fffc0, | |
502 | 0x003fffc0, | |
503 | FALSE), | |
504 | ||
505 | HOWTO (R_NIOS2_TLS_LDO16, | |
506 | 0, | |
507 | 2, | |
508 | 16, | |
509 | FALSE, | |
510 | 6, | |
511 | complain_overflow_bitfield, | |
512 | bfd_elf_generic_reloc, | |
513 | "R_NIOS2_TLS_LDO16", | |
514 | FALSE, | |
515 | 0x003fffc0, | |
516 | 0x003fffc0, | |
517 | FALSE), | |
518 | ||
519 | HOWTO (R_NIOS2_TLS_IE16, | |
520 | 0, | |
521 | 2, | |
522 | 16, | |
523 | FALSE, | |
524 | 6, | |
525 | complain_overflow_bitfield, | |
526 | bfd_elf_generic_reloc, | |
527 | "R_NIOS2_TLS_IE16", | |
528 | FALSE, | |
529 | 0x003fffc0, | |
530 | 0x003fffc0, | |
531 | FALSE), | |
532 | ||
533 | HOWTO (R_NIOS2_TLS_LE16, | |
534 | 0, | |
535 | 2, | |
536 | 16, | |
537 | FALSE, | |
538 | 6, | |
539 | complain_overflow_bitfield, | |
540 | bfd_elf_generic_reloc, | |
541 | "R_NIOS2_TLS_LE16", | |
542 | FALSE, | |
543 | 0x003fffc0, | |
544 | 0x003fffc0, | |
545 | FALSE), | |
546 | ||
547 | HOWTO (R_NIOS2_TLS_DTPMOD, | |
548 | 0, | |
549 | 2, | |
550 | 32, | |
551 | FALSE, | |
552 | 0, | |
553 | complain_overflow_dont, | |
554 | bfd_elf_generic_reloc, | |
555 | "R_NIOS2_TLS_DTPMOD", | |
556 | FALSE, | |
557 | 0xffffffff, | |
558 | 0xffffffff, | |
559 | FALSE), | |
560 | ||
561 | HOWTO (R_NIOS2_TLS_DTPREL, | |
562 | 0, | |
563 | 2, | |
564 | 32, | |
565 | FALSE, | |
566 | 0, | |
567 | complain_overflow_dont, | |
568 | bfd_elf_generic_reloc, | |
569 | "R_NIOS2_TLS_DTPREL", | |
570 | FALSE, | |
571 | 0xffffffff, | |
572 | 0xffffffff, | |
573 | FALSE), | |
574 | ||
575 | HOWTO (R_NIOS2_TLS_TPREL, | |
576 | 0, | |
577 | 2, | |
578 | 32, | |
579 | FALSE, | |
580 | 0, | |
581 | complain_overflow_dont, | |
582 | bfd_elf_generic_reloc, | |
583 | "R_NIOS2_TLS_TPREL", | |
584 | FALSE, | |
585 | 0xffffffff, | |
586 | 0xffffffff, | |
587 | FALSE), | |
588 | ||
589 | HOWTO (R_NIOS2_COPY, | |
590 | 0, | |
591 | 2, | |
592 | 32, | |
593 | FALSE, | |
594 | 0, | |
595 | complain_overflow_dont, | |
596 | bfd_elf_generic_reloc, | |
597 | "R_NIOS2_COPY", | |
598 | FALSE, | |
599 | 0, | |
600 | 0, | |
601 | FALSE), | |
602 | ||
603 | HOWTO (R_NIOS2_GLOB_DAT, | |
604 | 0, | |
605 | 2, | |
606 | 32, | |
607 | FALSE, | |
608 | 0, | |
609 | complain_overflow_dont, | |
610 | bfd_elf_generic_reloc, | |
611 | "R_NIOS2_GLOB_DAT", | |
612 | FALSE, | |
613 | 0xffffffff, | |
614 | 0xffffffff, | |
615 | FALSE), | |
616 | ||
617 | HOWTO (R_NIOS2_JUMP_SLOT, | |
618 | 0, | |
619 | 2, | |
620 | 32, | |
621 | FALSE, | |
622 | 0, | |
623 | complain_overflow_dont, | |
624 | bfd_elf_generic_reloc, | |
625 | "R_NIOS2_JUMP_SLOT", | |
626 | FALSE, | |
627 | 0xffffffff, | |
628 | 0xffffffff, | |
629 | FALSE), | |
630 | ||
631 | HOWTO (R_NIOS2_RELATIVE, | |
632 | 0, | |
633 | 2, | |
634 | 32, | |
635 | FALSE, | |
636 | 0, | |
637 | complain_overflow_dont, | |
638 | bfd_elf_generic_reloc, | |
639 | "R_NIOS2_RELATIVE", | |
640 | FALSE, | |
641 | 0xffffffff, | |
642 | 0xffffffff, | |
643 | FALSE), | |
644 | ||
645 | HOWTO (R_NIOS2_GOTOFF, | |
646 | 0, | |
647 | 2, | |
648 | 32, | |
649 | FALSE, | |
650 | 0, | |
651 | complain_overflow_dont, | |
652 | bfd_elf_generic_reloc, | |
653 | "R_NIOS2_GOTOFF", | |
654 | FALSE, | |
655 | 0xffffffff, | |
656 | 0xffffffff, | |
657 | FALSE), | |
658 | ||
78058a5e SL |
659 | HOWTO (R_NIOS2_CALL26_NOAT, /* type */ |
660 | 2, /* rightshift */ | |
661 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
662 | 26, /* bitsize */ | |
663 | FALSE, /* pc_relative */ | |
664 | 6, /* bitpos */ | |
665 | complain_overflow_dont, /* complain on overflow */ | |
666 | nios2_elf32_call26_relocate, /* special function */ | |
667 | "R_NIOS2_CALL26_NOAT", /* name */ | |
668 | FALSE, /* partial_inplace */ | |
669 | 0xffffffc0, /* src_mask */ | |
670 | 0xffffffc0, /* dst_mask */ | |
671 | FALSE), /* pcrel_offset */ | |
672 | ||
1c2de463 SL |
673 | HOWTO (R_NIOS2_GOT_LO, |
674 | 0, | |
675 | 2, | |
676 | 16, | |
677 | FALSE, | |
678 | 6, | |
679 | complain_overflow_dont, | |
680 | bfd_elf_generic_reloc, | |
681 | "R_NIOS2_GOT_LO", | |
682 | FALSE, | |
683 | 0x003fffc0, | |
684 | 0x003fffc0, | |
685 | FALSE), | |
686 | ||
687 | HOWTO (R_NIOS2_GOT_HA, | |
688 | 0, | |
689 | 2, | |
690 | 16, | |
691 | FALSE, | |
692 | 6, | |
693 | complain_overflow_dont, | |
694 | bfd_elf_generic_reloc, | |
695 | "R_NIOS2_GOT_HA", | |
696 | FALSE, | |
697 | 0x003fffc0, | |
698 | 0x003fffc0, | |
699 | FALSE), | |
700 | ||
701 | HOWTO (R_NIOS2_CALL_LO, | |
702 | 0, | |
703 | 2, | |
704 | 16, | |
705 | FALSE, | |
706 | 6, | |
707 | complain_overflow_dont, | |
708 | bfd_elf_generic_reloc, | |
709 | "R_NIOS2_CALL_LO", | |
710 | FALSE, | |
711 | 0x003fffc0, | |
712 | 0x003fffc0, | |
713 | FALSE), | |
714 | ||
715 | HOWTO (R_NIOS2_CALL_HA, | |
716 | 0, | |
717 | 2, | |
718 | 16, | |
719 | FALSE, | |
720 | 6, | |
721 | complain_overflow_dont, | |
722 | bfd_elf_generic_reloc, | |
723 | "R_NIOS2_CALL_HA", | |
724 | FALSE, | |
725 | 0x003fffc0, | |
726 | 0x003fffc0, | |
727 | FALSE), | |
728 | ||
36591ba1 SL |
729 | /* Add other relocations here. */ |
730 | }; | |
731 | ||
732 | static unsigned char elf_code_to_howto_index[R_NIOS2_ILLEGAL + 1]; | |
733 | ||
734 | /* Return the howto for relocation RTYPE. */ | |
735 | static reloc_howto_type * | |
736 | lookup_howto (unsigned int rtype) | |
737 | { | |
738 | static int initialized = 0; | |
739 | int i; | |
740 | int howto_tbl_size = (int) (sizeof (elf_nios2_howto_table_rel) | |
741 | / sizeof (elf_nios2_howto_table_rel[0])); | |
742 | ||
743 | if (!initialized) | |
744 | { | |
745 | initialized = 1; | |
746 | memset (elf_code_to_howto_index, 0xff, | |
747 | sizeof (elf_code_to_howto_index)); | |
748 | for (i = 0; i < howto_tbl_size; i++) | |
749 | elf_code_to_howto_index[elf_nios2_howto_table_rel[i].type] = i; | |
750 | } | |
751 | ||
752 | BFD_ASSERT (rtype <= R_NIOS2_ILLEGAL); | |
753 | i = elf_code_to_howto_index[rtype]; | |
754 | if (i >= howto_tbl_size) | |
755 | return 0; | |
756 | return elf_nios2_howto_table_rel + i; | |
757 | } | |
758 | ||
759 | /* Map for converting BFD reloc types to Nios II reloc types. */ | |
760 | struct elf_reloc_map | |
761 | { | |
762 | bfd_reloc_code_real_type bfd_val; | |
763 | enum elf_nios2_reloc_type elf_val; | |
764 | }; | |
765 | ||
766 | static const struct elf_reloc_map nios2_reloc_map[] = { | |
767 | {BFD_RELOC_NIOS2_S16, R_NIOS2_S16}, | |
768 | {BFD_RELOC_NIOS2_U16, R_NIOS2_U16}, | |
769 | {BFD_RELOC_16_PCREL, R_NIOS2_PCREL16}, | |
770 | {BFD_RELOC_NIOS2_CALL26, R_NIOS2_CALL26}, | |
771 | {BFD_RELOC_NIOS2_IMM5, R_NIOS2_IMM5}, | |
772 | {BFD_RELOC_NIOS2_CACHE_OPX, R_NIOS2_CACHE_OPX}, | |
773 | {BFD_RELOC_NIOS2_IMM6, R_NIOS2_IMM6}, | |
774 | {BFD_RELOC_NIOS2_IMM8, R_NIOS2_IMM8}, | |
775 | {BFD_RELOC_NIOS2_HI16, R_NIOS2_HI16}, | |
776 | {BFD_RELOC_NIOS2_LO16, R_NIOS2_LO16}, | |
777 | {BFD_RELOC_NIOS2_HIADJ16, R_NIOS2_HIADJ16}, | |
778 | {BFD_RELOC_32, R_NIOS2_BFD_RELOC_32}, | |
779 | {BFD_RELOC_16, R_NIOS2_BFD_RELOC_16}, | |
780 | {BFD_RELOC_8, R_NIOS2_BFD_RELOC_8}, | |
781 | {BFD_RELOC_NIOS2_GPREL, R_NIOS2_GPREL}, | |
782 | {BFD_RELOC_VTABLE_INHERIT, R_NIOS2_GNU_VTINHERIT}, | |
783 | {BFD_RELOC_VTABLE_ENTRY, R_NIOS2_GNU_VTENTRY}, | |
784 | {BFD_RELOC_NIOS2_UJMP, R_NIOS2_UJMP}, | |
785 | {BFD_RELOC_NIOS2_CJMP, R_NIOS2_CJMP}, | |
786 | {BFD_RELOC_NIOS2_CALLR, R_NIOS2_CALLR}, | |
787 | {BFD_RELOC_NIOS2_ALIGN, R_NIOS2_ALIGN}, | |
788 | {BFD_RELOC_NIOS2_GOT16, R_NIOS2_GOT16}, | |
789 | {BFD_RELOC_NIOS2_CALL16, R_NIOS2_CALL16}, | |
790 | {BFD_RELOC_NIOS2_GOTOFF_LO, R_NIOS2_GOTOFF_LO}, | |
791 | {BFD_RELOC_NIOS2_GOTOFF_HA, R_NIOS2_GOTOFF_HA}, | |
792 | {BFD_RELOC_NIOS2_PCREL_LO, R_NIOS2_PCREL_LO}, | |
793 | {BFD_RELOC_NIOS2_PCREL_HA, R_NIOS2_PCREL_HA}, | |
794 | {BFD_RELOC_NIOS2_TLS_GD16, R_NIOS2_TLS_GD16}, | |
795 | {BFD_RELOC_NIOS2_TLS_LDM16, R_NIOS2_TLS_LDM16}, | |
796 | {BFD_RELOC_NIOS2_TLS_LDO16, R_NIOS2_TLS_LDO16}, | |
797 | {BFD_RELOC_NIOS2_TLS_IE16, R_NIOS2_TLS_IE16}, | |
798 | {BFD_RELOC_NIOS2_TLS_LE16, R_NIOS2_TLS_LE16}, | |
799 | {BFD_RELOC_NIOS2_TLS_DTPMOD, R_NIOS2_TLS_DTPMOD}, | |
800 | {BFD_RELOC_NIOS2_TLS_DTPREL, R_NIOS2_TLS_DTPREL}, | |
801 | {BFD_RELOC_NIOS2_TLS_TPREL, R_NIOS2_TLS_TPREL}, | |
802 | {BFD_RELOC_NIOS2_COPY, R_NIOS2_COPY}, | |
803 | {BFD_RELOC_NIOS2_GLOB_DAT, R_NIOS2_GLOB_DAT}, | |
804 | {BFD_RELOC_NIOS2_JUMP_SLOT, R_NIOS2_JUMP_SLOT}, | |
805 | {BFD_RELOC_NIOS2_RELATIVE, R_NIOS2_RELATIVE}, | |
78058a5e SL |
806 | {BFD_RELOC_NIOS2_GOTOFF, R_NIOS2_GOTOFF}, |
807 | {BFD_RELOC_NIOS2_CALL26_NOAT, R_NIOS2_CALL26_NOAT}, | |
1c2de463 SL |
808 | {BFD_RELOC_NIOS2_GOT_LO, R_NIOS2_GOT_LO}, |
809 | {BFD_RELOC_NIOS2_GOT_HA, R_NIOS2_GOT_HA}, | |
810 | {BFD_RELOC_NIOS2_CALL_LO, R_NIOS2_CALL_LO}, | |
811 | {BFD_RELOC_NIOS2_CALL_HA, R_NIOS2_CALL_HA}, | |
78058a5e SL |
812 | }; |
813 | ||
814 | enum elf32_nios2_stub_type | |
815 | { | |
816 | nios2_stub_call26_before, | |
817 | nios2_stub_call26_after, | |
818 | nios2_stub_none | |
819 | }; | |
820 | ||
821 | struct elf32_nios2_stub_hash_entry | |
822 | { | |
823 | /* Base hash table entry structure. */ | |
824 | struct bfd_hash_entry bh_root; | |
825 | ||
826 | /* The stub section. */ | |
827 | asection *stub_sec; | |
828 | ||
829 | /* Offset within stub_sec of the beginning of this stub. */ | |
830 | bfd_vma stub_offset; | |
831 | ||
832 | /* Given the symbol's value and its section we can determine its final | |
833 | value when building the stubs (so the stub knows where to jump. */ | |
834 | bfd_vma target_value; | |
835 | asection *target_section; | |
836 | ||
837 | enum elf32_nios2_stub_type stub_type; | |
838 | ||
839 | /* The symbol table entry, if any, that this was derived from. */ | |
840 | struct elf32_nios2_link_hash_entry *hh; | |
841 | ||
842 | /* And the reloc addend that this was derived from. */ | |
843 | bfd_vma addend; | |
844 | ||
845 | /* Where this stub is being called from, or, in the case of combined | |
846 | stub sections, the first input section in the group. */ | |
847 | asection *id_sec; | |
36591ba1 SL |
848 | }; |
849 | ||
78058a5e SL |
850 | #define nios2_stub_hash_entry(ent) \ |
851 | ((struct elf32_nios2_stub_hash_entry *)(ent)) | |
852 | ||
853 | #define nios2_stub_hash_lookup(table, string, create, copy) \ | |
854 | ((struct elf32_nios2_stub_hash_entry *) \ | |
855 | bfd_hash_lookup ((table), (string), (create), (copy))) | |
856 | ||
857 | ||
36591ba1 SL |
858 | /* The Nios II linker needs to keep track of the number of relocs that it |
859 | decides to copy as dynamic relocs in check_relocs for each symbol. | |
860 | This is so that it can later discard them if they are found to be | |
861 | unnecessary. We store the information in a field extending the | |
862 | regular ELF linker hash table. */ | |
863 | ||
864 | struct elf32_nios2_dyn_relocs | |
865 | { | |
866 | struct elf32_nios2_dyn_relocs *next; | |
867 | ||
868 | /* The input section of the reloc. */ | |
869 | asection *sec; | |
870 | ||
871 | /* Total number of relocs copied for the input section. */ | |
872 | bfd_size_type count; | |
873 | ||
874 | /* Number of pc-relative relocs copied for the input section. */ | |
875 | bfd_size_type pc_count; | |
876 | }; | |
877 | ||
878 | /* Nios II ELF linker hash entry. */ | |
879 | ||
880 | struct elf32_nios2_link_hash_entry | |
881 | { | |
882 | struct elf_link_hash_entry root; | |
883 | ||
78058a5e SL |
884 | /* A pointer to the most recently used stub hash entry against this |
885 | symbol. */ | |
886 | struct elf32_nios2_stub_hash_entry *hsh_cache; | |
887 | ||
36591ba1 SL |
888 | /* Track dynamic relocs copied for this symbol. */ |
889 | struct elf32_nios2_dyn_relocs *dyn_relocs; | |
890 | ||
891 | #define GOT_UNKNOWN 0 | |
892 | #define GOT_NORMAL 1 | |
893 | #define GOT_TLS_GD 2 | |
894 | #define GOT_TLS_IE 4 | |
895 | unsigned char tls_type; | |
896 | ||
897 | /* We need to detect and take special action for symbols which are only | |
898 | referenced with %call() and not with %got(). Such symbols do not need | |
899 | a dynamic GOT reloc in shared objects, only a dynamic PLT reloc. Lazy | |
900 | linking will not work if the dynamic GOT reloc exists. | |
901 | To check for this condition efficiently, we compare got_types_used against | |
1c2de463 SL |
902 | CALL_USED, meaning |
903 | (got_types_used & (GOT_USED | CALL_USED)) == CALL_USED. | |
904 | */ | |
905 | #define GOT_USED 1 | |
906 | #define CALL_USED 2 | |
36591ba1 SL |
907 | unsigned char got_types_used; |
908 | }; | |
909 | ||
910 | #define elf32_nios2_hash_entry(ent) \ | |
911 | ((struct elf32_nios2_link_hash_entry *) (ent)) | |
912 | ||
913 | /* Get the Nios II elf linker hash table from a link_info structure. */ | |
914 | #define elf32_nios2_hash_table(info) \ | |
915 | ((struct elf32_nios2_link_hash_table *) ((info)->hash)) | |
916 | ||
917 | /* Nios II ELF linker hash table. */ | |
918 | struct elf32_nios2_link_hash_table | |
919 | { | |
920 | /* The main hash table. */ | |
921 | struct elf_link_hash_table root; | |
922 | ||
78058a5e SL |
923 | /* The stub hash table. */ |
924 | struct bfd_hash_table bstab; | |
925 | ||
926 | /* Linker stub bfd. */ | |
927 | bfd *stub_bfd; | |
928 | ||
929 | /* Linker call-backs. */ | |
930 | asection * (*add_stub_section) (const char *, asection *, bfd_boolean); | |
931 | void (*layout_sections_again) (void); | |
932 | ||
933 | /* Array to keep track of which stub sections have been created, and | |
934 | information on stub grouping. */ | |
935 | struct map_stub | |
936 | { | |
937 | /* These are the section to which stubs in the group will be | |
938 | attached. */ | |
939 | asection *first_sec, *last_sec; | |
940 | /* The stub sections. There might be stubs inserted either before | |
941 | or after the real section.*/ | |
942 | asection *first_stub_sec, *last_stub_sec; | |
943 | } *stub_group; | |
944 | ||
945 | /* Assorted information used by nios2_elf32_size_stubs. */ | |
946 | unsigned int bfd_count; | |
947 | int top_index; | |
948 | asection **input_list; | |
949 | Elf_Internal_Sym **all_local_syms; | |
950 | ||
36591ba1 SL |
951 | /* Short-cuts to get to dynamic linker sections. */ |
952 | asection *sdynbss; | |
953 | asection *srelbss; | |
954 | asection *sbss; | |
955 | ||
82e91538 SL |
956 | /* GOT pointer symbol _gp_got. */ |
957 | struct elf_link_hash_entry *h_gp_got; | |
958 | ||
36591ba1 SL |
959 | union { |
960 | bfd_signed_vma refcount; | |
961 | bfd_vma offset; | |
962 | } tls_ldm_got; | |
963 | ||
964 | /* Small local sym cache. */ | |
965 | struct sym_cache sym_cache; | |
966 | ||
967 | bfd_vma res_n_size; | |
968 | }; | |
969 | ||
970 | struct nios2_elf32_obj_tdata | |
971 | { | |
972 | struct elf_obj_tdata root; | |
973 | ||
974 | /* tls_type for each local got entry. */ | |
975 | char *local_got_tls_type; | |
976 | ||
977 | /* TRUE if TLS GD relocs have been seen for this object. */ | |
978 | bfd_boolean has_tlsgd; | |
979 | }; | |
980 | ||
981 | #define elf32_nios2_tdata(abfd) \ | |
982 | ((struct nios2_elf32_obj_tdata *) (abfd)->tdata.any) | |
983 | ||
984 | #define elf32_nios2_local_got_tls_type(abfd) \ | |
985 | (elf32_nios2_tdata (abfd)->local_got_tls_type) | |
986 | ||
987 | /* The name of the dynamic interpreter. This is put in the .interp | |
988 | section. */ | |
989 | #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1" | |
990 | ||
991 | /* PLT implementation for position-dependent code. */ | |
992 | static const bfd_vma nios2_plt_entry[] = { /* .PLTn: */ | |
993 | 0x03c00034, /* movhi r15, %hiadj(plt_got_slot_address) */ | |
994 | 0x7bc00017, /* ldw r15, %lo(plt_got_slot_address)(r15) */ | |
995 | 0x7800683a /* jmp r15 */ | |
996 | }; | |
997 | ||
998 | static const bfd_vma nios2_plt0_entry[] = { /* .PLTresolve */ | |
999 | 0x03800034, /* movhi r14, %hiadj(res_0) */ | |
1000 | 0x73800004, /* addi r14, r14, %lo(res_0) */ | |
1001 | 0x7b9fc83a, /* sub r15, r15, r14 */ | |
1002 | 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */ | |
1003 | 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */ | |
1004 | 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */ | |
1005 | 0x6800683a /* jmp r13 */ | |
1006 | }; | |
1007 | ||
1008 | /* PLT implementation for position-independent code. */ | |
1009 | static const bfd_vma nios2_so_plt_entry[] = { /* .PLTn */ | |
1010 | 0x03c00034, /* movhi r15, %hiadj(index * 4) */ | |
1011 | 0x7bc00004, /* addi r15, r15, %lo(index * 4) */ | |
1012 | 0x00000006 /* br .PLTresolve */ | |
1013 | }; | |
1014 | ||
1015 | static const bfd_vma nios2_so_plt0_entry[] = { /* .PLTresolve */ | |
1016 | 0x001ce03a, /* nextpc r14 */ | |
1017 | 0x03400034, /* movhi r13, %hiadj(_GLOBAL_OFFSET_TABLE_) */ | |
1018 | 0x6b9b883a, /* add r13, r13, r14 */ | |
1019 | 0x6b800017, /* ldw r14, %lo(_GLOBAL_OFFSET_TABLE_+4)(r13) */ | |
1020 | 0x6b400017, /* ldw r13, %lo(_GLOBAL_OFFSET_TABLE_+8)(r13) */ | |
1021 | 0x6800683a /* jmp r13 */ | |
1022 | }; | |
1023 | ||
78058a5e SL |
1024 | /* CALL26 stub. */ |
1025 | static const bfd_vma nios2_call26_stub_entry[] = { | |
1026 | 0x00400034, /* orhi at, r0, %hiadj(dest) */ | |
1027 | 0x08400004, /* addi at, at, %lo(dest) */ | |
1028 | 0x0800683a /* jmp at */ | |
1029 | }; | |
1030 | ||
1031 | /* Install 16-bit immediate value VALUE at offset OFFSET into section SEC. */ | |
1032 | static void | |
1033 | nios2_elf32_install_imm16 (asection *sec, bfd_vma offset, bfd_vma value) | |
1034 | { | |
1035 | bfd_vma word = bfd_get_32 (sec->owner, sec->contents + offset); | |
1036 | ||
1037 | BFD_ASSERT(value <= 0xffff); | |
1038 | ||
1039 | bfd_put_32 (sec->owner, word | ((value & 0xffff) << 6), | |
1040 | sec->contents + offset); | |
1041 | } | |
1042 | ||
1043 | /* Install COUNT 32-bit values DATA starting at offset OFFSET into | |
1044 | section SEC. */ | |
1045 | static void | |
1046 | nios2_elf32_install_data (asection *sec, const bfd_vma *data, bfd_vma offset, | |
1047 | int count) | |
1048 | { | |
1049 | while (count--) | |
1050 | { | |
1051 | bfd_put_32 (sec->owner, *data, sec->contents + offset); | |
1052 | offset += 4; | |
1053 | ++data; | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | /* The usual way of loading a 32-bit constant into a Nios II register is to | |
1058 | load the high 16 bits in one instruction and then add the low 16 bits with | |
1059 | a signed add. This means that the high halfword needs to be adjusted to | |
1060 | compensate for the sign bit of the low halfword. This function returns the | |
1061 | adjusted high halfword for a given 32-bit constant. */ | |
1062 | static | |
1063 | bfd_vma hiadj (bfd_vma symbol_value) | |
1064 | { | |
1065 | return ((symbol_value + 0x8000) >> 16) & 0xffff; | |
1066 | } | |
1067 | ||
36591ba1 SL |
1068 | /* Implement elf_backend_grok_prstatus: |
1069 | Support for core dump NOTE sections. */ | |
1070 | static bfd_boolean | |
1071 | nios2_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) | |
1072 | { | |
1073 | int offset; | |
1074 | size_t size; | |
1075 | ||
1076 | switch (note->descsz) | |
1077 | { | |
1078 | default: | |
1079 | return FALSE; | |
1080 | ||
1081 | case 212: /* Linux/Nios II */ | |
1082 | /* pr_cursig */ | |
228e534f | 1083 | elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12); |
36591ba1 SL |
1084 | |
1085 | /* pr_pid */ | |
228e534f | 1086 | elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24); |
36591ba1 SL |
1087 | |
1088 | /* pr_reg */ | |
1089 | offset = 72; | |
1090 | size = 136; | |
1091 | ||
1092 | break; | |
1093 | } | |
1094 | ||
1095 | /* Make a ".reg/999" section. */ | |
1096 | return _bfd_elfcore_make_pseudosection (abfd, ".reg", | |
1097 | size, note->descpos + offset); | |
1098 | } | |
1099 | ||
1100 | /* Implement elf_backend_grok_psinfo. */ | |
1101 | static bfd_boolean | |
1102 | nios2_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) | |
1103 | { | |
1104 | switch (note->descsz) | |
1105 | { | |
1106 | default: | |
1107 | return FALSE; | |
1108 | ||
1109 | case 124: /* Linux/Nios II elf_prpsinfo */ | |
228e534f | 1110 | elf_tdata (abfd)->core->program |
36591ba1 | 1111 | = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16); |
228e534f | 1112 | elf_tdata (abfd)->core->command |
36591ba1 SL |
1113 | = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80); |
1114 | } | |
1115 | ||
1116 | /* Note that for some reason, a spurious space is tacked | |
1117 | onto the end of the args in some (at least one anyway) | |
1118 | implementations, so strip it off if it exists. */ | |
1119 | ||
1120 | { | |
228e534f | 1121 | char *command = elf_tdata (abfd)->core->command; |
36591ba1 SL |
1122 | int n = strlen (command); |
1123 | ||
1124 | if (0 < n && command[n - 1] == ' ') | |
1125 | command[n - 1] = '\0'; | |
1126 | } | |
1127 | ||
1128 | return TRUE; | |
1129 | } | |
1130 | ||
78058a5e SL |
1131 | /* Assorted hash table functions. */ |
1132 | ||
1133 | /* Initialize an entry in the stub hash table. */ | |
1134 | static struct bfd_hash_entry * | |
1135 | stub_hash_newfunc (struct bfd_hash_entry *entry, | |
1136 | struct bfd_hash_table *table, | |
1137 | const char *string) | |
1138 | { | |
1139 | /* Allocate the structure if it has not already been allocated by a | |
1140 | subclass. */ | |
1141 | if (entry == NULL) | |
1142 | { | |
1143 | entry = bfd_hash_allocate (table, | |
1144 | sizeof (struct elf32_nios2_stub_hash_entry)); | |
1145 | if (entry == NULL) | |
1146 | return entry; | |
1147 | } | |
1148 | ||
1149 | /* Call the allocation method of the superclass. */ | |
1150 | entry = bfd_hash_newfunc (entry, table, string); | |
1151 | if (entry != NULL) | |
1152 | { | |
1153 | struct elf32_nios2_stub_hash_entry *hsh; | |
1154 | ||
1155 | /* Initialize the local fields. */ | |
1156 | hsh = (struct elf32_nios2_stub_hash_entry *) entry; | |
1157 | hsh->stub_sec = NULL; | |
1158 | hsh->stub_offset = 0; | |
1159 | hsh->target_value = 0; | |
1160 | hsh->target_section = NULL; | |
1161 | hsh->stub_type = nios2_stub_none; | |
1162 | hsh->hh = NULL; | |
1163 | hsh->id_sec = NULL; | |
1164 | } | |
1165 | ||
1166 | return entry; | |
1167 | } | |
1168 | ||
36591ba1 SL |
1169 | /* Create an entry in a Nios II ELF linker hash table. */ |
1170 | static struct bfd_hash_entry * | |
1171 | link_hash_newfunc (struct bfd_hash_entry *entry, | |
1172 | struct bfd_hash_table *table, const char *string) | |
1173 | { | |
1174 | /* Allocate the structure if it has not already been allocated by a | |
1175 | subclass. */ | |
1176 | if (entry == NULL) | |
1177 | { | |
1178 | entry = bfd_hash_allocate (table, | |
1179 | sizeof (struct elf32_nios2_link_hash_entry)); | |
1180 | if (entry == NULL) | |
1181 | return entry; | |
1182 | } | |
1183 | ||
1184 | /* Call the allocation method of the superclass. */ | |
1185 | entry = _bfd_elf_link_hash_newfunc (entry, table, string); | |
1186 | if (entry) | |
1187 | { | |
1188 | struct elf32_nios2_link_hash_entry *eh; | |
1189 | ||
1190 | eh = (struct elf32_nios2_link_hash_entry *) entry; | |
78058a5e | 1191 | eh->hsh_cache = NULL; |
36591ba1 SL |
1192 | eh->dyn_relocs = NULL; |
1193 | eh->tls_type = GOT_UNKNOWN; | |
1194 | eh->got_types_used = 0; | |
1195 | } | |
1196 | ||
1197 | return entry; | |
1198 | } | |
1199 | ||
78058a5e SL |
1200 | /* Section name for stubs is the associated section name plus this |
1201 | string. */ | |
1202 | #define STUB_SUFFIX ".stub" | |
1203 | ||
1204 | /* Build a name for an entry in the stub hash table. */ | |
1205 | static char * | |
1206 | nios2_stub_name (const asection *input_section, | |
1207 | const asection *sym_sec, | |
1208 | const struct elf32_nios2_link_hash_entry *hh, | |
1209 | const Elf_Internal_Rela *rel, | |
1210 | enum elf32_nios2_stub_type stub_type) | |
1211 | { | |
1212 | char *stub_name; | |
1213 | bfd_size_type len; | |
1214 | char stubpos = (stub_type == nios2_stub_call26_before) ? 'b' : 'a'; | |
1215 | ||
1216 | if (hh) | |
1217 | { | |
1218 | len = 8 + 1 + 1 + 1+ strlen (hh->root.root.root.string) + 1 + 8 + 1; | |
1219 | stub_name = bfd_malloc (len); | |
1220 | if (stub_name != NULL) | |
1221 | { | |
1222 | sprintf (stub_name, "%08x_%c_%s+%x", | |
1223 | input_section->id & 0xffffffff, | |
1224 | stubpos, | |
1225 | hh->root.root.root.string, | |
1226 | (int) rel->r_addend & 0xffffffff); | |
1227 | } | |
1228 | } | |
1229 | else | |
1230 | { | |
1231 | len = 8 + 1 + 1 + 1+ 8 + 1 + 8 + 1 + 8 + 1; | |
1232 | stub_name = bfd_malloc (len); | |
1233 | if (stub_name != NULL) | |
1234 | { | |
1235 | sprintf (stub_name, "%08x_%c_%x:%x+%x", | |
1236 | input_section->id & 0xffffffff, | |
1237 | stubpos, | |
1238 | sym_sec->id & 0xffffffff, | |
1239 | (int) ELF32_R_SYM (rel->r_info) & 0xffffffff, | |
1240 | (int) rel->r_addend & 0xffffffff); | |
1241 | } | |
1242 | } | |
1243 | return stub_name; | |
1244 | } | |
1245 | ||
1246 | /* Look up an entry in the stub hash. Stub entries are cached because | |
1247 | creating the stub name takes a bit of time. */ | |
1248 | static struct elf32_nios2_stub_hash_entry * | |
1249 | nios2_get_stub_entry (const asection *input_section, | |
1250 | const asection *sym_sec, | |
1251 | struct elf32_nios2_link_hash_entry *hh, | |
1252 | const Elf_Internal_Rela *rel, | |
1253 | struct elf32_nios2_link_hash_table *htab, | |
1254 | enum elf32_nios2_stub_type stub_type) | |
1255 | { | |
1256 | struct elf32_nios2_stub_hash_entry *hsh; | |
1257 | const asection *id_sec; | |
1258 | ||
1259 | /* If this input section is part of a group of sections sharing one | |
1260 | stub section, then use the id of the first/last section in the group, | |
1261 | depending on the stub section placement relative to the group. | |
1262 | Stub names need to include a section id, as there may well be | |
1263 | more than one stub used to reach say, printf, and we need to | |
1264 | distinguish between them. */ | |
1265 | if (stub_type == nios2_stub_call26_before) | |
1266 | id_sec = htab->stub_group[input_section->id].first_sec; | |
1267 | else | |
1268 | id_sec = htab->stub_group[input_section->id].last_sec; | |
1269 | ||
1270 | if (hh != NULL && hh->hsh_cache != NULL | |
1271 | && hh->hsh_cache->hh == hh | |
1272 | && hh->hsh_cache->id_sec == id_sec | |
1273 | && hh->hsh_cache->stub_type == stub_type) | |
1274 | { | |
1275 | hsh = hh->hsh_cache; | |
1276 | } | |
1277 | else | |
1278 | { | |
1279 | char *stub_name; | |
1280 | ||
1281 | stub_name = nios2_stub_name (id_sec, sym_sec, hh, rel, stub_type); | |
1282 | if (stub_name == NULL) | |
1283 | return NULL; | |
1284 | ||
1285 | hsh = nios2_stub_hash_lookup (&htab->bstab, | |
1286 | stub_name, FALSE, FALSE); | |
1287 | ||
1288 | if (hh != NULL) | |
1289 | hh->hsh_cache = hsh; | |
1290 | ||
1291 | free (stub_name); | |
1292 | } | |
1293 | ||
1294 | return hsh; | |
1295 | } | |
1296 | ||
1297 | /* Add a new stub entry to the stub hash. Not all fields of the new | |
1298 | stub entry are initialised. */ | |
1299 | static struct elf32_nios2_stub_hash_entry * | |
1300 | nios2_add_stub (const char *stub_name, | |
1301 | asection *section, | |
1302 | struct elf32_nios2_link_hash_table *htab, | |
1303 | enum elf32_nios2_stub_type stub_type) | |
1304 | { | |
1305 | asection *link_sec; | |
1306 | asection *stub_sec; | |
1307 | asection **secptr, **linkptr; | |
1308 | struct elf32_nios2_stub_hash_entry *hsh; | |
1309 | bfd_boolean afterp; | |
1310 | ||
1311 | if (stub_type == nios2_stub_call26_before) | |
1312 | { | |
1313 | link_sec = htab->stub_group[section->id].first_sec; | |
1314 | secptr = &(htab->stub_group[section->id].first_stub_sec); | |
1315 | linkptr = &(htab->stub_group[link_sec->id].first_stub_sec); | |
1316 | afterp = FALSE; | |
1317 | } | |
1318 | else | |
1319 | { | |
1320 | link_sec = htab->stub_group[section->id].last_sec; | |
1321 | secptr = &(htab->stub_group[section->id].last_stub_sec); | |
1322 | linkptr = &(htab->stub_group[link_sec->id].last_stub_sec); | |
1323 | afterp = TRUE; | |
1324 | } | |
1325 | stub_sec = *secptr; | |
1326 | if (stub_sec == NULL) | |
1327 | { | |
1328 | stub_sec = *linkptr; | |
1329 | if (stub_sec == NULL) | |
1330 | { | |
1331 | size_t namelen; | |
1332 | bfd_size_type len; | |
1333 | char *s_name; | |
1334 | ||
1335 | namelen = strlen (link_sec->name); | |
1336 | len = namelen + sizeof (STUB_SUFFIX); | |
1337 | s_name = bfd_alloc (htab->stub_bfd, len); | |
1338 | if (s_name == NULL) | |
1339 | return NULL; | |
1340 | ||
1341 | memcpy (s_name, link_sec->name, namelen); | |
1342 | memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX)); | |
1343 | ||
1344 | stub_sec = (*htab->add_stub_section) (s_name, link_sec, afterp); | |
1345 | if (stub_sec == NULL) | |
1346 | return NULL; | |
1347 | *linkptr = stub_sec; | |
1348 | } | |
1349 | *secptr = stub_sec; | |
1350 | } | |
1351 | ||
1352 | /* Enter this entry into the linker stub hash table. */ | |
1353 | hsh = nios2_stub_hash_lookup (&htab->bstab, stub_name, | |
1354 | TRUE, FALSE); | |
1355 | if (hsh == NULL) | |
1356 | { | |
1357 | (*_bfd_error_handler) (_("%B: cannot create stub entry %s"), | |
1358 | section->owner, | |
1359 | stub_name); | |
1360 | return NULL; | |
1361 | } | |
1362 | ||
1363 | hsh->stub_sec = stub_sec; | |
1364 | hsh->stub_offset = 0; | |
1365 | hsh->id_sec = link_sec; | |
1366 | return hsh; | |
1367 | } | |
1368 | ||
1369 | /* Set up various things so that we can make a list of input sections | |
1370 | for each output section included in the link. Returns -1 on error, | |
1371 | 0 when no stubs will be needed, and 1 on success. */ | |
1372 | int | |
1373 | nios2_elf32_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info) | |
1374 | { | |
1375 | bfd *input_bfd; | |
1376 | unsigned int bfd_count; | |
1377 | int top_id, top_index; | |
1378 | asection *section; | |
1379 | asection **input_list, **list; | |
1380 | bfd_size_type amt; | |
1381 | struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info); | |
1382 | ||
1383 | /* Count the number of input BFDs and find the top input section id. */ | |
1384 | for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0; | |
1385 | input_bfd != NULL; | |
c72f2fb2 | 1386 | input_bfd = input_bfd->link.next) |
78058a5e SL |
1387 | { |
1388 | bfd_count += 1; | |
1389 | for (section = input_bfd->sections; | |
1390 | section != NULL; | |
1391 | section = section->next) | |
1392 | { | |
1393 | if (top_id < section->id) | |
1394 | top_id = section->id; | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | htab->bfd_count = bfd_count; | |
1399 | ||
1400 | amt = sizeof (struct map_stub) * (top_id + 1); | |
1401 | htab->stub_group = bfd_zmalloc (amt); | |
1402 | if (htab->stub_group == NULL) | |
1403 | return -1; | |
1404 | ||
1405 | /* We can't use output_bfd->section_count here to find the top output | |
1406 | section index as some sections may have been removed, and | |
1407 | strip_excluded_output_sections doesn't renumber the indices. */ | |
1408 | for (section = output_bfd->sections, top_index = 0; | |
1409 | section != NULL; | |
1410 | section = section->next) | |
1411 | { | |
1412 | if (top_index < section->index) | |
1413 | top_index = section->index; | |
1414 | } | |
1415 | ||
1416 | htab->top_index = top_index; | |
1417 | amt = sizeof (asection *) * (top_index + 1); | |
1418 | input_list = bfd_malloc (amt); | |
1419 | htab->input_list = input_list; | |
1420 | if (input_list == NULL) | |
1421 | return -1; | |
1422 | ||
1423 | /* For sections we aren't interested in, mark their entries with a | |
1424 | value we can check later. */ | |
1425 | list = input_list + top_index; | |
1426 | do | |
1427 | *list = bfd_abs_section_ptr; | |
1428 | while (list-- != input_list); | |
1429 | ||
1430 | for (section = output_bfd->sections; | |
1431 | section != NULL; | |
1432 | section = section->next) | |
1433 | { | |
1434 | /* FIXME: This is a bit of hack. Currently our .ctors and .dtors | |
1435 | * have PC relative relocs in them but no code flag set. */ | |
1436 | if (((section->flags & SEC_CODE) != 0) || | |
1437 | strcmp(".ctors", section->name) || | |
1438 | strcmp(".dtors", section->name)) | |
1439 | input_list[section->index] = NULL; | |
1440 | } | |
1441 | ||
1442 | return 1; | |
1443 | } | |
1444 | ||
1445 | /* The linker repeatedly calls this function for each input section, | |
1446 | in the order that input sections are linked into output sections. | |
1447 | Build lists of input sections to determine groupings between which | |
1448 | we may insert linker stubs. */ | |
1449 | void | |
1450 | nios2_elf32_next_input_section (struct bfd_link_info *info, asection *isec) | |
1451 | { | |
1452 | struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info); | |
1453 | ||
1454 | if (isec->output_section->index <= htab->top_index) | |
1455 | { | |
1456 | asection **list = htab->input_list + isec->output_section->index; | |
1457 | if (*list != bfd_abs_section_ptr) | |
1458 | { | |
1459 | /* Steal the last_sec pointer for our list. | |
1460 | This happens to make the list in reverse order, | |
1461 | which is what we want. */ | |
1462 | htab->stub_group[isec->id].last_sec = *list; | |
1463 | *list = isec; | |
1464 | } | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | /* Segment mask for CALL26 relocation relaxation. */ | |
1469 | #define CALL26_SEGMENT(x) ((x) & 0xf0000000) | |
1470 | ||
1471 | /* Fudge factor for approximate maximum size of all stubs that might | |
1472 | be inserted by the linker. This does not actually limit the number | |
1473 | of stubs that might be inserted, and only affects strategy for grouping | |
1474 | and placement of stubs. Perhaps this should be computed based on number | |
1475 | of relocations seen, or be specifiable on the command line. */ | |
1476 | #define MAX_STUB_SECTION_SIZE 0xffff | |
1477 | ||
1478 | /* See whether we can group stub sections together. Grouping stub | |
1479 | sections may result in fewer stubs. More importantly, we need to | |
1480 | put all .init* and .fini* stubs at the end of the .init or | |
1481 | .fini output sections respectively, because glibc splits the | |
1482 | _init and _fini functions into multiple parts. Putting a stub in | |
1483 | the middle of a function is not a good idea. | |
1484 | Rather than computing groups of a maximum fixed size, for Nios II | |
1485 | CALL26 relaxation it makes more sense to compute the groups based on | |
1486 | sections that fit within a 256MB address segment. Also do not allow | |
1487 | a group to span more than one output section, since different output | |
1488 | sections might correspond to different memory banks on a bare-metal | |
1489 | target, etc. */ | |
1490 | static void | |
1491 | group_sections (struct elf32_nios2_link_hash_table *htab) | |
1492 | { | |
1493 | asection **list = htab->input_list + htab->top_index; | |
1494 | do | |
1495 | { | |
1496 | /* The list is in reverse order so we'll search backwards looking | |
1497 | for the first section that begins in the same memory segment, | |
1498 | marking sections along the way to point at the tail for this | |
1499 | group. */ | |
1500 | asection *tail = *list; | |
1501 | if (tail == bfd_abs_section_ptr) | |
1502 | continue; | |
1503 | while (tail != NULL) | |
1504 | { | |
1505 | bfd_vma start = tail->output_section->vma + tail->output_offset; | |
1506 | bfd_vma end = start + tail->size; | |
1507 | bfd_vma segment = CALL26_SEGMENT (end); | |
1508 | asection *prev; | |
1509 | ||
1510 | if (segment != CALL26_SEGMENT (start) | |
1511 | || segment != CALL26_SEGMENT (end + MAX_STUB_SECTION_SIZE)) | |
1512 | /* This section spans more than one memory segment, or is | |
1513 | close enough to the end of the segment that adding stub | |
1514 | sections before it might cause it to move so that it | |
1515 | spans memory segments, or that stubs added at the end of | |
1516 | this group might overflow into the next memory segment. | |
1517 | Put it in a group by itself to localize the effects. */ | |
1518 | { | |
1519 | prev = htab->stub_group[tail->id].last_sec; | |
1520 | htab->stub_group[tail->id].last_sec = tail; | |
1521 | htab->stub_group[tail->id].first_sec = tail; | |
1522 | } | |
1523 | else | |
1524 | /* Collect more sections for this group. */ | |
1525 | { | |
1526 | asection *curr, *first; | |
1527 | for (curr = tail; ; curr = prev) | |
1528 | { | |
1529 | prev = htab->stub_group[curr->id].last_sec; | |
1530 | if (!prev | |
1531 | || tail->output_section != prev->output_section | |
1532 | || (CALL26_SEGMENT (prev->output_section->vma | |
1533 | + prev->output_offset) | |
1534 | != segment)) | |
1535 | break; | |
1536 | } | |
1537 | first = curr; | |
1538 | for (curr = tail; ; curr = prev) | |
1539 | { | |
1540 | prev = htab->stub_group[curr->id].last_sec; | |
1541 | htab->stub_group[curr->id].last_sec = tail; | |
1542 | htab->stub_group[curr->id].first_sec = first; | |
1543 | if (curr == first) | |
1544 | break; | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | /* Reset tail for the next group. */ | |
1549 | tail = prev; | |
1550 | } | |
1551 | } | |
1552 | while (list-- != htab->input_list); | |
1553 | free (htab->input_list); | |
1554 | } | |
1555 | ||
1556 | /* Determine the type of stub needed, if any, for a call. */ | |
1557 | static enum elf32_nios2_stub_type | |
1558 | nios2_type_of_stub (asection *input_sec, | |
1559 | const Elf_Internal_Rela *rel, | |
1560 | struct elf32_nios2_link_hash_entry *hh, | |
1561 | struct elf32_nios2_link_hash_table *htab, | |
1562 | bfd_vma destination, | |
1563 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
1564 | { | |
1565 | bfd_vma location, segment, start, end; | |
1566 | asection *s0, *s1, *s; | |
1567 | ||
1568 | if (hh != NULL && | |
1569 | !(hh->root.root.type == bfd_link_hash_defined | |
1570 | || hh->root.root.type == bfd_link_hash_defweak)) | |
1571 | return nios2_stub_none; | |
1572 | ||
1573 | /* Determine where the call point is. */ | |
1574 | location = (input_sec->output_section->vma | |
1575 | + input_sec->output_offset + rel->r_offset); | |
1576 | segment = CALL26_SEGMENT (location); | |
1577 | ||
1578 | /* Nios II CALL and JMPI instructions can transfer control to addresses | |
1579 | within the same 256MB segment as the PC. */ | |
1580 | if (segment == CALL26_SEGMENT (destination)) | |
1581 | return nios2_stub_none; | |
1582 | ||
1583 | /* Find the start and end addresses of the stub group. Also account for | |
1584 | any already-created stub sections for this group. Note that for stubs | |
1585 | in the end section, only the first instruction of the last stub | |
1586 | (12 bytes long) needs to be within range. */ | |
1587 | s0 = htab->stub_group[input_sec->id].first_sec; | |
1588 | s = htab->stub_group[s0->id].first_stub_sec; | |
1589 | if (s != NULL && s->size > 0) | |
1590 | start = s->output_section->vma + s->output_offset; | |
1591 | else | |
1592 | start = s0->output_section->vma + s0->output_offset; | |
1593 | ||
1594 | s1 = htab->stub_group[input_sec->id].last_sec; | |
1595 | s = htab->stub_group[s1->id].last_stub_sec; | |
1596 | if (s != NULL && s->size > 0) | |
1597 | end = s->output_section->vma + s->output_offset + s->size - 8; | |
1598 | else | |
1599 | end = s1->output_section->vma + s1->output_offset + s1->size; | |
1600 | ||
1601 | BFD_ASSERT (start < end); | |
1602 | BFD_ASSERT (start <= location); | |
1603 | BFD_ASSERT (location < end); | |
1604 | ||
1605 | /* Put stubs at the end of the group unless that is not a valid | |
1606 | location and the beginning of the group is. It might be that | |
1607 | neither the beginning nor end works if we have an input section | |
1608 | so large that it spans multiple segment boundaries. In that | |
1609 | case, punt; the end result will be a relocation overflow error no | |
1610 | matter what we do here. | |
1611 | ||
1612 | Note that adding stubs pushes up the addresses of all subsequent | |
1613 | sections, so that stubs allocated on one pass through the | |
1614 | relaxation loop may not be valid on the next pass. (E.g., we may | |
1615 | allocate a stub at the beginning of the section on one pass and | |
1616 | find that the call site has been bumped into the next memory | |
1617 | segment on the next pass.) The important thing to note is that | |
1618 | we never try to reclaim the space allocated to such unused stubs, | |
1619 | so code size and section addresses can only increase with each | |
1620 | iteration. Accounting for the start and end addresses of the | |
1621 | already-created stub sections ensures that when the algorithm | |
1622 | converges, it converges accurately, with the entire appropriate | |
1623 | stub section accessible from the call site and not just the | |
1624 | address at the start or end of the stub group proper. */ | |
1625 | ||
1626 | if (segment == CALL26_SEGMENT (end)) | |
1627 | return nios2_stub_call26_after; | |
1628 | else if (segment == CALL26_SEGMENT (start)) | |
1629 | return nios2_stub_call26_before; | |
1630 | else | |
1631 | /* Perhaps this should be a dedicated error code. */ | |
1632 | return nios2_stub_none; | |
1633 | } | |
1634 | ||
1635 | static bfd_boolean | |
1636 | nios2_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED) | |
1637 | { | |
1638 | struct elf32_nios2_stub_hash_entry *hsh | |
1639 | = (struct elf32_nios2_stub_hash_entry *) gen_entry; | |
1640 | asection *stub_sec = hsh->stub_sec; | |
1641 | bfd_vma sym_value; | |
1642 | ||
1643 | /* Make a note of the offset within the stubs for this entry. */ | |
1644 | hsh->stub_offset = stub_sec->size; | |
1645 | ||
1646 | switch (hsh->stub_type) | |
1647 | { | |
1648 | case nios2_stub_call26_before: | |
1649 | case nios2_stub_call26_after: | |
1650 | /* A call26 stub looks like: | |
1651 | orhi at, %hiadj(dest) | |
1652 | addi at, at, %lo(dest) | |
1653 | jmp at | |
1654 | Note that call/jmpi instructions can't be used in PIC code | |
1655 | so there is no reason for the stub to be PIC, either. */ | |
1656 | sym_value = (hsh->target_value | |
1657 | + hsh->target_section->output_offset | |
1658 | + hsh->target_section->output_section->vma | |
1659 | + hsh->addend); | |
1660 | ||
1661 | nios2_elf32_install_data (stub_sec, nios2_call26_stub_entry, | |
1662 | hsh->stub_offset, 3); | |
1663 | nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset, | |
1664 | hiadj (sym_value)); | |
1665 | nios2_elf32_install_imm16 (stub_sec, hsh->stub_offset + 4, | |
1666 | (sym_value & 0xffff)); | |
1667 | stub_sec->size += 12; | |
1668 | break; | |
1669 | default: | |
1670 | BFD_FAIL (); | |
1671 | return FALSE; | |
1672 | } | |
1673 | ||
1674 | return TRUE; | |
1675 | } | |
1676 | ||
1677 | /* As above, but don't actually build the stub. Just bump offset so | |
1678 | we know stub section sizes. */ | |
1679 | static bfd_boolean | |
1680 | nios2_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED) | |
1681 | { | |
1682 | struct elf32_nios2_stub_hash_entry *hsh | |
1683 | = (struct elf32_nios2_stub_hash_entry *) gen_entry; | |
1684 | ||
1685 | switch (hsh->stub_type) | |
1686 | { | |
1687 | case nios2_stub_call26_before: | |
1688 | case nios2_stub_call26_after: | |
1689 | hsh->stub_sec->size += 12; | |
1690 | break; | |
1691 | default: | |
1692 | BFD_FAIL (); | |
1693 | return FALSE; | |
1694 | } | |
1695 | return TRUE; | |
1696 | } | |
1697 | ||
1698 | /* Read in all local syms for all input bfds. | |
1699 | Returns -1 on error, 0 otherwise. */ | |
1700 | ||
1701 | static int | |
1702 | get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd, | |
1703 | struct bfd_link_info *info) | |
1704 | { | |
1705 | unsigned int bfd_indx; | |
1706 | Elf_Internal_Sym *local_syms, **all_local_syms; | |
1707 | struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info); | |
1708 | ||
1709 | /* We want to read in symbol extension records only once. To do this | |
1710 | we need to read in the local symbols in parallel and save them for | |
1711 | later use; so hold pointers to the local symbols in an array. */ | |
1712 | bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count; | |
1713 | all_local_syms = bfd_zmalloc (amt); | |
1714 | htab->all_local_syms = all_local_syms; | |
1715 | if (all_local_syms == NULL) | |
1716 | return -1; | |
1717 | ||
1718 | /* Walk over all the input BFDs, swapping in local symbols. */ | |
1719 | for (bfd_indx = 0; | |
1720 | input_bfd != NULL; | |
c72f2fb2 | 1721 | input_bfd = input_bfd->link.next, bfd_indx++) |
78058a5e SL |
1722 | { |
1723 | Elf_Internal_Shdr *symtab_hdr; | |
1724 | ||
1725 | /* We'll need the symbol table in a second. */ | |
1726 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1727 | if (symtab_hdr->sh_info == 0) | |
1728 | continue; | |
1729 | ||
1730 | /* We need an array of the local symbols attached to the input bfd. */ | |
1731 | local_syms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1732 | if (local_syms == NULL) | |
1733 | { | |
1734 | local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, | |
1735 | symtab_hdr->sh_info, 0, | |
1736 | NULL, NULL, NULL); | |
1737 | /* Cache them for elf_link_input_bfd. */ | |
1738 | symtab_hdr->contents = (unsigned char *) local_syms; | |
1739 | } | |
1740 | if (local_syms == NULL) | |
1741 | return -1; | |
1742 | ||
1743 | all_local_syms[bfd_indx] = local_syms; | |
1744 | } | |
1745 | ||
1746 | return 0; | |
1747 | } | |
1748 | ||
1749 | /* Determine and set the size of the stub section for a final link. */ | |
1750 | bfd_boolean | |
1751 | nios2_elf32_size_stubs (bfd *output_bfd, bfd *stub_bfd, | |
1752 | struct bfd_link_info *info, | |
1753 | asection *(*add_stub_section) (const char *, | |
1754 | asection *, bfd_boolean), | |
1755 | void (*layout_sections_again) (void)) | |
1756 | { | |
1757 | bfd_boolean stub_changed = FALSE; | |
1758 | struct elf32_nios2_link_hash_table *htab = elf32_nios2_hash_table (info); | |
1759 | ||
1760 | /* Stash our params away. */ | |
1761 | htab->stub_bfd = stub_bfd; | |
1762 | htab->add_stub_section = add_stub_section; | |
1763 | htab->layout_sections_again = layout_sections_again; | |
1764 | ||
1765 | /* FIXME: We only compute the section groups once. This could cause | |
1766 | problems if adding a large stub section causes following sections, | |
1767 | or parts of them, to move into another segment. However, this seems | |
1768 | to be consistent with the way other back ends handle this.... */ | |
1769 | group_sections (htab); | |
1770 | ||
1771 | if (get_local_syms (output_bfd, info->input_bfds, info)) | |
1772 | { | |
1773 | if (htab->all_local_syms) | |
1774 | goto error_ret_free_local; | |
1775 | return FALSE; | |
1776 | } | |
1777 | ||
1778 | while (1) | |
1779 | { | |
1780 | bfd *input_bfd; | |
1781 | unsigned int bfd_indx; | |
1782 | asection *stub_sec; | |
1783 | ||
1784 | for (input_bfd = info->input_bfds, bfd_indx = 0; | |
1785 | input_bfd != NULL; | |
c72f2fb2 | 1786 | input_bfd = input_bfd->link.next, bfd_indx++) |
78058a5e SL |
1787 | { |
1788 | Elf_Internal_Shdr *symtab_hdr; | |
1789 | asection *section; | |
1790 | Elf_Internal_Sym *local_syms; | |
1791 | ||
1792 | /* We'll need the symbol table in a second. */ | |
1793 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1794 | if (symtab_hdr->sh_info == 0) | |
1795 | continue; | |
1796 | ||
1797 | local_syms = htab->all_local_syms[bfd_indx]; | |
1798 | ||
1799 | /* Walk over each section attached to the input bfd. */ | |
1800 | for (section = input_bfd->sections; | |
1801 | section != NULL; | |
1802 | section = section->next) | |
1803 | { | |
1804 | Elf_Internal_Rela *internal_relocs, *irelaend, *irela; | |
1805 | ||
1806 | /* If there aren't any relocs, then there's nothing more | |
1807 | to do. */ | |
1808 | if ((section->flags & SEC_RELOC) == 0 | |
1809 | || section->reloc_count == 0) | |
1810 | continue; | |
1811 | ||
1812 | /* If this section is a link-once section that will be | |
1813 | discarded, then don't create any stubs. */ | |
1814 | if (section->output_section == NULL | |
1815 | || section->output_section->owner != output_bfd) | |
1816 | continue; | |
1817 | ||
1818 | /* Get the relocs. */ | |
1819 | internal_relocs | |
1820 | = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL, | |
1821 | info->keep_memory); | |
1822 | if (internal_relocs == NULL) | |
1823 | goto error_ret_free_local; | |
1824 | ||
1825 | /* Now examine each relocation. */ | |
1826 | irela = internal_relocs; | |
1827 | irelaend = irela + section->reloc_count; | |
1828 | for (; irela < irelaend; irela++) | |
1829 | { | |
1830 | unsigned int r_type, r_indx; | |
1831 | enum elf32_nios2_stub_type stub_type; | |
1832 | struct elf32_nios2_stub_hash_entry *hsh; | |
1833 | asection *sym_sec; | |
1834 | bfd_vma sym_value; | |
1835 | bfd_vma destination; | |
1836 | struct elf32_nios2_link_hash_entry *hh; | |
1837 | char *stub_name; | |
1838 | const asection *id_sec; | |
1839 | ||
1840 | r_type = ELF32_R_TYPE (irela->r_info); | |
1841 | r_indx = ELF32_R_SYM (irela->r_info); | |
1842 | ||
1843 | if (r_type >= (unsigned int) R_NIOS2_ILLEGAL) | |
1844 | { | |
1845 | bfd_set_error (bfd_error_bad_value); | |
1846 | error_ret_free_internal: | |
1847 | if (elf_section_data (section)->relocs == NULL) | |
1848 | free (internal_relocs); | |
1849 | goto error_ret_free_local; | |
1850 | } | |
1851 | ||
1852 | /* Only look for stubs on CALL and JMPI instructions. */ | |
1853 | if (r_type != (unsigned int) R_NIOS2_CALL26) | |
1854 | continue; | |
1855 | ||
1856 | /* Now determine the call target, its name, value, | |
1857 | section. */ | |
1858 | sym_sec = NULL; | |
1859 | sym_value = 0; | |
1860 | destination = 0; | |
1861 | hh = NULL; | |
1862 | if (r_indx < symtab_hdr->sh_info) | |
1863 | { | |
1864 | /* It's a local symbol. */ | |
1865 | Elf_Internal_Sym *sym; | |
1866 | Elf_Internal_Shdr *hdr; | |
1867 | unsigned int shndx; | |
1868 | ||
1869 | sym = local_syms + r_indx; | |
1870 | if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) | |
1871 | sym_value = sym->st_value; | |
1872 | shndx = sym->st_shndx; | |
1873 | if (shndx < elf_numsections (input_bfd)) | |
1874 | { | |
1875 | hdr = elf_elfsections (input_bfd)[shndx]; | |
1876 | sym_sec = hdr->bfd_section; | |
1877 | destination = (sym_value + irela->r_addend | |
1878 | + sym_sec->output_offset | |
1879 | + sym_sec->output_section->vma); | |
1880 | } | |
1881 | } | |
1882 | else | |
1883 | { | |
1884 | /* It's an external symbol. */ | |
1885 | int e_indx; | |
1886 | ||
1887 | e_indx = r_indx - symtab_hdr->sh_info; | |
1888 | hh = ((struct elf32_nios2_link_hash_entry *) | |
1889 | elf_sym_hashes (input_bfd)[e_indx]); | |
1890 | ||
1891 | while (hh->root.root.type == bfd_link_hash_indirect | |
1892 | || hh->root.root.type == bfd_link_hash_warning) | |
1893 | hh = ((struct elf32_nios2_link_hash_entry *) | |
1894 | hh->root.root.u.i.link); | |
1895 | ||
1896 | if (hh->root.root.type == bfd_link_hash_defined | |
1897 | || hh->root.root.type == bfd_link_hash_defweak) | |
1898 | { | |
1899 | sym_sec = hh->root.root.u.def.section; | |
1900 | sym_value = hh->root.root.u.def.value; | |
1901 | ||
1902 | if (sym_sec->output_section != NULL) | |
1903 | destination = (sym_value + irela->r_addend | |
1904 | + sym_sec->output_offset | |
1905 | + sym_sec->output_section->vma); | |
1906 | else | |
1907 | continue; | |
1908 | } | |
1909 | else if (hh->root.root.type == bfd_link_hash_undefweak) | |
1910 | { | |
1911 | if (! info->shared) | |
1912 | continue; | |
1913 | } | |
1914 | else if (hh->root.root.type == bfd_link_hash_undefined) | |
1915 | { | |
1916 | if (! (info->unresolved_syms_in_objects == RM_IGNORE | |
1917 | && (ELF_ST_VISIBILITY (hh->root.other) | |
1918 | == STV_DEFAULT))) | |
1919 | continue; | |
1920 | } | |
1921 | else | |
1922 | { | |
1923 | bfd_set_error (bfd_error_bad_value); | |
1924 | goto error_ret_free_internal; | |
1925 | } | |
1926 | } | |
1927 | ||
1928 | /* Determine what (if any) linker stub is needed. */ | |
1929 | stub_type = nios2_type_of_stub (section, irela, hh, htab, | |
1930 | destination, info); | |
1931 | if (stub_type == nios2_stub_none) | |
1932 | continue; | |
1933 | ||
1934 | /* Support for grouping stub sections. */ | |
1935 | if (stub_type == nios2_stub_call26_before) | |
1936 | id_sec = htab->stub_group[section->id].first_sec; | |
1937 | else | |
1938 | id_sec = htab->stub_group[section->id].last_sec; | |
1939 | ||
1940 | /* Get the name of this stub. */ | |
1941 | stub_name = nios2_stub_name (id_sec, sym_sec, hh, irela, | |
1942 | stub_type); | |
1943 | if (!stub_name) | |
1944 | goto error_ret_free_internal; | |
1945 | ||
1946 | hsh = nios2_stub_hash_lookup (&htab->bstab, | |
1947 | stub_name, | |
1948 | FALSE, FALSE); | |
1949 | if (hsh != NULL) | |
1950 | { | |
1951 | /* The proper stub has already been created. */ | |
1952 | free (stub_name); | |
1953 | continue; | |
1954 | } | |
1955 | ||
1956 | hsh = nios2_add_stub (stub_name, section, htab, stub_type); | |
1957 | if (hsh == NULL) | |
1958 | { | |
1959 | free (stub_name); | |
1960 | goto error_ret_free_internal; | |
1961 | } | |
1962 | hsh->target_value = sym_value; | |
1963 | hsh->target_section = sym_sec; | |
1964 | hsh->stub_type = stub_type; | |
1965 | hsh->hh = hh; | |
1966 | hsh->addend = irela->r_addend; | |
1967 | stub_changed = TRUE; | |
1968 | } | |
1969 | ||
1970 | /* We're done with the internal relocs, free them. */ | |
1971 | if (elf_section_data (section)->relocs == NULL) | |
1972 | free (internal_relocs); | |
1973 | } | |
1974 | } | |
1975 | ||
1976 | if (!stub_changed) | |
1977 | break; | |
1978 | ||
1979 | /* OK, we've added some stubs. Find out the new size of the | |
1980 | stub sections. */ | |
1981 | for (stub_sec = htab->stub_bfd->sections; | |
1982 | stub_sec != NULL; | |
1983 | stub_sec = stub_sec->next) | |
1984 | stub_sec->size = 0; | |
1985 | ||
1986 | bfd_hash_traverse (&htab->bstab, nios2_size_one_stub, htab); | |
1987 | ||
1988 | /* Ask the linker to do its stuff. */ | |
1989 | (*htab->layout_sections_again) (); | |
1990 | stub_changed = FALSE; | |
1991 | } | |
1992 | ||
1993 | free (htab->all_local_syms); | |
1994 | return TRUE; | |
1995 | ||
1996 | error_ret_free_local: | |
1997 | free (htab->all_local_syms); | |
1998 | return FALSE; | |
1999 | } | |
2000 | ||
2001 | /* Build all the stubs associated with the current output file. The | |
2002 | stubs are kept in a hash table attached to the main linker hash | |
2003 | table. This function is called via nios2elf_finish in the linker. */ | |
2004 | bfd_boolean | |
2005 | nios2_elf32_build_stubs (struct bfd_link_info *info) | |
2006 | { | |
2007 | asection *stub_sec; | |
2008 | struct bfd_hash_table *table; | |
2009 | struct elf32_nios2_link_hash_table *htab; | |
2010 | ||
2011 | htab = elf32_nios2_hash_table (info); | |
2012 | ||
2013 | for (stub_sec = htab->stub_bfd->sections; | |
2014 | stub_sec != NULL; | |
2015 | stub_sec = stub_sec->next) | |
1511baec SL |
2016 | /* The stub_bfd may contain non-stub sections if it is also the |
2017 | dynobj. Any such non-stub sections are created with the | |
2018 | SEC_LINKER_CREATED flag set, while stub sections do not | |
2019 | have that flag. Ignore any non-stub sections here. */ | |
2020 | if ((stub_sec->flags & SEC_LINKER_CREATED) == 0) | |
2021 | { | |
2022 | bfd_size_type size; | |
2023 | ||
2024 | /* Allocate memory to hold the linker stubs. */ | |
2025 | size = stub_sec->size; | |
2026 | stub_sec->contents = bfd_zalloc (htab->stub_bfd, size); | |
2027 | if (stub_sec->contents == NULL && size != 0) | |
2028 | return FALSE; | |
2029 | stub_sec->size = 0; | |
2030 | } | |
78058a5e SL |
2031 | |
2032 | /* Build the stubs as directed by the stub hash table. */ | |
2033 | table = &htab->bstab; | |
2034 | bfd_hash_traverse (table, nios2_build_one_stub, info); | |
2035 | ||
2036 | return TRUE; | |
2037 | } | |
2038 | ||
2039 | ||
36591ba1 SL |
2040 | /* Implement bfd_elf32_bfd_reloc_type_lookup: |
2041 | Given a BFD reloc type, return a howto structure. */ | |
2042 | static reloc_howto_type * | |
2043 | nios2_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
2044 | bfd_reloc_code_real_type code) | |
2045 | { | |
2046 | int i; | |
2047 | for (i = 0; | |
2048 | i < (int) (sizeof (nios2_reloc_map) / sizeof (struct elf_reloc_map)); | |
2049 | ++i) | |
2050 | if (nios2_reloc_map[i].bfd_val == code) | |
2051 | return &elf_nios2_howto_table_rel[(int) nios2_reloc_map[i].elf_val]; | |
2052 | return NULL; | |
2053 | } | |
2054 | ||
2055 | /* Implement bfd_elf32_bfd_reloc_name_lookup: | |
2056 | Given a reloc name, return a howto structure. */ | |
2057 | static reloc_howto_type * | |
2058 | nios2_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
2059 | const char *r_name) | |
2060 | { | |
2061 | unsigned int i; | |
2062 | for (i = 0; | |
2063 | i < (sizeof (elf_nios2_howto_table_rel) | |
2064 | / sizeof (elf_nios2_howto_table_rel[0])); | |
2065 | i++) | |
2066 | if (elf_nios2_howto_table_rel[i].name | |
2067 | && strcasecmp (elf_nios2_howto_table_rel[i].name, r_name) == 0) | |
2068 | return &elf_nios2_howto_table_rel[i]; | |
2069 | ||
2070 | return NULL; | |
2071 | } | |
2072 | ||
2073 | /* Implement elf_info_to_howto: | |
2074 | Given a ELF32 relocation, fill in a arelent structure. */ | |
2075 | static void | |
2076 | nios2_elf32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr, | |
2077 | Elf_Internal_Rela *dst) | |
2078 | { | |
2079 | unsigned int r_type; | |
2080 | ||
2081 | r_type = ELF32_R_TYPE (dst->r_info); | |
2082 | BFD_ASSERT (r_type < R_NIOS2_ILLEGAL); | |
2083 | cache_ptr->howto = &elf_nios2_howto_table_rel[r_type]; | |
2084 | } | |
2085 | ||
2086 | /* Return the base VMA address which should be subtracted from real addresses | |
2087 | when resolving @dtpoff relocation. | |
2088 | This is PT_TLS segment p_vaddr. */ | |
2089 | static bfd_vma | |
2090 | dtpoff_base (struct bfd_link_info *info) | |
2091 | { | |
2092 | /* If tls_sec is NULL, we should have signalled an error already. */ | |
2093 | if (elf_hash_table (info)->tls_sec == NULL) | |
2094 | return 0; | |
2095 | return elf_hash_table (info)->tls_sec->vma; | |
2096 | } | |
2097 | ||
2098 | /* Return the relocation value for @tpoff relocation | |
2099 | if STT_TLS virtual address is ADDRESS. */ | |
2100 | static bfd_vma | |
2101 | tpoff (struct bfd_link_info *info, bfd_vma address) | |
2102 | { | |
2103 | struct elf_link_hash_table *htab = elf_hash_table (info); | |
2104 | ||
2105 | /* If tls_sec is NULL, we should have signalled an error already. */ | |
2106 | if (htab->tls_sec == NULL) | |
2107 | return 0; | |
2108 | return address - htab->tls_sec->vma; | |
2109 | } | |
2110 | ||
2111 | /* Set the GP value for OUTPUT_BFD. Returns FALSE if this is a | |
2112 | dangerous relocation. */ | |
2113 | static bfd_boolean | |
2114 | nios2_elf_assign_gp (bfd *output_bfd, bfd_vma *pgp, struct bfd_link_info *info) | |
2115 | { | |
2116 | ||
2117 | bfd_boolean gp_found; | |
2118 | struct bfd_hash_entry *h; | |
2119 | struct bfd_link_hash_entry *lh; | |
2120 | ||
2121 | /* If we've already figured out what GP will be, just return it. */ | |
2122 | *pgp = _bfd_get_gp_value (output_bfd); | |
2123 | if (*pgp) | |
2124 | return TRUE; | |
2125 | ||
2126 | h = bfd_hash_lookup (&info->hash->table, "_gp", FALSE, FALSE); | |
2127 | lh = (struct bfd_link_hash_entry *) h; | |
2128 | lookup: | |
2129 | if (lh) | |
2130 | { | |
2131 | switch (lh->type) | |
2132 | { | |
2133 | case bfd_link_hash_undefined: | |
2134 | case bfd_link_hash_undefweak: | |
2135 | case bfd_link_hash_common: | |
2136 | gp_found = FALSE; | |
2137 | break; | |
2138 | case bfd_link_hash_defined: | |
2139 | case bfd_link_hash_defweak: | |
2140 | gp_found = TRUE; | |
2141 | *pgp = lh->u.def.value; | |
2142 | break; | |
2143 | case bfd_link_hash_indirect: | |
2144 | case bfd_link_hash_warning: | |
2145 | lh = lh->u.i.link; | |
2146 | /* @@FIXME ignoring warning for now */ | |
2147 | goto lookup; | |
2148 | case bfd_link_hash_new: | |
2149 | default: | |
2150 | abort (); | |
2151 | } | |
2152 | } | |
2153 | else | |
2154 | gp_found = FALSE; | |
2155 | ||
2156 | if (!gp_found) | |
2157 | { | |
2158 | /* Only get the error once. */ | |
2159 | *pgp = 4; | |
2160 | _bfd_set_gp_value (output_bfd, *pgp); | |
2161 | return FALSE; | |
2162 | } | |
2163 | ||
2164 | _bfd_set_gp_value (output_bfd, *pgp); | |
2165 | ||
2166 | return TRUE; | |
2167 | } | |
2168 | ||
2169 | /* Retrieve the previously cached _gp pointer, returning bfd_reloc_dangerous | |
2170 | if it's not available as we don't have a link_info pointer available here | |
2171 | to look it up in the output symbol table. We don't need to adjust the | |
2172 | symbol value for an external symbol if we are producing relocatable | |
2173 | output. */ | |
2174 | static bfd_reloc_status_type | |
2175 | nios2_elf_final_gp (bfd *output_bfd, asymbol *symbol, bfd_boolean relocatable, | |
2176 | char **error_message, bfd_vma *pgp) | |
2177 | { | |
2178 | if (bfd_is_und_section (symbol->section) && !relocatable) | |
2179 | { | |
2180 | *pgp = 0; | |
2181 | return bfd_reloc_undefined; | |
2182 | } | |
2183 | ||
2184 | *pgp = _bfd_get_gp_value (output_bfd); | |
2185 | if (*pgp == 0 && (!relocatable || (symbol->flags & BSF_SECTION_SYM) != 0)) | |
2186 | { | |
2187 | if (relocatable) | |
2188 | { | |
2189 | /* Make up a value. */ | |
2190 | *pgp = symbol->section->output_section->vma + 0x4000; | |
2191 | _bfd_set_gp_value (output_bfd, *pgp); | |
2192 | } | |
2193 | else | |
2194 | { | |
2195 | *error_message | |
2196 | = (char *) _("global pointer relative relocation when _gp not defined"); | |
2197 | return bfd_reloc_dangerous; | |
2198 | } | |
2199 | } | |
2200 | ||
2201 | return bfd_reloc_ok; | |
2202 | } | |
2203 | ||
36591ba1 SL |
2204 | /* Do the relocations that require special handling. */ |
2205 | static bfd_reloc_status_type | |
2206 | nios2_elf32_do_hi16_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2207 | asection *input_section, |
36591ba1 SL |
2208 | bfd_byte *data, bfd_vma offset, |
2209 | bfd_vma symbol_value, bfd_vma addend) | |
2210 | { | |
2211 | symbol_value = symbol_value + addend; | |
2212 | addend = 0; | |
2213 | symbol_value = (symbol_value >> 16) & 0xffff; | |
2214 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2215 | data, offset, symbol_value, addend); | |
2216 | } | |
2217 | ||
2218 | static bfd_reloc_status_type | |
2219 | nios2_elf32_do_lo16_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2220 | asection *input_section, |
36591ba1 SL |
2221 | bfd_byte *data, bfd_vma offset, |
2222 | bfd_vma symbol_value, bfd_vma addend) | |
2223 | { | |
2224 | symbol_value = symbol_value + addend; | |
2225 | addend = 0; | |
2226 | symbol_value = symbol_value & 0xffff; | |
2227 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2228 | data, offset, symbol_value, addend); | |
2229 | } | |
2230 | ||
2231 | static bfd_reloc_status_type | |
2232 | nios2_elf32_do_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2233 | asection *input_section, |
36591ba1 SL |
2234 | bfd_byte *data, bfd_vma offset, |
2235 | bfd_vma symbol_value, bfd_vma addend) | |
2236 | { | |
2237 | symbol_value = symbol_value + addend; | |
2238 | addend = 0; | |
2239 | symbol_value = hiadj(symbol_value); | |
2240 | return _bfd_final_link_relocate (howto, abfd, input_section, data, offset, | |
2241 | symbol_value, addend); | |
2242 | } | |
2243 | ||
2244 | static bfd_reloc_status_type | |
2245 | nios2_elf32_do_pcrel_lo16_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2246 | asection *input_section, |
36591ba1 SL |
2247 | bfd_byte *data, bfd_vma offset, |
2248 | bfd_vma symbol_value, bfd_vma addend) | |
2249 | { | |
2250 | symbol_value = symbol_value + addend; | |
2251 | addend = 0; | |
2252 | symbol_value = symbol_value & 0xffff; | |
2253 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2254 | data, offset, symbol_value, addend); | |
2255 | } | |
2256 | ||
2257 | static bfd_reloc_status_type | |
2258 | nios2_elf32_do_pcrel_hiadj16_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2259 | asection *input_section, |
36591ba1 SL |
2260 | bfd_byte *data, bfd_vma offset, |
2261 | bfd_vma symbol_value, bfd_vma addend) | |
2262 | { | |
2263 | symbol_value = symbol_value + addend; | |
2264 | symbol_value -= (input_section->output_section->vma | |
2265 | + input_section->output_offset); | |
2266 | symbol_value -= offset; | |
2267 | addend = 0; | |
2268 | symbol_value = hiadj(symbol_value); | |
2269 | return _bfd_final_link_relocate (howto, abfd, input_section, data, offset, | |
2270 | symbol_value, addend); | |
2271 | } | |
2272 | ||
2273 | static bfd_reloc_status_type | |
2274 | nios2_elf32_do_pcrel16_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2275 | asection *input_section, |
36591ba1 SL |
2276 | bfd_byte *data, bfd_vma offset, |
2277 | bfd_vma symbol_value, bfd_vma addend) | |
2278 | { | |
2279 | /* NIOS2 pc relative relocations are relative to the next 32-bit instruction | |
2280 | so we need to subtract 4 before doing a final_link_relocate. */ | |
2281 | symbol_value = symbol_value + addend - 4; | |
2282 | addend = 0; | |
2283 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2284 | data, offset, symbol_value, addend); | |
2285 | } | |
2286 | ||
2287 | static bfd_reloc_status_type | |
2288 | nios2_elf32_do_call26_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2289 | asection *input_section, |
36591ba1 SL |
2290 | bfd_byte *data, bfd_vma offset, |
2291 | bfd_vma symbol_value, bfd_vma addend) | |
2292 | { | |
2293 | /* Check that the relocation is in the same page as the current address. */ | |
78058a5e SL |
2294 | if (CALL26_SEGMENT (symbol_value + addend) |
2295 | != CALL26_SEGMENT (input_section->output_section->vma | |
2296 | + input_section->output_offset | |
2297 | + offset)) | |
36591ba1 SL |
2298 | return bfd_reloc_overflow; |
2299 | ||
2300 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2301 | data, offset, symbol_value, addend); | |
2302 | } | |
2303 | ||
2304 | static bfd_reloc_status_type | |
2305 | nios2_elf32_do_gprel_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2306 | asection *input_section, |
36591ba1 SL |
2307 | bfd_byte *data, bfd_vma offset, |
2308 | bfd_vma symbol_value, bfd_vma addend) | |
2309 | { | |
2310 | /* Because we need the output_bfd, the special handling is done | |
2311 | in nios2_elf32_relocate_section or in nios2_elf32_gprel_relocate. */ | |
2312 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2313 | data, offset, symbol_value, addend); | |
2314 | } | |
2315 | ||
2316 | static bfd_reloc_status_type | |
2317 | nios2_elf32_do_ujmp_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2318 | asection *input_section, |
36591ba1 SL |
2319 | bfd_byte *data, bfd_vma offset, |
2320 | bfd_vma symbol_value, bfd_vma addend) | |
2321 | { | |
2322 | bfd_vma symbol_lo16, symbol_hi16; | |
2323 | bfd_reloc_status_type r; | |
2324 | symbol_value = symbol_value + addend; | |
2325 | addend = 0; | |
2326 | symbol_hi16 = (symbol_value >> 16) & 0xffff; | |
2327 | symbol_lo16 = symbol_value & 0xffff; | |
2328 | ||
2329 | r = _bfd_final_link_relocate (howto, abfd, input_section, | |
2330 | data, offset, symbol_hi16, addend); | |
2331 | ||
2332 | if (r == bfd_reloc_ok) | |
2333 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2334 | data, offset + 4, symbol_lo16, addend); | |
2335 | ||
2336 | return r; | |
2337 | } | |
2338 | ||
2339 | static bfd_reloc_status_type | |
2340 | nios2_elf32_do_cjmp_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2341 | asection *input_section, |
36591ba1 SL |
2342 | bfd_byte *data, bfd_vma offset, |
2343 | bfd_vma symbol_value, bfd_vma addend) | |
2344 | { | |
2345 | bfd_vma symbol_lo16, symbol_hi16; | |
2346 | bfd_reloc_status_type r; | |
2347 | symbol_value = symbol_value + addend; | |
2348 | addend = 0; | |
2349 | symbol_hi16 = (symbol_value >> 16) & 0xffff; | |
2350 | symbol_lo16 = symbol_value & 0xffff; | |
2351 | ||
2352 | r = _bfd_final_link_relocate (howto, abfd, input_section, | |
2353 | data, offset, symbol_hi16, addend); | |
2354 | ||
2355 | if (r == bfd_reloc_ok) | |
2356 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2357 | data, offset + 4, symbol_lo16, addend); | |
2358 | ||
2359 | return r; | |
2360 | } | |
2361 | ||
2362 | static bfd_reloc_status_type | |
2363 | nios2_elf32_do_callr_relocate (bfd *abfd, reloc_howto_type *howto, | |
25153ba0 | 2364 | asection *input_section, |
36591ba1 SL |
2365 | bfd_byte *data, bfd_vma offset, |
2366 | bfd_vma symbol_value, bfd_vma addend) | |
2367 | { | |
2368 | bfd_vma symbol_lo16, symbol_hi16; | |
2369 | bfd_reloc_status_type r; | |
2370 | symbol_value = symbol_value + addend; | |
2371 | addend = 0; | |
2372 | symbol_hi16 = (symbol_value >> 16) & 0xffff; | |
2373 | symbol_lo16 = symbol_value & 0xffff; | |
2374 | ||
2375 | r = _bfd_final_link_relocate (howto, abfd, input_section, | |
2376 | data, offset, symbol_hi16, addend); | |
2377 | ||
2378 | if (r == bfd_reloc_ok) | |
2379 | return _bfd_final_link_relocate (howto, abfd, input_section, | |
2380 | data, offset + 4, symbol_lo16, addend); | |
2381 | ||
2382 | return r; | |
2383 | } | |
2384 | ||
2385 | /* HOWTO handlers for relocations that require special handling. */ | |
2386 | ||
2387 | /* This is for relocations used only when relaxing to ensure | |
2388 | changes in size of section don't screw up .align. */ | |
2389 | static bfd_reloc_status_type | |
2390 | nios2_elf32_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry, | |
2391 | asymbol *symbol ATTRIBUTE_UNUSED, | |
2392 | void *data ATTRIBUTE_UNUSED, asection *input_section, | |
2393 | bfd *output_bfd, | |
2394 | char **error_message ATTRIBUTE_UNUSED) | |
2395 | { | |
2396 | if (output_bfd != NULL) | |
2397 | reloc_entry->address += input_section->output_offset; | |
2398 | return bfd_reloc_ok; | |
2399 | } | |
2400 | ||
2401 | static bfd_reloc_status_type | |
2402 | nios2_elf32_hi16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2403 | void *data, asection *input_section, | |
2404 | bfd *output_bfd, | |
2405 | char **error_message ATTRIBUTE_UNUSED) | |
2406 | { | |
2407 | /* This part is from bfd_elf_generic_reloc. */ | |
2408 | if (output_bfd != NULL | |
2409 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2410 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2411 | { | |
2412 | reloc_entry->address += input_section->output_offset; | |
2413 | return bfd_reloc_ok; | |
2414 | } | |
2415 | ||
2416 | if (output_bfd != NULL) | |
2417 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2418 | return bfd_reloc_continue; | |
2419 | ||
2420 | return nios2_elf32_do_hi16_relocate (abfd, reloc_entry->howto, | |
2421 | input_section, | |
2422 | data, reloc_entry->address, | |
2423 | (symbol->value | |
2424 | + symbol->section->output_section->vma | |
2425 | + symbol->section->output_offset), | |
2426 | reloc_entry->addend); | |
2427 | } | |
2428 | ||
2429 | static bfd_reloc_status_type | |
2430 | nios2_elf32_lo16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2431 | void *data, asection *input_section, | |
2432 | bfd *output_bfd, | |
2433 | char **error_message ATTRIBUTE_UNUSED) | |
2434 | { | |
2435 | /* This part is from bfd_elf_generic_reloc. */ | |
2436 | if (output_bfd != NULL | |
2437 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2438 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2439 | { | |
2440 | reloc_entry->address += input_section->output_offset; | |
2441 | return bfd_reloc_ok; | |
2442 | } | |
2443 | ||
2444 | if (output_bfd != NULL) | |
2445 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2446 | return bfd_reloc_continue; | |
2447 | ||
2448 | return nios2_elf32_do_lo16_relocate (abfd, reloc_entry->howto, | |
2449 | input_section, | |
2450 | data, reloc_entry->address, | |
2451 | (symbol->value | |
2452 | + symbol->section->output_section->vma | |
2453 | + symbol->section->output_offset), | |
2454 | reloc_entry->addend); | |
2455 | } | |
2456 | ||
2457 | static bfd_reloc_status_type | |
2458 | nios2_elf32_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2459 | void *data, asection *input_section, | |
2460 | bfd *output_bfd, | |
2461 | char **error_message ATTRIBUTE_UNUSED) | |
2462 | { | |
2463 | /* This part is from bfd_elf_generic_reloc. */ | |
2464 | if (output_bfd != NULL | |
2465 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2466 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2467 | { | |
2468 | reloc_entry->address += input_section->output_offset; | |
2469 | return bfd_reloc_ok; | |
2470 | } | |
2471 | ||
2472 | if (output_bfd != NULL) | |
2473 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2474 | return bfd_reloc_continue; | |
2475 | ||
2476 | return nios2_elf32_do_hiadj16_relocate (abfd, reloc_entry->howto, | |
2477 | input_section, | |
2478 | data, reloc_entry->address, | |
2479 | (symbol->value | |
2480 | + symbol->section->output_section->vma | |
2481 | + symbol->section->output_offset), | |
2482 | reloc_entry->addend); | |
2483 | } | |
2484 | ||
2485 | static bfd_reloc_status_type | |
2486 | nios2_elf32_pcrel_lo16_relocate (bfd *abfd, arelent *reloc_entry, | |
2487 | asymbol *symbol, void *data, | |
2488 | asection *input_section, bfd *output_bfd, | |
2489 | char **error_message ATTRIBUTE_UNUSED) | |
2490 | { | |
2491 | /* This part is from bfd_elf_generic_reloc. */ | |
2492 | if (output_bfd != NULL | |
2493 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2494 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2495 | { | |
2496 | reloc_entry->address += input_section->output_offset; | |
2497 | return bfd_reloc_ok; | |
2498 | } | |
2499 | ||
2500 | if (output_bfd != NULL) | |
2501 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2502 | return bfd_reloc_continue; | |
2503 | ||
2504 | return nios2_elf32_do_pcrel_lo16_relocate ( | |
2505 | abfd, reloc_entry->howto, input_section, data, reloc_entry->address, | |
2506 | (symbol->value + symbol->section->output_section->vma | |
2507 | + symbol->section->output_offset), | |
2508 | reloc_entry->addend); | |
2509 | } | |
2510 | ||
2511 | static bfd_reloc_status_type | |
2512 | nios2_elf32_pcrel_hiadj16_relocate (bfd *abfd, arelent *reloc_entry, | |
2513 | asymbol *symbol, void *data, | |
2514 | asection *input_section, bfd *output_bfd, | |
2515 | char **error_message ATTRIBUTE_UNUSED) | |
2516 | { | |
2517 | /* This part is from bfd_elf_generic_reloc. */ | |
2518 | if (output_bfd != NULL | |
2519 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2520 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2521 | { | |
2522 | reloc_entry->address += input_section->output_offset; | |
2523 | return bfd_reloc_ok; | |
2524 | } | |
2525 | ||
2526 | if (output_bfd != NULL) | |
2527 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2528 | return bfd_reloc_continue; | |
2529 | ||
2530 | return nios2_elf32_do_pcrel_hiadj16_relocate ( | |
2531 | abfd, reloc_entry->howto, input_section, data, reloc_entry->address, | |
2532 | (symbol->value + symbol->section->output_section->vma | |
2533 | + symbol->section->output_offset), | |
2534 | reloc_entry->addend); | |
2535 | } | |
2536 | ||
2537 | static bfd_reloc_status_type | |
2538 | nios2_elf32_pcrel16_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2539 | void *data, asection *input_section, | |
2540 | bfd *output_bfd, | |
2541 | char **error_message ATTRIBUTE_UNUSED) | |
2542 | { | |
2543 | /* This part is from bfd_elf_generic_reloc. */ | |
2544 | if (output_bfd != NULL | |
2545 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2546 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2547 | { | |
2548 | reloc_entry->address += input_section->output_offset; | |
2549 | return bfd_reloc_ok; | |
2550 | } | |
2551 | ||
2552 | if (output_bfd != NULL) | |
2553 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2554 | return bfd_reloc_continue; | |
2555 | ||
2556 | return nios2_elf32_do_pcrel16_relocate (abfd, reloc_entry->howto, | |
2557 | input_section, | |
2558 | data, reloc_entry->address, | |
2559 | (symbol->value | |
2560 | + symbol->section->output_section->vma | |
2561 | + symbol->section->output_offset), | |
2562 | reloc_entry->addend); | |
2563 | } | |
2564 | ||
2565 | static bfd_reloc_status_type | |
2566 | nios2_elf32_call26_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2567 | void *data, asection *input_section, | |
2568 | bfd *output_bfd, | |
2569 | char **error_message ATTRIBUTE_UNUSED) | |
2570 | { | |
2571 | /* This part is from bfd_elf_generic_reloc. */ | |
2572 | if (output_bfd != NULL | |
2573 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2574 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2575 | { | |
2576 | reloc_entry->address += input_section->output_offset; | |
2577 | return bfd_reloc_ok; | |
2578 | } | |
2579 | ||
2580 | if (output_bfd != NULL) | |
2581 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2582 | return bfd_reloc_continue; | |
2583 | ||
2584 | return nios2_elf32_do_call26_relocate (abfd, reloc_entry->howto, | |
2585 | input_section, | |
2586 | data, reloc_entry->address, | |
2587 | (symbol->value | |
2588 | + symbol->section->output_section->vma | |
2589 | + symbol->section->output_offset), | |
2590 | reloc_entry->addend); | |
2591 | } | |
2592 | ||
2593 | static bfd_reloc_status_type | |
2594 | nios2_elf32_gprel_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2595 | void *data, asection *input_section, | |
2596 | bfd *output_bfd, char **msg) | |
2597 | { | |
2598 | bfd_vma relocation; | |
2599 | bfd_vma gp; | |
2600 | bfd_reloc_status_type r; | |
2601 | ||
2602 | ||
2603 | /* This part is from bfd_elf_generic_reloc. */ | |
2604 | if (output_bfd != NULL | |
2605 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2606 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2607 | { | |
2608 | reloc_entry->address += input_section->output_offset; | |
2609 | return bfd_reloc_ok; | |
2610 | } | |
2611 | ||
2612 | if (output_bfd != NULL) | |
2613 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2614 | return bfd_reloc_continue; | |
2615 | ||
2616 | relocation = (symbol->value | |
2617 | + symbol->section->output_section->vma | |
2618 | + symbol->section->output_offset); | |
2619 | ||
2620 | /* This assumes we've already cached the _gp symbol. */ | |
2621 | r = nios2_elf_final_gp (abfd, symbol, FALSE, msg, &gp); | |
2622 | if (r == bfd_reloc_ok) | |
2623 | { | |
2624 | relocation = relocation + reloc_entry->addend - gp; | |
2625 | reloc_entry->addend = 0; | |
2626 | if ((signed) relocation < -32768 || (signed) relocation > 32767) | |
2627 | { | |
2628 | *msg = _("global pointer relative address out of range"); | |
2629 | r = bfd_reloc_outofrange; | |
2630 | } | |
2631 | else | |
2632 | r = nios2_elf32_do_gprel_relocate (abfd, reloc_entry->howto, | |
2633 | input_section, | |
2634 | data, reloc_entry->address, | |
2635 | relocation, reloc_entry->addend); | |
2636 | } | |
2637 | ||
2638 | return r; | |
2639 | } | |
2640 | ||
2641 | static bfd_reloc_status_type | |
2642 | nios2_elf32_ujmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2643 | void *data, asection *input_section, | |
2644 | bfd *output_bfd, char **msg ATTRIBUTE_UNUSED) | |
2645 | { | |
2646 | /* This part is from bfd_elf_generic_reloc. */ | |
2647 | if (output_bfd != NULL | |
2648 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2649 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2650 | { | |
2651 | reloc_entry->address += input_section->output_offset; | |
2652 | return bfd_reloc_ok; | |
2653 | } | |
2654 | ||
2655 | if (output_bfd != NULL) | |
2656 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2657 | return bfd_reloc_continue; | |
2658 | ||
2659 | return nios2_elf32_do_ujmp_relocate (abfd, reloc_entry->howto, | |
2660 | input_section, | |
2661 | data, reloc_entry->address, | |
2662 | (symbol->value | |
2663 | + symbol->section->output_section->vma | |
2664 | + symbol->section->output_offset), | |
2665 | reloc_entry->addend); | |
2666 | } | |
2667 | ||
2668 | static bfd_reloc_status_type | |
2669 | nios2_elf32_cjmp_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2670 | void *data, asection *input_section, | |
2671 | bfd *output_bfd, char **msg ATTRIBUTE_UNUSED) | |
2672 | { | |
2673 | /* This part is from bfd_elf_generic_reloc. */ | |
2674 | if (output_bfd != NULL | |
2675 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2676 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2677 | { | |
2678 | reloc_entry->address += input_section->output_offset; | |
2679 | return bfd_reloc_ok; | |
2680 | } | |
2681 | ||
2682 | if (output_bfd != NULL) | |
2683 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2684 | return bfd_reloc_continue; | |
2685 | ||
2686 | return nios2_elf32_do_cjmp_relocate (abfd, reloc_entry->howto, | |
2687 | input_section, | |
2688 | data, reloc_entry->address, | |
2689 | (symbol->value | |
2690 | + symbol->section->output_section->vma | |
2691 | + symbol->section->output_offset), | |
2692 | reloc_entry->addend); | |
2693 | } | |
2694 | ||
2695 | static bfd_reloc_status_type | |
2696 | nios2_elf32_callr_relocate (bfd *abfd, arelent *reloc_entry, asymbol *symbol, | |
2697 | void *data, asection *input_section, | |
2698 | bfd *output_bfd, char **msg ATTRIBUTE_UNUSED) | |
2699 | { | |
2700 | /* This part is from bfd_elf_generic_reloc. */ | |
2701 | if (output_bfd != NULL | |
2702 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
2703 | && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0)) | |
2704 | { | |
2705 | reloc_entry->address += input_section->output_offset; | |
2706 | return bfd_reloc_ok; | |
2707 | } | |
2708 | ||
2709 | if (output_bfd != NULL) | |
2710 | /* FIXME: See bfd_perform_relocation. Is this right? */ | |
2711 | return bfd_reloc_continue; | |
2712 | ||
2713 | return nios2_elf32_do_callr_relocate (abfd, reloc_entry->howto, | |
2714 | input_section, | |
2715 | data, reloc_entry->address, | |
2716 | (symbol->value | |
2717 | + symbol->section->output_section->vma | |
2718 | + symbol->section->output_offset), | |
2719 | reloc_entry->addend); | |
2720 | } | |
2721 | ||
2722 | ||
2723 | /* Implement elf_backend_relocate_section. */ | |
2724 | static bfd_boolean | |
2725 | nios2_elf32_relocate_section (bfd *output_bfd, | |
2726 | struct bfd_link_info *info, | |
2727 | bfd *input_bfd, | |
2728 | asection *input_section, | |
2729 | bfd_byte *contents, | |
2730 | Elf_Internal_Rela *relocs, | |
2731 | Elf_Internal_Sym *local_syms, | |
2732 | asection **local_sections) | |
2733 | { | |
2734 | Elf_Internal_Shdr *symtab_hdr; | |
2735 | struct elf_link_hash_entry **sym_hashes; | |
2736 | Elf_Internal_Rela *rel; | |
2737 | Elf_Internal_Rela *relend; | |
2738 | struct elf32_nios2_link_hash_table *htab; | |
2739 | asection *sgot; | |
2740 | asection *splt; | |
2741 | asection *sreloc = NULL; | |
2742 | bfd_vma *local_got_offsets; | |
82e91538 | 2743 | bfd_vma got_base; |
36591ba1 SL |
2744 | |
2745 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
2746 | sym_hashes = elf_sym_hashes (input_bfd); | |
2747 | relend = relocs + input_section->reloc_count; | |
2748 | ||
2749 | htab = elf32_nios2_hash_table (info); | |
2750 | sgot = htab->root.sgot; | |
2751 | splt = htab->root.splt; | |
2752 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
2753 | ||
82e91538 SL |
2754 | if (elf32_nios2_hash_table (info)->h_gp_got == NULL) |
2755 | got_base = 0; | |
2756 | else | |
2757 | got_base = elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value; | |
2758 | ||
36591ba1 SL |
2759 | for (rel = relocs; rel < relend; rel++) |
2760 | { | |
2761 | reloc_howto_type *howto; | |
2762 | unsigned long r_symndx; | |
2763 | Elf_Internal_Sym *sym; | |
2764 | asection *sec; | |
2765 | struct elf_link_hash_entry *h; | |
2766 | struct elf32_nios2_link_hash_entry *eh; | |
2767 | bfd_vma relocation; | |
2768 | bfd_vma gp; | |
2769 | bfd_vma reloc_address; | |
2770 | bfd_reloc_status_type r = bfd_reloc_ok; | |
2771 | const char *name = NULL; | |
2772 | int r_type; | |
2773 | const char *format; | |
2774 | char msgbuf[256]; | |
2775 | const char* msg = (const char*) NULL; | |
2776 | bfd_boolean unresolved_reloc; | |
2777 | bfd_vma off; | |
2778 | int use_plt; | |
2779 | ||
2780 | r_type = ELF32_R_TYPE (rel->r_info); | |
2781 | r_symndx = ELF32_R_SYM (rel->r_info); | |
2782 | ||
2783 | howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info)); | |
2784 | h = NULL; | |
2785 | sym = NULL; | |
2786 | sec = NULL; | |
2787 | ||
2788 | if (r_symndx < symtab_hdr->sh_info) | |
2789 | { | |
2790 | sym = local_syms + r_symndx; | |
2791 | sec = local_sections[r_symndx]; | |
2792 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); | |
2793 | } | |
2794 | else | |
2795 | { | |
62d887d4 | 2796 | bfd_boolean warned, ignored; |
36591ba1 SL |
2797 | |
2798 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, | |
2799 | r_symndx, symtab_hdr, sym_hashes, | |
2800 | h, sec, relocation, | |
62d887d4 | 2801 | unresolved_reloc, warned, ignored); |
36591ba1 SL |
2802 | } |
2803 | ||
2804 | if (sec && discarded_section (sec)) | |
2805 | RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, | |
2806 | rel, 1, relend, howto, 0, contents); | |
2807 | ||
2808 | /* Nothing more to do unless this is a final link. */ | |
2809 | if (info->relocatable) | |
2810 | continue; | |
2811 | ||
2812 | if (sec && sec->output_section) | |
2813 | reloc_address = (sec->output_section->vma + sec->output_offset | |
2814 | + rel->r_offset); | |
2815 | else | |
2816 | reloc_address = 0; | |
2817 | ||
2818 | if (howto) | |
2819 | { | |
2820 | switch (howto->type) | |
2821 | { | |
2822 | case R_NIOS2_HI16: | |
2823 | r = nios2_elf32_do_hi16_relocate (input_bfd, howto, | |
2824 | input_section, | |
2825 | contents, rel->r_offset, | |
2826 | relocation, rel->r_addend); | |
2827 | break; | |
2828 | case R_NIOS2_LO16: | |
2829 | r = nios2_elf32_do_lo16_relocate (input_bfd, howto, | |
2830 | input_section, | |
2831 | contents, rel->r_offset, | |
2832 | relocation, rel->r_addend); | |
2833 | break; | |
2834 | case R_NIOS2_PCREL_LO: | |
2835 | r = nios2_elf32_do_pcrel_lo16_relocate (input_bfd, howto, | |
2836 | input_section, | |
2837 | contents, | |
2838 | rel->r_offset, | |
2839 | relocation, | |
2840 | rel->r_addend); | |
2841 | break; | |
2842 | case R_NIOS2_HIADJ16: | |
2843 | r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto, | |
2844 | input_section, contents, | |
2845 | rel->r_offset, relocation, | |
2846 | rel->r_addend); | |
2847 | break; | |
2848 | case R_NIOS2_PCREL_HA: | |
2849 | r = nios2_elf32_do_pcrel_hiadj16_relocate (input_bfd, howto, | |
2850 | input_section, | |
2851 | contents, | |
2852 | rel->r_offset, | |
2853 | relocation, | |
2854 | rel->r_addend); | |
2855 | break; | |
2856 | case R_NIOS2_PCREL16: | |
2857 | r = nios2_elf32_do_pcrel16_relocate (input_bfd, howto, | |
2858 | input_section, contents, | |
2859 | rel->r_offset, relocation, | |
2860 | rel->r_addend); | |
2861 | break; | |
2862 | case R_NIOS2_GPREL: | |
2863 | /* Turns an absolute address into a gp-relative address. */ | |
2864 | if (!nios2_elf_assign_gp (output_bfd, &gp, info)) | |
2865 | { | |
2866 | format = _("global pointer relative relocation at address " | |
2867 | "0x%08x when _gp not defined\n"); | |
2868 | sprintf (msgbuf, format, reloc_address); | |
2869 | msg = msgbuf; | |
2870 | r = bfd_reloc_dangerous; | |
2871 | } | |
2872 | else | |
2873 | { | |
2874 | bfd_vma symbol_address = rel->r_addend + relocation; | |
2875 | relocation = relocation + rel->r_addend - gp; | |
2876 | rel->r_addend = 0; | |
2877 | if (((signed) relocation < -32768 | |
2878 | || (signed) relocation > 32767) | |
2879 | && (!h | |
2880 | || h->root.type == bfd_link_hash_defined | |
2881 | || h->root.type == bfd_link_hash_defweak)) | |
2882 | { | |
2883 | format = _("Unable to reach %s (at 0x%08x) from the " | |
2884 | "global pointer (at 0x%08x) because the " | |
2885 | "offset (%d) is out of the allowed range, " | |
2886 | "-32678 to 32767.\n" ); | |
2887 | sprintf (msgbuf, format, name, symbol_address, gp, | |
2888 | (signed)relocation); | |
2889 | msg = msgbuf; | |
2890 | r = bfd_reloc_outofrange; | |
2891 | } | |
2892 | else | |
2893 | r = _bfd_final_link_relocate (howto, input_bfd, | |
2894 | input_section, contents, | |
2895 | rel->r_offset, relocation, | |
2896 | rel->r_addend); | |
2897 | } | |
2898 | ||
2899 | break; | |
2900 | case R_NIOS2_UJMP: | |
2901 | r = nios2_elf32_do_ujmp_relocate (input_bfd, howto, | |
2902 | input_section, | |
2903 | contents, rel->r_offset, | |
2904 | relocation, rel->r_addend); | |
2905 | break; | |
2906 | case R_NIOS2_CJMP: | |
2907 | r = nios2_elf32_do_cjmp_relocate (input_bfd, howto, | |
2908 | input_section, | |
2909 | contents, rel->r_offset, | |
2910 | relocation, rel->r_addend); | |
2911 | break; | |
2912 | case R_NIOS2_CALLR: | |
2913 | r = nios2_elf32_do_callr_relocate (input_bfd, howto, | |
2914 | input_section, contents, | |
2915 | rel->r_offset, relocation, | |
2916 | rel->r_addend); | |
2917 | break; | |
2918 | case R_NIOS2_CALL26: | |
78058a5e | 2919 | case R_NIOS2_CALL26_NOAT: |
36591ba1 SL |
2920 | /* If we have a call to an undefined weak symbol, we just want |
2921 | to stuff a zero in the bits of the call instruction and | |
2922 | bypass the normal call26 relocation handling, because it'll | |
2923 | diagnose an overflow error if address 0 isn't in the same | |
2924 | 256MB segment as the call site. Presumably the call | |
2925 | should be guarded by a null check anyway. */ | |
2926 | if (h != NULL && h->root.type == bfd_link_hash_undefweak) | |
2927 | { | |
2928 | BFD_ASSERT (relocation == 0 && rel->r_addend == 0); | |
2929 | r = _bfd_final_link_relocate (howto, input_bfd, | |
2930 | input_section, contents, | |
2931 | rel->r_offset, relocation, | |
2932 | rel->r_addend); | |
2933 | break; | |
2934 | } | |
2935 | /* Handle relocations which should use the PLT entry. | |
2936 | NIOS2_BFD_RELOC_32 relocations will use the symbol's value, | |
2937 | which may point to a PLT entry, but we don't need to handle | |
2938 | that here. If we created a PLT entry, all branches in this | |
2939 | object should go to it. */ | |
2940 | if (h != NULL && splt != NULL && h->plt.offset != (bfd_vma) -1) | |
2941 | { | |
2942 | /* If we've created a .plt section, and assigned a PLT entry | |
2943 | to this function, it should not be known to bind locally. | |
2944 | If it were, we would have cleared the PLT entry. */ | |
2945 | BFD_ASSERT (!SYMBOL_CALLS_LOCAL (info, h)); | |
2946 | ||
2947 | relocation = (splt->output_section->vma | |
2948 | + splt->output_offset | |
2949 | + h->plt.offset); | |
2950 | ||
2951 | unresolved_reloc = FALSE; | |
2952 | } | |
78058a5e SL |
2953 | /* Detect R_NIOS2_CALL26 relocations that would overflow the |
2954 | 256MB segment. Replace the target with a reference to a | |
2955 | trampoline instead. | |
2956 | Note that htab->stub_group is null if relaxation has been | |
2957 | disabled by the --no-relax linker command-line option, so | |
2958 | we can use that to skip this processing entirely. */ | |
2959 | if (howto->type == R_NIOS2_CALL26 && htab->stub_group) | |
2960 | { | |
2961 | bfd_vma dest = relocation + rel->r_addend; | |
2962 | enum elf32_nios2_stub_type stub_type; | |
2963 | ||
2964 | eh = (struct elf32_nios2_link_hash_entry *)h; | |
2965 | stub_type = nios2_type_of_stub (input_section, rel, eh, | |
2966 | htab, dest, NULL); | |
2967 | ||
2968 | if (stub_type != nios2_stub_none) | |
2969 | { | |
2970 | struct elf32_nios2_stub_hash_entry *hsh; | |
2971 | ||
2972 | hsh = nios2_get_stub_entry (input_section, sec, | |
2973 | eh, rel, htab, stub_type); | |
2974 | if (hsh == NULL) | |
2975 | { | |
2976 | r = bfd_reloc_undefined; | |
2977 | break; | |
2978 | } | |
2979 | ||
2980 | dest = (hsh->stub_offset | |
2981 | + hsh->stub_sec->output_offset | |
2982 | + hsh->stub_sec->output_section->vma); | |
2983 | r = nios2_elf32_do_call26_relocate (input_bfd, howto, | |
2984 | input_section, | |
2985 | contents, | |
2986 | rel->r_offset, | |
2987 | dest, 0); | |
2988 | break; | |
2989 | } | |
2990 | } | |
2991 | ||
2992 | /* Normal case. */ | |
36591ba1 SL |
2993 | r = nios2_elf32_do_call26_relocate (input_bfd, howto, |
2994 | input_section, contents, | |
2995 | rel->r_offset, relocation, | |
2996 | rel->r_addend); | |
2997 | break; | |
2998 | case R_NIOS2_ALIGN: | |
2999 | r = bfd_reloc_ok; | |
3000 | /* For symmetry this would be | |
3001 | r = nios2_elf32_do_ignore_reloc (input_bfd, howto, | |
3002 | input_section, contents, | |
3003 | rel->r_offset, relocation, | |
3004 | rel->r_addend); | |
3005 | but do_ignore_reloc would do no more than return | |
3006 | bfd_reloc_ok. */ | |
3007 | break; | |
3008 | ||
3009 | case R_NIOS2_GOT16: | |
3010 | case R_NIOS2_CALL16: | |
1c2de463 SL |
3011 | case R_NIOS2_GOT_LO: |
3012 | case R_NIOS2_GOT_HA: | |
3013 | case R_NIOS2_CALL_LO: | |
3014 | case R_NIOS2_CALL_HA: | |
36591ba1 SL |
3015 | /* Relocation is to the entry for this symbol in the |
3016 | global offset table. */ | |
3017 | if (sgot == NULL) | |
3018 | { | |
3019 | r = bfd_reloc_notsupported; | |
3020 | break; | |
3021 | } | |
3022 | ||
3023 | use_plt = 0; | |
3024 | ||
3025 | if (h != NULL) | |
3026 | { | |
3027 | bfd_boolean dyn; | |
3028 | ||
3029 | eh = (struct elf32_nios2_link_hash_entry *)h; | |
1c2de463 | 3030 | use_plt = (eh->got_types_used == CALL_USED |
36591ba1 SL |
3031 | && h->plt.offset != (bfd_vma) -1); |
3032 | ||
3033 | off = h->got.offset; | |
3034 | BFD_ASSERT (off != (bfd_vma) -1); | |
3035 | dyn = elf_hash_table (info)->dynamic_sections_created; | |
3036 | if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) | |
3037 | || (info->shared | |
3038 | && SYMBOL_REFERENCES_LOCAL (info, h)) | |
3039 | || (ELF_ST_VISIBILITY (h->other) | |
3040 | && h->root.type == bfd_link_hash_undefweak)) | |
3041 | { | |
3042 | /* This is actually a static link, or it is a -Bsymbolic | |
3043 | link and the symbol is defined locally. We must | |
3044 | initialize this entry in the global offset table. | |
3045 | Since the offset must always be a multiple of 4, we | |
3046 | use the least significant bit to record whether we | |
3047 | have initialized it already. | |
3048 | ||
3049 | When doing a dynamic link, we create a .rela.got | |
3050 | relocation entry to initialize the value. This is | |
3051 | done in the finish_dynamic_symbol routine. */ | |
3052 | if ((off & 1) != 0) | |
3053 | off &= ~1; | |
3054 | else | |
3055 | { | |
3056 | bfd_put_32 (output_bfd, relocation, | |
3057 | sgot->contents + off); | |
3058 | h->got.offset |= 1; | |
3059 | } | |
3060 | } | |
3061 | else | |
3062 | unresolved_reloc = FALSE; | |
3063 | } | |
3064 | else | |
3065 | { | |
3066 | BFD_ASSERT (local_got_offsets != NULL | |
3067 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
3068 | ||
3069 | off = local_got_offsets[r_symndx]; | |
3070 | ||
3071 | /* The offset must always be a multiple of 4. We use the | |
3072 | least significant bit to record whether we have already | |
3073 | generated the necessary reloc. */ | |
3074 | if ((off & 1) != 0) | |
3075 | off &= ~1; | |
3076 | else | |
3077 | { | |
3078 | bfd_put_32 (output_bfd, relocation, | |
3079 | sgot->contents + off); | |
3080 | ||
3081 | if (info->shared) | |
3082 | { | |
3083 | asection *srelgot; | |
3084 | Elf_Internal_Rela outrel; | |
3085 | bfd_byte *loc; | |
3086 | ||
3087 | srelgot = htab->root.srelgot; | |
3088 | BFD_ASSERT (srelgot != NULL); | |
3089 | ||
3090 | outrel.r_addend = relocation; | |
3091 | outrel.r_offset = (sgot->output_section->vma | |
3092 | + sgot->output_offset | |
3093 | + off); | |
3094 | outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE); | |
3095 | loc = srelgot->contents; | |
3096 | loc += (srelgot->reloc_count++ * | |
3097 | sizeof (Elf32_External_Rela)); | |
3098 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); | |
3099 | } | |
3100 | ||
3101 | local_got_offsets[r_symndx] |= 1; | |
3102 | } | |
3103 | } | |
3104 | ||
3105 | if (use_plt && info->shared) | |
3106 | { | |
3107 | off = ((h->plt.offset - 24) / 12 + 3) * 4; | |
82e91538 SL |
3108 | relocation = (htab->root.sgotplt->output_offset + off |
3109 | - got_base); | |
36591ba1 SL |
3110 | } |
3111 | else | |
82e91538 | 3112 | relocation = sgot->output_offset + off - got_base; |
36591ba1 SL |
3113 | |
3114 | /* This relocation does not use the addend. */ | |
3115 | rel->r_addend = 0; | |
3116 | ||
1c2de463 SL |
3117 | switch (howto->type) |
3118 | { | |
3119 | case R_NIOS2_GOT_LO: | |
3120 | case R_NIOS2_CALL_LO: | |
3121 | r = nios2_elf32_do_lo16_relocate (input_bfd, howto, | |
3122 | input_section, contents, | |
3123 | rel->r_offset, relocation, | |
3124 | rel->r_addend); | |
3125 | break; | |
3126 | case R_NIOS2_GOT_HA: | |
3127 | case R_NIOS2_CALL_HA: | |
3128 | r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto, | |
3129 | input_section, contents, | |
3130 | rel->r_offset, | |
3131 | relocation, | |
3132 | rel->r_addend); | |
3133 | break; | |
3134 | default: | |
3135 | r = _bfd_final_link_relocate (howto, input_bfd, | |
3136 | input_section, contents, | |
3137 | rel->r_offset, relocation, | |
3138 | rel->r_addend); | |
3139 | break; | |
3140 | } | |
36591ba1 SL |
3141 | break; |
3142 | ||
3143 | case R_NIOS2_GOTOFF_LO: | |
3144 | case R_NIOS2_GOTOFF_HA: | |
3145 | case R_NIOS2_GOTOFF: | |
82e91538 | 3146 | /* Relocation is relative to the global offset table pointer. */ |
36591ba1 SL |
3147 | |
3148 | BFD_ASSERT (sgot != NULL); | |
3149 | if (sgot == NULL) | |
3150 | { | |
3151 | r = bfd_reloc_notsupported; | |
3152 | break; | |
3153 | } | |
3154 | ||
d9972968 CLT |
3155 | /* Note that sgot->output_offset is not involved in this |
3156 | calculation. We always want the start of .got. */ | |
3157 | relocation -= sgot->output_section->vma; | |
3158 | ||
3159 | /* Now we adjust the relocation to be relative to the GOT pointer | |
3160 | (the _gp_got symbol), which possibly contains the 0x8000 bias. */ | |
3161 | relocation -= got_base; | |
82e91538 | 3162 | |
36591ba1 SL |
3163 | switch (howto->type) |
3164 | { | |
3165 | case R_NIOS2_GOTOFF_LO: | |
3166 | r = nios2_elf32_do_lo16_relocate (input_bfd, howto, | |
3167 | input_section, contents, | |
3168 | rel->r_offset, relocation, | |
3169 | rel->r_addend); | |
3170 | break; | |
3171 | case R_NIOS2_GOTOFF_HA: | |
3172 | r = nios2_elf32_do_hiadj16_relocate (input_bfd, howto, | |
3173 | input_section, contents, | |
3174 | rel->r_offset, | |
3175 | relocation, | |
3176 | rel->r_addend); | |
3177 | break; | |
3178 | default: | |
3179 | r = _bfd_final_link_relocate (howto, input_bfd, | |
3180 | input_section, contents, | |
3181 | rel->r_offset, relocation, | |
3182 | rel->r_addend); | |
3183 | break; | |
3184 | } | |
3185 | break; | |
3186 | ||
3187 | case R_NIOS2_TLS_LDO16: | |
3188 | relocation -= dtpoff_base (info) + DTP_OFFSET; | |
3189 | ||
3190 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
3191 | contents, rel->r_offset, | |
3192 | relocation, rel->r_addend); | |
3193 | break; | |
3194 | case R_NIOS2_TLS_LDM16: | |
3195 | if (htab->root.sgot == NULL) | |
3196 | abort (); | |
3197 | ||
3198 | off = htab->tls_ldm_got.offset; | |
3199 | ||
3200 | if ((off & 1) != 0) | |
3201 | off &= ~1; | |
3202 | else | |
3203 | { | |
3204 | /* If we don't know the module number, create a relocation | |
3205 | for it. */ | |
3206 | if (info->shared) | |
3207 | { | |
3208 | Elf_Internal_Rela outrel; | |
3209 | bfd_byte *loc; | |
3210 | ||
3211 | if (htab->root.srelgot == NULL) | |
3212 | abort (); | |
3213 | ||
3214 | outrel.r_addend = 0; | |
3215 | outrel.r_offset = (htab->root.sgot->output_section->vma | |
3216 | + htab->root.sgot->output_offset | |
3217 | + off); | |
3218 | outrel.r_info = ELF32_R_INFO (0, R_NIOS2_TLS_DTPMOD); | |
3219 | ||
3220 | loc = htab->root.srelgot->contents; | |
3221 | loc += (htab->root.srelgot->reloc_count++ | |
3222 | * sizeof (Elf32_External_Rela)); | |
3223 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); | |
3224 | } | |
3225 | else | |
3226 | bfd_put_32 (output_bfd, 1, | |
3227 | htab->root.sgot->contents + off); | |
3228 | ||
3229 | htab->tls_ldm_got.offset |= 1; | |
3230 | } | |
3231 | ||
82e91538 | 3232 | relocation = htab->root.sgot->output_offset + off - got_base; |
36591ba1 SL |
3233 | |
3234 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
3235 | contents, rel->r_offset, | |
3236 | relocation, rel->r_addend); | |
3237 | ||
3238 | break; | |
3239 | case R_NIOS2_TLS_GD16: | |
3240 | case R_NIOS2_TLS_IE16: | |
3241 | { | |
3242 | int indx; | |
3243 | char tls_type; | |
3244 | ||
3245 | if (htab->root.sgot == NULL) | |
3246 | abort (); | |
3247 | ||
3248 | indx = 0; | |
3249 | if (h != NULL) | |
3250 | { | |
3251 | bfd_boolean dyn; | |
3252 | dyn = htab->root.dynamic_sections_created; | |
3253 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) | |
3254 | && (!info->shared | |
3255 | || !SYMBOL_REFERENCES_LOCAL (info, h))) | |
3256 | { | |
3257 | unresolved_reloc = FALSE; | |
3258 | indx = h->dynindx; | |
3259 | } | |
3260 | off = h->got.offset; | |
3261 | tls_type = (((struct elf32_nios2_link_hash_entry *) h) | |
3262 | ->tls_type); | |
3263 | } | |
3264 | else | |
3265 | { | |
3266 | if (local_got_offsets == NULL) | |
3267 | abort (); | |
3268 | off = local_got_offsets[r_symndx]; | |
3269 | tls_type = (elf32_nios2_local_got_tls_type (input_bfd) | |
3270 | [r_symndx]); | |
3271 | } | |
3272 | ||
3273 | if (tls_type == GOT_UNKNOWN) | |
3274 | abort (); | |
3275 | ||
3276 | if ((off & 1) != 0) | |
3277 | off &= ~1; | |
3278 | else | |
3279 | { | |
3280 | bfd_boolean need_relocs = FALSE; | |
3281 | Elf_Internal_Rela outrel; | |
3282 | bfd_byte *loc = NULL; | |
3283 | int cur_off = off; | |
3284 | ||
3285 | /* The GOT entries have not been initialized yet. Do it | |
3286 | now, and emit any relocations. If both an IE GOT and a | |
3287 | GD GOT are necessary, we emit the GD first. */ | |
3288 | ||
3289 | if ((info->shared || indx != 0) | |
3290 | && (h == NULL | |
3291 | || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
3292 | || h->root.type != bfd_link_hash_undefweak)) | |
3293 | { | |
3294 | need_relocs = TRUE; | |
3295 | if (htab->root.srelgot == NULL) | |
3296 | abort (); | |
3297 | loc = htab->root.srelgot->contents; | |
3298 | loc += (htab->root.srelgot->reloc_count * | |
3299 | sizeof (Elf32_External_Rela)); | |
3300 | } | |
3301 | ||
3302 | if (tls_type & GOT_TLS_GD) | |
3303 | { | |
3304 | if (need_relocs) | |
3305 | { | |
3306 | outrel.r_addend = 0; | |
3307 | outrel.r_offset = (htab->root.sgot->output_section->vma | |
3308 | + htab->root.sgot->output_offset | |
3309 | + cur_off); | |
3310 | outrel.r_info = ELF32_R_INFO (indx, | |
3311 | R_NIOS2_TLS_DTPMOD); | |
3312 | ||
3313 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
3314 | loc); | |
3315 | htab->root.srelgot->reloc_count++; | |
3316 | loc += sizeof (Elf32_External_Rela); | |
3317 | ||
3318 | if (indx == 0) | |
3319 | bfd_put_32 (output_bfd, | |
3320 | (relocation - dtpoff_base (info) - | |
3321 | DTP_OFFSET), | |
3322 | htab->root.sgot->contents + cur_off + 4); | |
3323 | else | |
3324 | { | |
3325 | outrel.r_addend = 0; | |
3326 | outrel.r_info = ELF32_R_INFO (indx, | |
3327 | R_NIOS2_TLS_DTPREL); | |
3328 | outrel.r_offset += 4; | |
3329 | ||
3330 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
3331 | loc); | |
3332 | htab->root.srelgot->reloc_count++; | |
3333 | loc += sizeof (Elf32_External_Rela); | |
3334 | } | |
3335 | } | |
3336 | else | |
3337 | { | |
3338 | /* If we are not emitting relocations for a | |
3339 | general dynamic reference, then we must be in a | |
3340 | static link or an executable link with the | |
3341 | symbol binding locally. Mark it as belonging | |
3342 | to module 1, the executable. */ | |
3343 | bfd_put_32 (output_bfd, 1, | |
3344 | htab->root.sgot->contents + cur_off); | |
3345 | bfd_put_32 (output_bfd, (relocation - | |
3346 | dtpoff_base (info) - | |
3347 | DTP_OFFSET), | |
3348 | htab->root.sgot->contents + cur_off + 4); | |
3349 | } | |
3350 | ||
3351 | cur_off += 8; | |
3352 | } | |
3353 | ||
3354 | if (tls_type & GOT_TLS_IE) | |
3355 | { | |
3356 | if (need_relocs) | |
3357 | { | |
3358 | if (indx == 0) | |
3359 | outrel.r_addend = (relocation - | |
3360 | dtpoff_base (info)); | |
3361 | else | |
3362 | outrel.r_addend = 0; | |
3363 | outrel.r_offset = (htab->root.sgot->output_section->vma | |
3364 | + htab->root.sgot->output_offset | |
3365 | + cur_off); | |
3366 | outrel.r_info = ELF32_R_INFO (indx, | |
3367 | R_NIOS2_TLS_TPREL); | |
3368 | ||
3369 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, | |
3370 | loc); | |
3371 | htab->root.srelgot->reloc_count++; | |
3372 | loc += sizeof (Elf32_External_Rela); | |
3373 | } | |
3374 | else | |
3375 | bfd_put_32 (output_bfd, (tpoff (info, relocation) | |
3376 | - TP_OFFSET), | |
3377 | htab->root.sgot->contents + cur_off); | |
3378 | cur_off += 4; | |
3379 | } | |
3380 | ||
3381 | if (h != NULL) | |
3382 | h->got.offset |= 1; | |
3383 | else | |
3384 | local_got_offsets[r_symndx] |= 1; | |
3385 | } | |
3386 | ||
3387 | if ((tls_type & GOT_TLS_GD) && r_type != R_NIOS2_TLS_GD16) | |
3388 | off += 8; | |
82e91538 | 3389 | relocation = htab->root.sgot->output_offset + off - got_base; |
36591ba1 SL |
3390 | |
3391 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
3392 | contents, rel->r_offset, | |
3393 | relocation, rel->r_addend); | |
3394 | } | |
3395 | ||
3396 | break; | |
3397 | case R_NIOS2_TLS_LE16: | |
3398 | if (info->shared && !info->pie) | |
3399 | { | |
3400 | (*_bfd_error_handler) | |
3401 | (_("%B(%A+0x%lx): R_NIOS2_TLS_LE16 relocation not " | |
3402 | "permitted in shared object"), | |
3403 | input_bfd, input_section, | |
3404 | (long) rel->r_offset, howto->name); | |
3405 | return FALSE; | |
3406 | } | |
3407 | else | |
3408 | relocation = tpoff (info, relocation) - TP_OFFSET; | |
3409 | ||
3410 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
3411 | contents, rel->r_offset, | |
3412 | relocation, rel->r_addend); | |
3413 | break; | |
3414 | ||
3415 | case R_NIOS2_BFD_RELOC_32: | |
3416 | if (info->shared | |
3417 | && (input_section->flags & SEC_ALLOC) != 0 | |
3418 | && (h == NULL | |
3419 | || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
3420 | || h->root.type != bfd_link_hash_undefweak)) | |
3421 | { | |
3422 | Elf_Internal_Rela outrel; | |
3423 | bfd_byte *loc; | |
3424 | bfd_boolean skip, relocate; | |
3425 | ||
3426 | /* When generating a shared object, these relocations | |
3427 | are copied into the output file to be resolved at run | |
3428 | time. */ | |
3429 | ||
3430 | skip = FALSE; | |
3431 | relocate = FALSE; | |
3432 | ||
3433 | outrel.r_offset | |
3434 | = _bfd_elf_section_offset (output_bfd, info, | |
3435 | input_section, rel->r_offset); | |
3436 | if (outrel.r_offset == (bfd_vma) -1) | |
3437 | skip = TRUE; | |
3438 | else if (outrel.r_offset == (bfd_vma) -2) | |
3439 | skip = TRUE, relocate = TRUE; | |
3440 | outrel.r_offset += (input_section->output_section->vma | |
3441 | + input_section->output_offset); | |
3442 | ||
3443 | if (skip) | |
3444 | memset (&outrel, 0, sizeof outrel); | |
3445 | else if (h != NULL | |
3446 | && h->dynindx != -1 | |
3447 | && (!info->shared | |
3448 | || !info->symbolic | |
3449 | || !h->def_regular)) | |
3450 | { | |
3451 | outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); | |
3452 | outrel.r_addend = rel->r_addend; | |
3453 | } | |
3454 | else | |
3455 | { | |
3456 | /* This symbol is local, or marked to become local. */ | |
3457 | outrel.r_addend = relocation + rel->r_addend; | |
3458 | relocate = TRUE; | |
3459 | outrel.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE); | |
3460 | } | |
3461 | ||
3462 | sreloc = elf_section_data (input_section)->sreloc; | |
3463 | if (sreloc == NULL) | |
3464 | abort (); | |
3465 | ||
3466 | loc = sreloc->contents; | |
3467 | loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela); | |
3468 | bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); | |
3469 | ||
3470 | /* This reloc will be computed at runtime, so there's no | |
3471 | need to do anything now, except for R_NIOS2_BFD_RELOC_32 | |
3472 | relocations that have been turned into | |
3473 | R_NIOS2_RELATIVE. */ | |
3474 | if (!relocate) | |
3475 | break; | |
3476 | } | |
3477 | ||
3478 | r = _bfd_final_link_relocate (howto, input_bfd, | |
3479 | input_section, contents, | |
3480 | rel->r_offset, relocation, | |
3481 | rel->r_addend); | |
3482 | break; | |
3483 | ||
3484 | case R_NIOS2_TLS_DTPREL: | |
3485 | relocation -= dtpoff_base (info); | |
3486 | /* Fall through. */ | |
3487 | ||
3488 | default: | |
3489 | r = _bfd_final_link_relocate (howto, input_bfd, | |
3490 | input_section, contents, | |
3491 | rel->r_offset, relocation, | |
3492 | rel->r_addend); | |
3493 | break; | |
3494 | } | |
3495 | } | |
3496 | else | |
3497 | r = bfd_reloc_notsupported; | |
3498 | ||
3499 | if (r != bfd_reloc_ok) | |
3500 | { | |
3501 | if (h != NULL) | |
3502 | name = h->root.root.string; | |
3503 | else | |
3504 | { | |
3505 | name = bfd_elf_string_from_elf_section (input_bfd, | |
3506 | symtab_hdr->sh_link, | |
3507 | sym->st_name); | |
3508 | if (name == NULL || *name == '\0') | |
3509 | name = bfd_section_name (input_bfd, sec); | |
3510 | } | |
3511 | ||
3512 | switch (r) | |
3513 | { | |
3514 | case bfd_reloc_overflow: | |
3515 | r = info->callbacks->reloc_overflow (info, NULL, name, | |
3516 | howto->name, (bfd_vma) 0, | |
3517 | input_bfd, input_section, | |
3518 | rel->r_offset); | |
3519 | break; | |
3520 | ||
3521 | case bfd_reloc_undefined: | |
3522 | r = info->callbacks->undefined_symbol (info, name, input_bfd, | |
3523 | input_section, | |
3524 | rel->r_offset, TRUE); | |
3525 | break; | |
3526 | ||
3527 | case bfd_reloc_outofrange: | |
3528 | if (msg == NULL) | |
3529 | msg = _("relocation out of range"); | |
3530 | break; | |
3531 | ||
3532 | case bfd_reloc_notsupported: | |
3533 | if (msg == NULL) | |
3534 | msg = _("unsupported relocation"); | |
3535 | break; | |
3536 | ||
3537 | case bfd_reloc_dangerous: | |
3538 | if (msg == NULL) | |
3539 | msg = _("dangerous relocation"); | |
3540 | break; | |
3541 | ||
3542 | default: | |
3543 | if (msg == NULL) | |
3544 | msg = _("unknown error"); | |
3545 | break; | |
3546 | } | |
3547 | ||
3548 | if (msg) | |
3549 | { | |
3550 | r = info->callbacks->warning | |
3551 | (info, msg, name, input_bfd, input_section, rel->r_offset); | |
3552 | return FALSE; | |
3553 | } | |
3554 | } | |
3555 | } | |
3556 | return TRUE; | |
3557 | } | |
3558 | ||
3559 | /* Implement elf-backend_section_flags: | |
3560 | Convert NIOS2 specific section flags to bfd internal section flags. */ | |
3561 | static bfd_boolean | |
3562 | nios2_elf32_section_flags (flagword *flags, const Elf_Internal_Shdr *hdr) | |
3563 | { | |
3564 | if (hdr->sh_flags & SHF_NIOS2_GPREL) | |
3565 | *flags |= SEC_SMALL_DATA; | |
3566 | ||
3567 | return TRUE; | |
3568 | } | |
3569 | ||
3570 | /* Implement elf_backend_fake_sections: | |
3571 | Set the correct type for an NIOS2 ELF section. We do this by the | |
3572 | section name, which is a hack, but ought to work. */ | |
3573 | static bfd_boolean | |
3574 | nios2_elf32_fake_sections (bfd *abfd ATTRIBUTE_UNUSED, | |
3575 | Elf_Internal_Shdr *hdr, asection *sec) | |
3576 | { | |
3577 | register const char *name = bfd_get_section_name (abfd, sec); | |
3578 | ||
3579 | if ((sec->flags & SEC_SMALL_DATA) | |
3580 | || strcmp (name, ".sdata") == 0 | |
3581 | || strcmp (name, ".sbss") == 0 | |
3582 | || strcmp (name, ".lit4") == 0 || strcmp (name, ".lit8") == 0) | |
3583 | hdr->sh_flags |= SHF_NIOS2_GPREL; | |
3584 | ||
3585 | return TRUE; | |
3586 | } | |
3587 | ||
3588 | /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up | |
3589 | shortcuts to them in our hash table. */ | |
3590 | static bfd_boolean | |
3591 | create_got_section (bfd *dynobj, struct bfd_link_info *info) | |
3592 | { | |
3593 | struct elf32_nios2_link_hash_table *htab; | |
82e91538 | 3594 | struct elf_link_hash_entry *h; |
36591ba1 SL |
3595 | |
3596 | htab = elf32_nios2_hash_table (info); | |
3597 | ||
3598 | if (! _bfd_elf_create_got_section (dynobj, info)) | |
3599 | return FALSE; | |
3600 | ||
3601 | /* In order for the two loads in .PLTresolve to share the same %hiadj, | |
3602 | _GLOBAL_OFFSET_TABLE_ must be aligned to a 16-byte boundary. */ | |
3603 | if (!bfd_set_section_alignment (dynobj, htab->root.sgotplt, 4)) | |
3604 | return FALSE; | |
3605 | ||
82e91538 SL |
3606 | /* The Nios II ABI specifies that GOT-relative relocations are relative |
3607 | to the linker-created symbol _gp_got, rather than using | |
3608 | _GLOBAL_OFFSET_TABLE_ directly. In particular, the latter always | |
3609 | points to the base of the GOT while _gp_got may include a bias. */ | |
3610 | h = _bfd_elf_define_linkage_sym (dynobj, info, htab->root.sgotplt, | |
3611 | "_gp_got"); | |
3612 | elf32_nios2_hash_table (info)->h_gp_got = h; | |
3613 | if (h == NULL) | |
3614 | return FALSE; | |
3615 | ||
36591ba1 SL |
3616 | return TRUE; |
3617 | } | |
3618 | ||
3619 | /* Implement elf_backend_create_dynamic_sections: | |
3620 | Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and | |
3621 | .rela.bss sections in DYNOBJ, and set up shortcuts to them in our | |
3622 | hash table. */ | |
3623 | static bfd_boolean | |
3624 | nios2_elf32_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info) | |
3625 | { | |
3626 | struct elf32_nios2_link_hash_table *htab; | |
3627 | ||
3628 | htab = elf32_nios2_hash_table (info); | |
3629 | if (!htab->root.sgot && !create_got_section (dynobj, info)) | |
3630 | return FALSE; | |
3631 | ||
3632 | _bfd_elf_create_dynamic_sections (dynobj, info); | |
3633 | ||
3634 | /* In order for the two loads in a shared object .PLTresolve to share the | |
3635 | same %hiadj, the start of the PLT (as well as the GOT) must be aligned | |
3636 | to a 16-byte boundary. This is because the addresses for these loads | |
3637 | include the -(.plt+4) PIC correction. */ | |
3638 | if (!bfd_set_section_alignment (dynobj, htab->root.splt, 4)) | |
3639 | return FALSE; | |
3640 | ||
3641 | htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss"); | |
3642 | if (!htab->sdynbss) | |
3643 | return FALSE; | |
3644 | if (!info->shared) | |
3645 | { | |
3646 | htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss"); | |
3647 | if (!htab->srelbss) | |
3648 | return FALSE; | |
3649 | } | |
3650 | ||
3651 | return TRUE; | |
3652 | } | |
3653 | ||
3654 | /* Implement elf_backend_copy_indirect_symbol: | |
3655 | Copy the extra info we tack onto an elf_link_hash_entry. */ | |
3656 | static void | |
3657 | nios2_elf32_copy_indirect_symbol (struct bfd_link_info *info, | |
3658 | struct elf_link_hash_entry *dir, | |
3659 | struct elf_link_hash_entry *ind) | |
3660 | { | |
3661 | struct elf32_nios2_link_hash_entry *edir, *eind; | |
3662 | ||
3663 | edir = (struct elf32_nios2_link_hash_entry *) dir; | |
3664 | eind = (struct elf32_nios2_link_hash_entry *) ind; | |
3665 | ||
3666 | if (eind->dyn_relocs != NULL) | |
3667 | { | |
3668 | if (edir->dyn_relocs != NULL) | |
3669 | { | |
3670 | struct elf32_nios2_dyn_relocs **pp; | |
3671 | struct elf32_nios2_dyn_relocs *p; | |
3672 | ||
3673 | /* Add reloc counts against the indirect sym to the direct sym | |
3674 | list. Merge any entries against the same section. */ | |
3675 | for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) | |
3676 | { | |
3677 | struct elf32_nios2_dyn_relocs *q; | |
3678 | ||
3679 | for (q = edir->dyn_relocs; q != NULL; q = q->next) | |
3680 | if (q->sec == p->sec) | |
3681 | { | |
3682 | q->pc_count += p->pc_count; | |
3683 | q->count += p->count; | |
3684 | *pp = p->next; | |
3685 | break; | |
3686 | } | |
3687 | if (q == NULL) | |
3688 | pp = &p->next; | |
3689 | } | |
3690 | *pp = edir->dyn_relocs; | |
3691 | } | |
3692 | ||
3693 | edir->dyn_relocs = eind->dyn_relocs; | |
3694 | eind->dyn_relocs = NULL; | |
3695 | } | |
3696 | ||
3697 | if (ind->root.type == bfd_link_hash_indirect | |
3698 | && dir->got.refcount <= 0) | |
3699 | { | |
3700 | edir->tls_type = eind->tls_type; | |
3701 | eind->tls_type = GOT_UNKNOWN; | |
3702 | } | |
3703 | ||
3704 | edir->got_types_used |= eind->got_types_used; | |
3705 | ||
3706 | _bfd_elf_link_hash_copy_indirect (info, dir, ind); | |
3707 | } | |
3708 | ||
3709 | /* Implement elf_backend_check_relocs: | |
3710 | Look through the relocs for a section during the first phase. */ | |
3711 | static bfd_boolean | |
3712 | nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info, | |
3713 | asection *sec, const Elf_Internal_Rela *relocs) | |
3714 | { | |
3715 | bfd *dynobj; | |
3716 | Elf_Internal_Shdr *symtab_hdr; | |
3717 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
3718 | const Elf_Internal_Rela *rel; | |
3719 | const Elf_Internal_Rela *rel_end; | |
3720 | struct elf32_nios2_link_hash_table *htab; | |
3721 | asection *sgot; | |
3722 | asection *srelgot; | |
3723 | asection *sreloc = NULL; | |
3724 | bfd_signed_vma *local_got_refcounts; | |
3725 | ||
3726 | if (info->relocatable) | |
3727 | return TRUE; | |
3728 | ||
3729 | dynobj = elf_hash_table (info)->dynobj; | |
3730 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
3731 | sym_hashes = elf_sym_hashes (abfd); | |
3732 | sym_hashes_end = (sym_hashes | |
3733 | + symtab_hdr->sh_size / sizeof (Elf32_External_Sym)); | |
3734 | if (!elf_bad_symtab (abfd)) | |
3735 | sym_hashes_end -= symtab_hdr->sh_info; | |
3736 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
3737 | ||
3738 | htab = elf32_nios2_hash_table (info); | |
3739 | sgot = htab->root.sgot; | |
3740 | srelgot = htab->root.srelgot; | |
3741 | ||
3742 | rel_end = relocs + sec->reloc_count; | |
3743 | for (rel = relocs; rel < rel_end; rel++) | |
3744 | { | |
3745 | unsigned int r_type; | |
3746 | struct elf_link_hash_entry *h; | |
3747 | unsigned long r_symndx; | |
3748 | ||
3749 | r_symndx = ELF32_R_SYM (rel->r_info); | |
3750 | if (r_symndx < symtab_hdr->sh_info) | |
3751 | h = NULL; | |
3752 | else | |
3753 | { | |
3754 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
3755 | while (h->root.type == bfd_link_hash_indirect | |
3756 | || h->root.type == bfd_link_hash_warning) | |
3757 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
81fbe831 AM |
3758 | |
3759 | /* PR15323, ref flags aren't set for references in the same | |
3760 | object. */ | |
3761 | h->root.non_ir_ref = 1; | |
36591ba1 SL |
3762 | } |
3763 | ||
3764 | r_type = ELF32_R_TYPE (rel->r_info); | |
3765 | ||
3766 | switch (r_type) | |
3767 | { | |
3768 | case R_NIOS2_GOT16: | |
1c2de463 SL |
3769 | case R_NIOS2_GOT_LO: |
3770 | case R_NIOS2_GOT_HA: | |
36591ba1 | 3771 | case R_NIOS2_CALL16: |
1c2de463 SL |
3772 | case R_NIOS2_CALL_LO: |
3773 | case R_NIOS2_CALL_HA: | |
36591ba1 SL |
3774 | case R_NIOS2_TLS_GD16: |
3775 | case R_NIOS2_TLS_IE16: | |
3776 | /* This symbol requires a global offset table entry. */ | |
3777 | { | |
3778 | int tls_type, old_tls_type; | |
3779 | ||
3780 | switch (r_type) | |
3781 | { | |
3782 | default: | |
3783 | case R_NIOS2_GOT16: | |
1c2de463 SL |
3784 | case R_NIOS2_GOT_LO: |
3785 | case R_NIOS2_GOT_HA: | |
36591ba1 | 3786 | case R_NIOS2_CALL16: |
1c2de463 SL |
3787 | case R_NIOS2_CALL_LO: |
3788 | case R_NIOS2_CALL_HA: | |
36591ba1 SL |
3789 | tls_type = GOT_NORMAL; |
3790 | break; | |
3791 | case R_NIOS2_TLS_GD16: | |
3792 | tls_type = GOT_TLS_GD; | |
3793 | break; | |
3794 | case R_NIOS2_TLS_IE16: | |
3795 | tls_type = GOT_TLS_IE; | |
3796 | break; | |
3797 | } | |
3798 | ||
3799 | if (dynobj == NULL) | |
3800 | { | |
3801 | /* Create the .got section. */ | |
3802 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
3803 | nios2_elf32_create_dynamic_sections (dynobj, info); | |
3804 | } | |
3805 | ||
3806 | if (sgot == NULL) | |
3807 | { | |
3808 | sgot = htab->root.sgot; | |
3809 | BFD_ASSERT (sgot != NULL); | |
3810 | } | |
3811 | ||
3812 | if (srelgot == NULL | |
3813 | && (h != NULL || info->shared)) | |
3814 | { | |
3815 | srelgot = htab->root.srelgot; | |
3816 | BFD_ASSERT (srelgot != NULL); | |
3817 | } | |
3818 | ||
3819 | if (h != NULL) | |
3820 | { | |
3821 | struct elf32_nios2_link_hash_entry *eh | |
3822 | = (struct elf32_nios2_link_hash_entry *)h; | |
3823 | h->got.refcount++; | |
3824 | old_tls_type = elf32_nios2_hash_entry(h)->tls_type; | |
1c2de463 SL |
3825 | if (r_type == R_NIOS2_CALL16 |
3826 | || r_type == R_NIOS2_CALL_LO | |
3827 | || r_type == R_NIOS2_CALL_HA) | |
36591ba1 SL |
3828 | { |
3829 | /* Make sure a plt entry is created for this symbol if | |
3830 | it turns out to be a function defined by a dynamic | |
3831 | object. */ | |
3832 | h->plt.refcount++; | |
3833 | h->needs_plt = 1; | |
3834 | h->type = STT_FUNC; | |
1c2de463 | 3835 | eh->got_types_used |= CALL_USED; |
36591ba1 SL |
3836 | } |
3837 | else | |
1c2de463 | 3838 | eh->got_types_used |= GOT_USED; |
36591ba1 SL |
3839 | } |
3840 | else | |
3841 | { | |
3842 | /* This is a global offset table entry for a local symbol. */ | |
3843 | if (local_got_refcounts == NULL) | |
3844 | { | |
3845 | bfd_size_type size; | |
3846 | ||
3847 | size = symtab_hdr->sh_info; | |
3848 | size *= (sizeof (bfd_signed_vma) + sizeof (char)); | |
3849 | local_got_refcounts | |
3850 | = ((bfd_signed_vma *) bfd_zalloc (abfd, size)); | |
3851 | if (local_got_refcounts == NULL) | |
3852 | return FALSE; | |
3853 | elf_local_got_refcounts (abfd) = local_got_refcounts; | |
3854 | elf32_nios2_local_got_tls_type (abfd) | |
3855 | = (char *) (local_got_refcounts + symtab_hdr->sh_info); | |
3856 | } | |
3857 | local_got_refcounts[r_symndx]++; | |
3858 | old_tls_type = elf32_nios2_local_got_tls_type (abfd) [r_symndx]; | |
3859 | } | |
3860 | ||
3861 | /* We will already have issued an error message if there is a | |
3862 | TLS / non-TLS mismatch, based on the symbol type. We don't | |
3863 | support any linker relaxations. So just combine any TLS | |
3864 | types needed. */ | |
3865 | if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL | |
3866 | && tls_type != GOT_NORMAL) | |
3867 | tls_type |= old_tls_type; | |
3868 | ||
3869 | if (old_tls_type != tls_type) | |
3870 | { | |
3871 | if (h != NULL) | |
3872 | elf32_nios2_hash_entry (h)->tls_type = tls_type; | |
3873 | else | |
3874 | elf32_nios2_local_got_tls_type (abfd) [r_symndx] = tls_type; | |
3875 | } | |
3876 | } | |
3877 | /* Fall through */ | |
3878 | case R_NIOS2_TLS_LDM16: | |
3879 | if (r_type == R_NIOS2_TLS_LDM16) | |
3880 | htab->tls_ldm_got.refcount++; | |
3881 | ||
3882 | if (htab->root.sgot == NULL) | |
3883 | { | |
3884 | if (htab->root.dynobj == NULL) | |
3885 | htab->root.dynobj = abfd; | |
3886 | if (!create_got_section (htab->root.dynobj, info)) | |
3887 | return FALSE; | |
3888 | } | |
3889 | break; | |
3890 | ||
3891 | /* This relocation describes the C++ object vtable hierarchy. | |
3892 | Reconstruct it for later use during GC. */ | |
3893 | case R_NIOS2_GNU_VTINHERIT: | |
3894 | if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
3895 | return FALSE; | |
3896 | break; | |
3897 | ||
3898 | /* This relocation describes which C++ vtable entries are actually | |
3899 | used. Record for later use during GC. */ | |
3900 | case R_NIOS2_GNU_VTENTRY: | |
3901 | if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) | |
3902 | return FALSE; | |
3903 | break; | |
3904 | ||
3905 | case R_NIOS2_BFD_RELOC_32: | |
3906 | case R_NIOS2_CALL26: | |
78058a5e | 3907 | case R_NIOS2_CALL26_NOAT: |
36591ba1 SL |
3908 | case R_NIOS2_HIADJ16: |
3909 | case R_NIOS2_LO16: | |
3910 | ||
3911 | if (h != NULL) | |
3912 | { | |
3913 | /* If this reloc is in a read-only section, we might | |
3914 | need a copy reloc. We can't check reliably at this | |
3915 | stage whether the section is read-only, as input | |
3916 | sections have not yet been mapped to output sections. | |
3917 | Tentatively set the flag for now, and correct in | |
3918 | adjust_dynamic_symbol. */ | |
3919 | if (!info->shared) | |
3920 | h->non_got_ref = 1; | |
3921 | ||
3922 | /* Make sure a plt entry is created for this symbol if it | |
3923 | turns out to be a function defined by a dynamic object. */ | |
3924 | h->plt.refcount++; | |
3925 | ||
78058a5e | 3926 | if (r_type == R_NIOS2_CALL26 || r_type == R_NIOS2_CALL26_NOAT) |
36591ba1 SL |
3927 | h->needs_plt = 1; |
3928 | } | |
3929 | ||
3930 | /* If we are creating a shared library, we need to copy the | |
3931 | reloc into the shared library. */ | |
3932 | if (info->shared | |
3933 | && (sec->flags & SEC_ALLOC) != 0 | |
3934 | && (r_type == R_NIOS2_BFD_RELOC_32 | |
3935 | || (h != NULL && ! h->needs_plt | |
3936 | && (! info->symbolic || ! h->def_regular)))) | |
3937 | { | |
3938 | struct elf32_nios2_dyn_relocs *p; | |
3939 | struct elf32_nios2_dyn_relocs **head; | |
3940 | ||
3941 | /* When creating a shared object, we must copy these | |
3942 | reloc types into the output file. We create a reloc | |
3943 | section in dynobj and make room for this reloc. */ | |
3944 | if (sreloc == NULL) | |
3945 | { | |
3946 | sreloc = _bfd_elf_make_dynamic_reloc_section | |
3947 | (sec, dynobj, 2, abfd, TRUE); | |
3948 | if (sreloc == NULL) | |
3949 | return FALSE; | |
3950 | } | |
3951 | ||
3952 | /* If this is a global symbol, we count the number of | |
3953 | relocations we need for this symbol. */ | |
3954 | if (h != NULL) | |
3955 | head = &((struct elf32_nios2_link_hash_entry *) h)->dyn_relocs; | |
3956 | else | |
3957 | { | |
3958 | /* Track dynamic relocs needed for local syms too. | |
3959 | We really need local syms available to do this | |
3960 | easily. Oh well. */ | |
3961 | ||
3962 | asection *s; | |
3963 | void *vpp; | |
3964 | Elf_Internal_Sym *isym; | |
3965 | ||
3966 | isym = bfd_sym_from_r_symndx (&htab->sym_cache, | |
3967 | abfd, r_symndx); | |
3968 | if (isym == NULL) | |
3969 | return FALSE; | |
3970 | ||
3971 | s = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
3972 | if (s == NULL) | |
3973 | s = sec; | |
3974 | ||
3975 | vpp = &elf_section_data (s)->local_dynrel; | |
3976 | head = (struct elf32_nios2_dyn_relocs **) vpp; | |
3977 | } | |
3978 | ||
3979 | p = *head; | |
3980 | if (p == NULL || p->sec != sec) | |
3981 | { | |
3982 | bfd_size_type amt = sizeof *p; | |
3983 | p = ((struct elf32_nios2_dyn_relocs *) | |
3984 | bfd_alloc (htab->root.dynobj, amt)); | |
3985 | if (p == NULL) | |
3986 | return FALSE; | |
3987 | p->next = *head; | |
3988 | *head = p; | |
3989 | p->sec = sec; | |
3990 | p->count = 0; | |
3991 | p->pc_count = 0; | |
3992 | } | |
3993 | ||
3994 | p->count += 1; | |
3995 | ||
3996 | } | |
3997 | break; | |
3998 | } | |
3999 | } | |
4000 | ||
4001 | return TRUE; | |
4002 | } | |
4003 | ||
4004 | ||
4005 | /* Implement elf_backend_gc_mark_hook: | |
4006 | Return the section that should be marked against GC for a given | |
4007 | relocation. */ | |
4008 | static asection * | |
4009 | nios2_elf32_gc_mark_hook (asection *sec, | |
25153ba0 | 4010 | struct bfd_link_info *info, |
36591ba1 SL |
4011 | Elf_Internal_Rela *rel, |
4012 | struct elf_link_hash_entry *h, | |
4013 | Elf_Internal_Sym *sym) | |
4014 | { | |
4015 | if (h != NULL) | |
4016 | switch (ELF32_R_TYPE (rel->r_info)) | |
4017 | { | |
4018 | case R_NIOS2_GNU_VTINHERIT: | |
4019 | case R_NIOS2_GNU_VTENTRY: | |
4020 | return NULL; | |
4021 | } | |
4022 | return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); | |
4023 | } | |
4024 | ||
4025 | /* Implement elf_backend_gc_sweep_hook: | |
4026 | Update the got entry reference counts for the section being removed. */ | |
4027 | static bfd_boolean | |
4028 | nios2_elf32_gc_sweep_hook (bfd *abfd, | |
4029 | struct bfd_link_info *info, | |
4030 | asection *sec, | |
4031 | const Elf_Internal_Rela *relocs) | |
4032 | { | |
4033 | Elf_Internal_Shdr *symtab_hdr; | |
4034 | struct elf_link_hash_entry **sym_hashes; | |
4035 | bfd_signed_vma *local_got_refcounts; | |
4036 | const Elf_Internal_Rela *rel, *relend; | |
4037 | bfd *dynobj; | |
4038 | ||
4039 | if (info->relocatable) | |
4040 | return TRUE; | |
4041 | ||
4042 | elf_section_data (sec)->local_dynrel = NULL; | |
4043 | ||
4044 | dynobj = elf_hash_table (info)->dynobj; | |
4045 | if (dynobj == NULL) | |
4046 | return TRUE; | |
4047 | ||
4048 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
4049 | sym_hashes = elf_sym_hashes (abfd); | |
4050 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
4051 | ||
4052 | relend = relocs + sec->reloc_count; | |
4053 | for (rel = relocs; rel < relend; rel++) | |
4054 | { | |
4055 | unsigned long r_symndx; | |
4056 | struct elf_link_hash_entry *h = NULL; | |
4057 | int r_type; | |
4058 | ||
4059 | r_symndx = ELF32_R_SYM (rel->r_info); | |
4060 | if (r_symndx >= symtab_hdr->sh_info) | |
4061 | { | |
4062 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
4063 | while (h->root.type == bfd_link_hash_indirect | |
4064 | || h->root.type == bfd_link_hash_warning) | |
4065 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4066 | } | |
4067 | ||
4068 | r_type = ELF32_R_TYPE (rel->r_info); | |
4069 | switch (r_type) | |
4070 | { | |
4071 | case R_NIOS2_GOT16: | |
1c2de463 SL |
4072 | case R_NIOS2_GOT_LO: |
4073 | case R_NIOS2_GOT_HA: | |
36591ba1 | 4074 | case R_NIOS2_CALL16: |
1c2de463 SL |
4075 | case R_NIOS2_CALL_LO: |
4076 | case R_NIOS2_CALL_HA: | |
36591ba1 SL |
4077 | if (h != NULL) |
4078 | { | |
4079 | if (h->got.refcount > 0) | |
4080 | --h->got.refcount; | |
4081 | } | |
4082 | else if (local_got_refcounts != NULL) | |
4083 | { | |
4084 | if (local_got_refcounts[r_symndx] > 0) | |
4085 | --local_got_refcounts[r_symndx]; | |
4086 | } | |
4087 | break; | |
4088 | ||
4089 | case R_NIOS2_PCREL_LO: | |
4090 | case R_NIOS2_PCREL_HA: | |
4091 | case R_NIOS2_BFD_RELOC_32: | |
4092 | case R_NIOS2_CALL26: | |
78058a5e | 4093 | case R_NIOS2_CALL26_NOAT: |
36591ba1 SL |
4094 | if (h != NULL) |
4095 | { | |
4096 | struct elf32_nios2_link_hash_entry *eh; | |
4097 | struct elf32_nios2_dyn_relocs **pp; | |
4098 | struct elf32_nios2_dyn_relocs *p; | |
4099 | ||
4100 | eh = (struct elf32_nios2_link_hash_entry *) h; | |
4101 | ||
4102 | if (h->plt.refcount > 0) | |
4103 | --h->plt.refcount; | |
4104 | ||
4105 | if (r_type == R_NIOS2_PCREL_LO || r_type == R_NIOS2_PCREL_HA | |
4106 | || r_type == R_NIOS2_BFD_RELOC_32) | |
4107 | { | |
4108 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; | |
4109 | pp = &p->next) | |
4110 | if (p->sec == sec) | |
4111 | { | |
4112 | p->count -= 1; | |
4113 | if (p->count == 0) | |
4114 | *pp = p->next; | |
4115 | break; | |
4116 | } | |
4117 | } | |
4118 | } | |
4119 | break; | |
4120 | ||
4121 | default: | |
4122 | break; | |
4123 | } | |
4124 | } | |
4125 | ||
4126 | return TRUE; | |
4127 | } | |
4128 | ||
36591ba1 SL |
4129 | /* Implement elf_backend_finish_dynamic_symbols: |
4130 | Finish up dynamic symbol handling. We set the contents of various | |
4131 | dynamic sections here. */ | |
4132 | static bfd_boolean | |
4133 | nios2_elf32_finish_dynamic_symbol (bfd *output_bfd, | |
4134 | struct bfd_link_info *info, | |
4135 | struct elf_link_hash_entry *h, | |
4136 | Elf_Internal_Sym *sym) | |
4137 | { | |
4138 | struct elf32_nios2_link_hash_table *htab; | |
4139 | struct elf32_nios2_link_hash_entry *eh | |
4140 | = (struct elf32_nios2_link_hash_entry *)h; | |
4141 | int use_plt; | |
4142 | ||
4143 | htab = elf32_nios2_hash_table (info); | |
4144 | ||
4145 | if (h->plt.offset != (bfd_vma) -1) | |
4146 | { | |
4147 | asection *splt; | |
4148 | asection *sgotplt; | |
4149 | asection *srela; | |
4150 | bfd_vma plt_index; | |
4151 | bfd_vma got_offset; | |
4152 | Elf_Internal_Rela rela; | |
4153 | bfd_byte *loc; | |
4154 | bfd_vma got_address; | |
4155 | ||
4156 | /* This symbol has an entry in the procedure linkage table. Set | |
4157 | it up. */ | |
4158 | BFD_ASSERT (h->dynindx != -1); | |
4159 | splt = htab->root.splt; | |
4160 | sgotplt = htab->root.sgotplt; | |
4161 | srela = htab->root.srelplt; | |
4162 | BFD_ASSERT (splt != NULL && sgotplt != NULL && srela != NULL); | |
4163 | ||
4164 | /* Emit the PLT entry. */ | |
4165 | if (info->shared) | |
4166 | { | |
4167 | nios2_elf32_install_data (splt, nios2_so_plt_entry, h->plt.offset, | |
4168 | 3); | |
4169 | plt_index = (h->plt.offset - 24) / 12; | |
4170 | got_offset = (plt_index + 3) * 4; | |
4171 | nios2_elf32_install_imm16 (splt, h->plt.offset, | |
4172 | hiadj(plt_index * 4)); | |
4173 | nios2_elf32_install_imm16 (splt, h->plt.offset + 4, | |
4174 | (plt_index * 4) & 0xffff); | |
4175 | nios2_elf32_install_imm16 (splt, h->plt.offset + 8, | |
4176 | 0xfff4 - h->plt.offset); | |
4177 | got_address = (sgotplt->output_section->vma + sgotplt->output_offset | |
4178 | + got_offset); | |
4179 | ||
4180 | /* Fill in the entry in the global offset table. There are no | |
4181 | res_n slots for a shared object PLT, instead the .got.plt entries | |
4182 | point to the PLT entries. */ | |
4183 | bfd_put_32 (output_bfd, | |
4184 | splt->output_section->vma + splt->output_offset | |
4185 | + h->plt.offset, sgotplt->contents + got_offset); | |
4186 | } | |
4187 | else | |
4188 | { | |
4189 | plt_index = (h->plt.offset - 28 - htab->res_n_size) / 12; | |
4190 | got_offset = (plt_index + 3) * 4; | |
4191 | ||
4192 | nios2_elf32_install_data (splt, nios2_plt_entry, h->plt.offset, 3); | |
4193 | got_address = (sgotplt->output_section->vma + sgotplt->output_offset | |
4194 | + got_offset); | |
4195 | nios2_elf32_install_imm16 (splt, h->plt.offset, hiadj(got_address)); | |
4196 | nios2_elf32_install_imm16 (splt, h->plt.offset + 4, | |
4197 | got_address & 0xffff); | |
4198 | ||
4199 | /* Fill in the entry in the global offset table. */ | |
4200 | bfd_put_32 (output_bfd, | |
4201 | splt->output_section->vma + splt->output_offset | |
4202 | + plt_index * 4, sgotplt->contents + got_offset); | |
4203 | } | |
4204 | ||
4205 | /* Fill in the entry in the .rela.plt section. */ | |
4206 | rela.r_offset = got_address; | |
4207 | rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_JUMP_SLOT); | |
4208 | rela.r_addend = 0; | |
4209 | loc = srela->contents + plt_index * sizeof (Elf32_External_Rela); | |
4210 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
4211 | ||
4212 | if (!h->def_regular) | |
4213 | { | |
4214 | /* Mark the symbol as undefined, rather than as defined in | |
4215 | the .plt section. Leave the value alone. */ | |
4216 | sym->st_shndx = SHN_UNDEF; | |
4217 | /* If the symbol is weak, we do need to clear the value. | |
4218 | Otherwise, the PLT entry would provide a definition for | |
4219 | the symbol even if the symbol wasn't defined anywhere, | |
4220 | and so the symbol would never be NULL. */ | |
4221 | if (!h->ref_regular_nonweak) | |
4222 | sym->st_value = 0; | |
4223 | } | |
4224 | } | |
4225 | ||
1c2de463 | 4226 | use_plt = (eh->got_types_used == CALL_USED |
36591ba1 SL |
4227 | && h->plt.offset != (bfd_vma) -1); |
4228 | ||
4229 | if (!use_plt && h->got.offset != (bfd_vma) -1 | |
4230 | && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_GD) == 0 | |
4231 | && (elf32_nios2_hash_entry (h)->tls_type & GOT_TLS_IE) == 0) | |
4232 | { | |
4233 | asection *sgot; | |
4234 | asection *srela; | |
4235 | Elf_Internal_Rela rela; | |
4236 | bfd_byte *loc; | |
4237 | bfd_vma offset; | |
4238 | ||
4239 | /* This symbol has an entry in the global offset table. Set it | |
4240 | up. */ | |
4241 | sgot = htab->root.sgot; | |
4242 | srela = htab->root.srelgot; | |
4243 | BFD_ASSERT (sgot != NULL && srela != NULL); | |
4244 | ||
4245 | offset = (h->got.offset & ~(bfd_vma) 1); | |
4246 | rela.r_offset = (sgot->output_section->vma | |
4247 | + sgot->output_offset + offset); | |
4248 | ||
4249 | /* If this is a -Bsymbolic link, and the symbol is defined | |
4250 | locally, we just want to emit a RELATIVE reloc. Likewise if | |
4251 | the symbol was forced to be local because of a version file. | |
4252 | The entry in the global offset table will already have been | |
4253 | initialized in the relocate_section function. */ | |
4254 | ||
4255 | if (info->shared && SYMBOL_REFERENCES_LOCAL (info, h)) | |
4256 | { | |
4257 | rela.r_info = ELF32_R_INFO (0, R_NIOS2_RELATIVE); | |
4258 | rela.r_addend = bfd_get_signed_32 (output_bfd, | |
4259 | (sgot->contents + offset)); | |
4260 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + offset); | |
4261 | } | |
4262 | else | |
4263 | { | |
4264 | bfd_put_32 (output_bfd, (bfd_vma) 0, | |
4265 | sgot->contents + offset); | |
4266 | rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_GLOB_DAT); | |
4267 | rela.r_addend = 0; | |
4268 | } | |
4269 | ||
4270 | loc = srela->contents; | |
4271 | loc += srela->reloc_count++ * sizeof (Elf32_External_Rela); | |
4272 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
4273 | } | |
4274 | ||
4275 | if (use_plt && h->got.offset != (bfd_vma) -1) | |
4276 | { | |
4277 | bfd_vma offset = (h->got.offset & ~(bfd_vma) 1); | |
4278 | asection *sgot = htab->root.sgot; | |
4279 | asection *splt = htab->root.splt; | |
4280 | bfd_put_32 (output_bfd, (splt->output_section->vma + splt->output_offset | |
4281 | + h->plt.offset), | |
4282 | sgot->contents + offset); | |
4283 | } | |
4284 | ||
4285 | if (h->needs_copy) | |
4286 | { | |
4287 | asection *s; | |
4288 | Elf_Internal_Rela rela; | |
4289 | bfd_byte *loc; | |
4290 | ||
4291 | /* This symbol needs a copy reloc. Set it up. */ | |
4292 | BFD_ASSERT (h->dynindx != -1 | |
4293 | && (h->root.type == bfd_link_hash_defined | |
4294 | || h->root.type == bfd_link_hash_defweak)); | |
4295 | ||
4296 | s = htab->srelbss; | |
4297 | BFD_ASSERT (s != NULL); | |
4298 | ||
4299 | rela.r_offset = (h->root.u.def.value | |
4300 | + h->root.u.def.section->output_section->vma | |
4301 | + h->root.u.def.section->output_offset); | |
4302 | rela.r_info = ELF32_R_INFO (h->dynindx, R_NIOS2_COPY); | |
4303 | rela.r_addend = 0; | |
4304 | loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela); | |
4305 | bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); | |
4306 | } | |
4307 | ||
82e91538 | 4308 | /* Mark _DYNAMIC, _GLOBAL_OFFSET_TABLE_, and _gp_got as absolute. */ |
36591ba1 | 4309 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 |
82e91538 SL |
4310 | || h == elf_hash_table (info)->hgot |
4311 | || h == elf32_nios2_hash_table (info)->h_gp_got) | |
36591ba1 SL |
4312 | sym->st_shndx = SHN_ABS; |
4313 | ||
4314 | return TRUE; | |
4315 | } | |
4316 | ||
4317 | /* Implement elf_backend_finish_dynamic_sections. */ | |
4318 | static bfd_boolean | |
4319 | nios2_elf32_finish_dynamic_sections (bfd *output_bfd, | |
4320 | struct bfd_link_info *info) | |
4321 | { | |
4322 | bfd *dynobj; | |
4323 | asection *sgotplt; | |
4324 | asection *sdyn; | |
4325 | struct elf32_nios2_link_hash_table *htab; | |
4326 | ||
4327 | htab = elf32_nios2_hash_table (info); | |
4328 | dynobj = elf_hash_table (info)->dynobj; | |
4329 | sgotplt = htab->root.sgotplt; | |
4330 | BFD_ASSERT (sgotplt != NULL); | |
4331 | sdyn = bfd_get_linker_section (dynobj, ".dynamic"); | |
4332 | ||
4333 | if (elf_hash_table (info)->dynamic_sections_created) | |
4334 | { | |
4335 | asection *splt; | |
4336 | Elf32_External_Dyn *dyncon, *dynconend; | |
4337 | ||
4338 | splt = htab->root.splt; | |
4339 | BFD_ASSERT (splt != NULL && sdyn != NULL); | |
4340 | ||
4341 | dyncon = (Elf32_External_Dyn *) sdyn->contents; | |
4342 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); | |
4343 | for (; dyncon < dynconend; dyncon++) | |
4344 | { | |
4345 | Elf_Internal_Dyn dyn; | |
4346 | asection *s; | |
4347 | ||
4348 | bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); | |
4349 | ||
4350 | switch (dyn.d_tag) | |
4351 | { | |
4352 | default: | |
4353 | break; | |
4354 | ||
4355 | case DT_PLTGOT: | |
4356 | s = htab->root.sgot; | |
4357 | BFD_ASSERT (s != NULL); | |
4358 | dyn.d_un.d_ptr = s->output_section->vma; | |
4359 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
4360 | break; | |
4361 | ||
4362 | case DT_JMPREL: | |
4363 | s = htab->root.srelplt; | |
4364 | BFD_ASSERT (s != NULL); | |
4365 | dyn.d_un.d_ptr = s->output_section->vma; | |
4366 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
4367 | break; | |
4368 | ||
4369 | case DT_PLTRELSZ: | |
4370 | s = htab->root.srelplt; | |
4371 | BFD_ASSERT (s != NULL); | |
4372 | dyn.d_un.d_val = s->size; | |
4373 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
4374 | break; | |
4375 | ||
4376 | case DT_RELASZ: | |
4377 | /* The procedure linkage table relocs (DT_JMPREL) should | |
4378 | not be included in the overall relocs (DT_RELA). | |
4379 | Therefore, we override the DT_RELASZ entry here to | |
4380 | make it not include the JMPREL relocs. Since the | |
4381 | linker script arranges for .rela.plt to follow all | |
4382 | other relocation sections, we don't have to worry | |
4383 | about changing the DT_RELA entry. */ | |
4384 | s = htab->root.srelplt; | |
4385 | if (s != NULL) | |
4386 | dyn.d_un.d_val -= s->size; | |
4387 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
4388 | break; | |
4389 | ||
4390 | case DT_NIOS2_GP: | |
4391 | s = htab->root.sgot; | |
4392 | BFD_ASSERT (s != NULL); | |
4393 | dyn.d_un.d_ptr = s->output_section->vma + 0x7ff0; | |
4394 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
4395 | break; | |
4396 | } | |
4397 | } | |
4398 | ||
4399 | /* Fill in the first entry in the procedure linkage table. */ | |
4400 | if (splt->size > 0) | |
4401 | { | |
4402 | bfd_vma got_address = (sgotplt->output_section->vma | |
4403 | + sgotplt->output_offset); | |
4404 | if (info->shared) | |
4405 | { | |
4406 | bfd_vma corrected = got_address - (splt->output_section->vma | |
4407 | + splt->output_offset + 4); | |
4408 | nios2_elf32_install_data (splt, nios2_so_plt0_entry, 0, 6); | |
4409 | nios2_elf32_install_imm16 (splt, 4, hiadj (corrected)); | |
4410 | nios2_elf32_install_imm16 (splt, 12, (corrected & 0xffff) + 4); | |
4411 | nios2_elf32_install_imm16 (splt, 16, (corrected & 0xffff) + 8); | |
36591ba1 SL |
4412 | } |
4413 | else | |
4414 | { | |
4415 | /* Divide by 4 here, not 3 because we already corrected for the | |
4416 | res_N branches. */ | |
4417 | bfd_vma res_size = (splt->size - 28) / 4; | |
4418 | bfd_vma res_start = (splt->output_section->vma | |
4419 | + splt->output_offset); | |
4420 | bfd_vma res_offset; | |
4421 | ||
4422 | for (res_offset = 0; res_offset < res_size; res_offset += 4) | |
4423 | bfd_put_32 (output_bfd, | |
4424 | 6 | ((res_size - (res_offset + 4)) << 6), | |
4425 | splt->contents + res_offset); | |
4426 | ||
4427 | nios2_elf32_install_data (splt, nios2_plt0_entry, res_size, 7); | |
4428 | nios2_elf32_install_imm16 (splt, res_size, hiadj (res_start)); | |
4429 | nios2_elf32_install_imm16 (splt, res_size + 4, | |
4430 | res_start & 0xffff); | |
4431 | nios2_elf32_install_imm16 (splt, res_size + 12, | |
4432 | hiadj (got_address)); | |
4433 | nios2_elf32_install_imm16 (splt, res_size + 16, | |
4434 | (got_address & 0xffff) + 4); | |
4435 | nios2_elf32_install_imm16 (splt, res_size + 20, | |
4436 | (got_address & 0xffff) + 8); | |
36591ba1 SL |
4437 | } |
4438 | } | |
4439 | } | |
4440 | /* Fill in the first three entries in the global offset table. */ | |
4441 | if (sgotplt->size > 0) | |
4442 | { | |
4443 | if (sdyn == NULL) | |
4444 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents); | |
4445 | else | |
4446 | bfd_put_32 (output_bfd, | |
4447 | sdyn->output_section->vma + sdyn->output_offset, | |
4448 | sgotplt->contents); | |
4449 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4); | |
4450 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8); | |
4451 | } | |
4452 | ||
4453 | elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4; | |
4454 | ||
4455 | return TRUE; | |
4456 | } | |
4457 | ||
4458 | /* Implement elf_backend_adjust_dynamic_symbol: | |
4459 | Adjust a symbol defined by a dynamic object and referenced by a | |
4460 | regular object. The current definition is in some section of the | |
4461 | dynamic object, but we're not including those sections. We have to | |
4462 | change the definition to something the rest of the link can | |
4463 | understand. */ | |
4464 | static bfd_boolean | |
4465 | nios2_elf32_adjust_dynamic_symbol (struct bfd_link_info *info, | |
4466 | struct elf_link_hash_entry *h) | |
4467 | { | |
4468 | struct elf32_nios2_link_hash_table *htab; | |
4469 | bfd *dynobj; | |
4470 | asection *s; | |
4471 | unsigned align2; | |
4472 | ||
4473 | htab = elf32_nios2_hash_table (info); | |
4474 | dynobj = elf_hash_table (info)->dynobj; | |
4475 | ||
4476 | /* Make sure we know what is going on here. */ | |
4477 | BFD_ASSERT (dynobj != NULL | |
4478 | && (h->needs_plt | |
4479 | || h->u.weakdef != NULL | |
4480 | || (h->def_dynamic | |
4481 | && h->ref_regular | |
4482 | && !h->def_regular))); | |
4483 | ||
4484 | /* If this is a function, put it in the procedure linkage table. We | |
4485 | will fill in the contents of the procedure linkage table later, | |
4486 | when we know the address of the .got section. */ | |
4487 | if (h->type == STT_FUNC || h->needs_plt) | |
4488 | { | |
4489 | if (h->plt.refcount <= 0 | |
4490 | || SYMBOL_CALLS_LOCAL (info, h) | |
4491 | || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT | |
4492 | && h->root.type == bfd_link_hash_undefweak)) | |
4493 | { | |
4494 | /* This case can occur if we saw a PLT reloc in an input | |
4495 | file, but the symbol was never referred to by a dynamic | |
4496 | object, or if all references were garbage collected. In | |
4497 | such a case, we don't actually need to build a procedure | |
4498 | linkage table, and we can just do a PCREL reloc instead. */ | |
4499 | h->plt.offset = (bfd_vma) -1; | |
4500 | h->needs_plt = 0; | |
4501 | } | |
4502 | ||
4503 | return TRUE; | |
4504 | } | |
4505 | ||
4506 | /* Reinitialize the plt offset now that it is not used as a reference | |
4507 | count any more. */ | |
4508 | h->plt.offset = (bfd_vma) -1; | |
4509 | ||
4510 | /* If this is a weak symbol, and there is a real definition, the | |
4511 | processor independent code will have arranged for us to see the | |
4512 | real definition first, and we can just use the same value. */ | |
4513 | if (h->u.weakdef != NULL) | |
4514 | { | |
4515 | BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined | |
4516 | || h->u.weakdef->root.type == bfd_link_hash_defweak); | |
4517 | h->root.u.def.section = h->u.weakdef->root.u.def.section; | |
4518 | h->root.u.def.value = h->u.weakdef->root.u.def.value; | |
4519 | return TRUE; | |
4520 | } | |
4521 | ||
4522 | /* If there are no non-GOT references, we do not need a copy | |
4523 | relocation. */ | |
4524 | if (!h->non_got_ref) | |
4525 | return TRUE; | |
4526 | ||
4527 | /* This is a reference to a symbol defined by a dynamic object which | |
4528 | is not a function. | |
4529 | If we are creating a shared library, we must presume that the | |
4530 | only references to the symbol are via the global offset table. | |
4531 | For such cases we need not do anything here; the relocations will | |
4532 | be handled correctly by relocate_section. */ | |
4533 | if (info->shared) | |
4534 | return TRUE; | |
4535 | ||
4536 | if (h->size == 0) | |
4537 | { | |
4538 | (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"), | |
4539 | h->root.root.string); | |
4540 | return TRUE; | |
4541 | } | |
4542 | ||
4543 | /* We must allocate the symbol in our .dynbss section, which will | |
4544 | become part of the .bss section of the executable. There will be | |
4545 | an entry for this symbol in the .dynsym section. The dynamic | |
4546 | object will contain position independent code, so all references | |
4547 | from the dynamic object to this symbol will go through the global | |
4548 | offset table. The dynamic linker will use the .dynsym entry to | |
4549 | determine the address it must put in the global offset table, so | |
4550 | both the dynamic object and the regular object will refer to the | |
4551 | same memory location for the variable. */ | |
4552 | s = htab->sdynbss; | |
4553 | BFD_ASSERT (s != NULL); | |
4554 | ||
4555 | /* We must generate a R_NIOS2_COPY reloc to tell the dynamic linker to | |
4556 | copy the initial value out of the dynamic object and into the | |
4557 | runtime process image. We need to remember the offset into the | |
4558 | .rela.bss section we are going to use. */ | |
4559 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) | |
4560 | { | |
4561 | asection *srel; | |
4562 | ||
4563 | srel = htab->srelbss; | |
4564 | BFD_ASSERT (srel != NULL); | |
4565 | srel->size += sizeof (Elf32_External_Rela); | |
4566 | h->needs_copy = 1; | |
4567 | } | |
4568 | ||
4569 | align2 = bfd_log2 (h->size); | |
4570 | if (align2 > h->root.u.def.section->alignment_power) | |
4571 | align2 = h->root.u.def.section->alignment_power; | |
4572 | ||
4573 | /* Align dynbss. */ | |
4574 | s->size = BFD_ALIGN (s->size, (bfd_size_type)1 << align2); | |
4575 | if (align2 > bfd_get_section_alignment (dynobj, s) | |
4576 | && !bfd_set_section_alignment (dynobj, s, align2)) | |
4577 | return FALSE; | |
4578 | ||
4579 | /* Define the symbol as being at this point in the section. */ | |
4580 | h->root.u.def.section = s; | |
4581 | h->root.u.def.value = s->size; | |
4582 | ||
4583 | /* Increment the section size to make room for the symbol. */ | |
4584 | s->size += h->size; | |
4585 | ||
4586 | return TRUE; | |
4587 | } | |
4588 | ||
4589 | /* Worker function for nios2_elf32_size_dynamic_sections. */ | |
4590 | static bfd_boolean | |
4591 | adjust_dynrelocs (struct elf_link_hash_entry *h, PTR inf) | |
4592 | { | |
4593 | struct bfd_link_info *info; | |
4594 | struct elf32_nios2_link_hash_table *htab; | |
4595 | ||
4596 | if (h->root.type == bfd_link_hash_indirect) | |
4597 | return TRUE; | |
4598 | ||
4599 | if (h->root.type == bfd_link_hash_warning) | |
4600 | /* When warning symbols are created, they **replace** the "real" | |
4601 | entry in the hash table, thus we never get to see the real | |
4602 | symbol in a hash traversal. So look at it now. */ | |
4603 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4604 | ||
4605 | info = (struct bfd_link_info *) inf; | |
4606 | htab = elf32_nios2_hash_table (info); | |
4607 | ||
4608 | if (h->plt.offset != (bfd_vma)-1) | |
4609 | h->plt.offset += htab->res_n_size; | |
4610 | if (htab->root.splt == h->root.u.def.section) | |
4611 | h->root.u.def.value += htab->res_n_size; | |
4612 | ||
4613 | return TRUE; | |
4614 | } | |
4615 | ||
4616 | /* Another worker function for nios2_elf32_size_dynamic_sections. | |
4617 | Allocate space in .plt, .got and associated reloc sections for | |
4618 | dynamic relocs. */ | |
4619 | static bfd_boolean | |
4620 | allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf) | |
4621 | { | |
4622 | struct bfd_link_info *info; | |
4623 | struct elf32_nios2_link_hash_table *htab; | |
4624 | struct elf32_nios2_link_hash_entry *eh; | |
4625 | struct elf32_nios2_dyn_relocs *p; | |
4626 | int use_plt; | |
4627 | ||
4628 | if (h->root.type == bfd_link_hash_indirect) | |
4629 | return TRUE; | |
4630 | ||
4631 | if (h->root.type == bfd_link_hash_warning) | |
4632 | /* When warning symbols are created, they **replace** the "real" | |
4633 | entry in the hash table, thus we never get to see the real | |
4634 | symbol in a hash traversal. So look at it now. */ | |
4635 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
4636 | ||
4637 | info = (struct bfd_link_info *) inf; | |
4638 | htab = elf32_nios2_hash_table (info); | |
4639 | ||
4640 | if (htab->root.dynamic_sections_created | |
4641 | && h->plt.refcount > 0) | |
4642 | { | |
4643 | /* Make sure this symbol is output as a dynamic symbol. | |
4644 | Undefined weak syms won't yet be marked as dynamic. */ | |
4645 | if (h->dynindx == -1 | |
4646 | && !h->forced_local | |
4647 | && !bfd_elf_link_record_dynamic_symbol (info, h)) | |
4648 | return FALSE; | |
4649 | ||
4650 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h)) | |
4651 | { | |
4652 | asection *s = htab->root.splt; | |
4653 | ||
4654 | /* Allocate room for the header. */ | |
4655 | if (s->size == 0) | |
4656 | { | |
4657 | if (info->shared) | |
4658 | s->size = 24; | |
4659 | else | |
4660 | s->size = 28; | |
4661 | } | |
4662 | ||
4663 | h->plt.offset = s->size; | |
4664 | ||
4665 | /* If this symbol is not defined in a regular file, and we are | |
4666 | not generating a shared library, then set the symbol to this | |
4667 | location in the .plt. This is required to make function | |
4668 | pointers compare as equal between the normal executable and | |
4669 | the shared library. */ | |
4670 | if (! info->shared | |
4671 | && !h->def_regular) | |
4672 | { | |
4673 | h->root.u.def.section = s; | |
4674 | h->root.u.def.value = h->plt.offset; | |
4675 | } | |
4676 | ||
4677 | /* Make room for this entry. */ | |
4678 | s->size += 12; | |
4679 | ||
4680 | /* We also need to make an entry in the .rela.plt section. */ | |
4681 | htab->root.srelplt->size += sizeof (Elf32_External_Rela); | |
4682 | ||
4683 | /* And the .got.plt section. */ | |
4684 | htab->root.sgotplt->size += 4; | |
4685 | } | |
4686 | else | |
4687 | { | |
4688 | h->plt.offset = (bfd_vma) -1; | |
4689 | h->needs_plt = 0; | |
4690 | } | |
4691 | } | |
4692 | else | |
4693 | { | |
4694 | h->plt.offset = (bfd_vma) -1; | |
4695 | h->needs_plt = 0; | |
4696 | } | |
4697 | ||
4698 | eh = (struct elf32_nios2_link_hash_entry *) h; | |
1c2de463 | 4699 | use_plt = (eh->got_types_used == CALL_USED |
36591ba1 SL |
4700 | && h->plt.offset != (bfd_vma) -1); |
4701 | ||
4702 | if (h->got.refcount > 0) | |
4703 | { | |
4704 | asection *s; | |
4705 | bfd_boolean dyn; | |
4706 | int tls_type = eh->tls_type; | |
4707 | int indx; | |
4708 | ||
4709 | /* Make sure this symbol is output as a dynamic symbol. | |
4710 | Undefined weak syms won't yet be marked as dynamic. */ | |
4711 | if (h->dynindx == -1 | |
4712 | && !h->forced_local | |
4713 | && !bfd_elf_link_record_dynamic_symbol (info, h)) | |
4714 | return FALSE; | |
4715 | ||
4716 | s = htab->root.sgot; | |
4717 | h->got.offset = s->size; | |
4718 | ||
4719 | if (tls_type == GOT_UNKNOWN) | |
4720 | abort (); | |
4721 | ||
4722 | if (tls_type == GOT_NORMAL) | |
4723 | /* Non-TLS symbols need one GOT slot. */ | |
4724 | s->size += 4; | |
4725 | else | |
4726 | { | |
4727 | if (tls_type & GOT_TLS_GD) | |
4728 | /* R_NIOS2_TLS_GD16 needs 2 consecutive GOT slots. */ | |
4729 | s->size += 8; | |
4730 | if (tls_type & GOT_TLS_IE) | |
4731 | /* R_NIOS2_TLS_IE16 needs one GOT slot. */ | |
4732 | s->size += 4; | |
4733 | } | |
4734 | ||
4735 | dyn = htab->root.dynamic_sections_created; | |
4736 | ||
4737 | indx = 0; | |
4738 | if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) | |
4739 | && (!info->shared | |
4740 | || !SYMBOL_REFERENCES_LOCAL (info, h))) | |
4741 | indx = h->dynindx; | |
4742 | ||
4743 | if (tls_type != GOT_NORMAL | |
4744 | && (info->shared || indx != 0) | |
4745 | && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
4746 | || h->root.type != bfd_link_hash_undefweak)) | |
4747 | { | |
4748 | if (tls_type & GOT_TLS_IE) | |
4749 | htab->root.srelgot->size += sizeof (Elf32_External_Rela); | |
4750 | ||
4751 | if (tls_type & GOT_TLS_GD) | |
4752 | htab->root.srelgot->size += sizeof (Elf32_External_Rela); | |
4753 | ||
4754 | if ((tls_type & GOT_TLS_GD) && indx != 0) | |
4755 | htab->root.srelgot->size += sizeof (Elf32_External_Rela); | |
4756 | } | |
4757 | else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT | |
4758 | || h->root.type != bfd_link_hash_undefweak) | |
4759 | && !use_plt | |
4760 | && (info->shared | |
4761 | || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))) | |
4762 | htab->root.srelgot->size += sizeof (Elf32_External_Rela); | |
4763 | } | |
4764 | else | |
4765 | h->got.offset = (bfd_vma) -1; | |
4766 | ||
4767 | if (eh->dyn_relocs == NULL) | |
4768 | return TRUE; | |
4769 | ||
4770 | /* In the shared -Bsymbolic case, discard space allocated for | |
4771 | dynamic pc-relative relocs against symbols which turn out to be | |
4772 | defined in regular objects. For the normal shared case, discard | |
4773 | space for pc-relative relocs that have become local due to symbol | |
4774 | visibility changes. */ | |
4775 | ||
4776 | if (info->shared) | |
4777 | { | |
4778 | if (h->def_regular | |
4779 | && (h->forced_local || info->symbolic)) | |
4780 | { | |
4781 | struct elf32_nios2_dyn_relocs **pp; | |
4782 | ||
4783 | for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) | |
4784 | { | |
4785 | p->count -= p->pc_count; | |
4786 | p->pc_count = 0; | |
4787 | if (p->count == 0) | |
4788 | *pp = p->next; | |
4789 | else | |
4790 | pp = &p->next; | |
4791 | } | |
4792 | } | |
4793 | ||
4794 | /* Also discard relocs on undefined weak syms with non-default | |
4795 | visibility. */ | |
4796 | if (eh->dyn_relocs != NULL | |
4797 | && h->root.type == bfd_link_hash_undefweak) | |
4798 | { | |
4799 | if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) | |
4800 | eh->dyn_relocs = NULL; | |
4801 | ||
4802 | /* Make sure undefined weak symbols are output as a dynamic | |
4803 | symbol in PIEs. */ | |
4804 | else if (h->dynindx == -1 | |
4805 | && !h->forced_local | |
4806 | && !bfd_elf_link_record_dynamic_symbol (info, h)) | |
4807 | return FALSE; | |
4808 | } | |
4809 | } | |
4810 | else | |
4811 | { | |
4812 | /* For the non-shared case, discard space for relocs against | |
4813 | symbols which turn out to need copy relocs or are not | |
4814 | dynamic. */ | |
4815 | ||
4816 | if (!h->non_got_ref | |
4817 | && ((h->def_dynamic && !h->def_regular) | |
4818 | || (htab->root.dynamic_sections_created | |
4819 | && (h->root.type == bfd_link_hash_undefweak | |
4820 | || h->root.type == bfd_link_hash_undefined)))) | |
4821 | { | |
4822 | /* Make sure this symbol is output as a dynamic symbol. | |
4823 | Undefined weak syms won't yet be marked as dynamic. */ | |
4824 | if (h->dynindx == -1 | |
4825 | && !h->forced_local | |
4826 | && !bfd_elf_link_record_dynamic_symbol (info, h)) | |
4827 | return FALSE; | |
4828 | ||
4829 | /* If that succeeded, we know we'll be keeping all the | |
4830 | relocs. */ | |
4831 | if (h->dynindx != -1) | |
4832 | goto keep; | |
4833 | } | |
4834 | ||
4835 | eh->dyn_relocs = NULL; | |
4836 | ||
4837 | keep: ; | |
4838 | } | |
4839 | ||
4840 | /* Finally, allocate space. */ | |
4841 | for (p = eh->dyn_relocs; p != NULL; p = p->next) | |
4842 | { | |
4843 | asection *sreloc = elf_section_data (p->sec)->sreloc; | |
4844 | sreloc->size += p->count * sizeof (Elf32_External_Rela); | |
4845 | } | |
4846 | ||
4847 | return TRUE; | |
4848 | } | |
4849 | ||
4850 | /* Implement elf_backend_size_dynamic_sections: | |
4851 | Set the sizes of the dynamic sections. */ | |
4852 | static bfd_boolean | |
4853 | nios2_elf32_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED, | |
4854 | struct bfd_link_info *info) | |
4855 | { | |
4856 | bfd *dynobj; | |
4857 | asection *s; | |
4858 | bfd_boolean plt; | |
4859 | bfd_boolean got; | |
4860 | bfd_boolean relocs; | |
4861 | bfd *ibfd; | |
4862 | struct elf32_nios2_link_hash_table *htab; | |
4863 | ||
4864 | htab = elf32_nios2_hash_table (info); | |
4865 | dynobj = elf_hash_table (info)->dynobj; | |
4866 | BFD_ASSERT (dynobj != NULL); | |
4867 | ||
4868 | htab->res_n_size = 0; | |
4869 | if (elf_hash_table (info)->dynamic_sections_created) | |
4870 | { | |
4871 | /* Set the contents of the .interp section to the interpreter. */ | |
4872 | if (info->executable) | |
4873 | { | |
4874 | s = bfd_get_linker_section (dynobj, ".interp"); | |
4875 | BFD_ASSERT (s != NULL); | |
4876 | s->size = sizeof ELF_DYNAMIC_INTERPRETER; | |
4877 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
4878 | } | |
4879 | } | |
4880 | else | |
4881 | { | |
4882 | /* We may have created entries in the .rela.got section. | |
4883 | However, if we are not creating the dynamic sections, we will | |
4884 | not actually use these entries. Reset the size of .rela.got, | |
4885 | which will cause it to get stripped from the output file | |
4886 | below. */ | |
4887 | s = htab->root.srelgot; | |
4888 | if (s != NULL) | |
4889 | s->size = 0; | |
4890 | } | |
4891 | ||
4892 | /* Set up .got offsets for local syms, and space for local dynamic | |
4893 | relocs. */ | |
c72f2fb2 | 4894 | for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) |
36591ba1 SL |
4895 | { |
4896 | bfd_signed_vma *local_got; | |
4897 | bfd_signed_vma *end_local_got; | |
4898 | char *local_tls_type; | |
4899 | bfd_size_type locsymcount; | |
4900 | Elf_Internal_Shdr *symtab_hdr; | |
4901 | asection *srel; | |
4902 | ||
4903 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) | |
4904 | continue; | |
4905 | ||
4906 | for (s = ibfd->sections; s != NULL; s = s->next) | |
4907 | { | |
4908 | struct elf32_nios2_dyn_relocs *p; | |
4909 | ||
4910 | for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next) | |
4911 | { | |
4912 | if (!bfd_is_abs_section (p->sec) | |
4913 | && bfd_is_abs_section (p->sec->output_section)) | |
4914 | { | |
4915 | /* Input section has been discarded, either because | |
4916 | it is a copy of a linkonce section or due to | |
4917 | linker script /DISCARD/, so we'll be discarding | |
4918 | the relocs too. */ | |
4919 | } | |
4920 | else if (p->count != 0) | |
4921 | { | |
4922 | srel = elf_section_data (p->sec)->sreloc; | |
4923 | srel->size += p->count * sizeof (Elf32_External_Rela); | |
4924 | if ((p->sec->output_section->flags & SEC_READONLY) != 0) | |
4925 | info->flags |= DF_TEXTREL; | |
4926 | } | |
4927 | } | |
4928 | } | |
4929 | ||
4930 | local_got = elf_local_got_refcounts (ibfd); | |
4931 | if (!local_got) | |
4932 | continue; | |
4933 | ||
4934 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
4935 | locsymcount = symtab_hdr->sh_info; | |
4936 | end_local_got = local_got + locsymcount; | |
4937 | local_tls_type = elf32_nios2_local_got_tls_type (ibfd); | |
4938 | s = htab->root.sgot; | |
4939 | srel = htab->root.srelgot; | |
4940 | for (; local_got < end_local_got; ++local_got, ++local_tls_type) | |
4941 | { | |
4942 | if (*local_got > 0) | |
4943 | { | |
4944 | *local_got = s->size; | |
4945 | if (*local_tls_type & GOT_TLS_GD) | |
4946 | /* TLS_GD relocs need an 8-byte structure in the GOT. */ | |
4947 | s->size += 8; | |
4948 | if (*local_tls_type & GOT_TLS_IE) | |
4949 | s->size += 4; | |
4950 | if (*local_tls_type == GOT_NORMAL) | |
4951 | s->size += 4; | |
4952 | ||
4953 | if (info->shared || *local_tls_type == GOT_TLS_GD) | |
4954 | srel->size += sizeof (Elf32_External_Rela); | |
4955 | } | |
4956 | else | |
4957 | *local_got = (bfd_vma) -1; | |
4958 | } | |
4959 | } | |
4960 | ||
4961 | if (htab->tls_ldm_got.refcount > 0) | |
4962 | { | |
4963 | /* Allocate two GOT entries and one dynamic relocation (if necessary) | |
4964 | for R_NIOS2_TLS_LDM16 relocations. */ | |
4965 | htab->tls_ldm_got.offset = htab->root.sgot->size; | |
4966 | htab->root.sgot->size += 8; | |
4967 | if (info->shared) | |
4968 | htab->root.srelgot->size += sizeof (Elf32_External_Rela); | |
4969 | } | |
4970 | else | |
4971 | htab->tls_ldm_got.offset = -1; | |
4972 | ||
4973 | /* Allocate global sym .plt and .got entries, and space for global | |
4974 | sym dynamic relocs. */ | |
4975 | elf_link_hash_traverse (& htab->root, allocate_dynrelocs, info); | |
4976 | ||
82e91538 SL |
4977 | if (elf_hash_table (info)->dynamic_sections_created) |
4978 | { | |
4979 | /* If the .got section is more than 0x8000 bytes, we add | |
4980 | 0x8000 to the value of _gp_got, so that 16-bit relocations | |
4981 | have a greater chance of working. */ | |
4982 | if (htab->root.sgot->size >= 0x8000 | |
4983 | && elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value == 0) | |
4984 | elf32_nios2_hash_table (info)->h_gp_got->root.u.def.value = 0x8000; | |
4985 | } | |
4986 | ||
36591ba1 SL |
4987 | /* The check_relocs and adjust_dynamic_symbol entry points have |
4988 | determined the sizes of the various dynamic sections. Allocate | |
4989 | memory for them. */ | |
4990 | plt = FALSE; | |
4991 | got = FALSE; | |
4992 | relocs = FALSE; | |
4993 | for (s = dynobj->sections; s != NULL; s = s->next) | |
4994 | { | |
4995 | const char *name; | |
4996 | ||
4997 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
4998 | continue; | |
4999 | ||
5000 | /* It's OK to base decisions on the section name, because none | |
5001 | of the dynobj section names depend upon the input files. */ | |
5002 | name = bfd_get_section_name (dynobj, s); | |
5003 | ||
5004 | if (strcmp (name, ".plt") == 0) | |
5005 | { | |
5006 | /* Remember whether there is a PLT. */ | |
5007 | plt = s->size != 0; | |
5008 | ||
5009 | /* Correct for the number of res_N branches. */ | |
5010 | if (plt && !info->shared) | |
5011 | { | |
5012 | htab->res_n_size = (s->size-28) / 3; | |
5013 | s->size += htab->res_n_size; | |
5014 | } | |
5015 | } | |
5016 | else if (CONST_STRNEQ (name, ".rela")) | |
5017 | { | |
5018 | if (s->size != 0) | |
5019 | { | |
5020 | relocs = TRUE; | |
5021 | ||
5022 | /* We use the reloc_count field as a counter if we need | |
5023 | to copy relocs into the output file. */ | |
5024 | s->reloc_count = 0; | |
5025 | } | |
5026 | } | |
5027 | else if (CONST_STRNEQ (name, ".got")) | |
5028 | got = s->size != 0; | |
5029 | else if (strcmp (name, ".dynbss") != 0) | |
5030 | /* It's not one of our sections, so don't allocate space. */ | |
5031 | continue; | |
5032 | ||
5033 | if (s->size == 0) | |
5034 | { | |
5035 | /* If we don't need this section, strip it from the | |
5036 | output file. This is mostly to handle .rela.bss and | |
5037 | .rela.plt. We must create both sections in | |
5038 | create_dynamic_sections, because they must be created | |
5039 | before the linker maps input sections to output | |
5040 | sections. The linker does that before | |
5041 | adjust_dynamic_symbol is called, and it is that | |
5042 | function which decides whether anything needs to go | |
5043 | into these sections. */ | |
5044 | s->flags |= SEC_EXCLUDE; | |
5045 | continue; | |
5046 | } | |
5047 | ||
5048 | if ((s->flags & SEC_HAS_CONTENTS) == 0) | |
5049 | continue; | |
5050 | ||
5051 | /* Allocate memory for the section contents. */ | |
5052 | /* FIXME: This should be a call to bfd_alloc not bfd_zalloc. | |
5053 | Unused entries should be reclaimed before the section's contents | |
5054 | are written out, but at the moment this does not happen. Thus in | |
5055 | order to prevent writing out garbage, we initialize the section's | |
5056 | contents to zero. */ | |
5057 | s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); | |
5058 | if (s->contents == NULL) | |
5059 | return FALSE; | |
5060 | } | |
5061 | ||
5062 | /* Adjust dynamic symbols that point to the plt to account for the | |
5063 | now-known number of resN slots. */ | |
5064 | if (htab->res_n_size) | |
5065 | elf_link_hash_traverse (& htab->root, adjust_dynrelocs, info); | |
5066 | ||
5067 | if (elf_hash_table (info)->dynamic_sections_created) | |
5068 | { | |
5069 | /* Add some entries to the .dynamic section. We fill in the | |
5070 | values later, in elf_nios2_finish_dynamic_sections, but we | |
5071 | must add the entries now so that we get the correct size for | |
5072 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
5073 | dynamic linker and used by the debugger. */ | |
5074 | #define add_dynamic_entry(TAG, VAL) \ | |
5075 | _bfd_elf_add_dynamic_entry (info, TAG, VAL) | |
5076 | ||
5077 | if (!info->shared && !add_dynamic_entry (DT_DEBUG, 0)) | |
5078 | return FALSE; | |
5079 | ||
5080 | if (got && !add_dynamic_entry (DT_PLTGOT, 0)) | |
5081 | return FALSE; | |
5082 | ||
5083 | if (plt | |
5084 | && (!add_dynamic_entry (DT_PLTRELSZ, 0) | |
5085 | || !add_dynamic_entry (DT_PLTREL, DT_RELA) | |
5086 | || !add_dynamic_entry (DT_JMPREL, 0))) | |
5087 | return FALSE; | |
5088 | ||
5089 | if (relocs | |
5090 | && (!add_dynamic_entry (DT_RELA, 0) | |
5091 | || !add_dynamic_entry (DT_RELASZ, 0) | |
5092 | || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))) | |
5093 | return FALSE; | |
5094 | ||
5095 | if (!info->shared && !add_dynamic_entry (DT_NIOS2_GP, 0)) | |
5096 | return FALSE; | |
5097 | ||
5098 | if ((info->flags & DF_TEXTREL) != 0 | |
5099 | && !add_dynamic_entry (DT_TEXTREL, 0)) | |
5100 | return FALSE; | |
5101 | } | |
5102 | #undef add_dynamic_entry | |
5103 | ||
5104 | return TRUE; | |
5105 | } | |
5106 | ||
68faa637 AM |
5107 | /* Free the derived linker hash table. */ |
5108 | static void | |
d495ab0d | 5109 | nios2_elf32_link_hash_table_free (bfd *obfd) |
68faa637 AM |
5110 | { |
5111 | struct elf32_nios2_link_hash_table *htab | |
d495ab0d | 5112 | = (struct elf32_nios2_link_hash_table *) obfd->link.hash; |
68faa637 AM |
5113 | |
5114 | bfd_hash_table_free (&htab->bstab); | |
d495ab0d | 5115 | _bfd_elf_link_hash_table_free (obfd); |
68faa637 AM |
5116 | } |
5117 | ||
36591ba1 SL |
5118 | /* Implement bfd_elf32_bfd_link_hash_table_create. */ |
5119 | static struct bfd_link_hash_table * | |
5120 | nios2_elf32_link_hash_table_create (bfd *abfd) | |
5121 | { | |
5122 | struct elf32_nios2_link_hash_table *ret; | |
5123 | bfd_size_type amt = sizeof (struct elf32_nios2_link_hash_table); | |
5124 | ||
7bf52ea2 | 5125 | ret = bfd_zmalloc (amt); |
36591ba1 SL |
5126 | if (ret == NULL) |
5127 | return NULL; | |
5128 | ||
5129 | if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, | |
5130 | link_hash_newfunc, | |
5131 | sizeof (struct | |
5132 | elf32_nios2_link_hash_entry), | |
5133 | NIOS2_ELF_DATA)) | |
5134 | { | |
5135 | free (ret); | |
5136 | return NULL; | |
5137 | } | |
5138 | ||
78058a5e SL |
5139 | /* Init the stub hash table too. */ |
5140 | if (!bfd_hash_table_init (&ret->bstab, stub_hash_newfunc, | |
5141 | sizeof (struct elf32_nios2_stub_hash_entry))) | |
d495ab0d AM |
5142 | { |
5143 | _bfd_elf_link_hash_table_free (abfd); | |
5144 | return NULL; | |
5145 | } | |
5146 | ret->root.root.hash_table_free = nios2_elf32_link_hash_table_free; | |
78058a5e | 5147 | |
36591ba1 SL |
5148 | return &ret->root.root; |
5149 | } | |
5150 | ||
5151 | /* Implement elf_backend_reloc_type_class. */ | |
5152 | static enum elf_reloc_type_class | |
7e612e98 AM |
5153 | nios2_elf32_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, |
5154 | const asection *rel_sec ATTRIBUTE_UNUSED, | |
5155 | const Elf_Internal_Rela *rela) | |
36591ba1 SL |
5156 | { |
5157 | switch ((int) ELF32_R_TYPE (rela->r_info)) | |
5158 | { | |
5159 | case R_NIOS2_RELATIVE: | |
5160 | return reloc_class_relative; | |
5161 | case R_NIOS2_JUMP_SLOT: | |
5162 | return reloc_class_plt; | |
5163 | case R_NIOS2_COPY: | |
5164 | return reloc_class_copy; | |
5165 | default: | |
5166 | return reloc_class_normal; | |
5167 | } | |
5168 | } | |
5169 | ||
5170 | /* Return 1 if target is one of ours. */ | |
5171 | static bfd_boolean | |
5172 | is_nios2_elf_target (const struct bfd_target *targ) | |
5173 | { | |
6d00b590 AM |
5174 | return (targ == &nios2_elf32_le_vec |
5175 | || targ == &nios2_elf32_be_vec); | |
36591ba1 SL |
5176 | } |
5177 | ||
5178 | /* Implement elf_backend_add_symbol_hook. | |
5179 | This hook is called by the linker when adding symbols from an object | |
5180 | file. We use it to put .comm items in .sbss, and not .bss. */ | |
5181 | static bfd_boolean | |
5182 | nios2_elf_add_symbol_hook (bfd *abfd, | |
5183 | struct bfd_link_info *info, | |
5184 | Elf_Internal_Sym *sym, | |
5185 | const char **namep ATTRIBUTE_UNUSED, | |
5186 | flagword *flagsp ATTRIBUTE_UNUSED, | |
5187 | asection **secp, | |
5188 | bfd_vma *valp) | |
5189 | { | |
5190 | bfd *dynobj; | |
5191 | ||
5192 | if (sym->st_shndx == SHN_COMMON | |
5193 | && !info->relocatable | |
5194 | && sym->st_size <= elf_gp_size (abfd) | |
5195 | && is_nios2_elf_target (info->output_bfd->xvec)) | |
5196 | { | |
5197 | /* Common symbols less than or equal to -G nn bytes are automatically | |
5198 | put into .sbss. */ | |
5199 | struct elf32_nios2_link_hash_table *htab; | |
5200 | ||
5201 | htab = elf32_nios2_hash_table (info); | |
5202 | if (htab->sbss == NULL) | |
5203 | { | |
5204 | flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED; | |
5205 | ||
5206 | dynobj = elf_hash_table (info)->dynobj; | |
5207 | if (!dynobj) | |
5208 | dynobj = abfd; | |
5209 | ||
5210 | htab->sbss = bfd_make_section_anyway_with_flags (dynobj, ".sbss", | |
5211 | flags); | |
5212 | if (htab->sbss == NULL) | |
5213 | return FALSE; | |
5214 | } | |
5215 | ||
5216 | *secp = htab->sbss; | |
5217 | *valp = sym->st_size; | |
5218 | } | |
5219 | ||
5220 | return TRUE; | |
5221 | } | |
5222 | ||
5223 | /* Implement elf_backend_can_make_relative_eh_frame: | |
5224 | Decide whether to attempt to turn absptr or lsda encodings in | |
5225 | shared libraries into pcrel within the given input section. */ | |
5226 | static bfd_boolean | |
5227 | nios2_elf32_can_make_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED, | |
5228 | struct bfd_link_info *info | |
5229 | ATTRIBUTE_UNUSED, | |
5230 | asection *eh_frame_section | |
5231 | ATTRIBUTE_UNUSED) | |
5232 | { | |
5233 | /* We can't use PC-relative encodings in the .eh_frame section. */ | |
5234 | return FALSE; | |
5235 | } | |
5236 | ||
5237 | /* Implement elf_backend_special_sections. */ | |
5238 | const struct bfd_elf_special_section elf32_nios2_special_sections[] = | |
5239 | { | |
5240 | { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, | |
5241 | SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL }, | |
5242 | { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, | |
5243 | SHF_ALLOC + SHF_WRITE + SHF_NIOS2_GPREL }, | |
5244 | { NULL, 0, 0, 0, 0 } | |
5245 | }; | |
5246 | ||
5247 | #define ELF_ARCH bfd_arch_nios2 | |
5248 | #define ELF_TARGET_ID NIOS2_ELF_DATA | |
5249 | #define ELF_MACHINE_CODE EM_ALTERA_NIOS2 | |
5250 | ||
5251 | /* The Nios II MMU uses a 4K page size. */ | |
5252 | ||
5253 | #define ELF_MAXPAGESIZE 0x1000 | |
5254 | ||
5255 | #define bfd_elf32_bfd_link_hash_table_create \ | |
5256 | nios2_elf32_link_hash_table_create | |
5257 | ||
5258 | /* Relocation table lookup macros. */ | |
5259 | ||
5260 | #define bfd_elf32_bfd_reloc_type_lookup nios2_elf32_bfd_reloc_type_lookup | |
5261 | #define bfd_elf32_bfd_reloc_name_lookup nios2_elf32_bfd_reloc_name_lookup | |
5262 | ||
5263 | /* JUMP_TABLE_LINK macros. */ | |
5264 | ||
5265 | /* elf_info_to_howto (using RELA relocations). */ | |
5266 | ||
5267 | #define elf_info_to_howto nios2_elf32_info_to_howto | |
5268 | ||
5269 | /* elf backend functions. */ | |
5270 | ||
5271 | #define elf_backend_can_gc_sections 1 | |
5272 | #define elf_backend_can_refcount 1 | |
5273 | #define elf_backend_plt_readonly 1 | |
5274 | #define elf_backend_want_got_plt 1 | |
5275 | #define elf_backend_rela_normal 1 | |
5276 | ||
5277 | #define elf_backend_relocate_section nios2_elf32_relocate_section | |
5278 | #define elf_backend_section_flags nios2_elf32_section_flags | |
5279 | #define elf_backend_fake_sections nios2_elf32_fake_sections | |
5280 | #define elf_backend_check_relocs nios2_elf32_check_relocs | |
5281 | ||
5282 | #define elf_backend_gc_mark_hook nios2_elf32_gc_mark_hook | |
5283 | #define elf_backend_gc_sweep_hook nios2_elf32_gc_sweep_hook | |
5284 | #define elf_backend_create_dynamic_sections \ | |
5285 | nios2_elf32_create_dynamic_sections | |
5286 | #define elf_backend_finish_dynamic_symbol nios2_elf32_finish_dynamic_symbol | |
5287 | #define elf_backend_finish_dynamic_sections \ | |
5288 | nios2_elf32_finish_dynamic_sections | |
5289 | #define elf_backend_adjust_dynamic_symbol nios2_elf32_adjust_dynamic_symbol | |
5290 | #define elf_backend_reloc_type_class nios2_elf32_reloc_type_class | |
5291 | #define elf_backend_size_dynamic_sections nios2_elf32_size_dynamic_sections | |
5292 | #define elf_backend_add_symbol_hook nios2_elf_add_symbol_hook | |
5293 | #define elf_backend_copy_indirect_symbol nios2_elf32_copy_indirect_symbol | |
5294 | ||
5295 | #define elf_backend_grok_prstatus nios2_grok_prstatus | |
5296 | #define elf_backend_grok_psinfo nios2_grok_psinfo | |
5297 | ||
5298 | #undef elf_backend_can_make_relative_eh_frame | |
5299 | #define elf_backend_can_make_relative_eh_frame \ | |
5300 | nios2_elf32_can_make_relative_eh_frame | |
5301 | ||
5302 | #define elf_backend_special_sections elf32_nios2_special_sections | |
5303 | ||
6d00b590 | 5304 | #define TARGET_LITTLE_SYM nios2_elf32_le_vec |
36591ba1 | 5305 | #define TARGET_LITTLE_NAME "elf32-littlenios2" |
6d00b590 | 5306 | #define TARGET_BIG_SYM nios2_elf32_be_vec |
36591ba1 SL |
5307 | #define TARGET_BIG_NAME "elf32-bignios2" |
5308 | ||
5309 | #define elf_backend_got_header_size 12 | |
5310 | ||
5311 | #include "elf32-target.h" |