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