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