1 /* CRIS-specific support for 32-bit ELF.
2    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006
3    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 2 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, MA 02110-1301, USA.  */
23 
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "elf/cris.h"
29 
30 /* Forward declarations.  */
31 static reloc_howto_type * cris_reloc_type_lookup
32   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
33 
34 static void cris_info_to_howto_rela
35   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
36 
37 static bfd_reloc_status_type cris_elf_pcrel_reloc
38   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
39 
40 static bfd_boolean cris_elf_grok_prstatus
41   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
42 
43 static bfd_boolean cris_elf_grok_psinfo
44   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
45 
46 static bfd_boolean cris_elf_relocate_section
47   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
48 	   Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
49 
50 static bfd_reloc_status_type cris_final_link_relocate
51   PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
52 	   Elf_Internal_Rela *, bfd_vma));
53 
54 static bfd_boolean cris_elf_gc_sweep_hook
55   PARAMS ((bfd *, struct bfd_link_info *, asection *,
56 	   const Elf_Internal_Rela *));
57 
58 static asection * cris_elf_gc_mark_hook
59   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
60 	   struct elf_link_hash_entry *, Elf_Internal_Sym *));
61 
62 static bfd_boolean cris_elf_object_p PARAMS ((bfd *));
63 
64 static void cris_elf_final_write_processing PARAMS ((bfd *, bfd_boolean));
65 
66 static bfd_boolean cris_elf_set_mach_from_flags
67   PARAMS ((bfd *, unsigned long int));
68 
69 static bfd_boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
70 
71 static bfd_boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
72 static bfd_boolean cris_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
73 
74 struct elf_cris_link_hash_entry;
75 static bfd_boolean elf_cris_discard_excess_dso_dynamics
76   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
77 static bfd_boolean elf_cris_discard_excess_program_dynamics
78   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
79 static bfd_boolean elf_cris_adjust_gotplt_to_got
80   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
81 static bfd_boolean elf_cris_try_fold_plt_to_got
82   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
83 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
84   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
85 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
86   PARAMS ((bfd *));
87 static bfd_boolean elf_cris_adjust_dynamic_symbol
88   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
89 static bfd_boolean cris_elf_check_relocs
90   PARAMS ((bfd *, struct bfd_link_info *, asection *,
91 	   const Elf_Internal_Rela *));
92 
93 static bfd_boolean elf_cris_size_dynamic_sections
94   PARAMS ((bfd *, struct bfd_link_info *));
95 static bfd_boolean elf_cris_finish_dynamic_symbol
96   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
97 	   Elf_Internal_Sym *));
98 static bfd_boolean elf_cris_finish_dynamic_sections
99   PARAMS ((bfd *, struct bfd_link_info *));
100 static void elf_cris_hide_symbol
101   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
102 static enum elf_reloc_type_class elf_cris_reloc_type_class
103   PARAMS ((const Elf_Internal_Rela *));
104 
105 static reloc_howto_type cris_elf_howto_table [] =
106 {
107   /* This reloc does nothing.  */
108   HOWTO (R_CRIS_NONE,		/* type */
109 	 0,			/* rightshift */
110 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
111 	 32,			/* bitsize */
112 	 FALSE,			/* pc_relative */
113 	 0,			/* bitpos */
114 	 complain_overflow_bitfield, /* complain_on_overflow */
115 	 bfd_elf_generic_reloc,	/* special_function */
116 	 "R_CRIS_NONE",		/* name */
117 	 FALSE,			/* partial_inplace */
118 	 0,			/* src_mask */
119 	 0,			/* dst_mask */
120 	 FALSE),		/* pcrel_offset */
121 
122   /* An 8 bit absolute relocation.  */
123   HOWTO (R_CRIS_8,		/* type */
124 	 0,			/* rightshift */
125 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
126 	 8,			/* bitsize */
127 	 FALSE,			/* pc_relative */
128 	 0,			/* bitpos */
129 	 complain_overflow_bitfield, /* complain_on_overflow */
130 	 bfd_elf_generic_reloc,	/* special_function */
131 	 "R_CRIS_8",		/* name */
132 	 FALSE,			/* partial_inplace */
133 	 0x0000,		/* src_mask */
134 	 0x00ff,		/* dst_mask */
135 	 FALSE),		/* pcrel_offset */
136 
137   /* A 16 bit absolute relocation.  */
138   HOWTO (R_CRIS_16,		/* type */
139 	 0,			/* rightshift */
140 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
141 	 16,			/* bitsize */
142 	 FALSE,			/* pc_relative */
143 	 0,			/* bitpos */
144 	 complain_overflow_bitfield, /* complain_on_overflow */
145 	 bfd_elf_generic_reloc,	/* special_function */
146 	 "R_CRIS_16",		/* name */
147 	 FALSE,			/* partial_inplace */
148 	 0x00000000,		/* src_mask */
149 	 0x0000ffff,		/* dst_mask */
150 	 FALSE),		/* pcrel_offset */
151 
152   /* A 32 bit absolute relocation.  */
153   HOWTO (R_CRIS_32,		/* type */
154 	 0,			/* rightshift */
155 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
156 	 32,			/* bitsize */
157 	 FALSE,			/* pc_relative */
158 	 0,			/* bitpos */
159 	 /* We don't want overflow complaints for 64-bit vma builds
160 	    for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
161 	    32-bit ELF) where sym=0xc0001234.
162 	    Don't do this for the PIC relocs, as we don't expect to
163 	    see them with large offsets.  */
164 	 complain_overflow_dont, /* complain_on_overflow */
165 	 bfd_elf_generic_reloc,	/* special_function */
166 	 "R_CRIS_32",		/* name */
167 	 FALSE,			/* partial_inplace */
168 	 0x00000000,		/* src_mask */
169 	 0xffffffff,		/* dst_mask */
170 	 FALSE),		/* pcrel_offset */
171 
172   /* An 8 bit PC-relative relocation.  */
173   HOWTO (R_CRIS_8_PCREL,	/* type */
174 	 0,			/* rightshift */
175 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
176 	 8,			/* bitsize */
177 	 TRUE,			/* pc_relative */
178 	 0,			/* bitpos */
179 	 complain_overflow_bitfield, /* complain_on_overflow */
180 	 cris_elf_pcrel_reloc,	/* special_function */
181 	 "R_CRIS_8_PCREL",	/* name */
182 	 FALSE,			/* partial_inplace */
183 	 0x0000,		/* src_mask */
184 	 0x00ff,		/* dst_mask */
185 	 TRUE),			/* pcrel_offset */
186 
187   /* A 16 bit PC-relative relocation.  */
188   HOWTO (R_CRIS_16_PCREL,	/* type */
189 	 0,			/* rightshift */
190 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
191 	 16,			/* bitsize */
192 	 TRUE,			/* pc_relative */
193 	 0,			/* bitpos */
194 	 complain_overflow_bitfield, /* complain_on_overflow */
195 	 cris_elf_pcrel_reloc,	/* special_function */
196 	 "R_CRIS_16_PCREL",	/* name */
197 	 FALSE,			/* partial_inplace */
198 	 0x00000000,		/* src_mask */
199 	 0x0000ffff,		/* dst_mask */
200 	 TRUE),			/* pcrel_offset */
201 
202   /* A 32 bit PC-relative relocation.  */
203   HOWTO (R_CRIS_32_PCREL,	/* type */
204 	 0,			/* rightshift */
205 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
206 	 32,			/* bitsize */
207 	 TRUE,			/* pc_relative */
208 	 0,			/* bitpos */
209 	 complain_overflow_bitfield, /* complain_on_overflow */
210 	 cris_elf_pcrel_reloc,	/* special_function */
211 	 "R_CRIS_32_PCREL",	/* name */
212 	 FALSE,			/* partial_inplace */
213 	 0x00000000,		/* src_mask */
214 	 0xffffffff,		/* dst_mask */
215 	 TRUE),			/* pcrel_offset */
216 
217   /* GNU extension to record C++ vtable hierarchy.  */
218   HOWTO (R_CRIS_GNU_VTINHERIT,	/* type */
219 	 0,			/* rightshift */
220 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
221 	 0,			/* bitsize */
222 	 FALSE,			/* pc_relative */
223 	 0,			/* bitpos */
224 	 complain_overflow_dont, /* complain_on_overflow */
225 	 NULL,			/* special_function */
226 	 "R_CRIS_GNU_VTINHERIT", /* name */
227 	 FALSE,			/* partial_inplace */
228 	 0,			/* src_mask */
229 	 0,			/* dst_mask */
230 	 FALSE),		/* pcrel_offset */
231 
232   /* GNU extension to record C++ vtable member usage.  */
233   HOWTO (R_CRIS_GNU_VTENTRY,	/* type */
234 	 0,			/* rightshift */
235 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
236 	 0,			/* bitsize */
237 	 FALSE,			/* pc_relative */
238 	 0,			/* bitpos */
239 	 complain_overflow_dont, /* complain_on_overflow */
240 	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
241 	 "R_CRIS_GNU_VTENTRY",	 /* name */
242 	 FALSE,			/* partial_inplace */
243 	 0,			/* src_mask */
244 	 0,			/* dst_mask */
245 	 FALSE),		/* pcrel_offset */
246 
247   /* This is used only by the dynamic linker.  The symbol should exist
248      both in the object being run and in some shared library.  The
249      dynamic linker copies the data addressed by the symbol from the
250      shared library into the object, because the object being
251      run has to have the data at some particular address.  */
252   HOWTO (R_CRIS_COPY,		/* type */
253 	 0,			/* rightshift */
254 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
255 	 32,			/* bitsize */
256 	 FALSE,			/* pc_relative */
257 	 0,			/* bitpos */
258 	 complain_overflow_bitfield, /* complain_on_overflow */
259 	 bfd_elf_generic_reloc,	/* special_function */
260 	 "R_CRIS_COPY",		/* name */
261 	 FALSE,			/* partial_inplace */
262 	 0,			/* src_mask */
263 	 0,			/* dst_mask */
264 	 FALSE),		/* pcrel_offset */
265 
266   /* Like R_CRIS_32, but used when setting global offset table entries.  */
267   HOWTO (R_CRIS_GLOB_DAT,	/* type */
268 	 0,			/* rightshift */
269 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
270 	 32,			/* bitsize */
271 	 FALSE,			/* pc_relative */
272 	 0,			/* bitpos */
273 	 complain_overflow_bitfield, /* complain_on_overflow */
274 	 bfd_elf_generic_reloc,	/* special_function */
275 	 "R_CRIS_GLOB_DAT",	/* name */
276 	 FALSE,			/* partial_inplace */
277 	 0,			/* src_mask */
278 	 0xffffffff,		/* dst_mask */
279 	 FALSE),		/* pcrel_offset */
280 
281   /* Marks a procedure linkage table entry for a symbol.  */
282   HOWTO (R_CRIS_JUMP_SLOT,	/* type */
283 	 0,			/* rightshift */
284 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
285 	 32,			/* bitsize */
286 	 FALSE,			/* pc_relative */
287 	 0,			/* bitpos */
288 	 complain_overflow_bitfield, /* complain_on_overflow */
289 	 bfd_elf_generic_reloc,	/* special_function */
290 	 "R_CRIS_JUMP_SLOT",	/* name */
291 	 FALSE,			/* partial_inplace */
292 	 0,			/* src_mask */
293 	 0,			/* dst_mask */
294 	 FALSE),		/* pcrel_offset */
295 
296   /* Used only by the dynamic linker.  When the object is run, this
297      longword is set to the load address of the object, plus the
298      addend.  */
299   HOWTO (R_CRIS_RELATIVE,	/* type */
300 	 0,			/* rightshift */
301 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
302 	 32,			/* bitsize */
303 	 FALSE,			/* pc_relative */
304 	 0,			/* bitpos */
305 	 complain_overflow_bitfield, /* complain_on_overflow */
306 	 bfd_elf_generic_reloc,	/* special_function */
307 	 "R_CRIS_RELATIVE",	/* name */
308 	 FALSE,			/* partial_inplace */
309 	 0,			/* src_mask */
310 	 0xffffffff,		/* dst_mask */
311 	 FALSE),		/* pcrel_offset */
312 
313   /* Like R_CRIS_32, but referring to the GOT table entry for the symbol.  */
314   HOWTO (R_CRIS_16_GOT,		/* type */
315 	 0,			/* rightshift */
316 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
317 	 16,			/* bitsize */
318 	 FALSE,			/* pc_relative */
319 	 0,			/* bitpos */
320 	 complain_overflow_bitfield, /* complain_on_overflow */
321 	 bfd_elf_generic_reloc,	/* special_function */
322 	 "R_CRIS_16_GOT",	/* name */
323 	 FALSE,			/* partial_inplace */
324 	 0,			/* src_mask */
325 	 0xffff,		/* dst_mask */
326 	 FALSE),		/* pcrel_offset */
327 
328   HOWTO (R_CRIS_32_GOT,		/* type */
329 	 0,			/* rightshift */
330 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
331 	 32,			/* bitsize */
332 	 FALSE,			/* pc_relative */
333 	 0,			/* bitpos */
334 	 complain_overflow_bitfield, /* complain_on_overflow */
335 	 bfd_elf_generic_reloc,	/* special_function */
336 	 "R_CRIS_32_GOT",	/* name */
337 	 FALSE,			/* partial_inplace */
338 	 0,			/* src_mask */
339 	 0xffffffff,		/* dst_mask */
340 	 FALSE),		/* pcrel_offset */
341 
342   /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
343      the GOT table for the symbol.  */
344   HOWTO (R_CRIS_16_GOTPLT,	/* type */
345 	 0,			/* rightshift */
346 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
347 	 16,			/* bitsize */
348 	 FALSE,			/* pc_relative */
349 	 0,			/* bitpos */
350 	 complain_overflow_bitfield, /* complain_on_overflow */
351 	 bfd_elf_generic_reloc,	/* special_function */
352 	 "R_CRIS_16_GOTPLT",	/* name */
353 	 FALSE,			/* partial_inplace */
354 	 0,			/* src_mask */
355 	 0xffff,		/* dst_mask */
356 	 FALSE),		/* pcrel_offset */
357 
358   HOWTO (R_CRIS_32_GOTPLT,	/* type */
359 	 0,			/* rightshift */
360 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
361 	 32,			/* bitsize */
362 	 FALSE,			/* pc_relative */
363 	 0,			/* bitpos */
364 	 complain_overflow_bitfield, /* complain_on_overflow */
365 	 bfd_elf_generic_reloc,	/* special_function */
366 	 "R_CRIS_32_GOTPLT",	/* name */
367 	 FALSE,			/* partial_inplace */
368 	 0,			/* src_mask */
369 	 0xffffffff,		/* dst_mask */
370 	 FALSE),		/* pcrel_offset */
371 
372   /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
373      be necessary.  */
374   HOWTO (R_CRIS_32_GOTREL,	/* type */
375 	 0,			/* rightshift */
376 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
377 	 32,			/* bitsize */
378 	 FALSE,			/* pc_relative */
379 	 0,			/* bitpos */
380 	 complain_overflow_bitfield, /* complain_on_overflow */
381 	 bfd_elf_generic_reloc,	/* special_function */
382 	 "R_CRIS_32_GOTREL",	/* name */
383 	 FALSE,			/* partial_inplace */
384 	 0,			/* src_mask */
385 	 0xffffffff,		/* dst_mask */
386 	 FALSE),		/* pcrel_offset */
387 
388   /* A 32-bit offset from GOT to entry for this symbol in PLT and request
389      to create PLT entry for symbol.  */
390   HOWTO (R_CRIS_32_PLT_GOTREL,	/* type */
391 	 0,			/* rightshift */
392 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
393 	 32,			/* bitsize */
394 	 FALSE,			/* pc_relative */
395 	 0,			/* bitpos */
396 	 complain_overflow_bitfield, /* complain_on_overflow */
397 	 bfd_elf_generic_reloc,	/* special_function */
398 	 "R_CRIS_32_PLT_GOTREL", /* name */
399 	 FALSE,			/* partial_inplace */
400 	 0,			/* src_mask */
401 	 0xffffffff,		/* dst_mask */
402 	 FALSE),		/* pcrel_offset */
403 
404   /* A 32-bit offset from PC (location after the relocation) + addend to
405      entry for this symbol in PLT and request to create PLT entry for
406      symbol.  */
407   HOWTO (R_CRIS_32_PLT_PCREL,	/* type */
408 	 0,			/* rightshift */
409 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
410 	 32,			/* bitsize */
411 	 TRUE,			/* pc_relative */
412 	 0,			/* bitpos */
413 	 complain_overflow_bitfield, /* complain_on_overflow */
414 	 cris_elf_pcrel_reloc,	/* special_function */
415 	 "R_CRIS_32_PLT_PCREL",	/* name */
416 	 FALSE,			/* partial_inplace */
417 	 0,			/* src_mask */
418 	 0xffffffff,		/* dst_mask */
419 	 TRUE)			/* pcrel_offset */
420 };
421 
422 /* Map BFD reloc types to CRIS ELF reloc types.  */
423 
424 struct cris_reloc_map
425 {
426   bfd_reloc_code_real_type bfd_reloc_val;
427   unsigned int cris_reloc_val;
428 };
429 
430 static const struct cris_reloc_map cris_reloc_map [] =
431 {
432   { BFD_RELOC_NONE,		R_CRIS_NONE },
433   { BFD_RELOC_8,		R_CRIS_8 },
434   { BFD_RELOC_16,		R_CRIS_16 },
435   { BFD_RELOC_32,		R_CRIS_32 },
436   { BFD_RELOC_8_PCREL,		R_CRIS_8_PCREL },
437   { BFD_RELOC_16_PCREL,		R_CRIS_16_PCREL },
438   { BFD_RELOC_32_PCREL,		R_CRIS_32_PCREL },
439   { BFD_RELOC_VTABLE_INHERIT,	R_CRIS_GNU_VTINHERIT },
440   { BFD_RELOC_VTABLE_ENTRY,	R_CRIS_GNU_VTENTRY },
441   { BFD_RELOC_CRIS_COPY,	R_CRIS_COPY },
442   { BFD_RELOC_CRIS_GLOB_DAT,	R_CRIS_GLOB_DAT },
443   { BFD_RELOC_CRIS_JUMP_SLOT,	R_CRIS_JUMP_SLOT },
444   { BFD_RELOC_CRIS_RELATIVE,	R_CRIS_RELATIVE },
445   { BFD_RELOC_CRIS_16_GOT,	R_CRIS_16_GOT },
446   { BFD_RELOC_CRIS_32_GOT,	R_CRIS_32_GOT },
447   { BFD_RELOC_CRIS_16_GOTPLT,	R_CRIS_16_GOTPLT },
448   { BFD_RELOC_CRIS_32_GOTPLT,	R_CRIS_32_GOTPLT },
449   { BFD_RELOC_CRIS_32_GOTREL,	R_CRIS_32_GOTREL },
450   { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
451   { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL }
452 };
453 
454 static reloc_howto_type *
cris_reloc_type_lookup(abfd,code)455 cris_reloc_type_lookup (abfd, code)
456      bfd * abfd ATTRIBUTE_UNUSED;
457      bfd_reloc_code_real_type code;
458 {
459   unsigned int i;
460 
461   for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
462     if (cris_reloc_map [i].bfd_reloc_val == code)
463       return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
464 
465   return NULL;
466 }
467 
468 /* Set the howto pointer for an CRIS ELF reloc.  */
469 
470 static void
cris_info_to_howto_rela(abfd,cache_ptr,dst)471 cris_info_to_howto_rela (abfd, cache_ptr, dst)
472      bfd * abfd ATTRIBUTE_UNUSED;
473      arelent * cache_ptr;
474      Elf_Internal_Rela * dst;
475 {
476   unsigned int r_type;
477 
478   r_type = ELF32_R_TYPE (dst->r_info);
479   BFD_ASSERT (r_type < (unsigned int) R_CRIS_max);
480   cache_ptr->howto = & cris_elf_howto_table [r_type];
481 }
482 
483 bfd_reloc_status_type
cris_elf_pcrel_reloc(abfd,reloc_entry,symbol,data,input_section,output_bfd,error_message)484 cris_elf_pcrel_reloc (abfd, reloc_entry, symbol, data, input_section,
485 		      output_bfd, error_message)
486      bfd *abfd ATTRIBUTE_UNUSED;
487      arelent *reloc_entry;
488      asymbol *symbol;
489      PTR data ATTRIBUTE_UNUSED;
490      asection *input_section;
491      bfd *output_bfd;
492      char **error_message ATTRIBUTE_UNUSED;
493 {
494   /* By default (using only bfd_elf_generic_reloc when linking to
495      non-ELF formats) PC-relative relocs are relative to the beginning
496      of the reloc.  CRIS PC-relative relocs are relative to the position
497      *after* the reloc because that's what pre-CRISv32 PC points to
498      after reading an insn field with that reloc.  (For CRISv32, PC is
499      actually relative to the start of the insn, but we keep the old
500      definition.)  Still, we use as much generic machinery as we can.
501 
502      Only adjust when doing a final link.  */
503   if (output_bfd == (bfd *) NULL)
504     reloc_entry->addend -= 1 << reloc_entry->howto->size;
505 
506   return
507     bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
508 			   input_section, output_bfd, error_message);
509 }
510 
511 /* Support for core dump NOTE sections.
512    The slightly unintuitive code layout is an attempt to keep at least
513    some similarities with other ports, hoping to simplify general
514    changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart.  */
515 
516 static bfd_boolean
cris_elf_grok_prstatus(abfd,note)517 cris_elf_grok_prstatus (abfd, note)
518      bfd *abfd;
519      Elf_Internal_Note *note;
520 {
521   int offset;
522   size_t size;
523 
524   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
525     switch (note->descsz)
526       {
527       default:
528 	return FALSE;
529 
530       case 202:		/* Linux/CRISv32 */
531 	/* pr_cursig */
532 	elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
533 
534 	/* pr_pid */
535 	elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
536 
537 	/* pr_reg */
538 	offset = 70;
539 	size = 128;
540 
541 	break;
542       }
543   else
544     switch (note->descsz)
545       {
546       default:
547 	return FALSE;
548 
549       case 214:		/* Linux/CRIS */
550 	/* pr_cursig */
551 	elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
552 
553 	/* pr_pid */
554 	elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22);
555 
556 	/* pr_reg */
557 	offset = 70;
558 	size = 140;
559 
560 	break;
561       }
562 
563   /* Make a ".reg/999" section.  */
564   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
565 					  size, note->descpos + offset);
566 }
567 
568 static bfd_boolean
cris_elf_grok_psinfo(abfd,note)569 cris_elf_grok_psinfo (abfd, note)
570      bfd *abfd;
571      Elf_Internal_Note *note;
572 {
573   if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
574     switch (note->descsz)
575       {
576       default:
577 	return FALSE;
578 
579       case 124:		/* Linux/CRISv32 elf_prpsinfo */
580 	elf_tdata (abfd)->core_program
581 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
582 	elf_tdata (abfd)->core_command
583 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
584       }
585   else
586     switch (note->descsz)
587       {
588       default:
589 	return FALSE;
590 
591       case 124:		/* Linux/CRIS elf_prpsinfo */
592 	elf_tdata (abfd)->core_program
593 	  = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
594 	elf_tdata (abfd)->core_command
595 	  = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
596       }
597 
598   /* Note that for some reason, a spurious space is tacked
599      onto the end of the args in some (at least one anyway)
600      implementations, so strip it off if it exists.  */
601 
602   {
603     char *command = elf_tdata (abfd)->core_command;
604     int n = strlen (command);
605 
606     if (0 < n && command[n - 1] == ' ')
607       command[n - 1] = '\0';
608   }
609 
610   return TRUE;
611 }
612 
613 /* The name of the dynamic interpreter.  This is put in the .interp
614    section.  */
615 
616 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
617 
618 /* The size in bytes of an entry in the procedure linkage table.  */
619 
620 #define PLT_ENTRY_SIZE 20
621 #define PLT_ENTRY_SIZE_V32 26
622 
623 /* The first entry in an absolute procedure linkage table looks like this.  */
624 
625 static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
626 {
627   0xfc, 0xe1,
628   0x7e, 0x7e,	/* push mof.  */
629   0x7f, 0x0d,   /*  (dip [pc+]) */
630   0, 0, 0, 0,	/*  Replaced with address of .got + 4.  */
631   0x30, 0x7a,	/* move [...],mof */
632   0x7f, 0x0d,   /*  (dip [pc+]) */
633   0, 0, 0, 0,	/*  Replaced with address of .got + 8.  */
634   0x30, 0x09	/* jump [...] */
635 };
636 
637 static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
638 {
639   0x84, 0xe2,	/* subq 4,$sp */
640   0x6f, 0xfe,	/* move.d 0,$acr */
641   0, 0, 0, 0,	/*  Replaced by address of .got + 4.  */
642   0x7e, 0x7a,	/* move $mof,[$sp] */
643   0x3f, 0x7a,	/* move [$acr],$mof */
644   0x04, 0xf2,	/* addq 4,acr */
645   0x6f, 0xfa,	/* move.d [$acr],$acr */
646   0xbf, 0x09,	/* jump $acr */
647   0xb0, 0x05,	/* nop */
648   0, 0		/*  Pad out to 26 bytes.  */
649 };
650 
651 /* Subsequent entries in an absolute procedure linkage table look like
652    this.  */
653 
654 static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
655 {
656   0x7f, 0x0d,   /*  (dip [pc+]) */
657   0, 0, 0, 0,	/*  Replaced with address of this symbol in .got.  */
658   0x30, 0x09,	/* jump [...] */
659   0x3f,	 0x7e,	/* move [pc+],mof */
660   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
661   0x2f, 0xfe,	/* add.d [pc+],pc */
662   0xec, 0xff,
663   0xff, 0xff	/*  Replaced with offset to start of .plt.  */
664 };
665 
666 static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
667 {
668   0x6f, 0xfe,	/* move.d 0,$acr */
669   0, 0, 0, 0,	/*  Replaced with address of this symbol in .got.  */
670   0x6f, 0xfa,   /* move.d [$acr],$acr */
671   0xbf, 0x09,   /* jump $acr */
672   0xb0, 0x05,	/* nop */
673   0x3f, 0x7e,	/* move 0,mof */
674   0, 0, 0, 0,	/*  Replaced with offset into relocation table. */
675   0xbf, 0x0e,	/* ba start_of_plt0_entry */
676   0, 0, 0, 0,	/*  Replaced with offset to plt0 entry.  */
677   0xb0, 0x05	/* nop */
678 };
679 
680 /* The first entry in a PIC procedure linkage table looks like this.  */
681 
682 static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
683 {
684   0xfc, 0xe1, 0x7e, 0x7e,	/* push mof */
685   0x04, 0x01, 0x30, 0x7a,	/* move [r0+4],mof */
686   0x08, 0x01, 0x30, 0x09,	/* jump [r0+8] */
687   0, 0, 0, 0, 0, 0, 0, 0,	/*  Pad out to 20 bytes.  */
688 };
689 
690 static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
691 {
692   0x84, 0xe2,	/* subq 4,$sp */
693   0x04, 0x01,	/* addoq 4,$r0,$acr */
694   0x7e, 0x7a,	/* move $mof,[$sp] */
695   0x3f, 0x7a,	/* move [$acr],$mof */
696   0x04, 0xf2,	/* addq 4,$acr */
697   0x6f, 0xfa,	/* move.d [$acr],$acr */
698   0xbf, 0x09,	/* jump $acr */
699   0xb0, 0x05,	/* nop */
700   0, 0,		/*  Pad out to 26 bytes.  */
701   0, 0, 0, 0,
702   0, 0, 0, 0
703 };
704 
705 /* Subsequent entries in a PIC procedure linkage table look like this.  */
706 
707 static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
708 {
709   0x6f, 0x0d,   /*  (bdap [pc+].d,r0) */
710   0, 0, 0, 0,	/*  Replaced with offset of this symbol in .got.  */
711   0x30, 0x09,	/* jump [...] */
712   0x3f, 0x7e,	/* move [pc+],mof */
713   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
714   0x2f, 0xfe,	/* add.d [pc+],pc */
715   0xec, 0xff,	/*  Replaced with offset to start of .plt.  */
716   0xff, 0xff
717 };
718 
719 static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
720 {
721   0x6f, 0x0d,	/* addo.d 0,$r0,$acr */
722   0, 0, 0, 0,	/*  Replaced with offset of this symbol in .got.  */
723   0x6f, 0xfa,	/* move.d [$acr],$acr */
724   0xbf, 0x09,	/* jump $acr */
725   0xb0, 0x05,	/* nop */
726   0x3f, 0x7e,	/* move relocoffs,$mof */
727   0, 0, 0, 0,	/*  Replaced with offset into relocation table.  */
728   0xbf, 0x0e,	/* ba start_of_plt */
729   0, 0, 0, 0,	/*  Replaced with offset to start of .plt.  */
730   0xb0, 0x05	/* nop */
731 };
732 
733 /* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
734    (and most other PIC/shlib stuff).  Check that we don't drift away
735    without reason.
736 
737    The CRIS linker, like the m68k and i386 linkers (and probably the rest
738    too) needs to keep track of the number of relocs that it decides to
739    copy in check_relocs for each symbol.  This is so that it can discard
740    PC relative relocs if it doesn't need them when linking with
741    -Bsymbolic.  We store the information in a field extending the regular
742    ELF linker hash table.  */
743 
744 /* This structure keeps track of the number of PC relative relocs we have
745    copied for a given symbol.  */
746 
747 struct elf_cris_pcrel_relocs_copied
748 {
749   /* Next section.  */
750   struct elf_cris_pcrel_relocs_copied *next;
751   /* A section in dynobj.  */
752   asection *section;
753   /* Number of relocs copied in this section.  */
754   bfd_size_type count;
755 };
756 
757 /* CRIS ELF linker hash entry.  */
758 
759 struct elf_cris_link_hash_entry
760 {
761   struct elf_link_hash_entry root;
762 
763   /* Number of PC relative relocs copied for this symbol.  */
764   struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
765 
766   /* The GOTPLT references are CRIS-specific; the goal is to avoid having
767      both a general GOT and a PLT-specific GOT entry for the same symbol,
768      when it is referenced both as a function and as a function pointer.
769 
770      Number of GOTPLT references for a function.  */
771   bfd_signed_vma gotplt_refcount;
772 
773   /* Actual GOTPLT index for this symbol, if applicable, or zero if not
774      (zero is never used as an index).  FIXME: We should be able to fold
775      this with gotplt_refcount in a union, like the got and plt unions in
776      elf_link_hash_entry.  */
777   bfd_size_type gotplt_offset;
778 };
779 
780 /* CRIS ELF linker hash table.  */
781 
782 struct elf_cris_link_hash_table
783 {
784   struct elf_link_hash_table root;
785 
786   /* We can't use the PLT offset and calculate to get the GOTPLT offset,
787      since we try and avoid creating GOTPLT:s when there's already a GOT.
788      Instead, we keep and update the next available index here.  */
789   bfd_size_type next_gotplt_entry;
790 };
791 
792 /* Traverse a CRIS ELF linker hash table.  */
793 
794 #define elf_cris_link_hash_traverse(table, func, info)			\
795   (elf_link_hash_traverse						\
796    (&(table)->root,							\
797     (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
798     (info)))
799 
800 /* Get the CRIS ELF linker hash table from a link_info structure.  */
801 
802 #define elf_cris_hash_table(p) \
803   ((struct elf_cris_link_hash_table *) (p)->hash)
804 
805 /* Create an entry in a CRIS ELF linker hash table.  */
806 
807 static struct bfd_hash_entry *
elf_cris_link_hash_newfunc(entry,table,string)808 elf_cris_link_hash_newfunc (entry, table, string)
809      struct bfd_hash_entry *entry;
810      struct bfd_hash_table *table;
811      const char *string;
812 {
813   struct elf_cris_link_hash_entry *ret =
814     (struct elf_cris_link_hash_entry *) entry;
815 
816   /* Allocate the structure if it has not already been allocated by a
817      subclass.  */
818   if (ret == (struct elf_cris_link_hash_entry *) NULL)
819     ret = ((struct elf_cris_link_hash_entry *)
820 	   bfd_hash_allocate (table,
821 			      sizeof (struct elf_cris_link_hash_entry)));
822   if (ret == (struct elf_cris_link_hash_entry *) NULL)
823     return (struct bfd_hash_entry *) ret;
824 
825   /* Call the allocation method of the superclass.  */
826   ret = ((struct elf_cris_link_hash_entry *)
827 	 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
828 				     table, string));
829   if (ret != (struct elf_cris_link_hash_entry *) NULL)
830     {
831       ret->pcrel_relocs_copied = NULL;
832       ret->gotplt_refcount = 0;
833       ret->gotplt_offset = 0;
834     }
835 
836   return (struct bfd_hash_entry *) ret;
837 }
838 
839 /* Create a CRIS ELF linker hash table.  */
840 
841 static struct bfd_link_hash_table *
elf_cris_link_hash_table_create(abfd)842 elf_cris_link_hash_table_create (abfd)
843      bfd *abfd;
844 {
845   struct elf_cris_link_hash_table *ret;
846   bfd_size_type amt = sizeof (struct elf_cris_link_hash_table);
847 
848   ret = ((struct elf_cris_link_hash_table *) bfd_malloc (amt));
849   if (ret == (struct elf_cris_link_hash_table *) NULL)
850     return NULL;
851 
852   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
853 				      elf_cris_link_hash_newfunc,
854 				      sizeof (struct elf_cris_link_hash_entry)))
855     {
856       free (ret);
857       return NULL;
858     }
859 
860   /* Initialize to skip over the first three entries in the gotplt; they
861      are used for run-time symbol evaluation.  */
862   ret->next_gotplt_entry = 12;
863 
864   return &ret->root.root;
865 }
866 
867 /* Perform a single relocation.  By default we use the standard BFD
868    routines, with a few tweaks.  */
869 
870 static bfd_reloc_status_type
cris_final_link_relocate(howto,input_bfd,input_section,contents,rel,relocation)871 cris_final_link_relocate (howto, input_bfd, input_section, contents, rel,
872 			  relocation)
873      reloc_howto_type *  howto;
874      bfd *               input_bfd;
875      asection *          input_section;
876      bfd_byte *          contents;
877      Elf_Internal_Rela * rel;
878      bfd_vma             relocation;
879 {
880   bfd_reloc_status_type r;
881 
882   /* PC-relative relocations are relative to the position *after*
883      the reloc.  Note that for R_CRIS_8_PCREL the adjustment is
884      not a single byte, since PC must be 16-bit-aligned.  */
885   switch (ELF32_R_TYPE (rel->r_info))
886     {
887       /* Check that the 16-bit GOT relocs are positive.  */
888     case R_CRIS_16_GOTPLT:
889     case R_CRIS_16_GOT:
890       if ((bfd_signed_vma) relocation < 0)
891 	return bfd_reloc_overflow;
892       break;
893 
894     case R_CRIS_32_PLT_PCREL:
895     case R_CRIS_32_PCREL:
896       relocation -= 2;
897       /* Fall through.  */
898     case R_CRIS_8_PCREL:
899     case R_CRIS_16_PCREL:
900       relocation -= 2;
901       break;
902 
903     default:
904       break;
905     }
906 
907   r = _bfd_final_link_relocate (howto, input_bfd, input_section,
908 				contents, rel->r_offset,
909 				relocation, rel->r_addend);
910   return r;
911 }
912 
913 /* Relocate an CRIS ELF section.  See elf32-fr30.c, from where this was
914    copied, for further comments.  */
915 
916 static bfd_boolean
cris_elf_relocate_section(output_bfd,info,input_bfd,input_section,contents,relocs,local_syms,local_sections)917 cris_elf_relocate_section (output_bfd, info, input_bfd, input_section,
918 			   contents, relocs, local_syms, local_sections)
919      bfd *output_bfd ATTRIBUTE_UNUSED;
920      struct bfd_link_info *info;
921      bfd *input_bfd;
922      asection *input_section;
923      bfd_byte *contents;
924      Elf_Internal_Rela *relocs;
925      Elf_Internal_Sym *local_syms;
926      asection **local_sections;
927 {
928   bfd *dynobj;
929   Elf_Internal_Shdr *symtab_hdr;
930   struct elf_link_hash_entry **sym_hashes;
931   bfd_vma *local_got_offsets;
932   asection *sgot;
933   asection *splt;
934   asection *sreloc;
935   Elf_Internal_Rela *rel;
936   Elf_Internal_Rela *relend;
937 
938   if (info->relocatable)
939     return TRUE;
940 
941   dynobj = elf_hash_table (info)->dynobj;
942   local_got_offsets = elf_local_got_offsets (input_bfd);
943   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
944   sym_hashes = elf_sym_hashes (input_bfd);
945   relend     = relocs + input_section->reloc_count;
946 
947   sgot = NULL;
948   splt = NULL;
949   sreloc = NULL;
950 
951   if (dynobj != NULL)
952     {
953       splt = bfd_get_section_by_name (dynobj, ".plt");
954       sgot = bfd_get_section_by_name (dynobj, ".got");
955     }
956 
957   for (rel = relocs; rel < relend; rel ++)
958     {
959       reloc_howto_type *howto;
960       unsigned long r_symndx;
961       Elf_Internal_Sym *sym;
962       asection *sec;
963       struct elf_link_hash_entry *h;
964       bfd_vma relocation;
965       bfd_reloc_status_type r;
966       const char *symname = NULL;
967       int r_type;
968 
969       r_type = ELF32_R_TYPE (rel->r_info);
970 
971       if (   r_type == R_CRIS_GNU_VTINHERIT
972 	  || r_type == R_CRIS_GNU_VTENTRY)
973 	continue;
974 
975       /* This is a final link.  */
976       r_symndx = ELF32_R_SYM (rel->r_info);
977       howto  = cris_elf_howto_table + r_type;
978       h      = NULL;
979       sym    = NULL;
980       sec    = NULL;
981 
982       if (r_symndx < symtab_hdr->sh_info)
983 	{
984 	  sym = local_syms + r_symndx;
985 	  sec = local_sections [r_symndx];
986 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
987 
988 	  symname = (bfd_elf_string_from_elf_section
989 		     (input_bfd, symtab_hdr->sh_link, sym->st_name));
990 	  if (symname == NULL)
991 	    symname = bfd_section_name (input_bfd, sec);
992 	}
993       else
994 	{
995 	  bfd_boolean warned;
996 	  bfd_boolean unresolved_reloc;
997 
998 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
999 				   r_symndx, symtab_hdr, sym_hashes,
1000 				   h, sec, relocation,
1001 				   unresolved_reloc, warned);
1002 
1003 	  if (unresolved_reloc
1004 	      /* Perhaps we should detect the cases that
1005 		 sec->output_section is expected to be NULL like i386 and
1006 		 m68k, but apparently (and according to elfxx-ia64.c) all
1007 		 valid cases are where the symbol is defined in a shared
1008 		 object which we link dynamically against.  This includes
1009 		 PLT relocs for which we've created a PLT entry and other
1010 		 relocs for which we're prepared to create dynamic
1011 		 relocations.
1012 
1013 		 For now, new situations cause us to just err when
1014 		 sec->output_offset is NULL but the object with the symbol
1015 		 is *not* dynamically linked against.  Thus this will
1016 		 automatically remind us so we can see if there are other
1017 		 valid cases we need to revisit.  */
1018 	      && (sec->owner->flags & DYNAMIC) != 0)
1019 	    relocation = 0;
1020 
1021 	  else if (h->root.type == bfd_link_hash_defined
1022 		   || h->root.type == bfd_link_hash_defweak)
1023 	    {
1024 	      /* Here follow the cases where the relocation value must
1025 		 be zero (or when further handling is simplified when
1026 		 zero).  I can't claim to understand the various
1027 		 conditions and they weren't described in the files
1028 		 where I copied them from (elf32-m68k.c and
1029 		 elf32-i386.c), but let's mention examples of where
1030 		 they happen.  FIXME: Perhaps define and use a
1031 		 dynamic_symbol_p function like ia64.
1032 
1033 		 - When creating a shared library, we can have an
1034 		 ordinary relocation for a symbol defined in a shared
1035 		 library (perhaps the one we create).  We then make
1036 		 the relocation value zero, as the value seen now will
1037 		 be added into the relocation addend in this shared
1038 		 library, but must be handled only at dynamic-link
1039 		 time.  FIXME: Not sure this example covers the
1040 		 h->elf_link_hash_flags test, though it's there in
1041 		 other targets.  */
1042 	      if (info->shared
1043 		  && ((! info->symbolic && h->dynindx != -1)
1044 		      || !h->def_regular)
1045 		  && (input_section->flags & SEC_ALLOC) != 0
1046 		  && (r_type == R_CRIS_8
1047 		      || r_type == R_CRIS_16
1048 		      || r_type == R_CRIS_32
1049 		      || r_type == R_CRIS_8_PCREL
1050 		      || r_type == R_CRIS_16_PCREL
1051 		      || r_type == R_CRIS_32_PCREL))
1052 		relocation = 0;
1053 	      else if (unresolved_reloc)
1054 		{
1055 		  _bfd_error_handler
1056 		    (_("%B, section %A: unresolvable relocation %s against symbol `%s'"),
1057 		     input_bfd,
1058 		     input_section,
1059 		     cris_elf_howto_table[r_type].name,
1060 		     symname);
1061 		  bfd_set_error (bfd_error_bad_value);
1062 		  return FALSE;
1063 		}
1064 	    }
1065 	}
1066 
1067       switch (r_type)
1068 	{
1069 	case R_CRIS_16_GOTPLT:
1070 	case R_CRIS_32_GOTPLT:
1071 	  /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
1072 	     but we require a PLT, and the PLT handling will take care of
1073 	     filling in the PLT-specific GOT entry.  For the GOT offset,
1074 	     calculate it as we do when filling it in for the .got.plt
1075 	     section.  If we don't have a PLT, punt to GOT handling.  */
1076 	  if (h != NULL
1077 	      && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
1078 	    {
1079 	      asection *sgotplt
1080 		= bfd_get_section_by_name (dynobj, ".got.plt");
1081 	      bfd_vma got_offset;
1082 
1083 	      BFD_ASSERT (h->dynindx != -1);
1084 	      BFD_ASSERT (sgotplt != NULL);
1085 
1086 	      got_offset
1087 		= ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1088 
1089 	      relocation = got_offset;
1090 	      break;
1091 	    }
1092 
1093 	  /* We didn't make a PLT entry for this symbol.  Maybe everything is
1094 	     folded into the GOT.  Other than folding, this happens when
1095 	     statically linking PIC code, or when using -Bsymbolic.  Check
1096 	     that we instead have a GOT entry as done for us by
1097 	     elf_cris_adjust_dynamic_symbol, and drop through into the
1098 	     ordinary GOT cases.  This must not happen for the
1099 	     executable, because any reference it does to a function
1100 	     that is satisfied by a DSO must generate a PLT.  We assume
1101 	     these call-specific relocs don't address non-functions.  */
1102 	  if (h != NULL
1103 	      && (h->got.offset == (bfd_vma) -1
1104 		  || (!info->shared
1105 		      && !(h->def_regular
1106 			   || (!h->def_dynamic
1107 			       && h->root.type == bfd_link_hash_undefweak)))))
1108 	    {
1109 	      (*_bfd_error_handler)
1110 		((h->got.offset == (bfd_vma) -1)
1111 		 ? _("%B, section %A: No PLT nor GOT for relocation %s"
1112 		     " against symbol `%s'")
1113 		 : _("%B, section %A: No PLT for relocation %s"
1114 		     " against symbol `%s'"),
1115 		 input_bfd,
1116 		 input_section,
1117 		 cris_elf_howto_table[r_type].name,
1118 		 (symname != NULL && symname[0] != '\0'
1119 		  ? symname : _("[whose name is lost]")));
1120 
1121 	      /* FIXME: Perhaps blaming input is not the right thing to
1122 		 do; this is probably an internal error.  But it is true
1123 		 that we didn't like that particular input.  */
1124 	      bfd_set_error (bfd_error_bad_value);
1125 	      return FALSE;
1126 	    }
1127 	  /* Fall through.  */
1128 
1129 	  /* The size of the actual relocation is not used here; we only
1130 	     fill in the GOT table here.  */
1131 	case R_CRIS_16_GOT:
1132 	case R_CRIS_32_GOT:
1133 	  {
1134 	    bfd_vma off;
1135 
1136 	    /* Note that despite using RELA relocations, the .got contents
1137 	       is always filled in with the link-relative relocation
1138 	       value; the addend.  */
1139 
1140 	    if (h != NULL)
1141 	      {
1142 		off = h->got.offset;
1143 		BFD_ASSERT (off != (bfd_vma) -1);
1144 
1145 		if (!elf_hash_table (info)->dynamic_sections_created
1146 		    || (! info->shared
1147 			&& (h->def_regular
1148 			    || h->type == STT_FUNC
1149 			    || h->needs_plt))
1150 		    || (info->shared
1151 			&& (info->symbolic || h->dynindx == -1)
1152 			&& h->def_regular))
1153 		  {
1154 		    /* This wasn't checked above for ! info->shared, but
1155 		       must hold there if we get here; the symbol must
1156 		       be defined in the regular program or be undefweak
1157 		       or be a function or otherwise need a PLT.  */
1158 		    BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
1159 				|| info->shared
1160 				|| h->def_regular
1161 				|| h->type == STT_FUNC
1162 				|| h->needs_plt
1163 				|| h->root.type == bfd_link_hash_undefweak);
1164 
1165 		    /* This is actually a static link, or it is a
1166 		       -Bsymbolic link and the symbol is defined locally,
1167 		       or is undefweak, or the symbol was forced to be
1168 		       local because of a version file, or we're not
1169 		       creating a dynamic object.  We must initialize this
1170 		       entry in the global offset table.  Since the offset
1171 		       must always be a multiple of 4, we use the least
1172 		       significant bit to record whether we have
1173 		       initialized it already.
1174 
1175 		       If this GOT entry should be runtime-initialized, we
1176 		       will create a .rela.got relocation entry to
1177 		       initialize the value.  This is done in the
1178 		       finish_dynamic_symbol routine.  */
1179 		    if ((off & 1) != 0)
1180 		      off &= ~1;
1181 		    else
1182 		      {
1183 			bfd_put_32 (output_bfd, relocation,
1184 				    sgot->contents + off);
1185 			h->got.offset |= 1;
1186 		      }
1187 		  }
1188 	      }
1189 	    else
1190 	      {
1191 		BFD_ASSERT (local_got_offsets != NULL
1192 			    && local_got_offsets[r_symndx] != (bfd_vma) -1);
1193 
1194 		off = local_got_offsets[r_symndx];
1195 
1196 		/* The offset must always be a multiple of 4.  We use
1197 		   the least significant bit to record whether we have
1198 		   already generated the necessary reloc.  */
1199 		if ((off & 1) != 0)
1200 		  off &= ~1;
1201 		else
1202 		  {
1203 		    bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1204 
1205 		    if (info->shared)
1206 		      {
1207 			asection *s;
1208 			Elf_Internal_Rela outrel;
1209 			bfd_byte *loc;
1210 
1211 			s = bfd_get_section_by_name (dynobj, ".rela.got");
1212 			BFD_ASSERT (s != NULL);
1213 
1214 			outrel.r_offset = (sgot->output_section->vma
1215 					   + sgot->output_offset
1216 					   + off);
1217 			outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1218 			outrel.r_addend = relocation;
1219 			loc = s->contents;
1220 			loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1221 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1222 		      }
1223 
1224 		    local_got_offsets[r_symndx] |= 1;
1225 		  }
1226 	      }
1227 
1228 	    relocation = sgot->output_offset + off;
1229 	    if (rel->r_addend != 0)
1230 	      {
1231 		/* We can't do anything for a relocation which is against
1232 		   a symbol *plus offset*.  GOT holds relocations for
1233 		   symbols.  Make this an error; the compiler isn't
1234 		   allowed to pass us these kinds of things.  */
1235 		if (h == NULL)
1236 		  (*_bfd_error_handler)
1237 		    (_("%B, section %A: relocation %s with non-zero addend %d"
1238 		       " against local symbol"),
1239 		     input_bfd,
1240 		     input_section,
1241 		     cris_elf_howto_table[r_type].name,
1242 		     rel->r_addend);
1243 		else
1244 		  (*_bfd_error_handler)
1245 		    (_("%B, section %A: relocation %s with non-zero addend %d"
1246 		       " against symbol `%s'"),
1247 		     input_bfd,
1248 		     input_section,
1249 		     cris_elf_howto_table[r_type].name,
1250 		     rel->r_addend,
1251 		     symname[0] != '\0' ? symname : _("[whose name is lost]"));
1252 
1253 		bfd_set_error (bfd_error_bad_value);
1254 		return FALSE;
1255 	      }
1256 	  }
1257 	  break;
1258 
1259 	case R_CRIS_32_GOTREL:
1260 	  /* This relocation must only be performed against local symbols.
1261 	     It's also ok when we link a program and the symbol is either
1262 	     defined in an ordinary (non-DSO) object or is undefined weak.  */
1263 	  if (h != NULL
1264 	      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1265 	      && !(!info->shared
1266 		   && (h->def_regular
1267 		       || (!h->def_dynamic
1268 			   && h->root.type == bfd_link_hash_undefweak))))
1269 	    {
1270 	      (*_bfd_error_handler)
1271 		(_("%B, section %A: relocation %s is"
1272 		   " not allowed for global symbol: `%s'"),
1273 		 input_bfd,
1274 		 input_section,
1275 		 cris_elf_howto_table[r_type].name,
1276 		 symname);
1277 	      bfd_set_error (bfd_error_bad_value);
1278 	      return FALSE;
1279 	    }
1280 
1281 	  /* This can happen if we get a link error with the input ELF
1282 	     variant mismatching the output variant.  Emit an error so
1283 	     it's noticed if it happens elsewhere.  */
1284 	  if (sgot == NULL)
1285 	    {
1286 	      (*_bfd_error_handler)
1287 		(_("%B, section %A: relocation %s with no GOT created"),
1288 		 input_bfd,
1289 		 input_section,
1290 		 cris_elf_howto_table[r_type].name);
1291 	      bfd_set_error (bfd_error_bad_value);
1292 	      return FALSE;
1293 	    }
1294 
1295 	  /* This relocation is like a PC-relative one, except the
1296 	     reference point is the location of GOT.  Note that
1297 	     sgot->output_offset is not involved in this calculation.  We
1298 	     always want the start of entire .got section, not the
1299 	     position after the reserved header.  */
1300 	  relocation -= sgot->output_section->vma;
1301 	  break;
1302 
1303 	case R_CRIS_32_PLT_PCREL:
1304 	  /* Relocation is to the entry for this symbol in the
1305 	     procedure linkage table.  */
1306 
1307 	  /* Resolve a PLT_PCREL reloc against a local symbol directly,
1308 	     without using the procedure linkage table.  */
1309 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1310 	    break;
1311 
1312 	  if (h->plt.offset == (bfd_vma) -1
1313 	      || splt == NULL)
1314 	    {
1315 	      /* We didn't make a PLT entry for this symbol.  This
1316 		 happens when statically linking PIC code, or when
1317 		 using -Bsymbolic.  */
1318 	      break;
1319 	    }
1320 
1321 	  relocation = (splt->output_section->vma
1322 			+ splt->output_offset
1323 			+ h->plt.offset);
1324 	  break;
1325 
1326 	case R_CRIS_32_PLT_GOTREL:
1327 	  /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1328 	     start of the .got section.  See also comment at
1329 	     R_CRIS_32_GOT.  */
1330 	  relocation -= sgot->output_section->vma;
1331 
1332 	  /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1333 	     without using the procedure linkage table.  */
1334 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1335 	    break;
1336 
1337 	  if (h->plt.offset == (bfd_vma) -1
1338 	      || splt == NULL)
1339 	    {
1340 	      /* We didn't make a PLT entry for this symbol.  This
1341 		 happens when statically linking PIC code, or when
1342 		 using -Bsymbolic.  */
1343 	      break;
1344 	    }
1345 
1346 	  relocation = (splt->output_section->vma
1347 			+ splt->output_offset
1348 			+ h->plt.offset
1349 			- sgot->output_section->vma);
1350 	  break;
1351 
1352 	case R_CRIS_8_PCREL:
1353 	case R_CRIS_16_PCREL:
1354 	case R_CRIS_32_PCREL:
1355 	  /* If the symbol was local, we need no shlib-specific handling.  */
1356 	  if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1357 	    break;
1358 
1359 	  /* Fall through.  */
1360 	case R_CRIS_8:
1361 	case R_CRIS_16:
1362 	case R_CRIS_32:
1363 	  if (info->shared
1364 	      && r_symndx != 0
1365 	      && (input_section->flags & SEC_ALLOC) != 0
1366 	      && ((r_type != R_CRIS_8_PCREL
1367 		   && r_type != R_CRIS_16_PCREL
1368 		   && r_type != R_CRIS_32_PCREL)
1369 		  || (!info->symbolic
1370 		      || !h->def_regular)))
1371 	    {
1372 	      Elf_Internal_Rela outrel;
1373 	      bfd_byte *loc;
1374 	      bfd_boolean skip, relocate;
1375 
1376 	      /* When generating a shared object, these relocations
1377 		 are copied into the output file to be resolved at run
1378 		 time.  */
1379 
1380 	      if (sreloc == NULL)
1381 		{
1382 		  const char *name;
1383 
1384 		  name = (bfd_elf_string_from_elf_section
1385 			  (input_bfd,
1386 			   elf_elfheader (input_bfd)->e_shstrndx,
1387 			   elf_section_data (input_section)->rel_hdr.sh_name));
1388 		  if (name == NULL)
1389 		    return FALSE;
1390 
1391 		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1392 			      && strcmp (bfd_get_section_name (input_bfd,
1393 							       input_section),
1394 					 name + 5) == 0);
1395 
1396 		  sreloc = bfd_get_section_by_name (dynobj, name);
1397 
1398 		  /* That section should have been created in
1399 		     cris_elf_check_relocs, but that function will not be
1400 		     called for objects which fail in
1401 		     cris_elf_merge_private_bfd_data.  */
1402 		  if (sreloc == NULL)
1403 		    {
1404 		      (*_bfd_error_handler)
1405 			(_("%B: Internal inconsistency; no relocation section %s"),
1406 			 input_bfd,
1407 			 name);
1408 
1409 		      bfd_set_error (bfd_error_bad_value);
1410 		      return FALSE;
1411 		    }
1412 		}
1413 
1414 	      skip = FALSE;
1415 	      relocate = FALSE;
1416 
1417 	      outrel.r_offset =
1418 		_bfd_elf_section_offset (output_bfd, info, input_section,
1419 					 rel->r_offset);
1420 	      if (outrel.r_offset == (bfd_vma) -1)
1421 		skip = TRUE;
1422 	      else if (outrel.r_offset == (bfd_vma) -2)
1423 		skip = TRUE, relocate = TRUE;
1424 	      outrel.r_offset += (input_section->output_section->vma
1425 				  + input_section->output_offset);
1426 
1427 	      if (skip)
1428 		memset (&outrel, 0, sizeof outrel);
1429 	      /* h->dynindx may be -1 if the symbol was marked to
1430 		 become local.  */
1431 	      else if (h != NULL
1432 		       && ((! info->symbolic && h->dynindx != -1)
1433 			   || !h->def_regular))
1434 		{
1435 		  BFD_ASSERT (h->dynindx != -1);
1436 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1437 		  outrel.r_addend = relocation + rel->r_addend;
1438 		}
1439 	      else
1440 		{
1441 		  if (r_type == R_CRIS_32)
1442 		    {
1443 		      relocate = TRUE;
1444 		      outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1445 		      outrel.r_addend = relocation + rel->r_addend;
1446 		    }
1447 		  else
1448 		    {
1449 		      long indx;
1450 
1451 		      if (bfd_is_abs_section (sec))
1452 			indx = 0;
1453 		      else if (sec == NULL || sec->owner == NULL)
1454 			{
1455 			  bfd_set_error (bfd_error_bad_value);
1456 			  return FALSE;
1457 			}
1458 		      else
1459 			{
1460 			  asection *osec;
1461 
1462 			  osec = sec->output_section;
1463 			  indx = elf_section_data (osec)->dynindx;
1464 			  BFD_ASSERT (indx > 0);
1465 			}
1466 
1467 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
1468 		      outrel.r_addend = relocation + rel->r_addend;
1469 		    }
1470 		}
1471 
1472 	      loc = sreloc->contents;
1473 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1474 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1475 
1476 	      /* This reloc will be computed at runtime, so there's no
1477                  need to do anything now, except for R_CRIS_32 relocations
1478                  that have been turned into R_CRIS_RELATIVE.  */
1479 	      if (!relocate)
1480 		continue;
1481 	    }
1482 
1483 	  break;
1484 	}
1485 
1486       r = cris_final_link_relocate (howto, input_bfd, input_section,
1487 				     contents, rel, relocation);
1488 
1489       if (r != bfd_reloc_ok)
1490 	{
1491 	  const char * msg = (const char *) NULL;
1492 
1493 	  switch (r)
1494 	    {
1495 	    case bfd_reloc_overflow:
1496 	      r = info->callbacks->reloc_overflow
1497 		(info, (h ? &h->root : NULL), symname, howto->name,
1498 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1499 	      break;
1500 
1501 	    case bfd_reloc_undefined:
1502 	      r = info->callbacks->undefined_symbol
1503 		(info, symname, input_bfd, input_section, rel->r_offset,
1504 		 TRUE);
1505 	      break;
1506 
1507 	    case bfd_reloc_outofrange:
1508 	      msg = _("internal error: out of range error");
1509 	      break;
1510 
1511 	    case bfd_reloc_notsupported:
1512 	      msg = _("internal error: unsupported relocation error");
1513 	      break;
1514 
1515 	    case bfd_reloc_dangerous:
1516 	      msg = _("internal error: dangerous relocation");
1517 	      break;
1518 
1519 	    default:
1520 	      msg = _("internal error: unknown error");
1521 	      break;
1522 	    }
1523 
1524 	  if (msg)
1525 	    r = info->callbacks->warning
1526 	      (info, msg, symname, input_bfd, input_section, rel->r_offset);
1527 
1528 	  if (! r)
1529 	    return FALSE;
1530 	}
1531     }
1532 
1533   return TRUE;
1534 }
1535 
1536 /* Finish up dynamic symbol handling.  We set the contents of various
1537    dynamic sections here.  */
1538 
1539 static bfd_boolean
elf_cris_finish_dynamic_symbol(output_bfd,info,h,sym)1540 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1541      bfd *output_bfd;
1542      struct bfd_link_info *info;
1543      struct elf_link_hash_entry *h;
1544      Elf_Internal_Sym *sym;
1545 {
1546   bfd *dynobj;
1547 
1548   /* Where in the plt entry to put values.  */
1549   int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1550 
1551   /* What offset to add to the distance to the first PLT entry for the
1552      value at plt_off3.   */
1553   int plt_off3_value_bias = 4;
1554 
1555   /* Where in the PLT entry the call-dynlink-stub is (happens to be same
1556      for PIC and non-PIC for v32 and pre-v32).  */
1557   int plt_stub_offset = 8;
1558   int plt_entry_size = PLT_ENTRY_SIZE;
1559   const bfd_byte *plt_entry = elf_cris_plt_entry;
1560   const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
1561 
1562   /* Adjust the various PLT entry offsets.  */
1563   if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
1564     {
1565       plt_off2 = 14;
1566       plt_off3 = 20;
1567       plt_off3_value_bias = -2;
1568       plt_stub_offset = 12;
1569       plt_entry_size = PLT_ENTRY_SIZE_V32;
1570       plt_entry = elf_cris_plt_entry_v32;
1571       plt_pic_entry = elf_cris_pic_plt_entry_v32;
1572     }
1573 
1574   dynobj = elf_hash_table (info)->dynobj;
1575 
1576   if (h->plt.offset != (bfd_vma) -1)
1577     {
1578       asection *splt;
1579       asection *sgotplt;
1580       asection *sgot;
1581       asection *srela;
1582       bfd_vma got_base;
1583 
1584       bfd_vma gotplt_offset
1585 	= ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1586       Elf_Internal_Rela rela;
1587       bfd_byte *loc;
1588       bfd_boolean has_gotplt = gotplt_offset != 0;
1589 
1590       /* Get the index in the procedure linkage table which
1591 	 corresponds to this symbol.  This is the index of this symbol
1592 	 in all the symbols for which we are making plt entries.  The
1593 	 first entry in the procedure linkage table is reserved.  */
1594       /* We have to count backwards here, and the result is only valid as
1595 	 an index into .got.plt and its relocations.  FIXME: Constants...  */
1596       bfd_vma gotplt_index = gotplt_offset/4 - 3;
1597 
1598       /* Get the offset into the .got table of the entry that corresponds
1599 	 to this function.  Note that we embed knowledge that "incoming"
1600 	 .got goes after .got.plt in the output without padding (pointer
1601 	 aligned).  However, that knowledge is present in several other
1602 	 places too.  */
1603       bfd_vma got_offset
1604 	= (has_gotplt
1605 	   ? gotplt_offset
1606 	   : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1607 
1608       /* This symbol has an entry in the procedure linkage table.  Set it
1609 	 up.  */
1610 
1611       BFD_ASSERT (h->dynindx != -1);
1612 
1613       splt = bfd_get_section_by_name (dynobj, ".plt");
1614       sgot = bfd_get_section_by_name (dynobj, ".got");
1615       sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1616       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1617       BFD_ASSERT (splt != NULL && sgotplt != NULL
1618 		  && (! has_gotplt || srela != NULL));
1619 
1620       got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1621 
1622       /* Fill in the entry in the procedure linkage table.  */
1623       if (! info->shared)
1624 	{
1625 	  memcpy (splt->contents + h->plt.offset, plt_entry,
1626 		  plt_entry_size);
1627 
1628 	  /* We need to enter the absolute address of the GOT entry here.  */
1629 	  bfd_put_32 (output_bfd, got_base + got_offset,
1630 		      splt->contents + h->plt.offset + plt_off1);
1631 	}
1632       else
1633 	{
1634 	  memcpy (splt->contents + h->plt.offset, plt_pic_entry,
1635 		  plt_entry_size);
1636 	  bfd_put_32 (output_bfd, got_offset,
1637 		      splt->contents + h->plt.offset + plt_off1);
1638 	}
1639 
1640       /* Fill in the plt entry and make a relocation, if this is a "real"
1641 	 PLT entry.  */
1642       if (has_gotplt)
1643 	{
1644 	  /* Fill in the offset to the reloc table.  */
1645 	  bfd_put_32 (output_bfd,
1646 		      gotplt_index * sizeof (Elf32_External_Rela),
1647 		      splt->contents + h->plt.offset + plt_off2);
1648 
1649 	  /* Fill in the offset to the first PLT entry, where to "jump".  */
1650 	  bfd_put_32 (output_bfd,
1651 		      - (h->plt.offset + plt_off3 + plt_off3_value_bias),
1652 		      splt->contents + h->plt.offset + plt_off3);
1653 
1654 	  /* Fill in the entry in the global offset table with the address of
1655 	     the relocating stub.  */
1656 	  bfd_put_32 (output_bfd,
1657 		      (splt->output_section->vma
1658 		       + splt->output_offset
1659 		       + h->plt.offset
1660 		       + plt_stub_offset),
1661 		      sgotplt->contents + got_offset);
1662 
1663 	  /* Fill in the entry in the .rela.plt section.  */
1664 	  rela.r_offset = (sgotplt->output_section->vma
1665 			   + sgotplt->output_offset
1666 			   + got_offset);
1667 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1668 	  rela.r_addend = 0;
1669 	  loc = srela->contents + gotplt_index * sizeof (Elf32_External_Rela);
1670 	  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1671 	}
1672 
1673       if (!h->def_regular)
1674 	{
1675 	  /* Mark the symbol as undefined, rather than as defined in
1676 	     the .plt section.  Leave the value alone.  */
1677 	  sym->st_shndx = SHN_UNDEF;
1678 
1679 	  /* FIXME: From elf32-sparc.c 2001-02-19 (1.18).  I still don't
1680 	     know whether resetting the value is significant; if it really
1681 	     is, rather than a quirk or bug in the sparc port, then I
1682 	     believe we'd see this elsewhere.  */
1683 	  /* If the symbol is weak, we do need to clear the value.
1684 	     Otherwise, the PLT entry would provide a definition for
1685 	     the symbol even if the symbol wasn't defined anywhere,
1686 	     and so the symbol would never be NULL.  */
1687 	  if (!h->ref_regular_nonweak)
1688 	    sym->st_value = 0;
1689 	}
1690     }
1691 
1692   /* For an ordinary program, we emit .got relocs only for symbols that
1693      are in the dynamic-symbols table and are either defined by the
1694      program or are undefined weak symbols, or are function symbols
1695      where we do not output a PLT: the PLT reloc was output above and all
1696      references to the function symbol are redirected to the PLT.  */
1697   if (h->got.offset != (bfd_vma) -1
1698       && (info->shared
1699 	  || (h->dynindx != -1
1700 	      && h->plt.offset == (bfd_vma) -1
1701 	      && !h->def_regular
1702 	      && h->root.type != bfd_link_hash_undefweak)))
1703     {
1704       asection *sgot;
1705       asection *srela;
1706       Elf_Internal_Rela rela;
1707       bfd_byte *loc;
1708       bfd_byte *where;
1709 
1710       /* This symbol has an entry in the global offset table.  Set it up.  */
1711 
1712       sgot = bfd_get_section_by_name (dynobj, ".got");
1713       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1714       BFD_ASSERT (sgot != NULL && srela != NULL);
1715 
1716       rela.r_offset = (sgot->output_section->vma
1717 		       + sgot->output_offset
1718 		       + (h->got.offset &~ (bfd_vma) 1));
1719 
1720       /* If this is a static link, or it is a -Bsymbolic link and the
1721 	 symbol is defined locally or was forced to be local because
1722 	 of a version file, we just want to emit a RELATIVE reloc.
1723 	 The entry in the global offset table will already have been
1724 	 initialized in the relocate_section function.  */
1725       where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
1726       if (! elf_hash_table (info)->dynamic_sections_created
1727 	  || (info->shared
1728 	      && (info->symbolic || h->dynindx == -1)
1729 	      && h->def_regular))
1730 	{
1731 	  rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1732 	  rela.r_addend = bfd_get_signed_32 (output_bfd, where);
1733 	}
1734       else
1735 	{
1736 	  bfd_put_32 (output_bfd, (bfd_vma) 0, where);
1737 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1738 	  rela.r_addend = 0;
1739 	}
1740 
1741       loc = srela->contents;
1742       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
1743       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1744     }
1745 
1746   if (h->needs_copy)
1747     {
1748       asection *s;
1749       Elf_Internal_Rela rela;
1750       bfd_byte *loc;
1751 
1752       /* This symbol needs a copy reloc.  Set it up.  */
1753 
1754       BFD_ASSERT (h->dynindx != -1
1755 		  && (h->root.type == bfd_link_hash_defined
1756 		      || h->root.type == bfd_link_hash_defweak));
1757 
1758       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1759 				   ".rela.bss");
1760       BFD_ASSERT (s != NULL);
1761 
1762       rela.r_offset = (h->root.u.def.value
1763 		       + h->root.u.def.section->output_section->vma
1764 		       + h->root.u.def.section->output_offset);
1765       rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1766       rela.r_addend = 0;
1767       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
1768       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1769     }
1770 
1771   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1772   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1773       || h == elf_hash_table (info)->hgot)
1774     sym->st_shndx = SHN_ABS;
1775 
1776   return TRUE;
1777 }
1778 
1779 /* Finish up the dynamic sections.  */
1780 
1781 static bfd_boolean
elf_cris_finish_dynamic_sections(output_bfd,info)1782 elf_cris_finish_dynamic_sections (output_bfd, info)
1783      bfd *output_bfd;
1784      struct bfd_link_info *info;
1785 {
1786   bfd *dynobj;
1787   asection *sgot;
1788   asection *sdyn;
1789 
1790   dynobj = elf_hash_table (info)->dynobj;
1791 
1792   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1793   BFD_ASSERT (sgot != NULL);
1794   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1795 
1796   if (elf_hash_table (info)->dynamic_sections_created)
1797     {
1798       asection *splt;
1799       Elf32_External_Dyn *dyncon, *dynconend;
1800 
1801       splt = bfd_get_section_by_name (dynobj, ".plt");
1802       BFD_ASSERT (splt != NULL && sdyn != NULL);
1803 
1804       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1805       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1806       for (; dyncon < dynconend; dyncon++)
1807 	{
1808 	  Elf_Internal_Dyn dyn;
1809 	  asection *s;
1810 
1811 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1812 
1813 	  switch (dyn.d_tag)
1814 	    {
1815 	    default:
1816 	      break;
1817 
1818 	    case DT_PLTGOT:
1819 	      s = bfd_get_section_by_name (output_bfd, ".got");
1820 	      BFD_ASSERT (s != NULL);
1821 	      dyn.d_un.d_ptr = s->vma;
1822 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1823 	      break;
1824 
1825 	    case DT_JMPREL:
1826 	      /* Yes, we *can* have a .plt and no .plt.rela, for instance
1827 		 if all symbols are found in the .got (not .got.plt).  */
1828 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1829 	      dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1830 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1831 	      break;
1832 
1833 	    case DT_PLTRELSZ:
1834 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1835 	      if (s == NULL)
1836 		dyn.d_un.d_val = 0;
1837 	      else
1838 		dyn.d_un.d_val = s->size;
1839 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1840 	      break;
1841 
1842 	    case DT_RELASZ:
1843 	      /* The procedure linkage table relocs (DT_JMPREL) should
1844 		 not be included in the overall relocs (DT_RELA).
1845 		 Therefore, we override the DT_RELASZ entry here to
1846 		 make it not include the JMPREL relocs.  Since the
1847 		 linker script arranges for .rela.plt to follow all
1848 		 other relocation sections, we don't have to worry
1849 		 about changing the DT_RELA entry.  */
1850 	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1851 	      if (s != NULL)
1852 		dyn.d_un.d_val -= s->size;
1853 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1854 	      break;
1855 	    }
1856 	}
1857 
1858       /* Fill in the first entry in the procedure linkage table.  */
1859       if (splt->size > 0)
1860 	{
1861 	  if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
1862 	    {
1863 	      if (info->shared)
1864 		memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
1865 			PLT_ENTRY_SIZE_V32);
1866 	      else
1867 		{
1868 		  memcpy (splt->contents, elf_cris_plt0_entry_v32,
1869 			  PLT_ENTRY_SIZE_V32);
1870 		  bfd_put_32 (output_bfd,
1871 			      sgot->output_section->vma
1872 			      + sgot->output_offset + 4,
1873 			      splt->contents + 4);
1874 
1875 		  elf_section_data (splt->output_section)->this_hdr.sh_entsize
1876 		    = PLT_ENTRY_SIZE_V32;
1877 		}
1878 	    }
1879 	  else
1880 	    {
1881 	      if (info->shared)
1882 		memcpy (splt->contents, elf_cris_pic_plt0_entry,
1883 			PLT_ENTRY_SIZE);
1884 	      else
1885 		{
1886 		  memcpy (splt->contents, elf_cris_plt0_entry,
1887 			  PLT_ENTRY_SIZE);
1888 		  bfd_put_32 (output_bfd,
1889 			      sgot->output_section->vma
1890 			      + sgot->output_offset + 4,
1891 			      splt->contents + 6);
1892 		  bfd_put_32 (output_bfd,
1893 			      sgot->output_section->vma
1894 			      + sgot->output_offset + 8,
1895 			      splt->contents + 14);
1896 
1897 		  elf_section_data (splt->output_section)->this_hdr.sh_entsize
1898 		    = PLT_ENTRY_SIZE;
1899 		}
1900             }
1901 	}
1902     }
1903 
1904   /* Fill in the first three entries in the global offset table.  */
1905   if (sgot->size > 0)
1906     {
1907       if (sdyn == NULL)
1908 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1909       else
1910 	bfd_put_32 (output_bfd,
1911 		    sdyn->output_section->vma + sdyn->output_offset,
1912 		    sgot->contents);
1913       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1914       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1915     }
1916 
1917   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1918 
1919   return TRUE;
1920 }
1921 
1922 /* Return the section that should be marked against GC for a given
1923    relocation.  */
1924 
1925 static asection *
cris_elf_gc_mark_hook(sec,info,rel,h,sym)1926 cris_elf_gc_mark_hook (sec, info, rel, h, sym)
1927      asection *sec;
1928      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1929      Elf_Internal_Rela *rel;
1930      struct elf_link_hash_entry *h;
1931      Elf_Internal_Sym *sym;
1932 {
1933   if (h != NULL)
1934     {
1935       switch (ELF32_R_TYPE (rel->r_info))
1936 	{
1937 	case R_CRIS_GNU_VTINHERIT:
1938 	case R_CRIS_GNU_VTENTRY:
1939 	  break;
1940 
1941 	default:
1942 	  switch (h->root.type)
1943 	    {
1944 	    case bfd_link_hash_defined:
1945 	    case bfd_link_hash_defweak:
1946 	      return h->root.u.def.section;
1947 
1948 	    case bfd_link_hash_common:
1949 	      return h->root.u.c.p->section;
1950 
1951 	    default:
1952 	      break;
1953 	    }
1954 	}
1955     }
1956   else
1957     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1958 
1959   return NULL;
1960 }
1961 
1962 /* Update the got entry reference counts for the section being removed.  */
1963 
1964 static bfd_boolean
cris_elf_gc_sweep_hook(abfd,info,sec,relocs)1965 cris_elf_gc_sweep_hook (abfd, info, sec, relocs)
1966      bfd *abfd ATTRIBUTE_UNUSED;
1967      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1968      asection *sec ATTRIBUTE_UNUSED;
1969      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
1970 {
1971   Elf_Internal_Shdr *symtab_hdr;
1972   struct elf_link_hash_entry **sym_hashes;
1973   bfd_signed_vma *local_got_refcounts;
1974   const Elf_Internal_Rela *rel, *relend;
1975   bfd *dynobj;
1976   asection *sgot;
1977   asection *srelgot;
1978 
1979   dynobj = elf_hash_table (info)->dynobj;
1980   if (dynobj == NULL)
1981     return TRUE;
1982 
1983   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1984   sym_hashes = elf_sym_hashes (abfd);
1985   local_got_refcounts = elf_local_got_refcounts (abfd);
1986 
1987   sgot = bfd_get_section_by_name (dynobj, ".got");
1988   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1989 
1990   relend = relocs + sec->reloc_count;
1991   for (rel = relocs; rel < relend; rel++)
1992     {
1993       unsigned long r_symndx;
1994       struct elf_link_hash_entry *h = NULL;
1995 
1996       r_symndx = ELF32_R_SYM (rel->r_info);
1997       if (r_symndx >= symtab_hdr->sh_info)
1998 	{
1999 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2000 	  while (h->root.type == bfd_link_hash_indirect
2001 		 || h->root.type == bfd_link_hash_warning)
2002 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2003 	}
2004 
2005       switch (ELF32_R_TYPE (rel->r_info))
2006 	{
2007 	case R_CRIS_16_GOT:
2008 	case R_CRIS_32_GOT:
2009 	  if (h != NULL)
2010 	    {
2011 	      if (h->got.refcount > 0)
2012 		{
2013 		  --h->got.refcount;
2014 		  if (h->got.refcount == 0)
2015 		    {
2016 		      /* We don't need the .got entry any more.  */
2017 		      sgot->size -= 4;
2018 		      srelgot->size -= sizeof (Elf32_External_Rela);
2019 		    }
2020 		}
2021 	      break;
2022 	    }
2023 
2024 	local_got_reloc:
2025 	  if (local_got_refcounts != NULL)
2026 	    {
2027 	      if (local_got_refcounts[r_symndx] > 0)
2028 		{
2029 		  --local_got_refcounts[r_symndx];
2030 		  if (local_got_refcounts[r_symndx] == 0)
2031 		    {
2032 		      /* We don't need the .got entry any more.  */
2033 		      sgot->size -= 4;
2034 		      if (info->shared)
2035 			srelgot->size -= sizeof (Elf32_External_Rela);
2036 		    }
2037 		}
2038 	    }
2039 	  break;
2040 
2041 	case R_CRIS_16_GOTPLT:
2042 	case R_CRIS_32_GOTPLT:
2043 	  /* For local symbols, treat these like GOT relocs.  */
2044 	  if (h == NULL)
2045 	    goto local_got_reloc;
2046 	  /* Fall through.  */
2047 
2048 	case R_CRIS_32_PLT_GOTREL:
2049 	  /* FIXME: We don't garbage-collect away the .got section.  */
2050 	  if (local_got_refcounts != NULL)
2051 	    local_got_refcounts[-1]--;
2052 	  /* Fall through.  */
2053 
2054 	case R_CRIS_8_PCREL:
2055 	case R_CRIS_16_PCREL:
2056 	case R_CRIS_32_PCREL:
2057 	case R_CRIS_32_PLT_PCREL:
2058 	  if (h != NULL)
2059 	    {
2060 	      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2061 		  && h->plt.refcount > 0)
2062 		--h->plt.refcount;
2063 	    }
2064 	  break;
2065 
2066 	default:
2067 	  break;
2068 	}
2069     }
2070 
2071   return TRUE;
2072 }
2073 
2074 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
2075    entry but we found we will not create any.  Called when we find we will
2076    not have any PLT for this symbol, by for example
2077    elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
2078    or elf_cris_size_dynamic_sections if no dynamic sections will be
2079    created (we're only linking static objects).  */
2080 
2081 static bfd_boolean
elf_cris_adjust_gotplt_to_got(h,p)2082 elf_cris_adjust_gotplt_to_got (h, p)
2083      struct elf_cris_link_hash_entry *h;
2084      PTR p;
2085 {
2086   struct bfd_link_info *info = (struct bfd_link_info *) p;
2087 
2088   if (h->root.root.type == bfd_link_hash_warning)
2089     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
2090 
2091   /* If nobody wanted a GOTPLT with this symbol, we're done.  */
2092   if (h->gotplt_refcount <= 0)
2093     return TRUE;
2094 
2095   if (h->root.got.refcount > 0)
2096     {
2097       /* There's a GOT entry for this symbol.  Just adjust the refcount.
2098 	 Probably not necessary at this stage, but keeping it accurate
2099 	 helps avoiding surprises later.  */
2100       h->root.got.refcount += h->gotplt_refcount;
2101       h->gotplt_refcount = 0;
2102     }
2103   else
2104     {
2105       /* No GOT entry for this symbol.  We need to create one.  */
2106       bfd *dynobj = elf_hash_table (info)->dynobj;
2107       asection *sgot;
2108       asection *srelgot;
2109 
2110       BFD_ASSERT (dynobj != NULL);
2111       sgot = bfd_get_section_by_name (dynobj, ".got");
2112       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2113 
2114       /* Put an accurate refcount there.  */
2115       h->root.got.refcount = h->gotplt_refcount;
2116 
2117       h->gotplt_refcount = 0;
2118 
2119       /* We always have a .got and a .rela.got section if there were
2120 	 GOTPLT relocs in input.  */
2121       BFD_ASSERT (sgot != NULL && srelgot != NULL);
2122 
2123       /* Allocate space in the .got section.  */
2124       sgot->size += 4;
2125 
2126       /* Allocate relocation space.  */
2127       srelgot->size += sizeof (Elf32_External_Rela);
2128     }
2129 
2130   return TRUE;
2131 }
2132 
2133 /* Try to fold PLT entries with GOT entries.  There are two cases when we
2134    want to do this:
2135 
2136    - When all PLT references are GOTPLT references, and there are GOT
2137      references, and this is not the executable.  We don't have to
2138      generate a PLT at all.
2139 
2140    - When there are both (ordinary) PLT references and GOT references,
2141      and this isn't the executable.
2142      We want to make the PLT reference use the ordinary GOT entry rather
2143      than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
2144      since the GOT entry will have to be resolved at startup anyway.
2145 
2146    Though the latter case is handled when room for the PLT is allocated,
2147    not here.
2148 
2149    By folding into the GOT, we may need a round-trip to a PLT in the
2150    executable for calls, a loss in performance.  Still, losing a
2151    reloc is a win in size and at least in start-up time.
2152 
2153    Note that this function is called before symbols are forced local by
2154    version scripts.  The differing cases are handled by
2155    elf_cris_hide_symbol.  */
2156 
2157 static bfd_boolean
elf_cris_try_fold_plt_to_got(h,p)2158 elf_cris_try_fold_plt_to_got (h, p)
2159      struct elf_cris_link_hash_entry *h;
2160      PTR p;
2161 {
2162   struct bfd_link_info *info = (struct bfd_link_info *) p;
2163 
2164   /* If there are no GOT references for this symbol, we can't fold any
2165      other reference so there's nothing to do.  Likewise if there are no
2166      PLT references; GOTPLT references included.  */
2167   if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
2168     return TRUE;
2169 
2170   /* GOTPLT relocs are supposed to be included into the PLT refcount.  */
2171   BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
2172 
2173   if (h->gotplt_refcount == h->root.plt.refcount)
2174     {
2175       /* The only PLT references are GOTPLT references, and there are GOT
2176 	 references.  Convert PLT to GOT references.  */
2177       if (! elf_cris_adjust_gotplt_to_got (h, info))
2178 	return FALSE;
2179 
2180       /* Clear the PLT references, so no PLT will be created.  */
2181       h->root.plt.offset = (bfd_vma) -1;
2182     }
2183 
2184   return TRUE;
2185 }
2186 
2187 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
2188    to use a GOT entry (and create one) rather than requiring a GOTPLT
2189    entry.  */
2190 
2191 static void
elf_cris_hide_symbol(info,h,force_local)2192 elf_cris_hide_symbol (info, h, force_local)
2193      struct bfd_link_info *info;
2194      struct elf_link_hash_entry *h;
2195      bfd_boolean force_local;
2196 {
2197   elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
2198 
2199   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2200 }
2201 
2202 /* Adjust a symbol defined by a dynamic object and referenced by a
2203    regular object.  The current definition is in some section of the
2204    dynamic object, but we're not including those sections.  We have to
2205    change the definition to something the rest of the link can
2206    understand.  */
2207 
2208 static bfd_boolean
elf_cris_adjust_dynamic_symbol(info,h)2209 elf_cris_adjust_dynamic_symbol (info, h)
2210      struct bfd_link_info *info;
2211      struct elf_link_hash_entry *h;
2212 {
2213   bfd *dynobj;
2214   asection *s;
2215   unsigned int power_of_two;
2216   bfd_size_type plt_entry_size;
2217 
2218   dynobj = elf_hash_table (info)->dynobj;
2219 
2220   /* Make sure we know what is going on here.  */
2221   BFD_ASSERT (dynobj != NULL
2222 	      && (h->needs_plt
2223 		  || h->u.weakdef != NULL
2224 		  || (h->def_dynamic
2225 		      && h->ref_regular
2226 		      && !h->def_regular)));
2227 
2228   plt_entry_size
2229     = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
2230        ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2231 
2232   /* If this is a function, put it in the procedure linkage table.  We
2233      will fill in the contents of the procedure linkage table later,
2234      when we know the address of the .got section.  */
2235   if (h->type == STT_FUNC
2236       || h->needs_plt)
2237     {
2238       /* If we link a program (not a DSO), we'll get rid of unnecessary
2239 	 PLT entries; we point to the actual symbols -- even for pic
2240 	 relocs, because a program built with -fpic should have the same
2241 	 result as one built without -fpic, specifically considering weak
2242 	 symbols.
2243 	 FIXME: m68k and i386 differ here, for unclear reasons.  */
2244       if (! info->shared
2245 	  && !h->def_dynamic)
2246 	{
2247 	  /* This case can occur if we saw a PLT reloc in an input file,
2248 	     but the symbol was not defined by a dynamic object.  In such
2249 	     a case, we don't actually need to build a procedure linkage
2250 	     table, and we can just do an absolute or PC reloc instead, or
2251 	     change a .got.plt index to a .got index for GOTPLT relocs.  */
2252 	  BFD_ASSERT (h->needs_plt);
2253 	  h->needs_plt = 0;
2254 	  h->plt.offset = (bfd_vma) -1;
2255 	  return
2256 	    elf_cris_adjust_gotplt_to_got ((struct
2257 					    elf_cris_link_hash_entry *) h,
2258 					   info);
2259 	}
2260 
2261       /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
2262 	 where a pointer-equivalent symbol was unimportant (i.e. more
2263 	 like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
2264 	 of the PLT.  We can't for the executable, because the GOT
2265 	 entries will point to the PLT there (and be constant).  */
2266       if (info->shared
2267 	  && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
2268 					    h, info))
2269 	return FALSE;
2270 
2271       /* GC or folding may have rendered this entry unused.  */
2272       if (h->plt.refcount <= 0)
2273 	{
2274 	  h->needs_plt = 0;
2275 	  h->plt.offset = (bfd_vma) -1;
2276 	  return TRUE;
2277 	}
2278 
2279       /* Make sure this symbol is output as a dynamic symbol.  */
2280       if (h->dynindx == -1)
2281 	{
2282 	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
2283 	    return FALSE;
2284 	}
2285 
2286       s = bfd_get_section_by_name (dynobj, ".plt");
2287       BFD_ASSERT (s != NULL);
2288 
2289       /* If this is the first .plt entry, make room for the special
2290 	 first entry.  */
2291       if (s->size == 0)
2292 	s->size += plt_entry_size;
2293 
2294       /* If this symbol is not defined in a regular file, and we are
2295 	 not generating a shared library, then set the symbol to this
2296 	 location in the .plt.  */
2297       if (!info->shared
2298 	  && !h->def_regular)
2299 	{
2300 	  h->root.u.def.section = s;
2301 	  h->root.u.def.value = s->size;
2302 	}
2303 
2304       /* If there's already a GOT entry, use that, not a .got.plt.  A
2305 	 GOT field still has a reference count when we get here; it's
2306 	 not yet changed to an offset.  We can't do this for an
2307 	 executable, because then the reloc associated with the PLT
2308 	 would get a non-PLT reloc pointing to the PLT.  FIXME: Move
2309 	 this to elf_cris_try_fold_plt_to_got.  */
2310       if (info->shared && h->got.refcount > 0)
2311 	{
2312 	  h->got.refcount += h->plt.refcount;
2313 
2314 	  /* Mark the PLT offset to use the GOT entry by setting the low
2315 	     bit in the plt offset; it is always a multiple of
2316 	     plt_entry_size (which is at least a multiple of 2).  */
2317 	  BFD_ASSERT ((s->size % plt_entry_size) == 0);
2318 
2319 	  /* Change the PLT refcount to an offset.  */
2320 	  h->plt.offset = s->size;
2321 
2322 	  /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2323 	     that the got entry should be used instead.  */
2324 	  BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2325 		       h)->gotplt_offset == 0);
2326 
2327 	  /* Make room for this entry.  */
2328 	  s->size += plt_entry_size;
2329 
2330 	  return TRUE;
2331 	}
2332 
2333       /* No GOT reference for this symbol; prepare for an ordinary PLT.  */
2334       h->plt.offset = s->size;
2335 
2336       /* Make room for this entry.  */
2337       s->size += plt_entry_size;
2338 
2339       /* We also need to make an entry in the .got.plt section, which
2340 	 will be placed in the .got section by the linker script.  */
2341       ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2342 	= elf_cris_hash_table (info)->next_gotplt_entry;
2343       elf_cris_hash_table (info)->next_gotplt_entry += 4;
2344 
2345       s = bfd_get_section_by_name (dynobj, ".got.plt");
2346       BFD_ASSERT (s != NULL);
2347       s->size += 4;
2348 
2349       /* We also need to make an entry in the .rela.plt section.  */
2350 
2351       s = bfd_get_section_by_name (dynobj, ".rela.plt");
2352       BFD_ASSERT (s != NULL);
2353       s->size += sizeof (Elf32_External_Rela);
2354 
2355       return TRUE;
2356     }
2357 
2358   /* Reinitialize the plt offset now that it is not used as a reference
2359      count any more.  */
2360   h->plt.offset = (bfd_vma) -1;
2361 
2362   /* If this is a weak symbol, and there is a real definition, the
2363      processor independent code will have arranged for us to see the
2364      real definition first, and we can just use the same value.  */
2365   if (h->u.weakdef != NULL)
2366     {
2367       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2368 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
2369       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2370       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2371       return TRUE;
2372     }
2373 
2374   /* This is a reference to a symbol defined by a dynamic object which
2375      is not a function.  */
2376 
2377   /* If we are creating a shared library, we must presume that the
2378      only references to the symbol are via the global offset table.
2379      For such cases we need not do anything here; the relocations will
2380      be handled correctly by relocate_section.  */
2381   if (info->shared)
2382     return TRUE;
2383 
2384   /* If there are no references to this symbol that do not use the
2385      GOT, we don't need to generate a copy reloc.  */
2386   if (!h->non_got_ref)
2387     return TRUE;
2388 
2389   if (h->size == 0)
2390     {
2391       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2392 			     h->root.root.string);
2393       return TRUE;
2394     }
2395 
2396   /* We must allocate the symbol in our .dynbss section, which will
2397      become part of the .bss section of the executable.  There will be
2398      an entry for this symbol in the .dynsym section.  The dynamic
2399      object will contain position independent code, so all references
2400      from the dynamic object to this symbol will go through the global
2401      offset table.  The dynamic linker will use the .dynsym entry to
2402      determine the address it must put in the global offset table, so
2403      both the dynamic object and the regular object will refer to the
2404      same memory location for the variable.  */
2405 
2406   s = bfd_get_section_by_name (dynobj, ".dynbss");
2407   BFD_ASSERT (s != NULL);
2408 
2409   /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2410      copy the initial value out of the dynamic object and into the
2411      runtime process image.  We need to remember the offset into the
2412      .rela.bss section we are going to use.  */
2413   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2414     {
2415       asection *srel;
2416 
2417       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2418       BFD_ASSERT (srel != NULL);
2419       srel->size += sizeof (Elf32_External_Rela);
2420       h->needs_copy = 1;
2421     }
2422 
2423   /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2424      thing to copy; so do we.  */
2425 
2426   /* We need to figure out the alignment required for this symbol.  I
2427      have no idea how ELF linkers handle this.  */
2428   power_of_two = bfd_log2 (h->size);
2429   if (power_of_two > 3)
2430     power_of_two = 3;
2431 
2432   /* Apply the required alignment.  */
2433   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2434   if (power_of_two > bfd_get_section_alignment (dynobj, s))
2435     {
2436       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2437 	return FALSE;
2438     }
2439 
2440   /* Define the symbol as being at this point in the section.  */
2441   h->root.u.def.section = s;
2442   h->root.u.def.value = s->size;
2443 
2444   /* Increment the section size to make room for the symbol.  */
2445   s->size += h->size;
2446 
2447   return TRUE;
2448 }
2449 
2450 /* Look through the relocs for a section during the first phase.  */
2451 
2452 static bfd_boolean
cris_elf_check_relocs(abfd,info,sec,relocs)2453 cris_elf_check_relocs (abfd, info, sec, relocs)
2454      bfd *abfd;
2455      struct bfd_link_info *info;
2456      asection *sec;
2457      const Elf_Internal_Rela *relocs;
2458 {
2459   bfd *dynobj;
2460   Elf_Internal_Shdr *symtab_hdr;
2461   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2462   bfd_signed_vma *local_got_refcounts;
2463   const Elf_Internal_Rela *rel;
2464   const Elf_Internal_Rela *rel_end;
2465   asection *sgot;
2466   asection *srelgot;
2467   asection *sreloc;
2468 
2469   if (info->relocatable)
2470     return TRUE;
2471 
2472   dynobj = elf_hash_table (info)->dynobj;
2473   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2474   sym_hashes = elf_sym_hashes (abfd);
2475   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2476   local_got_refcounts = elf_local_got_refcounts (abfd);
2477 
2478   sgot = NULL;
2479   srelgot = NULL;
2480   sreloc = NULL;
2481 
2482   if (!elf_bad_symtab (abfd))
2483     sym_hashes_end -= symtab_hdr->sh_info;
2484 
2485   rel_end = relocs + sec->reloc_count;
2486   for (rel = relocs; rel < rel_end; rel++)
2487     {
2488       struct elf_link_hash_entry *h;
2489       unsigned long r_symndx;
2490       enum elf_cris_reloc_type r_type;
2491 
2492       r_symndx = ELF32_R_SYM (rel->r_info);
2493       if (r_symndx < symtab_hdr->sh_info)
2494         h = NULL;
2495       else
2496 	{
2497 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2498 	  while (h->root.type == bfd_link_hash_indirect
2499 		 || h->root.type == bfd_link_hash_warning)
2500 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
2501 	}
2502 
2503       r_type = ELF32_R_TYPE (rel->r_info);
2504 
2505       /* Some relocs require linker-created sections; we need to hang them
2506 	 on the first input bfd we found that contained dynamic relocs.  */
2507       switch (r_type)
2508 	{
2509 	case R_CRIS_16_GOT:
2510 	case R_CRIS_32_GOT:
2511 	case R_CRIS_32_GOTREL:
2512 	case R_CRIS_32_PLT_GOTREL:
2513 	case R_CRIS_32_PLT_PCREL:
2514 	case R_CRIS_16_GOTPLT:
2515 	case R_CRIS_32_GOTPLT:
2516 	  if (dynobj == NULL)
2517 	    {
2518 	      elf_hash_table (info)->dynobj = dynobj = abfd;
2519 
2520 	      /* We could handle this if we can get a handle on the
2521 		 output bfd in elf_cris_adjust_dynamic_symbol.  Failing
2522 		 that, we must insist on dynobj being a specific mach.  */
2523 	      if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
2524 		{
2525 		  (*_bfd_error_handler)
2526 		    (_("%B, section %A:\n  v10/v32 compatible object %s"
2527 		       " must not contain a PIC relocation"),
2528 		     abfd, sec);
2529 		  return FALSE;
2530 		}
2531 
2532 	      /* Create the .got section, so we can assume it's always
2533 		 present whenever there's a dynobj.  */
2534 	      if (!_bfd_elf_create_got_section (dynobj, info))
2535 		return FALSE;
2536 	    }
2537 	  break;
2538 
2539 	default:
2540 	  break;
2541 	}
2542 
2543       /* Some relocs require a global offset table (but perhaps not a
2544 	 specific GOT entry).  */
2545       switch (r_type)
2546 	{
2547 	  /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
2548 	     entry only for local symbols.  Unfortunately, we don't know
2549 	     until later on if there's a version script that forces the
2550 	     symbol local.  We must have the .rela.got section in place
2551 	     before we know if the symbol looks global now, so we need
2552 	     to treat the reloc just like for R_CRIS_16_GOT and
2553 	     R_CRIS_32_GOT.  */
2554 	case R_CRIS_16_GOTPLT:
2555 	case R_CRIS_32_GOTPLT:
2556 	case R_CRIS_16_GOT:
2557 	case R_CRIS_32_GOT:
2558 	  if (srelgot == NULL
2559 	      && (h != NULL || info->shared))
2560 	    {
2561 	      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2562 	      if (srelgot == NULL)
2563 		{
2564 		  srelgot = bfd_make_section_with_flags (dynobj,
2565 							 ".rela.got",
2566 							 (SEC_ALLOC
2567 							  | SEC_LOAD
2568 							  | SEC_HAS_CONTENTS
2569 							  | SEC_IN_MEMORY
2570 							  | SEC_LINKER_CREATED
2571 							  | SEC_READONLY));
2572 		  if (srelgot == NULL
2573 		      || !bfd_set_section_alignment (dynobj, srelgot, 2))
2574 		    return FALSE;
2575 		}
2576 	    }
2577 	  /* Fall through.  */
2578 
2579 	case R_CRIS_32_GOTREL:
2580 	case R_CRIS_32_PLT_GOTREL:
2581 	  if (sgot == NULL)
2582 	    sgot = bfd_get_section_by_name (dynobj, ".got");
2583 
2584 	  if (local_got_refcounts == NULL)
2585 	    {
2586 	      bfd_size_type amt;
2587 
2588 	      /* We use index local_got_refcounts[-1] to count all
2589 		 GOT-relative relocations that do not have explicit
2590 		 GOT entries.  */
2591 	      amt = symtab_hdr->sh_info + 1;
2592 	      amt *= sizeof (bfd_signed_vma);
2593 	      local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
2594 	      if (local_got_refcounts == NULL)
2595 		return FALSE;
2596 
2597 	      local_got_refcounts++;
2598 	      elf_local_got_refcounts (abfd) = local_got_refcounts;
2599 	    }
2600 	  break;
2601 
2602 	default:
2603 	  break;
2604 	}
2605 
2606       switch (r_type)
2607         {
2608 	case R_CRIS_16_GOTPLT:
2609 	case R_CRIS_32_GOTPLT:
2610 	  /* Mark that we need a GOT entry if the PLT entry (and its GOT
2611 	     entry) is eliminated.  We can only do this for a non-local
2612 	     symbol.  */
2613 	  if (h != NULL)
2614 	    {
2615 	      ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2616 	      goto handle_gotplt_reloc;
2617 	    }
2618 	  /* If h is NULL then this is a local symbol, and we must make a
2619 	     GOT entry for it, so handle it like a GOT reloc.  */
2620 	  /* Fall through.  */
2621 
2622 	case R_CRIS_16_GOT:
2623 	case R_CRIS_32_GOT:
2624 	  /* This symbol requires a global offset table entry.  */
2625 	  if (h != NULL)
2626 	    {
2627 	      if (h->got.refcount == 0)
2628 		{
2629 		  /* Make sure this symbol is output as a dynamic symbol.  */
2630 		  if (h->dynindx == -1)
2631 		    {
2632 		      if (!bfd_elf_link_record_dynamic_symbol (info, h))
2633 			return FALSE;
2634 		    }
2635 
2636 		  /* Allocate space in the .got section.  */
2637 		  sgot->size += 4;
2638 		  /* Allocate relocation space.  */
2639 		  srelgot->size += sizeof (Elf32_External_Rela);
2640 		}
2641 	      h->got.refcount++;
2642 	    }
2643 	  else
2644 	    {
2645 	      /* This is a global offset table entry for a local symbol.  */
2646 	      if (local_got_refcounts[r_symndx] == 0)
2647 		{
2648 		  sgot->size += 4;
2649 		  if (info->shared)
2650 		    {
2651 		      /* If we are generating a shared object, we need to
2652 			 output a R_CRIS_RELATIVE reloc so that the dynamic
2653 			 linker can adjust this GOT entry.  */
2654 		      srelgot->size += sizeof (Elf32_External_Rela);
2655 		    }
2656 		}
2657 	      local_got_refcounts[r_symndx]++;
2658 	    }
2659 	  break;
2660 
2661 	case R_CRIS_32_GOTREL:
2662 	  /* This reference requires a global offset table.
2663 	     FIXME: The actual refcount isn't used currently; the .got
2664 	     section can't be removed if there were any references in the
2665 	     input.  */
2666 	  local_got_refcounts[-1]++;
2667 	  break;
2668 
2669 	handle_gotplt_reloc:
2670 
2671 	case R_CRIS_32_PLT_GOTREL:
2672 	  /* This reference requires a global offset table.  */
2673 	  local_got_refcounts[-1]++;
2674 	  /* Fall through.  */
2675 
2676 	case R_CRIS_32_PLT_PCREL:
2677 	  /* This symbol requires a procedure linkage table entry.  We
2678 	     actually build the entry in adjust_dynamic_symbol,
2679              because this might be a case of linking PIC code which is
2680              never referenced by a dynamic object, in which case we
2681              don't need to generate a procedure linkage table entry
2682              after all.  */
2683 
2684 	  /* Beware: if we'd check for visibility of the symbol here
2685 	     (and not marking the need for a PLT when non-visible), we'd
2686 	     get into trouble with keeping handling consistent with
2687 	     regards to relocs found before definition and GOTPLT
2688 	     handling.  Eliminable PLT entries will be dealt with later
2689 	     anyway.  */
2690 	  if (h == NULL)
2691 	    continue;
2692 
2693 	  h->needs_plt = 1;
2694 	  h->plt.refcount++;
2695 	  break;
2696 
2697 	case R_CRIS_8:
2698 	case R_CRIS_16:
2699 	case R_CRIS_32:
2700 	  /* Let's help debug shared library creation.  Any of these
2701 	     relocs can be used in shared libs, but pages containing them
2702 	     cannot be shared.  Don't warn for sections we don't care
2703 	     about, such as debug sections or non-constant sections.  We
2704 	     can't help tables of (global) function pointers, for example,
2705 	     though they must be emitted in a data section to avoid having
2706 	     impure text sections.  */
2707 	  if (info->shared
2708 	      && (sec->flags & SEC_ALLOC) != 0
2709 	      && (sec->flags & SEC_READONLY) != 0)
2710 	    {
2711 	      /* FIXME: How do we make this optionally a warning only?  */
2712 	      (*_bfd_error_handler)
2713 		(_("%B, section %A:\n  relocation %s should not"
2714 		   " be used in a shared object; recompile with -fPIC"),
2715 		 abfd,
2716 		 sec,
2717 		 cris_elf_howto_table[r_type].name);
2718 	    }
2719 	  /* Fall through.  */
2720 
2721 	case R_CRIS_8_PCREL:
2722 	case R_CRIS_16_PCREL:
2723 	case R_CRIS_32_PCREL:
2724 	  if (h != NULL)
2725 	    {
2726 	      h->non_got_ref = 1;
2727 
2728 	      /* Make sure a plt entry is created for this symbol if it
2729 		 turns out to be a function defined by a dynamic object.  */
2730 	      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2731 		h->plt.refcount++;
2732 	    }
2733 
2734 	  /* If we are creating a shared library and this is not a local
2735 	     symbol, we need to copy the reloc into the shared library.
2736 	     However when linking with -Bsymbolic and this is a global
2737 	     symbol which is defined in an object we are including in the
2738 	     link (i.e., DEF_REGULAR is set), then we can resolve the
2739 	     reloc directly.  At this point we have not seen all the input
2740 	     files, so it is possible that DEF_REGULAR is not set now but
2741 	     will be set later (it is never cleared).  In case of a weak
2742 	     definition, DEF_REGULAR may be cleared later by a strong
2743 	     definition in a shared library.  We account for that
2744 	     possibility below by storing information in the relocs_copied
2745 	     field of the hash table entry.  A similar situation occurs
2746 	     when creating shared libraries and symbol visibility changes
2747 	     render the symbol local.  */
2748 
2749 	  /* No need to do anything if we're not creating a shared object.  */
2750 	  if (! info->shared)
2751 	    break;
2752 
2753 	  /* We don't need to handle relocs into sections not going into
2754 	     the "real" output.  */
2755 	  if ((sec->flags & SEC_ALLOC) == 0)
2756 	    break;
2757 
2758 	  /* We can only eliminate PC-relative relocs.  */
2759 	  if (r_type == R_CRIS_8_PCREL
2760 	      || r_type == R_CRIS_16_PCREL
2761 	      || r_type == R_CRIS_32_PCREL)
2762 	    {
2763 	      /* If the symbol is local, then we can eliminate the reloc.  */
2764 	      if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2765 		break;
2766 
2767 	      /* If this is with -Bsymbolic and the symbol isn't weak, and
2768 		 is defined by an ordinary object (the ones we include in
2769 		 this shared library) then we can also eliminate the
2770 		 reloc.  See comment above for more eliminable cases which
2771 		 we can't identify at this time.  */
2772 	      if (info->symbolic
2773 		  && h->root.type != bfd_link_hash_defweak
2774 		  && h->def_regular)
2775 		break;
2776 
2777 	      if ((sec->flags & SEC_READONLY) != 0)
2778 		{
2779 		  /* FIXME: How do we make this optionally a warning only?  */
2780 		  (*_bfd_error_handler)
2781 		    (_("%B, section %A:\n  relocation %s should not be used"
2782 		       " in a shared object; recompile with -fPIC"),
2783 		     abfd,
2784 		     sec,
2785 		     cris_elf_howto_table[r_type].name);
2786 		}
2787 	    }
2788 
2789 	  /* We create a reloc section in dynobj and make room for this
2790 	     reloc.  */
2791 	  if (sreloc == NULL)
2792 	    {
2793 	      const char *name;
2794 
2795 	      name = (bfd_elf_string_from_elf_section
2796 		      (abfd,
2797 		       elf_elfheader (abfd)->e_shstrndx,
2798 		       elf_section_data (sec)->rel_hdr.sh_name));
2799 	      if (name == NULL)
2800 		return FALSE;
2801 
2802 	      BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2803 			  && strcmp (bfd_get_section_name (abfd, sec),
2804 				     name + 5) == 0);
2805 
2806 	      sreloc = bfd_get_section_by_name (dynobj, name);
2807 	      if (sreloc == NULL)
2808 		{
2809 		  sreloc = bfd_make_section_with_flags (dynobj, name,
2810 							(SEC_ALLOC
2811 							 | SEC_LOAD
2812 							 | SEC_HAS_CONTENTS
2813 							 | SEC_IN_MEMORY
2814 							 | SEC_LINKER_CREATED
2815 							 | SEC_READONLY));
2816 		  if (sreloc == NULL
2817 		      || !bfd_set_section_alignment (dynobj, sreloc, 2))
2818 		    return FALSE;
2819 		}
2820 	      if (sec->flags & SEC_READONLY)
2821 		info->flags |= DF_TEXTREL;
2822 	    }
2823 
2824 	  sreloc->size += sizeof (Elf32_External_Rela);
2825 
2826 	  /* If we are linking with -Bsymbolic, we count the number of PC
2827 	     relative relocations we have entered for this symbol, so that
2828 	     we can discard them again if the symbol is later defined by a
2829 	     regular object.  We know that h is really a pointer to an
2830 	     elf_cris_link_hash_entry.  */
2831 	  if ((r_type == R_CRIS_8_PCREL
2832 	       || r_type == R_CRIS_16_PCREL
2833 	       || r_type == R_CRIS_32_PCREL)
2834 	      && info->symbolic)
2835 	    {
2836 	      struct elf_cris_link_hash_entry *eh;
2837 	      struct elf_cris_pcrel_relocs_copied *p;
2838 
2839 	      eh = (struct elf_cris_link_hash_entry *) h;
2840 
2841 	      for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2842 		if (p->section == sreloc)
2843 		  break;
2844 
2845 	      if (p == NULL)
2846 		{
2847 		  p = ((struct elf_cris_pcrel_relocs_copied *)
2848 		       bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2849 		  if (p == NULL)
2850 		    return FALSE;
2851 		  p->next = eh->pcrel_relocs_copied;
2852 		  eh->pcrel_relocs_copied = p;
2853 		  p->section = sreloc;
2854 		  p->count = 0;
2855 		}
2856 
2857 	      ++p->count;
2858 	    }
2859 	  break;
2860 
2861         /* This relocation describes the C++ object vtable hierarchy.
2862            Reconstruct it for later use during GC.  */
2863         case R_CRIS_GNU_VTINHERIT:
2864           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2865             return FALSE;
2866           break;
2867 
2868         /* This relocation describes which C++ vtable entries are actually
2869            used.  Record for later use during GC.  */
2870         case R_CRIS_GNU_VTENTRY:
2871           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2872             return FALSE;
2873           break;
2874 
2875 	default:
2876 	  /* Other relocs do not appear here.  */
2877 	  bfd_set_error (bfd_error_bad_value);
2878 	  return FALSE;
2879         }
2880     }
2881 
2882   return TRUE;
2883 }
2884 
2885 /* Set the sizes of the dynamic sections.  */
2886 
2887 static bfd_boolean
elf_cris_size_dynamic_sections(output_bfd,info)2888 elf_cris_size_dynamic_sections (output_bfd, info)
2889      bfd *output_bfd ATTRIBUTE_UNUSED;
2890      struct bfd_link_info *info;
2891 {
2892   bfd *dynobj;
2893   asection *s;
2894   bfd_boolean plt;
2895   bfd_boolean relocs;
2896 
2897   dynobj = elf_hash_table (info)->dynobj;
2898   BFD_ASSERT (dynobj != NULL);
2899 
2900   if (elf_hash_table (info)->dynamic_sections_created)
2901     {
2902       /* Set the contents of the .interp section to the interpreter.  */
2903       if (info->executable)
2904 	{
2905 	  s = bfd_get_section_by_name (dynobj, ".interp");
2906 	  BFD_ASSERT (s != NULL);
2907 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2908 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2909 	}
2910     }
2911   else
2912     {
2913       /* Adjust all expected GOTPLT uses to use a GOT entry instead.  */
2914       elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2915 				   elf_cris_adjust_gotplt_to_got,
2916 				   (PTR) info);
2917 
2918       /* We may have created entries in the .rela.got section.
2919 	 However, if we are not creating the dynamic sections, we will
2920 	 not actually use these entries.  Reset the size of .rela.got,
2921 	 which will cause it to get stripped from the output file
2922 	 below.  */
2923       s = bfd_get_section_by_name (dynobj, ".rela.got");
2924       if (s != NULL)
2925 	s->size = 0;
2926     }
2927 
2928   /* If this is a -Bsymbolic shared link, then we need to discard all PC
2929      relative relocs against symbols defined in a regular object.  We
2930      allocated space for them in the check_relocs routine, but we will not
2931      fill them in in the relocate_section routine.  We also discard space
2932      for relocs that have become for local symbols due to symbol
2933      visibility changes.  For programs, we discard space for relocs for
2934      symbols not referenced by any dynamic object.  */
2935   if (info->shared)
2936     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2937 				 elf_cris_discard_excess_dso_dynamics,
2938 				 (PTR) info);
2939   else
2940     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2941 				 elf_cris_discard_excess_program_dynamics,
2942 				 (PTR) info);
2943 
2944   /* The check_relocs and adjust_dynamic_symbol entry points have
2945      determined the sizes of the various dynamic sections.  Allocate
2946      memory for them.  */
2947   plt = FALSE;
2948   relocs = FALSE;
2949   for (s = dynobj->sections; s != NULL; s = s->next)
2950     {
2951       const char *name;
2952 
2953       if ((s->flags & SEC_LINKER_CREATED) == 0)
2954 	continue;
2955 
2956       /* It's OK to base decisions on the section name, because none
2957 	 of the dynobj section names depend upon the input files.  */
2958       name = bfd_get_section_name (dynobj, s);
2959 
2960       if (strcmp (name, ".plt") == 0)
2961 	{
2962 	  /* Remember whether there is a PLT.  */
2963 	  plt = s->size != 0;
2964 	}
2965       else if (strncmp (name, ".rela", 5) == 0)
2966 	{
2967 	  if (s->size != 0)
2968 	    {
2969 	      /* Remember whether there are any reloc sections other
2970                  than .rela.plt.  */
2971 	      if (strcmp (name, ".rela.plt") != 0)
2972 		  relocs = TRUE;
2973 
2974 	      /* We use the reloc_count field as a counter if we need
2975 		 to copy relocs into the output file.  */
2976 	      s->reloc_count = 0;
2977 	    }
2978 	}
2979       else if (strncmp (name, ".got", 4) != 0
2980 	       && strcmp (name, ".dynbss") != 0)
2981 	{
2982 	  /* It's not one of our sections, so don't allocate space.  */
2983 	  continue;
2984 	}
2985 
2986       if (s->size == 0)
2987 	{
2988 	  /* If we don't need this section, strip it from the
2989 	     output file.  This is mostly to handle .rela.bss and
2990 	     .rela.plt.  We must create both sections in
2991 	     create_dynamic_sections, because they must be created
2992 	     before the linker maps input sections to output
2993 	     sections.  The linker does that before
2994 	     adjust_dynamic_symbol is called, and it is that
2995 	     function which decides whether anything needs to go
2996 	     into these sections.  */
2997 	  s->flags |= SEC_EXCLUDE;
2998 	  continue;
2999 	}
3000 
3001       if ((s->flags & SEC_HAS_CONTENTS) == 0)
3002 	continue;
3003 
3004       /* Allocate memory for the section contents. We use bfd_zalloc here
3005 	 in case unused entries are not reclaimed before the section's
3006 	 contents are written out.  This should not happen, but this way
3007 	 if it does, we will not write out garbage.  For reloc sections,
3008 	 this will make entries have the type R_CRIS_NONE.  */
3009       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3010       if (s->contents == NULL)
3011 	return FALSE;
3012     }
3013 
3014   if (elf_hash_table (info)->dynamic_sections_created)
3015     {
3016       /* Add some entries to the .dynamic section.  We fill in the
3017 	 values later, in elf_cris_finish_dynamic_sections, but we
3018 	 must add the entries now so that we get the correct size for
3019 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
3020 	 dynamic linker and used by the debugger.  */
3021 #define add_dynamic_entry(TAG, VAL) \
3022   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3023 
3024       if (!info->shared)
3025 	{
3026 	  if (!add_dynamic_entry (DT_DEBUG, 0))
3027 	    return FALSE;
3028 	}
3029 
3030       if (plt)
3031 	{
3032 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
3033 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
3034 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3035 	      || !add_dynamic_entry (DT_JMPREL, 0))
3036 	    return FALSE;
3037 	}
3038 
3039       if (relocs)
3040 	{
3041 	  if (!add_dynamic_entry (DT_RELA, 0)
3042 	      || !add_dynamic_entry (DT_RELASZ, 0)
3043 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3044 	    return FALSE;
3045 	}
3046 
3047       if ((info->flags & DF_TEXTREL) != 0)
3048 	{
3049 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
3050 	    return FALSE;
3051 	  info->flags |= DF_TEXTREL;
3052 	}
3053     }
3054 #undef add_dynamic_entry
3055 
3056   return TRUE;
3057 }
3058 
3059 /* This function is called via elf_cris_link_hash_traverse if we are
3060    creating a shared object.  In the -Bsymbolic case, it discards the
3061    space allocated to copy PC relative relocs against symbols which
3062    are defined in regular objects.  For the normal non-symbolic case,
3063    we also discard space for relocs that have become local due to
3064    symbol visibility changes.  We allocated space for them in the
3065    check_relocs routine, but we won't fill them in in the
3066    relocate_section routine.  */
3067 
3068 static bfd_boolean
elf_cris_discard_excess_dso_dynamics(h,inf)3069 elf_cris_discard_excess_dso_dynamics (h, inf)
3070      struct elf_cris_link_hash_entry *h;
3071      PTR inf;
3072 {
3073   struct elf_cris_pcrel_relocs_copied *s;
3074   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3075 
3076   if (h->root.root.type == bfd_link_hash_warning)
3077     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
3078 
3079   /* If a symbol has been forced local or we have found a regular
3080      definition for the symbolic link case, then we won't be needing
3081      any relocs.  */
3082   if (h->root.def_regular
3083       && (h->root.forced_local
3084 	  || info->symbolic))
3085     {
3086       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3087 	s->section->size -= s->count * sizeof (Elf32_External_Rela);
3088     }
3089 
3090   return TRUE;
3091 }
3092 
3093 /* This function is called via elf_cris_link_hash_traverse if we are *not*
3094    creating a shared object.  We discard space for relocs for symbols put
3095    in the .got, but which we found we do not have to resolve at run-time.  */
3096 
3097 static bfd_boolean
elf_cris_discard_excess_program_dynamics(h,inf)3098 elf_cris_discard_excess_program_dynamics (h, inf)
3099      struct elf_cris_link_hash_entry *h;
3100      PTR inf;
3101 {
3102   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3103 
3104   if (h->root.root.type == bfd_link_hash_warning)
3105     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
3106 
3107   /* If we're not creating a shared library and have a symbol which is
3108      referred to by .got references, but the symbol is defined locally,
3109      (or rather, not defined by a DSO) then lose the reloc for the .got
3110      (don't allocate room for it).  Likewise for relocs for something
3111      for which we create a PLT.  */
3112   if (!h->root.def_dynamic
3113       || h->root.plt.refcount > 0)
3114     {
3115       if (h->root.got.refcount > 0
3116 	  /* The size of this section is only valid and in sync with the
3117 	     various reference counts if we do dynamic; don't decrement it
3118 	     otherwise.  */
3119 	  && elf_hash_table (info)->dynamic_sections_created)
3120 	{
3121 	  bfd *dynobj = elf_hash_table (info)->dynobj;
3122 	  asection *srelgot;
3123 
3124 	  BFD_ASSERT (dynobj != NULL);
3125 
3126 	  srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3127 
3128 	  BFD_ASSERT (srelgot != NULL);
3129 
3130 	  srelgot->size -= sizeof (Elf32_External_Rela);
3131 	}
3132 
3133       /* If the locally-defined symbol isn't used by a DSO, then we don't
3134 	 have to export it as a dynamic symbol.  This was already done for
3135 	 functions; doing this for all symbols would presumably not
3136 	 introduce new problems.  Of course we don't do this if we're
3137 	 exporting all dynamic symbols.  */
3138       if (! info->export_dynamic
3139 	  && h->root.dynindx != -1
3140 	  && !h->root.def_dynamic
3141 	  && !h->root.ref_dynamic)
3142 	{
3143 	  h->root.dynindx = -1;
3144 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3145 				  h->root.dynstr_index);
3146 	}
3147     }
3148 
3149   return TRUE;
3150 }
3151 
3152 /* Reject a file depending on presence and expectation of prefixed
3153    underscores on symbols.  */
3154 
3155 static bfd_boolean
cris_elf_object_p(abfd)3156 cris_elf_object_p (abfd)
3157      bfd *abfd;
3158 {
3159   if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
3160     return FALSE;
3161 
3162   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
3163     return (bfd_get_symbol_leading_char (abfd) == '_');
3164   else
3165     return (bfd_get_symbol_leading_char (abfd) == 0);
3166 }
3167 
3168 /* Mark presence or absence of leading underscore.  Set machine type
3169    flags from mach type.  */
3170 
3171 static void
cris_elf_final_write_processing(abfd,linker)3172 cris_elf_final_write_processing (abfd, linker)
3173      bfd *abfd;
3174      bfd_boolean linker ATTRIBUTE_UNUSED;
3175 {
3176   unsigned long e_flags = elf_elfheader (abfd)->e_flags;
3177 
3178   e_flags &= ~EF_CRIS_UNDERSCORE;
3179   if (bfd_get_symbol_leading_char (abfd) == '_')
3180     e_flags |= EF_CRIS_UNDERSCORE;
3181 
3182   switch (bfd_get_mach (abfd))
3183     {
3184     case bfd_mach_cris_v0_v10:
3185       e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
3186       break;
3187 
3188     case bfd_mach_cris_v10_v32:
3189       e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
3190       break;
3191 
3192     case bfd_mach_cris_v32:
3193       e_flags |= EF_CRIS_VARIANT_V32;
3194       break;
3195 
3196     default:
3197       _bfd_abort (__FILE__, __LINE__,
3198 		  _("Unexpected machine number"));
3199     }
3200 
3201   elf_elfheader (abfd)->e_flags = e_flags;
3202 }
3203 
3204 /* Set the mach type from e_flags value.  */
3205 
3206 static bfd_boolean
cris_elf_set_mach_from_flags(abfd,flags)3207 cris_elf_set_mach_from_flags (abfd, flags)
3208      bfd *abfd;
3209      unsigned long flags;
3210 {
3211   switch (flags & EF_CRIS_VARIANT_MASK)
3212     {
3213     case EF_CRIS_VARIANT_ANY_V0_V10:
3214       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
3215       break;
3216 
3217     case EF_CRIS_VARIANT_V32:
3218       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
3219       break;
3220 
3221     case EF_CRIS_VARIANT_COMMON_V10_V32:
3222       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
3223       break;
3224 
3225     default:
3226       /* Since we don't recognize them, we obviously can't support them
3227 	 with this code; we'd have to require that all future handling
3228 	 would be optional.  */
3229       bfd_set_error (bfd_error_wrong_format);
3230       return FALSE;
3231     }
3232 
3233   return TRUE;
3234 }
3235 
3236 /* Display the flags field.  */
3237 
3238 static bfd_boolean
cris_elf_print_private_bfd_data(abfd,ptr)3239 cris_elf_print_private_bfd_data (abfd, ptr)
3240      bfd *abfd;
3241      PTR ptr;
3242 {
3243   FILE *file = (FILE *) ptr;
3244 
3245   BFD_ASSERT (abfd != NULL && ptr != NULL);
3246 
3247   _bfd_elf_print_private_bfd_data (abfd, ptr);
3248 
3249   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
3250 
3251   if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
3252     fprintf (file, _(" [symbols have a _ prefix]"));
3253   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3254       == EF_CRIS_VARIANT_COMMON_V10_V32)
3255     fprintf (file, _(" [v10 and v32]"));
3256   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3257       == EF_CRIS_VARIANT_V32)
3258     fprintf (file, _(" [v32]"));
3259 
3260   fputc ('\n', file);
3261   return TRUE;
3262 }
3263 
3264 /* Don't mix files with and without a leading underscore.  */
3265 
3266 static bfd_boolean
cris_elf_merge_private_bfd_data(ibfd,obfd)3267 cris_elf_merge_private_bfd_data (ibfd, obfd)
3268      bfd *ibfd;
3269      bfd *obfd;
3270 {
3271   int imach, omach;
3272 
3273   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3274     return FALSE;
3275 
3276   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3277       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3278     return TRUE;
3279 
3280   imach = bfd_get_mach (ibfd);
3281 
3282   if (! elf_flags_init (obfd))
3283     {
3284       /* This happens when ld starts out with a 'blank' output file.  */
3285       elf_flags_init (obfd) = TRUE;
3286 
3287       /* We ignore the linker-set mach, and instead set it according to
3288 	 the first input file.  This would also happen if we could
3289 	 somehow filter out the OUTPUT_ARCH () setting from elf.sc.
3290 	 This allows us to keep the same linker config across
3291 	 cris(v0..v10) and crisv32.  The drawback is that we can't force
3292 	 the output type, which might be a sane thing to do for a
3293 	 v10+v32 compatibility object.  */
3294       if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3295 	return FALSE;
3296     }
3297 
3298   if (bfd_get_symbol_leading_char (ibfd)
3299       != bfd_get_symbol_leading_char (obfd))
3300     {
3301       (*_bfd_error_handler)
3302 	(bfd_get_symbol_leading_char (ibfd) == '_'
3303 	 ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols")
3304 	 : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
3305 	 ibfd);
3306       bfd_set_error (bfd_error_bad_value);
3307       return FALSE;
3308     }
3309 
3310   omach = bfd_get_mach (obfd);
3311 
3312   if (imach != omach)
3313     {
3314       /* We can get an incompatible combination only if either is
3315 	 bfd_mach_cris_v32, and the other one isn't compatible.  */
3316       if ((imach == bfd_mach_cris_v32
3317 	   && omach != bfd_mach_cris_v10_v32)
3318 	  || (omach == bfd_mach_cris_v32
3319 	      && imach != bfd_mach_cris_v10_v32))
3320 	{
3321 	  (*_bfd_error_handler)
3322 	    ((imach == bfd_mach_cris_v32)
3323 	     ? _("%B contains CRIS v32 code, incompatible"
3324 		 " with previous objects")
3325 	     : _("%B contains non-CRIS-v32 code, incompatible"
3326 		 " with previous objects"),
3327 	     ibfd);
3328 	  bfd_set_error (bfd_error_bad_value);
3329 	  return FALSE;
3330 	}
3331 
3332       /* We don't have to check the case where the input is compatible
3333 	 with v10 and v32, because the output is already known to be set
3334 	 to the other (compatible) mach.  */
3335       if (omach == bfd_mach_cris_v10_v32
3336 	  && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3337 	return FALSE;
3338     }
3339 
3340   return TRUE;
3341 }
3342 
3343 /* Do side-effects of e_flags copying to obfd.  */
3344 
3345 static bfd_boolean
cris_elf_copy_private_bfd_data(ibfd,obfd)3346 cris_elf_copy_private_bfd_data (ibfd, obfd)
3347      bfd *ibfd;
3348      bfd *obfd;
3349 {
3350   /* Call the base function.  */
3351   if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
3352     return FALSE;
3353 
3354   /* If output is big-endian for some obscure reason, stop here.  */
3355   if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
3356     return FALSE;
3357 
3358   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3359       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3360     return TRUE;
3361 
3362   /* Do what we really came here for.  */
3363   return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
3364 }
3365 
3366 static enum elf_reloc_type_class
elf_cris_reloc_type_class(rela)3367 elf_cris_reloc_type_class (rela)
3368      const Elf_Internal_Rela *rela;
3369 {
3370   switch ((int) ELF32_R_TYPE (rela->r_info))
3371     {
3372     case R_CRIS_RELATIVE:
3373       return reloc_class_relative;
3374     case R_CRIS_JUMP_SLOT:
3375       return reloc_class_plt;
3376     case R_CRIS_COPY:
3377       return reloc_class_copy;
3378     default:
3379       return reloc_class_normal;
3380     }
3381 }
3382 
3383 #define ELF_ARCH		bfd_arch_cris
3384 #define ELF_MACHINE_CODE	EM_CRIS
3385 #define ELF_MAXPAGESIZE		0x2000
3386 
3387 #define TARGET_LITTLE_SYM	bfd_elf32_cris_vec
3388 #define TARGET_LITTLE_NAME	"elf32-cris"
3389 #define elf_symbol_leading_char 0
3390 
3391 #define elf_info_to_howto_rel			NULL
3392 #define elf_info_to_howto			cris_info_to_howto_rela
3393 #define elf_backend_relocate_section		cris_elf_relocate_section
3394 #define elf_backend_gc_mark_hook		cris_elf_gc_mark_hook
3395 #define elf_backend_gc_sweep_hook		cris_elf_gc_sweep_hook
3396 #define elf_backend_check_relocs                cris_elf_check_relocs
3397 #define elf_backend_grok_prstatus		cris_elf_grok_prstatus
3398 #define elf_backend_grok_psinfo			cris_elf_grok_psinfo
3399 
3400 #define elf_backend_can_gc_sections		1
3401 #define elf_backend_can_refcount		1
3402 
3403 #define elf_backend_object_p			cris_elf_object_p
3404 #define elf_backend_final_write_processing \
3405 	cris_elf_final_write_processing
3406 #define bfd_elf32_bfd_print_private_bfd_data \
3407 	cris_elf_print_private_bfd_data
3408 #define bfd_elf32_bfd_merge_private_bfd_data \
3409 	cris_elf_merge_private_bfd_data
3410 #define bfd_elf32_bfd_copy_private_bfd_data \
3411 	cris_elf_copy_private_bfd_data
3412 
3413 #define bfd_elf32_bfd_reloc_type_lookup		cris_reloc_type_lookup
3414 
3415 #define bfd_elf32_bfd_link_hash_table_create \
3416 	elf_cris_link_hash_table_create
3417 #define elf_backend_adjust_dynamic_symbol \
3418 	elf_cris_adjust_dynamic_symbol
3419 #define elf_backend_size_dynamic_sections \
3420 	elf_cris_size_dynamic_sections
3421 #define elf_backend_finish_dynamic_symbol \
3422 	elf_cris_finish_dynamic_symbol
3423 #define elf_backend_finish_dynamic_sections \
3424 	elf_cris_finish_dynamic_sections
3425 #define elf_backend_create_dynamic_sections \
3426 	_bfd_elf_create_dynamic_sections
3427 #define bfd_elf32_bfd_final_link \
3428 	bfd_elf_gc_common_final_link
3429 #define elf_backend_hide_symbol			elf_cris_hide_symbol
3430 #define elf_backend_reloc_type_class		elf_cris_reloc_type_class
3431 
3432 #define elf_backend_want_got_plt	1
3433 #define elf_backend_plt_readonly	1
3434 #define elf_backend_want_plt_sym	0
3435 #define elf_backend_got_header_size	12
3436 
3437 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3438    linking and libraries (if it's a win of any significance).  Until then,
3439    take the easy route.  */
3440 #define elf_backend_may_use_rel_p 0
3441 #define elf_backend_may_use_rela_p 1
3442 #define elf_backend_rela_normal		1
3443 
3444 #include "elf32-target.h"
3445 
3446 #define INCLUDED_TARGET_FILE
3447 
3448 #undef TARGET_LITTLE_SYM
3449 #undef TARGET_LITTLE_NAME
3450 #undef elf_symbol_leading_char
3451 
3452 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3453 #define TARGET_LITTLE_NAME "elf32-us-cris"
3454 #define elf_symbol_leading_char '_'
3455 
3456 #include "elf32-target.h"
3457