Remove sh5 and sh64 support
[external/binutils.git] / bfd / elf32-microblaze.c
1 /* Xilinx MicroBlaze-specific support for 32-bit ELF
2
3    Copyright (C) 2009-2018 Free Software Foundation, Inc.
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
23 int dbg = 0;
24
25 #include "sysdep.h"
26 #include "bfd.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
37 static int ro_small_data_pointer = 0;
38 static int rw_small_data_pointer = 0;
39
40 static reloc_howto_type * microblaze_elf_howto_table [(int) R_MICROBLAZE_max];
41
42 static reloc_howto_type microblaze_elf_howto_raw[] =
43 {
44    /* This reloc does nothing.  */
45    HOWTO (R_MICROBLAZE_NONE,    /* Type.  */
46           0,                    /* Rightshift.  */
47           3,                    /* Size (0 = byte, 1 = short, 2 = long).  */
48           0,                    /* Bitsize.  */
49           FALSE,                /* PC_relative.  */
50           0,                    /* Bitpos.  */
51           complain_overflow_dont,  /* 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           3,                    /* Size (0 = byte, 1 = short, 2 = long).  */
183           0,                    /* Bitsize.  */
184           TRUE,                 /* PC_relative.  */
185           0,                    /* Bitpos.  */
186           complain_overflow_dont, /* 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    /* Marker relocs for TLS.  */
375    HOWTO (R_MICROBLAZE_TLS,
376          0,                     /* rightshift */
377          2,                     /* size (0 = byte, 1 = short, 2 = long) */
378          32,                    /* bitsize */
379          FALSE,                 /* pc_relative */
380          0,                     /* bitpos */
381          complain_overflow_dont, /* complain_on_overflow */
382          bfd_elf_generic_reloc, /* special_function */
383          "R_MICROBLAZE_TLS",            /* name */
384          FALSE,                 /* partial_inplace */
385          0,                     /* src_mask */
386          0x0000ffff,                    /* dst_mask */
387          FALSE),                /* pcrel_offset */
388
389    HOWTO (R_MICROBLAZE_TLSGD,
390          0,                     /* rightshift */
391          2,                     /* size (0 = byte, 1 = short, 2 = long) */
392          32,                    /* bitsize */
393          FALSE,                 /* pc_relative */
394          0,                     /* bitpos */
395          complain_overflow_dont, /* complain_on_overflow */
396          bfd_elf_generic_reloc, /* special_function */
397          "R_MICROBLAZE_TLSGD",          /* name */
398          FALSE,                 /* partial_inplace */
399          0,                     /* src_mask */
400          0x0000ffff,                    /* dst_mask */
401          FALSE),                /* pcrel_offset */
402
403    HOWTO (R_MICROBLAZE_TLSLD,
404          0,                     /* rightshift */
405          2,                     /* size (0 = byte, 1 = short, 2 = long) */
406          32,                    /* bitsize */
407          FALSE,                 /* pc_relative */
408          0,                     /* bitpos */
409          complain_overflow_dont, /* complain_on_overflow */
410          bfd_elf_generic_reloc, /* special_function */
411          "R_MICROBLAZE_TLSLD",          /* name */
412          FALSE,                 /* partial_inplace */
413          0,                     /* src_mask */
414          0x0000ffff,            /* dst_mask */
415          FALSE),                /* pcrel_offset */
416
417    /* Computes the load module index of the load module that contains the
418       definition of its TLS sym.  */
419    HOWTO (R_MICROBLAZE_TLSDTPMOD32,
420          0,                     /* rightshift */
421          2,                     /* size (0 = byte, 1 = short, 2 = long) */
422          32,                    /* bitsize */
423          FALSE,                 /* pc_relative */
424          0,                     /* bitpos */
425          complain_overflow_dont, /* complain_on_overflow */
426          bfd_elf_generic_reloc, /* special_function */
427          "R_MICROBLAZE_TLSDTPMOD32",    /* name */
428          FALSE,                 /* partial_inplace */
429          0,                     /* src_mask */
430          0x0000ffff,            /* dst_mask */
431          FALSE),                /* pcrel_offset */
432
433    /* Computes a dtv-relative displacement, the difference between the value
434       of sym+add and the base address of the thread-local storage block that
435       contains the definition of sym, minus 0x8000.  Used for initializing GOT */
436    HOWTO (R_MICROBLAZE_TLSDTPREL32,
437          0,                     /* rightshift */
438          2,                     /* size (0 = byte, 1 = short, 2 = long) */
439          32,                    /* bitsize */
440          FALSE,                 /* pc_relative */
441          0,                     /* bitpos */
442          complain_overflow_dont, /* complain_on_overflow */
443          bfd_elf_generic_reloc, /* special_function */
444          "R_MICROBLAZE_TLSDTPREL32",    /* name */
445          FALSE,                 /* partial_inplace */
446          0,                     /* src_mask */
447          0x0000ffff,            /* dst_mask */
448          FALSE),                /* pcrel_offset */
449
450    /* Computes a dtv-relative displacement, the difference between the value
451       of sym+add and the base address of the thread-local storage block that
452       contains the definition of sym, minus 0x8000.  */
453    HOWTO (R_MICROBLAZE_TLSDTPREL64,
454          0,                     /* rightshift */
455          2,                     /* size (0 = byte, 1 = short, 2 = long) */
456          32,                    /* bitsize */
457          FALSE,                 /* pc_relative */
458          0,                     /* bitpos */
459          complain_overflow_dont, /* complain_on_overflow */
460          bfd_elf_generic_reloc, /* special_function */
461          "R_MICROBLAZE_TLSDTPREL64",    /* name */
462          FALSE,                 /* partial_inplace */
463          0,                     /* src_mask */
464          0x0000ffff,            /* dst_mask */
465          FALSE),                /* pcrel_offset */
466
467    /* Computes a tp-relative displacement, the difference between the value of
468       sym+add and the value of the thread pointer (r13).  */
469    HOWTO (R_MICROBLAZE_TLSGOTTPREL32,
470          0,                     /* rightshift */
471          2,                     /* size (0 = byte, 1 = short, 2 = long) */
472          32,                    /* bitsize */
473          FALSE,                 /* pc_relative */
474          0,                     /* bitpos */
475          complain_overflow_dont, /* complain_on_overflow */
476          bfd_elf_generic_reloc, /* special_function */
477          "R_MICROBLAZE_TLSGOTTPREL32",  /* name */
478          FALSE,                 /* partial_inplace */
479          0,                     /* src_mask */
480          0x0000ffff,            /* dst_mask */
481          FALSE),                /* pcrel_offset */
482
483    /* Computes a tp-relative displacement, the difference between the value of
484       sym+add and the value of the thread pointer (r13).  */
485    HOWTO (R_MICROBLAZE_TLSTPREL32,
486          0,                     /* rightshift */
487          2,                     /* size (0 = byte, 1 = short, 2 = long) */
488          32,                    /* bitsize */
489          FALSE,                 /* pc_relative */
490          0,                     /* bitpos */
491          complain_overflow_dont, /* complain_on_overflow */
492          bfd_elf_generic_reloc, /* special_function */
493          "R_MICROBLAZE_TLSTPREL32",     /* name */
494          FALSE,                 /* partial_inplace */
495          0,                     /* src_mask */
496          0x0000ffff,            /* dst_mask */
497          FALSE),                /* pcrel_offset */
498
499 };
500
501 #ifndef NUM_ELEM
502 #define NUM_ELEM(a) (sizeof (a) / sizeof (a)[0])
503 #endif
504 \f
505 /* Initialize the microblaze_elf_howto_table, so that linear accesses can be done.  */
506
507 static void
508 microblaze_elf_howto_init (void)
509 {
510   unsigned int i;
511
512   for (i = NUM_ELEM (microblaze_elf_howto_raw); i--;)
513     {
514       unsigned int type;
515
516       type = microblaze_elf_howto_raw[i].type;
517
518       BFD_ASSERT (type < NUM_ELEM (microblaze_elf_howto_table));
519
520       microblaze_elf_howto_table [type] = & microblaze_elf_howto_raw [i];
521     }
522 }
523 \f
524 static reloc_howto_type *
525 microblaze_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
526                                   bfd_reloc_code_real_type code)
527 {
528   enum elf_microblaze_reloc_type microblaze_reloc = R_MICROBLAZE_NONE;
529
530   switch (code)
531     {
532     case BFD_RELOC_NONE:
533       microblaze_reloc = R_MICROBLAZE_NONE;
534       break;
535     case BFD_RELOC_MICROBLAZE_64_NONE:
536       microblaze_reloc = R_MICROBLAZE_64_NONE;
537       break;
538     case BFD_RELOC_32:
539       microblaze_reloc = R_MICROBLAZE_32;
540       break;
541       /* RVA is treated the same as 32 */
542     case BFD_RELOC_RVA:
543       microblaze_reloc = R_MICROBLAZE_32;
544       break;
545     case BFD_RELOC_32_PCREL:
546       microblaze_reloc = R_MICROBLAZE_32_PCREL;
547       break;
548     case BFD_RELOC_64_PCREL:
549       microblaze_reloc = R_MICROBLAZE_64_PCREL;
550       break;
551     case BFD_RELOC_MICROBLAZE_32_LO_PCREL:
552       microblaze_reloc = R_MICROBLAZE_32_PCREL_LO;
553       break;
554     case BFD_RELOC_64:
555       microblaze_reloc = R_MICROBLAZE_64;
556       break;
557     case BFD_RELOC_MICROBLAZE_32_LO:
558       microblaze_reloc = R_MICROBLAZE_32_LO;
559       break;
560     case BFD_RELOC_MICROBLAZE_32_ROSDA:
561       microblaze_reloc = R_MICROBLAZE_SRO32;
562       break;
563     case BFD_RELOC_MICROBLAZE_32_RWSDA:
564       microblaze_reloc = R_MICROBLAZE_SRW32;
565       break;
566     case BFD_RELOC_MICROBLAZE_32_SYM_OP_SYM:
567       microblaze_reloc = R_MICROBLAZE_32_SYM_OP_SYM;
568       break;
569     case BFD_RELOC_VTABLE_INHERIT:
570       microblaze_reloc = R_MICROBLAZE_GNU_VTINHERIT;
571       break;
572     case BFD_RELOC_VTABLE_ENTRY:
573       microblaze_reloc = R_MICROBLAZE_GNU_VTENTRY;
574       break;
575     case BFD_RELOC_MICROBLAZE_64_GOTPC:
576       microblaze_reloc = R_MICROBLAZE_GOTPC_64;
577       break;
578     case BFD_RELOC_MICROBLAZE_64_GOT:
579       microblaze_reloc = R_MICROBLAZE_GOT_64;
580       break;
581     case BFD_RELOC_MICROBLAZE_64_PLT:
582       microblaze_reloc = R_MICROBLAZE_PLT_64;
583       break;
584     case BFD_RELOC_MICROBLAZE_64_GOTOFF:
585       microblaze_reloc = R_MICROBLAZE_GOTOFF_64;
586       break;
587     case BFD_RELOC_MICROBLAZE_32_GOTOFF:
588       microblaze_reloc = R_MICROBLAZE_GOTOFF_32;
589       break;
590     case BFD_RELOC_MICROBLAZE_64_TLSGD:
591       microblaze_reloc = R_MICROBLAZE_TLSGD;
592       break;
593     case BFD_RELOC_MICROBLAZE_64_TLSLD:
594       microblaze_reloc = R_MICROBLAZE_TLSLD;
595       break;
596     case BFD_RELOC_MICROBLAZE_32_TLSDTPREL:
597       microblaze_reloc = R_MICROBLAZE_TLSDTPREL32;
598       break;
599     case BFD_RELOC_MICROBLAZE_64_TLSDTPREL:
600       microblaze_reloc = R_MICROBLAZE_TLSDTPREL64;
601       break;
602     case BFD_RELOC_MICROBLAZE_32_TLSDTPMOD:
603       microblaze_reloc = R_MICROBLAZE_TLSDTPMOD32;
604       break;
605     case BFD_RELOC_MICROBLAZE_64_TLSGOTTPREL:
606       microblaze_reloc = R_MICROBLAZE_TLSGOTTPREL32;
607       break;
608     case BFD_RELOC_MICROBLAZE_64_TLSTPREL:
609       microblaze_reloc = R_MICROBLAZE_TLSTPREL32;
610       break;
611     case BFD_RELOC_MICROBLAZE_COPY:
612       microblaze_reloc = R_MICROBLAZE_COPY;
613       break;
614     default:
615       return (reloc_howto_type *) NULL;
616     }
617
618   if (!microblaze_elf_howto_table [R_MICROBLAZE_32])
619     /* Initialize howto table if needed.  */
620     microblaze_elf_howto_init ();
621
622   return microblaze_elf_howto_table [(int) microblaze_reloc];
623 };
624
625 static reloc_howto_type *
626 microblaze_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
627                                   const char *r_name)
628 {
629   unsigned int i;
630
631   for (i = 0; i < NUM_ELEM (microblaze_elf_howto_raw); i++)
632     if (microblaze_elf_howto_raw[i].name != NULL
633         && strcasecmp (microblaze_elf_howto_raw[i].name, r_name) == 0)
634       return &microblaze_elf_howto_raw[i];
635
636   return NULL;
637 }
638
639 /* Set the howto pointer for a RCE ELF reloc.  */
640
641 static bfd_boolean
642 microblaze_elf_info_to_howto (bfd * abfd,
643                               arelent * cache_ptr,
644                               Elf_Internal_Rela * dst)
645 {
646   unsigned int r_type;
647
648   if (!microblaze_elf_howto_table [R_MICROBLAZE_32])
649     /* Initialize howto table if needed.  */
650     microblaze_elf_howto_init ();
651
652   r_type = ELF32_R_TYPE (dst->r_info);
653   if (r_type >= R_MICROBLAZE_max)
654     {
655       /* xgettext:c-format */
656       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
657                           abfd, r_type);
658       bfd_set_error (bfd_error_bad_value);
659       return FALSE;
660     }
661
662   cache_ptr->howto = microblaze_elf_howto_table [r_type];
663   return TRUE;
664 }
665
666 /* Microblaze ELF local labels start with 'L.' or '$L', not '.L'.  */
667
668 static bfd_boolean
669 microblaze_elf_is_local_label_name (bfd *abfd, const char *name)
670 {
671   if (name[0] == 'L' && name[1] == '.')
672     return TRUE;
673
674   if (name[0] == '$' && name[1] == 'L')
675     return TRUE;
676
677   /* With gcc, the labels go back to starting with '.', so we accept
678      the generic ELF local label syntax as well.  */
679   return _bfd_elf_is_local_label_name (abfd, name);
680 }
681
682 /* ELF linker hash entry.  */
683
684 struct elf32_mb_link_hash_entry
685 {
686   struct elf_link_hash_entry elf;
687
688   /* Track dynamic relocs copied for this symbol.  */
689   struct elf_dyn_relocs *dyn_relocs;
690
691   /* TLS Reference Types for the symbol; Updated by check_relocs */
692 #define TLS_GD     1  /* GD reloc. */
693 #define TLS_LD     2  /* LD reloc. */
694 #define TLS_TPREL  4  /* TPREL reloc, => IE. */
695 #define TLS_DTPREL 8  /* DTPREL reloc, => LD. */
696 #define TLS_TLS    16 /* Any TLS reloc.  */
697   unsigned char tls_mask;
698
699 };
700
701 #define IS_TLS_GD(x)     (x == (TLS_TLS | TLS_GD))
702 #define IS_TLS_LD(x)     (x == (TLS_TLS | TLS_LD))
703 #define IS_TLS_DTPREL(x) (x == (TLS_TLS | TLS_DTPREL))
704 #define IS_TLS_NONE(x)   (x == 0)
705
706 #define elf32_mb_hash_entry(ent) ((struct elf32_mb_link_hash_entry *)(ent))
707
708 /* ELF linker hash table.  */
709
710 struct elf32_mb_link_hash_table
711 {
712   struct elf_link_hash_table elf;
713
714   /* Small local sym to section mapping cache.  */
715   struct sym_cache sym_sec;
716
717   /* TLS Local Dynamic GOT Entry */
718   union {
719     bfd_signed_vma refcount;
720     bfd_vma offset;
721   } tlsld_got;
722 };
723
724 /* Nonzero if this section has TLS related relocations.  */
725 #define has_tls_reloc sec_flg0
726
727 /* Get the ELF linker hash table from a link_info structure.  */
728
729 #define elf32_mb_hash_table(p)                          \
730   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
731   == MICROBLAZE_ELF_DATA ? ((struct elf32_mb_link_hash_table *) ((p)->hash)) : NULL)
732
733 /* Create an entry in a microblaze ELF linker hash table.  */
734
735 static struct bfd_hash_entry *
736 link_hash_newfunc (struct bfd_hash_entry *entry,
737                    struct bfd_hash_table *table,
738                    const char *string)
739 {
740   /* Allocate the structure if it has not already been allocated by a
741      subclass.  */
742   if (entry == NULL)
743     {
744       entry = bfd_hash_allocate (table,
745                                  sizeof (struct elf32_mb_link_hash_entry));
746       if (entry == NULL)
747         return entry;
748     }
749
750   /* Call the allocation method of the superclass.  */
751   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
752   if (entry != NULL)
753     {
754       struct elf32_mb_link_hash_entry *eh;
755
756       eh = (struct elf32_mb_link_hash_entry *) entry;
757       eh->dyn_relocs = NULL;
758       eh->tls_mask = 0;
759     }
760
761   return entry;
762 }
763
764 /* Create a mb ELF linker hash table.  */
765
766 static struct bfd_link_hash_table *
767 microblaze_elf_link_hash_table_create (bfd *abfd)
768 {
769   struct elf32_mb_link_hash_table *ret;
770   bfd_size_type amt = sizeof (struct elf32_mb_link_hash_table);
771
772   ret = (struct elf32_mb_link_hash_table *) bfd_zmalloc (amt);
773   if (ret == NULL)
774     return NULL;
775
776   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
777                                       sizeof (struct elf32_mb_link_hash_entry),
778                                       MICROBLAZE_ELF_DATA))
779     {
780       free (ret);
781       return NULL;
782     }
783
784   return &ret->elf.root;
785 }
786 \f
787 /* Set the values of the small data pointers.  */
788
789 static void
790 microblaze_elf_final_sdp (struct bfd_link_info *info)
791 {
792   struct bfd_link_hash_entry *h;
793
794   h = bfd_link_hash_lookup (info->hash, RO_SDA_ANCHOR_NAME, FALSE, FALSE, TRUE);
795   if (h != (struct bfd_link_hash_entry *) NULL
796       && h->type == bfd_link_hash_defined)
797     ro_small_data_pointer = (h->u.def.value
798                              + h->u.def.section->output_section->vma
799                              + h->u.def.section->output_offset);
800
801   h = bfd_link_hash_lookup (info->hash, RW_SDA_ANCHOR_NAME, FALSE, FALSE, TRUE);
802   if (h != (struct bfd_link_hash_entry *) NULL
803       && h->type == bfd_link_hash_defined)
804     rw_small_data_pointer = (h->u.def.value
805                              + h->u.def.section->output_section->vma
806                              + h->u.def.section->output_offset);
807 }
808
809 static bfd_vma
810 dtprel_base (struct bfd_link_info *info)
811 {
812   /* If tls_sec is NULL, we should have signalled an error already.  */
813   if (elf_hash_table (info)->tls_sec == NULL)
814     return 0;
815   return elf_hash_table (info)->tls_sec->vma;
816 }
817
818 /* The size of the thread control block.  */
819 #define TCB_SIZE        8
820
821 /* Output a simple dynamic relocation into SRELOC.  */
822
823 static void
824 microblaze_elf_output_dynamic_relocation (bfd *output_bfd,
825                                           asection *sreloc,
826                                           unsigned long reloc_index,
827                                           unsigned long indx,
828                                           int r_type,
829                                           bfd_vma offset,
830                                           bfd_vma addend)
831 {
832
833   Elf_Internal_Rela rel;
834
835   rel.r_info = ELF32_R_INFO (indx, r_type);
836   rel.r_offset = offset;
837   rel.r_addend = addend;
838
839   bfd_elf32_swap_reloca_out (output_bfd, &rel,
840               (sreloc->contents + reloc_index * sizeof (Elf32_External_Rela)));
841 }
842
843 /* This code is taken from elf32-m32r.c
844    There is some attempt to make this function usable for many architectures,
845    both USE_REL and USE_RELA ['twould be nice if such a critter existed],
846    if only to serve as a learning tool.
847
848    The RELOCATE_SECTION function is called by the new ELF backend linker
849    to handle the relocations for a section.
850
851    The relocs are always passed as Rela structures; if the section
852    actually uses Rel structures, the r_addend field will always be
853    zero.
854
855    This function is responsible for adjust the section contents as
856    necessary, and (if using Rela relocs and generating a
857    relocatable output file) adjusting the reloc addend as
858    necessary.
859
860    This function does not have to worry about setting the reloc
861    address or the reloc symbol index.
862
863    LOCAL_SYMS is a pointer to the swapped in local symbols.
864
865    LOCAL_SECTIONS is an array giving the section in the input file
866    corresponding to the st_shndx field of each local symbol.
867
868    The global hash table entry for the global symbols can be found
869    via elf_sym_hashes (input_bfd).
870
871    When generating relocatable output, this function must handle
872    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
873    going to be the section symbol corresponding to the output
874    section, which means that the addend must be adjusted
875    accordingly.  */
876
877 static bfd_boolean
878 microblaze_elf_relocate_section (bfd *output_bfd,
879                                  struct bfd_link_info *info,
880                                  bfd *input_bfd,
881                                  asection *input_section,
882                                  bfd_byte *contents,
883                                  Elf_Internal_Rela *relocs,
884                                  Elf_Internal_Sym *local_syms,
885                                  asection **local_sections)
886 {
887   struct elf32_mb_link_hash_table *htab;
888   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
889   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
890   Elf_Internal_Rela *rel, *relend;
891   int endian = (bfd_little_endian (output_bfd)) ? 0 : 2;
892   /* Assume success.  */
893   bfd_boolean ret = TRUE;
894   asection *sreloc;
895   bfd_vma *local_got_offsets;
896   unsigned int tls_type;
897
898   if (!microblaze_elf_howto_table[R_MICROBLAZE_max-1])
899     microblaze_elf_howto_init ();
900
901   htab = elf32_mb_hash_table (info);
902   if (htab == NULL)
903     return FALSE;
904
905   local_got_offsets = elf_local_got_offsets (input_bfd);
906
907   sreloc = elf_section_data (input_section)->sreloc;
908
909   rel = relocs;
910   relend = relocs + input_section->reloc_count;
911   for (; rel < relend; rel++)
912     {
913       int r_type;
914       reloc_howto_type *howto;
915       unsigned long r_symndx;
916       bfd_vma addend = rel->r_addend;
917       bfd_vma offset = rel->r_offset;
918       struct elf_link_hash_entry *h;
919       Elf_Internal_Sym *sym;
920       asection *sec;
921       const char *sym_name;
922       bfd_reloc_status_type r = bfd_reloc_ok;
923       const char *errmsg = NULL;
924       bfd_boolean unresolved_reloc = FALSE;
925
926       h = NULL;
927       r_type = ELF32_R_TYPE (rel->r_info);
928       tls_type = 0;
929
930       if (r_type < 0 || r_type >= (int) R_MICROBLAZE_max)
931         {
932           /* xgettext:c-format */
933           _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
934                               input_bfd, (int) r_type);
935           bfd_set_error (bfd_error_bad_value);
936           ret = FALSE;
937           continue;
938         }
939
940       howto = microblaze_elf_howto_table[r_type];
941       r_symndx = ELF32_R_SYM (rel->r_info);
942
943       if (bfd_link_relocatable (info))
944         {
945           /* This is a relocatable link.  We don't have to change
946              anything, unless the reloc is against a section symbol,
947              in which case we have to adjust according to where the
948              section symbol winds up in the output section.  */
949           sec = NULL;
950           if (r_symndx >= symtab_hdr->sh_info)
951             /* External symbol.  */
952             continue;
953
954           /* Local symbol.  */
955           sym = local_syms + r_symndx;
956           sym_name = "<local symbol>";
957           /* STT_SECTION: symbol is associated with a section.  */
958           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
959             /* Symbol isn't associated with a section.  Nothing to do.  */
960             continue;
961
962           sec = local_sections[r_symndx];
963           addend += sec->output_offset + sym->st_value;
964 #ifndef USE_REL
965           /* This can't be done for USE_REL because it doesn't mean anything
966              and elf_link_input_bfd asserts this stays zero.  */
967           /* rel->r_addend = addend; */
968 #endif
969
970 #ifndef USE_REL
971           /* Addends are stored with relocs.  We're done.  */
972           continue;
973 #else /* USE_REL */
974           /* If partial_inplace, we need to store any additional addend
975              back in the section.  */
976           if (!howto->partial_inplace)
977             continue;
978           /* ??? Here is a nice place to call a special_function like handler.  */
979           r = _bfd_relocate_contents (howto, input_bfd, addend,
980                                       contents + offset);
981 #endif /* USE_REL */
982         }
983       else
984         {
985           bfd_vma relocation;
986           bfd_boolean resolved_to_zero;
987
988           /* This is a final link.  */
989           sym = NULL;
990           sec = NULL;
991           unresolved_reloc = FALSE;
992
993           if (r_symndx < symtab_hdr->sh_info)
994             {
995               /* Local symbol.  */
996               sym = local_syms + r_symndx;
997               sec = local_sections[r_symndx];
998               if (sec == 0)
999                 continue;
1000               sym_name = "<local symbol>";
1001               relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1002               /* r_addend may have changed if the reference section was
1003                  a merge section.  */
1004               addend = rel->r_addend;
1005             }
1006           else
1007             {
1008               /* External symbol.  */
1009               bfd_boolean warned ATTRIBUTE_UNUSED;
1010               bfd_boolean ignored ATTRIBUTE_UNUSED;
1011
1012               RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1013                                        r_symndx, symtab_hdr, sym_hashes,
1014                                        h, sec, relocation,
1015                                        unresolved_reloc, warned, ignored);
1016               sym_name = h->root.root.string;
1017             }
1018
1019           /* Sanity check the address.  */
1020           if (offset > bfd_get_section_limit (input_bfd, input_section))
1021             {
1022               r = bfd_reloc_outofrange;
1023               goto check_reloc;
1024             }
1025
1026           resolved_to_zero = (h != NULL
1027                               && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
1028
1029           switch ((int) r_type)
1030             {
1031             case (int) R_MICROBLAZE_SRO32 :
1032               {
1033                 const char *name;
1034
1035                 /* Only relocate if the symbol is defined.  */
1036                 if (sec)
1037                   {
1038                     name = bfd_get_section_name (sec->owner, sec);
1039
1040                     if (strcmp (name, ".sdata2") == 0
1041                         || strcmp (name, ".sbss2") == 0)
1042                       {
1043                         if (ro_small_data_pointer == 0)
1044                           microblaze_elf_final_sdp (info);
1045                         if (ro_small_data_pointer == 0)
1046                           {
1047                             ret = FALSE;
1048                             r = bfd_reloc_undefined;
1049                             goto check_reloc;
1050                           }
1051
1052                         /* At this point `relocation' contains the object's
1053                            address.  */
1054                         relocation -= ro_small_data_pointer;
1055                         /* Now it contains the offset from _SDA2_BASE_.  */
1056                         r = _bfd_final_link_relocate (howto, input_bfd,
1057                                                       input_section,
1058                                                       contents, offset,
1059                                                       relocation, addend);
1060                       }
1061                     else
1062                       {
1063                         _bfd_error_handler
1064                           /* xgettext:c-format */
1065                           (_("%pB: the target (%s) of an %s relocation"
1066                              " is in the wrong section (%pA)"),
1067                            input_bfd,
1068                            sym_name,
1069                            microblaze_elf_howto_table[(int) r_type]->name,
1070                            sec);
1071                         /*bfd_set_error (bfd_error_bad_value); ??? why? */
1072                         ret = FALSE;
1073                         continue;
1074                       }
1075                   }
1076               }
1077               break;
1078
1079             case (int) R_MICROBLAZE_SRW32 :
1080               {
1081                 const char *name;
1082
1083                 /* Only relocate if the symbol is defined.  */
1084                 if (sec)
1085                   {
1086                     name = bfd_get_section_name (sec->owner, sec);
1087
1088                     if (strcmp (name, ".sdata") == 0
1089                         || strcmp (name, ".sbss") == 0)
1090                       {
1091                         if (rw_small_data_pointer == 0)
1092                           microblaze_elf_final_sdp (info);
1093                         if (rw_small_data_pointer == 0)
1094                           {
1095                             ret = FALSE;
1096                             r = bfd_reloc_undefined;
1097                             goto check_reloc;
1098                           }
1099
1100                         /* At this point `relocation' contains the object's
1101                            address.  */
1102                         relocation -= rw_small_data_pointer;
1103                         /* Now it contains the offset from _SDA_BASE_.  */
1104                         r = _bfd_final_link_relocate (howto, input_bfd,
1105                                                       input_section,
1106                                                       contents, offset,
1107                                                       relocation, addend);
1108                       }
1109                     else
1110                       {
1111                         _bfd_error_handler
1112                           /* xgettext:c-format */
1113                           (_("%pB: the target (%s) of an %s relocation"
1114                              " is in the wrong section (%pA)"),
1115                            input_bfd,
1116                            sym_name,
1117                            microblaze_elf_howto_table[(int) r_type]->name,
1118                            sec);
1119                         /*bfd_set_error (bfd_error_bad_value); ??? why? */
1120                         ret = FALSE;
1121                         continue;
1122                       }
1123                   }
1124               }
1125               break;
1126
1127             case (int) R_MICROBLAZE_32_SYM_OP_SYM:
1128               break; /* Do nothing.  */
1129
1130             case (int) R_MICROBLAZE_GOTPC_64:
1131               relocation = (htab->elf.sgotplt->output_section->vma
1132                             + htab->elf.sgotplt->output_offset);
1133               relocation -= (input_section->output_section->vma
1134                              + input_section->output_offset
1135                              + offset + INST_WORD_SIZE);
1136               relocation += addend;
1137               bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
1138                           contents + offset + endian);
1139               bfd_put_16 (input_bfd, relocation & 0xffff,
1140                           contents + offset + endian + INST_WORD_SIZE);
1141               break;
1142
1143             case (int) R_MICROBLAZE_PLT_64:
1144               {
1145                 bfd_vma immediate;
1146                 if (htab->elf.splt != NULL && h != NULL
1147                     && h->plt.offset != (bfd_vma) -1)
1148                   {
1149                     relocation = (htab->elf.splt->output_section->vma
1150                                   + htab->elf.splt->output_offset
1151                                   + h->plt.offset);
1152                     unresolved_reloc = FALSE;
1153                     immediate = relocation - (input_section->output_section->vma
1154                                               + input_section->output_offset
1155                                               + offset + INST_WORD_SIZE);
1156                     bfd_put_16 (input_bfd, (immediate >> 16) & 0xffff,
1157                                 contents + offset + endian);
1158                     bfd_put_16 (input_bfd, immediate & 0xffff,
1159                                 contents + offset + endian + INST_WORD_SIZE);
1160                   }
1161                 else
1162                   {
1163                     relocation -= (input_section->output_section->vma
1164                                    + input_section->output_offset
1165                                    + offset + INST_WORD_SIZE);
1166                     immediate = relocation;
1167                     bfd_put_16 (input_bfd, (immediate >> 16) & 0xffff,
1168                                 contents + offset + endian);
1169                     bfd_put_16 (input_bfd, immediate & 0xffff,
1170                                 contents + offset + endian + INST_WORD_SIZE);
1171                   }
1172                 break;
1173               }
1174
1175             case (int) R_MICROBLAZE_TLSGD:
1176               tls_type = (TLS_TLS | TLS_GD);
1177               goto dogot;
1178             case (int) R_MICROBLAZE_TLSLD:
1179               tls_type = (TLS_TLS | TLS_LD);
1180               /* Fall through.  */
1181             dogot:
1182             case (int) R_MICROBLAZE_GOT_64:
1183               {
1184                 bfd_vma *offp;
1185                 bfd_vma off, off2;
1186                 unsigned long indx;
1187                 bfd_vma static_value;
1188
1189                 bfd_boolean need_relocs = FALSE;
1190                 if (htab->elf.sgot == NULL)
1191                   abort ();
1192
1193                 indx = 0;
1194                 offp = NULL;
1195
1196                 /* 1. Identify GOT Offset;
1197                    2. Compute Static Values
1198                    3. Process Module Id, Process Offset
1199                    4. Fixup Relocation with GOT offset value. */
1200
1201                 /* 1. Determine GOT Offset to use : TLS_LD, global, local */
1202                 if (IS_TLS_LD (tls_type))
1203                   offp = &htab->tlsld_got.offset;
1204                 else if (h != NULL)
1205                   {
1206                     if (htab->elf.sgotplt != NULL
1207                         && h->got.offset != (bfd_vma) -1)
1208                       offp = &h->got.offset;
1209                     else
1210                       abort ();
1211                   }
1212                 else
1213                   {
1214                     if (local_got_offsets == NULL)
1215                       abort ();
1216                     offp = &local_got_offsets[r_symndx];
1217                   }
1218
1219                 if (!offp)
1220                   abort ();
1221
1222                 off = (*offp) & ~1;
1223                 off2 = off;
1224
1225                 if (IS_TLS_LD(tls_type) || IS_TLS_GD(tls_type))
1226                   off2 = off + 4;
1227
1228                 /* Symbol index to use for relocs */
1229                 if (h != NULL)
1230                   {
1231                     bfd_boolean dyn =
1232                         elf_hash_table (info)->dynamic_sections_created;
1233
1234                     if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1235                                                          bfd_link_pic (info),
1236                                                          h)
1237                         && (!bfd_link_pic (info)
1238                             || !SYMBOL_REFERENCES_LOCAL (info, h)))
1239                       indx = h->dynindx;
1240                   }
1241
1242                 /* Need to generate relocs ? */
1243                 if ((bfd_link_pic (info) || indx != 0)
1244                     && (h == NULL
1245                     || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1246                         && !resolved_to_zero)
1247                     || h->root.type != bfd_link_hash_undefweak))
1248                   need_relocs = TRUE;
1249
1250                 /* 2. Compute/Emit Static value of r-expression */
1251                 static_value = relocation + addend;
1252
1253                 /* 3. Process module-id and offset */
1254                 if (! ((*offp) & 1) )
1255                   {
1256                     bfd_vma got_offset;
1257
1258                     got_offset = (htab->elf.sgot->output_section->vma
1259                                   + htab->elf.sgot->output_offset
1260                                   + off);
1261
1262                     /* Process module-id */
1263                     if (IS_TLS_LD(tls_type))
1264                       {
1265                         if (! bfd_link_pic (info))
1266                           bfd_put_32 (output_bfd, 1,
1267                                       htab->elf.sgot->contents + off);
1268                         else
1269                           microblaze_elf_output_dynamic_relocation
1270                             (output_bfd,
1271                              htab->elf.srelgot,
1272                              htab->elf.srelgot->reloc_count++,
1273                              /* symindex= */ 0, R_MICROBLAZE_TLSDTPMOD32,
1274                              got_offset, 0);
1275                       }
1276                     else if (IS_TLS_GD(tls_type))
1277                       {
1278                         if (! need_relocs)
1279                           bfd_put_32 (output_bfd, 1,
1280                                       htab->elf.sgot->contents + off);
1281                         else
1282                           microblaze_elf_output_dynamic_relocation
1283                             (output_bfd,
1284                              htab->elf.srelgot,
1285                              htab->elf.srelgot->reloc_count++,
1286                              /* symindex= */ indx, R_MICROBLAZE_TLSDTPMOD32,
1287                              got_offset, indx ? 0 : static_value);
1288                       }
1289
1290                     /* Process Offset */
1291                     if (htab->elf.srelgot == NULL)
1292                       abort ();
1293
1294                     got_offset = (htab->elf.sgot->output_section->vma
1295                                   + htab->elf.sgot->output_offset
1296                                   + off2);
1297                     if (IS_TLS_LD(tls_type))
1298                       {
1299                         /* For LD, offset should be 0 */
1300                         *offp |= 1;
1301                         bfd_put_32 (output_bfd, 0,
1302                                     htab->elf.sgot->contents + off2);
1303                       }
1304                     else if (IS_TLS_GD(tls_type))
1305                       {
1306                         *offp |= 1;
1307                         static_value -= dtprel_base(info);
1308                         if (need_relocs)
1309                           microblaze_elf_output_dynamic_relocation
1310                             (output_bfd,
1311                              htab->elf.srelgot,
1312                              htab->elf.srelgot->reloc_count++,
1313                              /* symindex= */ indx, R_MICROBLAZE_TLSDTPREL32,
1314                              got_offset, indx ? 0 : static_value);
1315                         else
1316                           bfd_put_32 (output_bfd, static_value,
1317                                       htab->elf.sgot->contents + off2);
1318                       }
1319                     else
1320                       {
1321                         bfd_put_32 (output_bfd, static_value,
1322                                     htab->elf.sgot->contents + off2);
1323
1324                         /* Relocs for dyn symbols generated by
1325                            finish_dynamic_symbols */
1326                         if (bfd_link_pic (info) && h == NULL)
1327                           {
1328                             *offp |= 1;
1329                             microblaze_elf_output_dynamic_relocation
1330                               (output_bfd,
1331                                htab->elf.srelgot,
1332                                htab->elf.srelgot->reloc_count++,
1333                                /* symindex= */ indx, R_MICROBLAZE_REL,
1334                                got_offset, static_value);
1335                           }
1336                       }
1337                   }
1338
1339                 /* 4. Fixup Relocation with GOT offset value
1340                       Compute relative address of GOT entry for applying
1341                       the current relocation */
1342                 relocation = htab->elf.sgot->output_section->vma
1343                              + htab->elf.sgot->output_offset
1344                              + off
1345                              - htab->elf.sgotplt->output_section->vma
1346                              - htab->elf.sgotplt->output_offset;
1347
1348                 /* Apply Current Relocation */
1349                 bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
1350                             contents + offset + endian);
1351                 bfd_put_16 (input_bfd, relocation & 0xffff,
1352                             contents + offset + endian + INST_WORD_SIZE);
1353
1354                 unresolved_reloc = FALSE;
1355                 break;
1356               }
1357
1358             case (int) R_MICROBLAZE_GOTOFF_64:
1359               {
1360                 bfd_vma immediate;
1361                 unsigned short lo, high;
1362                 relocation += addend;
1363                 relocation -= (htab->elf.sgotplt->output_section->vma
1364                                + htab->elf.sgotplt->output_offset);
1365                 /* Write this value into correct location.  */
1366                 immediate = relocation;
1367                 lo = immediate & 0x0000ffff;
1368                 high = (immediate >> 16) & 0x0000ffff;
1369                 bfd_put_16 (input_bfd, high, contents + offset + endian);
1370                 bfd_put_16 (input_bfd, lo,
1371                             contents + offset + INST_WORD_SIZE + endian);
1372                 break;
1373               }
1374
1375             case (int) R_MICROBLAZE_GOTOFF_32:
1376               {
1377                 relocation += addend;
1378                 relocation -= (htab->elf.sgotplt->output_section->vma
1379                                + htab->elf.sgotplt->output_offset);
1380                 /* Write this value into correct location.  */
1381                 bfd_put_32 (input_bfd, relocation, contents + offset);
1382                 break;
1383               }
1384
1385             case (int) R_MICROBLAZE_TLSDTPREL64:
1386               relocation += addend;
1387               relocation -= dtprel_base(info);
1388               bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
1389                           contents + offset + 2);
1390               bfd_put_16 (input_bfd, relocation & 0xffff,
1391                           contents + offset + 2 + INST_WORD_SIZE);
1392               break;
1393             case (int) R_MICROBLAZE_64_PCREL :
1394             case (int) R_MICROBLAZE_64:
1395             case (int) R_MICROBLAZE_32:
1396               {
1397                 /* r_symndx will be STN_UNDEF (zero) only for relocs against symbols
1398                    from removed linkonce sections, or sections discarded by
1399                    a linker script.  */
1400                 if (r_symndx == STN_UNDEF || (input_section->flags & SEC_ALLOC) == 0)
1401                   {
1402                     relocation += addend;
1403                     if (r_type == R_MICROBLAZE_32)
1404                       bfd_put_32 (input_bfd, relocation, contents + offset);
1405                     else
1406                       {
1407                         if (r_type == R_MICROBLAZE_64_PCREL)
1408                           relocation -= (input_section->output_section->vma
1409                                          + input_section->output_offset
1410                                          + offset + INST_WORD_SIZE);
1411                         bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
1412                                     contents + offset + endian);
1413                         bfd_put_16 (input_bfd, relocation & 0xffff,
1414                                     contents + offset + endian + INST_WORD_SIZE);
1415                       }
1416                     break;
1417                   }
1418
1419                 if ((bfd_link_pic (info)
1420                      && (h == NULL
1421                          || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1422                              && !resolved_to_zero)
1423                          || h->root.type != bfd_link_hash_undefweak)
1424                      && (!howto->pc_relative
1425                          || (h != NULL
1426                              && h->dynindx != -1
1427                              && (!info->symbolic
1428                                  || !h->def_regular))))
1429                     || (!bfd_link_pic (info)
1430                         && h != NULL
1431                         && h->dynindx != -1
1432                         && !h->non_got_ref
1433                         && ((h->def_dynamic
1434                              && !h->def_regular)
1435                             || h->root.type == bfd_link_hash_undefweak
1436                             || h->root.type == bfd_link_hash_undefined)))
1437                   {
1438                     Elf_Internal_Rela outrel;
1439                     bfd_byte *loc;
1440                     bfd_boolean skip;
1441
1442                     /* When generating a shared object, these relocations
1443                        are copied into the output file to be resolved at run
1444                        time.  */
1445
1446                     BFD_ASSERT (sreloc != NULL);
1447
1448                     skip = FALSE;
1449
1450                     outrel.r_offset =
1451                       _bfd_elf_section_offset (output_bfd, info, input_section,
1452                                                rel->r_offset);
1453                     if (outrel.r_offset == (bfd_vma) -1)
1454                       skip = TRUE;
1455                     else if (outrel.r_offset == (bfd_vma) -2)
1456                       skip = TRUE;
1457                     outrel.r_offset += (input_section->output_section->vma
1458                                         + input_section->output_offset);
1459
1460                     if (skip)
1461                       memset (&outrel, 0, sizeof outrel);
1462                     /* h->dynindx may be -1 if the symbol was marked to
1463                        become local.  */
1464                     else if (h != NULL
1465                              && ((! info->symbolic && h->dynindx != -1)
1466                                  || !h->def_regular))
1467                       {
1468                         BFD_ASSERT (h->dynindx != -1);
1469                         outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1470                         outrel.r_addend = addend;
1471                       }
1472                     else
1473                       {
1474                         if (r_type == R_MICROBLAZE_32)
1475                           {
1476                             outrel.r_info = ELF32_R_INFO (0, R_MICROBLAZE_REL);
1477                             outrel.r_addend = relocation + addend;
1478                           }
1479                         else
1480                           {
1481                             BFD_FAIL ();
1482                             _bfd_error_handler
1483                               (_("%pB: probably compiled without -fPIC?"),
1484                                input_bfd);
1485                             bfd_set_error (bfd_error_bad_value);
1486                             return FALSE;
1487                           }
1488                       }
1489
1490                     loc = sreloc->contents;
1491                     loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1492                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1493                     break;
1494                   }
1495                 else
1496                   {
1497                     relocation += addend;
1498                     if (r_type == R_MICROBLAZE_32)
1499                       bfd_put_32 (input_bfd, relocation, contents + offset);
1500                     else
1501                       {
1502                         if (r_type == R_MICROBLAZE_64_PCREL)
1503                           relocation -= (input_section->output_section->vma
1504                                          + input_section->output_offset
1505                                          + offset + INST_WORD_SIZE);
1506                         bfd_put_16 (input_bfd, (relocation >> 16) & 0xffff,
1507                                     contents + offset + endian);
1508                         bfd_put_16 (input_bfd, relocation & 0xffff,
1509                                     contents + offset + endian + INST_WORD_SIZE);
1510                       }
1511                     break;
1512                   }
1513               }
1514
1515             default :
1516               r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1517                                             contents, offset,
1518                                             relocation, addend);
1519               break;
1520             }
1521         }
1522
1523     check_reloc:
1524
1525       if (r != bfd_reloc_ok)
1526         {
1527           /* FIXME: This should be generic enough to go in a utility.  */
1528           const char *name;
1529
1530           if (h != NULL)
1531             name = h->root.root.string;
1532           else
1533             {
1534               name = (bfd_elf_string_from_elf_section
1535                       (input_bfd, symtab_hdr->sh_link, sym->st_name));
1536               if (name == NULL || *name == '\0')
1537                 name = bfd_section_name (input_bfd, sec);
1538             }
1539
1540           if (errmsg != NULL)
1541             goto common_error;
1542
1543           switch (r)
1544             {
1545             case bfd_reloc_overflow:
1546               (*info->callbacks->reloc_overflow)
1547                 (info, (h ? &h->root : NULL), name, howto->name,
1548                  (bfd_vma) 0, input_bfd, input_section, offset);
1549               break;
1550
1551             case bfd_reloc_undefined:
1552               (*info->callbacks->undefined_symbol)
1553                 (info, name, input_bfd, input_section, offset, TRUE);
1554               break;
1555
1556             case bfd_reloc_outofrange:
1557               errmsg = _("internal error: out of range error");
1558               goto common_error;
1559
1560             case bfd_reloc_notsupported:
1561               errmsg = _("internal error: unsupported relocation error");
1562               goto common_error;
1563
1564             case bfd_reloc_dangerous:
1565               errmsg = _("internal error: dangerous error");
1566               goto common_error;
1567
1568             default:
1569               errmsg = _("internal error: unknown error");
1570               /* Fall through.  */
1571             common_error:
1572               (*info->callbacks->warning) (info, errmsg, name, input_bfd,
1573                                            input_section, offset);
1574               break;
1575             }
1576         }
1577     }
1578
1579   return ret;
1580 }
1581 \f
1582 /* Calculate fixup value for reference.  */
1583
1584 static int
1585 calc_fixup (bfd_vma start, bfd_vma size, asection *sec)
1586 {
1587   bfd_vma end = start + size;
1588   int i, fixup = 0;
1589
1590   if (sec == NULL || sec->relax == NULL)
1591     return 0;
1592
1593   /* Look for addr in relax table, total fixup value.  */
1594   for (i = 0; i < sec->relax_count; i++)
1595     {
1596       if (end <= sec->relax[i].addr)
1597         break;
1598       if ((end != start) && (start > sec->relax[i].addr))
1599         continue;
1600       fixup += sec->relax[i].size;
1601     }
1602   return fixup;
1603 }
1604
1605 /* Read-modify-write into the bfd, an immediate value into appropriate fields of
1606    a 32-bit instruction.  */
1607 static void
1608 microblaze_bfd_write_imm_value_32 (bfd *abfd, bfd_byte *bfd_addr, bfd_vma val)
1609 {
1610     unsigned long instr = bfd_get_32 (abfd, bfd_addr);
1611     instr &= ~0x0000ffff;
1612     instr |= (val & 0x0000ffff);
1613     bfd_put_32 (abfd, instr, bfd_addr);
1614 }
1615
1616 /* Read-modify-write into the bfd, an immediate value into appropriate fields of
1617    two consecutive 32-bit instructions.  */
1618 static void
1619 microblaze_bfd_write_imm_value_64 (bfd *abfd, bfd_byte *bfd_addr, bfd_vma val)
1620 {
1621     unsigned long instr_hi;
1622     unsigned long instr_lo;
1623
1624     instr_hi = bfd_get_32 (abfd, bfd_addr);
1625     instr_hi &= ~0x0000ffff;
1626     instr_hi |= ((val >> 16) & 0x0000ffff);
1627     bfd_put_32 (abfd, instr_hi, bfd_addr);
1628
1629     instr_lo = bfd_get_32 (abfd, bfd_addr + INST_WORD_SIZE);
1630     instr_lo &= ~0x0000ffff;
1631     instr_lo |= (val & 0x0000ffff);
1632     bfd_put_32 (abfd, instr_lo, bfd_addr + INST_WORD_SIZE);
1633 }
1634
1635 static bfd_boolean
1636 microblaze_elf_relax_section (bfd *abfd,
1637                               asection *sec,
1638                               struct bfd_link_info *link_info,
1639                               bfd_boolean *again)
1640 {
1641   Elf_Internal_Shdr *symtab_hdr;
1642   Elf_Internal_Rela *internal_relocs;
1643   Elf_Internal_Rela *free_relocs = NULL;
1644   Elf_Internal_Rela *irel, *irelend;
1645   bfd_byte *contents = NULL;
1646   bfd_byte *free_contents = NULL;
1647   int rel_count;
1648   unsigned int shndx;
1649   int i, sym_index;
1650   asection *o;
1651   struct elf_link_hash_entry *sym_hash;
1652   Elf_Internal_Sym *isymbuf, *isymend;
1653   Elf_Internal_Sym *isym;
1654   int symcount;
1655   int offset;
1656   bfd_vma src, dest;
1657
1658   /* We only do this once per section.  We may be able to delete some code
1659      by running multiple passes, but it is not worth it.  */
1660   *again = FALSE;
1661
1662   /* Only do this for a text section.  */
1663   if (bfd_link_relocatable (link_info)
1664       || (sec->flags & SEC_RELOC) == 0
1665       || (sec->reloc_count == 0)
1666       || (sec->flags & SEC_CODE) == 0)
1667     return TRUE;
1668
1669   BFD_ASSERT ((sec->size > 0) || (sec->rawsize > 0));
1670
1671   /* If this is the first time we have been called for this section,
1672      initialize the cooked size.  */
1673   if (sec->size == 0)
1674     sec->size = sec->rawsize;
1675
1676   /* Get symbols for this section.  */
1677   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1678   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1679   symcount =  symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
1680   if (isymbuf == NULL)
1681     isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, symcount,
1682                                     0, NULL, NULL, NULL);
1683   BFD_ASSERT (isymbuf != NULL);
1684
1685   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, link_info->keep_memory);
1686   if (internal_relocs == NULL)
1687     goto error_return;
1688   if (! link_info->keep_memory)
1689     free_relocs = internal_relocs;
1690
1691   sec->relax = (struct relax_table *) bfd_malloc ((sec->reloc_count + 1)
1692                                                   * sizeof (struct relax_table));
1693   if (sec->relax == NULL)
1694     goto error_return;
1695   sec->relax_count = 0;
1696
1697   irelend = internal_relocs + sec->reloc_count;
1698   rel_count = 0;
1699   for (irel = internal_relocs; irel < irelend; irel++, rel_count++)
1700     {
1701       bfd_vma symval;
1702       if ((ELF32_R_TYPE (irel->r_info) != (int) R_MICROBLAZE_64_PCREL)
1703           && (ELF32_R_TYPE (irel->r_info) != (int) R_MICROBLAZE_64 ))
1704         continue; /* Can't delete this reloc.  */
1705
1706       /* Get the section contents.  */
1707       if (contents == NULL)
1708         {
1709           if (elf_section_data (sec)->this_hdr.contents != NULL)
1710             contents = elf_section_data (sec)->this_hdr.contents;
1711           else
1712             {
1713               contents = (bfd_byte *) bfd_malloc (sec->size);
1714               if (contents == NULL)
1715                 goto error_return;
1716               free_contents = contents;
1717
1718               if (!bfd_get_section_contents (abfd, sec, contents,
1719                                              (file_ptr) 0, sec->size))
1720                 goto error_return;
1721               elf_section_data (sec)->this_hdr.contents = contents;
1722             }
1723         }
1724
1725       /* Get the value of the symbol referred to by the reloc.  */
1726       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1727         {
1728           /* A local symbol.  */
1729           asection *sym_sec;
1730
1731           isym = isymbuf + ELF32_R_SYM (irel->r_info);
1732           if (isym->st_shndx == SHN_UNDEF)
1733             sym_sec = bfd_und_section_ptr;
1734           else if (isym->st_shndx == SHN_ABS)
1735             sym_sec = bfd_abs_section_ptr;
1736           else if (isym->st_shndx == SHN_COMMON)
1737             sym_sec = bfd_com_section_ptr;
1738           else
1739             sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1740
1741           symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
1742         }
1743       else
1744         {
1745           unsigned long indx;
1746           struct elf_link_hash_entry *h;
1747
1748           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1749           h = elf_sym_hashes (abfd)[indx];
1750           BFD_ASSERT (h != NULL);
1751
1752           if (h->root.type != bfd_link_hash_defined
1753               && h->root.type != bfd_link_hash_defweak)
1754             /* This appears to be a reference to an undefined
1755                symbol.  Just ignore it--it will be caught by the
1756                regular reloc processing.  */
1757             continue;
1758
1759           symval = (h->root.u.def.value
1760                     + h->root.u.def.section->output_section->vma
1761                     + h->root.u.def.section->output_offset);
1762         }
1763
1764       /* If this is a PC-relative reloc, subtract the instr offset from
1765          the symbol value.  */
1766       if (ELF32_R_TYPE (irel->r_info) == (int) R_MICROBLAZE_64_PCREL)
1767         {
1768           symval = symval + irel->r_addend
1769             - (irel->r_offset
1770                + sec->output_section->vma
1771                + sec->output_offset);
1772         }
1773       else
1774         symval += irel->r_addend;
1775
1776       if ((symval & 0xffff8000) == 0
1777           || (symval & 0xffff8000) == 0xffff8000)
1778         {
1779           /* We can delete this instruction.  */
1780           sec->relax[sec->relax_count].addr = irel->r_offset;
1781           sec->relax[sec->relax_count].size = INST_WORD_SIZE;
1782           sec->relax_count++;
1783
1784           /* Rewrite relocation type.  */
1785           switch ((enum elf_microblaze_reloc_type) ELF32_R_TYPE (irel->r_info))
1786             {
1787             case R_MICROBLAZE_64_PCREL:
1788               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1789                                            (int) R_MICROBLAZE_32_PCREL_LO);
1790               break;
1791             case R_MICROBLAZE_64:
1792               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1793                                            (int) R_MICROBLAZE_32_LO);
1794               break;
1795             default:
1796               /* Cannot happen.  */
1797               BFD_ASSERT (FALSE);
1798             }
1799         }
1800     } /* Loop through all relocations.  */
1801
1802   /* Loop through the relocs again, and see if anything needs to change.  */
1803   if (sec->relax_count > 0)
1804     {
1805       shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1806       rel_count = 0;
1807       sec->relax[sec->relax_count].addr = sec->size;
1808
1809       for (irel = internal_relocs; irel < irelend; irel++, rel_count++)
1810         {
1811           bfd_vma nraddr;
1812
1813           /* Get the new reloc address.  */
1814           nraddr = irel->r_offset - calc_fixup (irel->r_offset, 0, sec);
1815           switch ((enum elf_microblaze_reloc_type) ELF32_R_TYPE (irel->r_info))
1816             {
1817             default:
1818               break;
1819             case R_MICROBLAZE_64_PCREL:
1820               break;
1821             case R_MICROBLAZE_64:
1822             case R_MICROBLAZE_32_LO:
1823               /* If this reloc is against a symbol defined in this
1824                  section, we must check the addend to see it will put the value in
1825                  range to be adjusted, and hence must be changed.  */
1826               if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1827                 {
1828                   isym = isymbuf + ELF32_R_SYM (irel->r_info);
1829                   /* Only handle relocs against .text.  */
1830                   if (isym->st_shndx == shndx
1831                       && ELF32_ST_TYPE (isym->st_info) == STT_SECTION)
1832                     irel->r_addend -= calc_fixup (irel->r_addend, 0, sec);
1833                 }
1834               break;
1835             case R_MICROBLAZE_NONE:
1836               {
1837                 /* This was a PC-relative instruction that was
1838                    completely resolved.  */
1839                 int sfix, efix;
1840                 bfd_vma target_address;
1841                 target_address = irel->r_addend + irel->r_offset;
1842                 sfix = calc_fixup (irel->r_offset, 0, sec);
1843                 efix = calc_fixup (target_address, 0, sec);
1844                 irel->r_addend -= (efix - sfix);
1845                 /* Should use HOWTO.  */
1846                 microblaze_bfd_write_imm_value_32 (abfd, contents + irel->r_offset,
1847                                                    irel->r_addend);
1848               }
1849               break;
1850             case R_MICROBLAZE_64_NONE:
1851               {
1852                 /* This was a PC-relative 64-bit instruction that was
1853                    completely resolved.  */
1854                 int sfix, efix;
1855                 bfd_vma target_address;
1856                 target_address = irel->r_addend + irel->r_offset + INST_WORD_SIZE;
1857                 sfix = calc_fixup (irel->r_offset + INST_WORD_SIZE, 0, sec);
1858                 efix = calc_fixup (target_address, 0, sec);
1859                 irel->r_addend -= (efix - sfix);
1860     microblaze_bfd_write_imm_value_32 (abfd, contents + irel->r_offset
1861                                        + INST_WORD_SIZE, irel->r_addend);
1862               }
1863               break;
1864             }
1865           irel->r_offset = nraddr;
1866         } /* Change all relocs in this section.  */
1867
1868       /* Look through all other sections.  */
1869       for (o = abfd->sections; o != NULL; o = o->next)
1870         {
1871           Elf_Internal_Rela *irelocs;
1872           Elf_Internal_Rela *irelscan, *irelscanend;
1873           bfd_byte *ocontents;
1874
1875           if (o == sec
1876               || (o->flags & SEC_RELOC) == 0
1877               || o->reloc_count == 0)
1878             continue;
1879
1880           /* We always cache the relocs.  Perhaps, if info->keep_memory is
1881              FALSE, we should free them, if we are permitted to.  */
1882
1883           irelocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, TRUE);
1884           if (irelocs == NULL)
1885             goto error_return;
1886
1887           ocontents = NULL;
1888           irelscanend = irelocs + o->reloc_count;
1889           for (irelscan = irelocs; irelscan < irelscanend; irelscan++)
1890             {
1891               if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32)
1892                 {
1893                   isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1894
1895                   /* Look at the reloc only if the value has been resolved.  */
1896                   if (isym->st_shndx == shndx
1897                       && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
1898                     {
1899                       if (ocontents == NULL)
1900                         {
1901                           if (elf_section_data (o)->this_hdr.contents != NULL)
1902                             ocontents = elf_section_data (o)->this_hdr.contents;
1903                           else
1904                             {
1905                               /* We always cache the section contents.
1906                                  Perhaps, if info->keep_memory is FALSE, we
1907                                  should free them, if we are permitted to.  */
1908                               if (o->rawsize == 0)
1909                                 o->rawsize = o->size;
1910                               ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
1911                               if (ocontents == NULL)
1912                                 goto error_return;
1913                               if (!bfd_get_section_contents (abfd, o, ocontents,
1914                                                              (file_ptr) 0,
1915                                                              o->rawsize))
1916                                 goto error_return;
1917                               elf_section_data (o)->this_hdr.contents = ocontents;
1918                             }
1919
1920                         }
1921                       irelscan->r_addend -= calc_fixup (irelscan->r_addend, 0, sec);
1922                     }
1923                   else if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32_SYM_OP_SYM)
1924                     {
1925                       isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1926
1927                       /* Look at the reloc only if the value has been resolved.  */
1928                       if (ocontents == NULL)
1929                         {
1930                           if (elf_section_data (o)->this_hdr.contents != NULL)
1931                             ocontents = elf_section_data (o)->this_hdr.contents;
1932                           else
1933                             {
1934                               /* We always cache the section contents.
1935                                  Perhaps, if info->keep_memory is FALSE, we
1936                                  should free them, if we are permitted to.  */
1937
1938                               if (o->rawsize == 0)
1939                                 o->rawsize = o->size;
1940                               ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
1941                               if (ocontents == NULL)
1942                                 goto error_return;
1943                               if (!bfd_get_section_contents (abfd, o, ocontents,
1944                                                              (file_ptr) 0,
1945                                                              o->rawsize))
1946                                 goto error_return;
1947                               elf_section_data (o)->this_hdr.contents = ocontents;
1948                             }
1949                         }
1950                       irelscan->r_addend -= calc_fixup (irel->r_addend
1951                                                         + isym->st_value,
1952                                                         0,
1953                                                         sec);
1954                     }
1955                 }
1956               else if ((ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32_PCREL_LO)
1957                        || (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_32_LO))
1958                 {
1959                   isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1960
1961                   /* Look at the reloc only if the value has been resolved.  */
1962                   if (isym->st_shndx == shndx
1963                       && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
1964                     {
1965                       bfd_vma immediate;
1966                       bfd_vma target_address;
1967
1968                       if (ocontents == NULL)
1969                         {
1970                           if (elf_section_data (o)->this_hdr.contents != NULL)
1971                             ocontents = elf_section_data (o)->this_hdr.contents;
1972                           else
1973                             {
1974                               /* We always cache the section contents.
1975                                  Perhaps, if info->keep_memory is FALSE, we
1976                                  should free them, if we are permitted to.  */
1977                               if (o->rawsize == 0)
1978                                 o->rawsize = o->size;
1979                               ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
1980                               if (ocontents == NULL)
1981                                 goto error_return;
1982                               if (!bfd_get_section_contents (abfd, o, ocontents,
1983                                                              (file_ptr) 0,
1984                                                              o->rawsize))
1985                                 goto error_return;
1986                               elf_section_data (o)->this_hdr.contents = ocontents;
1987                             }
1988                         }
1989
1990                       unsigned long instr = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1991                       immediate = instr & 0x0000ffff;
1992                       target_address = immediate;
1993                       offset = calc_fixup (target_address, 0, sec);
1994                       immediate -= offset;
1995                       irelscan->r_addend -= offset;
1996           microblaze_bfd_write_imm_value_32 (abfd, ocontents + irelscan->r_offset,
1997                                              irelscan->r_addend);
1998                     }
1999                 }
2000
2001               if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_64)
2002                 {
2003                   isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
2004
2005                   /* Look at the reloc only if the value has been resolved.  */
2006                   if (isym->st_shndx == shndx
2007                       && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
2008                     {
2009                       bfd_vma immediate;
2010
2011                       if (ocontents == NULL)
2012                         {
2013                           if (elf_section_data (o)->this_hdr.contents != NULL)
2014                             ocontents = elf_section_data (o)->this_hdr.contents;
2015                           else
2016                             {
2017                               /* We always cache the section contents.
2018                                  Perhaps, if info->keep_memory is FALSE, we
2019                                  should free them, if we are permitted to.  */
2020
2021                               if (o->rawsize == 0)
2022                                 o->rawsize = o->size;
2023                               ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
2024                               if (ocontents == NULL)
2025                                 goto error_return;
2026                               if (!bfd_get_section_contents (abfd, o, ocontents,
2027                                                              (file_ptr) 0,
2028                                                              o->rawsize))
2029                                 goto error_return;
2030                               elf_section_data (o)->this_hdr.contents = ocontents;
2031                             }
2032                         }
2033           unsigned long instr_hi =  bfd_get_32 (abfd, ocontents
2034                                                 + irelscan->r_offset);
2035           unsigned long instr_lo =  bfd_get_32 (abfd, ocontents
2036                                                 + irelscan->r_offset
2037                                                 + INST_WORD_SIZE);
2038           immediate = (instr_hi & 0x0000ffff) << 16;
2039           immediate |= (instr_lo & 0x0000ffff);
2040                       offset = calc_fixup (irelscan->r_addend, 0, sec);
2041                       immediate -= offset;
2042                       irelscan->r_addend -= offset;
2043                     }
2044                 }
2045               else if (ELF32_R_TYPE (irelscan->r_info) == (int) R_MICROBLAZE_64_PCREL)
2046                 {
2047                   isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
2048
2049                   /* Look at the reloc only if the value has been resolved.  */
2050                   if (isym->st_shndx == shndx
2051                       && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
2052                     {
2053                       bfd_vma immediate;
2054                       bfd_vma target_address;
2055
2056                       if (ocontents == NULL)
2057                         {
2058                           if (elf_section_data (o)->this_hdr.contents != NULL)
2059                             ocontents = elf_section_data (o)->this_hdr.contents;
2060                           else
2061                             {
2062                               /* We always cache the section contents.
2063                                  Perhaps, if info->keep_memory is FALSE, we
2064                                  should free them, if we are permitted to.  */
2065                               if (o->rawsize == 0)
2066                                 o->rawsize = o->size;
2067                               ocontents = (bfd_byte *) bfd_malloc (o->rawsize);
2068                               if (ocontents == NULL)
2069                                 goto error_return;
2070                               if (!bfd_get_section_contents (abfd, o, ocontents,
2071                                                              (file_ptr) 0,
2072                                                              o->rawsize))
2073                                 goto error_return;
2074                               elf_section_data (o)->this_hdr.contents = ocontents;
2075                             }
2076                         }
2077           unsigned long instr_hi =  bfd_get_32 (abfd, ocontents
2078                                                 + irelscan->r_offset);
2079           unsigned long instr_lo =  bfd_get_32 (abfd, ocontents
2080                                                 + irelscan->r_offset
2081                                                 + INST_WORD_SIZE);
2082           immediate = (instr_hi & 0x0000ffff) << 16;
2083           immediate |= (instr_lo & 0x0000ffff);
2084                       target_address = immediate;
2085                       offset = calc_fixup (target_address, 0, sec);
2086                       immediate -= offset;
2087                       irelscan->r_addend -= offset;
2088           microblaze_bfd_write_imm_value_64 (abfd, ocontents
2089                                              + irelscan->r_offset, immediate);
2090                     }
2091                 }
2092             }
2093         }
2094
2095       /* Adjust the local symbols defined in this section.  */
2096       isymend = isymbuf + symtab_hdr->sh_info;
2097       for (isym = isymbuf; isym < isymend; isym++)
2098         {
2099           if (isym->st_shndx == shndx)
2100             {
2101               isym->st_value -= calc_fixup (isym->st_value, 0, sec);
2102               if (isym->st_size)
2103                 isym->st_size -= calc_fixup (isym->st_value, isym->st_size, sec);
2104             }
2105         }
2106
2107       /* Now adjust the global symbols defined in this section.  */
2108       isym = isymbuf + symtab_hdr->sh_info;
2109       symcount =  (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)) - symtab_hdr->sh_info;
2110       for (sym_index = 0; sym_index < symcount; sym_index++)
2111         {
2112           sym_hash = elf_sym_hashes (abfd)[sym_index];
2113           if ((sym_hash->root.type == bfd_link_hash_defined
2114                   || sym_hash->root.type == bfd_link_hash_defweak)
2115               && sym_hash->root.u.def.section == sec)
2116             {
2117               sym_hash->root.u.def.value -= calc_fixup (sym_hash->root.u.def.value,
2118                                                         0, sec);
2119               if (sym_hash->size)
2120                 sym_hash->size -= calc_fixup (sym_hash->root.u.def.value,
2121                                               sym_hash->size, sec);
2122             }
2123         }
2124
2125       /* Physically move the code and change the cooked size.  */
2126       dest = sec->relax[0].addr;
2127       for (i = 0; i < sec->relax_count; i++)
2128         {
2129           int len;
2130           src = sec->relax[i].addr + sec->relax[i].size;
2131           len = sec->relax[i+1].addr - sec->relax[i].addr - sec->relax[i].size;
2132
2133           memmove (contents + dest, contents + src, len);
2134           sec->size -= sec->relax[i].size;
2135           dest += len;
2136         }
2137
2138       elf_section_data (sec)->relocs = internal_relocs;
2139       free_relocs = NULL;
2140
2141       elf_section_data (sec)->this_hdr.contents = contents;
2142       free_contents = NULL;
2143
2144       symtab_hdr->contents = (bfd_byte *) isymbuf;
2145     }
2146
2147   if (free_relocs != NULL)
2148     {
2149       free (free_relocs);
2150       free_relocs = NULL;
2151     }
2152
2153   if (free_contents != NULL)
2154     {
2155       if (!link_info->keep_memory)
2156         free (free_contents);
2157       else
2158         /* Cache the section contents for elf_link_input_bfd.  */
2159         elf_section_data (sec)->this_hdr.contents = contents;
2160       free_contents = NULL;
2161     }
2162
2163   if (sec->relax_count == 0)
2164     {
2165       *again = FALSE;
2166       free (sec->relax);
2167       sec->relax = NULL;
2168     }
2169   else
2170     *again = TRUE;
2171   return TRUE;
2172
2173  error_return:
2174   if (free_relocs != NULL)
2175     free (free_relocs);
2176   if (free_contents != NULL)
2177     free (free_contents);
2178   if (sec->relax != NULL)
2179     {
2180       free (sec->relax);
2181       sec->relax = NULL;
2182       sec->relax_count = 0;
2183     }
2184   return FALSE;
2185 }
2186
2187 /* Return the section that should be marked against GC for a given
2188    relocation.  */
2189
2190 static asection *
2191 microblaze_elf_gc_mark_hook (asection *sec,
2192                              struct bfd_link_info * info,
2193                              Elf_Internal_Rela * rel,
2194                              struct elf_link_hash_entry * h,
2195                              Elf_Internal_Sym * sym)
2196 {
2197   if (h != NULL)
2198     switch (ELF32_R_TYPE (rel->r_info))
2199       {
2200       case R_MICROBLAZE_GNU_VTINHERIT:
2201       case R_MICROBLAZE_GNU_VTENTRY:
2202         return NULL;
2203       }
2204
2205   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2206 }
2207
2208 /* PIC support.  */
2209
2210 #define PLT_ENTRY_SIZE 16
2211
2212 #define PLT_ENTRY_WORD_0  0xb0000000          /* "imm 0".  */
2213 #define PLT_ENTRY_WORD_1  0xe9940000          /* "lwi r12,r20,0" - relocated to lwi r12,r20,func@GOT.  */
2214 #define PLT_ENTRY_WORD_1_NOPIC  0xe9800000    /* "lwi r12,r0,0" - non-PIC object.  */
2215 #define PLT_ENTRY_WORD_2  0x98186000          /* "brad r12".  */
2216 #define PLT_ENTRY_WORD_3  0x80000000          /* "nop".  */
2217
2218 static bfd_boolean
2219 update_local_sym_info (bfd *abfd,
2220                        Elf_Internal_Shdr *symtab_hdr,
2221                        unsigned long r_symndx,
2222                        unsigned int tls_type)
2223 {
2224   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2225   unsigned char *local_got_tls_masks;
2226
2227   if (local_got_refcounts == NULL)
2228     {
2229       bfd_size_type size = symtab_hdr->sh_info;
2230
2231       size *= (sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks));
2232       local_got_refcounts = bfd_zalloc (abfd, size);
2233       if (local_got_refcounts == NULL)
2234         return FALSE;
2235       elf_local_got_refcounts (abfd) = local_got_refcounts;
2236     }
2237
2238   local_got_tls_masks =
2239          (unsigned char *) (local_got_refcounts + symtab_hdr->sh_info);
2240   local_got_tls_masks[r_symndx] |= tls_type;
2241   local_got_refcounts[r_symndx] += 1;
2242
2243   return TRUE;
2244 }
2245 /* Look through the relocs for a section during the first phase.  */
2246
2247 static bfd_boolean
2248 microblaze_elf_check_relocs (bfd * abfd,
2249                              struct bfd_link_info * info,
2250                              asection * sec,
2251                              const Elf_Internal_Rela * relocs)
2252 {
2253   Elf_Internal_Shdr *           symtab_hdr;
2254   struct elf_link_hash_entry ** sym_hashes;
2255   struct elf_link_hash_entry ** sym_hashes_end;
2256   const Elf_Internal_Rela *     rel;
2257   const Elf_Internal_Rela *     rel_end;
2258   struct elf32_mb_link_hash_table *htab;
2259   asection *sreloc = NULL;
2260
2261   if (bfd_link_relocatable (info))
2262     return TRUE;
2263
2264   htab = elf32_mb_hash_table (info);
2265   if (htab == NULL)
2266     return FALSE;
2267
2268   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2269   sym_hashes = elf_sym_hashes (abfd);
2270   sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
2271   if (!elf_bad_symtab (abfd))
2272     sym_hashes_end -= symtab_hdr->sh_info;
2273
2274   rel_end = relocs + sec->reloc_count;
2275
2276   for (rel = relocs; rel < rel_end; rel++)
2277     {
2278       unsigned int r_type;
2279       struct elf_link_hash_entry * h;
2280       unsigned long r_symndx;
2281       unsigned char tls_type = 0;
2282
2283       r_symndx = ELF32_R_SYM (rel->r_info);
2284       r_type = ELF32_R_TYPE (rel->r_info);
2285
2286       if (r_symndx < symtab_hdr->sh_info)
2287         h = NULL;
2288       else
2289         {
2290           h = sym_hashes [r_symndx - symtab_hdr->sh_info];
2291           while (h->root.type == bfd_link_hash_indirect
2292                  || h->root.type == bfd_link_hash_warning)
2293             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2294         }
2295
2296       switch (r_type)
2297         {
2298           /* This relocation describes the C++ object vtable hierarchy.
2299              Reconstruct it for later use during GC.  */
2300         case R_MICROBLAZE_GNU_VTINHERIT:
2301           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2302             return FALSE;
2303           break;
2304
2305           /* This relocation describes which C++ vtable entries are actually
2306              used.  Record for later use during GC.  */
2307         case R_MICROBLAZE_GNU_VTENTRY:
2308           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2309             return FALSE;
2310           break;
2311
2312           /* This relocation requires .plt entry.  */
2313         case R_MICROBLAZE_PLT_64:
2314           if (h != NULL)
2315             {
2316               h->needs_plt = 1;
2317               h->plt.refcount += 1;
2318             }
2319           break;
2320
2321           /* This relocation requires .got entry.  */
2322         case R_MICROBLAZE_TLSGD:
2323           tls_type |= (TLS_TLS | TLS_GD);
2324           goto dogottls;
2325         case R_MICROBLAZE_TLSLD:
2326           tls_type |= (TLS_TLS | TLS_LD);
2327           /* Fall through.  */
2328         dogottls:
2329           sec->has_tls_reloc = 1;
2330           /* Fall through.  */
2331         case R_MICROBLAZE_GOT_64:
2332           if (htab->elf.sgot == NULL)
2333             {
2334               if (htab->elf.dynobj == NULL)
2335                 htab->elf.dynobj = abfd;
2336               if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2337                 return FALSE;
2338             }
2339           if (h != NULL)
2340             {
2341               h->got.refcount += 1;
2342               elf32_mb_hash_entry (h)->tls_mask |= tls_type;
2343             }
2344           else
2345             {
2346               if (! update_local_sym_info(abfd, symtab_hdr, r_symndx, tls_type) )
2347                 return FALSE;
2348             }
2349           break;
2350
2351         case R_MICROBLAZE_GOTOFF_64:
2352         case R_MICROBLAZE_GOTOFF_32:
2353           if (htab->elf.sgot == NULL)
2354             {
2355               if (htab->elf.dynobj == NULL)
2356                 htab->elf.dynobj = abfd;
2357               if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2358                 return FALSE;
2359             }
2360           break;
2361
2362         case R_MICROBLAZE_64:
2363         case R_MICROBLAZE_64_PCREL:
2364         case R_MICROBLAZE_32:
2365           {
2366             if (h != NULL && !bfd_link_pic (info))
2367               {
2368                 /* we may need a copy reloc.  */
2369                 h->non_got_ref = 1;
2370
2371                 /* we may also need a .plt entry.  */
2372                 h->plt.refcount += 1;
2373                 if (ELF32_R_TYPE (rel->r_info) != R_MICROBLAZE_64_PCREL)
2374                   h->pointer_equality_needed = 1;
2375               }
2376
2377
2378             /* If we are creating a shared library, and this is a reloc
2379                against a global symbol, or a non PC relative reloc
2380                against a local symbol, then we need to copy the reloc
2381                into the shared library.  However, if we are linking with
2382                -Bsymbolic, we do not need to copy a reloc against a
2383                global symbol which is defined in an object we are
2384                including in the link (i.e., DEF_REGULAR is set).  At
2385                this point we have not seen all the input files, so it is
2386                possible that DEF_REGULAR is not set now but will be set
2387                later (it is never cleared).  In case of a weak definition,
2388                DEF_REGULAR may be cleared later by a strong definition in
2389                a shared library.  We account for that possibility below by
2390                storing information in the relocs_copied field of the hash
2391                table entry.  A similar situation occurs when creating
2392                shared libraries and symbol visibility changes render the
2393                symbol local.
2394
2395                If on the other hand, we are creating an executable, we
2396                may need to keep relocations for symbols satisfied by a
2397                dynamic library if we manage to avoid copy relocs for the
2398                symbol.  */
2399
2400             if ((bfd_link_pic (info)
2401                  && (sec->flags & SEC_ALLOC) != 0
2402                  && (r_type != R_MICROBLAZE_64_PCREL
2403                      || (h != NULL
2404                          && (! info->symbolic
2405                              || h->root.type == bfd_link_hash_defweak
2406                              || !h->def_regular))))
2407                 || (!bfd_link_pic (info)
2408                     && (sec->flags & SEC_ALLOC) != 0
2409                     && h != NULL
2410                     && (h->root.type == bfd_link_hash_defweak
2411                         || !h->def_regular)))
2412               {
2413                 struct elf_dyn_relocs *p;
2414                 struct elf_dyn_relocs **head;
2415
2416                 /* When creating a shared object, we must copy these
2417                    relocs into the output file.  We create a reloc
2418                    section in dynobj and make room for the reloc.  */
2419
2420                 if (sreloc == NULL)
2421                   {
2422                     bfd *dynobj;
2423
2424                     if (htab->elf.dynobj == NULL)
2425                       htab->elf.dynobj = abfd;
2426                     dynobj = htab->elf.dynobj;
2427
2428                     sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
2429                                                                   2, abfd, 1);
2430                     if (sreloc == NULL)
2431                       return FALSE;
2432                   }
2433
2434                 /* If this is a global symbol, we count the number of
2435                    relocations we need for this symbol.  */
2436                 if (h != NULL)
2437                   head = &((struct elf32_mb_link_hash_entry *) h)->dyn_relocs;
2438                 else
2439                   {
2440                     /* Track dynamic relocs needed for local syms too.
2441                        We really need local syms available to do this
2442                        easily.  Oh well.  */
2443
2444                     asection *s;
2445                     Elf_Internal_Sym *isym;
2446                     void *vpp;
2447
2448                     isym = bfd_sym_from_r_symndx (&htab->sym_sec,
2449                                                   abfd, r_symndx);
2450                     if (isym == NULL)
2451                       return FALSE;
2452
2453                     s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2454                     if (s == NULL)
2455                       return FALSE;
2456
2457                     vpp = &elf_section_data (s)->local_dynrel;
2458                     head = (struct elf_dyn_relocs **) vpp;
2459                   }
2460
2461                 p = *head;
2462                 if (p == NULL || p->sec != sec)
2463                   {
2464                     bfd_size_type amt = sizeof *p;
2465                     p = ((struct elf_dyn_relocs *)
2466                          bfd_alloc (htab->elf.dynobj, amt));
2467                     if (p == NULL)
2468                       return FALSE;
2469                     p->next = *head;
2470                     *head = p;
2471                     p->sec = sec;
2472                     p->count = 0;
2473                     p->pc_count = 0;
2474                   }
2475
2476                 p->count += 1;
2477                 if (r_type == R_MICROBLAZE_64_PCREL)
2478                   p->pc_count += 1;
2479               }
2480           }
2481           break;
2482         }
2483     }
2484
2485   return TRUE;
2486 }
2487
2488 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2489
2490 static void
2491 microblaze_elf_copy_indirect_symbol (struct bfd_link_info *info,
2492                                      struct elf_link_hash_entry *dir,
2493                                      struct elf_link_hash_entry *ind)
2494 {
2495   struct elf32_mb_link_hash_entry *edir, *eind;
2496
2497   edir = (struct elf32_mb_link_hash_entry *) dir;
2498   eind = (struct elf32_mb_link_hash_entry *) ind;
2499
2500   if (eind->dyn_relocs != NULL)
2501     {
2502       if (edir->dyn_relocs != NULL)
2503         {
2504           struct elf_dyn_relocs **pp;
2505           struct elf_dyn_relocs *p;
2506
2507           if (ind->root.type == bfd_link_hash_indirect)
2508             abort ();
2509
2510           /* Add reloc counts against the weak sym to the strong sym
2511              list.  Merge any entries against the same section.  */
2512           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2513             {
2514               struct elf_dyn_relocs *q;
2515
2516               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2517                 if (q->sec == p->sec)
2518                   {
2519                     q->pc_count += p->pc_count;
2520                     q->count += p->count;
2521                     *pp = p->next;
2522                     break;
2523                   }
2524               if (q == NULL)
2525                 pp = &p->next;
2526             }
2527           *pp = edir->dyn_relocs;
2528         }
2529
2530       edir->dyn_relocs = eind->dyn_relocs;
2531       eind->dyn_relocs = NULL;
2532     }
2533
2534   edir->tls_mask |= eind->tls_mask;
2535
2536   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2537 }
2538
2539 /* Find dynamic relocs for H that apply to read-only sections.  */
2540
2541 static asection *
2542 readonly_dynrelocs (struct elf_link_hash_entry *h)
2543 {
2544   struct elf_dyn_relocs *p;
2545
2546   for (p = elf32_mb_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2547     {
2548       asection *s = p->sec->output_section;
2549
2550       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2551         return p->sec;
2552     }
2553   return NULL;
2554 }
2555
2556 static bfd_boolean
2557 microblaze_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2558                                       struct elf_link_hash_entry *h)
2559 {
2560   struct elf32_mb_link_hash_table *htab;
2561   asection *s, *srel;
2562   unsigned int power_of_two;
2563
2564   htab = elf32_mb_hash_table (info);
2565   if (htab == NULL)
2566     return FALSE;
2567
2568   /* If this is a function, put it in the procedure linkage table.  We
2569      will fill in the contents of the procedure linkage table later,
2570      when we know the address of the .got section.  */
2571   if (h->type == STT_FUNC
2572       || h->needs_plt)
2573     {
2574       if (h->plt.refcount <= 0
2575           || SYMBOL_CALLS_LOCAL (info, h)
2576           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2577               && h->root.type == bfd_link_hash_undefweak))
2578         {
2579           /* This case can occur if we saw a PLT reloc in an input
2580              file, but the symbol was never referred to by a dynamic
2581              object, or if all references were garbage collected.  In
2582              such a case, we don't actually need to build a procedure
2583              linkage table, and we can just do a PC32 reloc instead.  */
2584           h->plt.offset = (bfd_vma) -1;
2585           h->needs_plt = 0;
2586         }
2587
2588       return TRUE;
2589     }
2590   else
2591     /* It's possible that we incorrectly decided a .plt reloc was
2592        needed for an R_MICROBLAZE_64_PCREL reloc to a non-function sym in
2593        check_relocs.  We can't decide accurately between function and
2594        non-function syms in check-relocs;  Objects loaded later in
2595        the link may change h->type.  So fix it now.  */
2596     h->plt.offset = (bfd_vma) -1;
2597
2598   /* If this is a weak symbol, and there is a real definition, the
2599      processor independent code will have arranged for us to see the
2600      real definition first, and we can just use the same value.  */
2601   if (h->is_weakalias)
2602     {
2603       struct elf_link_hash_entry *def = weakdef (h);
2604       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2605       h->root.u.def.section = def->root.u.def.section;
2606       h->root.u.def.value = def->root.u.def.value;
2607       return TRUE;
2608     }
2609
2610   /* This is a reference to a symbol defined by a dynamic object which
2611      is not a function.  */
2612
2613   /* If we are creating a shared library, we must presume that the
2614      only references to the symbol are via the global offset table.
2615      For such cases we need not do anything here; the relocations will
2616      be handled correctly by relocate_section.  */
2617   if (bfd_link_pic (info))
2618     return TRUE;
2619
2620   /* If there are no references to this symbol that do not use the
2621      GOT, we don't need to generate a copy reloc.  */
2622   if (!h->non_got_ref)
2623     return TRUE;
2624
2625   /* If -z nocopyreloc was given, we won't generate them either.  */
2626   if (info->nocopyreloc)
2627     {
2628       h->non_got_ref = 0;
2629       return TRUE;
2630     }
2631
2632   /* If we don't find any dynamic relocs in read-only sections, then
2633      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
2634   if (!readonly_dynrelocs (h))
2635     {
2636       h->non_got_ref = 0;
2637       return TRUE;
2638     }
2639
2640   /* We must allocate the symbol in our .dynbss section, which will
2641      become part of the .bss section of the executable.  There will be
2642      an entry for this symbol in the .dynsym section.  The dynamic
2643      object will contain position independent code, so all references
2644      from the dynamic object to this symbol will go through the global
2645      offset table.  The dynamic linker will use the .dynsym entry to
2646      determine the address it must put in the global offset table, so
2647      both the dynamic object and the regular object will refer to the
2648      same memory location for the variable.  */
2649
2650   /* We must generate a R_MICROBLAZE_COPY reloc to tell the dynamic linker
2651      to copy the initial value out of the dynamic object and into the
2652      runtime process image.  */
2653   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2654     {
2655       s = htab->elf.sdynrelro;
2656       srel = htab->elf.sreldynrelro;
2657     }
2658   else
2659     {
2660       s = htab->elf.sdynbss;
2661       srel = htab->elf.srelbss;
2662     }
2663   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2664     {
2665       srel->size += sizeof (Elf32_External_Rela);
2666       h->needs_copy = 1;
2667     }
2668
2669   /* We need to figure out the alignment required for this symbol.  I
2670      have no idea how ELF linkers handle this.  */
2671   power_of_two = bfd_log2 (h->size);
2672   if (power_of_two > 3)
2673     power_of_two = 3;
2674
2675   /* Apply the required alignment.  */
2676   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2677   if (power_of_two > s->alignment_power)
2678     {
2679       if (!bfd_set_section_alignment (s->owner, s, power_of_two))
2680         return FALSE;
2681     }
2682
2683   /* Define the symbol as being at this point in the section.  */
2684   h->root.u.def.section = s;
2685   h->root.u.def.value = s->size;
2686
2687   /* Increment the section size to make room for the symbol.  */
2688   s->size += h->size;
2689   return TRUE;
2690 }
2691
2692 /* Allocate space in .plt, .got and associated reloc sections for
2693    dynamic relocs.  */
2694
2695 static bfd_boolean
2696 allocate_dynrelocs (struct elf_link_hash_entry *h, void * dat)
2697 {
2698   struct bfd_link_info *info;
2699   struct elf32_mb_link_hash_table *htab;
2700   struct elf32_mb_link_hash_entry *eh;
2701   struct elf_dyn_relocs *p;
2702
2703   if (h->root.type == bfd_link_hash_indirect)
2704     return TRUE;
2705
2706   info = (struct bfd_link_info *) dat;
2707   htab = elf32_mb_hash_table (info);
2708   if (htab == NULL)
2709     return FALSE;
2710
2711   if (htab->elf.dynamic_sections_created
2712       && h->plt.refcount > 0)
2713     {
2714       /* Make sure this symbol is output as a dynamic symbol.
2715          Undefined weak syms won't yet be marked as dynamic.  */
2716       if (h->dynindx == -1
2717           && !h->forced_local)
2718         {
2719           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2720             return FALSE;
2721         }
2722
2723       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
2724         {
2725           asection *s = htab->elf.splt;
2726
2727           /* The first entry in .plt is reserved.  */
2728           if (s->size == 0)
2729             s->size = PLT_ENTRY_SIZE;
2730
2731           h->plt.offset = s->size;
2732
2733           /* If this symbol is not defined in a regular file, and we are
2734              not generating a shared library, then set the symbol to this
2735              location in the .plt.  This is required to make function
2736              pointers compare as equal between the normal executable and
2737              the shared library.  */
2738           if (! bfd_link_pic (info)
2739               && !h->def_regular)
2740             {
2741               h->root.u.def.section = s;
2742               h->root.u.def.value = h->plt.offset;
2743             }
2744
2745           /* Make room for this entry.  */
2746           s->size += PLT_ENTRY_SIZE;
2747
2748           /* We also need to make an entry in the .got.plt section, which
2749              will be placed in the .got section by the linker script.  */
2750           htab->elf.sgotplt->size += 4;
2751
2752           /* We also need to make an entry in the .rel.plt section.  */
2753           htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
2754         }
2755       else
2756         {
2757           h->plt.offset = (bfd_vma) -1;
2758           h->needs_plt = 0;
2759         }
2760     }
2761   else
2762     {
2763       h->plt.offset = (bfd_vma) -1;
2764       h->needs_plt = 0;
2765     }
2766
2767   eh = (struct elf32_mb_link_hash_entry *) h;
2768   if (h->got.refcount > 0)
2769     {
2770       unsigned int need;
2771       asection *s;
2772
2773       /* Make sure this symbol is output as a dynamic symbol.
2774          Undefined weak syms won't yet be marked as dynamic.  */
2775       if (h->dynindx == -1
2776           && !h->forced_local)
2777         {
2778           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2779             return FALSE;
2780         }
2781
2782       need = 0;
2783       if ((eh->tls_mask & TLS_TLS) != 0)
2784         {
2785           /* Handle TLS Symbol */
2786           if ((eh->tls_mask & TLS_LD) != 0)
2787             {
2788               if (!eh->elf.def_dynamic)
2789                 /* We'll just use htab->tlsld_got.offset.  This should
2790                    always be the case.  It's a little odd if we have
2791                    a local dynamic reloc against a non-local symbol.  */
2792                 htab->tlsld_got.refcount += 1;
2793               else
2794                 need += 8;
2795             }
2796           if ((eh->tls_mask & TLS_GD) != 0)
2797             need += 8;
2798         }
2799       else
2800         {
2801           /* Regular (non-TLS) symbol */
2802           need += 4;
2803         }
2804       if (need == 0)
2805         {
2806           h->got.offset = (bfd_vma) -1;
2807         }
2808       else
2809         {
2810           s = htab->elf.sgot;
2811           h->got.offset = s->size;
2812           s->size += need;
2813           htab->elf.srelgot->size += need * (sizeof (Elf32_External_Rela) / 4);
2814         }
2815     }
2816   else
2817     h->got.offset = (bfd_vma) -1;
2818
2819   if (eh->dyn_relocs == NULL)
2820     return TRUE;
2821
2822   /* In the shared -Bsymbolic case, discard space allocated for
2823      dynamic pc-relative relocs against symbols which turn out to be
2824      defined in regular objects.  For the normal shared case, discard
2825      space for pc-relative relocs that have become local due to symbol
2826      visibility changes.  */
2827
2828   if (bfd_link_pic (info))
2829     {
2830       if (h->def_regular
2831           && (h->forced_local
2832               || info->symbolic))
2833         {
2834           struct elf_dyn_relocs **pp;
2835
2836           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2837             {
2838               p->count -= p->pc_count;
2839               p->pc_count = 0;
2840               if (p->count == 0)
2841                 *pp = p->next;
2842               else
2843                 pp = &p->next;
2844             }
2845         }
2846       else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
2847         eh->dyn_relocs = NULL;
2848     }
2849   else
2850     {
2851       /* For the non-shared case, discard space for relocs against
2852          symbols which turn out to need copy relocs or are not
2853          dynamic.  */
2854
2855       if (!h->non_got_ref
2856           && ((h->def_dynamic
2857                && !h->def_regular)
2858               || (htab->elf.dynamic_sections_created
2859                   && (h->root.type == bfd_link_hash_undefweak
2860                       || h->root.type == bfd_link_hash_undefined))))
2861         {
2862           /* Make sure this symbol is output as a dynamic symbol.
2863              Undefined weak syms won't yet be marked as dynamic.  */
2864           if (h->dynindx == -1
2865               && !h->forced_local)
2866             {
2867               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2868                 return FALSE;
2869             }
2870
2871           /* If that succeeded, we know we'll be keeping all the
2872              relocs.  */
2873           if (h->dynindx != -1)
2874             goto keep;
2875         }
2876
2877       eh->dyn_relocs = NULL;
2878
2879     keep: ;
2880     }
2881
2882   /* Finally, allocate space.  */
2883   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2884     {
2885       asection *sreloc = elf_section_data (p->sec)->sreloc;
2886       sreloc->size += p->count * sizeof (Elf32_External_Rela);
2887     }
2888
2889   return TRUE;
2890 }
2891
2892 /* Set the sizes of the dynamic sections.  */
2893
2894 static bfd_boolean
2895 microblaze_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2896                                       struct bfd_link_info *info)
2897 {
2898   struct elf32_mb_link_hash_table *htab;
2899   bfd *dynobj;
2900   asection *s;
2901   bfd *ibfd;
2902
2903   htab = elf32_mb_hash_table (info);
2904   if (htab == NULL)
2905     return FALSE;
2906
2907   dynobj = htab->elf.dynobj;
2908   BFD_ASSERT (dynobj != NULL);
2909
2910   /* Set up .got offsets for local syms, and space for local dynamic
2911      relocs.  */
2912   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2913     {
2914       bfd_signed_vma *local_got;
2915       bfd_signed_vma *end_local_got;
2916       bfd_size_type locsymcount;
2917       Elf_Internal_Shdr *symtab_hdr;
2918       unsigned char *lgot_masks;
2919       asection *srel;
2920
2921       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2922         continue;
2923
2924       for (s = ibfd->sections; s != NULL; s = s->next)
2925         {
2926           struct elf_dyn_relocs *p;
2927
2928           for (p = ((struct elf_dyn_relocs *)
2929                     elf_section_data (s)->local_dynrel);
2930                p != NULL;
2931                p = p->next)
2932             {
2933               if (!bfd_is_abs_section (p->sec)
2934                   && bfd_is_abs_section (p->sec->output_section))
2935                 {
2936                   /* Input section has been discarded, either because
2937                      it is a copy of a linkonce section or due to
2938                      linker script /DISCARD/, so we'll be discarding
2939                      the relocs too.  */
2940                 }
2941               else if (p->count != 0)
2942                 {
2943                   srel = elf_section_data (p->sec)->sreloc;
2944                   srel->size += p->count * sizeof (Elf32_External_Rela);
2945                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2946                     info->flags |= DF_TEXTREL;
2947                 }
2948             }
2949         }
2950
2951       local_got = elf_local_got_refcounts (ibfd);
2952       if (!local_got)
2953         continue;
2954
2955       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2956       locsymcount = symtab_hdr->sh_info;
2957       end_local_got = local_got + locsymcount;
2958       lgot_masks = (unsigned char *) end_local_got;
2959       s = htab->elf.sgot;
2960       srel = htab->elf.srelgot;
2961
2962       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
2963         {
2964           if (*local_got > 0)
2965             {
2966               unsigned int need = 0;
2967               if ((*lgot_masks & TLS_TLS) != 0)
2968                 {
2969                   if ((*lgot_masks & TLS_GD) != 0)
2970                     need += 8;
2971                   if ((*lgot_masks & TLS_LD) != 0)
2972                     htab->tlsld_got.refcount += 1;
2973                 }
2974               else
2975                 need += 4;
2976
2977               if (need == 0)
2978                 {
2979                   *local_got = (bfd_vma) -1;
2980                 }
2981               else
2982                 {
2983                   *local_got = s->size;
2984                   s->size += need;
2985                   if (bfd_link_pic (info))
2986                     srel->size += need * (sizeof (Elf32_External_Rela) / 4);
2987                 }
2988             }
2989           else
2990             *local_got = (bfd_vma) -1;
2991         }
2992     }
2993
2994   /* Allocate global sym .plt and .got entries, and space for global
2995      sym dynamic relocs.  */
2996   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
2997
2998   if (htab->tlsld_got.refcount > 0)
2999     {
3000       htab->tlsld_got.offset = htab->elf.sgot->size;
3001       htab->elf.sgot->size += 8;
3002       if (bfd_link_pic (info))
3003         htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
3004     }
3005   else
3006     htab->tlsld_got.offset = (bfd_vma) -1;
3007
3008   if (elf_hash_table (info)->dynamic_sections_created)
3009     {
3010       /* Make space for the trailing nop in .plt.  */
3011       if (htab->elf.splt->size > 0)
3012         htab->elf.splt->size += 4;
3013     }
3014
3015   /* The check_relocs and adjust_dynamic_symbol entry points have
3016      determined the sizes of the various dynamic sections.  Allocate
3017      memory for them.  */
3018   for (s = dynobj->sections; s != NULL; s = s->next)
3019     {
3020       const char *name;
3021       bfd_boolean strip = FALSE;
3022
3023       if ((s->flags & SEC_LINKER_CREATED) == 0)
3024         continue;
3025
3026       /* It's OK to base decisions on the section name, because none
3027          of the dynobj section names depend upon the input files.  */
3028       name = bfd_get_section_name (dynobj, s);
3029
3030       if (strncmp (name, ".rela", 5) == 0)
3031         {
3032           if (s->size == 0)
3033             {
3034               /* If we don't need this section, strip it from the
3035                  output file.  This is to handle .rela.bss and
3036                  .rela.plt.  We must create it in
3037                  create_dynamic_sections, because it must be created
3038                  before the linker maps input sections to output
3039                  sections.  The linker does that before
3040                  adjust_dynamic_symbol is called, and it is that
3041                  function which decides whether anything needs to go
3042                  into these sections.  */
3043               strip = TRUE;
3044             }
3045           else
3046             {
3047               /* We use the reloc_count field as a counter if we need
3048                  to copy relocs into the output file.  */
3049               s->reloc_count = 0;
3050             }
3051         }
3052       else if (s != htab->elf.splt
3053                && s != htab->elf.sgot
3054                && s != htab->elf.sgotplt
3055                && s != htab->elf.sdynbss
3056                && s != htab->elf.sdynrelro)
3057         {
3058           /* It's not one of our sections, so don't allocate space.  */
3059           continue;
3060         }
3061
3062       if (strip)
3063         {
3064           s->flags |= SEC_EXCLUDE;
3065           continue;
3066         }
3067
3068       /* Allocate memory for the section contents.  */
3069       /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3070          Unused entries should be reclaimed before the section's contents
3071          are written out, but at the moment this does not happen.  Thus in
3072          order to prevent writing out garbage, we initialise the section's
3073          contents to zero.  */
3074       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3075       if (s->contents == NULL && s->size != 0)
3076         return FALSE;
3077     }
3078
3079   if (elf_hash_table (info)->dynamic_sections_created)
3080     {
3081       /* Add some entries to the .dynamic section.  We fill in the
3082          values later, in microblaze_elf_finish_dynamic_sections, but we
3083          must add the entries now so that we get the correct size for
3084          the .dynamic section.  The DT_DEBUG entry is filled in by the
3085          dynamic linker and used by the debugger.  */
3086 #define add_dynamic_entry(TAG, VAL)                     \
3087       _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3088
3089       if (bfd_link_executable (info))
3090         {
3091           if (!add_dynamic_entry (DT_DEBUG, 0))
3092             return FALSE;
3093         }
3094
3095       if (!add_dynamic_entry (DT_RELA, 0)
3096           || !add_dynamic_entry (DT_RELASZ, 0)
3097           || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3098         return FALSE;
3099
3100       if (htab->elf.splt->size != 0)
3101         {
3102           if (!add_dynamic_entry (DT_PLTGOT, 0)
3103               || !add_dynamic_entry (DT_PLTRELSZ, 0)
3104               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3105               || !add_dynamic_entry (DT_JMPREL, 0)
3106               || !add_dynamic_entry (DT_BIND_NOW, 1))
3107             return FALSE;
3108         }
3109
3110       if (info->flags & DF_TEXTREL)
3111         {
3112           if (!add_dynamic_entry (DT_TEXTREL, 0))
3113             return FALSE;
3114         }
3115     }
3116 #undef add_dynamic_entry
3117   return TRUE;
3118 }
3119
3120 /* Finish up dynamic symbol handling.  We set the contents of various
3121    dynamic sections here.  */
3122
3123 static bfd_boolean
3124 microblaze_elf_finish_dynamic_symbol (bfd *output_bfd,
3125                                       struct bfd_link_info *info,
3126                                       struct elf_link_hash_entry *h,
3127                                       Elf_Internal_Sym *sym)
3128 {
3129   struct elf32_mb_link_hash_table *htab;
3130   struct elf32_mb_link_hash_entry *eh = elf32_mb_hash_entry(h);
3131
3132   htab = elf32_mb_hash_table (info);
3133   if (htab == NULL)
3134     return FALSE;
3135
3136   if (h->plt.offset != (bfd_vma) -1)
3137     {
3138       asection *splt;
3139       asection *srela;
3140       asection *sgotplt;
3141       Elf_Internal_Rela rela;
3142       bfd_byte *loc;
3143       bfd_vma plt_index;
3144       bfd_vma got_offset;
3145       bfd_vma got_addr;
3146
3147       /* This symbol has an entry in the procedure linkage table.  Set
3148          it up.  */
3149       BFD_ASSERT (h->dynindx != -1);
3150
3151       splt = htab->elf.splt;
3152       srela = htab->elf.srelplt;
3153       sgotplt = htab->elf.sgotplt;
3154       BFD_ASSERT (splt != NULL && srela != NULL && sgotplt != NULL);
3155
3156       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; /* first entry reserved.  */
3157       got_offset = (plt_index + 3) * 4; /* 3 reserved ???  */
3158       got_addr = got_offset;
3159
3160       /* For non-PIC objects we need absolute address of the GOT entry.  */
3161       if (!bfd_link_pic (info))
3162         got_addr += sgotplt->output_section->vma + sgotplt->output_offset;
3163
3164       /* Fill in the entry in the procedure linkage table.  */
3165       bfd_put_32 (output_bfd, PLT_ENTRY_WORD_0 + ((got_addr >> 16) & 0xffff),
3166                   splt->contents + h->plt.offset);
3167       if (bfd_link_pic (info))
3168         bfd_put_32 (output_bfd, PLT_ENTRY_WORD_1 + (got_addr & 0xffff),
3169                     splt->contents + h->plt.offset + 4);
3170       else
3171         bfd_put_32 (output_bfd, PLT_ENTRY_WORD_1_NOPIC + (got_addr & 0xffff),
3172                     splt->contents + h->plt.offset + 4);
3173       bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD_2,
3174                   splt->contents + h->plt.offset + 8);
3175       bfd_put_32 (output_bfd, (bfd_vma) PLT_ENTRY_WORD_3,
3176                   splt->contents + h->plt.offset + 12);
3177
3178       /* Any additions to the .got section??? */
3179       /*      bfd_put_32 (output_bfd,
3180               splt->output_section->vma + splt->output_offset + h->plt.offset + 4,
3181               sgotplt->contents + got_offset); */
3182
3183       /* Fill in the entry in the .rela.plt section.  */
3184       rela.r_offset = (sgotplt->output_section->vma
3185                        + sgotplt->output_offset
3186                        + got_offset);
3187       rela.r_info = ELF32_R_INFO (h->dynindx, R_MICROBLAZE_JUMP_SLOT);
3188       rela.r_addend = 0;
3189       loc = srela->contents;
3190       loc += plt_index * sizeof (Elf32_External_Rela);
3191       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3192
3193       if (!h->def_regular)
3194         {
3195           /* Mark the symbol as undefined, rather than as defined in
3196              the .plt section.  Zero the value.  */
3197           sym->st_shndx = SHN_UNDEF;
3198           sym->st_value = 0;
3199         }
3200     }
3201
3202   /* h->got.refcount to be checked ? */
3203   if (h->got.offset != (bfd_vma) -1 &&
3204       ! ((h->got.offset & 1) ||
3205           IS_TLS_LD(eh->tls_mask) || IS_TLS_GD(eh->tls_mask)))
3206     {
3207       asection *sgot;
3208       asection *srela;
3209       bfd_vma offset;
3210
3211       /* This symbol has an entry in the global offset table.  Set it
3212          up.  */
3213
3214       sgot = htab->elf.sgot;
3215       srela = htab->elf.srelgot;
3216       BFD_ASSERT (sgot != NULL && srela != NULL);
3217
3218       offset = (sgot->output_section->vma + sgot->output_offset
3219                 + (h->got.offset &~ (bfd_vma) 1));
3220
3221       /* If this is a -Bsymbolic link, and the symbol is defined
3222          locally, we just want to emit a RELATIVE reloc.  Likewise if
3223          the symbol was forced to be local because of a version file.
3224          The entry in the global offset table will already have been
3225          initialized in the relocate_section function.  */
3226       if (bfd_link_pic (info)
3227           && ((info->symbolic && h->def_regular)
3228               || h->dynindx == -1))
3229         {
3230           asection *sec = h->root.u.def.section;
3231           bfd_vma value;
3232
3233           value = h->root.u.def.value;
3234           if (sec->output_section != NULL)
3235             /* PR 21180: If the output section is NULL, then the symbol is no
3236                longer needed, and in theory the GOT entry is redundant.  But
3237                it is too late to change our minds now...  */
3238             value += sec->output_section->vma + sec->output_offset;
3239
3240           microblaze_elf_output_dynamic_relocation (output_bfd,
3241                                                     srela, srela->reloc_count++,
3242                                                     /* symindex= */ 0,
3243                                                     R_MICROBLAZE_REL, offset,
3244                                                     value);
3245         }
3246       else
3247         {
3248           microblaze_elf_output_dynamic_relocation (output_bfd,
3249                                                     srela, srela->reloc_count++,
3250                                                     h->dynindx,
3251                                                     R_MICROBLAZE_GLOB_DAT,
3252                                                     offset, 0);
3253         }
3254
3255       bfd_put_32 (output_bfd, (bfd_vma) 0,
3256                   sgot->contents + (h->got.offset &~ (bfd_vma) 1));
3257     }
3258
3259   if (h->needs_copy)
3260     {
3261       asection *s;
3262       Elf_Internal_Rela rela;
3263       bfd_byte *loc;
3264
3265       /* This symbols needs a copy reloc.  Set it up.  */
3266
3267       BFD_ASSERT (h->dynindx != -1);
3268
3269       rela.r_offset = (h->root.u.def.value
3270                        + h->root.u.def.section->output_section->vma
3271                        + h->root.u.def.section->output_offset);
3272       rela.r_info = ELF32_R_INFO (h->dynindx, R_MICROBLAZE_COPY);
3273       rela.r_addend = 0;
3274       if (h->root.u.def.section == htab->elf.sdynrelro)
3275         s = htab->elf.sreldynrelro;
3276       else
3277         s = htab->elf.srelbss;
3278       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3279       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3280     }
3281
3282   /* Mark some specially defined symbols as absolute.  */
3283   if (h == htab->elf.hdynamic
3284       || h == htab->elf.hgot
3285       || h == htab->elf.hplt)
3286     sym->st_shndx = SHN_ABS;
3287
3288   return TRUE;
3289 }
3290
3291
3292 /* Finish up the dynamic sections.  */
3293
3294 static bfd_boolean
3295 microblaze_elf_finish_dynamic_sections (bfd *output_bfd,
3296                                         struct bfd_link_info *info)
3297 {
3298   bfd *dynobj;
3299   asection *sdyn, *sgot;
3300   struct elf32_mb_link_hash_table *htab;
3301
3302   htab = elf32_mb_hash_table (info);
3303   if (htab == NULL)
3304     return FALSE;
3305
3306   dynobj = htab->elf.dynobj;
3307
3308   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3309
3310   if (htab->elf.dynamic_sections_created)
3311     {
3312       asection *splt;
3313       Elf32_External_Dyn *dyncon, *dynconend;
3314
3315       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3316       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3317       for (; dyncon < dynconend; dyncon++)
3318         {
3319           Elf_Internal_Dyn dyn;
3320           asection *s;
3321           bfd_boolean size;
3322
3323           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3324
3325           switch (dyn.d_tag)
3326             {
3327             case DT_PLTGOT:
3328               s = htab->elf.sgotplt;
3329               size = FALSE;
3330               break;
3331
3332             case DT_PLTRELSZ:
3333               s = htab->elf.srelplt;
3334               size = TRUE;
3335               break;
3336
3337             case DT_JMPREL:
3338               s = htab->elf.srelplt;
3339               size = FALSE;
3340               break;
3341
3342             default:
3343               continue;
3344             }
3345
3346           if (s == NULL)
3347             dyn.d_un.d_val = 0;
3348           else
3349             {
3350               if (!size)
3351                 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3352               else
3353                 dyn.d_un.d_val = s->size;
3354             }
3355           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3356         }
3357
3358       splt = htab->elf.splt;
3359       BFD_ASSERT (splt != NULL && sdyn != NULL);
3360
3361       /* Clear the first entry in the procedure linkage table,
3362          and put a nop in the last four bytes.  */
3363       if (splt->size > 0)
3364         {
3365           memset (splt->contents, 0, PLT_ENTRY_SIZE);
3366           bfd_put_32 (output_bfd, (bfd_vma) 0x80000000 /* nop.  */,
3367                       splt->contents + splt->size - 4);
3368         }
3369
3370       elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
3371     }
3372
3373   /* Set the first entry in the global offset table to the address of
3374      the dynamic section.  */
3375   sgot = htab->elf.sgotplt;
3376   if (sgot && sgot->size > 0)
3377     {
3378       if (sdyn == NULL)
3379         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
3380       else
3381         bfd_put_32 (output_bfd,
3382                     sdyn->output_section->vma + sdyn->output_offset,
3383                     sgot->contents);
3384       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3385     }
3386
3387   if (htab->elf.sgot && htab->elf.sgot->size > 0)
3388     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
3389
3390   return TRUE;
3391 }
3392
3393 /* Hook called by the linker routine which adds symbols from an object
3394    file.  We use it to put .comm items in .sbss, and not .bss.  */
3395
3396 static bfd_boolean
3397 microblaze_elf_add_symbol_hook (bfd *abfd,
3398                                 struct bfd_link_info *info,
3399                                 Elf_Internal_Sym *sym,
3400                                 const char **namep ATTRIBUTE_UNUSED,
3401                                 flagword *flagsp ATTRIBUTE_UNUSED,
3402                                 asection **secp,
3403                                 bfd_vma *valp)
3404 {
3405   if (sym->st_shndx == SHN_COMMON
3406       && !bfd_link_relocatable (info)
3407       && sym->st_size <= elf_gp_size (abfd))
3408     {
3409       /* Common symbols less than or equal to -G nn bytes are automatically
3410          put into .sbss.  */
3411       *secp = bfd_make_section_old_way (abfd, ".sbss");
3412       if (*secp == NULL
3413           || ! bfd_set_section_flags (abfd, *secp, SEC_IS_COMMON))
3414         return FALSE;
3415
3416       *valp = sym->st_size;
3417     }
3418
3419   return TRUE;
3420 }
3421
3422 #define TARGET_LITTLE_SYM      microblaze_elf32_le_vec
3423 #define TARGET_LITTLE_NAME     "elf32-microblazeel"
3424
3425 #define TARGET_BIG_SYM          microblaze_elf32_vec
3426 #define TARGET_BIG_NAME         "elf32-microblaze"
3427
3428 #define ELF_ARCH                bfd_arch_microblaze
3429 #define ELF_TARGET_ID           MICROBLAZE_ELF_DATA
3430 #define ELF_MACHINE_CODE        EM_MICROBLAZE
3431 #define ELF_MACHINE_ALT1        EM_MICROBLAZE_OLD
3432 #define ELF_MAXPAGESIZE         0x1000
3433 #define elf_info_to_howto       microblaze_elf_info_to_howto
3434 #define elf_info_to_howto_rel   NULL
3435
3436 #define bfd_elf32_bfd_reloc_type_lookup         microblaze_elf_reloc_type_lookup
3437 #define bfd_elf32_bfd_is_local_label_name       microblaze_elf_is_local_label_name
3438 #define elf_backend_relocate_section            microblaze_elf_relocate_section
3439 #define bfd_elf32_bfd_relax_section             microblaze_elf_relax_section
3440 #define bfd_elf32_bfd_merge_private_bfd_data    _bfd_generic_verify_endian_match
3441 #define bfd_elf32_bfd_reloc_name_lookup         microblaze_elf_reloc_name_lookup
3442
3443 #define elf_backend_gc_mark_hook                microblaze_elf_gc_mark_hook
3444 #define elf_backend_check_relocs                microblaze_elf_check_relocs
3445 #define elf_backend_copy_indirect_symbol        microblaze_elf_copy_indirect_symbol
3446 #define bfd_elf32_bfd_link_hash_table_create    microblaze_elf_link_hash_table_create
3447 #define elf_backend_can_gc_sections             1
3448 #define elf_backend_can_refcount                1
3449 #define elf_backend_want_got_plt                1
3450 #define elf_backend_plt_readonly                1
3451 #define elf_backend_got_header_size             12
3452 #define elf_backend_want_dynrelro               1
3453 #define elf_backend_rela_normal                 1
3454 #define elf_backend_dtrel_excludes_plt          1
3455
3456 #define elf_backend_adjust_dynamic_symbol       microblaze_elf_adjust_dynamic_symbol
3457 #define elf_backend_create_dynamic_sections     _bfd_elf_create_dynamic_sections
3458 #define elf_backend_finish_dynamic_sections     microblaze_elf_finish_dynamic_sections
3459 #define elf_backend_finish_dynamic_symbol       microblaze_elf_finish_dynamic_symbol
3460 #define elf_backend_size_dynamic_sections       microblaze_elf_size_dynamic_sections
3461 #define elf_backend_add_symbol_hook             microblaze_elf_add_symbol_hook
3462
3463 #include "elf32-target.h"