]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-microblaze.c
Include MI command in remotelog.
[thirdparty/binutils-gdb.git] / bfd / elf32-microblaze.c
CommitLineData
7ba29e2a
NC
1/* Xilinx MicroBlaze-specific support for 32-bit ELF
2
fb34365b 3 Copyright 2009, 2010 Free Software Foundation, Inc.
7ba29e2a
NC
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22
23int dbg = 0;
24
25#include "bfd.h"
26#include "sysdep.h"
27#include "bfdlink.h"
28#include "libbfd.h"
29#include "elf-bfd.h"
30#include "elf/microblaze.h"
31#include <assert.h>
32
33#define USE_RELA /* Only USE_REL is actually significant, but this is
34 here are a reminder... */
35#define INST_WORD_SIZE 4
36
37static int ro_small_data_pointer = 0;
38static int rw_small_data_pointer = 0;
39
40static reloc_howto_type * microblaze_elf_howto_table [(int) R_MICROBLAZE_max];
41
42static reloc_howto_type microblaze_elf_howto_raw[] =
43{
44 /* This reloc does nothing. */
45 HOWTO (R_MICROBLAZE_NONE, /* Type. */
46 0, /* Rightshift. */
47 2, /* Size (0 = byte, 1 = short, 2 = long). */
48 32, /* Bitsize. */
49 FALSE, /* PC_relative. */
50 0, /* Bitpos. */
51 complain_overflow_bitfield, /* Complain on overflow. */
52 NULL, /* Special Function. */
53 "R_MICROBLAZE_NONE", /* Name. */
54 FALSE, /* Partial Inplace. */
55 0, /* Source Mask. */
56 0, /* Dest Mask. */
57 FALSE), /* PC relative offset? */
58
59 /* A standard 32 bit relocation. */
60 HOWTO (R_MICROBLAZE_32, /* Type. */
61 0, /* Rightshift. */
62 2, /* Size (0 = byte, 1 = short, 2 = long). */
63 32, /* Bitsize. */
64 FALSE, /* PC_relative. */
65 0, /* Bitpos. */
66 complain_overflow_bitfield, /* Complain on overflow. */
67 bfd_elf_generic_reloc,/* Special Function. */
68 "R_MICROBLAZE_32", /* Name. */
69 FALSE, /* Partial Inplace. */
70 0, /* Source Mask. */
71 0xffffffff, /* Dest Mask. */
72 FALSE), /* PC relative offset? */
73
74 /* A standard PCREL 32 bit relocation. */
75 HOWTO (R_MICROBLAZE_32_PCREL,/* Type. */
76 0, /* Rightshift. */
77 2, /* Size (0 = byte, 1 = short, 2 = long). */
78 32, /* Bitsize. */
79 TRUE, /* PC_relative. */
80 0, /* Bitpos. */
81 complain_overflow_bitfield, /* Complain on overflow. */
82 bfd_elf_generic_reloc,/* Special Function. */
83 "R_MICROBLAZE_32_PCREL", /* Name. */
84 TRUE, /* Partial Inplace. */
85 0, /* Source Mask. */
86 0xffffffff, /* Dest Mask. */
87 TRUE), /* PC relative offset? */
88
89 /* A 64 bit PCREL relocation. Table-entry not really used. */
90 HOWTO (R_MICROBLAZE_64_PCREL,/* Type. */
91 0, /* Rightshift. */
92 2, /* Size (0 = byte, 1 = short, 2 = long). */
93 16, /* Bitsize. */
94 TRUE, /* PC_relative. */
95 0, /* Bitpos. */
96 complain_overflow_dont, /* Complain on overflow. */
97 bfd_elf_generic_reloc,/* Special Function. */
98 "R_MICROBLAZE_64_PCREL", /* Name. */
99 FALSE, /* Partial Inplace. */
100 0, /* Source Mask. */
101 0x0000ffff, /* Dest Mask. */
102 TRUE), /* PC relative offset? */
103
104 /* The low half of a PCREL 32 bit relocation. */
105 HOWTO (R_MICROBLAZE_32_PCREL_LO, /* Type. */
106 0, /* Rightshift. */
107 2, /* Size (0 = byte, 1 = short, 2 = long). */
108 16, /* Bitsize. */
109 TRUE, /* PC_relative. */
110 0, /* Bitpos. */
111 complain_overflow_signed, /* Complain on overflow. */
112 bfd_elf_generic_reloc, /* Special Function. */
113 "R_MICROBLAZE_32_PCREL_LO", /* Name. */
114 FALSE, /* Partial Inplace. */
115 0, /* Source Mask. */
116 0x0000ffff, /* Dest Mask. */
117 TRUE), /* PC relative offset? */
118
119 /* A 64 bit relocation. Table entry not really used. */
120 HOWTO (R_MICROBLAZE_64, /* Type. */
121 0, /* Rightshift. */
122 2, /* Size (0 = byte, 1 = short, 2 = long). */
123 16, /* Bitsize. */
124 FALSE, /* PC_relative. */
125 0, /* Bitpos. */
126 complain_overflow_dont, /* Complain on overflow. */
127 bfd_elf_generic_reloc,/* Special Function. */
128 "R_MICROBLAZE_64", /* Name. */
129 FALSE, /* Partial Inplace. */
130 0, /* Source Mask. */
131 0x0000ffff, /* Dest Mask. */
132 FALSE), /* PC relative offset? */
133
134 /* The low half of a 32 bit relocation. */
135 HOWTO (R_MICROBLAZE_32_LO, /* Type. */
136 0, /* Rightshift. */
137 2, /* Size (0 = byte, 1 = short, 2 = long). */
138 16, /* Bitsize. */
139 FALSE, /* PC_relative. */
140 0, /* Bitpos. */
141 complain_overflow_signed, /* Complain on overflow. */
142 bfd_elf_generic_reloc,/* Special Function. */
143 "R_MICROBLAZE_32_LO", /* Name. */
144 FALSE, /* Partial Inplace. */
145 0, /* Source Mask. */
146 0x0000ffff, /* Dest Mask. */
147 FALSE), /* PC relative offset? */
148
149 /* Read-only small data section relocation. */
150 HOWTO (R_MICROBLAZE_SRO32, /* Type. */
151 0, /* Rightshift. */
152 2, /* Size (0 = byte, 1 = short, 2 = long). */
153 16, /* Bitsize. */
154 FALSE, /* PC_relative. */
155 0, /* Bitpos. */
156 complain_overflow_bitfield, /* Complain on overflow. */
157 bfd_elf_generic_reloc,/* Special Function. */
158 "R_MICROBLAZE_SRO32", /* Name. */
159 FALSE, /* Partial Inplace. */
160 0, /* Source Mask. */
161 0x0000ffff, /* Dest Mask. */
162 FALSE), /* PC relative offset? */
163
164 /* Read-write small data area relocation. */
165 HOWTO (R_MICROBLAZE_SRW32, /* Type. */
166 0, /* Rightshift. */
167 2, /* Size (0 = byte, 1 = short, 2 = long). */
168 16, /* Bitsize. */
169 FALSE, /* PC_relative. */
170 0, /* Bitpos. */
171 complain_overflow_bitfield, /* Complain on overflow. */
172 bfd_elf_generic_reloc,/* Special Function. */
173 "R_MICROBLAZE_SRW32", /* Name. */
174 FALSE, /* Partial Inplace. */
175 0, /* Source Mask. */
176 0x0000ffff, /* Dest Mask. */
177 FALSE), /* PC relative offset? */
178
179 /* This reloc does nothing. Used for relaxation. */
180 HOWTO (R_MICROBLAZE_64_NONE, /* Type. */
181 0, /* Rightshift. */
182 2, /* Size (0 = byte, 1 = short, 2 = long). */
183 32, /* Bitsize. */
184 TRUE, /* PC_relative. */
185 0, /* Bitpos. */
186 complain_overflow_bitfield, /* Complain on overflow. */
187 NULL, /* Special Function. */
188 "R_MICROBLAZE_64_NONE",/* Name. */
189 FALSE, /* Partial Inplace. */
190 0, /* Source Mask. */
191 0, /* Dest Mask. */
192 FALSE), /* PC relative offset? */
193
194 /* Symbol Op Symbol relocation. */
195 HOWTO (R_MICROBLAZE_32_SYM_OP_SYM, /* Type. */
196 0, /* Rightshift. */
197 2, /* Size (0 = byte, 1 = short, 2 = long). */
198 32, /* Bitsize. */
199 FALSE, /* PC_relative. */
200 0, /* Bitpos. */
201 complain_overflow_bitfield, /* Complain on overflow. */
202 bfd_elf_generic_reloc,/* Special Function. */
203 "R_MICROBLAZE_32_SYM_OP_SYM", /* Name. */
204 FALSE, /* Partial Inplace. */
205 0, /* Source Mask. */
206 0xffffffff, /* Dest Mask. */
207 FALSE), /* PC relative offset? */
208
209 /* GNU extension to record C++ vtable hierarchy. */
210 HOWTO (R_MICROBLAZE_GNU_VTINHERIT, /* Type. */
211 0, /* Rightshift. */
212 2, /* Size (0 = byte, 1 = short, 2 = long). */
213 0, /* Bitsize. */
214 FALSE, /* PC_relative. */
215 0, /* Bitpos. */
216 complain_overflow_dont,/* Complain on overflow. */
217 NULL, /* Special Function. */
218 "R_MICROBLAZE_GNU_VTINHERIT", /* Name. */
219 FALSE, /* Partial Inplace. */
220 0, /* Source Mask. */
221 0, /* Dest Mask. */
222 FALSE), /* PC relative offset? */
223
224 /* GNU extension to record C++ vtable member usage. */
225 HOWTO (R_MICROBLAZE_GNU_VTENTRY, /* Type. */
226 0, /* Rightshift. */
227 2, /* Size (0 = byte, 1 = short, 2 = long). */
228 0, /* Bitsize. */
229 FALSE, /* PC_relative. */
230 0, /* Bitpos. */
231 complain_overflow_dont,/* Complain on overflow. */
232 _bfd_elf_rel_vtable_reloc_fn, /* Special Function. */
233 "R_MICROBLAZE_GNU_VTENTRY", /* Name. */
234 FALSE, /* Partial Inplace. */
235 0, /* Source Mask. */
236 0, /* Dest Mask. */
237 FALSE), /* PC relative offset? */
238
239 /* A 64 bit GOTPC relocation. Table-entry not really used. */
240 HOWTO (R_MICROBLAZE_GOTPC_64, /* Type. */
241 0, /* Rightshift. */
242 2, /* Size (0 = byte, 1 = short, 2 = long). */
243 16, /* Bitsize. */
244 TRUE, /* PC_relative. */
245 0, /* Bitpos. */
246 complain_overflow_dont, /* Complain on overflow. */
247 bfd_elf_generic_reloc, /* Special Function. */
248 "R_MICROBLAZE_GOTPC_64", /* Name. */
249 FALSE, /* Partial Inplace. */
250 0, /* Source Mask. */
251 0x0000ffff, /* Dest Mask. */
252 TRUE), /* PC relative offset? */
253
254 /* A 64 bit GOT relocation. Table-entry not really used. */
255 HOWTO (R_MICROBLAZE_GOT_64, /* Type. */
256 0, /* Rightshift. */
257 2, /* Size (0 = byte, 1 = short, 2 = long). */
258 16, /* Bitsize. */
259 FALSE, /* PC_relative. */
260 0, /* Bitpos. */
261 complain_overflow_dont, /* Complain on overflow. */
262 bfd_elf_generic_reloc,/* Special Function. */
263 "R_MICROBLAZE_GOT_64",/* Name. */
264 FALSE, /* Partial Inplace. */
265 0, /* Source Mask. */
266 0x0000ffff, /* Dest Mask. */
267 FALSE), /* PC relative offset? */
268
269 /* A 64 bit PLT relocation. Table-entry not really used. */
270 HOWTO (R_MICROBLAZE_PLT_64, /* Type. */
271 0, /* Rightshift. */
272 2, /* Size (0 = byte, 1 = short, 2 = long). */
273 16, /* Bitsize. */
274 TRUE, /* PC_relative. */
275 0, /* Bitpos. */
276 complain_overflow_dont, /* Complain on overflow. */
277 bfd_elf_generic_reloc,/* Special Function. */
278 "R_MICROBLAZE_PLT_64",/* Name. */
279 FALSE, /* Partial Inplace. */
280 0, /* Source Mask. */
281 0x0000ffff, /* Dest Mask. */
282 TRUE), /* PC relative offset? */
283
284 /* Table-entry not really used. */
285 HOWTO (R_MICROBLAZE_REL, /* Type. */
286 0, /* Rightshift. */
287 2, /* Size (0 = byte, 1 = short, 2 = long). */
288 16, /* Bitsize. */
289 TRUE, /* PC_relative. */
290 0, /* Bitpos. */
291 complain_overflow_dont, /* Complain on overflow. */
292 bfd_elf_generic_reloc,/* Special Function. */
293 "R_MICROBLAZE_REL", /* Name. */
294 FALSE, /* Partial Inplace. */
295 0, /* Source Mask. */
296 0x0000ffff, /* Dest Mask. */
297 TRUE), /* PC relative offset? */
298
299 /* Table-entry not really used. */
300 HOWTO (R_MICROBLAZE_JUMP_SLOT,/* Type. */
301 0, /* Rightshift. */
302 2, /* Size (0 = byte, 1 = short, 2 = long). */
303 16, /* Bitsize. */
304 TRUE, /* PC_relative. */
305 0, /* Bitpos. */
306 complain_overflow_dont, /* Complain on overflow. */
307 bfd_elf_generic_reloc,/* Special Function. */
308 "R_MICROBLAZE_JUMP_SLOT", /* Name. */
309 FALSE, /* Partial Inplace. */
310 0, /* Source Mask. */
311 0x0000ffff, /* Dest Mask. */
312 TRUE), /* PC relative offset? */
313
314 /* Table-entry not really used. */
315 HOWTO (R_MICROBLAZE_GLOB_DAT,/* Type. */
316 0, /* Rightshift. */
317 2, /* Size (0 = byte, 1 = short, 2 = long). */
318 16, /* Bitsize. */
319 TRUE, /* PC_relative. */
320 0, /* Bitpos. */
321 complain_overflow_dont, /* Complain on overflow. */
322 bfd_elf_generic_reloc,/* Special Function. */
323 "R_MICROBLAZE_GLOB_DAT", /* Name. */
324 FALSE, /* Partial Inplace. */
325 0, /* Source Mask. */
326 0x0000ffff, /* Dest Mask. */
327 TRUE), /* PC relative offset? */
328
329 /* A 64 bit GOT relative relocation. Table-entry not really used. */
330 HOWTO (R_MICROBLAZE_GOTOFF_64, /* Type. */
331 0, /* Rightshift. */
332 2, /* Size (0 = byte, 1 = short, 2 = long). */
333 16, /* Bitsize. */
334 FALSE, /* PC_relative. */
335 0, /* Bitpos. */
336 complain_overflow_dont, /* Complain on overflow. */
337 bfd_elf_generic_reloc,/* Special Function. */
338 "R_MICROBLAZE_GOTOFF_64", /* Name. */
339 FALSE, /* Partial Inplace. */
340 0, /* Source Mask. */
341 0x0000ffff, /* Dest Mask. */
342 FALSE), /* PC relative offset? */
343
344 /* A 32 bit GOT relative relocation. Table-entry not really used. */
345 HOWTO (R_MICROBLAZE_GOTOFF_32, /* Type. */
346 0, /* Rightshift. */
347 2, /* Size (0 = byte, 1 = short, 2 = long). */
348 16, /* Bitsize. */
349 FALSE, /* PC_relative. */
350 0, /* Bitpos. */
351 complain_overflow_dont, /* Complain on overflow. */
352 bfd_elf_generic_reloc, /* Special Function. */
353 "R_MICROBLAZE_GOTOFF_32", /* Name. */
354 FALSE, /* Partial Inplace. */
355 0, /* Source Mask. */
356 0x0000ffff, /* Dest Mask. */
357 FALSE), /* PC relative offset? */
358
359 /* COPY relocation. Table-entry not really used. */
360 HOWTO (R_MICROBLAZE_COPY, /* Type. */
361 0, /* Rightshift. */
362 2, /* Size (0 = byte, 1 = short, 2 = long). */
363 16, /* Bitsize. */
364 FALSE, /* PC_relative. */
365 0, /* Bitpos. */
366 complain_overflow_dont, /* Complain on overflow. */
367 bfd_elf_generic_reloc,/* Special Function. */
368 "R_MICROBLAZE_COPY", /* Name. */
369 FALSE, /* Partial Inplace. */
370 0, /* Source Mask. */
371 0x0000ffff, /* Dest Mask. */
372 FALSE), /* PC relative offset? */
373};
374
375#ifndef NUM_ELEM
376#define NUM_ELEM(a) (sizeof (a) / sizeof (a)[0])
377#endif
378\f
379/* Initialize the microblaze_elf_howto_table, so that linear accesses can be done. */
380
381static void
382microblaze_elf_howto_init (void)
383{
384 unsigned int i;
385
386 for (i = NUM_ELEM (microblaze_elf_howto_raw); i--;)
387 {
388 unsigned int type;
389
390 type = microblaze_elf_howto_raw[i].type;
391
392 BFD_ASSERT (type < NUM_ELEM (microblaze_elf_howto_table));
393
394 microblaze_elf_howto_table [type] = & microblaze_elf_howto_raw [i];
395 }
396}
397\f
398static reloc_howto_type *
399microblaze_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
400 bfd_reloc_code_real_type code)
401{
402 enum elf_microblaze_reloc_type microblaze_reloc = R_MICROBLAZE_NONE;
403
404 switch (code)
405 {
406 case BFD_RELOC_NONE:
407 microblaze_reloc = R_MICROBLAZE_NONE;
408 break;
409 case BFD_RELOC_MICROBLAZE_64_NONE:
410 microblaze_reloc = R_MICROBLAZE_64_NONE;
411 break;
412 case BFD_RELOC_32:
413 microblaze_reloc = R_MICROBLAZE_32;
414 break;
415 /* RVA is treated the same as 32 */
416 case BFD_RELOC_RVA:
417 microblaze_reloc = R_MICROBLAZE_32;
418 break;
419 case BFD_RELOC_32_PCREL:
420 microblaze_reloc = R_MICROBLAZE_32_PCREL;
421 break;
422 case BFD_RELOC_64_PCREL:
423 microblaze_reloc = R_MICROBLAZE_64_PCREL;
424 break;
425 case BFD_RELOC_MICROBLAZE_32_LO_PCREL:
426 microblaze_reloc = R_MICROBLAZE_32_PCREL_LO;
427 break;
428 case BFD_RELOC_64:
429 microblaze_reloc = R_MICROBLAZE_64;
430 break;
431 case BFD_RELOC_MICROBLAZE_32_LO:
432 microblaze_reloc = R_MICROBLAZE_32_LO;
433 break;
434 case BFD_RELOC_MICROBLAZE_32_ROSDA:
435 microblaze_reloc = R_MICROBLAZE_SRO32;
436 break;
437 case BFD_RELOC_MICROBLAZE_32_RWSDA:
438 microblaze_reloc = R_MICROBLAZE_SRW32;
439 break;
440 case BFD_RELOC_MICROBLAZE_32_SYM_OP_SYM:
441 microblaze_reloc = R_MICROBLAZE_32_SYM_OP_SYM;
442 break;
443 case BFD_RELOC_VTABLE_INHERIT:
444 microblaze_reloc = R_MICROBLAZE_GNU_VTINHERIT;
445 break;
446 case BFD_RELOC_VTABLE_ENTRY:
447 microblaze_reloc = R_MICROBLAZE_GNU_VTENTRY;
448 break;
449 case BFD_RELOC_MICROBLAZE_64_GOTPC:
450 microblaze_reloc = R_MICROBLAZE_GOTPC_64;
451 break;
452 case BFD_RELOC_MICROBLAZE_64_GOT:
453 microblaze_reloc = R_MICROBLAZE_GOT_64;
454 break;
455 case BFD_RELOC_MICROBLAZE_64_PLT:
456 microblaze_reloc = R_MICROBLAZE_PLT_64;
457 break;
458 case BFD_RELOC_MICROBLAZE_64_GOTOFF:
459 microblaze_reloc = R_MICROBLAZE_GOTOFF_64;
460 break;
461 case BFD_RELOC_MICROBLAZE_32_GOTOFF:
462 microblaze_reloc = R_MICROBLAZE_GOTOFF_32;
463 break;
464 case BFD_RELOC_MICROBLAZE_COPY:
465 microblaze_reloc = R_MICROBLAZE_COPY;
466 break;
467 default:
468 return (reloc_howto_type *) NULL;
469 }
470
471 if (!microblaze_elf_howto_table [R_MICROBLAZE_32])
472 /* Initialize howto table if needed. */
473 microblaze_elf_howto_init ();
474
475 return microblaze_elf_howto_table [(int) microblaze_reloc];
476};
477
478static reloc_howto_type *
479microblaze_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
480 const char *r_name)
481{
482 unsigned int i;
483
484 for (i = 0; i < NUM_ELEM (microblaze_elf_howto_raw); i++)
485 if (microblaze_elf_howto_raw[i].name != NULL
486 && strcasecmp (microblaze_elf_howto_raw[i].name, r_name) == 0)
487 return &microblaze_elf_howto_raw[i];
488
489 return NULL;
490}
491
492/* Set the howto pointer for a RCE ELF reloc. */
493
494static void
495microblaze_elf_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
496 arelent * cache_ptr,
497 Elf_Internal_Rela * dst)
498{
499 if (!microblaze_elf_howto_table [R_MICROBLAZE_32])
500 /* Initialize howto table if needed. */
501 microblaze_elf_howto_init ();
502
503 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_MICROBLAZE_max);
504
505 cache_ptr->howto = microblaze_elf_howto_table [ELF32_R_TYPE (dst->r_info)];
506}
507
508/* Microblaze ELF local labels start with 'L.' or '$L', not '.L'. */
509
510static bfd_boolean
511microblaze_elf_is_local_label_name (bfd *abfd, const char *name)
512{
513 if (name[0] == 'L' && name[1] == '.')
514 return TRUE;
515
516 if (name[0] == '$' && name[1] == 'L')
517 return TRUE;
518
519 /* With gcc, the labels go back to starting with '.', so we accept
520 the generic ELF local label syntax as well. */
521 return _bfd_elf_is_local_label_name (abfd, name);
522}
523
524/* The microblaze linker (like many others) needs to keep track of
525 the number of relocs that it decides to copy as dynamic relocs in
526 check_relocs for each symbol. This is so that it can later discard
527 them if they are found to be unnecessary. We store the information
528 in a field extending the regular ELF linker hash table. */
529
530struct elf32_mb_dyn_relocs
531{
532 struct elf32_mb_dyn_relocs *next;
533
534 /* The input section of the reloc. */
535 asection *sec;
536
537 /* Total number of relocs copied for the input section. */
538 bfd_size_type count;
539
540 /* Number of pc-relative relocs copied for the input section. */
541 bfd_size_type pc_count;
542};
543
544/* ELF linker hash entry. */
545
546struct elf32_mb_link_hash_entry
547{
548 struct elf_link_hash_entry elf;
549
550 /* Track dynamic relocs copied for this symbol. */
551 struct elf32_mb_dyn_relocs *dyn_relocs;
552
553};
554
555#define elf32_mb_hash_entry(ent) ((struct elf32_mb_link_hash_entry *)(ent))
556
557/* ELF linker hash table. */
558
559struct elf32_mb_link_hash_table
560{
561 struct elf_link_hash_table elf;
562
563 /* Short-cuts to get to dynamic linker sections. */
564 asection *sgot;
565 asection *sgotplt;
566 asection *srelgot;
567 asection *splt;
568 asection *srelplt;
569 asection *sdynbss;
570 asection *srelbss;
571
572 /* Small local sym to section mapping cache. */
573 struct sym_cache sym_sec;
574};
575
576/* Get the ELF linker hash table from a link_info structure. */
577
578#define elf32_mb_hash_table(p) \
579 ((struct elf32_mb_link_hash_table *) ((p)->hash))
580
581/* Create an entry in a microblaze ELF linker hash table. */
582
583static struct bfd_hash_entry *
584link_hash_newfunc (struct bfd_hash_entry *entry,
585 struct bfd_hash_table *table,
586 const char *string)
587{
588 /* Allocate the structure if it has not already been allocated by a
589 subclass. */
590 if (entry == NULL)
591 {
592 entry = bfd_hash_allocate (table,
593 sizeof (struct elf32_mb_link_hash_entry));
594 if (entry == NULL)
595 return entry;
596 }
597
598 /* Call the allocation method of the superclass. */
599 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
600 if (entry != NULL)
601 {
602 struct elf32_mb_link_hash_entry *eh;
603
604 eh = (struct elf32_mb_link_hash_entry *) entry;
605 eh->dyn_relocs = NULL;
606 }
607
608 return entry;
609}
610
611/* Create a mb ELF linker hash table. */
612
613static struct bfd_link_hash_table *
614microblaze_elf_link_hash_table_create (bfd *abfd)
615{
616 struct elf32_mb_link_hash_table *ret;
617 bfd_size_type amt = sizeof (struct elf32_mb_link_hash_table);
618
619 ret = (struct elf32_mb_link_hash_table *) bfd_zmalloc (amt);
620 if (ret == NULL)
621 return NULL;
622
623 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
624 sizeof (struct elf32_mb_link_hash_entry)))
625 {
626 free (ret);
627 return NULL;
628 }
629
630 return &ret->elf.root;
631}
632\f
633/* Set the values of the small data pointers. */
634
635static void
636microblaze_elf_final_sdp (struct bfd_link_info *info)
637{
638 struct bfd_link_hash_entry *h;
639
640 h = bfd_link_hash_lookup (info->hash, RO_SDA_ANCHOR_NAME, FALSE, FALSE, TRUE);
641 if (h != (struct bfd_link_hash_entry *) NULL
642 && h->type == bfd_link_hash_defined)
643 ro_small_data_pointer = (h->u.def.value
644 + h->u.def.section->output_section->vma
645 + h->u.def.section->output_offset);
646
647 h = bfd_link_hash_lookup (info->hash, RW_SDA_ANCHOR_NAME, FALSE, FALSE, TRUE);
648 if (h != (struct bfd_link_hash_entry *) NULL
649 && h->type == bfd_link_hash_defined)
650 rw_small_data_pointer = (h->u.def.value
651 + h->u.def.section->output_section->vma
652 + h->u.def.section->output_offset);
653}
654
655/* This code is taken from elf32-m32r.c
656 There is some attempt to make this function usable for many architectures,
657 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
658 if only to serve as a learning tool.
659
660 The RELOCATE_SECTION function is called by the new ELF backend linker
661 to handle the relocations for a section.
662
663 The relocs are always passed as Rela structures; if the section
664 actually uses Rel structures, the r_addend field will always be
665 zero.
666
667 This function is responsible for adjust the section contents as
668 necessary, and (if using Rela relocs and generating a
669 relocatable output file) adjusting the reloc addend as
670 necessary.
671
672 This function does not have to worry about setting the reloc
673 address or the reloc symbol index.
674
675 LOCAL_SYMS is a pointer to the swapped in local symbols.
676
677 LOCAL_SECTIONS is an array giving the section in the input file
678 corresponding to the st_shndx field of each local symbol.
679
680 The global hash table entry for the global symbols can be found
681 via elf_sym_hashes (input_bfd).
682
683 When generating relocatable output, this function must handle
684 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
685 going to be the section symbol corresponding to the output
686 section, which means that the addend must be adjusted
687 accordingly. */
688
689static bfd_boolean
690microblaze_elf_relocate_section (bfd *output_bfd,
691 struct bfd_link_info *info,
692 bfd *input_bfd,
693 asection *input_section,
694 bfd_byte *contents,
695 Elf_Internal_Rela *relocs,
696 Elf_Internal_Sym *local_syms,
697 asection **local_sections)
698{
699 struct elf32_mb_link_hash_table *htab;
700 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
701 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
702 Elf_Internal_Rela *rel, *relend;
703 /* Assume success. */
704 bfd_boolean ret = TRUE;
705 asection *sreloc;
706 bfd_vma *local_got_offsets;
707
708 if (!microblaze_elf_howto_table[R_MICROBLAZE_max-1])
709 microblaze_elf_howto_init ();
710
711 htab = elf32_mb_hash_table (info);
712 local_got_offsets = elf_local_got_offsets (input_bfd);
713
714 sreloc = elf_section_data (input_section)->sreloc;
715
716 rel = relocs;
717 relend = relocs + input_section->reloc_count;
718 for (; rel < relend; rel++)
719 {
720 int r_type;
721 reloc_howto_type *howto;
722 unsigned long r_symndx;
723 bfd_vma addend = rel->r_addend;
724 bfd_vma offset = rel->r_offset;
725 struct elf_link_hash_entry *h;
726 Elf_Internal_Sym *sym;
727 asection *sec;
728 const char *sym_name;
729 bfd_reloc_status_type r = bfd_reloc_ok;
730 const char *errmsg = NULL;
731 bfd_boolean unresolved_reloc = FALSE;
732
733 h = NULL;
734 r_type = ELF32_R_TYPE (rel->r_info);
735 if (r_type < 0 || r_type >= (int) R_MICROBLAZE_max)
736 {
737 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
738 bfd_get_filename (input_bfd), (int) r_type);
739 bfd_set_error (bfd_error_bad_value);
740 ret = FALSE;
741 continue;
742 }
743
744 howto = microblaze_elf_howto_table[r_type];
745 r_symndx = ELF32_R_SYM (rel->r_info);
746
747 if (info->relocatable)
748 {
749 /* This is a relocatable link. We don't have to change
750 anything, unless the reloc is against a section symbol,
751 in which case we have to adjust according to where the
752 section symbol winds up in the output section. */
753 sec = NULL;
754 if (r_symndx >= symtab_hdr->sh_info)
755 /* External symbol. */
756 continue;
757
758 /* Local symbol. */
759 sym = local_syms + r_symndx;
760 sym_name = "<local symbol>";
761 /* STT_SECTION: symbol is associated with a section. */
762 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
763 /* Symbol isn't associated with a section. Nothing to do. */
764 continue;
765
766 sec = local_sections[r_symndx];
767 addend += sec->output_offset + sym->st_value;
768#ifndef USE_REL
769 /* This can't be done for USE_REL because it doesn't mean anything
770 and elf_link_input_bfd asserts this stays zero. */
771 /* rel->r_addend = addend; */
772#endif
773
774#ifndef USE_REL
775 /* Addends are stored with relocs. We're done. */
776 continue;
777#else /* USE_REL */
778 /* If partial_inplace, we need to store any additional addend
779 back in the section. */
780 if (!howto->partial_inplace)
781 continue;
782 /* ??? Here is a nice place to call a special_function like handler. */
783 r = _bfd_relocate_contents (howto, input_bfd, addend,
784 contents + offset);
785#endif /* USE_REL */
786 }
787 else
788 {
789 bfd_vma relocation;
790
791 /* This is a final link. */
792 sym = NULL;
793 sec = NULL;
794 unresolved_reloc = FALSE;
795
796 if (r_symndx < symtab_hdr->sh_info)
797 {
798 /* Local symbol. */
799 sym = local_syms + r_symndx;
800 sec = local_sections[r_symndx];
801 if (sec == 0)
802 continue;
803 sym_name = "<local symbol>";
804 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
805 /* r_addend may have changed if the reference section was
806 a merge section. */
807 addend = rel->r_addend;
808 }
809 else
810 {
811 /* External symbol. */
812 bfd_boolean warned ATTRIBUTE_UNUSED;
813
814 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
815 r_symndx, symtab_hdr, sym_hashes,
816 h, sec, relocation,
817 unresolved_reloc, warned);
818 sym_name = h->root.root.string;
819 }
820
821 /* Sanity check the address. */
822 if (offset > bfd_get_section_limit (output_bfd, input_section))
823 {
824 r = bfd_reloc_outofrange;
825 goto check_reloc;
826 }
827
828 switch ((int) r_type)
829 {
830 case (int) R_MICROBLAZE_SRO32 :
831 {
832 const char *name;
833
834 /* Only relocate if the symbol is defined. */
835 if (sec)
836 {
837 name = bfd_get_section_name (abfd, sec);
838
839 if (strcmp (name, ".sdata2") == 0
840 || strcmp (name, ".sbss2") == 0)
841 {
842 if (ro_small_data_pointer == 0)
843 microblaze_elf_final_sdp (info);
844 if (ro_small_data_pointer == 0)
845 {
846 ret = FALSE;
847 r = bfd_reloc_undefined;
848 goto check_reloc;
849 }
850
851 /* At this point `relocation' contains the object's
852 address. */
853 relocation -= ro_small_data_pointer;
854 /* Now it contains the offset from _SDA2_BASE_. */
855 r = _bfd_final_link_relocate (howto, input_bfd,
856 input_section,
857 contents, offset,
858 relocation, addend);
859 }
860 else
861 {
862 (*_bfd_error_handler) (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
863 bfd_get_filename (input_bfd),
864 sym_name,
865 microblaze_elf_howto_table[(int) r_type]->name,
866 bfd_get_section_name (abfd, sec));
867 /*bfd_set_error (bfd_error_bad_value); ??? why? */
868 ret = FALSE;
869 continue;
870 }
871 }
872 }
873 break;
874
875 case (int) R_MICROBLAZE_SRW32 :
876 {
877 const char *name;
878
879 /* Only relocate if the symbol is defined. */
880 if (sec)
881 {
882 name = bfd_get_section_name (abfd, sec);
883
884 if (strcmp (name, ".sdata") == 0
885 || strcmp (name, ".sbss") == 0)
886 {
887 if (rw_small_data_pointer == 0)
888 microblaze_elf_final_sdp (info);
889 if (rw_small_data_pointer == 0)
890 {
891 ret = FALSE;
892 r = bfd_reloc_undefined;
893 goto check_reloc;
894 }
895
896 /* At this point `relocation' contains the object's
897 address. */
898 relocation -= rw_small_data_pointer;
899 /* Now it contains the offset from _SDA_BASE_. */
900 r = _bfd_final_link_relocate (howto, input_bfd,
901 input_section,
902 contents, offset,
903 relocation, addend);
904 }
905 else
906 {
907 (*_bfd_error_handler) (_("%s: The target (%s) of an %s relocation is in the wrong section (%s)"),
908 bfd_get_filename (input_bfd),
909 sym_name,
910 microblaze_elf_howto_table[(int) r_type]->name,
911 bfd_get_section_name (abfd, sec));
912 /*bfd_set_error (bfd_error_bad_value); ??? why? */
913 ret = FALSE;
914 continue;
915 }
916 }
917 }
918 break;
919
920 case (int) R_MICROBLAZE_32_SYM_OP_SYM:
921 break; /* Do nothing. */
922
923 case (int) R_MICROBLAZE_GOTPC_64:
924 relocation = htab->sgotplt->output_section->vma
925 + htab->sgotplt->output_offset;
926 relocation -= (input_section->output_section->vma
927 + input_section->output_offset
928 + offset + INST_WORD_SIZE);
929 relocation += addend;
930 bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
931 contents + offset + 2);
932 bfd_put_16 (input_bfd, relocation & 0xffff,
933 contents + offset + 2 + INST_WORD_SIZE);
934 break;
935
936 case (int) R_MICROBLAZE_PLT_64:
937 {
938 bfd_vma immediate;
939 if (htab->splt != NULL && h != NULL
940 && h->plt.offset != (bfd_vma) -1)
941 {
942 relocation = (htab->splt->output_section->vma
943 + htab->splt->output_offset
944 + h->plt.offset);
945 unresolved_reloc = FALSE;
946 immediate = relocation - (input_section->output_section->vma
947 + input_section->output_offset
948 + offset + INST_WORD_SIZE);
949 bfd_put_16 (input_bfd, (immediate >> 16) & 0xffff,
950 contents + offset + 2);
951 bfd_put_16 (input_bfd, immediate & 0xffff,
952 contents + offset + 2 + INST_WORD_SIZE);
953 }
954 else
955 {
956 relocation -= (input_section->output_section->vma
957 + input_section->output_offset
958 + offset + INST_WORD_SIZE);
959 immediate = relocation;
960 bfd_put_16 (input_bfd, (immediate >> 16) & 0xffff,
961 contents + offset + 2);
962 bfd_put_16 (input_bfd, immediate & 0xffff,
963 contents + offset + 2 + INST_WORD_SIZE);
964 }
965 break;
966 }
967
968 case (int) R_MICROBLAZE_GOT_64:
969 {
970 if (htab->sgot == NULL)
971 abort ();
972 if (h == NULL)
973 {
974 bfd_vma off;
975 if (local_got_offsets == NULL)
976 abort ();
977 off = local_got_offsets[r_symndx];
978 /* The LSB indicates whether we've already
979 created relocation. */
980 if (off & 1)
981 off &= ~1;
982 else
983 {
984 bfd_put_32 (output_bfd, relocation + addend,
985 htab->sgot->contents + off);
986
987 if (info->shared)
988 {
989 Elf_Internal_Rela outrel;
990 bfd_byte *loc;
991 if (htab->srelgot == NULL)
992 abort ();
993 outrel.r_offset = (htab->sgot->output_section->vma
994 + htab->sgot->output_offset
995 + off);
996 outrel.r_info = ELF32_R_INFO (0, R_MICROBLAZE_REL);
997 outrel.r_addend = relocation + addend;
998 loc = htab->srelgot->contents;
999 loc += htab->srelgot->reloc_count++
1000 * sizeof (Elf32_External_Rela);
1001 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1002 }
1003 local_got_offsets[r_symndx] |= 1;
1004 }
1005 relocation = htab->sgot->output_section->vma
1006 + htab->sgot->output_offset + off
1007 - htab->sgotplt->output_section->vma
1008 - htab->sgotplt->output_offset;
1009 unresolved_reloc = FALSE;
1010 }
1011 else
1012 {
1013 if (htab->sgotplt != NULL && h != NULL
1014 && h->got.offset != (bfd_vma) -1)
1015 {
1016 bfd_put_32 (output_bfd, relocation + addend,
1017 htab->sgot->contents + h->got.offset);
1018 relocation = htab->sgot->output_section->vma
1019 + htab->sgot->output_offset
1020 + h->got.offset
1021 - htab->sgotplt->output_section->vma
1022 - htab->sgotplt->output_offset;
1023 unresolved_reloc = FALSE;
1024 }
1025 else
1026 abort (); /* ??? */
1027 }
1028 bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
1029 contents + offset + 2);
1030 bfd_put_16 (input_bfd, relocation & 0xffff,
1031 contents + offset + 2 + INST_WORD_SIZE);
1032 break;
1033 }
1034
1035 case (int) R_MICROBLAZE_GOTOFF_64:
1036 {
1037 bfd_vma immediate;
1038 unsigned short lo, high;
1039 relocation += addend;
1040 relocation -= htab->sgotplt->output_section->vma
1041 + htab->sgotplt->output_offset;
1042 /* Write this value into correct location. */
1043 immediate = relocation;
1044 lo = immediate & 0x0000ffff;
1045 high = (immediate >> 16) & 0x0000ffff;
1046 bfd_put_16 (input_bfd, high, contents + offset + 2);
1047 bfd_put_16 (input_bfd, lo, contents + offset + INST_WORD_SIZE + 2);
1048 break;
1049 }
1050
1051 case (int) R_MICROBLAZE_GOTOFF_32:
1052 {
1053 relocation += addend;
1054 relocation -= htab->sgotplt->output_section->vma
1055 + htab->sgotplt->output_offset;
1056 /* Write this value into correct location. */
1057 bfd_put_32 (input_bfd, relocation, contents + offset);
1058 break;
1059 }
1060
1061 case (int) R_MICROBLAZE_64_PCREL :
1062 case (int) R_MICROBLAZE_64:
1063 case (int) R_MICROBLAZE_32:
1064 {
1065 /* r_symndx will be zero only for relocs against symbols
1066 from removed linkonce sections, or sections discarded by
1067 a linker script. */
1068 if (r_symndx == 0 || (input_section->flags & SEC_ALLOC) == 0)
1069 {
1070 relocation += addend;
1071 if (r_type == R_MICROBLAZE_32)
1072 bfd_put_32 (input_bfd, relocation, contents + offset);
1073 else
1074 {
1075 if (r_type == R_MICROBLAZE_64_PCREL)
1076 relocation -= (input_section->output_section->vma
1077 + input_section->output_offset
1078 + offset + INST_WORD_SIZE);
1079 bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
1080 contents + offset + 2);
1081 bfd_put_16 (input_bfd, relocation & 0xffff,
1082 contents + offset + 2 + INST_WORD_SIZE);
1083 }
1084 break;
1085 }
1086
1087 if ((info->shared
1088 && (h == NULL
1089 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1090 || h->root.type != bfd_link_hash_undefweak)
1091 && (!howto->pc_relative
1092 || (h != NULL
1093 && h->dynindx != -1
1094 && (!info->symbolic
1095 || !h->def_regular))))
1096 || (!info->shared
1097 && h != NULL
1098 && h->dynindx != -1
1099 && !h->non_got_ref
1100 && ((h->def_dynamic
1101 && !h->def_regular)
1102 || h->root.type == bfd_link_hash_undefweak
1103 || h->root.type == bfd_link_hash_undefined)))
1104 {
1105 Elf_Internal_Rela outrel;
1106 bfd_byte *loc;
1107 bfd_boolean skip, relocate = FALSE;
1108
1109 /* When generating a shared object, these relocations
1110 are copied into the output file to be resolved at run
1111 time. */
1112
1113 BFD_ASSERT (sreloc != NULL);
1114
1115 skip = FALSE;
1116
1117 outrel.r_offset =
1118 _bfd_elf_section_offset (output_bfd, info, input_section,
1119 rel->r_offset);
1120 if (outrel.r_offset == (bfd_vma) -1)
1121 skip = TRUE;
1122 else if (outrel.r_offset == (bfd_vma) -2)
1123 skip = TRUE, relocate = TRUE;
1124 outrel.r_offset += (input_section->output_section->vma
1125 + input_section->output_offset);
1126
1127 if (skip)
1128 memset (&outrel, 0, sizeof outrel);
1129 /* h->dynindx may be -1 if the symbol was marked to
1130 become local. */
1131 else if (h != NULL
1132 && ((! info->symbolic && h->dynindx != -1)
1133 || !h->def_regular))
1134 {
1135 BFD_ASSERT (h->dynindx != -1);
1136 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1137 outrel.r_addend = addend;
1138 }
1139 else
1140 {
1141 if (r_type == R_MICROBLAZE_32)
1142 {
1143 outrel.r_info = ELF32_R_INFO (0, R_MICROBLAZE_REL);
1144 outrel.r_addend = relocation + addend;
1145 }
1146 else
1147 {
1148 BFD_FAIL ();
1149 (*_bfd_error_handler)
1150 (_("%B: probably compiled without -fPIC?"),
1151 input_bfd);
1152 bfd_set_error (bfd_error_bad_value);
1153 return FALSE;
1154 }
1155 }
1156
1157 loc = sreloc->contents;
1158 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1159 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1160 break;
1161 }
1162 else
1163 {
1164 relocation += addend;
1165 if (r_type == R_MICROBLAZE_32)
1166 bfd_put_32 (input_bfd, relocation, contents + offset);
1167 else
1168 {
1169 if (r_type == R_MICROBLAZE_64_PCREL)
1170 relocation -= (input_section->output_section->vma
1171 + input_section->output_offset
1172 + offset + INST_WORD_SIZE);
1173 bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
1174 contents + offset + 2);
1175 bfd_put_16 (input_bfd, relocation & 0xffff,
1176 contents + offset + 2 + INST_WORD_SIZE);
1177 }
1178 break;
1179 }
1180 }
1181
1182 default :
1183 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1184 contents, offset,
1185 relocation, addend);
1186 break;
1187 }
1188 }
1189
1190 check_reloc:
1191
1192 if (r != bfd_reloc_ok)
1193 {
1194 /* FIXME: This should be generic enough to go in a utility. */
1195 const char *name;
1196
1197 if (h != NULL)
1198 name = h->root.root.string;
1199 else
1200 {
1201 name = (bfd_elf_string_from_elf_section
1202 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1203 if (name == NULL || *name == '\0')
1204 name = bfd_section_name (input_bfd, sec);
1205 }
1206
1207 if (errmsg != NULL)
1208 goto common_error;
1209
1210 switch (r)
1211 {
1212 case bfd_reloc_overflow:
1213 if (!((*info->callbacks->reloc_overflow)
1214 (info, (h ? &h->root : NULL), name, howto->name,
1215 (bfd_vma) 0, input_bfd, input_section, offset)))
1216 return FALSE;
1217 break;
1218
1219 case bfd_reloc_undefined:
1220 if (!((*info->callbacks->undefined_symbol)
1221 (info, name, input_bfd, input_section, offset, TRUE)))
1222 return FALSE;
1223 break;
1224
1225 case bfd_reloc_outofrange:
1226 errmsg = _("internal error: out of range error");
1227 goto common_error;
1228
1229 case bfd_reloc_notsupported:
1230 errmsg = _("internal error: unsupported relocation error");
1231 goto common_error;
1232
1233 case bfd_reloc_dangerous:
1234 errmsg = _("internal error: dangerous error");
1235 goto common_error;
1236
1237 default:
1238 errmsg = _("internal error: unknown error");
1239 /* Fall through. */
1240 common_error:
1241 if (!((*info->callbacks->warning)
1242 (info, errmsg, name, input_bfd, input_section, offset)))
1243 return FALSE;
1244 break;
1245 }
1246 }
1247 }
1248
1249 return ret;
1250}
1251\f
1252/* Calculate fixup value for reference. */
1253
1254static int
1255calc_fixup (bfd_vma addr, asection *sec)
1256{
1257 int i, fixup = 0;
1258
1259 if (sec == NULL || sec->relax == NULL)
1260 return 0;
1261
1262 /* Look for addr in relax table, total fixup value. */
1263 for (i = 0; i < sec->relax_count; i++)
1264 {
1265 if (addr <= sec->relax[i].addr)
1266 break;
1267 fixup += sec->relax[i].size;
1268 }
1269
1270 return fixup;
1271}
1272
1273static bfd_boolean
1274microblaze_elf_relax_section (bfd *abfd,
1275 asection *sec,
1276 struct bfd_link_info *link_info,
1277 bfd_boolean *again)
1278{
1279 Elf_Internal_Shdr *symtab_hdr;
1280 Elf_Internal_Rela *internal_relocs;
1281 Elf_Internal_Rela *free_relocs = NULL;
1282 Elf_Internal_Rela *irel, *irelend;
1283 bfd_byte *contents = NULL;
1284 bfd_byte *free_contents = NULL;
1285 int rel_count;
1286 unsigned int shndx;
91d6fa6a 1287 int i, sym_index;
7ba29e2a
NC
1288 asection *o;
1289 struct elf_link_hash_entry *sym_hash;
1290 Elf_Internal_Sym *isymbuf, *isymend;
1291 Elf_Internal_Sym *isym;
1292 int symcount;
1293 int offset;
1294 bfd_vma src, dest;
1295
1296 /* We only do this once per section. We may be able to delete some code
1297 by running multiple passes, but it is not worth it. */
1298 *again = FALSE;
1299
1300 /* Only do this for a text section. */
1301 if (link_info->relocatable
1302 || (sec->flags & SEC_RELOC) == 0
1303 || (sec->reloc_count == 0))
1304 return TRUE;
1305
1306 BFD_ASSERT ((sec->size > 0) || (sec->rawsize > 0));
1307
1308 /* If this is the first time we have been called for this section,
1309 initialize the cooked size. */
1310 if (sec->size == 0)
1311 sec->size = sec->rawsize;
1312
1313 /* Get symbols for this section. */
1314 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1315 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1316 symcount = symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
1317 if (isymbuf == NULL)
1318 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, symcount,
1319 0, NULL, NULL, NULL);
1320 BFD_ASSERT (isymbuf != NULL);
1321
91d6fa6a 1322 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, link_info->keep_memory);
7ba29e2a
NC
1323 if (internal_relocs == NULL)
1324 goto error_return;
1325 if (! link_info->keep_memory)
1326 free_relocs = internal_relocs;
1327
1328 sec->relax = (struct relax_table *) bfd_malloc ((sec->reloc_count + 1)
1329 * sizeof (struct relax_table));
1330 if (sec->relax == NULL)
1331 goto error_return;
1332 sec->relax_count = 0;
1333
1334 irelend = internal_relocs + sec->reloc_count;
1335 rel_count = 0;
1336 for (irel = internal_relocs; irel < irelend; irel++, rel_count++)
1337 {
1338 bfd_vma symval;
1339 if ((ELF32_R_TYPE (irel->r_info) != (int) R_MICROBLAZE_64_PCREL)
1340 && (ELF32_R_TYPE (irel->r_info) != (int) R_MICROBLAZE_64 ))
1341 continue; /* Can't delete this reloc. */
1342
1343 /* Get the section contents. */
1344 if (contents == NULL)
1345 {
1346 if (elf_section_data (sec)->this_hdr.contents != NULL)
1347 contents = elf_section_data (sec)->this_hdr.contents;
1348 else
1349 {
1350 contents = (bfd_byte *) bfd_malloc (sec->size);
1351 if (contents == NULL)
1352 goto error_return;
1353 free_contents = contents;
1354
1355 if (!bfd_get_section_contents (abfd, sec, contents,
1356 (file_ptr) 0, sec->size))
1357 goto error_return;
1358 elf_section_data (sec)->this_hdr.contents = contents;
1359 }
1360 }
1361
1362 /* Get the value of the symbol referred to by the reloc. */
1363 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1364 {
1365 /* A local symbol. */
7ba29e2a
NC
1366 asection *sym_sec;
1367
1368 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1369 if (isym->st_shndx == SHN_UNDEF)
1370 sym_sec = bfd_und_section_ptr;
1371 else if (isym->st_shndx == SHN_ABS)
1372 sym_sec = bfd_abs_section_ptr;
1373 else if (isym->st_shndx == SHN_COMMON)
1374 sym_sec = bfd_com_section_ptr;
1375 else
1376 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1377
1378 symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
1379 }
1380 else
1381 {
1382 unsigned long indx;
1383 struct elf_link_hash_entry *h;
1384
1385 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1386 h = elf_sym_hashes (abfd)[indx];
1387 BFD_ASSERT (h != NULL);
1388
1389 if (h->root.type != bfd_link_hash_defined
1390 && h->root.type != bfd_link_hash_defweak)
1391 /* This appears to be a reference to an undefined
1392 symbol. Just ignore it--it will be caught by the
1393 regular reloc processing. */
1394 continue;
1395
1396 symval = (h->root.u.def.value
1397 + h->root.u.def.section->output_section->vma
1398 + h->root.u.def.section->output_offset);
1399 }
1400
1401 /* If this is a PC-relative reloc, subtract the instr offset from
1402 the symbol value. */
1403 if (ELF32_R_TYPE (irel->r_info) == (int) R_MICROBLAZE_64_PCREL)
1404 {
1405 symval = symval + irel->r_addend
1406 - (irel->r_offset
1407 + sec->output_section->vma
1408 + sec->output_offset);
1409 }
1410 else
1411 symval += irel->r_addend;
1412
1413 if ((symval & 0xffff8000) == 0
1414 || (symval & 0xffff8000) == 0xffff8000)
1415 {
1416 /* We can delete this instruction. */
1417 sec->relax[sec->relax_count].addr = irel->r_offset;
1418 sec->relax[sec->relax_count].size = INST_WORD_SIZE;
1419 sec->relax_count++;
1420
1421 /* Rewrite relocation type. */
1422 switch ((enum elf_microblaze_reloc_type) ELF32_R_TYPE (irel->r_info))
1423 {
1424 case R_MICROBLAZE_64_PCREL:
1425 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1426 (int) R_MICROBLAZE_32_PCREL_LO);
1427 break;
1428 case R_MICROBLAZE_64:
1429 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1430 (int) R_MICROBLAZE_32_LO);
1431 break;
1432 default:
1433 /* Cannot happen. */
1434 BFD_ASSERT (FALSE);
1435 }
1436 }
1437 } /* Loop through all relocations. */
1438
1439 /* Loop through the relocs again, and see if anything needs to change. */
1440 if (sec->relax_count > 0)
1441 {
1442 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1443 rel_count = 0;
1444 sec->relax[sec->relax_count].addr = sec->size;
1445
1446 for (irel = internal_relocs; irel < irelend; irel++, rel_count++)
1447 {
1448 bfd_vma nraddr;
1449
1450 /* Get the new reloc address. */
1451 nraddr = irel->r_offset - calc_fixup (irel->r_offset, sec);
1452 switch ((enum elf_microblaze_reloc_type) ELF32_R_TYPE (irel->r_info))
1453 {
1454 default:
1455 break;
1456 case R_MICROBLAZE_64_PCREL:
1457 break;
1458 case R_MICROBLAZE_64:
1459 case R_MICROBLAZE_32_LO:
1460 /* If this reloc is against a symbol defined in this
1461 section, we must check the addend to see it will put the value in
1462 range to be adjusted, and hence must be changed. */
1463 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1464 {
7ba29e2a
NC
1465 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1466 /* Only handle relocs against .text. */
1467 if (isym->st_shndx == shndx
1468 && ELF32_ST_TYPE (isym->st_info) == STT_SECTION)
1469 irel->r_addend -= calc_fixup (irel->r_addend, sec);
1470 }
1471 break;
1472 case R_MICROBLAZE_NONE:
1473 {
1474 /* This was a PC-relative instruction that was
1475 completely resolved. */
1476 int sfix, efix;
1477 bfd_vma target_address;
1478 target_address = irel->r_addend + irel->r_offset;
1479 sfix = calc_fixup (irel->r_offset, sec);
1480 efix = calc_fixup (target_address, sec);
1481 irel->r_addend -= (efix - sfix);
1482 /* Should use HOWTO. */
1483 bfd_put_16 (abfd, irel->r_addend, contents + irel->r_offset + 2);
1484 }
1485 break;
1486 case R_MICROBLAZE_64_NONE:
1487 {
1488 /* This was a PC-relative 64-bit instruction that was
1489 completely resolved. */
1490 int sfix, efix;
1491 bfd_vma target_address;
1492 target_address = irel->r_addend + irel->r_offset + INST_WORD_SIZE;
1493 sfix = calc_fixup (irel->r_offset + INST_WORD_SIZE, sec);
1494 efix = calc_fixup (target_address, sec);
1495 irel->r_addend -= (efix - sfix);
1496 bfd_put_16 (abfd, irel->r_addend, contents + irel->r_offset
1497 + INST_WORD_SIZE + 2);
1498 }
1499 break;
1500 }
1501 irel->r_offset = nraddr;
1502 } /* Change all relocs in this section. */
1503
1504 /* Look through all other sections. */
1505 for (o = abfd->sections; o != NULL; o = o->next)
1506 {
91d6fa6a 1507 Elf_Internal_Rela *irelocs;
7ba29e2a
NC
1508 Elf_Internal_Rela *irelscan, *irelscanend;
1509 bfd_byte *ocontents;
1510
1511 if (o == sec
1512 || (o->flags & SEC_RELOC) == 0
1513 || o->reloc_count == 0)
1514 continue;
1515
1516 /* We always cache the relocs. Perhaps, if info->keep_memory is
1517 FALSE, we should free them, if we are permitted to. */
1518
91d6fa6a
NC
1519 irelocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, TRUE);
1520 if (irelocs == NULL)
7ba29e2a
NC
1521 goto error_return;
1522
1523 ocontents = NULL;
91d6fa6a
NC
1524 irelscanend = irelocs + o->reloc_count;
1525 for (irelscan = irelocs; irelscan < irelscanend; irelscan++)
7ba29e2a
NC
1526 {
1527 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32)
1528 {
1529 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1530
1531 /* Look at the reloc only if the value has been resolved. */
1532 if (isym->st_shndx == shndx
1533 && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
1534 {
1535 if (ocontents == NULL)
1536 {
1537 if (elf_section_data (o)->this_hdr.contents != NULL)
1538 ocontents = elf_section_data (o)->this_hdr.contents;
1539 else
1540 {
1541 /* We always cache the section contents.
1542 Perhaps, if info->keep_memory is FALSE, we
1543 should free them, if we are permitted to. */
1544 if (o->rawsize == 0)
1545 o->rawsize = o->size;
1546 ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
1547 if (ocontents == NULL)
1548 goto error_return;
1549 if (!bfd_get_section_contents (abfd, o, ocontents,
1550 (file_ptr) 0,
1551 o->rawsize))
1552 goto error_return;
1553 elf_section_data (o)->this_hdr.contents = ocontents;
1554 }
1555
1556 }
1557 irelscan->r_addend -= calc_fixup (irelscan->r_addend, sec);
1558 }
1559 else if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32_SYM_OP_SYM)
1560 {
1561 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1562
1563 /* Look at the reloc only if the value has been resolved. */
1564 if (ocontents == NULL)
1565 {
1566 if (elf_section_data (o)->this_hdr.contents != NULL)
1567 ocontents = elf_section_data (o)->this_hdr.contents;
1568 else
1569 {
1570 /* We always cache the section contents.
1571 Perhaps, if info->keep_memory is FALSE, we
1572 should free them, if we are permitted to. */
1573
1574 if (o->rawsize == 0)
1575 o->rawsize = o->size;
1576 ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
1577 if (ocontents == NULL)
1578 goto error_return;
1579 if (!bfd_get_section_contents (abfd, o, ocontents,
1580 (file_ptr) 0,
1581 o->rawsize))
1582 goto error_return;
1583 elf_section_data (o)->this_hdr.contents = ocontents;
1584 }
1585 }
1586 irelscan->r_addend -= calc_fixup (irel->r_addend
1587 + isym->st_value,
1588 sec);
1589 }
1590 }
1591 else if ((ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32_PCREL_LO)
1592 || (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32_LO))
1593 {
1594 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1595
1596 /* Look at the reloc only if the value has been resolved. */
1597 if (isym->st_shndx == shndx
1598 && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
1599 {
1600 bfd_vma immediate;
1601 bfd_vma target_address;
1602
1603 if (ocontents == NULL)
1604 {
1605 if (elf_section_data (o)->this_hdr.contents != NULL)
1606 ocontents = elf_section_data (o)->this_hdr.contents;
1607 else
1608 {
1609 /* We always cache the section contents.
1610 Perhaps, if info->keep_memory is FALSE, we
1611 should free them, if we are permitted to. */
1612 if (o->rawsize == 0)
1613 o->rawsize = o->size;
1614 ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
1615 if (ocontents == NULL)
1616 goto error_return;
1617 if (!bfd_get_section_contents (abfd, o, ocontents,
1618 (file_ptr) 0,
1619 o->rawsize))
1620 goto error_return;
1621 elf_section_data (o)->this_hdr.contents = ocontents;
1622 }
1623 }
1624
1625 immediate = (unsigned short) bfd_get_16 (abfd, ocontents +
1626 irelscan->r_offset + 2);
1627 target_address = immediate;
1628 offset = calc_fixup (target_address, sec);
1629 immediate -= offset;
1630 irelscan->r_addend -= offset;
1631 bfd_put_16 (abfd, immediate, ocontents + irelscan->r_offset + 2);
1632 }
1633 }
1634
1635 if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_64)
1636 {
1637 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1638
1639 /* Look at the reloc only if the value has been resolved. */
1640 if (isym->st_shndx == shndx
1641 && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
1642 {
1643 bfd_vma immediate;
1644
1645 if (ocontents == NULL)
1646 {
1647 if (elf_section_data (o)->this_hdr.contents != NULL)
1648 ocontents = elf_section_data (o)->this_hdr.contents;
1649 else
1650 {
1651 /* We always cache the section contents.
1652 Perhaps, if info->keep_memory is FALSE, we
1653 should free them, if we are permitted to. */
1654
1655 if (o->rawsize == 0)
1656 o->rawsize = o->size;
1657 ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
1658 if (ocontents == NULL)
1659 goto error_return;
1660 if (!bfd_get_section_contents (abfd, o, ocontents,
1661 (file_ptr) 0,
1662 o->rawsize))
1663 goto error_return;
1664 elf_section_data (o)->this_hdr.contents = ocontents;
1665 }
1666 }
1667 immediate = (unsigned short) (bfd_get_16 (abfd, ocontents
1668 + irelscan->r_offset
1669 + 2) << 16)
1670 & 0xffff0000;
1671 immediate += (unsigned short) (bfd_get_16 (abfd, ocontents
1672 + irelscan->r_offset
1673 + INST_WORD_SIZE + 2))
1674 & 0x0000ffff;
1675
1676 offset = calc_fixup (irelscan->r_addend, sec);
1677 immediate -= offset;
1678 irelscan->r_addend -= offset;
1679 }
1680 }
1681 else if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_64_PCREL)
1682 {
1683 isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1684
1685 /* Look at the reloc only if the value has been resolved. */
1686 if (isym->st_shndx == shndx
1687 && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
1688 {
1689 bfd_vma immediate;
1690 bfd_vma target_address;
1691
1692 if (ocontents == NULL)
1693 {
1694 if (elf_section_data (o)->this_hdr.contents != NULL)
1695 ocontents = elf_section_data (o)->this_hdr.contents;
1696 else
1697 {
1698 /* We always cache the section contents.
1699 Perhaps, if info->keep_memory is FALSE, we
1700 should free them, if we are permitted to. */
1701 if (o->rawsize == 0)
1702 o->rawsize = o->size;
1703 ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
1704 if (ocontents == NULL)
1705 goto error_return;
1706 if (!bfd_get_section_contents (abfd, o, ocontents,
1707 (file_ptr) 0,
1708 o->rawsize))
1709 goto error_return;
1710 elf_section_data (o)->this_hdr.contents = ocontents;
1711 }
1712 }
1713
1714 immediate = (unsigned short)
1715 (bfd_get_16 (abfd, ocontents + irelscan->r_offset + 2) << 16)
1716 & 0xffff0000;
1717 immediate += (unsigned short)
1718 (bfd_get_16 (abfd, ocontents + irelscan->r_offset
1719 + INST_WORD_SIZE + 2))
1720 & 0x0000ffff;
1721 target_address = immediate;
1722 offset = calc_fixup (target_address, sec);
1723 immediate -= offset;
1724 irelscan->r_addend -= offset;
1725 bfd_put_16 (abfd, ((immediate >> 16) & 0x0000ffff),
1726 ocontents + irelscan->r_offset + 2);
1727 bfd_put_16 (abfd, (immediate & 0x0000ffff),
1728 ocontents + irelscan->r_offset + INST_WORD_SIZE + 2);
1729 }
1730 }
1731 }
1732 }
1733
1734 /* Adjust the local symbols defined in this section. */
1735 isymend = isymbuf + symtab_hdr->sh_info;
1736 for (isym = isymbuf; isym < isymend; isym++)
1737 {
1738 if (isym->st_shndx == shndx)
1739 isym->st_value =- calc_fixup (isym->st_value, sec);
1740 }
1741
1742 /* Now adjust the global symbols defined in this section. */
1743 isym = isymbuf + symtab_hdr->sh_info;
1744 isymend = isymbuf + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
91d6fa6a 1745 for (sym_index = 0; isym < isymend; isym++, sym_index++)
7ba29e2a 1746 {
91d6fa6a 1747 sym_hash = elf_sym_hashes (abfd)[sym_index];
7ba29e2a
NC
1748 if (isym->st_shndx == shndx
1749 && (sym_hash->root.type == bfd_link_hash_defined
1750 || sym_hash->root.type == bfd_link_hash_defweak)
1751 && sym_hash->root.u.def.section == sec)
1752 {
1753 sym_hash->root.u.def.value -= calc_fixup (sym_hash->root.u.def.value,
1754 sec);
1755 }
1756 }
1757
1758 /* Physically move the code and change the cooked size. */
1759 dest = sec->relax[0].addr;
1760 for (i = 0; i < sec->relax_count; i++)
1761 {
1762 int len;
1763 src = sec->relax[i].addr + sec->relax[i].size;
1764 len = sec->relax[i+1].addr - sec->relax[i].addr - sec->relax[i].size;
1765
1766 memmove (contents + dest, contents + src, len);
1767 sec->size -= sec->relax[i].size;
1768 dest += len;
1769 }
1770
1771 elf_section_data (sec)->relocs = internal_relocs;
1772 free_relocs = NULL;
1773
1774 elf_section_data (sec)->this_hdr.contents = contents;
1775 free_contents = NULL;
1776
1777 symtab_hdr->contents = (bfd_byte *) isymbuf;
1778 }
1779
1780 if (free_relocs != NULL)
1781 {
1782 free (free_relocs);
1783 free_relocs = NULL;
1784 }
1785
1786 if (free_contents != NULL)
1787 {
1788 if (!link_info->keep_memory)
1789 free (free_contents);
1790 else
1791 /* Cache the section contents for elf_link_input_bfd. */
1792 elf_section_data (sec)->this_hdr.contents = contents;
1793 free_contents = NULL;
1794 }
1795
1796 if (sec->relax_count == 0)
1797 {
1798 free (sec->relax);
1799 sec->relax = NULL;
1800 }
1801 return TRUE;
1802
1803 error_return:
1804 if (free_relocs != NULL)
1805 free (free_relocs);
1806 if (free_contents != NULL)
1807 free (free_contents);
1808 if (sec->relax != NULL)
1809 {
1810 free (sec->relax);
1811 sec->relax = NULL;
1812 sec->relax_count = 0;
1813 }
1814 return FALSE;
1815}
1816
1817/* Return the section that should be marked against GC for a given
1818 relocation. */
1819
1820static asection *
1821microblaze_elf_gc_mark_hook (asection *sec,
fb34365b 1822 struct bfd_link_info * info,
7ba29e2a
NC
1823 Elf_Internal_Rela * rel,
1824 struct elf_link_hash_entry * h,
1825 Elf_Internal_Sym * sym)
1826{
1827 if (h != NULL)
fb34365b
AM
1828 switch (ELF32_R_TYPE (rel->r_info))
1829 {
1830 case R_MICROBLAZE_GNU_VTINHERIT:
1831 case R_MICROBLAZE_GNU_VTENTRY:
1832 return NULL;
1833 }
1834
1835 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7ba29e2a
NC
1836}
1837
1838/* Update the got entry reference counts for the section being removed. */
1839
1840static bfd_boolean
1841microblaze_elf_gc_sweep_hook (bfd * abfd ATTRIBUTE_UNUSED,
1842 struct bfd_link_info * info ATTRIBUTE_UNUSED,
1843 asection * sec ATTRIBUTE_UNUSED,
1844 const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED)
1845{
1846 return TRUE;
1847}
1848
1849/* PIC support. */
1850
1851#define PLT_ENTRY_SIZE 16
1852
1853#define PLT_ENTRY_WORD_0 0xb0000000 /* "imm 0". */
1854#define PLT_ENTRY_WORD_1 0xe9940000 /* "lwi r12,r20,0" - relocated to lwi r12,r20,func@GOT. */
1855#define PLT_ENTRY_WORD_1_NOPIC 0xe9800000 /* "lwi r12,r0,0" - non-PIC object. */
1856#define PLT_ENTRY_WORD_2 0x98186000 /* "brad r12". */
1857#define PLT_ENTRY_WORD_3 0x80000000 /* "nop". */
1858
1859/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
1860 shortcuts to them in our hash table. */
1861
1862static bfd_boolean
1863create_got_section (bfd *dynobj, struct bfd_link_info *info)
1864{
1865 struct elf32_mb_link_hash_table *htab;
1866
1867 if (! _bfd_elf_create_got_section (dynobj, info))
1868 return FALSE;
1869 htab = elf32_mb_hash_table (info);
1870 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
1871 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1872 if (!htab->sgot || !htab->sgotplt)
1873 return FALSE;
1874
1875 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
1876 if (htab->srelgot == NULL
1877 || ! bfd_set_section_flags (dynobj, htab->srelgot, SEC_ALLOC
1878 | SEC_LOAD
1879 | SEC_HAS_CONTENTS
1880 | SEC_IN_MEMORY
1881 | SEC_LINKER_CREATED
1882 | SEC_READONLY)
1883 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
1884 return FALSE;
1885 return TRUE;
1886}
1887
1888/* Look through the relocs for a section during the first phase. */
1889
1890static bfd_boolean
1891microblaze_elf_check_relocs (bfd * abfd,
1892 struct bfd_link_info * info,
1893 asection * sec,
1894 const Elf_Internal_Rela * relocs)
1895{
1896 Elf_Internal_Shdr * symtab_hdr;
1897 struct elf_link_hash_entry ** sym_hashes;
1898 struct elf_link_hash_entry ** sym_hashes_end;
1899 const Elf_Internal_Rela * rel;
1900 const Elf_Internal_Rela * rel_end;
1901 struct elf32_mb_link_hash_table *htab;
1902 bfd_vma *local_got_offsets;
1903 asection *sreloc = NULL;
1904
1905 if (info->relocatable)
1906 return TRUE;
1907
1908 htab = elf32_mb_hash_table (info);
1909 local_got_offsets = elf_local_got_offsets (abfd);
1910 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1911 sym_hashes = elf_sym_hashes (abfd);
1912 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
1913 if (!elf_bad_symtab (abfd))
1914 sym_hashes_end -= symtab_hdr->sh_info;
1915
1916 rel_end = relocs + sec->reloc_count;
1917
1918 for (rel = relocs; rel < rel_end; rel++)
1919 {
1920 unsigned int r_type;
1921 struct elf_link_hash_entry * h;
1922 unsigned long r_symndx;
1923
1924 r_symndx = ELF32_R_SYM (rel->r_info);
1925 r_type = ELF32_R_TYPE (rel->r_info);
1926
1927 if (r_symndx < symtab_hdr->sh_info)
1928 h = NULL;
1929 else
1930 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1931
1932 switch (r_type)
1933 {
1934 /* This relocation describes the C++ object vtable hierarchy.
1935 Reconstruct it for later use during GC. */
1936 case R_MICROBLAZE_GNU_VTINHERIT:
1937 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1938 return FALSE;
1939 break;
1940
1941 /* This relocation describes which C++ vtable entries are actually
1942 used. Record for later use during GC. */
1943 case R_MICROBLAZE_GNU_VTENTRY:
1944 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1945 return FALSE;
1946 break;
1947
1948 /* This relocation requires .plt entry. */
1949 case R_MICROBLAZE_PLT_64:
1950 if (h != NULL)
1951 {
1952 h->needs_plt = 1;
1953 h->plt.refcount += 1;
1954 }
1955 break;
1956
1957 /* This relocation requires .got entry. */
1958 case R_MICROBLAZE_GOT_64:
1959 if (htab->sgot == NULL)
1960 {
1961 if (htab->elf.dynobj == NULL)
1962 htab->elf.dynobj = abfd;
1963 if (!create_got_section (htab->elf.dynobj, info))
1964 return FALSE;
1965 }
1966 if (h != NULL)
1967 {
1968 h->got.refcount += 1;
1969 }
1970 else
1971 {
1972 bfd_signed_vma *local_got_refcounts;
1973
1974 /* This is a global offset table entry for a local symbol. */
1975 local_got_refcounts = elf_local_got_refcounts (abfd);
1976 if (local_got_refcounts == NULL)
1977 {
1978 bfd_size_type size;
1979
1980 size = symtab_hdr->sh_info;
1981 size *= sizeof (bfd_signed_vma);
1982 local_got_refcounts = bfd_zalloc (abfd, size);
1983 if (local_got_refcounts == NULL)
1984 return FALSE;
1985 elf_local_got_refcounts (abfd) = local_got_refcounts;
1986 }
1987 local_got_refcounts[r_symndx] += 1;
1988 }
1989 break;
1990
1991 case R_MICROBLAZE_64:
1992 case R_MICROBLAZE_64_PCREL:
1993 case R_MICROBLAZE_32:
1994 {
1995 if (h != NULL && !info->shared)
1996 {
1997 /* we may need a copy reloc. */
1998 h->non_got_ref = 1;
1999
2000 /* we may also need a .plt entry. */
2001 h->plt.refcount += 1;
2002 if (ELF32_R_TYPE (rel->r_info) != R_MICROBLAZE_64_PCREL)
2003 h->pointer_equality_needed = 1;
2004 }
2005
2006
2007 /* If we are creating a shared library, and this is a reloc
2008 against a global symbol, or a non PC relative reloc
2009 against a local symbol, then we need to copy the reloc
2010 into the shared library. However, if we are linking with
2011 -Bsymbolic, we do not need to copy a reloc against a
2012 global symbol which is defined in an object we are
2013 including in the link (i.e., DEF_REGULAR is set). At
2014 this point we have not seen all the input files, so it is
2015 possible that DEF_REGULAR is not set now but will be set
2016 later (it is never cleared). In case of a weak definition,
2017 DEF_REGULAR may be cleared later by a strong definition in
2018 a shared library. We account for that possibility below by
2019 storing information in the relocs_copied field of the hash
2020 table entry. A similar situation occurs when creating
2021 shared libraries and symbol visibility changes render the
2022 symbol local.
2023
2024 If on the other hand, we are creating an executable, we
2025 may need to keep relocations for symbols satisfied by a
2026 dynamic library if we manage to avoid copy relocs for the
2027 symbol. */
2028
2029 if ((info->shared
2030 && (sec->flags & SEC_ALLOC) != 0
2031 && (r_type != R_MICROBLAZE_64_PCREL
2032 || (h != NULL
2033 && (! info->symbolic
2034 || h->root.type == bfd_link_hash_defweak
2035 || !h->def_regular))))
2036 || (!info->shared
2037 && (sec->flags & SEC_ALLOC) != 0
2038 && h != NULL
2039 && (h->root.type == bfd_link_hash_defweak
2040 || !h->def_regular)))
2041 {
2042 struct elf32_mb_dyn_relocs *p;
2043 struct elf32_mb_dyn_relocs **head;
2044
2045 /* When creating a shared object, we must copy these
2046 relocs into the output file. We create a reloc
2047 section in dynobj and make room for the reloc. */
2048
2049 if (sreloc == NULL)
2050 {
2051 const char *name;
2052 bfd *dynobj;
2053 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
2054 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
2055
2056 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
2057 if (name == NULL)
2058 return FALSE;
2059
2060 if (strncmp (name, ".rela", 5) != 0
2061 || strcmp (bfd_get_section_name (abfd, sec),
2062 name + 5) != 0)
2063 {
2064 (*_bfd_error_handler)
2065 (_("%B: bad relocation section name `%s\'"),
2066 abfd, name);
2067 }
2068
2069 if (htab->elf.dynobj == NULL)
2070 htab->elf.dynobj = abfd;
2071 dynobj = htab->elf.dynobj;
2072
2073 sreloc = bfd_get_section_by_name (dynobj, name);
2074 if (sreloc == NULL)
2075 {
2076 flagword flags;
2077
2078 sreloc = bfd_make_section (dynobj, name);
2079 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2080 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2081 if ((sec->flags & SEC_ALLOC) != 0)
2082 flags |= SEC_ALLOC | SEC_LOAD;
2083 if (sreloc == NULL
2084 || ! bfd_set_section_flags (dynobj, sreloc, flags)
2085 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2086 return FALSE;
2087 }
2088 elf_section_data (sec)->sreloc = sreloc;
2089 }
2090
2091 /* If this is a global symbol, we count the number of
2092 relocations we need for this symbol. */
2093 if (h != NULL)
2094 head = &((struct elf32_mb_link_hash_entry *) h)->dyn_relocs;
2095 else
2096 {
2097 /* Track dynamic relocs needed for local syms too.
2098 We really need local syms available to do this
2099 easily. Oh well. */
2100
2101 asection *s;
2102 Elf_Internal_Sym *isym;
918d9742 2103 void *vpp;
7ba29e2a
NC
2104
2105 isym = bfd_sym_from_r_symndx (&htab->sym_sec,
2106 abfd, r_symndx);
2107 if (isym == NULL)
2108 return FALSE;
2109
2110 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2111 if (s == NULL)
2112 return FALSE;
2113
918d9742
TT
2114 vpp = &elf_section_data (s)->local_dynrel;
2115 head = (struct elf32_mb_dyn_relocs **) vpp;
7ba29e2a
NC
2116 }
2117
2118 p = *head;
2119 if (p == NULL || p->sec != sec)
2120 {
2121 bfd_size_type amt = sizeof *p;
2122 p = ((struct elf32_mb_dyn_relocs *)
2123 bfd_alloc (htab->elf.dynobj, amt));
2124 if (p == NULL)
2125 return FALSE;
2126 p->next = *head;
2127 *head = p;
2128 p->sec = sec;
2129 p->count = 0;
2130 p->pc_count = 0;
2131 }
2132
2133 p->count += 1;
2134 if (r_type == R_MICROBLAZE_64_PCREL)
2135 p->pc_count += 1;
2136 }
2137 }
2138 break;
2139 }
2140 }
2141
2142 return TRUE;
2143}
2144
2145static bfd_boolean
2146microblaze_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
2147{
2148 struct elf32_mb_link_hash_table *htab;
2149
2150 htab = elf32_mb_hash_table (info);
2151 if (!htab->sgot && !create_got_section (dynobj, info))
2152 return FALSE;
2153
2154 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2155 return FALSE;
2156
2157 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2158 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2159 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2160 if (!info->shared)
2161 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
2162
2163 if (!htab->splt || !htab->srelplt || !htab->sdynbss
2164 || (!info->shared && !htab->srelbss))
2165 abort ();
2166
2167 return TRUE;
2168}
2169
2170/* Copy the extra info we tack onto an elf_link_hash_entry. */
2171
2172static void
2173microblaze_elf_copy_indirect_symbol (struct bfd_link_info *info,
2174 struct elf_link_hash_entry *dir,
2175 struct elf_link_hash_entry *ind)
2176{
2177 struct elf32_mb_link_hash_entry *edir, *eind;
2178
2179 edir = (struct elf32_mb_link_hash_entry *) dir;
2180 eind = (struct elf32_mb_link_hash_entry *) ind;
2181
2182 if (eind->dyn_relocs != NULL)
2183 {
2184 if (edir->dyn_relocs != NULL)
2185 {
2186 struct elf32_mb_dyn_relocs **pp;
2187 struct elf32_mb_dyn_relocs *p;
2188
2189 if (ind->root.type == bfd_link_hash_indirect)
2190 abort ();
2191
2192 /* Add reloc counts against the weak sym to the strong sym
2193 list. Merge any entries against the same section. */
2194 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2195 {
2196 struct elf32_mb_dyn_relocs *q;
2197
2198 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2199 if (q->sec == p->sec)
2200 {
2201 q->pc_count += p->pc_count;
2202 q->count += p->count;
2203 *pp = p->next;
2204 break;
2205 }
2206 if (q == NULL)
2207 pp = &p->next;
2208 }
2209 *pp = edir->dyn_relocs;
2210 }
2211
2212 edir->dyn_relocs = eind->dyn_relocs;
2213 eind->dyn_relocs = NULL;
2214 }
2215
2216 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2217}
2218
2219static bfd_boolean
2220microblaze_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2221 struct elf_link_hash_entry *h)
2222{
2223 struct elf32_mb_link_hash_table *htab;
2224 struct elf32_mb_link_hash_entry * eh;
2225 struct elf32_mb_dyn_relocs *p;
2226 asection *sdynbss, *s;
2227 unsigned int power_of_two;
2228 bfd *dynobj;
2229
2230 htab = elf32_mb_hash_table (info);
2231
2232 /* If this is a function, put it in the procedure linkage table. We
2233 will fill in the contents of the procedure linkage table later,
2234 when we know the address of the .got section. */
2235 if (h->type == STT_FUNC
2236 || h->needs_plt)
2237 {
2238 if (h->plt.refcount <= 0
2239 || SYMBOL_CALLS_LOCAL (info, h)
2240 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2241 && h->root.type == bfd_link_hash_undefweak))
2242 {
2243 /* This case can occur if we saw a PLT reloc in an input
2244 file, but the symbol was never referred to by a dynamic
2245 object, or if all references were garbage collected. In
2246 such a case, we don't actually need to build a procedure
2247 linkage table, and we can just do a PC32 reloc instead. */
2248 h->plt.offset = (bfd_vma) -1;
2249 h->needs_plt = 0;
2250 }
2251
2252 return TRUE;
2253 }
2254 else
2255 /* It's possible that we incorrectly decided a .plt reloc was
2256 needed for an R_MICROBLAZE_64_PCREL reloc to a non-function sym in
2257 check_relocs. We can't decide accurately between function and
2258 non-function syms in check-relocs; Objects loaded later in
2259 the link may change h->type. So fix it now. */
2260 h->plt.offset = (bfd_vma) -1;
2261
2262 /* If this is a weak symbol, and there is a real definition, the
2263 processor independent code will have arranged for us to see the
2264 real definition first, and we can just use the same value. */
2265 if (h->u.weakdef != NULL)
2266 {
2267 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2268 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2269 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2270 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2271 return TRUE;
2272 }
2273
2274 /* This is a reference to a symbol defined by a dynamic object which
2275 is not a function. */
2276
2277 /* If we are creating a shared library, we must presume that the
2278 only references to the symbol are via the global offset table.
2279 For such cases we need not do anything here; the relocations will
2280 be handled correctly by relocate_section. */
2281 if (info->shared)
2282 return TRUE;
2283
2284 /* If there are no references to this symbol that do not use the
2285 GOT, we don't need to generate a copy reloc. */
2286 if (!h->non_got_ref)
2287 return TRUE;
2288
2289 /* If -z nocopyreloc was given, we won't generate them either. */
2290 if (info->nocopyreloc)
2291 {
2292 h->non_got_ref = 0;
2293 return TRUE;
2294 }
2295
2296 eh = (struct elf32_mb_link_hash_entry *) h;
2297 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2298 {
2299 s = p->sec->output_section;
2300 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2301 break;
2302 }
2303
2304 /* If we didn't find any dynamic relocs in read-only sections, then
2305 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2306 if (p == NULL)
2307 {
2308 h->non_got_ref = 0;
2309 return TRUE;
2310 }
2311
2312 /* We must allocate the symbol in our .dynbss section, which will
2313 become part of the .bss section of the executable. There will be
2314 an entry for this symbol in the .dynsym section. The dynamic
2315 object will contain position independent code, so all references
2316 from the dynamic object to this symbol will go through the global
2317 offset table. The dynamic linker will use the .dynsym entry to
2318 determine the address it must put in the global offset table, so
2319 both the dynamic object and the regular object will refer to the
2320 same memory location for the variable. */
2321
2322 /* We must generate a R_MICROBLAZE_COPY reloc to tell the dynamic linker
2323 to copy the initial value out of the dynamic object and into the
2324 runtime process image. */
2325 dynobj = elf_hash_table (info)->dynobj;
2326 BFD_ASSERT (dynobj != NULL);
2327 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2328 {
2329 htab->srelbss->size += sizeof (Elf32_External_Rela);
2330 h->needs_copy = 1;
2331 }
2332
2333 /* We need to figure out the alignment required for this symbol. I
2334 have no idea how ELF linkers handle this. */
2335 power_of_two = bfd_log2 (h->size);
2336 if (power_of_two > 3)
2337 power_of_two = 3;
2338
2339 sdynbss = htab->sdynbss;
2340 /* Apply the required alignment. */
2341 sdynbss->size = BFD_ALIGN (sdynbss->size, (bfd_size_type) (1 << power_of_two));
2342 if (power_of_two > bfd_get_section_alignment (dynobj, sdynbss))
2343 {
2344 if (! bfd_set_section_alignment (dynobj, sdynbss, power_of_two))
2345 return FALSE;
2346 }
2347
2348 /* Define the symbol as being at this point in the section. */
2349 h->root.u.def.section = sdynbss;
2350 h->root.u.def.value = sdynbss->size;
2351
2352 /* Increment the section size to make room for the symbol. */
2353 sdynbss->size += h->size;
2354 return TRUE;
2355}
2356
2357/* Allocate space in .plt, .got and associated reloc sections for
2358 dynamic relocs. */
2359
2360static bfd_boolean
2361allocate_dynrelocs (struct elf_link_hash_entry *h, void * dat)
2362{
2363 struct bfd_link_info *info;
2364 struct elf32_mb_link_hash_table *htab;
2365 struct elf32_mb_link_hash_entry *eh;
2366 struct elf32_mb_dyn_relocs *p;
2367
2368 if (h->root.type == bfd_link_hash_indirect)
2369 return TRUE;
2370
2371 if (h->root.type == bfd_link_hash_warning)
2372 /* When warning symbols are created, they **replace** the "real"
2373 entry in the hash table, thus we never get to see the real
2374 symbol in a hash traversal. So look at it now. */
2375 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2376
2377 info = (struct bfd_link_info *) dat;
2378 htab = elf32_mb_hash_table (info);
2379
2380 if (htab->elf.dynamic_sections_created
2381 && h->plt.refcount > 0)
2382 {
2383 /* Make sure this symbol is output as a dynamic symbol.
2384 Undefined weak syms won't yet be marked as dynamic. */
2385 if (h->dynindx == -1
2386 && !h->forced_local)
2387 {
2388 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2389 return FALSE;
2390 }
2391
2392 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
2393 {
2394 asection *s = htab->splt;
2395
2396 /* The first entry in .plt is reserved. */
2397 if (s->size == 0)
2398 s->size = PLT_ENTRY_SIZE;
2399
2400 h->plt.offset = s->size;
2401
2402 /* If this symbol is not defined in a regular file, and we are
2403 not generating a shared library, then set the symbol to this
2404 location in the .plt. This is required to make function
2405 pointers compare as equal between the normal executable and
2406 the shared library. */
2407 if (! info->shared
2408 && !h->def_regular)
2409 {
2410 h->root.u.def.section = s;
2411 h->root.u.def.value = h->plt.offset;
2412 }
2413
2414 /* Make room for this entry. */
2415 s->size += PLT_ENTRY_SIZE;
2416
2417 /* We also need to make an entry in the .got.plt section, which
2418 will be placed in the .got section by the linker script. */
2419 htab->sgotplt->size += 4;
2420
2421 /* We also need to make an entry in the .rel.plt section. */
2422 htab->srelplt->size += sizeof (Elf32_External_Rela);
2423 }
2424 else
2425 {
2426 h->plt.offset = (bfd_vma) -1;
2427 h->needs_plt = 0;
2428 }
2429 }
2430 else
2431 {
2432 h->plt.offset = (bfd_vma) -1;
2433 h->needs_plt = 0;
2434 }
2435
2436 if (h->got.refcount > 0)
2437 {
2438 asection *s;
2439
2440 /* Make sure this symbol is output as a dynamic symbol.
2441 Undefined weak syms won't yet be marked as dynamic. */
2442 if (h->dynindx == -1
2443 && !h->forced_local)
2444 {
2445 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2446 return FALSE;
2447 }
2448
2449 s = htab->sgot;
2450 h->got.offset = s->size;
2451 s->size += 4;
2452 htab->srelgot->size += sizeof (Elf32_External_Rela);
2453 }
2454 else
2455 h->got.offset = (bfd_vma) -1;
2456
2457 eh = (struct elf32_mb_link_hash_entry *) h;
2458 if (eh->dyn_relocs == NULL)
2459 return TRUE;
2460
2461 /* In the shared -Bsymbolic case, discard space allocated for
2462 dynamic pc-relative relocs against symbols which turn out to be
2463 defined in regular objects. For the normal shared case, discard
2464 space for pc-relative relocs that have become local due to symbol
2465 visibility changes. */
2466
2467 if (info->shared)
2468 {
2469 if (h->def_regular
2470 && (h->forced_local
2471 || info->symbolic))
2472 {
2473 struct elf32_mb_dyn_relocs **pp;
2474
2475 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2476 {
2477 p->count -= p->pc_count;
2478 p->pc_count = 0;
2479 if (p->count == 0)
2480 *pp = p->next;
2481 else
2482 pp = &p->next;
2483 }
2484 }
2485 }
2486 else
2487 {
2488 /* For the non-shared case, discard space for relocs against
2489 symbols which turn out to need copy relocs or are not
2490 dynamic. */
2491
2492 if (!h->non_got_ref
2493 && ((h->def_dynamic
2494 && !h->def_regular)
2495 || (htab->elf.dynamic_sections_created
2496 && (h->root.type == bfd_link_hash_undefweak
2497 || h->root.type == bfd_link_hash_undefined))))
2498 {
2499 /* Make sure this symbol is output as a dynamic symbol.
2500 Undefined weak syms won't yet be marked as dynamic. */
2501 if (h->dynindx == -1
2502 && !h->forced_local)
2503 {
2504 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2505 return FALSE;
2506 }
2507
2508 /* If that succeeded, we know we'll be keeping all the
2509 relocs. */
2510 if (h->dynindx != -1)
2511 goto keep;
2512 }
2513
2514 eh->dyn_relocs = NULL;
2515
2516 keep: ;
2517 }
2518
2519 /* Finally, allocate space. */
2520 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2521 {
2522 asection *sreloc = elf_section_data (p->sec)->sreloc;
2523 sreloc->size += p->count * sizeof (Elf32_External_Rela);
2524 }
2525
2526 return TRUE;
2527}
2528
2529/* Set the sizes of the dynamic sections. */
2530
2531static bfd_boolean
2532microblaze_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2533 struct bfd_link_info *info)
2534{
2535 struct elf32_mb_link_hash_table *htab;
2536 bfd *dynobj;
2537 asection *s;
2538 bfd *ibfd;
2539
2540 htab = elf32_mb_hash_table (info);
2541 dynobj = htab->elf.dynobj;
2542 BFD_ASSERT (dynobj != NULL);
2543
2544 /* Set up .got offsets for local syms, and space for local dynamic
2545 relocs. */
2546 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2547 {
2548 bfd_signed_vma *local_got;
2549 bfd_signed_vma *end_local_got;
2550 bfd_size_type locsymcount;
2551 Elf_Internal_Shdr *symtab_hdr;
2552 asection *srel;
2553
2554 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2555 continue;
2556
2557 for (s = ibfd->sections; s != NULL; s = s->next)
2558 {
2559 struct elf32_mb_dyn_relocs *p;
2560
2561 for (p = ((struct elf32_mb_dyn_relocs *)
2562 elf_section_data (s)->local_dynrel);
2563 p != NULL;
2564 p = p->next)
2565 {
2566 if (!bfd_is_abs_section (p->sec)
2567 && bfd_is_abs_section (p->sec->output_section))
2568 {
2569 /* Input section has been discarded, either because
2570 it is a copy of a linkonce section or due to
2571 linker script /DISCARD/, so we'll be discarding
2572 the relocs too. */
2573 }
2574 else if (p->count != 0)
2575 {
2576 srel = elf_section_data (p->sec)->sreloc;
2577 srel->size += p->count * sizeof (Elf32_External_Rela);
2578 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2579 info->flags |= DF_TEXTREL;
2580 }
2581 }
2582 }
2583
2584 local_got = elf_local_got_refcounts (ibfd);
2585 if (!local_got)
2586 continue;
2587
2588 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2589 locsymcount = symtab_hdr->sh_info;
2590 end_local_got = local_got + locsymcount;
2591 s = htab->sgot;
2592 srel = htab->srelgot;
2593
2594 for (; local_got < end_local_got; ++local_got)
2595 {
2596 if (*local_got > 0)
2597 {
2598 *local_got = s->size;
2599 s->size += 4;
2600 if (info->shared)
2601 srel->size += sizeof (Elf32_External_Rela);
2602 }
2603 else
2604 *local_got = (bfd_vma) -1;
2605 }
2606 }
2607
2608 /* Allocate global sym .plt and .got entries, and space for global
2609 sym dynamic relocs. */
2610 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
2611
2612 if (elf_hash_table (info)->dynamic_sections_created)
2613 {
2614 /* Make space for the trailing nop in .plt. */
2615 if (htab->splt->size > 0)
2616 htab->splt->size += 4;
2617 }
2618
2619 /* The check_relocs and adjust_dynamic_symbol entry points have
2620 determined the sizes of the various dynamic sections. Allocate
2621 memory for them. */
2622 for (s = dynobj->sections; s != NULL; s = s->next)
2623 {
2624 const char *name;
2625 bfd_boolean strip = FALSE;
2626
2627 if ((s->flags & SEC_LINKER_CREATED) == 0)
2628 continue;
2629
2630 /* It's OK to base decisions on the section name, because none
2631 of the dynobj section names depend upon the input files. */
2632 name = bfd_get_section_name (dynobj, s);
2633
2634 if (strncmp (name, ".rela", 5) == 0)
2635 {
2636 if (s->size == 0)
2637 {
2638 /* If we don't need this section, strip it from the
2639 output file. This is to handle .rela.bss and
2640 .rela.plt. We must create it in
2641 create_dynamic_sections, because it must be created
2642 before the linker maps input sections to output
2643 sections. The linker does that before
2644 adjust_dynamic_symbol is called, and it is that
2645 function which decides whether anything needs to go
2646 into these sections. */
2647 strip = TRUE;
2648 }
2649 else
2650 {
2651 /* We use the reloc_count field as a counter if we need
2652 to copy relocs into the output file. */
2653 s->reloc_count = 0;
2654 }
2655 }
2656 else if (s != htab->splt && s != htab->sgot && s != htab->sgotplt)
2657 {
2658 /* It's not one of our sections, so don't allocate space. */
2659 continue;
2660 }
2661
2662 if (strip)
2663 {
2664 s->flags |= SEC_EXCLUDE;
2665 continue;
2666 }
2667
2668 /* Allocate memory for the section contents. */
2669 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
2670 Unused entries should be reclaimed before the section's contents
2671 are written out, but at the moment this does not happen. Thus in
2672 order to prevent writing out garbage, we initialise the section's
2673 contents to zero. */
2674 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2675 if (s->contents == NULL && s->size != 0)
2676 return FALSE;
2677 }
2678
2679 if (elf_hash_table (info)->dynamic_sections_created)
2680 {
2681 /* Add some entries to the .dynamic section. We fill in the
2682 values later, in microblaze_elf_finish_dynamic_sections, but we
2683 must add the entries now so that we get the correct size for
2684 the .dynamic section. The DT_DEBUG entry is filled in by the
2685 dynamic linker and used by the debugger. */
2686#define add_dynamic_entry(TAG, VAL) \
2687 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2688
2689 if (info->executable)
2690 {
2691 if (!add_dynamic_entry (DT_DEBUG, 0))
2692 return FALSE;
2693 }
2694
2695 if (!add_dynamic_entry (DT_RELA, 0)
2696 || !add_dynamic_entry (DT_RELASZ, 0)
2697 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2698 return FALSE;
2699
2700 if (htab->splt->size != 0)
2701 {
2702 if (!add_dynamic_entry (DT_PLTGOT, 0)
2703 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2704 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2705 || !add_dynamic_entry (DT_JMPREL, 0)
2706 || !add_dynamic_entry (DT_BIND_NOW, 1))
2707 return FALSE;
2708 }
2709
2710 if (info->flags & DF_TEXTREL)
2711 {
2712 if (!add_dynamic_entry (DT_TEXTREL, 0))
2713 return FALSE;
2714 }
2715 }
2716#undef add_dynamic_entry
2717 return TRUE;
2718}
2719
2720/* Finish up dynamic symbol handling. We set the contents of various
2721 dynamic sections here. */
2722
2723static bfd_boolean
2724microblaze_elf_finish_dynamic_symbol (bfd *output_bfd,
2725 struct bfd_link_info *info,
2726 struct elf_link_hash_entry *h,
2727 Elf_Internal_Sym *sym)
2728{
2729 bfd *dynobj;
2730 struct elf32_mb_link_hash_table *htab;
2731
2732 htab = elf32_mb_hash_table (info);
2733 dynobj = htab->elf.dynobj;
2734
2735 if (h->plt.offset != (bfd_vma) -1)
2736 {
2737 asection *splt;
2738 asection *srela;
2739 asection *sgotplt;
2740 Elf_Internal_Rela rela;
2741 bfd_byte *loc;
2742 bfd_vma plt_index;
2743 bfd_vma got_offset;
2744 bfd_vma got_addr;
2745
2746 /* This symbol has an entry in the procedure linkage table. Set
2747 it up. */
2748 BFD_ASSERT (h->dynindx != -1);
2749
2750 splt = htab->splt;
2751 srela = htab->srelplt;
2752 sgotplt = htab->sgotplt;
2753 BFD_ASSERT (splt != NULL && srela != NULL && sgotplt != NULL);
2754
2755 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; /* first entry reserved. */
2756 got_offset = (plt_index + 3) * 4; /* 3 reserved ??? */
2757 got_addr = got_offset;
2758
2759 /* For non-PIC objects we need absolute address of the GOT entry. */
2760 if (!info->shared)
2761 got_addr += htab->sgotplt->output_section->vma + sgotplt->output_offset;
2762
2763 /* Fill in the entry in the procedure linkage table. */
2764 bfd_put_32 (output_bfd, PLT_ENTRY_WORD_0 + ((got_addr >> 16) & 0xffff),
2765 splt->contents + h->plt.offset);
2766 if (info->shared)
2767 bfd_put_32 (output_bfd, PLT_ENTRY_WORD_1 + (got_addr & 0xffff),
2768 splt->contents + h->plt.offset + 4);
2769 else
2770 bfd_put_32 (output_bfd, PLT_ENTRY_WORD_1_NOPIC + (got_addr & 0xffff),
2771 splt->contents + h->plt.offset + 4);
2772 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD_2,
2773 splt->contents + h->plt.offset + 8);
2774 bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD_3,
2775 splt->contents + h->plt.offset + 12);
2776
2777 /* Any additions to the .got section??? */
2778 /* bfd_put_32 (output_bfd,
2779 splt->output_section->vma + splt->output_offset + h->plt.offset + 4,
2780 sgotplt->contents + got_offset); */
2781
2782 /* Fill in the entry in the .rela.plt section. */
2783 rela.r_offset = (sgotplt->output_section->vma
2784 + sgotplt->output_offset
2785 + got_offset);
2786 rela.r_info = ELF32_R_INFO (h->dynindx, R_MICROBLAZE_JUMP_SLOT);
2787 rela.r_addend = 0;
2788 loc = srela->contents;
2789 loc += plt_index * sizeof (Elf32_External_Rela);
2790 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2791
2792 if (!h->def_regular)
2793 {
2794 /* Mark the symbol as undefined, rather than as defined in
2795 the .plt section. Zero the value. */
2796 sym->st_shndx = SHN_UNDEF;
2797 sym->st_value = 0;
2798 }
2799 }
2800
2801 if (h->got.offset != (bfd_vma) -1)
2802 {
2803 asection *sgot;
2804 asection *srela;
2805 Elf_Internal_Rela rela;
2806 bfd_byte *loc;
2807
2808 /* This symbol has an entry in the global offset table. Set it
2809 up. */
2810
2811 sgot = htab->sgot;
2812 srela = htab->srelgot;
2813 BFD_ASSERT (sgot != NULL && srela != NULL);
2814
2815 rela.r_offset = (sgot->output_section->vma
2816 + sgot->output_offset
2817 + (h->got.offset &~ (bfd_vma) 1));
2818
2819 /* If this is a -Bsymbolic link, and the symbol is defined
2820 locally, we just want to emit a RELATIVE reloc. Likewise if
2821 the symbol was forced to be local because of a version file.
2822 The entry in the global offset table will already have been
2823 initialized in the relocate_section function. */
2824 if (info->shared
2825 && (info->symbolic || h->dynindx == -1)
2826 && h->def_regular)
2827 {
2828 asection *sec = h->root.u.def.section;
2829 rela.r_info = ELF32_R_INFO (0, R_MICROBLAZE_REL);
2830 rela.r_addend = (h->root.u.def.value
2831 + sec->output_section->vma
2832 + sec->output_offset);
2833 }
2834 else
2835 {
2836 rela.r_info = ELF32_R_INFO (h->dynindx, R_MICROBLAZE_GLOB_DAT);
2837 rela.r_addend = 0;
2838 }
2839
2840 bfd_put_32 (output_bfd, (bfd_vma) 0,
2841 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
2842 loc = srela->contents;
2843 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2844 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2845 }
2846
2847 if (h->needs_copy)
2848 {
2849 asection *s;
2850 Elf_Internal_Rela rela;
2851 bfd_byte *loc;
2852
2853 /* This symbols needs a copy reloc. Set it up. */
2854
2855 BFD_ASSERT (h->dynindx != -1);
2856
2857 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2858 ".rela.bss");
2859 BFD_ASSERT (s != NULL);
2860
2861 rela.r_offset = (h->root.u.def.value
2862 + h->root.u.def.section->output_section->vma
2863 + h->root.u.def.section->output_offset);
2864 rela.r_info = ELF32_R_INFO (h->dynindx, R_MICROBLAZE_COPY);
2865 rela.r_addend = 0;
2866 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2867 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2868 }
2869
2870 /* Mark some specially defined symbols as absolute. */
2871 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2872 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2873 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2874 sym->st_shndx = SHN_ABS;
2875
2876 return TRUE;
2877}
2878
2879
2880/* Finish up the dynamic sections. */
2881
2882static bfd_boolean
2883microblaze_elf_finish_dynamic_sections (bfd *output_bfd,
2884 struct bfd_link_info *info)
2885{
2886 bfd *dynobj;
2887 asection *sdyn, *sgot;
2888 struct elf32_mb_link_hash_table *htab;
2889
2890 htab = elf32_mb_hash_table (info);
2891 dynobj = htab->elf.dynobj;
2892
2893 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2894
2895 if (htab->elf.dynamic_sections_created)
2896 {
2897 asection *splt;
2898 Elf32_External_Dyn *dyncon, *dynconend;
2899
2900 splt = bfd_get_section_by_name (dynobj, ".plt");
2901 BFD_ASSERT (splt != NULL && sdyn != NULL);
2902
2903 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2904 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2905 for (; dyncon < dynconend; dyncon++)
2906 {
2907 Elf_Internal_Dyn dyn;
2908 const char *name;
2909 bfd_boolean size;
2910
2911 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2912
2913 switch (dyn.d_tag)
2914 {
2915 case DT_PLTGOT: name = ".got.plt"; size = FALSE; break;
2916 case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
2917 case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
2918 case DT_RELA: name = ".rela.dyn"; size = FALSE; break;
2919 case DT_RELASZ: name = ".rela.dyn"; size = TRUE; break;
2920 default: name = NULL; size = FALSE; break;
2921 }
2922
2923 if (name != NULL)
2924 {
2925 asection *s;
2926
2927 s = bfd_get_section_by_name (output_bfd, name);
2928 if (s == NULL)
2929 dyn.d_un.d_val = 0;
2930 else
2931 {
2932 if (! size)
2933 dyn.d_un.d_ptr = s->vma;
2934 else
2935 dyn.d_un.d_val = s->size;
2936 }
2937 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2938 }
2939 }
2940
2941 /* Clear the first entry in the procedure linkage table,
2942 and put a nop in the last four bytes. */
2943 if (splt->size > 0)
2944 {
2945 memset (splt->contents, 0, PLT_ENTRY_SIZE);
2946 bfd_put_32 (output_bfd, (bfd_vma) 0x80000000 /* nop. */,
2947 splt->contents + splt->size - 4);
2948 }
2949
2950 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
2951 }
2952
2953 /* Set the first entry in the global offset table to the address of
2954 the dynamic section. */
2955 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2956 if (sgot && sgot->size > 0)
2957 {
2958 if (sdyn == NULL)
2959 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2960 else
2961 bfd_put_32 (output_bfd,
2962 sdyn->output_section->vma + sdyn->output_offset,
2963 sgot->contents);
2964 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2965 }
2966
2967 if (htab->sgot && htab->sgot->size > 0)
2968 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
2969
2970 return TRUE;
2971}
2972
2973/* Hook called by the linker routine which adds symbols from an object
2974 file. We use it to put .comm items in .sbss, and not .bss. */
2975
2976static bfd_boolean
2977microblaze_elf_add_symbol_hook (bfd *abfd,
2978 struct bfd_link_info *info,
2979 Elf_Internal_Sym *sym,
2980 const char **namep ATTRIBUTE_UNUSED,
2981 flagword *flagsp ATTRIBUTE_UNUSED,
2982 asection **secp,
2983 bfd_vma *valp)
2984{
2985 if (sym->st_shndx == SHN_COMMON
2986 && !info->relocatable
2987 && sym->st_size <= elf_gp_size (abfd))
2988 {
2989 /* Common symbols less than or equal to -G nn bytes are automatically
2990 put into .sbss. */
2991 *secp = bfd_make_section_anyway (abfd, ".sbss");
2992 if (*secp == NULL
2993 || ! bfd_set_section_flags (abfd, *secp, SEC_IS_COMMON))
2994 return FALSE;
2995
2996 *valp = sym->st_size;
2997 }
2998
2999 return TRUE;
3000}
3001
3002
3003#define TARGET_BIG_SYM bfd_elf32_microblaze_vec
3004#define TARGET_BIG_NAME "elf32-microblaze"
3005
3006#define ELF_ARCH bfd_arch_microblaze
3007#define ELF_MACHINE_CODE EM_MICROBLAZE
3008#define ELF_MACHINE_ALT1 EM_MICROBLAZE_OLD
3009#define ELF_MAXPAGESIZE 0x4 /* 4k, if we ever have 'em. */
3010#define elf_info_to_howto microblaze_elf_info_to_howto
3011#define elf_info_to_howto_rel NULL
3012
3013#define bfd_elf32_bfd_reloc_type_lookup microblaze_elf_reloc_type_lookup
3014#define bfd_elf32_bfd_is_local_label_name microblaze_elf_is_local_label_name
3015#define elf_backend_relocate_section microblaze_elf_relocate_section
3016#define bfd_elf32_bfd_relax_section microblaze_elf_relax_section
3017#define bfd_elf32_bfd_reloc_name_lookup microblaze_elf_reloc_name_lookup
3018
3019#define elf_backend_gc_mark_hook microblaze_elf_gc_mark_hook
3020#define elf_backend_gc_sweep_hook microblaze_elf_gc_sweep_hook
3021#define elf_backend_check_relocs microblaze_elf_check_relocs
3022#define elf_backend_copy_indirect_symbol microblaze_elf_copy_indirect_symbol
3023#define bfd_elf32_bfd_link_hash_table_create microblaze_elf_link_hash_table_create
3024#define elf_backend_can_gc_sections 1
3025#define elf_backend_can_refcount 1
3026#define elf_backend_want_got_plt 1
3027#define elf_backend_plt_readonly 1
3028#define elf_backend_got_header_size 12
3029#define elf_backend_rela_normal 1
3030
3031#define elf_backend_adjust_dynamic_symbol microblaze_elf_adjust_dynamic_symbol
3032#define elf_backend_create_dynamic_sections microblaze_elf_create_dynamic_sections
3033#define elf_backend_finish_dynamic_sections microblaze_elf_finish_dynamic_sections
3034#define elf_backend_finish_dynamic_symbol microblaze_elf_finish_dynamic_symbol
3035#define elf_backend_size_dynamic_sections microblaze_elf_size_dynamic_sections
3036#define elf_backend_add_symbol_hook microblaze_elf_add_symbol_hook
3037
3038#include "elf32-target.h"