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