OSDN Git Service

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