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