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