OSDN Git Service

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