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