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