xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elf32-m68k.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* Motorola 68k series support for 32-bit ELF
2    Copyright (C) 1993-2016 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/m68k.h"
27 #include "opcode/m68k.h"
28 
29 static bfd_boolean
30 elf_m68k_discard_copies (struct elf_link_hash_entry *, void *);
31 
32 static reloc_howto_type howto_table[] =
33 {
34   HOWTO(R_68K_NONE,       0, 3, 0, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_NONE",      FALSE, 0, 0x00000000,FALSE),
35   HOWTO(R_68K_32,         0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32",        FALSE, 0, 0xffffffff,FALSE),
36   HOWTO(R_68K_16,         0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16",        FALSE, 0, 0x0000ffff,FALSE),
37   HOWTO(R_68K_8,          0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8",         FALSE, 0, 0x000000ff,FALSE),
38   HOWTO(R_68K_PC32,       0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32",      FALSE, 0, 0xffffffff,TRUE),
39   HOWTO(R_68K_PC16,       0, 1,16, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC16",      FALSE, 0, 0x0000ffff,TRUE),
40   HOWTO(R_68K_PC8,        0, 0, 8, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC8",       FALSE, 0, 0x000000ff,TRUE),
41   HOWTO(R_68K_GOT32,      0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32",     FALSE, 0, 0xffffffff,TRUE),
42   HOWTO(R_68K_GOT16,      0, 1,16, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT16",     FALSE, 0, 0x0000ffff,TRUE),
43   HOWTO(R_68K_GOT8,       0, 0, 8, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT8",      FALSE, 0, 0x000000ff,TRUE),
44   HOWTO(R_68K_GOT32O,     0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O",    FALSE, 0, 0xffffffff,FALSE),
45   HOWTO(R_68K_GOT16O,     0, 1,16, FALSE,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT16O",    FALSE, 0, 0x0000ffff,FALSE),
46   HOWTO(R_68K_GOT8O,      0, 0, 8, FALSE,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT8O",     FALSE, 0, 0x000000ff,FALSE),
47   HOWTO(R_68K_PLT32,      0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32",     FALSE, 0, 0xffffffff,TRUE),
48   HOWTO(R_68K_PLT16,      0, 1,16, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT16",     FALSE, 0, 0x0000ffff,TRUE),
49   HOWTO(R_68K_PLT8,       0, 0, 8, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT8",      FALSE, 0, 0x000000ff,TRUE),
50   HOWTO(R_68K_PLT32O,     0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O",    FALSE, 0, 0xffffffff,FALSE),
51   HOWTO(R_68K_PLT16O,     0, 1,16, FALSE,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT16O",    FALSE, 0, 0x0000ffff,FALSE),
52   HOWTO(R_68K_PLT8O,      0, 0, 8, FALSE,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT8O",     FALSE, 0, 0x000000ff,FALSE),
53   HOWTO(R_68K_COPY,       0, 0, 0, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_COPY",      FALSE, 0, 0xffffffff,FALSE),
54   HOWTO(R_68K_GLOB_DAT,   0, 2,32, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_GLOB_DAT",  FALSE, 0, 0xffffffff,FALSE),
55   HOWTO(R_68K_JMP_SLOT,   0, 2,32, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_JMP_SLOT",  FALSE, 0, 0xffffffff,FALSE),
56   HOWTO(R_68K_RELATIVE,   0, 2,32, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_RELATIVE",  FALSE, 0, 0xffffffff,FALSE),
57   /* GNU extension to record C++ vtable hierarchy.  */
58   HOWTO (R_68K_GNU_VTINHERIT,	/* type */
59 	 0,			/* rightshift */
60 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
61 	 0,			/* bitsize */
62 	 FALSE,			/* pc_relative */
63 	 0,			/* bitpos */
64 	 complain_overflow_dont, /* complain_on_overflow */
65 	 NULL,			/* special_function */
66 	 "R_68K_GNU_VTINHERIT",	/* name */
67 	 FALSE,			/* partial_inplace */
68 	 0,			/* src_mask */
69 	 0,			/* dst_mask */
70 	 FALSE),
71   /* GNU extension to record C++ vtable member usage.  */
72   HOWTO (R_68K_GNU_VTENTRY,	/* type */
73 	 0,			/* rightshift */
74 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
75 	 0,			/* bitsize */
76 	 FALSE,			/* pc_relative */
77 	 0,			/* bitpos */
78 	 complain_overflow_dont, /* complain_on_overflow */
79 	 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
80 	 "R_68K_GNU_VTENTRY",	/* name */
81 	 FALSE,			/* partial_inplace */
82 	 0,			/* src_mask */
83 	 0,			/* dst_mask */
84 	 FALSE),
85 
86   /* TLS general dynamic variable reference.  */
87   HOWTO (R_68K_TLS_GD32,	/* type */
88 	 0,			/* rightshift */
89 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
90 	 32,			/* bitsize */
91 	 FALSE,			/* pc_relative */
92 	 0,			/* bitpos */
93 	 complain_overflow_bitfield, /* complain_on_overflow */
94 	 bfd_elf_generic_reloc, /* special_function */
95 	 "R_68K_TLS_GD32",	/* name */
96 	 FALSE,			/* partial_inplace */
97 	 0,			/* src_mask */
98 	 0xffffffff,		/* dst_mask */
99 	 FALSE),		/* pcrel_offset */
100 
101   HOWTO (R_68K_TLS_GD16,	/* type */
102 	 0,			/* rightshift */
103 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
104 	 16,			/* bitsize */
105 	 FALSE,			/* pc_relative */
106 	 0,			/* bitpos */
107 	 complain_overflow_signed, /* complain_on_overflow */
108 	 bfd_elf_generic_reloc, /* special_function */
109 	 "R_68K_TLS_GD16",	/* name */
110 	 FALSE,			/* partial_inplace */
111 	 0,			/* src_mask */
112 	 0x0000ffff,		/* dst_mask */
113 	 FALSE),		/* pcrel_offset */
114 
115   HOWTO (R_68K_TLS_GD8,		/* type */
116 	 0,			/* rightshift */
117 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
118 	 8,			/* bitsize */
119 	 FALSE,			/* pc_relative */
120 	 0,			/* bitpos */
121 	 complain_overflow_signed, /* complain_on_overflow */
122 	 bfd_elf_generic_reloc, /* special_function */
123 	 "R_68K_TLS_GD8",	/* name */
124 	 FALSE,			/* partial_inplace */
125 	 0,			/* src_mask */
126 	 0x000000ff,		/* dst_mask */
127 	 FALSE),		/* pcrel_offset */
128 
129   /* TLS local dynamic variable reference.  */
130   HOWTO (R_68K_TLS_LDM32,	/* type */
131 	 0,			/* rightshift */
132 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
133 	 32,			/* bitsize */
134 	 FALSE,			/* pc_relative */
135 	 0,			/* bitpos */
136 	 complain_overflow_bitfield, /* complain_on_overflow */
137 	 bfd_elf_generic_reloc, /* special_function */
138 	 "R_68K_TLS_LDM32",	/* name */
139 	 FALSE,			/* partial_inplace */
140 	 0,			/* src_mask */
141 	 0xffffffff,		/* dst_mask */
142 	 FALSE),		/* pcrel_offset */
143 
144   HOWTO (R_68K_TLS_LDM16,	/* type */
145 	 0,			/* rightshift */
146 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
147 	 16,			/* bitsize */
148 	 FALSE,			/* pc_relative */
149 	 0,			/* bitpos */
150 	 complain_overflow_signed, /* complain_on_overflow */
151 	 bfd_elf_generic_reloc, /* special_function */
152 	 "R_68K_TLS_LDM16",	/* name */
153 	 FALSE,			/* partial_inplace */
154 	 0,			/* src_mask */
155 	 0x0000ffff,		/* dst_mask */
156 	 FALSE),		/* pcrel_offset */
157 
158   HOWTO (R_68K_TLS_LDM8,		/* type */
159 	 0,			/* rightshift */
160 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
161 	 8,			/* bitsize */
162 	 FALSE,			/* pc_relative */
163 	 0,			/* bitpos */
164 	 complain_overflow_signed, /* complain_on_overflow */
165 	 bfd_elf_generic_reloc, /* special_function */
166 	 "R_68K_TLS_LDM8",	/* name */
167 	 FALSE,			/* partial_inplace */
168 	 0,			/* src_mask */
169 	 0x000000ff,		/* dst_mask */
170 	 FALSE),		/* pcrel_offset */
171 
172   HOWTO (R_68K_TLS_LDO32,	/* type */
173 	 0,			/* rightshift */
174 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
175 	 32,			/* bitsize */
176 	 FALSE,			/* pc_relative */
177 	 0,			/* bitpos */
178 	 complain_overflow_bitfield, /* complain_on_overflow */
179 	 bfd_elf_generic_reloc, /* special_function */
180 	 "R_68K_TLS_LDO32",	/* name */
181 	 FALSE,			/* partial_inplace */
182 	 0,			/* src_mask */
183 	 0xffffffff,		/* dst_mask */
184 	 FALSE),		/* pcrel_offset */
185 
186   HOWTO (R_68K_TLS_LDO16,	/* type */
187 	 0,			/* rightshift */
188 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
189 	 16,			/* bitsize */
190 	 FALSE,			/* pc_relative */
191 	 0,			/* bitpos */
192 	 complain_overflow_signed, /* complain_on_overflow */
193 	 bfd_elf_generic_reloc, /* special_function */
194 	 "R_68K_TLS_LDO16",	/* name */
195 	 FALSE,			/* partial_inplace */
196 	 0,			/* src_mask */
197 	 0x0000ffff,		/* dst_mask */
198 	 FALSE),		/* pcrel_offset */
199 
200   HOWTO (R_68K_TLS_LDO8,		/* type */
201 	 0,			/* rightshift */
202 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
203 	 8,			/* bitsize */
204 	 FALSE,			/* pc_relative */
205 	 0,			/* bitpos */
206 	 complain_overflow_signed, /* complain_on_overflow */
207 	 bfd_elf_generic_reloc, /* special_function */
208 	 "R_68K_TLS_LDO8",	/* name */
209 	 FALSE,			/* partial_inplace */
210 	 0,			/* src_mask */
211 	 0x000000ff,		/* dst_mask */
212 	 FALSE),		/* pcrel_offset */
213 
214   /* TLS initial execution variable reference.  */
215   HOWTO (R_68K_TLS_IE32,	/* type */
216 	 0,			/* rightshift */
217 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
218 	 32,			/* bitsize */
219 	 FALSE,			/* pc_relative */
220 	 0,			/* bitpos */
221 	 complain_overflow_bitfield, /* complain_on_overflow */
222 	 bfd_elf_generic_reloc, /* special_function */
223 	 "R_68K_TLS_IE32",	/* name */
224 	 FALSE,			/* partial_inplace */
225 	 0,			/* src_mask */
226 	 0xffffffff,		/* dst_mask */
227 	 FALSE),		/* pcrel_offset */
228 
229   HOWTO (R_68K_TLS_IE16,	/* type */
230 	 0,			/* rightshift */
231 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
232 	 16,			/* bitsize */
233 	 FALSE,			/* pc_relative */
234 	 0,			/* bitpos */
235 	 complain_overflow_signed, /* complain_on_overflow */
236 	 bfd_elf_generic_reloc, /* special_function */
237 	 "R_68K_TLS_IE16",	/* name */
238 	 FALSE,			/* partial_inplace */
239 	 0,			/* src_mask */
240 	 0x0000ffff,		/* dst_mask */
241 	 FALSE),		/* pcrel_offset */
242 
243   HOWTO (R_68K_TLS_IE8,		/* type */
244 	 0,			/* rightshift */
245 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
246 	 8,			/* bitsize */
247 	 FALSE,			/* pc_relative */
248 	 0,			/* bitpos */
249 	 complain_overflow_signed, /* complain_on_overflow */
250 	 bfd_elf_generic_reloc, /* special_function */
251 	 "R_68K_TLS_IE8",	/* name */
252 	 FALSE,			/* partial_inplace */
253 	 0,			/* src_mask */
254 	 0x000000ff,		/* dst_mask */
255 	 FALSE),		/* pcrel_offset */
256 
257   /* TLS local execution variable reference.  */
258   HOWTO (R_68K_TLS_LE32,	/* type */
259 	 0,			/* rightshift */
260 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
261 	 32,			/* bitsize */
262 	 FALSE,			/* pc_relative */
263 	 0,			/* bitpos */
264 	 complain_overflow_bitfield, /* complain_on_overflow */
265 	 bfd_elf_generic_reloc, /* special_function */
266 	 "R_68K_TLS_LE32",	/* name */
267 	 FALSE,			/* partial_inplace */
268 	 0,			/* src_mask */
269 	 0xffffffff,		/* dst_mask */
270 	 FALSE),		/* pcrel_offset */
271 
272   HOWTO (R_68K_TLS_LE16,	/* type */
273 	 0,			/* rightshift */
274 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
275 	 16,			/* bitsize */
276 	 FALSE,			/* pc_relative */
277 	 0,			/* bitpos */
278 	 complain_overflow_signed, /* complain_on_overflow */
279 	 bfd_elf_generic_reloc, /* special_function */
280 	 "R_68K_TLS_LE16",	/* name */
281 	 FALSE,			/* partial_inplace */
282 	 0,			/* src_mask */
283 	 0x0000ffff,		/* dst_mask */
284 	 FALSE),		/* pcrel_offset */
285 
286   HOWTO (R_68K_TLS_LE8,		/* type */
287 	 0,			/* rightshift */
288 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
289 	 8,			/* bitsize */
290 	 FALSE,			/* pc_relative */
291 	 0,			/* bitpos */
292 	 complain_overflow_signed, /* complain_on_overflow */
293 	 bfd_elf_generic_reloc, /* special_function */
294 	 "R_68K_TLS_LE8",	/* name */
295 	 FALSE,			/* partial_inplace */
296 	 0,			/* src_mask */
297 	 0x000000ff,		/* dst_mask */
298 	 FALSE),		/* pcrel_offset */
299 
300   /* TLS GD/LD dynamic relocations.  */
301   HOWTO (R_68K_TLS_DTPMOD32,	/* type */
302 	 0,			/* rightshift */
303 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
304 	 32,			/* bitsize */
305 	 FALSE,			/* pc_relative */
306 	 0,			/* bitpos */
307 	 complain_overflow_dont, /* complain_on_overflow */
308 	 bfd_elf_generic_reloc, /* special_function */
309 	 "R_68K_TLS_DTPMOD32",	/* name */
310 	 FALSE,			/* partial_inplace */
311 	 0,			/* src_mask */
312 	 0xffffffff,		/* dst_mask */
313 	 FALSE),		/* pcrel_offset */
314 
315   HOWTO (R_68K_TLS_DTPREL32,	/* type */
316 	 0,			/* rightshift */
317 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
318 	 32,			/* bitsize */
319 	 FALSE,			/* pc_relative */
320 	 0,			/* bitpos */
321 	 complain_overflow_dont, /* complain_on_overflow */
322 	 bfd_elf_generic_reloc, /* special_function */
323 	 "R_68K_TLS_DTPREL32",	/* name */
324 	 FALSE,			/* partial_inplace */
325 	 0,			/* src_mask */
326 	 0xffffffff,		/* dst_mask */
327 	 FALSE),		/* pcrel_offset */
328 
329   HOWTO (R_68K_TLS_TPREL32,	/* type */
330 	 0,			/* rightshift */
331 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
332 	 32,			/* bitsize */
333 	 FALSE,			/* pc_relative */
334 	 0,			/* bitpos */
335 	 complain_overflow_dont, /* complain_on_overflow */
336 	 bfd_elf_generic_reloc, /* special_function */
337 	 "R_68K_TLS_TPREL32",	/* name */
338 	 FALSE,			/* partial_inplace */
339 	 0,			/* src_mask */
340 	 0xffffffff,		/* dst_mask */
341 	 FALSE),		/* pcrel_offset */
342 };
343 
344 static void
345 rtype_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
346 {
347   unsigned int indx = ELF32_R_TYPE (dst->r_info);
348 
349   if (indx >= (unsigned int) R_68K_max)
350     {
351       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
352 			     abfd, (int) indx);
353       indx = R_68K_NONE;
354     }
355   cache_ptr->howto = &howto_table[indx];
356 }
357 
358 #define elf_info_to_howto rtype_to_howto
359 
360 static const struct
361 {
362   bfd_reloc_code_real_type bfd_val;
363   int elf_val;
364 }
365   reloc_map[] =
366 {
367   { BFD_RELOC_NONE, R_68K_NONE },
368   { BFD_RELOC_32, R_68K_32 },
369   { BFD_RELOC_16, R_68K_16 },
370   { BFD_RELOC_8, R_68K_8 },
371   { BFD_RELOC_32_PCREL, R_68K_PC32 },
372   { BFD_RELOC_16_PCREL, R_68K_PC16 },
373   { BFD_RELOC_8_PCREL, R_68K_PC8 },
374   { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
375   { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
376   { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
377   { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
378   { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
379   { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
380   { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
381   { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
382   { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
383   { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
384   { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
385   { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
386   { BFD_RELOC_NONE, R_68K_COPY },
387   { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
388   { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
389   { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
390   { BFD_RELOC_CTOR, R_68K_32 },
391   { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
392   { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
393   { BFD_RELOC_68K_TLS_GD32, R_68K_TLS_GD32 },
394   { BFD_RELOC_68K_TLS_GD16, R_68K_TLS_GD16 },
395   { BFD_RELOC_68K_TLS_GD8, R_68K_TLS_GD8 },
396   { BFD_RELOC_68K_TLS_LDM32, R_68K_TLS_LDM32 },
397   { BFD_RELOC_68K_TLS_LDM16, R_68K_TLS_LDM16 },
398   { BFD_RELOC_68K_TLS_LDM8, R_68K_TLS_LDM8 },
399   { BFD_RELOC_68K_TLS_LDO32, R_68K_TLS_LDO32 },
400   { BFD_RELOC_68K_TLS_LDO16, R_68K_TLS_LDO16 },
401   { BFD_RELOC_68K_TLS_LDO8, R_68K_TLS_LDO8 },
402   { BFD_RELOC_68K_TLS_IE32, R_68K_TLS_IE32 },
403   { BFD_RELOC_68K_TLS_IE16, R_68K_TLS_IE16 },
404   { BFD_RELOC_68K_TLS_IE8, R_68K_TLS_IE8 },
405   { BFD_RELOC_68K_TLS_LE32, R_68K_TLS_LE32 },
406   { BFD_RELOC_68K_TLS_LE16, R_68K_TLS_LE16 },
407   { BFD_RELOC_68K_TLS_LE8, R_68K_TLS_LE8 },
408 };
409 
410 static reloc_howto_type *
411 reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
412 		   bfd_reloc_code_real_type code)
413 {
414   unsigned int i;
415   for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
416     {
417       if (reloc_map[i].bfd_val == code)
418 	return &howto_table[reloc_map[i].elf_val];
419     }
420   return 0;
421 }
422 
423 static reloc_howto_type *
424 reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
425 {
426   unsigned int i;
427 
428   for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
429     if (howto_table[i].name != NULL
430 	&& strcasecmp (howto_table[i].name, r_name) == 0)
431       return &howto_table[i];
432 
433   return NULL;
434 }
435 
436 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
437 #define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
438 #define ELF_ARCH bfd_arch_m68k
439 #define ELF_TARGET_ID M68K_ELF_DATA
440 
441 /* Functions for the m68k ELF linker.  */
442 
443 /* The name of the dynamic interpreter.  This is put in the .interp
444    section.  */
445 
446 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
447 
448 /* Describes one of the various PLT styles.  */
449 
450 struct elf_m68k_plt_info
451 {
452   /* The size of each PLT entry.  */
453   bfd_vma size;
454 
455   /* The template for the first PLT entry.  */
456   const bfd_byte *plt0_entry;
457 
458   /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
459      The comments by each member indicate the value that the relocation
460      is against.  */
461   struct {
462     unsigned int got4; /* .got + 4 */
463     unsigned int got8; /* .got + 8 */
464   } plt0_relocs;
465 
466   /* The template for a symbol's PLT entry.  */
467   const bfd_byte *symbol_entry;
468 
469   /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
470      The comments by each member indicate the value that the relocation
471      is against.  */
472   struct {
473     unsigned int got; /* the symbol's .got.plt entry */
474     unsigned int plt; /* .plt */
475   } symbol_relocs;
476 
477   /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
478      The stub starts with "move.l #relocoffset,%d0".  */
479   bfd_vma symbol_resolve_entry;
480 };
481 
482 /* The size in bytes of an entry in the procedure linkage table.  */
483 
484 #define PLT_ENTRY_SIZE 20
485 
486 /* The first entry in a procedure linkage table looks like this.  See
487    the SVR4 ABI m68k supplement to see how this works.  */
488 
489 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
490 {
491   0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
492   0, 0, 0, 2,		  /* + (.got + 4) - . */
493   0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
494   0, 0, 0, 2,		  /* + (.got + 8) - . */
495   0, 0, 0, 0		  /* pad out to 20 bytes.  */
496 };
497 
498 /* Subsequent entries in a procedure linkage table look like this.  */
499 
500 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
501 {
502   0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
503   0, 0, 0, 2,		  /* + (.got.plt entry) - . */
504   0x2f, 0x3c,		  /* move.l #offset,-(%sp) */
505   0, 0, 0, 0,		  /* + reloc index */
506   0x60, 0xff,		  /* bra.l .plt */
507   0, 0, 0, 0		  /* + .plt - . */
508 };
509 
510 static const struct elf_m68k_plt_info elf_m68k_plt_info = {
511   PLT_ENTRY_SIZE,
512   elf_m68k_plt0_entry, { 4, 12 },
513   elf_m68k_plt_entry, { 4, 16 }, 8
514 };
515 
516 #define ISAB_PLT_ENTRY_SIZE 24
517 
518 static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
519 {
520   0x20, 0x3c,             /* move.l #offset,%d0 */
521   0, 0, 0, 0,             /* + (.got + 4) - . */
522   0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
523   0x20, 0x3c,             /* move.l #offset,%d0 */
524   0, 0, 0, 0,             /* + (.got + 8) - . */
525   0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
526   0x4e, 0xd0,             /* jmp (%a0) */
527   0x4e, 0x71		  /* nop */
528 };
529 
530 /* Subsequent entries in a procedure linkage table look like this.  */
531 
532 static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
533 {
534   0x20, 0x3c,             /* move.l #offset,%d0 */
535   0, 0, 0, 0,             /* + (.got.plt entry) - . */
536   0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
537   0x4e, 0xd0,             /* jmp (%a0) */
538   0x2f, 0x3c,             /* move.l #offset,-(%sp) */
539   0, 0, 0, 0,             /* + reloc index */
540   0x60, 0xff,             /* bra.l .plt */
541   0, 0, 0, 0              /* + .plt - . */
542 };
543 
544 static const struct elf_m68k_plt_info elf_isab_plt_info = {
545   ISAB_PLT_ENTRY_SIZE,
546   elf_isab_plt0_entry, { 2, 12 },
547   elf_isab_plt_entry, { 2, 20 }, 12
548 };
549 
550 #define ISAC_PLT_ENTRY_SIZE 24
551 
552 static const bfd_byte elf_isac_plt0_entry[ISAC_PLT_ENTRY_SIZE] =
553 {
554   0x20, 0x3c,		  /* move.l #offset,%d0 */
555   0, 0, 0, 0,		  /* replaced with .got + 4 - . */
556   0x2e, 0xbb, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),(%sp) */
557   0x20, 0x3c,		  /* move.l #offset,%d0 */
558   0, 0, 0, 0,		  /* replaced with .got + 8 - . */
559   0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
560   0x4e, 0xd0,		  /* jmp (%a0) */
561   0x4e, 0x71		  /* nop */
562 };
563 
564 /* Subsequent entries in a procedure linkage table look like this.  */
565 
566 static const bfd_byte elf_isac_plt_entry[ISAC_PLT_ENTRY_SIZE] =
567 {
568   0x20, 0x3c,		  /* move.l #offset,%d0 */
569   0, 0, 0, 0,		  /* replaced with (.got entry) - . */
570   0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
571   0x4e, 0xd0,		  /* jmp (%a0) */
572   0x2f, 0x3c,		  /* move.l #offset,-(%sp) */
573   0, 0, 0, 0,		  /* replaced with offset into relocation table */
574   0x61, 0xff,		  /* bsr.l .plt */
575   0, 0, 0, 0 		  /* replaced with .plt - . */
576 };
577 
578 static const struct elf_m68k_plt_info elf_isac_plt_info = {
579   ISAC_PLT_ENTRY_SIZE,
580   elf_isac_plt0_entry, { 2, 12},
581   elf_isac_plt_entry, { 2, 20 }, 12
582 };
583 
584 #define CPU32_PLT_ENTRY_SIZE 24
585 /* Procedure linkage table entries for the cpu32 */
586 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
587 {
588   0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
589   0, 0, 0, 2,             /* + (.got + 4) - . */
590   0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
591   0, 0, 0, 2,             /* + (.got + 8) - . */
592   0x4e, 0xd1,             /* jmp %a1@ */
593   0, 0, 0, 0,             /* pad out to 24 bytes.  */
594   0, 0
595 };
596 
597 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
598 {
599   0x22, 0x7b, 0x01, 0x70,  /* moveal %pc@(0xc), %a1 */
600   0, 0, 0, 2,              /* + (.got.plt entry) - . */
601   0x4e, 0xd1,              /* jmp %a1@ */
602   0x2f, 0x3c,              /* move.l #offset,-(%sp) */
603   0, 0, 0, 0,              /* + reloc index */
604   0x60, 0xff,              /* bra.l .plt */
605   0, 0, 0, 0,              /* + .plt - . */
606   0, 0
607 };
608 
609 static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
610   CPU32_PLT_ENTRY_SIZE,
611   elf_cpu32_plt0_entry, { 4, 12 },
612   elf_cpu32_plt_entry, { 4, 18 }, 10
613 };
614 
615 /* The m68k linker needs to keep track of the number of relocs that it
616    decides to copy in check_relocs for each symbol.  This is so that it
617    can discard PC relative relocs if it doesn't need them when linking
618    with -Bsymbolic.  We store the information in a field extending the
619    regular ELF linker hash table.  */
620 
621 /* This structure keeps track of the number of PC relative relocs we have
622    copied for a given symbol.  */
623 
624 struct elf_m68k_pcrel_relocs_copied
625 {
626   /* Next section.  */
627   struct elf_m68k_pcrel_relocs_copied *next;
628   /* A section in dynobj.  */
629   asection *section;
630   /* Number of relocs copied in this section.  */
631   bfd_size_type count;
632 };
633 
634 /* Forward declaration.  */
635 struct elf_m68k_got_entry;
636 
637 /* m68k ELF linker hash entry.  */
638 
639 struct elf_m68k_link_hash_entry
640 {
641   struct elf_link_hash_entry root;
642 
643   /* Number of PC relative relocs copied for this symbol.  */
644   struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
645 
646   /* Key to got_entries.  */
647   unsigned long got_entry_key;
648 
649   /* List of GOT entries for this symbol.  This list is build during
650      offset finalization and is used within elf_m68k_finish_dynamic_symbol
651      to traverse all GOT entries for a particular symbol.
652 
653      ??? We could've used root.got.glist field instead, but having
654      a separate field is cleaner.  */
655   struct elf_m68k_got_entry *glist;
656 };
657 
658 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
659 
660 /* Key part of GOT entry in hashtable.  */
661 struct elf_m68k_got_entry_key
662 {
663   /* BFD in which this symbol was defined.  NULL for global symbols.  */
664   const bfd *bfd;
665 
666   /* Symbol index.  Either local symbol index or h->got_entry_key.  */
667   unsigned long symndx;
668 
669   /* Type is one of R_68K_GOT{8, 16, 32}O, R_68K_TLS_GD{8, 16, 32},
670      R_68K_TLS_LDM{8, 16, 32} or R_68K_TLS_IE{8, 16, 32}.
671 
672      From perspective of hashtable key, only elf_m68k_got_reloc_type (type)
673      matters.  That is, we distinguish between, say, R_68K_GOT16O
674      and R_68K_GOT32O when allocating offsets, but they are considered to be
675      the same when searching got->entries.  */
676   enum elf_m68k_reloc_type type;
677 };
678 
679 /* Size of the GOT offset suitable for relocation.  */
680 enum elf_m68k_got_offset_size { R_8, R_16, R_32, R_LAST };
681 
682 /* Entry of the GOT.  */
683 struct elf_m68k_got_entry
684 {
685   /* GOT entries are put into a got->entries hashtable.  This is the key.  */
686   struct elf_m68k_got_entry_key key_;
687 
688   /* GOT entry data.  We need s1 before offset finalization and s2 after.  */
689   union
690   {
691     struct
692     {
693       /* Number of times this entry is referenced.  It is used to
694 	 filter out unnecessary GOT slots in elf_m68k_gc_sweep_hook.  */
695       bfd_vma refcount;
696     } s1;
697 
698     struct
699     {
700       /* Offset from the start of .got section.  To calculate offset relative
701 	 to GOT pointer one should substract got->offset from this value.  */
702       bfd_vma offset;
703 
704       /* Pointer to the next GOT entry for this global symbol.
705 	 Symbols have at most one entry in one GOT, but might
706 	 have entries in more than one GOT.
707 	 Root of this list is h->glist.
708 	 NULL for local symbols.  */
709       struct elf_m68k_got_entry *next;
710     } s2;
711   } u;
712 };
713 
714 /* Return representative type for relocation R_TYPE.
715    This is used to avoid enumerating many relocations in comparisons,
716    switches etc.  */
717 
718 static enum elf_m68k_reloc_type
719 elf_m68k_reloc_got_type (enum elf_m68k_reloc_type r_type)
720 {
721   switch (r_type)
722     {
723       /* In most cases R_68K_GOTx relocations require the very same
724 	 handling as R_68K_GOT32O relocation.  In cases when we need
725 	 to distinguish between the two, we use explicitly compare against
726 	 r_type.  */
727     case R_68K_GOT32:
728     case R_68K_GOT16:
729     case R_68K_GOT8:
730     case R_68K_GOT32O:
731     case R_68K_GOT16O:
732     case R_68K_GOT8O:
733       return R_68K_GOT32O;
734 
735     case R_68K_TLS_GD32:
736     case R_68K_TLS_GD16:
737     case R_68K_TLS_GD8:
738       return R_68K_TLS_GD32;
739 
740     case R_68K_TLS_LDM32:
741     case R_68K_TLS_LDM16:
742     case R_68K_TLS_LDM8:
743       return R_68K_TLS_LDM32;
744 
745     case R_68K_TLS_IE32:
746     case R_68K_TLS_IE16:
747     case R_68K_TLS_IE8:
748       return R_68K_TLS_IE32;
749 
750     default:
751       BFD_ASSERT (FALSE);
752       return 0;
753     }
754 }
755 
756 /* Return size of the GOT entry offset for relocation R_TYPE.  */
757 
758 static enum elf_m68k_got_offset_size
759 elf_m68k_reloc_got_offset_size (enum elf_m68k_reloc_type r_type)
760 {
761   switch (r_type)
762     {
763     case R_68K_GOT32: case R_68K_GOT16: case R_68K_GOT8:
764     case R_68K_GOT32O: case R_68K_TLS_GD32: case R_68K_TLS_LDM32:
765     case R_68K_TLS_IE32:
766       return R_32;
767 
768     case R_68K_GOT16O: case R_68K_TLS_GD16: case R_68K_TLS_LDM16:
769     case R_68K_TLS_IE16:
770       return R_16;
771 
772     case R_68K_GOT8O: case R_68K_TLS_GD8: case R_68K_TLS_LDM8:
773     case R_68K_TLS_IE8:
774       return R_8;
775 
776     default:
777       BFD_ASSERT (FALSE);
778       return 0;
779     }
780 }
781 
782 /* Return number of GOT entries we need to allocate in GOT for
783    relocation R_TYPE.  */
784 
785 static bfd_vma
786 elf_m68k_reloc_got_n_slots (enum elf_m68k_reloc_type r_type)
787 {
788   switch (elf_m68k_reloc_got_type (r_type))
789     {
790     case R_68K_GOT32O:
791     case R_68K_TLS_IE32:
792       return 1;
793 
794     case R_68K_TLS_GD32:
795     case R_68K_TLS_LDM32:
796       return 2;
797 
798     default:
799       BFD_ASSERT (FALSE);
800       return 0;
801     }
802 }
803 
804 /* Return TRUE if relocation R_TYPE is a TLS one.  */
805 
806 static bfd_boolean
807 elf_m68k_reloc_tls_p (enum elf_m68k_reloc_type r_type)
808 {
809   switch (r_type)
810     {
811     case R_68K_TLS_GD32: case R_68K_TLS_GD16: case R_68K_TLS_GD8:
812     case R_68K_TLS_LDM32: case R_68K_TLS_LDM16: case R_68K_TLS_LDM8:
813     case R_68K_TLS_LDO32: case R_68K_TLS_LDO16: case R_68K_TLS_LDO8:
814     case R_68K_TLS_IE32: case R_68K_TLS_IE16: case R_68K_TLS_IE8:
815     case R_68K_TLS_LE32: case R_68K_TLS_LE16: case R_68K_TLS_LE8:
816     case R_68K_TLS_DTPMOD32: case R_68K_TLS_DTPREL32: case R_68K_TLS_TPREL32:
817       return TRUE;
818 
819     default:
820       return FALSE;
821     }
822 }
823 
824 /* Data structure representing a single GOT.  */
825 struct elf_m68k_got
826 {
827   /* Hashtable of 'struct elf_m68k_got_entry's.
828      Starting size of this table is the maximum number of
829      R_68K_GOT8O entries.  */
830   htab_t entries;
831 
832   /* Number of R_x slots in this GOT.  Some (e.g., TLS) entries require
833      several GOT slots.
834 
835      n_slots[R_8] is the count of R_8 slots in this GOT.
836      n_slots[R_16] is the cumulative count of R_8 and R_16 slots
837      in this GOT.
838      n_slots[R_32] is the cumulative count of R_8, R_16 and R_32 slots
839      in this GOT.  This is the total number of slots.  */
840   bfd_vma n_slots[R_LAST];
841 
842   /* Number of local (entry->key_.h == NULL) slots in this GOT.
843      This is only used to properly calculate size of .rela.got section;
844      see elf_m68k_partition_multi_got.  */
845   bfd_vma local_n_slots;
846 
847   /* Offset of this GOT relative to beginning of .got section.  */
848   bfd_vma offset;
849 };
850 
851 /* BFD and its GOT.  This is an entry in multi_got->bfd2got hashtable.  */
852 struct elf_m68k_bfd2got_entry
853 {
854   /* BFD.  */
855   const bfd *bfd;
856 
857   /* Assigned GOT.  Before partitioning multi-GOT each BFD has its own
858      GOT structure.  After partitioning several BFD's might [and often do]
859      share a single GOT.  */
860   struct elf_m68k_got *got;
861 };
862 
863 /* The main data structure holding all the pieces.  */
864 struct elf_m68k_multi_got
865 {
866   /* Hashtable mapping each BFD to its GOT.  If a BFD doesn't have an entry
867      here, then it doesn't need a GOT (this includes the case of a BFD
868      having an empty GOT).
869 
870      ??? This hashtable can be replaced by an array indexed by bfd->id.  */
871   htab_t bfd2got;
872 
873   /* Next symndx to assign a global symbol.
874      h->got_entry_key is initialized from this counter.  */
875   unsigned long global_symndx;
876 };
877 
878 /* m68k ELF linker hash table.  */
879 
880 struct elf_m68k_link_hash_table
881 {
882   struct elf_link_hash_table root;
883 
884   /* Small local sym cache.  */
885   struct sym_cache sym_cache;
886 
887   /* The PLT format used by this link, or NULL if the format has not
888      yet been chosen.  */
889   const struct elf_m68k_plt_info *plt_info;
890 
891   /* True, if GP is loaded within each function which uses it.
892      Set to TRUE when GOT negative offsets or multi-GOT is enabled.  */
893   bfd_boolean local_gp_p;
894 
895   /* Switch controlling use of negative offsets to double the size of GOTs.  */
896   bfd_boolean use_neg_got_offsets_p;
897 
898   /* Switch controlling generation of multiple GOTs.  */
899   bfd_boolean allow_multigot_p;
900 
901   /* Multi-GOT data structure.  */
902   struct elf_m68k_multi_got multi_got_;
903 };
904 
905 /* Get the m68k ELF linker hash table from a link_info structure.  */
906 
907 #define elf_m68k_hash_table(p) \
908   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
909   == M68K_ELF_DATA ? ((struct elf_m68k_link_hash_table *) ((p)->hash)) : NULL)
910 
911 /* Shortcut to multi-GOT data.  */
912 #define elf_m68k_multi_got(INFO) (&elf_m68k_hash_table (INFO)->multi_got_)
913 
914 /* Create an entry in an m68k ELF linker hash table.  */
915 
916 static struct bfd_hash_entry *
917 elf_m68k_link_hash_newfunc (struct bfd_hash_entry *entry,
918 			    struct bfd_hash_table *table,
919 			    const char *string)
920 {
921   struct bfd_hash_entry *ret = entry;
922 
923   /* Allocate the structure if it has not already been allocated by a
924      subclass.  */
925   if (ret == NULL)
926     ret = bfd_hash_allocate (table,
927 			     sizeof (struct elf_m68k_link_hash_entry));
928   if (ret == NULL)
929     return ret;
930 
931   /* Call the allocation method of the superclass.  */
932   ret = _bfd_elf_link_hash_newfunc (ret, table, string);
933   if (ret != NULL)
934     {
935       elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
936       elf_m68k_hash_entry (ret)->got_entry_key = 0;
937       elf_m68k_hash_entry (ret)->glist = NULL;
938     }
939 
940   return ret;
941 }
942 
943 /* Destroy an m68k ELF linker hash table.  */
944 
945 static void
946 elf_m68k_link_hash_table_free (bfd *obfd)
947 {
948   struct elf_m68k_link_hash_table *htab;
949 
950   htab = (struct elf_m68k_link_hash_table *) obfd->link.hash;
951 
952   if (htab->multi_got_.bfd2got != NULL)
953     {
954       htab_delete (htab->multi_got_.bfd2got);
955       htab->multi_got_.bfd2got = NULL;
956     }
957   _bfd_elf_link_hash_table_free (obfd);
958 }
959 
960 /* Create an m68k ELF linker hash table.  */
961 
962 static struct bfd_link_hash_table *
963 elf_m68k_link_hash_table_create (bfd *abfd)
964 {
965   struct elf_m68k_link_hash_table *ret;
966   bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
967 
968   ret = (struct elf_m68k_link_hash_table *) bfd_zmalloc (amt);
969   if (ret == (struct elf_m68k_link_hash_table *) NULL)
970     return NULL;
971 
972   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
973 				      elf_m68k_link_hash_newfunc,
974 				      sizeof (struct elf_m68k_link_hash_entry),
975 				      M68K_ELF_DATA))
976     {
977       free (ret);
978       return NULL;
979     }
980   ret->root.root.hash_table_free = elf_m68k_link_hash_table_free;
981 
982   ret->multi_got_.global_symndx = 1;
983 
984   return &ret->root.root;
985 }
986 
987 /* Set the right machine number.  */
988 
989 static bfd_boolean
990 elf32_m68k_object_p (bfd *abfd)
991 {
992   unsigned int mach = 0;
993   unsigned features = 0;
994   flagword eflags = elf_elfheader (abfd)->e_flags;
995 
996   if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
997     features |= m68000;
998   else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
999     features |= cpu32;
1000   else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1001     features |= fido_a;
1002   else
1003     {
1004       switch (eflags & EF_M68K_CF_ISA_MASK)
1005 	{
1006 	case EF_M68K_CF_ISA_A_NODIV:
1007 	  features |= mcfisa_a;
1008 	  break;
1009 	case EF_M68K_CF_ISA_A:
1010 	  features |= mcfisa_a|mcfhwdiv;
1011 	  break;
1012 	case EF_M68K_CF_ISA_A_PLUS:
1013 	  features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
1014 	  break;
1015 	case EF_M68K_CF_ISA_B_NOUSP:
1016 	  features |= mcfisa_a|mcfisa_b|mcfhwdiv;
1017 	  break;
1018 	case EF_M68K_CF_ISA_B:
1019 	  features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
1020 	  break;
1021 	case EF_M68K_CF_ISA_C:
1022 	  features |= mcfisa_a|mcfisa_c|mcfhwdiv|mcfusp;
1023 	  break;
1024 	case EF_M68K_CF_ISA_C_NODIV:
1025 	  features |= mcfisa_a|mcfisa_c|mcfusp;
1026 	  break;
1027 	}
1028       switch (eflags & EF_M68K_CF_MAC_MASK)
1029 	{
1030 	case EF_M68K_CF_MAC:
1031 	  features |= mcfmac;
1032 	  break;
1033 	case EF_M68K_CF_EMAC:
1034 	  features |= mcfemac;
1035 	  break;
1036 	}
1037       if (eflags & EF_M68K_CF_FLOAT)
1038 	features |= cfloat;
1039     }
1040 
1041   mach = bfd_m68k_features_to_mach (features);
1042   bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
1043 
1044   return TRUE;
1045 }
1046 
1047 /* Somewhat reverse of elf32_m68k_object_p, this sets the e_flag
1048    field based on the machine number.  */
1049 
1050 static void
1051 elf_m68k_final_write_processing (bfd *abfd,
1052 				 bfd_boolean linker ATTRIBUTE_UNUSED)
1053 {
1054   int mach = bfd_get_mach (abfd);
1055   unsigned long e_flags = elf_elfheader (abfd)->e_flags;
1056 
1057   if (!e_flags)
1058     {
1059       unsigned int arch_mask;
1060 
1061       arch_mask = bfd_m68k_mach_to_features (mach);
1062 
1063       if (arch_mask & m68000)
1064 	e_flags = EF_M68K_M68000;
1065       else if (arch_mask & cpu32)
1066 	e_flags = EF_M68K_CPU32;
1067       else if (arch_mask & fido_a)
1068 	e_flags = EF_M68K_FIDO;
1069       else
1070 	{
1071 	  switch (arch_mask
1072 		  & (mcfisa_a | mcfisa_aa | mcfisa_b | mcfisa_c | mcfhwdiv | mcfusp))
1073 	    {
1074 	    case mcfisa_a:
1075 	      e_flags |= EF_M68K_CF_ISA_A_NODIV;
1076 	      break;
1077 	    case mcfisa_a | mcfhwdiv:
1078 	      e_flags |= EF_M68K_CF_ISA_A;
1079 	      break;
1080 	    case mcfisa_a | mcfisa_aa | mcfhwdiv | mcfusp:
1081 	      e_flags |= EF_M68K_CF_ISA_A_PLUS;
1082 	      break;
1083 	    case mcfisa_a | mcfisa_b | mcfhwdiv:
1084 	      e_flags |= EF_M68K_CF_ISA_B_NOUSP;
1085 	      break;
1086 	    case mcfisa_a | mcfisa_b | mcfhwdiv | mcfusp:
1087 	      e_flags |= EF_M68K_CF_ISA_B;
1088 	      break;
1089 	    case mcfisa_a | mcfisa_c | mcfhwdiv | mcfusp:
1090 	      e_flags |= EF_M68K_CF_ISA_C;
1091 	      break;
1092 	    case mcfisa_a | mcfisa_c | mcfusp:
1093 	      e_flags |= EF_M68K_CF_ISA_C_NODIV;
1094 	      break;
1095 	    }
1096 	  if (arch_mask & mcfmac)
1097 	    e_flags |= EF_M68K_CF_MAC;
1098 	  else if (arch_mask & mcfemac)
1099 	    e_flags |= EF_M68K_CF_EMAC;
1100 	  if (arch_mask & cfloat)
1101 	    e_flags |= EF_M68K_CF_FLOAT | EF_M68K_CFV4E;
1102 	}
1103       elf_elfheader (abfd)->e_flags = e_flags;
1104     }
1105 }
1106 
1107 /* Keep m68k-specific flags in the ELF header.  */
1108 
1109 static bfd_boolean
1110 elf32_m68k_set_private_flags (bfd *abfd, flagword flags)
1111 {
1112   elf_elfheader (abfd)->e_flags = flags;
1113   elf_flags_init (abfd) = TRUE;
1114   return TRUE;
1115 }
1116 
1117 /* Merge backend specific data from an object file to the output
1118    object file when linking.  */
1119 static bfd_boolean
1120 elf32_m68k_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1121 {
1122   flagword out_flags;
1123   flagword in_flags;
1124   flagword out_isa;
1125   flagword in_isa;
1126   const bfd_arch_info_type *arch_info;
1127 
1128   if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1129       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1130     return FALSE;
1131 
1132   /* Get the merged machine.  This checks for incompatibility between
1133      Coldfire & non-Coldfire flags, incompability between different
1134      Coldfire ISAs, and incompability between different MAC types.  */
1135   arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
1136   if (!arch_info)
1137     return FALSE;
1138 
1139   bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
1140 
1141   in_flags = elf_elfheader (ibfd)->e_flags;
1142   if (!elf_flags_init (obfd))
1143     {
1144       elf_flags_init (obfd) = TRUE;
1145       out_flags = in_flags;
1146     }
1147   else
1148     {
1149       out_flags = elf_elfheader (obfd)->e_flags;
1150       unsigned int variant_mask;
1151 
1152       if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
1153 	variant_mask = 0;
1154       else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
1155 	variant_mask = 0;
1156       else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1157 	variant_mask = 0;
1158       else
1159 	variant_mask = EF_M68K_CF_ISA_MASK;
1160 
1161       in_isa = (in_flags & variant_mask);
1162       out_isa = (out_flags & variant_mask);
1163       if (in_isa > out_isa)
1164 	out_flags ^= in_isa ^ out_isa;
1165       if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
1166 	   && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1167 	  || ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
1168 	      && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
1169 	out_flags = EF_M68K_FIDO;
1170       else
1171       out_flags |= in_flags ^ in_isa;
1172     }
1173   elf_elfheader (obfd)->e_flags = out_flags;
1174 
1175   return TRUE;
1176 }
1177 
1178 /* Display the flags field.  */
1179 
1180 static bfd_boolean
1181 elf32_m68k_print_private_bfd_data (bfd *abfd, void * ptr)
1182 {
1183   FILE *file = (FILE *) ptr;
1184   flagword eflags = elf_elfheader (abfd)->e_flags;
1185 
1186   BFD_ASSERT (abfd != NULL && ptr != NULL);
1187 
1188   /* Print normal ELF private data.  */
1189   _bfd_elf_print_private_bfd_data (abfd, ptr);
1190 
1191   /* Ignore init flag - it may not be set, despite the flags field containing valid data.  */
1192 
1193   /* xgettext:c-format */
1194   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1195 
1196   if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
1197     fprintf (file, " [m68000]");
1198   else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
1199     fprintf (file, " [cpu32]");
1200   else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
1201     fprintf (file, " [fido]");
1202   else
1203     {
1204       if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
1205 	fprintf (file, " [cfv4e]");
1206 
1207       if (eflags & EF_M68K_CF_ISA_MASK)
1208 	{
1209 	  char const *isa = _("unknown");
1210 	  char const *mac = _("unknown");
1211 	  char const *additional = "";
1212 
1213 	  switch (eflags & EF_M68K_CF_ISA_MASK)
1214 	    {
1215 	    case EF_M68K_CF_ISA_A_NODIV:
1216 	      isa = "A";
1217 	      additional = " [nodiv]";
1218 	      break;
1219 	    case EF_M68K_CF_ISA_A:
1220 	      isa = "A";
1221 	      break;
1222 	    case EF_M68K_CF_ISA_A_PLUS:
1223 	      isa = "A+";
1224 	      break;
1225 	    case EF_M68K_CF_ISA_B_NOUSP:
1226 	      isa = "B";
1227 	      additional = " [nousp]";
1228 	      break;
1229 	    case EF_M68K_CF_ISA_B:
1230 	      isa = "B";
1231 	      break;
1232 	    case EF_M68K_CF_ISA_C:
1233 	      isa = "C";
1234 	      break;
1235 	    case EF_M68K_CF_ISA_C_NODIV:
1236 	      isa = "C";
1237 	      additional = " [nodiv]";
1238 	      break;
1239 	    }
1240 	  fprintf (file, " [isa %s]%s", isa, additional);
1241 
1242 	  if (eflags & EF_M68K_CF_FLOAT)
1243 	    fprintf (file, " [float]");
1244 
1245 	  switch (eflags & EF_M68K_CF_MAC_MASK)
1246 	    {
1247 	    case 0:
1248 	      mac = NULL;
1249 	      break;
1250 	    case EF_M68K_CF_MAC:
1251 	      mac = "mac";
1252 	      break;
1253 	    case EF_M68K_CF_EMAC:
1254 	      mac = "emac";
1255 	      break;
1256 	    case EF_M68K_CF_EMAC_B:
1257 	      mac = "emac_b";
1258 	      break;
1259 	    }
1260 	  if (mac)
1261 	    fprintf (file, " [%s]", mac);
1262 	}
1263     }
1264 
1265   fputc ('\n', file);
1266 
1267   return TRUE;
1268 }
1269 
1270 /* Multi-GOT support implementation design:
1271 
1272    Multi-GOT starts in check_relocs hook.  There we scan all
1273    relocations of a BFD and build a local GOT (struct elf_m68k_got)
1274    for it.  If a single BFD appears to require too many GOT slots with
1275    R_68K_GOT8O or R_68K_GOT16O relocations, we fail with notification
1276    to user.
1277    After check_relocs has been invoked for each input BFD, we have
1278    constructed a GOT for each input BFD.
1279 
1280    To minimize total number of GOTs required for a particular output BFD
1281    (as some environments support only 1 GOT per output object) we try
1282    to merge some of the GOTs to share an offset space.  Ideally [and in most
1283    cases] we end up with a single GOT.  In cases when there are too many
1284    restricted relocations (e.g., R_68K_GOT16O relocations) we end up with
1285    several GOTs, assuming the environment can handle them.
1286 
1287    Partitioning is done in elf_m68k_partition_multi_got.  We start with
1288    an empty GOT and traverse bfd2got hashtable putting got_entries from
1289    local GOTs to the new 'big' one.  We do that by constructing an
1290    intermediate GOT holding all the entries the local GOT has and the big
1291    GOT lacks.  Then we check if there is room in the big GOT to accomodate
1292    all the entries from diff.  On success we add those entries to the big
1293    GOT; on failure we start the new 'big' GOT and retry the adding of
1294    entries from the local GOT.  Note that this retry will always succeed as
1295    each local GOT doesn't overflow the limits.  After partitioning we
1296    end up with each bfd assigned one of the big GOTs.  GOT entries in the
1297    big GOTs are initialized with GOT offsets.  Note that big GOTs are
1298    positioned consequently in program space and represent a single huge GOT
1299    to the outside world.
1300 
1301    After that we get to elf_m68k_relocate_section.  There we
1302    adjust relocations of GOT pointer (_GLOBAL_OFFSET_TABLE_) and symbol
1303    relocations to refer to appropriate [assigned to current input_bfd]
1304    big GOT.
1305 
1306    Notes:
1307 
1308    GOT entry type: We have several types of GOT entries.
1309    * R_8 type is used in entries for symbols that have at least one
1310    R_68K_GOT8O or R_68K_TLS_*8 relocation.  We can have at most 0x40
1311    such entries in one GOT.
1312    * R_16 type is used in entries for symbols that have at least one
1313    R_68K_GOT16O or R_68K_TLS_*16 relocation and no R_8 relocations.
1314    We can have at most 0x4000 such entries in one GOT.
1315    * R_32 type is used in all other cases.  We can have as many
1316    such entries in one GOT as we'd like.
1317    When counting relocations we have to include the count of the smaller
1318    ranged relocations in the counts of the larger ranged ones in order
1319    to correctly detect overflow.
1320 
1321    Sorting the GOT: In each GOT starting offsets are assigned to
1322    R_8 entries, which are followed by R_16 entries, and
1323    R_32 entries go at the end.  See finalize_got_offsets for details.
1324 
1325    Negative GOT offsets: To double usable offset range of GOTs we use
1326    negative offsets.  As we assign entries with GOT offsets relative to
1327    start of .got section, the offset values are positive.  They become
1328    negative only in relocate_section where got->offset value is
1329    subtracted from them.
1330 
1331    3 special GOT entries: There are 3 special GOT entries used internally
1332    by loader.  These entries happen to be placed to .got.plt section,
1333    so we don't do anything about them in multi-GOT support.
1334 
1335    Memory management: All data except for hashtables
1336    multi_got->bfd2got and got->entries are allocated on
1337    elf_hash_table (info)->dynobj bfd (for this reason we pass 'info'
1338    to most functions), so we don't need to care to free them.  At the
1339    moment of allocation hashtables are being linked into main data
1340    structure (multi_got), all pieces of which are reachable from
1341    elf_m68k_multi_got (info).  We deallocate them in
1342    elf_m68k_link_hash_table_free.  */
1343 
1344 /* Initialize GOT.  */
1345 
1346 static void
1347 elf_m68k_init_got (struct elf_m68k_got *got)
1348 {
1349   got->entries = NULL;
1350   got->n_slots[R_8] = 0;
1351   got->n_slots[R_16] = 0;
1352   got->n_slots[R_32] = 0;
1353   got->local_n_slots = 0;
1354   got->offset = (bfd_vma) -1;
1355 }
1356 
1357 /* Destruct GOT.  */
1358 
1359 static void
1360 elf_m68k_clear_got (struct elf_m68k_got *got)
1361 {
1362   if (got->entries != NULL)
1363     {
1364       htab_delete (got->entries);
1365       got->entries = NULL;
1366     }
1367 }
1368 
1369 /* Create and empty GOT structure.  INFO is the context where memory
1370    should be allocated.  */
1371 
1372 static struct elf_m68k_got *
1373 elf_m68k_create_empty_got (struct bfd_link_info *info)
1374 {
1375   struct elf_m68k_got *got;
1376 
1377   got = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*got));
1378   if (got == NULL)
1379     return NULL;
1380 
1381   elf_m68k_init_got (got);
1382 
1383   return got;
1384 }
1385 
1386 /* Initialize KEY.  */
1387 
1388 static void
1389 elf_m68k_init_got_entry_key (struct elf_m68k_got_entry_key *key,
1390 			     struct elf_link_hash_entry *h,
1391 			     const bfd *abfd, unsigned long symndx,
1392 			     enum elf_m68k_reloc_type reloc_type)
1393 {
1394   if (elf_m68k_reloc_got_type (reloc_type) == R_68K_TLS_LDM32)
1395     /* All TLS_LDM relocations share a single GOT entry.  */
1396     {
1397       key->bfd = NULL;
1398       key->symndx = 0;
1399     }
1400   else if (h != NULL)
1401     /* Global symbols are identified with their got_entry_key.  */
1402     {
1403       key->bfd = NULL;
1404       key->symndx = elf_m68k_hash_entry (h)->got_entry_key;
1405       BFD_ASSERT (key->symndx != 0);
1406     }
1407   else
1408     /* Local symbols are identified by BFD they appear in and symndx.  */
1409     {
1410       key->bfd = abfd;
1411       key->symndx = symndx;
1412     }
1413 
1414   key->type = reloc_type;
1415 }
1416 
1417 /* Calculate hash of got_entry.
1418    ??? Is it good?  */
1419 
1420 static hashval_t
1421 elf_m68k_got_entry_hash (const void *_entry)
1422 {
1423   const struct elf_m68k_got_entry_key *key;
1424 
1425   key = &((const struct elf_m68k_got_entry *) _entry)->key_;
1426 
1427   return (key->symndx
1428 	  + (key->bfd != NULL ? (int) key->bfd->id : -1)
1429 	  + elf_m68k_reloc_got_type (key->type));
1430 }
1431 
1432 /* Check if two got entries are equal.  */
1433 
1434 static int
1435 elf_m68k_got_entry_eq (const void *_entry1, const void *_entry2)
1436 {
1437   const struct elf_m68k_got_entry_key *key1;
1438   const struct elf_m68k_got_entry_key *key2;
1439 
1440   key1 = &((const struct elf_m68k_got_entry *) _entry1)->key_;
1441   key2 = &((const struct elf_m68k_got_entry *) _entry2)->key_;
1442 
1443   return (key1->bfd == key2->bfd
1444 	  && key1->symndx == key2->symndx
1445 	  && (elf_m68k_reloc_got_type (key1->type)
1446 	      == elf_m68k_reloc_got_type (key2->type)));
1447 }
1448 
1449 /* When using negative offsets, we allocate one extra R_8, one extra R_16
1450    and one extra R_32 slots to simplify handling of 2-slot entries during
1451    offset allocation -- hence -1 for R_8 slots and -2 for R_16 slots.  */
1452 
1453 /* Maximal number of R_8 slots in a single GOT.  */
1454 #define ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT(INFO)		\
1455   (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p		\
1456    ? (0x40 - 1)							\
1457    : 0x20)
1458 
1459 /* Maximal number of R_8 and R_16 slots in a single GOT.  */
1460 #define ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT(INFO)		\
1461   (elf_m68k_hash_table (INFO)->use_neg_got_offsets_p		\
1462    ? (0x4000 - 2)						\
1463    : 0x2000)
1464 
1465 /* SEARCH - simply search the hashtable, don't insert new entries or fail when
1466    the entry cannot be found.
1467    FIND_OR_CREATE - search for an existing entry, but create new if there's
1468    no such.
1469    MUST_FIND - search for an existing entry and assert that it exist.
1470    MUST_CREATE - assert that there's no such entry and create new one.  */
1471 enum elf_m68k_get_entry_howto
1472   {
1473     SEARCH,
1474     FIND_OR_CREATE,
1475     MUST_FIND,
1476     MUST_CREATE
1477   };
1478 
1479 /* Get or create (depending on HOWTO) entry with KEY in GOT.
1480    INFO is context in which memory should be allocated (can be NULL if
1481    HOWTO is SEARCH or MUST_FIND).  */
1482 
1483 static struct elf_m68k_got_entry *
1484 elf_m68k_get_got_entry (struct elf_m68k_got *got,
1485 			const struct elf_m68k_got_entry_key *key,
1486 			enum elf_m68k_get_entry_howto howto,
1487 			struct bfd_link_info *info)
1488 {
1489   struct elf_m68k_got_entry entry_;
1490   struct elf_m68k_got_entry *entry;
1491   void **ptr;
1492 
1493   BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
1494 
1495   if (got->entries == NULL)
1496     /* This is the first entry in ABFD.  Initialize hashtable.  */
1497     {
1498       if (howto == SEARCH)
1499 	return NULL;
1500 
1501       got->entries = htab_try_create (ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT
1502 				      (info),
1503 				      elf_m68k_got_entry_hash,
1504 				      elf_m68k_got_entry_eq, NULL);
1505       if (got->entries == NULL)
1506 	{
1507 	  bfd_set_error (bfd_error_no_memory);
1508 	  return NULL;
1509 	}
1510     }
1511 
1512   entry_.key_ = *key;
1513   ptr = htab_find_slot (got->entries, &entry_, (howto != SEARCH
1514 						? INSERT : NO_INSERT));
1515   if (ptr == NULL)
1516     {
1517       if (howto == SEARCH)
1518 	/* Entry not found.  */
1519 	return NULL;
1520 
1521       /* We're out of memory.  */
1522       bfd_set_error (bfd_error_no_memory);
1523       return NULL;
1524     }
1525 
1526   if (*ptr == NULL)
1527     /* We didn't find the entry and we're asked to create a new one.  */
1528     {
1529       BFD_ASSERT (howto != MUST_FIND && howto != SEARCH);
1530 
1531       entry = bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry));
1532       if (entry == NULL)
1533 	return NULL;
1534 
1535       /* Initialize new entry.  */
1536       entry->key_ = *key;
1537 
1538       entry->u.s1.refcount = 0;
1539 
1540       /* Mark the entry as not initialized.  */
1541       entry->key_.type = R_68K_max;
1542 
1543       *ptr = entry;
1544     }
1545   else
1546     /* We found the entry.  */
1547     {
1548       BFD_ASSERT (howto != MUST_CREATE);
1549 
1550       entry = *ptr;
1551     }
1552 
1553   return entry;
1554 }
1555 
1556 /* Update GOT counters when merging entry of WAS type with entry of NEW type.
1557    Return the value to which ENTRY's type should be set.  */
1558 
1559 static enum elf_m68k_reloc_type
1560 elf_m68k_update_got_entry_type (struct elf_m68k_got *got,
1561 				enum elf_m68k_reloc_type was,
1562 				enum elf_m68k_reloc_type new_reloc)
1563 {
1564   enum elf_m68k_got_offset_size was_size;
1565   enum elf_m68k_got_offset_size new_size;
1566   bfd_vma n_slots;
1567 
1568   if (was == R_68K_max)
1569     /* The type of the entry is not initialized yet.  */
1570     {
1571       /* Update all got->n_slots counters, including n_slots[R_32].  */
1572       was_size = R_LAST;
1573 
1574       was = new_reloc;
1575     }
1576   else
1577     {
1578       /* !!! We, probably, should emit an error rather then fail on assert
1579 	 in such a case.  */
1580       BFD_ASSERT (elf_m68k_reloc_got_type (was)
1581 		  == elf_m68k_reloc_got_type (new_reloc));
1582 
1583       was_size = elf_m68k_reloc_got_offset_size (was);
1584     }
1585 
1586   new_size = elf_m68k_reloc_got_offset_size (new_reloc);
1587   n_slots = elf_m68k_reloc_got_n_slots (new_reloc);
1588 
1589   while (was_size > new_size)
1590     {
1591       --was_size;
1592       got->n_slots[was_size] += n_slots;
1593     }
1594 
1595   if (new_reloc > was)
1596     /* Relocations are ordered from bigger got offset size to lesser,
1597        so choose the relocation type with lesser offset size.  */
1598     was = new_reloc;
1599 
1600   return was;
1601 }
1602 
1603 /* Update GOT counters when removing an entry of type TYPE.  */
1604 
1605 static void
1606 elf_m68k_remove_got_entry_type (struct elf_m68k_got *got,
1607 				enum elf_m68k_reloc_type type)
1608 {
1609   enum elf_m68k_got_offset_size os;
1610   bfd_vma n_slots;
1611 
1612   n_slots = elf_m68k_reloc_got_n_slots (type);
1613 
1614   /* Decrese counter of slots with offset size corresponding to TYPE
1615      and all greater offset sizes.  */
1616   for (os = elf_m68k_reloc_got_offset_size (type); os <= R_32; ++os)
1617     {
1618       BFD_ASSERT (got->n_slots[os] >= n_slots);
1619 
1620       got->n_slots[os] -= n_slots;
1621     }
1622 }
1623 
1624 /* Add new or update existing entry to GOT.
1625    H, ABFD, TYPE and SYMNDX is data for the entry.
1626    INFO is a context where memory should be allocated.  */
1627 
1628 static struct elf_m68k_got_entry *
1629 elf_m68k_add_entry_to_got (struct elf_m68k_got *got,
1630 			   struct elf_link_hash_entry *h,
1631 			   const bfd *abfd,
1632 			   enum elf_m68k_reloc_type reloc_type,
1633 			   unsigned long symndx,
1634 			   struct bfd_link_info *info)
1635 {
1636   struct elf_m68k_got_entry_key key_;
1637   struct elf_m68k_got_entry *entry;
1638 
1639   if (h != NULL && elf_m68k_hash_entry (h)->got_entry_key == 0)
1640     elf_m68k_hash_entry (h)->got_entry_key
1641       = elf_m68k_multi_got (info)->global_symndx++;
1642 
1643   elf_m68k_init_got_entry_key (&key_, h, abfd, symndx, reloc_type);
1644 
1645   entry = elf_m68k_get_got_entry (got, &key_, FIND_OR_CREATE, info);
1646   if (entry == NULL)
1647     return NULL;
1648 
1649   /* Determine entry's type and update got->n_slots counters.  */
1650   entry->key_.type = elf_m68k_update_got_entry_type (got,
1651 						     entry->key_.type,
1652 						     reloc_type);
1653 
1654   /* Update refcount.  */
1655   ++entry->u.s1.refcount;
1656 
1657   if (entry->u.s1.refcount == 1)
1658     /* We see this entry for the first time.  */
1659     {
1660       if (entry->key_.bfd != NULL)
1661 	got->local_n_slots += elf_m68k_reloc_got_n_slots (entry->key_.type);
1662     }
1663 
1664   BFD_ASSERT (got->n_slots[R_32] >= got->local_n_slots);
1665 
1666   if ((got->n_slots[R_8]
1667        > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1668       || (got->n_slots[R_16]
1669 	  > ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info)))
1670     /* This BFD has too many relocation.  */
1671     {
1672       if (got->n_slots[R_8] > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1673 	(*_bfd_error_handler) (_("%B: GOT overflow: "
1674 				 "Number of relocations with 8-bit "
1675 				 "offset > %d"),
1676 			       abfd,
1677 			       ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info));
1678       else
1679 	(*_bfd_error_handler) (_("%B: GOT overflow: "
1680 				 "Number of relocations with 8- or 16-bit "
1681 				 "offset > %d"),
1682 			       abfd,
1683 			       ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info));
1684 
1685       return NULL;
1686     }
1687 
1688   return entry;
1689 }
1690 
1691 /* Compute the hash value of the bfd in a bfd2got hash entry.  */
1692 
1693 static hashval_t
1694 elf_m68k_bfd2got_entry_hash (const void *entry)
1695 {
1696   const struct elf_m68k_bfd2got_entry *e;
1697 
1698   e = (const struct elf_m68k_bfd2got_entry *) entry;
1699 
1700   return e->bfd->id;
1701 }
1702 
1703 /* Check whether two hash entries have the same bfd.  */
1704 
1705 static int
1706 elf_m68k_bfd2got_entry_eq (const void *entry1, const void *entry2)
1707 {
1708   const struct elf_m68k_bfd2got_entry *e1;
1709   const struct elf_m68k_bfd2got_entry *e2;
1710 
1711   e1 = (const struct elf_m68k_bfd2got_entry *) entry1;
1712   e2 = (const struct elf_m68k_bfd2got_entry *) entry2;
1713 
1714   return e1->bfd == e2->bfd;
1715 }
1716 
1717 /* Destruct a bfd2got entry.  */
1718 
1719 static void
1720 elf_m68k_bfd2got_entry_del (void *_entry)
1721 {
1722   struct elf_m68k_bfd2got_entry *entry;
1723 
1724   entry = (struct elf_m68k_bfd2got_entry *) _entry;
1725 
1726   BFD_ASSERT (entry->got != NULL);
1727   elf_m68k_clear_got (entry->got);
1728 }
1729 
1730 /* Find existing or create new (depending on HOWTO) bfd2got entry in
1731    MULTI_GOT.  ABFD is the bfd we need a GOT for.  INFO is a context where
1732    memory should be allocated.  */
1733 
1734 static struct elf_m68k_bfd2got_entry *
1735 elf_m68k_get_bfd2got_entry (struct elf_m68k_multi_got *multi_got,
1736 			    const bfd *abfd,
1737 			    enum elf_m68k_get_entry_howto howto,
1738 			    struct bfd_link_info *info)
1739 {
1740   struct elf_m68k_bfd2got_entry entry_;
1741   void **ptr;
1742   struct elf_m68k_bfd2got_entry *entry;
1743 
1744   BFD_ASSERT ((info == NULL) == (howto == SEARCH || howto == MUST_FIND));
1745 
1746   if (multi_got->bfd2got == NULL)
1747     /* This is the first GOT.  Initialize bfd2got.  */
1748     {
1749       if (howto == SEARCH)
1750 	return NULL;
1751 
1752       multi_got->bfd2got = htab_try_create (1, elf_m68k_bfd2got_entry_hash,
1753 					    elf_m68k_bfd2got_entry_eq,
1754 					    elf_m68k_bfd2got_entry_del);
1755       if (multi_got->bfd2got == NULL)
1756 	{
1757 	  bfd_set_error (bfd_error_no_memory);
1758 	  return NULL;
1759 	}
1760     }
1761 
1762   entry_.bfd = abfd;
1763   ptr = htab_find_slot (multi_got->bfd2got, &entry_, (howto != SEARCH
1764 						      ? INSERT : NO_INSERT));
1765   if (ptr == NULL)
1766     {
1767       if (howto == SEARCH)
1768 	/* Entry not found.  */
1769 	return NULL;
1770 
1771       /* We're out of memory.  */
1772       bfd_set_error (bfd_error_no_memory);
1773       return NULL;
1774     }
1775 
1776   if (*ptr == NULL)
1777     /* Entry was not found.  Create new one.  */
1778     {
1779       BFD_ASSERT (howto != MUST_FIND && howto != SEARCH);
1780 
1781       entry = ((struct elf_m68k_bfd2got_entry *)
1782 	       bfd_alloc (elf_hash_table (info)->dynobj, sizeof (*entry)));
1783       if (entry == NULL)
1784 	return NULL;
1785 
1786       entry->bfd = abfd;
1787 
1788       entry->got = elf_m68k_create_empty_got (info);
1789       if (entry->got == NULL)
1790 	return NULL;
1791 
1792       *ptr = entry;
1793     }
1794   else
1795     {
1796       BFD_ASSERT (howto != MUST_CREATE);
1797 
1798       /* Return existing entry.  */
1799       entry = *ptr;
1800     }
1801 
1802   return entry;
1803 }
1804 
1805 struct elf_m68k_can_merge_gots_arg
1806 {
1807   /* A current_got that we constructing a DIFF against.  */
1808   struct elf_m68k_got *big;
1809 
1810   /* GOT holding entries not present or that should be changed in
1811      BIG.  */
1812   struct elf_m68k_got *diff;
1813 
1814   /* Context where to allocate memory.  */
1815   struct bfd_link_info *info;
1816 
1817   /* Error flag.  */
1818   bfd_boolean error_p;
1819 };
1820 
1821 /* Process a single entry from the small GOT to see if it should be added
1822    or updated in the big GOT.  */
1823 
1824 static int
1825 elf_m68k_can_merge_gots_1 (void **_entry_ptr, void *_arg)
1826 {
1827   const struct elf_m68k_got_entry *entry1;
1828   struct elf_m68k_can_merge_gots_arg *arg;
1829   const struct elf_m68k_got_entry *entry2;
1830   enum elf_m68k_reloc_type type;
1831 
1832   entry1 = (const struct elf_m68k_got_entry *) *_entry_ptr;
1833   arg = (struct elf_m68k_can_merge_gots_arg *) _arg;
1834 
1835   entry2 = elf_m68k_get_got_entry (arg->big, &entry1->key_, SEARCH, NULL);
1836 
1837   if (entry2 != NULL)
1838     /* We found an existing entry.  Check if we should update it.  */
1839     {
1840       type = elf_m68k_update_got_entry_type (arg->diff,
1841 					     entry2->key_.type,
1842 					     entry1->key_.type);
1843 
1844       if (type == entry2->key_.type)
1845 	/* ENTRY1 doesn't update data in ENTRY2.  Skip it.
1846 	   To skip creation of difference entry we use the type,
1847 	   which we won't see in GOT entries for sure.  */
1848 	type = R_68K_max;
1849     }
1850   else
1851     /* We didn't find the entry.  Add entry1 to DIFF.  */
1852     {
1853       BFD_ASSERT (entry1->key_.type != R_68K_max);
1854 
1855       type = elf_m68k_update_got_entry_type (arg->diff,
1856 					     R_68K_max, entry1->key_.type);
1857 
1858       if (entry1->key_.bfd != NULL)
1859 	arg->diff->local_n_slots += elf_m68k_reloc_got_n_slots (type);
1860     }
1861 
1862   if (type != R_68K_max)
1863     /* Create an entry in DIFF.  */
1864     {
1865       struct elf_m68k_got_entry *entry;
1866 
1867       entry = elf_m68k_get_got_entry (arg->diff, &entry1->key_, MUST_CREATE,
1868 				      arg->info);
1869       if (entry == NULL)
1870 	{
1871 	  arg->error_p = TRUE;
1872 	  return 0;
1873 	}
1874 
1875       entry->key_.type = type;
1876     }
1877 
1878   return 1;
1879 }
1880 
1881 /* Return TRUE if SMALL GOT can be added to BIG GOT without overflowing it.
1882    Construct DIFF GOT holding the entries which should be added or updated
1883    in BIG GOT to accumulate information from SMALL.
1884    INFO is the context where memory should be allocated.  */
1885 
1886 static bfd_boolean
1887 elf_m68k_can_merge_gots (struct elf_m68k_got *big,
1888 			 const struct elf_m68k_got *small,
1889 			 struct bfd_link_info *info,
1890 			 struct elf_m68k_got *diff)
1891 {
1892   struct elf_m68k_can_merge_gots_arg arg_;
1893 
1894   BFD_ASSERT (small->offset == (bfd_vma) -1);
1895 
1896   arg_.big = big;
1897   arg_.diff = diff;
1898   arg_.info = info;
1899   arg_.error_p = FALSE;
1900   htab_traverse_noresize (small->entries, elf_m68k_can_merge_gots_1, &arg_);
1901   if (arg_.error_p)
1902     {
1903       diff->offset = 0;
1904       return FALSE;
1905     }
1906 
1907   /* Check for overflow.  */
1908   if ((big->n_slots[R_8] + arg_.diff->n_slots[R_8]
1909        > ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1910       || (big->n_slots[R_16] + arg_.diff->n_slots[R_16]
1911 	  > ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info)))
1912     return FALSE;
1913 
1914   return TRUE;
1915 }
1916 
1917 struct elf_m68k_merge_gots_arg
1918 {
1919   /* The BIG got.  */
1920   struct elf_m68k_got *big;
1921 
1922   /* Context where memory should be allocated.  */
1923   struct bfd_link_info *info;
1924 
1925   /* Error flag.  */
1926   bfd_boolean error_p;
1927 };
1928 
1929 /* Process a single entry from DIFF got.  Add or update corresponding
1930    entry in the BIG got.  */
1931 
1932 static int
1933 elf_m68k_merge_gots_1 (void **entry_ptr, void *_arg)
1934 {
1935   const struct elf_m68k_got_entry *from;
1936   struct elf_m68k_merge_gots_arg *arg;
1937   struct elf_m68k_got_entry *to;
1938 
1939   from = (const struct elf_m68k_got_entry *) *entry_ptr;
1940   arg = (struct elf_m68k_merge_gots_arg *) _arg;
1941 
1942   to = elf_m68k_get_got_entry (arg->big, &from->key_, FIND_OR_CREATE,
1943 			       arg->info);
1944   if (to == NULL)
1945     {
1946       arg->error_p = TRUE;
1947       return 0;
1948     }
1949 
1950   BFD_ASSERT (to->u.s1.refcount == 0);
1951   /* All we need to merge is TYPE.  */
1952   to->key_.type = from->key_.type;
1953 
1954   return 1;
1955 }
1956 
1957 /* Merge data from DIFF to BIG.  INFO is context where memory should be
1958    allocated.  */
1959 
1960 static bfd_boolean
1961 elf_m68k_merge_gots (struct elf_m68k_got *big,
1962 		     struct elf_m68k_got *diff,
1963 		     struct bfd_link_info *info)
1964 {
1965   if (diff->entries != NULL)
1966     /* DIFF is not empty.  Merge it into BIG GOT.  */
1967     {
1968       struct elf_m68k_merge_gots_arg arg_;
1969 
1970       /* Merge entries.  */
1971       arg_.big = big;
1972       arg_.info = info;
1973       arg_.error_p = FALSE;
1974       htab_traverse_noresize (diff->entries, elf_m68k_merge_gots_1, &arg_);
1975       if (arg_.error_p)
1976 	return FALSE;
1977 
1978       /* Merge counters.  */
1979       big->n_slots[R_8] += diff->n_slots[R_8];
1980       big->n_slots[R_16] += diff->n_slots[R_16];
1981       big->n_slots[R_32] += diff->n_slots[R_32];
1982       big->local_n_slots += diff->local_n_slots;
1983     }
1984   else
1985     /* DIFF is empty.  */
1986     {
1987       BFD_ASSERT (diff->n_slots[R_8] == 0);
1988       BFD_ASSERT (diff->n_slots[R_16] == 0);
1989       BFD_ASSERT (diff->n_slots[R_32] == 0);
1990       BFD_ASSERT (diff->local_n_slots == 0);
1991     }
1992 
1993   BFD_ASSERT (!elf_m68k_hash_table (info)->allow_multigot_p
1994 	      || ((big->n_slots[R_8]
1995 		   <= ELF_M68K_R_8_MAX_N_SLOTS_IN_GOT (info))
1996 		  && (big->n_slots[R_16]
1997 		      <= ELF_M68K_R_8_16_MAX_N_SLOTS_IN_GOT (info))));
1998 
1999   return TRUE;
2000 }
2001 
2002 struct elf_m68k_finalize_got_offsets_arg
2003 {
2004   /* Ranges of the offsets for GOT entries.
2005      R_x entries receive offsets between offset1[R_x] and offset2[R_x].
2006      R_x is R_8, R_16 and R_32.  */
2007   bfd_vma *offset1;
2008   bfd_vma *offset2;
2009 
2010   /* Mapping from global symndx to global symbols.
2011      This is used to build lists of got entries for global symbols.  */
2012   struct elf_m68k_link_hash_entry **symndx2h;
2013 
2014   bfd_vma n_ldm_entries;
2015 };
2016 
2017 /* Assign ENTRY an offset.  Build list of GOT entries for global symbols
2018    along the way.  */
2019 
2020 static int
2021 elf_m68k_finalize_got_offsets_1 (void **entry_ptr, void *_arg)
2022 {
2023   struct elf_m68k_got_entry *entry;
2024   struct elf_m68k_finalize_got_offsets_arg *arg;
2025 
2026   enum elf_m68k_got_offset_size got_offset_size;
2027   bfd_vma entry_size;
2028 
2029   entry = (struct elf_m68k_got_entry *) *entry_ptr;
2030   arg = (struct elf_m68k_finalize_got_offsets_arg *) _arg;
2031 
2032   /* This should be a fresh entry created in elf_m68k_can_merge_gots.  */
2033   BFD_ASSERT (entry->u.s1.refcount == 0);
2034 
2035   /* Get GOT offset size for the entry .  */
2036   got_offset_size = elf_m68k_reloc_got_offset_size (entry->key_.type);
2037 
2038   /* Calculate entry size in bytes.  */
2039   entry_size = 4 * elf_m68k_reloc_got_n_slots (entry->key_.type);
2040 
2041   /* Check if we should switch to negative range of the offsets. */
2042   if (arg->offset1[got_offset_size] + entry_size
2043       > arg->offset2[got_offset_size])
2044     {
2045       /* Verify that this is the only switch to negative range for
2046 	 got_offset_size.  If this assertion fails, then we've miscalculated
2047 	 range for got_offset_size entries in
2048 	 elf_m68k_finalize_got_offsets.  */
2049       BFD_ASSERT (arg->offset2[got_offset_size]
2050 		  != arg->offset2[-(int) got_offset_size - 1]);
2051 
2052       /* Switch.  */
2053       arg->offset1[got_offset_size] = arg->offset1[-(int) got_offset_size - 1];
2054       arg->offset2[got_offset_size] = arg->offset2[-(int) got_offset_size - 1];
2055 
2056       /* Verify that now we have enough room for the entry.  */
2057       BFD_ASSERT (arg->offset1[got_offset_size] + entry_size
2058 		  <= arg->offset2[got_offset_size]);
2059     }
2060 
2061   /* Assign offset to entry.  */
2062   entry->u.s2.offset = arg->offset1[got_offset_size];
2063   arg->offset1[got_offset_size] += entry_size;
2064 
2065   if (entry->key_.bfd == NULL)
2066     /* Hook up this entry into the list of got_entries of H.  */
2067     {
2068       struct elf_m68k_link_hash_entry *h;
2069 
2070       h = arg->symndx2h[entry->key_.symndx];
2071       if (h != NULL)
2072 	{
2073 	  entry->u.s2.next = h->glist;
2074 	  h->glist = entry;
2075 	}
2076       else
2077 	/* This should be the entry for TLS_LDM relocation then.  */
2078 	{
2079 	  BFD_ASSERT ((elf_m68k_reloc_got_type (entry->key_.type)
2080 		       == R_68K_TLS_LDM32)
2081 		      && entry->key_.symndx == 0);
2082 
2083 	  ++arg->n_ldm_entries;
2084 	}
2085     }
2086   else
2087     /* This entry is for local symbol.  */
2088     entry->u.s2.next = NULL;
2089 
2090   return 1;
2091 }
2092 
2093 /* Assign offsets within GOT.  USE_NEG_GOT_OFFSETS_P indicates if we
2094    should use negative offsets.
2095    Build list of GOT entries for global symbols along the way.
2096    SYMNDX2H is mapping from global symbol indices to actual
2097    global symbols.
2098    Return offset at which next GOT should start.  */
2099 
2100 static void
2101 elf_m68k_finalize_got_offsets (struct elf_m68k_got *got,
2102 			       bfd_boolean use_neg_got_offsets_p,
2103 			       struct elf_m68k_link_hash_entry **symndx2h,
2104 			       bfd_vma *final_offset, bfd_vma *n_ldm_entries)
2105 {
2106   struct elf_m68k_finalize_got_offsets_arg arg_;
2107   bfd_vma offset1_[2 * R_LAST];
2108   bfd_vma offset2_[2 * R_LAST];
2109   int i;
2110   bfd_vma start_offset;
2111 
2112   BFD_ASSERT (got->offset != (bfd_vma) -1);
2113 
2114   /* We set entry offsets relative to the .got section (and not the
2115      start of a particular GOT), so that we can use them in
2116      finish_dynamic_symbol without needing to know the GOT which they come
2117      from.  */
2118 
2119   /* Put offset1 in the middle of offset1_, same for offset2.  */
2120   arg_.offset1 = offset1_ + R_LAST;
2121   arg_.offset2 = offset2_ + R_LAST;
2122 
2123   start_offset = got->offset;
2124 
2125   if (use_neg_got_offsets_p)
2126     /* Setup both negative and positive ranges for R_8, R_16 and R_32.  */
2127     i = -(int) R_32 - 1;
2128   else
2129     /* Setup positives ranges for R_8, R_16 and R_32.  */
2130     i = (int) R_8;
2131 
2132   for (; i <= (int) R_32; ++i)
2133     {
2134       int j;
2135       size_t n;
2136 
2137       /* Set beginning of the range of offsets I.  */
2138       arg_.offset1[i] = start_offset;
2139 
2140       /* Calculate number of slots that require I offsets.  */
2141       j = (i >= 0) ? i : -i - 1;
2142       n = (j >= 1) ? got->n_slots[j - 1] : 0;
2143       n = got->n_slots[j] - n;
2144 
2145       if (use_neg_got_offsets_p && n != 0)
2146 	{
2147 	  if (i < 0)
2148 	    /* We first fill the positive side of the range, so we might
2149 	       end up with one empty slot at that side when we can't fit
2150 	       whole 2-slot entry.  Account for that at negative side of
2151 	       the interval with one additional entry.  */
2152 	    n = n / 2 + 1;
2153 	  else
2154 	    /* When the number of slots is odd, make positive side of the
2155 	       range one entry bigger.  */
2156 	    n = (n + 1) / 2;
2157 	}
2158 
2159       /* N is the number of slots that require I offsets.
2160 	 Calculate length of the range for I offsets.  */
2161       n = 4 * n;
2162 
2163       /* Set end of the range.  */
2164       arg_.offset2[i] = start_offset + n;
2165 
2166       start_offset = arg_.offset2[i];
2167     }
2168 
2169   if (!use_neg_got_offsets_p)
2170     /* Make sure that if we try to switch to negative offsets in
2171        elf_m68k_finalize_got_offsets_1, the assert therein will catch
2172        the bug.  */
2173     for (i = R_8; i <= R_32; ++i)
2174       arg_.offset2[-i - 1] = arg_.offset2[i];
2175 
2176   /* Setup got->offset.  offset1[R_8] is either in the middle or at the
2177      beginning of GOT depending on use_neg_got_offsets_p.  */
2178   got->offset = arg_.offset1[R_8];
2179 
2180   arg_.symndx2h = symndx2h;
2181   arg_.n_ldm_entries = 0;
2182 
2183   /* Assign offsets.  */
2184   htab_traverse (got->entries, elf_m68k_finalize_got_offsets_1, &arg_);
2185 
2186   /* Check offset ranges we have actually assigned.  */
2187   for (i = (int) R_8; i <= (int) R_32; ++i)
2188     BFD_ASSERT (arg_.offset2[i] - arg_.offset1[i] <= 4);
2189 
2190   *final_offset = start_offset;
2191   *n_ldm_entries = arg_.n_ldm_entries;
2192 }
2193 
2194 struct elf_m68k_partition_multi_got_arg
2195 {
2196   /* The GOT we are adding entries to.  Aka big got.  */
2197   struct elf_m68k_got *current_got;
2198 
2199   /* Offset to assign the next CURRENT_GOT.  */
2200   bfd_vma offset;
2201 
2202   /* Context where memory should be allocated.  */
2203   struct bfd_link_info *info;
2204 
2205   /* Total number of slots in the .got section.
2206      This is used to calculate size of the .got and .rela.got sections.  */
2207   bfd_vma n_slots;
2208 
2209   /* Difference in numbers of allocated slots in the .got section
2210      and necessary relocations in the .rela.got section.
2211      This is used to calculate size of the .rela.got section.  */
2212   bfd_vma slots_relas_diff;
2213 
2214   /* Error flag.  */
2215   bfd_boolean error_p;
2216 
2217   /* Mapping from global symndx to global symbols.
2218      This is used to build lists of got entries for global symbols.  */
2219   struct elf_m68k_link_hash_entry **symndx2h;
2220 };
2221 
2222 static void
2223 elf_m68k_partition_multi_got_2 (struct elf_m68k_partition_multi_got_arg *arg)
2224 {
2225   bfd_vma n_ldm_entries;
2226 
2227   elf_m68k_finalize_got_offsets (arg->current_got,
2228 				 (elf_m68k_hash_table (arg->info)
2229 				  ->use_neg_got_offsets_p),
2230 				 arg->symndx2h,
2231 				 &arg->offset, &n_ldm_entries);
2232 
2233   arg->n_slots += arg->current_got->n_slots[R_32];
2234 
2235   if (!bfd_link_pic (arg->info))
2236     /* If we are generating a shared object, we need to
2237        output a R_68K_RELATIVE reloc so that the dynamic
2238        linker can adjust this GOT entry.  Overwise we
2239        don't need space in .rela.got for local symbols.  */
2240     arg->slots_relas_diff += arg->current_got->local_n_slots;
2241 
2242   /* @LDM relocations require a 2-slot GOT entry, but only
2243      one relocation.  Account for that.  */
2244   arg->slots_relas_diff += n_ldm_entries;
2245 
2246   BFD_ASSERT (arg->slots_relas_diff <= arg->n_slots);
2247 }
2248 
2249 
2250 /* Process a single BFD2GOT entry and either merge GOT to CURRENT_GOT
2251    or start a new CURRENT_GOT.  */
2252 
2253 static int
2254 elf_m68k_partition_multi_got_1 (void **_entry, void *_arg)
2255 {
2256   struct elf_m68k_bfd2got_entry *entry;
2257   struct elf_m68k_partition_multi_got_arg *arg;
2258   struct elf_m68k_got *got;
2259   struct elf_m68k_got diff_;
2260   struct elf_m68k_got *diff;
2261 
2262   entry = (struct elf_m68k_bfd2got_entry *) *_entry;
2263   arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
2264 
2265   got = entry->got;
2266   BFD_ASSERT (got != NULL);
2267   BFD_ASSERT (got->offset == (bfd_vma) -1);
2268 
2269   diff = NULL;
2270 
2271   if (arg->current_got != NULL)
2272     /* Construct diff.  */
2273     {
2274       diff = &diff_;
2275       elf_m68k_init_got (diff);
2276 
2277       if (!elf_m68k_can_merge_gots (arg->current_got, got, arg->info, diff))
2278 	{
2279 	  if (diff->offset == 0)
2280 	    /* Offset set to 0 in the diff_ indicates an error.  */
2281 	    {
2282 	      arg->error_p = TRUE;
2283 	      goto final_return;
2284 	    }
2285 
2286 	  if (elf_m68k_hash_table (arg->info)->allow_multigot_p)
2287 	    {
2288 	      elf_m68k_clear_got (diff);
2289 	      /* Schedule to finish up current_got and start new one.  */
2290 	      diff = NULL;
2291 	    }
2292 	  /* else
2293 	     Merge GOTs no matter what.  If big GOT overflows,
2294 	     we'll fail in relocate_section due to truncated relocations.
2295 
2296 	     ??? May be fail earlier?  E.g., in can_merge_gots.  */
2297 	}
2298     }
2299   else
2300     /* Diff of got against empty current_got is got itself.  */
2301     {
2302       /* Create empty current_got to put subsequent GOTs to.  */
2303       arg->current_got = elf_m68k_create_empty_got (arg->info);
2304       if (arg->current_got == NULL)
2305 	{
2306 	  arg->error_p = TRUE;
2307 	  goto final_return;
2308 	}
2309 
2310       arg->current_got->offset = arg->offset;
2311 
2312       diff = got;
2313     }
2314 
2315   if (diff != NULL)
2316     {
2317       if (!elf_m68k_merge_gots (arg->current_got, diff, arg->info))
2318 	{
2319 	  arg->error_p = TRUE;
2320 	  goto final_return;
2321 	}
2322 
2323       /* Now we can free GOT.  */
2324       elf_m68k_clear_got (got);
2325 
2326       entry->got = arg->current_got;
2327     }
2328   else
2329     {
2330       /* Finish up current_got.  */
2331       elf_m68k_partition_multi_got_2 (arg);
2332 
2333       /* Schedule to start a new current_got.  */
2334       arg->current_got = NULL;
2335 
2336       /* Retry.  */
2337       if (!elf_m68k_partition_multi_got_1 (_entry, _arg))
2338 	{
2339 	  BFD_ASSERT (arg->error_p);
2340 	  goto final_return;
2341 	}
2342     }
2343 
2344  final_return:
2345   if (diff != NULL)
2346     elf_m68k_clear_got (diff);
2347 
2348   return arg->error_p == FALSE ? 1 : 0;
2349 }
2350 
2351 /* Helper function to build symndx2h mapping.  */
2352 
2353 static bfd_boolean
2354 elf_m68k_init_symndx2h_1 (struct elf_link_hash_entry *_h,
2355 			  void *_arg)
2356 {
2357   struct elf_m68k_link_hash_entry *h;
2358 
2359   h = elf_m68k_hash_entry (_h);
2360 
2361   if (h->got_entry_key != 0)
2362     /* H has at least one entry in the GOT.  */
2363     {
2364       struct elf_m68k_partition_multi_got_arg *arg;
2365 
2366       arg = (struct elf_m68k_partition_multi_got_arg *) _arg;
2367 
2368       BFD_ASSERT (arg->symndx2h[h->got_entry_key] == NULL);
2369       arg->symndx2h[h->got_entry_key] = h;
2370     }
2371 
2372   return TRUE;
2373 }
2374 
2375 /* Merge GOTs of some BFDs, assign offsets to GOT entries and build
2376    lists of GOT entries for global symbols.
2377    Calculate sizes of .got and .rela.got sections.  */
2378 
2379 static bfd_boolean
2380 elf_m68k_partition_multi_got (struct bfd_link_info *info)
2381 {
2382   struct elf_m68k_multi_got *multi_got;
2383   struct elf_m68k_partition_multi_got_arg arg_;
2384 
2385   multi_got = elf_m68k_multi_got (info);
2386 
2387   arg_.current_got = NULL;
2388   arg_.offset = 0;
2389   arg_.info = info;
2390   arg_.n_slots = 0;
2391   arg_.slots_relas_diff = 0;
2392   arg_.error_p = FALSE;
2393 
2394   if (multi_got->bfd2got != NULL)
2395     {
2396       /* Initialize symndx2h mapping.  */
2397       {
2398 	arg_.symndx2h = bfd_zmalloc (multi_got->global_symndx
2399 				     * sizeof (*arg_.symndx2h));
2400 	if (arg_.symndx2h == NULL)
2401 	  return FALSE;
2402 
2403 	elf_link_hash_traverse (elf_hash_table (info),
2404 				elf_m68k_init_symndx2h_1, &arg_);
2405       }
2406 
2407       /* Partition.  */
2408       htab_traverse (multi_got->bfd2got, elf_m68k_partition_multi_got_1,
2409 		     &arg_);
2410       if (arg_.error_p)
2411 	{
2412 	  free (arg_.symndx2h);
2413 	  arg_.symndx2h = NULL;
2414 
2415 	  return FALSE;
2416 	}
2417 
2418       /* Finish up last current_got.  */
2419       elf_m68k_partition_multi_got_2 (&arg_);
2420 
2421       free (arg_.symndx2h);
2422     }
2423 
2424   if (elf_hash_table (info)->dynobj != NULL)
2425     /* Set sizes of .got and .rela.got sections.  */
2426     {
2427       asection *s;
2428 
2429       s = bfd_get_linker_section (elf_hash_table (info)->dynobj, ".got");
2430       if (s != NULL)
2431 	s->size = arg_.offset;
2432       else
2433 	BFD_ASSERT (arg_.offset == 0);
2434 
2435       BFD_ASSERT (arg_.slots_relas_diff <= arg_.n_slots);
2436       arg_.n_slots -= arg_.slots_relas_diff;
2437 
2438       s = bfd_get_linker_section (elf_hash_table (info)->dynobj, ".rela.got");
2439       if (s != NULL)
2440 	s->size = arg_.n_slots * sizeof (Elf32_External_Rela);
2441       else
2442 	BFD_ASSERT (arg_.n_slots == 0);
2443     }
2444   else
2445     BFD_ASSERT (multi_got->bfd2got == NULL);
2446 
2447   return TRUE;
2448 }
2449 
2450 /* Specialized version of elf_m68k_get_got_entry that returns pointer
2451    to hashtable slot, thus allowing removal of entry via
2452    elf_m68k_remove_got_entry.  */
2453 
2454 static struct elf_m68k_got_entry **
2455 elf_m68k_find_got_entry_ptr (struct elf_m68k_got *got,
2456 			     struct elf_m68k_got_entry_key *key)
2457 {
2458   void **ptr;
2459   struct elf_m68k_got_entry entry_;
2460   struct elf_m68k_got_entry **entry_ptr;
2461 
2462   entry_.key_ = *key;
2463   ptr = htab_find_slot (got->entries, &entry_, NO_INSERT);
2464   BFD_ASSERT (ptr != NULL);
2465 
2466   entry_ptr = (struct elf_m68k_got_entry **) ptr;
2467 
2468   return entry_ptr;
2469 }
2470 
2471 /* Remove entry pointed to by ENTRY_PTR from GOT.  */
2472 
2473 static void
2474 elf_m68k_remove_got_entry (struct elf_m68k_got *got,
2475 			   struct elf_m68k_got_entry **entry_ptr)
2476 {
2477   struct elf_m68k_got_entry *entry;
2478 
2479   entry = *entry_ptr;
2480 
2481   /* Check that offsets have not been finalized yet.  */
2482   BFD_ASSERT (got->offset == (bfd_vma) -1);
2483   /* Check that this entry is indeed unused.  */
2484   BFD_ASSERT (entry->u.s1.refcount == 0);
2485 
2486   elf_m68k_remove_got_entry_type (got, entry->key_.type);
2487 
2488   if (entry->key_.bfd != NULL)
2489     got->local_n_slots -= elf_m68k_reloc_got_n_slots (entry->key_.type);
2490 
2491   BFD_ASSERT (got->n_slots[R_32] >= got->local_n_slots);
2492 
2493   htab_clear_slot (got->entries, (void **) entry_ptr);
2494 }
2495 
2496 /* Copy any information related to dynamic linking from a pre-existing
2497    symbol to a newly created symbol.  Also called to copy flags and
2498    other back-end info to a weakdef, in which case the symbol is not
2499    newly created and plt/got refcounts and dynamic indices should not
2500    be copied.  */
2501 
2502 static void
2503 elf_m68k_copy_indirect_symbol (struct bfd_link_info *info,
2504 			       struct elf_link_hash_entry *_dir,
2505 			       struct elf_link_hash_entry *_ind)
2506 {
2507   struct elf_m68k_link_hash_entry *dir;
2508   struct elf_m68k_link_hash_entry *ind;
2509 
2510   _bfd_elf_link_hash_copy_indirect (info, _dir, _ind);
2511 
2512   if (_ind->root.type != bfd_link_hash_indirect)
2513     return;
2514 
2515   dir = elf_m68k_hash_entry (_dir);
2516   ind = elf_m68k_hash_entry (_ind);
2517 
2518   /* Any absolute non-dynamic relocations against an indirect or weak
2519      definition will be against the target symbol.  */
2520   _dir->non_got_ref |= _ind->non_got_ref;
2521 
2522   /* We might have a direct symbol already having entries in the GOTs.
2523      Update its key only in case indirect symbol has GOT entries and
2524      assert that both indirect and direct symbols don't have GOT entries
2525      at the same time.  */
2526   if (ind->got_entry_key != 0)
2527     {
2528       BFD_ASSERT (dir->got_entry_key == 0);
2529       /* Assert that GOTs aren't partitioned yet.  */
2530       BFD_ASSERT (ind->glist == NULL);
2531 
2532       dir->got_entry_key = ind->got_entry_key;
2533       ind->got_entry_key = 0;
2534     }
2535 }
2536 
2537 /* Look through the relocs for a section during the first phase, and
2538    allocate space in the global offset table or procedure linkage
2539    table.  */
2540 
2541 static bfd_boolean
2542 elf_m68k_check_relocs (bfd *abfd,
2543 		       struct bfd_link_info *info,
2544 		       asection *sec,
2545 		       const Elf_Internal_Rela *relocs)
2546 {
2547   bfd *dynobj;
2548   Elf_Internal_Shdr *symtab_hdr;
2549   struct elf_link_hash_entry **sym_hashes;
2550   const Elf_Internal_Rela *rel;
2551   const Elf_Internal_Rela *rel_end;
2552   asection *sgot;
2553   asection *srelgot;
2554   asection *sreloc;
2555   struct elf_m68k_got *got;
2556 
2557   if (bfd_link_relocatable (info))
2558     return TRUE;
2559 
2560   dynobj = elf_hash_table (info)->dynobj;
2561   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2562   sym_hashes = elf_sym_hashes (abfd);
2563 
2564   sgot = NULL;
2565   srelgot = NULL;
2566   sreloc = NULL;
2567 
2568   got = NULL;
2569 
2570   rel_end = relocs + sec->reloc_count;
2571   for (rel = relocs; rel < rel_end; rel++)
2572     {
2573       unsigned long r_symndx;
2574       struct elf_link_hash_entry *h;
2575 
2576       r_symndx = ELF32_R_SYM (rel->r_info);
2577 
2578       if (r_symndx < symtab_hdr->sh_info)
2579 	h = NULL;
2580       else
2581 	{
2582 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2583 	  while (h->root.type == bfd_link_hash_indirect
2584 		 || h->root.type == bfd_link_hash_warning)
2585 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2586 
2587 	  /* PR15323, ref flags aren't set for references in the same
2588 	     object.  */
2589 	  h->root.non_ir_ref = 1;
2590 	}
2591 
2592       switch (ELF32_R_TYPE (rel->r_info))
2593 	{
2594 	case R_68K_GOT8:
2595 	case R_68K_GOT16:
2596 	case R_68K_GOT32:
2597 	  if (h != NULL
2598 	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2599 	    break;
2600 	  /* Fall through.  */
2601 
2602 	  /* Relative GOT relocations.  */
2603 	case R_68K_GOT8O:
2604 	case R_68K_GOT16O:
2605 	case R_68K_GOT32O:
2606 	  /* Fall through.  */
2607 
2608 	  /* TLS relocations.  */
2609 	case R_68K_TLS_GD8:
2610 	case R_68K_TLS_GD16:
2611 	case R_68K_TLS_GD32:
2612 	case R_68K_TLS_LDM8:
2613 	case R_68K_TLS_LDM16:
2614 	case R_68K_TLS_LDM32:
2615 	case R_68K_TLS_IE8:
2616 	case R_68K_TLS_IE16:
2617 	case R_68K_TLS_IE32:
2618 
2619 	case R_68K_TLS_TPREL32:
2620 	case R_68K_TLS_DTPREL32:
2621 
2622 	  if (ELF32_R_TYPE (rel->r_info) == R_68K_TLS_TPREL32
2623 	      && bfd_link_pic (info))
2624 	    /* Do the special chorus for libraries with static TLS.  */
2625 	    info->flags |= DF_STATIC_TLS;
2626 
2627 	  /* This symbol requires a global offset table entry.  */
2628 
2629 	  if (dynobj == NULL)
2630 	    {
2631 	      /* Create the .got section.  */
2632 	      elf_hash_table (info)->dynobj = dynobj = abfd;
2633 	      if (!_bfd_elf_create_got_section (dynobj, info))
2634 		return FALSE;
2635 	    }
2636 
2637 	  if (sgot == NULL)
2638 	    {
2639 	      sgot = bfd_get_linker_section (dynobj, ".got");
2640 	      BFD_ASSERT (sgot != NULL);
2641 	    }
2642 
2643 	  if (srelgot == NULL
2644 	      && (h != NULL || bfd_link_pic (info)))
2645 	    {
2646 	      srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2647 	      if (srelgot == NULL)
2648 		{
2649 		  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2650 				    | SEC_IN_MEMORY | SEC_LINKER_CREATED
2651 				    | SEC_READONLY);
2652 		  srelgot = bfd_make_section_anyway_with_flags (dynobj,
2653 								".rela.got",
2654 								flags);
2655 		  if (srelgot == NULL
2656 		      || !bfd_set_section_alignment (dynobj, srelgot, 2))
2657 		    return FALSE;
2658 		}
2659 	    }
2660 
2661 	  if (got == NULL)
2662 	    {
2663 	      struct elf_m68k_bfd2got_entry *bfd2got_entry;
2664 
2665 	      bfd2got_entry
2666 		= elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
2667 					      abfd, FIND_OR_CREATE, info);
2668 	      if (bfd2got_entry == NULL)
2669 		return FALSE;
2670 
2671 	      got = bfd2got_entry->got;
2672 	      BFD_ASSERT (got != NULL);
2673 	    }
2674 
2675 	  {
2676 	    struct elf_m68k_got_entry *got_entry;
2677 
2678 	    /* Add entry to got.  */
2679 	    got_entry = elf_m68k_add_entry_to_got (got, h, abfd,
2680 						   ELF32_R_TYPE (rel->r_info),
2681 						   r_symndx, info);
2682 	    if (got_entry == NULL)
2683 	      return FALSE;
2684 
2685 	    if (got_entry->u.s1.refcount == 1)
2686 	      {
2687 		/* Make sure this symbol is output as a dynamic symbol.  */
2688 		if (h != NULL
2689 		    && h->dynindx == -1
2690 		    && !h->forced_local)
2691 		  {
2692 		    if (!bfd_elf_link_record_dynamic_symbol (info, h))
2693 		      return FALSE;
2694 		  }
2695 	      }
2696 	  }
2697 
2698 	  break;
2699 
2700 	case R_68K_PLT8:
2701 	case R_68K_PLT16:
2702 	case R_68K_PLT32:
2703 	  /* This symbol requires a procedure linkage table entry.  We
2704 	     actually build the entry in adjust_dynamic_symbol,
2705              because this might be a case of linking PIC code which is
2706              never referenced by a dynamic object, in which case we
2707              don't need to generate a procedure linkage table entry
2708              after all.  */
2709 
2710 	  /* If this is a local symbol, we resolve it directly without
2711 	     creating a procedure linkage table entry.  */
2712 	  if (h == NULL)
2713 	    continue;
2714 
2715 	  h->needs_plt = 1;
2716 	  h->plt.refcount++;
2717 	  break;
2718 
2719 	case R_68K_PLT8O:
2720 	case R_68K_PLT16O:
2721 	case R_68K_PLT32O:
2722 	  /* This symbol requires a procedure linkage table entry.  */
2723 
2724 	  if (h == NULL)
2725 	    {
2726 	      /* It does not make sense to have this relocation for a
2727 		 local symbol.  FIXME: does it?  How to handle it if
2728 		 it does make sense?  */
2729 	      bfd_set_error (bfd_error_bad_value);
2730 	      return FALSE;
2731 	    }
2732 
2733 	  /* Make sure this symbol is output as a dynamic symbol.  */
2734 	  if (h->dynindx == -1
2735 	      && !h->forced_local)
2736 	    {
2737 	      if (!bfd_elf_link_record_dynamic_symbol (info, h))
2738 		return FALSE;
2739 	    }
2740 
2741 	  h->needs_plt = 1;
2742 	  h->plt.refcount++;
2743 	  break;
2744 
2745 	case R_68K_PC8:
2746 	case R_68K_PC16:
2747 	case R_68K_PC32:
2748 	  /* If we are creating a shared library and this is not a local
2749 	     symbol, we need to copy the reloc into the shared library.
2750 	     However when linking with -Bsymbolic and this is a global
2751 	     symbol which is defined in an object we are including in the
2752 	     link (i.e., DEF_REGULAR is set), then we can resolve the
2753 	     reloc directly.  At this point we have not seen all the input
2754 	     files, so it is possible that DEF_REGULAR is not set now but
2755 	     will be set later (it is never cleared).  We account for that
2756 	     possibility below by storing information in the
2757 	     pcrel_relocs_copied field of the hash table entry.  */
2758 	  if (!(bfd_link_pic (info)
2759 		&& (sec->flags & SEC_ALLOC) != 0
2760 		&& h != NULL
2761 		&& (!SYMBOLIC_BIND (info, h)
2762 		    || h->root.type == bfd_link_hash_defweak
2763 		    || !h->def_regular)))
2764 	    {
2765 	      if (h != NULL)
2766 		{
2767 		  /* Make sure a plt entry is created for this symbol if
2768 		     it turns out to be a function defined by a dynamic
2769 		     object.  */
2770 		  h->plt.refcount++;
2771 		}
2772 	      break;
2773 	    }
2774 	  /* Fall through.  */
2775 	case R_68K_8:
2776 	case R_68K_16:
2777 	case R_68K_32:
2778 	  /* We don't need to handle relocs into sections not going into
2779 	     the "real" output.  */
2780 	  if ((sec->flags & SEC_ALLOC) == 0)
2781 	      break;
2782 
2783 	  if (h != NULL)
2784 	    {
2785 	      /* Make sure a plt entry is created for this symbol if it
2786 		 turns out to be a function defined by a dynamic object.  */
2787 	      h->plt.refcount++;
2788 
2789 	      if (bfd_link_executable (info))
2790 		/* This symbol needs a non-GOT reference.  */
2791 		h->non_got_ref = 1;
2792 	    }
2793 
2794 	  /* If we are creating a shared library, we need to copy the
2795 	     reloc into the shared library.  */
2796 	  if (bfd_link_pic (info))
2797 	    {
2798 	      /* When creating a shared object, we must copy these
2799 		 reloc types into the output file.  We create a reloc
2800 		 section in dynobj and make room for this reloc.  */
2801 	      if (sreloc == NULL)
2802 		{
2803 		  sreloc = _bfd_elf_make_dynamic_reloc_section
2804 		    (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
2805 
2806 		  if (sreloc == NULL)
2807 		    return FALSE;
2808 		}
2809 
2810 	      if (sec->flags & SEC_READONLY
2811 		  /* Don't set DF_TEXTREL yet for PC relative
2812 		     relocations, they might be discarded later.  */
2813 		  && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2814 		       || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2815 		       || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
2816 		{
2817 		  if (info->warn_shared_textrel)
2818 		    (*_bfd_error_handler)
2819 		      (_("warning: dynamic relocation to `%s' in readonly section `%s'"),
2820 		      h->root.root.string, sec->name);
2821 		  info->flags |= DF_TEXTREL;
2822 		}
2823 
2824 	      sreloc->size += sizeof (Elf32_External_Rela);
2825 
2826 	      /* We count the number of PC relative relocations we have
2827 		 entered for this symbol, so that we can discard them
2828 		 again if, in the -Bsymbolic case, the symbol is later
2829 		 defined by a regular object, or, in the normal shared
2830 		 case, the symbol is forced to be local.  Note that this
2831 		 function is only called if we are using an m68kelf linker
2832 		 hash table, which means that h is really a pointer to an
2833 		 elf_m68k_link_hash_entry.  */
2834 	      if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
2835 		  || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
2836 		  || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
2837 		{
2838 		  struct elf_m68k_pcrel_relocs_copied *p;
2839 		  struct elf_m68k_pcrel_relocs_copied **head;
2840 
2841 		  if (h != NULL)
2842 		    {
2843 		      struct elf_m68k_link_hash_entry *eh
2844 			= elf_m68k_hash_entry (h);
2845 		      head = &eh->pcrel_relocs_copied;
2846 		    }
2847 		  else
2848 		    {
2849 		      asection *s;
2850 		      void *vpp;
2851 		      Elf_Internal_Sym *isym;
2852 
2853 		      isym = bfd_sym_from_r_symndx (&elf_m68k_hash_table (info)->sym_cache,
2854 						    abfd, r_symndx);
2855 		      if (isym == NULL)
2856 			return FALSE;
2857 
2858 		      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2859 		      if (s == NULL)
2860 			s = sec;
2861 
2862 		      vpp = &elf_section_data (s)->local_dynrel;
2863 		      head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
2864 		    }
2865 
2866 		  for (p = *head; p != NULL; p = p->next)
2867 		    if (p->section == sreloc)
2868 		      break;
2869 
2870 		  if (p == NULL)
2871 		    {
2872 		      p = ((struct elf_m68k_pcrel_relocs_copied *)
2873 			   bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2874 		      if (p == NULL)
2875 			return FALSE;
2876 		      p->next = *head;
2877 		      *head = p;
2878 		      p->section = sreloc;
2879 		      p->count = 0;
2880 		    }
2881 
2882 		  ++p->count;
2883 		}
2884 	    }
2885 
2886 	  break;
2887 
2888 	  /* This relocation describes the C++ object vtable hierarchy.
2889 	     Reconstruct it for later use during GC.  */
2890 	case R_68K_GNU_VTINHERIT:
2891 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2892 	    return FALSE;
2893 	  break;
2894 
2895 	  /* This relocation describes which C++ vtable entries are actually
2896 	     used.  Record for later use during GC.  */
2897 	case R_68K_GNU_VTENTRY:
2898 	  BFD_ASSERT (h != NULL);
2899 	  if (h != NULL
2900 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2901 	    return FALSE;
2902 	  break;
2903 
2904 	default:
2905 	  break;
2906 	}
2907     }
2908 
2909   return TRUE;
2910 }
2911 
2912 /* Return the section that should be marked against GC for a given
2913    relocation.  */
2914 
2915 static asection *
2916 elf_m68k_gc_mark_hook (asection *sec,
2917 		       struct bfd_link_info *info,
2918 		       Elf_Internal_Rela *rel,
2919 		       struct elf_link_hash_entry *h,
2920 		       Elf_Internal_Sym *sym)
2921 {
2922   if (h != NULL)
2923     switch (ELF32_R_TYPE (rel->r_info))
2924       {
2925       case R_68K_GNU_VTINHERIT:
2926       case R_68K_GNU_VTENTRY:
2927 	return NULL;
2928       }
2929 
2930   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2931 }
2932 
2933 /* Update the got entry reference counts for the section being removed.  */
2934 
2935 static bfd_boolean
2936 elf_m68k_gc_sweep_hook (bfd *abfd,
2937 			struct bfd_link_info *info,
2938 			asection *sec,
2939 			const Elf_Internal_Rela *relocs)
2940 {
2941   Elf_Internal_Shdr *symtab_hdr;
2942   struct elf_link_hash_entry **sym_hashes;
2943   const Elf_Internal_Rela *rel, *relend;
2944   bfd *dynobj;
2945   struct elf_m68k_got *got;
2946 
2947   if (bfd_link_relocatable (info))
2948     return TRUE;
2949 
2950   dynobj = elf_hash_table (info)->dynobj;
2951   if (dynobj == NULL)
2952     return TRUE;
2953 
2954   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2955   sym_hashes = elf_sym_hashes (abfd);
2956   got = NULL;
2957 
2958   relend = relocs + sec->reloc_count;
2959   for (rel = relocs; rel < relend; rel++)
2960     {
2961       unsigned long r_symndx;
2962       struct elf_link_hash_entry *h = NULL;
2963 
2964       r_symndx = ELF32_R_SYM (rel->r_info);
2965       if (r_symndx >= symtab_hdr->sh_info)
2966 	{
2967 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2968 	  while (h->root.type == bfd_link_hash_indirect
2969 		 || h->root.type == bfd_link_hash_warning)
2970 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2971 	}
2972 
2973       switch (ELF32_R_TYPE (rel->r_info))
2974 	{
2975 	case R_68K_GOT8:
2976 	case R_68K_GOT16:
2977 	case R_68K_GOT32:
2978 	  if (h != NULL
2979 	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2980 	    break;
2981 
2982 	  /* FALLTHRU */
2983 	case R_68K_GOT8O:
2984 	case R_68K_GOT16O:
2985 	case R_68K_GOT32O:
2986 	  /* Fall through.  */
2987 
2988 	  /* TLS relocations.  */
2989 	case R_68K_TLS_GD8:
2990 	case R_68K_TLS_GD16:
2991 	case R_68K_TLS_GD32:
2992 	case R_68K_TLS_LDM8:
2993 	case R_68K_TLS_LDM16:
2994 	case R_68K_TLS_LDM32:
2995 	case R_68K_TLS_IE8:
2996 	case R_68K_TLS_IE16:
2997 	case R_68K_TLS_IE32:
2998 
2999 	case R_68K_TLS_TPREL32:
3000 	case R_68K_TLS_DTPREL32:
3001 
3002 	  if (got == NULL)
3003 	    {
3004 	      got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3005 						abfd, MUST_FIND, NULL)->got;
3006 	      BFD_ASSERT (got != NULL);
3007 	    }
3008 
3009 	  {
3010 	    struct elf_m68k_got_entry_key key_;
3011 	    struct elf_m68k_got_entry **got_entry_ptr;
3012 	    struct elf_m68k_got_entry *got_entry;
3013 
3014 	    elf_m68k_init_got_entry_key (&key_, h, abfd, r_symndx,
3015 					 ELF32_R_TYPE (rel->r_info));
3016 	    got_entry_ptr = elf_m68k_find_got_entry_ptr (got, &key_);
3017 
3018 	    got_entry = *got_entry_ptr;
3019 
3020 	    if (got_entry->u.s1.refcount > 0)
3021 	      {
3022 		--got_entry->u.s1.refcount;
3023 
3024 		if (got_entry->u.s1.refcount == 0)
3025 		  /* We don't need the .got entry any more.  */
3026 		  elf_m68k_remove_got_entry (got, got_entry_ptr);
3027 	      }
3028 	  }
3029 	  break;
3030 
3031 	case R_68K_PLT8:
3032 	case R_68K_PLT16:
3033 	case R_68K_PLT32:
3034 	case R_68K_PLT8O:
3035 	case R_68K_PLT16O:
3036 	case R_68K_PLT32O:
3037 	case R_68K_PC8:
3038 	case R_68K_PC16:
3039 	case R_68K_PC32:
3040 	case R_68K_8:
3041 	case R_68K_16:
3042 	case R_68K_32:
3043 	  if (h != NULL)
3044 	    {
3045 	      if (h->plt.refcount > 0)
3046 		--h->plt.refcount;
3047 	    }
3048 	  break;
3049 
3050 	default:
3051 	  break;
3052 	}
3053     }
3054 
3055   return TRUE;
3056 }
3057 
3058 /* Return the type of PLT associated with OUTPUT_BFD.  */
3059 
3060 static const struct elf_m68k_plt_info *
3061 elf_m68k_get_plt_info (bfd *output_bfd)
3062 {
3063   unsigned int features;
3064 
3065   features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
3066   if (features & cpu32)
3067     return &elf_cpu32_plt_info;
3068   if (features & mcfisa_b)
3069     return &elf_isab_plt_info;
3070   if (features & mcfisa_c)
3071     return &elf_isac_plt_info;
3072   return &elf_m68k_plt_info;
3073 }
3074 
3075 /* This function is called after all the input files have been read,
3076    and the input sections have been assigned to output sections.
3077    It's a convenient place to determine the PLT style.  */
3078 
3079 static bfd_boolean
3080 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
3081 {
3082   /* Bind input BFDs to GOTs and calculate sizes of .got and .rela.got
3083      sections.  */
3084   if (!elf_m68k_partition_multi_got (info))
3085     return FALSE;
3086 
3087   elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
3088   return TRUE;
3089 }
3090 
3091 /* Adjust a symbol defined by a dynamic object and referenced by a
3092    regular object.  The current definition is in some section of the
3093    dynamic object, but we're not including those sections.  We have to
3094    change the definition to something the rest of the link can
3095    understand.  */
3096 
3097 static bfd_boolean
3098 elf_m68k_adjust_dynamic_symbol (struct bfd_link_info *info,
3099 				struct elf_link_hash_entry *h)
3100 {
3101   struct elf_m68k_link_hash_table *htab;
3102   bfd *dynobj;
3103   asection *s;
3104 
3105   htab = elf_m68k_hash_table (info);
3106   dynobj = elf_hash_table (info)->dynobj;
3107 
3108   /* Make sure we know what is going on here.  */
3109   BFD_ASSERT (dynobj != NULL
3110 	      && (h->needs_plt
3111 		  || h->type == STT_GNU_IFUNC
3112 		  || h->u.weakdef != NULL
3113 		  || (h->def_dynamic
3114 		      && h->ref_regular
3115 		      && !h->def_regular)));
3116 
3117   /* If this is a function, put it in the procedure linkage table.  We
3118      will fill in the contents of the procedure linkage table later,
3119      when we know the address of the .got section.  */
3120   if ((h->type == STT_FUNC || h->type == STT_GNU_IFUNC)
3121       || h->needs_plt)
3122     {
3123       if ((h->plt.refcount <= 0
3124            || SYMBOL_CALLS_LOCAL (info, h)
3125 	   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3126 	       && h->root.type == bfd_link_hash_undefweak))
3127 	  /* We must always create the plt entry if it was referenced
3128 	     by a PLTxxO relocation.  In this case we already recorded
3129 	     it as a dynamic symbol.  */
3130 	  && h->dynindx == -1)
3131 	{
3132 	  /* This case can occur if we saw a PLTxx reloc in an input
3133 	     file, but the symbol was never referred to by a dynamic
3134 	     object, or if all references were garbage collected.  In
3135 	     such a case, we don't actually need to build a procedure
3136 	     linkage table, and we can just do a PCxx reloc instead.  */
3137 	  h->plt.offset = (bfd_vma) -1;
3138 	  h->needs_plt = 0;
3139 	  return TRUE;
3140 	}
3141 
3142       /* Make sure this symbol is output as a dynamic symbol.  */
3143       if (h->dynindx == -1
3144 	  && !h->forced_local)
3145 	{
3146 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
3147 	    return FALSE;
3148 	}
3149 
3150       s = bfd_get_linker_section (dynobj, ".plt");
3151       BFD_ASSERT (s != NULL);
3152 
3153       /* If this is the first .plt entry, make room for the special
3154 	 first entry.  */
3155       if (s->size == 0)
3156 	s->size = htab->plt_info->size;
3157 
3158       /* If this symbol is not defined in a regular file, and we are
3159 	 not generating a shared library, then set the symbol to this
3160 	 location in the .plt.  This is required to make function
3161 	 pointers compare as equal between the normal executable and
3162 	 the shared library.  */
3163       if (!bfd_link_pic (info)
3164 	  && !h->def_regular)
3165 	{
3166 	  h->root.u.def.section = s;
3167 	  h->root.u.def.value = s->size;
3168 	}
3169 
3170       h->plt.offset = s->size;
3171 
3172       /* Make room for this entry.  */
3173       s->size += htab->plt_info->size;
3174 
3175       /* We also need to make an entry in the .got.plt section, which
3176 	 will be placed in the .got section by the linker script.  */
3177       s = bfd_get_linker_section (dynobj, ".got.plt");
3178       BFD_ASSERT (s != NULL);
3179       s->size += 4;
3180 
3181       /* We also need to make an entry in the .rela.plt section.  */
3182       s = bfd_get_linker_section (dynobj, ".rela.plt");
3183       BFD_ASSERT (s != NULL);
3184       s->size += sizeof (Elf32_External_Rela);
3185 
3186       return TRUE;
3187     }
3188 
3189   /* Reinitialize the plt offset now that it is not used as a reference
3190      count any more.  */
3191   h->plt.offset = (bfd_vma) -1;
3192 
3193   /* If this is a weak symbol, and there is a real definition, the
3194      processor independent code will have arranged for us to see the
3195      real definition first, and we can just use the same value.  */
3196   if (h->u.weakdef != NULL)
3197     {
3198       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3199 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
3200       h->root.u.def.section = h->u.weakdef->root.u.def.section;
3201       h->root.u.def.value = h->u.weakdef->root.u.def.value;
3202       return TRUE;
3203     }
3204 
3205   /* This is a reference to a symbol defined by a dynamic object which
3206      is not a function.  */
3207 
3208   /* If we are creating a shared library, we must presume that the
3209      only references to the symbol are via the global offset table.
3210      For such cases we need not do anything here; the relocations will
3211      be handled correctly by relocate_section.  */
3212   if (bfd_link_pic (info))
3213     return TRUE;
3214 
3215   /* If there are no references to this symbol that do not use the
3216      GOT, we don't need to generate a copy reloc.  */
3217   if (!h->non_got_ref)
3218     return TRUE;
3219 
3220   /* We must allocate the symbol in our .dynbss section, which will
3221      become part of the .bss section of the executable.  There will be
3222      an entry for this symbol in the .dynsym section.  The dynamic
3223      object will contain position independent code, so all references
3224      from the dynamic object to this symbol will go through the global
3225      offset table.  The dynamic linker will use the .dynsym entry to
3226      determine the address it must put in the global offset table, so
3227      both the dynamic object and the regular object will refer to the
3228      same memory location for the variable.  */
3229 
3230   s = bfd_get_linker_section (dynobj, ".dynbss");
3231   BFD_ASSERT (s != NULL);
3232 
3233   /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
3234      copy the initial value out of the dynamic object and into the
3235      runtime process image.  We need to remember the offset into the
3236      .rela.bss section we are going to use.  */
3237   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3238     {
3239       asection *srel;
3240 
3241       srel = bfd_get_linker_section (dynobj, ".rela.bss");
3242       BFD_ASSERT (srel != NULL);
3243       srel->size += sizeof (Elf32_External_Rela);
3244       h->needs_copy = 1;
3245     }
3246 
3247   return _bfd_elf_adjust_dynamic_copy (info, h, s);
3248 }
3249 
3250 /* Set the sizes of the dynamic sections.  */
3251 
3252 static bfd_boolean
3253 elf_m68k_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3254 				struct bfd_link_info *info)
3255 {
3256   bfd *dynobj;
3257   asection *s;
3258   bfd_boolean plt;
3259   bfd_boolean relocs;
3260 
3261   dynobj = elf_hash_table (info)->dynobj;
3262   BFD_ASSERT (dynobj != NULL);
3263 
3264   if (elf_hash_table (info)->dynamic_sections_created)
3265     {
3266       /* Set the contents of the .interp section to the interpreter.  */
3267       if (bfd_link_executable (info) && !info->nointerp)
3268 	{
3269 	  s = bfd_get_linker_section (dynobj, ".interp");
3270 	  BFD_ASSERT (s != NULL);
3271 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3272 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3273 	}
3274     }
3275   else
3276     {
3277       /* We may have created entries in the .rela.got section.
3278 	 However, if we are not creating the dynamic sections, we will
3279 	 not actually use these entries.  Reset the size of .rela.got,
3280 	 which will cause it to get stripped from the output file
3281 	 below.  */
3282       s = bfd_get_linker_section (dynobj, ".rela.got");
3283       if (s != NULL)
3284 	s->size = 0;
3285     }
3286 
3287   /* If this is a -Bsymbolic shared link, then we need to discard all
3288      PC relative relocs against symbols defined in a regular object.
3289      For the normal shared case we discard the PC relative relocs
3290      against symbols that have become local due to visibility changes.
3291      We allocated space for them in the check_relocs routine, but we
3292      will not fill them in in the relocate_section routine.  */
3293   if (bfd_link_pic (info))
3294     elf_link_hash_traverse (elf_hash_table (info),
3295 			    elf_m68k_discard_copies,
3296 			    info);
3297 
3298   /* The check_relocs and adjust_dynamic_symbol entry points have
3299      determined the sizes of the various dynamic sections.  Allocate
3300      memory for them.  */
3301   plt = FALSE;
3302   relocs = FALSE;
3303   for (s = dynobj->sections; s != NULL; s = s->next)
3304     {
3305       const char *name;
3306 
3307       if ((s->flags & SEC_LINKER_CREATED) == 0)
3308 	continue;
3309 
3310       /* It's OK to base decisions on the section name, because none
3311 	 of the dynobj section names depend upon the input files.  */
3312       name = bfd_get_section_name (dynobj, s);
3313 
3314       if (strcmp (name, ".plt") == 0)
3315 	{
3316 	  /* Remember whether there is a PLT.  */
3317 	  plt = s->size != 0;
3318 	}
3319       else if (CONST_STRNEQ (name, ".rela"))
3320 	{
3321 	  if (s->size != 0)
3322 	    {
3323 	      relocs = TRUE;
3324 
3325 	      /* We use the reloc_count field as a counter if we need
3326 		 to copy relocs into the output file.  */
3327 	      s->reloc_count = 0;
3328 	    }
3329 	}
3330       else if (! CONST_STRNEQ (name, ".got")
3331 	       && strcmp (name, ".dynbss") != 0)
3332 	{
3333 	  /* It's not one of our sections, so don't allocate space.  */
3334 	  continue;
3335 	}
3336 
3337       if (s->size == 0)
3338 	{
3339 	  /* If we don't need this section, strip it from the
3340 	     output file.  This is mostly to handle .rela.bss and
3341 	     .rela.plt.  We must create both sections in
3342 	     create_dynamic_sections, because they must be created
3343 	     before the linker maps input sections to output
3344 	     sections.  The linker does that before
3345 	     adjust_dynamic_symbol is called, and it is that
3346 	     function which decides whether anything needs to go
3347 	     into these sections.  */
3348 	  s->flags |= SEC_EXCLUDE;
3349 	  continue;
3350 	}
3351 
3352       if ((s->flags & SEC_HAS_CONTENTS) == 0)
3353 	continue;
3354 
3355       /* Allocate memory for the section contents.  */
3356       /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
3357 	 Unused entries should be reclaimed before the section's contents
3358 	 are written out, but at the moment this does not happen.  Thus in
3359 	 order to prevent writing out garbage, we initialise the section's
3360 	 contents to zero.  */
3361       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3362       if (s->contents == NULL)
3363 	return FALSE;
3364     }
3365 
3366   if (elf_hash_table (info)->dynamic_sections_created)
3367     {
3368       /* Add some entries to the .dynamic section.  We fill in the
3369 	 values later, in elf_m68k_finish_dynamic_sections, but we
3370 	 must add the entries now so that we get the correct size for
3371 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
3372 	 dynamic linker and used by the debugger.  */
3373 #define add_dynamic_entry(TAG, VAL) \
3374   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3375 
3376       if (bfd_link_executable (info))
3377 	{
3378 	  if (!add_dynamic_entry (DT_DEBUG, 0))
3379 	    return FALSE;
3380 	}
3381 
3382       if (plt)
3383 	{
3384 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
3385 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
3386 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3387 	      || !add_dynamic_entry (DT_JMPREL, 0))
3388 	    return FALSE;
3389 	}
3390 
3391       if (relocs)
3392 	{
3393 	  if (!add_dynamic_entry (DT_RELA, 0)
3394 	      || !add_dynamic_entry (DT_RELASZ, 0)
3395 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3396 	    return FALSE;
3397 	}
3398 
3399       if ((info->flags & DF_TEXTREL) != 0)
3400 	{
3401 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
3402 	    return FALSE;
3403 	}
3404     }
3405 #undef add_dynamic_entry
3406 
3407   return TRUE;
3408 }
3409 
3410 /* This function is called via elf_link_hash_traverse if we are
3411    creating a shared object.  In the -Bsymbolic case it discards the
3412    space allocated to copy PC relative relocs against symbols which
3413    are defined in regular objects.  For the normal shared case, it
3414    discards space for pc-relative relocs that have become local due to
3415    symbol visibility changes.  We allocated space for them in the
3416    check_relocs routine, but we won't fill them in in the
3417    relocate_section routine.
3418 
3419    We also check whether any of the remaining relocations apply
3420    against a readonly section, and set the DF_TEXTREL flag in this
3421    case.  */
3422 
3423 static bfd_boolean
3424 elf_m68k_discard_copies (struct elf_link_hash_entry *h,
3425 			 void * inf)
3426 {
3427   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3428   struct elf_m68k_pcrel_relocs_copied *s;
3429 
3430   if (!SYMBOL_CALLS_LOCAL (info, h))
3431     {
3432       if ((info->flags & DF_TEXTREL) == 0)
3433 	{
3434 	  /* Look for relocations against read-only sections.  */
3435 	  for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
3436 	       s != NULL;
3437 	       s = s->next)
3438 	    if ((s->section->flags & SEC_READONLY) != 0)
3439 	      {
3440 		if (info->warn_shared_textrel)
3441 		  (*_bfd_error_handler)
3442 		    (_("warning: dynamic relocation to `%s' in readonly section `%s'"),
3443 		    h->root.root.string, s->section->name);
3444 		info->flags |= DF_TEXTREL;
3445 		break;
3446 	      }
3447 	}
3448 
3449       /* Make sure undefined weak symbols are output as a dynamic symbol
3450 	 in PIEs.  */
3451       if (h->non_got_ref
3452 	  && h->root.type == bfd_link_hash_undefweak
3453 	  && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3454 	  && h->dynindx == -1
3455 	  && !h->forced_local)
3456 	{
3457 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
3458 	    return FALSE;
3459 	}
3460 
3461       return TRUE;
3462     }
3463 
3464   for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
3465        s != NULL;
3466        s = s->next)
3467     s->section->size -= s->count * sizeof (Elf32_External_Rela);
3468 
3469   return TRUE;
3470 }
3471 
3472 
3473 /* Install relocation RELA.  */
3474 
3475 static void
3476 elf_m68k_install_rela (bfd *output_bfd,
3477 		       asection *srela,
3478 		       Elf_Internal_Rela *rela)
3479 {
3480   bfd_byte *loc;
3481 
3482   loc = srela->contents;
3483   loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3484   bfd_elf32_swap_reloca_out (output_bfd, rela, loc);
3485 }
3486 
3487 /* Find the base offsets for thread-local storage in this object,
3488    for GD/LD and IE/LE respectively.  */
3489 
3490 #define DTP_OFFSET 0x8000
3491 #define TP_OFFSET  0x7000
3492 
3493 static bfd_vma
3494 dtpoff_base (struct bfd_link_info *info)
3495 {
3496   /* If tls_sec is NULL, we should have signalled an error already.  */
3497   if (elf_hash_table (info)->tls_sec == NULL)
3498     return 0;
3499   return elf_hash_table (info)->tls_sec->vma + DTP_OFFSET;
3500 }
3501 
3502 static bfd_vma
3503 tpoff_base (struct bfd_link_info *info)
3504 {
3505   /* If tls_sec is NULL, we should have signalled an error already.  */
3506   if (elf_hash_table (info)->tls_sec == NULL)
3507     return 0;
3508   return elf_hash_table (info)->tls_sec->vma + TP_OFFSET;
3509 }
3510 
3511 /* Output necessary relocation to handle a symbol during static link.
3512    This function is called from elf_m68k_relocate_section.  */
3513 
3514 static void
3515 elf_m68k_init_got_entry_static (struct bfd_link_info *info,
3516 				bfd *output_bfd,
3517 				enum elf_m68k_reloc_type r_type,
3518 				asection *sgot,
3519 				bfd_vma got_entry_offset,
3520 				bfd_vma relocation)
3521 {
3522   switch (elf_m68k_reloc_got_type (r_type))
3523     {
3524     case R_68K_GOT32O:
3525       bfd_put_32 (output_bfd, relocation, sgot->contents + got_entry_offset);
3526       break;
3527 
3528     case R_68K_TLS_GD32:
3529       /* We know the offset within the module,
3530 	 put it into the second GOT slot.  */
3531       bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3532 		  sgot->contents + got_entry_offset + 4);
3533       /* FALLTHRU */
3534 
3535     case R_68K_TLS_LDM32:
3536       /* Mark it as belonging to module 1, the executable.  */
3537       bfd_put_32 (output_bfd, 1, sgot->contents + got_entry_offset);
3538       break;
3539 
3540     case R_68K_TLS_IE32:
3541       bfd_put_32 (output_bfd, relocation - tpoff_base (info),
3542 		  sgot->contents + got_entry_offset);
3543       break;
3544 
3545     default:
3546       BFD_ASSERT (FALSE);
3547     }
3548 }
3549 
3550 /* Output necessary relocation to handle a local symbol
3551    during dynamic link.
3552    This function is called either from elf_m68k_relocate_section
3553    or from elf_m68k_finish_dynamic_symbol.  */
3554 
3555 static void
3556 elf_m68k_init_got_entry_local_shared (struct bfd_link_info *info,
3557 				      bfd *output_bfd,
3558 				      enum elf_m68k_reloc_type r_type,
3559 				      asection *sgot,
3560 				      bfd_vma got_entry_offset,
3561 				      bfd_vma relocation,
3562 				      asection *srela)
3563 {
3564   Elf_Internal_Rela outrel;
3565 
3566   switch (elf_m68k_reloc_got_type (r_type))
3567     {
3568     case R_68K_GOT32O:
3569       /* Emit RELATIVE relocation to initialize GOT slot
3570 	 at run-time.  */
3571       outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
3572       outrel.r_addend = relocation;
3573       break;
3574 
3575     case R_68K_TLS_GD32:
3576       /* We know the offset within the module,
3577 	 put it into the second GOT slot.  */
3578       bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3579 		  sgot->contents + got_entry_offset + 4);
3580       /* FALLTHRU */
3581 
3582     case R_68K_TLS_LDM32:
3583       /* We don't know the module number,
3584 	 create a relocation for it.  */
3585       outrel.r_info = ELF32_R_INFO (0, R_68K_TLS_DTPMOD32);
3586       outrel.r_addend = 0;
3587       break;
3588 
3589     case R_68K_TLS_IE32:
3590       /* Emit TPREL relocation to initialize GOT slot
3591 	 at run-time.  */
3592       outrel.r_info = ELF32_R_INFO (0, R_68K_TLS_TPREL32);
3593       outrel.r_addend = relocation - elf_hash_table (info)->tls_sec->vma;
3594       break;
3595 
3596     default:
3597       BFD_ASSERT (FALSE);
3598     }
3599 
3600   /* Offset of the GOT entry.  */
3601   outrel.r_offset = (sgot->output_section->vma
3602 		     + sgot->output_offset
3603 		     + got_entry_offset);
3604 
3605   /* Install one of the above relocations.  */
3606   elf_m68k_install_rela (output_bfd, srela, &outrel);
3607 
3608   bfd_put_32 (output_bfd, outrel.r_addend, sgot->contents + got_entry_offset);
3609 }
3610 
3611 /* Relocate an M68K ELF section.  */
3612 
3613 static bfd_boolean
3614 elf_m68k_relocate_section (bfd *output_bfd,
3615 			   struct bfd_link_info *info,
3616 			   bfd *input_bfd,
3617 			   asection *input_section,
3618 			   bfd_byte *contents,
3619 			   Elf_Internal_Rela *relocs,
3620 			   Elf_Internal_Sym *local_syms,
3621 			   asection **local_sections)
3622 {
3623   bfd *dynobj;
3624   Elf_Internal_Shdr *symtab_hdr;
3625   struct elf_link_hash_entry **sym_hashes;
3626   asection *sgot;
3627   asection *splt;
3628   asection *sreloc;
3629   asection *srela;
3630   struct elf_m68k_got *got;
3631   Elf_Internal_Rela *rel;
3632   Elf_Internal_Rela *relend;
3633 
3634   dynobj = elf_hash_table (info)->dynobj;
3635   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3636   sym_hashes = elf_sym_hashes (input_bfd);
3637 
3638   sgot = NULL;
3639   splt = NULL;
3640   sreloc = NULL;
3641   srela = NULL;
3642 
3643   got = NULL;
3644 
3645   rel = relocs;
3646   relend = relocs + input_section->reloc_count;
3647   for (; rel < relend; rel++)
3648     {
3649       int r_type;
3650       reloc_howto_type *howto;
3651       unsigned long r_symndx;
3652       struct elf_link_hash_entry *h;
3653       Elf_Internal_Sym *sym;
3654       asection *sec;
3655       bfd_vma relocation;
3656       bfd_boolean unresolved_reloc;
3657       bfd_reloc_status_type r;
3658 
3659       r_type = ELF32_R_TYPE (rel->r_info);
3660       if (r_type < 0 || r_type >= (int) R_68K_max)
3661 	{
3662 	  bfd_set_error (bfd_error_bad_value);
3663 	  return FALSE;
3664 	}
3665       howto = howto_table + r_type;
3666 
3667       r_symndx = ELF32_R_SYM (rel->r_info);
3668 
3669       h = NULL;
3670       sym = NULL;
3671       sec = NULL;
3672       unresolved_reloc = FALSE;
3673 
3674       if (r_symndx < symtab_hdr->sh_info)
3675 	{
3676 	  sym = local_syms + r_symndx;
3677 	  sec = local_sections[r_symndx];
3678 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3679 	}
3680       else
3681 	{
3682 	  bfd_boolean warned, ignored;
3683 
3684 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3685 				   r_symndx, symtab_hdr, sym_hashes,
3686 				   h, sec, relocation,
3687 				   unresolved_reloc, warned, ignored);
3688 	}
3689 
3690       if (sec != NULL && discarded_section (sec))
3691 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3692 					 rel, 1, relend, howto, 0, contents);
3693 
3694       if (bfd_link_relocatable (info))
3695 	continue;
3696 
3697       switch (r_type)
3698 	{
3699 	case R_68K_GOT8:
3700 	case R_68K_GOT16:
3701 	case R_68K_GOT32:
3702 	  /* Relocation is to the address of the entry for this symbol
3703 	     in the global offset table.  */
3704 	  if (h != NULL
3705 	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3706 	    {
3707 	      if (elf_m68k_hash_table (info)->local_gp_p)
3708 		{
3709 		  bfd_vma sgot_output_offset;
3710 		  bfd_vma got_offset;
3711 
3712 		  if (sgot == NULL)
3713 		    {
3714 		      sgot = bfd_get_linker_section (dynobj, ".got");
3715 
3716 		      if (sgot != NULL)
3717 			sgot_output_offset = sgot->output_offset;
3718 		      else
3719 			/* In this case we have a reference to
3720 			   _GLOBAL_OFFSET_TABLE_, but the GOT itself is
3721 			   empty.
3722 			   ??? Issue a warning?  */
3723 			sgot_output_offset = 0;
3724 		    }
3725 		  else
3726 		    sgot_output_offset = sgot->output_offset;
3727 
3728 		  if (got == NULL)
3729 		    {
3730 		      struct elf_m68k_bfd2got_entry *bfd2got_entry;
3731 
3732 		      bfd2got_entry
3733 			= elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3734 						      input_bfd, SEARCH, NULL);
3735 
3736 		      if (bfd2got_entry != NULL)
3737 			{
3738 			  got = bfd2got_entry->got;
3739 			  BFD_ASSERT (got != NULL);
3740 
3741 			  got_offset = got->offset;
3742 			}
3743 		      else
3744 			/* In this case we have a reference to
3745 			   _GLOBAL_OFFSET_TABLE_, but no other references
3746 			   accessing any GOT entries.
3747 			   ??? Issue a warning?  */
3748 			got_offset = 0;
3749 		    }
3750 		  else
3751 		    got_offset = got->offset;
3752 
3753 		  /* Adjust GOT pointer to point to the GOT
3754 		     assigned to input_bfd.  */
3755 		  rel->r_addend += sgot_output_offset + got_offset;
3756 		}
3757 	      else
3758 		BFD_ASSERT (got == NULL || got->offset == 0);
3759 
3760 	      break;
3761 	    }
3762 	  /* Fall through.  */
3763 	case R_68K_GOT8O:
3764 	case R_68K_GOT16O:
3765 	case R_68K_GOT32O:
3766 
3767 	case R_68K_TLS_LDM32:
3768 	case R_68K_TLS_LDM16:
3769 	case R_68K_TLS_LDM8:
3770 
3771 	case R_68K_TLS_GD8:
3772 	case R_68K_TLS_GD16:
3773 	case R_68K_TLS_GD32:
3774 
3775 	case R_68K_TLS_IE8:
3776 	case R_68K_TLS_IE16:
3777 	case R_68K_TLS_IE32:
3778 
3779 	  /* Relocation is the offset of the entry for this symbol in
3780 	     the global offset table.  */
3781 
3782 	  {
3783 	    struct elf_m68k_got_entry_key key_;
3784 	    bfd_vma *off_ptr;
3785 	    bfd_vma off;
3786 
3787 	    if (sgot == NULL)
3788 	      {
3789 		sgot = bfd_get_linker_section (dynobj, ".got");
3790 		BFD_ASSERT (sgot != NULL);
3791 	      }
3792 
3793 	    if (got == NULL)
3794 	      {
3795 		got = elf_m68k_get_bfd2got_entry (elf_m68k_multi_got (info),
3796 						  input_bfd, MUST_FIND,
3797 						  NULL)->got;
3798 		BFD_ASSERT (got != NULL);
3799 	      }
3800 
3801 	    /* Get GOT offset for this symbol.  */
3802 	    elf_m68k_init_got_entry_key (&key_, h, input_bfd, r_symndx,
3803 					 r_type);
3804 	    off_ptr = &elf_m68k_get_got_entry (got, &key_, MUST_FIND,
3805 					       NULL)->u.s2.offset;
3806 	    off = *off_ptr;
3807 
3808 	    /* The offset must always be a multiple of 4.  We use
3809 	       the least significant bit to record whether we have
3810 	       already generated the necessary reloc.  */
3811 	    if ((off & 1) != 0)
3812 	      off &= ~1;
3813 	    else
3814 	      {
3815 		if (h != NULL
3816 		    /* @TLSLDM relocations are bounded to the module, in
3817 		       which the symbol is defined -- not to the symbol
3818 		       itself.  */
3819 		    && elf_m68k_reloc_got_type (r_type) != R_68K_TLS_LDM32)
3820 		  {
3821 		    bfd_boolean dyn;
3822 
3823 		    dyn = elf_hash_table (info)->dynamic_sections_created;
3824 		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
3825 							  bfd_link_pic (info),
3826 							  h)
3827 			|| (bfd_link_pic (info)
3828 			    && SYMBOL_REFERENCES_LOCAL (info, h))
3829 			|| (ELF_ST_VISIBILITY (h->other)
3830 			    && h->root.type == bfd_link_hash_undefweak))
3831 		      {
3832 			/* This is actually a static link, or it is a
3833 			   -Bsymbolic link and the symbol is defined
3834 			   locally, or the symbol was forced to be local
3835 			   because of a version file.  We must initialize
3836 			   this entry in the global offset table.  Since
3837 			   the offset must always be a multiple of 4, we
3838 			   use the least significant bit to record whether
3839 			   we have initialized it already.
3840 
3841 			   When doing a dynamic link, we create a .rela.got
3842 			   relocation entry to initialize the value.  This
3843 			   is done in the finish_dynamic_symbol routine.  */
3844 
3845 			elf_m68k_init_got_entry_static (info,
3846 							output_bfd,
3847 							r_type,
3848 							sgot,
3849 							off,
3850 							relocation);
3851 
3852 			*off_ptr |= 1;
3853 		      }
3854 		    else
3855 		      unresolved_reloc = FALSE;
3856 		  }
3857 		else if (bfd_link_pic (info)) /* && h == NULL */
3858 		  /* Process local symbol during dynamic link.  */
3859 		  {
3860 		    if (srela == NULL)
3861 		      {
3862 			srela = bfd_get_linker_section (dynobj, ".rela.got");
3863 			BFD_ASSERT (srela != NULL);
3864 		      }
3865 
3866 		    elf_m68k_init_got_entry_local_shared (info,
3867 							  output_bfd,
3868 							  r_type,
3869 							  sgot,
3870 							  off,
3871 							  relocation,
3872 							  srela);
3873 
3874 		    *off_ptr |= 1;
3875 		  }
3876 		else /* h == NULL && !bfd_link_pic (info) */
3877 		  {
3878 		    elf_m68k_init_got_entry_static (info,
3879 						    output_bfd,
3880 						    r_type,
3881 						    sgot,
3882 						    off,
3883 						    relocation);
3884 
3885 		    *off_ptr |= 1;
3886 		  }
3887 	      }
3888 
3889 	    /* We don't use elf_m68k_reloc_got_type in the condition below
3890 	       because this is the only place where difference between
3891 	       R_68K_GOTx and R_68K_GOTxO relocations matters.  */
3892 	    if (r_type == R_68K_GOT32O
3893 		|| r_type == R_68K_GOT16O
3894 		|| r_type == R_68K_GOT8O
3895 		|| elf_m68k_reloc_got_type (r_type) == R_68K_TLS_GD32
3896 		|| elf_m68k_reloc_got_type (r_type) == R_68K_TLS_LDM32
3897 		|| elf_m68k_reloc_got_type (r_type) == R_68K_TLS_IE32)
3898 	      {
3899 		/* GOT pointer is adjusted to point to the start/middle
3900 		   of local GOT.  Adjust the offset accordingly.  */
3901 		BFD_ASSERT (elf_m68k_hash_table (info)->use_neg_got_offsets_p
3902 			    || off >= got->offset);
3903 
3904 		if (elf_m68k_hash_table (info)->local_gp_p)
3905 		  relocation = off - got->offset;
3906 		else
3907 		  {
3908 		    BFD_ASSERT (got->offset == 0);
3909 		    relocation = sgot->output_offset + off;
3910 		  }
3911 
3912 		/* This relocation does not use the addend.  */
3913 		rel->r_addend = 0;
3914 	      }
3915 	    else
3916 	      relocation = (sgot->output_section->vma + sgot->output_offset
3917 			    + off);
3918 	  }
3919 	  break;
3920 
3921 	case R_68K_TLS_LDO32:
3922 	case R_68K_TLS_LDO16:
3923 	case R_68K_TLS_LDO8:
3924 	  relocation -= dtpoff_base (info);
3925 	  break;
3926 
3927 	case R_68K_TLS_LE32:
3928 	case R_68K_TLS_LE16:
3929 	case R_68K_TLS_LE8:
3930 	  if (bfd_link_dll (info))
3931 	    {
3932 	      (*_bfd_error_handler)
3933 		(_("%B(%A+0x%lx): R_68K_TLS_LE32 relocation not permitted "
3934 		   "in shared object"),
3935 		 input_bfd, input_section, (long) rel->r_offset, howto->name);
3936 
3937 	      return FALSE;
3938 	    }
3939 	  else
3940 	    relocation -= tpoff_base (info);
3941 
3942 	  break;
3943 
3944 	case R_68K_PLT8:
3945 	case R_68K_PLT16:
3946 	case R_68K_PLT32:
3947 	  /* Relocation is to the entry for this symbol in the
3948 	     procedure linkage table.  */
3949 
3950 	  /* Resolve a PLTxx reloc against a local symbol directly,
3951 	     without using the procedure linkage table.  */
3952 	  if (h == NULL)
3953 	    break;
3954 
3955 	  if (h->plt.offset == (bfd_vma) -1
3956 	      || !elf_hash_table (info)->dynamic_sections_created)
3957 	    {
3958 	      /* We didn't make a PLT entry for this symbol.  This
3959 		 happens when statically linking PIC code, or when
3960 		 using -Bsymbolic.  */
3961 	      break;
3962 	    }
3963 
3964 	  if (splt == NULL)
3965 	    {
3966 	      splt = bfd_get_linker_section (dynobj, ".plt");
3967 	      BFD_ASSERT (splt != NULL);
3968 	    }
3969 
3970 	  relocation = (splt->output_section->vma
3971 			+ splt->output_offset
3972 			+ h->plt.offset);
3973 	  unresolved_reloc = FALSE;
3974 	  break;
3975 
3976 	case R_68K_PLT8O:
3977 	case R_68K_PLT16O:
3978 	case R_68K_PLT32O:
3979 	  /* Relocation is the offset of the entry for this symbol in
3980 	     the procedure linkage table.  */
3981 	  BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
3982 
3983 	  if (splt == NULL)
3984 	    {
3985 	      splt = bfd_get_linker_section (dynobj, ".plt");
3986 	      BFD_ASSERT (splt != NULL);
3987 	    }
3988 
3989 	  relocation = h->plt.offset;
3990 	  unresolved_reloc = FALSE;
3991 
3992 	  /* This relocation does not use the addend.  */
3993 	  rel->r_addend = 0;
3994 
3995 	  break;
3996 
3997 	case R_68K_8:
3998 	case R_68K_16:
3999 	case R_68K_32:
4000 	case R_68K_PC8:
4001 	case R_68K_PC16:
4002 	case R_68K_PC32:
4003 	  if (bfd_link_pic (info)
4004 	      && r_symndx != STN_UNDEF
4005 	      && (input_section->flags & SEC_ALLOC) != 0
4006 	      && (h == NULL
4007 		  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4008 		  || h->root.type != bfd_link_hash_undefweak)
4009 	      && ((r_type != R_68K_PC8
4010 		   && r_type != R_68K_PC16
4011 		   && r_type != R_68K_PC32)
4012 		  || !SYMBOL_CALLS_LOCAL (info, h)))
4013 	    {
4014 	      Elf_Internal_Rela outrel;
4015 	      bfd_byte *loc;
4016 	      bfd_boolean skip, relocate;
4017 
4018 	      /* When generating a shared object, these relocations
4019 		 are copied into the output file to be resolved at run
4020 		 time.  */
4021 
4022 	      skip = FALSE;
4023 	      relocate = FALSE;
4024 
4025 	      outrel.r_offset =
4026 		_bfd_elf_section_offset (output_bfd, info, input_section,
4027 					 rel->r_offset);
4028 	      if (outrel.r_offset == (bfd_vma) -1)
4029 		skip = TRUE;
4030 	      else if (outrel.r_offset == (bfd_vma) -2)
4031 		skip = TRUE, relocate = TRUE;
4032 	      outrel.r_offset += (input_section->output_section->vma
4033 				  + input_section->output_offset);
4034 
4035 	      if (skip)
4036 		memset (&outrel, 0, sizeof outrel);
4037 	      else if (h != NULL
4038 		       && h->dynindx != -1
4039 		       && (r_type == R_68K_PC8
4040 			   || r_type == R_68K_PC16
4041 			   || r_type == R_68K_PC32
4042 			   || !bfd_link_pic (info)
4043 			   || !SYMBOLIC_BIND (info, h)
4044 			   || !h->def_regular))
4045 		{
4046 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4047 		  outrel.r_addend = rel->r_addend;
4048 		}
4049 	      else
4050 		{
4051 		  /* This symbol is local, or marked to become local.  */
4052 		  outrel.r_addend = relocation + rel->r_addend;
4053 
4054 		  if (r_type == R_68K_32)
4055 		    {
4056 		      relocate = TRUE;
4057 		      outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
4058 		    }
4059 		  else
4060 		    {
4061 		      long indx;
4062 
4063 		      if (bfd_is_abs_section (sec))
4064 			indx = 0;
4065 		      else if (sec == NULL || sec->owner == NULL)
4066 			{
4067 			  bfd_set_error (bfd_error_bad_value);
4068 			  return FALSE;
4069 			}
4070 		      else
4071 			{
4072 			  asection *osec;
4073 
4074 			  /* We are turning this relocation into one
4075 			     against a section symbol.  It would be
4076 			     proper to subtract the symbol's value,
4077 			     osec->vma, from the emitted reloc addend,
4078 			     but ld.so expects buggy relocs.  */
4079 			  osec = sec->output_section;
4080 			  indx = elf_section_data (osec)->dynindx;
4081 			  if (indx == 0)
4082 			    {
4083 			      struct elf_link_hash_table *htab;
4084 			      htab = elf_hash_table (info);
4085 			      osec = htab->text_index_section;
4086 			      indx = elf_section_data (osec)->dynindx;
4087 			    }
4088 			  BFD_ASSERT (indx != 0);
4089 			}
4090 
4091 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
4092 		    }
4093 		}
4094 
4095 	      sreloc = elf_section_data (input_section)->sreloc;
4096 	      if (sreloc == NULL)
4097 		abort ();
4098 
4099 	      loc = sreloc->contents;
4100 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4101 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4102 
4103 	      /* This reloc will be computed at runtime, so there's no
4104                  need to do anything now, except for R_68K_32
4105                  relocations that have been turned into
4106                  R_68K_RELATIVE.  */
4107 	      if (!relocate)
4108 		continue;
4109 	    }
4110 
4111 	  break;
4112 
4113 	case R_68K_GNU_VTINHERIT:
4114 	case R_68K_GNU_VTENTRY:
4115 	  /* These are no-ops in the end.  */
4116 	  continue;
4117 
4118 	default:
4119 	  break;
4120 	}
4121 
4122       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4123 	 because such sections are not SEC_ALLOC and thus ld.so will
4124 	 not process them.  */
4125       if (unresolved_reloc
4126 	  && !((input_section->flags & SEC_DEBUGGING) != 0
4127 	       && h->def_dynamic)
4128 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
4129 				      rel->r_offset) != (bfd_vma) -1)
4130 	{
4131 	  (*_bfd_error_handler)
4132 	    (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4133 	     input_bfd,
4134 	     input_section,
4135 	     (long) rel->r_offset,
4136 	     howto->name,
4137 	     h->root.root.string);
4138 	  return FALSE;
4139 	}
4140 
4141       if (r_symndx != STN_UNDEF
4142 	  && r_type != R_68K_NONE
4143 	  && (h == NULL
4144 	      || h->root.type == bfd_link_hash_defined
4145 	      || h->root.type == bfd_link_hash_defweak))
4146 	{
4147 	  char sym_type;
4148 
4149 	  sym_type = (sym != NULL) ? ELF32_ST_TYPE (sym->st_info) : h->type;
4150 
4151 	  if (elf_m68k_reloc_tls_p (r_type) != (sym_type == STT_TLS))
4152 	    {
4153 	      const char *name;
4154 
4155 	      if (h != NULL)
4156 		name = h->root.root.string;
4157 	      else
4158 		{
4159 		  name = (bfd_elf_string_from_elf_section
4160 			  (input_bfd, symtab_hdr->sh_link, sym->st_name));
4161 		  if (name == NULL || *name == '\0')
4162 		    name = bfd_section_name (input_bfd, sec);
4163 		}
4164 
4165 	      (*_bfd_error_handler)
4166 		((sym_type == STT_TLS
4167 		  ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
4168 		  : _("%B(%A+0x%lx): %s used with non-TLS symbol %s")),
4169 		 input_bfd,
4170 		 input_section,
4171 		 (long) rel->r_offset,
4172 		 howto->name,
4173 		 name);
4174 	    }
4175 	}
4176 
4177       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4178 				    contents, rel->r_offset,
4179 				    relocation, rel->r_addend);
4180 
4181       if (r != bfd_reloc_ok)
4182 	{
4183 	  const char *name;
4184 
4185 	  if (h != NULL)
4186 	    name = h->root.root.string;
4187 	  else
4188 	    {
4189 	      name = bfd_elf_string_from_elf_section (input_bfd,
4190 						      symtab_hdr->sh_link,
4191 						      sym->st_name);
4192 	      if (name == NULL)
4193 		return FALSE;
4194 	      if (*name == '\0')
4195 		name = bfd_section_name (input_bfd, sec);
4196 	    }
4197 
4198 	  if (r == bfd_reloc_overflow)
4199 	    (*info->callbacks->reloc_overflow)
4200 	      (info, (h ? &h->root : NULL), name, howto->name,
4201 	       (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4202 	  else
4203 	    {
4204 	      (*_bfd_error_handler)
4205 		(_("%B(%A+0x%lx): reloc against `%s': error %d"),
4206 		 input_bfd, input_section,
4207 		 (long) rel->r_offset, name, (int) r);
4208 	      return FALSE;
4209 	    }
4210 	}
4211     }
4212 
4213   return TRUE;
4214 }
4215 
4216 /* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
4217    into section SEC.  */
4218 
4219 static void
4220 elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
4221 {
4222   /* Make VALUE PC-relative.  */
4223   value -= sec->output_section->vma + offset;
4224 
4225   /* Apply any in-place addend.  */
4226   value += bfd_get_32 (sec->owner, sec->contents + offset);
4227 
4228   bfd_put_32 (sec->owner, value, sec->contents + offset);
4229 }
4230 
4231 /* Finish up dynamic symbol handling.  We set the contents of various
4232    dynamic sections here.  */
4233 
4234 static bfd_boolean
4235 elf_m68k_finish_dynamic_symbol (bfd *output_bfd,
4236 				struct bfd_link_info *info,
4237 				struct elf_link_hash_entry *h,
4238 				Elf_Internal_Sym *sym)
4239 {
4240   bfd *dynobj;
4241 
4242   dynobj = elf_hash_table (info)->dynobj;
4243 
4244   if (h->plt.offset != (bfd_vma) -1)
4245     {
4246       const struct elf_m68k_plt_info *plt_info;
4247       asection *splt;
4248       asection *sgot;
4249       asection *srela;
4250       bfd_vma plt_index;
4251       bfd_vma got_offset;
4252       Elf_Internal_Rela rela;
4253       bfd_byte *loc;
4254 
4255       /* This symbol has an entry in the procedure linkage table.  Set
4256 	 it up.  */
4257 
4258       BFD_ASSERT (h->dynindx != -1);
4259 
4260       plt_info = elf_m68k_hash_table (info)->plt_info;
4261       splt = bfd_get_linker_section (dynobj, ".plt");
4262       sgot = bfd_get_linker_section (dynobj, ".got.plt");
4263       srela = bfd_get_linker_section (dynobj, ".rela.plt");
4264       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
4265 
4266       /* Get the index in the procedure linkage table which
4267 	 corresponds to this symbol.  This is the index of this symbol
4268 	 in all the symbols for which we are making plt entries.  The
4269 	 first entry in the procedure linkage table is reserved.  */
4270       plt_index = (h->plt.offset / plt_info->size) - 1;
4271 
4272       /* Get the offset into the .got table of the entry that
4273 	 corresponds to this function.  Each .got entry is 4 bytes.
4274 	 The first three are reserved.  */
4275       got_offset = (plt_index + 3) * 4;
4276 
4277       memcpy (splt->contents + h->plt.offset,
4278 	      plt_info->symbol_entry,
4279 	      plt_info->size);
4280 
4281       elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
4282 			     (sgot->output_section->vma
4283 			      + sgot->output_offset
4284 			      + got_offset));
4285 
4286       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4287 		  splt->contents
4288 		  + h->plt.offset
4289 		  + plt_info->symbol_resolve_entry + 2);
4290 
4291       elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
4292 			     splt->output_section->vma);
4293 
4294       /* Fill in the entry in the global offset table.  */
4295       bfd_put_32 (output_bfd,
4296 		  (splt->output_section->vma
4297 		   + splt->output_offset
4298 		   + h->plt.offset
4299 		   + plt_info->symbol_resolve_entry),
4300 		  sgot->contents + got_offset);
4301 
4302       /* Fill in the entry in the .rela.plt section.  */
4303       rela.r_offset = (sgot->output_section->vma
4304 		       + sgot->output_offset
4305 		       + got_offset);
4306       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
4307       rela.r_addend = 0;
4308       loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
4309       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4310 
4311       if (!h->def_regular)
4312 	{
4313 	  /* Mark the symbol as undefined, rather than as defined in
4314 	     the .plt section.  Leave the value alone.  */
4315 	  sym->st_shndx = SHN_UNDEF;
4316 	}
4317     }
4318 
4319   if (elf_m68k_hash_entry (h)->glist != NULL)
4320     {
4321       asection *sgot;
4322       asection *srela;
4323       struct elf_m68k_got_entry *got_entry;
4324 
4325       /* This symbol has an entry in the global offset table.  Set it
4326 	 up.  */
4327 
4328       sgot = bfd_get_linker_section (dynobj, ".got");
4329       srela = bfd_get_linker_section (dynobj, ".rela.got");
4330       BFD_ASSERT (sgot != NULL && srela != NULL);
4331 
4332       got_entry = elf_m68k_hash_entry (h)->glist;
4333 
4334       while (got_entry != NULL)
4335 	{
4336 	  enum elf_m68k_reloc_type r_type;
4337 	  bfd_vma got_entry_offset;
4338 
4339 	  r_type = got_entry->key_.type;
4340 	  got_entry_offset = got_entry->u.s2.offset &~ (bfd_vma) 1;
4341 
4342 	  /* If this is a -Bsymbolic link, and the symbol is defined
4343 	     locally, we just want to emit a RELATIVE reloc.  Likewise if
4344 	     the symbol was forced to be local because of a version file.
4345 	     The entry in the global offset table already have been
4346 	     initialized in the relocate_section function.  */
4347 	  if (bfd_link_pic (info)
4348 	      && SYMBOL_REFERENCES_LOCAL (info, h))
4349 	    {
4350 	      bfd_vma relocation;
4351 
4352 	      relocation = bfd_get_signed_32 (output_bfd,
4353 					      (sgot->contents
4354 					       + got_entry_offset));
4355 
4356 	      /* Undo TP bias.  */
4357 	      switch (elf_m68k_reloc_got_type (r_type))
4358 		{
4359 		case R_68K_GOT32O:
4360 		case R_68K_TLS_LDM32:
4361 		  break;
4362 
4363 		case R_68K_TLS_GD32:
4364 		  /* The value for this relocation is actually put in
4365 		     the second GOT slot.  */
4366 		  relocation = bfd_get_signed_32 (output_bfd,
4367 						  (sgot->contents
4368 						   + got_entry_offset + 4));
4369 		  relocation += dtpoff_base (info);
4370 		  break;
4371 
4372 		case R_68K_TLS_IE32:
4373 		  relocation += tpoff_base (info);
4374 		  break;
4375 
4376 		default:
4377 		  BFD_ASSERT (FALSE);
4378 		}
4379 
4380 	      elf_m68k_init_got_entry_local_shared (info,
4381 						    output_bfd,
4382 						    r_type,
4383 						    sgot,
4384 						    got_entry_offset,
4385 						    relocation,
4386 						    srela);
4387 	    }
4388 	  else
4389 	    {
4390 	      Elf_Internal_Rela rela;
4391 
4392 	      /* Put zeros to GOT slots that will be initialized
4393 		 at run-time.  */
4394 	      {
4395 		bfd_vma n_slots;
4396 
4397 		n_slots = elf_m68k_reloc_got_n_slots (got_entry->key_.type);
4398 		while (n_slots--)
4399 		  bfd_put_32 (output_bfd, (bfd_vma) 0,
4400 			      (sgot->contents + got_entry_offset
4401 			       + 4 * n_slots));
4402 	      }
4403 
4404 	      rela.r_addend = 0;
4405 	      rela.r_offset = (sgot->output_section->vma
4406 			       + sgot->output_offset
4407 			       + got_entry_offset);
4408 
4409 	      switch (elf_m68k_reloc_got_type (r_type))
4410 		{
4411 		case R_68K_GOT32O:
4412 		  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
4413 		  elf_m68k_install_rela (output_bfd, srela, &rela);
4414 		  break;
4415 
4416 		case R_68K_TLS_GD32:
4417 		  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_DTPMOD32);
4418 		  elf_m68k_install_rela (output_bfd, srela, &rela);
4419 
4420 		  rela.r_offset += 4;
4421 		  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_DTPREL32);
4422 		  elf_m68k_install_rela (output_bfd, srela, &rela);
4423 		  break;
4424 
4425 		case R_68K_TLS_IE32:
4426 		  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_TLS_TPREL32);
4427 		  elf_m68k_install_rela (output_bfd, srela, &rela);
4428 		  break;
4429 
4430 		default:
4431 		  BFD_ASSERT (FALSE);
4432 		  break;
4433 		}
4434 	    }
4435 
4436 	  got_entry = got_entry->u.s2.next;
4437 	}
4438     }
4439 
4440   if (h->needs_copy)
4441     {
4442       asection *s;
4443       Elf_Internal_Rela rela;
4444       bfd_byte *loc;
4445 
4446       /* This symbol needs a copy reloc.  Set it up.  */
4447 
4448       BFD_ASSERT (h->dynindx != -1
4449 		  && (h->root.type == bfd_link_hash_defined
4450 		      || h->root.type == bfd_link_hash_defweak));
4451 
4452       s = bfd_get_linker_section (dynobj, ".rela.bss");
4453       BFD_ASSERT (s != NULL);
4454 
4455       rela.r_offset = (h->root.u.def.value
4456 		       + h->root.u.def.section->output_section->vma
4457 		       + h->root.u.def.section->output_offset);
4458       rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
4459       rela.r_addend = 0;
4460       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4461       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4462     }
4463 
4464   return TRUE;
4465 }
4466 
4467 /* Finish up the dynamic sections.  */
4468 
4469 static bfd_boolean
4470 elf_m68k_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4471 {
4472   bfd *dynobj;
4473   asection *sgot;
4474   asection *sdyn;
4475 
4476   dynobj = elf_hash_table (info)->dynobj;
4477 
4478   sgot = bfd_get_linker_section (dynobj, ".got.plt");
4479   BFD_ASSERT (sgot != NULL);
4480   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4481 
4482   if (elf_hash_table (info)->dynamic_sections_created)
4483     {
4484       asection *splt;
4485       Elf32_External_Dyn *dyncon, *dynconend;
4486 
4487       splt = bfd_get_linker_section (dynobj, ".plt");
4488       BFD_ASSERT (splt != NULL && sdyn != NULL);
4489 
4490       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4491       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4492       for (; dyncon < dynconend; dyncon++)
4493 	{
4494 	  Elf_Internal_Dyn dyn;
4495 	  const char *name;
4496 	  asection *s;
4497 
4498 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4499 
4500 	  switch (dyn.d_tag)
4501 	    {
4502 	    default:
4503 	      break;
4504 
4505 	    case DT_PLTGOT:
4506 	      name = ".got.plt";
4507 	      goto get_vma;
4508 	    case DT_JMPREL:
4509 	      name = ".rela.plt";
4510 	    get_vma:
4511 	      s = bfd_get_linker_section (dynobj, name);
4512 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4513 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4514 	      break;
4515 
4516 	    case DT_PLTRELSZ:
4517 	      s = bfd_get_linker_section (dynobj, ".rela.plt");
4518 	      dyn.d_un.d_val = s->size;
4519 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4520 	      break;
4521 
4522 	    case DT_RELASZ:
4523 	      /* The procedure linkage table relocs (DT_JMPREL) should
4524 		 not be included in the overall relocs (DT_RELA).
4525 		 Therefore, we override the DT_RELASZ entry here to
4526 		 make it not include the JMPREL relocs.  Since the
4527 		 linker script arranges for .rela.plt to follow all
4528 		 other relocation sections, we don't have to worry
4529 		 about changing the DT_RELA entry.  */
4530 	      s = bfd_get_linker_section (dynobj, ".rela.plt");
4531 	      if (s != NULL)
4532 		dyn.d_un.d_val -= s->size;
4533 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4534 	      break;
4535 	    }
4536 	}
4537 
4538       /* Fill in the first entry in the procedure linkage table.  */
4539       if (splt->size > 0)
4540 	{
4541 	  const struct elf_m68k_plt_info *plt_info;
4542 
4543 	  plt_info = elf_m68k_hash_table (info)->plt_info;
4544 	  memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
4545 
4546 	  elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
4547 				 (sgot->output_section->vma
4548 				  + sgot->output_offset
4549 				  + 4));
4550 
4551 	  elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
4552 				 (sgot->output_section->vma
4553 				  + sgot->output_offset
4554 				  + 8));
4555 
4556 	  elf_section_data (splt->output_section)->this_hdr.sh_entsize
4557 	    = plt_info->size;
4558 	}
4559     }
4560 
4561   /* Fill in the first three entries in the global offset table.  */
4562   if (sgot->size > 0)
4563     {
4564       if (sdyn == NULL)
4565 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4566       else
4567 	bfd_put_32 (output_bfd,
4568 		    sdyn->output_section->vma + sdyn->output_offset,
4569 		    sgot->contents);
4570       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4571       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4572     }
4573 
4574   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4575 
4576   return TRUE;
4577 }
4578 
4579 /* Given a .data section and a .emreloc in-memory section, store
4580    relocation information into the .emreloc section which can be
4581    used at runtime to relocate the section.  This is called by the
4582    linker when the --embedded-relocs switch is used.  This is called
4583    after the add_symbols entry point has been called for all the
4584    objects, and before the final_link entry point is called.  */
4585 
4586 bfd_boolean
4587 bfd_m68k_elf32_create_embedded_relocs (bfd *abfd, struct bfd_link_info *info,
4588 				       asection *datasec, asection *relsec,
4589 				       char **errmsg)
4590 {
4591   Elf_Internal_Shdr *symtab_hdr;
4592   Elf_Internal_Sym *isymbuf = NULL;
4593   Elf_Internal_Rela *internal_relocs = NULL;
4594   Elf_Internal_Rela *irel, *irelend;
4595   bfd_byte *p;
4596   bfd_size_type amt;
4597 
4598   BFD_ASSERT (! bfd_link_relocatable (info));
4599 
4600   *errmsg = NULL;
4601 
4602   if (datasec->reloc_count == 0)
4603     return TRUE;
4604 
4605   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4606 
4607   /* Get a copy of the native relocations.  */
4608   internal_relocs = (_bfd_elf_link_read_relocs
4609 		     (abfd, datasec, NULL, (Elf_Internal_Rela *) NULL,
4610 		      info->keep_memory));
4611   if (internal_relocs == NULL)
4612     goto error_return;
4613 
4614   amt = (bfd_size_type) datasec->reloc_count * 12;
4615   relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
4616   if (relsec->contents == NULL)
4617     goto error_return;
4618 
4619   p = relsec->contents;
4620 
4621   irelend = internal_relocs + datasec->reloc_count;
4622   for (irel = internal_relocs; irel < irelend; irel++, p += 12)
4623     {
4624       asection *targetsec;
4625 
4626       /* We are going to write a four byte longword into the runtime
4627        reloc section.  The longword will be the address in the data
4628        section which must be relocated.  It is followed by the name
4629        of the target section NUL-padded or truncated to 8
4630        characters.  */
4631 
4632       /* We can only relocate absolute longword relocs at run time.  */
4633       if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
4634 	{
4635 	  *errmsg = _("unsupported reloc type");
4636 	  bfd_set_error (bfd_error_bad_value);
4637 	  goto error_return;
4638 	}
4639 
4640       /* Get the target section referred to by the reloc.  */
4641       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4642 	{
4643 	  /* A local symbol.  */
4644 	  Elf_Internal_Sym *isym;
4645 
4646 	  /* Read this BFD's local symbols if we haven't done so already.  */
4647 	  if (isymbuf == NULL)
4648 	    {
4649 	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4650 	      if (isymbuf == NULL)
4651 		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4652 						symtab_hdr->sh_info, 0,
4653 						NULL, NULL, NULL);
4654 	      if (isymbuf == NULL)
4655 		goto error_return;
4656 	    }
4657 
4658 	  isym = isymbuf + ELF32_R_SYM (irel->r_info);
4659 	  targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4660 	}
4661       else
4662 	{
4663 	  unsigned long indx;
4664 	  struct elf_link_hash_entry *h;
4665 
4666 	  /* An external symbol.  */
4667 	  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4668 	  h = elf_sym_hashes (abfd)[indx];
4669 	  BFD_ASSERT (h != NULL);
4670 	  if (h->root.type == bfd_link_hash_defined
4671 	      || h->root.type == bfd_link_hash_defweak)
4672 	    targetsec = h->root.u.def.section;
4673 	  else
4674 	    targetsec = NULL;
4675 	}
4676 
4677       bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
4678       memset (p + 4, 0, 8);
4679       if (targetsec != NULL)
4680 	strncpy ((char *) p + 4, targetsec->output_section->name, 8);
4681     }
4682 
4683   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4684     free (isymbuf);
4685   if (internal_relocs != NULL
4686       && elf_section_data (datasec)->relocs != internal_relocs)
4687     free (internal_relocs);
4688   return TRUE;
4689 
4690 error_return:
4691   if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4692     free (isymbuf);
4693   if (internal_relocs != NULL
4694       && elf_section_data (datasec)->relocs != internal_relocs)
4695     free (internal_relocs);
4696   return FALSE;
4697 }
4698 
4699 /* Set target options.  */
4700 
4701 void
4702 bfd_elf_m68k_set_target_options (struct bfd_link_info *info, int got_handling)
4703 {
4704   struct elf_m68k_link_hash_table *htab;
4705   bfd_boolean use_neg_got_offsets_p;
4706   bfd_boolean allow_multigot_p;
4707   bfd_boolean local_gp_p;
4708 
4709   switch (got_handling)
4710     {
4711     case 0:
4712       /* --got=single.  */
4713       local_gp_p = FALSE;
4714       use_neg_got_offsets_p = FALSE;
4715       allow_multigot_p = FALSE;
4716       break;
4717 
4718     case 1:
4719       /* --got=negative.  */
4720       local_gp_p = TRUE;
4721       use_neg_got_offsets_p = TRUE;
4722       allow_multigot_p = FALSE;
4723       break;
4724 
4725     case 2:
4726       /* --got=multigot.  */
4727       local_gp_p = TRUE;
4728       use_neg_got_offsets_p = TRUE;
4729       allow_multigot_p = TRUE;
4730       break;
4731 
4732     default:
4733       BFD_ASSERT (FALSE);
4734       return;
4735     }
4736 
4737   htab = elf_m68k_hash_table (info);
4738   if (htab != NULL)
4739     {
4740       htab->local_gp_p = local_gp_p;
4741       htab->use_neg_got_offsets_p = use_neg_got_offsets_p;
4742       htab->allow_multigot_p = allow_multigot_p;
4743     }
4744 }
4745 
4746 static enum elf_reloc_type_class
4747 elf32_m68k_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4748 			     const asection *rel_sec ATTRIBUTE_UNUSED,
4749 			     const Elf_Internal_Rela *rela)
4750 {
4751   switch ((int) ELF32_R_TYPE (rela->r_info))
4752     {
4753     case R_68K_RELATIVE:
4754       return reloc_class_relative;
4755     case R_68K_JMP_SLOT:
4756       return reloc_class_plt;
4757     case R_68K_COPY:
4758       return reloc_class_copy;
4759     default:
4760       return reloc_class_normal;
4761     }
4762 }
4763 
4764 /* Return address for Ith PLT stub in section PLT, for relocation REL
4765    or (bfd_vma) -1 if it should not be included.  */
4766 
4767 static bfd_vma
4768 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
4769 		      const arelent *rel ATTRIBUTE_UNUSED)
4770 {
4771   return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
4772 }
4773 
4774 /* Support for core dump NOTE sections.  */
4775 
4776 static bfd_boolean
4777 elf_m68k_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
4778 {
4779   int offset;
4780   size_t size;
4781 
4782   switch (note->descsz)
4783     {
4784     default:
4785       return FALSE;
4786 
4787     case 154:		/* Linux/m68k */
4788       /* pr_cursig */
4789       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
4790 
4791       /* pr_pid */
4792       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
4793 
4794       /* pr_reg */
4795       offset = 70;
4796       size = 80;
4797 
4798       break;
4799     }
4800 
4801   /* Make a ".reg/999" section.  */
4802   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
4803 					  size, note->descpos + offset);
4804 }
4805 
4806 static bfd_boolean
4807 elf_m68k_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
4808 {
4809   switch (note->descsz)
4810     {
4811     default:
4812       return FALSE;
4813 
4814     case 124:		/* Linux/m68k elf_prpsinfo.  */
4815       elf_tdata (abfd)->core->pid
4816 	= bfd_get_32 (abfd, note->descdata + 12);
4817       elf_tdata (abfd)->core->program
4818 	= _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
4819       elf_tdata (abfd)->core->command
4820 	= _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
4821     }
4822 
4823   /* Note that for some reason, a spurious space is tacked
4824      onto the end of the args in some (at least one anyway)
4825      implementations, so strip it off if it exists.  */
4826   {
4827     char *command = elf_tdata (abfd)->core->command;
4828     int n = strlen (command);
4829 
4830     if (n > 0 && command[n - 1] == ' ')
4831       command[n - 1] = '\0';
4832   }
4833 
4834   return TRUE;
4835 }
4836 
4837 /* Hook called by the linker routine which adds symbols from an object
4838    file.  */
4839 
4840 static bfd_boolean
4841 elf_m68k_add_symbol_hook (bfd *abfd,
4842 			  struct bfd_link_info *info,
4843 			  Elf_Internal_Sym *sym,
4844 			  const char **namep ATTRIBUTE_UNUSED,
4845 			  flagword *flagsp ATTRIBUTE_UNUSED,
4846 			  asection **secp ATTRIBUTE_UNUSED,
4847 			  bfd_vma *valp ATTRIBUTE_UNUSED)
4848 {
4849   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
4850       && (abfd->flags & DYNAMIC) == 0
4851       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4852     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4853 
4854   return TRUE;
4855 }
4856 
4857 #define TARGET_BIG_SYM			m68k_elf32_vec
4858 #define TARGET_BIG_NAME			"elf32-m68k"
4859 #define ELF_MACHINE_CODE		EM_68K
4860 #define ELF_MAXPAGESIZE			0x2000
4861 #define elf_backend_create_dynamic_sections \
4862 					_bfd_elf_create_dynamic_sections
4863 #define bfd_elf32_bfd_link_hash_table_create \
4864 					elf_m68k_link_hash_table_create
4865 #define bfd_elf32_bfd_final_link	bfd_elf_final_link
4866 
4867 #define elf_backend_check_relocs	elf_m68k_check_relocs
4868 #define elf_backend_always_size_sections \
4869 					elf_m68k_always_size_sections
4870 #define elf_backend_adjust_dynamic_symbol \
4871 					elf_m68k_adjust_dynamic_symbol
4872 #define elf_backend_size_dynamic_sections \
4873 					elf_m68k_size_dynamic_sections
4874 #define elf_backend_final_write_processing	elf_m68k_final_write_processing
4875 #define elf_backend_init_index_section	_bfd_elf_init_1_index_section
4876 #define elf_backend_relocate_section	elf_m68k_relocate_section
4877 #define elf_backend_finish_dynamic_symbol \
4878 					elf_m68k_finish_dynamic_symbol
4879 #define elf_backend_finish_dynamic_sections \
4880 					elf_m68k_finish_dynamic_sections
4881 #define elf_backend_gc_mark_hook	elf_m68k_gc_mark_hook
4882 #define elf_backend_gc_sweep_hook	elf_m68k_gc_sweep_hook
4883 #define elf_backend_copy_indirect_symbol elf_m68k_copy_indirect_symbol
4884 #define bfd_elf32_bfd_merge_private_bfd_data \
4885                                         elf32_m68k_merge_private_bfd_data
4886 #define bfd_elf32_bfd_set_private_flags \
4887                                         elf32_m68k_set_private_flags
4888 #define bfd_elf32_bfd_print_private_bfd_data \
4889                                         elf32_m68k_print_private_bfd_data
4890 #define elf_backend_reloc_type_class	elf32_m68k_reloc_type_class
4891 #define elf_backend_plt_sym_val		elf_m68k_plt_sym_val
4892 #define elf_backend_object_p		elf32_m68k_object_p
4893 #define elf_backend_grok_prstatus	elf_m68k_grok_prstatus
4894 #define elf_backend_grok_psinfo		elf_m68k_grok_psinfo
4895 #define elf_backend_add_symbol_hook	elf_m68k_add_symbol_hook
4896 
4897 #define elf_backend_can_gc_sections 1
4898 #define elf_backend_can_refcount 1
4899 #define elf_backend_want_got_plt 1
4900 #define elf_backend_plt_readonly 1
4901 #define elf_backend_want_plt_sym 0
4902 #define elf_backend_got_header_size	12
4903 #define elf_backend_rela_normal		1
4904 
4905 #include "elf32-target.h"
4906