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