Add c-format tags to translatable strings with more than one argument-using formattin...
[external/binutils.git] / bfd / elf32-metag.c
1 /* Meta support for 32-bit ELF
2    Copyright (C) 2013-2016 Free Software Foundation, Inc.
3    Contributed by Imagination Technologies Ltd.
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 Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf32-metag.h"
27 #include "elf/metag.h"
28
29 #define GOT_ENTRY_SIZE 4
30 #define ELF_DYNAMIC_INTERPRETER "/lib/ld-uClibc.so.0"
31
32 /* ABI version:
33     0 - original
34     1 - with GOT offset */
35 #define METAG_ELF_ABI_VERSION 1
36
37 static const unsigned int plt0_entry[] =
38   {
39     0x02000005, /* MOVT D0Re0, #HI(GOT+4) */
40     0x02000000, /* ADD  D0Re0, D0Re0, #LO(GOT+4) */
41     0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
42     0xc600012a, /* GETD PC, [D0Re0+#4] */
43     0xa0fffffe  /* NOP */
44   };
45
46 static const unsigned int plt0_pic_entry[] =
47   {
48     0x82900001, /* ADDT A0.2, CPC0, #0 */
49     0x82100000, /* ADD  A0.2, A0.2, #0 */
50     0xa3100c20, /* MOV  D0Re0, A0.2 */
51     0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */
52     0xc600012a, /* GETD PC, [D0Re0+#4] */
53   };
54
55 static const unsigned int plt_entry[] =
56   {
57     0x82100005, /* MOVT A0.2, #HI(GOT+off) */
58     0x82100000, /* ADD  A0.2, A0.2, #LO(GOT+off) */
59     0xc600806a, /* GETD PC, [A0.2] */
60     0x03000004, /* MOV  D1Re0, #LO(offset) */
61     0xa0000000  /* B    PLT0 */
62   };
63
64 static const unsigned int plt_pic_entry[] =
65   {
66     0x82900001, /* ADDT A0.2, CPC0, #HI(GOT+off) */
67     0x82100000, /* ADD  A0.2, A0.2, #LO(GOT+off) */
68     0xc600806a, /* GETD PC, [A0.2] */
69     0x03000004, /* MOV  D1Re0, #LO(offset) */
70     0xa0000000  /* B    PLT0 */
71   };
72
73 /* Variable names follow a coding style.
74    Please follow this (Apps Hungarian) style:
75
76    Structure/Variable              Prefix
77    elf_link_hash_table             "etab"
78    elf_link_hash_entry             "eh"
79
80    elf_metag_link_hash_table       "htab"
81    elf_metag_link_hash_entry       "hh"
82
83    bfd_link_hash_table             "btab"
84    bfd_link_hash_entry             "bh"
85
86    bfd_hash_table containing stubs "bstab"
87    elf_metag_stub_hash_entry       "hsh"
88
89    elf_metag_dyn_reloc_entry       "hdh"
90
91    Always remember to use GNU Coding Style.  */
92
93 #define PLT_ENTRY_SIZE sizeof(plt_entry)
94
95 static reloc_howto_type elf_metag_howto_table[] =
96 {
97   /* High order 16 bit absolute.  */
98   HOWTO (R_METAG_HIADDR16,      /* type */
99          16,                    /* rightshift */
100          2,                     /* size (0 = byte, 1 = short, 2 = long) */
101          16,                    /* bitsize */
102          FALSE,                 /* pc_relative */
103          3,                     /* bitpos */
104          complain_overflow_dont, /* complain_on_overflow */
105          bfd_elf_generic_reloc, /* special_function */
106          "R_METAG_HIADDR16",    /* name */
107          FALSE,                 /* partial_inplace */
108          0,                     /* src_mask */
109          0x0007fff8,            /* dst_mask */
110          FALSE),                /* pcrel_offset */
111
112   /* Low order 16 bit absolute.  */
113   HOWTO (R_METAG_LOADDR16,      /* type */
114          0,                     /* rightshift */
115          2,                     /* size (0 = byte, 1 = short, 2 = long) */
116          16,                    /* bitsize */
117          FALSE,                 /* pc_relative */
118          3,                     /* bitpos */
119          complain_overflow_dont,/* complain_on_overflow */
120          bfd_elf_generic_reloc, /* special_function */
121          "R_METAG_LOADDR16",    /* name */
122          FALSE,                 /* partial_inplace */
123          0,                     /* src_mask */
124          0x0007fff8,            /* dst_mask */
125          FALSE),                /* pcrel_offset */
126
127   /* 32 bit absolute.  */
128   HOWTO (R_METAG_ADDR32,        /* type */
129          0,                     /* rightshift */
130          2,                     /* size (0 = byte, 1 = short, 2 = long) */
131          32,                    /* bitsize */
132          FALSE,                 /* pc_relative */
133          0,                     /* bitpos */
134          complain_overflow_bitfield, /* complain_on_overflow */
135          bfd_elf_generic_reloc, /* special_function */
136          "R_METAG_ADDR32",      /* name */
137          FALSE,                 /* partial_inplace */
138          0x00000000,            /* src_mask */
139          0xffffffff,            /* dst_mask */
140          FALSE),                /* pcrel_offset */
141
142   /* No relocation.  */
143   HOWTO (R_METAG_NONE,          /* type */
144          0,                     /* rightshift */
145          3,                     /* size (0 = byte, 1 = short, 2 = long) */
146          0,                     /* bitsize */
147          FALSE,                 /* pc_relative */
148          0,                     /* bitpos */
149          complain_overflow_dont, /* complain_on_overflow */
150          bfd_elf_generic_reloc, /* special_function */
151          "R_METAG_NONE",        /* name */
152          FALSE,                 /* partial_inplace */
153          0,                     /* src_mask */
154          0,                     /* dst_mask */
155          FALSE),                /* pcrel_offset */
156
157   /* 19 bit pc relative */
158   HOWTO (R_METAG_RELBRANCH,     /* type */
159          2,                     /* rightshift */
160          2,                     /* size (0 = byte, 1 = short, 2 = long) */
161          19,                    /* bitsize */
162          TRUE,                  /* pc_relative */
163          5,                     /* bitpos */
164          complain_overflow_signed, /* complain_on_overflow */
165          bfd_elf_generic_reloc, /* special_function */
166          "R_METAG_RELBRANCH",   /* name */
167          FALSE,                 /* partial_inplace */
168          0,                     /* src_mask */
169          0x00ffffe0,            /* dst_mask */
170          FALSE),                /* pcrel_offset */
171
172   /* GET/SET offset */
173   HOWTO (R_METAG_GETSETOFF,     /* type */
174          0,                     /* rightshift */
175          1,                     /* size (0 = byte, 1 = short, 2 = long) */
176          12,                    /* bitsize */
177          FALSE,                 /* pc_relative */
178          7,                     /* bitpos */
179          complain_overflow_dont, /* complain_on_overflow */
180          bfd_elf_generic_reloc, /* special_function */
181          "R_METAG_GETSETOFF",   /* name */
182          FALSE,                 /* partial_inplace */
183          0,                     /* src_mask */
184          0,                     /* dst_mask */
185          FALSE),                /* pcrel_offset */
186
187   EMPTY_HOWTO (6),
188   EMPTY_HOWTO (7),
189   EMPTY_HOWTO (8),
190   EMPTY_HOWTO (9),
191   EMPTY_HOWTO (10),
192   EMPTY_HOWTO (11),
193   EMPTY_HOWTO (12),
194   EMPTY_HOWTO (13),
195   EMPTY_HOWTO (14),
196   EMPTY_HOWTO (15),
197   EMPTY_HOWTO (16),
198   EMPTY_HOWTO (17),
199   EMPTY_HOWTO (18),
200   EMPTY_HOWTO (19),
201   EMPTY_HOWTO (20),
202   EMPTY_HOWTO (21),
203   EMPTY_HOWTO (22),
204   EMPTY_HOWTO (23),
205   EMPTY_HOWTO (24),
206   EMPTY_HOWTO (25),
207   EMPTY_HOWTO (26),
208   EMPTY_HOWTO (27),
209   EMPTY_HOWTO (28),
210   EMPTY_HOWTO (29),
211
212   HOWTO (R_METAG_GNU_VTINHERIT, /* type */
213          0,                     /* rightshift */
214          2,                     /* size (0 = byte, 1 = short, 2 = long) */
215          0,                     /* bitsize */
216          FALSE,         /* pc_relative */
217          0,                     /* bitpos */
218          complain_overflow_dont, /* complain_on_overflow */
219          NULL,                  /* special_function */
220          "R_METAG_GNU_VTINHERIT", /* name */
221          FALSE,         /* partial_inplace */
222          0,                     /* src_mask */
223          0,                     /* dst_mask */
224          FALSE),                /* pcrel_offset */
225
226   HOWTO (R_METAG_GNU_VTENTRY,   /* type */
227          0,                     /* rightshift */
228          2,                     /* size (0 = byte, 1 = short, 2 = long) */
229          0,                     /* bitsize */
230          FALSE,                 /* pc_relative */
231          0,                     /* bitpos */
232          complain_overflow_dont, /* complain_on_overflow */
233          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
234          "R_METAG_GNU_VTENTRY",  /* name */
235          FALSE,                 /* partial_inplace */
236          0,                     /* src_mask */
237          0,                     /* dst_mask */
238          FALSE),                /* pcrel_offset */
239
240   /* High order 16 bit GOT offset */
241   HOWTO (R_METAG_HI16_GOTOFF,   /* type */
242          16,                    /* rightshift */
243          2,                     /* size (0 = byte, 1 = short, 2 = long) */
244          16,                    /* bitsize */
245          FALSE,                 /* pc_relative */
246          3,                     /* bitpos */
247          complain_overflow_dont, /* complain_on_overflow */
248          bfd_elf_generic_reloc, /* special_function */
249          "R_METAG_HI16_GOTOFF", /* name */
250          FALSE,                 /* partial_inplace */
251          0,                     /* src_mask */
252          0x0007fff8,            /* dst_mask */
253          FALSE),                /* pcrel_offset */
254
255   /* Low order 16 bit GOT offset */
256   HOWTO (R_METAG_LO16_GOTOFF,   /* type */
257          0,                     /* rightshift */
258          2,                     /* size (0 = byte, 1 = short, 2 = long) */
259          16,                    /* bitsize */
260          FALSE,                 /* pc_relative */
261          3,                     /* bitpos */
262          complain_overflow_dont, /* complain_on_overflow */
263          bfd_elf_generic_reloc, /* special_function */
264          "R_METAG_LO16_GOTOFF", /* name */
265          FALSE,                 /* partial_inplace */
266          0,                     /* src_mask */
267          0x0007fff8,            /* dst_mask */
268          FALSE),                /* pcrel_offset */
269
270   /* GET/SET GOT offset */
271   HOWTO (R_METAG_GETSET_GOTOFF, /* type */
272          0,                     /* rightshift */
273          1,                     /* size (0 = byte, 1 = short, 2 = long) */
274          12,                    /* bitsize */
275          FALSE,                 /* pc_relative */
276          7,                     /* bitpos */
277          complain_overflow_dont, /* complain_on_overflow */
278          bfd_elf_generic_reloc, /* special_function */
279          "R_METAG_GETSET_GOTOFF", /* name */
280          FALSE,                 /* partial_inplace */
281          0,                     /* src_mask */
282          0,                     /* dst_mask */
283          FALSE),                /* pcrel_offset */
284
285   /* GET/SET GOT relative */
286   HOWTO (R_METAG_GETSET_GOT,    /* type */
287          0,                     /* rightshift */
288          1,                     /* size (0 = byte, 1 = short, 2 = long) */
289          12,                    /* bitsize */
290          FALSE,                 /* pc_relative */
291          7,                     /* bitpos */
292          complain_overflow_dont, /* complain_on_overflow */
293          bfd_elf_generic_reloc, /* special_function */
294          "R_METAG_GETSET_GOT",  /* name */
295          FALSE,                 /* partial_inplace */
296          0,                     /* src_mask */
297          0,                     /* dst_mask */
298          FALSE),                /* pcrel_offset */
299
300   /* High order 16 bit GOT reference */
301   HOWTO (R_METAG_HI16_GOTPC,    /* type */
302          16,                    /* rightshift */
303          2,                     /* size (0 = byte, 1 = short, 2 = long) */
304          16,                    /* bitsize */
305          FALSE,                 /* pc_relative */
306          3,                     /* bitpos */
307          complain_overflow_dont, /* complain_on_overflow */
308          bfd_elf_generic_reloc, /* special_function */
309          "R_METAG_HI16_GOTPC",  /* name */
310          FALSE,                 /* partial_inplace */
311          0,                     /* src_mask */
312          0x0007fff8,            /* dst_mask */
313          FALSE),                /* pcrel_offset */
314
315   /* Low order 16 bit GOT reference */
316   HOWTO (R_METAG_LO16_GOTPC,    /* type */
317          0,                     /* rightshift */
318          2,                     /* size (0 = byte, 1 = short, 2 = long) */
319          16,                    /* bitsize */
320          FALSE,                 /* pc_relative */
321          3,                     /* bitpos */
322          complain_overflow_dont, /* complain_on_overflow */
323          bfd_elf_generic_reloc, /* special_function */
324          "R_METAG_LO16_GOTPC",  /* name */
325          FALSE,                 /* partial_inplace */
326          0,                     /* src_mask */
327          0x0007fff8,            /* dst_mask */
328          FALSE),                /* pcrel_offset */
329
330   /* High order 16 bit PLT */
331   HOWTO (R_METAG_HI16_PLT,      /* type */
332          16,                    /* rightshift */
333          2,                     /* size (0 = byte, 1 = short, 2 = long) */
334          16,                    /* bitsize */
335          FALSE,                 /* pc_relative */
336          3,                     /* bitpos */
337          complain_overflow_dont, /* complain_on_overflow */
338          bfd_elf_generic_reloc, /* special_function */
339          "R_METAG_HI16_PLT",    /* name */
340          FALSE,                 /* partial_inplace */
341          0,                     /* src_mask */
342          0x0007fff8,            /* dst_mask */
343          FALSE),                /* pcrel_offset */
344
345   /* Low order 16 bit PLT */
346   HOWTO (R_METAG_LO16_PLT,      /* type */
347          0,                     /* rightshift */
348          2,                     /* size (0 = byte, 1 = short, 2 = long) */
349          16,                    /* bitsize */
350          FALSE,                 /* pc_relative */
351          3,                     /* bitpos */
352          complain_overflow_dont, /* complain_on_overflow */
353          bfd_elf_generic_reloc, /* special_function */
354          "R_METAG_LO16_PLT",    /* name */
355          FALSE,                 /* partial_inplace */
356          0,                     /* src_mask */
357          0xffffffff,            /* dst_mask */
358          FALSE),                /* pcrel_offset */
359
360   HOWTO (R_METAG_RELBRANCH_PLT, /* type */
361          2,                     /* rightshift */
362          2,                     /* size (0 = byte, 1 = short, 2 = long) */
363          19,                    /* bitsize */
364          TRUE,                  /* pc_relative */
365          5,                     /* bitpos */
366          complain_overflow_signed, /* complain_on_overflow */
367          bfd_elf_generic_reloc, /* special_function */
368          "R_METAG_RELBRANCH_PLT", /* name */
369          FALSE,                 /* partial_inplace */
370          0,                     /* src_mask */
371          0x00ffffe0,            /* dst_mask */
372          FALSE),                /* pcrel_offset */
373
374   /* Dummy relocs used by the linker internally.  */
375   HOWTO (R_METAG_GOTOFF,        /* type */
376          0,                     /* rightshift */
377          2,                     /* size (0 = byte, 1 = short, 2 = long) */
378          32,                    /* bitsize */
379          FALSE,                 /* pc_relative */
380          0,                     /* bitpos */
381          complain_overflow_bitfield, /* complain_on_overflow */
382          bfd_elf_generic_reloc, /* special_function */
383          "R_METAG_GOTOFF",      /* name */
384          FALSE,                 /* partial_inplace */
385          0xffffffff,            /* src_mask */
386          0xffffffff,            /* dst_mask */
387          FALSE),                /* pcrel_offset */
388
389   HOWTO (R_METAG_PLT,           /* type */
390          0,                     /* rightshift */
391          2,                     /* size (0 = byte, 1 = short, 2 = long) */
392          32,                    /* bitsize */
393          FALSE,                 /* pc_relative */
394          0,                     /* bitpos */
395          complain_overflow_bitfield, /* complain_on_overflow */
396          bfd_elf_generic_reloc, /* special_function */
397          "R_METAG_GOTOFF",      /* name */
398          FALSE,                 /* partial_inplace */
399          0xffffffff,            /* src_mask */
400          0xffffffff,            /* dst_mask */
401          FALSE),                /* pcrel_offset */
402
403   /* This is used only by the dynamic linker.  The symbol should exist
404      both in the object being run and in some shared library.  The
405      dynamic linker copies the data addressed by the symbol from the
406      shared library into the object, because the object being
407      run has to have the data at some particular address.  */
408   HOWTO (R_METAG_COPY,          /* type */
409          0,                     /* rightshift */
410          2,                     /* size (0 = byte, 1 = short, 2 = long) */
411          32,                    /* bitsize */
412          FALSE,                 /* pc_relative */
413          0,                     /* bitpos */
414          complain_overflow_bitfield, /* complain_on_overflow */
415          bfd_elf_generic_reloc, /* special_function */
416          "R_METAG_COPY",        /* name */
417          FALSE,                 /* partial_inplace */
418          0xffffffff,            /* src_mask */
419          0xffffffff,            /* dst_mask */
420          FALSE),                /* pcrel_offset */
421
422   /* Marks a procedure linkage table entry for a symbol.  */
423   HOWTO (R_METAG_JMP_SLOT,      /* type */
424          0,                     /* rightshift */
425          2,                     /* size (0 = byte, 1 = short, 2 = long) */
426          32,                    /* bitsize */
427          FALSE,                 /* pc_relative */
428          0,                     /* bitpos */
429          complain_overflow_bitfield, /* complain_on_overflow */
430          bfd_elf_generic_reloc, /* special_function */
431          "R_METAG_JMP_SLOT",    /* name */
432          FALSE,                 /* partial_inplace */
433          0xffffffff,            /* src_mask */
434          0xffffffff,            /* dst_mask */
435          FALSE),                /* pcrel_offset */
436
437   /* Used only by the dynamic linker.  When the object is run, this
438      longword is set to the load address of the object, plus the
439      addend.  */
440   HOWTO (R_METAG_RELATIVE,      /* type */
441          0,                     /* rightshift */
442          2,                     /* size (0 = byte, 1 = short, 2 = long) */
443          32,                    /* bitsize */
444          FALSE,                 /* pc_relative */
445          0,                     /* bitpos */
446          complain_overflow_bitfield, /* complain_on_overflow */
447          bfd_elf_generic_reloc, /* special_function */
448          "R_METAG_RELATIVE",    /* name */
449          FALSE,                 /* partial_inplace */
450          0xffffffff,            /* src_mask */
451          0xffffffff,            /* dst_mask */
452          FALSE),                /* pcrel_offset */
453
454   HOWTO (R_METAG_GLOB_DAT,      /* type */
455          0,                     /* rightshift */
456          2,                     /* size (0 = byte, 1 = short, 2 = long) */
457          32,                    /* bitsize */
458          FALSE,                 /* pc_relative */
459          0,                     /* bitpos */
460          complain_overflow_bitfield, /* complain_on_overflow */
461          bfd_elf_generic_reloc, /* special_function */
462          "R_METAG_GLOB_DAT",    /* name */
463          FALSE,                 /* partial_inplace */
464          0xffffffff,            /* src_mask */
465          0xffffffff,            /* dst_mask */
466          FALSE),                /* pcrel_offset */
467
468   HOWTO (R_METAG_TLS_GD,        /* type */
469          0,                     /* rightshift */
470          2,                     /* size (0 = byte, 1 = short, 2 = long) */
471          16,                    /* bitsize */
472          FALSE,                 /* pc_relative */
473          3,                     /* bitpos */
474          complain_overflow_dont, /* complain_on_overflow */
475          bfd_elf_generic_reloc, /* special_function */
476          "R_METAG_TLS_GD",      /* name */
477          FALSE,                 /* partial_inplace */
478          0,                     /* src_mask */
479          0x0007fff8,            /* dst_mask */
480          FALSE),                /* pcrel_offset */
481
482   HOWTO (R_METAG_TLS_LDM,       /* type */
483          0,                     /* rightshift */
484          2,                     /* size (0 = byte, 1 = short, 2 = long) */
485          16,                    /* bitsize */
486          FALSE,                 /* pc_relative */
487          3,                     /* bitpos */
488          complain_overflow_bitfield, /* complain_on_overflow */
489          bfd_elf_generic_reloc, /* special_function */
490          "R_METAG_TLS_LDM",     /* name */
491          FALSE,                 /* partial_inplace */
492          0,                     /* src_mask */
493          0x0007fff8,            /* dst_mask */
494          FALSE),                /* pcrel_offset */
495
496   HOWTO (R_METAG_TLS_LDO_HI16,  /* type */
497          16,                    /* rightshift */
498          2,                     /* size (0 = byte, 1 = short, 2 = long) */
499          16,                    /* bitsize */
500          FALSE,                 /* pc_relative */
501          3,                     /* bitpos */
502          complain_overflow_bitfield, /* complain_on_overflow */
503          bfd_elf_generic_reloc, /* special_function */
504          "R_METAG_TLS_LDO_HI16", /* name */
505          FALSE,                 /* partial_inplace */
506          0,                     /* src_mask */
507          0x0007fff8,            /* dst_mask */
508          FALSE),                /* pcrel_offset */
509
510   HOWTO (R_METAG_TLS_LDO_LO16,  /* type */
511          0,                     /* rightshift */
512          2,                     /* size (0 = byte, 1 = short, 2 = long) */
513          16,                    /* bitsize */
514          FALSE,                 /* pc_relative */
515          3,                     /* bitpos */
516          complain_overflow_bitfield, /* complain_on_overflow */
517          bfd_elf_generic_reloc, /* special_function */
518          "R_METAG_TLS_LDO_LO16", /* name */
519          FALSE,                 /* partial_inplace */
520          0,                     /* src_mask */
521          0x0007fff8,            /* dst_mask */
522          FALSE),                /* pcrel_offset */
523
524   /* Dummy reloc used by the linker internally.  */
525   HOWTO (R_METAG_TLS_LDO,       /* type */
526          0,                     /* rightshift */
527          2,                     /* size (0 = byte, 1 = short, 2 = long) */
528          16,                    /* bitsize */
529          FALSE,                 /* pc_relative */
530          3,                     /* bitpos */
531          complain_overflow_bitfield, /* complain_on_overflow */
532          bfd_elf_generic_reloc, /* special_function */
533          "R_METAG_TLS_LDO",     /* name */
534          FALSE,                 /* partial_inplace */
535          0,                     /* src_mask */
536          0x0007fff8,            /* dst_mask */
537          FALSE),                /* pcrel_offset */
538
539   HOWTO (R_METAG_TLS_IE,        /* type */
540          2,                     /* rightshift */
541          2,                     /* size (0 = byte, 1 = short, 2 = long) */
542          12,                    /* bitsize */
543          FALSE,                 /* pc_relative */
544          7,                     /* bitpos */
545          complain_overflow_dont, /* complain_on_overflow */
546          bfd_elf_generic_reloc, /* special_function */
547          "R_METAG_TLS_IE",      /* name */
548          FALSE,                 /* partial_inplace */
549          0,                     /* src_mask */
550          0x0007ff80,            /* dst_mask */
551          FALSE),                /* pcrel_offset */
552
553   /* Dummy reloc used by the linker internally.  */
554   HOWTO (R_METAG_TLS_IENONPIC,  /* type */
555          0,                     /* rightshift */
556          2,                     /* size (0 = byte, 1 = short, 2 = long) */
557          16,                    /* bitsize */
558          FALSE,                 /* pc_relative */
559          3,                     /* bitpos */
560          complain_overflow_dont, /* complain_on_overflow */
561          bfd_elf_generic_reloc, /* special_function */
562          "R_METAG_TLS_IENONPIC", /* name */
563          FALSE,                 /* partial_inplace */
564          0,                     /* src_mask */
565          0x0007fff8,            /* dst_mask */
566          FALSE),                /* pcrel_offset */
567
568   HOWTO (R_METAG_TLS_IENONPIC_HI16,/* type */
569          16,                    /* rightshift */
570          2,                     /* size (0 = byte, 1 = short, 2 = long) */
571          16,                    /* bitsize */
572          FALSE,                 /* pc_relative */
573          3,                     /* bitpos */
574          complain_overflow_dont, /* complain_on_overflow */
575          bfd_elf_generic_reloc, /* special_function */
576          "R_METAG_TLS_IENONPIC_HI16", /* name */
577          FALSE,                 /* partial_inplace */
578          0,                     /* src_mask */
579          0x0007fff8,            /* dst_mask */
580          FALSE),                /* pcrel_offset */
581
582   HOWTO (R_METAG_TLS_IENONPIC_LO16,/* type */
583          0,                     /* rightshift */
584          2,                     /* size (0 = byte, 1 = short, 2 = long) */
585          16,                    /* bitsize */
586          FALSE,                 /* pc_relative */
587          3,                     /* bitpos */
588          complain_overflow_dont, /* complain_on_overflow */
589          bfd_elf_generic_reloc, /* special_function */
590          "R_METAG_TLS_IENONPIC_LO16", /* name */
591          FALSE,                 /* partial_inplace */
592          0,                     /* src_mask */
593          0x0007fff8,            /* dst_mask */
594          FALSE),                /* pcrel_offset */
595
596   HOWTO (R_METAG_TLS_TPOFF,     /* type */
597          0,                     /* rightshift */
598          2,                     /* size (0 = byte, 1 = short, 2 = long) */
599          32,                    /* bitsize */
600          FALSE,                 /* pc_relative */
601          0,                     /* bitpos */
602          complain_overflow_bitfield, /* complain_on_overflow */
603          bfd_elf_generic_reloc, /* special_function */
604          "R_METAG_TLS_TPOFF",   /* name */
605          FALSE,                 /* partial_inplace */
606          0,                     /* src_mask */
607          0xffffffff,            /* dst_mask */
608          FALSE),                /* pcrel_offset */
609
610   HOWTO (R_METAG_TLS_DTPMOD,    /* type */
611          0,                     /* rightshift */
612          2,                     /* size (0 = byte, 1 = short, 2 = long) */
613          32,                    /* bitsize */
614          FALSE,                 /* pc_relative */
615          0,                     /* bitpos */
616          complain_overflow_bitfield, /* complain_on_overflow */
617          bfd_elf_generic_reloc, /* special_function */
618          "R_METAG_TLS_DTPMOD",  /* name */
619          FALSE,                 /* partial_inplace */
620          0,                     /* src_mask */
621          0xffffffff,            /* dst_mask */
622          FALSE),                /* pcrel_offset */
623
624   HOWTO (R_METAG_TLS_DTPOFF,    /* type */
625          0,                     /* rightshift */
626          2,                     /* size (0 = byte, 1 = short, 2 = long) */
627          32,                    /* bitsize */
628          FALSE,                 /* pc_relative */
629          0,                     /* bitpos */
630          complain_overflow_bitfield, /* complain_on_overflow */
631          bfd_elf_generic_reloc, /* special_function */
632          "R_METAG_TLS_DTPOFF",  /* name */
633          FALSE,                 /* partial_inplace */
634          0,                     /* src_mask */
635          0xffffffff,            /* dst_mask */
636          FALSE),                /* pcrel_offset */
637
638   /* Dummy reloc used by the linker internally.  */
639   HOWTO (R_METAG_TLS_LE,        /* type */
640          0,                     /* rightshift */
641          2,                     /* size (0 = byte, 1 = short, 2 = long) */
642          32,                    /* bitsize */
643          FALSE,                 /* pc_relative */
644          0,                     /* bitpos */
645          complain_overflow_bitfield, /* complain_on_overflow */
646          bfd_elf_generic_reloc, /* special_function */
647          "R_METAG_TLS_LE",      /* name */
648          FALSE,                 /* partial_inplace */
649          0,                     /* src_mask */
650          0xffffffff,            /* dst_mask */
651          FALSE),                /* pcrel_offset */
652
653   HOWTO (R_METAG_TLS_LE_HI16,   /* type */
654          16,                    /* rightshift */
655          2,                     /* size (0 = byte, 1 = short, 2 = long) */
656          16,                    /* bitsize */
657          FALSE,                 /* pc_relative */
658          3,                     /* bitpos */
659          complain_overflow_dont, /* complain_on_overflow */
660          bfd_elf_generic_reloc, /* special_function */
661          "R_METAG_TLS_LE_HI16", /* name */
662          FALSE,                 /* partial_inplace */
663          0,                     /* src_mask */
664          0x0007fff8,            /* dst_mask */
665          FALSE),                /* pcrel_offset */
666
667   HOWTO (R_METAG_TLS_LE_LO16,   /* type */
668          0,                     /* rightshift */
669          2,                     /* size (0 = byte, 1 = short, 2 = long) */
670          16,                    /* bitsize */
671          FALSE,                 /* pc_relative */
672          3,                     /* bitpos */
673          complain_overflow_dont, /* complain_on_overflow */
674          bfd_elf_generic_reloc, /* special_function */
675          "R_METAG_TLS_LE_LO16", /* name */
676          FALSE,                 /* partial_inplace */
677          0,                     /* src_mask */
678          0x0007fff8,            /* dst_mask */
679          FALSE),                /* pcrel_offset */
680
681 };
682
683 #define BRANCH_BITS 19
684
685 /* The GOT is typically accessed using a [GS]ETD instruction. The size of the
686    immediate offset which can be used in such instructions therefore limits
687    the usable size of the GOT. If the base register for the [GS]ETD (A1LbP)
688    is pointing to the base of the GOT then the size is limited to the maximum
689    11 bits unsigned dword offset, or 2^13 = 0x2000 bytes. However the offset
690    in a [GS]ETD instruction is signed, so by setting the base address register
691    to an offset of that 0x2000 byte maximum unsigned offset from the base of
692    the GOT we can use negative offsets in addition to positive. This
693    effectively doubles the usable GOT size to 0x4000 bytes.  */
694 #define GOT_REG_OFFSET 0x2000
695
696 struct metag_reloc_map
697 {
698   bfd_reloc_code_real_type bfd_reloc_val;
699   unsigned int metag_reloc_val;
700 };
701
702 static const struct metag_reloc_map metag_reloc_map [] =
703   {
704     { BFD_RELOC_NONE,                R_METAG_NONE },
705     { BFD_RELOC_32,                  R_METAG_ADDR32 },
706     { BFD_RELOC_METAG_HIADDR16,      R_METAG_HIADDR16 },
707     { BFD_RELOC_METAG_LOADDR16,      R_METAG_LOADDR16 },
708     { BFD_RELOC_METAG_RELBRANCH,     R_METAG_RELBRANCH },
709     { BFD_RELOC_METAG_GETSETOFF,     R_METAG_GETSETOFF },
710     { BFD_RELOC_VTABLE_INHERIT,      R_METAG_GNU_VTINHERIT },
711     { BFD_RELOC_VTABLE_ENTRY,        R_METAG_GNU_VTENTRY },
712     { BFD_RELOC_METAG_REL8,          R_METAG_REL8 },
713     { BFD_RELOC_METAG_REL16,         R_METAG_REL16 },
714     { BFD_RELOC_METAG_HI16_GOTOFF,   R_METAG_HI16_GOTOFF },
715     { BFD_RELOC_METAG_LO16_GOTOFF,   R_METAG_LO16_GOTOFF },
716     { BFD_RELOC_METAG_GETSET_GOTOFF, R_METAG_GETSET_GOTOFF },
717     { BFD_RELOC_METAG_GETSET_GOT,    R_METAG_GETSET_GOT },
718     { BFD_RELOC_METAG_HI16_GOTPC,    R_METAG_HI16_GOTPC },
719     { BFD_RELOC_METAG_LO16_GOTPC,    R_METAG_LO16_GOTPC },
720     { BFD_RELOC_METAG_HI16_PLT,      R_METAG_HI16_PLT },
721     { BFD_RELOC_METAG_LO16_PLT,      R_METAG_LO16_PLT },
722     { BFD_RELOC_METAG_RELBRANCH_PLT, R_METAG_RELBRANCH_PLT },
723     { BFD_RELOC_METAG_GOTOFF,        R_METAG_GOTOFF },
724     { BFD_RELOC_METAG_PLT,           R_METAG_PLT },
725     { BFD_RELOC_METAG_COPY,          R_METAG_COPY },
726     { BFD_RELOC_METAG_JMP_SLOT,      R_METAG_JMP_SLOT },
727     { BFD_RELOC_METAG_RELATIVE,      R_METAG_RELATIVE },
728     { BFD_RELOC_METAG_GLOB_DAT,      R_METAG_GLOB_DAT },
729     { BFD_RELOC_METAG_TLS_GD,        R_METAG_TLS_GD },
730     { BFD_RELOC_METAG_TLS_LDM,       R_METAG_TLS_LDM },
731     { BFD_RELOC_METAG_TLS_LDO_HI16,  R_METAG_TLS_LDO_HI16 },
732     { BFD_RELOC_METAG_TLS_LDO_LO16,  R_METAG_TLS_LDO_LO16 },
733     { BFD_RELOC_METAG_TLS_LDO,       R_METAG_TLS_LDO },
734     { BFD_RELOC_METAG_TLS_IE,        R_METAG_TLS_IE },
735     { BFD_RELOC_METAG_TLS_IENONPIC,  R_METAG_TLS_IENONPIC },
736     { BFD_RELOC_METAG_TLS_IENONPIC_HI16, R_METAG_TLS_IENONPIC_HI16 },
737     { BFD_RELOC_METAG_TLS_IENONPIC_LO16, R_METAG_TLS_IENONPIC_LO16 },
738     { BFD_RELOC_METAG_TLS_TPOFF,     R_METAG_TLS_TPOFF },
739     { BFD_RELOC_METAG_TLS_DTPMOD,    R_METAG_TLS_DTPMOD },
740     { BFD_RELOC_METAG_TLS_DTPOFF,    R_METAG_TLS_DTPOFF },
741     { BFD_RELOC_METAG_TLS_LE,        R_METAG_TLS_LE },
742     { BFD_RELOC_METAG_TLS_LE_HI16,   R_METAG_TLS_LE_HI16 },
743     { BFD_RELOC_METAG_TLS_LE_LO16,   R_METAG_TLS_LE_LO16 },
744   };
745
746 enum elf_metag_stub_type
747 {
748   metag_stub_long_branch,
749   metag_stub_long_branch_shared,
750   metag_stub_none
751 };
752
753 struct elf_metag_stub_hash_entry
754 {
755   /* Base hash table entry structure.  */
756   struct bfd_hash_entry bh_root;
757
758   /* The stub section.  */
759   asection *stub_sec;
760
761   /* Offset within stub_sec of the beginning of this stub.  */
762   bfd_vma stub_offset;
763
764   /* Given the symbol's value and its section we can determine its final
765      value when building the stubs (so the stub knows where to jump.  */
766   bfd_vma target_value;
767   asection *target_section;
768
769   enum elf_metag_stub_type stub_type;
770
771   /* The symbol table entry, if any, that this was derived from.  */
772   struct elf_metag_link_hash_entry *hh;
773
774   /* And the reloc addend that this was derived from.  */
775   bfd_vma addend;
776
777   /* Where this stub is being called from, or, in the case of combined
778      stub sections, the first input section in the group.  */
779   asection *id_sec;
780 };
781
782 struct elf_metag_link_hash_entry
783 {
784   struct elf_link_hash_entry eh;
785
786   /* A pointer to the most recently used stub hash entry against this
787      symbol.  */
788   struct elf_metag_stub_hash_entry *hsh_cache;
789
790   /* Used to count relocations for delayed sizing of relocation
791      sections.  */
792   struct elf_metag_dyn_reloc_entry {
793
794     /* Next relocation in the chain.  */
795     struct elf_metag_dyn_reloc_entry *hdh_next;
796
797     /* The input section of the reloc.  */
798     asection *sec;
799
800     /* Number of relocs copied in this section.  */
801     bfd_size_type count;
802
803     /* Number of relative relocs copied for the input section.  */
804     bfd_size_type relative_count;
805   } *dyn_relocs;
806
807   enum
808     {
809       GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_IE = 2, GOT_TLS_LDM = 4, GOT_TLS_GD = 8
810     } tls_type;
811 };
812
813 struct elf_metag_link_hash_table
814 {
815   /* The main hash table.  */
816   struct elf_link_hash_table etab;
817
818   /* The stub hash table.  */
819   struct bfd_hash_table bstab;
820
821   /* Linker stub bfd.  */
822   bfd *stub_bfd;
823
824   /* Linker call-backs.  */
825   asection * (*add_stub_section) (const char *, asection *);
826   void (*layout_sections_again) (void);
827
828   /* Array to keep track of which stub sections have been created, and
829      information on stub grouping.  */
830   struct map_stub
831   {
832     /* This is the section to which stubs in the group will be
833        attached.  */
834     asection *link_sec;
835     /* The stub section.  */
836     asection *stub_sec;
837   } *stub_group;
838
839   /* Assorted information used by elf_metag_size_stubs.  */
840   unsigned int bfd_count;
841   unsigned int top_index;
842   asection **input_list;
843   Elf_Internal_Sym **all_local_syms;
844
845   /* Short-cuts to get to dynamic linker sections.  */
846   asection *sgot;
847   asection *sgotplt;
848   asection *srelgot;
849   asection *splt;
850   asection *srelplt;
851   asection *sdynbss;
852   asection *srelbss;
853
854   /* Small local sym cache.  */
855   struct sym_cache sym_cache;
856
857   /* Data for LDM relocations.  */
858   union
859   {
860     bfd_signed_vma refcount;
861     bfd_vma offset;
862   } tls_ldm_got;
863 };
864
865 /* Return the base vma address which should be subtracted from the
866    real address when resolving a dtpoff relocation.  This is PT_TLS
867    segment p_vaddr.  */
868 static bfd_vma
869 dtpoff_base (struct bfd_link_info *info)
870 {
871   /* If tls_sec is NULL, we should have signalled an error already.  */
872   if (elf_hash_table (info)->tls_sec == NULL)
873     return 0;
874   return elf_hash_table (info)->tls_sec->vma;
875 }
876
877 /* Return the relocation value for R_METAG_TLS_IE */
878 static bfd_vma
879 tpoff (struct bfd_link_info *info, bfd_vma address)
880 {
881   /* If tls_sec is NULL, we should have signalled an error already.  */
882   if (elf_hash_table (info)->tls_sec == NULL)
883     return 0;
884   /* METAG TLS ABI is variant I and static TLS blocks start just after
885      tcbhead structure which has 2 pointer fields.  */
886   return (address - elf_hash_table (info)->tls_sec->vma
887           + align_power ((bfd_vma) 8,
888                          elf_hash_table (info)->tls_sec->alignment_power));
889 }
890
891 static void
892 metag_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
893                           arelent *cache_ptr,
894                           Elf_Internal_Rela *dst)
895 {
896   unsigned int r_type;
897
898   r_type = ELF32_R_TYPE (dst->r_info);
899   if (r_type >= (unsigned int) R_METAG_MAX)
900     {
901       /* xgettext:c-format */
902       _bfd_error_handler (_("%B: invalid METAG reloc number: %d"), abfd, r_type);
903       r_type = 0;
904     }
905   cache_ptr->howto = & elf_metag_howto_table [r_type];
906 }
907
908 static reloc_howto_type *
909 metag_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
910                          bfd_reloc_code_real_type code)
911 {
912   unsigned int i;
913
914   for (i = 0; i < sizeof (metag_reloc_map) / sizeof (metag_reloc_map[0]); i++)
915     if (metag_reloc_map [i].bfd_reloc_val == code)
916       return & elf_metag_howto_table [metag_reloc_map[i].metag_reloc_val];
917
918   return NULL;
919 }
920
921 static reloc_howto_type *
922 metag_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
923                          const char *r_name)
924 {
925   unsigned int i;
926
927   for (i = 0; i < sizeof (elf_metag_howto_table) / sizeof (elf_metag_howto_table[0]); i++)
928     if (elf_metag_howto_table[i].name != NULL
929         && strcasecmp (elf_metag_howto_table[i].name, r_name) == 0)
930       return &elf_metag_howto_table[i];
931
932   return NULL;
933 }
934
935 /* Various hash macros and functions.  */
936 #define metag_link_hash_table(p) \
937   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
938   == METAG_ELF_DATA ? ((struct elf_metag_link_hash_table *) ((p)->hash)) : NULL)
939
940 #define metag_elf_hash_entry(ent) \
941   ((struct elf_metag_link_hash_entry *)(ent))
942
943 #define metag_stub_hash_entry(ent) \
944   ((struct elf_metag_stub_hash_entry *)(ent))
945
946 #define metag_stub_hash_lookup(table, string, create, copy) \
947   ((struct elf_metag_stub_hash_entry *) \
948    bfd_hash_lookup ((table), (string), (create), (copy)))
949
950 #define metag_elf_local_got_tls_type(abfd) \
951   ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info)))
952
953 /* Assorted hash table functions.  */
954
955 /* Initialize an entry in the stub hash table.  */
956
957 static struct bfd_hash_entry *
958 stub_hash_newfunc (struct bfd_hash_entry *entry,
959                    struct bfd_hash_table *table,
960                    const char *string)
961 {
962   /* Allocate the structure if it has not already been allocated by a
963      subclass.  */
964   if (entry == NULL)
965     {
966       entry = bfd_hash_allocate (table,
967                                  sizeof (struct elf_metag_stub_hash_entry));
968       if (entry == NULL)
969         return entry;
970     }
971
972   /* Call the allocation method of the superclass.  */
973   entry = bfd_hash_newfunc (entry, table, string);
974   if (entry != NULL)
975     {
976       struct elf_metag_stub_hash_entry *hsh;
977
978       /* Initialize the local fields.  */
979       hsh = (struct elf_metag_stub_hash_entry *) entry;
980       hsh->stub_sec = NULL;
981       hsh->stub_offset = 0;
982       hsh->target_value = 0;
983       hsh->target_section = NULL;
984       hsh->stub_type = metag_stub_long_branch;
985       hsh->hh = NULL;
986       hsh->id_sec = NULL;
987     }
988
989   return entry;
990 }
991
992 /* Initialize an entry in the link hash table.  */
993
994 static struct bfd_hash_entry *
995 metag_link_hash_newfunc (struct bfd_hash_entry *entry,
996                          struct bfd_hash_table *table,
997                          const char *string)
998 {
999   /* Allocate the structure if it has not already been allocated by a
1000      subclass.  */
1001   if (entry == NULL)
1002     {
1003       entry = bfd_hash_allocate (table,
1004                                  sizeof (struct elf_metag_link_hash_entry));
1005       if (entry == NULL)
1006         return entry;
1007     }
1008
1009   /* Call the allocation method of the superclass.  */
1010   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1011   if (entry != NULL)
1012     {
1013       struct elf_metag_link_hash_entry *hh;
1014
1015       /* Initialize the local fields.  */
1016       hh = (struct elf_metag_link_hash_entry *) entry;
1017       hh->hsh_cache = NULL;
1018       hh->dyn_relocs = NULL;
1019       hh->tls_type = GOT_UNKNOWN;
1020     }
1021
1022   return entry;
1023 }
1024
1025 /* Free the derived linker hash table.  */
1026
1027 static void
1028 elf_metag_link_hash_table_free (bfd *obfd)
1029 {
1030   struct elf_metag_link_hash_table *htab
1031     = (struct elf_metag_link_hash_table *) obfd->link.hash;
1032
1033   bfd_hash_table_free (&htab->bstab);
1034   _bfd_elf_link_hash_table_free (obfd);
1035 }
1036
1037 /* Create the derived linker hash table.  The Meta ELF port uses the derived
1038    hash table to keep information specific to the Meta ELF linker (without
1039    using static variables).  */
1040
1041 static struct bfd_link_hash_table *
1042 elf_metag_link_hash_table_create (bfd *abfd)
1043 {
1044   struct elf_metag_link_hash_table *htab;
1045   bfd_size_type amt = sizeof (*htab);
1046
1047   htab = bfd_zmalloc (amt);
1048   if (htab == NULL)
1049     return NULL;
1050
1051   if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
1052                                       metag_link_hash_newfunc,
1053                                       sizeof (struct elf_metag_link_hash_entry),
1054                                       METAG_ELF_DATA))
1055     {
1056       free (htab);
1057       return NULL;
1058     }
1059
1060   /* Init the stub hash table too.  */
1061   if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
1062                             sizeof (struct elf_metag_stub_hash_entry)))
1063     {
1064       _bfd_elf_link_hash_table_free (abfd);
1065       return NULL;
1066     }
1067   htab->etab.root.hash_table_free = elf_metag_link_hash_table_free;
1068
1069   return &htab->etab.root;
1070 }
1071
1072 /* Section name for stubs is the associated section name plus this
1073    string.  */
1074 #define STUB_SUFFIX ".stub"
1075
1076 /* Build a name for an entry in the stub hash table.  */
1077
1078 static char *
1079 metag_stub_name (const asection *input_section,
1080                  const asection *sym_sec,
1081                  const struct elf_metag_link_hash_entry *hh,
1082                  const Elf_Internal_Rela *rel)
1083 {
1084   char *stub_name;
1085   bfd_size_type len;
1086
1087   if (hh)
1088     {
1089       len = 8 + 1 + strlen (hh->eh.root.root.string) + 1 + 8 + 1;
1090       stub_name = bfd_malloc (len);
1091       if (stub_name != NULL)
1092         {
1093           sprintf (stub_name, "%08x_%s+%x",
1094                    input_section->id & 0xffffffff,
1095                    hh->eh.root.root.string,
1096                    (int) rel->r_addend & 0xffffffff);
1097         }
1098     }
1099   else
1100     {
1101       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1102       stub_name = bfd_malloc (len);
1103       if (stub_name != NULL)
1104         {
1105           sprintf (stub_name, "%08x_%x:%x+%x",
1106                    input_section->id & 0xffffffff,
1107                    sym_sec->id & 0xffffffff,
1108                    (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
1109                    (int) rel->r_addend & 0xffffffff);
1110         }
1111     }
1112   return stub_name;
1113 }
1114
1115 /* Look up an entry in the stub hash.  Stub entries are cached because
1116    creating the stub name takes a bit of time.  */
1117
1118 static struct elf_metag_stub_hash_entry *
1119 metag_get_stub_entry (const asection *input_section,
1120                       const asection *sym_sec,
1121                       struct elf_metag_link_hash_entry *hh,
1122                       const Elf_Internal_Rela *rel,
1123                       struct elf_metag_link_hash_table *htab)
1124 {
1125   struct elf_metag_stub_hash_entry *hsh;
1126   const asection *id_sec;
1127
1128   /* If this input section is part of a group of sections sharing one
1129      stub section, then use the id of the first section in the group.
1130      Stub names need to include a section id, as there may well be
1131      more than one stub used to reach say, printf, and we need to
1132      distinguish between them.  */
1133   id_sec = htab->stub_group[input_section->id].link_sec;
1134
1135   if (hh != NULL && hh->hsh_cache != NULL
1136       && hh->hsh_cache->hh == hh
1137       && hh->hsh_cache->id_sec == id_sec)
1138     {
1139       hsh = hh->hsh_cache;
1140     }
1141   else
1142     {
1143       char *stub_name;
1144
1145       stub_name = metag_stub_name (id_sec, sym_sec, hh, rel);
1146       if (stub_name == NULL)
1147         return NULL;
1148
1149       hsh = metag_stub_hash_lookup (&htab->bstab,
1150                                     stub_name, FALSE, FALSE);
1151
1152       if (hh != NULL)
1153         hh->hsh_cache = hsh;
1154
1155       free (stub_name);
1156     }
1157
1158   return hsh;
1159 }
1160
1161 /* Add a new stub entry to the stub hash.  Not all fields of the new
1162    stub entry are initialised.  */
1163
1164 static struct elf_metag_stub_hash_entry *
1165 metag_add_stub (const char *stub_name,
1166                 asection *section,
1167                 struct elf_metag_link_hash_table *htab)
1168 {
1169   asection *link_sec;
1170   asection *stub_sec;
1171   struct elf_metag_stub_hash_entry *hsh;
1172
1173   link_sec = htab->stub_group[section->id].link_sec;
1174   stub_sec = htab->stub_group[section->id].stub_sec;
1175   if (stub_sec == NULL)
1176     {
1177       stub_sec = htab->stub_group[link_sec->id].stub_sec;
1178       if (stub_sec == NULL)
1179         {
1180           size_t namelen;
1181           bfd_size_type len;
1182           char *s_name;
1183
1184           namelen = strlen (link_sec->name);
1185           len = namelen + sizeof (STUB_SUFFIX);
1186           s_name = bfd_alloc (htab->stub_bfd, len);
1187           if (s_name == NULL)
1188             return NULL;
1189
1190           memcpy (s_name, link_sec->name, namelen);
1191           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
1192
1193           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
1194           if (stub_sec == NULL)
1195             return NULL;
1196           htab->stub_group[link_sec->id].stub_sec = stub_sec;
1197         }
1198       htab->stub_group[section->id].stub_sec = stub_sec;
1199     }
1200
1201   /* Enter this entry into the linker stub hash table.  */
1202   hsh = metag_stub_hash_lookup (&htab->bstab, stub_name,
1203                                 TRUE, FALSE);
1204   if (hsh == NULL)
1205     {
1206       /* xgettext:c-format */
1207       _bfd_error_handler (_("%B: cannot create stub entry %s"),
1208                           section->owner, stub_name);
1209       return NULL;
1210     }
1211
1212   hsh->stub_sec = stub_sec;
1213   hsh->stub_offset = 0;
1214   hsh->id_sec = link_sec;
1215   return hsh;
1216 }
1217
1218 /* Check a signed integer value can be represented in the given number
1219    of bits.  */
1220
1221 static bfd_boolean
1222 within_signed_range (int value, unsigned int bits)
1223 {
1224   int min_val = -(1 << (bits - 1));
1225   int max_val = (1 << (bits - 1)) - 1;
1226   return (value <= max_val) && (value >= min_val);
1227 }
1228
1229 /* Perform a relocation as part of a final link.  */
1230
1231 static bfd_reloc_status_type
1232 metag_final_link_relocate (reloc_howto_type *howto,
1233                            bfd *input_bfd,
1234                            asection *input_section,
1235                            bfd_byte *contents,
1236                            Elf_Internal_Rela *rel,
1237                            bfd_vma relocation,
1238                            struct elf_metag_link_hash_entry *hh,
1239                            struct elf_metag_link_hash_table *htab,
1240                            asection *sym_sec)
1241 {
1242   bfd_reloc_status_type r = bfd_reloc_ok;
1243   bfd_byte *hit_data = contents + rel->r_offset;
1244   int opcode, op_shift, op_extended, l1, l2;
1245   bfd_signed_vma srel, addend = rel->r_addend;
1246   struct elf_metag_stub_hash_entry *hsh = NULL;
1247   bfd_vma location;
1248
1249   /* Find out where we are and where we're going.  */
1250   location = (rel->r_offset +
1251               input_section->output_offset +
1252               input_section->output_section->vma);
1253
1254   switch (howto->type)
1255     {
1256     case R_METAG_RELBRANCH:
1257     case R_METAG_RELBRANCH_PLT:
1258       /* Make it a pc relative offset.  */
1259       relocation -= location;
1260       break;
1261     case R_METAG_TLS_GD:
1262     case R_METAG_TLS_IE:
1263       relocation -= elf_gp (input_section->output_section->owner);
1264       break;
1265     default:
1266       break;
1267     }
1268
1269   switch (howto->type)
1270     {
1271     case R_METAG_RELBRANCH_PLT:
1272     case R_METAG_RELBRANCH:
1273       opcode = bfd_get_32 (input_bfd, hit_data);
1274
1275       srel = (bfd_signed_vma) relocation;
1276       srel += addend;
1277
1278       /* If the branch is out of reach, then redirect the
1279          call to the local stub for this function.  */
1280       if (srel > ((1 << (BRANCH_BITS + 1)) - 1) ||
1281           (srel < - (1 << (BRANCH_BITS + 1))))
1282         {
1283           if (sym_sec == NULL)
1284             break;
1285
1286           hsh = metag_get_stub_entry (input_section, sym_sec,
1287                                       hh, rel, htab);
1288           if (hsh == NULL)
1289             return bfd_reloc_undefined;
1290
1291           /* Munge up the value and addend so that we call the stub
1292              rather than the procedure directly.  */
1293           srel = (hsh->stub_offset
1294                   + hsh->stub_sec->output_offset
1295                   + hsh->stub_sec->output_section->vma);
1296           srel -= location;
1297         }
1298
1299       srel = srel >> 2;
1300
1301       if (!within_signed_range (srel, BRANCH_BITS))
1302         {
1303           if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1304             srel = 0;
1305           else
1306             return bfd_reloc_overflow;
1307         }
1308
1309       opcode &= ~(0x7ffff << 5);
1310       opcode |= ((srel & 0x7ffff) << 5);
1311
1312       bfd_put_32 (input_bfd, opcode, hit_data);
1313       break;
1314     case R_METAG_GETSETOFF:
1315     case R_METAG_GETSET_GOT:
1316     case R_METAG_GETSET_GOTOFF:
1317       opcode = bfd_get_32 (input_bfd, hit_data);
1318
1319       srel = (bfd_signed_vma) relocation;
1320       srel += addend;
1321
1322       /* Is this a standard or extended GET/SET?  */
1323       if ((opcode & 0xf0000000) == 0xa0000000)
1324         {
1325           /* Extended GET/SET.  */
1326           l1 = opcode & 0x2;
1327           l2 = opcode & 0x4;
1328           op_extended = 1;
1329         }
1330       else
1331         {
1332           /* Standard GET/SET.  */
1333           l1 = opcode & 0x01000000;
1334           l2 = opcode & 0x04000000;
1335           op_extended = 0;
1336         }
1337
1338       /* Calculate the width of the GET/SET and how much we need to
1339          shift the result by.  */
1340       if (l2)
1341         if (l1)
1342           op_shift = 3;
1343         else
1344           op_shift = 2;
1345       else
1346         if (l1)
1347           op_shift = 1;
1348         else
1349           op_shift = 0;
1350
1351       /* GET/SET offsets are scaled by the width of the transfer.  */
1352       srel = srel >> op_shift;
1353
1354       /* Extended GET/SET has signed 12 bits of offset, standard has
1355          signed 6 bits.  */
1356       if (op_extended)
1357         {
1358           if (!within_signed_range (srel, 12))
1359             {
1360               if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1361                 srel = 0;
1362               else
1363                 return bfd_reloc_overflow;
1364             }
1365           opcode &= ~(0xfff << 7);
1366           opcode |= ((srel & 0xfff) << 7);
1367         }
1368       else
1369         {
1370           if (!within_signed_range (srel, 5))
1371             {
1372               if (hh && hh->eh.root.type == bfd_link_hash_undefweak)
1373                 srel = 0;
1374               else
1375                 return bfd_reloc_overflow;
1376             }
1377           opcode &= ~(0x3f << 8);
1378           opcode |= ((srel & 0x3f) << 8);
1379         }
1380
1381       bfd_put_32 (input_bfd, opcode, hit_data);
1382       break;
1383     case R_METAG_TLS_GD:
1384     case R_METAG_TLS_LDM:
1385       opcode = bfd_get_32 (input_bfd, hit_data);
1386
1387       if ((bfd_signed_vma)relocation < 0)
1388        {
1389          /* sign extend immediate */
1390          if ((opcode & 0xf2000001) == 0x02000000)
1391           {
1392             /* ADD De.e,Dx.r,#I16 */
1393             /* set SE bit */
1394             opcode |= (1 << 1);
1395           } else
1396             return bfd_reloc_overflow;
1397        }
1398
1399       bfd_put_32 (input_bfd, opcode, hit_data);
1400
1401       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1402                                     contents, rel->r_offset,
1403                                     relocation, rel->r_addend);
1404       break;
1405     default:
1406       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1407                                     contents, rel->r_offset,
1408                                     relocation, rel->r_addend);
1409     }
1410
1411   return r;
1412 }
1413
1414 /* This is defined because R_METAG_NONE != 0...
1415    See RELOC_AGAINST_DISCARDED_SECTION for details.  */
1416 #define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \
1417                                               rel, relend, howto, contents) \
1418   {                                                                     \
1419     _bfd_clear_contents (howto, input_bfd, input_section,               \
1420                          contents + rel->r_offset);                     \
1421                                                                         \
1422     if (bfd_link_relocatable (info)                                     \
1423         && (input_section->flags & SEC_DEBUGGING))                      \
1424       {                                                                 \
1425         /* Only remove relocations in debug sections since other        \
1426            sections may require relocations.  */                        \
1427         Elf_Internal_Shdr *rel_hdr;                                     \
1428                                                                         \
1429         rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \
1430                                                                         \
1431         /* Avoid empty output section.  */                              \
1432         if (rel_hdr->sh_size > rel_hdr->sh_entsize)                     \
1433           {                                                             \
1434             rel_hdr->sh_size -= rel_hdr->sh_entsize;                    \
1435             rel_hdr = _bfd_elf_single_rel_hdr (input_section);          \
1436             rel_hdr->sh_size -= rel_hdr->sh_entsize;                    \
1437                                                                         \
1438             memmove (rel, rel + 1, (relend - rel) * sizeof (*rel));     \
1439                                                                         \
1440             input_section->reloc_count--;                               \
1441             relend--;                                                   \
1442             rel--;                                                      \
1443             continue;                                                   \
1444           }                                                             \
1445       }                                                                 \
1446                                                                         \
1447     rel->r_info = R_METAG_NONE;                                         \
1448     rel->r_addend = 0;                                                  \
1449     continue;                                                           \
1450   }
1451
1452 /* Relocate a META ELF section.
1453
1454 The RELOCATE_SECTION function is called by the new ELF backend linker
1455 to handle the relocations for a section.
1456
1457 The relocs are always passed as Rela structures; if the section
1458 actually uses Rel structures, the r_addend field will always be
1459 zero.
1460
1461 This function is responsible for adjusting the section contents as
1462 necessary, and (if using Rela relocs and generating a relocatable
1463 output file) adjusting the reloc addend as necessary.
1464
1465 This function does not have to worry about setting the reloc
1466 address or the reloc symbol index.
1467
1468 LOCAL_SYMS is a pointer to the swapped in local symbols.
1469
1470 LOCAL_SECTIONS is an array giving the section in the input file
1471 corresponding to the st_shndx field of each local symbol.
1472
1473 The global hash table entry for the global symbols can be found
1474 via elf_sym_hashes (input_bfd).
1475
1476 When generating relocatable output, this function must handle
1477 STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
1478 going to be the section symbol corresponding to the output
1479 section, which means that the addend must be adjusted
1480 accordingly.  */
1481
1482 static bfd_boolean
1483 elf_metag_relocate_section (bfd *output_bfd,
1484                             struct bfd_link_info *info,
1485                             bfd *input_bfd,
1486                             asection *input_section,
1487                             bfd_byte *contents,
1488                             Elf_Internal_Rela *relocs,
1489                             Elf_Internal_Sym *local_syms,
1490                             asection **local_sections)
1491 {
1492   bfd_vma *local_got_offsets;
1493   Elf_Internal_Shdr *symtab_hdr;
1494   struct elf_link_hash_entry **eh_syms;
1495   struct elf_metag_link_hash_table *htab;
1496   Elf_Internal_Rela *rel;
1497   Elf_Internal_Rela *relend;
1498   asection *sreloc;
1499
1500   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1501   eh_syms = elf_sym_hashes (input_bfd);
1502   relend = relocs + input_section->reloc_count;
1503
1504   htab = metag_link_hash_table (info);
1505   local_got_offsets = elf_local_got_offsets (input_bfd);
1506
1507   sreloc = NULL;
1508
1509   for (rel = relocs; rel < relend; rel ++)
1510     {
1511       reloc_howto_type *howto;
1512       unsigned long r_symndx;
1513       Elf_Internal_Sym *sym;
1514       asection *sec;
1515       struct elf_metag_link_hash_entry *hh;
1516       bfd_vma relocation;
1517       bfd_reloc_status_type r;
1518       const char *name;
1519       int r_type;
1520
1521       r_type = ELF32_R_TYPE (rel->r_info);
1522
1523       if (r_type == R_METAG_GNU_VTINHERIT
1524           || r_type == R_METAG_GNU_VTENTRY
1525           || r_type == R_METAG_NONE)
1526         continue;
1527
1528       r_symndx = ELF32_R_SYM (rel->r_info);
1529
1530       howto  = elf_metag_howto_table + ELF32_R_TYPE (rel->r_info);
1531       hh     = NULL;
1532       sym    = NULL;
1533       sec    = NULL;
1534
1535       if (r_symndx < symtab_hdr->sh_info)
1536         {
1537           sym = local_syms + r_symndx;
1538           sec = local_sections [r_symndx];
1539           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1540
1541           name = bfd_elf_string_from_elf_section
1542             (input_bfd, symtab_hdr->sh_link, sym->st_name);
1543           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1544         }
1545       else
1546         {
1547           struct elf_link_hash_entry *eh;
1548           bfd_boolean unresolved_reloc, warned, ignored;
1549
1550           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1551                                    r_symndx, symtab_hdr, eh_syms,
1552                                    eh, sec, relocation,
1553                                    unresolved_reloc, warned, ignored);
1554
1555           name = eh->root.root.string;
1556           hh = (struct elf_metag_link_hash_entry *) eh;
1557         }
1558
1559       if (sec != NULL && discarded_section (sec))
1560           METAG_RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1561                                                  rel, relend, howto, contents);
1562
1563       if (bfd_link_relocatable (info))
1564         continue;
1565
1566       switch (r_type)
1567         {
1568         case R_METAG_ADDR32:
1569         case R_METAG_RELBRANCH:
1570           if ((input_section->flags & SEC_ALLOC) == 0)
1571             break;
1572
1573           if ((bfd_link_pic (info)
1574                && r_symndx != STN_UNDEF
1575                && (input_section->flags & SEC_ALLOC) != 0
1576                && (r_type != R_METAG_RELBRANCH
1577                    || !SYMBOL_CALLS_LOCAL (info, &hh->eh)))
1578               || (!bfd_link_pic (info)
1579                   && hh != NULL
1580                   && hh->eh.dynindx != -1
1581                   && !hh->eh.non_got_ref
1582                   && ((hh->eh.def_dynamic
1583                        && !hh->eh.def_regular)
1584                       || hh->eh.root.type == bfd_link_hash_undefweak
1585                       || hh->eh.root.type == bfd_link_hash_undefined)))
1586             {
1587               Elf_Internal_Rela outrel;
1588               bfd_boolean skip, relocate;
1589               bfd_byte *loc;
1590
1591               /* When generating a shared object, these relocations
1592                  are copied into the output file to be resolved at run
1593                  time.  */
1594
1595               sreloc = elf_section_data (input_section)->sreloc;
1596               BFD_ASSERT (sreloc != NULL);
1597
1598               skip = FALSE;
1599               relocate = FALSE;
1600
1601               outrel.r_offset = _bfd_elf_section_offset (output_bfd,
1602                                                          info,
1603                                                          input_section,
1604                                                          rel->r_offset);
1605               if (outrel.r_offset == (bfd_vma) -1)
1606                 skip = TRUE;
1607               else if (outrel.r_offset == (bfd_vma) -2)
1608                 skip = TRUE, relocate = TRUE;
1609               outrel.r_offset += (input_section->output_section->vma
1610                                   + input_section->output_offset);
1611
1612               if (skip)
1613                 {
1614                   memset (&outrel, 0, sizeof outrel);
1615                   outrel.r_info = ELF32_R_INFO (0, R_METAG_NONE);
1616                 }
1617               else if (r_type == R_METAG_RELBRANCH)
1618                 {
1619                   BFD_ASSERT (hh != NULL && hh->eh.dynindx != -1);
1620                   outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
1621                   outrel.r_addend = rel->r_addend;
1622                 }
1623               else
1624                 {
1625                   /* h->dynindx may be -1 if this symbol was marked to
1626                      become local.  */
1627                   if (hh == NULL
1628                       || ((info->symbolic || hh->eh.dynindx == -1)
1629                           && hh->eh.def_regular))
1630                     {
1631                       relocate = TRUE;
1632                       outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
1633                       outrel.r_addend = relocation + rel->r_addend;
1634                     }
1635                   else
1636                     {
1637                       BFD_ASSERT (hh->eh.dynindx != -1);
1638                       outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
1639                       outrel.r_addend = rel->r_addend;
1640                     }
1641                 }
1642
1643               loc = sreloc->contents;
1644               loc += sreloc->reloc_count * sizeof(Elf32_External_Rela);
1645               bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
1646               ++sreloc->reloc_count;
1647
1648               /* If this reloc is against an external symbol, we do
1649                  not want to fiddle with the addend.  Otherwise, we
1650                  need to include the symbol value so that it becomes
1651                  an addend for the dynamic reloc.  */
1652               if (! relocate)
1653                 continue;
1654             }
1655           break;
1656
1657         case R_METAG_RELBRANCH_PLT:
1658           /* Relocation is to the entry for this symbol in the
1659              procedure linkage table.  */
1660
1661           if (hh == NULL)
1662             break;
1663
1664           if (hh->eh.forced_local)
1665             break;
1666
1667           if (hh->eh.plt.offset == (bfd_vma) -1 ||
1668               htab->splt == NULL)
1669             {
1670               /* We didn't make a PLT entry for this symbol.  This
1671                  happens when statically linking PIC code, or when
1672                  using -Bsymbolic.  */
1673               break;
1674             }
1675
1676           relocation = (htab->splt->output_section->vma
1677                         + htab->splt->output_offset
1678                         + hh->eh.plt.offset);
1679           break;
1680         case R_METAG_HI16_GOTPC:
1681         case R_METAG_LO16_GOTPC:
1682           BFD_ASSERT (htab->sgot != NULL);
1683
1684           relocation = (htab->sgot->output_section->vma +
1685                         htab->sgot->output_offset);
1686           relocation += GOT_REG_OFFSET;
1687           relocation -= (input_section->output_section->vma
1688                          + input_section->output_offset
1689                          + rel->r_offset);
1690           break;
1691         case R_METAG_HI16_GOTOFF:
1692         case R_METAG_LO16_GOTOFF:
1693         case R_METAG_GETSET_GOTOFF:
1694           BFD_ASSERT (htab->sgot != NULL);
1695
1696           relocation -= (htab->sgot->output_section->vma +
1697                          htab->sgot->output_offset);
1698           relocation -= GOT_REG_OFFSET;
1699           break;
1700         case R_METAG_GETSET_GOT:
1701           {
1702             bfd_vma off;
1703             bfd_boolean do_got = 0;
1704
1705             /* Relocation is to the entry for this symbol in the
1706                global offset table.  */
1707             if (hh != NULL)
1708               {
1709                 bfd_boolean dyn;
1710
1711                 off = hh->eh.got.offset;
1712                 dyn = htab->etab.dynamic_sections_created;
1713                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1714                                                        bfd_link_pic (info),
1715                                                        &hh->eh))
1716                   {
1717                     /* If we aren't going to call finish_dynamic_symbol,
1718                        then we need to handle initialisation of the .got
1719                        entry and create needed relocs here.  Since the
1720                        offset must always be a multiple of 4, we use the
1721                        least significant bit to record whether we have
1722                        initialised it already.  */
1723                     if ((off & 1) != 0)
1724                       off &= ~1;
1725                     else
1726                       {
1727                         hh->eh.got.offset |= 1;
1728                         do_got = 1;
1729                       }
1730                   }
1731               }
1732             else
1733               {
1734                 /* Local symbol case.  */
1735                 if (local_got_offsets == NULL)
1736                   abort ();
1737
1738                 off = local_got_offsets[r_symndx];
1739
1740                 /* The offset must always be a multiple of 4.  We use
1741                    the least significant bit to record whether we have
1742                    already generated the necessary reloc.  */
1743                 if ((off & 1) != 0)
1744                   off &= ~1;
1745                 else
1746                   {
1747                     local_got_offsets[r_symndx] |= 1;
1748                     do_got = 1;
1749                   }
1750               }
1751
1752             if (do_got)
1753               {
1754                 if (bfd_link_pic (info))
1755                   {
1756                     /* Output a dynamic relocation for this GOT entry.
1757                        In this case it is relative to the base of the
1758                        object because the symbol index is zero.  */
1759                     Elf_Internal_Rela outrel;
1760                     bfd_byte *loc;
1761                     asection *s = htab->srelgot;
1762
1763                     outrel.r_offset = (off
1764                                        + htab->sgot->output_offset
1765                                        + htab->sgot->output_section->vma);
1766                     outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
1767                     outrel.r_addend = relocation;
1768                     loc = s->contents;
1769                     loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1770                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1771                   }
1772                 else
1773                   bfd_put_32 (output_bfd, relocation,
1774                               htab->sgot->contents + off);
1775               }
1776
1777             if (off >= (bfd_vma) -2)
1778               abort ();
1779
1780             relocation = off - GOT_REG_OFFSET;
1781           }
1782           break;
1783         case R_METAG_TLS_GD:
1784         case R_METAG_TLS_IE:
1785           {
1786             /* XXXMJF There is room here for optimisations. For example
1787                converting from GD->IE, etc.  */
1788             bfd_vma off;
1789             int indx;
1790             char tls_type;
1791
1792             if (htab->sgot == NULL)
1793               abort();
1794
1795             indx = 0;
1796             if (hh != NULL)
1797               {
1798                 bfd_boolean dyn;
1799                 dyn = htab->etab.dynamic_sections_created;
1800
1801                 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1802                                                      bfd_link_pic (info),
1803                                                      &hh->eh)
1804                     && (!bfd_link_pic (info)
1805                         || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
1806                   {
1807                     indx = hh->eh.dynindx;
1808                   }
1809                 off = hh->eh.got.offset;
1810                 tls_type = hh->tls_type;
1811               }
1812             else
1813               {
1814                 /* Local symbol case.  */
1815                 if (local_got_offsets == NULL)
1816                   abort ();
1817
1818                 off = local_got_offsets[r_symndx];
1819                 tls_type = metag_elf_local_got_tls_type (input_bfd) [r_symndx];
1820               }
1821
1822             if (tls_type == GOT_UNKNOWN)
1823               abort();
1824
1825             if ((off & 1) != 0)
1826               off &= ~1;
1827             else
1828               {
1829                 bfd_boolean need_relocs = FALSE;
1830                 Elf_Internal_Rela outrel;
1831                 bfd_byte *loc = NULL;
1832                 int cur_off = off;
1833
1834                 /* The GOT entries have not been initialized yet.  Do it
1835                    now, and emit any relocations.  If both an IE GOT and a
1836                    GD GOT are necessary, we emit the GD first.  */
1837
1838                 if ((bfd_link_pic (info) || indx != 0)
1839                     && (hh == NULL
1840                         || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
1841                         || hh->eh.root.type != bfd_link_hash_undefweak))
1842                   {
1843                     need_relocs = TRUE;
1844                     loc = htab->srelgot->contents;
1845                     /* FIXME (CAO): Should this be reloc_count++ ? */
1846                     loc += htab->srelgot->reloc_count * sizeof (Elf32_External_Rela);
1847                   }
1848
1849                 if (tls_type & GOT_TLS_GD)
1850                   {
1851                     if (need_relocs)
1852                       {
1853                         outrel.r_offset = (cur_off
1854                                            + htab->sgot->output_section->vma
1855                                            + htab->sgot->output_offset);
1856                         outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_DTPMOD);
1857                         outrel.r_addend = 0;
1858                         bfd_put_32 (output_bfd, 0, htab->sgot->contents + cur_off);
1859
1860                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1861                         htab->srelgot->reloc_count++;
1862                         loc += sizeof (Elf32_External_Rela);
1863
1864                         if (indx == 0)
1865                           bfd_put_32 (output_bfd, 0,
1866                                       htab->sgot->contents + cur_off + 4);
1867                         else
1868                           {
1869                             bfd_put_32 (output_bfd, 0,
1870                                         htab->sgot->contents + cur_off + 4);
1871                             outrel.r_info = ELF32_R_INFO (indx,
1872                                                       R_METAG_TLS_DTPOFF);
1873                             outrel.r_offset += 4;
1874                             bfd_elf32_swap_reloca_out (output_bfd,
1875                                                        &outrel, loc);
1876                             htab->srelgot->reloc_count++;
1877                             loc += sizeof (Elf32_External_Rela);
1878                           }
1879                       }
1880                     else
1881                       {
1882                         /* We don't support changing the TLS model.  */
1883                         abort ();
1884                       }
1885
1886                     cur_off += 8;
1887                   }
1888
1889                 if (tls_type & GOT_TLS_IE)
1890                   {
1891                     if (need_relocs)
1892                       {
1893                         outrel.r_offset = (cur_off
1894                                            + htab->sgot->output_section->vma
1895                                            + htab->sgot->output_offset);
1896                         outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_TPOFF);
1897
1898                         if (indx == 0)
1899                           outrel.r_addend = relocation - dtpoff_base (info);
1900                         else
1901                           outrel.r_addend = 0;
1902
1903                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1904                         htab->srelgot->reloc_count++;
1905                         loc += sizeof (Elf32_External_Rela);
1906                       }
1907                     else
1908                       bfd_put_32 (output_bfd, tpoff (info, relocation),
1909                                   htab->sgot->contents + cur_off);
1910
1911                     cur_off += 4;
1912                   }
1913
1914                   if (hh != NULL)
1915                     hh->eh.got.offset |= 1;
1916                   else
1917                     local_got_offsets[r_symndx] |= 1;
1918               }
1919
1920             /* Add the base of the GOT to the relocation value.  */
1921             relocation = off - GOT_REG_OFFSET;
1922
1923             break;
1924           }
1925
1926         case R_METAG_TLS_IENONPIC_HI16:
1927         case R_METAG_TLS_IENONPIC_LO16:
1928         case R_METAG_TLS_LE_HI16:
1929         case R_METAG_TLS_LE_LO16:
1930           if (bfd_link_pic (info))
1931             {
1932               _bfd_error_handler
1933                 /* xgettext:c-format */
1934                 (_("%B(%A+0x%lx): R_METAG_TLS_LE/IENONPIC relocation not permitted in shared object"),
1935                  input_bfd, input_section,
1936                  (long) rel->r_offset, howto->name);
1937               return FALSE;
1938             }
1939           else
1940             relocation = tpoff (info, relocation);
1941           break;
1942         case R_METAG_TLS_LDO_HI16:
1943         case R_METAG_TLS_LDO_LO16:
1944           if (! bfd_link_pic (info))
1945             relocation = tpoff (info, relocation);
1946           else
1947             relocation -= dtpoff_base (info);
1948           break;
1949         case R_METAG_TLS_LDM:
1950           {
1951             bfd_vma off;
1952
1953             if (htab->sgot == NULL)
1954               abort();
1955             off = htab->tls_ldm_got.offset;
1956             if (off & 1)
1957               off &= ~1;
1958             else
1959               {
1960                 Elf_Internal_Rela outrel;
1961                 bfd_byte *loc;
1962
1963                 outrel.r_offset = (off
1964                                    + htab->sgot->output_section->vma
1965                                    + htab->sgot->output_offset);
1966
1967                 outrel.r_addend = 0;
1968                 outrel.r_info = ELF32_R_INFO (0, R_METAG_TLS_DTPMOD);
1969                 loc = htab->srelgot->contents;
1970                 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1971                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1972                 htab->tls_ldm_got.offset |= 1;
1973               }
1974
1975             relocation = off - GOT_REG_OFFSET;
1976             break;
1977           }
1978         default:
1979           break;
1980         }
1981
1982       r = metag_final_link_relocate (howto, input_bfd, input_section,
1983                                      contents, rel, relocation, hh, htab,
1984                                      sec);
1985
1986       if (r != bfd_reloc_ok)
1987         {
1988           const char * msg = (const char *) NULL;
1989
1990           switch (r)
1991             {
1992             case bfd_reloc_overflow:
1993               (*info->callbacks->reloc_overflow)
1994                 (info, (hh ? &hh->eh.root : NULL), name, howto->name,
1995                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1996               break;
1997
1998             case bfd_reloc_undefined:
1999               (*info->callbacks->undefined_symbol)
2000                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
2001               break;
2002
2003             case bfd_reloc_outofrange:
2004               msg = _("internal error: out of range error");
2005               break;
2006
2007             case bfd_reloc_notsupported:
2008               msg = _("internal error: unsupported relocation error");
2009               break;
2010
2011             case bfd_reloc_dangerous:
2012               msg = _("internal error: dangerous relocation");
2013               break;
2014
2015             default:
2016               msg = _("internal error: unknown error");
2017               break;
2018             }
2019
2020           if (msg)
2021             (*info->callbacks->warning) (info, msg, name, input_bfd,
2022                                          input_section, rel->r_offset);
2023         }
2024     }
2025
2026   return TRUE;
2027 }
2028
2029 /* Create the .plt and .got sections, and set up our hash table
2030    short-cuts to various dynamic sections.  */
2031
2032 static bfd_boolean
2033 elf_metag_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2034 {
2035   struct elf_metag_link_hash_table *htab;
2036   struct elf_link_hash_entry *eh;
2037   struct bfd_link_hash_entry *bh;
2038   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2039
2040   /* Don't try to create the .plt and .got twice.  */
2041   htab = metag_link_hash_table (info);
2042   if (htab->splt != NULL)
2043     return TRUE;
2044
2045   /* Call the generic code to do most of the work.  */
2046   if (! _bfd_elf_create_dynamic_sections (abfd, info))
2047     return FALSE;
2048
2049   htab->sgot = bfd_get_linker_section (abfd, ".got");
2050   if (! htab->sgot)
2051     return FALSE;
2052
2053   htab->sgotplt = bfd_make_section_with_flags (abfd, ".got.plt",
2054                                                (SEC_ALLOC | SEC_LOAD |
2055                                                 SEC_HAS_CONTENTS |
2056                                                 SEC_IN_MEMORY |
2057                                                 SEC_LINKER_CREATED));
2058   if (htab->sgotplt == NULL
2059       || !bfd_set_section_alignment (abfd, htab->sgotplt, 2))
2060     return FALSE;
2061
2062   /* Define the symbol __GLOBAL_OFFSET_TABLE__ at the start of the .got
2063      section.  We don't do this in the linker script because we don't want
2064      to define the symbol if we are not creating a global offset table.  */
2065   bh = NULL;
2066   if (!(_bfd_generic_link_add_one_symbol
2067         (info, abfd, "__GLOBAL_OFFSET_TABLE__", BSF_GLOBAL, htab->sgot,
2068          (bfd_vma) 0, NULL, FALSE, bed->collect, &bh)))
2069     return FALSE;
2070   eh = (struct elf_link_hash_entry *) bh;
2071   eh->def_regular = 1;
2072   eh->type = STT_OBJECT;
2073   eh->other = STV_HIDDEN;
2074
2075   if (! bfd_link_executable (info)
2076       && ! bfd_elf_link_record_dynamic_symbol (info, eh))
2077     return FALSE;
2078
2079   elf_hash_table (info)->hgot = eh;
2080
2081   htab->splt = bfd_get_linker_section (abfd, ".plt");
2082   htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
2083
2084   htab->srelgot = bfd_get_linker_section (abfd, ".rela.got");
2085
2086   htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
2087   htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
2088
2089   return TRUE;
2090 }
2091
2092 /* Look through the relocs for a section during the first phase, and
2093    calculate needed space in the global offset table, procedure linkage
2094    table, and dynamic reloc sections.  At this point we haven't
2095    necessarily read all the input files.  */
2096
2097 static bfd_boolean
2098 elf_metag_check_relocs (bfd *abfd,
2099                         struct bfd_link_info *info,
2100                         asection *sec,
2101                         const Elf_Internal_Rela *relocs)
2102 {
2103   Elf_Internal_Shdr *symtab_hdr;
2104   struct elf_link_hash_entry **eh_syms;
2105   const Elf_Internal_Rela *rel;
2106   const Elf_Internal_Rela *rel_end;
2107   struct elf_metag_link_hash_table *htab;
2108   asection *sreloc;
2109   bfd *dynobj;
2110   int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN;
2111
2112   if (bfd_link_relocatable (info))
2113     return TRUE;
2114
2115   htab = metag_link_hash_table (info);
2116   dynobj = htab->etab.dynobj;
2117   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2118   eh_syms = elf_sym_hashes (abfd);
2119   sreloc = NULL;
2120
2121   if (htab == NULL)
2122     return FALSE;
2123
2124   rel_end = relocs + sec->reloc_count;
2125   for (rel = relocs; rel < rel_end; rel++)
2126     {
2127       int r_type;
2128       struct elf_metag_link_hash_entry *hh;
2129       Elf_Internal_Sym *isym;
2130       unsigned long r_symndx;
2131
2132       r_symndx = ELF32_R_SYM (rel->r_info);
2133       r_type = ELF32_R_TYPE (rel->r_info);
2134       if (r_symndx < symtab_hdr->sh_info)
2135         {
2136           /* A local symbol.  */
2137           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2138                                         abfd, r_symndx);
2139           if (isym == NULL)
2140             return FALSE;
2141
2142           hh = NULL;
2143         }
2144       else
2145         {
2146           isym = NULL;
2147
2148           hh = (struct elf_metag_link_hash_entry *)
2149             eh_syms[r_symndx - symtab_hdr->sh_info];
2150           while (hh->eh.root.type == bfd_link_hash_indirect
2151                  || hh->eh.root.type == bfd_link_hash_warning)
2152             hh = (struct elf_metag_link_hash_entry *) hh->eh.root.u.i.link;
2153
2154           /* PR15323, ref flags aren't set for references in the same
2155              object.  */
2156           hh->eh.root.non_ir_ref = 1;
2157         }
2158
2159       /* Some relocs require a global offset table.  */
2160       if (htab->sgot == NULL)
2161         {
2162           switch (r_type)
2163             {
2164             case R_METAG_TLS_GD:
2165             case R_METAG_TLS_LDM:
2166             case R_METAG_TLS_IE:
2167               if (bfd_link_pic (info))
2168                 info->flags |= DF_STATIC_TLS;
2169               /* Fall through.  */
2170
2171             case R_METAG_HI16_GOTOFF:
2172             case R_METAG_LO16_GOTOFF:
2173             case R_METAG_GETSET_GOTOFF:
2174             case R_METAG_GETSET_GOT:
2175             case R_METAG_HI16_GOTPC:
2176             case R_METAG_LO16_GOTPC:
2177               if (dynobj == NULL)
2178                 htab->etab.dynobj = dynobj = abfd;
2179               if (!elf_metag_create_dynamic_sections (dynobj, info))
2180                 return FALSE;
2181               break;
2182
2183             default:
2184               break;
2185             }
2186         }
2187
2188       switch (r_type)
2189         {
2190         case R_METAG_TLS_IE:
2191         case R_METAG_TLS_GD:
2192         case R_METAG_GETSET_GOT:
2193           switch (r_type)
2194             {
2195             default:
2196               tls_type = GOT_NORMAL;
2197               break;
2198             case R_METAG_TLS_IE:
2199               tls_type = GOT_TLS_IE;
2200               break;
2201             case R_METAG_TLS_GD:
2202               tls_type = GOT_TLS_GD;
2203               break;
2204             }
2205
2206           if (hh != NULL)
2207             {
2208               hh->eh.got.refcount += 1;
2209               old_tls_type = hh->tls_type;
2210             }
2211           else
2212             {
2213               bfd_signed_vma *local_got_refcounts;
2214
2215               /* This is a global offset table entry for a local
2216                  symbol.  */
2217               local_got_refcounts = elf_local_got_refcounts (abfd);
2218               if (local_got_refcounts == NULL)
2219                 {
2220                   bfd_size_type size;
2221
2222                   size = symtab_hdr->sh_info;
2223                   size *= sizeof (bfd_signed_vma);
2224                   /* Add in space to store the local GOT TLS types.  */
2225                   size += symtab_hdr->sh_info;
2226                   local_got_refcounts = ((bfd_signed_vma *)
2227                                          bfd_zalloc (abfd, size));
2228                   if (local_got_refcounts == NULL)
2229                     return FALSE;
2230                   elf_local_got_refcounts (abfd) = local_got_refcounts;
2231                   memset (metag_elf_local_got_tls_type (abfd),
2232                           GOT_UNKNOWN, symtab_hdr->sh_info);
2233                 }
2234               local_got_refcounts[r_symndx] += 1;
2235               old_tls_type = metag_elf_local_got_tls_type (abfd) [r_symndx];
2236             }
2237
2238           if (old_tls_type != tls_type)
2239             {
2240               if (hh != NULL)
2241                 {
2242                   hh->tls_type = tls_type;
2243                 }
2244               else
2245                 {
2246                   metag_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
2247                 }
2248             }
2249
2250           break;
2251
2252         case R_METAG_TLS_LDM:
2253           metag_link_hash_table (info)->tls_ldm_got.refcount += 1;
2254           break;
2255
2256         case R_METAG_RELBRANCH_PLT:
2257           /* This symbol requires a procedure linkage table entry.  We
2258              actually build the entry in adjust_dynamic_symbol,
2259              because this might be a case of linking PIC code without
2260              linking in any dynamic objects, in which case we don't
2261              need to generate a procedure linkage table after all.  */
2262
2263           /* If this is a local symbol, we resolve it directly without
2264              creating a procedure linkage table entry.  */
2265           if (hh == NULL)
2266             continue;
2267
2268           if (hh->eh.forced_local)
2269             break;
2270
2271           hh->eh.needs_plt = 1;
2272           hh->eh.plt.refcount += 1;
2273           break;
2274
2275         case R_METAG_HIADDR16:
2276         case R_METAG_LOADDR16:
2277           /* Let's help debug shared library creation.  These relocs
2278              cannot be used in shared libs.  Don't error out for
2279              sections we don't care about, such as debug sections or
2280              non-constant sections.  */
2281           if (bfd_link_pic (info)
2282               && (sec->flags & SEC_ALLOC) != 0
2283               && (sec->flags & SEC_READONLY) != 0)
2284             {
2285               const char *name;
2286
2287               if (hh)
2288                 name = hh->eh.root.root.string;
2289               else
2290                 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
2291               _bfd_error_handler
2292                 /* xgettext:c-format */
2293                 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2294                  abfd, elf_metag_howto_table[r_type].name, name);
2295               bfd_set_error (bfd_error_bad_value);
2296               return FALSE;
2297             }
2298
2299           /* Fall through.  */
2300         case R_METAG_ADDR32:
2301         case R_METAG_RELBRANCH:
2302         case R_METAG_GETSETOFF:
2303           if (hh != NULL && !bfd_link_pic (info))
2304             {
2305               hh->eh.non_got_ref = 1;
2306               hh->eh.plt.refcount += 1;
2307             }
2308
2309           /* If we are creating a shared library, and this is a reloc
2310              against a global symbol, or a non PC relative reloc
2311              against a local symbol, then we need to copy the reloc
2312              into the shared library.  However, if we are linking with
2313              -Bsymbolic, we do not need to copy a reloc against a
2314              global symbol which is defined in an object we are
2315              including in the link (i.e., DEF_REGULAR is set).  At
2316              this point we have not seen all the input files, so it is
2317              possible that DEF_REGULAR is not set now but will be set
2318              later (it is never cleared).  We account for that
2319              possibility below by storing information in the
2320              dyn_relocs field of the hash table entry. A similar
2321              situation occurs when creating shared libraries and symbol
2322              visibility changes render the symbol local.
2323
2324              If on the other hand, we are creating an executable, we
2325              may need to keep relocations for symbols satisfied by a
2326              dynamic library if we manage to avoid copy relocs for the
2327              symbol.  */
2328           if ((bfd_link_pic (info)
2329                && (sec->flags & SEC_ALLOC) != 0
2330                && (r_type != R_METAG_RELBRANCH
2331                    || (hh != NULL
2332                        && (! info->symbolic
2333                            || hh->eh.root.type == bfd_link_hash_defweak
2334                            || !hh->eh.def_regular))))
2335               || (!bfd_link_pic (info)
2336                   && (sec->flags & SEC_ALLOC) != 0
2337                   && hh != NULL
2338                   && (hh->eh.root.type == bfd_link_hash_defweak
2339                       || !hh->eh.def_regular)))
2340             {
2341               struct elf_metag_dyn_reloc_entry *hdh_p;
2342               struct elf_metag_dyn_reloc_entry **hdh_head;
2343
2344               if (dynobj == NULL)
2345                 htab->etab.dynobj = dynobj = abfd;
2346
2347               /* When creating a shared object, we must copy these
2348                  relocs into the output file.  We create a reloc
2349                  section in dynobj and make room for the reloc.  */
2350               if (sreloc == NULL)
2351                 {
2352                   sreloc = _bfd_elf_make_dynamic_reloc_section
2353                     (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ TRUE);
2354
2355                   if (sreloc == NULL)
2356                     {
2357                       bfd_set_error (bfd_error_bad_value);
2358                       return FALSE;
2359                     }
2360
2361                   elf_section_data (sec)->sreloc = sreloc;
2362                 }
2363
2364               /* If this is a global symbol, we count the number of
2365                  relocations we need for this symbol.  */
2366               if (hh != NULL)
2367                 hdh_head = &((struct elf_metag_link_hash_entry *) hh)->dyn_relocs;
2368               else
2369                 {
2370                   /* Track dynamic relocs needed for local syms too.  */
2371                   asection *sr;
2372                   void *vpp;
2373
2374                   sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
2375                   if (sr == NULL)
2376                     sr = sec;
2377
2378                   vpp = &elf_section_data (sr)->local_dynrel;
2379                   hdh_head = (struct elf_metag_dyn_reloc_entry **) vpp;
2380                 }
2381
2382               hdh_p = *hdh_head;
2383               if (hdh_p == NULL || hdh_p->sec != sec)
2384                 {
2385                   hdh_p = ((struct elf_metag_dyn_reloc_entry *)
2386                            bfd_alloc (dynobj, sizeof *hdh_p));
2387                   if (hdh_p == NULL)
2388                     return FALSE;
2389                   hdh_p->hdh_next = *hdh_head;
2390                   *hdh_head = hdh_p;
2391                   hdh_p->sec = sec;
2392                   hdh_p->count = 0;
2393                   hdh_p->relative_count = 0;
2394                 }
2395
2396               hdh_p->count += 1;
2397               if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH)
2398                 hdh_p->relative_count += 1;
2399             }
2400           break;
2401
2402           /* This relocation describes the C++ object vtable hierarchy.
2403              Reconstruct it for later use during GC.  */
2404         case R_METAG_GNU_VTINHERIT:
2405           if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh,
2406                                             rel->r_offset))
2407             return FALSE;
2408           break;
2409
2410           /* This relocation describes which C++ vtable entries are actually
2411              used.  Record for later use during GC.  */
2412         case R_METAG_GNU_VTENTRY:
2413           BFD_ASSERT (hh != NULL);
2414           if (hh != NULL
2415               && !bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rel->r_addend))
2416             return FALSE;
2417           break;
2418         }
2419     }
2420
2421   return TRUE;
2422 }
2423
2424 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2425
2426 static void
2427 elf_metag_copy_indirect_symbol (struct bfd_link_info *info,
2428                                 struct elf_link_hash_entry *eh_dir,
2429                                 struct elf_link_hash_entry *eh_ind)
2430 {
2431   struct elf_metag_link_hash_entry *hh_dir, *hh_ind;
2432
2433   hh_dir = metag_elf_hash_entry (eh_dir);
2434   hh_ind = metag_elf_hash_entry (eh_ind);
2435
2436   if (hh_ind->dyn_relocs != NULL)
2437     {
2438       if (hh_dir->dyn_relocs != NULL)
2439         {
2440           struct elf_metag_dyn_reloc_entry **hdh_pp;
2441           struct elf_metag_dyn_reloc_entry *hdh_p;
2442
2443           if (eh_ind->root.type == bfd_link_hash_indirect)
2444             abort ();
2445
2446           /* Add reloc counts against the weak sym to the strong sym
2447              list.  Merge any entries against the same section.  */
2448           for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2449             {
2450               struct elf_metag_dyn_reloc_entry *hdh_q;
2451
2452               for (hdh_q = hh_dir->dyn_relocs; hdh_q != NULL;
2453                    hdh_q = hdh_q->hdh_next)
2454                 if (hdh_q->sec == hdh_p->sec)
2455                   {
2456                     hdh_q->relative_count += hdh_p->relative_count;
2457                     hdh_q->count += hdh_p->count;
2458                     *hdh_pp = hdh_p->hdh_next;
2459                     break;
2460                   }
2461               if (hdh_q == NULL)
2462                 hdh_pp = &hdh_p->hdh_next;
2463             }
2464           *hdh_pp = hh_dir->dyn_relocs;
2465         }
2466
2467       hh_dir->dyn_relocs = hh_ind->dyn_relocs;
2468       hh_ind->dyn_relocs = NULL;
2469     }
2470
2471   if (eh_ind->root.type == bfd_link_hash_indirect
2472       && eh_dir->got.refcount <= 0)
2473     {
2474       hh_dir->tls_type = hh_ind->tls_type;
2475       hh_ind->tls_type = GOT_UNKNOWN;
2476     }
2477
2478   _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
2479 }
2480
2481 /* Adjust a symbol defined by a dynamic object and referenced by a
2482    regular object.  The current definition is in some section of the
2483    dynamic object, but we're not including those sections.  We have to
2484    change the definition to something the rest of the link can
2485    understand.  */
2486
2487 static bfd_boolean
2488 elf_metag_adjust_dynamic_symbol (struct bfd_link_info *info,
2489                                  struct elf_link_hash_entry *eh)
2490 {
2491   struct elf_metag_link_hash_table *htab;
2492   struct elf_metag_link_hash_entry *hh;
2493   struct elf_metag_dyn_reloc_entry *hdh_p;
2494   asection *s;
2495
2496   /* If this is a function, put it in the procedure linkage table.  We
2497      will fill in the contents of the procedure linkage table later,
2498      when we know the address of the .got section.  */
2499   if (eh->type == STT_FUNC
2500       || eh->needs_plt)
2501     {
2502       if (eh->plt.refcount <= 0
2503           || SYMBOL_CALLS_LOCAL (info, eh)
2504           || (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT
2505               && eh->root.type == bfd_link_hash_undefweak))
2506         {
2507           /* This case can occur if we saw a PLT reloc in an input
2508              file, but the symbol was never referred to by a dynamic
2509              object.  In such a case, we don't actually need to build
2510              a procedure linkage table, and we can just do a PCREL
2511              reloc instead.  */
2512           eh->plt.offset = (bfd_vma) -1;
2513           eh->needs_plt = 0;
2514         }
2515
2516       return TRUE;
2517     }
2518   else
2519     eh->plt.offset = (bfd_vma) -1;
2520
2521   /* If this is a weak symbol, and there is a real definition, the
2522      processor independent code will have arranged for us to see the
2523      real definition first, and we can just use the same value.  */
2524   if (eh->u.weakdef != NULL)
2525     {
2526       if (eh->u.weakdef->root.type != bfd_link_hash_defined
2527           && eh->u.weakdef->root.type != bfd_link_hash_defweak)
2528         abort ();
2529       eh->root.u.def.section = eh->u.weakdef->root.u.def.section;
2530       eh->root.u.def.value = eh->u.weakdef->root.u.def.value;
2531       eh->non_got_ref = eh->u.weakdef->non_got_ref;
2532       return TRUE;
2533     }
2534
2535   /* This is a reference to a symbol defined by a dynamic object which
2536      is not a function.  */
2537
2538   /* If we are creating a shared library, we must presume that the
2539      only references to the symbol are via the global offset table.
2540      For such cases we need not do anything here; the relocations will
2541      be handled correctly by relocate_section.  */
2542   if (bfd_link_pic (info))
2543     return TRUE;
2544
2545   /* If there are no references to this symbol that do not use the
2546      GOT, we don't need to generate a copy reloc.  */
2547   if (!eh->non_got_ref)
2548     return TRUE;
2549
2550   /* If -z nocopyreloc was given, we won't generate them either.  */
2551   if (info->nocopyreloc)
2552     {
2553       eh->non_got_ref = 0;
2554       return TRUE;
2555     }
2556
2557   hh = (struct elf_metag_link_hash_entry *) eh;
2558   for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2559     {
2560       s = hdh_p->sec->output_section;
2561       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2562         break;
2563     }
2564
2565   /* If we didn't find any dynamic relocs in read-only sections, then
2566      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
2567   if (hdh_p == NULL)
2568     {
2569       eh->non_got_ref = 0;
2570       return TRUE;
2571     }
2572
2573   /* We must allocate the symbol in our .dynbss section, which will
2574      become part of the .bss section of the executable.  There will be
2575      an entry for this symbol in the .dynsym section.  The dynamic
2576      object will contain position independent code, so all references
2577      from the dynamic object to this symbol will go through the global
2578      offset table.  The dynamic linker will use the .dynsym entry to
2579      determine the address it must put in the global offset table, so
2580      both the dynamic object and the regular object will refer to the
2581      same memory location for the variable.  */
2582
2583   htab = metag_link_hash_table (info);
2584
2585   /* We must generate a COPY reloc to tell the dynamic linker to
2586      copy the initial value out of the dynamic object and into the
2587      runtime process image.  */
2588   if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
2589     {
2590       htab->srelbss->size += sizeof (Elf32_External_Rela);
2591       eh->needs_copy = 1;
2592     }
2593
2594   s = htab->sdynbss;
2595
2596   return _bfd_elf_adjust_dynamic_copy (info, eh, s);
2597 }
2598
2599 /* Allocate space in .plt, .got and associated reloc sections for
2600    global syms.  */
2601
2602 static bfd_boolean
2603 allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2604 {
2605   struct bfd_link_info *info;
2606   struct elf_metag_link_hash_table *htab;
2607   struct elf_metag_link_hash_entry *hh;
2608   struct elf_metag_dyn_reloc_entry *hdh_p;
2609
2610   if (eh->root.type == bfd_link_hash_indirect)
2611     return TRUE;
2612
2613   if (eh->root.type == bfd_link_hash_warning)
2614     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2615
2616   info = inf;
2617   htab = metag_link_hash_table (info);
2618
2619   if (htab->etab.dynamic_sections_created
2620       && eh->plt.refcount > 0)
2621     {
2622       /* Make sure this symbol is output as a dynamic symbol.
2623          Undefined weak syms won't yet be marked as dynamic.  */
2624       if (eh->dynindx == -1
2625           && !eh->forced_local)
2626         {
2627           if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2628             return FALSE;
2629         }
2630
2631       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), eh))
2632         {
2633           asection *s = htab->splt;
2634
2635           /* If this is the first .plt entry, make room for the special
2636              first entry.  */
2637           if (s->size == 0)
2638             s->size += PLT_ENTRY_SIZE;
2639
2640           eh->plt.offset = s->size;
2641
2642           /* If this symbol is not defined in a regular file, and we are
2643              not generating a shared library, then set the symbol to this
2644              location in the .plt.  This is required to make function
2645              pointers compare as equal between the normal executable and
2646              the shared library.  */
2647           if (! bfd_link_pic (info)
2648               && !eh->def_regular)
2649             {
2650               eh->root.u.def.section = s;
2651               eh->root.u.def.value = eh->plt.offset;
2652             }
2653
2654           /* Make room for this entry.  */
2655           s->size += PLT_ENTRY_SIZE;
2656
2657           /* We also need to make an entry in the .got.plt section, which
2658              will be placed in the .got section by the linker script.  */
2659           htab->sgotplt->size += 4;
2660
2661           /* We also need to make an entry in the .rel.plt section.  */
2662           htab->srelplt->size += sizeof (Elf32_External_Rela);
2663         }
2664       else
2665         {
2666           eh->plt.offset = (bfd_vma) -1;
2667           eh->needs_plt = 0;
2668         }
2669     }
2670   else
2671     {
2672       eh->plt.offset = (bfd_vma) -1;
2673       eh->needs_plt = 0;
2674     }
2675
2676   if (eh->got.refcount > 0)
2677     {
2678       asection *s;
2679       bfd_boolean dyn;
2680       int tls_type = metag_elf_hash_entry (eh)->tls_type;
2681
2682       /* Make sure this symbol is output as a dynamic symbol.
2683          Undefined weak syms won't yet be marked as dynamic.  */
2684       if (eh->dynindx == -1
2685           && !eh->forced_local)
2686         {
2687           if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2688             return FALSE;
2689         }
2690
2691       s = htab->sgot;
2692
2693       eh->got.offset = s->size;
2694       s->size += 4;
2695       /* R_METAG_TLS_GD needs 2 consecutive GOT slots.  */
2696       if (tls_type == GOT_TLS_GD)
2697           s->size += 4;
2698       dyn = htab->etab.dynamic_sections_created;
2699       /* R_METAG_TLS_IE needs one dynamic relocation if dynamic,
2700          R_METAG_TLS_GD needs one if local symbol and two if global.  */
2701       if ((tls_type == GOT_TLS_GD && eh->dynindx == -1)
2702           || (tls_type == GOT_TLS_IE && dyn))
2703         htab->srelgot->size += sizeof (Elf32_External_Rela);
2704       else if (tls_type == GOT_TLS_GD)
2705           htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
2706       else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2707                                                 bfd_link_pic (info),
2708                                                 eh))
2709           htab->srelgot->size += sizeof (Elf32_External_Rela);
2710     }
2711   else
2712     eh->got.offset = (bfd_vma) -1;
2713
2714   hh = (struct elf_metag_link_hash_entry *) eh;
2715   if (hh->dyn_relocs == NULL)
2716     return TRUE;
2717
2718   /* If this is a -Bsymbolic shared link, then we need to discard all
2719      space allocated for dynamic pc-relative relocs against symbols
2720      defined in a regular object.  For the normal shared case, discard
2721      space for relocs that have become local due to symbol visibility
2722      changes.  */
2723   if (bfd_link_pic (info))
2724     {
2725       if (SYMBOL_CALLS_LOCAL (info, eh))
2726         {
2727           struct elf_metag_dyn_reloc_entry **hdh_pp;
2728
2729           for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2730             {
2731               hdh_p->count -= hdh_p->relative_count;
2732               hdh_p->relative_count = 0;
2733               if (hdh_p->count == 0)
2734                 *hdh_pp = hdh_p->hdh_next;
2735               else
2736                 hdh_pp = &hdh_p->hdh_next;
2737             }
2738         }
2739
2740       /* Also discard relocs on undefined weak syms with non-default
2741          visibility.  */
2742       if (hh->dyn_relocs != NULL
2743           && eh->root.type == bfd_link_hash_undefweak)
2744         {
2745           if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
2746             hh->dyn_relocs = NULL;
2747
2748           /* Make sure undefined weak symbols are output as a dynamic
2749              symbol in PIEs.  */
2750           else if (eh->dynindx == -1
2751                    && !eh->forced_local)
2752             {
2753               if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2754                 return FALSE;
2755             }
2756         }
2757     }
2758   else
2759     {
2760       /* For the non-shared case, discard space for relocs against
2761          symbols which turn out to need copy relocs or are not
2762          dynamic.  */
2763       if (!eh->non_got_ref
2764           && ((eh->def_dynamic
2765                && !eh->def_regular)
2766               || (htab->etab.dynamic_sections_created
2767                   && (eh->root.type == bfd_link_hash_undefweak
2768                       || eh->root.type == bfd_link_hash_undefined))))
2769         {
2770           /* Make sure this symbol is output as a dynamic symbol.
2771              Undefined weak syms won't yet be marked as dynamic.  */
2772           if (eh->dynindx == -1
2773               && !eh->forced_local)
2774             {
2775               if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2776                 return FALSE;
2777             }
2778
2779           /* If that succeeded, we know we'll be keeping all the
2780              relocs.  */
2781           if (eh->dynindx != -1)
2782             goto keep;
2783         }
2784
2785       hh->dyn_relocs = NULL;
2786       return TRUE;
2787
2788     keep: ;
2789     }
2790
2791   /* Finally, allocate space.  */
2792   for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2793     {
2794       asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
2795       sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
2796     }
2797
2798   return TRUE;
2799 }
2800
2801 /* Find any dynamic relocs that apply to read-only sections.  */
2802
2803 static bfd_boolean
2804 readonly_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2805 {
2806   struct elf_metag_link_hash_entry *hh;
2807   struct elf_metag_dyn_reloc_entry *hdh_p;
2808
2809   if (eh->root.type == bfd_link_hash_warning)
2810     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2811
2812   hh = (struct elf_metag_link_hash_entry *) eh;
2813   for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2814     {
2815       asection *s = hdh_p->sec->output_section;
2816
2817       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2818         {
2819           struct bfd_link_info *info = inf;
2820
2821           info->flags |= DF_TEXTREL;
2822
2823           /* Not an error, just cut short the traversal.  */
2824           return FALSE;
2825         }
2826     }
2827   return TRUE;
2828 }
2829
2830 /* Set the sizes of the dynamic sections.  */
2831
2832 static bfd_boolean
2833 elf_metag_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2834                                  struct bfd_link_info *info)
2835 {
2836   struct elf_metag_link_hash_table *htab;
2837   bfd *dynobj;
2838   bfd *ibfd;
2839   asection *s;
2840   bfd_boolean relocs;
2841
2842   htab = metag_link_hash_table (info);
2843   dynobj = htab->etab.dynobj;
2844   if (dynobj == NULL)
2845     abort ();
2846
2847   if (htab->etab.dynamic_sections_created)
2848     {
2849       /* Set the contents of the .interp section to the interpreter.  */
2850       if (bfd_link_executable (info) && !info->nointerp)
2851         {
2852           s = bfd_get_linker_section (dynobj, ".interp");
2853           if (s == NULL)
2854             abort ();
2855           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2856           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2857         }
2858     }
2859
2860   /* Set up .got offsets for local syms, and space for local dynamic
2861      relocs.  */
2862   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2863     {
2864       bfd_signed_vma *local_got;
2865       bfd_signed_vma *end_local_got;
2866       bfd_size_type locsymcount;
2867       Elf_Internal_Shdr *symtab_hdr;
2868       asection *srel;
2869       char *local_tls_type;
2870
2871       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2872         continue;
2873
2874       for (s = ibfd->sections; s != NULL; s = s->next)
2875         {
2876           struct elf_metag_dyn_reloc_entry *hdh_p;
2877
2878           for (hdh_p = ((struct elf_metag_dyn_reloc_entry *)
2879                         elf_section_data (s)->local_dynrel);
2880                hdh_p != NULL;
2881                hdh_p = hdh_p->hdh_next)
2882             {
2883               if (!bfd_is_abs_section (hdh_p->sec)
2884                   && bfd_is_abs_section (hdh_p->sec->output_section))
2885                 {
2886                   /* Input section has been discarded, either because
2887                      it is a copy of a linkonce section or due to
2888                      linker script /DISCARD/, so we'll be discarding
2889                      the relocs too.  */
2890                 }
2891               else if (hdh_p->count != 0)
2892                 {
2893                   srel = elf_section_data (hdh_p->sec)->sreloc;
2894                   srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
2895                   if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
2896                     info->flags |= DF_TEXTREL;
2897                 }
2898             }
2899         }
2900
2901       local_got = elf_local_got_refcounts (ibfd);
2902       if (!local_got)
2903         continue;
2904
2905       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2906       locsymcount = symtab_hdr->sh_info;
2907       end_local_got = local_got + locsymcount;
2908       local_tls_type = metag_elf_local_got_tls_type (ibfd);
2909       s = htab->sgot;
2910       srel = htab->srelgot;
2911       for (; local_got < end_local_got; ++local_got)
2912         {
2913           if (*local_got > 0)
2914             {
2915               *local_got = s->size;
2916               s->size += GOT_ENTRY_SIZE;
2917               /* R_METAG_TLS_GD relocs need 2 consecutive GOT entries.  */
2918               if (*local_tls_type == GOT_TLS_GD)
2919                 s->size += 4;
2920               if (bfd_link_pic (info))
2921                 srel->size += sizeof (Elf32_External_Rela);
2922             }
2923           else
2924             *local_got = (bfd_vma) -1;
2925           ++local_tls_type;
2926         }
2927     }
2928
2929   if (htab->tls_ldm_got.refcount > 0)
2930     {
2931       /* Allocate 2 got entries and 1 dynamic reloc for R_METAG_TLS_LDM
2932          reloc.  */
2933       htab->tls_ldm_got.offset = htab->sgot->size;
2934       htab->sgot->size += 8;
2935       htab->srelgot->size += sizeof (Elf32_External_Rela);
2936     }
2937   else
2938     htab->tls_ldm_got.offset = -1;
2939
2940   /* Allocate global sym .plt and .got entries, and space for global
2941      sym dynamic relocs.  */
2942   elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
2943
2944   /* We now have determined the sizes of the various dynamic sections.
2945      Allocate memory for them.  */
2946   relocs = FALSE;
2947   for (s = dynobj->sections; s != NULL; s = s->next)
2948     {
2949       bfd_boolean reloc_section = FALSE;
2950
2951       if ((s->flags & SEC_LINKER_CREATED) == 0)
2952         continue;
2953
2954       if (s == htab->splt
2955           || s == htab->sgot
2956           || s == htab->sgotplt
2957           || s == htab->sdynbss)
2958         {
2959           /* Strip this section if we don't need it; see the
2960              comment below.  */
2961         }
2962       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2963         {
2964           if (s->size != 0 && s != htab->srelplt)
2965             relocs = TRUE;
2966
2967           /* We use the reloc_count field as a counter if we need
2968              to copy relocs into the output file.  */
2969           s->reloc_count = 0;
2970           reloc_section = TRUE;
2971         }
2972       else
2973         {
2974           /* It's not one of our sections, so don't allocate space.  */
2975           continue;
2976         }
2977
2978       if (s->size == 0)
2979         {
2980           /* If we don't need this section, strip it from the
2981              output file.  This is mostly to handle .rela.bss and
2982              .rela.plt.  We must create both sections in
2983              create_dynamic_sections, because they must be created
2984              before the linker maps input sections to output
2985              sections.  The linker does that before
2986              adjust_dynamic_symbol is called, and it is that
2987              function which decides whether anything needs to go
2988              into these sections.  */
2989           s->flags |= SEC_EXCLUDE;
2990           continue;
2991         }
2992
2993       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2994         continue;
2995
2996       /* Allocate memory for the section contents.  */
2997       s->contents = bfd_zalloc (dynobj, s->size);
2998       if (s->contents == NULL)
2999         return FALSE;
3000       else if (reloc_section)
3001         {
3002           unsigned char *contents = s->contents;
3003           Elf32_External_Rela reloc;
3004
3005           /* Fill the reloc section with a R_METAG_NONE type reloc.  */
3006           memset(&reloc, 0, sizeof(Elf32_External_Rela));
3007           reloc.r_info[0] = R_METAG_NONE;
3008           for (; contents < (s->contents + s->size);
3009                contents += sizeof(Elf32_External_Rela))
3010             {
3011               memcpy(contents, &reloc, sizeof(Elf32_External_Rela));
3012             }
3013         }
3014     }
3015
3016   if (htab->etab.dynamic_sections_created)
3017     {
3018       /* Add some entries to the .dynamic section.  We fill in the
3019          values later, in elf_metag_finish_dynamic_sections, but we
3020          must add the entries now so that we get the correct size for
3021          the .dynamic section.  The DT_DEBUG entry is filled in by the
3022          dynamic linker and used by the debugger.  */
3023 #define add_dynamic_entry(TAG, VAL) \
3024   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3025
3026       if (!add_dynamic_entry (DT_PLTGOT, 0))
3027         return FALSE;
3028
3029       if (bfd_link_executable (info))
3030         {
3031           if (!add_dynamic_entry (DT_DEBUG, 0))
3032             return FALSE;
3033         }
3034
3035       if (htab->srelplt->size != 0)
3036         {
3037           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3038               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3039               || !add_dynamic_entry (DT_JMPREL, 0))
3040             return FALSE;
3041         }
3042
3043       if (relocs)
3044         {
3045           if (!add_dynamic_entry (DT_RELA, 0)
3046               || !add_dynamic_entry (DT_RELASZ, 0)
3047               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3048             return FALSE;
3049
3050           /* If any dynamic relocs apply to a read-only section,
3051              then we need a DT_TEXTREL entry.  */
3052           if ((info->flags & DF_TEXTREL) == 0)
3053             elf_link_hash_traverse (&htab->etab, readonly_dynrelocs, info);
3054
3055           if ((info->flags & DF_TEXTREL) != 0)
3056             {
3057               if (!add_dynamic_entry (DT_TEXTREL, 0))
3058                 return FALSE;
3059             }
3060         }
3061     }
3062 #undef add_dynamic_entry
3063
3064   return TRUE;
3065 }
3066
3067 /* Finish up dynamic symbol handling.  We set the contents of various
3068    dynamic sections here.  */
3069
3070 static bfd_boolean
3071 elf_metag_finish_dynamic_symbol (bfd *output_bfd,
3072                                  struct bfd_link_info *info,
3073                                  struct elf_link_hash_entry *eh,
3074                                  Elf_Internal_Sym *sym)
3075 {
3076   struct elf_metag_link_hash_table *htab;
3077   Elf_Internal_Rela rel;
3078   bfd_byte *loc;
3079
3080   htab = metag_link_hash_table (info);
3081
3082   if (eh->plt.offset != (bfd_vma) -1)
3083     {
3084       asection *splt;
3085       asection *sgot;
3086       asection *srela;
3087
3088       bfd_vma plt_index;
3089       bfd_vma got_offset;
3090       bfd_vma got_entry;
3091
3092       if (eh->plt.offset & 1)
3093         abort ();
3094
3095       BFD_ASSERT (eh->dynindx != -1);
3096
3097       splt = htab->splt;
3098       sgot = htab->sgotplt;
3099       srela = htab->srelplt;
3100       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
3101
3102       /* Get the index in the procedure linkage table which
3103          corresponds to this symbol.  This is the index of this symbol
3104          in all the symbols for which we are making plt entries.  The
3105          first entry in the procedure linkage table is reserved.  */
3106       plt_index = eh->plt.offset / PLT_ENTRY_SIZE - 1;
3107
3108       /* Get the offset into the .got.plt table of the entry that
3109          corresponds to this function.  */
3110       got_offset = plt_index * GOT_ENTRY_SIZE;
3111
3112       BFD_ASSERT (got_offset < (1 << 16));
3113
3114       got_entry = sgot->output_section->vma
3115         + sgot->output_offset
3116         + got_offset;
3117
3118       BFD_ASSERT (plt_index < (1 << 16));
3119
3120       /* Fill in the entry in the procedure linkage table.  */
3121       if (! bfd_link_pic (info))
3122         {
3123           bfd_put_32 (output_bfd,
3124                       (plt_entry[0]
3125                        | (((got_entry >> 16) & 0xffff) << 3)),
3126                       splt->contents + eh->plt.offset);
3127           bfd_put_32 (output_bfd,
3128                       (plt_entry[1]
3129                        | ((got_entry & 0xffff) << 3)),
3130                       splt->contents + eh->plt.offset + 4);
3131           bfd_put_32 (output_bfd, plt_entry[2],
3132                       splt->contents + eh->plt.offset + 8);
3133           bfd_put_32 (output_bfd,
3134                       (plt_entry[3] | (plt_index << 3)),
3135                       splt->contents + eh->plt.offset + 12);
3136           bfd_put_32 (output_bfd,
3137                       (plt_entry[4]
3138                        | ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
3139                       splt->contents + eh->plt.offset + 16);
3140         }
3141       else
3142         {
3143           bfd_vma addr = got_entry - (splt->output_section->vma +
3144                                       splt->output_offset + eh->plt.offset);
3145
3146           bfd_put_32 (output_bfd,
3147                       plt_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
3148                       splt->contents + eh->plt.offset);
3149           bfd_put_32 (output_bfd,
3150                       plt_pic_entry[1] | ((addr & 0xffff) << 3),
3151                       splt->contents + eh->plt.offset + 4);
3152           bfd_put_32 (output_bfd, plt_pic_entry[2],
3153                       splt->contents + eh->plt.offset + 8);
3154           bfd_put_32 (output_bfd,
3155                       (plt_pic_entry[3] | (plt_index << 3)),
3156                       splt->contents + eh->plt.offset + 12);
3157           bfd_put_32 (output_bfd,
3158                       (plt_pic_entry[4]
3159                        + ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)),
3160                       splt->contents + eh->plt.offset + 16);
3161         }
3162
3163       /* Fill in the entry in the global offset table.  */
3164       bfd_put_32 (output_bfd,
3165                   (splt->output_section->vma
3166                    + splt->output_offset
3167                    + eh->plt.offset
3168                    + 12), /* offset within PLT entry */
3169                   sgot->contents + got_offset);
3170
3171       /* Fill in the entry in the .rela.plt section.  */
3172       rel.r_offset = (sgot->output_section->vma
3173                       + sgot->output_offset
3174                       + got_offset);
3175       rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_JMP_SLOT);
3176       rel.r_addend = 0;
3177       loc = htab->srelplt->contents;
3178       loc += plt_index * sizeof(Elf32_External_Rela);
3179       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3180
3181       if (!eh->def_regular)
3182         {
3183           /* Mark the symbol as undefined, rather than as defined in
3184              the .plt section.  Leave the value alone.  */
3185           sym->st_shndx = SHN_UNDEF;
3186         }
3187     }
3188
3189   if (eh->got.offset != (bfd_vma) -1
3190       && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0
3191       && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0)
3192     {
3193       /* This symbol has an entry in the global offset table.  Set it
3194          up.  */
3195
3196       rel.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
3197                       + htab->sgot->output_offset
3198                       + htab->sgot->output_section->vma);
3199
3200       /* If this is a -Bsymbolic link and the symbol is defined
3201          locally or was forced to be local because of a version file,
3202          we just want to emit a RELATIVE reloc.  The entry in the
3203          global offset table will already have been initialized in the
3204          relocate_section function.  */
3205       if (bfd_link_pic (info)
3206           && (info->symbolic || eh->dynindx == -1)
3207           && eh->def_regular)
3208         {
3209           rel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE);
3210           rel.r_addend = (eh->root.u.def.value
3211                           + eh->root.u.def.section->output_offset
3212                           + eh->root.u.def.section->output_section->vma);
3213         }
3214       else
3215         {
3216           if ((eh->got.offset & 1) != 0)
3217             abort ();
3218           bfd_put_32 (output_bfd, 0, htab->sgot->contents + eh->got.offset);
3219           rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_GLOB_DAT);
3220           rel.r_addend = 0;
3221         }
3222
3223       loc = htab->srelgot->contents;
3224       loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3225       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3226     }
3227
3228   if (eh->needs_copy)
3229     {
3230       asection *s;
3231
3232       /* This symbol needs a copy reloc.  Set it up.  */
3233
3234       if (! (eh->dynindx != -1
3235              && (eh->root.type == bfd_link_hash_defined
3236                  || eh->root.type == bfd_link_hash_defweak)))
3237         abort ();
3238
3239       s = htab->srelbss;
3240
3241       rel.r_offset = (eh->root.u.def.value
3242                       + eh->root.u.def.section->output_offset
3243                       + eh->root.u.def.section->output_section->vma);
3244       rel.r_addend = 0;
3245       rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_COPY);
3246       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3247       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3248     }
3249
3250   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
3251   if (eh->root.root.string[0] == '_'
3252       && (strcmp (eh->root.root.string, "_DYNAMIC") == 0
3253           || eh == htab->etab.hgot))
3254     {
3255       sym->st_shndx = SHN_ABS;
3256     }
3257
3258   return TRUE;
3259 }
3260
3261 /* Set the Meta ELF ABI version.  */
3262
3263 static void
3264 elf_metag_post_process_headers (bfd * abfd, struct bfd_link_info * link_info)
3265 {
3266   Elf_Internal_Ehdr * i_ehdrp;  /* ELF file header, internal form.  */
3267
3268   _bfd_elf_post_process_headers (abfd, link_info);
3269   i_ehdrp = elf_elfheader (abfd);
3270   i_ehdrp->e_ident[EI_ABIVERSION] = METAG_ELF_ABI_VERSION;
3271 }
3272
3273 /* Used to decide how to sort relocs in an optimal manner for the
3274    dynamic linker, before writing them out.  */
3275
3276 static enum elf_reloc_type_class
3277 elf_metag_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3278                             const asection *rel_sec ATTRIBUTE_UNUSED,
3279                             const Elf_Internal_Rela *rela)
3280 {
3281   switch ((int) ELF32_R_TYPE (rela->r_info))
3282     {
3283     case R_METAG_RELATIVE:
3284       return reloc_class_relative;
3285     case R_METAG_JMP_SLOT:
3286       return reloc_class_plt;
3287     case R_METAG_COPY:
3288       return reloc_class_copy;
3289     default:
3290       return reloc_class_normal;
3291     }
3292 }
3293
3294 /* Finish up the dynamic sections.  */
3295
3296 static bfd_boolean
3297 elf_metag_finish_dynamic_sections (bfd *output_bfd,
3298                                    struct bfd_link_info *info)
3299 {
3300   bfd *dynobj;
3301   struct elf_metag_link_hash_table *htab;
3302   asection *sdyn;
3303
3304   htab = metag_link_hash_table (info);
3305   dynobj = htab->etab.dynobj;
3306
3307   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3308
3309   if (htab->etab.dynamic_sections_created)
3310     {
3311       asection *splt;
3312       Elf32_External_Dyn *dyncon, *dynconend;
3313
3314       if (sdyn == NULL)
3315         abort ();
3316
3317       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3318       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3319       for (; dyncon < dynconend; dyncon++)
3320         {
3321           Elf_Internal_Dyn dyn;
3322           asection *s;
3323
3324           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3325
3326           switch (dyn.d_tag)
3327             {
3328             default:
3329               continue;
3330
3331             case DT_PLTGOT:
3332               s = htab->sgot;
3333               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3334               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3335               break;
3336
3337             case DT_JMPREL:
3338               s = htab->srelplt;
3339               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3340               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3341               break;
3342
3343             case DT_PLTRELSZ:
3344               s = htab->srelplt;
3345               dyn.d_un.d_val = s->size;
3346               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3347               break;
3348
3349             case DT_RELASZ:
3350               /* Don't count procedure linkage table relocs in the
3351                  overall reloc count.  */
3352               if (htab->srelplt) {
3353                 s = htab->srelplt;
3354                 dyn.d_un.d_val -= s->size;
3355               }
3356               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3357               break;
3358
3359             case DT_RELA:
3360               /* We may not be using the standard ELF linker script.
3361                  If .rela.plt is the first .rela section, we adjust
3362                  DT_RELA to not include it.  */
3363               if (htab->srelplt) {
3364                 s = htab->srelplt;
3365                 if (dyn.d_un.d_ptr == s->output_section->vma + s->output_offset)
3366                   dyn.d_un.d_ptr += s->size;
3367               }
3368               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3369               break;
3370             }
3371
3372         }
3373
3374       /* Fill in the first entry in the procedure linkage table.  */
3375       splt = htab->splt;
3376       if (splt && splt->size > 0)
3377         {
3378           unsigned long addr;
3379           /* addr = .got + 4 */
3380           addr = htab->sgot->output_section->vma +
3381             htab->sgot->output_offset + 4;
3382           if (bfd_link_pic (info))
3383             {
3384               addr -= splt->output_section->vma + splt->output_offset;
3385               bfd_put_32 (output_bfd,
3386                           plt0_pic_entry[0] | (((addr >> 16) & 0xffff) << 3),
3387                           splt->contents);
3388               bfd_put_32 (output_bfd,
3389                           plt0_pic_entry[1] | ((addr & 0xffff) << 3),
3390                           splt->contents + 4);
3391               bfd_put_32 (output_bfd, plt0_pic_entry[2], splt->contents + 8);
3392               bfd_put_32 (output_bfd, plt0_pic_entry[3], splt->contents + 12);
3393               bfd_put_32 (output_bfd, plt0_pic_entry[4], splt->contents + 16);
3394             }
3395           else
3396             {
3397               bfd_put_32 (output_bfd,
3398                           plt0_entry[0] | (((addr >> 16) & 0xffff) << 3),
3399                           splt->contents);
3400               bfd_put_32 (output_bfd,
3401                           plt0_entry[1] | ((addr & 0xffff) << 3),
3402                           splt->contents + 4);
3403               bfd_put_32 (output_bfd, plt0_entry[2], splt->contents + 8);
3404               bfd_put_32 (output_bfd, plt0_entry[3], splt->contents + 12);
3405               bfd_put_32 (output_bfd, plt0_entry[4], splt->contents + 16);
3406             }
3407
3408           elf_section_data (splt->output_section)->this_hdr.sh_entsize =
3409             PLT_ENTRY_SIZE;
3410         }
3411     }
3412
3413   if (htab->sgot != NULL && htab->sgot->size != 0)
3414     {
3415       /* Fill in the first entry in the global offset table.
3416          We use it to point to our dynamic section, if we have one.  */
3417       bfd_put_32 (output_bfd,
3418                   sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
3419                   htab->sgot->contents);
3420
3421       /* The second entry is reserved for use by the dynamic linker.  */
3422       memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
3423
3424       /* Set .got entry size.  */
3425       elf_section_data (htab->sgot->output_section)
3426         ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3427     }
3428
3429   return TRUE;
3430 }
3431
3432 /* Return the section that should be marked against GC for a given
3433    relocation.  */
3434
3435 static asection *
3436 elf_metag_gc_mark_hook (asection *sec,
3437                         struct bfd_link_info *info,
3438                         Elf_Internal_Rela *rela,
3439                         struct elf_link_hash_entry *hh,
3440                         Elf_Internal_Sym *sym)
3441 {
3442   if (hh != NULL)
3443     switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
3444       {
3445       case R_METAG_GNU_VTINHERIT:
3446       case R_METAG_GNU_VTENTRY:
3447         return NULL;
3448       }
3449
3450   return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
3451 }
3452
3453 /* Update the got and plt entry reference counts for the section being
3454    removed.  */
3455
3456 static bfd_boolean
3457 elf_metag_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
3458                          struct bfd_link_info *info ATTRIBUTE_UNUSED,
3459                          asection *sec ATTRIBUTE_UNUSED,
3460                          const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
3461 {
3462   Elf_Internal_Shdr *symtab_hdr;
3463   struct elf_link_hash_entry **eh_syms;
3464   bfd_signed_vma *local_got_refcounts;
3465   bfd_signed_vma *local_plt_refcounts;
3466   const Elf_Internal_Rela *rel, *relend;
3467
3468   if (bfd_link_relocatable (info))
3469     return TRUE;
3470
3471   elf_section_data (sec)->local_dynrel = NULL;
3472
3473   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3474   eh_syms = elf_sym_hashes (abfd);
3475   local_got_refcounts = elf_local_got_refcounts (abfd);
3476   local_plt_refcounts = local_got_refcounts;
3477   if (local_plt_refcounts != NULL)
3478     local_plt_refcounts += symtab_hdr->sh_info;
3479
3480   relend = relocs + sec->reloc_count;
3481   for (rel = relocs; rel < relend; rel++)
3482     {
3483       unsigned long r_symndx;
3484       unsigned int r_type;
3485       struct elf_link_hash_entry *eh = NULL;
3486
3487       r_symndx = ELF32_R_SYM (rel->r_info);
3488       if (r_symndx >= symtab_hdr->sh_info)
3489         {
3490           struct elf_metag_link_hash_entry *hh;
3491           struct elf_metag_dyn_reloc_entry **hdh_pp;
3492           struct elf_metag_dyn_reloc_entry *hdh_p;
3493
3494           eh = eh_syms[r_symndx - symtab_hdr->sh_info];
3495           while (eh->root.type == bfd_link_hash_indirect
3496                  || eh->root.type == bfd_link_hash_warning)
3497             eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3498           hh = (struct elf_metag_link_hash_entry *) eh;
3499
3500           for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL;
3501                hdh_pp = &hdh_p->hdh_next)
3502             if (hdh_p->sec == sec)
3503               {
3504                 /* Everything must go for SEC.  */
3505                 *hdh_pp = hdh_p->hdh_next;
3506                 break;
3507               }
3508         }
3509
3510       r_type = ELF32_R_TYPE (rel->r_info);
3511       switch (r_type)
3512         {
3513         case R_METAG_TLS_LDM:
3514           if (metag_link_hash_table (info)->tls_ldm_got.refcount > 0)
3515             metag_link_hash_table (info)->tls_ldm_got.refcount -= 1;
3516           break;
3517         case R_METAG_TLS_IE:
3518         case R_METAG_TLS_GD:
3519         case R_METAG_GETSET_GOT:
3520           if (eh != NULL)
3521             {
3522               if (eh->got.refcount > 0)
3523                 eh->got.refcount -= 1;
3524             }
3525           else if (local_got_refcounts != NULL)
3526             {
3527               if (local_got_refcounts[r_symndx] > 0)
3528                 local_got_refcounts[r_symndx] -= 1;
3529             }
3530           break;
3531
3532         case R_METAG_RELBRANCH_PLT:
3533           if (eh != NULL)
3534             {
3535               if (eh->plt.refcount > 0)
3536                 eh->plt.refcount -= 1;
3537             }
3538           break;
3539
3540         case R_METAG_ADDR32:
3541         case R_METAG_HIADDR16:
3542         case R_METAG_LOADDR16:
3543         case R_METAG_GETSETOFF:
3544         case R_METAG_RELBRANCH:
3545           if (eh != NULL)
3546             {
3547               struct elf_metag_link_hash_entry *hh;
3548               struct elf_metag_dyn_reloc_entry **hdh_pp;
3549               struct elf_metag_dyn_reloc_entry *hdh_p;
3550
3551               if (!bfd_link_pic (info) && eh->plt.refcount > 0)
3552                 eh->plt.refcount -= 1;
3553
3554               hh = (struct elf_metag_link_hash_entry *) eh;
3555
3556               for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL;
3557                    hdh_pp = &hdh_p->hdh_next)
3558                 if (hdh_p->sec == sec)
3559                   {
3560                     if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH)
3561                       hdh_p->relative_count -= 1;
3562                     hdh_p->count -= 1;
3563                     if (hdh_p->count == 0)
3564                       *hdh_pp = hdh_p->hdh_next;
3565                     break;
3566                   }
3567             }
3568           break;
3569
3570         default:
3571           break;
3572         }
3573     }
3574
3575   return TRUE;
3576 }
3577
3578 /* Determine the type of stub needed, if any, for a call.  */
3579
3580 static enum elf_metag_stub_type
3581 metag_type_of_stub (asection *input_sec,
3582                     const Elf_Internal_Rela *rel,
3583                     struct elf_metag_link_hash_entry *hh,
3584                     bfd_vma destination,
3585                     struct bfd_link_info *info ATTRIBUTE_UNUSED)
3586 {
3587   bfd_vma location;
3588   bfd_vma branch_offset;
3589   bfd_vma max_branch_offset;
3590
3591   if (hh != NULL &&
3592       !(hh->eh.root.type == bfd_link_hash_defined
3593         || hh->eh.root.type == bfd_link_hash_defweak))
3594     return metag_stub_none;
3595
3596   /* Determine where the call point is.  */
3597   location = (input_sec->output_offset
3598               + input_sec->output_section->vma
3599               + rel->r_offset);
3600
3601   branch_offset = destination - location;
3602
3603   /* Determine if a long branch stub is needed.  Meta branch offsets
3604      are signed 19 bits 4 byte aligned.  */
3605   max_branch_offset = (1 << (BRANCH_BITS-1)) << 2;
3606
3607   if (branch_offset + max_branch_offset >= 2*max_branch_offset)
3608     {
3609       if (bfd_link_pic (info))
3610         return metag_stub_long_branch_shared;
3611       else
3612         return metag_stub_long_branch;
3613     }
3614
3615   return metag_stub_none;
3616 }
3617
3618 #define MOVT_A0_3       0x82180005
3619 #define JUMP_A0_3       0xac180003
3620
3621 #define MOVT_A1LBP      0x83080005
3622 #define ADD_A1LBP       0x83080000
3623
3624 #define ADDT_A0_3_CPC   0x82980001
3625 #define ADD_A0_3_A0_3   0x82180000
3626 #define MOV_PC_A0_3     0xa3180ca0
3627
3628 static bfd_boolean
3629 metag_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
3630 {
3631   struct elf_metag_stub_hash_entry *hsh;
3632   asection *stub_sec;
3633   bfd *stub_bfd;
3634   bfd_byte *loc;
3635   bfd_vma sym_value;
3636   int size;
3637
3638   /* Massage our args to the form they really have.  */
3639   hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
3640
3641   stub_sec = hsh->stub_sec;
3642
3643   /* Make a note of the offset within the stubs for this entry.  */
3644   hsh->stub_offset = stub_sec->size;
3645   loc = stub_sec->contents + hsh->stub_offset;
3646
3647   stub_bfd = stub_sec->owner;
3648
3649   switch (hsh->stub_type)
3650     {
3651     case metag_stub_long_branch_shared:
3652       /* A PIC long branch stub is an ADDT and an ADD instruction used to
3653          calculate the jump target using A0.3 as a temporary. Then a MOV
3654          to PC carries out the jump.  */
3655       sym_value = (hsh->target_value
3656                    + hsh->target_section->output_offset
3657                    + hsh->target_section->output_section->vma
3658                    + hsh->addend);
3659
3660       sym_value -= (hsh->stub_offset
3661                     + stub_sec->output_offset
3662                     + stub_sec->output_section->vma);
3663
3664       bfd_put_32 (stub_bfd, ADDT_A0_3_CPC | (((sym_value >> 16) & 0xffff) << 3),
3665                   loc);
3666
3667       bfd_put_32 (stub_bfd, ADD_A0_3_A0_3 | ((sym_value & 0xffff) << 3),
3668                   loc + 4);
3669
3670       bfd_put_32 (stub_bfd, MOV_PC_A0_3, loc + 8);
3671
3672       size = 12;
3673       break;
3674     case metag_stub_long_branch:
3675       /* A standard long branch stub is a MOVT instruction followed by a
3676          JUMP instruction using the A0.3 register as a temporary. This is
3677          the same method used by the LDLK linker (patch.c).  */
3678       sym_value = (hsh->target_value
3679                    + hsh->target_section->output_offset
3680                    + hsh->target_section->output_section->vma
3681                    + hsh->addend);
3682
3683       bfd_put_32 (stub_bfd, MOVT_A0_3 | (((sym_value >> 16) & 0xffff) << 3),
3684                   loc);
3685
3686       bfd_put_32 (stub_bfd, JUMP_A0_3 | ((sym_value & 0xffff) << 3), loc + 4);
3687
3688       size = 8;
3689       break;
3690     default:
3691       BFD_FAIL ();
3692       return FALSE;
3693     }
3694
3695   stub_sec->size += size;
3696   return TRUE;
3697 }
3698
3699 /* As above, but don't actually build the stub.  Just bump offset so
3700    we know stub section sizes.  */
3701
3702 static bfd_boolean
3703 metag_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED)
3704 {
3705   struct elf_metag_stub_hash_entry *hsh;
3706   int size = 0;
3707
3708   /* Massage our args to the form they really have.  */
3709   hsh = (struct elf_metag_stub_hash_entry *) gen_entry;
3710
3711   if (hsh->stub_type == metag_stub_long_branch)
3712     size = 8;
3713   else if (hsh->stub_type == metag_stub_long_branch_shared)
3714     size = 12;
3715
3716   hsh->stub_sec->size += size;
3717   return TRUE;
3718 }
3719
3720 /* Set up various things so that we can make a list of input sections
3721    for each output section included in the link.  Returns -1 on error,
3722    0 when no stubs will be needed, and 1 on success.  */
3723
3724 int
3725 elf_metag_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
3726 {
3727   bfd *input_bfd;
3728   unsigned int bfd_count;
3729   unsigned int top_id, top_index;
3730   asection *section;
3731   asection **input_list, **list;
3732   bfd_size_type amt;
3733   struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3734
3735   /* Count the number of input BFDs and find the top input section id.  */
3736   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3737        input_bfd != NULL;
3738        input_bfd = input_bfd->link.next)
3739     {
3740       bfd_count += 1;
3741       for (section = input_bfd->sections;
3742            section != NULL;
3743            section = section->next)
3744         {
3745           if (top_id < section->id)
3746             top_id = section->id;
3747         }
3748     }
3749
3750   htab->bfd_count = bfd_count;
3751
3752   amt = sizeof (struct map_stub) * (top_id + 1);
3753   htab->stub_group = bfd_zmalloc (amt);
3754   if (htab->stub_group == NULL)
3755     return -1;
3756
3757   /* We can't use output_bfd->section_count here to find the top output
3758      section index as some sections may have been removed, and
3759      strip_excluded_output_sections doesn't renumber the indices.  */
3760   for (section = output_bfd->sections, top_index = 0;
3761        section != NULL;
3762        section = section->next)
3763     {
3764       if (top_index < section->index)
3765         top_index = section->index;
3766     }
3767
3768   htab->top_index = top_index;
3769   amt = sizeof (asection *) * (top_index + 1);
3770   input_list = bfd_malloc (amt);
3771   htab->input_list = input_list;
3772   if (input_list == NULL)
3773     return -1;
3774
3775   /* For sections we aren't interested in, mark their entries with a
3776      value we can check later.  */
3777   list = input_list + top_index;
3778   do
3779     *list = bfd_abs_section_ptr;
3780   while (list-- != input_list);
3781
3782   for (section = output_bfd->sections;
3783        section != NULL;
3784        section = section->next)
3785     {
3786       /* FIXME: This is a bit of hack. Currently our .ctors and .dtors
3787        * have PC relative relocs in them but no code flag set.  */
3788       if (((section->flags & SEC_CODE) != 0) ||
3789           strcmp(".ctors", section->name) ||
3790           strcmp(".dtors", section->name))
3791         input_list[section->index] = NULL;
3792     }
3793
3794   return 1;
3795 }
3796
3797 /* The linker repeatedly calls this function for each input section,
3798    in the order that input sections are linked into output sections.
3799    Build lists of input sections to determine groupings between which
3800    we may insert linker stubs.  */
3801
3802 void
3803 elf_metag_next_input_section (struct bfd_link_info *info, asection *isec)
3804 {
3805   struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3806
3807   if (isec->output_section->index <= htab->top_index)
3808     {
3809       asection **list = htab->input_list + isec->output_section->index;
3810       if (*list != bfd_abs_section_ptr)
3811         {
3812           /* Steal the link_sec pointer for our list.  */
3813 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3814           /* This happens to make the list in reverse order,
3815              which is what we want.  */
3816           PREV_SEC (isec) = *list;
3817           *list = isec;
3818         }
3819     }
3820 }
3821
3822 /* See whether we can group stub sections together.  Grouping stub
3823    sections may result in fewer stubs.  More importantly, we need to
3824    put all .init* and .fini* stubs at the beginning of the .init or
3825    .fini output sections respectively, because glibc splits the
3826    _init and _fini functions into multiple parts.  Putting a stub in
3827    the middle of a function is not a good idea.  */
3828
3829 static void
3830 group_sections (struct elf_metag_link_hash_table *htab,
3831                 bfd_size_type stub_group_size,
3832                 bfd_boolean stubs_always_before_branch)
3833 {
3834   asection **list = htab->input_list + htab->top_index;
3835   do
3836     {
3837       asection *tail = *list;
3838       if (tail == bfd_abs_section_ptr)
3839         continue;
3840       while (tail != NULL)
3841         {
3842           asection *curr;
3843           asection *prev;
3844           bfd_size_type total;
3845           bfd_boolean big_sec;
3846
3847           curr = tail;
3848           total = tail->size;
3849           big_sec = total >= stub_group_size;
3850
3851           while ((prev = PREV_SEC (curr)) != NULL
3852                  && ((total += curr->output_offset - prev->output_offset)
3853                      < stub_group_size))
3854             curr = prev;
3855
3856           /* OK, the size from the start of CURR to the end is less
3857              than stub_group_size bytes and thus can be handled by one stub
3858              section.  (or the tail section is itself larger than
3859              stub_group_size bytes, in which case we may be toast.)
3860              We should really be keeping track of the total size of
3861              stubs added here, as stubs contribute to the final output
3862              section size.  */
3863           do
3864             {
3865               prev = PREV_SEC (tail);
3866               /* Set up this stub group.  */
3867               htab->stub_group[tail->id].link_sec = curr;
3868             }
3869           while (tail != curr && (tail = prev) != NULL);
3870
3871           /* But wait, there's more!  Input sections up to stub_group_size
3872              bytes before the stub section can be handled by it too.
3873              Don't do this if we have a really large section after the
3874              stubs, as adding more stubs increases the chance that
3875              branches may not reach into the stub section.  */
3876           if (!stubs_always_before_branch && !big_sec)
3877             {
3878               total = 0;
3879               while (prev != NULL
3880                      && ((total += tail->output_offset - prev->output_offset)
3881                          < stub_group_size))
3882                 {
3883                   tail = prev;
3884                   prev = PREV_SEC (tail);
3885                   htab->stub_group[tail->id].link_sec = curr;
3886                 }
3887             }
3888           tail = prev;
3889         }
3890     }
3891   while (list-- != htab->input_list);
3892   free (htab->input_list);
3893 #undef PREV_SEC
3894 }
3895
3896 /* Read in all local syms for all input bfds.
3897    Returns -1 on error, 0 otherwise.  */
3898
3899 static int
3900 get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd,
3901                 struct bfd_link_info *info)
3902 {
3903   unsigned int bfd_indx;
3904   Elf_Internal_Sym *local_syms, **all_local_syms;
3905   int stub_changed = 0;
3906   struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3907
3908   /* We want to read in symbol extension records only once.  To do this
3909      we need to read in the local symbols in parallel and save them for
3910      later use; so hold pointers to the local symbols in an array.  */
3911   bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3912   all_local_syms = bfd_zmalloc (amt);
3913   htab->all_local_syms = all_local_syms;
3914   if (all_local_syms == NULL)
3915     return -1;
3916
3917   /* Walk over all the input BFDs, swapping in local symbols.  */
3918   for (bfd_indx = 0;
3919        input_bfd != NULL;
3920        input_bfd = input_bfd->link.next, bfd_indx++)
3921     {
3922       Elf_Internal_Shdr *symtab_hdr;
3923
3924       /* We'll need the symbol table in a second.  */
3925       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3926       if (symtab_hdr->sh_info == 0)
3927         continue;
3928
3929       /* We need an array of the local symbols attached to the input bfd.  */
3930       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3931       if (local_syms == NULL)
3932         {
3933           local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3934                                              symtab_hdr->sh_info, 0,
3935                                              NULL, NULL, NULL);
3936           /* Cache them for elf_link_input_bfd.  */
3937           symtab_hdr->contents = (unsigned char *) local_syms;
3938         }
3939       if (local_syms == NULL)
3940         return -1;
3941
3942       all_local_syms[bfd_indx] = local_syms;
3943     }
3944
3945   return stub_changed;
3946 }
3947
3948 /* Determine and set the size of the stub section for a final link.
3949
3950 The basic idea here is to examine all the relocations looking for
3951 PC-relative calls to a target that is unreachable with a "CALLR"
3952 instruction.  */
3953
3954 /* See elf32-hppa.c and elf64-ppc.c.  */
3955
3956 bfd_boolean
3957 elf_metag_size_stubs(bfd *output_bfd, bfd *stub_bfd,
3958                      struct bfd_link_info *info,
3959                      bfd_signed_vma group_size,
3960                      asection * (*add_stub_section) (const char *, asection *),
3961                      void (*layout_sections_again) (void))
3962 {
3963   bfd_size_type stub_group_size;
3964   bfd_boolean stubs_always_before_branch;
3965   bfd_boolean stub_changed;
3966   struct elf_metag_link_hash_table *htab = metag_link_hash_table (info);
3967
3968   /* Stash our params away.  */
3969   htab->stub_bfd = stub_bfd;
3970   htab->add_stub_section = add_stub_section;
3971   htab->layout_sections_again = layout_sections_again;
3972   stubs_always_before_branch = group_size < 0;
3973   if (group_size < 0)
3974     stub_group_size = -group_size;
3975   else
3976     stub_group_size = group_size;
3977   if (stub_group_size == 1)
3978     {
3979       /* Default values.  */
3980       /* FIXME: not sure what these values should be */
3981       if (stubs_always_before_branch)
3982         {
3983           stub_group_size = (1 << BRANCH_BITS);
3984         }
3985       else
3986         {
3987           stub_group_size = (1 << BRANCH_BITS);
3988         }
3989     }
3990
3991   group_sections (htab, stub_group_size, stubs_always_before_branch);
3992
3993   switch (get_local_syms (output_bfd, info->input_bfds, info))
3994     {
3995     default:
3996       if (htab->all_local_syms)
3997         goto error_ret_free_local;
3998       return FALSE;
3999
4000     case 0:
4001       stub_changed = FALSE;
4002       break;
4003
4004     case 1:
4005       stub_changed = TRUE;
4006       break;
4007     }
4008
4009   while (1)
4010     {
4011       bfd *input_bfd;
4012       unsigned int bfd_indx;
4013       asection *stub_sec;
4014
4015       for (input_bfd = info->input_bfds, bfd_indx = 0;
4016            input_bfd != NULL;
4017            input_bfd = input_bfd->link.next, bfd_indx++)
4018         {
4019           Elf_Internal_Shdr *symtab_hdr;
4020           asection *section;
4021           Elf_Internal_Sym *local_syms;
4022
4023           /* We'll need the symbol table in a second.  */
4024           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4025           if (symtab_hdr->sh_info == 0)
4026             continue;
4027
4028           local_syms = htab->all_local_syms[bfd_indx];
4029
4030           /* Walk over each section attached to the input bfd.  */
4031           for (section = input_bfd->sections;
4032                section != NULL;
4033                section = section->next)
4034             {
4035               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4036
4037               /* If there aren't any relocs, then there's nothing more
4038                  to do.  */
4039               if ((section->flags & SEC_RELOC) == 0
4040                   || section->reloc_count == 0)
4041                 continue;
4042
4043               /* If this section is a link-once section that will be
4044                  discarded, then don't create any stubs.  */
4045               if (section->output_section == NULL
4046                   || section->output_section->owner != output_bfd)
4047                 continue;
4048
4049               /* Get the relocs.  */
4050               internal_relocs
4051                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
4052                                              info->keep_memory);
4053               if (internal_relocs == NULL)
4054                 goto error_ret_free_local;
4055
4056               /* Now examine each relocation.  */
4057               irela = internal_relocs;
4058               irelaend = irela + section->reloc_count;
4059               for (; irela < irelaend; irela++)
4060                 {
4061                   unsigned int r_type, r_indx;
4062                   enum elf_metag_stub_type stub_type;
4063                   struct elf_metag_stub_hash_entry *hsh;
4064                   asection *sym_sec;
4065                   bfd_vma sym_value;
4066                   bfd_vma destination;
4067                   struct elf_metag_link_hash_entry *hh;
4068                   char *stub_name;
4069                   const asection *id_sec;
4070
4071                   r_type = ELF32_R_TYPE (irela->r_info);
4072                   r_indx = ELF32_R_SYM (irela->r_info);
4073
4074                   if (r_type >= (unsigned int) R_METAG_MAX)
4075                     {
4076                       bfd_set_error (bfd_error_bad_value);
4077                     error_ret_free_internal:
4078                       if (elf_section_data (section)->relocs == NULL)
4079                         free (internal_relocs);
4080                       goto error_ret_free_local;
4081                     }
4082
4083                   /* Only look for stubs on CALLR and B instructions.  */
4084                   if (!(r_type == (unsigned int) R_METAG_RELBRANCH ||
4085                         r_type == (unsigned int) R_METAG_RELBRANCH_PLT))
4086                     continue;
4087
4088                   /* Now determine the call target, its name, value,
4089                      section.  */
4090                   sym_sec = NULL;
4091                   sym_value = 0;
4092                   destination = 0;
4093                   hh = NULL;
4094                   if (r_indx < symtab_hdr->sh_info)
4095                     {
4096                       /* It's a local symbol.  */
4097                       Elf_Internal_Sym *sym;
4098                       Elf_Internal_Shdr *hdr;
4099                       unsigned int shndx;
4100
4101                       sym = local_syms + r_indx;
4102                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4103                         sym_value = sym->st_value;
4104                       shndx = sym->st_shndx;
4105                       if (shndx < elf_numsections (input_bfd))
4106                         {
4107                           hdr = elf_elfsections (input_bfd)[shndx];
4108                           sym_sec = hdr->bfd_section;
4109                           destination = (sym_value + irela->r_addend
4110                                          + sym_sec->output_offset
4111                                          + sym_sec->output_section->vma);
4112                         }
4113                     }
4114                   else
4115                     {
4116                       /* It's an external symbol.  */
4117                       int e_indx;
4118
4119                       e_indx = r_indx - symtab_hdr->sh_info;
4120                       hh = ((struct elf_metag_link_hash_entry *)
4121                             elf_sym_hashes (input_bfd)[e_indx]);
4122
4123                       while (hh->eh.root.type == bfd_link_hash_indirect
4124                              || hh->eh.root.type == bfd_link_hash_warning)
4125                         hh = ((struct elf_metag_link_hash_entry *)
4126                               hh->eh.root.u.i.link);
4127
4128                       if (hh->eh.root.type == bfd_link_hash_defined
4129                           || hh->eh.root.type == bfd_link_hash_defweak)
4130                         {
4131                           sym_sec = hh->eh.root.u.def.section;
4132                           sym_value = hh->eh.root.u.def.value;
4133                           if (hh->eh.plt.offset != (bfd_vma) -1
4134                               && hh->eh.dynindx != -1
4135                               && r_type == (unsigned int) R_METAG_RELBRANCH_PLT)
4136                             {
4137                               sym_sec = htab->splt;
4138                               sym_value = hh->eh.plt.offset;
4139                             }
4140
4141                           if (sym_sec->output_section != NULL)
4142                             destination = (sym_value + irela->r_addend
4143                                            + sym_sec->output_offset
4144                                            + sym_sec->output_section->vma);
4145                           else
4146                             continue;
4147                         }
4148                       else if (hh->eh.root.type == bfd_link_hash_undefweak)
4149                         {
4150                           if (! bfd_link_pic (info))
4151                             continue;
4152                         }
4153                       else if (hh->eh.root.type == bfd_link_hash_undefined)
4154                         {
4155                           if (! (info->unresolved_syms_in_objects == RM_IGNORE
4156                                  && (ELF_ST_VISIBILITY (hh->eh.other)
4157                                      == STV_DEFAULT)))
4158                             continue;
4159                         }
4160                       else
4161                         {
4162                           bfd_set_error (bfd_error_bad_value);
4163                           goto error_ret_free_internal;
4164                         }
4165                     }
4166
4167                   /* Determine what (if any) linker stub is needed.  */
4168                   stub_type = metag_type_of_stub (section, irela, hh,
4169                                                   destination, info);
4170                   if (stub_type == metag_stub_none)
4171                     continue;
4172
4173                   /* Support for grouping stub sections.  */
4174                   id_sec = htab->stub_group[section->id].link_sec;
4175
4176                   /* Get the name of this stub.  */
4177                   stub_name = metag_stub_name (id_sec, sym_sec, hh, irela);
4178                   if (!stub_name)
4179                     goto error_ret_free_internal;
4180
4181                   hsh = metag_stub_hash_lookup (&htab->bstab,
4182                                                 stub_name,
4183                                                 FALSE, FALSE);
4184                   if (hsh != NULL)
4185                     {
4186                       /* The proper stub has already been created.  */
4187                       free (stub_name);
4188                       continue;
4189                     }
4190
4191                   hsh = metag_add_stub (stub_name, section, htab);
4192                   if (hsh == NULL)
4193                     {
4194                       free (stub_name);
4195                       goto error_ret_free_internal;
4196                     }
4197                   hsh->target_value = sym_value;
4198                   hsh->target_section = sym_sec;
4199                   hsh->stub_type = stub_type;
4200                   hsh->hh = hh;
4201                   hsh->addend = irela->r_addend;
4202                   stub_changed = TRUE;
4203                 }
4204
4205               /* We're done with the internal relocs, free them.  */
4206               if (elf_section_data (section)->relocs == NULL)
4207                 free (internal_relocs);
4208             }
4209         }
4210
4211       if (!stub_changed)
4212         break;
4213
4214       /* OK, we've added some stubs.  Find out the new size of the
4215          stub sections.  */
4216       for (stub_sec = htab->stub_bfd->sections;
4217            stub_sec != NULL;
4218            stub_sec = stub_sec->next)
4219         stub_sec->size = 0;
4220
4221       bfd_hash_traverse (&htab->bstab, metag_size_one_stub, htab);
4222
4223       /* Ask the linker to do its stuff.  */
4224       (*htab->layout_sections_again) ();
4225       stub_changed = FALSE;
4226     }
4227
4228   free (htab->all_local_syms);
4229   return TRUE;
4230
4231  error_ret_free_local:
4232   free (htab->all_local_syms);
4233   return FALSE;
4234 }
4235
4236 /* Build all the stubs associated with the current output file.  The
4237    stubs are kept in a hash table attached to the main linker hash
4238    table.  This function is called via metagelf_finish in the linker.  */
4239
4240 bfd_boolean
4241 elf_metag_build_stubs (struct bfd_link_info *info)
4242 {
4243   asection *stub_sec;
4244   struct bfd_hash_table *table;
4245   struct elf_metag_link_hash_table *htab;
4246
4247   htab = metag_link_hash_table (info);
4248
4249   for (stub_sec = htab->stub_bfd->sections;
4250        stub_sec != NULL;
4251        stub_sec = stub_sec->next)
4252     {
4253       bfd_size_type size;
4254
4255       /* Allocate memory to hold the linker stubs.  */
4256       size = stub_sec->size;
4257       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
4258       if (stub_sec->contents == NULL && size != 0)
4259         return FALSE;
4260       stub_sec->size = 0;
4261     }
4262
4263   /* Build the stubs as directed by the stub hash table.  */
4264   table = &htab->bstab;
4265   bfd_hash_traverse (table, metag_build_one_stub, info);
4266
4267   return TRUE;
4268 }
4269
4270 /* Return TRUE if SYM represents a local label symbol.  */
4271
4272 static bfd_boolean
4273 elf_metag_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
4274 {
4275   if (name[0] == '$' && name[1] == 'L')
4276     return 1;
4277   return _bfd_elf_is_local_label_name (abfd, name);
4278 }
4279
4280 /* Return address for Ith PLT stub in section PLT, for relocation REL
4281    or (bfd_vma) -1 if it should not be included.  */
4282
4283 static bfd_vma
4284 elf_metag_plt_sym_val (bfd_vma i, const asection *plt,
4285                        const arelent *rel ATTRIBUTE_UNUSED)
4286 {
4287   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4288 }
4289
4290 #define ELF_ARCH                bfd_arch_metag
4291 #define ELF_TARGET_ID           METAG_ELF_DATA
4292 #define ELF_MACHINE_CODE        EM_METAG
4293 #define ELF_MINPAGESIZE 0x1000
4294 #define ELF_MAXPAGESIZE 0x4000
4295 #define ELF_COMMONPAGESIZE      0x1000
4296
4297 #define TARGET_LITTLE_SYM       metag_elf32_vec
4298 #define TARGET_LITTLE_NAME      "elf32-metag"
4299
4300 #define elf_symbol_leading_char '_'
4301
4302 #define elf_info_to_howto_rel                   NULL
4303 #define elf_info_to_howto                       metag_info_to_howto_rela
4304
4305 #define bfd_elf32_bfd_is_local_label_name       elf_metag_is_local_label_name
4306 #define bfd_elf32_bfd_link_hash_table_create \
4307         elf_metag_link_hash_table_create
4308 #define elf_backend_relocate_section            elf_metag_relocate_section
4309 #define elf_backend_gc_mark_hook                elf_metag_gc_mark_hook
4310 #define elf_backend_gc_sweep_hook               elf_metag_gc_sweep_hook
4311 #define elf_backend_check_relocs                elf_metag_check_relocs
4312 #define elf_backend_create_dynamic_sections     elf_metag_create_dynamic_sections
4313 #define elf_backend_adjust_dynamic_symbol       elf_metag_adjust_dynamic_symbol
4314 #define elf_backend_finish_dynamic_symbol       elf_metag_finish_dynamic_symbol
4315 #define elf_backend_finish_dynamic_sections     elf_metag_finish_dynamic_sections
4316 #define elf_backend_size_dynamic_sections       elf_metag_size_dynamic_sections
4317 #define elf_backend_omit_section_dynsym \
4318   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4319 #define elf_backend_post_process_headers        elf_metag_post_process_headers
4320 #define elf_backend_reloc_type_class            elf_metag_reloc_type_class
4321 #define elf_backend_copy_indirect_symbol        elf_metag_copy_indirect_symbol
4322 #define elf_backend_plt_sym_val         elf_metag_plt_sym_val
4323
4324 #define elf_backend_can_gc_sections             1
4325 #define elf_backend_can_refcount                1
4326 #define elf_backend_got_header_size             12
4327 #define elf_backend_rela_normal         1
4328 #define elf_backend_want_got_sym                0
4329 #define elf_backend_want_plt_sym                0
4330 #define elf_backend_plt_readonly                1
4331
4332 #define bfd_elf32_bfd_reloc_type_lookup metag_reloc_type_lookup
4333 #define bfd_elf32_bfd_reloc_name_lookup metag_reloc_name_lookup
4334
4335 #include "elf32-target.h"