xref: /netbsd-src/external/gpl3/binutils/dist/bfd/elf32-cris.c (revision 48fb7bfab72acd4281a53bbee5ccf3f809019e75)
1 /* CRIS-specific support for 32-bit ELF.
2    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3    2010, 2011, 2012 Free Software Foundation, Inc.
4    Contributed by Axis Communications AB.
5    Written by Hans-Peter Nilsson, based on elf32-fr30.c
6    PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
7 
8    This file is part of BFD, the Binary File Descriptor library.
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23    MA 02110-1301, USA.  */
24 
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf/cris.h"
30 #include <limits.h>
31 
32 bfd_reloc_status_type
33 cris_elf_pcrel_reloc (bfd *, arelent *, asymbol *, void *,
34 		      asection *, bfd *, char **);
35 static bfd_boolean
36 cris_elf_set_mach_from_flags (bfd *, unsigned long);
37 
38 /* Forward declarations.  */
39 static reloc_howto_type cris_elf_howto_table [] =
40 {
41   /* This reloc does nothing.  */
42   HOWTO (R_CRIS_NONE,		/* type */
43 	 0,			/* rightshift */
44 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
45 	 32,			/* bitsize */
46 	 FALSE,			/* pc_relative */
47 	 0,			/* bitpos */
48 	 complain_overflow_bitfield, /* complain_on_overflow */
49 	 bfd_elf_generic_reloc,	/* special_function */
50 	 "R_CRIS_NONE",		/* name */
51 	 FALSE,			/* partial_inplace */
52 	 0,			/* src_mask */
53 	 0,			/* dst_mask */
54 	 FALSE),		/* pcrel_offset */
55 
56   /* An 8 bit absolute relocation.  */
57   HOWTO (R_CRIS_8,		/* type */
58 	 0,			/* rightshift */
59 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
60 	 8,			/* bitsize */
61 	 FALSE,			/* pc_relative */
62 	 0,			/* bitpos */
63 	 complain_overflow_bitfield, /* complain_on_overflow */
64 	 bfd_elf_generic_reloc,	/* special_function */
65 	 "R_CRIS_8",		/* name */
66 	 FALSE,			/* partial_inplace */
67 	 0x0000,		/* src_mask */
68 	 0x00ff,		/* dst_mask */
69 	 FALSE),		/* pcrel_offset */
70 
71   /* A 16 bit absolute relocation.  */
72   HOWTO (R_CRIS_16,		/* type */
73 	 0,			/* rightshift */
74 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
75 	 16,			/* bitsize */
76 	 FALSE,			/* pc_relative */
77 	 0,			/* bitpos */
78 	 complain_overflow_bitfield, /* complain_on_overflow */
79 	 bfd_elf_generic_reloc,	/* special_function */
80 	 "R_CRIS_16",		/* name */
81 	 FALSE,			/* partial_inplace */
82 	 0x00000000,		/* src_mask */
83 	 0x0000ffff,		/* dst_mask */
84 	 FALSE),		/* pcrel_offset */
85 
86   /* A 32 bit absolute relocation.  */
87   HOWTO (R_CRIS_32,		/* type */
88 	 0,			/* rightshift */
89 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
90 	 32,			/* bitsize */
91 	 FALSE,			/* pc_relative */
92 	 0,			/* bitpos */
93 	 /* We don't want overflow complaints for 64-bit vma builds
94 	    for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
95 	    32-bit ELF) where sym=0xc0001234.
96 	    Don't do this for the PIC relocs, as we don't expect to
97 	    see them with large offsets.  */
98 	 complain_overflow_dont, /* complain_on_overflow */
99 	 bfd_elf_generic_reloc,	/* special_function */
100 	 "R_CRIS_32",		/* name */
101 	 FALSE,			/* partial_inplace */
102 	 0x00000000,		/* src_mask */
103 	 0xffffffff,		/* dst_mask */
104 	 FALSE),		/* pcrel_offset */
105 
106   /* An 8 bit PC-relative relocation.  */
107   HOWTO (R_CRIS_8_PCREL,	/* type */
108 	 0,			/* rightshift */
109 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
110 	 8,			/* bitsize */
111 	 TRUE,			/* pc_relative */
112 	 0,			/* bitpos */
113 	 complain_overflow_bitfield, /* complain_on_overflow */
114 	 cris_elf_pcrel_reloc,	/* special_function */
115 	 "R_CRIS_8_PCREL",	/* name */
116 	 FALSE,			/* partial_inplace */
117 	 0x0000,		/* src_mask */
118 	 0x00ff,		/* dst_mask */
119 	 TRUE),			/* pcrel_offset */
120 
121   /* A 16 bit PC-relative relocation.  */
122   HOWTO (R_CRIS_16_PCREL,	/* type */
123 	 0,			/* rightshift */
124 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
125 	 16,			/* bitsize */
126 	 TRUE,			/* pc_relative */
127 	 0,			/* bitpos */
128 	 complain_overflow_bitfield, /* complain_on_overflow */
129 	 cris_elf_pcrel_reloc,	/* special_function */
130 	 "R_CRIS_16_PCREL",	/* name */
131 	 FALSE,			/* partial_inplace */
132 	 0x00000000,		/* src_mask */
133 	 0x0000ffff,		/* dst_mask */
134 	 TRUE),			/* pcrel_offset */
135 
136   /* A 32 bit PC-relative relocation.  */
137   HOWTO (R_CRIS_32_PCREL,	/* type */
138 	 0,			/* rightshift */
139 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
140 	 32,			/* bitsize */
141 	 TRUE,			/* pc_relative */
142 	 0,			/* bitpos */
143 	 complain_overflow_bitfield, /* complain_on_overflow */
144 	 cris_elf_pcrel_reloc,	/* special_function */
145 	 "R_CRIS_32_PCREL",	/* name */
146 	 FALSE,			/* partial_inplace */
147 	 0x00000000,		/* src_mask */
148 	 0xffffffff,		/* dst_mask */
149 	 TRUE),			/* pcrel_offset */
150 
151   /* GNU extension to record C++ vtable hierarchy.  */
152   HOWTO (R_CRIS_GNU_VTINHERIT,	/* type */
153 	 0,			/* rightshift */
154 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
155 	 0,			/* bitsize */
156 	 FALSE,			/* pc_relative */
157 	 0,			/* bitpos */
158 	 complain_overflow_dont, /* complain_on_overflow */
159 	 NULL,			/* special_function */
160 	 "R_CRIS_GNU_VTINHERIT", /* name */
161 	 FALSE,			/* partial_inplace */
162 	 0,			/* src_mask */
163 	 0,			/* dst_mask */
164 	 FALSE),		/* pcrel_offset */
165 
166   /* GNU extension to record C++ vtable member usage.  */
167   HOWTO (R_CRIS_GNU_VTENTRY,	/* type */
168 	 0,			/* rightshift */
169 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
170 	 0,			/* bitsize */
171 	 FALSE,			/* pc_relative */
172 	 0,			/* bitpos */
173 	 complain_overflow_dont, /* complain_on_overflow */
174 	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
175 	 "R_CRIS_GNU_VTENTRY",	 /* name */
176 	 FALSE,			/* partial_inplace */
177 	 0,			/* src_mask */
178 	 0,			/* dst_mask */
179 	 FALSE),		/* pcrel_offset */
180 
181   /* This is used only by the dynamic linker.  The symbol should exist
182      both in the object being run and in some shared library.  The
183      dynamic linker copies the data addressed by the symbol from the
184      shared library into the object, because the object being
185      run has to have the data at some particular address.  */
186   HOWTO (R_CRIS_COPY,		/* type */
187 	 0,			/* rightshift */
188 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
189 	 32,			/* bitsize */
190 	 FALSE,			/* pc_relative */
191 	 0,			/* bitpos */
192 	 complain_overflow_bitfield, /* complain_on_overflow */
193 	 bfd_elf_generic_reloc,	/* special_function */
194 	 "R_CRIS_COPY",		/* name */
195 	 FALSE,			/* partial_inplace */
196 	 0,			/* src_mask */
197 	 0,			/* dst_mask */
198 	 FALSE),		/* pcrel_offset */
199 
200   /* Like R_CRIS_32, but used when setting global offset table entries.  */
201   HOWTO (R_CRIS_GLOB_DAT,	/* type */
202 	 0,			/* rightshift */
203 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
204 	 32,			/* bitsize */
205 	 FALSE,			/* pc_relative */
206 	 0,			/* bitpos */
207 	 complain_overflow_bitfield, /* complain_on_overflow */
208 	 bfd_elf_generic_reloc,	/* special_function */
209 	 "R_CRIS_GLOB_DAT",	/* name */
210 	 FALSE,			/* partial_inplace */
211 	 0,			/* src_mask */
212 	 0xffffffff,		/* dst_mask */
213 	 FALSE),		/* pcrel_offset */
214 
215   /* Marks a procedure linkage table entry for a symbol.  */
216   HOWTO (R_CRIS_JUMP_SLOT,	/* type */
217 	 0,			/* rightshift */
218 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
219 	 32,			/* bitsize */
220 	 FALSE,			/* pc_relative */
221 	 0,			/* bitpos */
222 	 complain_overflow_bitfield, /* complain_on_overflow */
223 	 bfd_elf_generic_reloc,	/* special_function */
224 	 "R_CRIS_JUMP_SLOT",	/* name */
225 	 FALSE,			/* partial_inplace */
226 	 0,			/* src_mask */
227 	 0,			/* dst_mask */
228 	 FALSE),		/* pcrel_offset */
229 
230   /* Used only by the dynamic linker.  When the object is run, this
231      longword is set to the load address of the object, plus the
232      addend.  */
233   HOWTO (R_CRIS_RELATIVE,	/* type */
234 	 0,			/* rightshift */
235 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
236 	 32,			/* bitsize */
237 	 FALSE,			/* pc_relative */
238 	 0,			/* bitpos */
239 	 complain_overflow_bitfield, /* complain_on_overflow */
240 	 bfd_elf_generic_reloc,	/* special_function */
241 	 "R_CRIS_RELATIVE",	/* name */
242 	 FALSE,			/* partial_inplace */
243 	 0,			/* src_mask */
244 	 0xffffffff,		/* dst_mask */
245 	 FALSE),		/* pcrel_offset */
246 
247   /* Like R_CRIS_32, but referring to the GOT table entry for the symbol.  */
248   HOWTO (R_CRIS_16_GOT,		/* type */
249 	 0,			/* rightshift */
250 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
251 	 16,			/* bitsize */
252 	 FALSE,			/* pc_relative */
253 	 0,			/* bitpos */
254 	 complain_overflow_bitfield, /* complain_on_overflow */
255 	 bfd_elf_generic_reloc,	/* special_function */
256 	 "R_CRIS_16_GOT",	/* name */
257 	 FALSE,			/* partial_inplace */
258 	 0,			/* src_mask */
259 	 0xffff,		/* dst_mask */
260 	 FALSE),		/* pcrel_offset */
261 
262   HOWTO (R_CRIS_32_GOT,		/* type */
263 	 0,			/* rightshift */
264 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
265 	 32,			/* bitsize */
266 	 FALSE,			/* pc_relative */
267 	 0,			/* bitpos */
268 	 complain_overflow_bitfield, /* complain_on_overflow */
269 	 bfd_elf_generic_reloc,	/* special_function */
270 	 "R_CRIS_32_GOT",	/* name */
271 	 FALSE,			/* partial_inplace */
272 	 0,			/* src_mask */
273 	 0xffffffff,		/* dst_mask */
274 	 FALSE),		/* pcrel_offset */
275 
276   /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
277      the GOT table for the symbol.  */
278   HOWTO (R_CRIS_16_GOTPLT,	/* type */
279 	 0,			/* rightshift */
280 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
281 	 16,			/* bitsize */
282 	 FALSE,			/* pc_relative */
283 	 0,			/* bitpos */
284 	 complain_overflow_bitfield, /* complain_on_overflow */
285 	 bfd_elf_generic_reloc,	/* special_function */
286 	 "R_CRIS_16_GOTPLT",	/* name */
287 	 FALSE,			/* partial_inplace */
288 	 0,			/* src_mask */
289 	 0xffff,		/* dst_mask */
290 	 FALSE),		/* pcrel_offset */
291 
292   HOWTO (R_CRIS_32_GOTPLT,	/* type */
293 	 0,			/* rightshift */
294 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
295 	 32,			/* bitsize */
296 	 FALSE,			/* pc_relative */
297 	 0,			/* bitpos */
298 	 complain_overflow_bitfield, /* complain_on_overflow */
299 	 bfd_elf_generic_reloc,	/* special_function */
300 	 "R_CRIS_32_GOTPLT",	/* name */
301 	 FALSE,			/* partial_inplace */
302 	 0,			/* src_mask */
303 	 0xffffffff,		/* dst_mask */
304 	 FALSE),		/* pcrel_offset */
305 
306   /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
307      be necessary.  */
308   HOWTO (R_CRIS_32_GOTREL,	/* type */
309 	 0,			/* rightshift */
310 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
311 	 32,			/* bitsize */
312 	 FALSE,			/* pc_relative */
313 	 0,			/* bitpos */
314 	 complain_overflow_bitfield, /* complain_on_overflow */
315 	 bfd_elf_generic_reloc,	/* special_function */
316 	 "R_CRIS_32_GOTREL",	/* name */
317 	 FALSE,			/* partial_inplace */
318 	 0,			/* src_mask */
319 	 0xffffffff,		/* dst_mask */
320 	 FALSE),		/* pcrel_offset */
321 
322   /* A 32-bit offset from GOT to entry for this symbol in PLT and request
323      to create PLT entry for symbol.  */
324   HOWTO (R_CRIS_32_PLT_GOTREL,	/* type */
325 	 0,			/* rightshift */
326 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
327 	 32,			/* bitsize */
328 	 FALSE,			/* pc_relative */
329 	 0,			/* bitpos */
330 	 complain_overflow_bitfield, /* complain_on_overflow */
331 	 bfd_elf_generic_reloc,	/* special_function */
332 	 "R_CRIS_32_PLT_GOTREL", /* name */
333 	 FALSE,			/* partial_inplace */
334 	 0,			/* src_mask */
335 	 0xffffffff,		/* dst_mask */
336 	 FALSE),		/* pcrel_offset */
337 
338   /* A 32-bit offset from PC (location after the relocation) + addend to
339      entry for this symbol in PLT and request to create PLT entry for
340      symbol.  */
341   HOWTO (R_CRIS_32_PLT_PCREL,	/* type */
342 	 0,			/* rightshift */
343 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
344 	 32,			/* bitsize */
345 	 TRUE,			/* pc_relative */
346 	 0,			/* bitpos */
347 	 complain_overflow_bitfield, /* complain_on_overflow */
348 	 cris_elf_pcrel_reloc,	/* special_function */
349 	 "R_CRIS_32_PLT_PCREL",	/* name */
350 	 FALSE,			/* partial_inplace */
351 	 0,			/* src_mask */
352 	 0xffffffff,		/* dst_mask */
353 	 TRUE),			/* pcrel_offset */
354 
355   /* We don't handle these in any special manner and cross-format
356      linking is not supported; just recognize them enough to pass them
357      around.  FIXME: do the same for most PIC relocs and add sanity
358      tests to actually refuse gracefully to handle these and PIC
359      relocs for cross-format linking.  */
360 #define TLSHOWTO32(name) \
361  HOWTO (name, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, \
362         bfd_elf_generic_reloc, #name, FALSE, 0, 0xffffffff, FALSE)
363 #define TLSHOWTO16X(name, X)	     \
364  HOWTO (name, 0, 1, 16, FALSE, 0, complain_overflow_ ## X, \
365         bfd_elf_generic_reloc, #name, FALSE, 0, 0xffff, FALSE)
366 #define TLSHOWTO16(name) TLSHOWTO16X(name, unsigned)
367 #define TLSHOWTO16S(name) TLSHOWTO16X(name, signed)
368 
369   TLSHOWTO32 (R_CRIS_32_GOT_GD),
370   TLSHOWTO16 (R_CRIS_16_GOT_GD),
371   TLSHOWTO32 (R_CRIS_32_GD),
372   TLSHOWTO32 (R_CRIS_DTP),
373   TLSHOWTO32 (R_CRIS_32_DTPREL),
374   TLSHOWTO16S (R_CRIS_16_DTPREL),
375   TLSHOWTO32 (R_CRIS_32_GOT_TPREL),
376   TLSHOWTO16S (R_CRIS_16_GOT_TPREL),
377   TLSHOWTO32 (R_CRIS_32_TPREL),
378   TLSHOWTO16S (R_CRIS_16_TPREL),
379   TLSHOWTO32 (R_CRIS_DTPMOD),
380   TLSHOWTO32 (R_CRIS_32_IE)
381 };
382 
383 /* Map BFD reloc types to CRIS ELF reloc types.  */
384 
385 struct cris_reloc_map
386 {
387   bfd_reloc_code_real_type bfd_reloc_val;
388   unsigned int cris_reloc_val;
389 };
390 
391 static const struct cris_reloc_map cris_reloc_map [] =
392 {
393   { BFD_RELOC_NONE,		R_CRIS_NONE },
394   { BFD_RELOC_8,		R_CRIS_8 },
395   { BFD_RELOC_16,		R_CRIS_16 },
396   { BFD_RELOC_32,		R_CRIS_32 },
397   { BFD_RELOC_8_PCREL,		R_CRIS_8_PCREL },
398   { BFD_RELOC_16_PCREL,		R_CRIS_16_PCREL },
399   { BFD_RELOC_32_PCREL,		R_CRIS_32_PCREL },
400   { BFD_RELOC_VTABLE_INHERIT,	R_CRIS_GNU_VTINHERIT },
401   { BFD_RELOC_VTABLE_ENTRY,	R_CRIS_GNU_VTENTRY },
402   { BFD_RELOC_CRIS_COPY,	R_CRIS_COPY },
403   { BFD_RELOC_CRIS_GLOB_DAT,	R_CRIS_GLOB_DAT },
404   { BFD_RELOC_CRIS_JUMP_SLOT,	R_CRIS_JUMP_SLOT },
405   { BFD_RELOC_CRIS_RELATIVE,	R_CRIS_RELATIVE },
406   { BFD_RELOC_CRIS_16_GOT,	R_CRIS_16_GOT },
407   { BFD_RELOC_CRIS_32_GOT,	R_CRIS_32_GOT },
408   { BFD_RELOC_CRIS_16_GOTPLT,	R_CRIS_16_GOTPLT },
409   { BFD_RELOC_CRIS_32_GOTPLT,	R_CRIS_32_GOTPLT },
410   { BFD_RELOC_CRIS_32_GOTREL,	R_CRIS_32_GOTREL },
411   { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
412   { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL },
413   { BFD_RELOC_CRIS_32_GOT_GD,	R_CRIS_32_GOT_GD },
414   { BFD_RELOC_CRIS_16_GOT_GD,	R_CRIS_16_GOT_GD },
415   { BFD_RELOC_CRIS_32_GD,	R_CRIS_32_GD },
416   { BFD_RELOC_CRIS_DTP,	R_CRIS_DTP },
417   { BFD_RELOC_CRIS_32_DTPREL,	R_CRIS_32_DTPREL },
418   { BFD_RELOC_CRIS_16_DTPREL,	R_CRIS_16_DTPREL },
419   { BFD_RELOC_CRIS_32_GOT_TPREL, R_CRIS_32_GOT_TPREL },
420   { BFD_RELOC_CRIS_16_GOT_TPREL, R_CRIS_16_GOT_TPREL },
421   { BFD_RELOC_CRIS_32_TPREL,	R_CRIS_32_TPREL },
422   { BFD_RELOC_CRIS_16_TPREL,	R_CRIS_16_TPREL },
423   { BFD_RELOC_CRIS_DTPMOD,	R_CRIS_DTPMOD },
424   { BFD_RELOC_CRIS_32_IE,	R_CRIS_32_IE }
425 };
426 
427 static reloc_howto_type *
428 cris_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
429 			bfd_reloc_code_real_type code)
430 {
431   unsigned int i;
432 
433   for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
434     if (cris_reloc_map [i].bfd_reloc_val == code)
435       return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
436 
437   return NULL;
438 }
439 
440 static reloc_howto_type *
441 cris_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
442 {
443   unsigned int i;
444 
445   for (i = 0;
446        i < sizeof (cris_elf_howto_table) / sizeof (cris_elf_howto_table[0]);
447        i++)
448     if (cris_elf_howto_table[i].name != NULL
449 	&& strcasecmp (cris_elf_howto_table[i].name, r_name) == 0)
450       return &cris_elf_howto_table[i];
451 
452   return NULL;
453 }
454 
455 /* Set the howto pointer for an CRIS ELF reloc.  */
456 
457 static void
458 cris_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
459 			 arelent * cache_ptr,
460 			 Elf_Internal_Rela * dst)
461 {
462   enum elf_cris_reloc_type r_type;
463 
464   r_type = ELF32_R_TYPE (dst->r_info);
465   BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
466   cache_ptr->howto = & cris_elf_howto_table [r_type];
467 }
468 
469 bfd_reloc_status_type
470 cris_elf_pcrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
471 		      arelent *reloc_entry,
472 		      asymbol *symbol,
473 		      void * data ATTRIBUTE_UNUSED,
474 		      asection *input_section,
475 		      bfd *output_bfd,
476 		      char **error_message ATTRIBUTE_UNUSED)
477 {
478   /* By default (using only bfd_elf_generic_reloc when linking to
479      non-ELF formats) PC-relative relocs are relative to the beginning
480      of the reloc.  CRIS PC-relative relocs are relative to the position
481      *after* the reloc because that's what pre-CRISv32 PC points to
482      after reading an insn field with that reloc.  (For CRISv32, PC is
483      actually relative to the start of the insn, but we keep the old
484      definition.)  Still, we use as much generic machinery as we can.
485 
486      Only adjust when doing a final link.  */
487   if (output_bfd == (bfd *) NULL)
488     reloc_entry->addend -= 1 << reloc_entry->howto->size;
489 
490   return
491     bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
492 			   input_section, output_bfd, error_message);
493 }
494 
495 /* Support for core dump NOTE sections.
496    The slightly unintuitive code layout is an attempt to keep at least
497    some similarities with other ports, hoping to simplify general
498    changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart.  */
499 
500 static bfd_boolean
501 cris_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
502 {
503   int offset;
504   size_t size;
505 
506   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
507     switch (note->descsz)
508       {
509       default:
510 	return FALSE;
511 
512       case 202:		/* Linux/CRISv32 */
513 	/* pr_cursig */
514 	elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
515 
516 	/* pr_pid */
517 	elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 22);
518 
519 	/* pr_reg */
520 	offset = 70;
521 	size = 128;
522 
523 	break;
524       }
525   else
526     switch (note->descsz)
527       {
528       default:
529 	return FALSE;
530 
531       case 214:		/* Linux/CRIS */
532 	/* pr_cursig */
533 	elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
534 
535 	/* pr_pid */
536 	elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 22);
537 
538 	/* pr_reg */
539 	offset = 70;
540 	size = 140;
541 
542 	break;
543       }
544 
545   /* Make a ".reg/999" section.  */
546   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
547 					  size, note->descpos + offset);
548 }
549 
550 static bfd_boolean
551 cris_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
552 {
553   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
554     switch (note->descsz)
555       {
556       default:
557 	return FALSE;
558 
559       case 124:		/* Linux/CRISv32 elf_prpsinfo */
560 	elf_tdata (abfd)->core_program
561 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
562 	elf_tdata (abfd)->core_command
563 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
564       }
565   else
566     switch (note->descsz)
567       {
568       default:
569 	return FALSE;
570 
571       case 124:		/* Linux/CRIS elf_prpsinfo */
572 	elf_tdata (abfd)->core_program
573 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
574 	elf_tdata (abfd)->core_command
575 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
576       }
577 
578   /* Note that for some reason, a spurious space is tacked
579      onto the end of the args in some (at least one anyway)
580      implementations, so strip it off if it exists.  */
581 
582   {
583     char *command = elf_tdata (abfd)->core_command;
584     int n = strlen (command);
585 
586     if (0 < n && command[n - 1] == ' ')
587       command[n - 1] = '\0';
588   }
589 
590   return TRUE;
591 }
592 
593 /* The name of the dynamic interpreter.  This is put in the .interp
594    section.  */
595 
596 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
597 
598 /* The size in bytes of an entry in the procedure linkage table.  */
599 
600 #define PLT_ENTRY_SIZE 20
601 #define PLT_ENTRY_SIZE_V32 26
602 
603 /* The first entry in an absolute procedure linkage table looks like this.  */
604 
605 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
606 {
607   0xfc, 0xe1,
608   0x7e, 0x7e,	/* push mof.  */
609   0x7f, 0x0d,   /*  (dip [pc+]) */
610   0, 0, 0, 0,	/*  Replaced with address of .got + 4.  */
611   0x30, 0x7a,	/* move [...],mof */
612   0x7f, 0x0d,   /*  (dip [pc+]) */
613   0, 0, 0, 0,	/*  Replaced with address of .got + 8.  */
614   0x30, 0x09	/* jump [...] */
615 };
616 
617 static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
618 {
619   0x84, 0xe2,	/* subq 4,$sp */
620   0x6f, 0xfe,	/* move.d 0,$acr */
621   0, 0, 0, 0,	/*  Replaced by address of .got + 4.  */
622   0x7e, 0x7a,	/* move $mof,[$sp] */
623   0x3f, 0x7a,	/* move [$acr],$mof */
624   0x04, 0xf2,	/* addq 4,acr */
625   0x6f, 0xfa,	/* move.d [$acr],$acr */
626   0xbf, 0x09,	/* jump $acr */
627   0xb0, 0x05,	/* nop */
628   0, 0		/*  Pad out to 26 bytes.  */
629 };
630 
631 /* Subsequent entries in an absolute procedure linkage table look like
632    this.  */
633 
634 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
635 {
636   0x7f, 0x0d,   /*  (dip [pc+]) */
637   0, 0, 0, 0,	/*  Replaced with address of this symbol in .got.  */
638   0x30, 0x09,	/* jump [...] */
639   0x3f,	 0x7e,	/* move [pc+],mof */
640   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
641   0x2f, 0xfe,	/* add.d [pc+],pc */
642   0xec, 0xff,
643   0xff, 0xff	/*  Replaced with offset to start of .plt.  */
644 };
645 
646 static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
647 {
648   0x6f, 0xfe,	/* move.d 0,$acr */
649   0, 0, 0, 0,	/*  Replaced with address of this symbol in .got.  */
650   0x6f, 0xfa,   /* move.d [$acr],$acr */
651   0xbf, 0x09,   /* jump $acr */
652   0xb0, 0x05,	/* nop */
653   0x3f, 0x7e,	/* move 0,mof */
654   0, 0, 0, 0,	/*  Replaced with offset into relocation table. */
655   0xbf, 0x0e,	/* ba start_of_plt0_entry */
656   0, 0, 0, 0,	/*  Replaced with offset to plt0 entry.  */
657   0xb0, 0x05	/* nop */
658 };
659 
660 /* The first entry in a PIC procedure linkage table looks like this.  */
661 
662 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
663 {
664   0xfc, 0xe1, 0x7e, 0x7e,	/* push mof */
665   0x04, 0x01, 0x30, 0x7a,	/* move [r0+4],mof */
666   0x08, 0x01, 0x30, 0x09,	/* jump [r0+8] */
667   0, 0, 0, 0, 0, 0, 0, 0,	/*  Pad out to 20 bytes.  */
668 };
669 
670 static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
671 {
672   0x84, 0xe2,	/* subq 4,$sp */
673   0x04, 0x01,	/* addoq 4,$r0,$acr */
674   0x7e, 0x7a,	/* move $mof,[$sp] */
675   0x3f, 0x7a,	/* move [$acr],$mof */
676   0x04, 0xf2,	/* addq 4,$acr */
677   0x6f, 0xfa,	/* move.d [$acr],$acr */
678   0xbf, 0x09,	/* jump $acr */
679   0xb0, 0x05,	/* nop */
680   0, 0,		/*  Pad out to 26 bytes.  */
681   0, 0, 0, 0,
682   0, 0, 0, 0
683 };
684 
685 /* Subsequent entries in a PIC procedure linkage table look like this.  */
686 
687 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
688 {
689   0x6f, 0x0d,   /*  (bdap [pc+].d,r0) */
690   0, 0, 0, 0,	/*  Replaced with offset of this symbol in .got.  */
691   0x30, 0x09,	/* jump [...] */
692   0x3f, 0x7e,	/* move [pc+],mof */
693   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
694   0x2f, 0xfe,	/* add.d [pc+],pc */
695   0xec, 0xff,	/*  Replaced with offset to start of .plt.  */
696   0xff, 0xff
697 };
698 
699 static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
700 {
701   0x6f, 0x0d,	/* addo.d 0,$r0,$acr */
702   0, 0, 0, 0,	/*  Replaced with offset of this symbol in .got.  */
703   0x6f, 0xfa,	/* move.d [$acr],$acr */
704   0xbf, 0x09,	/* jump $acr */
705   0xb0, 0x05,	/* nop */
706   0x3f, 0x7e,	/* move relocoffs,$mof */
707   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
708   0xbf, 0x0e,	/* ba start_of_plt */
709   0, 0, 0, 0,	/*  Replaced with offset to start of .plt.  */
710   0xb0, 0x05	/* nop */
711 };
712 
713 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
714    (and most other PIC/shlib stuff).  Check that we don't drift away
715    without reason.
716 
717    The CRIS linker, like the m68k and i386 linkers (and probably the rest
718    too) needs to keep track of the number of relocs that it decides to
719    copy in check_relocs for each symbol.  This is so that it can discard
720    PC relative relocs if it doesn't need them when linking with
721    -Bsymbolic.  We store the information in a field extending the regular
722    ELF linker hash table.  */
723 
724 /* This structure keeps track of the number of PC relative relocs we have
725    copied for a given symbol.  */
726 
727 struct elf_cris_pcrel_relocs_copied
728 {
729   /* Next section.  */
730   struct elf_cris_pcrel_relocs_copied *next;
731 
732   /* A section in dynobj.  */
733   asection *section;
734 
735   /* Number of relocs copied in this section.  */
736   bfd_size_type count;
737 
738   /* Example of reloc being copied, for message.  */
739   enum elf_cris_reloc_type r_type;
740 };
741 
742 /* CRIS ELF linker hash entry.  */
743 
744 struct elf_cris_link_hash_entry
745 {
746   struct elf_link_hash_entry root;
747 
748   /* Number of PC relative relocs copied for this symbol.  */
749   struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
750 
751   /* The GOTPLT references are CRIS-specific; the goal is to avoid having
752      both a general GOT and a PLT-specific GOT entry for the same symbol,
753      when it is referenced both as a function and as a function pointer.
754 
755      Number of GOTPLT references for a function.  */
756   bfd_signed_vma gotplt_refcount;
757 
758   /* Actual GOTPLT index for this symbol, if applicable, or zero if not
759      (zero is never used as an index).  FIXME: We should be able to fold
760      this with gotplt_refcount in a union, like the got and plt unions in
761      elf_link_hash_entry.  */
762   bfd_size_type gotplt_offset;
763 
764   /* The root.got.refcount is the sum of the regular reference counts
765      (this) and those members below.  We have to keep a separate count
766      to track when we've found the first (or last) reference to a
767      regular got entry.  The offset is in root.got.offset.  */
768   bfd_signed_vma reg_got_refcount;
769 
770   /* Similar to the above, the number of reloc references to this
771      symbols that need a R_CRIS_32_TPREL slot.  The offset is in
772      root.got.offset, because this and .dtp_refcount can't validly
773      happen when there's also a regular GOT entry; that's invalid
774      input for which an error is emitted.  */
775   bfd_signed_vma tprel_refcount;
776 
777   /* Similar to the above, the number of reloc references to this
778      symbols that need a R_CRIS_DTP slot.  The offset is in
779      root.got.offset; plus 4 if .tprel_refcount > 0.  */
780   bfd_signed_vma dtp_refcount;
781 };
782 
783 static bfd_boolean
784 elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *,
785 				      void * );
786 static bfd_boolean
787 elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *,
788 					  void *);
789 
790 /* The local_got_refcounts and local_got_offsets are a multiple of
791    LSNUM in size, namely LGOT_ALLOC_NELTS_FOR(LSNUM) (plus one for the
792    refcount for GOT itself, see code), with the summary / group offset
793    for local symbols located at offset N, reference counts for
794    ordinary (address) relocs at offset N + LSNUM, for R_CRIS_DTP
795    relocs at offset N + 2*LSNUM, and for R_CRIS_32_TPREL relocs at N +
796    3*LSNUM.  */
797 
798 #define LGOT_REG_NDX(x) ((x) + symtab_hdr->sh_info)
799 #define LGOT_DTP_NDX(x) ((x) + 2 * symtab_hdr->sh_info)
800 #define LGOT_TPREL_NDX(x) ((x) + 3 * symtab_hdr->sh_info)
801 #define LGOT_ALLOC_NELTS_FOR(x) ((x) * 4)
802 
803 /* CRIS ELF linker hash table.  */
804 
805 struct elf_cris_link_hash_table
806 {
807   struct elf_link_hash_table root;
808 
809   /* We can't use the PLT offset and calculate to get the GOTPLT offset,
810      since we try and avoid creating GOTPLT:s when there's already a GOT.
811      Instead, we keep and update the next available index here.  */
812   bfd_size_type next_gotplt_entry;
813 
814   /* The number of R_CRIS_32_DTPREL and R_CRIS_16_DTPREL that have
815      been seen for any input; if != 0, then the constant-offset
816      R_CRIS_DTPMOD is needed for this DSO/executable.  This turns
817      negative at relocation, so that we don't need an extra flag for
818      when the reloc is output.  */
819   bfd_signed_vma dtpmod_refcount;
820 };
821 
822 /* Traverse a CRIS ELF linker hash table.  */
823 
824 #define elf_cris_link_hash_traverse(table, func, info)			\
825   (elf_link_hash_traverse						\
826    (&(table)->root,							\
827     (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func),	\
828     (info)))
829 
830 /* Get the CRIS ELF linker hash table from a link_info structure.  */
831 
832 #define elf_cris_hash_table(p) \
833   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
834   == CRIS_ELF_DATA ? ((struct elf_cris_link_hash_table *) ((p)->hash)) : NULL)
835 
836 /* Get the CRIS ELF linker hash entry from a regular hash entry (the
837    "parent class").  The .root reference is just a simple type
838    check on the argument.  */
839 
840 #define elf_cris_hash_entry(p) \
841  ((struct elf_cris_link_hash_entry *) (&(p)->root))
842 
843 /* Create an entry in a CRIS ELF linker hash table.  */
844 
845 static struct bfd_hash_entry *
846 elf_cris_link_hash_newfunc (struct bfd_hash_entry *entry,
847 			    struct bfd_hash_table *table,
848 			    const char *string)
849 {
850   struct elf_cris_link_hash_entry *ret =
851     (struct elf_cris_link_hash_entry *) entry;
852 
853   /* Allocate the structure if it has not already been allocated by a
854      subclass.  */
855   if (ret == (struct elf_cris_link_hash_entry *) NULL)
856     ret = ((struct elf_cris_link_hash_entry *)
857 	   bfd_hash_allocate (table,
858 			      sizeof (struct elf_cris_link_hash_entry)));
859   if (ret == (struct elf_cris_link_hash_entry *) NULL)
860     return (struct bfd_hash_entry *) ret;
861 
862   /* Call the allocation method of the superclass.  */
863   ret = ((struct elf_cris_link_hash_entry *)
864 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
865 				     table, string));
866   if (ret != (struct elf_cris_link_hash_entry *) NULL)
867     {
868       ret->pcrel_relocs_copied = NULL;
869       ret->gotplt_refcount = 0;
870       ret->gotplt_offset = 0;
871       ret->dtp_refcount = 0;
872       ret->tprel_refcount = 0;
873       ret->reg_got_refcount = 0;
874     }
875 
876   return (struct bfd_hash_entry *) ret;
877 }
878 
879 /* Create a CRIS ELF linker hash table.  */
880 
881 static struct bfd_link_hash_table *
882 elf_cris_link_hash_table_create (bfd *abfd)
883 {
884   struct elf_cris_link_hash_table *ret;
885   bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
886 
887   ret = ((struct elf_cris_link_hash_table *) bfd_malloc (amt));
888   if (ret == (struct elf_cris_link_hash_table *) NULL)
889     return NULL;
890 
891   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
892 				      elf_cris_link_hash_newfunc,
893 				      sizeof (struct elf_cris_link_hash_entry),
894 				      CRIS_ELF_DATA))
895     {
896       free (ret);
897       return NULL;
898     }
899 
900   /* Initialize to skip over the first three entries in the gotplt; they
901      are used for run-time symbol evaluation.  */
902   ret->next_gotplt_entry = 12;
903 
904   /* We haven't seen any R_CRIS_nn_GOT_TPREL initially.  */
905   ret->dtpmod_refcount = 0;
906 
907   return &ret->root.root;
908 }
909 
910 /* Perform a single relocation.  By default we use the standard BFD
911    routines, with a few tweaks.  */
912 
913 static bfd_reloc_status_type
914 cris_final_link_relocate (reloc_howto_type *  howto,
915 			  bfd *               input_bfd,
916 			  asection *          input_section,
917 			  bfd_byte *          contents,
918 			  Elf_Internal_Rela * rel,
919 			  bfd_vma             relocation)
920 {
921   bfd_reloc_status_type r;
922   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
923 
924   /* PC-relative relocations are relative to the position *after*
925      the reloc.  Note that for R_CRIS_8_PCREL the adjustment is
926      not a single byte, since PC must be 16-bit-aligned.  */
927   switch (r_type)
928     {
929       /* Check that the 16-bit GOT relocs are positive.  */
930     case R_CRIS_16_GOTPLT:
931     case R_CRIS_16_GOT:
932       if ((bfd_signed_vma) relocation < 0)
933 	return bfd_reloc_overflow;
934       break;
935 
936     case R_CRIS_32_PLT_PCREL:
937     case R_CRIS_32_PCREL:
938       relocation -= 2;
939       /* Fall through.  */
940     case R_CRIS_8_PCREL:
941     case R_CRIS_16_PCREL:
942       relocation -= 2;
943       break;
944 
945     default:
946       break;
947     }
948 
949   r = _bfd_final_link_relocate (howto, input_bfd, input_section,
950 				contents, rel->r_offset,
951 				relocation, rel->r_addend);
952   return r;
953 }
954 
955 
956 /* The number of errors left before we stop outputting reloc-specific
957    explanatory messages.  By coincidence, this works nicely together
958    with the default number of messages you'll get from LD about
959    "relocation truncated to fit" messages before you get an
960    "additional relocation overflows omitted from the output".  */
961 static int additional_relocation_error_msg_count = 10;
962 
963 /* Relocate an CRIS ELF section.  See elf32-fr30.c, from where this was
964    copied, for further comments.  */
965 
966 static bfd_boolean
967 cris_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
968 			   struct bfd_link_info *info,
969 			   bfd *input_bfd,
970 			   asection *input_section,
971 			   bfd_byte *contents,
972 			   Elf_Internal_Rela *relocs,
973 			   Elf_Internal_Sym *local_syms,
974 			   asection **local_sections)
975 {
976   struct elf_cris_link_hash_table * htab;
977   bfd *dynobj;
978   Elf_Internal_Shdr *symtab_hdr;
979   struct elf_link_hash_entry **sym_hashes;
980   bfd_vma *local_got_offsets;
981   asection *sgot;
982   asection *splt;
983   asection *sreloc;
984   Elf_Internal_Rela *rel;
985   Elf_Internal_Rela *relend;
986   asection *srelgot;
987 
988   htab = elf_cris_hash_table (info);
989   if (htab == NULL)
990     return FALSE;
991 
992   dynobj = elf_hash_table (info)->dynobj;
993   local_got_offsets = elf_local_got_offsets (input_bfd);
994   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
995   sym_hashes = elf_sym_hashes (input_bfd);
996   relend     = relocs + input_section->reloc_count;
997 
998   sgot = NULL;
999   splt = NULL;
1000   sreloc = NULL;
1001   srelgot = NULL;
1002 
1003   if (dynobj != NULL)
1004     {
1005       splt = bfd_get_linker_section (dynobj, ".plt");
1006       sgot = bfd_get_linker_section (dynobj, ".got");
1007     }
1008 
1009   for (rel = relocs; rel < relend; rel ++)
1010     {
1011       reloc_howto_type *howto;
1012       unsigned long r_symndx;
1013       Elf_Internal_Sym *sym;
1014       asection *sec;
1015       struct elf_link_hash_entry *h;
1016       bfd_vma relocation;
1017       bfd_reloc_status_type r;
1018       const char *symname = NULL;
1019       enum elf_cris_reloc_type r_type;
1020 
1021       r_type = ELF32_R_TYPE (rel->r_info);
1022 
1023       if (   r_type == R_CRIS_GNU_VTINHERIT
1024 	  || r_type == R_CRIS_GNU_VTENTRY)
1025 	continue;
1026 
1027       r_symndx = ELF32_R_SYM (rel->r_info);
1028       howto  = cris_elf_howto_table + r_type;
1029       h      = NULL;
1030       sym    = NULL;
1031       sec    = NULL;
1032 
1033       if (r_symndx < symtab_hdr->sh_info)
1034 	{
1035 	  sym = local_syms + r_symndx;
1036 	  sec = local_sections [r_symndx];
1037 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1038 
1039 	  symname = (bfd_elf_string_from_elf_section
1040 		     (input_bfd, symtab_hdr->sh_link, sym->st_name));
1041 	  if (symname == NULL)
1042 	    symname = bfd_section_name (input_bfd, sec);
1043 	}
1044       else
1045 	{
1046 	  bfd_boolean warned;
1047 	  bfd_boolean unresolved_reloc;
1048 
1049 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1050 				   r_symndx, symtab_hdr, sym_hashes,
1051 				   h, sec, relocation,
1052 				   unresolved_reloc, warned);
1053 
1054 	  symname = h->root.root.string;
1055 
1056 	  if (unresolved_reloc
1057 	      /* Perhaps we should detect the cases that
1058 		 sec->output_section is expected to be NULL like i386 and
1059 		 m68k, but apparently (and according to elfxx-ia64.c) all
1060 		 valid cases are where the symbol is defined in a shared
1061 		 object which we link dynamically against.  This includes
1062 		 PLT relocs for which we've created a PLT entry and other
1063 		 relocs for which we're prepared to create dynamic
1064 		 relocations.
1065 
1066 		 For now, new situations cause us to just err when
1067 		 sec->output_offset is NULL but the object with the symbol
1068 		 is *not* dynamically linked against.  Thus this will
1069 		 automatically remind us so we can see if there are other
1070 		 valid cases we need to revisit.  */
1071 	      && (sec->owner->flags & DYNAMIC) != 0)
1072 	    relocation = 0;
1073 
1074 	  else if (h->root.type == bfd_link_hash_defined
1075 		   || h->root.type == bfd_link_hash_defweak)
1076 	    {
1077 	      /* Here follow the cases where the relocation value must
1078 		 be zero (or when further handling is simplified when
1079 		 zero).  I can't claim to understand the various
1080 		 conditions and they weren't described in the files
1081 		 where I copied them from (elf32-m68k.c and
1082 		 elf32-i386.c), but let's mention examples of where
1083 		 they happen.  FIXME: Perhaps define and use a
1084 		 dynamic_symbol_p function like ia64.
1085 
1086 		 - When creating a shared library, we can have an
1087 		 ordinary relocation for a symbol defined in a shared
1088 		 library (perhaps the one we create).  We then make
1089 		 the relocation value zero, as the value seen now will
1090 		 be added into the relocation addend in this shared
1091 		 library, but must be handled only at dynamic-link
1092 		 time.  FIXME: Not sure this example covers the
1093 		 h->elf_link_hash_flags test, though it's there in
1094 		 other targets.  */
1095 	      if (info->shared
1096 		  && ((! info->symbolic && h->dynindx != -1)
1097 		      || !h->def_regular)
1098 		  && (input_section->flags & SEC_ALLOC) != 0
1099 		  && (r_type == R_CRIS_8
1100 		      || r_type == R_CRIS_16
1101 		      || r_type == R_CRIS_32
1102 		      || r_type == R_CRIS_8_PCREL
1103 		      || r_type == R_CRIS_16_PCREL
1104 		      || r_type == R_CRIS_32_PCREL))
1105 		relocation = 0;
1106 	      else if (!info->relocatable && unresolved_reloc
1107 		       && (_bfd_elf_section_offset (output_bfd, info,
1108 						    input_section,
1109 						    rel->r_offset)
1110 			   != (bfd_vma) -1))
1111 		{
1112 		  _bfd_error_handler
1113 		    (_("%B, section %A: unresolvable relocation %s against symbol `%s'"),
1114 		     input_bfd,
1115 		     input_section,
1116 		     cris_elf_howto_table[r_type].name,
1117 		     symname);
1118 		  bfd_set_error (bfd_error_bad_value);
1119 		  return FALSE;
1120 		}
1121 	    }
1122 	}
1123 
1124       if (sec != NULL && discarded_section (sec))
1125 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1126 					 rel, 1, relend, howto, 0, contents);
1127 
1128       if (info->relocatable)
1129 	continue;
1130 
1131       switch (r_type)
1132 	{
1133 	case R_CRIS_16_GOTPLT:
1134 	case R_CRIS_32_GOTPLT:
1135 	  /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
1136 	     but we require a PLT, and the PLT handling will take care of
1137 	     filling in the PLT-specific GOT entry.  For the GOT offset,
1138 	     calculate it as we do when filling it in for the .got.plt
1139 	     section.  If we don't have a PLT, punt to GOT handling.  */
1140 	  if (h != NULL
1141 	      && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
1142 	    {
1143 	      asection *sgotplt
1144 		= bfd_get_linker_section (dynobj, ".got.plt");
1145 	      bfd_vma got_offset;
1146 
1147 	      BFD_ASSERT (h->dynindx != -1);
1148 	      BFD_ASSERT (sgotplt != NULL);
1149 
1150 	      got_offset
1151 		= ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1152 
1153 	      relocation = got_offset;
1154 	      break;
1155 	    }
1156 
1157 	  /* We didn't make a PLT entry for this symbol.  Maybe everything is
1158 	     folded into the GOT.  Other than folding, this happens when
1159 	     statically linking PIC code, or when using -Bsymbolic.  Check
1160 	     that we instead have a GOT entry as done for us by
1161 	     elf_cris_adjust_dynamic_symbol, and drop through into the
1162 	     ordinary GOT cases.  This must not happen for the
1163 	     executable, because any reference it does to a function
1164 	     that is satisfied by a DSO must generate a PLT.  We assume
1165 	     these call-specific relocs don't address non-functions.  */
1166 	  if (h != NULL
1167 	      && (h->got.offset == (bfd_vma) -1
1168 		  || (!info->shared
1169 		      && !(h->def_regular
1170 			   || (!h->def_dynamic
1171 			       && h->root.type == bfd_link_hash_undefweak)))))
1172 	    {
1173 	      (*_bfd_error_handler)
1174 		((h->got.offset == (bfd_vma) -1)
1175 		 ? _("%B, section %A: No PLT nor GOT for relocation %s"
1176 		     " against symbol `%s'")
1177 		 : _("%B, section %A: No PLT for relocation %s"
1178 		     " against symbol `%s'"),
1179 		 input_bfd,
1180 		 input_section,
1181 		 cris_elf_howto_table[r_type].name,
1182 		 (symname != NULL && symname[0] != '\0'
1183 		  ? symname : _("[whose name is lost]")));
1184 
1185 	      /* FIXME: Perhaps blaming input is not the right thing to
1186 		 do; this is probably an internal error.  But it is true
1187 		 that we didn't like that particular input.  */
1188 	      bfd_set_error (bfd_error_bad_value);
1189 	      return FALSE;
1190 	    }
1191 	  /* Fall through.  */
1192 
1193 	  /* The size of the actual relocation is not used here; we only
1194 	     fill in the GOT table here.  */
1195 	case R_CRIS_16_GOT:
1196 	case R_CRIS_32_GOT:
1197 	  {
1198 	    bfd_vma off;
1199 
1200 	    /* Note that despite using RELA relocations, the .got contents
1201 	       is always filled in with the link-relative relocation
1202 	       value; the addend.  */
1203 
1204 	    if (h != NULL)
1205 	      {
1206 		off = h->got.offset;
1207 		BFD_ASSERT (off != (bfd_vma) -1);
1208 
1209 		if (!elf_hash_table (info)->dynamic_sections_created
1210 		    || (! info->shared
1211 			&& (h->def_regular
1212 			    || h->type == STT_FUNC
1213 			    || h->needs_plt))
1214 		    || (info->shared
1215 			&& (info->symbolic || h->dynindx == -1)
1216 			&& h->def_regular))
1217 		  {
1218 		    /* This wasn't checked above for ! info->shared, but
1219 		       must hold there if we get here; the symbol must
1220 		       be defined in the regular program or be undefweak
1221 		       or be a function or otherwise need a PLT.  */
1222 		    BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
1223 				|| info->shared
1224 				|| h->def_regular
1225 				|| h->type == STT_FUNC
1226 				|| h->needs_plt
1227 				|| h->root.type == bfd_link_hash_undefweak);
1228 
1229 		    /* This is actually a static link, or it is a
1230 		       -Bsymbolic link and the symbol is defined locally,
1231 		       or is undefweak, or the symbol was forced to be
1232 		       local because of a version file, or we're not
1233 		       creating a dynamic object.  We must initialize this
1234 		       entry in the global offset table.  Since the offset
1235 		       must always be a multiple of 4, we use the least
1236 		       significant bit to record whether we have
1237 		       initialized it already.
1238 
1239 		       If this GOT entry should be runtime-initialized, we
1240 		       will create a .rela.got relocation entry to
1241 		       initialize the value.  This is done in the
1242 		       finish_dynamic_symbol routine.  */
1243 		    if ((off & 1) != 0)
1244 		      off &= ~1;
1245 		    else
1246 		      {
1247 			bfd_put_32 (output_bfd, relocation,
1248 				    sgot->contents + off);
1249 			h->got.offset |= 1;
1250 		      }
1251 		  }
1252 	      }
1253 	    else
1254 	      {
1255 		BFD_ASSERT (local_got_offsets != NULL
1256 			    && local_got_offsets[r_symndx] != (bfd_vma) -1);
1257 
1258 		off = local_got_offsets[r_symndx];
1259 
1260 		/* The offset must always be a multiple of 4.  We use
1261 		   the least significant bit to record whether we have
1262 		   already generated the necessary reloc.  */
1263 		if ((off & 1) != 0)
1264 		  off &= ~1;
1265 		else
1266 		  {
1267 		    bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1268 
1269 		    if (info->shared)
1270 		      {
1271 			Elf_Internal_Rela outrel;
1272 			bfd_byte *loc;
1273 
1274 			if (srelgot == NULL)
1275 			  srelgot
1276 			    = bfd_get_linker_section (dynobj, ".rela.got");
1277 			BFD_ASSERT (srelgot != NULL);
1278 
1279 			outrel.r_offset = (sgot->output_section->vma
1280 					   + sgot->output_offset
1281 					   + off);
1282 			outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1283 			outrel.r_addend = relocation;
1284 			loc = srelgot->contents;
1285 			loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1286 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1287 		      }
1288 
1289 		    local_got_offsets[r_symndx] |= 1;
1290 		  }
1291 	      }
1292 
1293 	    relocation = sgot->output_offset + off;
1294 	    if (rel->r_addend != 0)
1295 	      {
1296 		/* We can't do anything for a relocation which is against
1297 		   a symbol *plus offset*.  GOT holds relocations for
1298 		   symbols.  Make this an error; the compiler isn't
1299 		   allowed to pass us these kinds of things.  */
1300 		if (h == NULL)
1301 		  (*_bfd_error_handler)
1302 		    (_("%B, section %A: relocation %s with non-zero addend %d"
1303 		       " against local symbol"),
1304 		     input_bfd,
1305 		     input_section,
1306 		     cris_elf_howto_table[r_type].name,
1307 		     rel->r_addend);
1308 		else
1309 		  (*_bfd_error_handler)
1310 		    (_("%B, section %A: relocation %s with non-zero addend %d"
1311 		       " against symbol `%s'"),
1312 		     input_bfd,
1313 		     input_section,
1314 		     cris_elf_howto_table[r_type].name,
1315 		     rel->r_addend,
1316 		     symname[0] != '\0' ? symname : _("[whose name is lost]"));
1317 
1318 		bfd_set_error (bfd_error_bad_value);
1319 		return FALSE;
1320 	      }
1321 	  }
1322 	  break;
1323 
1324 	case R_CRIS_32_GOTREL:
1325 	  /* This relocation must only be performed against local symbols.
1326 	     It's also ok when we link a program and the symbol is either
1327 	     defined in an ordinary (non-DSO) object or is undefined weak.  */
1328 	  if (h != NULL
1329 	      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1330 	      && !(!info->shared
1331 		   && (h->def_regular
1332 		       || (!h->def_dynamic
1333 			   && h->root.type == bfd_link_hash_undefweak))))
1334 	    {
1335 	      (*_bfd_error_handler)
1336 		(_("%B, section %A: relocation %s is"
1337 		   " not allowed for global symbol: `%s'"),
1338 		 input_bfd,
1339 		 input_section,
1340 		 cris_elf_howto_table[r_type].name,
1341 		 symname);
1342 	      bfd_set_error (bfd_error_bad_value);
1343 	      return FALSE;
1344 	    }
1345 
1346 	  /* This can happen if we get a link error with the input ELF
1347 	     variant mismatching the output variant.  Emit an error so
1348 	     it's noticed if it happens elsewhere.  */
1349 	  if (sgot == NULL)
1350 	    {
1351 	      (*_bfd_error_handler)
1352 		(_("%B, section %A: relocation %s with no GOT created"),
1353 		 input_bfd,
1354 		 input_section,
1355 		 cris_elf_howto_table[r_type].name);
1356 	      bfd_set_error (bfd_error_bad_value);
1357 	      return FALSE;
1358 	    }
1359 
1360 	  /* This relocation is like a PC-relative one, except the
1361 	     reference point is the location of GOT.  Note that
1362 	     sgot->output_offset is not involved in this calculation.  We
1363 	     always want the start of entire .got section, not the
1364 	     position after the reserved header.  */
1365 	  relocation -= sgot->output_section->vma;
1366 	  break;
1367 
1368 	case R_CRIS_32_PLT_PCREL:
1369 	  /* Relocation is to the entry for this symbol in the
1370 	     procedure linkage table.  */
1371 
1372 	  /* Resolve a PLT_PCREL reloc against a local symbol directly,
1373 	     without using the procedure linkage table.  */
1374 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1375 	    break;
1376 
1377 	  if (h->plt.offset == (bfd_vma) -1
1378 	      || splt == NULL)
1379 	    {
1380 	      /* We didn't make a PLT entry for this symbol.  This
1381 		 happens when statically linking PIC code, or when
1382 		 using -Bsymbolic.  */
1383 	      break;
1384 	    }
1385 
1386 	  relocation = (splt->output_section->vma
1387 			+ splt->output_offset
1388 			+ h->plt.offset);
1389 	  break;
1390 
1391 	case R_CRIS_32_PLT_GOTREL:
1392 	  /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1393 	     start of the .got section.  See also comment at
1394 	     R_CRIS_32_GOT.  */
1395 	  relocation -= sgot->output_section->vma;
1396 
1397 	  /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1398 	     without using the procedure linkage table.  */
1399 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1400 	    break;
1401 
1402 	  if (h->plt.offset == (bfd_vma) -1
1403 	      || splt == NULL)
1404 	    {
1405 	      /* We didn't make a PLT entry for this symbol.  This
1406 		 happens when statically linking PIC code, or when
1407 		 using -Bsymbolic.  */
1408 	      break;
1409 	    }
1410 
1411 	  relocation = (splt->output_section->vma
1412 			+ splt->output_offset
1413 			+ h->plt.offset
1414 			- sgot->output_section->vma);
1415 	  break;
1416 
1417 	case R_CRIS_8_PCREL:
1418 	case R_CRIS_16_PCREL:
1419 	case R_CRIS_32_PCREL:
1420 	  /* If the symbol was local, we need no shlib-specific handling.  */
1421 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1422 	      || h->dynindx == -1)
1423 	    break;
1424 
1425 	  /* Fall through.  */
1426 	case R_CRIS_8:
1427 	case R_CRIS_16:
1428 	case R_CRIS_32:
1429 	  if (info->shared
1430 	      && r_symndx != STN_UNDEF
1431 	      && (input_section->flags & SEC_ALLOC) != 0
1432 	      && ((r_type != R_CRIS_8_PCREL
1433 		   && r_type != R_CRIS_16_PCREL
1434 		   && r_type != R_CRIS_32_PCREL)
1435 		  || (!info->symbolic
1436 		      || (h != NULL && !h->def_regular))))
1437 	    {
1438 	      Elf_Internal_Rela outrel;
1439 	      bfd_byte *loc;
1440 	      bfd_boolean skip, relocate;
1441 
1442 	      /* When generating a shared object, these relocations
1443 		 are copied into the output file to be resolved at run
1444 		 time.  */
1445 
1446 	      if (sreloc == NULL)
1447 		{
1448 		  sreloc = _bfd_elf_get_dynamic_reloc_section
1449 		    (dynobj, input_section, /*rela?*/ TRUE);
1450 		  /* The section should have been created in cris_elf_check_relocs,
1451 		     but that function will not be called for objects which fail in
1452 		     cris_elf_merge_private_bfd_data.  */
1453 		  if (sreloc == NULL)
1454 		    {
1455 		      bfd_set_error (bfd_error_bad_value);
1456 		      return FALSE;
1457 		    }
1458 		}
1459 
1460 	      skip = FALSE;
1461 	      relocate = FALSE;
1462 
1463 	      outrel.r_offset =
1464 		_bfd_elf_section_offset (output_bfd, info, input_section,
1465 					 rel->r_offset);
1466 	      if (outrel.r_offset == (bfd_vma) -1)
1467 		skip = TRUE;
1468 	      else if (outrel.r_offset == (bfd_vma) -2
1469 		       /* For now, undefined weak symbols with non-default
1470 			  visibility (yielding 0), like exception info for
1471 			  discarded sections, will get a R_CRIS_NONE
1472 			  relocation rather than no relocation, because we
1473 			  notice too late that the symbol doesn't need a
1474 			  relocation.  */
1475 		       || (h != NULL
1476 			   && h->root.type == bfd_link_hash_undefweak
1477 			   && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))
1478 		skip = TRUE, relocate = TRUE;
1479 	      outrel.r_offset += (input_section->output_section->vma
1480 				  + input_section->output_offset);
1481 
1482 	      if (skip)
1483 		memset (&outrel, 0, sizeof outrel);
1484 	      /* h->dynindx may be -1 if the symbol was marked to
1485 		 become local.  */
1486 	      else if (h != NULL
1487 		       && ((! info->symbolic && h->dynindx != -1)
1488 			   || !h->def_regular))
1489 		{
1490 		  BFD_ASSERT (h->dynindx != -1);
1491 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1492 		  outrel.r_addend = relocation + rel->r_addend;
1493 		}
1494 	      else
1495 		{
1496 		  outrel.r_addend = relocation + rel->r_addend;
1497 
1498 		  if (r_type == R_CRIS_32)
1499 		    {
1500 		      relocate = TRUE;
1501 		      outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1502 		    }
1503 		  else
1504 		    {
1505 		      long indx;
1506 
1507 		      if (bfd_is_abs_section (sec))
1508 			indx = 0;
1509 		      else if (sec == NULL || sec->owner == NULL)
1510 			{
1511 			  bfd_set_error (bfd_error_bad_value);
1512 			  return FALSE;
1513 			}
1514 		      else
1515 			{
1516 			  asection *osec;
1517 
1518 			  /* We are turning this relocation into one
1519 			     against a section symbol.  It would be
1520 			     proper to subtract the symbol's value,
1521 			     osec->vma, from the emitted reloc addend,
1522 			     but ld.so expects buggy relocs.  */
1523 			  osec = sec->output_section;
1524 			  indx = elf_section_data (osec)->dynindx;
1525 			  if (indx == 0)
1526 			    {
1527 			      osec = htab->root.text_index_section;
1528 			      indx = elf_section_data (osec)->dynindx;
1529 			    }
1530 			  BFD_ASSERT (indx != 0);
1531 			}
1532 
1533 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
1534 		    }
1535 		}
1536 
1537 	      loc = sreloc->contents;
1538 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1539 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1540 
1541 	      /* This reloc will be computed at runtime, so there's no
1542                  need to do anything now, except for R_CRIS_32 relocations
1543                  that have been turned into R_CRIS_RELATIVE.  */
1544 	      if (!relocate)
1545 		continue;
1546 	    }
1547 
1548 	  break;
1549 
1550 	case R_CRIS_16_DTPREL:
1551 	case R_CRIS_32_DTPREL:
1552 	  /* This relocation must only be performed against local
1553 	     symbols, or to sections that are not loadable.  It's also
1554 	     ok when we link a program and the symbol is defined in an
1555 	     ordinary (non-DSO) object (if it's undefined there, we've
1556 	     already seen an error).  */
1557 	  if (h != NULL
1558 	      && (input_section->flags & SEC_ALLOC) != 0
1559 	      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1560 	      && (info->shared
1561 		  || (!h->def_regular
1562 		      && h->root.type != bfd_link_hash_undefined)))
1563 	    {
1564 	      (*_bfd_error_handler)
1565 		((h->root.type == bfd_link_hash_undefined)
1566 		 /* We shouldn't get here for GCC-emitted code.  */
1567 		 ? _("%B, section %A: relocation %s has an undefined"
1568 		     " reference to `%s', perhaps a declaration mixup?")
1569 		 : ("%B, section %A: relocation %s is"
1570 		     " not allowed for `%s', a global symbol with default"
1571 		     " visibility, perhaps a declaration mixup?"),
1572 		 input_bfd,
1573 		 input_section,
1574 		 cris_elf_howto_table[r_type].name,
1575 		 symname != NULL && symname[0] != '\0'
1576 		 ? symname : _("[whose name is lost]"));
1577 	      bfd_set_error (bfd_error_bad_value);
1578 	      return FALSE;
1579 	    }
1580 
1581 	  BFD_ASSERT ((input_section->flags & SEC_ALLOC) == 0
1582 		      || htab->dtpmod_refcount != 0);
1583 
1584 	  /* Fill in a R_CRIS_DTPMOD reloc at offset 3 if we haven't
1585 	     already done so.  Note that we do this in .got.plt, not
1586 	     in .got, as .got.plt contains the first part, still the
1587 	     reloc is against .got, because the linker script directs
1588 	     (is required to direct) them both into .got.  */
1589 	  if (htab->dtpmod_refcount > 0
1590 	      && (input_section->flags & SEC_ALLOC) != 0)
1591 	    {
1592 	      asection *sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
1593 	      BFD_ASSERT (sgotplt != NULL);
1594 
1595 	      if (info->shared)
1596 		{
1597 		  Elf_Internal_Rela outrel;
1598 		  bfd_byte *loc;
1599 
1600 		  if (srelgot == NULL)
1601 		    srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1602 		  BFD_ASSERT (srelgot != NULL);
1603 		  loc = srelgot->contents;
1604 		  loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1605 
1606 		  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 12);
1607 		  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
1608 		  outrel.r_offset = (sgotplt->output_section->vma
1609 				     + sgotplt->output_offset
1610 				     + 12);
1611 		  outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTPMOD);
1612 		  outrel.r_addend = 0;
1613 		  bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1614 		}
1615 	      else
1616 		{
1617 		  /* For an executable, the GOT entry contents is known.  */
1618 		  bfd_put_32 (output_bfd, (bfd_vma) 1, sgotplt->contents + 12);
1619 		  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
1620 		}
1621 
1622 	      /* Reverse the sign to mark that we've emitted the
1623 		 required GOT entry.  */
1624 	      htab->dtpmod_refcount = - htab->dtpmod_refcount;
1625 	    }
1626 
1627 	  /* The relocation is the offset from the start of the module
1628 	     TLS block to the (local) symbol.  */
1629 	  relocation -= elf_hash_table (info)->tls_sec == NULL
1630 	    ? 0 : elf_hash_table (info)->tls_sec->vma;
1631 	  break;
1632 
1633 	case R_CRIS_32_GD:
1634 	  if (info->shared)
1635 	    {
1636 	      bfd_set_error (bfd_error_invalid_operation);
1637 
1638 	      /* We've already informed in cris_elf_check_relocs that
1639 		 this is an error.  */
1640 	      return FALSE;
1641 	    }
1642 	  /* Fall through.  */
1643 
1644 	case R_CRIS_16_GOT_GD:
1645 	case R_CRIS_32_GOT_GD:
1646 	  if (rel->r_addend != 0)
1647 	    {
1648 	      /* We can't do anything for a relocation which is against a
1649 		 symbol *plus offset*.  The GOT holds relocations for
1650 		 symbols.  Make this an error; the compiler isn't allowed
1651 		 to pass us these kinds of things.  */
1652 	      (*_bfd_error_handler)
1653 		(_("%B, section %A: relocation %s with non-zero addend %d"
1654 		   " against symbol `%s'"),
1655 		 input_bfd,
1656 		 input_section,
1657 		 cris_elf_howto_table[r_type].name,
1658 		 rel->r_addend,
1659 		 symname[0] != '\0' ? symname : _("[whose name is lost]"));
1660 
1661 	      bfd_set_error (bfd_error_bad_value);
1662 	      return FALSE;
1663 	    }
1664 
1665 	  if (!info->shared
1666 	      && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
1667 	    {
1668 	      /* Known contents of the GOT.  */
1669 	      bfd_vma off;
1670 
1671 	      /* The symbol is defined in the program, so just write
1672 		 (1, known_tpoffset) into the GOT.  */
1673 	      relocation -= elf_hash_table (info)->tls_sec->vma;
1674 
1675 	      if (h != NULL)
1676 		{
1677 		  off = elf_cris_hash_entry (h)->tprel_refcount > 0
1678 		    ? h->got.offset + 4 : h->got.offset;
1679 		}
1680 	      else
1681 		{
1682 		  off = local_got_offsets[r_symndx];
1683 		  if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
1684 		    off += 4;
1685 		}
1686 
1687 	      /* We use bit 1 of the offset as a flag for GOT entry with
1688 		 the R_CRIS_DTP reloc, setting it when we've emitted the
1689 		 GOT entry and reloc.  Bit 0 is used for R_CRIS_32_TPREL
1690 		 relocs.  */
1691 	      if ((off & 2) == 0)
1692 		{
1693 		  off &= ~3;
1694 
1695 		  if (h != NULL)
1696 		    h->got.offset |= 2;
1697 		  else
1698 		    local_got_offsets[r_symndx] |= 2;
1699 
1700 		  bfd_put_32 (output_bfd, 1, sgot->contents + off);
1701 		  bfd_put_32 (output_bfd, relocation, sgot->contents + off + 4);
1702 		}
1703 	      else
1704 		off &= ~3;
1705 
1706 	      relocation = sgot->output_offset + off
1707 		+ (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
1708 	    }
1709 	  else
1710 	    {
1711 	      /* Not all parts of the GOT entry are known; emit a real
1712 		 relocation.  */
1713 	      bfd_vma off;
1714 
1715 	      if (h != NULL)
1716 		off = elf_cris_hash_entry (h)->tprel_refcount > 0
1717 		  ? h->got.offset + 4 : h->got.offset;
1718 	      else
1719 		{
1720 		  off = local_got_offsets[r_symndx];
1721 		  if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
1722 		    off += 4;
1723 		}
1724 
1725 	      /* See above re bit 1 and bit 0 usage.  */
1726 	      if ((off & 2) == 0)
1727 		{
1728 		  Elf_Internal_Rela outrel;
1729 		  bfd_byte *loc;
1730 
1731 		  off &= ~3;
1732 
1733 		  if (h != NULL)
1734 		    h->got.offset |= 2;
1735 		  else
1736 		    local_got_offsets[r_symndx] |= 2;
1737 
1738 		  /* Clear the target contents of the GOT (just as a
1739 		     gesture; it's already cleared on allocation): this
1740 		     relocation is not like the other dynrelocs.  */
1741 		  bfd_put_32 (output_bfd, 0, sgot->contents + off);
1742 		  bfd_put_32 (output_bfd, 0, sgot->contents + off + 4);
1743 
1744 		  if (srelgot == NULL)
1745 		    srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1746 		  BFD_ASSERT (srelgot != NULL);
1747 
1748 		  if (h != NULL && h->dynindx != -1)
1749 		    {
1750 		      outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_DTP);
1751 		      relocation = 0;
1752 		    }
1753 		  else
1754 		    {
1755 		      outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTP);
1756 
1757 		      /* NULL if we had an error.  */
1758 		      relocation -= elf_hash_table (info)->tls_sec == NULL
1759 			? 0 : elf_hash_table (info)->tls_sec->vma;
1760 		    }
1761 
1762 		  outrel.r_offset = (sgot->output_section->vma
1763 				     + sgot->output_offset
1764 				     + off);
1765 		  outrel.r_addend = relocation;
1766 		  loc = srelgot->contents;
1767 		  loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1768 
1769 		  /* NULL if we had an error.  */
1770 		  if (srelgot->contents != NULL)
1771 		    bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1772 		}
1773 	      else
1774 		off &= ~3;
1775 
1776 	      relocation = sgot->output_offset + off
1777 		+ (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
1778 	    }
1779 
1780 	  /* The GOT-relative offset to the GOT entry is the
1781 	     relocation, or for R_CRIS_32_GD, the actual address of
1782 	     the GOT entry.  */
1783 	  break;
1784 
1785 	case R_CRIS_32_IE:
1786 	  if (info->shared)
1787 	    {
1788 	      bfd_set_error (bfd_error_invalid_operation);
1789 
1790 	      /* We've already informed in cris_elf_check_relocs that
1791 		 this is an error.  */
1792 	      return FALSE;
1793 	    }
1794 	  /* Fall through.  */
1795 
1796 	case R_CRIS_32_GOT_TPREL:
1797 	case R_CRIS_16_GOT_TPREL:
1798 	  if (rel->r_addend != 0)
1799 	    {
1800 	      /* We can't do anything for a relocation which is
1801 		 against a symbol *plus offset*.  GOT holds
1802 		 relocations for symbols.  Make this an error; the
1803 		 compiler isn't allowed to pass us these kinds of
1804 		 things.  */
1805 	      (*_bfd_error_handler)
1806 		(_("%B, section %A: relocation %s with non-zero addend %d"
1807 		   " against symbol `%s'"),
1808 		 input_bfd,
1809 		 input_section,
1810 		 cris_elf_howto_table[r_type].name,
1811 		 rel->r_addend,
1812 		 symname[0] != '\0' ? symname : _("[whose name is lost]"));
1813 	      bfd_set_error (bfd_error_bad_value);
1814 	      return FALSE;
1815 	    }
1816 
1817 	  if (!info->shared
1818 	      && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
1819 	    {
1820 	      /* Known contents of the GOT.  */
1821 	      bfd_vma off;
1822 
1823 	      /* The symbol is defined in the program, so just write
1824 		 the -prog_tls_size+known_tpoffset into the GOT.  */
1825 	      relocation -= elf_hash_table (info)->tls_sec->vma;
1826 	      relocation -= elf_hash_table (info)->tls_size;
1827 
1828 	      if (h != NULL)
1829 		off = h->got.offset;
1830 	      else
1831 		off = local_got_offsets[r_symndx];
1832 
1833 	      /* Bit 0 is used to mark whether we've emitted the required
1834 		 entry (and if needed R_CRIS_32_TPREL reloc).  Bit 1
1835 		 is used similarly for R_CRIS_DTP, see above.  */
1836 	      if ((off & 1) == 0)
1837 		{
1838 		  off &= ~3;
1839 
1840 		  if (h != NULL)
1841 		    h->got.offset |= 1;
1842 		  else
1843 		    local_got_offsets[r_symndx] |= 1;
1844 
1845 		  bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1846 		}
1847 	      else
1848 		off &= ~3;
1849 
1850 	      relocation = sgot->output_offset + off
1851 		+ (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
1852 	    }
1853 	  else
1854 	    {
1855 	      /* Emit a real relocation.  */
1856 	      bfd_vma off;
1857 
1858 	      if (h != NULL)
1859 		off = h->got.offset;
1860 	      else
1861 		off = local_got_offsets[r_symndx];
1862 
1863 	      /* See above re usage of bit 0 and 1.  */
1864 	      if ((off & 1) == 0)
1865 		{
1866 		  Elf_Internal_Rela outrel;
1867 		  bfd_byte *loc;
1868 
1869 		  off &= ~3;
1870 
1871 		  if (h != NULL)
1872 		    h->got.offset |= 1;
1873 		  else
1874 		    local_got_offsets[r_symndx] |= 1;
1875 
1876 		  if (srelgot == NULL)
1877 		    srelgot = bfd_get_linker_section (dynobj, ".rela.got");
1878 		  BFD_ASSERT (srelgot != NULL);
1879 
1880 		  if (h != NULL && h->dynindx != -1)
1881 		    {
1882 		      outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_32_TPREL);
1883 		      relocation = 0;
1884 		    }
1885 		  else
1886 		    {
1887 		      outrel.r_info = ELF32_R_INFO (0, R_CRIS_32_TPREL);
1888 
1889 		      /* NULL if we had an error.  */
1890 		      relocation -= elf_hash_table (info)->tls_sec == NULL
1891 			? 0 : elf_hash_table (info)->tls_sec->vma;
1892 		    }
1893 
1894 		  /* Just "define" the initial contents in some
1895 		     semi-logical way.  */
1896 		  bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1897 
1898 		  outrel.r_offset = (sgot->output_section->vma
1899 				     + sgot->output_offset
1900 				     + off);
1901 		  outrel.r_addend = relocation;
1902 		  loc = srelgot->contents;
1903 		  loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1904 		  /* NULL if we had an error.  */
1905 		  if (srelgot->contents != NULL)
1906 		    bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1907 		}
1908 	      else
1909 		off &= ~3;
1910 
1911 	      relocation = sgot->output_offset + off
1912 		+ (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
1913 	    }
1914 
1915 	  /* The GOT-relative offset to the GOT entry is the relocation,
1916 	     or for R_CRIS_32_GD, the actual address of the GOT entry.  */
1917 	  break;
1918 
1919 	case R_CRIS_16_TPREL:
1920 	case R_CRIS_32_TPREL:
1921 	  /* This relocation must only be performed against symbols
1922 	     defined in an ordinary (non-DSO) object.  */
1923 	  if (info->shared)
1924 	    {
1925 	      bfd_set_error (bfd_error_invalid_operation);
1926 
1927 	      /* We've already informed in cris_elf_check_relocs that
1928 		 this is an error.  */
1929 	      return FALSE;
1930 	    }
1931 
1932 	  if (h != NULL
1933 	      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1934 	      && !(h->def_regular || ELF_COMMON_DEF_P (h))
1935 	      /* If it's undefined, then an error message has already
1936 		 been emitted.  */
1937 	      && h->root.type != bfd_link_hash_undefined)
1938 	    {
1939 	      (*_bfd_error_handler)
1940 		(_("%B, section %A: relocation %s is"
1941 		   " not allowed for symbol: `%s'"
1942 		   " which is defined outside the program,"
1943 		   " perhaps a declaration mixup?"),
1944 		 input_bfd,
1945 		 input_section,
1946 		 cris_elf_howto_table[r_type].name,
1947 		 symname);
1948 	      bfd_set_error (bfd_error_bad_value);
1949 	      return FALSE;
1950 	    }
1951 
1952 	  /* NULL if we had an error.  */
1953 	  relocation -= elf_hash_table (info)->tls_sec == NULL
1954 	    ? 0
1955 	    : (elf_hash_table (info)->tls_sec->vma
1956 	       + elf_hash_table (info)->tls_size);
1957 
1958 	  /* The TLS-relative offset is the relocation.  */
1959 	  break;
1960 
1961 	default:
1962 	  BFD_FAIL ();
1963 	  return FALSE;
1964 	}
1965 
1966       r = cris_final_link_relocate (howto, input_bfd, input_section,
1967 				     contents, rel, relocation);
1968 
1969       if (r != bfd_reloc_ok)
1970 	{
1971 	  const char * msg = (const char *) NULL;
1972 
1973 	  switch (r)
1974 	    {
1975 	    case bfd_reloc_overflow:
1976 	      r = info->callbacks->reloc_overflow
1977 		(info, (h ? &h->root : NULL), symname, howto->name,
1978 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1979 	      if (additional_relocation_error_msg_count > 0)
1980 		{
1981 		  additional_relocation_error_msg_count--;
1982 		  switch (r_type)
1983 		    {
1984 		    case R_CRIS_16_GOTPLT:
1985 		    case R_CRIS_16_GOT:
1986 
1987 		      /* Not just TLS is involved here, so we make
1988 			 generation and message depend on -fPIC/-fpic
1989 			 only.  */
1990 		    case R_CRIS_16_GOT_TPREL:
1991 		    case R_CRIS_16_GOT_GD:
1992 		      (*_bfd_error_handler)
1993 			(_("(too many global variables for -fpic:"
1994 			   " recompile with -fPIC)"));
1995 		      break;
1996 
1997 		    case R_CRIS_16_TPREL:
1998 		    case R_CRIS_16_DTPREL:
1999 		      (*_bfd_error_handler)
2000 			(_("(thread-local data too big for -fpic or"
2001 			   " -msmall-tls: recompile with -fPIC or"
2002 			   " -mno-small-tls)"));
2003 		      break;
2004 
2005 		      /* No known cause for overflow for other relocs.  */
2006 		    default:
2007 		      break;
2008 		    }
2009 		}
2010 	      break;
2011 
2012 	    case bfd_reloc_undefined:
2013 	      r = info->callbacks->undefined_symbol
2014 		(info, symname, input_bfd, input_section, rel->r_offset,
2015 		 TRUE);
2016 	      break;
2017 
2018 	    case bfd_reloc_outofrange:
2019 	      msg = _("internal error: out of range error");
2020 	      break;
2021 
2022 	    case bfd_reloc_notsupported:
2023 	      msg = _("internal error: unsupported relocation error");
2024 	      break;
2025 
2026 	    case bfd_reloc_dangerous:
2027 	      msg = _("internal error: dangerous relocation");
2028 	      break;
2029 
2030 	    default:
2031 	      msg = _("internal error: unknown error");
2032 	      break;
2033 	    }
2034 
2035 	  if (msg)
2036 	    r = info->callbacks->warning
2037 	      (info, msg, symname, input_bfd, input_section, rel->r_offset);
2038 
2039 	  if (! r)
2040 	    return FALSE;
2041 	}
2042     }
2043 
2044   return TRUE;
2045 }
2046 
2047 /* Finish up dynamic symbol handling.  We set the contents of various
2048    dynamic sections here.  */
2049 
2050 static bfd_boolean
2051 elf_cris_finish_dynamic_symbol (bfd *output_bfd,
2052 				struct bfd_link_info *info,
2053 				struct elf_link_hash_entry *h,
2054 				Elf_Internal_Sym *sym)
2055 {
2056   struct elf_cris_link_hash_table * htab;
2057   bfd *dynobj;
2058 
2059   /* Where in the plt entry to put values.  */
2060   int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
2061 
2062   /* What offset to add to the distance to the first PLT entry for the
2063      value at plt_off3.   */
2064   int plt_off3_value_bias = 4;
2065 
2066   /* Where in the PLT entry the call-dynlink-stub is (happens to be same
2067      for PIC and non-PIC for v32 and pre-v32).  */
2068   int plt_stub_offset = 8;
2069   int plt_entry_size = PLT_ENTRY_SIZE;
2070   const bfd_byte *plt_entry = elf_cris_plt_entry;
2071   const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
2072 
2073   htab = elf_cris_hash_table (info);
2074   if (htab == NULL)
2075     return FALSE;
2076 
2077   /* Adjust the various PLT entry offsets.  */
2078   if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
2079     {
2080       plt_off2 = 14;
2081       plt_off3 = 20;
2082       plt_off3_value_bias = -2;
2083       plt_stub_offset = 12;
2084       plt_entry_size = PLT_ENTRY_SIZE_V32;
2085       plt_entry = elf_cris_plt_entry_v32;
2086       plt_pic_entry = elf_cris_pic_plt_entry_v32;
2087     }
2088 
2089   dynobj = elf_hash_table (info)->dynobj;
2090 
2091   if (h->plt.offset != (bfd_vma) -1)
2092     {
2093       asection *splt;
2094       asection *sgotplt;
2095       asection *srela;
2096       bfd_vma got_base;
2097 
2098       bfd_vma gotplt_offset
2099 	= elf_cris_hash_entry (h)->gotplt_offset;
2100       Elf_Internal_Rela rela;
2101       bfd_byte *loc;
2102       bfd_boolean has_gotplt = gotplt_offset != 0;
2103 
2104       /* Get the index in the .rela.plt relocations for the .got.plt
2105 	 entry that corresponds to this symbol.
2106 	 We have to count backwards here, and the result is only valid
2107 	 as an index into .rela.plt.  We also have to undo the effect
2108 	 of the R_CRIS_DTPMOD entry at .got index 3 (offset 12 into
2109 	 .got.plt) for which gotplt_offset is adjusted, because while
2110 	 that entry goes into .got.plt, its relocation goes into
2111 	 .rela.got, not .rela.plt.  (It's not PLT-specific; not to be
2112 	 processed as part of the runtime lazy .rela.plt relocation).
2113 	 FIXME: There be literal constants here...  */
2114       bfd_vma rela_plt_index
2115 	= (htab->dtpmod_refcount != 0
2116 	   ? gotplt_offset/4 - 2 - 3 : gotplt_offset/4 - 3);
2117 
2118       /* Get the offset into the .got table of the entry that corresponds
2119 	 to this function.  Note that we embed knowledge that "incoming"
2120 	 .got goes after .got.plt in the output without padding (pointer
2121 	 aligned).  However, that knowledge is present in several other
2122 	 places too.  */
2123       bfd_vma got_offset
2124 	= (has_gotplt
2125 	   ? gotplt_offset
2126 	   : h->got.offset + htab->next_gotplt_entry);
2127 
2128       /* This symbol has an entry in the procedure linkage table.  Set it
2129 	 up.  */
2130 
2131       BFD_ASSERT (h->dynindx != -1);
2132 
2133       splt = bfd_get_linker_section (dynobj, ".plt");
2134       sgotplt = bfd_get_linker_section (dynobj, ".got.plt");
2135       srela = bfd_get_linker_section (dynobj, ".rela.plt");
2136       BFD_ASSERT (splt != NULL && sgotplt != NULL
2137 		  && (! has_gotplt || srela != NULL));
2138 
2139       got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2140 
2141       /* Fill in the entry in the procedure linkage table.  */
2142       if (! info->shared)
2143 	{
2144 	  memcpy (splt->contents + h->plt.offset, plt_entry,
2145 		  plt_entry_size);
2146 
2147 	  /* We need to enter the absolute address of the GOT entry here.  */
2148 	  bfd_put_32 (output_bfd, got_base + got_offset,
2149 		      splt->contents + h->plt.offset + plt_off1);
2150 	}
2151       else
2152 	{
2153 	  memcpy (splt->contents + h->plt.offset, plt_pic_entry,
2154 		  plt_entry_size);
2155 	  bfd_put_32 (output_bfd, got_offset,
2156 		      splt->contents + h->plt.offset + plt_off1);
2157 	}
2158 
2159       /* Fill in the plt entry and make a relocation, if this is a "real"
2160 	 PLT entry.  */
2161       if (has_gotplt)
2162 	{
2163 	  /* Fill in the offset to the reloc table.  */
2164 	  bfd_put_32 (output_bfd,
2165 		      rela_plt_index * sizeof (Elf32_External_Rela),
2166 		      splt->contents + h->plt.offset + plt_off2);
2167 
2168 	  /* Fill in the offset to the first PLT entry, where to "jump".  */
2169 	  bfd_put_32 (output_bfd,
2170 		      - (h->plt.offset + plt_off3 + plt_off3_value_bias),
2171 		      splt->contents + h->plt.offset + plt_off3);
2172 
2173 	  /* Fill in the entry in the global offset table with the address of
2174 	     the relocating stub.  */
2175 	  bfd_put_32 (output_bfd,
2176 		      (splt->output_section->vma
2177 		       + splt->output_offset
2178 		       + h->plt.offset
2179 		       + plt_stub_offset),
2180 		      sgotplt->contents + got_offset);
2181 
2182 	  /* Fill in the entry in the .rela.plt section.  */
2183 	  rela.r_offset = (sgotplt->output_section->vma
2184 			   + sgotplt->output_offset
2185 			   + got_offset);
2186 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
2187 	  rela.r_addend = 0;
2188 	  loc = srela->contents + rela_plt_index * sizeof (Elf32_External_Rela);
2189 	  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2190 	}
2191 
2192       if (!h->def_regular)
2193 	{
2194 	  /* Mark the symbol as undefined, rather than as defined in
2195 	     the .plt section.  Leave the value alone.  */
2196 	  sym->st_shndx = SHN_UNDEF;
2197 
2198 	  /* FIXME: From elf32-sparc.c 2001-02-19 (1.18).  I still don't
2199 	     know whether resetting the value is significant; if it really
2200 	     is, rather than a quirk or bug in the sparc port, then I
2201 	     believe we'd see this elsewhere.  */
2202 	  /* If the symbol is weak, we do need to clear the value.
2203 	     Otherwise, the PLT entry would provide a definition for
2204 	     the symbol even if the symbol wasn't defined anywhere,
2205 	     and so the symbol would never be NULL.  */
2206 	  if (!h->ref_regular_nonweak)
2207 	    sym->st_value = 0;
2208 	}
2209     }
2210 
2211   /* For an ordinary program, we emit .got relocs only for symbols that
2212      are in the dynamic-symbols table and are either defined by the
2213      program or are undefined weak symbols, or are function symbols
2214      where we do not output a PLT: the PLT reloc was output above and all
2215      references to the function symbol are redirected to the PLT.  */
2216   if (h->got.offset != (bfd_vma) -1
2217       && (elf_cris_hash_entry (h)->reg_got_refcount > 0)
2218       && (info->shared
2219 	  || (h->dynindx != -1
2220 	      && h->plt.offset == (bfd_vma) -1
2221 	      && !h->def_regular
2222 	      && h->root.type != bfd_link_hash_undefweak)))
2223     {
2224       asection *sgot;
2225       asection *srela;
2226       Elf_Internal_Rela rela;
2227       bfd_byte *loc;
2228       bfd_byte *where;
2229 
2230       /* This symbol has an entry in the global offset table.  Set it up.  */
2231 
2232       sgot = bfd_get_linker_section (dynobj, ".got");
2233       srela = bfd_get_linker_section (dynobj, ".rela.got");
2234       BFD_ASSERT (sgot != NULL && srela != NULL);
2235 
2236       rela.r_offset = (sgot->output_section->vma
2237 		       + sgot->output_offset
2238 		       + (h->got.offset &~ (bfd_vma) 1));
2239 
2240       /* If this is a static link, or it is a -Bsymbolic link and the
2241 	 symbol is defined locally or was forced to be local because
2242 	 of a version file, we just want to emit a RELATIVE reloc.
2243 	 The entry in the global offset table will already have been
2244 	 initialized in the relocate_section function.  */
2245       where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
2246       if (! elf_hash_table (info)->dynamic_sections_created
2247 	  || (info->shared
2248 	      && (info->symbolic || h->dynindx == -1)
2249 	      && h->def_regular))
2250 	{
2251 	  rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
2252 	  rela.r_addend = bfd_get_signed_32 (output_bfd, where);
2253 	}
2254       else
2255 	{
2256 	  bfd_put_32 (output_bfd, (bfd_vma) 0, where);
2257 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
2258 	  rela.r_addend = 0;
2259 	}
2260 
2261       loc = srela->contents;
2262       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2263       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2264     }
2265 
2266   if (h->needs_copy)
2267     {
2268       asection *s;
2269       Elf_Internal_Rela rela;
2270       bfd_byte *loc;
2271 
2272       /* This symbol needs a copy reloc.  Set it up.  */
2273 
2274       BFD_ASSERT (h->dynindx != -1
2275 		  && (h->root.type == bfd_link_hash_defined
2276 		      || h->root.type == bfd_link_hash_defweak));
2277 
2278       s = bfd_get_linker_section (dynobj, ".rela.bss");
2279       BFD_ASSERT (s != NULL);
2280 
2281       rela.r_offset = (h->root.u.def.value
2282 		       + h->root.u.def.section->output_section->vma
2283 		       + h->root.u.def.section->output_offset);
2284       rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
2285       rela.r_addend = 0;
2286       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2287       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2288     }
2289 
2290   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2291   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2292       || h == elf_hash_table (info)->hgot)
2293     sym->st_shndx = SHN_ABS;
2294 
2295   return TRUE;
2296 }
2297 
2298 /* Finish up the dynamic sections.  Do *not* emit relocs here, as their
2299    offsets were changed, as part of -z combreloc handling, from those we
2300    computed.  */
2301 
2302 static bfd_boolean
2303 elf_cris_finish_dynamic_sections (bfd *output_bfd,
2304 				  struct bfd_link_info *info)
2305 {
2306   bfd *dynobj;
2307   asection *sgot;
2308   asection *sdyn;
2309 
2310   dynobj = elf_hash_table (info)->dynobj;
2311 
2312   sgot = bfd_get_linker_section (dynobj, ".got.plt");
2313   BFD_ASSERT (sgot != NULL);
2314   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2315 
2316   if (elf_hash_table (info)->dynamic_sections_created)
2317     {
2318       asection *splt;
2319       Elf32_External_Dyn *dyncon, *dynconend;
2320 
2321       splt = bfd_get_linker_section (dynobj, ".plt");
2322       BFD_ASSERT (splt != NULL && sdyn != NULL);
2323 
2324       dyncon = (Elf32_External_Dyn *) sdyn->contents;
2325       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2326       for (; dyncon < dynconend; dyncon++)
2327 	{
2328 	  Elf_Internal_Dyn dyn;
2329 	  asection *s;
2330 
2331 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2332 
2333 	  switch (dyn.d_tag)
2334 	    {
2335 	    default:
2336 	      break;
2337 
2338 	    case DT_PLTGOT:
2339 	      s = bfd_get_section_by_name (output_bfd, ".got");
2340 	      BFD_ASSERT (s != NULL);
2341 	      dyn.d_un.d_ptr = s->vma;
2342 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2343 	      break;
2344 
2345 	    case DT_JMPREL:
2346 	      /* Yes, we *can* have a .plt and no .plt.rela, for instance
2347 		 if all symbols are found in the .got (not .got.plt).  */
2348 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2349 	      dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
2350 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2351 	      break;
2352 
2353 	    case DT_PLTRELSZ:
2354 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2355 	      if (s == NULL)
2356 		dyn.d_un.d_val = 0;
2357 	      else
2358 		dyn.d_un.d_val = s->size;
2359 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2360 	      break;
2361 
2362 	    case DT_RELASZ:
2363 	      /* The procedure linkage table relocs (DT_JMPREL) should
2364 		 not be included in the overall relocs (DT_RELA).
2365 		 Therefore, we override the DT_RELASZ entry here to
2366 		 make it not include the JMPREL relocs.  Since the
2367 		 linker script arranges for .rela.plt to follow all
2368 		 other relocation sections, we don't have to worry
2369 		 about changing the DT_RELA entry.  */
2370 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2371 	      if (s != NULL)
2372 		dyn.d_un.d_val -= s->size;
2373 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2374 	      break;
2375 	    }
2376 	}
2377 
2378       /* Fill in the first entry in the procedure linkage table.  */
2379       if (splt->size > 0)
2380 	{
2381 	  if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
2382 	    {
2383 	      if (info->shared)
2384 		memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
2385 			PLT_ENTRY_SIZE_V32);
2386 	      else
2387 		{
2388 		  memcpy (splt->contents, elf_cris_plt0_entry_v32,
2389 			  PLT_ENTRY_SIZE_V32);
2390 		  bfd_put_32 (output_bfd,
2391 			      sgot->output_section->vma
2392 			      + sgot->output_offset + 4,
2393 			      splt->contents + 4);
2394 
2395 		  elf_section_data (splt->output_section)->this_hdr.sh_entsize
2396 		    = PLT_ENTRY_SIZE_V32;
2397 		}
2398 	    }
2399 	  else
2400 	    {
2401 	      if (info->shared)
2402 		memcpy (splt->contents, elf_cris_pic_plt0_entry,
2403 			PLT_ENTRY_SIZE);
2404 	      else
2405 		{
2406 		  memcpy (splt->contents, elf_cris_plt0_entry,
2407 			  PLT_ENTRY_SIZE);
2408 		  bfd_put_32 (output_bfd,
2409 			      sgot->output_section->vma
2410 			      + sgot->output_offset + 4,
2411 			      splt->contents + 6);
2412 		  bfd_put_32 (output_bfd,
2413 			      sgot->output_section->vma
2414 			      + sgot->output_offset + 8,
2415 			      splt->contents + 14);
2416 
2417 		  elf_section_data (splt->output_section)->this_hdr.sh_entsize
2418 		    = PLT_ENTRY_SIZE;
2419 		}
2420             }
2421 	}
2422     }
2423 
2424   /* Fill in the first three entries in the global offset table.  */
2425   if (sgot->size > 0)
2426     {
2427       if (sdyn == NULL)
2428 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2429       else
2430 	bfd_put_32 (output_bfd,
2431 		    sdyn->output_section->vma + sdyn->output_offset,
2432 		    sgot->contents);
2433       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2434       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2435     }
2436 
2437   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2438 
2439   return TRUE;
2440 }
2441 
2442 /* Return the section that should be marked against GC for a given
2443    relocation.  */
2444 
2445 static asection *
2446 cris_elf_gc_mark_hook (asection *sec,
2447 		       struct bfd_link_info *info,
2448 		       Elf_Internal_Rela *rel,
2449 		       struct elf_link_hash_entry *h,
2450 		       Elf_Internal_Sym *sym)
2451 {
2452   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
2453   if (h != NULL)
2454     switch (r_type)
2455       {
2456       case R_CRIS_GNU_VTINHERIT:
2457       case R_CRIS_GNU_VTENTRY:
2458 	return NULL;
2459 
2460       default:
2461 	break;
2462       }
2463 
2464   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2465 }
2466 
2467 /* Update the got entry reference counts for the section being removed.  */
2468 
2469 static bfd_boolean
2470 cris_elf_gc_sweep_hook (bfd *abfd,
2471 			struct bfd_link_info *info,
2472 			asection *sec,
2473 			const Elf_Internal_Rela *relocs)
2474 {
2475   struct elf_cris_link_hash_table * htab;
2476   Elf_Internal_Shdr *symtab_hdr;
2477   struct elf_link_hash_entry **sym_hashes;
2478   bfd_signed_vma *local_got_refcounts;
2479   const Elf_Internal_Rela *rel, *relend;
2480   bfd *dynobj;
2481   asection *sgot;
2482   asection *srelgot;
2483 
2484   if (info->relocatable)
2485     return TRUE;
2486 
2487   dynobj = elf_hash_table (info)->dynobj;
2488   if (dynobj == NULL)
2489     return TRUE;
2490 
2491   htab = elf_cris_hash_table (info);
2492   if (htab == NULL)
2493     return FALSE;
2494 
2495   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2496   sym_hashes = elf_sym_hashes (abfd);
2497   local_got_refcounts = elf_local_got_refcounts (abfd);
2498 
2499   sgot = bfd_get_linker_section (dynobj, ".got");
2500   srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2501 
2502   relend = relocs + sec->reloc_count;
2503   for (rel = relocs; rel < relend; rel++)
2504     {
2505       unsigned long r_symndx;
2506       struct elf_link_hash_entry *h = NULL;
2507       bfd_signed_vma got_element_size = 4;
2508       bfd_signed_vma *specific_refcount = NULL;
2509       enum elf_cris_reloc_type r_type;
2510 
2511       r_symndx = ELF32_R_SYM (rel->r_info);
2512       if (r_symndx >= symtab_hdr->sh_info)
2513 	{
2514 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2515 	  while (h->root.type == bfd_link_hash_indirect
2516 		 || h->root.type == bfd_link_hash_warning)
2517 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2518 	}
2519 
2520       r_type = ELF32_R_TYPE (rel->r_info);
2521       switch (r_type)
2522 	{
2523 	case R_CRIS_32_GOT:
2524 	case R_CRIS_16_GOT:
2525 	case R_CRIS_16_GOTPLT:
2526 	case R_CRIS_32_GOTPLT:
2527 	  specific_refcount = h != NULL
2528 	    ? &((struct elf_cris_link_hash_entry *) h)->reg_got_refcount
2529 	    : &local_got_refcounts[LGOT_REG_NDX (r_symndx)];
2530 	  break;
2531 
2532 	case R_CRIS_32_GD:
2533 	case R_CRIS_32_GOT_GD:
2534 	case R_CRIS_16_GOT_GD:
2535 	  got_element_size = 8;
2536 	  specific_refcount = h != NULL
2537 	    ? &((struct elf_cris_link_hash_entry *) h)->dtp_refcount
2538 	    : &local_got_refcounts[LGOT_DTP_NDX (r_symndx)];
2539 	  break;
2540 
2541 	case R_CRIS_32_IE:
2542 	case R_CRIS_16_GOT_TPREL:
2543 	case R_CRIS_32_GOT_TPREL:
2544 	  specific_refcount = h != NULL
2545 	    ? &((struct elf_cris_link_hash_entry *) h)->tprel_refcount
2546 	    : &local_got_refcounts[LGOT_TPREL_NDX (r_symndx)];
2547 	  break;
2548 
2549 	default:
2550 	  break;
2551 	}
2552 
2553       switch (r_type)
2554 	{
2555 	case R_CRIS_32_IE:
2556 	case R_CRIS_32_GD:
2557 	case R_CRIS_16_GOT_TPREL:
2558 	case R_CRIS_32_GOT_TPREL:
2559 	case R_CRIS_32_GOT_GD:
2560 	case R_CRIS_16_GOT_GD:
2561 	case R_CRIS_16_GOT:
2562 	case R_CRIS_32_GOT:
2563 	  if (h != NULL)
2564 	    {
2565 	      /* If the counters are 0 when we got here, we've
2566 		 miscounted somehow somewhere, an internal error.  */
2567 	      BFD_ASSERT (h->got.refcount > 0);
2568 	      --h->got.refcount;
2569 
2570 	      BFD_ASSERT (*specific_refcount > 0);
2571 	      --*specific_refcount;
2572 	      if (*specific_refcount == 0)
2573 		{
2574 		  /* We don't need the .got entry any more.  */
2575 		  sgot->size -= got_element_size;
2576 		  srelgot->size -= sizeof (Elf32_External_Rela);
2577 		}
2578 	      break;
2579 	    }
2580 
2581 	local_got_reloc:
2582 	  if (local_got_refcounts != NULL)
2583 	    {
2584 	      /* If the counters are 0 when we got here, we've
2585 		 miscounted somehow somewhere, an internal error.  */
2586 	      BFD_ASSERT (local_got_refcounts[r_symndx] > 0);
2587 	      --local_got_refcounts[r_symndx];
2588 
2589 	      BFD_ASSERT (*specific_refcount > 0);
2590 	      --*specific_refcount;
2591 	      if (*specific_refcount == 0)
2592 		{
2593 		  /* We don't need the .got entry any more.  */
2594 		  sgot->size -= got_element_size;
2595 		  if (info->shared)
2596 		    srelgot->size -= sizeof (Elf32_External_Rela);
2597 		}
2598 	    }
2599 	  break;
2600 
2601 	case R_CRIS_16_GOTPLT:
2602 	case R_CRIS_32_GOTPLT:
2603 	  /* For local symbols, treat these like GOT relocs.  */
2604 	  if (h == NULL)
2605 	    goto local_got_reloc;
2606 	  else
2607 	    /* For global symbols, adjust the reloc-specific refcount.  */
2608 	    elf_cris_hash_entry (h)->gotplt_refcount--;
2609 	  /* Fall through.  */
2610 
2611 	case R_CRIS_32_PLT_GOTREL:
2612 	  /* FIXME: We don't garbage-collect away the .got section.  */
2613 	  if (local_got_refcounts != NULL)
2614 	    local_got_refcounts[-1]--;
2615 	  /* Fall through.  */
2616 
2617 	case R_CRIS_8:
2618 	case R_CRIS_16:
2619 	case R_CRIS_32:
2620 	case R_CRIS_8_PCREL:
2621 	case R_CRIS_16_PCREL:
2622 	case R_CRIS_32_PCREL:
2623 	case R_CRIS_32_PLT_PCREL:
2624 	  /* Negate the increment we did in cris_elf_check_relocs.  */
2625 	  if (h != NULL)
2626 	    {
2627 	      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2628 		  && h->plt.refcount > 0)
2629 		--h->plt.refcount;
2630 	    }
2631 	  break;
2632 
2633 	case R_CRIS_32_DTPREL:
2634 	  /* This'd be a .dtpreld entry in e.g. debug info.  */
2635 	  if ((sec->flags & SEC_ALLOC) == 0)
2636 	    break;
2637 	  /* Fall through.  */
2638 	case R_CRIS_16_DTPREL:
2639 	  htab->dtpmod_refcount--;
2640 	  if (htab->dtpmod_refcount == 0)
2641 	    htab->next_gotplt_entry -= 8;
2642 	  BFD_ASSERT (local_got_refcounts != NULL);
2643 	  local_got_refcounts[-1]--;
2644 	  break;
2645 
2646 	default:
2647 	  break;
2648 	}
2649     }
2650 
2651   return TRUE;
2652 }
2653 
2654 /* The elf_backend_plt_sym_val hook function.  */
2655 
2656 static bfd_vma
2657 cris_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED, const asection *plt,
2658 		      const arelent *rel)
2659 {
2660   bfd_size_type plt_entry_size;
2661   bfd_size_type pltoffs;
2662   bfd *abfd = plt->owner;
2663 
2664   /* Same for CRIS and CRIS v32; see elf_cris_(|pic_)plt_entry(|_v32)[].  */
2665   bfd_size_type plt_entry_got_offset = 2;
2666   bfd_size_type plt_sec_size;
2667   bfd_size_type got_vma_for_dyn;
2668   asection *got;
2669 
2670   /* FIXME: the .got section should be readily available also when
2671      we're not linking.  */
2672   if ((got = bfd_get_section_by_name (abfd, ".got")) == NULL)
2673     return (bfd_vma) -1;
2674 
2675   plt_sec_size =  bfd_section_size (plt->owner, plt);
2676   plt_entry_size
2677     = (bfd_get_mach (abfd) == bfd_mach_cris_v32
2678        ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2679 
2680   /* Data in PLT is GOT-relative for DYN, but absolute for EXE.  */
2681   got_vma_for_dyn = (abfd->flags & EXEC_P) ? 0 : got->vma;
2682 
2683   /* Because we can have merged GOT entries; a single .got entry for
2684      both GOT and the PLT part of the GOT (.got.plt), the index of the
2685      reloc in .rela.plt is not the same as the index in the PLT.
2686      Instead, we have to hunt down the GOT offset in the PLT that
2687      corresponds to that of this reloc.  Unfortunately, we will only
2688      be called for the .rela.plt relocs, so we'll miss synthetic
2689      symbols for .plt entries with merged GOT entries.  (FIXME:
2690      fixable by providing our own bfd_elf32_get_synthetic_symtab.
2691      Doesn't seem worthwile at time of this writing.)  FIXME: we've
2692      gone from O(1) to O(N) (N number of PLT entries) for finding each
2693      PLT address.  Shouldn't matter in practice though.  */
2694 
2695   for (pltoffs = plt_entry_size;
2696        pltoffs < plt_sec_size;
2697        pltoffs += plt_entry_size)
2698     {
2699       bfd_size_type got_offset;
2700       bfd_byte gotoffs_raw[4];
2701 
2702       if (!bfd_get_section_contents (abfd, (asection *) plt, gotoffs_raw,
2703 				     pltoffs + plt_entry_got_offset,
2704 				     sizeof (gotoffs_raw)))
2705 	return (bfd_vma) -1;
2706 
2707       got_offset = bfd_get_32 (abfd, gotoffs_raw);
2708       if (got_offset + got_vma_for_dyn == rel->address)
2709 	return plt->vma + pltoffs;
2710     }
2711 
2712   /* While it's tempting to BFD_ASSERT that we shouldn't get here,
2713      that'd not be graceful behavior for invalid input.  */
2714   return (bfd_vma) -1;
2715 }
2716 
2717 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
2718    entry but we found we will not create any.  Called when we find we will
2719    not have any PLT for this symbol, by for example
2720    elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
2721    or elf_cris_size_dynamic_sections if no dynamic sections will be
2722    created (we're only linking static objects).  */
2723 
2724 static bfd_boolean
2725 elf_cris_adjust_gotplt_to_got (struct elf_cris_link_hash_entry *h, void * p)
2726 {
2727   struct bfd_link_info *info = (struct bfd_link_info *) p;
2728 
2729   /* A GOTPLT reloc, when activated, is supposed to be included into
2730      the PLT refcount.  */
2731   BFD_ASSERT (h->gotplt_refcount == 0
2732 	      || h->gotplt_refcount <= h->root.plt.refcount);
2733 
2734   /* If nobody wanted a GOTPLT with this symbol, we're done.  */
2735   if (h->gotplt_refcount <= 0)
2736     return TRUE;
2737 
2738   if (h->reg_got_refcount > 0)
2739     {
2740       /* There's a GOT entry for this symbol.  Just adjust the refcounts.
2741 	 Probably not necessary at this stage, but keeping them accurate
2742 	 helps avoiding surprises later.  */
2743       h->root.got.refcount += h->gotplt_refcount;
2744       h->reg_got_refcount += h->gotplt_refcount;
2745       h->gotplt_refcount = 0;
2746     }
2747   else
2748     {
2749       /* No GOT entry for this symbol.  We need to create one.  */
2750       bfd *dynobj = elf_hash_table (info)->dynobj;
2751       asection *sgot;
2752       asection *srelgot;
2753 
2754       BFD_ASSERT (dynobj != NULL);
2755       sgot = bfd_get_linker_section (dynobj, ".got");
2756       srelgot = bfd_get_linker_section (dynobj, ".rela.got");
2757 
2758       /* Put accurate refcounts there.  */
2759       h->root.got.refcount += h->gotplt_refcount;
2760       h->reg_got_refcount = h->gotplt_refcount;
2761 
2762       h->gotplt_refcount = 0;
2763 
2764       /* We always have a .got and a .rela.got section if there were
2765 	 GOTPLT relocs in input.  */
2766       BFD_ASSERT (sgot != NULL && srelgot != NULL);
2767 
2768       /* Allocate space in the .got section.  */
2769       sgot->size += 4;
2770 
2771       /* Allocate relocation space.  */
2772       srelgot->size += sizeof (Elf32_External_Rela);
2773     }
2774 
2775   return TRUE;
2776 }
2777 
2778 /* Try to fold PLT entries with GOT entries.  There are two cases when we
2779    want to do this:
2780 
2781    - When all PLT references are GOTPLT references, and there are GOT
2782      references, and this is not the executable.  We don't have to
2783      generate a PLT at all.
2784 
2785    - When there are both (ordinary) PLT references and GOT references,
2786      and this isn't the executable.
2787      We want to make the PLT reference use the ordinary GOT entry rather
2788      than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
2789      since the GOT entry will have to be resolved at startup anyway.
2790 
2791    Though the latter case is handled when room for the PLT is allocated,
2792    not here.
2793 
2794    By folding into the GOT, we may need a round-trip to a PLT in the
2795    executable for calls, a loss in performance.  Still, losing a
2796    reloc is a win in size and at least in start-up time.
2797 
2798    Note that this function is called before symbols are forced local by
2799    version scripts.  The differing cases are handled by
2800    elf_cris_hide_symbol.  */
2801 
2802 static bfd_boolean
2803 elf_cris_try_fold_plt_to_got (struct elf_cris_link_hash_entry *h, void * p)
2804 {
2805   struct bfd_link_info *info = (struct bfd_link_info *) p;
2806 
2807   /* If there are no GOT references for this symbol, we can't fold any
2808      other reference so there's nothing to do.  Likewise if there are no
2809      PLT references; GOTPLT references included.  */
2810   if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
2811     return TRUE;
2812 
2813   /* GOTPLT relocs are supposed to be included into the PLT refcount.  */
2814   BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
2815 
2816   if (h->gotplt_refcount == h->root.plt.refcount)
2817     {
2818       /* The only PLT references are GOTPLT references, and there are GOT
2819 	 references.  Convert PLT to GOT references.  */
2820       if (! elf_cris_adjust_gotplt_to_got (h, info))
2821 	return FALSE;
2822 
2823       /* Clear the PLT references, so no PLT will be created.  */
2824       h->root.plt.offset = (bfd_vma) -1;
2825     }
2826 
2827   return TRUE;
2828 }
2829 
2830 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
2831    to use a GOT entry (and create one) rather than requiring a GOTPLT
2832    entry.  */
2833 
2834 static void
2835 elf_cris_hide_symbol (struct bfd_link_info *info,
2836 		      struct elf_link_hash_entry *h,
2837 		      bfd_boolean force_local)
2838 {
2839   elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
2840 
2841   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2842 }
2843 
2844 /* Adjust a symbol defined by a dynamic object and referenced by a
2845    regular object.  The current definition is in some section of the
2846    dynamic object, but we're not including those sections.  We have to
2847    change the definition to something the rest of the link can
2848    understand.  */
2849 
2850 static bfd_boolean
2851 elf_cris_adjust_dynamic_symbol (struct bfd_link_info *info,
2852 				struct elf_link_hash_entry *h)
2853 {
2854   struct elf_cris_link_hash_table * htab;
2855   bfd *dynobj;
2856   asection *s;
2857   bfd_size_type plt_entry_size;
2858 
2859   htab = elf_cris_hash_table (info);
2860   if (htab == NULL)
2861     return FALSE;
2862 
2863   dynobj = elf_hash_table (info)->dynobj;
2864 
2865   /* Make sure we know what is going on here.  */
2866   BFD_ASSERT (dynobj != NULL
2867 	      && (h->needs_plt
2868 		  || h->u.weakdef != NULL
2869 		  || (h->def_dynamic
2870 		      && h->ref_regular
2871 		      && !h->def_regular)));
2872 
2873   plt_entry_size
2874     = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
2875        ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2876 
2877   /* If this is a function, put it in the procedure linkage table.  We
2878      will fill in the contents of the procedure linkage table later,
2879      when we know the address of the .got section.  */
2880   if (h->type == STT_FUNC
2881       || h->needs_plt)
2882     {
2883       /* If we link a program (not a DSO), we'll get rid of unnecessary
2884 	 PLT entries; we point to the actual symbols -- even for pic
2885 	 relocs, because a program built with -fpic should have the same
2886 	 result as one built without -fpic, specifically considering weak
2887 	 symbols.
2888 	 FIXME: m68k and i386 differ here, for unclear reasons.  */
2889       if (! info->shared
2890 	  && !h->def_dynamic)
2891 	{
2892 	  /* This case can occur if we saw a PLT reloc in an input file,
2893 	     but the symbol was not defined by a dynamic object.  In such
2894 	     a case, we don't actually need to build a procedure linkage
2895 	     table, and we can just do an absolute or PC reloc instead, or
2896 	     change a .got.plt index to a .got index for GOTPLT relocs.  */
2897 	  BFD_ASSERT (h->needs_plt);
2898 	  h->needs_plt = 0;
2899 	  h->plt.offset = (bfd_vma) -1;
2900 	  return
2901 	    elf_cris_adjust_gotplt_to_got ((struct
2902 					    elf_cris_link_hash_entry *) h,
2903 					   info);
2904 	}
2905 
2906       /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
2907 	 where a pointer-equivalent symbol was unimportant (i.e. more
2908 	 like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
2909 	 of the PLT.  We can't for the executable, because the GOT
2910 	 entries will point to the PLT there (and be constant).  */
2911       if (info->shared
2912 	  && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
2913 					    h, info))
2914 	return FALSE;
2915 
2916       /* GC or folding may have rendered this entry unused.  */
2917       if (h->plt.refcount <= 0)
2918 	{
2919 	  h->needs_plt = 0;
2920 	  h->plt.offset = (bfd_vma) -1;
2921 	  return TRUE;
2922 	}
2923 
2924       /* Make sure this symbol is output as a dynamic symbol.  */
2925       if (h->dynindx == -1)
2926 	{
2927 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
2928 	    return FALSE;
2929 	}
2930 
2931       s = bfd_get_linker_section (dynobj, ".plt");
2932       BFD_ASSERT (s != NULL);
2933 
2934       /* If this is the first .plt entry, make room for the special
2935 	 first entry.  */
2936       if (s->size == 0)
2937 	s->size += plt_entry_size;
2938 
2939       /* If this symbol is not defined in a regular file, and we are
2940 	 not generating a shared library, then set the symbol to this
2941 	 location in the .plt.  */
2942       if (!info->shared
2943 	  && !h->def_regular)
2944 	{
2945 	  h->root.u.def.section = s;
2946 	  h->root.u.def.value = s->size;
2947 	}
2948 
2949       /* If there's already a GOT entry, use that, not a .got.plt.  A
2950 	 GOT field still has a reference count when we get here; it's
2951 	 not yet changed to an offset.  We can't do this for an
2952 	 executable, because then the reloc associated with the PLT
2953 	 would get a non-PLT reloc pointing to the PLT.  FIXME: Move
2954 	 this to elf_cris_try_fold_plt_to_got.  */
2955       if (info->shared && h->got.refcount > 0)
2956 	{
2957 	  h->got.refcount += h->plt.refcount;
2958 
2959 	  /* Mark the PLT offset to use the GOT entry by setting the low
2960 	     bit in the plt offset; it is always a multiple of
2961 	     plt_entry_size (which is at least a multiple of 2).  */
2962 	  BFD_ASSERT ((s->size % plt_entry_size) == 0);
2963 
2964 	  /* Change the PLT refcount to an offset.  */
2965 	  h->plt.offset = s->size;
2966 
2967 	  /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2968 	     that the got entry should be used instead.  */
2969 	  BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2970 		       h)->gotplt_offset == 0);
2971 
2972 	  /* Make room for this entry.  */
2973 	  s->size += plt_entry_size;
2974 
2975 	  return TRUE;
2976 	}
2977 
2978       /* No GOT reference for this symbol; prepare for an ordinary PLT.  */
2979       h->plt.offset = s->size;
2980 
2981       /* Make room for this entry.  */
2982       s->size += plt_entry_size;
2983 
2984       /* We also need to make an entry in the .got.plt section, which
2985 	 will be placed in the .got section by the linker script.  */
2986       ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2987 	= htab->next_gotplt_entry;
2988       htab->next_gotplt_entry += 4;
2989 
2990       s = bfd_get_linker_section (dynobj, ".got.plt");
2991       BFD_ASSERT (s != NULL);
2992       s->size += 4;
2993 
2994       /* We also need to make an entry in the .rela.plt section.  */
2995 
2996       s = bfd_get_linker_section (dynobj, ".rela.plt");
2997       BFD_ASSERT (s != NULL);
2998       s->size += sizeof (Elf32_External_Rela);
2999 
3000       return TRUE;
3001     }
3002 
3003   /* Reinitialize the plt offset now that it is not used as a reference
3004      count any more.  */
3005   h->plt.offset = (bfd_vma) -1;
3006 
3007   /* If this is a weak symbol, and there is a real definition, the
3008      processor independent code will have arranged for us to see the
3009      real definition first, and we can just use the same value.  */
3010   if (h->u.weakdef != NULL)
3011     {
3012       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3013 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
3014       h->root.u.def.section = h->u.weakdef->root.u.def.section;
3015       h->root.u.def.value = h->u.weakdef->root.u.def.value;
3016       return TRUE;
3017     }
3018 
3019   /* This is a reference to a symbol defined by a dynamic object which
3020      is not a function.  */
3021 
3022   /* If we are creating a shared library, we must presume that the
3023      only references to the symbol are via the global offset table.
3024      For such cases we need not do anything here; the relocations will
3025      be handled correctly by relocate_section.  */
3026   if (info->shared)
3027     return TRUE;
3028 
3029   /* If there are no references to this symbol that do not use the
3030      GOT, we don't need to generate a copy reloc.  */
3031   if (!h->non_got_ref)
3032     return TRUE;
3033 
3034   /* We must allocate the symbol in our .dynbss section, which will
3035      become part of the .bss section of the executable.  There will be
3036      an entry for this symbol in the .dynsym section.  The dynamic
3037      object will contain position independent code, so all references
3038      from the dynamic object to this symbol will go through the global
3039      offset table.  The dynamic linker will use the .dynsym entry to
3040      determine the address it must put in the global offset table, so
3041      both the dynamic object and the regular object will refer to the
3042      same memory location for the variable.  */
3043 
3044   s = bfd_get_linker_section (dynobj, ".dynbss");
3045   BFD_ASSERT (s != NULL);
3046 
3047   /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
3048      copy the initial value out of the dynamic object and into the
3049      runtime process image.  We need to remember the offset into the
3050      .rela.bss section we are going to use.  */
3051   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3052     {
3053       asection *srel;
3054 
3055       srel = bfd_get_linker_section (dynobj, ".rela.bss");
3056       BFD_ASSERT (srel != NULL);
3057       srel->size += sizeof (Elf32_External_Rela);
3058       h->needs_copy = 1;
3059     }
3060 
3061   return _bfd_elf_adjust_dynamic_copy (h, s);
3062 }
3063 
3064 /* Adjust our "subclass" elements for an indirect symbol.  */
3065 
3066 static void
3067 elf_cris_copy_indirect_symbol (struct bfd_link_info *info,
3068 			       struct elf_link_hash_entry *dir,
3069 			       struct elf_link_hash_entry *ind)
3070 {
3071   struct elf_cris_link_hash_entry *edir, *eind;
3072 
3073   edir = (struct elf_cris_link_hash_entry *) dir;
3074   eind = (struct elf_cris_link_hash_entry *) ind;
3075 
3076   /* Only indirect symbols are replaced; we're not interested in
3077      updating any of EIND's fields for other symbols.  */
3078   if (eind->root.root.type != bfd_link_hash_indirect)
3079     {
3080       /* Still, we need to copy flags for e.g. weak definitions.  */
3081       _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3082       return;
3083     }
3084 
3085   BFD_ASSERT (edir->gotplt_offset == 0 || eind->gotplt_offset == 0);
3086 
3087 #define XMOVOPZ(F, OP, Z) edir->F OP eind->F; eind->F = Z
3088 #define XMOVE(F) XMOVOPZ (F, +=, 0)
3089   if (eind->pcrel_relocs_copied != NULL)
3090     {
3091       if (edir->pcrel_relocs_copied != NULL)
3092 	{
3093 	  struct elf_cris_pcrel_relocs_copied **pp;
3094 	  struct elf_cris_pcrel_relocs_copied *p;
3095 
3096 	  /* Add reloc counts against the indirect sym to the direct sym
3097 	     list.  Merge any entries against the same section.  */
3098 	  for (pp = &eind->pcrel_relocs_copied; *pp != NULL;)
3099 	    {
3100 	      struct elf_cris_pcrel_relocs_copied *q;
3101 	      p = *pp;
3102 	      for (q = edir->pcrel_relocs_copied; q != NULL; q = q->next)
3103 		if (q->section == p->section)
3104 		  {
3105 		    q->count += p->count;
3106 		    *pp = p->next;
3107 		    break;
3108 		  }
3109 	      if (q == NULL)
3110 		pp = &p->next;
3111 	    }
3112 	  *pp = edir->pcrel_relocs_copied;
3113 	}
3114       XMOVOPZ (pcrel_relocs_copied, =, NULL);
3115     }
3116   XMOVE (gotplt_refcount);
3117   XMOVE (gotplt_offset);
3118   XMOVE (reg_got_refcount);
3119   XMOVE (tprel_refcount);
3120   XMOVE (dtp_refcount);
3121 #undef XMOVE
3122 #undef XMOVOPZ
3123 
3124   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3125 }
3126 
3127 /* Look through the relocs for a section during the first phase.  */
3128 
3129 static bfd_boolean
3130 cris_elf_check_relocs (bfd *abfd,
3131 		       struct bfd_link_info *info,
3132 		       asection *sec,
3133 		       const Elf_Internal_Rela *relocs)
3134 {
3135   struct elf_cris_link_hash_table * htab;
3136   bfd *dynobj;
3137   Elf_Internal_Shdr *symtab_hdr;
3138   struct elf_link_hash_entry **sym_hashes;
3139   bfd_signed_vma *local_got_refcounts;
3140   const Elf_Internal_Rela *rel;
3141   const Elf_Internal_Rela *rel_end;
3142   asection *sgot;
3143   asection *srelgot;
3144   asection *sreloc;
3145 
3146   if (info->relocatable)
3147     return TRUE;
3148 
3149   htab = elf_cris_hash_table (info);
3150   if (htab == NULL)
3151     return FALSE;
3152 
3153   dynobj = elf_hash_table (info)->dynobj;
3154   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3155   sym_hashes = elf_sym_hashes (abfd);
3156   local_got_refcounts = elf_local_got_refcounts (abfd);
3157 
3158   sgot = NULL;
3159   srelgot = NULL;
3160   sreloc = NULL;
3161 
3162   rel_end = relocs + sec->reloc_count;
3163   for (rel = relocs; rel < rel_end; rel++)
3164     {
3165       struct elf_link_hash_entry *h;
3166       unsigned long r_symndx;
3167       enum elf_cris_reloc_type r_type;
3168       bfd_signed_vma got_element_size = 4;
3169       unsigned long r_symndx_lgot = INT_MAX;
3170 
3171       r_symndx = ELF32_R_SYM (rel->r_info);
3172       if (r_symndx < symtab_hdr->sh_info)
3173 	{
3174 	  h = NULL;
3175 	  r_symndx_lgot = LGOT_REG_NDX (r_symndx);
3176 	}
3177       else
3178 	{
3179 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3180 	  while (h->root.type == bfd_link_hash_indirect
3181 		 || h->root.type == bfd_link_hash_warning)
3182 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3183 	}
3184 
3185       r_type = ELF32_R_TYPE (rel->r_info);
3186 
3187       /* Some relocs require linker-created sections; we need to hang them
3188 	 on the first input bfd we found that contained dynamic relocs.  */
3189       switch (r_type)
3190 	{
3191 	case R_CRIS_32_DTPREL:
3192 	  if ((sec->flags & SEC_ALLOC) == 0)
3193 	    /* This'd be a .dtpreld entry in e.g. debug info.  We have
3194 	       several different switch statements below, but none of
3195 	       that is needed; we need no preparations for resolving
3196 	       R_CRIS_32_DTPREL into a non-allocated section (debug
3197 	       info), so let's just move on to the next
3198 	       relocation.  */
3199 	    continue;
3200 	  /* Fall through.  */
3201 	case R_CRIS_16_DTPREL:
3202 	  /* The first .got.plt entry is right after the R_CRIS_DTPMOD
3203 	     entry at index 3. */
3204 	  if (htab->dtpmod_refcount == 0)
3205 	    htab->next_gotplt_entry += 8;
3206 
3207 	  htab->dtpmod_refcount++;
3208 	  /* Fall through.  */
3209 
3210 	case R_CRIS_32_IE:
3211 	case R_CRIS_32_GD:
3212 	case R_CRIS_16_GOT_GD:
3213 	case R_CRIS_32_GOT_GD:
3214 	case R_CRIS_32_GOT_TPREL:
3215 	case R_CRIS_16_GOT_TPREL:
3216 	case R_CRIS_16_GOT:
3217 	case R_CRIS_32_GOT:
3218 	case R_CRIS_32_GOTREL:
3219 	case R_CRIS_32_PLT_GOTREL:
3220 	case R_CRIS_32_PLT_PCREL:
3221 	case R_CRIS_16_GOTPLT:
3222 	case R_CRIS_32_GOTPLT:
3223 	  if (dynobj == NULL)
3224 	    {
3225 	      elf_hash_table (info)->dynobj = dynobj = abfd;
3226 
3227 	      /* We could handle this if we can get a handle on the
3228 		 output bfd in elf_cris_adjust_dynamic_symbol.  Failing
3229 		 that, we must insist on dynobj being a specific mach.  */
3230 	      if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
3231 		{
3232 		  (*_bfd_error_handler)
3233 		    (_("%B, section %A:\n  v10/v32 compatible object %s"
3234 		       " must not contain a PIC relocation"),
3235 		     abfd, sec);
3236 		  return FALSE;
3237 		}
3238 
3239 	      /* Create the .got section, so we can assume it's always
3240 		 present whenever there's a dynobj.  */
3241 	      if (!_bfd_elf_create_got_section (dynobj, info))
3242 		return FALSE;
3243 	    }
3244 
3245 	  if (sgot == NULL)
3246 	    sgot = bfd_get_linker_section (dynobj, ".got");
3247 
3248 	  if (local_got_refcounts == NULL)
3249 	    {
3250 	      bfd_size_type amt;
3251 
3252 	      /* We use index local_got_refcounts[-1] to count all
3253 		 GOT-relative relocations that do not have explicit
3254 		 GOT entries.  */
3255 	      amt = LGOT_ALLOC_NELTS_FOR (symtab_hdr->sh_info) + 1;
3256 	      amt *= sizeof (bfd_signed_vma);
3257 	      local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
3258 	      if (local_got_refcounts == NULL)
3259 		return FALSE;
3260 
3261 	      local_got_refcounts++;
3262 	      elf_local_got_refcounts (abfd) = local_got_refcounts;
3263 	    }
3264 	  break;
3265 
3266 	default:
3267 	  break;
3268 	}
3269 
3270       /* Some relocs require a global offset table (but perhaps not a
3271 	 specific GOT entry).  */
3272       switch (r_type)
3273 	{
3274 	case R_CRIS_16_DTPREL:
3275 	case R_CRIS_32_DTPREL:
3276 	  /* Not requesting .got.rela for an executable: the contents
3277 	     of the first entry is constant there.  For a shared
3278 	     library, we need .got.rela for the R_CRIS_DTPMOD
3279 	     relocation at index 3.  */
3280 	  if (!info->shared)
3281 	    break;
3282 	  /* Fall through.  */
3283 
3284 	case R_CRIS_32_IE:
3285 	case R_CRIS_32_GD:
3286 	case R_CRIS_16_GOT_GD:
3287 	case R_CRIS_32_GOT_GD:
3288 	case R_CRIS_32_GOT_TPREL:
3289 	case R_CRIS_16_GOT_TPREL:
3290 	  /* Fall through.  */
3291 
3292 	  /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
3293 	     entry only for local symbols.  Unfortunately, we don't know
3294 	     until later on if there's a version script that forces the
3295 	     symbol local.  We must have the .rela.got section in place
3296 	     before we know if the symbol looks global now, so we need
3297 	     to treat the reloc just like for R_CRIS_16_GOT and
3298 	     R_CRIS_32_GOT.  */
3299 	case R_CRIS_16_GOTPLT:
3300 	case R_CRIS_32_GOTPLT:
3301 	case R_CRIS_16_GOT:
3302 	case R_CRIS_32_GOT:
3303 	  if (srelgot == NULL
3304 	      && (h != NULL || info->shared))
3305 	    {
3306 	      srelgot = bfd_get_linker_section (dynobj, ".rela.got");
3307 	      if (srelgot == NULL)
3308 		{
3309 		  flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3310 				    | SEC_IN_MEMORY | SEC_LINKER_CREATED
3311 				    | SEC_READONLY);
3312 		  srelgot = bfd_make_section_anyway_with_flags (dynobj,
3313 								".rela.got",
3314 								flags);
3315 		  if (srelgot == NULL
3316 		      || !bfd_set_section_alignment (dynobj, srelgot, 2))
3317 		    return FALSE;
3318 		}
3319 	    }
3320 	  break;
3321 
3322 	default:
3323 	  break;
3324 	}
3325 
3326       /* Warn and error for invalid input.  */
3327       switch (r_type)
3328 	{
3329 	case R_CRIS_32_IE:
3330 	case R_CRIS_32_TPREL:
3331 	case R_CRIS_16_TPREL:
3332 	case R_CRIS_32_GD:
3333 	  if (info->shared)
3334 	    {
3335 	      (*_bfd_error_handler)
3336 		(_("%B, section %A:\n  relocation %s not valid"
3337 		   " in a shared object;"
3338 		   " typically an option mixup, recompile with -fPIC"),
3339 		 abfd,
3340 		 sec,
3341 		 cris_elf_howto_table[r_type].name);
3342 	      /* Don't return FALSE here; we want messages for all of
3343 		 these and the error behavior is ungraceful
3344 		 anyway.  */
3345 	    }
3346 	default:
3347 	  break;
3348 	}
3349 
3350       switch (r_type)
3351 	{
3352 	case R_CRIS_32_GD:
3353 	case R_CRIS_16_GOT_GD:
3354 	case R_CRIS_32_GOT_GD:
3355 	  /* These are requests for tls_index entries, run-time R_CRIS_DTP.  */
3356 	  got_element_size = 8;
3357 	  r_symndx_lgot = LGOT_DTP_NDX (r_symndx);
3358 	  break;
3359 
3360 	case R_CRIS_16_DTPREL:
3361 	case R_CRIS_32_DTPREL:
3362 	  /* These two just request for the constant-index
3363 	     module-local tls_index-sized GOT entry, which we add
3364 	     elsewhere.  */
3365 	  break;
3366 
3367 	case R_CRIS_32_IE:
3368 	case R_CRIS_32_GOT_TPREL:
3369 	case R_CRIS_16_GOT_TPREL:
3370 	  r_symndx_lgot = LGOT_TPREL_NDX (r_symndx);
3371 
3372 	  /* Those relocs also require that a DSO is of type
3373 	     Initial Exec.  Like other targets, we don't reset this
3374 	     flag even if the relocs are GC:ed away.  */
3375 	  if (info->shared)
3376 	    info->flags |= DF_STATIC_TLS;
3377 	  break;
3378 
3379 	  /* Let's list the other assembler-generated TLS-relocs too,
3380 	     just to show that they're not forgotten. */
3381 	case R_CRIS_16_TPREL:
3382 	case R_CRIS_32_TPREL:
3383 	default:
3384 	  break;
3385 	}
3386 
3387       switch (r_type)
3388         {
3389 	case R_CRIS_16_GOTPLT:
3390 	case R_CRIS_32_GOTPLT:
3391 	  /* Mark that we need a GOT entry if the PLT entry (and its GOT
3392 	     entry) is eliminated.  We can only do this for a non-local
3393 	     symbol.  */
3394 	  if (h != NULL)
3395 	    {
3396 	      elf_cris_hash_entry (h)->gotplt_refcount++;
3397 	      goto handle_gotplt_reloc;
3398 	    }
3399 	  /* If h is NULL then this is a local symbol, and we must make a
3400 	     GOT entry for it, so handle it like a GOT reloc.  */
3401 	  /* Fall through.  */
3402 
3403 	case R_CRIS_32_IE:
3404 	case R_CRIS_32_GD:
3405 	case R_CRIS_16_GOT_GD:
3406 	case R_CRIS_32_GOT_GD:
3407 	case R_CRIS_32_GOT_TPREL:
3408 	case R_CRIS_16_GOT_TPREL:
3409 	case R_CRIS_16_GOT:
3410 	case R_CRIS_32_GOT:
3411 	  /* This symbol requires a global offset table entry.  */
3412 	  if (h != NULL)
3413 	    {
3414 	      if (h->got.refcount == 0)
3415 		{
3416 		  /* Make sure this symbol is output as a dynamic symbol.  */
3417 		  if (h->dynindx == -1)
3418 		    {
3419 		      if (!bfd_elf_link_record_dynamic_symbol (info, h))
3420 			return FALSE;
3421 		    }
3422 		}
3423 
3424 	      /* Update the sum of reloc counts for this symbol.  */
3425 	      h->got.refcount++;
3426 
3427 	      switch (r_type)
3428 		{
3429 		case R_CRIS_16_GOT:
3430 		case R_CRIS_32_GOT:
3431 		  if (elf_cris_hash_entry (h)->reg_got_refcount == 0)
3432 		    {
3433 		      /* Allocate space in the .got section.  */
3434 		      sgot->size += got_element_size;
3435 		      /* Allocate relocation space.  */
3436 		      srelgot->size += sizeof (Elf32_External_Rela);
3437 		    }
3438 		  elf_cris_hash_entry (h)->reg_got_refcount++;
3439 		  break;
3440 
3441 		case R_CRIS_32_GD:
3442 		case R_CRIS_16_GOT_GD:
3443 		case R_CRIS_32_GOT_GD:
3444 		  if (elf_cris_hash_entry (h)->dtp_refcount == 0)
3445 		    {
3446 		      /* Allocate space in the .got section.  */
3447 		      sgot->size += got_element_size;
3448 		      /* Allocate relocation space.  */
3449 		      srelgot->size += sizeof (Elf32_External_Rela);
3450 		    }
3451 		  elf_cris_hash_entry (h)->dtp_refcount++;
3452 		  break;
3453 
3454 		case R_CRIS_32_IE:
3455 		case R_CRIS_32_GOT_TPREL:
3456 		case R_CRIS_16_GOT_TPREL:
3457 		  if (elf_cris_hash_entry (h)->tprel_refcount == 0)
3458 		    {
3459 		      /* Allocate space in the .got section.  */
3460 		      sgot->size += got_element_size;
3461 		      /* Allocate relocation space.  */
3462 		      srelgot->size += sizeof (Elf32_External_Rela);
3463 		    }
3464 		  elf_cris_hash_entry (h)->tprel_refcount++;
3465 		  break;
3466 
3467 		default:
3468 		  BFD_FAIL ();
3469 		  break;
3470 		}
3471 	    }
3472 	  else
3473 	    {
3474 	      /* This is a global offset table entry for a local symbol.  */
3475 	      if (local_got_refcounts[r_symndx_lgot] == 0)
3476 		{
3477 		  sgot->size += got_element_size;
3478 		  if (info->shared)
3479 		    {
3480 		      /* If we are generating a shared object, we need
3481 			 to output a R_CRIS_RELATIVE reloc so that the
3482 			 dynamic linker can adjust this GOT entry.
3483 			 Similarly for non-regular got entries.  */
3484 		      srelgot->size += sizeof (Elf32_External_Rela);
3485 		    }
3486 		}
3487 	      /* Update the reloc-specific count.  */
3488 	      local_got_refcounts[r_symndx_lgot]++;
3489 
3490 	      /* This one is the sum of all the others.  */
3491 	      local_got_refcounts[r_symndx]++;
3492 	    }
3493 	  break;
3494 
3495 	case R_CRIS_16_DTPREL:
3496 	case R_CRIS_32_DTPREL:
3497 	case R_CRIS_32_GOTREL:
3498 	  /* This reference requires a global offset table.
3499 	     FIXME: The actual refcount isn't used currently; the .got
3500 	     section can't be removed if there were any references in the
3501 	     input.  */
3502 	  local_got_refcounts[-1]++;
3503 	  break;
3504 
3505 	handle_gotplt_reloc:
3506 
3507 	case R_CRIS_32_PLT_GOTREL:
3508 	  /* This reference requires a global offset table.  */
3509 	  local_got_refcounts[-1]++;
3510 	  /* Fall through.  */
3511 
3512 	case R_CRIS_32_PLT_PCREL:
3513 	  /* This symbol requires a procedure linkage table entry.  We
3514 	     actually build the entry in adjust_dynamic_symbol,
3515              because this might be a case of linking PIC code which is
3516              never referenced by a dynamic object, in which case we
3517              don't need to generate a procedure linkage table entry
3518              after all.  */
3519 
3520 	  /* Beware: if we'd check for visibility of the symbol here
3521 	     (and not marking the need for a PLT when non-visible), we'd
3522 	     get into trouble with keeping handling consistent with
3523 	     regards to relocs found before definition and GOTPLT
3524 	     handling.  Eliminable PLT entries will be dealt with later
3525 	     anyway.  */
3526 	  if (h == NULL)
3527 	    continue;
3528 
3529 	  h->needs_plt = 1;
3530 	  h->plt.refcount++;
3531 	  break;
3532 
3533 	case R_CRIS_8:
3534 	case R_CRIS_16:
3535 	case R_CRIS_32:
3536 	  /* Let's help debug shared library creation.  Any of these
3537 	     relocs *can* be used in shared libs, but pages containing
3538 	     them cannot be shared, so they're not appropriate for
3539 	     common use.  Don't warn for sections we don't care about,
3540 	     such as debug sections or non-constant sections.  We
3541 	     can't help tables of (global) function pointers, for
3542 	     example, though they must be emitted in a (writable) data
3543 	     section to avoid having impure text sections.  */
3544 	  if (info->shared
3545 	      && (sec->flags & SEC_ALLOC) != 0
3546 	      && (sec->flags & SEC_READONLY) != 0)
3547 	    {
3548 	      /* FIXME: How do we make this optionally a warning only?  */
3549 	      (*_bfd_error_handler)
3550 		(_("%B, section %A:\n  relocation %s should not"
3551 		   " be used in a shared object; recompile with -fPIC"),
3552 		 abfd,
3553 		 sec,
3554 		 cris_elf_howto_table[r_type].name);
3555 	    }
3556 
3557 	  /* We don't need to handle relocs into sections not going into
3558 	     the "real" output.  */
3559 	  if ((sec->flags & SEC_ALLOC) == 0)
3560 	    break;
3561 
3562 	  if (h != NULL)
3563 	    {
3564 	      h->non_got_ref = 1;
3565 
3566 	      /* Make sure a plt entry is created for this symbol if it
3567 		 turns out to be a function defined by a dynamic object.  */
3568 	      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3569 		h->plt.refcount++;
3570 	    }
3571 
3572 	  /* If we are creating a shared library and this is not a local
3573 	     symbol, we need to copy the reloc into the shared library.
3574 	     However when linking with -Bsymbolic and this is a global
3575 	     symbol which is defined in an object we are including in the
3576 	     link (i.e., DEF_REGULAR is set), then we can resolve the
3577 	     reloc directly.  At this point we have not seen all the input
3578 	     files, so it is possible that DEF_REGULAR is not set now but
3579 	     will be set later (it is never cleared).  In case of a weak
3580 	     definition, DEF_REGULAR may be cleared later by a strong
3581 	     definition in a shared library.  We account for that
3582 	     possibility below by storing information in the relocs_copied
3583 	     field of the hash table entry.  A similar situation occurs
3584 	     when creating shared libraries and symbol visibility changes
3585 	     render the symbol local.  */
3586 
3587 	  /* No need to do anything if we're not creating a shared object.  */
3588 	  if (! info->shared)
3589 	    break;
3590 
3591 	  /* We may need to create a reloc section in the dynobj and made room
3592 	     for this reloc.  */
3593 	  if (sreloc == NULL)
3594 	    {
3595 	      sreloc = _bfd_elf_make_dynamic_reloc_section
3596 		(sec, dynobj, 2, abfd, /*rela?*/ TRUE);
3597 
3598 	      if (sreloc == NULL)
3599 		return FALSE;
3600 	    }
3601 
3602 	  if (sec->flags & SEC_READONLY)
3603 	    info->flags |= DF_TEXTREL;
3604 
3605 	  sreloc->size += sizeof (Elf32_External_Rela);
3606 	  break;
3607 
3608 	case R_CRIS_8_PCREL:
3609 	case R_CRIS_16_PCREL:
3610 	case R_CRIS_32_PCREL:
3611 	  if (h != NULL)
3612 	    {
3613 	      h->non_got_ref = 1;
3614 
3615 	      /* Make sure a plt entry is created for this symbol if it
3616 		 turns out to be a function defined by a dynamic object.  */
3617 	      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3618 		h->plt.refcount++;
3619 	    }
3620 
3621 	  /* If we are creating a shared library and this is not a local
3622 	     symbol, we need to copy the reloc into the shared library.
3623 	     However when linking with -Bsymbolic and this is a global
3624 	     symbol which is defined in an object we are including in the
3625 	     link (i.e., DEF_REGULAR is set), then we can resolve the
3626 	     reloc directly.  At this point we have not seen all the input
3627 	     files, so it is possible that DEF_REGULAR is not set now but
3628 	     will be set later (it is never cleared).  In case of a weak
3629 	     definition, DEF_REGULAR may be cleared later by a strong
3630 	     definition in a shared library.  We account for that
3631 	     possibility below by storing information in the relocs_copied
3632 	     field of the hash table entry.  A similar situation occurs
3633 	     when creating shared libraries and symbol visibility changes
3634 	     render the symbol local.  */
3635 
3636 	  /* No need to do anything if we're not creating a shared object.  */
3637 	  if (! info->shared)
3638 	    break;
3639 
3640 	  /* We don't need to handle relocs into sections not going into
3641 	     the "real" output.  */
3642 	  if ((sec->flags & SEC_ALLOC) == 0)
3643 	    break;
3644 
3645 	  /* If the symbol is local, then we know already we can
3646 	     eliminate the reloc.  */
3647 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3648 	    break;
3649 
3650 	  /* If this is with -Bsymbolic and the symbol isn't weak, and
3651 	     is defined by an ordinary object (the ones we include in
3652 	     this shared library) then we can also eliminate the
3653 	     reloc.  See comment above for more eliminable cases which
3654 	     we can't identify at this time.  */
3655 	  if (info->symbolic
3656 	      && h->root.type != bfd_link_hash_defweak
3657 	      && h->def_regular)
3658 	    break;
3659 
3660 	  /* We may need to create a reloc section in the dynobj and made room
3661 	     for this reloc.  */
3662 	  if (sreloc == NULL)
3663 	    {
3664 	      sreloc = _bfd_elf_make_dynamic_reloc_section
3665 		(sec, dynobj, 2, abfd, /*rela?*/ TRUE);
3666 
3667 	      if (sreloc == NULL)
3668 		return FALSE;
3669 	    }
3670 
3671 	  sreloc->size += sizeof (Elf32_External_Rela);
3672 
3673 	  /* We count the number of PC relative relocations we have
3674 	     entered for this symbol, so that we can discard them
3675 	     again if the symbol is later defined by a regular object.
3676 	     We know that h is really a pointer to an
3677 	     elf_cris_link_hash_entry.  */
3678 	  {
3679 	    struct elf_cris_link_hash_entry *eh;
3680 	    struct elf_cris_pcrel_relocs_copied *p;
3681 
3682 	    eh = elf_cris_hash_entry (h);
3683 
3684 	    for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
3685 	      if (p->section == sec)
3686 		break;
3687 
3688 	    if (p == NULL)
3689 	      {
3690 		p = ((struct elf_cris_pcrel_relocs_copied *)
3691 		     bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
3692 		if (p == NULL)
3693 		  return FALSE;
3694 		p->next = eh->pcrel_relocs_copied;
3695 		eh->pcrel_relocs_copied = p;
3696 		p->section = sec;
3697 		p->count = 0;
3698 		p->r_type = r_type;
3699 	      }
3700 
3701 	    ++p->count;
3702 	  }
3703 	  break;
3704 
3705         /* This relocation describes the C++ object vtable hierarchy.
3706            Reconstruct it for later use during GC.  */
3707         case R_CRIS_GNU_VTINHERIT:
3708           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3709             return FALSE;
3710           break;
3711 
3712         /* This relocation describes which C++ vtable entries are actually
3713            used.  Record for later use during GC.  */
3714         case R_CRIS_GNU_VTENTRY:
3715           BFD_ASSERT (h != NULL);
3716           if (h != NULL
3717               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3718             return FALSE;
3719           break;
3720 
3721 	case R_CRIS_16_TPREL:
3722 	case R_CRIS_32_TPREL:
3723 	  /* Already warned above, when necessary.  */
3724 	  break;
3725 
3726 	default:
3727 	  /* Other relocs do not appear here.  */
3728 	  bfd_set_error (bfd_error_bad_value);
3729 	  return FALSE;
3730         }
3731     }
3732 
3733   return TRUE;
3734 }
3735 
3736 /* Set the sizes of the dynamic sections.  */
3737 
3738 static bfd_boolean
3739 elf_cris_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3740 				struct bfd_link_info *info)
3741 {
3742   struct elf_cris_link_hash_table * htab;
3743   bfd *dynobj;
3744   asection *s;
3745   bfd_boolean plt;
3746   bfd_boolean relocs;
3747 
3748   htab = elf_cris_hash_table (info);
3749   if (htab == NULL)
3750     return FALSE;
3751 
3752   dynobj = elf_hash_table (info)->dynobj;
3753   BFD_ASSERT (dynobj != NULL);
3754 
3755   if (elf_hash_table (info)->dynamic_sections_created)
3756     {
3757       /* Set the contents of the .interp section to the interpreter.  */
3758       if (info->executable)
3759 	{
3760 	  s = bfd_get_linker_section (dynobj, ".interp");
3761 	  BFD_ASSERT (s != NULL);
3762 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3763 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3764 	}
3765     }
3766   else
3767     {
3768       /* Adjust all expected GOTPLT uses to use a GOT entry instead.  */
3769       elf_cris_link_hash_traverse (htab, elf_cris_adjust_gotplt_to_got,
3770 				   info);
3771 
3772       /* We may have created entries in the .rela.got section.
3773 	 However, if we are not creating the dynamic sections, we will
3774 	 not actually use these entries.  Reset the size of .rela.got,
3775 	 which will cause it to get stripped from the output file
3776 	 below.  */
3777       s = bfd_get_linker_section (dynobj, ".rela.got");
3778       if (s != NULL)
3779 	s->size = 0;
3780     }
3781 
3782   /* If this is a -Bsymbolic shared link, then we need to discard all PC
3783      relative relocs against symbols defined in a regular object.  We
3784      allocated space for them in the check_relocs routine, but we will not
3785      fill them in in the relocate_section routine.  We also discard space
3786      for relocs that have become for local symbols due to symbol
3787      visibility changes.  For programs, we discard space for relocs for
3788      symbols not referenced by any dynamic object.  */
3789   if (info->shared)
3790     elf_cris_link_hash_traverse (htab,
3791 				 elf_cris_discard_excess_dso_dynamics,
3792 				 info);
3793   else
3794     elf_cris_link_hash_traverse (htab,
3795 				 elf_cris_discard_excess_program_dynamics,
3796 				 info);
3797 
3798   /* The check_relocs and adjust_dynamic_symbol entry points have
3799      determined the sizes of the various dynamic sections.  Allocate
3800      memory for them.  */
3801   plt = FALSE;
3802   relocs = FALSE;
3803   for (s = dynobj->sections; s != NULL; s = s->next)
3804     {
3805       const char *name;
3806 
3807       if ((s->flags & SEC_LINKER_CREATED) == 0)
3808 	continue;
3809 
3810       /* It's OK to base decisions on the section name, because none
3811 	 of the dynobj section names depend upon the input files.  */
3812       name = bfd_get_section_name (dynobj, s);
3813 
3814       if (strcmp (name, ".plt") == 0)
3815 	{
3816 	  /* Remember whether there is a PLT.  */
3817 	  plt = s->size != 0;
3818 	}
3819       else if (strcmp (name, ".got.plt") == 0)
3820 	{
3821 	  /* The .got.plt contains the .got header as well as the
3822 	     actual .got.plt contents.  The .got header may contain a
3823 	     R_CRIS_DTPMOD entry at index 3.  */
3824 	  s->size += htab->dtpmod_refcount != 0
3825 	    ? 8 : 0;
3826 	}
3827       else if (CONST_STRNEQ (name, ".rela"))
3828 	{
3829 	  if (strcmp (name, ".rela.got") == 0
3830 	      && htab->dtpmod_refcount != 0
3831 	      && info->shared)
3832 	    s->size += sizeof (Elf32_External_Rela);
3833 
3834 	  if (s->size != 0)
3835 	    {
3836 	      /* Remember whether there are any reloc sections other
3837                  than .rela.plt.  */
3838 	      if (strcmp (name, ".rela.plt") != 0)
3839 		  relocs = TRUE;
3840 
3841 	      /* We use the reloc_count field as a counter if we need
3842 		 to copy relocs into the output file.  */
3843 	      s->reloc_count = 0;
3844 	    }
3845 	}
3846       else if (! CONST_STRNEQ (name, ".got")
3847 	       && strcmp (name, ".dynbss") != 0)
3848 	{
3849 	  /* It's not one of our sections, so don't allocate space.  */
3850 	  continue;
3851 	}
3852 
3853       if (s->size == 0)
3854 	{
3855 	  /* If we don't need this section, strip it from the
3856 	     output file.  This is mostly to handle .rela.bss and
3857 	     .rela.plt.  We must create both sections in
3858 	     create_dynamic_sections, because they must be created
3859 	     before the linker maps input sections to output
3860 	     sections.  The linker does that before
3861 	     adjust_dynamic_symbol is called, and it is that
3862 	     function which decides whether anything needs to go
3863 	     into these sections.  */
3864 	  s->flags |= SEC_EXCLUDE;
3865 	  continue;
3866 	}
3867 
3868       if ((s->flags & SEC_HAS_CONTENTS) == 0)
3869 	continue;
3870 
3871       /* Allocate memory for the section contents. We use bfd_zalloc here
3872 	 in case unused entries are not reclaimed before the section's
3873 	 contents are written out.  This should not happen, but this way
3874 	 if it does, we will not write out garbage.  For reloc sections,
3875 	 this will make entries have the type R_CRIS_NONE.  */
3876       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3877       if (s->contents == NULL)
3878 	return FALSE;
3879     }
3880 
3881   if (elf_hash_table (info)->dynamic_sections_created)
3882     {
3883       /* Add some entries to the .dynamic section.  We fill in the
3884 	 values later, in elf_cris_finish_dynamic_sections, but we
3885 	 must add the entries now so that we get the correct size for
3886 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
3887 	 dynamic linker and used by the debugger.  */
3888 #define add_dynamic_entry(TAG, VAL) \
3889   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3890 
3891       if (!info->shared)
3892 	{
3893 	  if (!add_dynamic_entry (DT_DEBUG, 0))
3894 	    return FALSE;
3895 	}
3896 
3897       if (plt)
3898 	{
3899 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
3900 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
3901 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3902 	      || !add_dynamic_entry (DT_JMPREL, 0))
3903 	    return FALSE;
3904 	}
3905 
3906       if (relocs)
3907 	{
3908 	  if (!add_dynamic_entry (DT_RELA, 0)
3909 	      || !add_dynamic_entry (DT_RELASZ, 0)
3910 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3911 	    return FALSE;
3912 	}
3913 
3914       if ((info->flags & DF_TEXTREL) != 0)
3915 	{
3916 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
3917 	    return FALSE;
3918 	  info->flags |= DF_TEXTREL;
3919 	}
3920     }
3921 #undef add_dynamic_entry
3922 
3923   return TRUE;
3924 }
3925 
3926 /* This function is called via elf_cris_link_hash_traverse if we are
3927    creating a shared object.  In the -Bsymbolic case, it discards the
3928    space allocated to copy PC relative relocs against symbols which
3929    are defined in regular objects.  For the normal non-symbolic case,
3930    we also discard space for relocs that have become local due to
3931    symbol visibility changes.  We allocated space for them in the
3932    check_relocs routine, but we won't fill them in in the
3933    relocate_section routine.  */
3934 
3935 static bfd_boolean
3936 elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *h,
3937 				      void * inf)
3938 {
3939   struct elf_cris_pcrel_relocs_copied *s;
3940   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3941 
3942   /* If a symbol has been forced local or we have found a regular
3943      definition for the symbolic link case, then we won't be needing
3944      any relocs.  */
3945   if (h->root.def_regular
3946       && (h->root.forced_local
3947 	  || info->symbolic))
3948     {
3949       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3950 	{
3951 	  asection *sreloc
3952 	    = _bfd_elf_get_dynamic_reloc_section (elf_hash_table (info)
3953 						  ->dynobj,
3954 						  s->section,
3955 						  /*rela?*/ TRUE);
3956 	  sreloc->size -= s->count * sizeof (Elf32_External_Rela);
3957 	}
3958       return TRUE;
3959     }
3960 
3961   /* If we have accounted for PC-relative relocs for read-only
3962      sections, now is the time to warn for them.  We can't do it in
3963      cris_elf_check_relocs, because we don't know the status of all
3964      symbols at that time (and it's common to force symbols local
3965      late).  */
3966 
3967   for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3968     if ((s->section->flags & SEC_READONLY) != 0)
3969       {
3970 	/* FIXME: How do we make this optionally a warning only?  */
3971 	(*_bfd_error_handler)
3972 	  (_("%B, section `%A', to symbol `%s':\n"
3973 	     "  relocation %s should not be used"
3974 	     " in a shared object; recompile with -fPIC"),
3975 	   s->section->owner,
3976 	   s->section,
3977 	   h->root.root.root.string,
3978 	   cris_elf_howto_table[s->r_type].name);
3979 
3980 	info->flags |= DF_TEXTREL;
3981       }
3982 
3983   return TRUE;
3984 }
3985 
3986 /* This function is called via elf_cris_link_hash_traverse if we are *not*
3987    creating a shared object.  We discard space for relocs for symbols put
3988    in the .got, but which we found we do not have to resolve at run-time.  */
3989 
3990 static bfd_boolean
3991 elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *h,
3992 					  void * inf)
3993 {
3994   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3995 
3996   /* If we're not creating a shared library and have a symbol which is
3997      referred to by .got references, but the symbol is defined locally,
3998      (or rather, not defined by a DSO) then lose the reloc for the .got
3999      (don't allocate room for it).  Likewise for relocs for something
4000      for which we create a PLT.  */
4001   if (!h->root.def_dynamic
4002       || h->root.plt.refcount > 0)
4003     {
4004       if (h->reg_got_refcount > 0
4005 	  /* The size of this section is only valid and in sync with the
4006 	     various reference counts if we do dynamic; don't decrement it
4007 	     otherwise.  */
4008 	  && elf_hash_table (info)->dynamic_sections_created)
4009 	{
4010 	  bfd *dynobj = elf_hash_table (info)->dynobj;
4011 	  asection *srelgot;
4012 
4013 	  BFD_ASSERT (dynobj != NULL);
4014 
4015 	  srelgot = bfd_get_linker_section (dynobj, ".rela.got");
4016 
4017 	  BFD_ASSERT (srelgot != NULL);
4018 
4019 	  srelgot->size -= sizeof (Elf32_External_Rela);
4020 	}
4021 
4022       /* If the locally-defined symbol isn't used by a DSO, then we don't
4023 	 have to export it as a dynamic symbol.  This was already done for
4024 	 functions; doing this for all symbols would presumably not
4025 	 introduce new problems.  Of course we don't do this if we're
4026 	 exporting all dynamic symbols.  */
4027       if (! info->export_dynamic
4028 	  && h->root.dynindx != -1
4029 	  && !h->root.def_dynamic
4030 	  && !h->root.ref_dynamic)
4031 	{
4032 	  h->root.dynindx = -1;
4033 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4034 				  h->root.dynstr_index);
4035 	}
4036     }
4037 
4038   return TRUE;
4039 }
4040 
4041 /* Reject a file depending on presence and expectation of prefixed
4042    underscores on symbols.  */
4043 
4044 static bfd_boolean
4045 cris_elf_object_p (bfd *abfd)
4046 {
4047   if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
4048     return FALSE;
4049 
4050   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
4051     return (bfd_get_symbol_leading_char (abfd) == '_');
4052   else
4053     return (bfd_get_symbol_leading_char (abfd) == 0);
4054 }
4055 
4056 /* Mark presence or absence of leading underscore.  Set machine type
4057    flags from mach type.  */
4058 
4059 static void
4060 cris_elf_final_write_processing (bfd *abfd,
4061 				 bfd_boolean linker ATTRIBUTE_UNUSED)
4062 {
4063   unsigned long e_flags = elf_elfheader (abfd)->e_flags;
4064 
4065   e_flags &= ~EF_CRIS_UNDERSCORE;
4066   if (bfd_get_symbol_leading_char (abfd) == '_')
4067     e_flags |= EF_CRIS_UNDERSCORE;
4068 
4069   switch (bfd_get_mach (abfd))
4070     {
4071     case bfd_mach_cris_v0_v10:
4072       e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
4073       break;
4074 
4075     case bfd_mach_cris_v10_v32:
4076       e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
4077       break;
4078 
4079     case bfd_mach_cris_v32:
4080       e_flags |= EF_CRIS_VARIANT_V32;
4081       break;
4082 
4083     default:
4084       _bfd_abort (__FILE__, __LINE__,
4085 		  _("Unexpected machine number"));
4086     }
4087 
4088   elf_elfheader (abfd)->e_flags = e_flags;
4089 }
4090 
4091 /* Set the mach type from e_flags value.  */
4092 
4093 static bfd_boolean
4094 cris_elf_set_mach_from_flags (bfd *abfd,
4095 			      unsigned long flags)
4096 {
4097   switch (flags & EF_CRIS_VARIANT_MASK)
4098     {
4099     case EF_CRIS_VARIANT_ANY_V0_V10:
4100       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
4101       break;
4102 
4103     case EF_CRIS_VARIANT_V32:
4104       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
4105       break;
4106 
4107     case EF_CRIS_VARIANT_COMMON_V10_V32:
4108       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
4109       break;
4110 
4111     default:
4112       /* Since we don't recognize them, we obviously can't support them
4113 	 with this code; we'd have to require that all future handling
4114 	 would be optional.  */
4115       bfd_set_error (bfd_error_wrong_format);
4116       return FALSE;
4117     }
4118 
4119   return TRUE;
4120 }
4121 
4122 /* Display the flags field.  */
4123 
4124 static bfd_boolean
4125 cris_elf_print_private_bfd_data (bfd *abfd, void * ptr)
4126 {
4127   FILE *file = (FILE *) ptr;
4128 
4129   BFD_ASSERT (abfd != NULL && ptr != NULL);
4130 
4131   _bfd_elf_print_private_bfd_data (abfd, ptr);
4132 
4133   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
4134 
4135   if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
4136     fprintf (file, _(" [symbols have a _ prefix]"));
4137   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
4138       == EF_CRIS_VARIANT_COMMON_V10_V32)
4139     fprintf (file, _(" [v10 and v32]"));
4140   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
4141       == EF_CRIS_VARIANT_V32)
4142     fprintf (file, _(" [v32]"));
4143 
4144   fputc ('\n', file);
4145   return TRUE;
4146 }
4147 
4148 /* Don't mix files with and without a leading underscore.  */
4149 
4150 static bfd_boolean
4151 cris_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4152 {
4153   int imach, omach;
4154 
4155   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
4156     return FALSE;
4157 
4158   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4159       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4160     return TRUE;
4161 
4162   imach = bfd_get_mach (ibfd);
4163 
4164   if (! elf_flags_init (obfd))
4165     {
4166       /* This happens when ld starts out with a 'blank' output file.  */
4167       elf_flags_init (obfd) = TRUE;
4168 
4169       /* We ignore the linker-set mach, and instead set it according to
4170 	 the first input file.  This would also happen if we could
4171 	 somehow filter out the OUTPUT_ARCH () setting from elf.sc.
4172 	 This allows us to keep the same linker config across
4173 	 cris(v0..v10) and crisv32.  The drawback is that we can't force
4174 	 the output type, which might be a sane thing to do for a
4175 	 v10+v32 compatibility object.  */
4176       if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
4177 	return FALSE;
4178     }
4179 
4180   if (bfd_get_symbol_leading_char (ibfd)
4181       != bfd_get_symbol_leading_char (obfd))
4182     {
4183       (*_bfd_error_handler)
4184 	(bfd_get_symbol_leading_char (ibfd) == '_'
4185 	 ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols")
4186 	 : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
4187 	 ibfd);
4188       bfd_set_error (bfd_error_bad_value);
4189       return FALSE;
4190     }
4191 
4192   omach = bfd_get_mach (obfd);
4193 
4194   if (imach != omach)
4195     {
4196       /* We can get an incompatible combination only if either is
4197 	 bfd_mach_cris_v32, and the other one isn't compatible.  */
4198       if ((imach == bfd_mach_cris_v32
4199 	   && omach != bfd_mach_cris_v10_v32)
4200 	  || (omach == bfd_mach_cris_v32
4201 	      && imach != bfd_mach_cris_v10_v32))
4202 	{
4203 	  (*_bfd_error_handler)
4204 	    ((imach == bfd_mach_cris_v32)
4205 	     ? _("%B contains CRIS v32 code, incompatible"
4206 		 " with previous objects")
4207 	     : _("%B contains non-CRIS-v32 code, incompatible"
4208 		 " with previous objects"),
4209 	     ibfd);
4210 	  bfd_set_error (bfd_error_bad_value);
4211 	  return FALSE;
4212 	}
4213 
4214       /* We don't have to check the case where the input is compatible
4215 	 with v10 and v32, because the output is already known to be set
4216 	 to the other (compatible) mach.  */
4217       if (omach == bfd_mach_cris_v10_v32
4218 	  && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
4219 	return FALSE;
4220     }
4221 
4222   return TRUE;
4223 }
4224 
4225 /* Do side-effects of e_flags copying to obfd.  */
4226 
4227 static bfd_boolean
4228 cris_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
4229 {
4230   /* Call the base function.  */
4231   if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
4232     return FALSE;
4233 
4234   /* If output is big-endian for some obscure reason, stop here.  */
4235   if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
4236     return FALSE;
4237 
4238   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4239       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4240     return TRUE;
4241 
4242   /* Do what we really came here for.  */
4243   return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
4244 }
4245 
4246 static enum elf_reloc_type_class
4247 elf_cris_reloc_type_class (const Elf_Internal_Rela *rela)
4248 {
4249   enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rela->r_info);
4250   switch (r_type)
4251     {
4252     case R_CRIS_RELATIVE:
4253       return reloc_class_relative;
4254     case R_CRIS_JUMP_SLOT:
4255       return reloc_class_plt;
4256     case R_CRIS_COPY:
4257       return reloc_class_copy;
4258     default:
4259       return reloc_class_normal;
4260     }
4261 }
4262 
4263 /* The elf_backend_got_elt_size worker.  For one symbol, we can have up to
4264    two GOT entries from three types with two different sizes.  We handle
4265    it as a single entry, so we can use the regular offset-calculation
4266    machinery.  */
4267 
4268 static bfd_vma
4269 elf_cris_got_elt_size (bfd *abfd ATTRIBUTE_UNUSED,
4270 		       struct bfd_link_info *info ATTRIBUTE_UNUSED,
4271 		       struct elf_link_hash_entry *hr,
4272 		       bfd *ibfd,
4273 		       unsigned long symndx)
4274 {
4275   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) hr;
4276   bfd_vma eltsiz = 0;
4277 
4278   /* We may have one regular GOT entry or up to two TLS GOT
4279      entries.  */
4280   if (h == NULL)
4281     {
4282       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4283       bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (ibfd);
4284 
4285       BFD_ASSERT (local_got_refcounts != NULL);
4286 
4287       if (local_got_refcounts[LGOT_REG_NDX (symndx)] > 0)
4288 	{
4289 	  /* We can't have a variable referred to both as a regular
4290 	     variable and through TLS relocs.  */
4291 	  BFD_ASSERT (local_got_refcounts[LGOT_DTP_NDX (symndx)] == 0
4292 		      && local_got_refcounts[LGOT_TPREL_NDX (symndx)] == 0);
4293 	  return 4;
4294 	}
4295 
4296       if (local_got_refcounts[LGOT_DTP_NDX (symndx)] > 0)
4297 	eltsiz += 8;
4298 
4299       if (local_got_refcounts[LGOT_TPREL_NDX (symndx)] > 0)
4300 	eltsiz += 4;
4301     }
4302   else
4303     {
4304       struct elf_cris_link_hash_entry *hh = elf_cris_hash_entry (h);
4305       if (hh->reg_got_refcount > 0)
4306 	{
4307 	  /* The actual error-on-input is emitted elsewhere.  */
4308 	  BFD_ASSERT (hh->dtp_refcount == 0 && hh->tprel_refcount == 0);
4309 	  return 4;
4310 	}
4311 
4312       if (hh->dtp_refcount > 0)
4313 	eltsiz += 8;
4314 
4315       if (hh->tprel_refcount > 0)
4316 	eltsiz += 4;
4317     }
4318 
4319   /* We're only called when h->got.refcount is non-zero, so we must
4320      have a non-zero size.  */
4321   BFD_ASSERT (eltsiz != 0);
4322   return eltsiz;
4323 }
4324 
4325 #define ELF_ARCH		bfd_arch_cris
4326 #define ELF_TARGET_ID		CRIS_ELF_DATA
4327 #define ELF_MACHINE_CODE	EM_CRIS
4328 #define ELF_MAXPAGESIZE		0x2000
4329 
4330 #define TARGET_LITTLE_SYM	bfd_elf32_cris_vec
4331 #define TARGET_LITTLE_NAME	"elf32-cris"
4332 #define elf_symbol_leading_char 0
4333 
4334 #define elf_info_to_howto_rel			NULL
4335 #define elf_info_to_howto			cris_info_to_howto_rela
4336 #define elf_backend_relocate_section		cris_elf_relocate_section
4337 #define elf_backend_gc_mark_hook		cris_elf_gc_mark_hook
4338 #define elf_backend_gc_sweep_hook		cris_elf_gc_sweep_hook
4339 #define elf_backend_plt_sym_val			cris_elf_plt_sym_val
4340 #define elf_backend_check_relocs                cris_elf_check_relocs
4341 #define elf_backend_grok_prstatus		cris_elf_grok_prstatus
4342 #define elf_backend_grok_psinfo			cris_elf_grok_psinfo
4343 
4344 #define elf_backend_can_gc_sections		1
4345 #define elf_backend_can_refcount		1
4346 
4347 #define elf_backend_object_p			cris_elf_object_p
4348 #define elf_backend_final_write_processing \
4349 	cris_elf_final_write_processing
4350 #define bfd_elf32_bfd_print_private_bfd_data \
4351 	cris_elf_print_private_bfd_data
4352 #define bfd_elf32_bfd_merge_private_bfd_data \
4353 	cris_elf_merge_private_bfd_data
4354 #define bfd_elf32_bfd_copy_private_bfd_data \
4355 	cris_elf_copy_private_bfd_data
4356 
4357 #define bfd_elf32_bfd_reloc_type_lookup		cris_reloc_type_lookup
4358 #define bfd_elf32_bfd_reloc_name_lookup	cris_reloc_name_lookup
4359 
4360 #define bfd_elf32_bfd_link_hash_table_create \
4361 	elf_cris_link_hash_table_create
4362 #define elf_backend_adjust_dynamic_symbol \
4363 	elf_cris_adjust_dynamic_symbol
4364 #define elf_backend_copy_indirect_symbol \
4365 	elf_cris_copy_indirect_symbol
4366 #define elf_backend_size_dynamic_sections \
4367 	elf_cris_size_dynamic_sections
4368 #define elf_backend_init_index_section		_bfd_elf_init_1_index_section
4369 #define elf_backend_finish_dynamic_symbol \
4370 	elf_cris_finish_dynamic_symbol
4371 #define elf_backend_finish_dynamic_sections \
4372 	elf_cris_finish_dynamic_sections
4373 #define elf_backend_create_dynamic_sections \
4374 	_bfd_elf_create_dynamic_sections
4375 #define bfd_elf32_bfd_final_link \
4376 	bfd_elf_gc_common_final_link
4377 #define elf_backend_hide_symbol			elf_cris_hide_symbol
4378 #define elf_backend_reloc_type_class		elf_cris_reloc_type_class
4379 
4380 #define elf_backend_want_got_plt	1
4381 #define elf_backend_plt_readonly	1
4382 #define elf_backend_want_plt_sym	0
4383 #define elf_backend_got_header_size	12
4384 #define elf_backend_got_elt_size elf_cris_got_elt_size
4385 
4386 /* Later, we my want to optimize RELA entries into REL entries for dynamic
4387    linking and libraries (if it's a win of any significance).  Until then,
4388    take the easy route.  */
4389 #define elf_backend_may_use_rel_p 0
4390 #define elf_backend_may_use_rela_p 1
4391 #define elf_backend_rela_normal		1
4392 
4393 #include "elf32-target.h"
4394 
4395 #undef TARGET_LITTLE_SYM
4396 #undef TARGET_LITTLE_NAME
4397 #undef elf_symbol_leading_char
4398 
4399 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
4400 #define TARGET_LITTLE_NAME "elf32-us-cris"
4401 #define elf_symbol_leading_char '_'
4402 #undef elf32_bed
4403 #define elf32_bed elf32_us_cris_bed
4404 
4405 #include "elf32-target.h"
4406