* config/tc-i386.h (NOP_OPCODE): Restore.
[external/binutils.git] / bfd / elf32-cris.c
1 /* CRIS-specific support for 32-bit ELF.
2    Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3    Free Software Foundation, Inc.
4    Contributed by Axis Communications AB.
5    Written by Hans-Peter Nilsson, based on elf32-fr30.c
6    PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
7
8    This file is part of BFD, the Binary File Descriptor library.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "elf/cris.h"
29
30 /* Forward declarations.  */
31 static reloc_howto_type * cris_reloc_type_lookup
32   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
33
34 static void cris_info_to_howto_rela
35   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
36
37 static bfd_reloc_status_type cris_elf_pcrel_reloc
38   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
39
40 static bfd_boolean cris_elf_grok_prstatus
41   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
42
43 static bfd_boolean cris_elf_grok_psinfo
44   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
45
46 static bfd_boolean cris_elf_relocate_section
47   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
48            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
49
50 static bfd_reloc_status_type cris_final_link_relocate
51   PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *,
52            Elf_Internal_Rela *, bfd_vma));
53
54 static bfd_boolean cris_elf_object_p PARAMS ((bfd *));
55
56 static void cris_elf_final_write_processing PARAMS ((bfd *, bfd_boolean));
57
58 static bfd_boolean cris_elf_set_mach_from_flags
59   PARAMS ((bfd *, unsigned long int));
60
61 static bfd_boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
62
63 static bfd_boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
64 static bfd_boolean cris_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
65
66 struct elf_cris_link_hash_entry;
67 static bfd_boolean elf_cris_discard_excess_dso_dynamics
68   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
69 static bfd_boolean elf_cris_discard_excess_program_dynamics
70   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
71 static bfd_boolean elf_cris_adjust_gotplt_to_got
72   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
73 static bfd_boolean elf_cris_try_fold_plt_to_got
74   PARAMS ((struct elf_cris_link_hash_entry *, PTR));
75 static struct bfd_hash_entry *elf_cris_link_hash_newfunc
76   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
77 static struct bfd_link_hash_table *elf_cris_link_hash_table_create
78   PARAMS ((bfd *));
79 static bfd_boolean elf_cris_adjust_dynamic_symbol
80   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
81 static bfd_boolean cris_elf_check_relocs
82   PARAMS ((bfd *, struct bfd_link_info *, asection *,
83            const Elf_Internal_Rela *));
84
85 static bfd_boolean elf_cris_size_dynamic_sections
86   PARAMS ((bfd *, struct bfd_link_info *));
87 static bfd_boolean elf_cris_finish_dynamic_symbol
88   PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
89            Elf_Internal_Sym *));
90 static bfd_boolean elf_cris_finish_dynamic_sections
91   PARAMS ((bfd *, struct bfd_link_info *));
92 static void elf_cris_hide_symbol
93   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
94 static enum elf_reloc_type_class elf_cris_reloc_type_class
95   PARAMS ((const Elf_Internal_Rela *));
96
97 static 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 \f
414 /* Map BFD reloc types to CRIS ELF reloc types.  */
415
416 struct cris_reloc_map
417 {
418   bfd_reloc_code_real_type bfd_reloc_val;
419   unsigned int cris_reloc_val;
420 };
421
422 static 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
446 static reloc_howto_type *
447 cris_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
462 static void
463 cris_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
475 bfd_reloc_status_type
476 cris_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 \f
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
508 static bfd_boolean
509 cris_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
560 static bfd_boolean
561 cris_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 \f
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
617 static 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
629 static 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
646 static 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
658 static 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
674 static 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
682 static 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
699 static 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
711 static 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 \f
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
739 struct 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
751 struct 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
774 struct 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
799 static struct bfd_hash_entry *
800 elf_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
833 static struct bfd_link_hash_table *
834 elf_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 \f
859 /* Perform a single relocation.  By default we use the standard BFD
860    routines, with a few tweaks.  */
861
862 static bfd_reloc_status_type
863 cris_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 \f
905 /* Relocate an CRIS ELF section.  See elf32-fr30.c, from where this was
906    copied, for further comments.  */
907
908 static bfd_boolean
909 cris_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   dynobj = elf_hash_table (info)->dynobj;
931   local_got_offsets = elf_local_got_offsets (input_bfd);
932   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
933   sym_hashes = elf_sym_hashes (input_bfd);
934   relend     = relocs + input_section->reloc_count;
935
936   sgot = NULL;
937   splt = NULL;
938   sreloc = NULL;
939
940   if (dynobj != NULL)
941     {
942       splt = bfd_get_section_by_name (dynobj, ".plt");
943       sgot = bfd_get_section_by_name (dynobj, ".got");
944     }
945
946   for (rel = relocs; rel < relend; rel ++)
947     {
948       reloc_howto_type *howto;
949       unsigned long r_symndx;
950       Elf_Internal_Sym *sym;
951       asection *sec;
952       struct elf_link_hash_entry *h;
953       bfd_vma relocation;
954       bfd_reloc_status_type r;
955       const char *symname = NULL;
956       int r_type;
957
958       r_type = ELF32_R_TYPE (rel->r_info);
959
960       if (   r_type == R_CRIS_GNU_VTINHERIT
961           || r_type == R_CRIS_GNU_VTENTRY)
962         continue;
963
964       r_symndx = ELF32_R_SYM (rel->r_info);
965       howto  = cris_elf_howto_table + r_type;
966       h      = NULL;
967       sym    = NULL;
968       sec    = NULL;
969
970       if (r_symndx < symtab_hdr->sh_info)
971         {
972           sym = local_syms + r_symndx;
973           sec = local_sections [r_symndx];
974           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
975
976           symname = (bfd_elf_string_from_elf_section
977                      (input_bfd, symtab_hdr->sh_link, sym->st_name));
978           if (symname == NULL)
979             symname = bfd_section_name (input_bfd, sec);
980         }
981       else
982         {
983           bfd_boolean warned;
984           bfd_boolean unresolved_reloc;
985
986           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
987                                    r_symndx, symtab_hdr, sym_hashes,
988                                    h, sec, relocation,
989                                    unresolved_reloc, warned);
990
991           if (unresolved_reloc
992               /* Perhaps we should detect the cases that
993                  sec->output_section is expected to be NULL like i386 and
994                  m68k, but apparently (and according to elfxx-ia64.c) all
995                  valid cases are where the symbol is defined in a shared
996                  object which we link dynamically against.  This includes
997                  PLT relocs for which we've created a PLT entry and other
998                  relocs for which we're prepared to create dynamic
999                  relocations.
1000
1001                  For now, new situations cause us to just err when
1002                  sec->output_offset is NULL but the object with the symbol
1003                  is *not* dynamically linked against.  Thus this will
1004                  automatically remind us so we can see if there are other
1005                  valid cases we need to revisit.  */
1006               && (sec->owner->flags & DYNAMIC) != 0)
1007             relocation = 0;
1008
1009           else if (h->root.type == bfd_link_hash_defined
1010                    || h->root.type == bfd_link_hash_defweak)
1011             {
1012               /* Here follow the cases where the relocation value must
1013                  be zero (or when further handling is simplified when
1014                  zero).  I can't claim to understand the various
1015                  conditions and they weren't described in the files
1016                  where I copied them from (elf32-m68k.c and
1017                  elf32-i386.c), but let's mention examples of where
1018                  they happen.  FIXME: Perhaps define and use a
1019                  dynamic_symbol_p function like ia64.
1020
1021                  - When creating a shared library, we can have an
1022                  ordinary relocation for a symbol defined in a shared
1023                  library (perhaps the one we create).  We then make
1024                  the relocation value zero, as the value seen now will
1025                  be added into the relocation addend in this shared
1026                  library, but must be handled only at dynamic-link
1027                  time.  FIXME: Not sure this example covers the
1028                  h->elf_link_hash_flags test, though it's there in
1029                  other targets.  */
1030               if (info->shared
1031                   && ((! info->symbolic && h->dynindx != -1)
1032                       || !h->def_regular)
1033                   && (input_section->flags & SEC_ALLOC) != 0
1034                   && (r_type == R_CRIS_8
1035                       || r_type == R_CRIS_16
1036                       || r_type == R_CRIS_32
1037                       || r_type == R_CRIS_8_PCREL
1038                       || r_type == R_CRIS_16_PCREL
1039                       || r_type == R_CRIS_32_PCREL))
1040                 relocation = 0;
1041               else if (!info->relocatable && unresolved_reloc)
1042                 {
1043                   _bfd_error_handler
1044                     (_("%B, section %A: unresolvable relocation %s against symbol `%s'"),
1045                      input_bfd,
1046                      input_section,
1047                      cris_elf_howto_table[r_type].name,
1048                      symname);
1049                   bfd_set_error (bfd_error_bad_value);
1050                   return FALSE;
1051                 }
1052             }
1053         }
1054
1055       if (sec != NULL && elf_discarded_section (sec))
1056         {
1057           /* For relocs against symbols from removed linkonce sections,
1058              or sections discarded by a linker script, we just want the
1059              section contents zeroed.  Avoid any special processing.  */
1060           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1061           rel->r_info = 0;
1062           rel->r_addend = 0;
1063           continue;
1064         }
1065
1066       if (info->relocatable)
1067         continue;
1068
1069       switch (r_type)
1070         {
1071         case R_CRIS_16_GOTPLT:
1072         case R_CRIS_32_GOTPLT:
1073           /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
1074              but we require a PLT, and the PLT handling will take care of
1075              filling in the PLT-specific GOT entry.  For the GOT offset,
1076              calculate it as we do when filling it in for the .got.plt
1077              section.  If we don't have a PLT, punt to GOT handling.  */
1078           if (h != NULL
1079               && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
1080             {
1081               asection *sgotplt
1082                 = bfd_get_section_by_name (dynobj, ".got.plt");
1083               bfd_vma got_offset;
1084
1085               BFD_ASSERT (h->dynindx != -1);
1086               BFD_ASSERT (sgotplt != NULL);
1087
1088               got_offset
1089                 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1090
1091               relocation = got_offset;
1092               break;
1093             }
1094
1095           /* We didn't make a PLT entry for this symbol.  Maybe everything is
1096              folded into the GOT.  Other than folding, this happens when
1097              statically linking PIC code, or when using -Bsymbolic.  Check
1098              that we instead have a GOT entry as done for us by
1099              elf_cris_adjust_dynamic_symbol, and drop through into the
1100              ordinary GOT cases.  This must not happen for the
1101              executable, because any reference it does to a function
1102              that is satisfied by a DSO must generate a PLT.  We assume
1103              these call-specific relocs don't address non-functions.  */
1104           if (h != NULL
1105               && (h->got.offset == (bfd_vma) -1
1106                   || (!info->shared
1107                       && !(h->def_regular
1108                            || (!h->def_dynamic
1109                                && h->root.type == bfd_link_hash_undefweak)))))
1110             {
1111               (*_bfd_error_handler)
1112                 ((h->got.offset == (bfd_vma) -1)
1113                  ? _("%B, section %A: No PLT nor GOT for relocation %s"
1114                      " against symbol `%s'")
1115                  : _("%B, section %A: No PLT for relocation %s"
1116                      " against symbol `%s'"),
1117                  input_bfd,
1118                  input_section,
1119                  cris_elf_howto_table[r_type].name,
1120                  (symname != NULL && symname[0] != '\0'
1121                   ? symname : _("[whose name is lost]")));
1122
1123               /* FIXME: Perhaps blaming input is not the right thing to
1124                  do; this is probably an internal error.  But it is true
1125                  that we didn't like that particular input.  */
1126               bfd_set_error (bfd_error_bad_value);
1127               return FALSE;
1128             }
1129           /* Fall through.  */
1130
1131           /* The size of the actual relocation is not used here; we only
1132              fill in the GOT table here.  */
1133         case R_CRIS_16_GOT:
1134         case R_CRIS_32_GOT:
1135           {
1136             bfd_vma off;
1137
1138             /* Note that despite using RELA relocations, the .got contents
1139                is always filled in with the link-relative relocation
1140                value; the addend.  */
1141
1142             if (h != NULL)
1143               {
1144                 off = h->got.offset;
1145                 BFD_ASSERT (off != (bfd_vma) -1);
1146
1147                 if (!elf_hash_table (info)->dynamic_sections_created
1148                     || (! info->shared
1149                         && (h->def_regular
1150                             || h->type == STT_FUNC
1151                             || h->needs_plt))
1152                     || (info->shared
1153                         && (info->symbolic || h->dynindx == -1)
1154                         && h->def_regular))
1155                   {
1156                     /* This wasn't checked above for ! info->shared, but
1157                        must hold there if we get here; the symbol must
1158                        be defined in the regular program or be undefweak
1159                        or be a function or otherwise need a PLT.  */
1160                     BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
1161                                 || info->shared
1162                                 || h->def_regular
1163                                 || h->type == STT_FUNC
1164                                 || h->needs_plt
1165                                 || h->root.type == bfd_link_hash_undefweak);
1166
1167                     /* This is actually a static link, or it is a
1168                        -Bsymbolic link and the symbol is defined locally,
1169                        or is undefweak, or the symbol was forced to be
1170                        local because of a version file, or we're not
1171                        creating a dynamic object.  We must initialize this
1172                        entry in the global offset table.  Since the offset
1173                        must always be a multiple of 4, we use the least
1174                        significant bit to record whether we have
1175                        initialized it already.
1176
1177                        If this GOT entry should be runtime-initialized, we
1178                        will create a .rela.got relocation entry to
1179                        initialize the value.  This is done in the
1180                        finish_dynamic_symbol routine.  */
1181                     if ((off & 1) != 0)
1182                       off &= ~1;
1183                     else
1184                       {
1185                         bfd_put_32 (output_bfd, relocation,
1186                                     sgot->contents + off);
1187                         h->got.offset |= 1;
1188                       }
1189                   }
1190               }
1191             else
1192               {
1193                 BFD_ASSERT (local_got_offsets != NULL
1194                             && local_got_offsets[r_symndx] != (bfd_vma) -1);
1195
1196                 off = local_got_offsets[r_symndx];
1197
1198                 /* The offset must always be a multiple of 4.  We use
1199                    the least significant bit to record whether we have
1200                    already generated the necessary reloc.  */
1201                 if ((off & 1) != 0)
1202                   off &= ~1;
1203                 else
1204                   {
1205                     bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1206
1207                     if (info->shared)
1208                       {
1209                         asection *s;
1210                         Elf_Internal_Rela outrel;
1211                         bfd_byte *loc;
1212
1213                         s = bfd_get_section_by_name (dynobj, ".rela.got");
1214                         BFD_ASSERT (s != NULL);
1215
1216                         outrel.r_offset = (sgot->output_section->vma
1217                                            + sgot->output_offset
1218                                            + off);
1219                         outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1220                         outrel.r_addend = relocation;
1221                         loc = s->contents;
1222                         loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1223                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1224                       }
1225
1226                     local_got_offsets[r_symndx] |= 1;
1227                   }
1228               }
1229
1230             relocation = sgot->output_offset + off;
1231             if (rel->r_addend != 0)
1232               {
1233                 /* We can't do anything for a relocation which is against
1234                    a symbol *plus offset*.  GOT holds relocations for
1235                    symbols.  Make this an error; the compiler isn't
1236                    allowed to pass us these kinds of things.  */
1237                 if (h == NULL)
1238                   (*_bfd_error_handler)
1239                     (_("%B, section %A: relocation %s with non-zero addend %d"
1240                        " against local symbol"),
1241                      input_bfd,
1242                      input_section,
1243                      cris_elf_howto_table[r_type].name,
1244                      rel->r_addend);
1245                 else
1246                   (*_bfd_error_handler)
1247                     (_("%B, section %A: relocation %s with non-zero addend %d"
1248                        " against symbol `%s'"),
1249                      input_bfd,
1250                      input_section,
1251                      cris_elf_howto_table[r_type].name,
1252                      rel->r_addend,
1253                      symname[0] != '\0' ? symname : _("[whose name is lost]"));
1254
1255                 bfd_set_error (bfd_error_bad_value);
1256                 return FALSE;
1257               }
1258           }
1259           break;
1260
1261         case R_CRIS_32_GOTREL:
1262           /* This relocation must only be performed against local symbols.
1263              It's also ok when we link a program and the symbol is either
1264              defined in an ordinary (non-DSO) object or is undefined weak.  */
1265           if (h != NULL
1266               && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1267               && !(!info->shared
1268                    && (h->def_regular
1269                        || (!h->def_dynamic
1270                            && h->root.type == bfd_link_hash_undefweak))))
1271             {
1272               (*_bfd_error_handler)
1273                 (_("%B, section %A: relocation %s is"
1274                    " not allowed for global symbol: `%s'"),
1275                  input_bfd,
1276                  input_section,
1277                  cris_elf_howto_table[r_type].name,
1278                  symname);
1279               bfd_set_error (bfd_error_bad_value);
1280               return FALSE;
1281             }
1282
1283           /* This can happen if we get a link error with the input ELF
1284              variant mismatching the output variant.  Emit an error so
1285              it's noticed if it happens elsewhere.  */
1286           if (sgot == NULL)
1287             {
1288               (*_bfd_error_handler)
1289                 (_("%B, section %A: relocation %s with no GOT created"),
1290                  input_bfd,
1291                  input_section,
1292                  cris_elf_howto_table[r_type].name);
1293               bfd_set_error (bfd_error_bad_value);
1294               return FALSE;
1295             }
1296
1297           /* This relocation is like a PC-relative one, except the
1298              reference point is the location of GOT.  Note that
1299              sgot->output_offset is not involved in this calculation.  We
1300              always want the start of entire .got section, not the
1301              position after the reserved header.  */
1302           relocation -= sgot->output_section->vma;
1303           break;
1304
1305         case R_CRIS_32_PLT_PCREL:
1306           /* Relocation is to the entry for this symbol in the
1307              procedure linkage table.  */
1308
1309           /* Resolve a PLT_PCREL reloc against a local symbol directly,
1310              without using the procedure linkage table.  */
1311           if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1312             break;
1313
1314           if (h->plt.offset == (bfd_vma) -1
1315               || splt == NULL)
1316             {
1317               /* We didn't make a PLT entry for this symbol.  This
1318                  happens when statically linking PIC code, or when
1319                  using -Bsymbolic.  */
1320               break;
1321             }
1322
1323           relocation = (splt->output_section->vma
1324                         + splt->output_offset
1325                         + h->plt.offset);
1326           break;
1327
1328         case R_CRIS_32_PLT_GOTREL:
1329           /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1330              start of the .got section.  See also comment at
1331              R_CRIS_32_GOT.  */
1332           relocation -= sgot->output_section->vma;
1333
1334           /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1335              without using the procedure linkage table.  */
1336           if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1337             break;
1338
1339           if (h->plt.offset == (bfd_vma) -1
1340               || splt == NULL)
1341             {
1342               /* We didn't make a PLT entry for this symbol.  This
1343                  happens when statically linking PIC code, or when
1344                  using -Bsymbolic.  */
1345               break;
1346             }
1347
1348           relocation = (splt->output_section->vma
1349                         + splt->output_offset
1350                         + h->plt.offset
1351                         - sgot->output_section->vma);
1352           break;
1353
1354         case R_CRIS_8_PCREL:
1355         case R_CRIS_16_PCREL:
1356         case R_CRIS_32_PCREL:
1357           /* If the symbol was local, we need no shlib-specific handling.  */
1358           if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1359             break;
1360
1361           /* Fall through.  */
1362         case R_CRIS_8:
1363         case R_CRIS_16:
1364         case R_CRIS_32:
1365           if (info->shared
1366               && r_symndx != 0
1367               && (input_section->flags & SEC_ALLOC) != 0
1368               && ((r_type != R_CRIS_8_PCREL
1369                    && r_type != R_CRIS_16_PCREL
1370                    && r_type != R_CRIS_32_PCREL)
1371                   || (!info->symbolic
1372                       || !h->def_regular)))
1373             {
1374               Elf_Internal_Rela outrel;
1375               bfd_byte *loc;
1376               bfd_boolean skip, relocate;
1377
1378               /* When generating a shared object, these relocations
1379                  are copied into the output file to be resolved at run
1380                  time.  */
1381
1382               if (sreloc == NULL)
1383                 {
1384                   const char *name;
1385
1386                   name = (bfd_elf_string_from_elf_section
1387                           (input_bfd,
1388                            elf_elfheader (input_bfd)->e_shstrndx,
1389                            elf_section_data (input_section)->rel_hdr.sh_name));
1390                   if (name == NULL)
1391                     return FALSE;
1392
1393                   BFD_ASSERT (CONST_STRNEQ (name, ".rela")
1394                               && strcmp (bfd_get_section_name (input_bfd,
1395                                                                input_section),
1396                                          name + 5) == 0);
1397
1398                   sreloc = bfd_get_section_by_name (dynobj, name);
1399
1400                   /* That section should have been created in
1401                      cris_elf_check_relocs, but that function will not be
1402                      called for objects which fail in
1403                      cris_elf_merge_private_bfd_data.  */
1404                   if (sreloc == NULL)
1405                     {
1406                       (*_bfd_error_handler)
1407                         (_("%B: Internal inconsistency; no relocation section %s"),
1408                          input_bfd,
1409                          name);
1410
1411                       bfd_set_error (bfd_error_bad_value);
1412                       return FALSE;
1413                     }
1414                 }
1415
1416               skip = FALSE;
1417               relocate = FALSE;
1418
1419               outrel.r_offset =
1420                 _bfd_elf_section_offset (output_bfd, info, input_section,
1421                                          rel->r_offset);
1422               if (outrel.r_offset == (bfd_vma) -1)
1423                 skip = TRUE;
1424               else if (outrel.r_offset == (bfd_vma) -2)
1425                 skip = TRUE, relocate = TRUE;
1426               outrel.r_offset += (input_section->output_section->vma
1427                                   + input_section->output_offset);
1428
1429               if (skip)
1430                 memset (&outrel, 0, sizeof outrel);
1431               /* h->dynindx may be -1 if the symbol was marked to
1432                  become local.  */
1433               else if (h != NULL
1434                        && ((! info->symbolic && h->dynindx != -1)
1435                            || !h->def_regular))
1436                 {
1437                   BFD_ASSERT (h->dynindx != -1);
1438                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1439                   outrel.r_addend = relocation + rel->r_addend;
1440                 }
1441               else
1442                 {
1443                   outrel.r_addend = relocation + rel->r_addend;
1444
1445                   if (r_type == R_CRIS_32)
1446                     {
1447                       relocate = TRUE;
1448                       outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1449                     }
1450                   else
1451                     {
1452                       long indx;
1453
1454                       if (bfd_is_abs_section (sec))
1455                         indx = 0;
1456                       else if (sec == NULL || sec->owner == NULL)
1457                         {
1458                           bfd_set_error (bfd_error_bad_value);
1459                           return FALSE;
1460                         }
1461                       else
1462                         {
1463                           asection *osec;
1464
1465                           /* We are turning this relocation into one
1466                              against a section symbol.  It would be
1467                              proper to subtract the symbol's value,
1468                              osec->vma, from the emitted reloc addend,
1469                              but ld.so expects buggy relocs.  */
1470                           osec = sec->output_section;
1471                           indx = elf_section_data (osec)->dynindx;
1472                           if (indx == 0)
1473                             {
1474                               struct elf_cris_link_hash_table *htab;
1475                               htab = elf_cris_hash_table (info);
1476                               osec = htab->root.text_index_section;
1477                               indx = elf_section_data (osec)->dynindx;
1478                             }
1479                           BFD_ASSERT (indx != 0);
1480                         }
1481
1482                       outrel.r_info = ELF32_R_INFO (indx, r_type);
1483                     }
1484                 }
1485
1486               loc = sreloc->contents;
1487               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1488               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1489
1490               /* This reloc will be computed at runtime, so there's no
1491                  need to do anything now, except for R_CRIS_32 relocations
1492                  that have been turned into R_CRIS_RELATIVE.  */
1493               if (!relocate)
1494                 continue;
1495             }
1496
1497           break;
1498         }
1499
1500       r = cris_final_link_relocate (howto, input_bfd, input_section,
1501                                      contents, rel, relocation);
1502
1503       if (r != bfd_reloc_ok)
1504         {
1505           const char * msg = (const char *) NULL;
1506
1507           switch (r)
1508             {
1509             case bfd_reloc_overflow:
1510               r = info->callbacks->reloc_overflow
1511                 (info, (h ? &h->root : NULL), symname, howto->name,
1512                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1513               break;
1514
1515             case bfd_reloc_undefined:
1516               r = info->callbacks->undefined_symbol
1517                 (info, symname, input_bfd, input_section, rel->r_offset,
1518                  TRUE);
1519               break;
1520
1521             case bfd_reloc_outofrange:
1522               msg = _("internal error: out of range error");
1523               break;
1524
1525             case bfd_reloc_notsupported:
1526               msg = _("internal error: unsupported relocation error");
1527               break;
1528
1529             case bfd_reloc_dangerous:
1530               msg = _("internal error: dangerous relocation");
1531               break;
1532
1533             default:
1534               msg = _("internal error: unknown error");
1535               break;
1536             }
1537
1538           if (msg)
1539             r = info->callbacks->warning
1540               (info, msg, symname, input_bfd, input_section, rel->r_offset);
1541
1542           if (! r)
1543             return FALSE;
1544         }
1545     }
1546
1547   return TRUE;
1548 }
1549 \f
1550 /* Finish up dynamic symbol handling.  We set the contents of various
1551    dynamic sections here.  */
1552
1553 static bfd_boolean
1554 elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym)
1555      bfd *output_bfd;
1556      struct bfd_link_info *info;
1557      struct elf_link_hash_entry *h;
1558      Elf_Internal_Sym *sym;
1559 {
1560   bfd *dynobj;
1561
1562   /* Where in the plt entry to put values.  */
1563   int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
1564
1565   /* What offset to add to the distance to the first PLT entry for the
1566      value at plt_off3.   */
1567   int plt_off3_value_bias = 4;
1568
1569   /* Where in the PLT entry the call-dynlink-stub is (happens to be same
1570      for PIC and non-PIC for v32 and pre-v32).  */
1571   int plt_stub_offset = 8;
1572   int plt_entry_size = PLT_ENTRY_SIZE;
1573   const bfd_byte *plt_entry = elf_cris_plt_entry;
1574   const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
1575
1576   /* Adjust the various PLT entry offsets.  */
1577   if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
1578     {
1579       plt_off2 = 14;
1580       plt_off3 = 20;
1581       plt_off3_value_bias = -2;
1582       plt_stub_offset = 12;
1583       plt_entry_size = PLT_ENTRY_SIZE_V32;
1584       plt_entry = elf_cris_plt_entry_v32;
1585       plt_pic_entry = elf_cris_pic_plt_entry_v32;
1586     }
1587
1588   dynobj = elf_hash_table (info)->dynobj;
1589
1590   if (h->plt.offset != (bfd_vma) -1)
1591     {
1592       asection *splt;
1593       asection *sgotplt;
1594       asection *sgot;
1595       asection *srela;
1596       bfd_vma got_base;
1597
1598       bfd_vma gotplt_offset
1599         = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1600       Elf_Internal_Rela rela;
1601       bfd_byte *loc;
1602       bfd_boolean has_gotplt = gotplt_offset != 0;
1603
1604       /* Get the index in the procedure linkage table which
1605          corresponds to this symbol.  This is the index of this symbol
1606          in all the symbols for which we are making plt entries.  The
1607          first entry in the procedure linkage table is reserved.  */
1608       /* We have to count backwards here, and the result is only valid as
1609          an index into .got.plt and its relocations.  FIXME: Constants...  */
1610       bfd_vma gotplt_index = gotplt_offset/4 - 3;
1611
1612       /* Get the offset into the .got table of the entry that corresponds
1613          to this function.  Note that we embed knowledge that "incoming"
1614          .got goes after .got.plt in the output without padding (pointer
1615          aligned).  However, that knowledge is present in several other
1616          places too.  */
1617       bfd_vma got_offset
1618         = (has_gotplt
1619            ? gotplt_offset
1620            : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry);
1621
1622       /* This symbol has an entry in the procedure linkage table.  Set it
1623          up.  */
1624
1625       BFD_ASSERT (h->dynindx != -1);
1626
1627       splt = bfd_get_section_by_name (dynobj, ".plt");
1628       sgot = bfd_get_section_by_name (dynobj, ".got");
1629       sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
1630       srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1631       BFD_ASSERT (splt != NULL && sgotplt != NULL
1632                   && (! has_gotplt || srela != NULL));
1633
1634       got_base = sgotplt->output_section->vma + sgotplt->output_offset;
1635
1636       /* Fill in the entry in the procedure linkage table.  */
1637       if (! info->shared)
1638         {
1639           memcpy (splt->contents + h->plt.offset, plt_entry,
1640                   plt_entry_size);
1641
1642           /* We need to enter the absolute address of the GOT entry here.  */
1643           bfd_put_32 (output_bfd, got_base + got_offset,
1644                       splt->contents + h->plt.offset + plt_off1);
1645         }
1646       else
1647         {
1648           memcpy (splt->contents + h->plt.offset, plt_pic_entry,
1649                   plt_entry_size);
1650           bfd_put_32 (output_bfd, got_offset,
1651                       splt->contents + h->plt.offset + plt_off1);
1652         }
1653
1654       /* Fill in the plt entry and make a relocation, if this is a "real"
1655          PLT entry.  */
1656       if (has_gotplt)
1657         {
1658           /* Fill in the offset to the reloc table.  */
1659           bfd_put_32 (output_bfd,
1660                       gotplt_index * sizeof (Elf32_External_Rela),
1661                       splt->contents + h->plt.offset + plt_off2);
1662
1663           /* Fill in the offset to the first PLT entry, where to "jump".  */
1664           bfd_put_32 (output_bfd,
1665                       - (h->plt.offset + plt_off3 + plt_off3_value_bias),
1666                       splt->contents + h->plt.offset + plt_off3);
1667
1668           /* Fill in the entry in the global offset table with the address of
1669              the relocating stub.  */
1670           bfd_put_32 (output_bfd,
1671                       (splt->output_section->vma
1672                        + splt->output_offset
1673                        + h->plt.offset
1674                        + plt_stub_offset),
1675                       sgotplt->contents + got_offset);
1676
1677           /* Fill in the entry in the .rela.plt section.  */
1678           rela.r_offset = (sgotplt->output_section->vma
1679                            + sgotplt->output_offset
1680                            + got_offset);
1681           rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
1682           rela.r_addend = 0;
1683           loc = srela->contents + gotplt_index * sizeof (Elf32_External_Rela);
1684           bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1685         }
1686
1687       if (!h->def_regular)
1688         {
1689           /* Mark the symbol as undefined, rather than as defined in
1690              the .plt section.  Leave the value alone.  */
1691           sym->st_shndx = SHN_UNDEF;
1692
1693           /* FIXME: From elf32-sparc.c 2001-02-19 (1.18).  I still don't
1694              know whether resetting the value is significant; if it really
1695              is, rather than a quirk or bug in the sparc port, then I
1696              believe we'd see this elsewhere.  */
1697           /* If the symbol is weak, we do need to clear the value.
1698              Otherwise, the PLT entry would provide a definition for
1699              the symbol even if the symbol wasn't defined anywhere,
1700              and so the symbol would never be NULL.  */
1701           if (!h->ref_regular_nonweak)
1702             sym->st_value = 0;
1703         }
1704     }
1705
1706   /* For an ordinary program, we emit .got relocs only for symbols that
1707      are in the dynamic-symbols table and are either defined by the
1708      program or are undefined weak symbols, or are function symbols
1709      where we do not output a PLT: the PLT reloc was output above and all
1710      references to the function symbol are redirected to the PLT.  */
1711   if (h->got.offset != (bfd_vma) -1
1712       && (info->shared
1713           || (h->dynindx != -1
1714               && h->plt.offset == (bfd_vma) -1
1715               && !h->def_regular
1716               && h->root.type != bfd_link_hash_undefweak)))
1717     {
1718       asection *sgot;
1719       asection *srela;
1720       Elf_Internal_Rela rela;
1721       bfd_byte *loc;
1722       bfd_byte *where;
1723
1724       /* This symbol has an entry in the global offset table.  Set it up.  */
1725
1726       sgot = bfd_get_section_by_name (dynobj, ".got");
1727       srela = bfd_get_section_by_name (dynobj, ".rela.got");
1728       BFD_ASSERT (sgot != NULL && srela != NULL);
1729
1730       rela.r_offset = (sgot->output_section->vma
1731                        + sgot->output_offset
1732                        + (h->got.offset &~ (bfd_vma) 1));
1733
1734       /* If this is a static link, or it is a -Bsymbolic link and the
1735          symbol is defined locally or was forced to be local because
1736          of a version file, we just want to emit a RELATIVE reloc.
1737          The entry in the global offset table will already have been
1738          initialized in the relocate_section function.  */
1739       where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
1740       if (! elf_hash_table (info)->dynamic_sections_created
1741           || (info->shared
1742               && (info->symbolic || h->dynindx == -1)
1743               && h->def_regular))
1744         {
1745           rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1746           rela.r_addend = bfd_get_signed_32 (output_bfd, where);
1747         }
1748       else
1749         {
1750           bfd_put_32 (output_bfd, (bfd_vma) 0, where);
1751           rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
1752           rela.r_addend = 0;
1753         }
1754
1755       loc = srela->contents;
1756       loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
1757       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1758     }
1759
1760   if (h->needs_copy)
1761     {
1762       asection *s;
1763       Elf_Internal_Rela rela;
1764       bfd_byte *loc;
1765
1766       /* This symbol needs a copy reloc.  Set it up.  */
1767
1768       BFD_ASSERT (h->dynindx != -1
1769                   && (h->root.type == bfd_link_hash_defined
1770                       || h->root.type == bfd_link_hash_defweak));
1771
1772       s = bfd_get_section_by_name (h->root.u.def.section->owner,
1773                                    ".rela.bss");
1774       BFD_ASSERT (s != NULL);
1775
1776       rela.r_offset = (h->root.u.def.value
1777                        + h->root.u.def.section->output_section->vma
1778                        + h->root.u.def.section->output_offset);
1779       rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
1780       rela.r_addend = 0;
1781       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
1782       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
1783     }
1784
1785   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
1786   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1787       || h == elf_hash_table (info)->hgot)
1788     sym->st_shndx = SHN_ABS;
1789
1790   return TRUE;
1791 }
1792 \f
1793 /* Finish up the dynamic sections.  */
1794
1795 static bfd_boolean
1796 elf_cris_finish_dynamic_sections (output_bfd, info)
1797      bfd *output_bfd;
1798      struct bfd_link_info *info;
1799 {
1800   bfd *dynobj;
1801   asection *sgot;
1802   asection *sdyn;
1803
1804   dynobj = elf_hash_table (info)->dynobj;
1805
1806   sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1807   BFD_ASSERT (sgot != NULL);
1808   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1809
1810   if (elf_hash_table (info)->dynamic_sections_created)
1811     {
1812       asection *splt;
1813       Elf32_External_Dyn *dyncon, *dynconend;
1814
1815       splt = bfd_get_section_by_name (dynobj, ".plt");
1816       BFD_ASSERT (splt != NULL && sdyn != NULL);
1817
1818       dyncon = (Elf32_External_Dyn *) sdyn->contents;
1819       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
1820       for (; dyncon < dynconend; dyncon++)
1821         {
1822           Elf_Internal_Dyn dyn;
1823           asection *s;
1824
1825           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
1826
1827           switch (dyn.d_tag)
1828             {
1829             default:
1830               break;
1831
1832             case DT_PLTGOT:
1833               s = bfd_get_section_by_name (output_bfd, ".got");
1834               BFD_ASSERT (s != NULL);
1835               dyn.d_un.d_ptr = s->vma;
1836               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1837               break;
1838
1839             case DT_JMPREL:
1840               /* Yes, we *can* have a .plt and no .plt.rela, for instance
1841                  if all symbols are found in the .got (not .got.plt).  */
1842               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1843               dyn.d_un.d_ptr = s != NULL ? s->vma : 0;
1844               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1845               break;
1846
1847             case DT_PLTRELSZ:
1848               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1849               if (s == NULL)
1850                 dyn.d_un.d_val = 0;
1851               else
1852                 dyn.d_un.d_val = s->size;
1853               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1854               break;
1855
1856             case DT_RELASZ:
1857               /* The procedure linkage table relocs (DT_JMPREL) should
1858                  not be included in the overall relocs (DT_RELA).
1859                  Therefore, we override the DT_RELASZ entry here to
1860                  make it not include the JMPREL relocs.  Since the
1861                  linker script arranges for .rela.plt to follow all
1862                  other relocation sections, we don't have to worry
1863                  about changing the DT_RELA entry.  */
1864               s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1865               if (s != NULL)
1866                 dyn.d_un.d_val -= s->size;
1867               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
1868               break;
1869             }
1870         }
1871
1872       /* Fill in the first entry in the procedure linkage table.  */
1873       if (splt->size > 0)
1874         {
1875           if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
1876             {
1877               if (info->shared)
1878                 memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
1879                         PLT_ENTRY_SIZE_V32);
1880               else
1881                 {
1882                   memcpy (splt->contents, elf_cris_plt0_entry_v32,
1883                           PLT_ENTRY_SIZE_V32);
1884                   bfd_put_32 (output_bfd,
1885                               sgot->output_section->vma
1886                               + sgot->output_offset + 4,
1887                               splt->contents + 4);
1888
1889                   elf_section_data (splt->output_section)->this_hdr.sh_entsize
1890                     = PLT_ENTRY_SIZE_V32;
1891                 }
1892             }
1893           else
1894             {
1895               if (info->shared)
1896                 memcpy (splt->contents, elf_cris_pic_plt0_entry,
1897                         PLT_ENTRY_SIZE);
1898               else
1899                 {
1900                   memcpy (splt->contents, elf_cris_plt0_entry,
1901                           PLT_ENTRY_SIZE);
1902                   bfd_put_32 (output_bfd,
1903                               sgot->output_section->vma
1904                               + sgot->output_offset + 4,
1905                               splt->contents + 6);
1906                   bfd_put_32 (output_bfd,
1907                               sgot->output_section->vma
1908                               + sgot->output_offset + 8,
1909                               splt->contents + 14);
1910
1911                   elf_section_data (splt->output_section)->this_hdr.sh_entsize
1912                     = PLT_ENTRY_SIZE;
1913                 }
1914             }
1915         }
1916     }
1917
1918   /* Fill in the first three entries in the global offset table.  */
1919   if (sgot->size > 0)
1920     {
1921       if (sdyn == NULL)
1922         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
1923       else
1924         bfd_put_32 (output_bfd,
1925                     sdyn->output_section->vma + sdyn->output_offset,
1926                     sgot->contents);
1927       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
1928       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
1929     }
1930
1931   elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
1932
1933   return TRUE;
1934 }
1935 \f
1936 /* Return the section that should be marked against GC for a given
1937    relocation.  */
1938
1939 static asection *
1940 cris_elf_gc_mark_hook (asection *sec,
1941                        struct bfd_link_info *info,
1942                        Elf_Internal_Rela *rel,
1943                        struct elf_link_hash_entry *h,
1944                        Elf_Internal_Sym *sym)
1945 {
1946   if (h != NULL)
1947     switch (ELF32_R_TYPE (rel->r_info))
1948       {
1949       case R_CRIS_GNU_VTINHERIT:
1950       case R_CRIS_GNU_VTENTRY:
1951         return NULL;
1952       }
1953
1954   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1955 }
1956
1957 /* Update the got entry reference counts for the section being removed.  */
1958
1959 static bfd_boolean
1960 cris_elf_gc_sweep_hook (bfd *abfd,
1961                         struct bfd_link_info *info,
1962                         asection *sec,
1963                         const Elf_Internal_Rela *relocs)
1964 {
1965   Elf_Internal_Shdr *symtab_hdr;
1966   struct elf_link_hash_entry **sym_hashes;
1967   bfd_signed_vma *local_got_refcounts;
1968   const Elf_Internal_Rela *rel, *relend;
1969   bfd *dynobj;
1970   asection *sgot;
1971   asection *srelgot;
1972
1973   dynobj = elf_hash_table (info)->dynobj;
1974   if (dynobj == NULL)
1975     return TRUE;
1976
1977   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1978   sym_hashes = elf_sym_hashes (abfd);
1979   local_got_refcounts = elf_local_got_refcounts (abfd);
1980
1981   sgot = bfd_get_section_by_name (dynobj, ".got");
1982   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1983
1984   relend = relocs + sec->reloc_count;
1985   for (rel = relocs; rel < relend; rel++)
1986     {
1987       unsigned long r_symndx;
1988       struct elf_link_hash_entry *h = NULL;
1989
1990       r_symndx = ELF32_R_SYM (rel->r_info);
1991       if (r_symndx >= symtab_hdr->sh_info)
1992         {
1993           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1994           while (h->root.type == bfd_link_hash_indirect
1995                  || h->root.type == bfd_link_hash_warning)
1996             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1997         }
1998
1999       switch (ELF32_R_TYPE (rel->r_info))
2000         {
2001         case R_CRIS_16_GOT:
2002         case R_CRIS_32_GOT:
2003           if (h != NULL)
2004             {
2005               if (h->got.refcount > 0)
2006                 {
2007                   --h->got.refcount;
2008                   if (h->got.refcount == 0)
2009                     {
2010                       /* We don't need the .got entry any more.  */
2011                       sgot->size -= 4;
2012                       srelgot->size -= sizeof (Elf32_External_Rela);
2013                     }
2014                 }
2015               break;
2016             }
2017
2018         local_got_reloc:
2019           if (local_got_refcounts != NULL)
2020             {
2021               if (local_got_refcounts[r_symndx] > 0)
2022                 {
2023                   --local_got_refcounts[r_symndx];
2024                   if (local_got_refcounts[r_symndx] == 0)
2025                     {
2026                       /* We don't need the .got entry any more.  */
2027                       sgot->size -= 4;
2028                       if (info->shared)
2029                         srelgot->size -= sizeof (Elf32_External_Rela);
2030                     }
2031                 }
2032             }
2033           break;
2034
2035         case R_CRIS_16_GOTPLT:
2036         case R_CRIS_32_GOTPLT:
2037           /* For local symbols, treat these like GOT relocs.  */
2038           if (h == NULL)
2039             goto local_got_reloc;
2040           /* Fall through.  */
2041
2042         case R_CRIS_32_PLT_GOTREL:
2043           /* FIXME: We don't garbage-collect away the .got section.  */
2044           if (local_got_refcounts != NULL)
2045             local_got_refcounts[-1]--;
2046           /* Fall through.  */
2047
2048         case R_CRIS_8_PCREL:
2049         case R_CRIS_16_PCREL:
2050         case R_CRIS_32_PCREL:
2051         case R_CRIS_32_PLT_PCREL:
2052           if (h != NULL)
2053             {
2054               if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2055                   && h->plt.refcount > 0)
2056                 --h->plt.refcount;
2057             }
2058           break;
2059
2060         default:
2061           break;
2062         }
2063     }
2064
2065   return TRUE;
2066 }
2067
2068 /* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
2069    entry but we found we will not create any.  Called when we find we will
2070    not have any PLT for this symbol, by for example
2071    elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
2072    or elf_cris_size_dynamic_sections if no dynamic sections will be
2073    created (we're only linking static objects).  */
2074
2075 static bfd_boolean
2076 elf_cris_adjust_gotplt_to_got (h, p)
2077      struct elf_cris_link_hash_entry *h;
2078      PTR p;
2079 {
2080   struct bfd_link_info *info = (struct bfd_link_info *) p;
2081
2082   if (h->root.root.type == bfd_link_hash_warning)
2083     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
2084
2085   /* If nobody wanted a GOTPLT with this symbol, we're done.  */
2086   if (h->gotplt_refcount <= 0)
2087     return TRUE;
2088
2089   if (h->root.got.refcount > 0)
2090     {
2091       /* There's a GOT entry for this symbol.  Just adjust the refcount.
2092          Probably not necessary at this stage, but keeping it accurate
2093          helps avoiding surprises later.  */
2094       h->root.got.refcount += h->gotplt_refcount;
2095       h->gotplt_refcount = 0;
2096     }
2097   else
2098     {
2099       /* No GOT entry for this symbol.  We need to create one.  */
2100       bfd *dynobj = elf_hash_table (info)->dynobj;
2101       asection *sgot;
2102       asection *srelgot;
2103
2104       BFD_ASSERT (dynobj != NULL);
2105       sgot = bfd_get_section_by_name (dynobj, ".got");
2106       srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2107
2108       /* Put an accurate refcount there.  */
2109       h->root.got.refcount = h->gotplt_refcount;
2110
2111       h->gotplt_refcount = 0;
2112
2113       /* We always have a .got and a .rela.got section if there were
2114          GOTPLT relocs in input.  */
2115       BFD_ASSERT (sgot != NULL && srelgot != NULL);
2116
2117       /* Allocate space in the .got section.  */
2118       sgot->size += 4;
2119
2120       /* Allocate relocation space.  */
2121       srelgot->size += sizeof (Elf32_External_Rela);
2122     }
2123
2124   return TRUE;
2125 }
2126
2127 /* Try to fold PLT entries with GOT entries.  There are two cases when we
2128    want to do this:
2129
2130    - When all PLT references are GOTPLT references, and there are GOT
2131      references, and this is not the executable.  We don't have to
2132      generate a PLT at all.
2133
2134    - When there are both (ordinary) PLT references and GOT references,
2135      and this isn't the executable.
2136      We want to make the PLT reference use the ordinary GOT entry rather
2137      than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
2138      since the GOT entry will have to be resolved at startup anyway.
2139
2140    Though the latter case is handled when room for the PLT is allocated,
2141    not here.
2142
2143    By folding into the GOT, we may need a round-trip to a PLT in the
2144    executable for calls, a loss in performance.  Still, losing a
2145    reloc is a win in size and at least in start-up time.
2146
2147    Note that this function is called before symbols are forced local by
2148    version scripts.  The differing cases are handled by
2149    elf_cris_hide_symbol.  */
2150
2151 static bfd_boolean
2152 elf_cris_try_fold_plt_to_got (h, p)
2153      struct elf_cris_link_hash_entry *h;
2154      PTR p;
2155 {
2156   struct bfd_link_info *info = (struct bfd_link_info *) p;
2157
2158   /* If there are no GOT references for this symbol, we can't fold any
2159      other reference so there's nothing to do.  Likewise if there are no
2160      PLT references; GOTPLT references included.  */
2161   if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
2162     return TRUE;
2163
2164   /* GOTPLT relocs are supposed to be included into the PLT refcount.  */
2165   BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
2166
2167   if (h->gotplt_refcount == h->root.plt.refcount)
2168     {
2169       /* The only PLT references are GOTPLT references, and there are GOT
2170          references.  Convert PLT to GOT references.  */
2171       if (! elf_cris_adjust_gotplt_to_got (h, info))
2172         return FALSE;
2173
2174       /* Clear the PLT references, so no PLT will be created.  */
2175       h->root.plt.offset = (bfd_vma) -1;
2176     }
2177
2178   return TRUE;
2179 }
2180
2181 /* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
2182    to use a GOT entry (and create one) rather than requiring a GOTPLT
2183    entry.  */
2184
2185 static void
2186 elf_cris_hide_symbol (info, h, force_local)
2187      struct bfd_link_info *info;
2188      struct elf_link_hash_entry *h;
2189      bfd_boolean force_local;
2190 {
2191   elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
2192
2193   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2194 }
2195
2196 /* Adjust a symbol defined by a dynamic object and referenced by a
2197    regular object.  The current definition is in some section of the
2198    dynamic object, but we're not including those sections.  We have to
2199    change the definition to something the rest of the link can
2200    understand.  */
2201
2202 static bfd_boolean
2203 elf_cris_adjust_dynamic_symbol (info, h)
2204      struct bfd_link_info *info;
2205      struct elf_link_hash_entry *h;
2206 {
2207   bfd *dynobj;
2208   asection *s;
2209   unsigned int power_of_two;
2210   bfd_size_type plt_entry_size;
2211
2212   dynobj = elf_hash_table (info)->dynobj;
2213
2214   /* Make sure we know what is going on here.  */
2215   BFD_ASSERT (dynobj != NULL
2216               && (h->needs_plt
2217                   || h->u.weakdef != NULL
2218                   || (h->def_dynamic
2219                       && h->ref_regular
2220                       && !h->def_regular)));
2221
2222   plt_entry_size
2223     = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
2224        ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2225
2226   /* If this is a function, put it in the procedure linkage table.  We
2227      will fill in the contents of the procedure linkage table later,
2228      when we know the address of the .got section.  */
2229   if (h->type == STT_FUNC
2230       || h->needs_plt)
2231     {
2232       /* If we link a program (not a DSO), we'll get rid of unnecessary
2233          PLT entries; we point to the actual symbols -- even for pic
2234          relocs, because a program built with -fpic should have the same
2235          result as one built without -fpic, specifically considering weak
2236          symbols.
2237          FIXME: m68k and i386 differ here, for unclear reasons.  */
2238       if (! info->shared
2239           && !h->def_dynamic)
2240         {
2241           /* This case can occur if we saw a PLT reloc in an input file,
2242              but the symbol was not defined by a dynamic object.  In such
2243              a case, we don't actually need to build a procedure linkage
2244              table, and we can just do an absolute or PC reloc instead, or
2245              change a .got.plt index to a .got index for GOTPLT relocs.  */
2246           BFD_ASSERT (h->needs_plt);
2247           h->needs_plt = 0;
2248           h->plt.offset = (bfd_vma) -1;
2249           return
2250             elf_cris_adjust_gotplt_to_got ((struct
2251                                             elf_cris_link_hash_entry *) h,
2252                                            info);
2253         }
2254
2255       /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
2256          where a pointer-equivalent symbol was unimportant (i.e. more
2257          like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
2258          of the PLT.  We can't for the executable, because the GOT
2259          entries will point to the PLT there (and be constant).  */
2260       if (info->shared
2261           && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
2262                                             h, info))
2263         return FALSE;
2264
2265       /* GC or folding may have rendered this entry unused.  */
2266       if (h->plt.refcount <= 0)
2267         {
2268           h->needs_plt = 0;
2269           h->plt.offset = (bfd_vma) -1;
2270           return TRUE;
2271         }
2272
2273       /* Make sure this symbol is output as a dynamic symbol.  */
2274       if (h->dynindx == -1)
2275         {
2276           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2277             return FALSE;
2278         }
2279
2280       s = bfd_get_section_by_name (dynobj, ".plt");
2281       BFD_ASSERT (s != NULL);
2282
2283       /* If this is the first .plt entry, make room for the special
2284          first entry.  */
2285       if (s->size == 0)
2286         s->size += plt_entry_size;
2287
2288       /* If this symbol is not defined in a regular file, and we are
2289          not generating a shared library, then set the symbol to this
2290          location in the .plt.  */
2291       if (!info->shared
2292           && !h->def_regular)
2293         {
2294           h->root.u.def.section = s;
2295           h->root.u.def.value = s->size;
2296         }
2297
2298       /* If there's already a GOT entry, use that, not a .got.plt.  A
2299          GOT field still has a reference count when we get here; it's
2300          not yet changed to an offset.  We can't do this for an
2301          executable, because then the reloc associated with the PLT
2302          would get a non-PLT reloc pointing to the PLT.  FIXME: Move
2303          this to elf_cris_try_fold_plt_to_got.  */
2304       if (info->shared && h->got.refcount > 0)
2305         {
2306           h->got.refcount += h->plt.refcount;
2307
2308           /* Mark the PLT offset to use the GOT entry by setting the low
2309              bit in the plt offset; it is always a multiple of
2310              plt_entry_size (which is at least a multiple of 2).  */
2311           BFD_ASSERT ((s->size % plt_entry_size) == 0);
2312
2313           /* Change the PLT refcount to an offset.  */
2314           h->plt.offset = s->size;
2315
2316           /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2317              that the got entry should be used instead.  */
2318           BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2319                        h)->gotplt_offset == 0);
2320
2321           /* Make room for this entry.  */
2322           s->size += plt_entry_size;
2323
2324           return TRUE;
2325         }
2326
2327       /* No GOT reference for this symbol; prepare for an ordinary PLT.  */
2328       h->plt.offset = s->size;
2329
2330       /* Make room for this entry.  */
2331       s->size += plt_entry_size;
2332
2333       /* We also need to make an entry in the .got.plt section, which
2334          will be placed in the .got section by the linker script.  */
2335       ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2336         = elf_cris_hash_table (info)->next_gotplt_entry;
2337       elf_cris_hash_table (info)->next_gotplt_entry += 4;
2338
2339       s = bfd_get_section_by_name (dynobj, ".got.plt");
2340       BFD_ASSERT (s != NULL);
2341       s->size += 4;
2342
2343       /* We also need to make an entry in the .rela.plt section.  */
2344
2345       s = bfd_get_section_by_name (dynobj, ".rela.plt");
2346       BFD_ASSERT (s != NULL);
2347       s->size += sizeof (Elf32_External_Rela);
2348
2349       return TRUE;
2350     }
2351
2352   /* Reinitialize the plt offset now that it is not used as a reference
2353      count any more.  */
2354   h->plt.offset = (bfd_vma) -1;
2355
2356   /* If this is a weak symbol, and there is a real definition, the
2357      processor independent code will have arranged for us to see the
2358      real definition first, and we can just use the same value.  */
2359   if (h->u.weakdef != NULL)
2360     {
2361       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2362                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
2363       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2364       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2365       return TRUE;
2366     }
2367
2368   /* This is a reference to a symbol defined by a dynamic object which
2369      is not a function.  */
2370
2371   /* If we are creating a shared library, we must presume that the
2372      only references to the symbol are via the global offset table.
2373      For such cases we need not do anything here; the relocations will
2374      be handled correctly by relocate_section.  */
2375   if (info->shared)
2376     return TRUE;
2377
2378   /* If there are no references to this symbol that do not use the
2379      GOT, we don't need to generate a copy reloc.  */
2380   if (!h->non_got_ref)
2381     return TRUE;
2382
2383   if (h->size == 0)
2384     {
2385       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2386                              h->root.root.string);
2387       return TRUE;
2388     }
2389
2390   /* We must allocate the symbol in our .dynbss section, which will
2391      become part of the .bss section of the executable.  There will be
2392      an entry for this symbol in the .dynsym section.  The dynamic
2393      object will contain position independent code, so all references
2394      from the dynamic object to this symbol will go through the global
2395      offset table.  The dynamic linker will use the .dynsym entry to
2396      determine the address it must put in the global offset table, so
2397      both the dynamic object and the regular object will refer to the
2398      same memory location for the variable.  */
2399
2400   s = bfd_get_section_by_name (dynobj, ".dynbss");
2401   BFD_ASSERT (s != NULL);
2402
2403   /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2404      copy the initial value out of the dynamic object and into the
2405      runtime process image.  We need to remember the offset into the
2406      .rela.bss section we are going to use.  */
2407   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2408     {
2409       asection *srel;
2410
2411       srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2412       BFD_ASSERT (srel != NULL);
2413       srel->size += sizeof (Elf32_External_Rela);
2414       h->needs_copy = 1;
2415     }
2416
2417   /* Historic precedent: m68k and i386 allow max 8-byte alignment for the
2418      thing to copy; so do we.  */
2419
2420   /* We need to figure out the alignment required for this symbol.  I
2421      have no idea how ELF linkers handle this.  */
2422   power_of_two = bfd_log2 (h->size);
2423   if (power_of_two > 3)
2424     power_of_two = 3;
2425
2426   /* Apply the required alignment.  */
2427   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
2428   if (power_of_two > bfd_get_section_alignment (dynobj, s))
2429     {
2430       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
2431         return FALSE;
2432     }
2433
2434   /* Define the symbol as being at this point in the section.  */
2435   h->root.u.def.section = s;
2436   h->root.u.def.value = s->size;
2437
2438   /* Increment the section size to make room for the symbol.  */
2439   s->size += h->size;
2440
2441   return TRUE;
2442 }
2443
2444 /* Look through the relocs for a section during the first phase.  */
2445
2446 static bfd_boolean
2447 cris_elf_check_relocs (abfd, info, sec, relocs)
2448      bfd *abfd;
2449      struct bfd_link_info *info;
2450      asection *sec;
2451      const Elf_Internal_Rela *relocs;
2452 {
2453   bfd *dynobj;
2454   Elf_Internal_Shdr *symtab_hdr;
2455   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2456   bfd_signed_vma *local_got_refcounts;
2457   const Elf_Internal_Rela *rel;
2458   const Elf_Internal_Rela *rel_end;
2459   asection *sgot;
2460   asection *srelgot;
2461   asection *sreloc;
2462
2463   if (info->relocatable)
2464     return TRUE;
2465
2466   dynobj = elf_hash_table (info)->dynobj;
2467   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2468   sym_hashes = elf_sym_hashes (abfd);
2469   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2470   local_got_refcounts = elf_local_got_refcounts (abfd);
2471
2472   sgot = NULL;
2473   srelgot = NULL;
2474   sreloc = NULL;
2475
2476   if (!elf_bad_symtab (abfd))
2477     sym_hashes_end -= symtab_hdr->sh_info;
2478
2479   rel_end = relocs + sec->reloc_count;
2480   for (rel = relocs; rel < rel_end; rel++)
2481     {
2482       struct elf_link_hash_entry *h;
2483       unsigned long r_symndx;
2484       enum elf_cris_reloc_type r_type;
2485
2486       r_symndx = ELF32_R_SYM (rel->r_info);
2487       if (r_symndx < symtab_hdr->sh_info)
2488         h = NULL;
2489       else
2490         {
2491           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2492           while (h->root.type == bfd_link_hash_indirect
2493                  || h->root.type == bfd_link_hash_warning)
2494             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2495         }
2496
2497       r_type = ELF32_R_TYPE (rel->r_info);
2498
2499       /* Some relocs require linker-created sections; we need to hang them
2500          on the first input bfd we found that contained dynamic relocs.  */
2501       switch (r_type)
2502         {
2503         case R_CRIS_16_GOT:
2504         case R_CRIS_32_GOT:
2505         case R_CRIS_32_GOTREL:
2506         case R_CRIS_32_PLT_GOTREL:
2507         case R_CRIS_32_PLT_PCREL:
2508         case R_CRIS_16_GOTPLT:
2509         case R_CRIS_32_GOTPLT:
2510           if (dynobj == NULL)
2511             {
2512               elf_hash_table (info)->dynobj = dynobj = abfd;
2513
2514               /* We could handle this if we can get a handle on the
2515                  output bfd in elf_cris_adjust_dynamic_symbol.  Failing
2516                  that, we must insist on dynobj being a specific mach.  */
2517               if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
2518                 {
2519                   (*_bfd_error_handler)
2520                     (_("%B, section %A:\n  v10/v32 compatible object %s"
2521                        " must not contain a PIC relocation"),
2522                      abfd, sec);
2523                   return FALSE;
2524                 }
2525
2526               /* Create the .got section, so we can assume it's always
2527                  present whenever there's a dynobj.  */
2528               if (!_bfd_elf_create_got_section (dynobj, info))
2529                 return FALSE;
2530             }
2531           break;
2532
2533         default:
2534           break;
2535         }
2536
2537       /* Some relocs require a global offset table (but perhaps not a
2538          specific GOT entry).  */
2539       switch (r_type)
2540         {
2541           /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT
2542              entry only for local symbols.  Unfortunately, we don't know
2543              until later on if there's a version script that forces the
2544              symbol local.  We must have the .rela.got section in place
2545              before we know if the symbol looks global now, so we need
2546              to treat the reloc just like for R_CRIS_16_GOT and
2547              R_CRIS_32_GOT.  */
2548         case R_CRIS_16_GOTPLT:
2549         case R_CRIS_32_GOTPLT:
2550         case R_CRIS_16_GOT:
2551         case R_CRIS_32_GOT:
2552           if (srelgot == NULL
2553               && (h != NULL || info->shared))
2554             {
2555               srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2556               if (srelgot == NULL)
2557                 {
2558                   srelgot = bfd_make_section_with_flags (dynobj,
2559                                                          ".rela.got",
2560                                                          (SEC_ALLOC
2561                                                           | SEC_LOAD
2562                                                           | SEC_HAS_CONTENTS
2563                                                           | SEC_IN_MEMORY
2564                                                           | SEC_LINKER_CREATED
2565                                                           | SEC_READONLY));
2566                   if (srelgot == NULL
2567                       || !bfd_set_section_alignment (dynobj, srelgot, 2))
2568                     return FALSE;
2569                 }
2570             }
2571           /* Fall through.  */
2572
2573         case R_CRIS_32_GOTREL:
2574         case R_CRIS_32_PLT_GOTREL:
2575           if (sgot == NULL)
2576             sgot = bfd_get_section_by_name (dynobj, ".got");
2577
2578           if (local_got_refcounts == NULL)
2579             {
2580               bfd_size_type amt;
2581
2582               /* We use index local_got_refcounts[-1] to count all
2583                  GOT-relative relocations that do not have explicit
2584                  GOT entries.  */
2585               amt = symtab_hdr->sh_info + 1;
2586               amt *= sizeof (bfd_signed_vma);
2587               local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
2588               if (local_got_refcounts == NULL)
2589                 return FALSE;
2590
2591               local_got_refcounts++;
2592               elf_local_got_refcounts (abfd) = local_got_refcounts;
2593             }
2594           break;
2595
2596         default:
2597           break;
2598         }
2599
2600       switch (r_type)
2601         {
2602         case R_CRIS_16_GOTPLT:
2603         case R_CRIS_32_GOTPLT:
2604           /* Mark that we need a GOT entry if the PLT entry (and its GOT
2605              entry) is eliminated.  We can only do this for a non-local
2606              symbol.  */
2607           if (h != NULL)
2608             {
2609               ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++;
2610               goto handle_gotplt_reloc;
2611             }
2612           /* If h is NULL then this is a local symbol, and we must make a
2613              GOT entry for it, so handle it like a GOT reloc.  */
2614           /* Fall through.  */
2615
2616         case R_CRIS_16_GOT:
2617         case R_CRIS_32_GOT:
2618           /* This symbol requires a global offset table entry.  */
2619           if (h != NULL)
2620             {
2621               if (h->got.refcount == 0)
2622                 {
2623                   /* Make sure this symbol is output as a dynamic symbol.  */
2624                   if (h->dynindx == -1)
2625                     {
2626                       if (!bfd_elf_link_record_dynamic_symbol (info, h))
2627                         return FALSE;
2628                     }
2629
2630                   /* Allocate space in the .got section.  */
2631                   sgot->size += 4;
2632                   /* Allocate relocation space.  */
2633                   srelgot->size += sizeof (Elf32_External_Rela);
2634                 }
2635               h->got.refcount++;
2636             }
2637           else
2638             {
2639               /* This is a global offset table entry for a local symbol.  */
2640               if (local_got_refcounts[r_symndx] == 0)
2641                 {
2642                   sgot->size += 4;
2643                   if (info->shared)
2644                     {
2645                       /* If we are generating a shared object, we need to
2646                          output a R_CRIS_RELATIVE reloc so that the dynamic
2647                          linker can adjust this GOT entry.  */
2648                       srelgot->size += sizeof (Elf32_External_Rela);
2649                     }
2650                 }
2651               local_got_refcounts[r_symndx]++;
2652             }
2653           break;
2654
2655         case R_CRIS_32_GOTREL:
2656           /* This reference requires a global offset table.
2657              FIXME: The actual refcount isn't used currently; the .got
2658              section can't be removed if there were any references in the
2659              input.  */
2660           local_got_refcounts[-1]++;
2661           break;
2662
2663         handle_gotplt_reloc:
2664
2665         case R_CRIS_32_PLT_GOTREL:
2666           /* This reference requires a global offset table.  */
2667           local_got_refcounts[-1]++;
2668           /* Fall through.  */
2669
2670         case R_CRIS_32_PLT_PCREL:
2671           /* This symbol requires a procedure linkage table entry.  We
2672              actually build the entry in adjust_dynamic_symbol,
2673              because this might be a case of linking PIC code which is
2674              never referenced by a dynamic object, in which case we
2675              don't need to generate a procedure linkage table entry
2676              after all.  */
2677
2678           /* Beware: if we'd check for visibility of the symbol here
2679              (and not marking the need for a PLT when non-visible), we'd
2680              get into trouble with keeping handling consistent with
2681              regards to relocs found before definition and GOTPLT
2682              handling.  Eliminable PLT entries will be dealt with later
2683              anyway.  */
2684           if (h == NULL)
2685             continue;
2686
2687           h->needs_plt = 1;
2688           h->plt.refcount++;
2689           break;
2690
2691         case R_CRIS_8:
2692         case R_CRIS_16:
2693         case R_CRIS_32:
2694           /* Let's help debug shared library creation.  Any of these
2695              relocs can be used in shared libs, but pages containing them
2696              cannot be shared.  Don't warn for sections we don't care
2697              about, such as debug sections or non-constant sections.  We
2698              can't help tables of (global) function pointers, for example,
2699              though they must be emitted in a data section to avoid having
2700              impure text sections.  */
2701           if (info->shared
2702               && (sec->flags & SEC_ALLOC) != 0
2703               && (sec->flags & SEC_READONLY) != 0)
2704             {
2705               /* FIXME: How do we make this optionally a warning only?  */
2706               (*_bfd_error_handler)
2707                 (_("%B, section %A:\n  relocation %s should not"
2708                    " be used in a shared object; recompile with -fPIC"),
2709                  abfd,
2710                  sec,
2711                  cris_elf_howto_table[r_type].name);
2712             }
2713           /* Fall through.  */
2714
2715         case R_CRIS_8_PCREL:
2716         case R_CRIS_16_PCREL:
2717         case R_CRIS_32_PCREL:
2718           if (h != NULL)
2719             {
2720               h->non_got_ref = 1;
2721
2722               /* Make sure a plt entry is created for this symbol if it
2723                  turns out to be a function defined by a dynamic object.  */
2724               if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2725                 h->plt.refcount++;
2726             }
2727
2728           /* If we are creating a shared library and this is not a local
2729              symbol, we need to copy the reloc into the shared library.
2730              However when linking with -Bsymbolic and this is a global
2731              symbol which is defined in an object we are including in the
2732              link (i.e., DEF_REGULAR is set), then we can resolve the
2733              reloc directly.  At this point we have not seen all the input
2734              files, so it is possible that DEF_REGULAR is not set now but
2735              will be set later (it is never cleared).  In case of a weak
2736              definition, DEF_REGULAR may be cleared later by a strong
2737              definition in a shared library.  We account for that
2738              possibility below by storing information in the relocs_copied
2739              field of the hash table entry.  A similar situation occurs
2740              when creating shared libraries and symbol visibility changes
2741              render the symbol local.  */
2742
2743           /* No need to do anything if we're not creating a shared object.  */
2744           if (! info->shared)
2745             break;
2746
2747           /* We don't need to handle relocs into sections not going into
2748              the "real" output.  */
2749           if ((sec->flags & SEC_ALLOC) == 0)
2750             break;
2751
2752           /* We can only eliminate PC-relative relocs.  */
2753           if (r_type == R_CRIS_8_PCREL
2754               || r_type == R_CRIS_16_PCREL
2755               || r_type == R_CRIS_32_PCREL)
2756             {
2757               /* If the symbol is local, then we can eliminate the reloc.  */
2758               if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2759                 break;
2760
2761               /* If this is with -Bsymbolic and the symbol isn't weak, and
2762                  is defined by an ordinary object (the ones we include in
2763                  this shared library) then we can also eliminate the
2764                  reloc.  See comment above for more eliminable cases which
2765                  we can't identify at this time.  */
2766               if (info->symbolic
2767                   && h->root.type != bfd_link_hash_defweak
2768                   && h->def_regular)
2769                 break;
2770
2771               if ((sec->flags & SEC_READONLY) != 0)
2772                 {
2773                   /* FIXME: How do we make this optionally a warning only?  */
2774                   (*_bfd_error_handler)
2775                     (_("%B, section %A:\n  relocation %s should not be used"
2776                        " in a shared object; recompile with -fPIC"),
2777                      abfd,
2778                      sec,
2779                      cris_elf_howto_table[r_type].name);
2780                 }
2781             }
2782
2783           /* We create a reloc section in dynobj and make room for this
2784              reloc.  */
2785           if (sreloc == NULL)
2786             {
2787               const char *name;
2788
2789               name = (bfd_elf_string_from_elf_section
2790                       (abfd,
2791                        elf_elfheader (abfd)->e_shstrndx,
2792                        elf_section_data (sec)->rel_hdr.sh_name));
2793               if (name == NULL)
2794                 return FALSE;
2795
2796               BFD_ASSERT (CONST_STRNEQ (name, ".rela")
2797                           && strcmp (bfd_get_section_name (abfd, sec),
2798                                      name + 5) == 0);
2799
2800               sreloc = bfd_get_section_by_name (dynobj, name);
2801               if (sreloc == NULL)
2802                 {
2803                   sreloc = bfd_make_section_with_flags (dynobj, name,
2804                                                         (SEC_ALLOC
2805                                                          | SEC_LOAD
2806                                                          | SEC_HAS_CONTENTS
2807                                                          | SEC_IN_MEMORY
2808                                                          | SEC_LINKER_CREATED
2809                                                          | SEC_READONLY));
2810                   if (sreloc == NULL
2811                       || !bfd_set_section_alignment (dynobj, sreloc, 2))
2812                     return FALSE;
2813                 }
2814               if (sec->flags & SEC_READONLY)
2815                 info->flags |= DF_TEXTREL;
2816             }
2817
2818           sreloc->size += sizeof (Elf32_External_Rela);
2819
2820           /* If we are linking with -Bsymbolic, we count the number of PC
2821              relative relocations we have entered for this symbol, so that
2822              we can discard them again if the symbol is later defined by a
2823              regular object.  We know that h is really a pointer to an
2824              elf_cris_link_hash_entry.  */
2825           if ((r_type == R_CRIS_8_PCREL
2826                || r_type == R_CRIS_16_PCREL
2827                || r_type == R_CRIS_32_PCREL)
2828               && info->symbolic)
2829             {
2830               struct elf_cris_link_hash_entry *eh;
2831               struct elf_cris_pcrel_relocs_copied *p;
2832
2833               eh = (struct elf_cris_link_hash_entry *) h;
2834
2835               for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
2836                 if (p->section == sreloc)
2837                   break;
2838
2839               if (p == NULL)
2840                 {
2841                   p = ((struct elf_cris_pcrel_relocs_copied *)
2842                        bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
2843                   if (p == NULL)
2844                     return FALSE;
2845                   p->next = eh->pcrel_relocs_copied;
2846                   eh->pcrel_relocs_copied = p;
2847                   p->section = sreloc;
2848                   p->count = 0;
2849                 }
2850
2851               ++p->count;
2852             }
2853           break;
2854
2855         /* This relocation describes the C++ object vtable hierarchy.
2856            Reconstruct it for later use during GC.  */
2857         case R_CRIS_GNU_VTINHERIT:
2858           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2859             return FALSE;
2860           break;
2861
2862         /* This relocation describes which C++ vtable entries are actually
2863            used.  Record for later use during GC.  */
2864         case R_CRIS_GNU_VTENTRY:
2865           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2866             return FALSE;
2867           break;
2868
2869         default:
2870           /* Other relocs do not appear here.  */
2871           bfd_set_error (bfd_error_bad_value);
2872           return FALSE;
2873         }
2874     }
2875
2876   return TRUE;
2877 }
2878
2879 /* Set the sizes of the dynamic sections.  */
2880
2881 static bfd_boolean
2882 elf_cris_size_dynamic_sections (output_bfd, info)
2883      bfd *output_bfd ATTRIBUTE_UNUSED;
2884      struct bfd_link_info *info;
2885 {
2886   bfd *dynobj;
2887   asection *s;
2888   bfd_boolean plt;
2889   bfd_boolean relocs;
2890
2891   dynobj = elf_hash_table (info)->dynobj;
2892   BFD_ASSERT (dynobj != NULL);
2893
2894   if (elf_hash_table (info)->dynamic_sections_created)
2895     {
2896       /* Set the contents of the .interp section to the interpreter.  */
2897       if (info->executable)
2898         {
2899           s = bfd_get_section_by_name (dynobj, ".interp");
2900           BFD_ASSERT (s != NULL);
2901           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2902           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2903         }
2904     }
2905   else
2906     {
2907       /* Adjust all expected GOTPLT uses to use a GOT entry instead.  */
2908       elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2909                                    elf_cris_adjust_gotplt_to_got,
2910                                    (PTR) info);
2911
2912       /* We may have created entries in the .rela.got section.
2913          However, if we are not creating the dynamic sections, we will
2914          not actually use these entries.  Reset the size of .rela.got,
2915          which will cause it to get stripped from the output file
2916          below.  */
2917       s = bfd_get_section_by_name (dynobj, ".rela.got");
2918       if (s != NULL)
2919         s->size = 0;
2920     }
2921
2922   /* If this is a -Bsymbolic shared link, then we need to discard all PC
2923      relative relocs against symbols defined in a regular object.  We
2924      allocated space for them in the check_relocs routine, but we will not
2925      fill them in in the relocate_section routine.  We also discard space
2926      for relocs that have become for local symbols due to symbol
2927      visibility changes.  For programs, we discard space for relocs for
2928      symbols not referenced by any dynamic object.  */
2929   if (info->shared)
2930     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2931                                  elf_cris_discard_excess_dso_dynamics,
2932                                  (PTR) info);
2933   else
2934     elf_cris_link_hash_traverse (elf_cris_hash_table (info),
2935                                  elf_cris_discard_excess_program_dynamics,
2936                                  (PTR) info);
2937
2938   /* The check_relocs and adjust_dynamic_symbol entry points have
2939      determined the sizes of the various dynamic sections.  Allocate
2940      memory for them.  */
2941   plt = FALSE;
2942   relocs = FALSE;
2943   for (s = dynobj->sections; s != NULL; s = s->next)
2944     {
2945       const char *name;
2946
2947       if ((s->flags & SEC_LINKER_CREATED) == 0)
2948         continue;
2949
2950       /* It's OK to base decisions on the section name, because none
2951          of the dynobj section names depend upon the input files.  */
2952       name = bfd_get_section_name (dynobj, s);
2953
2954       if (strcmp (name, ".plt") == 0)
2955         {
2956           /* Remember whether there is a PLT.  */
2957           plt = s->size != 0;
2958         }
2959       else if (CONST_STRNEQ (name, ".rela"))
2960         {
2961           if (s->size != 0)
2962             {
2963               /* Remember whether there are any reloc sections other
2964                  than .rela.plt.  */
2965               if (strcmp (name, ".rela.plt") != 0)
2966                   relocs = TRUE;
2967
2968               /* We use the reloc_count field as a counter if we need
2969                  to copy relocs into the output file.  */
2970               s->reloc_count = 0;
2971             }
2972         }
2973       else if (! CONST_STRNEQ (name, ".got")
2974                && strcmp (name, ".dynbss") != 0)
2975         {
2976           /* It's not one of our sections, so don't allocate space.  */
2977           continue;
2978         }
2979
2980       if (s->size == 0)
2981         {
2982           /* If we don't need this section, strip it from the
2983              output file.  This is mostly to handle .rela.bss and
2984              .rela.plt.  We must create both sections in
2985              create_dynamic_sections, because they must be created
2986              before the linker maps input sections to output
2987              sections.  The linker does that before
2988              adjust_dynamic_symbol is called, and it is that
2989              function which decides whether anything needs to go
2990              into these sections.  */
2991           s->flags |= SEC_EXCLUDE;
2992           continue;
2993         }
2994
2995       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2996         continue;
2997
2998       /* Allocate memory for the section contents. We use bfd_zalloc here
2999          in case unused entries are not reclaimed before the section's
3000          contents are written out.  This should not happen, but this way
3001          if it does, we will not write out garbage.  For reloc sections,
3002          this will make entries have the type R_CRIS_NONE.  */
3003       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3004       if (s->contents == NULL)
3005         return FALSE;
3006     }
3007
3008   if (elf_hash_table (info)->dynamic_sections_created)
3009     {
3010       /* Add some entries to the .dynamic section.  We fill in the
3011          values later, in elf_cris_finish_dynamic_sections, but we
3012          must add the entries now so that we get the correct size for
3013          the .dynamic section.  The DT_DEBUG entry is filled in by the
3014          dynamic linker and used by the debugger.  */
3015 #define add_dynamic_entry(TAG, VAL) \
3016   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3017
3018       if (!info->shared)
3019         {
3020           if (!add_dynamic_entry (DT_DEBUG, 0))
3021             return FALSE;
3022         }
3023
3024       if (plt)
3025         {
3026           if (!add_dynamic_entry (DT_PLTGOT, 0)
3027               || !add_dynamic_entry (DT_PLTRELSZ, 0)
3028               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3029               || !add_dynamic_entry (DT_JMPREL, 0))
3030             return FALSE;
3031         }
3032
3033       if (relocs)
3034         {
3035           if (!add_dynamic_entry (DT_RELA, 0)
3036               || !add_dynamic_entry (DT_RELASZ, 0)
3037               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3038             return FALSE;
3039         }
3040
3041       if ((info->flags & DF_TEXTREL) != 0)
3042         {
3043           if (!add_dynamic_entry (DT_TEXTREL, 0))
3044             return FALSE;
3045           info->flags |= DF_TEXTREL;
3046         }
3047     }
3048 #undef add_dynamic_entry
3049
3050   return TRUE;
3051 }
3052
3053 /* This function is called via elf_cris_link_hash_traverse if we are
3054    creating a shared object.  In the -Bsymbolic case, it discards the
3055    space allocated to copy PC relative relocs against symbols which
3056    are defined in regular objects.  For the normal non-symbolic case,
3057    we also discard space for relocs that have become local due to
3058    symbol visibility changes.  We allocated space for them in the
3059    check_relocs routine, but we won't fill them in in the
3060    relocate_section routine.  */
3061
3062 static bfd_boolean
3063 elf_cris_discard_excess_dso_dynamics (h, inf)
3064      struct elf_cris_link_hash_entry *h;
3065      PTR inf;
3066 {
3067   struct elf_cris_pcrel_relocs_copied *s;
3068   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3069
3070   if (h->root.root.type == bfd_link_hash_warning)
3071     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
3072
3073   /* If a symbol has been forced local or we have found a regular
3074      definition for the symbolic link case, then we won't be needing
3075      any relocs.  */
3076   if (h->root.def_regular
3077       && (h->root.forced_local
3078           || info->symbolic))
3079     {
3080       for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3081         s->section->size -= s->count * sizeof (Elf32_External_Rela);
3082     }
3083
3084   return TRUE;
3085 }
3086
3087 /* This function is called via elf_cris_link_hash_traverse if we are *not*
3088    creating a shared object.  We discard space for relocs for symbols put
3089    in the .got, but which we found we do not have to resolve at run-time.  */
3090
3091 static bfd_boolean
3092 elf_cris_discard_excess_program_dynamics (h, inf)
3093      struct elf_cris_link_hash_entry *h;
3094      PTR inf;
3095 {
3096   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3097
3098   if (h->root.root.type == bfd_link_hash_warning)
3099     h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link;
3100
3101   /* If we're not creating a shared library and have a symbol which is
3102      referred to by .got references, but the symbol is defined locally,
3103      (or rather, not defined by a DSO) then lose the reloc for the .got
3104      (don't allocate room for it).  Likewise for relocs for something
3105      for which we create a PLT.  */
3106   if (!h->root.def_dynamic
3107       || h->root.plt.refcount > 0)
3108     {
3109       if (h->root.got.refcount > 0
3110           /* The size of this section is only valid and in sync with the
3111              various reference counts if we do dynamic; don't decrement it
3112              otherwise.  */
3113           && elf_hash_table (info)->dynamic_sections_created)
3114         {
3115           bfd *dynobj = elf_hash_table (info)->dynobj;
3116           asection *srelgot;
3117
3118           BFD_ASSERT (dynobj != NULL);
3119
3120           srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3121
3122           BFD_ASSERT (srelgot != NULL);
3123
3124           srelgot->size -= sizeof (Elf32_External_Rela);
3125         }
3126
3127       /* If the locally-defined symbol isn't used by a DSO, then we don't
3128          have to export it as a dynamic symbol.  This was already done for
3129          functions; doing this for all symbols would presumably not
3130          introduce new problems.  Of course we don't do this if we're
3131          exporting all dynamic symbols.  */
3132       if (! info->export_dynamic
3133           && h->root.dynindx != -1
3134           && !h->root.def_dynamic
3135           && !h->root.ref_dynamic)
3136         {
3137           h->root.dynindx = -1;
3138           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3139                                   h->root.dynstr_index);
3140         }
3141     }
3142
3143   return TRUE;
3144 }
3145
3146 /* Reject a file depending on presence and expectation of prefixed
3147    underscores on symbols.  */
3148
3149 static bfd_boolean
3150 cris_elf_object_p (abfd)
3151      bfd *abfd;
3152 {
3153   if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
3154     return FALSE;
3155
3156   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
3157     return (bfd_get_symbol_leading_char (abfd) == '_');
3158   else
3159     return (bfd_get_symbol_leading_char (abfd) == 0);
3160 }
3161
3162 /* Mark presence or absence of leading underscore.  Set machine type
3163    flags from mach type.  */
3164
3165 static void
3166 cris_elf_final_write_processing (abfd, linker)
3167      bfd *abfd;
3168      bfd_boolean linker ATTRIBUTE_UNUSED;
3169 {
3170   unsigned long e_flags = elf_elfheader (abfd)->e_flags;
3171
3172   e_flags &= ~EF_CRIS_UNDERSCORE;
3173   if (bfd_get_symbol_leading_char (abfd) == '_')
3174     e_flags |= EF_CRIS_UNDERSCORE;
3175
3176   switch (bfd_get_mach (abfd))
3177     {
3178     case bfd_mach_cris_v0_v10:
3179       e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
3180       break;
3181
3182     case bfd_mach_cris_v10_v32:
3183       e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
3184       break;
3185
3186     case bfd_mach_cris_v32:
3187       e_flags |= EF_CRIS_VARIANT_V32;
3188       break;
3189
3190     default:
3191       _bfd_abort (__FILE__, __LINE__,
3192                   _("Unexpected machine number"));
3193     }
3194
3195   elf_elfheader (abfd)->e_flags = e_flags;
3196 }
3197
3198 /* Set the mach type from e_flags value.  */
3199
3200 static bfd_boolean
3201 cris_elf_set_mach_from_flags (abfd, flags)
3202      bfd *abfd;
3203      unsigned long flags;
3204 {
3205   switch (flags & EF_CRIS_VARIANT_MASK)
3206     {
3207     case EF_CRIS_VARIANT_ANY_V0_V10:
3208       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
3209       break;
3210
3211     case EF_CRIS_VARIANT_V32:
3212       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
3213       break;
3214
3215     case EF_CRIS_VARIANT_COMMON_V10_V32:
3216       bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
3217       break;
3218
3219     default:
3220       /* Since we don't recognize them, we obviously can't support them
3221          with this code; we'd have to require that all future handling
3222          would be optional.  */
3223       bfd_set_error (bfd_error_wrong_format);
3224       return FALSE;
3225     }
3226
3227   return TRUE;
3228 }
3229
3230 /* Display the flags field.  */
3231
3232 static bfd_boolean
3233 cris_elf_print_private_bfd_data (abfd, ptr)
3234      bfd *abfd;
3235      PTR ptr;
3236 {
3237   FILE *file = (FILE *) ptr;
3238
3239   BFD_ASSERT (abfd != NULL && ptr != NULL);
3240
3241   _bfd_elf_print_private_bfd_data (abfd, ptr);
3242
3243   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
3244
3245   if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
3246     fprintf (file, _(" [symbols have a _ prefix]"));
3247   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3248       == EF_CRIS_VARIANT_COMMON_V10_V32)
3249     fprintf (file, _(" [v10 and v32]"));
3250   if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3251       == EF_CRIS_VARIANT_V32)
3252     fprintf (file, _(" [v32]"));
3253
3254   fputc ('\n', file);
3255   return TRUE;
3256 }
3257
3258 /* Don't mix files with and without a leading underscore.  */
3259
3260 static bfd_boolean
3261 cris_elf_merge_private_bfd_data (ibfd, obfd)
3262      bfd *ibfd;
3263      bfd *obfd;
3264 {
3265   int imach, omach;
3266
3267   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3268     return FALSE;
3269
3270   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3271       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3272     return TRUE;
3273
3274   imach = bfd_get_mach (ibfd);
3275
3276   if (! elf_flags_init (obfd))
3277     {
3278       /* This happens when ld starts out with a 'blank' output file.  */
3279       elf_flags_init (obfd) = TRUE;
3280
3281       /* We ignore the linker-set mach, and instead set it according to
3282          the first input file.  This would also happen if we could
3283          somehow filter out the OUTPUT_ARCH () setting from elf.sc.
3284          This allows us to keep the same linker config across
3285          cris(v0..v10) and crisv32.  The drawback is that we can't force
3286          the output type, which might be a sane thing to do for a
3287          v10+v32 compatibility object.  */
3288       if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3289         return FALSE;
3290     }
3291
3292   if (bfd_get_symbol_leading_char (ibfd)
3293       != bfd_get_symbol_leading_char (obfd))
3294     {
3295       (*_bfd_error_handler)
3296         (bfd_get_symbol_leading_char (ibfd) == '_'
3297          ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols")
3298          : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
3299          ibfd);
3300       bfd_set_error (bfd_error_bad_value);
3301       return FALSE;
3302     }
3303
3304   omach = bfd_get_mach (obfd);
3305
3306   if (imach != omach)
3307     {
3308       /* We can get an incompatible combination only if either is
3309          bfd_mach_cris_v32, and the other one isn't compatible.  */
3310       if ((imach == bfd_mach_cris_v32
3311            && omach != bfd_mach_cris_v10_v32)
3312           || (omach == bfd_mach_cris_v32
3313               && imach != bfd_mach_cris_v10_v32))
3314         {
3315           (*_bfd_error_handler)
3316             ((imach == bfd_mach_cris_v32)
3317              ? _("%B contains CRIS v32 code, incompatible"
3318                  " with previous objects")
3319              : _("%B contains non-CRIS-v32 code, incompatible"
3320                  " with previous objects"),
3321              ibfd);
3322           bfd_set_error (bfd_error_bad_value);
3323           return FALSE;
3324         }
3325
3326       /* We don't have to check the case where the input is compatible
3327          with v10 and v32, because the output is already known to be set
3328          to the other (compatible) mach.  */
3329       if (omach == bfd_mach_cris_v10_v32
3330           && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3331         return FALSE;
3332     }
3333
3334   return TRUE;
3335 }
3336
3337 /* Do side-effects of e_flags copying to obfd.  */
3338
3339 static bfd_boolean
3340 cris_elf_copy_private_bfd_data (ibfd, obfd)
3341      bfd *ibfd;
3342      bfd *obfd;
3343 {
3344   /* Call the base function.  */
3345   if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
3346     return FALSE;
3347
3348   /* If output is big-endian for some obscure reason, stop here.  */
3349   if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
3350     return FALSE;
3351
3352   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3353       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3354     return TRUE;
3355
3356   /* Do what we really came here for.  */
3357   return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
3358 }
3359
3360 static enum elf_reloc_type_class
3361 elf_cris_reloc_type_class (rela)
3362      const Elf_Internal_Rela *rela;
3363 {
3364   switch ((int) ELF32_R_TYPE (rela->r_info))
3365     {
3366     case R_CRIS_RELATIVE:
3367       return reloc_class_relative;
3368     case R_CRIS_JUMP_SLOT:
3369       return reloc_class_plt;
3370     case R_CRIS_COPY:
3371       return reloc_class_copy;
3372     default:
3373       return reloc_class_normal;
3374     }
3375 }
3376 \f
3377 #define ELF_ARCH                bfd_arch_cris
3378 #define ELF_MACHINE_CODE        EM_CRIS
3379 #define ELF_MAXPAGESIZE         0x2000
3380
3381 #define TARGET_LITTLE_SYM       bfd_elf32_cris_vec
3382 #define TARGET_LITTLE_NAME      "elf32-cris"
3383 #define elf_symbol_leading_char 0
3384
3385 #define elf_info_to_howto_rel                   NULL
3386 #define elf_info_to_howto                       cris_info_to_howto_rela
3387 #define elf_backend_relocate_section            cris_elf_relocate_section
3388 #define elf_backend_gc_mark_hook                cris_elf_gc_mark_hook
3389 #define elf_backend_gc_sweep_hook               cris_elf_gc_sweep_hook
3390 #define elf_backend_check_relocs                cris_elf_check_relocs
3391 #define elf_backend_grok_prstatus               cris_elf_grok_prstatus
3392 #define elf_backend_grok_psinfo                 cris_elf_grok_psinfo
3393
3394 #define elf_backend_can_gc_sections             1
3395 #define elf_backend_can_refcount                1
3396
3397 #define elf_backend_object_p                    cris_elf_object_p
3398 #define elf_backend_final_write_processing \
3399         cris_elf_final_write_processing
3400 #define bfd_elf32_bfd_print_private_bfd_data \
3401         cris_elf_print_private_bfd_data
3402 #define bfd_elf32_bfd_merge_private_bfd_data \
3403         cris_elf_merge_private_bfd_data
3404 #define bfd_elf32_bfd_copy_private_bfd_data \
3405         cris_elf_copy_private_bfd_data
3406
3407 #define bfd_elf32_bfd_reloc_type_lookup         cris_reloc_type_lookup
3408
3409 #define bfd_elf32_bfd_link_hash_table_create \
3410         elf_cris_link_hash_table_create
3411 #define elf_backend_adjust_dynamic_symbol \
3412         elf_cris_adjust_dynamic_symbol
3413 #define elf_backend_size_dynamic_sections \
3414         elf_cris_size_dynamic_sections
3415 #define elf_backend_init_index_section          _bfd_elf_init_1_index_section
3416 #define elf_backend_finish_dynamic_symbol \
3417         elf_cris_finish_dynamic_symbol
3418 #define elf_backend_finish_dynamic_sections \
3419         elf_cris_finish_dynamic_sections
3420 #define elf_backend_create_dynamic_sections \
3421         _bfd_elf_create_dynamic_sections
3422 #define bfd_elf32_bfd_final_link \
3423         bfd_elf_gc_common_final_link
3424 #define elf_backend_hide_symbol                 elf_cris_hide_symbol
3425 #define elf_backend_reloc_type_class            elf_cris_reloc_type_class
3426
3427 #define elf_backend_want_got_plt        1
3428 #define elf_backend_plt_readonly        1
3429 #define elf_backend_want_plt_sym        0
3430 #define elf_backend_got_header_size     12
3431
3432 /* Later, we my want to optimize RELA entries into REL entries for dynamic
3433    linking and libraries (if it's a win of any significance).  Until then,
3434    take the easy route.  */
3435 #define elf_backend_may_use_rel_p 0
3436 #define elf_backend_may_use_rela_p 1
3437 #define elf_backend_rela_normal         1
3438
3439 #include "elf32-target.h"
3440
3441 #undef TARGET_LITTLE_SYM
3442 #undef TARGET_LITTLE_NAME
3443 #undef elf_symbol_leading_char
3444
3445 #define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec
3446 #define TARGET_LITTLE_NAME "elf32-us-cris"
3447 #define elf_symbol_leading_char '_'
3448 #undef elf32_bed
3449 #define elf32_bed elf32_us_cris_bed
3450
3451 #include "elf32-target.h"